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>
40 #include "blockcheck.h"
42 #include "extent_map.h"
45 #include "localalloc.h"
53 #include "buffer_head_io.h"
57 * Operations for a specific extent tree type.
59 * To implement an on-disk btree (extent tree) type in ocfs2, add
60 * an ocfs2_extent_tree_operations structure and the matching
61 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
62 * for the allocation portion of the extent tree.
64 struct ocfs2_extent_tree_operations
{
66 * last_eb_blk is the block number of the right most leaf extent
67 * block. Most on-disk structures containing an extent tree store
68 * this value for fast access. The ->eo_set_last_eb_blk() and
69 * ->eo_get_last_eb_blk() operations access this value. They are
72 void (*eo_set_last_eb_blk
)(struct ocfs2_extent_tree
*et
,
74 u64 (*eo_get_last_eb_blk
)(struct ocfs2_extent_tree
*et
);
77 * The on-disk structure usually keeps track of how many total
78 * clusters are stored in this extent tree. This function updates
79 * that value. new_clusters is the delta, and must be
80 * added to the total. Required.
82 void (*eo_update_clusters
)(struct ocfs2_extent_tree
*et
,
86 * If this extent tree is supported by an extent map, truncate the
89 void (*eo_extent_map_truncate
)(struct ocfs2_extent_tree
*et
,
93 * If ->eo_insert_check() exists, it is called before rec is
94 * inserted into the extent tree. It is optional.
96 int (*eo_insert_check
)(struct ocfs2_extent_tree
*et
,
97 struct ocfs2_extent_rec
*rec
);
98 int (*eo_sanity_check
)(struct ocfs2_extent_tree
*et
);
101 * --------------------------------------------------------------
102 * The remaining are internal to ocfs2_extent_tree and don't have
107 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
110 void (*eo_fill_root_el
)(struct ocfs2_extent_tree
*et
);
113 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
114 * it exists. If it does not, et->et_max_leaf_clusters is set
115 * to 0 (unlimited). Optional.
117 void (*eo_fill_max_leaf_clusters
)(struct ocfs2_extent_tree
*et
);
122 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
125 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
);
126 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
128 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree
*et
,
130 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree
*et
,
132 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree
*et
,
133 struct ocfs2_extent_rec
*rec
);
134 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree
*et
);
135 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
);
136 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops
= {
137 .eo_set_last_eb_blk
= ocfs2_dinode_set_last_eb_blk
,
138 .eo_get_last_eb_blk
= ocfs2_dinode_get_last_eb_blk
,
139 .eo_update_clusters
= ocfs2_dinode_update_clusters
,
140 .eo_extent_map_truncate
= ocfs2_dinode_extent_map_truncate
,
141 .eo_insert_check
= ocfs2_dinode_insert_check
,
142 .eo_sanity_check
= ocfs2_dinode_sanity_check
,
143 .eo_fill_root_el
= ocfs2_dinode_fill_root_el
,
146 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
149 struct ocfs2_dinode
*di
= et
->et_object
;
151 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
152 di
->i_last_eb_blk
= cpu_to_le64(blkno
);
155 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
157 struct ocfs2_dinode
*di
= et
->et_object
;
159 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
160 return le64_to_cpu(di
->i_last_eb_blk
);
163 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree
*et
,
166 struct ocfs2_inode_info
*oi
= cache_info_to_inode(et
->et_ci
);
167 struct ocfs2_dinode
*di
= et
->et_object
;
169 le32_add_cpu(&di
->i_clusters
, clusters
);
170 spin_lock(&oi
->ip_lock
);
171 oi
->ip_clusters
= le32_to_cpu(di
->i_clusters
);
172 spin_unlock(&oi
->ip_lock
);
175 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree
*et
,
178 struct inode
*inode
= &cache_info_to_inode(et
->et_ci
)->vfs_inode
;
180 ocfs2_extent_map_trunc(inode
, clusters
);
183 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree
*et
,
184 struct ocfs2_extent_rec
*rec
)
186 struct ocfs2_inode_info
*oi
= cache_info_to_inode(et
->et_ci
);
187 struct ocfs2_super
*osb
= OCFS2_SB(oi
->vfs_inode
.i_sb
);
189 BUG_ON(oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
190 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
191 (oi
->ip_clusters
!= le32_to_cpu(rec
->e_cpos
)),
192 "Device %s, asking for sparse allocation: inode %llu, "
193 "cpos %u, clusters %u\n",
195 (unsigned long long)oi
->ip_blkno
,
196 rec
->e_cpos
, oi
->ip_clusters
);
201 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree
*et
)
203 struct ocfs2_dinode
*di
= et
->et_object
;
205 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
206 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
211 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
)
213 struct ocfs2_dinode
*di
= et
->et_object
;
215 et
->et_root_el
= &di
->id2
.i_list
;
219 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree
*et
)
221 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
223 et
->et_root_el
= &vb
->vb_xv
->xr_list
;
226 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
229 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
231 vb
->vb_xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
234 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
236 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
238 return le64_to_cpu(vb
->vb_xv
->xr_last_eb_blk
);
241 static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree
*et
,
244 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
246 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, clusters
);
249 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops
= {
250 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
251 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
252 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
253 .eo_fill_root_el
= ocfs2_xattr_value_fill_root_el
,
256 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree
*et
)
258 struct ocfs2_xattr_block
*xb
= et
->et_object
;
260 et
->et_root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
263 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree
*et
)
265 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
266 et
->et_max_leaf_clusters
=
267 ocfs2_clusters_for_bytes(sb
, OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
270 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
273 struct ocfs2_xattr_block
*xb
= et
->et_object
;
274 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
276 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
279 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
281 struct ocfs2_xattr_block
*xb
= et
->et_object
;
282 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
284 return le64_to_cpu(xt
->xt_last_eb_blk
);
287 static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree
*et
,
290 struct ocfs2_xattr_block
*xb
= et
->et_object
;
292 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
295 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
296 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
297 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
298 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
299 .eo_fill_root_el
= ocfs2_xattr_tree_fill_root_el
,
300 .eo_fill_max_leaf_clusters
= ocfs2_xattr_tree_fill_max_leaf_clusters
,
303 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
306 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
308 dx_root
->dr_last_eb_blk
= cpu_to_le64(blkno
);
311 static u64
ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
313 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
315 return le64_to_cpu(dx_root
->dr_last_eb_blk
);
318 static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree
*et
,
321 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
323 le32_add_cpu(&dx_root
->dr_clusters
, clusters
);
326 static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree
*et
)
328 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
330 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root
));
335 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree
*et
)
337 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
339 et
->et_root_el
= &dx_root
->dr_list
;
342 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops
= {
343 .eo_set_last_eb_blk
= ocfs2_dx_root_set_last_eb_blk
,
344 .eo_get_last_eb_blk
= ocfs2_dx_root_get_last_eb_blk
,
345 .eo_update_clusters
= ocfs2_dx_root_update_clusters
,
346 .eo_sanity_check
= ocfs2_dx_root_sanity_check
,
347 .eo_fill_root_el
= ocfs2_dx_root_fill_root_el
,
350 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree
*et
,
352 struct buffer_head
*bh
,
353 ocfs2_journal_access_func access
,
355 struct ocfs2_extent_tree_operations
*ops
)
359 et
->et_ci
= INODE_CACHE(inode
);
360 et
->et_root_journal_access
= access
;
362 obj
= (void *)bh
->b_data
;
365 et
->et_ops
->eo_fill_root_el(et
);
366 if (!et
->et_ops
->eo_fill_max_leaf_clusters
)
367 et
->et_max_leaf_clusters
= 0;
369 et
->et_ops
->eo_fill_max_leaf_clusters(et
);
372 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree
*et
,
374 struct buffer_head
*bh
)
376 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_di
,
377 NULL
, &ocfs2_dinode_et_ops
);
380 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree
*et
,
382 struct buffer_head
*bh
)
384 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_xb
,
385 NULL
, &ocfs2_xattr_tree_et_ops
);
388 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree
*et
,
390 struct ocfs2_xattr_value_buf
*vb
)
392 __ocfs2_init_extent_tree(et
, inode
, vb
->vb_bh
, vb
->vb_access
, vb
,
393 &ocfs2_xattr_value_et_ops
);
396 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree
*et
,
398 struct buffer_head
*bh
)
400 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_dr
,
401 NULL
, &ocfs2_dx_root_et_ops
);
404 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
407 et
->et_ops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
410 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
412 return et
->et_ops
->eo_get_last_eb_blk(et
);
415 static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree
*et
,
418 et
->et_ops
->eo_update_clusters(et
, clusters
);
421 static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree
*et
,
424 if (et
->et_ops
->eo_extent_map_truncate
)
425 et
->et_ops
->eo_extent_map_truncate(et
, clusters
);
428 static inline int ocfs2_et_root_journal_access(handle_t
*handle
,
429 struct ocfs2_extent_tree
*et
,
432 return et
->et_root_journal_access(handle
, et
->et_ci
, et
->et_root_bh
,
436 static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree
*et
,
437 struct ocfs2_extent_rec
*rec
)
441 if (et
->et_ops
->eo_insert_check
)
442 ret
= et
->et_ops
->eo_insert_check(et
, rec
);
446 static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree
*et
)
450 if (et
->et_ops
->eo_sanity_check
)
451 ret
= et
->et_ops
->eo_sanity_check(et
);
455 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
456 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
457 struct ocfs2_extent_block
*eb
);
460 * Structures which describe a path through a btree, and functions to
463 * The idea here is to be as generic as possible with the tree
466 struct ocfs2_path_item
{
467 struct buffer_head
*bh
;
468 struct ocfs2_extent_list
*el
;
471 #define OCFS2_MAX_PATH_DEPTH 5
475 ocfs2_journal_access_func p_root_access
;
476 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
479 #define path_root_bh(_path) ((_path)->p_node[0].bh)
480 #define path_root_el(_path) ((_path)->p_node[0].el)
481 #define path_root_access(_path)((_path)->p_root_access)
482 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
483 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
484 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
486 static int ocfs2_find_path(struct ocfs2_caching_info
*ci
,
487 struct ocfs2_path
*path
, u32 cpos
);
488 static void ocfs2_adjust_rightmost_records(handle_t
*handle
,
489 struct ocfs2_extent_tree
*et
,
490 struct ocfs2_path
*path
,
491 struct ocfs2_extent_rec
*insert_rec
);
493 * Reset the actual path elements so that we can re-use the structure
494 * to build another path. Generally, this involves freeing the buffer
497 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
499 int i
, start
= 0, depth
= 0;
500 struct ocfs2_path_item
*node
;
505 for(i
= start
; i
< path_num_items(path
); i
++) {
506 node
= &path
->p_node
[i
];
514 * Tree depth may change during truncate, or insert. If we're
515 * keeping the root extent list, then make sure that our path
516 * structure reflects the proper depth.
519 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
521 path_root_access(path
) = NULL
;
523 path
->p_tree_depth
= depth
;
526 static void ocfs2_free_path(struct ocfs2_path
*path
)
529 ocfs2_reinit_path(path
, 0);
535 * All the elements of src into dest. After this call, src could be freed
536 * without affecting dest.
538 * Both paths should have the same root. Any non-root elements of dest
541 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
545 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
546 BUG_ON(path_root_el(dest
) != path_root_el(src
));
547 BUG_ON(path_root_access(dest
) != path_root_access(src
));
549 ocfs2_reinit_path(dest
, 1);
551 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
552 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
553 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
555 if (dest
->p_node
[i
].bh
)
556 get_bh(dest
->p_node
[i
].bh
);
561 * Make the *dest path the same as src and re-initialize src path to
564 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
568 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
569 BUG_ON(path_root_access(dest
) != path_root_access(src
));
571 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
572 brelse(dest
->p_node
[i
].bh
);
574 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
575 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
577 src
->p_node
[i
].bh
= NULL
;
578 src
->p_node
[i
].el
= NULL
;
583 * Insert an extent block at given index.
585 * This will not take an additional reference on eb_bh.
587 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
588 struct buffer_head
*eb_bh
)
590 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
593 * Right now, no root bh is an extent block, so this helps
594 * catch code errors with dinode trees. The assertion can be
595 * safely removed if we ever need to insert extent block
596 * structures at the root.
600 path
->p_node
[index
].bh
= eb_bh
;
601 path
->p_node
[index
].el
= &eb
->h_list
;
604 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
605 struct ocfs2_extent_list
*root_el
,
606 ocfs2_journal_access_func access
)
608 struct ocfs2_path
*path
;
610 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
612 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
614 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
616 path_root_bh(path
) = root_bh
;
617 path_root_el(path
) = root_el
;
618 path_root_access(path
) = access
;
624 static struct ocfs2_path
*ocfs2_new_path_from_path(struct ocfs2_path
*path
)
626 return ocfs2_new_path(path_root_bh(path
), path_root_el(path
),
627 path_root_access(path
));
630 static struct ocfs2_path
*ocfs2_new_path_from_et(struct ocfs2_extent_tree
*et
)
632 return ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
,
633 et
->et_root_journal_access
);
637 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
638 * otherwise it's the root_access function.
640 * I don't like the way this function's name looks next to
641 * ocfs2_journal_access_path(), but I don't have a better one.
643 static int ocfs2_path_bh_journal_access(handle_t
*handle
,
644 struct ocfs2_caching_info
*ci
,
645 struct ocfs2_path
*path
,
648 ocfs2_journal_access_func access
= path_root_access(path
);
651 access
= ocfs2_journal_access
;
654 access
= ocfs2_journal_access_eb
;
656 return access(handle
, ci
, path
->p_node
[idx
].bh
,
657 OCFS2_JOURNAL_ACCESS_WRITE
);
661 * Convenience function to journal all components in a path.
663 static int ocfs2_journal_access_path(struct ocfs2_caching_info
*ci
,
665 struct ocfs2_path
*path
)
672 for(i
= 0; i
< path_num_items(path
); i
++) {
673 ret
= ocfs2_path_bh_journal_access(handle
, ci
, path
, i
);
685 * Return the index of the extent record which contains cluster #v_cluster.
686 * -1 is returned if it was not found.
688 * Should work fine on interior and exterior nodes.
690 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
694 struct ocfs2_extent_rec
*rec
;
695 u32 rec_end
, rec_start
, clusters
;
697 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
698 rec
= &el
->l_recs
[i
];
700 rec_start
= le32_to_cpu(rec
->e_cpos
);
701 clusters
= ocfs2_rec_clusters(el
, rec
);
703 rec_end
= rec_start
+ clusters
;
705 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
714 enum ocfs2_contig_type
{
723 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
724 * ocfs2_extent_contig only work properly against leaf nodes!
726 static int ocfs2_block_extent_contig(struct super_block
*sb
,
727 struct ocfs2_extent_rec
*ext
,
730 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
732 blk_end
+= ocfs2_clusters_to_blocks(sb
,
733 le16_to_cpu(ext
->e_leaf_clusters
));
735 return blkno
== blk_end
;
738 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
739 struct ocfs2_extent_rec
*right
)
743 left_range
= le32_to_cpu(left
->e_cpos
) +
744 le16_to_cpu(left
->e_leaf_clusters
);
746 return (left_range
== le32_to_cpu(right
->e_cpos
));
749 static enum ocfs2_contig_type
750 ocfs2_extent_contig(struct super_block
*sb
,
751 struct ocfs2_extent_rec
*ext
,
752 struct ocfs2_extent_rec
*insert_rec
)
754 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
757 * Refuse to coalesce extent records with different flag
758 * fields - we don't want to mix unwritten extents with user
761 if (ext
->e_flags
!= insert_rec
->e_flags
)
764 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
765 ocfs2_block_extent_contig(sb
, ext
, blkno
))
768 blkno
= le64_to_cpu(ext
->e_blkno
);
769 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
770 ocfs2_block_extent_contig(sb
, insert_rec
, blkno
))
777 * NOTE: We can have pretty much any combination of contiguousness and
780 * The usefulness of APPEND_TAIL is more in that it lets us know that
781 * we'll have to update the path to that leaf.
783 enum ocfs2_append_type
{
788 enum ocfs2_split_type
{
794 struct ocfs2_insert_type
{
795 enum ocfs2_split_type ins_split
;
796 enum ocfs2_append_type ins_appending
;
797 enum ocfs2_contig_type ins_contig
;
798 int ins_contig_index
;
802 struct ocfs2_merge_ctxt
{
803 enum ocfs2_contig_type c_contig_type
;
804 int c_has_empty_extent
;
805 int c_split_covers_rec
;
808 static int ocfs2_validate_extent_block(struct super_block
*sb
,
809 struct buffer_head
*bh
)
812 struct ocfs2_extent_block
*eb
=
813 (struct ocfs2_extent_block
*)bh
->b_data
;
815 mlog(0, "Validating extent block %llu\n",
816 (unsigned long long)bh
->b_blocknr
);
818 BUG_ON(!buffer_uptodate(bh
));
821 * If the ecc fails, we return the error but otherwise
822 * leave the filesystem running. We know any error is
823 * local to this block.
825 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &eb
->h_check
);
827 mlog(ML_ERROR
, "Checksum failed for extent block %llu\n",
828 (unsigned long long)bh
->b_blocknr
);
833 * Errors after here are fatal.
836 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
838 "Extent block #%llu has bad signature %.*s",
839 (unsigned long long)bh
->b_blocknr
, 7,
844 if (le64_to_cpu(eb
->h_blkno
) != bh
->b_blocknr
) {
846 "Extent block #%llu has an invalid h_blkno "
848 (unsigned long long)bh
->b_blocknr
,
849 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
853 if (le32_to_cpu(eb
->h_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
855 "Extent block #%llu has an invalid "
856 "h_fs_generation of #%u",
857 (unsigned long long)bh
->b_blocknr
,
858 le32_to_cpu(eb
->h_fs_generation
));
865 int ocfs2_read_extent_block(struct ocfs2_caching_info
*ci
, u64 eb_blkno
,
866 struct buffer_head
**bh
)
869 struct buffer_head
*tmp
= *bh
;
871 rc
= ocfs2_read_block(ci
, eb_blkno
, &tmp
,
872 ocfs2_validate_extent_block
);
874 /* If ocfs2_read_block() got us a new bh, pass it up. */
883 * How many free extents have we got before we need more meta data?
885 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
886 struct ocfs2_extent_tree
*et
)
889 struct ocfs2_extent_list
*el
= NULL
;
890 struct ocfs2_extent_block
*eb
;
891 struct buffer_head
*eb_bh
= NULL
;
897 last_eb_blk
= ocfs2_et_get_last_eb_blk(et
);
900 retval
= ocfs2_read_extent_block(et
->et_ci
, last_eb_blk
,
906 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
910 BUG_ON(el
->l_tree_depth
!= 0);
912 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
920 /* expects array to already be allocated
922 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
925 static int ocfs2_create_new_meta_bhs(handle_t
*handle
,
926 struct ocfs2_extent_tree
*et
,
928 struct ocfs2_alloc_context
*meta_ac
,
929 struct buffer_head
*bhs
[])
931 int count
, status
, i
;
932 u16 suballoc_bit_start
;
935 struct ocfs2_super
*osb
=
936 OCFS2_SB(ocfs2_metadata_cache_get_super(et
->et_ci
));
937 struct ocfs2_extent_block
*eb
;
942 while (count
< wanted
) {
943 status
= ocfs2_claim_metadata(osb
,
955 for(i
= count
; i
< (num_got
+ count
); i
++) {
956 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
957 if (bhs
[i
] == NULL
) {
962 ocfs2_set_new_buffer_uptodate(et
->et_ci
, bhs
[i
]);
964 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
,
966 OCFS2_JOURNAL_ACCESS_CREATE
);
972 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
973 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
974 /* Ok, setup the minimal stuff here. */
975 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
976 eb
->h_blkno
= cpu_to_le64(first_blkno
);
977 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
978 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
979 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
981 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
983 suballoc_bit_start
++;
986 /* We'll also be dirtied by the caller, so
987 * this isn't absolutely necessary. */
988 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
1001 for(i
= 0; i
< wanted
; i
++) {
1011 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1013 * Returns the sum of the rightmost extent rec logical offset and
1016 * ocfs2_add_branch() uses this to determine what logical cluster
1017 * value should be populated into the leftmost new branch records.
1019 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1020 * value for the new topmost tree record.
1022 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
1026 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1028 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
1029 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
1033 * Change range of the branches in the right most path according to the leaf
1034 * extent block's rightmost record.
1036 static int ocfs2_adjust_rightmost_branch(handle_t
*handle
,
1037 struct ocfs2_extent_tree
*et
)
1040 struct ocfs2_path
*path
= NULL
;
1041 struct ocfs2_extent_list
*el
;
1042 struct ocfs2_extent_rec
*rec
;
1044 path
= ocfs2_new_path_from_et(et
);
1050 status
= ocfs2_find_path(et
->et_ci
, path
, UINT_MAX
);
1056 status
= ocfs2_extend_trans(handle
, path_num_items(path
) +
1057 handle
->h_buffer_credits
);
1063 status
= ocfs2_journal_access_path(et
->et_ci
, handle
, path
);
1069 el
= path_leaf_el(path
);
1070 rec
= &el
->l_recs
[le32_to_cpu(el
->l_next_free_rec
) - 1];
1072 ocfs2_adjust_rightmost_records(handle
, et
, path
, rec
);
1075 ocfs2_free_path(path
);
1080 * Add an entire tree branch to our inode. eb_bh is the extent block
1081 * to start at, if we don't want to start the branch at the root
1084 * last_eb_bh is required as we have to update it's next_leaf pointer
1085 * for the new last extent block.
1087 * the new branch will be 'empty' in the sense that every block will
1088 * contain a single record with cluster count == 0.
1090 static int ocfs2_add_branch(handle_t
*handle
,
1091 struct ocfs2_extent_tree
*et
,
1092 struct buffer_head
*eb_bh
,
1093 struct buffer_head
**last_eb_bh
,
1094 struct ocfs2_alloc_context
*meta_ac
)
1096 int status
, new_blocks
, i
;
1097 u64 next_blkno
, new_last_eb_blk
;
1098 struct buffer_head
*bh
;
1099 struct buffer_head
**new_eb_bhs
= NULL
;
1100 struct ocfs2_extent_block
*eb
;
1101 struct ocfs2_extent_list
*eb_el
;
1102 struct ocfs2_extent_list
*el
;
1103 u32 new_cpos
, root_end
;
1107 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
1110 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
1113 el
= et
->et_root_el
;
1115 /* we never add a branch to a leaf. */
1116 BUG_ON(!el
->l_tree_depth
);
1118 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
1120 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
1121 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
1122 root_end
= ocfs2_sum_rightmost_rec(et
->et_root_el
);
1125 * If there is a gap before the root end and the real end
1126 * of the righmost leaf block, we need to remove the gap
1127 * between new_cpos and root_end first so that the tree
1128 * is consistent after we add a new branch(it will start
1131 if (root_end
> new_cpos
) {
1132 mlog(0, "adjust the cluster end from %u to %u\n",
1133 root_end
, new_cpos
);
1134 status
= ocfs2_adjust_rightmost_branch(handle
, et
);
1141 /* allocate the number of new eb blocks we need */
1142 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
1150 status
= ocfs2_create_new_meta_bhs(handle
, et
, new_blocks
,
1151 meta_ac
, new_eb_bhs
);
1157 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1158 * linked with the rest of the tree.
1159 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1161 * when we leave the loop, new_last_eb_blk will point to the
1162 * newest leaf, and next_blkno will point to the topmost extent
1164 next_blkno
= new_last_eb_blk
= 0;
1165 for(i
= 0; i
< new_blocks
; i
++) {
1167 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1168 /* ocfs2_create_new_meta_bhs() should create it right! */
1169 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1170 eb_el
= &eb
->h_list
;
1172 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
, bh
,
1173 OCFS2_JOURNAL_ACCESS_CREATE
);
1179 eb
->h_next_leaf_blk
= 0;
1180 eb_el
->l_tree_depth
= cpu_to_le16(i
);
1181 eb_el
->l_next_free_rec
= cpu_to_le16(1);
1183 * This actually counts as an empty extent as
1186 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
1187 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
1189 * eb_el isn't always an interior node, but even leaf
1190 * nodes want a zero'd flags and reserved field so
1191 * this gets the whole 32 bits regardless of use.
1193 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
1194 if (!eb_el
->l_tree_depth
)
1195 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
1197 status
= ocfs2_journal_dirty(handle
, bh
);
1203 next_blkno
= le64_to_cpu(eb
->h_blkno
);
1206 /* This is a bit hairy. We want to update up to three blocks
1207 * here without leaving any of them in an inconsistent state
1208 * in case of error. We don't have to worry about
1209 * journal_dirty erroring as it won't unless we've aborted the
1210 * handle (in which case we would never be here) so reserving
1211 * the write with journal_access is all we need to do. */
1212 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
, *last_eb_bh
,
1213 OCFS2_JOURNAL_ACCESS_WRITE
);
1218 status
= ocfs2_et_root_journal_access(handle
, et
,
1219 OCFS2_JOURNAL_ACCESS_WRITE
);
1225 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
, eb_bh
,
1226 OCFS2_JOURNAL_ACCESS_WRITE
);
1233 /* Link the new branch into the rest of the tree (el will
1234 * either be on the root_bh, or the extent block passed in. */
1235 i
= le16_to_cpu(el
->l_next_free_rec
);
1236 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
1237 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1238 el
->l_recs
[i
].e_int_clusters
= 0;
1239 le16_add_cpu(&el
->l_next_free_rec
, 1);
1241 /* fe needs a new last extent block pointer, as does the
1242 * next_leaf on the previously last-extent-block. */
1243 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
1245 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
1246 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
1248 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
1251 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1255 status
= ocfs2_journal_dirty(handle
, eb_bh
);
1261 * Some callers want to track the rightmost leaf so pass it
1264 brelse(*last_eb_bh
);
1265 get_bh(new_eb_bhs
[0]);
1266 *last_eb_bh
= new_eb_bhs
[0];
1271 for (i
= 0; i
< new_blocks
; i
++)
1272 brelse(new_eb_bhs
[i
]);
1281 * adds another level to the allocation tree.
1282 * returns back the new extent block so you can add a branch to it
1285 static int ocfs2_shift_tree_depth(handle_t
*handle
,
1286 struct ocfs2_extent_tree
*et
,
1287 struct ocfs2_alloc_context
*meta_ac
,
1288 struct buffer_head
**ret_new_eb_bh
)
1292 struct buffer_head
*new_eb_bh
= NULL
;
1293 struct ocfs2_extent_block
*eb
;
1294 struct ocfs2_extent_list
*root_el
;
1295 struct ocfs2_extent_list
*eb_el
;
1299 status
= ocfs2_create_new_meta_bhs(handle
, et
, 1, meta_ac
,
1306 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
1307 /* ocfs2_create_new_meta_bhs() should create it right! */
1308 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1310 eb_el
= &eb
->h_list
;
1311 root_el
= et
->et_root_el
;
1313 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
, new_eb_bh
,
1314 OCFS2_JOURNAL_ACCESS_CREATE
);
1320 /* copy the root extent list data into the new extent block */
1321 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
1322 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
1323 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1324 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1326 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1332 status
= ocfs2_et_root_journal_access(handle
, et
,
1333 OCFS2_JOURNAL_ACCESS_WRITE
);
1339 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1341 /* update root_bh now */
1342 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1343 root_el
->l_recs
[0].e_cpos
= 0;
1344 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1345 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1346 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1347 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1348 root_el
->l_next_free_rec
= cpu_to_le16(1);
1350 /* If this is our 1st tree depth shift, then last_eb_blk
1351 * becomes the allocated extent block */
1352 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1353 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1355 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1361 *ret_new_eb_bh
= new_eb_bh
;
1372 * Should only be called when there is no space left in any of the
1373 * leaf nodes. What we want to do is find the lowest tree depth
1374 * non-leaf extent block with room for new records. There are three
1375 * valid results of this search:
1377 * 1) a lowest extent block is found, then we pass it back in
1378 * *lowest_eb_bh and return '0'
1380 * 2) the search fails to find anything, but the root_el has room. We
1381 * pass NULL back in *lowest_eb_bh, but still return '0'
1383 * 3) the search fails to find anything AND the root_el is full, in
1384 * which case we return > 0
1386 * return status < 0 indicates an error.
1388 static int ocfs2_find_branch_target(struct ocfs2_extent_tree
*et
,
1389 struct buffer_head
**target_bh
)
1393 struct ocfs2_extent_block
*eb
;
1394 struct ocfs2_extent_list
*el
;
1395 struct buffer_head
*bh
= NULL
;
1396 struct buffer_head
*lowest_bh
= NULL
;
1402 el
= et
->et_root_el
;
1404 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1405 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1406 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
1407 "Owner %llu has empty "
1408 "extent list (next_free_rec == 0)",
1409 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
));
1413 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1414 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1416 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
1417 "Owner %llu has extent "
1418 "list where extent # %d has no physical "
1420 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
), i
);
1428 status
= ocfs2_read_extent_block(et
->et_ci
, blkno
, &bh
);
1434 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1437 if (le16_to_cpu(el
->l_next_free_rec
) <
1438 le16_to_cpu(el
->l_count
)) {
1445 /* If we didn't find one and the fe doesn't have any room,
1446 * then return '1' */
1447 el
= et
->et_root_el
;
1448 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1451 *target_bh
= lowest_bh
;
1460 * Grow a b-tree so that it has more records.
1462 * We might shift the tree depth in which case existing paths should
1463 * be considered invalid.
1465 * Tree depth after the grow is returned via *final_depth.
1467 * *last_eb_bh will be updated by ocfs2_add_branch().
1469 static int ocfs2_grow_tree(handle_t
*handle
, struct ocfs2_extent_tree
*et
,
1470 int *final_depth
, struct buffer_head
**last_eb_bh
,
1471 struct ocfs2_alloc_context
*meta_ac
)
1474 struct ocfs2_extent_list
*el
= et
->et_root_el
;
1475 int depth
= le16_to_cpu(el
->l_tree_depth
);
1476 struct buffer_head
*bh
= NULL
;
1478 BUG_ON(meta_ac
== NULL
);
1480 shift
= ocfs2_find_branch_target(et
, &bh
);
1487 /* We traveled all the way to the bottom of the allocation tree
1488 * and didn't find room for any more extents - we need to add
1489 * another tree level */
1492 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1494 /* ocfs2_shift_tree_depth will return us a buffer with
1495 * the new extent block (so we can pass that to
1496 * ocfs2_add_branch). */
1497 ret
= ocfs2_shift_tree_depth(handle
, et
, meta_ac
, &bh
);
1505 * Special case: we have room now if we shifted from
1506 * tree_depth 0, so no more work needs to be done.
1508 * We won't be calling add_branch, so pass
1509 * back *last_eb_bh as the new leaf. At depth
1510 * zero, it should always be null so there's
1511 * no reason to brelse.
1513 BUG_ON(*last_eb_bh
);
1520 /* call ocfs2_add_branch to add the final part of the tree with
1522 mlog(0, "add branch. bh = %p\n", bh
);
1523 ret
= ocfs2_add_branch(handle
, et
, bh
, last_eb_bh
,
1532 *final_depth
= depth
;
1538 * This function will discard the rightmost extent record.
1540 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1542 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1543 int count
= le16_to_cpu(el
->l_count
);
1544 unsigned int num_bytes
;
1547 /* This will cause us to go off the end of our extent list. */
1548 BUG_ON(next_free
>= count
);
1550 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1552 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1555 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1556 struct ocfs2_extent_rec
*insert_rec
)
1558 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1559 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1560 struct ocfs2_extent_rec
*rec
;
1562 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1563 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1567 /* The tree code before us didn't allow enough room in the leaf. */
1568 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1571 * The easiest way to approach this is to just remove the
1572 * empty extent and temporarily decrement next_free.
1576 * If next_free was 1 (only an empty extent), this
1577 * loop won't execute, which is fine. We still want
1578 * the decrement above to happen.
1580 for(i
= 0; i
< (next_free
- 1); i
++)
1581 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1587 * Figure out what the new record index should be.
1589 for(i
= 0; i
< next_free
; i
++) {
1590 rec
= &el
->l_recs
[i
];
1592 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1597 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1598 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1600 BUG_ON(insert_index
< 0);
1601 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1602 BUG_ON(insert_index
> next_free
);
1605 * No need to memmove if we're just adding to the tail.
1607 if (insert_index
!= next_free
) {
1608 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1610 num_bytes
= next_free
- insert_index
;
1611 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1612 memmove(&el
->l_recs
[insert_index
+ 1],
1613 &el
->l_recs
[insert_index
],
1618 * Either we had an empty extent, and need to re-increment or
1619 * there was no empty extent on a non full rightmost leaf node,
1620 * in which case we still need to increment.
1623 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1625 * Make sure none of the math above just messed up our tree.
1627 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1629 el
->l_recs
[insert_index
] = *insert_rec
;
1633 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1635 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1637 BUG_ON(num_recs
== 0);
1639 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1641 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1642 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1643 memset(&el
->l_recs
[num_recs
], 0,
1644 sizeof(struct ocfs2_extent_rec
));
1645 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1650 * Create an empty extent record .
1652 * l_next_free_rec may be updated.
1654 * If an empty extent already exists do nothing.
1656 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1658 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1660 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1665 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1668 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1669 "Asked to create an empty extent in a full list:\n"
1670 "count = %u, tree depth = %u",
1671 le16_to_cpu(el
->l_count
),
1672 le16_to_cpu(el
->l_tree_depth
));
1674 ocfs2_shift_records_right(el
);
1677 le16_add_cpu(&el
->l_next_free_rec
, 1);
1678 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1682 * For a rotation which involves two leaf nodes, the "root node" is
1683 * the lowest level tree node which contains a path to both leafs. This
1684 * resulting set of information can be used to form a complete "subtree"
1686 * This function is passed two full paths from the dinode down to a
1687 * pair of adjacent leaves. It's task is to figure out which path
1688 * index contains the subtree root - this can be the root index itself
1689 * in a worst-case rotation.
1691 * The array index of the subtree root is passed back.
1693 static int ocfs2_find_subtree_root(struct ocfs2_extent_tree
*et
,
1694 struct ocfs2_path
*left
,
1695 struct ocfs2_path
*right
)
1700 * Check that the caller passed in two paths from the same tree.
1702 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1708 * The caller didn't pass two adjacent paths.
1710 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1711 "Owner %llu, left depth %u, right depth %u\n"
1712 "left leaf blk %llu, right leaf blk %llu\n",
1713 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
1714 left
->p_tree_depth
, right
->p_tree_depth
,
1715 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1716 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1717 } while (left
->p_node
[i
].bh
->b_blocknr
==
1718 right
->p_node
[i
].bh
->b_blocknr
);
1723 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1726 * Traverse a btree path in search of cpos, starting at root_el.
1728 * This code can be called with a cpos larger than the tree, in which
1729 * case it will return the rightmost path.
1731 static int __ocfs2_find_path(struct ocfs2_caching_info
*ci
,
1732 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1733 path_insert_t
*func
, void *data
)
1738 struct buffer_head
*bh
= NULL
;
1739 struct ocfs2_extent_block
*eb
;
1740 struct ocfs2_extent_list
*el
;
1741 struct ocfs2_extent_rec
*rec
;
1744 while (el
->l_tree_depth
) {
1745 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1746 ocfs2_error(ocfs2_metadata_cache_get_super(ci
),
1747 "Owner %llu has empty extent list at "
1749 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1750 le16_to_cpu(el
->l_tree_depth
));
1756 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1757 rec
= &el
->l_recs
[i
];
1760 * In the case that cpos is off the allocation
1761 * tree, this should just wind up returning the
1764 range
= le32_to_cpu(rec
->e_cpos
) +
1765 ocfs2_rec_clusters(el
, rec
);
1766 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1770 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1772 ocfs2_error(ocfs2_metadata_cache_get_super(ci
),
1773 "Owner %llu has bad blkno in extent list "
1774 "at depth %u (index %d)\n",
1775 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1776 le16_to_cpu(el
->l_tree_depth
), i
);
1783 ret
= ocfs2_read_extent_block(ci
, blkno
, &bh
);
1789 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1792 if (le16_to_cpu(el
->l_next_free_rec
) >
1793 le16_to_cpu(el
->l_count
)) {
1794 ocfs2_error(ocfs2_metadata_cache_get_super(ci
),
1795 "Owner %llu has bad count in extent list "
1796 "at block %llu (next free=%u, count=%u)\n",
1797 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1798 (unsigned long long)bh
->b_blocknr
,
1799 le16_to_cpu(el
->l_next_free_rec
),
1800 le16_to_cpu(el
->l_count
));
1811 * Catch any trailing bh that the loop didn't handle.
1819 * Given an initialized path (that is, it has a valid root extent
1820 * list), this function will traverse the btree in search of the path
1821 * which would contain cpos.
1823 * The path traveled is recorded in the path structure.
1825 * Note that this will not do any comparisons on leaf node extent
1826 * records, so it will work fine in the case that we just added a tree
1829 struct find_path_data
{
1831 struct ocfs2_path
*path
;
1833 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1835 struct find_path_data
*fp
= data
;
1838 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1841 static int ocfs2_find_path(struct ocfs2_caching_info
*ci
,
1842 struct ocfs2_path
*path
, u32 cpos
)
1844 struct find_path_data data
;
1848 return __ocfs2_find_path(ci
, path_root_el(path
), cpos
,
1849 find_path_ins
, &data
);
1852 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1854 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1855 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1856 struct buffer_head
**ret
= data
;
1858 /* We want to retain only the leaf block. */
1859 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1865 * Find the leaf block in the tree which would contain cpos. No
1866 * checking of the actual leaf is done.
1868 * Some paths want to call this instead of allocating a path structure
1869 * and calling ocfs2_find_path().
1871 * This function doesn't handle non btree extent lists.
1873 int ocfs2_find_leaf(struct ocfs2_caching_info
*ci
,
1874 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1875 struct buffer_head
**leaf_bh
)
1878 struct buffer_head
*bh
= NULL
;
1880 ret
= __ocfs2_find_path(ci
, root_el
, cpos
, find_leaf_ins
, &bh
);
1892 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1894 * Basically, we've moved stuff around at the bottom of the tree and
1895 * we need to fix up the extent records above the changes to reflect
1898 * left_rec: the record on the left.
1899 * left_child_el: is the child list pointed to by left_rec
1900 * right_rec: the record to the right of left_rec
1901 * right_child_el: is the child list pointed to by right_rec
1903 * By definition, this only works on interior nodes.
1905 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1906 struct ocfs2_extent_list
*left_child_el
,
1907 struct ocfs2_extent_rec
*right_rec
,
1908 struct ocfs2_extent_list
*right_child_el
)
1910 u32 left_clusters
, right_end
;
1913 * Interior nodes never have holes. Their cpos is the cpos of
1914 * the leftmost record in their child list. Their cluster
1915 * count covers the full theoretical range of their child list
1916 * - the range between their cpos and the cpos of the record
1917 * immediately to their right.
1919 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1920 if (!ocfs2_rec_clusters(right_child_el
, &right_child_el
->l_recs
[0])) {
1921 BUG_ON(right_child_el
->l_tree_depth
);
1922 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1923 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1925 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1926 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1929 * Calculate the rightmost cluster count boundary before
1930 * moving cpos - we will need to adjust clusters after
1931 * updating e_cpos to keep the same highest cluster count.
1933 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1934 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1936 right_rec
->e_cpos
= left_rec
->e_cpos
;
1937 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1939 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1940 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1944 * Adjust the adjacent root node records involved in a
1945 * rotation. left_el_blkno is passed in as a key so that we can easily
1946 * find it's index in the root list.
1948 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1949 struct ocfs2_extent_list
*left_el
,
1950 struct ocfs2_extent_list
*right_el
,
1955 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1956 le16_to_cpu(left_el
->l_tree_depth
));
1958 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1959 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1964 * The path walking code should have never returned a root and
1965 * two paths which are not adjacent.
1967 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1969 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1970 &root_el
->l_recs
[i
+ 1], right_el
);
1974 * We've changed a leaf block (in right_path) and need to reflect that
1975 * change back up the subtree.
1977 * This happens in multiple places:
1978 * - When we've moved an extent record from the left path leaf to the right
1979 * path leaf to make room for an empty extent in the left path leaf.
1980 * - When our insert into the right path leaf is at the leftmost edge
1981 * and requires an update of the path immediately to it's left. This
1982 * can occur at the end of some types of rotation and appending inserts.
1983 * - When we've adjusted the last extent record in the left path leaf and the
1984 * 1st extent record in the right path leaf during cross extent block merge.
1986 static void ocfs2_complete_edge_insert(handle_t
*handle
,
1987 struct ocfs2_path
*left_path
,
1988 struct ocfs2_path
*right_path
,
1992 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1993 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1994 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1997 * Update the counts and position values within all the
1998 * interior nodes to reflect the leaf rotation we just did.
2000 * The root node is handled below the loop.
2002 * We begin the loop with right_el and left_el pointing to the
2003 * leaf lists and work our way up.
2005 * NOTE: within this loop, left_el and right_el always refer
2006 * to the *child* lists.
2008 left_el
= path_leaf_el(left_path
);
2009 right_el
= path_leaf_el(right_path
);
2010 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
2011 mlog(0, "Adjust records at index %u\n", i
);
2014 * One nice property of knowing that all of these
2015 * nodes are below the root is that we only deal with
2016 * the leftmost right node record and the rightmost
2019 el
= left_path
->p_node
[i
].el
;
2020 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
2021 left_rec
= &el
->l_recs
[idx
];
2023 el
= right_path
->p_node
[i
].el
;
2024 right_rec
= &el
->l_recs
[0];
2026 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
2029 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
2033 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
2038 * Setup our list pointers now so that the current
2039 * parents become children in the next iteration.
2041 left_el
= left_path
->p_node
[i
].el
;
2042 right_el
= right_path
->p_node
[i
].el
;
2046 * At the root node, adjust the two adjacent records which
2047 * begin our path to the leaves.
2050 el
= left_path
->p_node
[subtree_index
].el
;
2051 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
2052 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
2054 ocfs2_adjust_root_records(el
, left_el
, right_el
,
2055 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
2057 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2059 ret
= ocfs2_journal_dirty(handle
, root_bh
);
2064 static int ocfs2_rotate_subtree_right(handle_t
*handle
,
2065 struct ocfs2_extent_tree
*et
,
2066 struct ocfs2_path
*left_path
,
2067 struct ocfs2_path
*right_path
,
2071 struct buffer_head
*right_leaf_bh
;
2072 struct buffer_head
*left_leaf_bh
= NULL
;
2073 struct buffer_head
*root_bh
;
2074 struct ocfs2_extent_list
*right_el
, *left_el
;
2075 struct ocfs2_extent_rec move_rec
;
2077 left_leaf_bh
= path_leaf_bh(left_path
);
2078 left_el
= path_leaf_el(left_path
);
2080 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
2081 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
2082 "Inode %llu has non-full interior leaf node %llu"
2084 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
2085 (unsigned long long)left_leaf_bh
->b_blocknr
,
2086 le16_to_cpu(left_el
->l_next_free_rec
));
2091 * This extent block may already have an empty record, so we
2092 * return early if so.
2094 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
2097 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2098 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2100 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
2107 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2108 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2115 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2123 right_leaf_bh
= path_leaf_bh(right_path
);
2124 right_el
= path_leaf_el(right_path
);
2126 /* This is a code error, not a disk corruption. */
2127 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
2128 "because rightmost leaf block %llu is empty\n",
2129 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
2130 (unsigned long long)right_leaf_bh
->b_blocknr
);
2132 ocfs2_create_empty_extent(right_el
);
2134 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
2140 /* Do the copy now. */
2141 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
2142 move_rec
= left_el
->l_recs
[i
];
2143 right_el
->l_recs
[0] = move_rec
;
2146 * Clear out the record we just copied and shift everything
2147 * over, leaving an empty extent in the left leaf.
2149 * We temporarily subtract from next_free_rec so that the
2150 * shift will lose the tail record (which is now defunct).
2152 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
2153 ocfs2_shift_records_right(left_el
);
2154 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2155 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
2157 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
2163 ocfs2_complete_edge_insert(handle
, left_path
, right_path
,
2171 * Given a full path, determine what cpos value would return us a path
2172 * containing the leaf immediately to the left of the current one.
2174 * Will return zero if the path passed in is already the leftmost path.
2176 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
2177 struct ocfs2_path
*path
, u32
*cpos
)
2181 struct ocfs2_extent_list
*el
;
2183 BUG_ON(path
->p_tree_depth
== 0);
2187 blkno
= path_leaf_bh(path
)->b_blocknr
;
2189 /* Start at the tree node just above the leaf and work our way up. */
2190 i
= path
->p_tree_depth
- 1;
2192 el
= path
->p_node
[i
].el
;
2195 * Find the extent record just before the one in our
2198 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2199 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2203 * We've determined that the
2204 * path specified is already
2205 * the leftmost one - return a
2211 * The leftmost record points to our
2212 * leaf - we need to travel up the
2218 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
2219 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
2220 &el
->l_recs
[j
- 1]);
2227 * If we got here, we never found a valid node where
2228 * the tree indicated one should be.
2231 "Invalid extent tree at extent block %llu\n",
2232 (unsigned long long)blkno
);
2237 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2246 * Extend the transaction by enough credits to complete the rotation,
2247 * and still leave at least the original number of credits allocated
2248 * to this transaction.
2250 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
2252 struct ocfs2_path
*path
)
2254 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
2256 if (handle
->h_buffer_credits
< credits
)
2257 return ocfs2_extend_trans(handle
, credits
);
2263 * Trap the case where we're inserting into the theoretical range past
2264 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2265 * whose cpos is less than ours into the right leaf.
2267 * It's only necessary to look at the rightmost record of the left
2268 * leaf because the logic that calls us should ensure that the
2269 * theoretical ranges in the path components above the leaves are
2272 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
2275 struct ocfs2_extent_list
*left_el
;
2276 struct ocfs2_extent_rec
*rec
;
2279 left_el
= path_leaf_el(left_path
);
2280 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
2281 rec
= &left_el
->l_recs
[next_free
- 1];
2283 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
2288 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
2290 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
2292 struct ocfs2_extent_rec
*rec
;
2297 rec
= &el
->l_recs
[0];
2298 if (ocfs2_is_empty_extent(rec
)) {
2302 rec
= &el
->l_recs
[1];
2305 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2306 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2312 * Rotate all the records in a btree right one record, starting at insert_cpos.
2314 * The path to the rightmost leaf should be passed in.
2316 * The array is assumed to be large enough to hold an entire path (tree depth).
2318 * Upon succesful return from this function:
2320 * - The 'right_path' array will contain a path to the leaf block
2321 * whose range contains e_cpos.
2322 * - That leaf block will have a single empty extent in list index 0.
2323 * - In the case that the rotation requires a post-insert update,
2324 * *ret_left_path will contain a valid path which can be passed to
2325 * ocfs2_insert_path().
2327 static int ocfs2_rotate_tree_right(handle_t
*handle
,
2328 struct ocfs2_extent_tree
*et
,
2329 enum ocfs2_split_type split
,
2331 struct ocfs2_path
*right_path
,
2332 struct ocfs2_path
**ret_left_path
)
2334 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2336 struct ocfs2_path
*left_path
= NULL
;
2337 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
2339 *ret_left_path
= NULL
;
2341 left_path
= ocfs2_new_path_from_path(right_path
);
2348 ret
= ocfs2_find_cpos_for_left_leaf(sb
, right_path
, &cpos
);
2354 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2357 * What we want to do here is:
2359 * 1) Start with the rightmost path.
2361 * 2) Determine a path to the leaf block directly to the left
2364 * 3) Determine the 'subtree root' - the lowest level tree node
2365 * which contains a path to both leaves.
2367 * 4) Rotate the subtree.
2369 * 5) Find the next subtree by considering the left path to be
2370 * the new right path.
2372 * The check at the top of this while loop also accepts
2373 * insert_cpos == cpos because cpos is only a _theoretical_
2374 * value to get us the left path - insert_cpos might very well
2375 * be filling that hole.
2377 * Stop at a cpos of '0' because we either started at the
2378 * leftmost branch (i.e., a tree with one branch and a
2379 * rotation inside of it), or we've gone as far as we can in
2380 * rotating subtrees.
2382 while (cpos
&& insert_cpos
<= cpos
) {
2383 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2386 ret
= ocfs2_find_path(et
->et_ci
, left_path
, cpos
);
2392 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2393 path_leaf_bh(right_path
),
2394 "Owner %llu: error during insert of %u "
2395 "(left path cpos %u) results in two identical "
2396 "paths ending at %llu\n",
2397 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
2399 (unsigned long long)
2400 path_leaf_bh(left_path
)->b_blocknr
);
2402 if (split
== SPLIT_NONE
&&
2403 ocfs2_rotate_requires_path_adjustment(left_path
,
2407 * We've rotated the tree as much as we
2408 * should. The rest is up to
2409 * ocfs2_insert_path() to complete, after the
2410 * record insertion. We indicate this
2411 * situation by returning the left path.
2413 * The reason we don't adjust the records here
2414 * before the record insert is that an error
2415 * later might break the rule where a parent
2416 * record e_cpos will reflect the actual
2417 * e_cpos of the 1st nonempty record of the
2420 *ret_left_path
= left_path
;
2424 start
= ocfs2_find_subtree_root(et
, left_path
, right_path
);
2426 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2428 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2429 right_path
->p_tree_depth
);
2431 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2432 orig_credits
, right_path
);
2438 ret
= ocfs2_rotate_subtree_right(handle
, et
, left_path
,
2445 if (split
!= SPLIT_NONE
&&
2446 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2449 * A rotate moves the rightmost left leaf
2450 * record over to the leftmost right leaf
2451 * slot. If we're doing an extent split
2452 * instead of a real insert, then we have to
2453 * check that the extent to be split wasn't
2454 * just moved over. If it was, then we can
2455 * exit here, passing left_path back -
2456 * ocfs2_split_extent() is smart enough to
2457 * search both leaves.
2459 *ret_left_path
= left_path
;
2464 * There is no need to re-read the next right path
2465 * as we know that it'll be our current left
2466 * path. Optimize by copying values instead.
2468 ocfs2_mv_path(right_path
, left_path
);
2470 ret
= ocfs2_find_cpos_for_left_leaf(sb
, right_path
, &cpos
);
2478 ocfs2_free_path(left_path
);
2484 static int ocfs2_update_edge_lengths(handle_t
*handle
,
2485 struct ocfs2_extent_tree
*et
,
2486 int subtree_index
, struct ocfs2_path
*path
)
2489 struct ocfs2_extent_rec
*rec
;
2490 struct ocfs2_extent_list
*el
;
2491 struct ocfs2_extent_block
*eb
;
2495 * In normal tree rotation process, we will never touch the
2496 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2497 * doesn't reserve the credits for them either.
2499 * But we do have a special case here which will update the rightmost
2500 * records for all the bh in the path.
2501 * So we have to allocate extra credits and access them.
2503 ret
= ocfs2_extend_trans(handle
,
2504 handle
->h_buffer_credits
+ subtree_index
);
2510 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, path
);
2516 /* Path should always be rightmost. */
2517 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2518 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2521 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2522 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2523 rec
= &el
->l_recs
[idx
];
2524 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2526 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2527 el
= path
->p_node
[i
].el
;
2528 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2529 rec
= &el
->l_recs
[idx
];
2531 rec
->e_int_clusters
= cpu_to_le32(range
);
2532 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2534 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2540 static void ocfs2_unlink_path(handle_t
*handle
,
2541 struct ocfs2_extent_tree
*et
,
2542 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2543 struct ocfs2_path
*path
, int unlink_start
)
2546 struct ocfs2_extent_block
*eb
;
2547 struct ocfs2_extent_list
*el
;
2548 struct buffer_head
*bh
;
2550 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2551 bh
= path
->p_node
[i
].bh
;
2553 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2555 * Not all nodes might have had their final count
2556 * decremented by the caller - handle this here.
2559 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2561 "Inode %llu, attempted to remove extent block "
2562 "%llu with %u records\n",
2563 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
2564 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2565 le16_to_cpu(el
->l_next_free_rec
));
2567 ocfs2_journal_dirty(handle
, bh
);
2568 ocfs2_remove_from_cache(et
->et_ci
, bh
);
2572 el
->l_next_free_rec
= 0;
2573 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2575 ocfs2_journal_dirty(handle
, bh
);
2577 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2581 ocfs2_remove_from_cache(et
->et_ci
, bh
);
2585 static void ocfs2_unlink_subtree(handle_t
*handle
,
2586 struct ocfs2_extent_tree
*et
,
2587 struct ocfs2_path
*left_path
,
2588 struct ocfs2_path
*right_path
,
2590 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2593 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2594 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2595 struct ocfs2_extent_list
*el
;
2596 struct ocfs2_extent_block
*eb
;
2598 el
= path_leaf_el(left_path
);
2600 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2602 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2603 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2606 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2608 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2609 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2611 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2612 eb
->h_next_leaf_blk
= 0;
2614 ocfs2_journal_dirty(handle
, root_bh
);
2615 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2617 ocfs2_unlink_path(handle
, et
, dealloc
, right_path
,
2621 static int ocfs2_rotate_subtree_left(handle_t
*handle
,
2622 struct ocfs2_extent_tree
*et
,
2623 struct ocfs2_path
*left_path
,
2624 struct ocfs2_path
*right_path
,
2626 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2629 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2630 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2631 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2632 struct ocfs2_extent_block
*eb
;
2636 right_leaf_el
= path_leaf_el(right_path
);
2637 left_leaf_el
= path_leaf_el(left_path
);
2638 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2639 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2641 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2644 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2645 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2647 * It's legal for us to proceed if the right leaf is
2648 * the rightmost one and it has an empty extent. There
2649 * are two cases to handle - whether the leaf will be
2650 * empty after removal or not. If the leaf isn't empty
2651 * then just remove the empty extent up front. The
2652 * next block will handle empty leaves by flagging
2655 * Non rightmost leaves will throw -EAGAIN and the
2656 * caller can manually move the subtree and retry.
2659 if (eb
->h_next_leaf_blk
!= 0ULL)
2662 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2663 ret
= ocfs2_journal_access_eb(handle
, et
->et_ci
,
2664 path_leaf_bh(right_path
),
2665 OCFS2_JOURNAL_ACCESS_WRITE
);
2671 ocfs2_remove_empty_extent(right_leaf_el
);
2673 right_has_empty
= 1;
2676 if (eb
->h_next_leaf_blk
== 0ULL &&
2677 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2679 * We have to update i_last_eb_blk during the meta
2682 ret
= ocfs2_et_root_journal_access(handle
, et
,
2683 OCFS2_JOURNAL_ACCESS_WRITE
);
2689 del_right_subtree
= 1;
2693 * Getting here with an empty extent in the right path implies
2694 * that it's the rightmost path and will be deleted.
2696 BUG_ON(right_has_empty
&& !del_right_subtree
);
2698 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
2705 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2706 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2713 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2721 if (!right_has_empty
) {
2723 * Only do this if we're moving a real
2724 * record. Otherwise, the action is delayed until
2725 * after removal of the right path in which case we
2726 * can do a simple shift to remove the empty extent.
2728 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2729 memset(&right_leaf_el
->l_recs
[0], 0,
2730 sizeof(struct ocfs2_extent_rec
));
2732 if (eb
->h_next_leaf_blk
== 0ULL) {
2734 * Move recs over to get rid of empty extent, decrease
2735 * next_free. This is allowed to remove the last
2736 * extent in our leaf (setting l_next_free_rec to
2737 * zero) - the delete code below won't care.
2739 ocfs2_remove_empty_extent(right_leaf_el
);
2742 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2745 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2749 if (del_right_subtree
) {
2750 ocfs2_unlink_subtree(handle
, et
, left_path
, right_path
,
2751 subtree_index
, dealloc
);
2752 ret
= ocfs2_update_edge_lengths(handle
, et
, subtree_index
,
2759 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2760 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2763 * Removal of the extent in the left leaf was skipped
2764 * above so we could delete the right path
2767 if (right_has_empty
)
2768 ocfs2_remove_empty_extent(left_leaf_el
);
2770 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2776 ocfs2_complete_edge_insert(handle
, left_path
, right_path
,
2784 * Given a full path, determine what cpos value would return us a path
2785 * containing the leaf immediately to the right of the current one.
2787 * Will return zero if the path passed in is already the rightmost path.
2789 * This looks similar, but is subtly different to
2790 * ocfs2_find_cpos_for_left_leaf().
2792 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2793 struct ocfs2_path
*path
, u32
*cpos
)
2797 struct ocfs2_extent_list
*el
;
2801 if (path
->p_tree_depth
== 0)
2804 blkno
= path_leaf_bh(path
)->b_blocknr
;
2806 /* Start at the tree node just above the leaf and work our way up. */
2807 i
= path
->p_tree_depth
- 1;
2811 el
= path
->p_node
[i
].el
;
2814 * Find the extent record just after the one in our
2817 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2818 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2819 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2820 if (j
== (next_free
- 1)) {
2823 * We've determined that the
2824 * path specified is already
2825 * the rightmost one - return a
2831 * The rightmost record points to our
2832 * leaf - we need to travel up the
2838 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2844 * If we got here, we never found a valid node where
2845 * the tree indicated one should be.
2848 "Invalid extent tree at extent block %llu\n",
2849 (unsigned long long)blkno
);
2854 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2862 static int ocfs2_rotate_rightmost_leaf_left(handle_t
*handle
,
2863 struct ocfs2_extent_tree
*et
,
2864 struct ocfs2_path
*path
)
2867 struct buffer_head
*bh
= path_leaf_bh(path
);
2868 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
2870 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2873 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, path
,
2874 path_num_items(path
) - 1);
2880 ocfs2_remove_empty_extent(el
);
2882 ret
= ocfs2_journal_dirty(handle
, bh
);
2890 static int __ocfs2_rotate_tree_left(handle_t
*handle
,
2891 struct ocfs2_extent_tree
*et
,
2893 struct ocfs2_path
*path
,
2894 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2895 struct ocfs2_path
**empty_extent_path
)
2897 int ret
, subtree_root
, deleted
;
2899 struct ocfs2_path
*left_path
= NULL
;
2900 struct ocfs2_path
*right_path
= NULL
;
2901 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
2903 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2905 *empty_extent_path
= NULL
;
2907 ret
= ocfs2_find_cpos_for_right_leaf(sb
, path
, &right_cpos
);
2913 left_path
= ocfs2_new_path_from_path(path
);
2920 ocfs2_cp_path(left_path
, path
);
2922 right_path
= ocfs2_new_path_from_path(path
);
2929 while (right_cpos
) {
2930 ret
= ocfs2_find_path(et
->et_ci
, right_path
, right_cpos
);
2936 subtree_root
= ocfs2_find_subtree_root(et
, left_path
,
2939 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2941 (unsigned long long)
2942 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2943 right_path
->p_tree_depth
);
2945 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2946 orig_credits
, left_path
);
2953 * Caller might still want to make changes to the
2954 * tree root, so re-add it to the journal here.
2956 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2963 ret
= ocfs2_rotate_subtree_left(handle
, et
, left_path
,
2964 right_path
, subtree_root
,
2966 if (ret
== -EAGAIN
) {
2968 * The rotation has to temporarily stop due to
2969 * the right subtree having an empty
2970 * extent. Pass it back to the caller for a
2973 *empty_extent_path
= right_path
;
2983 * The subtree rotate might have removed records on
2984 * the rightmost edge. If so, then rotation is
2990 ocfs2_mv_path(left_path
, right_path
);
2992 ret
= ocfs2_find_cpos_for_right_leaf(sb
, left_path
,
3001 ocfs2_free_path(right_path
);
3002 ocfs2_free_path(left_path
);
3007 static int ocfs2_remove_rightmost_path(handle_t
*handle
,
3008 struct ocfs2_extent_tree
*et
,
3009 struct ocfs2_path
*path
,
3010 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3012 int ret
, subtree_index
;
3014 struct ocfs2_path
*left_path
= NULL
;
3015 struct ocfs2_extent_block
*eb
;
3016 struct ocfs2_extent_list
*el
;
3019 ret
= ocfs2_et_sanity_check(et
);
3023 * There's two ways we handle this depending on
3024 * whether path is the only existing one.
3026 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
3027 handle
->h_buffer_credits
,
3034 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, path
);
3040 ret
= ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et
->et_ci
),
3049 * We have a path to the left of this one - it needs
3052 left_path
= ocfs2_new_path_from_path(path
);
3059 ret
= ocfs2_find_path(et
->et_ci
, left_path
, cpos
);
3065 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, left_path
);
3071 subtree_index
= ocfs2_find_subtree_root(et
, left_path
, path
);
3073 ocfs2_unlink_subtree(handle
, et
, left_path
, path
,
3074 subtree_index
, dealloc
);
3075 ret
= ocfs2_update_edge_lengths(handle
, et
, subtree_index
,
3082 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
3083 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
3086 * 'path' is also the leftmost path which
3087 * means it must be the only one. This gets
3088 * handled differently because we want to
3089 * revert the root back to having extents
3092 ocfs2_unlink_path(handle
, et
, dealloc
, path
, 1);
3094 el
= et
->et_root_el
;
3095 el
->l_tree_depth
= 0;
3096 el
->l_next_free_rec
= 0;
3097 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3099 ocfs2_et_set_last_eb_blk(et
, 0);
3102 ocfs2_journal_dirty(handle
, path_root_bh(path
));
3105 ocfs2_free_path(left_path
);
3110 * Left rotation of btree records.
3112 * In many ways, this is (unsurprisingly) the opposite of right
3113 * rotation. We start at some non-rightmost path containing an empty
3114 * extent in the leaf block. The code works its way to the rightmost
3115 * path by rotating records to the left in every subtree.
3117 * This is used by any code which reduces the number of extent records
3118 * in a leaf. After removal, an empty record should be placed in the
3119 * leftmost list position.
3121 * This won't handle a length update of the rightmost path records if
3122 * the rightmost tree leaf record is removed so the caller is
3123 * responsible for detecting and correcting that.
3125 static int ocfs2_rotate_tree_left(handle_t
*handle
,
3126 struct ocfs2_extent_tree
*et
,
3127 struct ocfs2_path
*path
,
3128 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3130 int ret
, orig_credits
= handle
->h_buffer_credits
;
3131 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
3132 struct ocfs2_extent_block
*eb
;
3133 struct ocfs2_extent_list
*el
;
3135 el
= path_leaf_el(path
);
3136 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
3139 if (path
->p_tree_depth
== 0) {
3140 rightmost_no_delete
:
3142 * Inline extents. This is trivially handled, so do
3145 ret
= ocfs2_rotate_rightmost_leaf_left(handle
, et
, path
);
3152 * Handle rightmost branch now. There's several cases:
3153 * 1) simple rotation leaving records in there. That's trivial.
3154 * 2) rotation requiring a branch delete - there's no more
3155 * records left. Two cases of this:
3156 * a) There are branches to the left.
3157 * b) This is also the leftmost (the only) branch.
3159 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3160 * 2a) we need the left branch so that we can update it with the unlink
3161 * 2b) we need to bring the root back to inline extents.
3164 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
3166 if (eb
->h_next_leaf_blk
== 0) {
3168 * This gets a bit tricky if we're going to delete the
3169 * rightmost path. Get the other cases out of the way
3172 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
3173 goto rightmost_no_delete
;
3175 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
3177 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
3178 "Owner %llu has empty extent block at %llu",
3179 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
3180 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
3185 * XXX: The caller can not trust "path" any more after
3186 * this as it will have been deleted. What do we do?
3188 * In theory the rotate-for-merge code will never get
3189 * here because it'll always ask for a rotate in a
3193 ret
= ocfs2_remove_rightmost_path(handle
, et
, path
,
3201 * Now we can loop, remembering the path we get from -EAGAIN
3202 * and restarting from there.
3205 ret
= __ocfs2_rotate_tree_left(handle
, et
, orig_credits
, path
,
3206 dealloc
, &restart_path
);
3207 if (ret
&& ret
!= -EAGAIN
) {
3212 while (ret
== -EAGAIN
) {
3213 tmp_path
= restart_path
;
3214 restart_path
= NULL
;
3216 ret
= __ocfs2_rotate_tree_left(handle
, et
, orig_credits
,
3219 if (ret
&& ret
!= -EAGAIN
) {
3224 ocfs2_free_path(tmp_path
);
3232 ocfs2_free_path(tmp_path
);
3233 ocfs2_free_path(restart_path
);
3237 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
3240 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
3243 if (rec
->e_leaf_clusters
== 0) {
3245 * We consumed all of the merged-from record. An empty
3246 * extent cannot exist anywhere but the 1st array
3247 * position, so move things over if the merged-from
3248 * record doesn't occupy that position.
3250 * This creates a new empty extent so the caller
3251 * should be smart enough to have removed any existing
3255 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3256 size
= index
* sizeof(struct ocfs2_extent_rec
);
3257 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
3261 * Always memset - the caller doesn't check whether it
3262 * created an empty extent, so there could be junk in
3265 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3269 static int ocfs2_get_right_path(struct ocfs2_extent_tree
*et
,
3270 struct ocfs2_path
*left_path
,
3271 struct ocfs2_path
**ret_right_path
)
3275 struct ocfs2_path
*right_path
= NULL
;
3276 struct ocfs2_extent_list
*left_el
;
3278 *ret_right_path
= NULL
;
3280 /* This function shouldn't be called for non-trees. */
3281 BUG_ON(left_path
->p_tree_depth
== 0);
3283 left_el
= path_leaf_el(left_path
);
3284 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
3286 ret
= ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et
->et_ci
),
3287 left_path
, &right_cpos
);
3293 /* This function shouldn't be called for the rightmost leaf. */
3294 BUG_ON(right_cpos
== 0);
3296 right_path
= ocfs2_new_path_from_path(left_path
);
3303 ret
= ocfs2_find_path(et
->et_ci
, right_path
, right_cpos
);
3309 *ret_right_path
= right_path
;
3312 ocfs2_free_path(right_path
);
3317 * Remove split_rec clusters from the record at index and merge them
3318 * onto the beginning of the record "next" to it.
3319 * For index < l_count - 1, the next means the extent rec at index + 1.
3320 * For index == l_count - 1, the "next" means the 1st extent rec of the
3321 * next extent block.
3323 static int ocfs2_merge_rec_right(struct ocfs2_path
*left_path
,
3325 struct ocfs2_extent_tree
*et
,
3326 struct ocfs2_extent_rec
*split_rec
,
3329 int ret
, next_free
, i
;
3330 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3331 struct ocfs2_extent_rec
*left_rec
;
3332 struct ocfs2_extent_rec
*right_rec
;
3333 struct ocfs2_extent_list
*right_el
;
3334 struct ocfs2_path
*right_path
= NULL
;
3335 int subtree_index
= 0;
3336 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3337 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3338 struct buffer_head
*root_bh
= NULL
;
3340 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3341 left_rec
= &el
->l_recs
[index
];
3343 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3344 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3345 /* we meet with a cross extent block merge. */
3346 ret
= ocfs2_get_right_path(et
, left_path
, &right_path
);
3352 right_el
= path_leaf_el(right_path
);
3353 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3354 BUG_ON(next_free
<= 0);
3355 right_rec
= &right_el
->l_recs
[0];
3356 if (ocfs2_is_empty_extent(right_rec
)) {
3357 BUG_ON(next_free
<= 1);
3358 right_rec
= &right_el
->l_recs
[1];
3361 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3362 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3363 le32_to_cpu(right_rec
->e_cpos
));
3365 subtree_index
= ocfs2_find_subtree_root(et
, left_path
,
3368 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3369 handle
->h_buffer_credits
,
3376 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3377 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3379 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
3386 for (i
= subtree_index
+ 1;
3387 i
< path_num_items(right_path
); i
++) {
3388 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3395 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3404 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3405 right_rec
= &el
->l_recs
[index
+ 1];
3408 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, left_path
,
3409 path_num_items(left_path
) - 1);
3415 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3417 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3418 le64_add_cpu(&right_rec
->e_blkno
,
3419 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et
->et_ci
),
3421 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3423 ocfs2_cleanup_merge(el
, index
);
3425 ret
= ocfs2_journal_dirty(handle
, bh
);
3430 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3434 ocfs2_complete_edge_insert(handle
, left_path
, right_path
,
3439 ocfs2_free_path(right_path
);
3443 static int ocfs2_get_left_path(struct ocfs2_extent_tree
*et
,
3444 struct ocfs2_path
*right_path
,
3445 struct ocfs2_path
**ret_left_path
)
3449 struct ocfs2_path
*left_path
= NULL
;
3451 *ret_left_path
= NULL
;
3453 /* This function shouldn't be called for non-trees. */
3454 BUG_ON(right_path
->p_tree_depth
== 0);
3456 ret
= ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et
->et_ci
),
3457 right_path
, &left_cpos
);
3463 /* This function shouldn't be called for the leftmost leaf. */
3464 BUG_ON(left_cpos
== 0);
3466 left_path
= ocfs2_new_path_from_path(right_path
);
3473 ret
= ocfs2_find_path(et
->et_ci
, left_path
, left_cpos
);
3479 *ret_left_path
= left_path
;
3482 ocfs2_free_path(left_path
);
3487 * Remove split_rec clusters from the record at index and merge them
3488 * onto the tail of the record "before" it.
3489 * For index > 0, the "before" means the extent rec at index - 1.
3491 * For index == 0, the "before" means the last record of the previous
3492 * extent block. And there is also a situation that we may need to
3493 * remove the rightmost leaf extent block in the right_path and change
3494 * the right path to indicate the new rightmost path.
3496 static int ocfs2_merge_rec_left(struct ocfs2_path
*right_path
,
3498 struct ocfs2_extent_tree
*et
,
3499 struct ocfs2_extent_rec
*split_rec
,
3500 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3503 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3504 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3505 struct ocfs2_extent_rec
*left_rec
;
3506 struct ocfs2_extent_rec
*right_rec
;
3507 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3508 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3509 struct buffer_head
*root_bh
= NULL
;
3510 struct ocfs2_path
*left_path
= NULL
;
3511 struct ocfs2_extent_list
*left_el
;
3515 right_rec
= &el
->l_recs
[index
];
3517 /* we meet with a cross extent block merge. */
3518 ret
= ocfs2_get_left_path(et
, right_path
, &left_path
);
3524 left_el
= path_leaf_el(left_path
);
3525 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3526 le16_to_cpu(left_el
->l_count
));
3528 left_rec
= &left_el
->l_recs
[
3529 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3530 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3531 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3532 le32_to_cpu(split_rec
->e_cpos
));
3534 subtree_index
= ocfs2_find_subtree_root(et
, left_path
,
3537 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3538 handle
->h_buffer_credits
,
3545 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3546 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3548 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
3555 for (i
= subtree_index
+ 1;
3556 i
< path_num_items(right_path
); i
++) {
3557 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3564 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3572 left_rec
= &el
->l_recs
[index
- 1];
3573 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3574 has_empty_extent
= 1;
3577 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
3578 path_num_items(right_path
) - 1);
3584 if (has_empty_extent
&& index
== 1) {
3586 * The easy case - we can just plop the record right in.
3588 *left_rec
= *split_rec
;
3590 has_empty_extent
= 0;
3592 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3594 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3595 le64_add_cpu(&right_rec
->e_blkno
,
3596 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et
->et_ci
),
3598 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3600 ocfs2_cleanup_merge(el
, index
);
3602 ret
= ocfs2_journal_dirty(handle
, bh
);
3607 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3612 * In the situation that the right_rec is empty and the extent
3613 * block is empty also, ocfs2_complete_edge_insert can't handle
3614 * it and we need to delete the right extent block.
3616 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3617 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3619 ret
= ocfs2_remove_rightmost_path(handle
, et
,
3627 /* Now the rightmost extent block has been deleted.
3628 * So we use the new rightmost path.
3630 ocfs2_mv_path(right_path
, left_path
);
3633 ocfs2_complete_edge_insert(handle
, left_path
,
3634 right_path
, subtree_index
);
3638 ocfs2_free_path(left_path
);
3642 static int ocfs2_try_to_merge_extent(handle_t
*handle
,
3643 struct ocfs2_extent_tree
*et
,
3644 struct ocfs2_path
*path
,
3646 struct ocfs2_extent_rec
*split_rec
,
3647 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3648 struct ocfs2_merge_ctxt
*ctxt
)
3651 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3652 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3654 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3656 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3658 * The merge code will need to create an empty
3659 * extent to take the place of the newly
3660 * emptied slot. Remove any pre-existing empty
3661 * extents - having more than one in a leaf is
3664 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
3670 rec
= &el
->l_recs
[split_index
];
3673 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3675 * Left-right contig implies this.
3677 BUG_ON(!ctxt
->c_split_covers_rec
);
3680 * Since the leftright insert always covers the entire
3681 * extent, this call will delete the insert record
3682 * entirely, resulting in an empty extent record added to
3685 * Since the adding of an empty extent shifts
3686 * everything back to the right, there's no need to
3687 * update split_index here.
3689 * When the split_index is zero, we need to merge it to the
3690 * prevoius extent block. It is more efficient and easier
3691 * if we do merge_right first and merge_left later.
3693 ret
= ocfs2_merge_rec_right(path
, handle
, et
, split_rec
,
3701 * We can only get this from logic error above.
3703 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3705 /* The merge left us with an empty extent, remove it. */
3706 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
3712 rec
= &el
->l_recs
[split_index
];
3715 * Note that we don't pass split_rec here on purpose -
3716 * we've merged it into the rec already.
3718 ret
= ocfs2_merge_rec_left(path
, handle
, et
, rec
,
3719 dealloc
, split_index
);
3726 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
3728 * Error from this last rotate is not critical, so
3729 * print but don't bubble it up.
3736 * Merge a record to the left or right.
3738 * 'contig_type' is relative to the existing record,
3739 * so for example, if we're "right contig", it's to
3740 * the record on the left (hence the left merge).
3742 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3743 ret
= ocfs2_merge_rec_left(path
, handle
, et
,
3751 ret
= ocfs2_merge_rec_right(path
, handle
,
3760 if (ctxt
->c_split_covers_rec
) {
3762 * The merge may have left an empty extent in
3763 * our leaf. Try to rotate it away.
3765 ret
= ocfs2_rotate_tree_left(handle
, et
, path
,
3777 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3778 enum ocfs2_split_type split
,
3779 struct ocfs2_extent_rec
*rec
,
3780 struct ocfs2_extent_rec
*split_rec
)
3784 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3785 le16_to_cpu(split_rec
->e_leaf_clusters
));
3787 if (split
== SPLIT_LEFT
) {
3789 * Region is on the left edge of the existing
3792 le32_add_cpu(&rec
->e_cpos
,
3793 le16_to_cpu(split_rec
->e_leaf_clusters
));
3794 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3795 le16_add_cpu(&rec
->e_leaf_clusters
,
3796 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3799 * Region is on the right edge of the existing
3802 le16_add_cpu(&rec
->e_leaf_clusters
,
3803 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3808 * Do the final bits of extent record insertion at the target leaf
3809 * list. If this leaf is part of an allocation tree, it is assumed
3810 * that the tree above has been prepared.
3812 static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree
*et
,
3813 struct ocfs2_extent_rec
*insert_rec
,
3814 struct ocfs2_extent_list
*el
,
3815 struct ocfs2_insert_type
*insert
)
3817 int i
= insert
->ins_contig_index
;
3819 struct ocfs2_extent_rec
*rec
;
3821 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3823 if (insert
->ins_split
!= SPLIT_NONE
) {
3824 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3826 rec
= &el
->l_recs
[i
];
3827 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et
->et_ci
),
3828 insert
->ins_split
, rec
,
3834 * Contiguous insert - either left or right.
3836 if (insert
->ins_contig
!= CONTIG_NONE
) {
3837 rec
= &el
->l_recs
[i
];
3838 if (insert
->ins_contig
== CONTIG_LEFT
) {
3839 rec
->e_blkno
= insert_rec
->e_blkno
;
3840 rec
->e_cpos
= insert_rec
->e_cpos
;
3842 le16_add_cpu(&rec
->e_leaf_clusters
,
3843 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3848 * Handle insert into an empty leaf.
3850 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3851 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3852 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3853 el
->l_recs
[0] = *insert_rec
;
3854 el
->l_next_free_rec
= cpu_to_le16(1);
3861 if (insert
->ins_appending
== APPEND_TAIL
) {
3862 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3863 rec
= &el
->l_recs
[i
];
3864 range
= le32_to_cpu(rec
->e_cpos
)
3865 + le16_to_cpu(rec
->e_leaf_clusters
);
3866 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3868 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3869 le16_to_cpu(el
->l_count
),
3870 "owner %llu, depth %u, count %u, next free %u, "
3871 "rec.cpos %u, rec.clusters %u, "
3872 "insert.cpos %u, insert.clusters %u\n",
3873 ocfs2_metadata_cache_owner(et
->et_ci
),
3874 le16_to_cpu(el
->l_tree_depth
),
3875 le16_to_cpu(el
->l_count
),
3876 le16_to_cpu(el
->l_next_free_rec
),
3877 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3878 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3879 le32_to_cpu(insert_rec
->e_cpos
),
3880 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3882 el
->l_recs
[i
] = *insert_rec
;
3883 le16_add_cpu(&el
->l_next_free_rec
, 1);
3889 * Ok, we have to rotate.
3891 * At this point, it is safe to assume that inserting into an
3892 * empty leaf and appending to a leaf have both been handled
3895 * This leaf needs to have space, either by the empty 1st
3896 * extent record, or by virtue of an l_next_rec < l_count.
3898 ocfs2_rotate_leaf(el
, insert_rec
);
3901 static void ocfs2_adjust_rightmost_records(handle_t
*handle
,
3902 struct ocfs2_extent_tree
*et
,
3903 struct ocfs2_path
*path
,
3904 struct ocfs2_extent_rec
*insert_rec
)
3906 int ret
, i
, next_free
;
3907 struct buffer_head
*bh
;
3908 struct ocfs2_extent_list
*el
;
3909 struct ocfs2_extent_rec
*rec
;
3912 * Update everything except the leaf block.
3914 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3915 bh
= path
->p_node
[i
].bh
;
3916 el
= path
->p_node
[i
].el
;
3918 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3919 if (next_free
== 0) {
3920 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
3921 "Owner %llu has a bad extent list",
3922 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
));
3927 rec
= &el
->l_recs
[next_free
- 1];
3929 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3930 le32_add_cpu(&rec
->e_int_clusters
,
3931 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3932 le32_add_cpu(&rec
->e_int_clusters
,
3933 -le32_to_cpu(rec
->e_cpos
));
3935 ret
= ocfs2_journal_dirty(handle
, bh
);
3942 static int ocfs2_append_rec_to_path(handle_t
*handle
,
3943 struct ocfs2_extent_tree
*et
,
3944 struct ocfs2_extent_rec
*insert_rec
,
3945 struct ocfs2_path
*right_path
,
3946 struct ocfs2_path
**ret_left_path
)
3949 struct ocfs2_extent_list
*el
;
3950 struct ocfs2_path
*left_path
= NULL
;
3952 *ret_left_path
= NULL
;
3955 * This shouldn't happen for non-trees. The extent rec cluster
3956 * count manipulation below only works for interior nodes.
3958 BUG_ON(right_path
->p_tree_depth
== 0);
3961 * If our appending insert is at the leftmost edge of a leaf,
3962 * then we might need to update the rightmost records of the
3965 el
= path_leaf_el(right_path
);
3966 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3967 if (next_free
== 0 ||
3968 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3971 ret
= ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et
->et_ci
),
3972 right_path
, &left_cpos
);
3978 mlog(0, "Append may need a left path update. cpos: %u, "
3979 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3983 * No need to worry if the append is already in the
3987 left_path
= ocfs2_new_path_from_path(right_path
);
3994 ret
= ocfs2_find_path(et
->et_ci
, left_path
,
4002 * ocfs2_insert_path() will pass the left_path to the
4008 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, right_path
);
4014 ocfs2_adjust_rightmost_records(handle
, et
, right_path
, insert_rec
);
4016 *ret_left_path
= left_path
;
4020 ocfs2_free_path(left_path
);
4025 static void ocfs2_split_record(struct ocfs2_extent_tree
*et
,
4026 struct ocfs2_path
*left_path
,
4027 struct ocfs2_path
*right_path
,
4028 struct ocfs2_extent_rec
*split_rec
,
4029 enum ocfs2_split_type split
)
4032 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
4033 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
4034 struct ocfs2_extent_rec
*rec
, *tmprec
;
4036 right_el
= path_leaf_el(right_path
);
4038 left_el
= path_leaf_el(left_path
);
4041 insert_el
= right_el
;
4042 index
= ocfs2_search_extent_list(el
, cpos
);
4044 if (index
== 0 && left_path
) {
4045 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
4048 * This typically means that the record
4049 * started in the left path but moved to the
4050 * right as a result of rotation. We either
4051 * move the existing record to the left, or we
4052 * do the later insert there.
4054 * In this case, the left path should always
4055 * exist as the rotate code will have passed
4056 * it back for a post-insert update.
4059 if (split
== SPLIT_LEFT
) {
4061 * It's a left split. Since we know
4062 * that the rotate code gave us an
4063 * empty extent in the left path, we
4064 * can just do the insert there.
4066 insert_el
= left_el
;
4069 * Right split - we have to move the
4070 * existing record over to the left
4071 * leaf. The insert will be into the
4072 * newly created empty extent in the
4075 tmprec
= &right_el
->l_recs
[index
];
4076 ocfs2_rotate_leaf(left_el
, tmprec
);
4079 memset(tmprec
, 0, sizeof(*tmprec
));
4080 index
= ocfs2_search_extent_list(left_el
, cpos
);
4081 BUG_ON(index
== -1);
4086 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
4088 * Left path is easy - we can just allow the insert to
4092 insert_el
= left_el
;
4093 index
= ocfs2_search_extent_list(el
, cpos
);
4094 BUG_ON(index
== -1);
4097 rec
= &el
->l_recs
[index
];
4098 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et
->et_ci
),
4099 split
, rec
, split_rec
);
4100 ocfs2_rotate_leaf(insert_el
, split_rec
);
4104 * This function only does inserts on an allocation b-tree. For tree
4105 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4107 * right_path is the path we want to do the actual insert
4108 * in. left_path should only be passed in if we need to update that
4109 * portion of the tree after an edge insert.
4111 static int ocfs2_insert_path(handle_t
*handle
,
4112 struct ocfs2_extent_tree
*et
,
4113 struct ocfs2_path
*left_path
,
4114 struct ocfs2_path
*right_path
,
4115 struct ocfs2_extent_rec
*insert_rec
,
4116 struct ocfs2_insert_type
*insert
)
4118 int ret
, subtree_index
;
4119 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
4122 int credits
= handle
->h_buffer_credits
;
4125 * There's a chance that left_path got passed back to
4126 * us without being accounted for in the
4127 * journal. Extend our transaction here to be sure we
4128 * can change those blocks.
4130 credits
+= left_path
->p_tree_depth
;
4132 ret
= ocfs2_extend_trans(handle
, credits
);
4138 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, left_path
);
4146 * Pass both paths to the journal. The majority of inserts
4147 * will be touching all components anyway.
4149 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, right_path
);
4155 if (insert
->ins_split
!= SPLIT_NONE
) {
4157 * We could call ocfs2_insert_at_leaf() for some types
4158 * of splits, but it's easier to just let one separate
4159 * function sort it all out.
4161 ocfs2_split_record(et
, left_path
, right_path
,
4162 insert_rec
, insert
->ins_split
);
4165 * Split might have modified either leaf and we don't
4166 * have a guarantee that the later edge insert will
4167 * dirty this for us.
4170 ret
= ocfs2_journal_dirty(handle
,
4171 path_leaf_bh(left_path
));
4175 ocfs2_insert_at_leaf(et
, insert_rec
, path_leaf_el(right_path
),
4178 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
4184 * The rotate code has indicated that we need to fix
4185 * up portions of the tree after the insert.
4187 * XXX: Should we extend the transaction here?
4189 subtree_index
= ocfs2_find_subtree_root(et
, left_path
,
4191 ocfs2_complete_edge_insert(handle
, left_path
, right_path
,
4200 static int ocfs2_do_insert_extent(handle_t
*handle
,
4201 struct ocfs2_extent_tree
*et
,
4202 struct ocfs2_extent_rec
*insert_rec
,
4203 struct ocfs2_insert_type
*type
)
4205 int ret
, rotate
= 0;
4207 struct ocfs2_path
*right_path
= NULL
;
4208 struct ocfs2_path
*left_path
= NULL
;
4209 struct ocfs2_extent_list
*el
;
4211 el
= et
->et_root_el
;
4213 ret
= ocfs2_et_root_journal_access(handle
, et
,
4214 OCFS2_JOURNAL_ACCESS_WRITE
);
4220 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
4221 ocfs2_insert_at_leaf(et
, insert_rec
, el
, type
);
4222 goto out_update_clusters
;
4225 right_path
= ocfs2_new_path_from_et(et
);
4233 * Determine the path to start with. Rotations need the
4234 * rightmost path, everything else can go directly to the
4237 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4238 if (type
->ins_appending
== APPEND_NONE
&&
4239 type
->ins_contig
== CONTIG_NONE
) {
4244 ret
= ocfs2_find_path(et
->et_ci
, right_path
, cpos
);
4251 * Rotations and appends need special treatment - they modify
4252 * parts of the tree's above them.
4254 * Both might pass back a path immediate to the left of the
4255 * one being inserted to. This will be cause
4256 * ocfs2_insert_path() to modify the rightmost records of
4257 * left_path to account for an edge insert.
4259 * XXX: When modifying this code, keep in mind that an insert
4260 * can wind up skipping both of these two special cases...
4263 ret
= ocfs2_rotate_tree_right(handle
, et
, type
->ins_split
,
4264 le32_to_cpu(insert_rec
->e_cpos
),
4265 right_path
, &left_path
);
4272 * ocfs2_rotate_tree_right() might have extended the
4273 * transaction without re-journaling our tree root.
4275 ret
= ocfs2_et_root_journal_access(handle
, et
,
4276 OCFS2_JOURNAL_ACCESS_WRITE
);
4281 } else if (type
->ins_appending
== APPEND_TAIL
4282 && type
->ins_contig
!= CONTIG_LEFT
) {
4283 ret
= ocfs2_append_rec_to_path(handle
, et
, insert_rec
,
4284 right_path
, &left_path
);
4291 ret
= ocfs2_insert_path(handle
, et
, left_path
, right_path
,
4298 out_update_clusters
:
4299 if (type
->ins_split
== SPLIT_NONE
)
4300 ocfs2_et_update_clusters(et
,
4301 le16_to_cpu(insert_rec
->e_leaf_clusters
));
4303 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4308 ocfs2_free_path(left_path
);
4309 ocfs2_free_path(right_path
);
4314 static enum ocfs2_contig_type
4315 ocfs2_figure_merge_contig_type(struct inode
*inode
, struct ocfs2_path
*path
,
4316 struct ocfs2_extent_list
*el
, int index
,
4317 struct ocfs2_extent_rec
*split_rec
)
4320 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4321 u32 left_cpos
, right_cpos
;
4322 struct ocfs2_extent_rec
*rec
= NULL
;
4323 struct ocfs2_extent_list
*new_el
;
4324 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4325 struct buffer_head
*bh
;
4326 struct ocfs2_extent_block
*eb
;
4329 rec
= &el
->l_recs
[index
- 1];
4330 } else if (path
->p_tree_depth
> 0) {
4331 status
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
4336 if (left_cpos
!= 0) {
4337 left_path
= ocfs2_new_path_from_path(path
);
4341 status
= ocfs2_find_path(INODE_CACHE(inode
),
4342 left_path
, left_cpos
);
4346 new_el
= path_leaf_el(left_path
);
4348 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4349 le16_to_cpu(new_el
->l_count
)) {
4350 bh
= path_leaf_bh(left_path
);
4351 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4352 ocfs2_error(inode
->i_sb
,
4353 "Extent block #%llu has an "
4354 "invalid l_next_free_rec of "
4355 "%d. It should have "
4356 "matched the l_count of %d",
4357 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4358 le16_to_cpu(new_el
->l_next_free_rec
),
4359 le16_to_cpu(new_el
->l_count
));
4363 rec
= &new_el
->l_recs
[
4364 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4369 * We're careful to check for an empty extent record here -
4370 * the merge code will know what to do if it sees one.
4373 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4374 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4377 ret
= ocfs2_extent_contig(inode
->i_sb
, rec
, split_rec
);
4382 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4383 rec
= &el
->l_recs
[index
+ 1];
4384 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4385 path
->p_tree_depth
> 0) {
4386 status
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
,
4391 if (right_cpos
== 0)
4394 right_path
= ocfs2_new_path_from_path(path
);
4398 status
= ocfs2_find_path(INODE_CACHE(inode
), right_path
, right_cpos
);
4402 new_el
= path_leaf_el(right_path
);
4403 rec
= &new_el
->l_recs
[0];
4404 if (ocfs2_is_empty_extent(rec
)) {
4405 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4406 bh
= path_leaf_bh(right_path
);
4407 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4408 ocfs2_error(inode
->i_sb
,
4409 "Extent block #%llu has an "
4410 "invalid l_next_free_rec of %d",
4411 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4412 le16_to_cpu(new_el
->l_next_free_rec
));
4416 rec
= &new_el
->l_recs
[1];
4421 enum ocfs2_contig_type contig_type
;
4423 contig_type
= ocfs2_extent_contig(inode
->i_sb
, rec
, split_rec
);
4425 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4426 ret
= CONTIG_LEFTRIGHT
;
4427 else if (ret
== CONTIG_NONE
)
4433 ocfs2_free_path(left_path
);
4435 ocfs2_free_path(right_path
);
4440 static void ocfs2_figure_contig_type(struct inode
*inode
,
4441 struct ocfs2_insert_type
*insert
,
4442 struct ocfs2_extent_list
*el
,
4443 struct ocfs2_extent_rec
*insert_rec
,
4444 struct ocfs2_extent_tree
*et
)
4447 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4449 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4451 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4452 contig_type
= ocfs2_extent_contig(inode
->i_sb
, &el
->l_recs
[i
],
4454 if (contig_type
!= CONTIG_NONE
) {
4455 insert
->ins_contig_index
= i
;
4459 insert
->ins_contig
= contig_type
;
4461 if (insert
->ins_contig
!= CONTIG_NONE
) {
4462 struct ocfs2_extent_rec
*rec
=
4463 &el
->l_recs
[insert
->ins_contig_index
];
4464 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4465 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4468 * Caller might want us to limit the size of extents, don't
4469 * calculate contiguousness if we might exceed that limit.
4471 if (et
->et_max_leaf_clusters
&&
4472 (len
> et
->et_max_leaf_clusters
))
4473 insert
->ins_contig
= CONTIG_NONE
;
4478 * This should only be called against the righmost leaf extent list.
4480 * ocfs2_figure_appending_type() will figure out whether we'll have to
4481 * insert at the tail of the rightmost leaf.
4483 * This should also work against the root extent list for tree's with 0
4484 * depth. If we consider the root extent list to be the rightmost leaf node
4485 * then the logic here makes sense.
4487 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4488 struct ocfs2_extent_list
*el
,
4489 struct ocfs2_extent_rec
*insert_rec
)
4492 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4493 struct ocfs2_extent_rec
*rec
;
4495 insert
->ins_appending
= APPEND_NONE
;
4497 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4499 if (!el
->l_next_free_rec
)
4500 goto set_tail_append
;
4502 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4503 /* Were all records empty? */
4504 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4505 goto set_tail_append
;
4508 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4509 rec
= &el
->l_recs
[i
];
4512 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4513 goto set_tail_append
;
4518 insert
->ins_appending
= APPEND_TAIL
;
4522 * Helper function called at the begining of an insert.
4524 * This computes a few things that are commonly used in the process of
4525 * inserting into the btree:
4526 * - Whether the new extent is contiguous with an existing one.
4527 * - The current tree depth.
4528 * - Whether the insert is an appending one.
4529 * - The total # of free records in the tree.
4531 * All of the information is stored on the ocfs2_insert_type
4534 static int ocfs2_figure_insert_type(struct inode
*inode
,
4535 struct ocfs2_extent_tree
*et
,
4536 struct buffer_head
**last_eb_bh
,
4537 struct ocfs2_extent_rec
*insert_rec
,
4539 struct ocfs2_insert_type
*insert
)
4542 struct ocfs2_extent_block
*eb
;
4543 struct ocfs2_extent_list
*el
;
4544 struct ocfs2_path
*path
= NULL
;
4545 struct buffer_head
*bh
= NULL
;
4547 insert
->ins_split
= SPLIT_NONE
;
4549 el
= et
->et_root_el
;
4550 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4552 if (el
->l_tree_depth
) {
4554 * If we have tree depth, we read in the
4555 * rightmost extent block ahead of time as
4556 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4557 * may want it later.
4559 ret
= ocfs2_read_extent_block(et
->et_ci
,
4560 ocfs2_et_get_last_eb_blk(et
),
4566 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4571 * Unless we have a contiguous insert, we'll need to know if
4572 * there is room left in our allocation tree for another
4575 * XXX: This test is simplistic, we can search for empty
4576 * extent records too.
4578 *free_records
= le16_to_cpu(el
->l_count
) -
4579 le16_to_cpu(el
->l_next_free_rec
);
4581 if (!insert
->ins_tree_depth
) {
4582 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4583 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4587 path
= ocfs2_new_path_from_et(et
);
4595 * In the case that we're inserting past what the tree
4596 * currently accounts for, ocfs2_find_path() will return for
4597 * us the rightmost tree path. This is accounted for below in
4598 * the appending code.
4600 ret
= ocfs2_find_path(et
->et_ci
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4606 el
= path_leaf_el(path
);
4609 * Now that we have the path, there's two things we want to determine:
4610 * 1) Contiguousness (also set contig_index if this is so)
4612 * 2) Are we doing an append? We can trivially break this up
4613 * into two types of appends: simple record append, or a
4614 * rotate inside the tail leaf.
4616 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4619 * The insert code isn't quite ready to deal with all cases of
4620 * left contiguousness. Specifically, if it's an insert into
4621 * the 1st record in a leaf, it will require the adjustment of
4622 * cluster count on the last record of the path directly to it's
4623 * left. For now, just catch that case and fool the layers
4624 * above us. This works just fine for tree_depth == 0, which
4625 * is why we allow that above.
4627 if (insert
->ins_contig
== CONTIG_LEFT
&&
4628 insert
->ins_contig_index
== 0)
4629 insert
->ins_contig
= CONTIG_NONE
;
4632 * Ok, so we can simply compare against last_eb to figure out
4633 * whether the path doesn't exist. This will only happen in
4634 * the case that we're doing a tail append, so maybe we can
4635 * take advantage of that information somehow.
4637 if (ocfs2_et_get_last_eb_blk(et
) ==
4638 path_leaf_bh(path
)->b_blocknr
) {
4640 * Ok, ocfs2_find_path() returned us the rightmost
4641 * tree path. This might be an appending insert. There are
4643 * 1) We're doing a true append at the tail:
4644 * -This might even be off the end of the leaf
4645 * 2) We're "appending" by rotating in the tail
4647 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4651 ocfs2_free_path(path
);
4661 * Insert an extent into an inode btree.
4663 * The caller needs to update fe->i_clusters
4665 int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4667 struct inode
*inode
,
4668 struct ocfs2_extent_tree
*et
,
4673 struct ocfs2_alloc_context
*meta_ac
)
4676 int uninitialized_var(free_records
);
4677 struct buffer_head
*last_eb_bh
= NULL
;
4678 struct ocfs2_insert_type insert
= {0, };
4679 struct ocfs2_extent_rec rec
;
4681 mlog(0, "add %u clusters at position %u to inode %llu\n",
4682 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4684 memset(&rec
, 0, sizeof(rec
));
4685 rec
.e_cpos
= cpu_to_le32(cpos
);
4686 rec
.e_blkno
= cpu_to_le64(start_blk
);
4687 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4688 rec
.e_flags
= flags
;
4689 status
= ocfs2_et_insert_check(et
, &rec
);
4695 status
= ocfs2_figure_insert_type(inode
, et
, &last_eb_bh
, &rec
,
4696 &free_records
, &insert
);
4702 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4703 "Insert.contig_index: %d, Insert.free_records: %d, "
4704 "Insert.tree_depth: %d\n",
4705 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4706 free_records
, insert
.ins_tree_depth
);
4708 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4709 status
= ocfs2_grow_tree(handle
, et
,
4710 &insert
.ins_tree_depth
, &last_eb_bh
,
4718 /* Finally, we can add clusters. This might rotate the tree for us. */
4719 status
= ocfs2_do_insert_extent(handle
, et
, &rec
, &insert
);
4722 else if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4723 ocfs2_extent_map_insert_rec(inode
, &rec
);
4733 * Allcate and add clusters into the extent b-tree.
4734 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4735 * The extent b-tree's root is specified by et, and
4736 * it is not limited to the file storage. Any extent tree can use this
4737 * function if it implements the proper ocfs2_extent_tree.
4739 int ocfs2_add_clusters_in_btree(struct ocfs2_super
*osb
,
4740 struct inode
*inode
,
4741 u32
*logical_offset
,
4742 u32 clusters_to_add
,
4744 struct ocfs2_extent_tree
*et
,
4746 struct ocfs2_alloc_context
*data_ac
,
4747 struct ocfs2_alloc_context
*meta_ac
,
4748 enum ocfs2_alloc_restarted
*reason_ret
)
4752 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4753 u32 bit_off
, num_bits
;
4757 BUG_ON(!clusters_to_add
);
4760 flags
= OCFS2_EXT_UNWRITTEN
;
4762 free_extents
= ocfs2_num_free_extents(osb
, et
);
4763 if (free_extents
< 0) {
4764 status
= free_extents
;
4769 /* there are two cases which could cause us to EAGAIN in the
4770 * we-need-more-metadata case:
4771 * 1) we haven't reserved *any*
4772 * 2) we are so fragmented, we've needed to add metadata too
4774 if (!free_extents
&& !meta_ac
) {
4775 mlog(0, "we haven't reserved any metadata!\n");
4777 reason
= RESTART_META
;
4779 } else if ((!free_extents
)
4780 && (ocfs2_alloc_context_bits_left(meta_ac
)
4781 < ocfs2_extend_meta_needed(et
->et_root_el
))) {
4782 mlog(0, "filesystem is really fragmented...\n");
4784 reason
= RESTART_META
;
4788 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4789 clusters_to_add
, &bit_off
, &num_bits
);
4791 if (status
!= -ENOSPC
)
4796 BUG_ON(num_bits
> clusters_to_add
);
4798 /* reserve our write early -- insert_extent may update the tree root */
4799 status
= ocfs2_et_root_journal_access(handle
, et
,
4800 OCFS2_JOURNAL_ACCESS_WRITE
);
4806 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4807 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4808 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4809 status
= ocfs2_insert_extent(osb
, handle
, inode
, et
,
4810 *logical_offset
, block
,
4811 num_bits
, flags
, meta_ac
);
4817 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4823 clusters_to_add
-= num_bits
;
4824 *logical_offset
+= num_bits
;
4826 if (clusters_to_add
) {
4827 mlog(0, "need to alloc once more, wanted = %u\n",
4830 reason
= RESTART_TRANS
;
4836 *reason_ret
= reason
;
4840 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4841 struct ocfs2_extent_rec
*split_rec
,
4843 struct ocfs2_extent_rec
*rec
)
4845 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4846 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4848 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4850 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4851 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4853 split_rec
->e_blkno
= rec
->e_blkno
;
4854 le64_add_cpu(&split_rec
->e_blkno
,
4855 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4857 split_rec
->e_flags
= rec
->e_flags
;
4860 static int ocfs2_split_and_insert(struct inode
*inode
,
4862 struct ocfs2_path
*path
,
4863 struct ocfs2_extent_tree
*et
,
4864 struct buffer_head
**last_eb_bh
,
4866 struct ocfs2_extent_rec
*orig_split_rec
,
4867 struct ocfs2_alloc_context
*meta_ac
)
4870 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4871 struct ocfs2_extent_rec tmprec
;
4872 struct ocfs2_extent_list
*rightmost_el
;
4873 struct ocfs2_extent_rec rec
;
4874 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4875 struct ocfs2_insert_type insert
;
4876 struct ocfs2_extent_block
*eb
;
4880 * Store a copy of the record on the stack - it might move
4881 * around as the tree is manipulated below.
4883 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4885 rightmost_el
= et
->et_root_el
;
4887 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4889 BUG_ON(!(*last_eb_bh
));
4890 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4891 rightmost_el
= &eb
->h_list
;
4894 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4895 le16_to_cpu(rightmost_el
->l_count
)) {
4896 ret
= ocfs2_grow_tree(handle
, et
,
4897 &depth
, last_eb_bh
, meta_ac
);
4904 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4905 insert
.ins_appending
= APPEND_NONE
;
4906 insert
.ins_contig
= CONTIG_NONE
;
4907 insert
.ins_tree_depth
= depth
;
4909 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4910 le16_to_cpu(split_rec
.e_leaf_clusters
);
4911 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4912 le16_to_cpu(rec
.e_leaf_clusters
);
4914 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4915 insert
.ins_split
= SPLIT_LEFT
;
4916 } else if (insert_range
== rec_range
) {
4917 insert
.ins_split
= SPLIT_RIGHT
;
4920 * Left/right split. We fake this as a right split
4921 * first and then make a second pass as a left split.
4923 insert
.ins_split
= SPLIT_RIGHT
;
4925 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4930 BUG_ON(do_leftright
);
4934 ret
= ocfs2_do_insert_extent(handle
, et
, &split_rec
, &insert
);
4940 if (do_leftright
== 1) {
4942 struct ocfs2_extent_list
*el
;
4945 split_rec
= *orig_split_rec
;
4947 ocfs2_reinit_path(path
, 1);
4949 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4950 ret
= ocfs2_find_path(et
->et_ci
, path
, cpos
);
4956 el
= path_leaf_el(path
);
4957 split_index
= ocfs2_search_extent_list(el
, cpos
);
4965 static int ocfs2_replace_extent_rec(struct inode
*inode
,
4967 struct ocfs2_path
*path
,
4968 struct ocfs2_extent_list
*el
,
4970 struct ocfs2_extent_rec
*split_rec
)
4974 ret
= ocfs2_path_bh_journal_access(handle
, INODE_CACHE(inode
), path
,
4975 path_num_items(path
) - 1);
4981 el
->l_recs
[split_index
] = *split_rec
;
4983 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
4989 * Mark part or all of the extent record at split_index in the leaf
4990 * pointed to by path as written. This removes the unwritten
4993 * Care is taken to handle contiguousness so as to not grow the tree.
4995 * meta_ac is not strictly necessary - we only truly need it if growth
4996 * of the tree is required. All other cases will degrade into a less
4997 * optimal tree layout.
4999 * last_eb_bh should be the rightmost leaf block for any extent
5000 * btree. Since a split may grow the tree or a merge might shrink it,
5001 * the caller cannot trust the contents of that buffer after this call.
5003 * This code is optimized for readability - several passes might be
5004 * made over certain portions of the tree. All of those blocks will
5005 * have been brought into cache (and pinned via the journal), so the
5006 * extra overhead is not expressed in terms of disk reads.
5008 static int __ocfs2_mark_extent_written(struct inode
*inode
,
5009 struct ocfs2_extent_tree
*et
,
5011 struct ocfs2_path
*path
,
5013 struct ocfs2_extent_rec
*split_rec
,
5014 struct ocfs2_alloc_context
*meta_ac
,
5015 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5018 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5019 struct buffer_head
*last_eb_bh
= NULL
;
5020 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
5021 struct ocfs2_merge_ctxt ctxt
;
5022 struct ocfs2_extent_list
*rightmost_el
;
5024 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
5030 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
5031 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
5032 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
5038 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, path
, el
,
5043 * The core merge / split code wants to know how much room is
5044 * left in this inodes allocation tree, so we pass the
5045 * rightmost extent list.
5047 if (path
->p_tree_depth
) {
5048 struct ocfs2_extent_block
*eb
;
5050 ret
= ocfs2_read_extent_block(et
->et_ci
,
5051 ocfs2_et_get_last_eb_blk(et
),
5058 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5059 rightmost_el
= &eb
->h_list
;
5061 rightmost_el
= path_root_el(path
);
5063 if (rec
->e_cpos
== split_rec
->e_cpos
&&
5064 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
5065 ctxt
.c_split_covers_rec
= 1;
5067 ctxt
.c_split_covers_rec
= 0;
5069 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
5071 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5072 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
5073 ctxt
.c_split_covers_rec
);
5075 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
5076 if (ctxt
.c_split_covers_rec
)
5077 ret
= ocfs2_replace_extent_rec(inode
, handle
,
5079 split_index
, split_rec
);
5081 ret
= ocfs2_split_and_insert(inode
, handle
, path
, et
,
5082 &last_eb_bh
, split_index
,
5083 split_rec
, meta_ac
);
5087 ret
= ocfs2_try_to_merge_extent(handle
, et
, path
,
5088 split_index
, split_rec
,
5100 * Mark the already-existing extent at cpos as written for len clusters.
5102 * If the existing extent is larger than the request, initiate a
5103 * split. An attempt will be made at merging with adjacent extents.
5105 * The caller is responsible for passing down meta_ac if we'll need it.
5107 int ocfs2_mark_extent_written(struct inode
*inode
,
5108 struct ocfs2_extent_tree
*et
,
5109 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
5110 struct ocfs2_alloc_context
*meta_ac
,
5111 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5114 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
5115 struct ocfs2_extent_rec split_rec
;
5116 struct ocfs2_path
*left_path
= NULL
;
5117 struct ocfs2_extent_list
*el
;
5119 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5120 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
5122 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
5123 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
5124 "that are being written to, but the feature bit "
5125 "is not set in the super block.",
5126 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
5132 * XXX: This should be fixed up so that we just re-insert the
5133 * next extent records.
5135 ocfs2_et_extent_map_truncate(et
, 0);
5137 left_path
= ocfs2_new_path_from_et(et
);
5144 ret
= ocfs2_find_path(et
->et_ci
, left_path
, cpos
);
5149 el
= path_leaf_el(left_path
);
5151 index
= ocfs2_search_extent_list(el
, cpos
);
5152 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5153 ocfs2_error(inode
->i_sb
,
5154 "Inode %llu has an extent at cpos %u which can no "
5155 "longer be found.\n",
5156 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5161 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
5162 split_rec
.e_cpos
= cpu_to_le32(cpos
);
5163 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
5164 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
5165 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
5166 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
5168 ret
= __ocfs2_mark_extent_written(inode
, et
, handle
, left_path
,
5169 index
, &split_rec
, meta_ac
,
5175 ocfs2_free_path(left_path
);
5179 static int ocfs2_split_tree(struct inode
*inode
, struct ocfs2_extent_tree
*et
,
5180 handle_t
*handle
, struct ocfs2_path
*path
,
5181 int index
, u32 new_range
,
5182 struct ocfs2_alloc_context
*meta_ac
)
5184 int ret
, depth
, credits
= handle
->h_buffer_credits
;
5185 struct buffer_head
*last_eb_bh
= NULL
;
5186 struct ocfs2_extent_block
*eb
;
5187 struct ocfs2_extent_list
*rightmost_el
, *el
;
5188 struct ocfs2_extent_rec split_rec
;
5189 struct ocfs2_extent_rec
*rec
;
5190 struct ocfs2_insert_type insert
;
5193 * Setup the record to split before we grow the tree.
5195 el
= path_leaf_el(path
);
5196 rec
= &el
->l_recs
[index
];
5197 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
5199 depth
= path
->p_tree_depth
;
5201 ret
= ocfs2_read_extent_block(et
->et_ci
,
5202 ocfs2_et_get_last_eb_blk(et
),
5209 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5210 rightmost_el
= &eb
->h_list
;
5212 rightmost_el
= path_leaf_el(path
);
5214 credits
+= path
->p_tree_depth
+
5215 ocfs2_extend_meta_needed(et
->et_root_el
);
5216 ret
= ocfs2_extend_trans(handle
, credits
);
5222 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
5223 le16_to_cpu(rightmost_el
->l_count
)) {
5224 ret
= ocfs2_grow_tree(handle
, et
, &depth
, &last_eb_bh
,
5232 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
5233 insert
.ins_appending
= APPEND_NONE
;
5234 insert
.ins_contig
= CONTIG_NONE
;
5235 insert
.ins_split
= SPLIT_RIGHT
;
5236 insert
.ins_tree_depth
= depth
;
5238 ret
= ocfs2_do_insert_extent(handle
, et
, &split_rec
, &insert
);
5247 static int ocfs2_truncate_rec(handle_t
*handle
,
5248 struct ocfs2_extent_tree
*et
,
5249 struct ocfs2_path
*path
, int index
,
5250 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5254 u32 left_cpos
, rec_range
, trunc_range
;
5255 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5256 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
5257 struct ocfs2_path
*left_path
= NULL
;
5258 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5259 struct ocfs2_extent_rec
*rec
;
5260 struct ocfs2_extent_block
*eb
;
5262 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5263 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
5272 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5273 path
->p_tree_depth
) {
5275 * Check whether this is the rightmost tree record. If
5276 * we remove all of this record or part of its right
5277 * edge then an update of the record lengths above it
5280 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5281 if (eb
->h_next_leaf_blk
== 0)
5282 is_rightmost_tree_rec
= 1;
5285 rec
= &el
->l_recs
[index
];
5286 if (index
== 0 && path
->p_tree_depth
&&
5287 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5289 * Changing the leftmost offset (via partial or whole
5290 * record truncate) of an interior (or rightmost) path
5291 * means we have to update the subtree that is formed
5292 * by this leaf and the one to it's left.
5294 * There are two cases we can skip:
5295 * 1) Path is the leftmost one in our btree.
5296 * 2) The leaf is rightmost and will be empty after
5297 * we remove the extent record - the rotate code
5298 * knows how to update the newly formed edge.
5301 ret
= ocfs2_find_cpos_for_left_leaf(sb
, path
, &left_cpos
);
5307 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5308 left_path
= ocfs2_new_path_from_path(path
);
5315 ret
= ocfs2_find_path(et
->et_ci
, left_path
,
5324 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5325 handle
->h_buffer_credits
,
5332 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, path
);
5338 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, left_path
);
5344 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5345 trunc_range
= cpos
+ len
;
5347 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5350 memset(rec
, 0, sizeof(*rec
));
5351 ocfs2_cleanup_merge(el
, index
);
5354 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5355 if (is_rightmost_tree_rec
&& next_free
> 1) {
5357 * We skip the edge update if this path will
5358 * be deleted by the rotate code.
5360 rec
= &el
->l_recs
[next_free
- 1];
5361 ocfs2_adjust_rightmost_records(handle
, et
, path
,
5364 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5365 /* Remove leftmost portion of the record. */
5366 le32_add_cpu(&rec
->e_cpos
, len
);
5367 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5368 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5369 } else if (rec_range
== trunc_range
) {
5370 /* Remove rightmost portion of the record */
5371 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5372 if (is_rightmost_tree_rec
)
5373 ocfs2_adjust_rightmost_records(handle
, et
, path
, rec
);
5375 /* Caller should have trapped this. */
5376 mlog(ML_ERROR
, "Owner %llu: Invalid record truncate: (%u, %u) "
5378 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
5379 le32_to_cpu(rec
->e_cpos
),
5380 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5387 subtree_index
= ocfs2_find_subtree_root(et
, left_path
, path
);
5388 ocfs2_complete_edge_insert(handle
, left_path
, path
,
5392 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5394 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
5401 ocfs2_free_path(left_path
);
5405 int ocfs2_remove_extent(struct inode
*inode
,
5406 struct ocfs2_extent_tree
*et
,
5407 u32 cpos
, u32 len
, handle_t
*handle
,
5408 struct ocfs2_alloc_context
*meta_ac
,
5409 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5412 u32 rec_range
, trunc_range
;
5413 struct ocfs2_extent_rec
*rec
;
5414 struct ocfs2_extent_list
*el
;
5415 struct ocfs2_path
*path
= NULL
;
5418 * XXX: Why are we truncating to 0 instead of wherever this
5421 ocfs2_et_extent_map_truncate(et
, 0);
5423 path
= ocfs2_new_path_from_et(et
);
5430 ret
= ocfs2_find_path(et
->et_ci
, path
, cpos
);
5436 el
= path_leaf_el(path
);
5437 index
= ocfs2_search_extent_list(el
, cpos
);
5438 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5439 ocfs2_error(inode
->i_sb
,
5440 "Inode %llu has an extent at cpos %u which can no "
5441 "longer be found.\n",
5442 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5448 * We have 3 cases of extent removal:
5449 * 1) Range covers the entire extent rec
5450 * 2) Range begins or ends on one edge of the extent rec
5451 * 3) Range is in the middle of the extent rec (no shared edges)
5453 * For case 1 we remove the extent rec and left rotate to
5456 * For case 2 we just shrink the existing extent rec, with a
5457 * tree update if the shrinking edge is also the edge of an
5460 * For case 3 we do a right split to turn the extent rec into
5461 * something case 2 can handle.
5463 rec
= &el
->l_recs
[index
];
5464 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5465 trunc_range
= cpos
+ len
;
5467 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5469 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5470 "(cpos %u, len %u)\n",
5471 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
5472 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5474 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5475 ret
= ocfs2_truncate_rec(handle
, et
, path
, index
, dealloc
,
5482 ret
= ocfs2_split_tree(inode
, et
, handle
, path
, index
,
5483 trunc_range
, meta_ac
);
5490 * The split could have manipulated the tree enough to
5491 * move the record location, so we have to look for it again.
5493 ocfs2_reinit_path(path
, 1);
5495 ret
= ocfs2_find_path(et
->et_ci
, path
, cpos
);
5501 el
= path_leaf_el(path
);
5502 index
= ocfs2_search_extent_list(el
, cpos
);
5503 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5504 ocfs2_error(inode
->i_sb
,
5505 "Inode %llu: split at cpos %u lost record.",
5506 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5513 * Double check our values here. If anything is fishy,
5514 * it's easier to catch it at the top level.
5516 rec
= &el
->l_recs
[index
];
5517 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5518 ocfs2_rec_clusters(el
, rec
);
5519 if (rec_range
!= trunc_range
) {
5520 ocfs2_error(inode
->i_sb
,
5521 "Inode %llu: error after split at cpos %u"
5522 "trunc len %u, existing record is (%u,%u)",
5523 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5524 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5525 ocfs2_rec_clusters(el
, rec
));
5530 ret
= ocfs2_truncate_rec(handle
, et
, path
, index
, dealloc
,
5539 ocfs2_free_path(path
);
5543 int ocfs2_remove_btree_range(struct inode
*inode
,
5544 struct ocfs2_extent_tree
*et
,
5545 u32 cpos
, u32 phys_cpos
, u32 len
,
5546 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5549 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
5550 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5551 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5553 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5555 ret
= ocfs2_lock_allocators(inode
, et
, 0, 1, NULL
, &meta_ac
);
5561 mutex_lock(&tl_inode
->i_mutex
);
5563 if (ocfs2_truncate_log_needs_flush(osb
)) {
5564 ret
= __ocfs2_flush_truncate_log(osb
);
5571 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5572 if (IS_ERR(handle
)) {
5573 ret
= PTR_ERR(handle
);
5578 ret
= ocfs2_et_root_journal_access(handle
, et
,
5579 OCFS2_JOURNAL_ACCESS_WRITE
);
5585 vfs_dq_free_space_nodirty(inode
,
5586 ocfs2_clusters_to_bytes(inode
->i_sb
, len
));
5588 ret
= ocfs2_remove_extent(inode
, et
, cpos
, len
, handle
, meta_ac
,
5595 ocfs2_et_update_clusters(et
, -len
);
5597 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
5603 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
5608 ocfs2_commit_trans(osb
, handle
);
5610 mutex_unlock(&tl_inode
->i_mutex
);
5613 ocfs2_free_alloc_context(meta_ac
);
5618 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5620 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5621 struct ocfs2_dinode
*di
;
5622 struct ocfs2_truncate_log
*tl
;
5624 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5625 tl
= &di
->id2
.i_dealloc
;
5627 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5628 "slot %d, invalid truncate log parameters: used = "
5629 "%u, count = %u\n", osb
->slot_num
,
5630 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5631 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5634 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5635 unsigned int new_start
)
5637 unsigned int tail_index
;
5638 unsigned int current_tail
;
5640 /* No records, nothing to coalesce */
5641 if (!le16_to_cpu(tl
->tl_used
))
5644 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5645 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5646 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5648 return current_tail
== new_start
;
5651 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5654 unsigned int num_clusters
)
5657 unsigned int start_cluster
, tl_count
;
5658 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5659 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5660 struct ocfs2_dinode
*di
;
5661 struct ocfs2_truncate_log
*tl
;
5663 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5664 (unsigned long long)start_blk
, num_clusters
);
5666 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5668 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5670 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5672 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5673 * by the underlying call to ocfs2_read_inode_block(), so any
5674 * corruption is a code bug */
5675 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5677 tl
= &di
->id2
.i_dealloc
;
5678 tl_count
= le16_to_cpu(tl
->tl_count
);
5679 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5681 "Truncate record count on #%llu invalid "
5682 "wanted %u, actual %u\n",
5683 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5684 ocfs2_truncate_recs_per_inode(osb
->sb
),
5685 le16_to_cpu(tl
->tl_count
));
5687 /* Caller should have known to flush before calling us. */
5688 index
= le16_to_cpu(tl
->tl_used
);
5689 if (index
>= tl_count
) {
5695 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(tl_inode
), tl_bh
,
5696 OCFS2_JOURNAL_ACCESS_WRITE
);
5702 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5703 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5704 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5706 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5708 * Move index back to the record we are coalescing with.
5709 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5713 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5714 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5715 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5718 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5719 tl
->tl_used
= cpu_to_le16(index
+ 1);
5721 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5723 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5734 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5736 struct inode
*data_alloc_inode
,
5737 struct buffer_head
*data_alloc_bh
)
5741 unsigned int num_clusters
;
5743 struct ocfs2_truncate_rec rec
;
5744 struct ocfs2_dinode
*di
;
5745 struct ocfs2_truncate_log
*tl
;
5746 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5747 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5751 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5752 tl
= &di
->id2
.i_dealloc
;
5753 i
= le16_to_cpu(tl
->tl_used
) - 1;
5755 /* Caller has given us at least enough credits to
5756 * update the truncate log dinode */
5757 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(tl_inode
), tl_bh
,
5758 OCFS2_JOURNAL_ACCESS_WRITE
);
5764 tl
->tl_used
= cpu_to_le16(i
);
5766 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5772 /* TODO: Perhaps we can calculate the bulk of the
5773 * credits up front rather than extending like
5775 status
= ocfs2_extend_trans(handle
,
5776 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5782 rec
= tl
->tl_recs
[i
];
5783 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5784 le32_to_cpu(rec
.t_start
));
5785 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5787 /* if start_blk is not set, we ignore the record as
5790 mlog(0, "free record %d, start = %u, clusters = %u\n",
5791 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5793 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5794 data_alloc_bh
, start_blk
,
5809 /* Expects you to already be holding tl_inode->i_mutex */
5810 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5813 unsigned int num_to_flush
;
5815 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5816 struct inode
*data_alloc_inode
= NULL
;
5817 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5818 struct buffer_head
*data_alloc_bh
= NULL
;
5819 struct ocfs2_dinode
*di
;
5820 struct ocfs2_truncate_log
*tl
;
5824 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5826 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5828 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5829 * by the underlying call to ocfs2_read_inode_block(), so any
5830 * corruption is a code bug */
5831 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5833 tl
= &di
->id2
.i_dealloc
;
5834 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5835 mlog(0, "Flush %u records from truncate log #%llu\n",
5836 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5837 if (!num_to_flush
) {
5842 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5843 GLOBAL_BITMAP_SYSTEM_INODE
,
5844 OCFS2_INVALID_SLOT
);
5845 if (!data_alloc_inode
) {
5847 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5851 mutex_lock(&data_alloc_inode
->i_mutex
);
5853 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5859 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5860 if (IS_ERR(handle
)) {
5861 status
= PTR_ERR(handle
);
5866 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5871 ocfs2_commit_trans(osb
, handle
);
5874 brelse(data_alloc_bh
);
5875 ocfs2_inode_unlock(data_alloc_inode
, 1);
5878 mutex_unlock(&data_alloc_inode
->i_mutex
);
5879 iput(data_alloc_inode
);
5886 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5889 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5891 mutex_lock(&tl_inode
->i_mutex
);
5892 status
= __ocfs2_flush_truncate_log(osb
);
5893 mutex_unlock(&tl_inode
->i_mutex
);
5898 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5901 struct ocfs2_super
*osb
=
5902 container_of(work
, struct ocfs2_super
,
5903 osb_truncate_log_wq
.work
);
5907 status
= ocfs2_flush_truncate_log(osb
);
5911 ocfs2_init_inode_steal_slot(osb
);
5916 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5917 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5920 if (osb
->osb_tl_inode
) {
5921 /* We want to push off log flushes while truncates are
5924 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5926 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5927 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5931 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5933 struct inode
**tl_inode
,
5934 struct buffer_head
**tl_bh
)
5937 struct inode
*inode
= NULL
;
5938 struct buffer_head
*bh
= NULL
;
5940 inode
= ocfs2_get_system_file_inode(osb
,
5941 TRUNCATE_LOG_SYSTEM_INODE
,
5945 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5949 status
= ocfs2_read_inode_block(inode
, &bh
);
5963 /* called during the 1st stage of node recovery. we stamp a clean
5964 * truncate log and pass back a copy for processing later. if the
5965 * truncate log does not require processing, a *tl_copy is set to
5967 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5969 struct ocfs2_dinode
**tl_copy
)
5972 struct inode
*tl_inode
= NULL
;
5973 struct buffer_head
*tl_bh
= NULL
;
5974 struct ocfs2_dinode
*di
;
5975 struct ocfs2_truncate_log
*tl
;
5979 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5981 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5987 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5989 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5990 * validated by the underlying call to ocfs2_read_inode_block(),
5991 * so any corruption is a code bug */
5992 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5994 tl
= &di
->id2
.i_dealloc
;
5995 if (le16_to_cpu(tl
->tl_used
)) {
5996 mlog(0, "We'll have %u logs to recover\n",
5997 le16_to_cpu(tl
->tl_used
));
5999 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
6006 /* Assuming the write-out below goes well, this copy
6007 * will be passed back to recovery for processing. */
6008 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
6010 /* All we need to do to clear the truncate log is set
6014 ocfs2_compute_meta_ecc(osb
->sb
, tl_bh
->b_data
, &di
->i_check
);
6015 status
= ocfs2_write_block(osb
, tl_bh
, INODE_CACHE(tl_inode
));
6027 if (status
< 0 && (*tl_copy
)) {
6036 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
6037 struct ocfs2_dinode
*tl_copy
)
6041 unsigned int clusters
, num_recs
, start_cluster
;
6044 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6045 struct ocfs2_truncate_log
*tl
;
6049 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
6050 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
6054 tl
= &tl_copy
->id2
.i_dealloc
;
6055 num_recs
= le16_to_cpu(tl
->tl_used
);
6056 mlog(0, "cleanup %u records from %llu\n", num_recs
,
6057 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
6059 mutex_lock(&tl_inode
->i_mutex
);
6060 for(i
= 0; i
< num_recs
; i
++) {
6061 if (ocfs2_truncate_log_needs_flush(osb
)) {
6062 status
= __ocfs2_flush_truncate_log(osb
);
6069 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6070 if (IS_ERR(handle
)) {
6071 status
= PTR_ERR(handle
);
6076 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
6077 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
6078 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
6080 status
= ocfs2_truncate_log_append(osb
, handle
,
6081 start_blk
, clusters
);
6082 ocfs2_commit_trans(osb
, handle
);
6090 mutex_unlock(&tl_inode
->i_mutex
);
6096 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
6099 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6104 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
6105 flush_workqueue(ocfs2_wq
);
6107 status
= ocfs2_flush_truncate_log(osb
);
6111 brelse(osb
->osb_tl_bh
);
6112 iput(osb
->osb_tl_inode
);
6118 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
6121 struct inode
*tl_inode
= NULL
;
6122 struct buffer_head
*tl_bh
= NULL
;
6126 status
= ocfs2_get_truncate_log_info(osb
,
6133 /* ocfs2_truncate_log_shutdown keys on the existence of
6134 * osb->osb_tl_inode so we don't set any of the osb variables
6135 * until we're sure all is well. */
6136 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
6137 ocfs2_truncate_log_worker
);
6138 osb
->osb_tl_bh
= tl_bh
;
6139 osb
->osb_tl_inode
= tl_inode
;
6146 * Delayed de-allocation of suballocator blocks.
6148 * Some sets of block de-allocations might involve multiple suballocator inodes.
6150 * The locking for this can get extremely complicated, especially when
6151 * the suballocator inodes to delete from aren't known until deep
6152 * within an unrelated codepath.
6154 * ocfs2_extent_block structures are a good example of this - an inode
6155 * btree could have been grown by any number of nodes each allocating
6156 * out of their own suballoc inode.
6158 * These structures allow the delay of block de-allocation until a
6159 * later time, when locking of multiple cluster inodes won't cause
6164 * Describe a single bit freed from a suballocator. For the block
6165 * suballocators, it represents one block. For the global cluster
6166 * allocator, it represents some clusters and free_bit indicates
6169 struct ocfs2_cached_block_free
{
6170 struct ocfs2_cached_block_free
*free_next
;
6172 unsigned int free_bit
;
6175 struct ocfs2_per_slot_free_list
{
6176 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
6179 struct ocfs2_cached_block_free
*f_first
;
6182 static int ocfs2_free_cached_blocks(struct ocfs2_super
*osb
,
6185 struct ocfs2_cached_block_free
*head
)
6190 struct inode
*inode
;
6191 struct buffer_head
*di_bh
= NULL
;
6192 struct ocfs2_cached_block_free
*tmp
;
6194 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
6201 mutex_lock(&inode
->i_mutex
);
6203 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
6209 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
6210 if (IS_ERR(handle
)) {
6211 ret
= PTR_ERR(handle
);
6217 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
6219 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6220 head
->free_bit
, (unsigned long long)head
->free_blk
);
6222 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
6223 head
->free_bit
, bg_blkno
, 1);
6229 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
6236 head
= head
->free_next
;
6241 ocfs2_commit_trans(osb
, handle
);
6244 ocfs2_inode_unlock(inode
, 1);
6247 mutex_unlock(&inode
->i_mutex
);
6251 /* Premature exit may have left some dangling items. */
6253 head
= head
->free_next
;
6260 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6261 u64 blkno
, unsigned int bit
)
6264 struct ocfs2_cached_block_free
*item
;
6266 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6273 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6274 bit
, (unsigned long long)blkno
);
6276 item
->free_blk
= blkno
;
6277 item
->free_bit
= bit
;
6278 item
->free_next
= ctxt
->c_global_allocator
;
6280 ctxt
->c_global_allocator
= item
;
6284 static int ocfs2_free_cached_clusters(struct ocfs2_super
*osb
,
6285 struct ocfs2_cached_block_free
*head
)
6287 struct ocfs2_cached_block_free
*tmp
;
6288 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6292 mutex_lock(&tl_inode
->i_mutex
);
6295 if (ocfs2_truncate_log_needs_flush(osb
)) {
6296 ret
= __ocfs2_flush_truncate_log(osb
);
6303 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6304 if (IS_ERR(handle
)) {
6305 ret
= PTR_ERR(handle
);
6310 ret
= ocfs2_truncate_log_append(osb
, handle
, head
->free_blk
,
6313 ocfs2_commit_trans(osb
, handle
);
6315 head
= head
->free_next
;
6324 mutex_unlock(&tl_inode
->i_mutex
);
6327 /* Premature exit may have left some dangling items. */
6329 head
= head
->free_next
;
6336 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
6337 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6340 struct ocfs2_per_slot_free_list
*fl
;
6345 while (ctxt
->c_first_suballocator
) {
6346 fl
= ctxt
->c_first_suballocator
;
6349 mlog(0, "Free items: (type %u, slot %d)\n",
6350 fl
->f_inode_type
, fl
->f_slot
);
6351 ret2
= ocfs2_free_cached_blocks(osb
,
6361 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
6365 if (ctxt
->c_global_allocator
) {
6366 ret2
= ocfs2_free_cached_clusters(osb
,
6367 ctxt
->c_global_allocator
);
6373 ctxt
->c_global_allocator
= NULL
;
6379 static struct ocfs2_per_slot_free_list
*
6380 ocfs2_find_per_slot_free_list(int type
,
6382 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6384 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
6387 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
6390 fl
= fl
->f_next_suballocator
;
6393 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
6395 fl
->f_inode_type
= type
;
6398 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
6400 ctxt
->c_first_suballocator
= fl
;
6405 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6406 int type
, int slot
, u64 blkno
,
6410 struct ocfs2_per_slot_free_list
*fl
;
6411 struct ocfs2_cached_block_free
*item
;
6413 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6420 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6427 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6428 type
, slot
, bit
, (unsigned long long)blkno
);
6430 item
->free_blk
= blkno
;
6431 item
->free_bit
= bit
;
6432 item
->free_next
= fl
->f_first
;
6441 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6442 struct ocfs2_extent_block
*eb
)
6444 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6445 le16_to_cpu(eb
->h_suballoc_slot
),
6446 le64_to_cpu(eb
->h_blkno
),
6447 le16_to_cpu(eb
->h_suballoc_bit
));
6450 /* This function will figure out whether the currently last extent
6451 * block will be deleted, and if it will, what the new last extent
6452 * block will be so we can update his h_next_leaf_blk field, as well
6453 * as the dinodes i_last_eb_blk */
6454 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6455 unsigned int clusters_to_del
,
6456 struct ocfs2_path
*path
,
6457 struct buffer_head
**new_last_eb
)
6459 int next_free
, ret
= 0;
6461 struct ocfs2_extent_rec
*rec
;
6462 struct ocfs2_extent_block
*eb
;
6463 struct ocfs2_extent_list
*el
;
6464 struct buffer_head
*bh
= NULL
;
6466 *new_last_eb
= NULL
;
6468 /* we have no tree, so of course, no last_eb. */
6469 if (!path
->p_tree_depth
)
6472 /* trunc to zero special case - this makes tree_depth = 0
6473 * regardless of what it is. */
6474 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6477 el
= path_leaf_el(path
);
6478 BUG_ON(!el
->l_next_free_rec
);
6481 * Make sure that this extent list will actually be empty
6482 * after we clear away the data. We can shortcut out if
6483 * there's more than one non-empty extent in the
6484 * list. Otherwise, a check of the remaining extent is
6487 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6489 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6493 /* We may have a valid extent in index 1, check it. */
6495 rec
= &el
->l_recs
[1];
6498 * Fall through - no more nonempty extents, so we want
6499 * to delete this leaf.
6505 rec
= &el
->l_recs
[0];
6510 * Check it we'll only be trimming off the end of this
6513 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6517 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6523 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), path_root_el(path
), cpos
, &bh
);
6529 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6532 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6533 * Any corruption is a code bug. */
6534 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
6537 get_bh(*new_last_eb
);
6538 mlog(0, "returning block %llu, (cpos: %u)\n",
6539 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6547 * Trim some clusters off the rightmost edge of a tree. Only called
6550 * The caller needs to:
6551 * - start journaling of each path component.
6552 * - compute and fully set up any new last ext block
6554 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6555 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6556 u32 clusters_to_del
, u64
*delete_start
)
6558 int ret
, i
, index
= path
->p_tree_depth
;
6561 struct buffer_head
*bh
;
6562 struct ocfs2_extent_list
*el
;
6563 struct ocfs2_extent_rec
*rec
;
6567 while (index
>= 0) {
6568 bh
= path
->p_node
[index
].bh
;
6569 el
= path
->p_node
[index
].el
;
6571 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6572 index
, (unsigned long long)bh
->b_blocknr
);
6574 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6577 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6578 ocfs2_error(inode
->i_sb
,
6579 "Inode %lu has invalid ext. block %llu",
6581 (unsigned long long)bh
->b_blocknr
);
6587 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6588 rec
= &el
->l_recs
[i
];
6590 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6591 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6592 ocfs2_rec_clusters(el
, rec
),
6593 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6594 le16_to_cpu(el
->l_next_free_rec
));
6596 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6598 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6600 * If the leaf block contains a single empty
6601 * extent and no records, we can just remove
6604 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6606 sizeof(struct ocfs2_extent_rec
));
6607 el
->l_next_free_rec
= cpu_to_le16(0);
6613 * Remove any empty extents by shifting things
6614 * left. That should make life much easier on
6615 * the code below. This condition is rare
6616 * enough that we shouldn't see a performance
6619 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6620 le16_add_cpu(&el
->l_next_free_rec
, -1);
6623 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6624 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6626 memset(&el
->l_recs
[i
], 0,
6627 sizeof(struct ocfs2_extent_rec
));
6630 * We've modified our extent list. The
6631 * simplest way to handle this change
6632 * is to being the search from the
6635 goto find_tail_record
;
6638 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6641 * We'll use "new_edge" on our way back up the
6642 * tree to know what our rightmost cpos is.
6644 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6645 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6648 * The caller will use this to delete data blocks.
6650 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6651 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6652 le16_to_cpu(rec
->e_leaf_clusters
));
6655 * If it's now empty, remove this record.
6657 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6659 sizeof(struct ocfs2_extent_rec
));
6660 le16_add_cpu(&el
->l_next_free_rec
, -1);
6663 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6665 sizeof(struct ocfs2_extent_rec
));
6666 le16_add_cpu(&el
->l_next_free_rec
, -1);
6671 /* Can this actually happen? */
6672 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6676 * We never actually deleted any clusters
6677 * because our leaf was empty. There's no
6678 * reason to adjust the rightmost edge then.
6683 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6684 le32_add_cpu(&rec
->e_int_clusters
,
6685 -le32_to_cpu(rec
->e_cpos
));
6688 * A deleted child record should have been
6691 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6695 ret
= ocfs2_journal_dirty(handle
, bh
);
6701 mlog(0, "extent list container %llu, after: record %d: "
6702 "(%u, %u, %llu), next = %u.\n",
6703 (unsigned long long)bh
->b_blocknr
, i
,
6704 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6705 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6706 le16_to_cpu(el
->l_next_free_rec
));
6709 * We must be careful to only attempt delete of an
6710 * extent block (and not the root inode block).
6712 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6713 struct ocfs2_extent_block
*eb
=
6714 (struct ocfs2_extent_block
*)bh
->b_data
;
6717 * Save this for use when processing the
6720 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6722 mlog(0, "deleting this extent block.\n");
6724 ocfs2_remove_from_cache(INODE_CACHE(inode
), bh
);
6726 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6727 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6728 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6730 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6731 /* An error here is not fatal. */
6746 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6747 unsigned int clusters_to_del
,
6748 struct inode
*inode
,
6749 struct buffer_head
*fe_bh
,
6751 struct ocfs2_truncate_context
*tc
,
6752 struct ocfs2_path
*path
)
6755 struct ocfs2_dinode
*fe
;
6756 struct ocfs2_extent_block
*last_eb
= NULL
;
6757 struct ocfs2_extent_list
*el
;
6758 struct buffer_head
*last_eb_bh
= NULL
;
6761 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6763 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6771 * Each component will be touched, so we might as well journal
6772 * here to avoid having to handle errors later.
6774 status
= ocfs2_journal_access_path(INODE_CACHE(inode
), handle
, path
);
6781 status
= ocfs2_journal_access_eb(handle
, INODE_CACHE(inode
), last_eb_bh
,
6782 OCFS2_JOURNAL_ACCESS_WRITE
);
6788 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6791 el
= &(fe
->id2
.i_list
);
6794 * Lower levels depend on this never happening, but it's best
6795 * to check it up here before changing the tree.
6797 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6798 ocfs2_error(inode
->i_sb
,
6799 "Inode %lu has an empty extent record, depth %u\n",
6800 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6805 vfs_dq_free_space_nodirty(inode
,
6806 ocfs2_clusters_to_bytes(osb
->sb
, clusters_to_del
));
6807 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6808 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6810 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6811 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6812 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6814 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6815 clusters_to_del
, &delete_blk
);
6821 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6822 /* trunc to zero is a special case. */
6823 el
->l_tree_depth
= 0;
6824 fe
->i_last_eb_blk
= 0;
6826 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6828 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6835 /* If there will be a new last extent block, then by
6836 * definition, there cannot be any leaves to the right of
6838 last_eb
->h_next_leaf_blk
= 0;
6839 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6847 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
6861 static int ocfs2_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6863 set_buffer_uptodate(bh
);
6864 mark_buffer_dirty(bh
);
6868 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
6869 unsigned int from
, unsigned int to
,
6870 struct page
*page
, int zero
, u64
*phys
)
6872 int ret
, partial
= 0;
6874 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
6879 zero_user_segment(page
, from
, to
);
6882 * Need to set the buffers we zero'd into uptodate
6883 * here if they aren't - ocfs2_map_page_blocks()
6884 * might've skipped some
6886 ret
= walk_page_buffers(handle
, page_buffers(page
),
6891 else if (ocfs2_should_order_data(inode
)) {
6892 ret
= ocfs2_jbd2_file_inode(handle
, inode
);
6898 SetPageUptodate(page
);
6900 flush_dcache_page(page
);
6903 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
6904 loff_t end
, struct page
**pages
,
6905 int numpages
, u64 phys
, handle_t
*handle
)
6909 unsigned int from
, to
= PAGE_CACHE_SIZE
;
6910 struct super_block
*sb
= inode
->i_sb
;
6912 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
6917 to
= PAGE_CACHE_SIZE
;
6918 for(i
= 0; i
< numpages
; i
++) {
6921 from
= start
& (PAGE_CACHE_SIZE
- 1);
6922 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6923 to
= end
& (PAGE_CACHE_SIZE
- 1);
6925 BUG_ON(from
> PAGE_CACHE_SIZE
);
6926 BUG_ON(to
> PAGE_CACHE_SIZE
);
6928 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6931 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6935 ocfs2_unlock_and_free_pages(pages
, numpages
);
6938 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6939 struct page
**pages
, int *num
)
6941 int numpages
, ret
= 0;
6942 struct super_block
*sb
= inode
->i_sb
;
6943 struct address_space
*mapping
= inode
->i_mapping
;
6944 unsigned long index
;
6945 loff_t last_page_bytes
;
6947 BUG_ON(start
> end
);
6949 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6950 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6953 last_page_bytes
= PAGE_ALIGN(end
);
6954 index
= start
>> PAGE_CACHE_SHIFT
;
6956 pages
[numpages
] = grab_cache_page(mapping
, index
);
6957 if (!pages
[numpages
]) {
6965 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6970 ocfs2_unlock_and_free_pages(pages
, numpages
);
6980 * Zero the area past i_size but still within an allocated
6981 * cluster. This avoids exposing nonzero data on subsequent file
6984 * We need to call this before i_size is updated on the inode because
6985 * otherwise block_write_full_page() will skip writeout of pages past
6986 * i_size. The new_i_size parameter is passed for this reason.
6988 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6989 u64 range_start
, u64 range_end
)
6991 int ret
= 0, numpages
;
6992 struct page
**pages
= NULL
;
6994 unsigned int ext_flags
;
6995 struct super_block
*sb
= inode
->i_sb
;
6998 * File systems which don't support sparse files zero on every
7001 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
7004 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
7005 sizeof(struct page
*), GFP_NOFS
);
7006 if (pages
== NULL
) {
7012 if (range_start
== range_end
)
7015 ret
= ocfs2_extent_map_get_blocks(inode
,
7016 range_start
>> sb
->s_blocksize_bits
,
7017 &phys
, NULL
, &ext_flags
);
7024 * Tail is a hole, or is marked unwritten. In either case, we
7025 * can count on read and write to return/push zero's.
7027 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
7030 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
7037 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
7038 numpages
, phys
, handle
);
7041 * Initiate writeout of the pages we zero'd here. We don't
7042 * wait on them - the truncate_inode_pages() call later will
7045 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
7046 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
7057 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
7058 struct ocfs2_dinode
*di
)
7060 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
7061 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
7063 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
7064 memset(&di
->id2
, 0, blocksize
-
7065 offsetof(struct ocfs2_dinode
, id2
) -
7068 memset(&di
->id2
, 0, blocksize
-
7069 offsetof(struct ocfs2_dinode
, id2
));
7072 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
7073 struct ocfs2_dinode
*di
)
7075 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
7076 di
->id2
.i_list
.l_tree_depth
= 0;
7077 di
->id2
.i_list
.l_next_free_rec
= 0;
7078 di
->id2
.i_list
.l_count
= cpu_to_le16(
7079 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
7082 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
7084 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
7085 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7087 spin_lock(&oi
->ip_lock
);
7088 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
7089 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
7090 spin_unlock(&oi
->ip_lock
);
7093 * We clear the entire i_data structure here so that all
7094 * fields can be properly initialized.
7096 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
7098 idata
->id_count
= cpu_to_le16(
7099 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
7102 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
7103 struct buffer_head
*di_bh
)
7105 int ret
, i
, has_data
, num_pages
= 0;
7107 u64
uninitialized_var(block
);
7108 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
7109 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7110 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7111 struct ocfs2_alloc_context
*data_ac
= NULL
;
7112 struct page
**pages
= NULL
;
7113 loff_t end
= osb
->s_clustersize
;
7114 struct ocfs2_extent_tree et
;
7117 has_data
= i_size_read(inode
) ? 1 : 0;
7120 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
7121 sizeof(struct page
*), GFP_NOFS
);
7122 if (pages
== NULL
) {
7128 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
7135 handle
= ocfs2_start_trans(osb
,
7136 ocfs2_inline_to_extents_credits(osb
->sb
));
7137 if (IS_ERR(handle
)) {
7138 ret
= PTR_ERR(handle
);
7143 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
7144 OCFS2_JOURNAL_ACCESS_WRITE
);
7152 unsigned int page_end
;
7155 if (vfs_dq_alloc_space_nodirty(inode
,
7156 ocfs2_clusters_to_bytes(osb
->sb
, 1))) {
7162 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
7170 * Save two copies, one for insert, and one that can
7171 * be changed by ocfs2_map_and_dirty_page() below.
7173 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
7176 * Non sparse file systems zero on extend, so no need
7179 if (!ocfs2_sparse_alloc(osb
) &&
7180 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
7181 end
= PAGE_CACHE_SIZE
;
7183 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
7190 * This should populate the 1st page for us and mark
7193 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
7199 page_end
= PAGE_CACHE_SIZE
;
7200 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
7201 page_end
= osb
->s_clustersize
;
7203 for (i
= 0; i
< num_pages
; i
++)
7204 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
7205 pages
[i
], i
> 0, &phys
);
7208 spin_lock(&oi
->ip_lock
);
7209 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
7210 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
7211 spin_unlock(&oi
->ip_lock
);
7213 ocfs2_dinode_new_extent_list(inode
, di
);
7215 ocfs2_journal_dirty(handle
, di_bh
);
7219 * An error at this point should be extremely rare. If
7220 * this proves to be false, we could always re-build
7221 * the in-inode data from our pages.
7223 ocfs2_init_dinode_extent_tree(&et
, inode
, di_bh
);
7224 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
,
7225 0, block
, 1, 0, NULL
);
7231 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7235 if (ret
< 0 && did_quota
)
7236 vfs_dq_free_space_nodirty(inode
,
7237 ocfs2_clusters_to_bytes(osb
->sb
, 1));
7239 ocfs2_commit_trans(osb
, handle
);
7243 ocfs2_free_alloc_context(data_ac
);
7247 ocfs2_unlock_and_free_pages(pages
, num_pages
);
7255 * It is expected, that by the time you call this function,
7256 * inode->i_size and fe->i_size have been adjusted.
7258 * WARNING: This will kfree the truncate context
7260 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
7261 struct inode
*inode
,
7262 struct buffer_head
*fe_bh
,
7263 struct ocfs2_truncate_context
*tc
)
7265 int status
, i
, credits
, tl_sem
= 0;
7266 u32 clusters_to_del
, new_highest_cpos
, range
;
7267 struct ocfs2_extent_list
*el
;
7268 handle_t
*handle
= NULL
;
7269 struct inode
*tl_inode
= osb
->osb_tl_inode
;
7270 struct ocfs2_path
*path
= NULL
;
7271 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
7275 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
7276 i_size_read(inode
));
7278 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
,
7279 ocfs2_journal_access_di
);
7286 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
7290 * Check that we still have allocation to delete.
7292 if (OCFS2_I(inode
)->ip_clusters
== 0) {
7298 * Truncate always works against the rightmost tree branch.
7300 status
= ocfs2_find_path(INODE_CACHE(inode
), path
, UINT_MAX
);
7306 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7307 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
7310 * By now, el will point to the extent list on the bottom most
7311 * portion of this tree. Only the tail record is considered in
7314 * We handle the following cases, in order:
7315 * - empty extent: delete the remaining branch
7316 * - remove the entire record
7317 * - remove a partial record
7318 * - no record needs to be removed (truncate has completed)
7320 el
= path_leaf_el(path
);
7321 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
7322 ocfs2_error(inode
->i_sb
,
7323 "Inode %llu has empty extent block at %llu\n",
7324 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7325 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7330 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
7331 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
7332 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7333 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
7334 clusters_to_del
= 0;
7335 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
7336 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7337 } else if (range
> new_highest_cpos
) {
7338 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
7339 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
7346 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7347 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7349 mutex_lock(&tl_inode
->i_mutex
);
7351 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7352 * record is free for use. If there isn't any, we flush to get
7353 * an empty truncate log. */
7354 if (ocfs2_truncate_log_needs_flush(osb
)) {
7355 status
= __ocfs2_flush_truncate_log(osb
);
7362 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
7363 (struct ocfs2_dinode
*)fe_bh
->b_data
,
7365 handle
= ocfs2_start_trans(osb
, credits
);
7366 if (IS_ERR(handle
)) {
7367 status
= PTR_ERR(handle
);
7373 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
7380 mutex_unlock(&tl_inode
->i_mutex
);
7383 ocfs2_commit_trans(osb
, handle
);
7386 ocfs2_reinit_path(path
, 1);
7389 * The check above will catch the case where we've truncated
7390 * away all allocation.
7396 ocfs2_schedule_truncate_log_flush(osb
, 1);
7399 mutex_unlock(&tl_inode
->i_mutex
);
7402 ocfs2_commit_trans(osb
, handle
);
7404 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
7406 ocfs2_free_path(path
);
7408 /* This will drop the ext_alloc cluster lock for us */
7409 ocfs2_free_truncate_context(tc
);
7416 * Expects the inode to already be locked.
7418 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
7419 struct inode
*inode
,
7420 struct buffer_head
*fe_bh
,
7421 struct ocfs2_truncate_context
**tc
)
7424 unsigned int new_i_clusters
;
7425 struct ocfs2_dinode
*fe
;
7426 struct ocfs2_extent_block
*eb
;
7427 struct buffer_head
*last_eb_bh
= NULL
;
7433 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7434 i_size_read(inode
));
7435 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7437 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7438 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7439 (unsigned long long)le64_to_cpu(fe
->i_size
));
7441 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7447 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7449 if (fe
->id2
.i_list
.l_tree_depth
) {
7450 status
= ocfs2_read_extent_block(INODE_CACHE(inode
),
7451 le64_to_cpu(fe
->i_last_eb_blk
),
7457 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7460 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7466 ocfs2_free_truncate_context(*tc
);
7474 * 'start' is inclusive, 'end' is not.
7476 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7477 unsigned int start
, unsigned int end
, int trunc
)
7480 unsigned int numbytes
;
7482 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7483 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7484 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7486 if (end
> i_size_read(inode
))
7487 end
= i_size_read(inode
);
7489 BUG_ON(start
>= end
);
7491 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7492 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7493 !ocfs2_supports_inline_data(osb
)) {
7494 ocfs2_error(inode
->i_sb
,
7495 "Inline data flags for inode %llu don't agree! "
7496 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7497 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7498 le16_to_cpu(di
->i_dyn_features
),
7499 OCFS2_I(inode
)->ip_dyn_features
,
7500 osb
->s_feature_incompat
);
7505 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7506 if (IS_ERR(handle
)) {
7507 ret
= PTR_ERR(handle
);
7512 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
7513 OCFS2_JOURNAL_ACCESS_WRITE
);
7519 numbytes
= end
- start
;
7520 memset(idata
->id_data
+ start
, 0, numbytes
);
7523 * No need to worry about the data page here - it's been
7524 * truncated already and inline data doesn't need it for
7525 * pushing zero's to disk, so we'll let readpage pick it up
7529 i_size_write(inode
, start
);
7530 di
->i_size
= cpu_to_le64(start
);
7533 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7534 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7536 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7537 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7539 ocfs2_journal_dirty(handle
, di_bh
);
7542 ocfs2_commit_trans(osb
, handle
);
7548 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7551 * The caller is responsible for completing deallocation
7552 * before freeing the context.
7554 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7556 "Truncate completion has non-empty dealloc context\n");
7558 brelse(tc
->tc_last_eb_bh
);