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"
52 #include "refcounttree.h"
54 #include "buffer_head_io.h"
56 enum ocfs2_contig_type
{
63 static enum ocfs2_contig_type
64 ocfs2_extent_rec_contig(struct super_block
*sb
,
65 struct ocfs2_extent_rec
*ext
,
66 struct ocfs2_extent_rec
*insert_rec
);
68 * Operations for a specific extent tree type.
70 * To implement an on-disk btree (extent tree) type in ocfs2, add
71 * an ocfs2_extent_tree_operations structure and the matching
72 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
73 * for the allocation portion of the extent tree.
75 struct ocfs2_extent_tree_operations
{
77 * last_eb_blk is the block number of the right most leaf extent
78 * block. Most on-disk structures containing an extent tree store
79 * this value for fast access. The ->eo_set_last_eb_blk() and
80 * ->eo_get_last_eb_blk() operations access this value. They are
83 void (*eo_set_last_eb_blk
)(struct ocfs2_extent_tree
*et
,
85 u64 (*eo_get_last_eb_blk
)(struct ocfs2_extent_tree
*et
);
88 * The on-disk structure usually keeps track of how many total
89 * clusters are stored in this extent tree. This function updates
90 * that value. new_clusters is the delta, and must be
91 * added to the total. Required.
93 void (*eo_update_clusters
)(struct ocfs2_extent_tree
*et
,
97 * If this extent tree is supported by an extent map, insert
98 * a record into the map.
100 void (*eo_extent_map_insert
)(struct ocfs2_extent_tree
*et
,
101 struct ocfs2_extent_rec
*rec
);
104 * If this extent tree is supported by an extent map, truncate the
107 void (*eo_extent_map_truncate
)(struct ocfs2_extent_tree
*et
,
111 * If ->eo_insert_check() exists, it is called before rec is
112 * inserted into the extent tree. It is optional.
114 int (*eo_insert_check
)(struct ocfs2_extent_tree
*et
,
115 struct ocfs2_extent_rec
*rec
);
116 int (*eo_sanity_check
)(struct ocfs2_extent_tree
*et
);
119 * --------------------------------------------------------------
120 * The remaining are internal to ocfs2_extent_tree and don't have
125 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
128 void (*eo_fill_root_el
)(struct ocfs2_extent_tree
*et
);
131 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
132 * it exists. If it does not, et->et_max_leaf_clusters is set
133 * to 0 (unlimited). Optional.
135 void (*eo_fill_max_leaf_clusters
)(struct ocfs2_extent_tree
*et
);
138 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
139 * are contiguous or not. Optional. Don't need to set it if use
140 * ocfs2_extent_rec as the tree leaf.
142 enum ocfs2_contig_type
143 (*eo_extent_contig
)(struct ocfs2_extent_tree
*et
,
144 struct ocfs2_extent_rec
*ext
,
145 struct ocfs2_extent_rec
*insert_rec
);
150 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
153 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
);
154 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
156 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree
*et
,
158 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree
*et
,
159 struct ocfs2_extent_rec
*rec
);
160 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree
*et
,
162 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree
*et
,
163 struct ocfs2_extent_rec
*rec
);
164 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree
*et
);
165 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
);
166 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops
= {
167 .eo_set_last_eb_blk
= ocfs2_dinode_set_last_eb_blk
,
168 .eo_get_last_eb_blk
= ocfs2_dinode_get_last_eb_blk
,
169 .eo_update_clusters
= ocfs2_dinode_update_clusters
,
170 .eo_extent_map_insert
= ocfs2_dinode_extent_map_insert
,
171 .eo_extent_map_truncate
= ocfs2_dinode_extent_map_truncate
,
172 .eo_insert_check
= ocfs2_dinode_insert_check
,
173 .eo_sanity_check
= ocfs2_dinode_sanity_check
,
174 .eo_fill_root_el
= ocfs2_dinode_fill_root_el
,
177 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
180 struct ocfs2_dinode
*di
= et
->et_object
;
182 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
183 di
->i_last_eb_blk
= cpu_to_le64(blkno
);
186 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
188 struct ocfs2_dinode
*di
= et
->et_object
;
190 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
191 return le64_to_cpu(di
->i_last_eb_blk
);
194 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree
*et
,
197 struct ocfs2_inode_info
*oi
= cache_info_to_inode(et
->et_ci
);
198 struct ocfs2_dinode
*di
= et
->et_object
;
200 le32_add_cpu(&di
->i_clusters
, clusters
);
201 spin_lock(&oi
->ip_lock
);
202 oi
->ip_clusters
= le32_to_cpu(di
->i_clusters
);
203 spin_unlock(&oi
->ip_lock
);
206 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree
*et
,
207 struct ocfs2_extent_rec
*rec
)
209 struct inode
*inode
= &cache_info_to_inode(et
->et_ci
)->vfs_inode
;
211 ocfs2_extent_map_insert_rec(inode
, rec
);
214 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree
*et
,
217 struct inode
*inode
= &cache_info_to_inode(et
->et_ci
)->vfs_inode
;
219 ocfs2_extent_map_trunc(inode
, clusters
);
222 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree
*et
,
223 struct ocfs2_extent_rec
*rec
)
225 struct ocfs2_inode_info
*oi
= cache_info_to_inode(et
->et_ci
);
226 struct ocfs2_super
*osb
= OCFS2_SB(oi
->vfs_inode
.i_sb
);
228 BUG_ON(oi
->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
229 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
230 (oi
->ip_clusters
!= le32_to_cpu(rec
->e_cpos
)),
231 "Device %s, asking for sparse allocation: inode %llu, "
232 "cpos %u, clusters %u\n",
234 (unsigned long long)oi
->ip_blkno
,
235 rec
->e_cpos
, oi
->ip_clusters
);
240 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree
*et
)
242 struct ocfs2_dinode
*di
= et
->et_object
;
244 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
245 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
250 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
)
252 struct ocfs2_dinode
*di
= et
->et_object
;
254 et
->et_root_el
= &di
->id2
.i_list
;
258 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree
*et
)
260 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
262 et
->et_root_el
= &vb
->vb_xv
->xr_list
;
265 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
268 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
270 vb
->vb_xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
273 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
275 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
277 return le64_to_cpu(vb
->vb_xv
->xr_last_eb_blk
);
280 static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree
*et
,
283 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
285 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, clusters
);
288 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops
= {
289 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
290 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
291 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
292 .eo_fill_root_el
= ocfs2_xattr_value_fill_root_el
,
295 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree
*et
)
297 struct ocfs2_xattr_block
*xb
= et
->et_object
;
299 et
->et_root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
302 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree
*et
)
304 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
305 et
->et_max_leaf_clusters
=
306 ocfs2_clusters_for_bytes(sb
, OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
309 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
312 struct ocfs2_xattr_block
*xb
= et
->et_object
;
313 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
315 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
318 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
320 struct ocfs2_xattr_block
*xb
= et
->et_object
;
321 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
323 return le64_to_cpu(xt
->xt_last_eb_blk
);
326 static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree
*et
,
329 struct ocfs2_xattr_block
*xb
= et
->et_object
;
331 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
334 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
335 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
336 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
337 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
338 .eo_fill_root_el
= ocfs2_xattr_tree_fill_root_el
,
339 .eo_fill_max_leaf_clusters
= ocfs2_xattr_tree_fill_max_leaf_clusters
,
342 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
345 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
347 dx_root
->dr_last_eb_blk
= cpu_to_le64(blkno
);
350 static u64
ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
352 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
354 return le64_to_cpu(dx_root
->dr_last_eb_blk
);
357 static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree
*et
,
360 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
362 le32_add_cpu(&dx_root
->dr_clusters
, clusters
);
365 static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree
*et
)
367 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
369 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root
));
374 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree
*et
)
376 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
378 et
->et_root_el
= &dx_root
->dr_list
;
381 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops
= {
382 .eo_set_last_eb_blk
= ocfs2_dx_root_set_last_eb_blk
,
383 .eo_get_last_eb_blk
= ocfs2_dx_root_get_last_eb_blk
,
384 .eo_update_clusters
= ocfs2_dx_root_update_clusters
,
385 .eo_sanity_check
= ocfs2_dx_root_sanity_check
,
386 .eo_fill_root_el
= ocfs2_dx_root_fill_root_el
,
389 static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree
*et
)
391 struct ocfs2_refcount_block
*rb
= et
->et_object
;
393 et
->et_root_el
= &rb
->rf_list
;
396 static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
399 struct ocfs2_refcount_block
*rb
= et
->et_object
;
401 rb
->rf_last_eb_blk
= cpu_to_le64(blkno
);
404 static u64
ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
406 struct ocfs2_refcount_block
*rb
= et
->et_object
;
408 return le64_to_cpu(rb
->rf_last_eb_blk
);
411 static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree
*et
,
414 struct ocfs2_refcount_block
*rb
= et
->et_object
;
416 le32_add_cpu(&rb
->rf_clusters
, clusters
);
419 static enum ocfs2_contig_type
420 ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree
*et
,
421 struct ocfs2_extent_rec
*ext
,
422 struct ocfs2_extent_rec
*insert_rec
)
427 static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops
= {
428 .eo_set_last_eb_blk
= ocfs2_refcount_tree_set_last_eb_blk
,
429 .eo_get_last_eb_blk
= ocfs2_refcount_tree_get_last_eb_blk
,
430 .eo_update_clusters
= ocfs2_refcount_tree_update_clusters
,
431 .eo_fill_root_el
= ocfs2_refcount_tree_fill_root_el
,
432 .eo_extent_contig
= ocfs2_refcount_tree_extent_contig
,
435 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree
*et
,
436 struct ocfs2_caching_info
*ci
,
437 struct buffer_head
*bh
,
438 ocfs2_journal_access_func access
,
440 struct ocfs2_extent_tree_operations
*ops
)
445 et
->et_root_journal_access
= access
;
447 obj
= (void *)bh
->b_data
;
450 et
->et_ops
->eo_fill_root_el(et
);
451 if (!et
->et_ops
->eo_fill_max_leaf_clusters
)
452 et
->et_max_leaf_clusters
= 0;
454 et
->et_ops
->eo_fill_max_leaf_clusters(et
);
457 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree
*et
,
458 struct ocfs2_caching_info
*ci
,
459 struct buffer_head
*bh
)
461 __ocfs2_init_extent_tree(et
, ci
, bh
, ocfs2_journal_access_di
,
462 NULL
, &ocfs2_dinode_et_ops
);
465 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree
*et
,
466 struct ocfs2_caching_info
*ci
,
467 struct buffer_head
*bh
)
469 __ocfs2_init_extent_tree(et
, ci
, bh
, ocfs2_journal_access_xb
,
470 NULL
, &ocfs2_xattr_tree_et_ops
);
473 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree
*et
,
474 struct ocfs2_caching_info
*ci
,
475 struct ocfs2_xattr_value_buf
*vb
)
477 __ocfs2_init_extent_tree(et
, ci
, vb
->vb_bh
, vb
->vb_access
, vb
,
478 &ocfs2_xattr_value_et_ops
);
481 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree
*et
,
482 struct ocfs2_caching_info
*ci
,
483 struct buffer_head
*bh
)
485 __ocfs2_init_extent_tree(et
, ci
, bh
, ocfs2_journal_access_dr
,
486 NULL
, &ocfs2_dx_root_et_ops
);
489 void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree
*et
,
490 struct ocfs2_caching_info
*ci
,
491 struct buffer_head
*bh
)
493 __ocfs2_init_extent_tree(et
, ci
, bh
, ocfs2_journal_access_rb
,
494 NULL
, &ocfs2_refcount_tree_et_ops
);
497 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
500 et
->et_ops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
503 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
505 return et
->et_ops
->eo_get_last_eb_blk(et
);
508 static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree
*et
,
511 et
->et_ops
->eo_update_clusters(et
, clusters
);
514 static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree
*et
,
515 struct ocfs2_extent_rec
*rec
)
517 if (et
->et_ops
->eo_extent_map_insert
)
518 et
->et_ops
->eo_extent_map_insert(et
, rec
);
521 static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree
*et
,
524 if (et
->et_ops
->eo_extent_map_truncate
)
525 et
->et_ops
->eo_extent_map_truncate(et
, clusters
);
528 static inline int ocfs2_et_root_journal_access(handle_t
*handle
,
529 struct ocfs2_extent_tree
*et
,
532 return et
->et_root_journal_access(handle
, et
->et_ci
, et
->et_root_bh
,
536 static inline enum ocfs2_contig_type
537 ocfs2_et_extent_contig(struct ocfs2_extent_tree
*et
,
538 struct ocfs2_extent_rec
*rec
,
539 struct ocfs2_extent_rec
*insert_rec
)
541 if (et
->et_ops
->eo_extent_contig
)
542 return et
->et_ops
->eo_extent_contig(et
, rec
, insert_rec
);
544 return ocfs2_extent_rec_contig(
545 ocfs2_metadata_cache_get_super(et
->et_ci
),
549 static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree
*et
,
550 struct ocfs2_extent_rec
*rec
)
554 if (et
->et_ops
->eo_insert_check
)
555 ret
= et
->et_ops
->eo_insert_check(et
, rec
);
559 static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree
*et
)
563 if (et
->et_ops
->eo_sanity_check
)
564 ret
= et
->et_ops
->eo_sanity_check(et
);
568 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
569 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
570 struct ocfs2_extent_block
*eb
);
571 static void ocfs2_adjust_rightmost_records(handle_t
*handle
,
572 struct ocfs2_extent_tree
*et
,
573 struct ocfs2_path
*path
,
574 struct ocfs2_extent_rec
*insert_rec
);
576 * Reset the actual path elements so that we can re-use the structure
577 * to build another path. Generally, this involves freeing the buffer
580 void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
582 int i
, start
= 0, depth
= 0;
583 struct ocfs2_path_item
*node
;
588 for(i
= start
; i
< path_num_items(path
); i
++) {
589 node
= &path
->p_node
[i
];
597 * Tree depth may change during truncate, or insert. If we're
598 * keeping the root extent list, then make sure that our path
599 * structure reflects the proper depth.
602 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
604 path_root_access(path
) = NULL
;
606 path
->p_tree_depth
= depth
;
609 void ocfs2_free_path(struct ocfs2_path
*path
)
612 ocfs2_reinit_path(path
, 0);
618 * All the elements of src into dest. After this call, src could be freed
619 * without affecting dest.
621 * Both paths should have the same root. Any non-root elements of dest
624 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
628 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
629 BUG_ON(path_root_el(dest
) != path_root_el(src
));
630 BUG_ON(path_root_access(dest
) != path_root_access(src
));
632 ocfs2_reinit_path(dest
, 1);
634 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
635 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
636 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
638 if (dest
->p_node
[i
].bh
)
639 get_bh(dest
->p_node
[i
].bh
);
644 * Make the *dest path the same as src and re-initialize src path to
647 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
651 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
652 BUG_ON(path_root_access(dest
) != path_root_access(src
));
654 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
655 brelse(dest
->p_node
[i
].bh
);
657 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
658 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
660 src
->p_node
[i
].bh
= NULL
;
661 src
->p_node
[i
].el
= NULL
;
666 * Insert an extent block at given index.
668 * This will not take an additional reference on eb_bh.
670 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
671 struct buffer_head
*eb_bh
)
673 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
676 * Right now, no root bh is an extent block, so this helps
677 * catch code errors with dinode trees. The assertion can be
678 * safely removed if we ever need to insert extent block
679 * structures at the root.
683 path
->p_node
[index
].bh
= eb_bh
;
684 path
->p_node
[index
].el
= &eb
->h_list
;
687 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
688 struct ocfs2_extent_list
*root_el
,
689 ocfs2_journal_access_func access
)
691 struct ocfs2_path
*path
;
693 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
695 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
697 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
699 path_root_bh(path
) = root_bh
;
700 path_root_el(path
) = root_el
;
701 path_root_access(path
) = access
;
707 struct ocfs2_path
*ocfs2_new_path_from_path(struct ocfs2_path
*path
)
709 return ocfs2_new_path(path_root_bh(path
), path_root_el(path
),
710 path_root_access(path
));
713 struct ocfs2_path
*ocfs2_new_path_from_et(struct ocfs2_extent_tree
*et
)
715 return ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
,
716 et
->et_root_journal_access
);
720 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
721 * otherwise it's the root_access function.
723 * I don't like the way this function's name looks next to
724 * ocfs2_journal_access_path(), but I don't have a better one.
726 int ocfs2_path_bh_journal_access(handle_t
*handle
,
727 struct ocfs2_caching_info
*ci
,
728 struct ocfs2_path
*path
,
731 ocfs2_journal_access_func access
= path_root_access(path
);
734 access
= ocfs2_journal_access
;
737 access
= ocfs2_journal_access_eb
;
739 return access(handle
, ci
, path
->p_node
[idx
].bh
,
740 OCFS2_JOURNAL_ACCESS_WRITE
);
744 * Convenience function to journal all components in a path.
746 int ocfs2_journal_access_path(struct ocfs2_caching_info
*ci
,
748 struct ocfs2_path
*path
)
755 for(i
= 0; i
< path_num_items(path
); i
++) {
756 ret
= ocfs2_path_bh_journal_access(handle
, ci
, path
, i
);
768 * Return the index of the extent record which contains cluster #v_cluster.
769 * -1 is returned if it was not found.
771 * Should work fine on interior and exterior nodes.
773 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
777 struct ocfs2_extent_rec
*rec
;
778 u32 rec_end
, rec_start
, clusters
;
780 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
781 rec
= &el
->l_recs
[i
];
783 rec_start
= le32_to_cpu(rec
->e_cpos
);
784 clusters
= ocfs2_rec_clusters(el
, rec
);
786 rec_end
= rec_start
+ clusters
;
788 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
798 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
799 * ocfs2_extent_rec_contig only work properly against leaf nodes!
801 static int ocfs2_block_extent_contig(struct super_block
*sb
,
802 struct ocfs2_extent_rec
*ext
,
805 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
807 blk_end
+= ocfs2_clusters_to_blocks(sb
,
808 le16_to_cpu(ext
->e_leaf_clusters
));
810 return blkno
== blk_end
;
813 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
814 struct ocfs2_extent_rec
*right
)
818 left_range
= le32_to_cpu(left
->e_cpos
) +
819 le16_to_cpu(left
->e_leaf_clusters
);
821 return (left_range
== le32_to_cpu(right
->e_cpos
));
824 static enum ocfs2_contig_type
825 ocfs2_extent_rec_contig(struct super_block
*sb
,
826 struct ocfs2_extent_rec
*ext
,
827 struct ocfs2_extent_rec
*insert_rec
)
829 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
832 * Refuse to coalesce extent records with different flag
833 * fields - we don't want to mix unwritten extents with user
836 if (ext
->e_flags
!= insert_rec
->e_flags
)
839 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
840 ocfs2_block_extent_contig(sb
, ext
, blkno
))
843 blkno
= le64_to_cpu(ext
->e_blkno
);
844 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
845 ocfs2_block_extent_contig(sb
, insert_rec
, blkno
))
852 * NOTE: We can have pretty much any combination of contiguousness and
855 * The usefulness of APPEND_TAIL is more in that it lets us know that
856 * we'll have to update the path to that leaf.
858 enum ocfs2_append_type
{
863 enum ocfs2_split_type
{
869 struct ocfs2_insert_type
{
870 enum ocfs2_split_type ins_split
;
871 enum ocfs2_append_type ins_appending
;
872 enum ocfs2_contig_type ins_contig
;
873 int ins_contig_index
;
877 struct ocfs2_merge_ctxt
{
878 enum ocfs2_contig_type c_contig_type
;
879 int c_has_empty_extent
;
880 int c_split_covers_rec
;
883 static int ocfs2_validate_extent_block(struct super_block
*sb
,
884 struct buffer_head
*bh
)
887 struct ocfs2_extent_block
*eb
=
888 (struct ocfs2_extent_block
*)bh
->b_data
;
890 mlog(0, "Validating extent block %llu\n",
891 (unsigned long long)bh
->b_blocknr
);
893 BUG_ON(!buffer_uptodate(bh
));
896 * If the ecc fails, we return the error but otherwise
897 * leave the filesystem running. We know any error is
898 * local to this block.
900 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &eb
->h_check
);
902 mlog(ML_ERROR
, "Checksum failed for extent block %llu\n",
903 (unsigned long long)bh
->b_blocknr
);
908 * Errors after here are fatal.
911 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
913 "Extent block #%llu has bad signature %.*s",
914 (unsigned long long)bh
->b_blocknr
, 7,
919 if (le64_to_cpu(eb
->h_blkno
) != bh
->b_blocknr
) {
921 "Extent block #%llu has an invalid h_blkno "
923 (unsigned long long)bh
->b_blocknr
,
924 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
928 if (le32_to_cpu(eb
->h_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
930 "Extent block #%llu has an invalid "
931 "h_fs_generation of #%u",
932 (unsigned long long)bh
->b_blocknr
,
933 le32_to_cpu(eb
->h_fs_generation
));
940 int ocfs2_read_extent_block(struct ocfs2_caching_info
*ci
, u64 eb_blkno
,
941 struct buffer_head
**bh
)
944 struct buffer_head
*tmp
= *bh
;
946 rc
= ocfs2_read_block(ci
, eb_blkno
, &tmp
,
947 ocfs2_validate_extent_block
);
949 /* If ocfs2_read_block() got us a new bh, pass it up. */
958 * How many free extents have we got before we need more meta data?
960 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
961 struct ocfs2_extent_tree
*et
)
964 struct ocfs2_extent_list
*el
= NULL
;
965 struct ocfs2_extent_block
*eb
;
966 struct buffer_head
*eb_bh
= NULL
;
972 last_eb_blk
= ocfs2_et_get_last_eb_blk(et
);
975 retval
= ocfs2_read_extent_block(et
->et_ci
, last_eb_blk
,
981 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
985 BUG_ON(el
->l_tree_depth
!= 0);
987 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
995 /* expects array to already be allocated
997 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
1000 static int ocfs2_create_new_meta_bhs(handle_t
*handle
,
1001 struct ocfs2_extent_tree
*et
,
1003 struct ocfs2_alloc_context
*meta_ac
,
1004 struct buffer_head
*bhs
[])
1006 int count
, status
, i
;
1007 u16 suballoc_bit_start
;
1010 struct ocfs2_super
*osb
=
1011 OCFS2_SB(ocfs2_metadata_cache_get_super(et
->et_ci
));
1012 struct ocfs2_extent_block
*eb
;
1017 while (count
< wanted
) {
1018 status
= ocfs2_claim_metadata(osb
,
1022 &suballoc_bit_start
,
1030 for(i
= count
; i
< (num_got
+ count
); i
++) {
1031 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
1032 if (bhs
[i
] == NULL
) {
1037 ocfs2_set_new_buffer_uptodate(et
->et_ci
, bhs
[i
]);
1039 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
,
1041 OCFS2_JOURNAL_ACCESS_CREATE
);
1047 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
1048 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
1049 /* Ok, setup the minimal stuff here. */
1050 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
1051 eb
->h_blkno
= cpu_to_le64(first_blkno
);
1052 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
1053 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
1054 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1055 eb
->h_list
.l_count
=
1056 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
1058 suballoc_bit_start
++;
1061 /* We'll also be dirtied by the caller, so
1062 * this isn't absolutely necessary. */
1063 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
1076 for(i
= 0; i
< wanted
; i
++) {
1086 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1088 * Returns the sum of the rightmost extent rec logical offset and
1091 * ocfs2_add_branch() uses this to determine what logical cluster
1092 * value should be populated into the leftmost new branch records.
1094 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1095 * value for the new topmost tree record.
1097 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
1101 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1103 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
1104 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
1108 * Change range of the branches in the right most path according to the leaf
1109 * extent block's rightmost record.
1111 static int ocfs2_adjust_rightmost_branch(handle_t
*handle
,
1112 struct ocfs2_extent_tree
*et
)
1115 struct ocfs2_path
*path
= NULL
;
1116 struct ocfs2_extent_list
*el
;
1117 struct ocfs2_extent_rec
*rec
;
1119 path
= ocfs2_new_path_from_et(et
);
1125 status
= ocfs2_find_path(et
->et_ci
, path
, UINT_MAX
);
1131 status
= ocfs2_extend_trans(handle
, path_num_items(path
) +
1132 handle
->h_buffer_credits
);
1138 status
= ocfs2_journal_access_path(et
->et_ci
, handle
, path
);
1144 el
= path_leaf_el(path
);
1145 rec
= &el
->l_recs
[le32_to_cpu(el
->l_next_free_rec
) - 1];
1147 ocfs2_adjust_rightmost_records(handle
, et
, path
, rec
);
1150 ocfs2_free_path(path
);
1155 * Add an entire tree branch to our inode. eb_bh is the extent block
1156 * to start at, if we don't want to start the branch at the root
1159 * last_eb_bh is required as we have to update it's next_leaf pointer
1160 * for the new last extent block.
1162 * the new branch will be 'empty' in the sense that every block will
1163 * contain a single record with cluster count == 0.
1165 static int ocfs2_add_branch(handle_t
*handle
,
1166 struct ocfs2_extent_tree
*et
,
1167 struct buffer_head
*eb_bh
,
1168 struct buffer_head
**last_eb_bh
,
1169 struct ocfs2_alloc_context
*meta_ac
)
1171 int status
, new_blocks
, i
;
1172 u64 next_blkno
, new_last_eb_blk
;
1173 struct buffer_head
*bh
;
1174 struct buffer_head
**new_eb_bhs
= NULL
;
1175 struct ocfs2_extent_block
*eb
;
1176 struct ocfs2_extent_list
*eb_el
;
1177 struct ocfs2_extent_list
*el
;
1178 u32 new_cpos
, root_end
;
1182 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
1185 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
1188 el
= et
->et_root_el
;
1190 /* we never add a branch to a leaf. */
1191 BUG_ON(!el
->l_tree_depth
);
1193 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
1195 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
1196 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
1197 root_end
= ocfs2_sum_rightmost_rec(et
->et_root_el
);
1200 * If there is a gap before the root end and the real end
1201 * of the righmost leaf block, we need to remove the gap
1202 * between new_cpos and root_end first so that the tree
1203 * is consistent after we add a new branch(it will start
1206 if (root_end
> new_cpos
) {
1207 mlog(0, "adjust the cluster end from %u to %u\n",
1208 root_end
, new_cpos
);
1209 status
= ocfs2_adjust_rightmost_branch(handle
, et
);
1216 /* allocate the number of new eb blocks we need */
1217 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
1225 status
= ocfs2_create_new_meta_bhs(handle
, et
, new_blocks
,
1226 meta_ac
, new_eb_bhs
);
1232 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1233 * linked with the rest of the tree.
1234 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1236 * when we leave the loop, new_last_eb_blk will point to the
1237 * newest leaf, and next_blkno will point to the topmost extent
1239 next_blkno
= new_last_eb_blk
= 0;
1240 for(i
= 0; i
< new_blocks
; i
++) {
1242 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1243 /* ocfs2_create_new_meta_bhs() should create it right! */
1244 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1245 eb_el
= &eb
->h_list
;
1247 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
, bh
,
1248 OCFS2_JOURNAL_ACCESS_CREATE
);
1254 eb
->h_next_leaf_blk
= 0;
1255 eb_el
->l_tree_depth
= cpu_to_le16(i
);
1256 eb_el
->l_next_free_rec
= cpu_to_le16(1);
1258 * This actually counts as an empty extent as
1261 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
1262 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
1264 * eb_el isn't always an interior node, but even leaf
1265 * nodes want a zero'd flags and reserved field so
1266 * this gets the whole 32 bits regardless of use.
1268 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
1269 if (!eb_el
->l_tree_depth
)
1270 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
1272 status
= ocfs2_journal_dirty(handle
, bh
);
1278 next_blkno
= le64_to_cpu(eb
->h_blkno
);
1281 /* This is a bit hairy. We want to update up to three blocks
1282 * here without leaving any of them in an inconsistent state
1283 * in case of error. We don't have to worry about
1284 * journal_dirty erroring as it won't unless we've aborted the
1285 * handle (in which case we would never be here) so reserving
1286 * the write with journal_access is all we need to do. */
1287 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
, *last_eb_bh
,
1288 OCFS2_JOURNAL_ACCESS_WRITE
);
1293 status
= ocfs2_et_root_journal_access(handle
, et
,
1294 OCFS2_JOURNAL_ACCESS_WRITE
);
1300 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
, eb_bh
,
1301 OCFS2_JOURNAL_ACCESS_WRITE
);
1308 /* Link the new branch into the rest of the tree (el will
1309 * either be on the root_bh, or the extent block passed in. */
1310 i
= le16_to_cpu(el
->l_next_free_rec
);
1311 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
1312 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1313 el
->l_recs
[i
].e_int_clusters
= 0;
1314 le16_add_cpu(&el
->l_next_free_rec
, 1);
1316 /* fe needs a new last extent block pointer, as does the
1317 * next_leaf on the previously last-extent-block. */
1318 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
1320 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
1321 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
1323 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
1326 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1330 status
= ocfs2_journal_dirty(handle
, eb_bh
);
1336 * Some callers want to track the rightmost leaf so pass it
1339 brelse(*last_eb_bh
);
1340 get_bh(new_eb_bhs
[0]);
1341 *last_eb_bh
= new_eb_bhs
[0];
1346 for (i
= 0; i
< new_blocks
; i
++)
1347 brelse(new_eb_bhs
[i
]);
1356 * adds another level to the allocation tree.
1357 * returns back the new extent block so you can add a branch to it
1360 static int ocfs2_shift_tree_depth(handle_t
*handle
,
1361 struct ocfs2_extent_tree
*et
,
1362 struct ocfs2_alloc_context
*meta_ac
,
1363 struct buffer_head
**ret_new_eb_bh
)
1367 struct buffer_head
*new_eb_bh
= NULL
;
1368 struct ocfs2_extent_block
*eb
;
1369 struct ocfs2_extent_list
*root_el
;
1370 struct ocfs2_extent_list
*eb_el
;
1374 status
= ocfs2_create_new_meta_bhs(handle
, et
, 1, meta_ac
,
1381 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
1382 /* ocfs2_create_new_meta_bhs() should create it right! */
1383 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1385 eb_el
= &eb
->h_list
;
1386 root_el
= et
->et_root_el
;
1388 status
= ocfs2_journal_access_eb(handle
, et
->et_ci
, new_eb_bh
,
1389 OCFS2_JOURNAL_ACCESS_CREATE
);
1395 /* copy the root extent list data into the new extent block */
1396 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
1397 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
1398 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1399 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1401 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1407 status
= ocfs2_et_root_journal_access(handle
, et
,
1408 OCFS2_JOURNAL_ACCESS_WRITE
);
1414 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1416 /* update root_bh now */
1417 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1418 root_el
->l_recs
[0].e_cpos
= 0;
1419 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1420 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1421 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1422 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1423 root_el
->l_next_free_rec
= cpu_to_le16(1);
1425 /* If this is our 1st tree depth shift, then last_eb_blk
1426 * becomes the allocated extent block */
1427 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1428 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1430 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1436 *ret_new_eb_bh
= new_eb_bh
;
1447 * Should only be called when there is no space left in any of the
1448 * leaf nodes. What we want to do is find the lowest tree depth
1449 * non-leaf extent block with room for new records. There are three
1450 * valid results of this search:
1452 * 1) a lowest extent block is found, then we pass it back in
1453 * *lowest_eb_bh and return '0'
1455 * 2) the search fails to find anything, but the root_el has room. We
1456 * pass NULL back in *lowest_eb_bh, but still return '0'
1458 * 3) the search fails to find anything AND the root_el is full, in
1459 * which case we return > 0
1461 * return status < 0 indicates an error.
1463 static int ocfs2_find_branch_target(struct ocfs2_extent_tree
*et
,
1464 struct buffer_head
**target_bh
)
1468 struct ocfs2_extent_block
*eb
;
1469 struct ocfs2_extent_list
*el
;
1470 struct buffer_head
*bh
= NULL
;
1471 struct buffer_head
*lowest_bh
= NULL
;
1477 el
= et
->et_root_el
;
1479 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1480 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1481 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
1482 "Owner %llu has empty "
1483 "extent list (next_free_rec == 0)",
1484 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
));
1488 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1489 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1491 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
1492 "Owner %llu has extent "
1493 "list where extent # %d has no physical "
1495 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
), i
);
1503 status
= ocfs2_read_extent_block(et
->et_ci
, blkno
, &bh
);
1509 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1512 if (le16_to_cpu(el
->l_next_free_rec
) <
1513 le16_to_cpu(el
->l_count
)) {
1520 /* If we didn't find one and the fe doesn't have any room,
1521 * then return '1' */
1522 el
= et
->et_root_el
;
1523 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1526 *target_bh
= lowest_bh
;
1535 * Grow a b-tree so that it has more records.
1537 * We might shift the tree depth in which case existing paths should
1538 * be considered invalid.
1540 * Tree depth after the grow is returned via *final_depth.
1542 * *last_eb_bh will be updated by ocfs2_add_branch().
1544 static int ocfs2_grow_tree(handle_t
*handle
, struct ocfs2_extent_tree
*et
,
1545 int *final_depth
, struct buffer_head
**last_eb_bh
,
1546 struct ocfs2_alloc_context
*meta_ac
)
1549 struct ocfs2_extent_list
*el
= et
->et_root_el
;
1550 int depth
= le16_to_cpu(el
->l_tree_depth
);
1551 struct buffer_head
*bh
= NULL
;
1553 BUG_ON(meta_ac
== NULL
);
1555 shift
= ocfs2_find_branch_target(et
, &bh
);
1562 /* We traveled all the way to the bottom of the allocation tree
1563 * and didn't find room for any more extents - we need to add
1564 * another tree level */
1567 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1569 /* ocfs2_shift_tree_depth will return us a buffer with
1570 * the new extent block (so we can pass that to
1571 * ocfs2_add_branch). */
1572 ret
= ocfs2_shift_tree_depth(handle
, et
, meta_ac
, &bh
);
1580 * Special case: we have room now if we shifted from
1581 * tree_depth 0, so no more work needs to be done.
1583 * We won't be calling add_branch, so pass
1584 * back *last_eb_bh as the new leaf. At depth
1585 * zero, it should always be null so there's
1586 * no reason to brelse.
1588 BUG_ON(*last_eb_bh
);
1595 /* call ocfs2_add_branch to add the final part of the tree with
1597 mlog(0, "add branch. bh = %p\n", bh
);
1598 ret
= ocfs2_add_branch(handle
, et
, bh
, last_eb_bh
,
1607 *final_depth
= depth
;
1613 * This function will discard the rightmost extent record.
1615 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1617 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1618 int count
= le16_to_cpu(el
->l_count
);
1619 unsigned int num_bytes
;
1622 /* This will cause us to go off the end of our extent list. */
1623 BUG_ON(next_free
>= count
);
1625 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1627 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1630 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1631 struct ocfs2_extent_rec
*insert_rec
)
1633 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1634 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1635 struct ocfs2_extent_rec
*rec
;
1637 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1638 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1642 /* The tree code before us didn't allow enough room in the leaf. */
1643 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1646 * The easiest way to approach this is to just remove the
1647 * empty extent and temporarily decrement next_free.
1651 * If next_free was 1 (only an empty extent), this
1652 * loop won't execute, which is fine. We still want
1653 * the decrement above to happen.
1655 for(i
= 0; i
< (next_free
- 1); i
++)
1656 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1662 * Figure out what the new record index should be.
1664 for(i
= 0; i
< next_free
; i
++) {
1665 rec
= &el
->l_recs
[i
];
1667 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1672 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1673 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1675 BUG_ON(insert_index
< 0);
1676 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1677 BUG_ON(insert_index
> next_free
);
1680 * No need to memmove if we're just adding to the tail.
1682 if (insert_index
!= next_free
) {
1683 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1685 num_bytes
= next_free
- insert_index
;
1686 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1687 memmove(&el
->l_recs
[insert_index
+ 1],
1688 &el
->l_recs
[insert_index
],
1693 * Either we had an empty extent, and need to re-increment or
1694 * there was no empty extent on a non full rightmost leaf node,
1695 * in which case we still need to increment.
1698 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1700 * Make sure none of the math above just messed up our tree.
1702 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1704 el
->l_recs
[insert_index
] = *insert_rec
;
1708 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1710 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1712 BUG_ON(num_recs
== 0);
1714 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1716 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1717 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1718 memset(&el
->l_recs
[num_recs
], 0,
1719 sizeof(struct ocfs2_extent_rec
));
1720 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1725 * Create an empty extent record .
1727 * l_next_free_rec may be updated.
1729 * If an empty extent already exists do nothing.
1731 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1733 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1735 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1740 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1743 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1744 "Asked to create an empty extent in a full list:\n"
1745 "count = %u, tree depth = %u",
1746 le16_to_cpu(el
->l_count
),
1747 le16_to_cpu(el
->l_tree_depth
));
1749 ocfs2_shift_records_right(el
);
1752 le16_add_cpu(&el
->l_next_free_rec
, 1);
1753 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1757 * For a rotation which involves two leaf nodes, the "root node" is
1758 * the lowest level tree node which contains a path to both leafs. This
1759 * resulting set of information can be used to form a complete "subtree"
1761 * This function is passed two full paths from the dinode down to a
1762 * pair of adjacent leaves. It's task is to figure out which path
1763 * index contains the subtree root - this can be the root index itself
1764 * in a worst-case rotation.
1766 * The array index of the subtree root is passed back.
1768 int ocfs2_find_subtree_root(struct ocfs2_extent_tree
*et
,
1769 struct ocfs2_path
*left
,
1770 struct ocfs2_path
*right
)
1775 * Check that the caller passed in two paths from the same tree.
1777 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1783 * The caller didn't pass two adjacent paths.
1785 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1786 "Owner %llu, left depth %u, right depth %u\n"
1787 "left leaf blk %llu, right leaf blk %llu\n",
1788 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
1789 left
->p_tree_depth
, right
->p_tree_depth
,
1790 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1791 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1792 } while (left
->p_node
[i
].bh
->b_blocknr
==
1793 right
->p_node
[i
].bh
->b_blocknr
);
1798 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1801 * Traverse a btree path in search of cpos, starting at root_el.
1803 * This code can be called with a cpos larger than the tree, in which
1804 * case it will return the rightmost path.
1806 static int __ocfs2_find_path(struct ocfs2_caching_info
*ci
,
1807 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1808 path_insert_t
*func
, void *data
)
1813 struct buffer_head
*bh
= NULL
;
1814 struct ocfs2_extent_block
*eb
;
1815 struct ocfs2_extent_list
*el
;
1816 struct ocfs2_extent_rec
*rec
;
1819 while (el
->l_tree_depth
) {
1820 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1821 ocfs2_error(ocfs2_metadata_cache_get_super(ci
),
1822 "Owner %llu has empty extent list at "
1824 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1825 le16_to_cpu(el
->l_tree_depth
));
1831 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1832 rec
= &el
->l_recs
[i
];
1835 * In the case that cpos is off the allocation
1836 * tree, this should just wind up returning the
1839 range
= le32_to_cpu(rec
->e_cpos
) +
1840 ocfs2_rec_clusters(el
, rec
);
1841 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1845 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1847 ocfs2_error(ocfs2_metadata_cache_get_super(ci
),
1848 "Owner %llu has bad blkno in extent list "
1849 "at depth %u (index %d)\n",
1850 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1851 le16_to_cpu(el
->l_tree_depth
), i
);
1858 ret
= ocfs2_read_extent_block(ci
, blkno
, &bh
);
1864 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1867 if (le16_to_cpu(el
->l_next_free_rec
) >
1868 le16_to_cpu(el
->l_count
)) {
1869 ocfs2_error(ocfs2_metadata_cache_get_super(ci
),
1870 "Owner %llu has bad count in extent list "
1871 "at block %llu (next free=%u, count=%u)\n",
1872 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1873 (unsigned long long)bh
->b_blocknr
,
1874 le16_to_cpu(el
->l_next_free_rec
),
1875 le16_to_cpu(el
->l_count
));
1886 * Catch any trailing bh that the loop didn't handle.
1894 * Given an initialized path (that is, it has a valid root extent
1895 * list), this function will traverse the btree in search of the path
1896 * which would contain cpos.
1898 * The path traveled is recorded in the path structure.
1900 * Note that this will not do any comparisons on leaf node extent
1901 * records, so it will work fine in the case that we just added a tree
1904 struct find_path_data
{
1906 struct ocfs2_path
*path
;
1908 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1910 struct find_path_data
*fp
= data
;
1913 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1916 int ocfs2_find_path(struct ocfs2_caching_info
*ci
,
1917 struct ocfs2_path
*path
, u32 cpos
)
1919 struct find_path_data data
;
1923 return __ocfs2_find_path(ci
, path_root_el(path
), cpos
,
1924 find_path_ins
, &data
);
1927 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1929 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1930 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1931 struct buffer_head
**ret
= data
;
1933 /* We want to retain only the leaf block. */
1934 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1940 * Find the leaf block in the tree which would contain cpos. No
1941 * checking of the actual leaf is done.
1943 * Some paths want to call this instead of allocating a path structure
1944 * and calling ocfs2_find_path().
1946 * This function doesn't handle non btree extent lists.
1948 int ocfs2_find_leaf(struct ocfs2_caching_info
*ci
,
1949 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1950 struct buffer_head
**leaf_bh
)
1953 struct buffer_head
*bh
= NULL
;
1955 ret
= __ocfs2_find_path(ci
, root_el
, cpos
, find_leaf_ins
, &bh
);
1967 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1969 * Basically, we've moved stuff around at the bottom of the tree and
1970 * we need to fix up the extent records above the changes to reflect
1973 * left_rec: the record on the left.
1974 * left_child_el: is the child list pointed to by left_rec
1975 * right_rec: the record to the right of left_rec
1976 * right_child_el: is the child list pointed to by right_rec
1978 * By definition, this only works on interior nodes.
1980 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1981 struct ocfs2_extent_list
*left_child_el
,
1982 struct ocfs2_extent_rec
*right_rec
,
1983 struct ocfs2_extent_list
*right_child_el
)
1985 u32 left_clusters
, right_end
;
1988 * Interior nodes never have holes. Their cpos is the cpos of
1989 * the leftmost record in their child list. Their cluster
1990 * count covers the full theoretical range of their child list
1991 * - the range between their cpos and the cpos of the record
1992 * immediately to their right.
1994 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1995 if (!ocfs2_rec_clusters(right_child_el
, &right_child_el
->l_recs
[0])) {
1996 BUG_ON(right_child_el
->l_tree_depth
);
1997 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1998 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
2000 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
2001 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
2004 * Calculate the rightmost cluster count boundary before
2005 * moving cpos - we will need to adjust clusters after
2006 * updating e_cpos to keep the same highest cluster count.
2008 right_end
= le32_to_cpu(right_rec
->e_cpos
);
2009 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
2011 right_rec
->e_cpos
= left_rec
->e_cpos
;
2012 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
2014 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
2015 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
2019 * Adjust the adjacent root node records involved in a
2020 * rotation. left_el_blkno is passed in as a key so that we can easily
2021 * find it's index in the root list.
2023 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
2024 struct ocfs2_extent_list
*left_el
,
2025 struct ocfs2_extent_list
*right_el
,
2030 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
2031 le16_to_cpu(left_el
->l_tree_depth
));
2033 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
2034 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
2039 * The path walking code should have never returned a root and
2040 * two paths which are not adjacent.
2042 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
2044 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
2045 &root_el
->l_recs
[i
+ 1], right_el
);
2049 * We've changed a leaf block (in right_path) and need to reflect that
2050 * change back up the subtree.
2052 * This happens in multiple places:
2053 * - When we've moved an extent record from the left path leaf to the right
2054 * path leaf to make room for an empty extent in the left path leaf.
2055 * - When our insert into the right path leaf is at the leftmost edge
2056 * and requires an update of the path immediately to it's left. This
2057 * can occur at the end of some types of rotation and appending inserts.
2058 * - When we've adjusted the last extent record in the left path leaf and the
2059 * 1st extent record in the right path leaf during cross extent block merge.
2061 static void ocfs2_complete_edge_insert(handle_t
*handle
,
2062 struct ocfs2_path
*left_path
,
2063 struct ocfs2_path
*right_path
,
2067 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
2068 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
2069 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2072 * Update the counts and position values within all the
2073 * interior nodes to reflect the leaf rotation we just did.
2075 * The root node is handled below the loop.
2077 * We begin the loop with right_el and left_el pointing to the
2078 * leaf lists and work our way up.
2080 * NOTE: within this loop, left_el and right_el always refer
2081 * to the *child* lists.
2083 left_el
= path_leaf_el(left_path
);
2084 right_el
= path_leaf_el(right_path
);
2085 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
2086 mlog(0, "Adjust records at index %u\n", i
);
2089 * One nice property of knowing that all of these
2090 * nodes are below the root is that we only deal with
2091 * the leftmost right node record and the rightmost
2094 el
= left_path
->p_node
[i
].el
;
2095 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
2096 left_rec
= &el
->l_recs
[idx
];
2098 el
= right_path
->p_node
[i
].el
;
2099 right_rec
= &el
->l_recs
[0];
2101 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
2104 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
2108 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
2113 * Setup our list pointers now so that the current
2114 * parents become children in the next iteration.
2116 left_el
= left_path
->p_node
[i
].el
;
2117 right_el
= right_path
->p_node
[i
].el
;
2121 * At the root node, adjust the two adjacent records which
2122 * begin our path to the leaves.
2125 el
= left_path
->p_node
[subtree_index
].el
;
2126 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
2127 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
2129 ocfs2_adjust_root_records(el
, left_el
, right_el
,
2130 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
2132 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2134 ret
= ocfs2_journal_dirty(handle
, root_bh
);
2139 static int ocfs2_rotate_subtree_right(handle_t
*handle
,
2140 struct ocfs2_extent_tree
*et
,
2141 struct ocfs2_path
*left_path
,
2142 struct ocfs2_path
*right_path
,
2146 struct buffer_head
*right_leaf_bh
;
2147 struct buffer_head
*left_leaf_bh
= NULL
;
2148 struct buffer_head
*root_bh
;
2149 struct ocfs2_extent_list
*right_el
, *left_el
;
2150 struct ocfs2_extent_rec move_rec
;
2152 left_leaf_bh
= path_leaf_bh(left_path
);
2153 left_el
= path_leaf_el(left_path
);
2155 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
2156 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
2157 "Inode %llu has non-full interior leaf node %llu"
2159 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
2160 (unsigned long long)left_leaf_bh
->b_blocknr
,
2161 le16_to_cpu(left_el
->l_next_free_rec
));
2166 * This extent block may already have an empty record, so we
2167 * return early if so.
2169 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
2172 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2173 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2175 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
2182 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2183 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2190 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2198 right_leaf_bh
= path_leaf_bh(right_path
);
2199 right_el
= path_leaf_el(right_path
);
2201 /* This is a code error, not a disk corruption. */
2202 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
2203 "because rightmost leaf block %llu is empty\n",
2204 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
2205 (unsigned long long)right_leaf_bh
->b_blocknr
);
2207 ocfs2_create_empty_extent(right_el
);
2209 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
2215 /* Do the copy now. */
2216 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
2217 move_rec
= left_el
->l_recs
[i
];
2218 right_el
->l_recs
[0] = move_rec
;
2221 * Clear out the record we just copied and shift everything
2222 * over, leaving an empty extent in the left leaf.
2224 * We temporarily subtract from next_free_rec so that the
2225 * shift will lose the tail record (which is now defunct).
2227 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
2228 ocfs2_shift_records_right(left_el
);
2229 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2230 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
2232 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
2238 ocfs2_complete_edge_insert(handle
, left_path
, right_path
,
2246 * Given a full path, determine what cpos value would return us a path
2247 * containing the leaf immediately to the left of the current one.
2249 * Will return zero if the path passed in is already the leftmost path.
2251 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
2252 struct ocfs2_path
*path
, u32
*cpos
)
2256 struct ocfs2_extent_list
*el
;
2258 BUG_ON(path
->p_tree_depth
== 0);
2262 blkno
= path_leaf_bh(path
)->b_blocknr
;
2264 /* Start at the tree node just above the leaf and work our way up. */
2265 i
= path
->p_tree_depth
- 1;
2267 el
= path
->p_node
[i
].el
;
2270 * Find the extent record just before the one in our
2273 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2274 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2278 * We've determined that the
2279 * path specified is already
2280 * the leftmost one - return a
2286 * The leftmost record points to our
2287 * leaf - we need to travel up the
2293 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
2294 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
2295 &el
->l_recs
[j
- 1]);
2302 * If we got here, we never found a valid node where
2303 * the tree indicated one should be.
2306 "Invalid extent tree at extent block %llu\n",
2307 (unsigned long long)blkno
);
2312 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2321 * Extend the transaction by enough credits to complete the rotation,
2322 * and still leave at least the original number of credits allocated
2323 * to this transaction.
2325 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
2327 struct ocfs2_path
*path
)
2330 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
2332 if (handle
->h_buffer_credits
< credits
) {
2333 ret
= ocfs2_extend_trans(handle
,
2334 credits
- handle
->h_buffer_credits
);
2338 if (unlikely(handle
->h_buffer_credits
< credits
))
2339 return ocfs2_extend_trans(handle
, credits
);
2346 * Trap the case where we're inserting into the theoretical range past
2347 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2348 * whose cpos is less than ours into the right leaf.
2350 * It's only necessary to look at the rightmost record of the left
2351 * leaf because the logic that calls us should ensure that the
2352 * theoretical ranges in the path components above the leaves are
2355 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
2358 struct ocfs2_extent_list
*left_el
;
2359 struct ocfs2_extent_rec
*rec
;
2362 left_el
= path_leaf_el(left_path
);
2363 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
2364 rec
= &left_el
->l_recs
[next_free
- 1];
2366 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
2371 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
2373 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
2375 struct ocfs2_extent_rec
*rec
;
2380 rec
= &el
->l_recs
[0];
2381 if (ocfs2_is_empty_extent(rec
)) {
2385 rec
= &el
->l_recs
[1];
2388 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2389 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2395 * Rotate all the records in a btree right one record, starting at insert_cpos.
2397 * The path to the rightmost leaf should be passed in.
2399 * The array is assumed to be large enough to hold an entire path (tree depth).
2401 * Upon successful return from this function:
2403 * - The 'right_path' array will contain a path to the leaf block
2404 * whose range contains e_cpos.
2405 * - That leaf block will have a single empty extent in list index 0.
2406 * - In the case that the rotation requires a post-insert update,
2407 * *ret_left_path will contain a valid path which can be passed to
2408 * ocfs2_insert_path().
2410 static int ocfs2_rotate_tree_right(handle_t
*handle
,
2411 struct ocfs2_extent_tree
*et
,
2412 enum ocfs2_split_type split
,
2414 struct ocfs2_path
*right_path
,
2415 struct ocfs2_path
**ret_left_path
)
2417 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2419 struct ocfs2_path
*left_path
= NULL
;
2420 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
2422 *ret_left_path
= NULL
;
2424 left_path
= ocfs2_new_path_from_path(right_path
);
2431 ret
= ocfs2_find_cpos_for_left_leaf(sb
, right_path
, &cpos
);
2437 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2440 * What we want to do here is:
2442 * 1) Start with the rightmost path.
2444 * 2) Determine a path to the leaf block directly to the left
2447 * 3) Determine the 'subtree root' - the lowest level tree node
2448 * which contains a path to both leaves.
2450 * 4) Rotate the subtree.
2452 * 5) Find the next subtree by considering the left path to be
2453 * the new right path.
2455 * The check at the top of this while loop also accepts
2456 * insert_cpos == cpos because cpos is only a _theoretical_
2457 * value to get us the left path - insert_cpos might very well
2458 * be filling that hole.
2460 * Stop at a cpos of '0' because we either started at the
2461 * leftmost branch (i.e., a tree with one branch and a
2462 * rotation inside of it), or we've gone as far as we can in
2463 * rotating subtrees.
2465 while (cpos
&& insert_cpos
<= cpos
) {
2466 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2469 ret
= ocfs2_find_path(et
->et_ci
, left_path
, cpos
);
2475 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2476 path_leaf_bh(right_path
),
2477 "Owner %llu: error during insert of %u "
2478 "(left path cpos %u) results in two identical "
2479 "paths ending at %llu\n",
2480 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
2482 (unsigned long long)
2483 path_leaf_bh(left_path
)->b_blocknr
);
2485 if (split
== SPLIT_NONE
&&
2486 ocfs2_rotate_requires_path_adjustment(left_path
,
2490 * We've rotated the tree as much as we
2491 * should. The rest is up to
2492 * ocfs2_insert_path() to complete, after the
2493 * record insertion. We indicate this
2494 * situation by returning the left path.
2496 * The reason we don't adjust the records here
2497 * before the record insert is that an error
2498 * later might break the rule where a parent
2499 * record e_cpos will reflect the actual
2500 * e_cpos of the 1st nonempty record of the
2503 *ret_left_path
= left_path
;
2507 start
= ocfs2_find_subtree_root(et
, left_path
, right_path
);
2509 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2511 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2512 right_path
->p_tree_depth
);
2514 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2515 orig_credits
, right_path
);
2521 ret
= ocfs2_rotate_subtree_right(handle
, et
, left_path
,
2528 if (split
!= SPLIT_NONE
&&
2529 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2532 * A rotate moves the rightmost left leaf
2533 * record over to the leftmost right leaf
2534 * slot. If we're doing an extent split
2535 * instead of a real insert, then we have to
2536 * check that the extent to be split wasn't
2537 * just moved over. If it was, then we can
2538 * exit here, passing left_path back -
2539 * ocfs2_split_extent() is smart enough to
2540 * search both leaves.
2542 *ret_left_path
= left_path
;
2547 * There is no need to re-read the next right path
2548 * as we know that it'll be our current left
2549 * path. Optimize by copying values instead.
2551 ocfs2_mv_path(right_path
, left_path
);
2553 ret
= ocfs2_find_cpos_for_left_leaf(sb
, right_path
, &cpos
);
2561 ocfs2_free_path(left_path
);
2567 static int ocfs2_update_edge_lengths(handle_t
*handle
,
2568 struct ocfs2_extent_tree
*et
,
2569 int subtree_index
, struct ocfs2_path
*path
)
2572 struct ocfs2_extent_rec
*rec
;
2573 struct ocfs2_extent_list
*el
;
2574 struct ocfs2_extent_block
*eb
;
2578 * In normal tree rotation process, we will never touch the
2579 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2580 * doesn't reserve the credits for them either.
2582 * But we do have a special case here which will update the rightmost
2583 * records for all the bh in the path.
2584 * So we have to allocate extra credits and access them.
2586 ret
= ocfs2_extend_trans(handle
,
2587 handle
->h_buffer_credits
+ subtree_index
);
2593 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, path
);
2599 /* Path should always be rightmost. */
2600 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2601 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2604 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2605 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2606 rec
= &el
->l_recs
[idx
];
2607 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2609 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2610 el
= path
->p_node
[i
].el
;
2611 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2612 rec
= &el
->l_recs
[idx
];
2614 rec
->e_int_clusters
= cpu_to_le32(range
);
2615 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2617 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2623 static void ocfs2_unlink_path(handle_t
*handle
,
2624 struct ocfs2_extent_tree
*et
,
2625 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2626 struct ocfs2_path
*path
, int unlink_start
)
2629 struct ocfs2_extent_block
*eb
;
2630 struct ocfs2_extent_list
*el
;
2631 struct buffer_head
*bh
;
2633 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2634 bh
= path
->p_node
[i
].bh
;
2636 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2638 * Not all nodes might have had their final count
2639 * decremented by the caller - handle this here.
2642 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2644 "Inode %llu, attempted to remove extent block "
2645 "%llu with %u records\n",
2646 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
2647 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2648 le16_to_cpu(el
->l_next_free_rec
));
2650 ocfs2_journal_dirty(handle
, bh
);
2651 ocfs2_remove_from_cache(et
->et_ci
, bh
);
2655 el
->l_next_free_rec
= 0;
2656 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2658 ocfs2_journal_dirty(handle
, bh
);
2660 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2664 ocfs2_remove_from_cache(et
->et_ci
, bh
);
2668 static void ocfs2_unlink_subtree(handle_t
*handle
,
2669 struct ocfs2_extent_tree
*et
,
2670 struct ocfs2_path
*left_path
,
2671 struct ocfs2_path
*right_path
,
2673 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2676 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2677 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2678 struct ocfs2_extent_list
*el
;
2679 struct ocfs2_extent_block
*eb
;
2681 el
= path_leaf_el(left_path
);
2683 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2685 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2686 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2689 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2691 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2692 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2694 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2695 eb
->h_next_leaf_blk
= 0;
2697 ocfs2_journal_dirty(handle
, root_bh
);
2698 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2700 ocfs2_unlink_path(handle
, et
, dealloc
, right_path
,
2704 static int ocfs2_rotate_subtree_left(handle_t
*handle
,
2705 struct ocfs2_extent_tree
*et
,
2706 struct ocfs2_path
*left_path
,
2707 struct ocfs2_path
*right_path
,
2709 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2712 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2713 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2714 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2715 struct ocfs2_extent_block
*eb
;
2719 right_leaf_el
= path_leaf_el(right_path
);
2720 left_leaf_el
= path_leaf_el(left_path
);
2721 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2722 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2724 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2727 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2728 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2730 * It's legal for us to proceed if the right leaf is
2731 * the rightmost one and it has an empty extent. There
2732 * are two cases to handle - whether the leaf will be
2733 * empty after removal or not. If the leaf isn't empty
2734 * then just remove the empty extent up front. The
2735 * next block will handle empty leaves by flagging
2738 * Non rightmost leaves will throw -EAGAIN and the
2739 * caller can manually move the subtree and retry.
2742 if (eb
->h_next_leaf_blk
!= 0ULL)
2745 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2746 ret
= ocfs2_journal_access_eb(handle
, et
->et_ci
,
2747 path_leaf_bh(right_path
),
2748 OCFS2_JOURNAL_ACCESS_WRITE
);
2754 ocfs2_remove_empty_extent(right_leaf_el
);
2756 right_has_empty
= 1;
2759 if (eb
->h_next_leaf_blk
== 0ULL &&
2760 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2762 * We have to update i_last_eb_blk during the meta
2765 ret
= ocfs2_et_root_journal_access(handle
, et
,
2766 OCFS2_JOURNAL_ACCESS_WRITE
);
2772 del_right_subtree
= 1;
2776 * Getting here with an empty extent in the right path implies
2777 * that it's the rightmost path and will be deleted.
2779 BUG_ON(right_has_empty
&& !del_right_subtree
);
2781 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
2788 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2789 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2796 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
2804 if (!right_has_empty
) {
2806 * Only do this if we're moving a real
2807 * record. Otherwise, the action is delayed until
2808 * after removal of the right path in which case we
2809 * can do a simple shift to remove the empty extent.
2811 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2812 memset(&right_leaf_el
->l_recs
[0], 0,
2813 sizeof(struct ocfs2_extent_rec
));
2815 if (eb
->h_next_leaf_blk
== 0ULL) {
2817 * Move recs over to get rid of empty extent, decrease
2818 * next_free. This is allowed to remove the last
2819 * extent in our leaf (setting l_next_free_rec to
2820 * zero) - the delete code below won't care.
2822 ocfs2_remove_empty_extent(right_leaf_el
);
2825 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2828 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2832 if (del_right_subtree
) {
2833 ocfs2_unlink_subtree(handle
, et
, left_path
, right_path
,
2834 subtree_index
, dealloc
);
2835 ret
= ocfs2_update_edge_lengths(handle
, et
, subtree_index
,
2842 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2843 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2846 * Removal of the extent in the left leaf was skipped
2847 * above so we could delete the right path
2850 if (right_has_empty
)
2851 ocfs2_remove_empty_extent(left_leaf_el
);
2853 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2859 ocfs2_complete_edge_insert(handle
, left_path
, right_path
,
2867 * Given a full path, determine what cpos value would return us a path
2868 * containing the leaf immediately to the right of the current one.
2870 * Will return zero if the path passed in is already the rightmost path.
2872 * This looks similar, but is subtly different to
2873 * ocfs2_find_cpos_for_left_leaf().
2875 int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2876 struct ocfs2_path
*path
, u32
*cpos
)
2880 struct ocfs2_extent_list
*el
;
2884 if (path
->p_tree_depth
== 0)
2887 blkno
= path_leaf_bh(path
)->b_blocknr
;
2889 /* Start at the tree node just above the leaf and work our way up. */
2890 i
= path
->p_tree_depth
- 1;
2894 el
= path
->p_node
[i
].el
;
2897 * Find the extent record just after the one in our
2900 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2901 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2902 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2903 if (j
== (next_free
- 1)) {
2906 * We've determined that the
2907 * path specified is already
2908 * the rightmost one - return a
2914 * The rightmost record points to our
2915 * leaf - we need to travel up the
2921 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2927 * If we got here, we never found a valid node where
2928 * the tree indicated one should be.
2931 "Invalid extent tree at extent block %llu\n",
2932 (unsigned long long)blkno
);
2937 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2945 static int ocfs2_rotate_rightmost_leaf_left(handle_t
*handle
,
2946 struct ocfs2_extent_tree
*et
,
2947 struct ocfs2_path
*path
)
2950 struct buffer_head
*bh
= path_leaf_bh(path
);
2951 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
2953 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2956 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, path
,
2957 path_num_items(path
) - 1);
2963 ocfs2_remove_empty_extent(el
);
2965 ret
= ocfs2_journal_dirty(handle
, bh
);
2973 static int __ocfs2_rotate_tree_left(handle_t
*handle
,
2974 struct ocfs2_extent_tree
*et
,
2976 struct ocfs2_path
*path
,
2977 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2978 struct ocfs2_path
**empty_extent_path
)
2980 int ret
, subtree_root
, deleted
;
2982 struct ocfs2_path
*left_path
= NULL
;
2983 struct ocfs2_path
*right_path
= NULL
;
2984 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
2986 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2988 *empty_extent_path
= NULL
;
2990 ret
= ocfs2_find_cpos_for_right_leaf(sb
, path
, &right_cpos
);
2996 left_path
= ocfs2_new_path_from_path(path
);
3003 ocfs2_cp_path(left_path
, path
);
3005 right_path
= ocfs2_new_path_from_path(path
);
3012 while (right_cpos
) {
3013 ret
= ocfs2_find_path(et
->et_ci
, right_path
, right_cpos
);
3019 subtree_root
= ocfs2_find_subtree_root(et
, left_path
,
3022 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
3024 (unsigned long long)
3025 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
3026 right_path
->p_tree_depth
);
3028 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
3029 orig_credits
, left_path
);
3036 * Caller might still want to make changes to the
3037 * tree root, so re-add it to the journal here.
3039 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3046 ret
= ocfs2_rotate_subtree_left(handle
, et
, left_path
,
3047 right_path
, subtree_root
,
3049 if (ret
== -EAGAIN
) {
3051 * The rotation has to temporarily stop due to
3052 * the right subtree having an empty
3053 * extent. Pass it back to the caller for a
3056 *empty_extent_path
= right_path
;
3066 * The subtree rotate might have removed records on
3067 * the rightmost edge. If so, then rotation is
3073 ocfs2_mv_path(left_path
, right_path
);
3075 ret
= ocfs2_find_cpos_for_right_leaf(sb
, left_path
,
3084 ocfs2_free_path(right_path
);
3085 ocfs2_free_path(left_path
);
3090 static int ocfs2_remove_rightmost_path(handle_t
*handle
,
3091 struct ocfs2_extent_tree
*et
,
3092 struct ocfs2_path
*path
,
3093 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3095 int ret
, subtree_index
;
3097 struct ocfs2_path
*left_path
= NULL
;
3098 struct ocfs2_extent_block
*eb
;
3099 struct ocfs2_extent_list
*el
;
3102 ret
= ocfs2_et_sanity_check(et
);
3106 * There's two ways we handle this depending on
3107 * whether path is the only existing one.
3109 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
3110 handle
->h_buffer_credits
,
3117 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, path
);
3123 ret
= ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et
->et_ci
),
3132 * We have a path to the left of this one - it needs
3135 left_path
= ocfs2_new_path_from_path(path
);
3142 ret
= ocfs2_find_path(et
->et_ci
, left_path
, cpos
);
3148 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, left_path
);
3154 subtree_index
= ocfs2_find_subtree_root(et
, left_path
, path
);
3156 ocfs2_unlink_subtree(handle
, et
, left_path
, path
,
3157 subtree_index
, dealloc
);
3158 ret
= ocfs2_update_edge_lengths(handle
, et
, subtree_index
,
3165 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
3166 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
3169 * 'path' is also the leftmost path which
3170 * means it must be the only one. This gets
3171 * handled differently because we want to
3172 * revert the root back to having extents
3175 ocfs2_unlink_path(handle
, et
, dealloc
, path
, 1);
3177 el
= et
->et_root_el
;
3178 el
->l_tree_depth
= 0;
3179 el
->l_next_free_rec
= 0;
3180 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3182 ocfs2_et_set_last_eb_blk(et
, 0);
3185 ocfs2_journal_dirty(handle
, path_root_bh(path
));
3188 ocfs2_free_path(left_path
);
3193 * Left rotation of btree records.
3195 * In many ways, this is (unsurprisingly) the opposite of right
3196 * rotation. We start at some non-rightmost path containing an empty
3197 * extent in the leaf block. The code works its way to the rightmost
3198 * path by rotating records to the left in every subtree.
3200 * This is used by any code which reduces the number of extent records
3201 * in a leaf. After removal, an empty record should be placed in the
3202 * leftmost list position.
3204 * This won't handle a length update of the rightmost path records if
3205 * the rightmost tree leaf record is removed so the caller is
3206 * responsible for detecting and correcting that.
3208 static int ocfs2_rotate_tree_left(handle_t
*handle
,
3209 struct ocfs2_extent_tree
*et
,
3210 struct ocfs2_path
*path
,
3211 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3213 int ret
, orig_credits
= handle
->h_buffer_credits
;
3214 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
3215 struct ocfs2_extent_block
*eb
;
3216 struct ocfs2_extent_list
*el
;
3218 el
= path_leaf_el(path
);
3219 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
3222 if (path
->p_tree_depth
== 0) {
3223 rightmost_no_delete
:
3225 * Inline extents. This is trivially handled, so do
3228 ret
= ocfs2_rotate_rightmost_leaf_left(handle
, et
, path
);
3235 * Handle rightmost branch now. There's several cases:
3236 * 1) simple rotation leaving records in there. That's trivial.
3237 * 2) rotation requiring a branch delete - there's no more
3238 * records left. Two cases of this:
3239 * a) There are branches to the left.
3240 * b) This is also the leftmost (the only) branch.
3242 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3243 * 2a) we need the left branch so that we can update it with the unlink
3244 * 2b) we need to bring the root back to inline extents.
3247 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
3249 if (eb
->h_next_leaf_blk
== 0) {
3251 * This gets a bit tricky if we're going to delete the
3252 * rightmost path. Get the other cases out of the way
3255 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
3256 goto rightmost_no_delete
;
3258 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
3260 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
3261 "Owner %llu has empty extent block at %llu",
3262 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
3263 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
3268 * XXX: The caller can not trust "path" any more after
3269 * this as it will have been deleted. What do we do?
3271 * In theory the rotate-for-merge code will never get
3272 * here because it'll always ask for a rotate in a
3276 ret
= ocfs2_remove_rightmost_path(handle
, et
, path
,
3284 * Now we can loop, remembering the path we get from -EAGAIN
3285 * and restarting from there.
3288 ret
= __ocfs2_rotate_tree_left(handle
, et
, orig_credits
, path
,
3289 dealloc
, &restart_path
);
3290 if (ret
&& ret
!= -EAGAIN
) {
3295 while (ret
== -EAGAIN
) {
3296 tmp_path
= restart_path
;
3297 restart_path
= NULL
;
3299 ret
= __ocfs2_rotate_tree_left(handle
, et
, orig_credits
,
3302 if (ret
&& ret
!= -EAGAIN
) {
3307 ocfs2_free_path(tmp_path
);
3315 ocfs2_free_path(tmp_path
);
3316 ocfs2_free_path(restart_path
);
3320 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
3323 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
3326 if (rec
->e_leaf_clusters
== 0) {
3328 * We consumed all of the merged-from record. An empty
3329 * extent cannot exist anywhere but the 1st array
3330 * position, so move things over if the merged-from
3331 * record doesn't occupy that position.
3333 * This creates a new empty extent so the caller
3334 * should be smart enough to have removed any existing
3338 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3339 size
= index
* sizeof(struct ocfs2_extent_rec
);
3340 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
3344 * Always memset - the caller doesn't check whether it
3345 * created an empty extent, so there could be junk in
3348 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3352 static int ocfs2_get_right_path(struct ocfs2_extent_tree
*et
,
3353 struct ocfs2_path
*left_path
,
3354 struct ocfs2_path
**ret_right_path
)
3358 struct ocfs2_path
*right_path
= NULL
;
3359 struct ocfs2_extent_list
*left_el
;
3361 *ret_right_path
= NULL
;
3363 /* This function shouldn't be called for non-trees. */
3364 BUG_ON(left_path
->p_tree_depth
== 0);
3366 left_el
= path_leaf_el(left_path
);
3367 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
3369 ret
= ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et
->et_ci
),
3370 left_path
, &right_cpos
);
3376 /* This function shouldn't be called for the rightmost leaf. */
3377 BUG_ON(right_cpos
== 0);
3379 right_path
= ocfs2_new_path_from_path(left_path
);
3386 ret
= ocfs2_find_path(et
->et_ci
, right_path
, right_cpos
);
3392 *ret_right_path
= right_path
;
3395 ocfs2_free_path(right_path
);
3400 * Remove split_rec clusters from the record at index and merge them
3401 * onto the beginning of the record "next" to it.
3402 * For index < l_count - 1, the next means the extent rec at index + 1.
3403 * For index == l_count - 1, the "next" means the 1st extent rec of the
3404 * next extent block.
3406 static int ocfs2_merge_rec_right(struct ocfs2_path
*left_path
,
3408 struct ocfs2_extent_tree
*et
,
3409 struct ocfs2_extent_rec
*split_rec
,
3412 int ret
, next_free
, i
;
3413 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3414 struct ocfs2_extent_rec
*left_rec
;
3415 struct ocfs2_extent_rec
*right_rec
;
3416 struct ocfs2_extent_list
*right_el
;
3417 struct ocfs2_path
*right_path
= NULL
;
3418 int subtree_index
= 0;
3419 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3420 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3421 struct buffer_head
*root_bh
= NULL
;
3423 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3424 left_rec
= &el
->l_recs
[index
];
3426 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3427 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3428 /* we meet with a cross extent block merge. */
3429 ret
= ocfs2_get_right_path(et
, left_path
, &right_path
);
3435 right_el
= path_leaf_el(right_path
);
3436 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3437 BUG_ON(next_free
<= 0);
3438 right_rec
= &right_el
->l_recs
[0];
3439 if (ocfs2_is_empty_extent(right_rec
)) {
3440 BUG_ON(next_free
<= 1);
3441 right_rec
= &right_el
->l_recs
[1];
3444 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3445 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3446 le32_to_cpu(right_rec
->e_cpos
));
3448 subtree_index
= ocfs2_find_subtree_root(et
, left_path
,
3451 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3452 handle
->h_buffer_credits
,
3459 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3460 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3462 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
3469 for (i
= subtree_index
+ 1;
3470 i
< path_num_items(right_path
); i
++) {
3471 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3478 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3487 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3488 right_rec
= &el
->l_recs
[index
+ 1];
3491 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, left_path
,
3492 path_num_items(left_path
) - 1);
3498 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3500 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3501 le64_add_cpu(&right_rec
->e_blkno
,
3502 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et
->et_ci
),
3504 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3506 ocfs2_cleanup_merge(el
, index
);
3508 ret
= ocfs2_journal_dirty(handle
, bh
);
3513 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3517 ocfs2_complete_edge_insert(handle
, left_path
, right_path
,
3522 ocfs2_free_path(right_path
);
3526 static int ocfs2_get_left_path(struct ocfs2_extent_tree
*et
,
3527 struct ocfs2_path
*right_path
,
3528 struct ocfs2_path
**ret_left_path
)
3532 struct ocfs2_path
*left_path
= NULL
;
3534 *ret_left_path
= NULL
;
3536 /* This function shouldn't be called for non-trees. */
3537 BUG_ON(right_path
->p_tree_depth
== 0);
3539 ret
= ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et
->et_ci
),
3540 right_path
, &left_cpos
);
3546 /* This function shouldn't be called for the leftmost leaf. */
3547 BUG_ON(left_cpos
== 0);
3549 left_path
= ocfs2_new_path_from_path(right_path
);
3556 ret
= ocfs2_find_path(et
->et_ci
, left_path
, left_cpos
);
3562 *ret_left_path
= left_path
;
3565 ocfs2_free_path(left_path
);
3570 * Remove split_rec clusters from the record at index and merge them
3571 * onto the tail of the record "before" it.
3572 * For index > 0, the "before" means the extent rec at index - 1.
3574 * For index == 0, the "before" means the last record of the previous
3575 * extent block. And there is also a situation that we may need to
3576 * remove the rightmost leaf extent block in the right_path and change
3577 * the right path to indicate the new rightmost path.
3579 static int ocfs2_merge_rec_left(struct ocfs2_path
*right_path
,
3581 struct ocfs2_extent_tree
*et
,
3582 struct ocfs2_extent_rec
*split_rec
,
3583 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3586 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3587 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3588 struct ocfs2_extent_rec
*left_rec
;
3589 struct ocfs2_extent_rec
*right_rec
;
3590 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3591 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3592 struct buffer_head
*root_bh
= NULL
;
3593 struct ocfs2_path
*left_path
= NULL
;
3594 struct ocfs2_extent_list
*left_el
;
3598 right_rec
= &el
->l_recs
[index
];
3600 /* we meet with a cross extent block merge. */
3601 ret
= ocfs2_get_left_path(et
, right_path
, &left_path
);
3607 left_el
= path_leaf_el(left_path
);
3608 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3609 le16_to_cpu(left_el
->l_count
));
3611 left_rec
= &left_el
->l_recs
[
3612 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3613 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3614 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3615 le32_to_cpu(split_rec
->e_cpos
));
3617 subtree_index
= ocfs2_find_subtree_root(et
, left_path
,
3620 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3621 handle
->h_buffer_credits
,
3628 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3629 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3631 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
3638 for (i
= subtree_index
+ 1;
3639 i
< path_num_items(right_path
); i
++) {
3640 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3647 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
,
3655 left_rec
= &el
->l_recs
[index
- 1];
3656 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3657 has_empty_extent
= 1;
3660 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, right_path
,
3661 path_num_items(right_path
) - 1);
3667 if (has_empty_extent
&& index
== 1) {
3669 * The easy case - we can just plop the record right in.
3671 *left_rec
= *split_rec
;
3673 has_empty_extent
= 0;
3675 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3677 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3678 le64_add_cpu(&right_rec
->e_blkno
,
3679 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et
->et_ci
),
3681 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3683 ocfs2_cleanup_merge(el
, index
);
3685 ret
= ocfs2_journal_dirty(handle
, bh
);
3690 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3695 * In the situation that the right_rec is empty and the extent
3696 * block is empty also, ocfs2_complete_edge_insert can't handle
3697 * it and we need to delete the right extent block.
3699 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3700 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3702 ret
= ocfs2_remove_rightmost_path(handle
, et
,
3710 /* Now the rightmost extent block has been deleted.
3711 * So we use the new rightmost path.
3713 ocfs2_mv_path(right_path
, left_path
);
3716 ocfs2_complete_edge_insert(handle
, left_path
,
3717 right_path
, subtree_index
);
3721 ocfs2_free_path(left_path
);
3725 static int ocfs2_try_to_merge_extent(handle_t
*handle
,
3726 struct ocfs2_extent_tree
*et
,
3727 struct ocfs2_path
*path
,
3729 struct ocfs2_extent_rec
*split_rec
,
3730 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3731 struct ocfs2_merge_ctxt
*ctxt
)
3734 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3735 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3737 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3739 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3741 * The merge code will need to create an empty
3742 * extent to take the place of the newly
3743 * emptied slot. Remove any pre-existing empty
3744 * extents - having more than one in a leaf is
3747 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
3753 rec
= &el
->l_recs
[split_index
];
3756 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3758 * Left-right contig implies this.
3760 BUG_ON(!ctxt
->c_split_covers_rec
);
3763 * Since the leftright insert always covers the entire
3764 * extent, this call will delete the insert record
3765 * entirely, resulting in an empty extent record added to
3768 * Since the adding of an empty extent shifts
3769 * everything back to the right, there's no need to
3770 * update split_index here.
3772 * When the split_index is zero, we need to merge it to the
3773 * prevoius extent block. It is more efficient and easier
3774 * if we do merge_right first and merge_left later.
3776 ret
= ocfs2_merge_rec_right(path
, handle
, et
, split_rec
,
3784 * We can only get this from logic error above.
3786 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3788 /* The merge left us with an empty extent, remove it. */
3789 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
3795 rec
= &el
->l_recs
[split_index
];
3798 * Note that we don't pass split_rec here on purpose -
3799 * we've merged it into the rec already.
3801 ret
= ocfs2_merge_rec_left(path
, handle
, et
, rec
,
3802 dealloc
, split_index
);
3809 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
3811 * Error from this last rotate is not critical, so
3812 * print but don't bubble it up.
3819 * Merge a record to the left or right.
3821 * 'contig_type' is relative to the existing record,
3822 * so for example, if we're "right contig", it's to
3823 * the record on the left (hence the left merge).
3825 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3826 ret
= ocfs2_merge_rec_left(path
, handle
, et
,
3834 ret
= ocfs2_merge_rec_right(path
, handle
,
3843 if (ctxt
->c_split_covers_rec
) {
3845 * The merge may have left an empty extent in
3846 * our leaf. Try to rotate it away.
3848 ret
= ocfs2_rotate_tree_left(handle
, et
, path
,
3860 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3861 enum ocfs2_split_type split
,
3862 struct ocfs2_extent_rec
*rec
,
3863 struct ocfs2_extent_rec
*split_rec
)
3867 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3868 le16_to_cpu(split_rec
->e_leaf_clusters
));
3870 if (split
== SPLIT_LEFT
) {
3872 * Region is on the left edge of the existing
3875 le32_add_cpu(&rec
->e_cpos
,
3876 le16_to_cpu(split_rec
->e_leaf_clusters
));
3877 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3878 le16_add_cpu(&rec
->e_leaf_clusters
,
3879 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3882 * Region is on the right edge of the existing
3885 le16_add_cpu(&rec
->e_leaf_clusters
,
3886 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3891 * Do the final bits of extent record insertion at the target leaf
3892 * list. If this leaf is part of an allocation tree, it is assumed
3893 * that the tree above has been prepared.
3895 static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree
*et
,
3896 struct ocfs2_extent_rec
*insert_rec
,
3897 struct ocfs2_extent_list
*el
,
3898 struct ocfs2_insert_type
*insert
)
3900 int i
= insert
->ins_contig_index
;
3902 struct ocfs2_extent_rec
*rec
;
3904 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3906 if (insert
->ins_split
!= SPLIT_NONE
) {
3907 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3909 rec
= &el
->l_recs
[i
];
3910 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et
->et_ci
),
3911 insert
->ins_split
, rec
,
3917 * Contiguous insert - either left or right.
3919 if (insert
->ins_contig
!= CONTIG_NONE
) {
3920 rec
= &el
->l_recs
[i
];
3921 if (insert
->ins_contig
== CONTIG_LEFT
) {
3922 rec
->e_blkno
= insert_rec
->e_blkno
;
3923 rec
->e_cpos
= insert_rec
->e_cpos
;
3925 le16_add_cpu(&rec
->e_leaf_clusters
,
3926 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3931 * Handle insert into an empty leaf.
3933 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3934 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3935 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3936 el
->l_recs
[0] = *insert_rec
;
3937 el
->l_next_free_rec
= cpu_to_le16(1);
3944 if (insert
->ins_appending
== APPEND_TAIL
) {
3945 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3946 rec
= &el
->l_recs
[i
];
3947 range
= le32_to_cpu(rec
->e_cpos
)
3948 + le16_to_cpu(rec
->e_leaf_clusters
);
3949 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3951 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3952 le16_to_cpu(el
->l_count
),
3953 "owner %llu, depth %u, count %u, next free %u, "
3954 "rec.cpos %u, rec.clusters %u, "
3955 "insert.cpos %u, insert.clusters %u\n",
3956 ocfs2_metadata_cache_owner(et
->et_ci
),
3957 le16_to_cpu(el
->l_tree_depth
),
3958 le16_to_cpu(el
->l_count
),
3959 le16_to_cpu(el
->l_next_free_rec
),
3960 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3961 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3962 le32_to_cpu(insert_rec
->e_cpos
),
3963 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3965 el
->l_recs
[i
] = *insert_rec
;
3966 le16_add_cpu(&el
->l_next_free_rec
, 1);
3972 * Ok, we have to rotate.
3974 * At this point, it is safe to assume that inserting into an
3975 * empty leaf and appending to a leaf have both been handled
3978 * This leaf needs to have space, either by the empty 1st
3979 * extent record, or by virtue of an l_next_rec < l_count.
3981 ocfs2_rotate_leaf(el
, insert_rec
);
3984 static void ocfs2_adjust_rightmost_records(handle_t
*handle
,
3985 struct ocfs2_extent_tree
*et
,
3986 struct ocfs2_path
*path
,
3987 struct ocfs2_extent_rec
*insert_rec
)
3989 int ret
, i
, next_free
;
3990 struct buffer_head
*bh
;
3991 struct ocfs2_extent_list
*el
;
3992 struct ocfs2_extent_rec
*rec
;
3995 * Update everything except the leaf block.
3997 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3998 bh
= path
->p_node
[i
].bh
;
3999 el
= path
->p_node
[i
].el
;
4001 next_free
= le16_to_cpu(el
->l_next_free_rec
);
4002 if (next_free
== 0) {
4003 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
4004 "Owner %llu has a bad extent list",
4005 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
));
4010 rec
= &el
->l_recs
[next_free
- 1];
4012 rec
->e_int_clusters
= insert_rec
->e_cpos
;
4013 le32_add_cpu(&rec
->e_int_clusters
,
4014 le16_to_cpu(insert_rec
->e_leaf_clusters
));
4015 le32_add_cpu(&rec
->e_int_clusters
,
4016 -le32_to_cpu(rec
->e_cpos
));
4018 ret
= ocfs2_journal_dirty(handle
, bh
);
4025 static int ocfs2_append_rec_to_path(handle_t
*handle
,
4026 struct ocfs2_extent_tree
*et
,
4027 struct ocfs2_extent_rec
*insert_rec
,
4028 struct ocfs2_path
*right_path
,
4029 struct ocfs2_path
**ret_left_path
)
4032 struct ocfs2_extent_list
*el
;
4033 struct ocfs2_path
*left_path
= NULL
;
4035 *ret_left_path
= NULL
;
4038 * This shouldn't happen for non-trees. The extent rec cluster
4039 * count manipulation below only works for interior nodes.
4041 BUG_ON(right_path
->p_tree_depth
== 0);
4044 * If our appending insert is at the leftmost edge of a leaf,
4045 * then we might need to update the rightmost records of the
4048 el
= path_leaf_el(right_path
);
4049 next_free
= le16_to_cpu(el
->l_next_free_rec
);
4050 if (next_free
== 0 ||
4051 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
4054 ret
= ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et
->et_ci
),
4055 right_path
, &left_cpos
);
4061 mlog(0, "Append may need a left path update. cpos: %u, "
4062 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
4066 * No need to worry if the append is already in the
4070 left_path
= ocfs2_new_path_from_path(right_path
);
4077 ret
= ocfs2_find_path(et
->et_ci
, left_path
,
4085 * ocfs2_insert_path() will pass the left_path to the
4091 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, right_path
);
4097 ocfs2_adjust_rightmost_records(handle
, et
, right_path
, insert_rec
);
4099 *ret_left_path
= left_path
;
4103 ocfs2_free_path(left_path
);
4108 static void ocfs2_split_record(struct ocfs2_extent_tree
*et
,
4109 struct ocfs2_path
*left_path
,
4110 struct ocfs2_path
*right_path
,
4111 struct ocfs2_extent_rec
*split_rec
,
4112 enum ocfs2_split_type split
)
4115 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
4116 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
4117 struct ocfs2_extent_rec
*rec
, *tmprec
;
4119 right_el
= path_leaf_el(right_path
);
4121 left_el
= path_leaf_el(left_path
);
4124 insert_el
= right_el
;
4125 index
= ocfs2_search_extent_list(el
, cpos
);
4127 if (index
== 0 && left_path
) {
4128 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
4131 * This typically means that the record
4132 * started in the left path but moved to the
4133 * right as a result of rotation. We either
4134 * move the existing record to the left, or we
4135 * do the later insert there.
4137 * In this case, the left path should always
4138 * exist as the rotate code will have passed
4139 * it back for a post-insert update.
4142 if (split
== SPLIT_LEFT
) {
4144 * It's a left split. Since we know
4145 * that the rotate code gave us an
4146 * empty extent in the left path, we
4147 * can just do the insert there.
4149 insert_el
= left_el
;
4152 * Right split - we have to move the
4153 * existing record over to the left
4154 * leaf. The insert will be into the
4155 * newly created empty extent in the
4158 tmprec
= &right_el
->l_recs
[index
];
4159 ocfs2_rotate_leaf(left_el
, tmprec
);
4162 memset(tmprec
, 0, sizeof(*tmprec
));
4163 index
= ocfs2_search_extent_list(left_el
, cpos
);
4164 BUG_ON(index
== -1);
4169 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
4171 * Left path is easy - we can just allow the insert to
4175 insert_el
= left_el
;
4176 index
= ocfs2_search_extent_list(el
, cpos
);
4177 BUG_ON(index
== -1);
4180 rec
= &el
->l_recs
[index
];
4181 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et
->et_ci
),
4182 split
, rec
, split_rec
);
4183 ocfs2_rotate_leaf(insert_el
, split_rec
);
4187 * This function only does inserts on an allocation b-tree. For tree
4188 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4190 * right_path is the path we want to do the actual insert
4191 * in. left_path should only be passed in if we need to update that
4192 * portion of the tree after an edge insert.
4194 static int ocfs2_insert_path(handle_t
*handle
,
4195 struct ocfs2_extent_tree
*et
,
4196 struct ocfs2_path
*left_path
,
4197 struct ocfs2_path
*right_path
,
4198 struct ocfs2_extent_rec
*insert_rec
,
4199 struct ocfs2_insert_type
*insert
)
4201 int ret
, subtree_index
;
4202 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
4205 int credits
= handle
->h_buffer_credits
;
4208 * There's a chance that left_path got passed back to
4209 * us without being accounted for in the
4210 * journal. Extend our transaction here to be sure we
4211 * can change those blocks.
4213 credits
+= left_path
->p_tree_depth
;
4215 ret
= ocfs2_extend_trans(handle
, credits
);
4221 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, left_path
);
4229 * Pass both paths to the journal. The majority of inserts
4230 * will be touching all components anyway.
4232 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, right_path
);
4238 if (insert
->ins_split
!= SPLIT_NONE
) {
4240 * We could call ocfs2_insert_at_leaf() for some types
4241 * of splits, but it's easier to just let one separate
4242 * function sort it all out.
4244 ocfs2_split_record(et
, left_path
, right_path
,
4245 insert_rec
, insert
->ins_split
);
4248 * Split might have modified either leaf and we don't
4249 * have a guarantee that the later edge insert will
4250 * dirty this for us.
4253 ret
= ocfs2_journal_dirty(handle
,
4254 path_leaf_bh(left_path
));
4258 ocfs2_insert_at_leaf(et
, insert_rec
, path_leaf_el(right_path
),
4261 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
4267 * The rotate code has indicated that we need to fix
4268 * up portions of the tree after the insert.
4270 * XXX: Should we extend the transaction here?
4272 subtree_index
= ocfs2_find_subtree_root(et
, left_path
,
4274 ocfs2_complete_edge_insert(handle
, left_path
, right_path
,
4283 static int ocfs2_do_insert_extent(handle_t
*handle
,
4284 struct ocfs2_extent_tree
*et
,
4285 struct ocfs2_extent_rec
*insert_rec
,
4286 struct ocfs2_insert_type
*type
)
4288 int ret
, rotate
= 0;
4290 struct ocfs2_path
*right_path
= NULL
;
4291 struct ocfs2_path
*left_path
= NULL
;
4292 struct ocfs2_extent_list
*el
;
4294 el
= et
->et_root_el
;
4296 ret
= ocfs2_et_root_journal_access(handle
, et
,
4297 OCFS2_JOURNAL_ACCESS_WRITE
);
4303 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
4304 ocfs2_insert_at_leaf(et
, insert_rec
, el
, type
);
4305 goto out_update_clusters
;
4308 right_path
= ocfs2_new_path_from_et(et
);
4316 * Determine the path to start with. Rotations need the
4317 * rightmost path, everything else can go directly to the
4320 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4321 if (type
->ins_appending
== APPEND_NONE
&&
4322 type
->ins_contig
== CONTIG_NONE
) {
4327 ret
= ocfs2_find_path(et
->et_ci
, right_path
, cpos
);
4334 * Rotations and appends need special treatment - they modify
4335 * parts of the tree's above them.
4337 * Both might pass back a path immediate to the left of the
4338 * one being inserted to. This will be cause
4339 * ocfs2_insert_path() to modify the rightmost records of
4340 * left_path to account for an edge insert.
4342 * XXX: When modifying this code, keep in mind that an insert
4343 * can wind up skipping both of these two special cases...
4346 ret
= ocfs2_rotate_tree_right(handle
, et
, type
->ins_split
,
4347 le32_to_cpu(insert_rec
->e_cpos
),
4348 right_path
, &left_path
);
4355 * ocfs2_rotate_tree_right() might have extended the
4356 * transaction without re-journaling our tree root.
4358 ret
= ocfs2_et_root_journal_access(handle
, et
,
4359 OCFS2_JOURNAL_ACCESS_WRITE
);
4364 } else if (type
->ins_appending
== APPEND_TAIL
4365 && type
->ins_contig
!= CONTIG_LEFT
) {
4366 ret
= ocfs2_append_rec_to_path(handle
, et
, insert_rec
,
4367 right_path
, &left_path
);
4374 ret
= ocfs2_insert_path(handle
, et
, left_path
, right_path
,
4381 out_update_clusters
:
4382 if (type
->ins_split
== SPLIT_NONE
)
4383 ocfs2_et_update_clusters(et
,
4384 le16_to_cpu(insert_rec
->e_leaf_clusters
));
4386 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4391 ocfs2_free_path(left_path
);
4392 ocfs2_free_path(right_path
);
4397 static enum ocfs2_contig_type
4398 ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree
*et
,
4399 struct ocfs2_path
*path
,
4400 struct ocfs2_extent_list
*el
, int index
,
4401 struct ocfs2_extent_rec
*split_rec
)
4404 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4405 u32 left_cpos
, right_cpos
;
4406 struct ocfs2_extent_rec
*rec
= NULL
;
4407 struct ocfs2_extent_list
*new_el
;
4408 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4409 struct buffer_head
*bh
;
4410 struct ocfs2_extent_block
*eb
;
4411 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
4414 rec
= &el
->l_recs
[index
- 1];
4415 } else if (path
->p_tree_depth
> 0) {
4416 status
= ocfs2_find_cpos_for_left_leaf(sb
, path
, &left_cpos
);
4420 if (left_cpos
!= 0) {
4421 left_path
= ocfs2_new_path_from_path(path
);
4425 status
= ocfs2_find_path(et
->et_ci
, left_path
,
4430 new_el
= path_leaf_el(left_path
);
4432 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4433 le16_to_cpu(new_el
->l_count
)) {
4434 bh
= path_leaf_bh(left_path
);
4435 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4437 "Extent block #%llu has an "
4438 "invalid l_next_free_rec of "
4439 "%d. It should have "
4440 "matched the l_count of %d",
4441 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4442 le16_to_cpu(new_el
->l_next_free_rec
),
4443 le16_to_cpu(new_el
->l_count
));
4447 rec
= &new_el
->l_recs
[
4448 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4453 * We're careful to check for an empty extent record here -
4454 * the merge code will know what to do if it sees one.
4457 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4458 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4461 ret
= ocfs2_et_extent_contig(et
, rec
, split_rec
);
4466 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4467 rec
= &el
->l_recs
[index
+ 1];
4468 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4469 path
->p_tree_depth
> 0) {
4470 status
= ocfs2_find_cpos_for_right_leaf(sb
, path
, &right_cpos
);
4474 if (right_cpos
== 0)
4477 right_path
= ocfs2_new_path_from_path(path
);
4481 status
= ocfs2_find_path(et
->et_ci
, right_path
, right_cpos
);
4485 new_el
= path_leaf_el(right_path
);
4486 rec
= &new_el
->l_recs
[0];
4487 if (ocfs2_is_empty_extent(rec
)) {
4488 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4489 bh
= path_leaf_bh(right_path
);
4490 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4492 "Extent block #%llu has an "
4493 "invalid l_next_free_rec of %d",
4494 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4495 le16_to_cpu(new_el
->l_next_free_rec
));
4499 rec
= &new_el
->l_recs
[1];
4504 enum ocfs2_contig_type contig_type
;
4506 contig_type
= ocfs2_et_extent_contig(et
, rec
, split_rec
);
4508 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4509 ret
= CONTIG_LEFTRIGHT
;
4510 else if (ret
== CONTIG_NONE
)
4516 ocfs2_free_path(left_path
);
4518 ocfs2_free_path(right_path
);
4523 static void ocfs2_figure_contig_type(struct ocfs2_extent_tree
*et
,
4524 struct ocfs2_insert_type
*insert
,
4525 struct ocfs2_extent_list
*el
,
4526 struct ocfs2_extent_rec
*insert_rec
)
4529 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4531 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4533 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4534 contig_type
= ocfs2_et_extent_contig(et
, &el
->l_recs
[i
],
4536 if (contig_type
!= CONTIG_NONE
) {
4537 insert
->ins_contig_index
= i
;
4541 insert
->ins_contig
= contig_type
;
4543 if (insert
->ins_contig
!= CONTIG_NONE
) {
4544 struct ocfs2_extent_rec
*rec
=
4545 &el
->l_recs
[insert
->ins_contig_index
];
4546 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4547 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4550 * Caller might want us to limit the size of extents, don't
4551 * calculate contiguousness if we might exceed that limit.
4553 if (et
->et_max_leaf_clusters
&&
4554 (len
> et
->et_max_leaf_clusters
))
4555 insert
->ins_contig
= CONTIG_NONE
;
4560 * This should only be called against the righmost leaf extent list.
4562 * ocfs2_figure_appending_type() will figure out whether we'll have to
4563 * insert at the tail of the rightmost leaf.
4565 * This should also work against the root extent list for tree's with 0
4566 * depth. If we consider the root extent list to be the rightmost leaf node
4567 * then the logic here makes sense.
4569 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4570 struct ocfs2_extent_list
*el
,
4571 struct ocfs2_extent_rec
*insert_rec
)
4574 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4575 struct ocfs2_extent_rec
*rec
;
4577 insert
->ins_appending
= APPEND_NONE
;
4579 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4581 if (!el
->l_next_free_rec
)
4582 goto set_tail_append
;
4584 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4585 /* Were all records empty? */
4586 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4587 goto set_tail_append
;
4590 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4591 rec
= &el
->l_recs
[i
];
4594 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4595 goto set_tail_append
;
4600 insert
->ins_appending
= APPEND_TAIL
;
4604 * Helper function called at the begining of an insert.
4606 * This computes a few things that are commonly used in the process of
4607 * inserting into the btree:
4608 * - Whether the new extent is contiguous with an existing one.
4609 * - The current tree depth.
4610 * - Whether the insert is an appending one.
4611 * - The total # of free records in the tree.
4613 * All of the information is stored on the ocfs2_insert_type
4616 static int ocfs2_figure_insert_type(struct ocfs2_extent_tree
*et
,
4617 struct buffer_head
**last_eb_bh
,
4618 struct ocfs2_extent_rec
*insert_rec
,
4620 struct ocfs2_insert_type
*insert
)
4623 struct ocfs2_extent_block
*eb
;
4624 struct ocfs2_extent_list
*el
;
4625 struct ocfs2_path
*path
= NULL
;
4626 struct buffer_head
*bh
= NULL
;
4628 insert
->ins_split
= SPLIT_NONE
;
4630 el
= et
->et_root_el
;
4631 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4633 if (el
->l_tree_depth
) {
4635 * If we have tree depth, we read in the
4636 * rightmost extent block ahead of time as
4637 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4638 * may want it later.
4640 ret
= ocfs2_read_extent_block(et
->et_ci
,
4641 ocfs2_et_get_last_eb_blk(et
),
4647 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4652 * Unless we have a contiguous insert, we'll need to know if
4653 * there is room left in our allocation tree for another
4656 * XXX: This test is simplistic, we can search for empty
4657 * extent records too.
4659 *free_records
= le16_to_cpu(el
->l_count
) -
4660 le16_to_cpu(el
->l_next_free_rec
);
4662 if (!insert
->ins_tree_depth
) {
4663 ocfs2_figure_contig_type(et
, insert
, el
, insert_rec
);
4664 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4668 path
= ocfs2_new_path_from_et(et
);
4676 * In the case that we're inserting past what the tree
4677 * currently accounts for, ocfs2_find_path() will return for
4678 * us the rightmost tree path. This is accounted for below in
4679 * the appending code.
4681 ret
= ocfs2_find_path(et
->et_ci
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4687 el
= path_leaf_el(path
);
4690 * Now that we have the path, there's two things we want to determine:
4691 * 1) Contiguousness (also set contig_index if this is so)
4693 * 2) Are we doing an append? We can trivially break this up
4694 * into two types of appends: simple record append, or a
4695 * rotate inside the tail leaf.
4697 ocfs2_figure_contig_type(et
, insert
, el
, insert_rec
);
4700 * The insert code isn't quite ready to deal with all cases of
4701 * left contiguousness. Specifically, if it's an insert into
4702 * the 1st record in a leaf, it will require the adjustment of
4703 * cluster count on the last record of the path directly to it's
4704 * left. For now, just catch that case and fool the layers
4705 * above us. This works just fine for tree_depth == 0, which
4706 * is why we allow that above.
4708 if (insert
->ins_contig
== CONTIG_LEFT
&&
4709 insert
->ins_contig_index
== 0)
4710 insert
->ins_contig
= CONTIG_NONE
;
4713 * Ok, so we can simply compare against last_eb to figure out
4714 * whether the path doesn't exist. This will only happen in
4715 * the case that we're doing a tail append, so maybe we can
4716 * take advantage of that information somehow.
4718 if (ocfs2_et_get_last_eb_blk(et
) ==
4719 path_leaf_bh(path
)->b_blocknr
) {
4721 * Ok, ocfs2_find_path() returned us the rightmost
4722 * tree path. This might be an appending insert. There are
4724 * 1) We're doing a true append at the tail:
4725 * -This might even be off the end of the leaf
4726 * 2) We're "appending" by rotating in the tail
4728 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4732 ocfs2_free_path(path
);
4742 * Insert an extent into a btree.
4744 * The caller needs to update the owning btree's cluster count.
4746 int ocfs2_insert_extent(handle_t
*handle
,
4747 struct ocfs2_extent_tree
*et
,
4752 struct ocfs2_alloc_context
*meta_ac
)
4755 int uninitialized_var(free_records
);
4756 struct buffer_head
*last_eb_bh
= NULL
;
4757 struct ocfs2_insert_type insert
= {0, };
4758 struct ocfs2_extent_rec rec
;
4760 mlog(0, "add %u clusters at position %u to owner %llu\n",
4762 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
));
4764 memset(&rec
, 0, sizeof(rec
));
4765 rec
.e_cpos
= cpu_to_le32(cpos
);
4766 rec
.e_blkno
= cpu_to_le64(start_blk
);
4767 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4768 rec
.e_flags
= flags
;
4769 status
= ocfs2_et_insert_check(et
, &rec
);
4775 status
= ocfs2_figure_insert_type(et
, &last_eb_bh
, &rec
,
4776 &free_records
, &insert
);
4782 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4783 "Insert.contig_index: %d, Insert.free_records: %d, "
4784 "Insert.tree_depth: %d\n",
4785 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4786 free_records
, insert
.ins_tree_depth
);
4788 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4789 status
= ocfs2_grow_tree(handle
, et
,
4790 &insert
.ins_tree_depth
, &last_eb_bh
,
4798 /* Finally, we can add clusters. This might rotate the tree for us. */
4799 status
= ocfs2_do_insert_extent(handle
, et
, &rec
, &insert
);
4803 ocfs2_et_extent_map_insert(et
, &rec
);
4813 * Allcate and add clusters into the extent b-tree.
4814 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4815 * The extent b-tree's root is specified by et, and
4816 * it is not limited to the file storage. Any extent tree can use this
4817 * function if it implements the proper ocfs2_extent_tree.
4819 int ocfs2_add_clusters_in_btree(handle_t
*handle
,
4820 struct ocfs2_extent_tree
*et
,
4821 u32
*logical_offset
,
4822 u32 clusters_to_add
,
4824 struct ocfs2_alloc_context
*data_ac
,
4825 struct ocfs2_alloc_context
*meta_ac
,
4826 enum ocfs2_alloc_restarted
*reason_ret
)
4830 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4831 u32 bit_off
, num_bits
;
4834 struct ocfs2_super
*osb
=
4835 OCFS2_SB(ocfs2_metadata_cache_get_super(et
->et_ci
));
4837 BUG_ON(!clusters_to_add
);
4840 flags
= OCFS2_EXT_UNWRITTEN
;
4842 free_extents
= ocfs2_num_free_extents(osb
, et
);
4843 if (free_extents
< 0) {
4844 status
= free_extents
;
4849 /* there are two cases which could cause us to EAGAIN in the
4850 * we-need-more-metadata case:
4851 * 1) we haven't reserved *any*
4852 * 2) we are so fragmented, we've needed to add metadata too
4854 if (!free_extents
&& !meta_ac
) {
4855 mlog(0, "we haven't reserved any metadata!\n");
4857 reason
= RESTART_META
;
4859 } else if ((!free_extents
)
4860 && (ocfs2_alloc_context_bits_left(meta_ac
)
4861 < ocfs2_extend_meta_needed(et
->et_root_el
))) {
4862 mlog(0, "filesystem is really fragmented...\n");
4864 reason
= RESTART_META
;
4868 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4869 clusters_to_add
, &bit_off
, &num_bits
);
4871 if (status
!= -ENOSPC
)
4876 BUG_ON(num_bits
> clusters_to_add
);
4878 /* reserve our write early -- insert_extent may update the tree root */
4879 status
= ocfs2_et_root_journal_access(handle
, et
,
4880 OCFS2_JOURNAL_ACCESS_WRITE
);
4886 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4887 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4889 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
));
4890 status
= ocfs2_insert_extent(handle
, et
, *logical_offset
, block
,
4891 num_bits
, flags
, meta_ac
);
4897 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4903 clusters_to_add
-= num_bits
;
4904 *logical_offset
+= num_bits
;
4906 if (clusters_to_add
) {
4907 mlog(0, "need to alloc once more, wanted = %u\n",
4910 reason
= RESTART_TRANS
;
4916 *reason_ret
= reason
;
4920 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4921 struct ocfs2_extent_rec
*split_rec
,
4923 struct ocfs2_extent_rec
*rec
)
4925 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4926 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4928 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4930 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4931 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4933 split_rec
->e_blkno
= rec
->e_blkno
;
4934 le64_add_cpu(&split_rec
->e_blkno
,
4935 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4937 split_rec
->e_flags
= rec
->e_flags
;
4940 static int ocfs2_split_and_insert(handle_t
*handle
,
4941 struct ocfs2_extent_tree
*et
,
4942 struct ocfs2_path
*path
,
4943 struct buffer_head
**last_eb_bh
,
4945 struct ocfs2_extent_rec
*orig_split_rec
,
4946 struct ocfs2_alloc_context
*meta_ac
)
4949 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4950 struct ocfs2_extent_rec tmprec
;
4951 struct ocfs2_extent_list
*rightmost_el
;
4952 struct ocfs2_extent_rec rec
;
4953 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4954 struct ocfs2_insert_type insert
;
4955 struct ocfs2_extent_block
*eb
;
4959 * Store a copy of the record on the stack - it might move
4960 * around as the tree is manipulated below.
4962 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4964 rightmost_el
= et
->et_root_el
;
4966 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4968 BUG_ON(!(*last_eb_bh
));
4969 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4970 rightmost_el
= &eb
->h_list
;
4973 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4974 le16_to_cpu(rightmost_el
->l_count
)) {
4975 ret
= ocfs2_grow_tree(handle
, et
,
4976 &depth
, last_eb_bh
, meta_ac
);
4983 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4984 insert
.ins_appending
= APPEND_NONE
;
4985 insert
.ins_contig
= CONTIG_NONE
;
4986 insert
.ins_tree_depth
= depth
;
4988 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4989 le16_to_cpu(split_rec
.e_leaf_clusters
);
4990 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4991 le16_to_cpu(rec
.e_leaf_clusters
);
4993 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4994 insert
.ins_split
= SPLIT_LEFT
;
4995 } else if (insert_range
== rec_range
) {
4996 insert
.ins_split
= SPLIT_RIGHT
;
4999 * Left/right split. We fake this as a right split
5000 * first and then make a second pass as a left split.
5002 insert
.ins_split
= SPLIT_RIGHT
;
5004 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et
->et_ci
),
5005 &tmprec
, insert_range
, &rec
);
5009 BUG_ON(do_leftright
);
5013 ret
= ocfs2_do_insert_extent(handle
, et
, &split_rec
, &insert
);
5019 if (do_leftright
== 1) {
5021 struct ocfs2_extent_list
*el
;
5024 split_rec
= *orig_split_rec
;
5026 ocfs2_reinit_path(path
, 1);
5028 cpos
= le32_to_cpu(split_rec
.e_cpos
);
5029 ret
= ocfs2_find_path(et
->et_ci
, path
, cpos
);
5035 el
= path_leaf_el(path
);
5036 split_index
= ocfs2_search_extent_list(el
, cpos
);
5044 static int ocfs2_replace_extent_rec(handle_t
*handle
,
5045 struct ocfs2_extent_tree
*et
,
5046 struct ocfs2_path
*path
,
5047 struct ocfs2_extent_list
*el
,
5049 struct ocfs2_extent_rec
*split_rec
)
5053 ret
= ocfs2_path_bh_journal_access(handle
, et
->et_ci
, path
,
5054 path_num_items(path
) - 1);
5060 el
->l_recs
[split_index
] = *split_rec
;
5062 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5068 * Split part or all of the extent record at split_index in the leaf
5069 * pointed to by path. Merge with the contiguous extent record if needed.
5071 * Care is taken to handle contiguousness so as to not grow the tree.
5073 * meta_ac is not strictly necessary - we only truly need it if growth
5074 * of the tree is required. All other cases will degrade into a less
5075 * optimal tree layout.
5077 * last_eb_bh should be the rightmost leaf block for any extent
5078 * btree. Since a split may grow the tree or a merge might shrink it,
5079 * the caller cannot trust the contents of that buffer after this call.
5081 * This code is optimized for readability - several passes might be
5082 * made over certain portions of the tree. All of those blocks will
5083 * have been brought into cache (and pinned via the journal), so the
5084 * extra overhead is not expressed in terms of disk reads.
5086 int ocfs2_split_extent(handle_t
*handle
,
5087 struct ocfs2_extent_tree
*et
,
5088 struct ocfs2_path
*path
,
5090 struct ocfs2_extent_rec
*split_rec
,
5091 struct ocfs2_alloc_context
*meta_ac
,
5092 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5095 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5096 struct buffer_head
*last_eb_bh
= NULL
;
5097 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
5098 struct ocfs2_merge_ctxt ctxt
;
5099 struct ocfs2_extent_list
*rightmost_el
;
5101 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
5102 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
5103 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
5109 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(et
, path
, el
,
5114 * The core merge / split code wants to know how much room is
5115 * left in this allocation tree, so we pass the
5116 * rightmost extent list.
5118 if (path
->p_tree_depth
) {
5119 struct ocfs2_extent_block
*eb
;
5121 ret
= ocfs2_read_extent_block(et
->et_ci
,
5122 ocfs2_et_get_last_eb_blk(et
),
5129 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5130 rightmost_el
= &eb
->h_list
;
5132 rightmost_el
= path_root_el(path
);
5134 if (rec
->e_cpos
== split_rec
->e_cpos
&&
5135 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
5136 ctxt
.c_split_covers_rec
= 1;
5138 ctxt
.c_split_covers_rec
= 0;
5140 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
5142 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5143 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
5144 ctxt
.c_split_covers_rec
);
5146 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
5147 if (ctxt
.c_split_covers_rec
)
5148 ret
= ocfs2_replace_extent_rec(handle
, et
, path
, el
,
5149 split_index
, split_rec
);
5151 ret
= ocfs2_split_and_insert(handle
, et
, path
,
5152 &last_eb_bh
, split_index
,
5153 split_rec
, meta_ac
);
5157 ret
= ocfs2_try_to_merge_extent(handle
, et
, path
,
5158 split_index
, split_rec
,
5170 * Change the flags of the already-existing extent at cpos for len clusters.
5172 * new_flags: the flags we want to set.
5173 * clear_flags: the flags we want to clear.
5174 * phys: the new physical offset we want this new extent starts from.
5176 * If the existing extent is larger than the request, initiate a
5177 * split. An attempt will be made at merging with adjacent extents.
5179 * The caller is responsible for passing down meta_ac if we'll need it.
5181 int ocfs2_change_extent_flag(handle_t
*handle
,
5182 struct ocfs2_extent_tree
*et
,
5183 u32 cpos
, u32 len
, u32 phys
,
5184 struct ocfs2_alloc_context
*meta_ac
,
5185 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5186 int new_flags
, int clear_flags
)
5189 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
5190 u64 start_blkno
= ocfs2_clusters_to_blocks(sb
, phys
);
5191 struct ocfs2_extent_rec split_rec
;
5192 struct ocfs2_path
*left_path
= NULL
;
5193 struct ocfs2_extent_list
*el
;
5194 struct ocfs2_extent_rec
*rec
;
5196 left_path
= ocfs2_new_path_from_et(et
);
5203 ret
= ocfs2_find_path(et
->et_ci
, left_path
, cpos
);
5208 el
= path_leaf_el(left_path
);
5210 index
= ocfs2_search_extent_list(el
, cpos
);
5211 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5213 "Owner %llu has an extent at cpos %u which can no "
5214 "longer be found.\n",
5215 (unsigned long long)
5216 ocfs2_metadata_cache_owner(et
->et_ci
), cpos
);
5222 rec
= &el
->l_recs
[index
];
5223 if (new_flags
&& (rec
->e_flags
& new_flags
)) {
5224 mlog(ML_ERROR
, "Owner %llu tried to set %d flags on an "
5225 "extent that already had them",
5226 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
5231 if (clear_flags
&& !(rec
->e_flags
& clear_flags
)) {
5232 mlog(ML_ERROR
, "Owner %llu tried to clear %d flags on an "
5233 "extent that didn't have them",
5234 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
5239 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
5240 split_rec
.e_cpos
= cpu_to_le32(cpos
);
5241 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
5242 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
5243 split_rec
.e_flags
= rec
->e_flags
;
5245 split_rec
.e_flags
|= new_flags
;
5247 split_rec
.e_flags
&= ~clear_flags
;
5249 ret
= ocfs2_split_extent(handle
, et
, left_path
,
5250 index
, &split_rec
, meta_ac
,
5256 ocfs2_free_path(left_path
);
5262 * Mark the already-existing extent at cpos as written for len clusters.
5263 * This removes the unwritten extent flag.
5265 * If the existing extent is larger than the request, initiate a
5266 * split. An attempt will be made at merging with adjacent extents.
5268 * The caller is responsible for passing down meta_ac if we'll need it.
5270 int ocfs2_mark_extent_written(struct inode
*inode
,
5271 struct ocfs2_extent_tree
*et
,
5272 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
5273 struct ocfs2_alloc_context
*meta_ac
,
5274 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5278 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5279 inode
->i_ino
, cpos
, len
, phys
);
5281 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
5282 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
5283 "that are being written to, but the feature bit "
5284 "is not set in the super block.",
5285 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
5291 * XXX: This should be fixed up so that we just re-insert the
5292 * next extent records.
5294 ocfs2_et_extent_map_truncate(et
, 0);
5296 ret
= ocfs2_change_extent_flag(handle
, et
, cpos
,
5297 len
, phys
, meta_ac
, dealloc
,
5298 0, OCFS2_EXT_UNWRITTEN
);
5306 static int ocfs2_split_tree(handle_t
*handle
, struct ocfs2_extent_tree
*et
,
5307 struct ocfs2_path
*path
,
5308 int index
, u32 new_range
,
5309 struct ocfs2_alloc_context
*meta_ac
)
5311 int ret
, depth
, credits
= handle
->h_buffer_credits
;
5312 struct buffer_head
*last_eb_bh
= NULL
;
5313 struct ocfs2_extent_block
*eb
;
5314 struct ocfs2_extent_list
*rightmost_el
, *el
;
5315 struct ocfs2_extent_rec split_rec
;
5316 struct ocfs2_extent_rec
*rec
;
5317 struct ocfs2_insert_type insert
;
5320 * Setup the record to split before we grow the tree.
5322 el
= path_leaf_el(path
);
5323 rec
= &el
->l_recs
[index
];
5324 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et
->et_ci
),
5325 &split_rec
, new_range
, rec
);
5327 depth
= path
->p_tree_depth
;
5329 ret
= ocfs2_read_extent_block(et
->et_ci
,
5330 ocfs2_et_get_last_eb_blk(et
),
5337 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5338 rightmost_el
= &eb
->h_list
;
5340 rightmost_el
= path_leaf_el(path
);
5342 credits
+= path
->p_tree_depth
+
5343 ocfs2_extend_meta_needed(et
->et_root_el
);
5344 ret
= ocfs2_extend_trans(handle
, credits
);
5350 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
5351 le16_to_cpu(rightmost_el
->l_count
)) {
5352 ret
= ocfs2_grow_tree(handle
, et
, &depth
, &last_eb_bh
,
5360 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
5361 insert
.ins_appending
= APPEND_NONE
;
5362 insert
.ins_contig
= CONTIG_NONE
;
5363 insert
.ins_split
= SPLIT_RIGHT
;
5364 insert
.ins_tree_depth
= depth
;
5366 ret
= ocfs2_do_insert_extent(handle
, et
, &split_rec
, &insert
);
5375 static int ocfs2_truncate_rec(handle_t
*handle
,
5376 struct ocfs2_extent_tree
*et
,
5377 struct ocfs2_path
*path
, int index
,
5378 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5382 u32 left_cpos
, rec_range
, trunc_range
;
5383 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5384 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
5385 struct ocfs2_path
*left_path
= NULL
;
5386 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5387 struct ocfs2_extent_rec
*rec
;
5388 struct ocfs2_extent_block
*eb
;
5390 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5391 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
5400 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5401 path
->p_tree_depth
) {
5403 * Check whether this is the rightmost tree record. If
5404 * we remove all of this record or part of its right
5405 * edge then an update of the record lengths above it
5408 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5409 if (eb
->h_next_leaf_blk
== 0)
5410 is_rightmost_tree_rec
= 1;
5413 rec
= &el
->l_recs
[index
];
5414 if (index
== 0 && path
->p_tree_depth
&&
5415 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5417 * Changing the leftmost offset (via partial or whole
5418 * record truncate) of an interior (or rightmost) path
5419 * means we have to update the subtree that is formed
5420 * by this leaf and the one to it's left.
5422 * There are two cases we can skip:
5423 * 1) Path is the leftmost one in our btree.
5424 * 2) The leaf is rightmost and will be empty after
5425 * we remove the extent record - the rotate code
5426 * knows how to update the newly formed edge.
5429 ret
= ocfs2_find_cpos_for_left_leaf(sb
, path
, &left_cpos
);
5435 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5436 left_path
= ocfs2_new_path_from_path(path
);
5443 ret
= ocfs2_find_path(et
->et_ci
, left_path
,
5452 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5453 handle
->h_buffer_credits
,
5460 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, path
);
5466 ret
= ocfs2_journal_access_path(et
->et_ci
, handle
, left_path
);
5472 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5473 trunc_range
= cpos
+ len
;
5475 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5478 memset(rec
, 0, sizeof(*rec
));
5479 ocfs2_cleanup_merge(el
, index
);
5482 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5483 if (is_rightmost_tree_rec
&& next_free
> 1) {
5485 * We skip the edge update if this path will
5486 * be deleted by the rotate code.
5488 rec
= &el
->l_recs
[next_free
- 1];
5489 ocfs2_adjust_rightmost_records(handle
, et
, path
,
5492 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5493 /* Remove leftmost portion of the record. */
5494 le32_add_cpu(&rec
->e_cpos
, len
);
5495 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5496 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5497 } else if (rec_range
== trunc_range
) {
5498 /* Remove rightmost portion of the record */
5499 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5500 if (is_rightmost_tree_rec
)
5501 ocfs2_adjust_rightmost_records(handle
, et
, path
, rec
);
5503 /* Caller should have trapped this. */
5504 mlog(ML_ERROR
, "Owner %llu: Invalid record truncate: (%u, %u) "
5506 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
5507 le32_to_cpu(rec
->e_cpos
),
5508 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5515 subtree_index
= ocfs2_find_subtree_root(et
, left_path
, path
);
5516 ocfs2_complete_edge_insert(handle
, left_path
, path
,
5520 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5522 ret
= ocfs2_rotate_tree_left(handle
, et
, path
, dealloc
);
5529 ocfs2_free_path(left_path
);
5533 int ocfs2_remove_extent(handle_t
*handle
,
5534 struct ocfs2_extent_tree
*et
,
5536 struct ocfs2_alloc_context
*meta_ac
,
5537 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5540 u32 rec_range
, trunc_range
;
5541 struct ocfs2_extent_rec
*rec
;
5542 struct ocfs2_extent_list
*el
;
5543 struct ocfs2_path
*path
= NULL
;
5546 * XXX: Why are we truncating to 0 instead of wherever this
5549 ocfs2_et_extent_map_truncate(et
, 0);
5551 path
= ocfs2_new_path_from_et(et
);
5558 ret
= ocfs2_find_path(et
->et_ci
, path
, cpos
);
5564 el
= path_leaf_el(path
);
5565 index
= ocfs2_search_extent_list(el
, cpos
);
5566 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5567 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
5568 "Owner %llu has an extent at cpos %u which can no "
5569 "longer be found.\n",
5570 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
5577 * We have 3 cases of extent removal:
5578 * 1) Range covers the entire extent rec
5579 * 2) Range begins or ends on one edge of the extent rec
5580 * 3) Range is in the middle of the extent rec (no shared edges)
5582 * For case 1 we remove the extent rec and left rotate to
5585 * For case 2 we just shrink the existing extent rec, with a
5586 * tree update if the shrinking edge is also the edge of an
5589 * For case 3 we do a right split to turn the extent rec into
5590 * something case 2 can handle.
5592 rec
= &el
->l_recs
[index
];
5593 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5594 trunc_range
= cpos
+ len
;
5596 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5598 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
5599 "(cpos %u, len %u)\n",
5600 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
5602 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5604 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5605 ret
= ocfs2_truncate_rec(handle
, et
, path
, index
, dealloc
,
5612 ret
= ocfs2_split_tree(handle
, et
, path
, index
,
5613 trunc_range
, meta_ac
);
5620 * The split could have manipulated the tree enough to
5621 * move the record location, so we have to look for it again.
5623 ocfs2_reinit_path(path
, 1);
5625 ret
= ocfs2_find_path(et
->et_ci
, path
, cpos
);
5631 el
= path_leaf_el(path
);
5632 index
= ocfs2_search_extent_list(el
, cpos
);
5633 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5634 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
5635 "Owner %llu: split at cpos %u lost record.",
5636 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
5643 * Double check our values here. If anything is fishy,
5644 * it's easier to catch it at the top level.
5646 rec
= &el
->l_recs
[index
];
5647 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5648 ocfs2_rec_clusters(el
, rec
);
5649 if (rec_range
!= trunc_range
) {
5650 ocfs2_error(ocfs2_metadata_cache_get_super(et
->et_ci
),
5651 "Owner %llu: error after split at cpos %u"
5652 "trunc len %u, existing record is (%u,%u)",
5653 (unsigned long long)ocfs2_metadata_cache_owner(et
->et_ci
),
5654 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5655 ocfs2_rec_clusters(el
, rec
));
5660 ret
= ocfs2_truncate_rec(handle
, et
, path
, index
, dealloc
,
5669 ocfs2_free_path(path
);
5673 int ocfs2_remove_btree_range(struct inode
*inode
,
5674 struct ocfs2_extent_tree
*et
,
5675 u32 cpos
, u32 phys_cpos
, u32 len
,
5676 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5679 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
5680 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5681 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5683 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5685 ret
= ocfs2_lock_allocators(inode
, et
, 0, 1, NULL
, &meta_ac
);
5691 mutex_lock(&tl_inode
->i_mutex
);
5693 if (ocfs2_truncate_log_needs_flush(osb
)) {
5694 ret
= __ocfs2_flush_truncate_log(osb
);
5701 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5702 if (IS_ERR(handle
)) {
5703 ret
= PTR_ERR(handle
);
5708 ret
= ocfs2_et_root_journal_access(handle
, et
,
5709 OCFS2_JOURNAL_ACCESS_WRITE
);
5715 vfs_dq_free_space_nodirty(inode
,
5716 ocfs2_clusters_to_bytes(inode
->i_sb
, len
));
5718 ret
= ocfs2_remove_extent(handle
, et
, cpos
, len
, meta_ac
, dealloc
);
5724 ocfs2_et_update_clusters(et
, -len
);
5726 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
5732 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
5737 ocfs2_commit_trans(osb
, handle
);
5739 mutex_unlock(&tl_inode
->i_mutex
);
5742 ocfs2_free_alloc_context(meta_ac
);
5747 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5749 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5750 struct ocfs2_dinode
*di
;
5751 struct ocfs2_truncate_log
*tl
;
5753 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5754 tl
= &di
->id2
.i_dealloc
;
5756 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5757 "slot %d, invalid truncate log parameters: used = "
5758 "%u, count = %u\n", osb
->slot_num
,
5759 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5760 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5763 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5764 unsigned int new_start
)
5766 unsigned int tail_index
;
5767 unsigned int current_tail
;
5769 /* No records, nothing to coalesce */
5770 if (!le16_to_cpu(tl
->tl_used
))
5773 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5774 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5775 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5777 return current_tail
== new_start
;
5780 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5783 unsigned int num_clusters
)
5786 unsigned int start_cluster
, tl_count
;
5787 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5788 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5789 struct ocfs2_dinode
*di
;
5790 struct ocfs2_truncate_log
*tl
;
5792 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5793 (unsigned long long)start_blk
, num_clusters
);
5795 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5797 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5799 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5801 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5802 * by the underlying call to ocfs2_read_inode_block(), so any
5803 * corruption is a code bug */
5804 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5806 tl
= &di
->id2
.i_dealloc
;
5807 tl_count
= le16_to_cpu(tl
->tl_count
);
5808 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5810 "Truncate record count on #%llu invalid "
5811 "wanted %u, actual %u\n",
5812 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5813 ocfs2_truncate_recs_per_inode(osb
->sb
),
5814 le16_to_cpu(tl
->tl_count
));
5816 /* Caller should have known to flush before calling us. */
5817 index
= le16_to_cpu(tl
->tl_used
);
5818 if (index
>= tl_count
) {
5824 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(tl_inode
), tl_bh
,
5825 OCFS2_JOURNAL_ACCESS_WRITE
);
5831 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5832 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5833 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5835 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5837 * Move index back to the record we are coalescing with.
5838 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5842 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5843 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5844 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5847 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5848 tl
->tl_used
= cpu_to_le16(index
+ 1);
5850 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5852 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5863 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5865 struct inode
*data_alloc_inode
,
5866 struct buffer_head
*data_alloc_bh
)
5870 unsigned int num_clusters
;
5872 struct ocfs2_truncate_rec rec
;
5873 struct ocfs2_dinode
*di
;
5874 struct ocfs2_truncate_log
*tl
;
5875 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5876 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5880 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5881 tl
= &di
->id2
.i_dealloc
;
5882 i
= le16_to_cpu(tl
->tl_used
) - 1;
5884 /* Caller has given us at least enough credits to
5885 * update the truncate log dinode */
5886 status
= ocfs2_journal_access_di(handle
, INODE_CACHE(tl_inode
), tl_bh
,
5887 OCFS2_JOURNAL_ACCESS_WRITE
);
5893 tl
->tl_used
= cpu_to_le16(i
);
5895 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5901 /* TODO: Perhaps we can calculate the bulk of the
5902 * credits up front rather than extending like
5904 status
= ocfs2_extend_trans(handle
,
5905 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5911 rec
= tl
->tl_recs
[i
];
5912 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5913 le32_to_cpu(rec
.t_start
));
5914 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5916 /* if start_blk is not set, we ignore the record as
5919 mlog(0, "free record %d, start = %u, clusters = %u\n",
5920 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5922 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5923 data_alloc_bh
, start_blk
,
5938 /* Expects you to already be holding tl_inode->i_mutex */
5939 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5942 unsigned int num_to_flush
;
5944 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5945 struct inode
*data_alloc_inode
= NULL
;
5946 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5947 struct buffer_head
*data_alloc_bh
= NULL
;
5948 struct ocfs2_dinode
*di
;
5949 struct ocfs2_truncate_log
*tl
;
5953 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5955 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5957 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5958 * by the underlying call to ocfs2_read_inode_block(), so any
5959 * corruption is a code bug */
5960 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5962 tl
= &di
->id2
.i_dealloc
;
5963 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5964 mlog(0, "Flush %u records from truncate log #%llu\n",
5965 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5966 if (!num_to_flush
) {
5971 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5972 GLOBAL_BITMAP_SYSTEM_INODE
,
5973 OCFS2_INVALID_SLOT
);
5974 if (!data_alloc_inode
) {
5976 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5980 mutex_lock(&data_alloc_inode
->i_mutex
);
5982 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5988 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5989 if (IS_ERR(handle
)) {
5990 status
= PTR_ERR(handle
);
5995 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
6000 ocfs2_commit_trans(osb
, handle
);
6003 brelse(data_alloc_bh
);
6004 ocfs2_inode_unlock(data_alloc_inode
, 1);
6007 mutex_unlock(&data_alloc_inode
->i_mutex
);
6008 iput(data_alloc_inode
);
6015 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
6018 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6020 mutex_lock(&tl_inode
->i_mutex
);
6021 status
= __ocfs2_flush_truncate_log(osb
);
6022 mutex_unlock(&tl_inode
->i_mutex
);
6027 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
6030 struct ocfs2_super
*osb
=
6031 container_of(work
, struct ocfs2_super
,
6032 osb_truncate_log_wq
.work
);
6036 status
= ocfs2_flush_truncate_log(osb
);
6040 ocfs2_init_inode_steal_slot(osb
);
6045 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6046 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
6049 if (osb
->osb_tl_inode
) {
6050 /* We want to push off log flushes while truncates are
6053 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
6055 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
6056 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
6060 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
6062 struct inode
**tl_inode
,
6063 struct buffer_head
**tl_bh
)
6066 struct inode
*inode
= NULL
;
6067 struct buffer_head
*bh
= NULL
;
6069 inode
= ocfs2_get_system_file_inode(osb
,
6070 TRUNCATE_LOG_SYSTEM_INODE
,
6074 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
6078 status
= ocfs2_read_inode_block(inode
, &bh
);
6092 /* called during the 1st stage of node recovery. we stamp a clean
6093 * truncate log and pass back a copy for processing later. if the
6094 * truncate log does not require processing, a *tl_copy is set to
6096 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
6098 struct ocfs2_dinode
**tl_copy
)
6101 struct inode
*tl_inode
= NULL
;
6102 struct buffer_head
*tl_bh
= NULL
;
6103 struct ocfs2_dinode
*di
;
6104 struct ocfs2_truncate_log
*tl
;
6108 mlog(0, "recover truncate log from slot %d\n", slot_num
);
6110 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
6116 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
6118 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6119 * validated by the underlying call to ocfs2_read_inode_block(),
6120 * so any corruption is a code bug */
6121 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
6123 tl
= &di
->id2
.i_dealloc
;
6124 if (le16_to_cpu(tl
->tl_used
)) {
6125 mlog(0, "We'll have %u logs to recover\n",
6126 le16_to_cpu(tl
->tl_used
));
6128 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
6135 /* Assuming the write-out below goes well, this copy
6136 * will be passed back to recovery for processing. */
6137 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
6139 /* All we need to do to clear the truncate log is set
6143 ocfs2_compute_meta_ecc(osb
->sb
, tl_bh
->b_data
, &di
->i_check
);
6144 status
= ocfs2_write_block(osb
, tl_bh
, INODE_CACHE(tl_inode
));
6156 if (status
< 0 && (*tl_copy
)) {
6165 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
6166 struct ocfs2_dinode
*tl_copy
)
6170 unsigned int clusters
, num_recs
, start_cluster
;
6173 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6174 struct ocfs2_truncate_log
*tl
;
6178 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
6179 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
6183 tl
= &tl_copy
->id2
.i_dealloc
;
6184 num_recs
= le16_to_cpu(tl
->tl_used
);
6185 mlog(0, "cleanup %u records from %llu\n", num_recs
,
6186 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
6188 mutex_lock(&tl_inode
->i_mutex
);
6189 for(i
= 0; i
< num_recs
; i
++) {
6190 if (ocfs2_truncate_log_needs_flush(osb
)) {
6191 status
= __ocfs2_flush_truncate_log(osb
);
6198 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6199 if (IS_ERR(handle
)) {
6200 status
= PTR_ERR(handle
);
6205 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
6206 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
6207 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
6209 status
= ocfs2_truncate_log_append(osb
, handle
,
6210 start_blk
, clusters
);
6211 ocfs2_commit_trans(osb
, handle
);
6219 mutex_unlock(&tl_inode
->i_mutex
);
6225 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
6228 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6233 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
6234 flush_workqueue(ocfs2_wq
);
6236 status
= ocfs2_flush_truncate_log(osb
);
6240 brelse(osb
->osb_tl_bh
);
6241 iput(osb
->osb_tl_inode
);
6247 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
6250 struct inode
*tl_inode
= NULL
;
6251 struct buffer_head
*tl_bh
= NULL
;
6255 status
= ocfs2_get_truncate_log_info(osb
,
6262 /* ocfs2_truncate_log_shutdown keys on the existence of
6263 * osb->osb_tl_inode so we don't set any of the osb variables
6264 * until we're sure all is well. */
6265 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
6266 ocfs2_truncate_log_worker
);
6267 osb
->osb_tl_bh
= tl_bh
;
6268 osb
->osb_tl_inode
= tl_inode
;
6275 * Delayed de-allocation of suballocator blocks.
6277 * Some sets of block de-allocations might involve multiple suballocator inodes.
6279 * The locking for this can get extremely complicated, especially when
6280 * the suballocator inodes to delete from aren't known until deep
6281 * within an unrelated codepath.
6283 * ocfs2_extent_block structures are a good example of this - an inode
6284 * btree could have been grown by any number of nodes each allocating
6285 * out of their own suballoc inode.
6287 * These structures allow the delay of block de-allocation until a
6288 * later time, when locking of multiple cluster inodes won't cause
6293 * Describe a single bit freed from a suballocator. For the block
6294 * suballocators, it represents one block. For the global cluster
6295 * allocator, it represents some clusters and free_bit indicates
6298 struct ocfs2_cached_block_free
{
6299 struct ocfs2_cached_block_free
*free_next
;
6301 unsigned int free_bit
;
6304 struct ocfs2_per_slot_free_list
{
6305 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
6308 struct ocfs2_cached_block_free
*f_first
;
6311 static int ocfs2_free_cached_blocks(struct ocfs2_super
*osb
,
6314 struct ocfs2_cached_block_free
*head
)
6319 struct inode
*inode
;
6320 struct buffer_head
*di_bh
= NULL
;
6321 struct ocfs2_cached_block_free
*tmp
;
6323 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
6330 mutex_lock(&inode
->i_mutex
);
6332 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
6338 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
6339 if (IS_ERR(handle
)) {
6340 ret
= PTR_ERR(handle
);
6346 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
6348 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6349 head
->free_bit
, (unsigned long long)head
->free_blk
);
6351 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
6352 head
->free_bit
, bg_blkno
, 1);
6358 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
6365 head
= head
->free_next
;
6370 ocfs2_commit_trans(osb
, handle
);
6373 ocfs2_inode_unlock(inode
, 1);
6376 mutex_unlock(&inode
->i_mutex
);
6380 /* Premature exit may have left some dangling items. */
6382 head
= head
->free_next
;
6389 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6390 u64 blkno
, unsigned int bit
)
6393 struct ocfs2_cached_block_free
*item
;
6395 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6402 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6403 bit
, (unsigned long long)blkno
);
6405 item
->free_blk
= blkno
;
6406 item
->free_bit
= bit
;
6407 item
->free_next
= ctxt
->c_global_allocator
;
6409 ctxt
->c_global_allocator
= item
;
6413 static int ocfs2_free_cached_clusters(struct ocfs2_super
*osb
,
6414 struct ocfs2_cached_block_free
*head
)
6416 struct ocfs2_cached_block_free
*tmp
;
6417 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6421 mutex_lock(&tl_inode
->i_mutex
);
6424 if (ocfs2_truncate_log_needs_flush(osb
)) {
6425 ret
= __ocfs2_flush_truncate_log(osb
);
6432 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6433 if (IS_ERR(handle
)) {
6434 ret
= PTR_ERR(handle
);
6439 ret
= ocfs2_truncate_log_append(osb
, handle
, head
->free_blk
,
6442 ocfs2_commit_trans(osb
, handle
);
6444 head
= head
->free_next
;
6453 mutex_unlock(&tl_inode
->i_mutex
);
6456 /* Premature exit may have left some dangling items. */
6458 head
= head
->free_next
;
6465 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
6466 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6469 struct ocfs2_per_slot_free_list
*fl
;
6474 while (ctxt
->c_first_suballocator
) {
6475 fl
= ctxt
->c_first_suballocator
;
6478 mlog(0, "Free items: (type %u, slot %d)\n",
6479 fl
->f_inode_type
, fl
->f_slot
);
6480 ret2
= ocfs2_free_cached_blocks(osb
,
6490 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
6494 if (ctxt
->c_global_allocator
) {
6495 ret2
= ocfs2_free_cached_clusters(osb
,
6496 ctxt
->c_global_allocator
);
6502 ctxt
->c_global_allocator
= NULL
;
6508 static struct ocfs2_per_slot_free_list
*
6509 ocfs2_find_per_slot_free_list(int type
,
6511 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6513 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
6516 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
6519 fl
= fl
->f_next_suballocator
;
6522 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
6524 fl
->f_inode_type
= type
;
6527 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
6529 ctxt
->c_first_suballocator
= fl
;
6534 int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6535 int type
, int slot
, u64 blkno
,
6539 struct ocfs2_per_slot_free_list
*fl
;
6540 struct ocfs2_cached_block_free
*item
;
6542 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6549 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6556 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6557 type
, slot
, bit
, (unsigned long long)blkno
);
6559 item
->free_blk
= blkno
;
6560 item
->free_bit
= bit
;
6561 item
->free_next
= fl
->f_first
;
6570 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6571 struct ocfs2_extent_block
*eb
)
6573 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6574 le16_to_cpu(eb
->h_suballoc_slot
),
6575 le64_to_cpu(eb
->h_blkno
),
6576 le16_to_cpu(eb
->h_suballoc_bit
));
6579 /* This function will figure out whether the currently last extent
6580 * block will be deleted, and if it will, what the new last extent
6581 * block will be so we can update his h_next_leaf_blk field, as well
6582 * as the dinodes i_last_eb_blk */
6583 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6584 unsigned int clusters_to_del
,
6585 struct ocfs2_path
*path
,
6586 struct buffer_head
**new_last_eb
)
6588 int next_free
, ret
= 0;
6590 struct ocfs2_extent_rec
*rec
;
6591 struct ocfs2_extent_block
*eb
;
6592 struct ocfs2_extent_list
*el
;
6593 struct buffer_head
*bh
= NULL
;
6595 *new_last_eb
= NULL
;
6597 /* we have no tree, so of course, no last_eb. */
6598 if (!path
->p_tree_depth
)
6601 /* trunc to zero special case - this makes tree_depth = 0
6602 * regardless of what it is. */
6603 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6606 el
= path_leaf_el(path
);
6607 BUG_ON(!el
->l_next_free_rec
);
6610 * Make sure that this extent list will actually be empty
6611 * after we clear away the data. We can shortcut out if
6612 * there's more than one non-empty extent in the
6613 * list. Otherwise, a check of the remaining extent is
6616 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6618 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6622 /* We may have a valid extent in index 1, check it. */
6624 rec
= &el
->l_recs
[1];
6627 * Fall through - no more nonempty extents, so we want
6628 * to delete this leaf.
6634 rec
= &el
->l_recs
[0];
6639 * Check it we'll only be trimming off the end of this
6642 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6646 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6652 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), path_root_el(path
), cpos
, &bh
);
6658 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6661 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6662 * Any corruption is a code bug. */
6663 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
6666 get_bh(*new_last_eb
);
6667 mlog(0, "returning block %llu, (cpos: %u)\n",
6668 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6676 * Trim some clusters off the rightmost edge of a tree. Only called
6679 * The caller needs to:
6680 * - start journaling of each path component.
6681 * - compute and fully set up any new last ext block
6683 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6684 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6685 u32 clusters_to_del
, u64
*delete_start
, u8
*flags
)
6687 int ret
, i
, index
= path
->p_tree_depth
;
6690 struct buffer_head
*bh
;
6691 struct ocfs2_extent_list
*el
;
6692 struct ocfs2_extent_rec
*rec
;
6697 while (index
>= 0) {
6698 bh
= path
->p_node
[index
].bh
;
6699 el
= path
->p_node
[index
].el
;
6701 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6702 index
, (unsigned long long)bh
->b_blocknr
);
6704 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6707 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6708 ocfs2_error(inode
->i_sb
,
6709 "Inode %lu has invalid ext. block %llu",
6711 (unsigned long long)bh
->b_blocknr
);
6717 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6718 rec
= &el
->l_recs
[i
];
6720 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6721 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6722 ocfs2_rec_clusters(el
, rec
),
6723 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6724 le16_to_cpu(el
->l_next_free_rec
));
6726 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6728 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6730 * If the leaf block contains a single empty
6731 * extent and no records, we can just remove
6734 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6736 sizeof(struct ocfs2_extent_rec
));
6737 el
->l_next_free_rec
= cpu_to_le16(0);
6743 * Remove any empty extents by shifting things
6744 * left. That should make life much easier on
6745 * the code below. This condition is rare
6746 * enough that we shouldn't see a performance
6749 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6750 le16_add_cpu(&el
->l_next_free_rec
, -1);
6753 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6754 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6756 memset(&el
->l_recs
[i
], 0,
6757 sizeof(struct ocfs2_extent_rec
));
6760 * We've modified our extent list. The
6761 * simplest way to handle this change
6762 * is to being the search from the
6765 goto find_tail_record
;
6768 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6771 * We'll use "new_edge" on our way back up the
6772 * tree to know what our rightmost cpos is.
6774 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6775 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6778 * The caller will use this to delete data blocks.
6780 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6781 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6782 le16_to_cpu(rec
->e_leaf_clusters
));
6783 *flags
= rec
->e_flags
;
6786 * If it's now empty, remove this record.
6788 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6790 sizeof(struct ocfs2_extent_rec
));
6791 le16_add_cpu(&el
->l_next_free_rec
, -1);
6794 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6796 sizeof(struct ocfs2_extent_rec
));
6797 le16_add_cpu(&el
->l_next_free_rec
, -1);
6802 /* Can this actually happen? */
6803 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6807 * We never actually deleted any clusters
6808 * because our leaf was empty. There's no
6809 * reason to adjust the rightmost edge then.
6814 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6815 le32_add_cpu(&rec
->e_int_clusters
,
6816 -le32_to_cpu(rec
->e_cpos
));
6819 * A deleted child record should have been
6822 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6826 ret
= ocfs2_journal_dirty(handle
, bh
);
6832 mlog(0, "extent list container %llu, after: record %d: "
6833 "(%u, %u, %llu), next = %u.\n",
6834 (unsigned long long)bh
->b_blocknr
, i
,
6835 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6836 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6837 le16_to_cpu(el
->l_next_free_rec
));
6840 * We must be careful to only attempt delete of an
6841 * extent block (and not the root inode block).
6843 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6844 struct ocfs2_extent_block
*eb
=
6845 (struct ocfs2_extent_block
*)bh
->b_data
;
6848 * Save this for use when processing the
6851 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6853 mlog(0, "deleting this extent block.\n");
6855 ocfs2_remove_from_cache(INODE_CACHE(inode
), bh
);
6857 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6858 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6859 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6861 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6862 /* An error here is not fatal. */
6877 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6878 unsigned int clusters_to_del
,
6879 struct inode
*inode
,
6880 struct buffer_head
*fe_bh
,
6882 struct ocfs2_truncate_context
*tc
,
6883 struct ocfs2_path
*path
,
6884 struct ocfs2_alloc_context
*meta_ac
)
6887 struct ocfs2_dinode
*fe
;
6888 struct ocfs2_extent_block
*last_eb
= NULL
;
6889 struct ocfs2_extent_list
*el
;
6890 struct buffer_head
*last_eb_bh
= NULL
;
6894 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6896 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6904 * Each component will be touched, so we might as well journal
6905 * here to avoid having to handle errors later.
6907 status
= ocfs2_journal_access_path(INODE_CACHE(inode
), handle
, path
);
6914 status
= ocfs2_journal_access_eb(handle
, INODE_CACHE(inode
), last_eb_bh
,
6915 OCFS2_JOURNAL_ACCESS_WRITE
);
6921 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6924 el
= &(fe
->id2
.i_list
);
6927 * Lower levels depend on this never happening, but it's best
6928 * to check it up here before changing the tree.
6930 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6931 ocfs2_error(inode
->i_sb
,
6932 "Inode %lu has an empty extent record, depth %u\n",
6933 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6938 vfs_dq_free_space_nodirty(inode
,
6939 ocfs2_clusters_to_bytes(osb
->sb
, clusters_to_del
));
6940 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6941 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6943 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6944 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6945 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6947 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6948 clusters_to_del
, &delete_blk
, &rec_flags
);
6954 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6955 /* trunc to zero is a special case. */
6956 el
->l_tree_depth
= 0;
6957 fe
->i_last_eb_blk
= 0;
6959 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6961 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6968 /* If there will be a new last extent block, then by
6969 * definition, there cannot be any leaves to the right of
6971 last_eb
->h_next_leaf_blk
= 0;
6972 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6980 if (rec_flags
& OCFS2_EXT_REFCOUNTED
)
6981 status
= ocfs2_decrease_refcount(inode
, handle
,
6982 ocfs2_blocks_to_clusters(osb
->sb
,
6984 clusters_to_del
, meta_ac
,
6985 &tc
->tc_dealloc
, 1);
6987 status
= ocfs2_truncate_log_append(osb
, handle
,
7002 static int ocfs2_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
7004 set_buffer_uptodate(bh
);
7005 mark_buffer_dirty(bh
);
7009 void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
7010 unsigned int from
, unsigned int to
,
7011 struct page
*page
, int zero
, u64
*phys
)
7013 int ret
, partial
= 0;
7015 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
7020 zero_user_segment(page
, from
, to
);
7023 * Need to set the buffers we zero'd into uptodate
7024 * here if they aren't - ocfs2_map_page_blocks()
7025 * might've skipped some
7027 ret
= walk_page_buffers(handle
, page_buffers(page
),
7032 else if (ocfs2_should_order_data(inode
)) {
7033 ret
= ocfs2_jbd2_file_inode(handle
, inode
);
7039 SetPageUptodate(page
);
7041 flush_dcache_page(page
);
7044 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
7045 loff_t end
, struct page
**pages
,
7046 int numpages
, u64 phys
, handle_t
*handle
)
7050 unsigned int from
, to
= PAGE_CACHE_SIZE
;
7051 struct super_block
*sb
= inode
->i_sb
;
7053 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
7058 to
= PAGE_CACHE_SIZE
;
7059 for(i
= 0; i
< numpages
; i
++) {
7062 from
= start
& (PAGE_CACHE_SIZE
- 1);
7063 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
7064 to
= end
& (PAGE_CACHE_SIZE
- 1);
7066 BUG_ON(from
> PAGE_CACHE_SIZE
);
7067 BUG_ON(to
> PAGE_CACHE_SIZE
);
7069 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
7072 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
7076 ocfs2_unlock_and_free_pages(pages
, numpages
);
7079 int ocfs2_grab_pages(struct inode
*inode
, loff_t start
, loff_t end
,
7080 struct page
**pages
, int *num
)
7082 int numpages
, ret
= 0;
7083 struct address_space
*mapping
= inode
->i_mapping
;
7084 unsigned long index
;
7085 loff_t last_page_bytes
;
7087 BUG_ON(start
> end
);
7090 last_page_bytes
= PAGE_ALIGN(end
);
7091 index
= start
>> PAGE_CACHE_SHIFT
;
7093 pages
[numpages
] = grab_cache_page(mapping
, index
);
7094 if (!pages
[numpages
]) {
7102 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
7107 ocfs2_unlock_and_free_pages(pages
, numpages
);
7116 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
7117 struct page
**pages
, int *num
)
7119 struct super_block
*sb
= inode
->i_sb
;
7121 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
7122 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
7124 return ocfs2_grab_pages(inode
, start
, end
, pages
, num
);
7128 * Zero the area past i_size but still within an allocated
7129 * cluster. This avoids exposing nonzero data on subsequent file
7132 * We need to call this before i_size is updated on the inode because
7133 * otherwise block_write_full_page() will skip writeout of pages past
7134 * i_size. The new_i_size parameter is passed for this reason.
7136 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
7137 u64 range_start
, u64 range_end
)
7139 int ret
= 0, numpages
;
7140 struct page
**pages
= NULL
;
7142 unsigned int ext_flags
;
7143 struct super_block
*sb
= inode
->i_sb
;
7146 * File systems which don't support sparse files zero on every
7149 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
7152 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
7153 sizeof(struct page
*), GFP_NOFS
);
7154 if (pages
== NULL
) {
7160 if (range_start
== range_end
)
7163 ret
= ocfs2_extent_map_get_blocks(inode
,
7164 range_start
>> sb
->s_blocksize_bits
,
7165 &phys
, NULL
, &ext_flags
);
7172 * Tail is a hole, or is marked unwritten. In either case, we
7173 * can count on read and write to return/push zero's.
7175 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
7178 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
7185 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
7186 numpages
, phys
, handle
);
7189 * Initiate writeout of the pages we zero'd here. We don't
7190 * wait on them - the truncate_inode_pages() call later will
7193 ret
= filemap_fdatawrite_range(inode
->i_mapping
, range_start
,
7205 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
7206 struct ocfs2_dinode
*di
)
7208 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
7209 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
7211 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
7212 memset(&di
->id2
, 0, blocksize
-
7213 offsetof(struct ocfs2_dinode
, id2
) -
7216 memset(&di
->id2
, 0, blocksize
-
7217 offsetof(struct ocfs2_dinode
, id2
));
7220 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
7221 struct ocfs2_dinode
*di
)
7223 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
7224 di
->id2
.i_list
.l_tree_depth
= 0;
7225 di
->id2
.i_list
.l_next_free_rec
= 0;
7226 di
->id2
.i_list
.l_count
= cpu_to_le16(
7227 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
7230 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
7232 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
7233 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7235 spin_lock(&oi
->ip_lock
);
7236 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
7237 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
7238 spin_unlock(&oi
->ip_lock
);
7241 * We clear the entire i_data structure here so that all
7242 * fields can be properly initialized.
7244 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
7246 idata
->id_count
= cpu_to_le16(
7247 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
7250 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
7251 struct buffer_head
*di_bh
)
7253 int ret
, i
, has_data
, num_pages
= 0;
7255 u64
uninitialized_var(block
);
7256 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
7257 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7258 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7259 struct ocfs2_alloc_context
*data_ac
= NULL
;
7260 struct page
**pages
= NULL
;
7261 loff_t end
= osb
->s_clustersize
;
7262 struct ocfs2_extent_tree et
;
7265 has_data
= i_size_read(inode
) ? 1 : 0;
7268 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
7269 sizeof(struct page
*), GFP_NOFS
);
7270 if (pages
== NULL
) {
7276 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
7283 handle
= ocfs2_start_trans(osb
,
7284 ocfs2_inline_to_extents_credits(osb
->sb
));
7285 if (IS_ERR(handle
)) {
7286 ret
= PTR_ERR(handle
);
7291 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
7292 OCFS2_JOURNAL_ACCESS_WRITE
);
7300 unsigned int page_end
;
7303 if (vfs_dq_alloc_space_nodirty(inode
,
7304 ocfs2_clusters_to_bytes(osb
->sb
, 1))) {
7310 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
7318 * Save two copies, one for insert, and one that can
7319 * be changed by ocfs2_map_and_dirty_page() below.
7321 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
7324 * Non sparse file systems zero on extend, so no need
7327 if (!ocfs2_sparse_alloc(osb
) &&
7328 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
7329 end
= PAGE_CACHE_SIZE
;
7331 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
7338 * This should populate the 1st page for us and mark
7341 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
7347 page_end
= PAGE_CACHE_SIZE
;
7348 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
7349 page_end
= osb
->s_clustersize
;
7351 for (i
= 0; i
< num_pages
; i
++)
7352 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
7353 pages
[i
], i
> 0, &phys
);
7356 spin_lock(&oi
->ip_lock
);
7357 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
7358 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
7359 spin_unlock(&oi
->ip_lock
);
7361 ocfs2_dinode_new_extent_list(inode
, di
);
7363 ocfs2_journal_dirty(handle
, di_bh
);
7367 * An error at this point should be extremely rare. If
7368 * this proves to be false, we could always re-build
7369 * the in-inode data from our pages.
7371 ocfs2_init_dinode_extent_tree(&et
, INODE_CACHE(inode
), di_bh
);
7372 ret
= ocfs2_insert_extent(handle
, &et
, 0, block
, 1, 0, NULL
);
7378 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7382 if (ret
< 0 && did_quota
)
7383 vfs_dq_free_space_nodirty(inode
,
7384 ocfs2_clusters_to_bytes(osb
->sb
, 1));
7386 ocfs2_commit_trans(osb
, handle
);
7390 ocfs2_free_alloc_context(data_ac
);
7394 ocfs2_unlock_and_free_pages(pages
, num_pages
);
7402 * It is expected, that by the time you call this function,
7403 * inode->i_size and fe->i_size have been adjusted.
7405 * WARNING: This will kfree the truncate context
7407 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
7408 struct inode
*inode
,
7409 struct buffer_head
*fe_bh
,
7410 struct ocfs2_truncate_context
*tc
)
7412 int status
, i
, credits
, tl_sem
= 0;
7413 u32 clusters_to_del
, new_highest_cpos
, range
;
7415 struct ocfs2_extent_list
*el
;
7416 handle_t
*handle
= NULL
;
7417 struct inode
*tl_inode
= osb
->osb_tl_inode
;
7418 struct ocfs2_path
*path
= NULL
;
7419 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
7420 struct ocfs2_alloc_context
*meta_ac
= NULL
;
7421 struct ocfs2_refcount_tree
*ref_tree
= NULL
;
7425 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
7426 i_size_read(inode
));
7428 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
,
7429 ocfs2_journal_access_di
);
7436 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
7440 * Check that we still have allocation to delete.
7442 if (OCFS2_I(inode
)->ip_clusters
== 0) {
7450 * Truncate always works against the rightmost tree branch.
7452 status
= ocfs2_find_path(INODE_CACHE(inode
), path
, UINT_MAX
);
7458 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7459 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
7462 * By now, el will point to the extent list on the bottom most
7463 * portion of this tree. Only the tail record is considered in
7466 * We handle the following cases, in order:
7467 * - empty extent: delete the remaining branch
7468 * - remove the entire record
7469 * - remove a partial record
7470 * - no record needs to be removed (truncate has completed)
7472 el
= path_leaf_el(path
);
7473 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
7474 ocfs2_error(inode
->i_sb
,
7475 "Inode %llu has empty extent block at %llu\n",
7476 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7477 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7482 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
7483 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
7484 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7485 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
7486 clusters_to_del
= 0;
7487 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
7488 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7489 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
7490 } else if (range
> new_highest_cpos
) {
7491 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
7492 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
7494 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
) +
7495 ocfs2_clusters_to_blocks(inode
->i_sb
,
7496 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) -
7503 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7504 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7506 if (el
->l_recs
[i
].e_flags
& OCFS2_EXT_REFCOUNTED
&& clusters_to_del
) {
7507 BUG_ON(!(OCFS2_I(inode
)->ip_dyn_features
&
7508 OCFS2_HAS_REFCOUNT_FL
));
7510 status
= ocfs2_lock_refcount_tree(osb
,
7511 le64_to_cpu(di
->i_refcount_loc
),
7512 1, &ref_tree
, NULL
);
7518 status
= ocfs2_prepare_refcount_change_for_del(inode
, fe_bh
,
7529 mutex_lock(&tl_inode
->i_mutex
);
7531 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7532 * record is free for use. If there isn't any, we flush to get
7533 * an empty truncate log. */
7534 if (ocfs2_truncate_log_needs_flush(osb
)) {
7535 status
= __ocfs2_flush_truncate_log(osb
);
7542 credits
+= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
7543 (struct ocfs2_dinode
*)fe_bh
->b_data
,
7545 handle
= ocfs2_start_trans(osb
, credits
);
7546 if (IS_ERR(handle
)) {
7547 status
= PTR_ERR(handle
);
7553 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
7560 mutex_unlock(&tl_inode
->i_mutex
);
7563 ocfs2_commit_trans(osb
, handle
);
7566 ocfs2_reinit_path(path
, 1);
7569 ocfs2_free_alloc_context(meta_ac
);
7574 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
7579 * The check above will catch the case where we've truncated
7580 * away all allocation.
7586 ocfs2_schedule_truncate_log_flush(osb
, 1);
7589 mutex_unlock(&tl_inode
->i_mutex
);
7592 ocfs2_commit_trans(osb
, handle
);
7595 ocfs2_free_alloc_context(meta_ac
);
7598 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
7600 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
7602 ocfs2_free_path(path
);
7604 /* This will drop the ext_alloc cluster lock for us */
7605 ocfs2_free_truncate_context(tc
);
7612 * Expects the inode to already be locked.
7614 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
7615 struct inode
*inode
,
7616 struct buffer_head
*fe_bh
,
7617 struct ocfs2_truncate_context
**tc
)
7620 unsigned int new_i_clusters
;
7621 struct ocfs2_dinode
*fe
;
7622 struct ocfs2_extent_block
*eb
;
7623 struct buffer_head
*last_eb_bh
= NULL
;
7629 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7630 i_size_read(inode
));
7631 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7633 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7634 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7635 (unsigned long long)le64_to_cpu(fe
->i_size
));
7637 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7643 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7645 if (fe
->id2
.i_list
.l_tree_depth
) {
7646 status
= ocfs2_read_extent_block(INODE_CACHE(inode
),
7647 le64_to_cpu(fe
->i_last_eb_blk
),
7653 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7656 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7662 ocfs2_free_truncate_context(*tc
);
7670 * 'start' is inclusive, 'end' is not.
7672 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7673 unsigned int start
, unsigned int end
, int trunc
)
7676 unsigned int numbytes
;
7678 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7679 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7680 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7682 if (end
> i_size_read(inode
))
7683 end
= i_size_read(inode
);
7685 BUG_ON(start
>= end
);
7687 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7688 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7689 !ocfs2_supports_inline_data(osb
)) {
7690 ocfs2_error(inode
->i_sb
,
7691 "Inline data flags for inode %llu don't agree! "
7692 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7693 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7694 le16_to_cpu(di
->i_dyn_features
),
7695 OCFS2_I(inode
)->ip_dyn_features
,
7696 osb
->s_feature_incompat
);
7701 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7702 if (IS_ERR(handle
)) {
7703 ret
= PTR_ERR(handle
);
7708 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
7709 OCFS2_JOURNAL_ACCESS_WRITE
);
7715 numbytes
= end
- start
;
7716 memset(idata
->id_data
+ start
, 0, numbytes
);
7719 * No need to worry about the data page here - it's been
7720 * truncated already and inline data doesn't need it for
7721 * pushing zero's to disk, so we'll let readpage pick it up
7725 i_size_write(inode
, start
);
7726 di
->i_size
= cpu_to_le64(start
);
7729 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7730 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7732 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7733 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7735 ocfs2_journal_dirty(handle
, di_bh
);
7738 ocfs2_commit_trans(osb
, handle
);
7744 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7747 * The caller is responsible for completing deallocation
7748 * before freeing the context.
7750 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7752 "Truncate completion has non-empty dealloc context\n");
7754 brelse(tc
->tc_last_eb_bh
);