1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Copyright (C) 2009 Oracle. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
18 #include <linux/sort.h>
19 #define MLOG_MASK_PREFIX ML_REFCOUNT
20 #include <cluster/masklog.h>
28 #include "buffer_head_io.h"
29 #include "blockcheck.h"
30 #include "refcounttree.h"
33 #include "extent_map.h"
35 static inline struct ocfs2_refcount_tree
*
36 cache_info_to_refcount(struct ocfs2_caching_info
*ci
)
38 return container_of(ci
, struct ocfs2_refcount_tree
, rf_ci
);
41 static int ocfs2_validate_refcount_block(struct super_block
*sb
,
42 struct buffer_head
*bh
)
45 struct ocfs2_refcount_block
*rb
=
46 (struct ocfs2_refcount_block
*)bh
->b_data
;
48 mlog(0, "Validating refcount block %llu\n",
49 (unsigned long long)bh
->b_blocknr
);
51 BUG_ON(!buffer_uptodate(bh
));
54 * If the ecc fails, we return the error but otherwise
55 * leave the filesystem running. We know any error is
56 * local to this block.
58 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &rb
->rf_check
);
60 mlog(ML_ERROR
, "Checksum failed for refcount block %llu\n",
61 (unsigned long long)bh
->b_blocknr
);
66 if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb
)) {
68 "Refcount block #%llu has bad signature %.*s",
69 (unsigned long long)bh
->b_blocknr
, 7,
74 if (le64_to_cpu(rb
->rf_blkno
) != bh
->b_blocknr
) {
76 "Refcount block #%llu has an invalid rf_blkno "
78 (unsigned long long)bh
->b_blocknr
,
79 (unsigned long long)le64_to_cpu(rb
->rf_blkno
));
83 if (le32_to_cpu(rb
->rf_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
85 "Refcount block #%llu has an invalid "
86 "rf_fs_generation of #%u",
87 (unsigned long long)bh
->b_blocknr
,
88 le32_to_cpu(rb
->rf_fs_generation
));
95 static int ocfs2_read_refcount_block(struct ocfs2_caching_info
*ci
,
97 struct buffer_head
**bh
)
100 struct buffer_head
*tmp
= *bh
;
102 rc
= ocfs2_read_block(ci
, rb_blkno
, &tmp
,
103 ocfs2_validate_refcount_block
);
105 /* If ocfs2_read_block() got us a new bh, pass it up. */
112 static u64
ocfs2_refcount_cache_owner(struct ocfs2_caching_info
*ci
)
114 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
119 static struct super_block
*
120 ocfs2_refcount_cache_get_super(struct ocfs2_caching_info
*ci
)
122 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
127 static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info
*ci
)
129 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
131 spin_lock(&rf
->rf_lock
);
134 static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info
*ci
)
136 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
138 spin_unlock(&rf
->rf_lock
);
141 static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info
*ci
)
143 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
145 mutex_lock(&rf
->rf_io_mutex
);
148 static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info
*ci
)
150 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
152 mutex_unlock(&rf
->rf_io_mutex
);
155 static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops
= {
156 .co_owner
= ocfs2_refcount_cache_owner
,
157 .co_get_super
= ocfs2_refcount_cache_get_super
,
158 .co_cache_lock
= ocfs2_refcount_cache_lock
,
159 .co_cache_unlock
= ocfs2_refcount_cache_unlock
,
160 .co_io_lock
= ocfs2_refcount_cache_io_lock
,
161 .co_io_unlock
= ocfs2_refcount_cache_io_unlock
,
164 static struct ocfs2_refcount_tree
*
165 ocfs2_find_refcount_tree(struct ocfs2_super
*osb
, u64 blkno
)
167 struct rb_node
*n
= osb
->osb_rf_lock_tree
.rb_node
;
168 struct ocfs2_refcount_tree
*tree
= NULL
;
171 tree
= rb_entry(n
, struct ocfs2_refcount_tree
, rf_node
);
173 if (blkno
< tree
->rf_blkno
)
175 else if (blkno
> tree
->rf_blkno
)
184 /* osb_lock is already locked. */
185 static void ocfs2_insert_refcount_tree(struct ocfs2_super
*osb
,
186 struct ocfs2_refcount_tree
*new)
188 u64 rf_blkno
= new->rf_blkno
;
189 struct rb_node
*parent
= NULL
;
190 struct rb_node
**p
= &osb
->osb_rf_lock_tree
.rb_node
;
191 struct ocfs2_refcount_tree
*tmp
;
196 tmp
= rb_entry(parent
, struct ocfs2_refcount_tree
,
199 if (rf_blkno
< tmp
->rf_blkno
)
201 else if (rf_blkno
> tmp
->rf_blkno
)
204 /* This should never happen! */
205 mlog(ML_ERROR
, "Duplicate refcount block %llu found!\n",
206 (unsigned long long)rf_blkno
);
211 rb_link_node(&new->rf_node
, parent
, p
);
212 rb_insert_color(&new->rf_node
, &osb
->osb_rf_lock_tree
);
215 static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree
*tree
)
217 ocfs2_metadata_cache_exit(&tree
->rf_ci
);
218 ocfs2_simple_drop_lockres(OCFS2_SB(tree
->rf_sb
), &tree
->rf_lockres
);
219 ocfs2_lock_res_free(&tree
->rf_lockres
);
224 ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super
*osb
,
225 struct ocfs2_refcount_tree
*tree
)
227 rb_erase(&tree
->rf_node
, &osb
->osb_rf_lock_tree
);
228 if (osb
->osb_ref_tree_lru
&& osb
->osb_ref_tree_lru
== tree
)
229 osb
->osb_ref_tree_lru
= NULL
;
232 static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super
*osb
,
233 struct ocfs2_refcount_tree
*tree
)
235 spin_lock(&osb
->osb_lock
);
236 ocfs2_erase_refcount_tree_from_list_no_lock(osb
, tree
);
237 spin_unlock(&osb
->osb_lock
);
240 void ocfs2_kref_remove_refcount_tree(struct kref
*kref
)
242 struct ocfs2_refcount_tree
*tree
=
243 container_of(kref
, struct ocfs2_refcount_tree
, rf_getcnt
);
245 ocfs2_free_refcount_tree(tree
);
249 ocfs2_refcount_tree_get(struct ocfs2_refcount_tree
*tree
)
251 kref_get(&tree
->rf_getcnt
);
255 ocfs2_refcount_tree_put(struct ocfs2_refcount_tree
*tree
)
257 kref_put(&tree
->rf_getcnt
, ocfs2_kref_remove_refcount_tree
);
260 static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree
*new,
261 struct super_block
*sb
)
263 ocfs2_metadata_cache_init(&new->rf_ci
, &ocfs2_refcount_caching_ops
);
264 mutex_init(&new->rf_io_mutex
);
266 spin_lock_init(&new->rf_lock
);
269 static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super
*osb
,
270 struct ocfs2_refcount_tree
*new,
271 u64 rf_blkno
, u32 generation
)
273 init_rwsem(&new->rf_sem
);
274 ocfs2_refcount_lock_res_init(&new->rf_lockres
, osb
,
275 rf_blkno
, generation
);
278 static struct ocfs2_refcount_tree
*
279 ocfs2_allocate_refcount_tree(struct ocfs2_super
*osb
, u64 rf_blkno
)
281 struct ocfs2_refcount_tree
*new;
283 new = kzalloc(sizeof(struct ocfs2_refcount_tree
), GFP_NOFS
);
287 new->rf_blkno
= rf_blkno
;
288 kref_init(&new->rf_getcnt
);
289 ocfs2_init_refcount_tree_ci(new, osb
->sb
);
294 static int ocfs2_get_refcount_tree(struct ocfs2_super
*osb
, u64 rf_blkno
,
295 struct ocfs2_refcount_tree
**ret_tree
)
298 struct ocfs2_refcount_tree
*tree
, *new = NULL
;
299 struct buffer_head
*ref_root_bh
= NULL
;
300 struct ocfs2_refcount_block
*ref_rb
;
302 spin_lock(&osb
->osb_lock
);
303 if (osb
->osb_ref_tree_lru
&&
304 osb
->osb_ref_tree_lru
->rf_blkno
== rf_blkno
)
305 tree
= osb
->osb_ref_tree_lru
;
307 tree
= ocfs2_find_refcount_tree(osb
, rf_blkno
);
311 spin_unlock(&osb
->osb_lock
);
313 new = ocfs2_allocate_refcount_tree(osb
, rf_blkno
);
320 * We need the generation to create the refcount tree lock and since
321 * it isn't changed during the tree modification, we are safe here to
322 * read without protection.
323 * We also have to purge the cache after we create the lock since the
324 * refcount block may have the stale data. It can only be trusted when
325 * we hold the refcount lock.
327 ret
= ocfs2_read_refcount_block(&new->rf_ci
, rf_blkno
, &ref_root_bh
);
330 ocfs2_metadata_cache_exit(&new->rf_ci
);
335 ref_rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
336 new->rf_generation
= le32_to_cpu(ref_rb
->rf_generation
);
337 ocfs2_init_refcount_tree_lock(osb
, new, rf_blkno
,
339 ocfs2_metadata_cache_purge(&new->rf_ci
);
341 spin_lock(&osb
->osb_lock
);
342 tree
= ocfs2_find_refcount_tree(osb
, rf_blkno
);
346 ocfs2_insert_refcount_tree(osb
, new);
354 osb
->osb_ref_tree_lru
= tree
;
356 spin_unlock(&osb
->osb_lock
);
359 ocfs2_free_refcount_tree(new);
365 static int ocfs2_get_refcount_block(struct inode
*inode
, u64
*ref_blkno
)
368 struct buffer_head
*di_bh
= NULL
;
369 struct ocfs2_dinode
*di
;
371 ret
= ocfs2_read_inode_block(inode
, &di_bh
);
377 BUG_ON(!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
379 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
380 *ref_blkno
= le64_to_cpu(di
->i_refcount_loc
);
386 static int __ocfs2_lock_refcount_tree(struct ocfs2_super
*osb
,
387 struct ocfs2_refcount_tree
*tree
, int rw
)
391 ret
= ocfs2_refcount_lock(tree
, rw
);
398 down_write(&tree
->rf_sem
);
400 down_read(&tree
->rf_sem
);
407 * Lock the refcount tree pointed by ref_blkno and return the tree.
408 * In most case, we lock the tree and read the refcount block.
409 * So read it here if the caller really needs it.
411 * If the tree has been re-created by other node, it will free the
412 * old one and re-create it.
414 int ocfs2_lock_refcount_tree(struct ocfs2_super
*osb
,
415 u64 ref_blkno
, int rw
,
416 struct ocfs2_refcount_tree
**ret_tree
,
417 struct buffer_head
**ref_bh
)
419 int ret
, delete_tree
= 0;
420 struct ocfs2_refcount_tree
*tree
= NULL
;
421 struct buffer_head
*ref_root_bh
= NULL
;
422 struct ocfs2_refcount_block
*rb
;
425 ret
= ocfs2_get_refcount_tree(osb
, ref_blkno
, &tree
);
431 ocfs2_refcount_tree_get(tree
);
433 ret
= __ocfs2_lock_refcount_tree(osb
, tree
, rw
);
436 ocfs2_refcount_tree_put(tree
);
440 ret
= ocfs2_read_refcount_block(&tree
->rf_ci
, tree
->rf_blkno
,
444 ocfs2_unlock_refcount_tree(osb
, tree
, rw
);
445 ocfs2_refcount_tree_put(tree
);
449 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
451 * If the refcount block has been freed and re-created, we may need
452 * to recreate the refcount tree also.
454 * Here we just remove the tree from the rb-tree, and the last
455 * kref holder will unlock and delete this refcount_tree.
456 * Then we goto "again" and ocfs2_get_refcount_tree will create
457 * the new refcount tree for us.
459 if (tree
->rf_generation
!= le32_to_cpu(rb
->rf_generation
)) {
460 if (!tree
->rf_removed
) {
461 ocfs2_erase_refcount_tree_from_list(osb
, tree
);
462 tree
->rf_removed
= 1;
466 ocfs2_unlock_refcount_tree(osb
, tree
, rw
);
468 * We get an extra reference when we create the refcount
469 * tree, so another put will destroy it.
472 ocfs2_refcount_tree_put(tree
);
480 *ref_bh
= ref_root_bh
;
488 int ocfs2_lock_refcount_tree_by_inode(struct inode
*inode
, int rw
,
489 struct ocfs2_refcount_tree
**ret_tree
,
490 struct buffer_head
**ref_bh
)
495 ret
= ocfs2_get_refcount_block(inode
, &ref_blkno
);
501 return ocfs2_lock_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_blkno
,
502 rw
, ret_tree
, ref_bh
);
505 void ocfs2_unlock_refcount_tree(struct ocfs2_super
*osb
,
506 struct ocfs2_refcount_tree
*tree
, int rw
)
509 up_write(&tree
->rf_sem
);
511 up_read(&tree
->rf_sem
);
513 ocfs2_refcount_unlock(tree
, rw
);
514 ocfs2_refcount_tree_put(tree
);
517 void ocfs2_purge_refcount_trees(struct ocfs2_super
*osb
)
519 struct rb_node
*node
;
520 struct ocfs2_refcount_tree
*tree
;
521 struct rb_root
*root
= &osb
->osb_rf_lock_tree
;
523 while ((node
= rb_last(root
)) != NULL
) {
524 tree
= rb_entry(node
, struct ocfs2_refcount_tree
, rf_node
);
526 mlog(0, "Purge tree %llu\n",
527 (unsigned long long) tree
->rf_blkno
);
529 rb_erase(&tree
->rf_node
, root
);
530 ocfs2_free_refcount_tree(tree
);
535 * Create a refcount tree for an inode.
536 * We take for granted that the inode is already locked.
538 static int ocfs2_create_refcount_tree(struct inode
*inode
,
539 struct buffer_head
*di_bh
)
542 handle_t
*handle
= NULL
;
543 struct ocfs2_alloc_context
*meta_ac
= NULL
;
544 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
545 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
546 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
547 struct buffer_head
*new_bh
= NULL
;
548 struct ocfs2_refcount_block
*rb
;
549 struct ocfs2_refcount_tree
*new_tree
= NULL
, *tree
= NULL
;
550 u16 suballoc_bit_start
;
554 BUG_ON(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
);
556 mlog(0, "create tree for inode %lu\n", inode
->i_ino
);
558 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &meta_ac
);
564 handle
= ocfs2_start_trans(osb
, OCFS2_REFCOUNT_TREE_CREATE_CREDITS
);
565 if (IS_ERR(handle
)) {
566 ret
= PTR_ERR(handle
);
571 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
572 OCFS2_JOURNAL_ACCESS_WRITE
);
578 ret
= ocfs2_claim_metadata(osb
, handle
, meta_ac
, 1,
579 &suballoc_bit_start
, &num_got
,
586 new_tree
= ocfs2_allocate_refcount_tree(osb
, first_blkno
);
593 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
594 ocfs2_set_new_buffer_uptodate(&new_tree
->rf_ci
, new_bh
);
596 ret
= ocfs2_journal_access_rb(handle
, &new_tree
->rf_ci
, new_bh
,
597 OCFS2_JOURNAL_ACCESS_CREATE
);
603 /* Initialize ocfs2_refcount_block. */
604 rb
= (struct ocfs2_refcount_block
*)new_bh
->b_data
;
605 memset(rb
, 0, inode
->i_sb
->s_blocksize
);
606 strcpy((void *)rb
, OCFS2_REFCOUNT_BLOCK_SIGNATURE
);
607 rb
->rf_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
608 rb
->rf_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
609 rb
->rf_fs_generation
= cpu_to_le32(osb
->fs_generation
);
610 rb
->rf_blkno
= cpu_to_le64(first_blkno
);
611 rb
->rf_count
= cpu_to_le32(1);
612 rb
->rf_records
.rl_count
=
613 cpu_to_le16(ocfs2_refcount_recs_per_rb(osb
->sb
));
614 spin_lock(&osb
->osb_lock
);
615 rb
->rf_generation
= osb
->s_next_generation
++;
616 spin_unlock(&osb
->osb_lock
);
618 ocfs2_journal_dirty(handle
, new_bh
);
620 spin_lock(&oi
->ip_lock
);
621 oi
->ip_dyn_features
|= OCFS2_HAS_REFCOUNT_FL
;
622 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
623 di
->i_refcount_loc
= cpu_to_le64(first_blkno
);
624 spin_unlock(&oi
->ip_lock
);
626 mlog(0, "created tree for inode %lu, refblock %llu\n",
627 inode
->i_ino
, (unsigned long long)first_blkno
);
629 ocfs2_journal_dirty(handle
, di_bh
);
632 * We have to init the tree lock here since it will use
633 * the generation number to create it.
635 new_tree
->rf_generation
= le32_to_cpu(rb
->rf_generation
);
636 ocfs2_init_refcount_tree_lock(osb
, new_tree
, first_blkno
,
637 new_tree
->rf_generation
);
639 spin_lock(&osb
->osb_lock
);
640 tree
= ocfs2_find_refcount_tree(osb
, first_blkno
);
643 * We've just created a new refcount tree in this block. If
644 * we found a refcount tree on the ocfs2_super, it must be
645 * one we just deleted. We free the old tree before
646 * inserting the new tree.
648 BUG_ON(tree
&& tree
->rf_generation
== new_tree
->rf_generation
);
650 ocfs2_erase_refcount_tree_from_list_no_lock(osb
, tree
);
651 ocfs2_insert_refcount_tree(osb
, new_tree
);
652 spin_unlock(&osb
->osb_lock
);
655 ocfs2_refcount_tree_put(tree
);
658 ocfs2_commit_trans(osb
, handle
);
662 ocfs2_metadata_cache_exit(&new_tree
->rf_ci
);
668 ocfs2_free_alloc_context(meta_ac
);
673 static int ocfs2_set_refcount_tree(struct inode
*inode
,
674 struct buffer_head
*di_bh
,
678 handle_t
*handle
= NULL
;
679 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
680 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
681 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
682 struct buffer_head
*ref_root_bh
= NULL
;
683 struct ocfs2_refcount_block
*rb
;
684 struct ocfs2_refcount_tree
*ref_tree
;
686 BUG_ON(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
);
688 ret
= ocfs2_lock_refcount_tree(osb
, refcount_loc
, 1,
689 &ref_tree
, &ref_root_bh
);
695 handle
= ocfs2_start_trans(osb
, OCFS2_REFCOUNT_TREE_SET_CREDITS
);
696 if (IS_ERR(handle
)) {
697 ret
= PTR_ERR(handle
);
702 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
703 OCFS2_JOURNAL_ACCESS_WRITE
);
709 ret
= ocfs2_journal_access_rb(handle
, &ref_tree
->rf_ci
, ref_root_bh
,
710 OCFS2_JOURNAL_ACCESS_WRITE
);
716 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
717 le32_add_cpu(&rb
->rf_count
, 1);
719 ocfs2_journal_dirty(handle
, ref_root_bh
);
721 spin_lock(&oi
->ip_lock
);
722 oi
->ip_dyn_features
|= OCFS2_HAS_REFCOUNT_FL
;
723 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
724 di
->i_refcount_loc
= cpu_to_le64(refcount_loc
);
725 spin_unlock(&oi
->ip_lock
);
726 ocfs2_journal_dirty(handle
, di_bh
);
729 ocfs2_commit_trans(osb
, handle
);
731 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
737 int ocfs2_remove_refcount_tree(struct inode
*inode
, struct buffer_head
*di_bh
)
739 int ret
, delete_tree
= 0;
740 handle_t
*handle
= NULL
;
741 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
742 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
743 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
744 struct ocfs2_refcount_block
*rb
;
745 struct inode
*alloc_inode
= NULL
;
746 struct buffer_head
*alloc_bh
= NULL
;
747 struct buffer_head
*blk_bh
= NULL
;
748 struct ocfs2_refcount_tree
*ref_tree
;
749 int credits
= OCFS2_REFCOUNT_TREE_REMOVE_CREDITS
;
750 u64 blk
= 0, bg_blkno
= 0, ref_blkno
= le64_to_cpu(di
->i_refcount_loc
);
753 if (!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
))
757 ret
= ocfs2_lock_refcount_tree(osb
, ref_blkno
, 1, &ref_tree
, &blk_bh
);
763 rb
= (struct ocfs2_refcount_block
*)blk_bh
->b_data
;
766 * If we are the last user, we need to free the block.
767 * So lock the allocator ahead.
769 if (le32_to_cpu(rb
->rf_count
) == 1) {
770 blk
= le64_to_cpu(rb
->rf_blkno
);
771 bit
= le16_to_cpu(rb
->rf_suballoc_bit
);
772 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
774 alloc_inode
= ocfs2_get_system_file_inode(osb
,
775 EXTENT_ALLOC_SYSTEM_INODE
,
776 le16_to_cpu(rb
->rf_suballoc_slot
));
782 mutex_lock(&alloc_inode
->i_mutex
);
784 ret
= ocfs2_inode_lock(alloc_inode
, &alloc_bh
, 1);
790 credits
+= OCFS2_SUBALLOC_FREE
;
793 handle
= ocfs2_start_trans(osb
, credits
);
794 if (IS_ERR(handle
)) {
795 ret
= PTR_ERR(handle
);
800 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
801 OCFS2_JOURNAL_ACCESS_WRITE
);
807 ret
= ocfs2_journal_access_rb(handle
, &ref_tree
->rf_ci
, blk_bh
,
808 OCFS2_JOURNAL_ACCESS_WRITE
);
814 spin_lock(&oi
->ip_lock
);
815 oi
->ip_dyn_features
&= ~OCFS2_HAS_REFCOUNT_FL
;
816 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
817 di
->i_refcount_loc
= 0;
818 spin_unlock(&oi
->ip_lock
);
819 ocfs2_journal_dirty(handle
, di_bh
);
821 le32_add_cpu(&rb
->rf_count
, -1);
822 ocfs2_journal_dirty(handle
, blk_bh
);
826 ocfs2_erase_refcount_tree_from_list(osb
, ref_tree
);
827 ret
= ocfs2_free_suballoc_bits(handle
, alloc_inode
,
828 alloc_bh
, bit
, bg_blkno
, 1);
834 ocfs2_commit_trans(osb
, handle
);
837 ocfs2_inode_unlock(alloc_inode
, 1);
842 mutex_unlock(&alloc_inode
->i_mutex
);
846 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
848 ocfs2_refcount_tree_put(ref_tree
);
854 static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info
*ci
,
855 struct buffer_head
*ref_leaf_bh
,
856 u64 cpos
, unsigned int len
,
857 struct ocfs2_refcount_rec
*ret_rec
,
861 struct ocfs2_refcount_block
*rb
=
862 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
863 struct ocfs2_refcount_rec
*rec
= NULL
;
865 for (; i
< le16_to_cpu(rb
->rf_records
.rl_used
); i
++) {
866 rec
= &rb
->rf_records
.rl_recs
[i
];
868 if (le64_to_cpu(rec
->r_cpos
) +
869 le32_to_cpu(rec
->r_clusters
) <= cpos
)
871 else if (le64_to_cpu(rec
->r_cpos
) > cpos
)
874 /* ok, cpos fail in this rec. Just return. */
881 /* We meet with a hole here, so fake the rec. */
882 ret_rec
->r_cpos
= cpu_to_le64(cpos
);
883 ret_rec
->r_refcount
= 0;
884 if (i
< le16_to_cpu(rb
->rf_records
.rl_used
) &&
885 le64_to_cpu(rec
->r_cpos
) < cpos
+ len
)
886 ret_rec
->r_clusters
=
887 cpu_to_le32(le64_to_cpu(rec
->r_cpos
) - cpos
);
889 ret_rec
->r_clusters
= cpu_to_le32(len
);
897 * Given a cpos and len, try to find the refcount record which contains cpos.
898 * 1. If cpos can be found in one refcount record, return the record.
899 * 2. If cpos can't be found, return a fake record which start from cpos
900 * and end at a small value between cpos+len and start of the next record.
901 * This fake record has r_refcount = 0.
903 static int ocfs2_get_refcount_rec(struct ocfs2_caching_info
*ci
,
904 struct buffer_head
*ref_root_bh
,
905 u64 cpos
, unsigned int len
,
906 struct ocfs2_refcount_rec
*ret_rec
,
908 struct buffer_head
**ret_bh
)
910 int ret
= 0, i
, found
;
912 struct ocfs2_extent_list
*el
;
913 struct ocfs2_extent_rec
*tmp
, *rec
= NULL
;
914 struct ocfs2_extent_block
*eb
;
915 struct buffer_head
*eb_bh
= NULL
, *ref_leaf_bh
= NULL
;
916 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
917 struct ocfs2_refcount_block
*rb
=
918 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
920 if (!(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)) {
921 ocfs2_find_refcount_rec_in_rl(ci
, ref_root_bh
, cpos
, len
,
923 *ret_bh
= ref_root_bh
;
929 low_cpos
= cpos
& OCFS2_32BIT_POS_MASK
;
931 if (el
->l_tree_depth
) {
932 ret
= ocfs2_find_leaf(ci
, el
, low_cpos
, &eb_bh
);
938 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
941 if (el
->l_tree_depth
) {
943 "refcount tree %llu has non zero tree "
944 "depth in leaf btree tree block %llu\n",
945 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
946 (unsigned long long)eb_bh
->b_blocknr
);
953 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
954 rec
= &el
->l_recs
[i
];
956 if (le32_to_cpu(rec
->e_cpos
) <= low_cpos
) {
962 /* adjust len when we have ocfs2_extent_rec after it. */
963 if (found
&& i
< le16_to_cpu(el
->l_next_free_rec
) - 1) {
964 tmp
= &el
->l_recs
[i
+1];
966 if (le32_to_cpu(tmp
->e_cpos
) < cpos
+ len
)
967 len
= le32_to_cpu(tmp
->e_cpos
) - cpos
;
970 ret
= ocfs2_read_refcount_block(ci
, le64_to_cpu(rec
->e_blkno
),
977 ocfs2_find_refcount_rec_in_rl(ci
, ref_leaf_bh
, cpos
, len
,
979 *ret_bh
= ref_leaf_bh
;
985 enum ocfs2_ref_rec_contig
{
989 REF_CONTIG_LEFTRIGHT
,
992 static enum ocfs2_ref_rec_contig
993 ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block
*rb
,
996 if ((rb
->rf_records
.rl_recs
[index
].r_refcount
==
997 rb
->rf_records
.rl_recs
[index
+ 1].r_refcount
) &&
998 (le64_to_cpu(rb
->rf_records
.rl_recs
[index
].r_cpos
) +
999 le32_to_cpu(rb
->rf_records
.rl_recs
[index
].r_clusters
) ==
1000 le64_to_cpu(rb
->rf_records
.rl_recs
[index
+ 1].r_cpos
)))
1001 return REF_CONTIG_RIGHT
;
1003 return REF_CONTIG_NONE
;
1006 static enum ocfs2_ref_rec_contig
1007 ocfs2_refcount_rec_contig(struct ocfs2_refcount_block
*rb
,
1010 enum ocfs2_ref_rec_contig ret
= REF_CONTIG_NONE
;
1012 if (index
< le16_to_cpu(rb
->rf_records
.rl_used
) - 1)
1013 ret
= ocfs2_refcount_rec_adjacent(rb
, index
);
1016 enum ocfs2_ref_rec_contig tmp
;
1018 tmp
= ocfs2_refcount_rec_adjacent(rb
, index
- 1);
1020 if (tmp
== REF_CONTIG_RIGHT
) {
1021 if (ret
== REF_CONTIG_RIGHT
)
1022 ret
= REF_CONTIG_LEFTRIGHT
;
1024 ret
= REF_CONTIG_LEFT
;
1031 static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block
*rb
,
1034 BUG_ON(rb
->rf_records
.rl_recs
[index
].r_refcount
!=
1035 rb
->rf_records
.rl_recs
[index
+1].r_refcount
);
1037 le32_add_cpu(&rb
->rf_records
.rl_recs
[index
].r_clusters
,
1038 le32_to_cpu(rb
->rf_records
.rl_recs
[index
+1].r_clusters
));
1040 if (index
< le16_to_cpu(rb
->rf_records
.rl_used
) - 2)
1041 memmove(&rb
->rf_records
.rl_recs
[index
+ 1],
1042 &rb
->rf_records
.rl_recs
[index
+ 2],
1043 sizeof(struct ocfs2_refcount_rec
) *
1044 (le16_to_cpu(rb
->rf_records
.rl_used
) - index
- 2));
1046 memset(&rb
->rf_records
.rl_recs
[le16_to_cpu(rb
->rf_records
.rl_used
) - 1],
1047 0, sizeof(struct ocfs2_refcount_rec
));
1048 le16_add_cpu(&rb
->rf_records
.rl_used
, -1);
1052 * Merge the refcount rec if we are contiguous with the adjacent recs.
1054 static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block
*rb
,
1057 enum ocfs2_ref_rec_contig contig
=
1058 ocfs2_refcount_rec_contig(rb
, index
);
1060 if (contig
== REF_CONTIG_NONE
)
1063 if (contig
== REF_CONTIG_LEFT
|| contig
== REF_CONTIG_LEFTRIGHT
) {
1068 ocfs2_rotate_refcount_rec_left(rb
, index
);
1070 if (contig
== REF_CONTIG_LEFTRIGHT
)
1071 ocfs2_rotate_refcount_rec_left(rb
, index
);
1075 * Change the refcount indexed by "index" in ref_bh.
1076 * If refcount reaches 0, remove it.
1078 static int ocfs2_change_refcount_rec(handle_t
*handle
,
1079 struct ocfs2_caching_info
*ci
,
1080 struct buffer_head
*ref_leaf_bh
,
1081 int index
, int change
)
1084 struct ocfs2_refcount_block
*rb
=
1085 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1086 struct ocfs2_refcount_list
*rl
= &rb
->rf_records
;
1087 struct ocfs2_refcount_rec
*rec
= &rl
->rl_recs
[index
];
1089 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1090 OCFS2_JOURNAL_ACCESS_WRITE
);
1096 mlog(0, "change index %d, old count %u, change %d\n", index
,
1097 le32_to_cpu(rec
->r_refcount
), change
);
1098 le32_add_cpu(&rec
->r_refcount
, change
);
1100 if (!rec
->r_refcount
) {
1101 if (index
!= le16_to_cpu(rl
->rl_used
) - 1) {
1102 memmove(rec
, rec
+ 1,
1103 (le16_to_cpu(rl
->rl_used
) - index
- 1) *
1104 sizeof(struct ocfs2_refcount_rec
));
1105 memset(&rl
->rl_recs
[le16_to_cpu(rl
->rl_used
) - 1],
1106 0, sizeof(struct ocfs2_refcount_rec
));
1109 le16_add_cpu(&rl
->rl_used
, -1);
1111 ocfs2_refcount_rec_merge(rb
, index
);
1113 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1120 static int ocfs2_expand_inline_ref_root(handle_t
*handle
,
1121 struct ocfs2_caching_info
*ci
,
1122 struct buffer_head
*ref_root_bh
,
1123 struct buffer_head
**ref_leaf_bh
,
1124 struct ocfs2_alloc_context
*meta_ac
)
1127 u16 suballoc_bit_start
;
1130 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1131 struct buffer_head
*new_bh
= NULL
;
1132 struct ocfs2_refcount_block
*new_rb
;
1133 struct ocfs2_refcount_block
*root_rb
=
1134 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1136 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
1137 OCFS2_JOURNAL_ACCESS_WRITE
);
1143 ret
= ocfs2_claim_metadata(OCFS2_SB(sb
), handle
, meta_ac
, 1,
1144 &suballoc_bit_start
, &num_got
,
1151 new_bh
= sb_getblk(sb
, blkno
);
1152 if (new_bh
== NULL
) {
1157 ocfs2_set_new_buffer_uptodate(ci
, new_bh
);
1159 ret
= ocfs2_journal_access_rb(handle
, ci
, new_bh
,
1160 OCFS2_JOURNAL_ACCESS_CREATE
);
1167 * Initialize ocfs2_refcount_block.
1168 * It should contain the same information as the old root.
1169 * so just memcpy it and change the corresponding field.
1171 memcpy(new_bh
->b_data
, ref_root_bh
->b_data
, sb
->s_blocksize
);
1173 new_rb
= (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1174 new_rb
->rf_suballoc_slot
= cpu_to_le16(OCFS2_SB(sb
)->slot_num
);
1175 new_rb
->rf_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1176 new_rb
->rf_blkno
= cpu_to_le64(blkno
);
1177 new_rb
->rf_cpos
= cpu_to_le32(0);
1178 new_rb
->rf_parent
= cpu_to_le64(ref_root_bh
->b_blocknr
);
1179 new_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL
);
1180 ocfs2_journal_dirty(handle
, new_bh
);
1182 /* Now change the root. */
1183 memset(&root_rb
->rf_list
, 0, sb
->s_blocksize
-
1184 offsetof(struct ocfs2_refcount_block
, rf_list
));
1185 root_rb
->rf_list
.l_count
= cpu_to_le16(ocfs2_extent_recs_per_rb(sb
));
1186 root_rb
->rf_clusters
= cpu_to_le32(1);
1187 root_rb
->rf_list
.l_next_free_rec
= cpu_to_le16(1);
1188 root_rb
->rf_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
1189 root_rb
->rf_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
1190 root_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_TREE_FL
);
1192 ocfs2_journal_dirty(handle
, ref_root_bh
);
1194 mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno
,
1195 le16_to_cpu(new_rb
->rf_records
.rl_used
));
1197 *ref_leaf_bh
= new_bh
;
1204 static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec
*prev
,
1205 struct ocfs2_refcount_rec
*next
)
1207 if (ocfs2_get_ref_rec_low_cpos(prev
) + le32_to_cpu(prev
->r_clusters
) <=
1208 ocfs2_get_ref_rec_low_cpos(next
))
1214 static int cmp_refcount_rec_by_low_cpos(const void *a
, const void *b
)
1216 const struct ocfs2_refcount_rec
*l
= a
, *r
= b
;
1217 u32 l_cpos
= ocfs2_get_ref_rec_low_cpos(l
);
1218 u32 r_cpos
= ocfs2_get_ref_rec_low_cpos(r
);
1220 if (l_cpos
> r_cpos
)
1222 if (l_cpos
< r_cpos
)
1227 static int cmp_refcount_rec_by_cpos(const void *a
, const void *b
)
1229 const struct ocfs2_refcount_rec
*l
= a
, *r
= b
;
1230 u64 l_cpos
= le64_to_cpu(l
->r_cpos
);
1231 u64 r_cpos
= le64_to_cpu(r
->r_cpos
);
1233 if (l_cpos
> r_cpos
)
1235 if (l_cpos
< r_cpos
)
1240 static void swap_refcount_rec(void *a
, void *b
, int size
)
1242 struct ocfs2_refcount_rec
*l
= a
, *r
= b
, tmp
;
1244 tmp
= *(struct ocfs2_refcount_rec
*)l
;
1245 *(struct ocfs2_refcount_rec
*)l
=
1246 *(struct ocfs2_refcount_rec
*)r
;
1247 *(struct ocfs2_refcount_rec
*)r
= tmp
;
1251 * The refcount cpos are ordered by their 64bit cpos,
1252 * But we will use the low 32 bit to be the e_cpos in the b-tree.
1253 * So we need to make sure that this pos isn't intersected with others.
1255 * Note: The refcount block is already sorted by their low 32 bit cpos,
1256 * So just try the middle pos first, and we will exit when we find
1257 * the good position.
1259 static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list
*rl
,
1260 u32
*split_pos
, int *split_index
)
1262 int num_used
= le16_to_cpu(rl
->rl_used
);
1263 int delta
, middle
= num_used
/ 2;
1265 for (delta
= 0; delta
< middle
; delta
++) {
1266 /* Let's check delta earlier than middle */
1267 if (ocfs2_refcount_rec_no_intersect(
1268 &rl
->rl_recs
[middle
- delta
- 1],
1269 &rl
->rl_recs
[middle
- delta
])) {
1270 *split_index
= middle
- delta
;
1274 /* For even counts, don't walk off the end */
1275 if ((middle
+ delta
+ 1) == num_used
)
1278 /* Now try delta past middle */
1279 if (ocfs2_refcount_rec_no_intersect(
1280 &rl
->rl_recs
[middle
+ delta
],
1281 &rl
->rl_recs
[middle
+ delta
+ 1])) {
1282 *split_index
= middle
+ delta
+ 1;
1287 if (delta
>= middle
)
1290 *split_pos
= ocfs2_get_ref_rec_low_cpos(&rl
->rl_recs
[*split_index
]);
1294 static int ocfs2_divide_leaf_refcount_block(struct buffer_head
*ref_leaf_bh
,
1295 struct buffer_head
*new_bh
,
1298 int split_index
= 0, num_moved
, ret
;
1300 struct ocfs2_refcount_block
*rb
=
1301 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1302 struct ocfs2_refcount_list
*rl
= &rb
->rf_records
;
1303 struct ocfs2_refcount_block
*new_rb
=
1304 (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1305 struct ocfs2_refcount_list
*new_rl
= &new_rb
->rf_records
;
1307 mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
1308 (unsigned long long)ref_leaf_bh
->b_blocknr
,
1309 le32_to_cpu(rl
->rl_count
), le32_to_cpu(rl
->rl_used
));
1312 * XXX: Improvement later.
1313 * If we know all the high 32 bit cpos is the same, no need to sort.
1315 * In order to make the whole process safe, we do:
1316 * 1. sort the entries by their low 32 bit cpos first so that we can
1317 * find the split cpos easily.
1318 * 2. call ocfs2_insert_extent to insert the new refcount block.
1319 * 3. move the refcount rec to the new block.
1320 * 4. sort the entries by their 64 bit cpos.
1321 * 5. dirty the new_rb and rb.
1323 sort(&rl
->rl_recs
, le16_to_cpu(rl
->rl_used
),
1324 sizeof(struct ocfs2_refcount_rec
),
1325 cmp_refcount_rec_by_low_cpos
, swap_refcount_rec
);
1327 ret
= ocfs2_find_refcount_split_pos(rl
, &cpos
, &split_index
);
1333 new_rb
->rf_cpos
= cpu_to_le32(cpos
);
1335 /* move refcount records starting from split_index to the new block. */
1336 num_moved
= le16_to_cpu(rl
->rl_used
) - split_index
;
1337 memcpy(new_rl
->rl_recs
, &rl
->rl_recs
[split_index
],
1338 num_moved
* sizeof(struct ocfs2_refcount_rec
));
1340 /*ok, remove the entries we just moved over to the other block. */
1341 memset(&rl
->rl_recs
[split_index
], 0,
1342 num_moved
* sizeof(struct ocfs2_refcount_rec
));
1344 /* change old and new rl_used accordingly. */
1345 le16_add_cpu(&rl
->rl_used
, -num_moved
);
1346 new_rl
->rl_used
= cpu_to_le32(num_moved
);
1348 sort(&rl
->rl_recs
, le16_to_cpu(rl
->rl_used
),
1349 sizeof(struct ocfs2_refcount_rec
),
1350 cmp_refcount_rec_by_cpos
, swap_refcount_rec
);
1352 sort(&new_rl
->rl_recs
, le16_to_cpu(new_rl
->rl_used
),
1353 sizeof(struct ocfs2_refcount_rec
),
1354 cmp_refcount_rec_by_cpos
, swap_refcount_rec
);
1360 static int ocfs2_new_leaf_refcount_block(handle_t
*handle
,
1361 struct ocfs2_caching_info
*ci
,
1362 struct buffer_head
*ref_root_bh
,
1363 struct buffer_head
*ref_leaf_bh
,
1364 struct ocfs2_alloc_context
*meta_ac
)
1367 u16 suballoc_bit_start
;
1368 u32 num_got
, new_cpos
;
1370 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1371 struct ocfs2_refcount_block
*root_rb
=
1372 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1373 struct buffer_head
*new_bh
= NULL
;
1374 struct ocfs2_refcount_block
*new_rb
;
1375 struct ocfs2_extent_tree ref_et
;
1377 BUG_ON(!(le32_to_cpu(root_rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
));
1379 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
1380 OCFS2_JOURNAL_ACCESS_WRITE
);
1386 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1387 OCFS2_JOURNAL_ACCESS_WRITE
);
1393 ret
= ocfs2_claim_metadata(OCFS2_SB(sb
), handle
, meta_ac
, 1,
1394 &suballoc_bit_start
, &num_got
,
1401 new_bh
= sb_getblk(sb
, blkno
);
1402 if (new_bh
== NULL
) {
1407 ocfs2_set_new_buffer_uptodate(ci
, new_bh
);
1409 ret
= ocfs2_journal_access_rb(handle
, ci
, new_bh
,
1410 OCFS2_JOURNAL_ACCESS_CREATE
);
1416 /* Initialize ocfs2_refcount_block. */
1417 new_rb
= (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1418 memset(new_rb
, 0, sb
->s_blocksize
);
1419 strcpy((void *)new_rb
, OCFS2_REFCOUNT_BLOCK_SIGNATURE
);
1420 new_rb
->rf_suballoc_slot
= cpu_to_le16(OCFS2_SB(sb
)->slot_num
);
1421 new_rb
->rf_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1422 new_rb
->rf_fs_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
1423 new_rb
->rf_blkno
= cpu_to_le64(blkno
);
1424 new_rb
->rf_parent
= cpu_to_le64(ref_root_bh
->b_blocknr
);
1425 new_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL
);
1426 new_rb
->rf_records
.rl_count
=
1427 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb
));
1428 new_rb
->rf_generation
= root_rb
->rf_generation
;
1430 ret
= ocfs2_divide_leaf_refcount_block(ref_leaf_bh
, new_bh
, &new_cpos
);
1436 ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1437 ocfs2_journal_dirty(handle
, new_bh
);
1439 ocfs2_init_refcount_extent_tree(&ref_et
, ci
, ref_root_bh
);
1441 mlog(0, "insert new leaf block %llu at %u\n",
1442 (unsigned long long)new_bh
->b_blocknr
, new_cpos
);
1444 /* Insert the new leaf block with the specific offset cpos. */
1445 ret
= ocfs2_insert_extent(handle
, &ref_et
, new_cpos
, new_bh
->b_blocknr
,
1455 static int ocfs2_expand_refcount_tree(handle_t
*handle
,
1456 struct ocfs2_caching_info
*ci
,
1457 struct buffer_head
*ref_root_bh
,
1458 struct buffer_head
*ref_leaf_bh
,
1459 struct ocfs2_alloc_context
*meta_ac
)
1462 struct buffer_head
*expand_bh
= NULL
;
1464 if (ref_root_bh
== ref_leaf_bh
) {
1466 * the old root bh hasn't been expanded to a b-tree,
1467 * so expand it first.
1469 ret
= ocfs2_expand_inline_ref_root(handle
, ci
, ref_root_bh
,
1470 &expand_bh
, meta_ac
);
1476 expand_bh
= ref_leaf_bh
;
1481 /* Now add a new refcount block into the tree.*/
1482 ret
= ocfs2_new_leaf_refcount_block(handle
, ci
, ref_root_bh
,
1483 expand_bh
, meta_ac
);
1492 * Adjust the extent rec in b-tree representing ref_leaf_bh.
1494 * Only called when we have inserted a new refcount rec at index 0
1495 * which means ocfs2_extent_rec.e_cpos may need some change.
1497 static int ocfs2_adjust_refcount_rec(handle_t
*handle
,
1498 struct ocfs2_caching_info
*ci
,
1499 struct buffer_head
*ref_root_bh
,
1500 struct buffer_head
*ref_leaf_bh
,
1501 struct ocfs2_refcount_rec
*rec
)
1504 u32 new_cpos
, old_cpos
;
1505 struct ocfs2_path
*path
= NULL
;
1506 struct ocfs2_extent_tree et
;
1507 struct ocfs2_refcount_block
*rb
=
1508 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1509 struct ocfs2_extent_list
*el
;
1511 if (!(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
))
1514 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1515 old_cpos
= le32_to_cpu(rb
->rf_cpos
);
1516 new_cpos
= le64_to_cpu(rec
->r_cpos
) & OCFS2_32BIT_POS_MASK
;
1517 if (old_cpos
<= new_cpos
)
1520 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
1522 path
= ocfs2_new_path_from_et(&et
);
1529 ret
= ocfs2_find_path(ci
, path
, old_cpos
);
1536 * 2 more credits, one for the leaf refcount block, one for
1537 * the extent block contains the extent rec.
1539 ret
= ocfs2_extend_trans(handle
, handle
->h_buffer_credits
+ 2);
1545 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1546 OCFS2_JOURNAL_ACCESS_WRITE
);
1552 ret
= ocfs2_journal_access_eb(handle
, ci
, path_leaf_bh(path
),
1553 OCFS2_JOURNAL_ACCESS_WRITE
);
1559 /* change the leaf extent block first. */
1560 el
= path_leaf_el(path
);
1562 for (i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
1563 if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) == old_cpos
)
1566 BUG_ON(i
== le16_to_cpu(el
->l_next_free_rec
));
1568 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1570 /* change the r_cpos in the leaf block. */
1571 rb
->rf_cpos
= cpu_to_le32(new_cpos
);
1573 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
1574 ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1577 ocfs2_free_path(path
);
1581 static int ocfs2_insert_refcount_rec(handle_t
*handle
,
1582 struct ocfs2_caching_info
*ci
,
1583 struct buffer_head
*ref_root_bh
,
1584 struct buffer_head
*ref_leaf_bh
,
1585 struct ocfs2_refcount_rec
*rec
,
1587 struct ocfs2_alloc_context
*meta_ac
)
1590 struct ocfs2_refcount_block
*rb
=
1591 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1592 struct ocfs2_refcount_list
*rf_list
= &rb
->rf_records
;
1593 struct buffer_head
*new_bh
= NULL
;
1595 BUG_ON(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
);
1597 if (rf_list
->rl_used
== rf_list
->rl_count
) {
1598 u64 cpos
= le64_to_cpu(rec
->r_cpos
);
1599 u32 len
= le32_to_cpu(rec
->r_clusters
);
1601 ret
= ocfs2_expand_refcount_tree(handle
, ci
, ref_root_bh
,
1602 ref_leaf_bh
, meta_ac
);
1608 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1609 cpos
, len
, NULL
, &index
,
1616 ref_leaf_bh
= new_bh
;
1617 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1618 rf_list
= &rb
->rf_records
;
1621 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1622 OCFS2_JOURNAL_ACCESS_WRITE
);
1628 if (index
< le16_to_cpu(rf_list
->rl_used
))
1629 memmove(&rf_list
->rl_recs
[index
+ 1],
1630 &rf_list
->rl_recs
[index
],
1631 (le16_to_cpu(rf_list
->rl_used
) - index
) *
1632 sizeof(struct ocfs2_refcount_rec
));
1634 mlog(0, "insert refcount record start %llu, len %u, count %u "
1635 "to leaf block %llu at index %d\n",
1636 (unsigned long long)le64_to_cpu(rec
->r_cpos
),
1637 le32_to_cpu(rec
->r_clusters
), le32_to_cpu(rec
->r_refcount
),
1638 (unsigned long long)ref_leaf_bh
->b_blocknr
, index
);
1640 rf_list
->rl_recs
[index
] = *rec
;
1642 le16_add_cpu(&rf_list
->rl_used
, 1);
1644 ocfs2_refcount_rec_merge(rb
, index
);
1646 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1653 ret
= ocfs2_adjust_refcount_rec(handle
, ci
,
1665 * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1666 * This is much simple than our b-tree code.
1667 * split_rec is the new refcount rec we want to insert.
1668 * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1669 * increase refcount or decrease a refcount to non-zero).
1670 * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1671 * rec( in case we decrease a refcount to zero).
1673 static int ocfs2_split_refcount_rec(handle_t
*handle
,
1674 struct ocfs2_caching_info
*ci
,
1675 struct buffer_head
*ref_root_bh
,
1676 struct buffer_head
*ref_leaf_bh
,
1677 struct ocfs2_refcount_rec
*split_rec
,
1679 struct ocfs2_alloc_context
*meta_ac
,
1680 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1684 struct ocfs2_refcount_block
*rb
=
1685 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1686 struct ocfs2_refcount_list
*rf_list
= &rb
->rf_records
;
1687 struct ocfs2_refcount_rec
*orig_rec
= &rf_list
->rl_recs
[index
];
1688 struct ocfs2_refcount_rec
*tail_rec
= NULL
;
1689 struct buffer_head
*new_bh
= NULL
;
1691 BUG_ON(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
);
1693 mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
1694 le64_to_cpu(orig_rec
->r_cpos
), le32_to_cpu(orig_rec
->r_clusters
),
1695 le64_to_cpu(split_rec
->r_cpos
),
1696 le32_to_cpu(split_rec
->r_clusters
));
1699 * If we just need to split the header or tail clusters,
1700 * no more recs are needed, just split is OK.
1701 * Otherwise we at least need one new recs.
1703 if (!split_rec
->r_refcount
&&
1704 (split_rec
->r_cpos
== orig_rec
->r_cpos
||
1705 le64_to_cpu(split_rec
->r_cpos
) +
1706 le32_to_cpu(split_rec
->r_clusters
) ==
1707 le64_to_cpu(orig_rec
->r_cpos
) + le32_to_cpu(orig_rec
->r_clusters
)))
1713 * We need one more rec if we split in the middle and the new rec have
1714 * some refcount in it.
1716 if (split_rec
->r_refcount
&&
1717 (split_rec
->r_cpos
!= orig_rec
->r_cpos
&&
1718 le64_to_cpu(split_rec
->r_cpos
) +
1719 le32_to_cpu(split_rec
->r_clusters
) !=
1720 le64_to_cpu(orig_rec
->r_cpos
) + le32_to_cpu(orig_rec
->r_clusters
)))
1723 /* If the leaf block don't have enough record, expand it. */
1724 if (le16_to_cpu(rf_list
->rl_used
) + recs_need
> rf_list
->rl_count
) {
1725 struct ocfs2_refcount_rec tmp_rec
;
1726 u64 cpos
= le64_to_cpu(orig_rec
->r_cpos
);
1727 len
= le32_to_cpu(orig_rec
->r_clusters
);
1728 ret
= ocfs2_expand_refcount_tree(handle
, ci
, ref_root_bh
,
1729 ref_leaf_bh
, meta_ac
);
1736 * We have to re-get it since now cpos may be moved to
1737 * another leaf block.
1739 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1740 cpos
, len
, &tmp_rec
, &index
,
1747 ref_leaf_bh
= new_bh
;
1748 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1749 rf_list
= &rb
->rf_records
;
1750 orig_rec
= &rf_list
->rl_recs
[index
];
1753 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1754 OCFS2_JOURNAL_ACCESS_WRITE
);
1761 * We have calculated out how many new records we need and store
1762 * in recs_need, so spare enough space first by moving the records
1763 * after "index" to the end.
1765 if (index
!= le16_to_cpu(rf_list
->rl_used
) - 1)
1766 memmove(&rf_list
->rl_recs
[index
+ 1 + recs_need
],
1767 &rf_list
->rl_recs
[index
+ 1],
1768 (le16_to_cpu(rf_list
->rl_used
) - index
- 1) *
1769 sizeof(struct ocfs2_refcount_rec
));
1771 len
= (le64_to_cpu(orig_rec
->r_cpos
) +
1772 le32_to_cpu(orig_rec
->r_clusters
)) -
1773 (le64_to_cpu(split_rec
->r_cpos
) +
1774 le32_to_cpu(split_rec
->r_clusters
));
1777 * If we have "len", the we will split in the tail and move it
1778 * to the end of the space we have just spared.
1781 tail_rec
= &rf_list
->rl_recs
[index
+ recs_need
];
1783 memcpy(tail_rec
, orig_rec
, sizeof(struct ocfs2_refcount_rec
));
1784 le64_add_cpu(&tail_rec
->r_cpos
,
1785 le32_to_cpu(tail_rec
->r_clusters
) - len
);
1786 tail_rec
->r_clusters
= le32_to_cpu(len
);
1790 * If the split pos isn't the same as the original one, we need to
1791 * split in the head.
1793 * Note: We have the chance that split_rec.r_refcount = 0,
1794 * recs_need = 0 and len > 0, which means we just cut the head from
1795 * the orig_rec and in that case we have done some modification in
1796 * orig_rec above, so the check for r_cpos is faked.
1798 if (split_rec
->r_cpos
!= orig_rec
->r_cpos
&& tail_rec
!= orig_rec
) {
1799 len
= le64_to_cpu(split_rec
->r_cpos
) -
1800 le64_to_cpu(orig_rec
->r_cpos
);
1801 orig_rec
->r_clusters
= cpu_to_le32(len
);
1805 le16_add_cpu(&rf_list
->rl_used
, recs_need
);
1807 if (split_rec
->r_refcount
) {
1808 rf_list
->rl_recs
[index
] = *split_rec
;
1809 mlog(0, "insert refcount record start %llu, len %u, count %u "
1810 "to leaf block %llu at index %d\n",
1811 (unsigned long long)le64_to_cpu(split_rec
->r_cpos
),
1812 le32_to_cpu(split_rec
->r_clusters
),
1813 le32_to_cpu(split_rec
->r_refcount
),
1814 (unsigned long long)ref_leaf_bh
->b_blocknr
, index
);
1816 ocfs2_refcount_rec_merge(rb
, index
);
1819 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1828 static int __ocfs2_increase_refcount(handle_t
*handle
,
1829 struct ocfs2_caching_info
*ci
,
1830 struct buffer_head
*ref_root_bh
,
1832 struct ocfs2_alloc_context
*meta_ac
,
1833 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1836 struct buffer_head
*ref_leaf_bh
= NULL
;
1837 struct ocfs2_refcount_rec rec
;
1838 unsigned int set_len
= 0;
1840 mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
1841 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1842 (unsigned long long)cpos
, len
);
1845 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1846 cpos
, len
, &rec
, &index
,
1853 set_len
= le32_to_cpu(rec
.r_clusters
);
1856 * Here we may meet with 3 situations:
1858 * 1. If we find an already existing record, and the length
1859 * is the same, cool, we just need to increase the r_refcount
1861 * 2. If we find a hole, just insert it with r_refcount = 1.
1862 * 3. If we are in the middle of one extent record, split
1865 if (rec
.r_refcount
&& le64_to_cpu(rec
.r_cpos
) == cpos
&&
1867 mlog(0, "increase refcount rec, start %llu, len %u, "
1868 "count %u\n", (unsigned long long)cpos
, set_len
,
1869 le32_to_cpu(rec
.r_refcount
));
1870 ret
= ocfs2_change_refcount_rec(handle
, ci
,
1871 ref_leaf_bh
, index
, 1);
1876 } else if (!rec
.r_refcount
) {
1877 rec
.r_refcount
= cpu_to_le32(1);
1879 mlog(0, "insert refcount rec, start %llu, len %u\n",
1880 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
1882 ret
= ocfs2_insert_refcount_rec(handle
, ci
, ref_root_bh
,
1884 &rec
, index
, meta_ac
);
1890 set_len
= min((u64
)(cpos
+ len
),
1891 le64_to_cpu(rec
.r_cpos
) + set_len
) - cpos
;
1892 rec
.r_cpos
= cpu_to_le64(cpos
);
1893 rec
.r_clusters
= cpu_to_le32(set_len
);
1894 le32_add_cpu(&rec
.r_refcount
, 1);
1896 mlog(0, "split refcount rec, start %llu, "
1897 "len %u, count %u\n",
1898 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
1899 set_len
, le32_to_cpu(rec
.r_refcount
));
1900 ret
= ocfs2_split_refcount_rec(handle
, ci
,
1901 ref_root_bh
, ref_leaf_bh
,
1912 brelse(ref_leaf_bh
);
1917 brelse(ref_leaf_bh
);
1921 static int ocfs2_remove_refcount_extent(handle_t
*handle
,
1922 struct ocfs2_caching_info
*ci
,
1923 struct buffer_head
*ref_root_bh
,
1924 struct buffer_head
*ref_leaf_bh
,
1925 struct ocfs2_alloc_context
*meta_ac
,
1926 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1929 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1930 struct ocfs2_refcount_block
*rb
=
1931 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1932 struct ocfs2_extent_tree et
;
1934 BUG_ON(rb
->rf_records
.rl_used
);
1936 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
1937 ret
= ocfs2_remove_extent(handle
, &et
, le32_to_cpu(rb
->rf_cpos
),
1938 1, meta_ac
, dealloc
);
1944 ocfs2_remove_from_cache(ci
, ref_leaf_bh
);
1947 * add the freed block to the dealloc so that it will be freed
1948 * when we run dealloc.
1950 ret
= ocfs2_cache_block_dealloc(dealloc
, EXTENT_ALLOC_SYSTEM_INODE
,
1951 le16_to_cpu(rb
->rf_suballoc_slot
),
1952 le64_to_cpu(rb
->rf_blkno
),
1953 le16_to_cpu(rb
->rf_suballoc_bit
));
1959 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
1960 OCFS2_JOURNAL_ACCESS_WRITE
);
1966 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1968 le32_add_cpu(&rb
->rf_clusters
, -1);
1971 * check whether we need to restore the root refcount block if
1972 * there is no leaf extent block at atll.
1974 if (!rb
->rf_list
.l_next_free_rec
) {
1975 BUG_ON(rb
->rf_clusters
);
1977 mlog(0, "reset refcount tree root %llu to be a record block.\n",
1978 (unsigned long long)ref_root_bh
->b_blocknr
);
1983 memset(&rb
->rf_records
, 0, sb
->s_blocksize
-
1984 offsetof(struct ocfs2_refcount_block
, rf_records
));
1985 rb
->rf_records
.rl_count
=
1986 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb
));
1989 ocfs2_journal_dirty(handle
, ref_root_bh
);
1995 static int ocfs2_decrease_refcount_rec(handle_t
*handle
,
1996 struct ocfs2_caching_info
*ci
,
1997 struct buffer_head
*ref_root_bh
,
1998 struct buffer_head
*ref_leaf_bh
,
1999 int index
, u64 cpos
, unsigned int len
,
2000 struct ocfs2_alloc_context
*meta_ac
,
2001 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2004 struct ocfs2_refcount_block
*rb
=
2005 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
2006 struct ocfs2_refcount_rec
*rec
= &rb
->rf_records
.rl_recs
[index
];
2008 BUG_ON(cpos
< le64_to_cpu(rec
->r_cpos
));
2010 le64_to_cpu(rec
->r_cpos
) + le32_to_cpu(rec
->r_clusters
));
2012 if (cpos
== le64_to_cpu(rec
->r_cpos
) &&
2013 len
== le32_to_cpu(rec
->r_clusters
))
2014 ret
= ocfs2_change_refcount_rec(handle
, ci
,
2015 ref_leaf_bh
, index
, -1);
2017 struct ocfs2_refcount_rec split
= *rec
;
2018 split
.r_cpos
= cpu_to_le64(cpos
);
2019 split
.r_clusters
= cpu_to_le32(len
);
2021 le32_add_cpu(&split
.r_refcount
, -1);
2023 mlog(0, "split refcount rec, start %llu, "
2024 "len %u, count %u, original start %llu, len %u\n",
2025 (unsigned long long)le64_to_cpu(split
.r_cpos
),
2026 len
, le32_to_cpu(split
.r_refcount
),
2027 (unsigned long long)le64_to_cpu(rec
->r_cpos
),
2028 le32_to_cpu(rec
->r_clusters
));
2029 ret
= ocfs2_split_refcount_rec(handle
, ci
,
2030 ref_root_bh
, ref_leaf_bh
,
2040 /* Remove the leaf refcount block if it contains no refcount record. */
2041 if (!rb
->rf_records
.rl_used
&& ref_leaf_bh
!= ref_root_bh
) {
2042 ret
= ocfs2_remove_refcount_extent(handle
, ci
, ref_root_bh
,
2043 ref_leaf_bh
, meta_ac
,
2053 static int __ocfs2_decrease_refcount(handle_t
*handle
,
2054 struct ocfs2_caching_info
*ci
,
2055 struct buffer_head
*ref_root_bh
,
2057 struct ocfs2_alloc_context
*meta_ac
,
2058 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2060 int ret
= 0, index
= 0;
2061 struct ocfs2_refcount_rec rec
;
2062 unsigned int r_count
= 0, r_len
;
2063 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
2064 struct buffer_head
*ref_leaf_bh
= NULL
;
2066 mlog(0, "Tree owner %llu, decrease refcount start %llu, len %u\n",
2067 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
2068 (unsigned long long)cpos
, len
);
2071 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
2072 cpos
, len
, &rec
, &index
,
2079 r_count
= le32_to_cpu(rec
.r_refcount
);
2080 BUG_ON(r_count
== 0);
2082 r_len
= min((u64
)(cpos
+ len
), le64_to_cpu(rec
.r_cpos
) +
2083 le32_to_cpu(rec
.r_clusters
)) - cpos
;
2085 ret
= ocfs2_decrease_refcount_rec(handle
, ci
, ref_root_bh
,
2094 if (le32_to_cpu(rec
.r_refcount
) == 1) {
2095 ret
= ocfs2_cache_cluster_dealloc(dealloc
,
2096 ocfs2_clusters_to_blocks(sb
, cpos
),
2106 brelse(ref_leaf_bh
);
2111 brelse(ref_leaf_bh
);
2115 /* Caller must hold refcount tree lock. */
2116 int ocfs2_decrease_refcount(struct inode
*inode
,
2117 handle_t
*handle
, u32 cpos
, u32 len
,
2118 struct ocfs2_alloc_context
*meta_ac
,
2119 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2123 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2124 struct buffer_head
*ref_root_bh
= NULL
;
2125 struct ocfs2_refcount_tree
*tree
;
2127 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
2129 ret
= ocfs2_get_refcount_block(inode
, &ref_blkno
);
2135 ret
= ocfs2_get_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_blkno
, &tree
);
2141 ret
= ocfs2_read_refcount_block(&tree
->rf_ci
, tree
->rf_blkno
,
2148 ret
= __ocfs2_decrease_refcount(handle
, &tree
->rf_ci
, ref_root_bh
,
2149 cpos
, len
, meta_ac
, dealloc
);
2153 brelse(ref_root_bh
);
2158 * Mark the already-existing extent at cpos as refcounted for len clusters.
2159 * This adds the refcount extent flag.
2161 * If the existing extent is larger than the request, initiate a
2162 * split. An attempt will be made at merging with adjacent extents.
2164 * The caller is responsible for passing down meta_ac if we'll need it.
2166 static int ocfs2_mark_extent_refcounted(struct inode
*inode
,
2167 struct ocfs2_extent_tree
*et
,
2168 handle_t
*handle
, u32 cpos
,
2170 struct ocfs2_alloc_context
*meta_ac
,
2171 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2175 mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
2176 inode
->i_ino
, cpos
, len
, phys
);
2178 if (!ocfs2_refcount_tree(OCFS2_SB(inode
->i_sb
))) {
2179 ocfs2_error(inode
->i_sb
, "Inode %lu want to use refcount "
2180 "tree, but the feature bit is not set in the "
2181 "super block.", inode
->i_ino
);
2186 ret
= ocfs2_change_extent_flag(handle
, et
, cpos
,
2187 len
, phys
, meta_ac
, dealloc
,
2188 OCFS2_EXT_REFCOUNTED
, 0);
2197 * Given some contiguous physical clusters, calculate what we need
2198 * for modifying their refcount.
2200 static int ocfs2_calc_refcount_meta_credits(struct super_block
*sb
,
2201 struct ocfs2_caching_info
*ci
,
2202 struct buffer_head
*ref_root_bh
,
2208 int ret
= 0, index
, ref_blocks
= 0, recs_add
= 0;
2209 u64 cpos
= start_cpos
;
2210 struct ocfs2_refcount_block
*rb
;
2211 struct ocfs2_refcount_rec rec
;
2212 struct buffer_head
*ref_leaf_bh
= NULL
, *prev_bh
= NULL
;
2215 mlog(0, "start_cpos %llu, clusters %u\n",
2216 (unsigned long long)start_cpos
, clusters
);
2218 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
2219 cpos
, clusters
, &rec
,
2220 &index
, &ref_leaf_bh
);
2226 if (ref_leaf_bh
!= prev_bh
) {
2228 * Now we encounter a new leaf block, so calculate
2229 * whether we need to extend the old leaf.
2232 rb
= (struct ocfs2_refcount_block
*)
2235 if (le64_to_cpu(rb
->rf_records
.rl_used
) +
2237 le16_to_cpu(rb
->rf_records
.rl_count
))
2244 prev_bh
= ref_leaf_bh
;
2248 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
2250 mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
2251 "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
2252 recs_add
, (unsigned long long)cpos
, clusters
,
2253 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
2254 le32_to_cpu(rec
.r_clusters
),
2255 le32_to_cpu(rec
.r_refcount
), index
);
2257 len
= min((u64
)cpos
+ clusters
, le64_to_cpu(rec
.r_cpos
) +
2258 le32_to_cpu(rec
.r_clusters
)) - cpos
;
2260 * If the refcount rec already exist, cool. We just need
2261 * to check whether there is a split. Otherwise we just need
2262 * to increase the refcount.
2263 * If we will insert one, increases recs_add.
2265 * We record all the records which will be inserted to the
2266 * same refcount block, so that we can tell exactly whether
2267 * we need a new refcount block or not.
2269 if (rec
.r_refcount
) {
2270 /* Check whether we need a split at the beginning. */
2271 if (cpos
== start_cpos
&&
2272 cpos
!= le64_to_cpu(rec
.r_cpos
))
2275 /* Check whether we need a split in the end. */
2276 if (cpos
+ clusters
< le64_to_cpu(rec
.r_cpos
) +
2277 le32_to_cpu(rec
.r_clusters
))
2282 brelse(ref_leaf_bh
);
2289 rb
= (struct ocfs2_refcount_block
*)prev_bh
->b_data
;
2291 if (le64_to_cpu(rb
->rf_records
.rl_used
) + recs_add
>
2292 le16_to_cpu(rb
->rf_records
.rl_count
))
2301 mlog(0, "we need ref_blocks %d\n", ref_blocks
);
2302 *meta_add
+= ref_blocks
;
2303 *credits
+= ref_blocks
;
2306 * So we may need ref_blocks to insert into the tree.
2307 * That also means we need to change the b-tree and add that number
2308 * of records since we never merge them.
2309 * We need one more block for expansion since the new created leaf
2310 * block is also full and needs split.
2312 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
2313 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
) {
2314 struct ocfs2_extent_tree et
;
2316 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
2317 *meta_add
+= ocfs2_extend_meta_needed(et
.et_root_el
);
2318 *credits
+= ocfs2_calc_extend_credits(sb
,
2322 *credits
+= OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
2327 brelse(ref_leaf_bh
);
2333 * For refcount tree, we will decrease some contiguous clusters
2334 * refcount count, so just go through it to see how many blocks
2335 * we gonna touch and whether we need to create new blocks.
2337 * Normally the refcount blocks store these refcount should be
2338 * continguous also, so that we can get the number easily.
2339 * As for meta_ac, we will at most add split 2 refcount record and
2340 * 2 more refcount block, so just check it in a rough way.
2342 * Caller must hold refcount tree lock.
2344 int ocfs2_prepare_refcount_change_for_del(struct inode
*inode
,
2345 struct buffer_head
*di_bh
,
2349 struct ocfs2_alloc_context
**meta_ac
)
2351 int ret
, ref_blocks
= 0;
2352 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2353 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2354 struct buffer_head
*ref_root_bh
= NULL
;
2355 struct ocfs2_refcount_tree
*tree
;
2356 u64 start_cpos
= ocfs2_blocks_to_clusters(inode
->i_sb
, phys_blkno
);
2358 if (!ocfs2_refcount_tree(OCFS2_SB(inode
->i_sb
))) {
2359 ocfs2_error(inode
->i_sb
, "Inode %lu want to use refcount "
2360 "tree, but the feature bit is not set in the "
2361 "super block.", inode
->i_ino
);
2366 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
2368 ret
= ocfs2_get_refcount_tree(OCFS2_SB(inode
->i_sb
),
2369 le64_to_cpu(di
->i_refcount_loc
), &tree
);
2375 ret
= ocfs2_read_refcount_block(&tree
->rf_ci
,
2376 le64_to_cpu(di
->i_refcount_loc
),
2383 ret
= ocfs2_calc_refcount_meta_credits(inode
->i_sb
,
2386 start_cpos
, clusters
,
2387 &ref_blocks
, credits
);
2393 mlog(0, "reserve new metadata %d, credits = %d\n",
2394 ref_blocks
, *credits
);
2397 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
2398 ref_blocks
, meta_ac
);
2404 brelse(ref_root_bh
);