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"
37 #include <linux/bio.h>
38 #include <linux/blkdev.h>
39 #include <linux/gfp.h>
40 #include <linux/slab.h>
41 #include <linux/writeback.h>
42 #include <linux/pagevec.h>
43 #include <linux/swap.h>
45 struct ocfs2_cow_context
{
49 struct ocfs2_extent_tree data_et
;
50 struct ocfs2_refcount_tree
*ref_tree
;
51 struct buffer_head
*ref_root_bh
;
52 struct ocfs2_alloc_context
*meta_ac
;
53 struct ocfs2_alloc_context
*data_ac
;
54 struct ocfs2_cached_dealloc_ctxt dealloc
;
56 struct ocfs2_post_refcount
*post_refcount
;
58 int (*get_clusters
)(struct ocfs2_cow_context
*context
,
59 u32 v_cluster
, u32
*p_cluster
,
61 unsigned int *extent_flags
);
62 int (*cow_duplicate_clusters
)(handle_t
*handle
,
63 struct ocfs2_cow_context
*context
,
64 u32 cpos
, u32 old_cluster
,
65 u32 new_cluster
, u32 new_len
);
68 static inline struct ocfs2_refcount_tree
*
69 cache_info_to_refcount(struct ocfs2_caching_info
*ci
)
71 return container_of(ci
, struct ocfs2_refcount_tree
, rf_ci
);
74 static int ocfs2_validate_refcount_block(struct super_block
*sb
,
75 struct buffer_head
*bh
)
78 struct ocfs2_refcount_block
*rb
=
79 (struct ocfs2_refcount_block
*)bh
->b_data
;
81 mlog(0, "Validating refcount block %llu\n",
82 (unsigned long long)bh
->b_blocknr
);
84 BUG_ON(!buffer_uptodate(bh
));
87 * If the ecc fails, we return the error but otherwise
88 * leave the filesystem running. We know any error is
89 * local to this block.
91 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &rb
->rf_check
);
93 mlog(ML_ERROR
, "Checksum failed for refcount block %llu\n",
94 (unsigned long long)bh
->b_blocknr
);
99 if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb
)) {
101 "Refcount block #%llu has bad signature %.*s",
102 (unsigned long long)bh
->b_blocknr
, 7,
107 if (le64_to_cpu(rb
->rf_blkno
) != bh
->b_blocknr
) {
109 "Refcount block #%llu has an invalid rf_blkno "
111 (unsigned long long)bh
->b_blocknr
,
112 (unsigned long long)le64_to_cpu(rb
->rf_blkno
));
116 if (le32_to_cpu(rb
->rf_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
118 "Refcount block #%llu has an invalid "
119 "rf_fs_generation of #%u",
120 (unsigned long long)bh
->b_blocknr
,
121 le32_to_cpu(rb
->rf_fs_generation
));
128 static int ocfs2_read_refcount_block(struct ocfs2_caching_info
*ci
,
130 struct buffer_head
**bh
)
133 struct buffer_head
*tmp
= *bh
;
135 rc
= ocfs2_read_block(ci
, rb_blkno
, &tmp
,
136 ocfs2_validate_refcount_block
);
138 /* If ocfs2_read_block() got us a new bh, pass it up. */
145 static u64
ocfs2_refcount_cache_owner(struct ocfs2_caching_info
*ci
)
147 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
152 static struct super_block
*
153 ocfs2_refcount_cache_get_super(struct ocfs2_caching_info
*ci
)
155 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
160 static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info
*ci
)
162 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
164 spin_lock(&rf
->rf_lock
);
167 static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info
*ci
)
169 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
171 spin_unlock(&rf
->rf_lock
);
174 static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info
*ci
)
176 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
178 mutex_lock(&rf
->rf_io_mutex
);
181 static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info
*ci
)
183 struct ocfs2_refcount_tree
*rf
= cache_info_to_refcount(ci
);
185 mutex_unlock(&rf
->rf_io_mutex
);
188 static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops
= {
189 .co_owner
= ocfs2_refcount_cache_owner
,
190 .co_get_super
= ocfs2_refcount_cache_get_super
,
191 .co_cache_lock
= ocfs2_refcount_cache_lock
,
192 .co_cache_unlock
= ocfs2_refcount_cache_unlock
,
193 .co_io_lock
= ocfs2_refcount_cache_io_lock
,
194 .co_io_unlock
= ocfs2_refcount_cache_io_unlock
,
197 static struct ocfs2_refcount_tree
*
198 ocfs2_find_refcount_tree(struct ocfs2_super
*osb
, u64 blkno
)
200 struct rb_node
*n
= osb
->osb_rf_lock_tree
.rb_node
;
201 struct ocfs2_refcount_tree
*tree
= NULL
;
204 tree
= rb_entry(n
, struct ocfs2_refcount_tree
, rf_node
);
206 if (blkno
< tree
->rf_blkno
)
208 else if (blkno
> tree
->rf_blkno
)
217 /* osb_lock is already locked. */
218 static void ocfs2_insert_refcount_tree(struct ocfs2_super
*osb
,
219 struct ocfs2_refcount_tree
*new)
221 u64 rf_blkno
= new->rf_blkno
;
222 struct rb_node
*parent
= NULL
;
223 struct rb_node
**p
= &osb
->osb_rf_lock_tree
.rb_node
;
224 struct ocfs2_refcount_tree
*tmp
;
229 tmp
= rb_entry(parent
, struct ocfs2_refcount_tree
,
232 if (rf_blkno
< tmp
->rf_blkno
)
234 else if (rf_blkno
> tmp
->rf_blkno
)
237 /* This should never happen! */
238 mlog(ML_ERROR
, "Duplicate refcount block %llu found!\n",
239 (unsigned long long)rf_blkno
);
244 rb_link_node(&new->rf_node
, parent
, p
);
245 rb_insert_color(&new->rf_node
, &osb
->osb_rf_lock_tree
);
248 static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree
*tree
)
250 ocfs2_metadata_cache_exit(&tree
->rf_ci
);
251 ocfs2_simple_drop_lockres(OCFS2_SB(tree
->rf_sb
), &tree
->rf_lockres
);
252 ocfs2_lock_res_free(&tree
->rf_lockres
);
257 ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super
*osb
,
258 struct ocfs2_refcount_tree
*tree
)
260 rb_erase(&tree
->rf_node
, &osb
->osb_rf_lock_tree
);
261 if (osb
->osb_ref_tree_lru
&& osb
->osb_ref_tree_lru
== tree
)
262 osb
->osb_ref_tree_lru
= NULL
;
265 static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super
*osb
,
266 struct ocfs2_refcount_tree
*tree
)
268 spin_lock(&osb
->osb_lock
);
269 ocfs2_erase_refcount_tree_from_list_no_lock(osb
, tree
);
270 spin_unlock(&osb
->osb_lock
);
273 void ocfs2_kref_remove_refcount_tree(struct kref
*kref
)
275 struct ocfs2_refcount_tree
*tree
=
276 container_of(kref
, struct ocfs2_refcount_tree
, rf_getcnt
);
278 ocfs2_free_refcount_tree(tree
);
282 ocfs2_refcount_tree_get(struct ocfs2_refcount_tree
*tree
)
284 kref_get(&tree
->rf_getcnt
);
288 ocfs2_refcount_tree_put(struct ocfs2_refcount_tree
*tree
)
290 kref_put(&tree
->rf_getcnt
, ocfs2_kref_remove_refcount_tree
);
293 static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree
*new,
294 struct super_block
*sb
)
296 ocfs2_metadata_cache_init(&new->rf_ci
, &ocfs2_refcount_caching_ops
);
297 mutex_init(&new->rf_io_mutex
);
299 spin_lock_init(&new->rf_lock
);
302 static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super
*osb
,
303 struct ocfs2_refcount_tree
*new,
304 u64 rf_blkno
, u32 generation
)
306 init_rwsem(&new->rf_sem
);
307 ocfs2_refcount_lock_res_init(&new->rf_lockres
, osb
,
308 rf_blkno
, generation
);
311 static struct ocfs2_refcount_tree
*
312 ocfs2_allocate_refcount_tree(struct ocfs2_super
*osb
, u64 rf_blkno
)
314 struct ocfs2_refcount_tree
*new;
316 new = kzalloc(sizeof(struct ocfs2_refcount_tree
), GFP_NOFS
);
320 new->rf_blkno
= rf_blkno
;
321 kref_init(&new->rf_getcnt
);
322 ocfs2_init_refcount_tree_ci(new, osb
->sb
);
327 static int ocfs2_get_refcount_tree(struct ocfs2_super
*osb
, u64 rf_blkno
,
328 struct ocfs2_refcount_tree
**ret_tree
)
331 struct ocfs2_refcount_tree
*tree
, *new = NULL
;
332 struct buffer_head
*ref_root_bh
= NULL
;
333 struct ocfs2_refcount_block
*ref_rb
;
335 spin_lock(&osb
->osb_lock
);
336 if (osb
->osb_ref_tree_lru
&&
337 osb
->osb_ref_tree_lru
->rf_blkno
== rf_blkno
)
338 tree
= osb
->osb_ref_tree_lru
;
340 tree
= ocfs2_find_refcount_tree(osb
, rf_blkno
);
344 spin_unlock(&osb
->osb_lock
);
346 new = ocfs2_allocate_refcount_tree(osb
, rf_blkno
);
353 * We need the generation to create the refcount tree lock and since
354 * it isn't changed during the tree modification, we are safe here to
355 * read without protection.
356 * We also have to purge the cache after we create the lock since the
357 * refcount block may have the stale data. It can only be trusted when
358 * we hold the refcount lock.
360 ret
= ocfs2_read_refcount_block(&new->rf_ci
, rf_blkno
, &ref_root_bh
);
363 ocfs2_metadata_cache_exit(&new->rf_ci
);
368 ref_rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
369 new->rf_generation
= le32_to_cpu(ref_rb
->rf_generation
);
370 ocfs2_init_refcount_tree_lock(osb
, new, rf_blkno
,
372 ocfs2_metadata_cache_purge(&new->rf_ci
);
374 spin_lock(&osb
->osb_lock
);
375 tree
= ocfs2_find_refcount_tree(osb
, rf_blkno
);
379 ocfs2_insert_refcount_tree(osb
, new);
387 osb
->osb_ref_tree_lru
= tree
;
389 spin_unlock(&osb
->osb_lock
);
392 ocfs2_free_refcount_tree(new);
398 static int ocfs2_get_refcount_block(struct inode
*inode
, u64
*ref_blkno
)
401 struct buffer_head
*di_bh
= NULL
;
402 struct ocfs2_dinode
*di
;
404 ret
= ocfs2_read_inode_block(inode
, &di_bh
);
410 BUG_ON(!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
412 di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
413 *ref_blkno
= le64_to_cpu(di
->i_refcount_loc
);
419 static int __ocfs2_lock_refcount_tree(struct ocfs2_super
*osb
,
420 struct ocfs2_refcount_tree
*tree
, int rw
)
424 ret
= ocfs2_refcount_lock(tree
, rw
);
431 down_write(&tree
->rf_sem
);
433 down_read(&tree
->rf_sem
);
440 * Lock the refcount tree pointed by ref_blkno and return the tree.
441 * In most case, we lock the tree and read the refcount block.
442 * So read it here if the caller really needs it.
444 * If the tree has been re-created by other node, it will free the
445 * old one and re-create it.
447 int ocfs2_lock_refcount_tree(struct ocfs2_super
*osb
,
448 u64 ref_blkno
, int rw
,
449 struct ocfs2_refcount_tree
**ret_tree
,
450 struct buffer_head
**ref_bh
)
452 int ret
, delete_tree
= 0;
453 struct ocfs2_refcount_tree
*tree
= NULL
;
454 struct buffer_head
*ref_root_bh
= NULL
;
455 struct ocfs2_refcount_block
*rb
;
458 ret
= ocfs2_get_refcount_tree(osb
, ref_blkno
, &tree
);
464 ocfs2_refcount_tree_get(tree
);
466 ret
= __ocfs2_lock_refcount_tree(osb
, tree
, rw
);
469 ocfs2_refcount_tree_put(tree
);
473 ret
= ocfs2_read_refcount_block(&tree
->rf_ci
, tree
->rf_blkno
,
477 ocfs2_unlock_refcount_tree(osb
, tree
, rw
);
478 ocfs2_refcount_tree_put(tree
);
482 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
484 * If the refcount block has been freed and re-created, we may need
485 * to recreate the refcount tree also.
487 * Here we just remove the tree from the rb-tree, and the last
488 * kref holder will unlock and delete this refcount_tree.
489 * Then we goto "again" and ocfs2_get_refcount_tree will create
490 * the new refcount tree for us.
492 if (tree
->rf_generation
!= le32_to_cpu(rb
->rf_generation
)) {
493 if (!tree
->rf_removed
) {
494 ocfs2_erase_refcount_tree_from_list(osb
, tree
);
495 tree
->rf_removed
= 1;
499 ocfs2_unlock_refcount_tree(osb
, tree
, rw
);
501 * We get an extra reference when we create the refcount
502 * tree, so another put will destroy it.
505 ocfs2_refcount_tree_put(tree
);
513 *ref_bh
= ref_root_bh
;
521 int ocfs2_lock_refcount_tree_by_inode(struct inode
*inode
, int rw
,
522 struct ocfs2_refcount_tree
**ret_tree
,
523 struct buffer_head
**ref_bh
)
528 ret
= ocfs2_get_refcount_block(inode
, &ref_blkno
);
534 return ocfs2_lock_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_blkno
,
535 rw
, ret_tree
, ref_bh
);
538 void ocfs2_unlock_refcount_tree(struct ocfs2_super
*osb
,
539 struct ocfs2_refcount_tree
*tree
, int rw
)
542 up_write(&tree
->rf_sem
);
544 up_read(&tree
->rf_sem
);
546 ocfs2_refcount_unlock(tree
, rw
);
547 ocfs2_refcount_tree_put(tree
);
550 void ocfs2_purge_refcount_trees(struct ocfs2_super
*osb
)
552 struct rb_node
*node
;
553 struct ocfs2_refcount_tree
*tree
;
554 struct rb_root
*root
= &osb
->osb_rf_lock_tree
;
556 while ((node
= rb_last(root
)) != NULL
) {
557 tree
= rb_entry(node
, struct ocfs2_refcount_tree
, rf_node
);
559 mlog(0, "Purge tree %llu\n",
560 (unsigned long long) tree
->rf_blkno
);
562 rb_erase(&tree
->rf_node
, root
);
563 ocfs2_free_refcount_tree(tree
);
568 * Create a refcount tree for an inode.
569 * We take for granted that the inode is already locked.
571 static int ocfs2_create_refcount_tree(struct inode
*inode
,
572 struct buffer_head
*di_bh
)
575 handle_t
*handle
= NULL
;
576 struct ocfs2_alloc_context
*meta_ac
= NULL
;
577 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
578 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
579 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
580 struct buffer_head
*new_bh
= NULL
;
581 struct ocfs2_refcount_block
*rb
;
582 struct ocfs2_refcount_tree
*new_tree
= NULL
, *tree
= NULL
;
583 u16 suballoc_bit_start
;
587 BUG_ON(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
);
589 mlog(0, "create tree for inode %lu\n", inode
->i_ino
);
591 ret
= ocfs2_reserve_new_metadata_blocks(osb
, 1, &meta_ac
);
597 handle
= ocfs2_start_trans(osb
, OCFS2_REFCOUNT_TREE_CREATE_CREDITS
);
598 if (IS_ERR(handle
)) {
599 ret
= PTR_ERR(handle
);
604 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
605 OCFS2_JOURNAL_ACCESS_WRITE
);
611 ret
= ocfs2_claim_metadata(osb
, handle
, meta_ac
, 1,
612 &suballoc_bit_start
, &num_got
,
619 new_tree
= ocfs2_allocate_refcount_tree(osb
, first_blkno
);
626 new_bh
= sb_getblk(inode
->i_sb
, first_blkno
);
627 ocfs2_set_new_buffer_uptodate(&new_tree
->rf_ci
, new_bh
);
629 ret
= ocfs2_journal_access_rb(handle
, &new_tree
->rf_ci
, new_bh
,
630 OCFS2_JOURNAL_ACCESS_CREATE
);
636 /* Initialize ocfs2_refcount_block. */
637 rb
= (struct ocfs2_refcount_block
*)new_bh
->b_data
;
638 memset(rb
, 0, inode
->i_sb
->s_blocksize
);
639 strcpy((void *)rb
, OCFS2_REFCOUNT_BLOCK_SIGNATURE
);
640 rb
->rf_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
641 rb
->rf_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
642 rb
->rf_fs_generation
= cpu_to_le32(osb
->fs_generation
);
643 rb
->rf_blkno
= cpu_to_le64(first_blkno
);
644 rb
->rf_count
= cpu_to_le32(1);
645 rb
->rf_records
.rl_count
=
646 cpu_to_le16(ocfs2_refcount_recs_per_rb(osb
->sb
));
647 spin_lock(&osb
->osb_lock
);
648 rb
->rf_generation
= osb
->s_next_generation
++;
649 spin_unlock(&osb
->osb_lock
);
651 ocfs2_journal_dirty(handle
, new_bh
);
653 spin_lock(&oi
->ip_lock
);
654 oi
->ip_dyn_features
|= OCFS2_HAS_REFCOUNT_FL
;
655 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
656 di
->i_refcount_loc
= cpu_to_le64(first_blkno
);
657 spin_unlock(&oi
->ip_lock
);
659 mlog(0, "created tree for inode %lu, refblock %llu\n",
660 inode
->i_ino
, (unsigned long long)first_blkno
);
662 ocfs2_journal_dirty(handle
, di_bh
);
665 * We have to init the tree lock here since it will use
666 * the generation number to create it.
668 new_tree
->rf_generation
= le32_to_cpu(rb
->rf_generation
);
669 ocfs2_init_refcount_tree_lock(osb
, new_tree
, first_blkno
,
670 new_tree
->rf_generation
);
672 spin_lock(&osb
->osb_lock
);
673 tree
= ocfs2_find_refcount_tree(osb
, first_blkno
);
676 * We've just created a new refcount tree in this block. If
677 * we found a refcount tree on the ocfs2_super, it must be
678 * one we just deleted. We free the old tree before
679 * inserting the new tree.
681 BUG_ON(tree
&& tree
->rf_generation
== new_tree
->rf_generation
);
683 ocfs2_erase_refcount_tree_from_list_no_lock(osb
, tree
);
684 ocfs2_insert_refcount_tree(osb
, new_tree
);
685 spin_unlock(&osb
->osb_lock
);
688 ocfs2_refcount_tree_put(tree
);
691 ocfs2_commit_trans(osb
, handle
);
695 ocfs2_metadata_cache_exit(&new_tree
->rf_ci
);
701 ocfs2_free_alloc_context(meta_ac
);
706 static int ocfs2_set_refcount_tree(struct inode
*inode
,
707 struct buffer_head
*di_bh
,
711 handle_t
*handle
= NULL
;
712 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
713 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
714 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
715 struct buffer_head
*ref_root_bh
= NULL
;
716 struct ocfs2_refcount_block
*rb
;
717 struct ocfs2_refcount_tree
*ref_tree
;
719 BUG_ON(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
);
721 ret
= ocfs2_lock_refcount_tree(osb
, refcount_loc
, 1,
722 &ref_tree
, &ref_root_bh
);
728 handle
= ocfs2_start_trans(osb
, OCFS2_REFCOUNT_TREE_SET_CREDITS
);
729 if (IS_ERR(handle
)) {
730 ret
= PTR_ERR(handle
);
735 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
736 OCFS2_JOURNAL_ACCESS_WRITE
);
742 ret
= ocfs2_journal_access_rb(handle
, &ref_tree
->rf_ci
, ref_root_bh
,
743 OCFS2_JOURNAL_ACCESS_WRITE
);
749 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
750 le32_add_cpu(&rb
->rf_count
, 1);
752 ocfs2_journal_dirty(handle
, ref_root_bh
);
754 spin_lock(&oi
->ip_lock
);
755 oi
->ip_dyn_features
|= OCFS2_HAS_REFCOUNT_FL
;
756 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
757 di
->i_refcount_loc
= cpu_to_le64(refcount_loc
);
758 spin_unlock(&oi
->ip_lock
);
759 ocfs2_journal_dirty(handle
, di_bh
);
762 ocfs2_commit_trans(osb
, handle
);
764 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
770 int ocfs2_remove_refcount_tree(struct inode
*inode
, struct buffer_head
*di_bh
)
772 int ret
, delete_tree
= 0;
773 handle_t
*handle
= NULL
;
774 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
775 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
776 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
777 struct ocfs2_refcount_block
*rb
;
778 struct inode
*alloc_inode
= NULL
;
779 struct buffer_head
*alloc_bh
= NULL
;
780 struct buffer_head
*blk_bh
= NULL
;
781 struct ocfs2_refcount_tree
*ref_tree
;
782 int credits
= OCFS2_REFCOUNT_TREE_REMOVE_CREDITS
;
783 u64 blk
= 0, bg_blkno
= 0, ref_blkno
= le64_to_cpu(di
->i_refcount_loc
);
786 if (!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
))
790 ret
= ocfs2_lock_refcount_tree(osb
, ref_blkno
, 1, &ref_tree
, &blk_bh
);
796 rb
= (struct ocfs2_refcount_block
*)blk_bh
->b_data
;
799 * If we are the last user, we need to free the block.
800 * So lock the allocator ahead.
802 if (le32_to_cpu(rb
->rf_count
) == 1) {
803 blk
= le64_to_cpu(rb
->rf_blkno
);
804 bit
= le16_to_cpu(rb
->rf_suballoc_bit
);
805 bg_blkno
= ocfs2_which_suballoc_group(blk
, bit
);
807 alloc_inode
= ocfs2_get_system_file_inode(osb
,
808 EXTENT_ALLOC_SYSTEM_INODE
,
809 le16_to_cpu(rb
->rf_suballoc_slot
));
815 mutex_lock(&alloc_inode
->i_mutex
);
817 ret
= ocfs2_inode_lock(alloc_inode
, &alloc_bh
, 1);
823 credits
+= OCFS2_SUBALLOC_FREE
;
826 handle
= ocfs2_start_trans(osb
, credits
);
827 if (IS_ERR(handle
)) {
828 ret
= PTR_ERR(handle
);
833 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
834 OCFS2_JOURNAL_ACCESS_WRITE
);
840 ret
= ocfs2_journal_access_rb(handle
, &ref_tree
->rf_ci
, blk_bh
,
841 OCFS2_JOURNAL_ACCESS_WRITE
);
847 spin_lock(&oi
->ip_lock
);
848 oi
->ip_dyn_features
&= ~OCFS2_HAS_REFCOUNT_FL
;
849 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
850 di
->i_refcount_loc
= 0;
851 spin_unlock(&oi
->ip_lock
);
852 ocfs2_journal_dirty(handle
, di_bh
);
854 le32_add_cpu(&rb
->rf_count
, -1);
855 ocfs2_journal_dirty(handle
, blk_bh
);
859 ocfs2_erase_refcount_tree_from_list(osb
, ref_tree
);
860 ret
= ocfs2_free_suballoc_bits(handle
, alloc_inode
,
861 alloc_bh
, bit
, bg_blkno
, 1);
867 ocfs2_commit_trans(osb
, handle
);
870 ocfs2_inode_unlock(alloc_inode
, 1);
875 mutex_unlock(&alloc_inode
->i_mutex
);
879 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
881 ocfs2_refcount_tree_put(ref_tree
);
887 static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info
*ci
,
888 struct buffer_head
*ref_leaf_bh
,
889 u64 cpos
, unsigned int len
,
890 struct ocfs2_refcount_rec
*ret_rec
,
894 struct ocfs2_refcount_block
*rb
=
895 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
896 struct ocfs2_refcount_rec
*rec
= NULL
;
898 for (; i
< le16_to_cpu(rb
->rf_records
.rl_used
); i
++) {
899 rec
= &rb
->rf_records
.rl_recs
[i
];
901 if (le64_to_cpu(rec
->r_cpos
) +
902 le32_to_cpu(rec
->r_clusters
) <= cpos
)
904 else if (le64_to_cpu(rec
->r_cpos
) > cpos
)
907 /* ok, cpos fail in this rec. Just return. */
914 /* We meet with a hole here, so fake the rec. */
915 ret_rec
->r_cpos
= cpu_to_le64(cpos
);
916 ret_rec
->r_refcount
= 0;
917 if (i
< le16_to_cpu(rb
->rf_records
.rl_used
) &&
918 le64_to_cpu(rec
->r_cpos
) < cpos
+ len
)
919 ret_rec
->r_clusters
=
920 cpu_to_le32(le64_to_cpu(rec
->r_cpos
) - cpos
);
922 ret_rec
->r_clusters
= cpu_to_le32(len
);
930 * Given a cpos and len, try to find the refcount record which contains cpos.
931 * 1. If cpos can be found in one refcount record, return the record.
932 * 2. If cpos can't be found, return a fake record which start from cpos
933 * and end at a small value between cpos+len and start of the next record.
934 * This fake record has r_refcount = 0.
936 static int ocfs2_get_refcount_rec(struct ocfs2_caching_info
*ci
,
937 struct buffer_head
*ref_root_bh
,
938 u64 cpos
, unsigned int len
,
939 struct ocfs2_refcount_rec
*ret_rec
,
941 struct buffer_head
**ret_bh
)
943 int ret
= 0, i
, found
;
945 struct ocfs2_extent_list
*el
;
946 struct ocfs2_extent_rec
*tmp
, *rec
= NULL
;
947 struct ocfs2_extent_block
*eb
;
948 struct buffer_head
*eb_bh
= NULL
, *ref_leaf_bh
= NULL
;
949 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
950 struct ocfs2_refcount_block
*rb
=
951 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
953 if (!(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)) {
954 ocfs2_find_refcount_rec_in_rl(ci
, ref_root_bh
, cpos
, len
,
956 *ret_bh
= ref_root_bh
;
962 low_cpos
= cpos
& OCFS2_32BIT_POS_MASK
;
964 if (el
->l_tree_depth
) {
965 ret
= ocfs2_find_leaf(ci
, el
, low_cpos
, &eb_bh
);
971 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
974 if (el
->l_tree_depth
) {
976 "refcount tree %llu has non zero tree "
977 "depth in leaf btree tree block %llu\n",
978 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
979 (unsigned long long)eb_bh
->b_blocknr
);
986 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
987 rec
= &el
->l_recs
[i
];
989 if (le32_to_cpu(rec
->e_cpos
) <= low_cpos
) {
995 /* adjust len when we have ocfs2_extent_rec after it. */
996 if (found
&& i
< le16_to_cpu(el
->l_next_free_rec
) - 1) {
997 tmp
= &el
->l_recs
[i
+1];
999 if (le32_to_cpu(tmp
->e_cpos
) < cpos
+ len
)
1000 len
= le32_to_cpu(tmp
->e_cpos
) - cpos
;
1003 ret
= ocfs2_read_refcount_block(ci
, le64_to_cpu(rec
->e_blkno
),
1010 ocfs2_find_refcount_rec_in_rl(ci
, ref_leaf_bh
, cpos
, len
,
1012 *ret_bh
= ref_leaf_bh
;
1018 enum ocfs2_ref_rec_contig
{
1019 REF_CONTIG_NONE
= 0,
1022 REF_CONTIG_LEFTRIGHT
,
1025 static enum ocfs2_ref_rec_contig
1026 ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block
*rb
,
1029 if ((rb
->rf_records
.rl_recs
[index
].r_refcount
==
1030 rb
->rf_records
.rl_recs
[index
+ 1].r_refcount
) &&
1031 (le64_to_cpu(rb
->rf_records
.rl_recs
[index
].r_cpos
) +
1032 le32_to_cpu(rb
->rf_records
.rl_recs
[index
].r_clusters
) ==
1033 le64_to_cpu(rb
->rf_records
.rl_recs
[index
+ 1].r_cpos
)))
1034 return REF_CONTIG_RIGHT
;
1036 return REF_CONTIG_NONE
;
1039 static enum ocfs2_ref_rec_contig
1040 ocfs2_refcount_rec_contig(struct ocfs2_refcount_block
*rb
,
1043 enum ocfs2_ref_rec_contig ret
= REF_CONTIG_NONE
;
1045 if (index
< le16_to_cpu(rb
->rf_records
.rl_used
) - 1)
1046 ret
= ocfs2_refcount_rec_adjacent(rb
, index
);
1049 enum ocfs2_ref_rec_contig tmp
;
1051 tmp
= ocfs2_refcount_rec_adjacent(rb
, index
- 1);
1053 if (tmp
== REF_CONTIG_RIGHT
) {
1054 if (ret
== REF_CONTIG_RIGHT
)
1055 ret
= REF_CONTIG_LEFTRIGHT
;
1057 ret
= REF_CONTIG_LEFT
;
1064 static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block
*rb
,
1067 BUG_ON(rb
->rf_records
.rl_recs
[index
].r_refcount
!=
1068 rb
->rf_records
.rl_recs
[index
+1].r_refcount
);
1070 le32_add_cpu(&rb
->rf_records
.rl_recs
[index
].r_clusters
,
1071 le32_to_cpu(rb
->rf_records
.rl_recs
[index
+1].r_clusters
));
1073 if (index
< le16_to_cpu(rb
->rf_records
.rl_used
) - 2)
1074 memmove(&rb
->rf_records
.rl_recs
[index
+ 1],
1075 &rb
->rf_records
.rl_recs
[index
+ 2],
1076 sizeof(struct ocfs2_refcount_rec
) *
1077 (le16_to_cpu(rb
->rf_records
.rl_used
) - index
- 2));
1079 memset(&rb
->rf_records
.rl_recs
[le16_to_cpu(rb
->rf_records
.rl_used
) - 1],
1080 0, sizeof(struct ocfs2_refcount_rec
));
1081 le16_add_cpu(&rb
->rf_records
.rl_used
, -1);
1085 * Merge the refcount rec if we are contiguous with the adjacent recs.
1087 static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block
*rb
,
1090 enum ocfs2_ref_rec_contig contig
=
1091 ocfs2_refcount_rec_contig(rb
, index
);
1093 if (contig
== REF_CONTIG_NONE
)
1096 if (contig
== REF_CONTIG_LEFT
|| contig
== REF_CONTIG_LEFTRIGHT
) {
1101 ocfs2_rotate_refcount_rec_left(rb
, index
);
1103 if (contig
== REF_CONTIG_LEFTRIGHT
)
1104 ocfs2_rotate_refcount_rec_left(rb
, index
);
1108 * Change the refcount indexed by "index" in ref_bh.
1109 * If refcount reaches 0, remove it.
1111 static int ocfs2_change_refcount_rec(handle_t
*handle
,
1112 struct ocfs2_caching_info
*ci
,
1113 struct buffer_head
*ref_leaf_bh
,
1114 int index
, int change
)
1117 struct ocfs2_refcount_block
*rb
=
1118 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1119 struct ocfs2_refcount_list
*rl
= &rb
->rf_records
;
1120 struct ocfs2_refcount_rec
*rec
= &rl
->rl_recs
[index
];
1122 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1123 OCFS2_JOURNAL_ACCESS_WRITE
);
1129 mlog(0, "change index %d, old count %u, change %d\n", index
,
1130 le32_to_cpu(rec
->r_refcount
), change
);
1131 le32_add_cpu(&rec
->r_refcount
, change
);
1133 if (!rec
->r_refcount
) {
1134 if (index
!= le16_to_cpu(rl
->rl_used
) - 1) {
1135 memmove(rec
, rec
+ 1,
1136 (le16_to_cpu(rl
->rl_used
) - index
- 1) *
1137 sizeof(struct ocfs2_refcount_rec
));
1138 memset(&rl
->rl_recs
[le16_to_cpu(rl
->rl_used
) - 1],
1139 0, sizeof(struct ocfs2_refcount_rec
));
1142 le16_add_cpu(&rl
->rl_used
, -1);
1144 ocfs2_refcount_rec_merge(rb
, index
);
1146 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1153 static int ocfs2_expand_inline_ref_root(handle_t
*handle
,
1154 struct ocfs2_caching_info
*ci
,
1155 struct buffer_head
*ref_root_bh
,
1156 struct buffer_head
**ref_leaf_bh
,
1157 struct ocfs2_alloc_context
*meta_ac
)
1160 u16 suballoc_bit_start
;
1163 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1164 struct buffer_head
*new_bh
= NULL
;
1165 struct ocfs2_refcount_block
*new_rb
;
1166 struct ocfs2_refcount_block
*root_rb
=
1167 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1169 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
1170 OCFS2_JOURNAL_ACCESS_WRITE
);
1176 ret
= ocfs2_claim_metadata(OCFS2_SB(sb
), handle
, meta_ac
, 1,
1177 &suballoc_bit_start
, &num_got
,
1184 new_bh
= sb_getblk(sb
, blkno
);
1185 if (new_bh
== NULL
) {
1190 ocfs2_set_new_buffer_uptodate(ci
, new_bh
);
1192 ret
= ocfs2_journal_access_rb(handle
, ci
, new_bh
,
1193 OCFS2_JOURNAL_ACCESS_CREATE
);
1200 * Initialize ocfs2_refcount_block.
1201 * It should contain the same information as the old root.
1202 * so just memcpy it and change the corresponding field.
1204 memcpy(new_bh
->b_data
, ref_root_bh
->b_data
, sb
->s_blocksize
);
1206 new_rb
= (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1207 new_rb
->rf_suballoc_slot
= cpu_to_le16(OCFS2_SB(sb
)->slot_num
);
1208 new_rb
->rf_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1209 new_rb
->rf_blkno
= cpu_to_le64(blkno
);
1210 new_rb
->rf_cpos
= cpu_to_le32(0);
1211 new_rb
->rf_parent
= cpu_to_le64(ref_root_bh
->b_blocknr
);
1212 new_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL
);
1213 ocfs2_journal_dirty(handle
, new_bh
);
1215 /* Now change the root. */
1216 memset(&root_rb
->rf_list
, 0, sb
->s_blocksize
-
1217 offsetof(struct ocfs2_refcount_block
, rf_list
));
1218 root_rb
->rf_list
.l_count
= cpu_to_le16(ocfs2_extent_recs_per_rb(sb
));
1219 root_rb
->rf_clusters
= cpu_to_le32(1);
1220 root_rb
->rf_list
.l_next_free_rec
= cpu_to_le16(1);
1221 root_rb
->rf_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
1222 root_rb
->rf_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
1223 root_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_TREE_FL
);
1225 ocfs2_journal_dirty(handle
, ref_root_bh
);
1227 mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno
,
1228 le16_to_cpu(new_rb
->rf_records
.rl_used
));
1230 *ref_leaf_bh
= new_bh
;
1237 static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec
*prev
,
1238 struct ocfs2_refcount_rec
*next
)
1240 if (ocfs2_get_ref_rec_low_cpos(prev
) + le32_to_cpu(prev
->r_clusters
) <=
1241 ocfs2_get_ref_rec_low_cpos(next
))
1247 static int cmp_refcount_rec_by_low_cpos(const void *a
, const void *b
)
1249 const struct ocfs2_refcount_rec
*l
= a
, *r
= b
;
1250 u32 l_cpos
= ocfs2_get_ref_rec_low_cpos(l
);
1251 u32 r_cpos
= ocfs2_get_ref_rec_low_cpos(r
);
1253 if (l_cpos
> r_cpos
)
1255 if (l_cpos
< r_cpos
)
1260 static int cmp_refcount_rec_by_cpos(const void *a
, const void *b
)
1262 const struct ocfs2_refcount_rec
*l
= a
, *r
= b
;
1263 u64 l_cpos
= le64_to_cpu(l
->r_cpos
);
1264 u64 r_cpos
= le64_to_cpu(r
->r_cpos
);
1266 if (l_cpos
> r_cpos
)
1268 if (l_cpos
< r_cpos
)
1273 static void swap_refcount_rec(void *a
, void *b
, int size
)
1275 struct ocfs2_refcount_rec
*l
= a
, *r
= b
, tmp
;
1277 tmp
= *(struct ocfs2_refcount_rec
*)l
;
1278 *(struct ocfs2_refcount_rec
*)l
=
1279 *(struct ocfs2_refcount_rec
*)r
;
1280 *(struct ocfs2_refcount_rec
*)r
= tmp
;
1284 * The refcount cpos are ordered by their 64bit cpos,
1285 * But we will use the low 32 bit to be the e_cpos in the b-tree.
1286 * So we need to make sure that this pos isn't intersected with others.
1288 * Note: The refcount block is already sorted by their low 32 bit cpos,
1289 * So just try the middle pos first, and we will exit when we find
1290 * the good position.
1292 static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list
*rl
,
1293 u32
*split_pos
, int *split_index
)
1295 int num_used
= le16_to_cpu(rl
->rl_used
);
1296 int delta
, middle
= num_used
/ 2;
1298 for (delta
= 0; delta
< middle
; delta
++) {
1299 /* Let's check delta earlier than middle */
1300 if (ocfs2_refcount_rec_no_intersect(
1301 &rl
->rl_recs
[middle
- delta
- 1],
1302 &rl
->rl_recs
[middle
- delta
])) {
1303 *split_index
= middle
- delta
;
1307 /* For even counts, don't walk off the end */
1308 if ((middle
+ delta
+ 1) == num_used
)
1311 /* Now try delta past middle */
1312 if (ocfs2_refcount_rec_no_intersect(
1313 &rl
->rl_recs
[middle
+ delta
],
1314 &rl
->rl_recs
[middle
+ delta
+ 1])) {
1315 *split_index
= middle
+ delta
+ 1;
1320 if (delta
>= middle
)
1323 *split_pos
= ocfs2_get_ref_rec_low_cpos(&rl
->rl_recs
[*split_index
]);
1327 static int ocfs2_divide_leaf_refcount_block(struct buffer_head
*ref_leaf_bh
,
1328 struct buffer_head
*new_bh
,
1331 int split_index
= 0, num_moved
, ret
;
1333 struct ocfs2_refcount_block
*rb
=
1334 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1335 struct ocfs2_refcount_list
*rl
= &rb
->rf_records
;
1336 struct ocfs2_refcount_block
*new_rb
=
1337 (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1338 struct ocfs2_refcount_list
*new_rl
= &new_rb
->rf_records
;
1340 mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
1341 (unsigned long long)ref_leaf_bh
->b_blocknr
,
1342 le32_to_cpu(rl
->rl_count
), le32_to_cpu(rl
->rl_used
));
1345 * XXX: Improvement later.
1346 * If we know all the high 32 bit cpos is the same, no need to sort.
1348 * In order to make the whole process safe, we do:
1349 * 1. sort the entries by their low 32 bit cpos first so that we can
1350 * find the split cpos easily.
1351 * 2. call ocfs2_insert_extent to insert the new refcount block.
1352 * 3. move the refcount rec to the new block.
1353 * 4. sort the entries by their 64 bit cpos.
1354 * 5. dirty the new_rb and rb.
1356 sort(&rl
->rl_recs
, le16_to_cpu(rl
->rl_used
),
1357 sizeof(struct ocfs2_refcount_rec
),
1358 cmp_refcount_rec_by_low_cpos
, swap_refcount_rec
);
1360 ret
= ocfs2_find_refcount_split_pos(rl
, &cpos
, &split_index
);
1366 new_rb
->rf_cpos
= cpu_to_le32(cpos
);
1368 /* move refcount records starting from split_index to the new block. */
1369 num_moved
= le16_to_cpu(rl
->rl_used
) - split_index
;
1370 memcpy(new_rl
->rl_recs
, &rl
->rl_recs
[split_index
],
1371 num_moved
* sizeof(struct ocfs2_refcount_rec
));
1373 /*ok, remove the entries we just moved over to the other block. */
1374 memset(&rl
->rl_recs
[split_index
], 0,
1375 num_moved
* sizeof(struct ocfs2_refcount_rec
));
1377 /* change old and new rl_used accordingly. */
1378 le16_add_cpu(&rl
->rl_used
, -num_moved
);
1379 new_rl
->rl_used
= cpu_to_le32(num_moved
);
1381 sort(&rl
->rl_recs
, le16_to_cpu(rl
->rl_used
),
1382 sizeof(struct ocfs2_refcount_rec
),
1383 cmp_refcount_rec_by_cpos
, swap_refcount_rec
);
1385 sort(&new_rl
->rl_recs
, le16_to_cpu(new_rl
->rl_used
),
1386 sizeof(struct ocfs2_refcount_rec
),
1387 cmp_refcount_rec_by_cpos
, swap_refcount_rec
);
1393 static int ocfs2_new_leaf_refcount_block(handle_t
*handle
,
1394 struct ocfs2_caching_info
*ci
,
1395 struct buffer_head
*ref_root_bh
,
1396 struct buffer_head
*ref_leaf_bh
,
1397 struct ocfs2_alloc_context
*meta_ac
)
1400 u16 suballoc_bit_start
;
1401 u32 num_got
, new_cpos
;
1403 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1404 struct ocfs2_refcount_block
*root_rb
=
1405 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1406 struct buffer_head
*new_bh
= NULL
;
1407 struct ocfs2_refcount_block
*new_rb
;
1408 struct ocfs2_extent_tree ref_et
;
1410 BUG_ON(!(le32_to_cpu(root_rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
));
1412 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
1413 OCFS2_JOURNAL_ACCESS_WRITE
);
1419 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1420 OCFS2_JOURNAL_ACCESS_WRITE
);
1426 ret
= ocfs2_claim_metadata(OCFS2_SB(sb
), handle
, meta_ac
, 1,
1427 &suballoc_bit_start
, &num_got
,
1434 new_bh
= sb_getblk(sb
, blkno
);
1435 if (new_bh
== NULL
) {
1440 ocfs2_set_new_buffer_uptodate(ci
, new_bh
);
1442 ret
= ocfs2_journal_access_rb(handle
, ci
, new_bh
,
1443 OCFS2_JOURNAL_ACCESS_CREATE
);
1449 /* Initialize ocfs2_refcount_block. */
1450 new_rb
= (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1451 memset(new_rb
, 0, sb
->s_blocksize
);
1452 strcpy((void *)new_rb
, OCFS2_REFCOUNT_BLOCK_SIGNATURE
);
1453 new_rb
->rf_suballoc_slot
= cpu_to_le16(OCFS2_SB(sb
)->slot_num
);
1454 new_rb
->rf_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1455 new_rb
->rf_fs_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
1456 new_rb
->rf_blkno
= cpu_to_le64(blkno
);
1457 new_rb
->rf_parent
= cpu_to_le64(ref_root_bh
->b_blocknr
);
1458 new_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL
);
1459 new_rb
->rf_records
.rl_count
=
1460 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb
));
1461 new_rb
->rf_generation
= root_rb
->rf_generation
;
1463 ret
= ocfs2_divide_leaf_refcount_block(ref_leaf_bh
, new_bh
, &new_cpos
);
1469 ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1470 ocfs2_journal_dirty(handle
, new_bh
);
1472 ocfs2_init_refcount_extent_tree(&ref_et
, ci
, ref_root_bh
);
1474 mlog(0, "insert new leaf block %llu at %u\n",
1475 (unsigned long long)new_bh
->b_blocknr
, new_cpos
);
1477 /* Insert the new leaf block with the specific offset cpos. */
1478 ret
= ocfs2_insert_extent(handle
, &ref_et
, new_cpos
, new_bh
->b_blocknr
,
1488 static int ocfs2_expand_refcount_tree(handle_t
*handle
,
1489 struct ocfs2_caching_info
*ci
,
1490 struct buffer_head
*ref_root_bh
,
1491 struct buffer_head
*ref_leaf_bh
,
1492 struct ocfs2_alloc_context
*meta_ac
)
1495 struct buffer_head
*expand_bh
= NULL
;
1497 if (ref_root_bh
== ref_leaf_bh
) {
1499 * the old root bh hasn't been expanded to a b-tree,
1500 * so expand it first.
1502 ret
= ocfs2_expand_inline_ref_root(handle
, ci
, ref_root_bh
,
1503 &expand_bh
, meta_ac
);
1509 expand_bh
= ref_leaf_bh
;
1514 /* Now add a new refcount block into the tree.*/
1515 ret
= ocfs2_new_leaf_refcount_block(handle
, ci
, ref_root_bh
,
1516 expand_bh
, meta_ac
);
1525 * Adjust the extent rec in b-tree representing ref_leaf_bh.
1527 * Only called when we have inserted a new refcount rec at index 0
1528 * which means ocfs2_extent_rec.e_cpos may need some change.
1530 static int ocfs2_adjust_refcount_rec(handle_t
*handle
,
1531 struct ocfs2_caching_info
*ci
,
1532 struct buffer_head
*ref_root_bh
,
1533 struct buffer_head
*ref_leaf_bh
,
1534 struct ocfs2_refcount_rec
*rec
)
1537 u32 new_cpos
, old_cpos
;
1538 struct ocfs2_path
*path
= NULL
;
1539 struct ocfs2_extent_tree et
;
1540 struct ocfs2_refcount_block
*rb
=
1541 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1542 struct ocfs2_extent_list
*el
;
1544 if (!(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
))
1547 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1548 old_cpos
= le32_to_cpu(rb
->rf_cpos
);
1549 new_cpos
= le64_to_cpu(rec
->r_cpos
) & OCFS2_32BIT_POS_MASK
;
1550 if (old_cpos
<= new_cpos
)
1553 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
1555 path
= ocfs2_new_path_from_et(&et
);
1562 ret
= ocfs2_find_path(ci
, path
, old_cpos
);
1569 * 2 more credits, one for the leaf refcount block, one for
1570 * the extent block contains the extent rec.
1572 ret
= ocfs2_extend_trans(handle
, handle
->h_buffer_credits
+ 2);
1578 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1579 OCFS2_JOURNAL_ACCESS_WRITE
);
1585 ret
= ocfs2_journal_access_eb(handle
, ci
, path_leaf_bh(path
),
1586 OCFS2_JOURNAL_ACCESS_WRITE
);
1592 /* change the leaf extent block first. */
1593 el
= path_leaf_el(path
);
1595 for (i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
1596 if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) == old_cpos
)
1599 BUG_ON(i
== le16_to_cpu(el
->l_next_free_rec
));
1601 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1603 /* change the r_cpos in the leaf block. */
1604 rb
->rf_cpos
= cpu_to_le32(new_cpos
);
1606 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
1607 ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1610 ocfs2_free_path(path
);
1614 static int ocfs2_insert_refcount_rec(handle_t
*handle
,
1615 struct ocfs2_caching_info
*ci
,
1616 struct buffer_head
*ref_root_bh
,
1617 struct buffer_head
*ref_leaf_bh
,
1618 struct ocfs2_refcount_rec
*rec
,
1620 struct ocfs2_alloc_context
*meta_ac
)
1623 struct ocfs2_refcount_block
*rb
=
1624 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1625 struct ocfs2_refcount_list
*rf_list
= &rb
->rf_records
;
1626 struct buffer_head
*new_bh
= NULL
;
1628 BUG_ON(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
);
1630 if (rf_list
->rl_used
== rf_list
->rl_count
) {
1631 u64 cpos
= le64_to_cpu(rec
->r_cpos
);
1632 u32 len
= le32_to_cpu(rec
->r_clusters
);
1634 ret
= ocfs2_expand_refcount_tree(handle
, ci
, ref_root_bh
,
1635 ref_leaf_bh
, meta_ac
);
1641 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1642 cpos
, len
, NULL
, &index
,
1649 ref_leaf_bh
= new_bh
;
1650 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1651 rf_list
= &rb
->rf_records
;
1654 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1655 OCFS2_JOURNAL_ACCESS_WRITE
);
1661 if (index
< le16_to_cpu(rf_list
->rl_used
))
1662 memmove(&rf_list
->rl_recs
[index
+ 1],
1663 &rf_list
->rl_recs
[index
],
1664 (le16_to_cpu(rf_list
->rl_used
) - index
) *
1665 sizeof(struct ocfs2_refcount_rec
));
1667 mlog(0, "insert refcount record start %llu, len %u, count %u "
1668 "to leaf block %llu at index %d\n",
1669 (unsigned long long)le64_to_cpu(rec
->r_cpos
),
1670 le32_to_cpu(rec
->r_clusters
), le32_to_cpu(rec
->r_refcount
),
1671 (unsigned long long)ref_leaf_bh
->b_blocknr
, index
);
1673 rf_list
->rl_recs
[index
] = *rec
;
1675 le16_add_cpu(&rf_list
->rl_used
, 1);
1677 ocfs2_refcount_rec_merge(rb
, index
);
1679 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1686 ret
= ocfs2_adjust_refcount_rec(handle
, ci
,
1698 * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1699 * This is much simple than our b-tree code.
1700 * split_rec is the new refcount rec we want to insert.
1701 * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1702 * increase refcount or decrease a refcount to non-zero).
1703 * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1704 * rec( in case we decrease a refcount to zero).
1706 static int ocfs2_split_refcount_rec(handle_t
*handle
,
1707 struct ocfs2_caching_info
*ci
,
1708 struct buffer_head
*ref_root_bh
,
1709 struct buffer_head
*ref_leaf_bh
,
1710 struct ocfs2_refcount_rec
*split_rec
,
1712 struct ocfs2_alloc_context
*meta_ac
,
1713 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1717 struct ocfs2_refcount_block
*rb
=
1718 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1719 struct ocfs2_refcount_list
*rf_list
= &rb
->rf_records
;
1720 struct ocfs2_refcount_rec
*orig_rec
= &rf_list
->rl_recs
[index
];
1721 struct ocfs2_refcount_rec
*tail_rec
= NULL
;
1722 struct buffer_head
*new_bh
= NULL
;
1724 BUG_ON(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
);
1726 mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
1727 le64_to_cpu(orig_rec
->r_cpos
), le32_to_cpu(orig_rec
->r_clusters
),
1728 le64_to_cpu(split_rec
->r_cpos
),
1729 le32_to_cpu(split_rec
->r_clusters
));
1732 * If we just need to split the header or tail clusters,
1733 * no more recs are needed, just split is OK.
1734 * Otherwise we at least need one new recs.
1736 if (!split_rec
->r_refcount
&&
1737 (split_rec
->r_cpos
== orig_rec
->r_cpos
||
1738 le64_to_cpu(split_rec
->r_cpos
) +
1739 le32_to_cpu(split_rec
->r_clusters
) ==
1740 le64_to_cpu(orig_rec
->r_cpos
) + le32_to_cpu(orig_rec
->r_clusters
)))
1746 * We need one more rec if we split in the middle and the new rec have
1747 * some refcount in it.
1749 if (split_rec
->r_refcount
&&
1750 (split_rec
->r_cpos
!= orig_rec
->r_cpos
&&
1751 le64_to_cpu(split_rec
->r_cpos
) +
1752 le32_to_cpu(split_rec
->r_clusters
) !=
1753 le64_to_cpu(orig_rec
->r_cpos
) + le32_to_cpu(orig_rec
->r_clusters
)))
1756 /* If the leaf block don't have enough record, expand it. */
1757 if (le16_to_cpu(rf_list
->rl_used
) + recs_need
> rf_list
->rl_count
) {
1758 struct ocfs2_refcount_rec tmp_rec
;
1759 u64 cpos
= le64_to_cpu(orig_rec
->r_cpos
);
1760 len
= le32_to_cpu(orig_rec
->r_clusters
);
1761 ret
= ocfs2_expand_refcount_tree(handle
, ci
, ref_root_bh
,
1762 ref_leaf_bh
, meta_ac
);
1769 * We have to re-get it since now cpos may be moved to
1770 * another leaf block.
1772 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1773 cpos
, len
, &tmp_rec
, &index
,
1780 ref_leaf_bh
= new_bh
;
1781 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1782 rf_list
= &rb
->rf_records
;
1783 orig_rec
= &rf_list
->rl_recs
[index
];
1786 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1787 OCFS2_JOURNAL_ACCESS_WRITE
);
1794 * We have calculated out how many new records we need and store
1795 * in recs_need, so spare enough space first by moving the records
1796 * after "index" to the end.
1798 if (index
!= le16_to_cpu(rf_list
->rl_used
) - 1)
1799 memmove(&rf_list
->rl_recs
[index
+ 1 + recs_need
],
1800 &rf_list
->rl_recs
[index
+ 1],
1801 (le16_to_cpu(rf_list
->rl_used
) - index
- 1) *
1802 sizeof(struct ocfs2_refcount_rec
));
1804 len
= (le64_to_cpu(orig_rec
->r_cpos
) +
1805 le32_to_cpu(orig_rec
->r_clusters
)) -
1806 (le64_to_cpu(split_rec
->r_cpos
) +
1807 le32_to_cpu(split_rec
->r_clusters
));
1810 * If we have "len", the we will split in the tail and move it
1811 * to the end of the space we have just spared.
1814 tail_rec
= &rf_list
->rl_recs
[index
+ recs_need
];
1816 memcpy(tail_rec
, orig_rec
, sizeof(struct ocfs2_refcount_rec
));
1817 le64_add_cpu(&tail_rec
->r_cpos
,
1818 le32_to_cpu(tail_rec
->r_clusters
) - len
);
1819 tail_rec
->r_clusters
= le32_to_cpu(len
);
1823 * If the split pos isn't the same as the original one, we need to
1824 * split in the head.
1826 * Note: We have the chance that split_rec.r_refcount = 0,
1827 * recs_need = 0 and len > 0, which means we just cut the head from
1828 * the orig_rec and in that case we have done some modification in
1829 * orig_rec above, so the check for r_cpos is faked.
1831 if (split_rec
->r_cpos
!= orig_rec
->r_cpos
&& tail_rec
!= orig_rec
) {
1832 len
= le64_to_cpu(split_rec
->r_cpos
) -
1833 le64_to_cpu(orig_rec
->r_cpos
);
1834 orig_rec
->r_clusters
= cpu_to_le32(len
);
1838 le16_add_cpu(&rf_list
->rl_used
, recs_need
);
1840 if (split_rec
->r_refcount
) {
1841 rf_list
->rl_recs
[index
] = *split_rec
;
1842 mlog(0, "insert refcount record start %llu, len %u, count %u "
1843 "to leaf block %llu at index %d\n",
1844 (unsigned long long)le64_to_cpu(split_rec
->r_cpos
),
1845 le32_to_cpu(split_rec
->r_clusters
),
1846 le32_to_cpu(split_rec
->r_refcount
),
1847 (unsigned long long)ref_leaf_bh
->b_blocknr
, index
);
1849 ocfs2_refcount_rec_merge(rb
, index
);
1852 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1861 static int __ocfs2_increase_refcount(handle_t
*handle
,
1862 struct ocfs2_caching_info
*ci
,
1863 struct buffer_head
*ref_root_bh
,
1865 struct ocfs2_alloc_context
*meta_ac
,
1866 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1869 struct buffer_head
*ref_leaf_bh
= NULL
;
1870 struct ocfs2_refcount_rec rec
;
1871 unsigned int set_len
= 0;
1873 mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
1874 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1875 (unsigned long long)cpos
, len
);
1878 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1879 cpos
, len
, &rec
, &index
,
1886 set_len
= le32_to_cpu(rec
.r_clusters
);
1889 * Here we may meet with 3 situations:
1891 * 1. If we find an already existing record, and the length
1892 * is the same, cool, we just need to increase the r_refcount
1894 * 2. If we find a hole, just insert it with r_refcount = 1.
1895 * 3. If we are in the middle of one extent record, split
1898 if (rec
.r_refcount
&& le64_to_cpu(rec
.r_cpos
) == cpos
&&
1900 mlog(0, "increase refcount rec, start %llu, len %u, "
1901 "count %u\n", (unsigned long long)cpos
, set_len
,
1902 le32_to_cpu(rec
.r_refcount
));
1903 ret
= ocfs2_change_refcount_rec(handle
, ci
,
1904 ref_leaf_bh
, index
, 1);
1909 } else if (!rec
.r_refcount
) {
1910 rec
.r_refcount
= cpu_to_le32(1);
1912 mlog(0, "insert refcount rec, start %llu, len %u\n",
1913 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
1915 ret
= ocfs2_insert_refcount_rec(handle
, ci
, ref_root_bh
,
1917 &rec
, index
, meta_ac
);
1923 set_len
= min((u64
)(cpos
+ len
),
1924 le64_to_cpu(rec
.r_cpos
) + set_len
) - cpos
;
1925 rec
.r_cpos
= cpu_to_le64(cpos
);
1926 rec
.r_clusters
= cpu_to_le32(set_len
);
1927 le32_add_cpu(&rec
.r_refcount
, 1);
1929 mlog(0, "split refcount rec, start %llu, "
1930 "len %u, count %u\n",
1931 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
1932 set_len
, le32_to_cpu(rec
.r_refcount
));
1933 ret
= ocfs2_split_refcount_rec(handle
, ci
,
1934 ref_root_bh
, ref_leaf_bh
,
1945 brelse(ref_leaf_bh
);
1950 brelse(ref_leaf_bh
);
1954 static int ocfs2_remove_refcount_extent(handle_t
*handle
,
1955 struct ocfs2_caching_info
*ci
,
1956 struct buffer_head
*ref_root_bh
,
1957 struct buffer_head
*ref_leaf_bh
,
1958 struct ocfs2_alloc_context
*meta_ac
,
1959 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1962 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1963 struct ocfs2_refcount_block
*rb
=
1964 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1965 struct ocfs2_extent_tree et
;
1967 BUG_ON(rb
->rf_records
.rl_used
);
1969 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
1970 ret
= ocfs2_remove_extent(handle
, &et
, le32_to_cpu(rb
->rf_cpos
),
1971 1, meta_ac
, dealloc
);
1977 ocfs2_remove_from_cache(ci
, ref_leaf_bh
);
1980 * add the freed block to the dealloc so that it will be freed
1981 * when we run dealloc.
1983 ret
= ocfs2_cache_block_dealloc(dealloc
, EXTENT_ALLOC_SYSTEM_INODE
,
1984 le16_to_cpu(rb
->rf_suballoc_slot
),
1985 le64_to_cpu(rb
->rf_blkno
),
1986 le16_to_cpu(rb
->rf_suballoc_bit
));
1992 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
1993 OCFS2_JOURNAL_ACCESS_WRITE
);
1999 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
2001 le32_add_cpu(&rb
->rf_clusters
, -1);
2004 * check whether we need to restore the root refcount block if
2005 * there is no leaf extent block at atll.
2007 if (!rb
->rf_list
.l_next_free_rec
) {
2008 BUG_ON(rb
->rf_clusters
);
2010 mlog(0, "reset refcount tree root %llu to be a record block.\n",
2011 (unsigned long long)ref_root_bh
->b_blocknr
);
2016 memset(&rb
->rf_records
, 0, sb
->s_blocksize
-
2017 offsetof(struct ocfs2_refcount_block
, rf_records
));
2018 rb
->rf_records
.rl_count
=
2019 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb
));
2022 ocfs2_journal_dirty(handle
, ref_root_bh
);
2028 static int ocfs2_decrease_refcount_rec(handle_t
*handle
,
2029 struct ocfs2_caching_info
*ci
,
2030 struct buffer_head
*ref_root_bh
,
2031 struct buffer_head
*ref_leaf_bh
,
2032 int index
, u64 cpos
, unsigned int len
,
2033 struct ocfs2_alloc_context
*meta_ac
,
2034 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2037 struct ocfs2_refcount_block
*rb
=
2038 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
2039 struct ocfs2_refcount_rec
*rec
= &rb
->rf_records
.rl_recs
[index
];
2041 BUG_ON(cpos
< le64_to_cpu(rec
->r_cpos
));
2043 le64_to_cpu(rec
->r_cpos
) + le32_to_cpu(rec
->r_clusters
));
2045 if (cpos
== le64_to_cpu(rec
->r_cpos
) &&
2046 len
== le32_to_cpu(rec
->r_clusters
))
2047 ret
= ocfs2_change_refcount_rec(handle
, ci
,
2048 ref_leaf_bh
, index
, -1);
2050 struct ocfs2_refcount_rec split
= *rec
;
2051 split
.r_cpos
= cpu_to_le64(cpos
);
2052 split
.r_clusters
= cpu_to_le32(len
);
2054 le32_add_cpu(&split
.r_refcount
, -1);
2056 mlog(0, "split refcount rec, start %llu, "
2057 "len %u, count %u, original start %llu, len %u\n",
2058 (unsigned long long)le64_to_cpu(split
.r_cpos
),
2059 len
, le32_to_cpu(split
.r_refcount
),
2060 (unsigned long long)le64_to_cpu(rec
->r_cpos
),
2061 le32_to_cpu(rec
->r_clusters
));
2062 ret
= ocfs2_split_refcount_rec(handle
, ci
,
2063 ref_root_bh
, ref_leaf_bh
,
2073 /* Remove the leaf refcount block if it contains no refcount record. */
2074 if (!rb
->rf_records
.rl_used
&& ref_leaf_bh
!= ref_root_bh
) {
2075 ret
= ocfs2_remove_refcount_extent(handle
, ci
, ref_root_bh
,
2076 ref_leaf_bh
, meta_ac
,
2086 static int __ocfs2_decrease_refcount(handle_t
*handle
,
2087 struct ocfs2_caching_info
*ci
,
2088 struct buffer_head
*ref_root_bh
,
2090 struct ocfs2_alloc_context
*meta_ac
,
2091 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2094 int ret
= 0, index
= 0;
2095 struct ocfs2_refcount_rec rec
;
2096 unsigned int r_count
= 0, r_len
;
2097 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
2098 struct buffer_head
*ref_leaf_bh
= NULL
;
2100 mlog(0, "Tree owner %llu, decrease refcount start %llu, "
2101 "len %u, delete %u\n",
2102 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
2103 (unsigned long long)cpos
, len
, delete);
2106 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
2107 cpos
, len
, &rec
, &index
,
2114 r_count
= le32_to_cpu(rec
.r_refcount
);
2115 BUG_ON(r_count
== 0);
2117 BUG_ON(r_count
> 1);
2119 r_len
= min((u64
)(cpos
+ len
), le64_to_cpu(rec
.r_cpos
) +
2120 le32_to_cpu(rec
.r_clusters
)) - cpos
;
2122 ret
= ocfs2_decrease_refcount_rec(handle
, ci
, ref_root_bh
,
2131 if (le32_to_cpu(rec
.r_refcount
) == 1 && delete) {
2132 ret
= ocfs2_cache_cluster_dealloc(dealloc
,
2133 ocfs2_clusters_to_blocks(sb
, cpos
),
2143 brelse(ref_leaf_bh
);
2148 brelse(ref_leaf_bh
);
2152 /* Caller must hold refcount tree lock. */
2153 int ocfs2_decrease_refcount(struct inode
*inode
,
2154 handle_t
*handle
, u32 cpos
, u32 len
,
2155 struct ocfs2_alloc_context
*meta_ac
,
2156 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2161 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2162 struct buffer_head
*ref_root_bh
= NULL
;
2163 struct ocfs2_refcount_tree
*tree
;
2165 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
2167 ret
= ocfs2_get_refcount_block(inode
, &ref_blkno
);
2173 ret
= ocfs2_get_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_blkno
, &tree
);
2179 ret
= ocfs2_read_refcount_block(&tree
->rf_ci
, tree
->rf_blkno
,
2186 ret
= __ocfs2_decrease_refcount(handle
, &tree
->rf_ci
, ref_root_bh
,
2187 cpos
, len
, meta_ac
, dealloc
, delete);
2191 brelse(ref_root_bh
);
2196 * Mark the already-existing extent at cpos as refcounted for len clusters.
2197 * This adds the refcount extent flag.
2199 * If the existing extent is larger than the request, initiate a
2200 * split. An attempt will be made at merging with adjacent extents.
2202 * The caller is responsible for passing down meta_ac if we'll need it.
2204 static int ocfs2_mark_extent_refcounted(struct inode
*inode
,
2205 struct ocfs2_extent_tree
*et
,
2206 handle_t
*handle
, u32 cpos
,
2208 struct ocfs2_alloc_context
*meta_ac
,
2209 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2213 mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
2214 inode
->i_ino
, cpos
, len
, phys
);
2216 if (!ocfs2_refcount_tree(OCFS2_SB(inode
->i_sb
))) {
2217 ocfs2_error(inode
->i_sb
, "Inode %lu want to use refcount "
2218 "tree, but the feature bit is not set in the "
2219 "super block.", inode
->i_ino
);
2224 ret
= ocfs2_change_extent_flag(handle
, et
, cpos
,
2225 len
, phys
, meta_ac
, dealloc
,
2226 OCFS2_EXT_REFCOUNTED
, 0);
2235 * Given some contiguous physical clusters, calculate what we need
2236 * for modifying their refcount.
2238 static int ocfs2_calc_refcount_meta_credits(struct super_block
*sb
,
2239 struct ocfs2_caching_info
*ci
,
2240 struct buffer_head
*ref_root_bh
,
2246 int ret
= 0, index
, ref_blocks
= 0, recs_add
= 0;
2247 u64 cpos
= start_cpos
;
2248 struct ocfs2_refcount_block
*rb
;
2249 struct ocfs2_refcount_rec rec
;
2250 struct buffer_head
*ref_leaf_bh
= NULL
, *prev_bh
= NULL
;
2253 mlog(0, "start_cpos %llu, clusters %u\n",
2254 (unsigned long long)start_cpos
, clusters
);
2256 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
2257 cpos
, clusters
, &rec
,
2258 &index
, &ref_leaf_bh
);
2264 if (ref_leaf_bh
!= prev_bh
) {
2266 * Now we encounter a new leaf block, so calculate
2267 * whether we need to extend the old leaf.
2270 rb
= (struct ocfs2_refcount_block
*)
2273 if (le64_to_cpu(rb
->rf_records
.rl_used
) +
2275 le16_to_cpu(rb
->rf_records
.rl_count
))
2282 prev_bh
= ref_leaf_bh
;
2286 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
2288 mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
2289 "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
2290 recs_add
, (unsigned long long)cpos
, clusters
,
2291 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
2292 le32_to_cpu(rec
.r_clusters
),
2293 le32_to_cpu(rec
.r_refcount
), index
);
2295 len
= min((u64
)cpos
+ clusters
, le64_to_cpu(rec
.r_cpos
) +
2296 le32_to_cpu(rec
.r_clusters
)) - cpos
;
2298 * If the refcount rec already exist, cool. We just need
2299 * to check whether there is a split. Otherwise we just need
2300 * to increase the refcount.
2301 * If we will insert one, increases recs_add.
2303 * We record all the records which will be inserted to the
2304 * same refcount block, so that we can tell exactly whether
2305 * we need a new refcount block or not.
2307 if (rec
.r_refcount
) {
2308 /* Check whether we need a split at the beginning. */
2309 if (cpos
== start_cpos
&&
2310 cpos
!= le64_to_cpu(rec
.r_cpos
))
2313 /* Check whether we need a split in the end. */
2314 if (cpos
+ clusters
< le64_to_cpu(rec
.r_cpos
) +
2315 le32_to_cpu(rec
.r_clusters
))
2320 brelse(ref_leaf_bh
);
2327 rb
= (struct ocfs2_refcount_block
*)prev_bh
->b_data
;
2329 if (le64_to_cpu(rb
->rf_records
.rl_used
) + recs_add
>
2330 le16_to_cpu(rb
->rf_records
.rl_count
))
2339 mlog(0, "we need ref_blocks %d\n", ref_blocks
);
2340 *meta_add
+= ref_blocks
;
2341 *credits
+= ref_blocks
;
2344 * So we may need ref_blocks to insert into the tree.
2345 * That also means we need to change the b-tree and add that number
2346 * of records since we never merge them.
2347 * We need one more block for expansion since the new created leaf
2348 * block is also full and needs split.
2350 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
2351 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
) {
2352 struct ocfs2_extent_tree et
;
2354 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
2355 *meta_add
+= ocfs2_extend_meta_needed(et
.et_root_el
);
2356 *credits
+= ocfs2_calc_extend_credits(sb
,
2360 *credits
+= OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
2365 brelse(ref_leaf_bh
);
2371 * For refcount tree, we will decrease some contiguous clusters
2372 * refcount count, so just go through it to see how many blocks
2373 * we gonna touch and whether we need to create new blocks.
2375 * Normally the refcount blocks store these refcount should be
2376 * continguous also, so that we can get the number easily.
2377 * As for meta_ac, we will at most add split 2 refcount record and
2378 * 2 more refcount block, so just check it in a rough way.
2380 * Caller must hold refcount tree lock.
2382 int ocfs2_prepare_refcount_change_for_del(struct inode
*inode
,
2383 struct buffer_head
*di_bh
,
2387 struct ocfs2_alloc_context
**meta_ac
)
2389 int ret
, ref_blocks
= 0;
2390 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2391 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2392 struct buffer_head
*ref_root_bh
= NULL
;
2393 struct ocfs2_refcount_tree
*tree
;
2394 u64 start_cpos
= ocfs2_blocks_to_clusters(inode
->i_sb
, phys_blkno
);
2396 if (!ocfs2_refcount_tree(OCFS2_SB(inode
->i_sb
))) {
2397 ocfs2_error(inode
->i_sb
, "Inode %lu want to use refcount "
2398 "tree, but the feature bit is not set in the "
2399 "super block.", inode
->i_ino
);
2404 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
2406 ret
= ocfs2_get_refcount_tree(OCFS2_SB(inode
->i_sb
),
2407 le64_to_cpu(di
->i_refcount_loc
), &tree
);
2413 ret
= ocfs2_read_refcount_block(&tree
->rf_ci
,
2414 le64_to_cpu(di
->i_refcount_loc
),
2421 ret
= ocfs2_calc_refcount_meta_credits(inode
->i_sb
,
2424 start_cpos
, clusters
,
2425 &ref_blocks
, credits
);
2431 mlog(0, "reserve new metadata %d, credits = %d\n",
2432 ref_blocks
, *credits
);
2435 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
2436 ref_blocks
, meta_ac
);
2442 brelse(ref_root_bh
);
2446 #define MAX_CONTIG_BYTES 1048576
2448 static inline unsigned int ocfs2_cow_contig_clusters(struct super_block
*sb
)
2450 return ocfs2_clusters_for_bytes(sb
, MAX_CONTIG_BYTES
);
2453 static inline unsigned int ocfs2_cow_contig_mask(struct super_block
*sb
)
2455 return ~(ocfs2_cow_contig_clusters(sb
) - 1);
2459 * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
2460 * find an offset (start + (n * contig_clusters)) that is closest to cpos
2461 * while still being less than or equal to it.
2463 * The goal is to break the extent at a multiple of contig_clusters.
2465 static inline unsigned int ocfs2_cow_align_start(struct super_block
*sb
,
2469 BUG_ON(start
> cpos
);
2471 return start
+ ((cpos
- start
) & ocfs2_cow_contig_mask(sb
));
2475 * Given a cluster count of len, pad it out so that it is a multiple
2476 * of contig_clusters.
2478 static inline unsigned int ocfs2_cow_align_length(struct super_block
*sb
,
2481 unsigned int padded
=
2482 (len
+ (ocfs2_cow_contig_clusters(sb
) - 1)) &
2483 ocfs2_cow_contig_mask(sb
);
2493 * Calculate out the start and number of virtual clusters we need to to CoW.
2495 * cpos is vitual start cluster position we want to do CoW in a
2496 * file and write_len is the cluster length.
2497 * max_cpos is the place where we want to stop CoW intentionally.
2499 * Normal we will start CoW from the beginning of extent record cotaining cpos.
2500 * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
2501 * get good I/O from the resulting extent tree.
2503 static int ocfs2_refcount_cal_cow_clusters(struct inode
*inode
,
2504 struct ocfs2_extent_list
*el
,
2512 int tree_height
= le16_to_cpu(el
->l_tree_depth
), i
;
2513 struct buffer_head
*eb_bh
= NULL
;
2514 struct ocfs2_extent_block
*eb
= NULL
;
2515 struct ocfs2_extent_rec
*rec
;
2516 unsigned int want_clusters
, rec_end
= 0;
2517 int contig_clusters
= ocfs2_cow_contig_clusters(inode
->i_sb
);
2520 BUG_ON(cpos
+ write_len
> max_cpos
);
2522 if (tree_height
> 0) {
2523 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, cpos
, &eb_bh
);
2529 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2532 if (el
->l_tree_depth
) {
2533 ocfs2_error(inode
->i_sb
,
2534 "Inode %lu has non zero tree depth in "
2535 "leaf block %llu\n", inode
->i_ino
,
2536 (unsigned long long)eb_bh
->b_blocknr
);
2543 for (i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
2544 rec
= &el
->l_recs
[i
];
2546 if (ocfs2_is_empty_extent(rec
)) {
2547 mlog_bug_on_msg(i
!= 0, "Inode %lu has empty record in "
2548 "index %d\n", inode
->i_ino
, i
);
2552 if (le32_to_cpu(rec
->e_cpos
) +
2553 le16_to_cpu(rec
->e_leaf_clusters
) <= cpos
)
2556 if (*cow_len
== 0) {
2558 * We should find a refcounted record in the
2561 BUG_ON(!(rec
->e_flags
& OCFS2_EXT_REFCOUNTED
));
2562 *cow_start
= le32_to_cpu(rec
->e_cpos
);
2566 * If we encounter a hole, a non-refcounted record or
2567 * pass the max_cpos, stop the search.
2569 if ((!(rec
->e_flags
& OCFS2_EXT_REFCOUNTED
)) ||
2570 (*cow_len
&& rec_end
!= le32_to_cpu(rec
->e_cpos
)) ||
2571 (max_cpos
<= le32_to_cpu(rec
->e_cpos
)))
2574 leaf_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
2575 rec_end
= le32_to_cpu(rec
->e_cpos
) + leaf_clusters
;
2576 if (rec_end
> max_cpos
) {
2578 leaf_clusters
= rec_end
- le32_to_cpu(rec
->e_cpos
);
2582 * How many clusters do we actually need from
2583 * this extent? First we see how many we actually
2584 * need to complete the write. If that's smaller
2585 * than contig_clusters, we try for contig_clusters.
2588 want_clusters
= write_len
;
2590 want_clusters
= (cpos
+ write_len
) -
2591 (*cow_start
+ *cow_len
);
2592 if (want_clusters
< contig_clusters
)
2593 want_clusters
= contig_clusters
;
2596 * If the write does not cover the whole extent, we
2597 * need to calculate how we're going to split the extent.
2598 * We try to do it on contig_clusters boundaries.
2600 * Any extent smaller than contig_clusters will be
2601 * CoWed in its entirety.
2603 if (leaf_clusters
<= contig_clusters
)
2604 *cow_len
+= leaf_clusters
;
2605 else if (*cow_len
|| (*cow_start
== cpos
)) {
2607 * This extent needs to be CoW'd from its
2608 * beginning, so all we have to do is compute
2609 * how many clusters to grab. We align
2610 * want_clusters to the edge of contig_clusters
2611 * to get better I/O.
2613 want_clusters
= ocfs2_cow_align_length(inode
->i_sb
,
2616 if (leaf_clusters
< want_clusters
)
2617 *cow_len
+= leaf_clusters
;
2619 *cow_len
+= want_clusters
;
2620 } else if ((*cow_start
+ contig_clusters
) >=
2621 (cpos
+ write_len
)) {
2623 * Breaking off contig_clusters at the front
2624 * of the extent will cover our write. That's
2627 *cow_len
= contig_clusters
;
2628 } else if ((rec_end
- cpos
) <= contig_clusters
) {
2630 * Breaking off contig_clusters at the tail of
2631 * this extent will cover cpos.
2633 *cow_start
= rec_end
- contig_clusters
;
2634 *cow_len
= contig_clusters
;
2635 } else if ((rec_end
- cpos
) <= want_clusters
) {
2637 * While we can't fit the entire write in this
2638 * extent, we know that the write goes from cpos
2639 * to the end of the extent. Break that off.
2640 * We try to break it at some multiple of
2641 * contig_clusters from the front of the extent.
2642 * Failing that (ie, cpos is within
2643 * contig_clusters of the front), we'll CoW the
2646 *cow_start
= ocfs2_cow_align_start(inode
->i_sb
,
2648 *cow_len
= rec_end
- *cow_start
;
2651 * Ok, the entire write lives in the middle of
2652 * this extent. Let's try to slice the extent up
2653 * nicely. Optimally, our CoW region starts at
2654 * m*contig_clusters from the beginning of the
2655 * extent and goes for n*contig_clusters,
2656 * covering the entire write.
2658 *cow_start
= ocfs2_cow_align_start(inode
->i_sb
,
2661 want_clusters
= (cpos
+ write_len
) - *cow_start
;
2662 want_clusters
= ocfs2_cow_align_length(inode
->i_sb
,
2664 if (*cow_start
+ want_clusters
<= rec_end
)
2665 *cow_len
= want_clusters
;
2667 *cow_len
= rec_end
- *cow_start
;
2670 /* Have we covered our entire write yet? */
2671 if ((*cow_start
+ *cow_len
) >= (cpos
+ write_len
))
2675 * If we reach the end of the extent block and don't get enough
2676 * clusters, continue with the next extent block if possible.
2678 if (i
+ 1 == le16_to_cpu(el
->l_next_free_rec
) &&
2679 eb
&& eb
->h_next_leaf_blk
) {
2683 ret
= ocfs2_read_extent_block(INODE_CACHE(inode
),
2684 le64_to_cpu(eb
->h_next_leaf_blk
),
2691 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2703 * Prepare meta_ac, data_ac and calculate credits when we want to add some
2704 * num_clusters in data_tree "et" and change the refcount for the old
2705 * clusters(starting form p_cluster) in the refcount tree.
2708 * 1. since we may split the old tree, so we at most will need num_clusters + 2
2709 * more new leaf records.
2710 * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
2711 * just give data_ac = NULL.
2713 static int ocfs2_lock_refcount_allocators(struct super_block
*sb
,
2714 u32 p_cluster
, u32 num_clusters
,
2715 struct ocfs2_extent_tree
*et
,
2716 struct ocfs2_caching_info
*ref_ci
,
2717 struct buffer_head
*ref_root_bh
,
2718 struct ocfs2_alloc_context
**meta_ac
,
2719 struct ocfs2_alloc_context
**data_ac
,
2722 int ret
= 0, meta_add
= 0;
2723 int num_free_extents
= ocfs2_num_free_extents(OCFS2_SB(sb
), et
);
2725 if (num_free_extents
< 0) {
2726 ret
= num_free_extents
;
2731 if (num_free_extents
< num_clusters
+ 2)
2733 ocfs2_extend_meta_needed(et
->et_root_el
);
2735 *credits
+= ocfs2_calc_extend_credits(sb
, et
->et_root_el
,
2738 ret
= ocfs2_calc_refcount_meta_credits(sb
, ref_ci
, ref_root_bh
,
2739 p_cluster
, num_clusters
,
2740 &meta_add
, credits
);
2746 mlog(0, "reserve new metadata %d, clusters %u, credits = %d\n",
2747 meta_add
, num_clusters
, *credits
);
2748 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb
), meta_add
,
2756 ret
= ocfs2_reserve_clusters(OCFS2_SB(sb
), num_clusters
,
2765 ocfs2_free_alloc_context(*meta_ac
);
2773 static int ocfs2_clear_cow_buffer(handle_t
*handle
, struct buffer_head
*bh
)
2775 BUG_ON(buffer_dirty(bh
));
2777 clear_buffer_mapped(bh
);
2782 static int ocfs2_duplicate_clusters_by_page(handle_t
*handle
,
2783 struct ocfs2_cow_context
*context
,
2784 u32 cpos
, u32 old_cluster
,
2785 u32 new_cluster
, u32 new_len
)
2787 int ret
= 0, partial
;
2788 struct ocfs2_caching_info
*ci
= context
->data_et
.et_ci
;
2789 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
2790 u64 new_block
= ocfs2_clusters_to_blocks(sb
, new_cluster
);
2793 unsigned int from
, to
;
2794 loff_t offset
, end
, map_end
;
2795 struct address_space
*mapping
= context
->inode
->i_mapping
;
2797 mlog(0, "old_cluster %u, new %u, len %u at offset %u\n", old_cluster
,
2798 new_cluster
, new_len
, cpos
);
2800 offset
= ((loff_t
)cpos
) << OCFS2_SB(sb
)->s_clustersize_bits
;
2801 end
= offset
+ (new_len
<< OCFS2_SB(sb
)->s_clustersize_bits
);
2803 while (offset
< end
) {
2804 page_index
= offset
>> PAGE_CACHE_SHIFT
;
2805 map_end
= (page_index
+ 1) << PAGE_CACHE_SHIFT
;
2809 /* from, to is the offset within the page. */
2810 from
= offset
& (PAGE_CACHE_SIZE
- 1);
2811 to
= PAGE_CACHE_SIZE
;
2812 if (map_end
& (PAGE_CACHE_SIZE
- 1))
2813 to
= map_end
& (PAGE_CACHE_SIZE
- 1);
2815 page
= grab_cache_page(mapping
, page_index
);
2817 /* This page can't be dirtied before we CoW it out. */
2818 BUG_ON(PageDirty(page
));
2820 if (!PageUptodate(page
)) {
2821 ret
= block_read_full_page(page
, ocfs2_get_block
);
2829 if (page_has_buffers(page
)) {
2830 ret
= walk_page_buffers(handle
, page_buffers(page
),
2832 ocfs2_clear_cow_buffer
);
2839 ocfs2_map_and_dirty_page(context
->inode
,
2841 page
, 0, &new_block
);
2842 mark_page_accessed(page
);
2845 page_cache_release(page
);
2855 static int ocfs2_duplicate_clusters_by_jbd(handle_t
*handle
,
2856 struct ocfs2_cow_context
*context
,
2857 u32 cpos
, u32 old_cluster
,
2858 u32 new_cluster
, u32 new_len
)
2861 struct super_block
*sb
= context
->inode
->i_sb
;
2862 struct ocfs2_caching_info
*ci
= context
->data_et
.et_ci
;
2863 int i
, blocks
= ocfs2_clusters_to_blocks(sb
, new_len
);
2864 u64 old_block
= ocfs2_clusters_to_blocks(sb
, old_cluster
);
2865 u64 new_block
= ocfs2_clusters_to_blocks(sb
, new_cluster
);
2866 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
2867 struct buffer_head
*old_bh
= NULL
;
2868 struct buffer_head
*new_bh
= NULL
;
2870 mlog(0, "old_cluster %u, new %u, len %u\n", old_cluster
,
2871 new_cluster
, new_len
);
2873 for (i
= 0; i
< blocks
; i
++, old_block
++, new_block
++) {
2874 new_bh
= sb_getblk(osb
->sb
, new_block
);
2875 if (new_bh
== NULL
) {
2881 ocfs2_set_new_buffer_uptodate(ci
, new_bh
);
2883 ret
= ocfs2_read_block(ci
, old_block
, &old_bh
, NULL
);
2889 ret
= ocfs2_journal_access(handle
, ci
, new_bh
,
2890 OCFS2_JOURNAL_ACCESS_CREATE
);
2896 memcpy(new_bh
->b_data
, old_bh
->b_data
, sb
->s_blocksize
);
2897 ret
= ocfs2_journal_dirty(handle
, new_bh
);
2914 static int ocfs2_clear_ext_refcount(handle_t
*handle
,
2915 struct ocfs2_extent_tree
*et
,
2916 u32 cpos
, u32 p_cluster
, u32 len
,
2917 unsigned int ext_flags
,
2918 struct ocfs2_alloc_context
*meta_ac
,
2919 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2922 struct ocfs2_extent_rec replace_rec
;
2923 struct ocfs2_path
*path
= NULL
;
2924 struct ocfs2_extent_list
*el
;
2925 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
2926 u64 ino
= ocfs2_metadata_cache_owner(et
->et_ci
);
2928 mlog(0, "inode %llu cpos %u, len %u, p_cluster %u, ext_flags %u\n",
2929 (unsigned long long)ino
, cpos
, len
, p_cluster
, ext_flags
);
2931 memset(&replace_rec
, 0, sizeof(replace_rec
));
2932 replace_rec
.e_cpos
= cpu_to_le32(cpos
);
2933 replace_rec
.e_leaf_clusters
= cpu_to_le16(len
);
2934 replace_rec
.e_blkno
= cpu_to_le64(ocfs2_clusters_to_blocks(sb
,
2936 replace_rec
.e_flags
= ext_flags
;
2937 replace_rec
.e_flags
&= ~OCFS2_EXT_REFCOUNTED
;
2939 path
= ocfs2_new_path_from_et(et
);
2946 ret
= ocfs2_find_path(et
->et_ci
, path
, cpos
);
2952 el
= path_leaf_el(path
);
2954 index
= ocfs2_search_extent_list(el
, cpos
);
2955 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
2957 "Inode %llu has an extent at cpos %u which can no "
2958 "longer be found.\n",
2959 (unsigned long long)ino
, cpos
);
2964 ret
= ocfs2_split_extent(handle
, et
, path
, index
,
2965 &replace_rec
, meta_ac
, dealloc
);
2970 ocfs2_free_path(path
);
2974 static int ocfs2_replace_clusters(handle_t
*handle
,
2975 struct ocfs2_cow_context
*context
,
2978 unsigned int ext_flags
)
2981 struct ocfs2_caching_info
*ci
= context
->data_et
.et_ci
;
2982 u64 ino
= ocfs2_metadata_cache_owner(ci
);
2984 mlog(0, "inode %llu, cpos %u, old %u, new %u, len %u, ext_flags %u\n",
2985 (unsigned long long)ino
, cpos
, old
, new, len
, ext_flags
);
2987 /*If the old clusters is unwritten, no need to duplicate. */
2988 if (!(ext_flags
& OCFS2_EXT_UNWRITTEN
)) {
2989 ret
= context
->cow_duplicate_clusters(handle
, context
, cpos
,
2997 ret
= ocfs2_clear_ext_refcount(handle
, &context
->data_et
,
2998 cpos
, new, len
, ext_flags
,
2999 context
->meta_ac
, &context
->dealloc
);
3006 static int ocfs2_cow_sync_writeback(struct super_block
*sb
,
3007 struct ocfs2_cow_context
*context
,
3008 u32 cpos
, u32 num_clusters
)
3011 loff_t offset
, end
, map_end
;
3015 if (ocfs2_should_order_data(context
->inode
))
3018 offset
= ((loff_t
)cpos
) << OCFS2_SB(sb
)->s_clustersize_bits
;
3019 end
= offset
+ (num_clusters
<< OCFS2_SB(sb
)->s_clustersize_bits
);
3021 ret
= filemap_fdatawrite_range(context
->inode
->i_mapping
,
3028 while (offset
< end
) {
3029 page_index
= offset
>> PAGE_CACHE_SHIFT
;
3030 map_end
= (page_index
+ 1) << PAGE_CACHE_SHIFT
;
3034 page
= grab_cache_page(context
->inode
->i_mapping
, page_index
);
3037 wait_on_page_writeback(page
);
3038 if (PageError(page
)) {
3042 mark_page_accessed(page
);
3045 page_cache_release(page
);
3055 static int ocfs2_di_get_clusters(struct ocfs2_cow_context
*context
,
3056 u32 v_cluster
, u32
*p_cluster
,
3058 unsigned int *extent_flags
)
3060 return ocfs2_get_clusters(context
->inode
, v_cluster
, p_cluster
,
3061 num_clusters
, extent_flags
);
3064 static int ocfs2_make_clusters_writable(struct super_block
*sb
,
3065 struct ocfs2_cow_context
*context
,
3066 u32 cpos
, u32 p_cluster
,
3067 u32 num_clusters
, unsigned int e_flags
)
3069 int ret
, delete, index
, credits
= 0;
3070 u32 new_bit
, new_len
;
3071 unsigned int set_len
;
3072 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
3074 struct buffer_head
*ref_leaf_bh
= NULL
;
3075 struct ocfs2_caching_info
*ref_ci
= &context
->ref_tree
->rf_ci
;
3076 struct ocfs2_refcount_rec rec
;
3078 mlog(0, "cpos %u, p_cluster %u, num_clusters %u, e_flags %u\n",
3079 cpos
, p_cluster
, num_clusters
, e_flags
);
3081 ret
= ocfs2_lock_refcount_allocators(sb
, p_cluster
, num_clusters
,
3084 context
->ref_root_bh
,
3086 &context
->data_ac
, &credits
);
3092 if (context
->post_refcount
)
3093 credits
+= context
->post_refcount
->credits
;
3095 credits
+= context
->extra_credits
;
3096 handle
= ocfs2_start_trans(osb
, credits
);
3097 if (IS_ERR(handle
)) {
3098 ret
= PTR_ERR(handle
);
3103 while (num_clusters
) {
3104 ret
= ocfs2_get_refcount_rec(ref_ci
, context
->ref_root_bh
,
3105 p_cluster
, num_clusters
,
3106 &rec
, &index
, &ref_leaf_bh
);
3112 BUG_ON(!rec
.r_refcount
);
3113 set_len
= min((u64
)p_cluster
+ num_clusters
,
3114 le64_to_cpu(rec
.r_cpos
) +
3115 le32_to_cpu(rec
.r_clusters
)) - p_cluster
;
3118 * There are many different situation here.
3119 * 1. If refcount == 1, remove the flag and don't COW.
3120 * 2. If refcount > 1, allocate clusters.
3121 * Here we may not allocate r_len once at a time, so continue
3122 * until we reach num_clusters.
3124 if (le32_to_cpu(rec
.r_refcount
) == 1) {
3126 ret
= ocfs2_clear_ext_refcount(handle
,
3139 ret
= __ocfs2_claim_clusters(osb
, handle
,
3142 &new_bit
, &new_len
);
3148 ret
= ocfs2_replace_clusters(handle
, context
,
3149 cpos
, p_cluster
, new_bit
,
3158 ret
= __ocfs2_decrease_refcount(handle
, ref_ci
,
3159 context
->ref_root_bh
,
3162 &context
->dealloc
, delete);
3169 p_cluster
+= set_len
;
3170 num_clusters
-= set_len
;
3171 brelse(ref_leaf_bh
);
3175 /* handle any post_cow action. */
3176 if (context
->post_refcount
&& context
->post_refcount
->func
) {
3177 ret
= context
->post_refcount
->func(context
->inode
, handle
,
3178 context
->post_refcount
->para
);
3186 * Here we should write the new page out first if we are
3187 * in write-back mode.
3189 if (context
->get_clusters
== ocfs2_di_get_clusters
) {
3190 ret
= ocfs2_cow_sync_writeback(sb
, context
, cpos
, num_clusters
);
3196 ocfs2_commit_trans(osb
, handle
);
3199 if (context
->data_ac
) {
3200 ocfs2_free_alloc_context(context
->data_ac
);
3201 context
->data_ac
= NULL
;
3203 if (context
->meta_ac
) {
3204 ocfs2_free_alloc_context(context
->meta_ac
);
3205 context
->meta_ac
= NULL
;
3207 brelse(ref_leaf_bh
);
3212 static int ocfs2_replace_cow(struct ocfs2_cow_context
*context
)
3215 struct inode
*inode
= context
->inode
;
3216 u32 cow_start
= context
->cow_start
, cow_len
= context
->cow_len
;
3217 u32 p_cluster
, num_clusters
;
3218 unsigned int ext_flags
;
3219 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3221 if (!ocfs2_refcount_tree(OCFS2_SB(inode
->i_sb
))) {
3222 ocfs2_error(inode
->i_sb
, "Inode %lu want to use refcount "
3223 "tree, but the feature bit is not set in the "
3224 "super block.", inode
->i_ino
);
3228 ocfs2_init_dealloc_ctxt(&context
->dealloc
);
3231 ret
= context
->get_clusters(context
, cow_start
, &p_cluster
,
3232 &num_clusters
, &ext_flags
);
3238 BUG_ON(!(ext_flags
& OCFS2_EXT_REFCOUNTED
));
3240 if (cow_len
< num_clusters
)
3241 num_clusters
= cow_len
;
3243 ret
= ocfs2_make_clusters_writable(inode
->i_sb
, context
,
3244 cow_start
, p_cluster
,
3245 num_clusters
, ext_flags
);
3251 cow_len
-= num_clusters
;
3252 cow_start
+= num_clusters
;
3255 if (ocfs2_dealloc_has_cluster(&context
->dealloc
)) {
3256 ocfs2_schedule_truncate_log_flush(osb
, 1);
3257 ocfs2_run_deallocs(osb
, &context
->dealloc
);
3264 * Starting at cpos, try to CoW write_len clusters. Don't CoW
3265 * past max_cpos. This will stop when it runs into a hole or an
3266 * unrefcounted extent.
3268 static int ocfs2_refcount_cow_hunk(struct inode
*inode
,
3269 struct buffer_head
*di_bh
,
3270 u32 cpos
, u32 write_len
, u32 max_cpos
)
3273 u32 cow_start
= 0, cow_len
= 0;
3274 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
3275 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3276 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3277 struct buffer_head
*ref_root_bh
= NULL
;
3278 struct ocfs2_refcount_tree
*ref_tree
;
3279 struct ocfs2_cow_context
*context
= NULL
;
3281 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
3283 ret
= ocfs2_refcount_cal_cow_clusters(inode
, &di
->id2
.i_list
,
3284 cpos
, write_len
, max_cpos
,
3285 &cow_start
, &cow_len
);
3291 mlog(0, "CoW inode %lu, cpos %u, write_len %u, cow_start %u, "
3292 "cow_len %u\n", inode
->i_ino
,
3293 cpos
, write_len
, cow_start
, cow_len
);
3295 BUG_ON(cow_len
== 0);
3297 context
= kzalloc(sizeof(struct ocfs2_cow_context
), GFP_NOFS
);
3304 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
3305 1, &ref_tree
, &ref_root_bh
);
3311 context
->inode
= inode
;
3312 context
->cow_start
= cow_start
;
3313 context
->cow_len
= cow_len
;
3314 context
->ref_tree
= ref_tree
;
3315 context
->ref_root_bh
= ref_root_bh
;
3316 context
->cow_duplicate_clusters
= ocfs2_duplicate_clusters_by_page
;
3317 context
->get_clusters
= ocfs2_di_get_clusters
;
3319 ocfs2_init_dinode_extent_tree(&context
->data_et
,
3320 INODE_CACHE(inode
), di_bh
);
3322 ret
= ocfs2_replace_cow(context
);
3327 * truncate the extent map here since no matter whether we meet with
3328 * any error during the action, we shouldn't trust cached extent map
3331 ocfs2_extent_map_trunc(inode
, cow_start
);
3333 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3334 brelse(ref_root_bh
);
3341 * CoW any and all clusters between cpos and cpos+write_len.
3342 * Don't CoW past max_cpos. If this returns successfully, all
3343 * clusters between cpos and cpos+write_len are safe to modify.
3345 int ocfs2_refcount_cow(struct inode
*inode
,
3346 struct buffer_head
*di_bh
,
3347 u32 cpos
, u32 write_len
, u32 max_cpos
)
3350 u32 p_cluster
, num_clusters
;
3351 unsigned int ext_flags
;
3354 ret
= ocfs2_get_clusters(inode
, cpos
, &p_cluster
,
3355 &num_clusters
, &ext_flags
);
3361 if (write_len
< num_clusters
)
3362 num_clusters
= write_len
;
3364 if (ext_flags
& OCFS2_EXT_REFCOUNTED
) {
3365 ret
= ocfs2_refcount_cow_hunk(inode
, di_bh
, cpos
,
3366 num_clusters
, max_cpos
);
3373 write_len
-= num_clusters
;
3374 cpos
+= num_clusters
;
3380 static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context
*context
,
3381 u32 v_cluster
, u32
*p_cluster
,
3383 unsigned int *extent_flags
)
3385 struct inode
*inode
= context
->inode
;
3386 struct ocfs2_xattr_value_root
*xv
= context
->cow_object
;
3388 return ocfs2_xattr_get_clusters(inode
, v_cluster
, p_cluster
,
3389 num_clusters
, &xv
->xr_list
,
3394 * Given a xattr value root, calculate the most meta/credits we need for
3395 * refcount tree change if we truncate it to 0.
3397 int ocfs2_refcounted_xattr_delete_need(struct inode
*inode
,
3398 struct ocfs2_caching_info
*ref_ci
,
3399 struct buffer_head
*ref_root_bh
,
3400 struct ocfs2_xattr_value_root
*xv
,
3401 int *meta_add
, int *credits
)
3403 int ret
= 0, index
, ref_blocks
= 0;
3404 u32 p_cluster
, num_clusters
;
3405 u32 cpos
= 0, clusters
= le32_to_cpu(xv
->xr_clusters
);
3406 struct ocfs2_refcount_block
*rb
;
3407 struct ocfs2_refcount_rec rec
;
3408 struct buffer_head
*ref_leaf_bh
= NULL
;
3410 while (cpos
< clusters
) {
3411 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
3412 &num_clusters
, &xv
->xr_list
,
3419 cpos
+= num_clusters
;
3421 while (num_clusters
) {
3422 ret
= ocfs2_get_refcount_rec(ref_ci
, ref_root_bh
,
3423 p_cluster
, num_clusters
,
3431 BUG_ON(!rec
.r_refcount
);
3433 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
3436 * We really don't know whether the other clusters is in
3437 * this refcount block or not, so just take the worst
3438 * case that all the clusters are in this block and each
3439 * one will split a refcount rec, so totally we need
3440 * clusters * 2 new refcount rec.
3442 if (le64_to_cpu(rb
->rf_records
.rl_used
) + clusters
* 2 >
3443 le16_to_cpu(rb
->rf_records
.rl_count
))
3447 brelse(ref_leaf_bh
);
3450 if (num_clusters
<= le32_to_cpu(rec
.r_clusters
))
3453 num_clusters
-= le32_to_cpu(rec
.r_clusters
);
3454 p_cluster
+= num_clusters
;
3458 *meta_add
+= ref_blocks
;
3462 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
3463 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
3464 *credits
+= OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
3466 struct ocfs2_extent_tree et
;
3468 ocfs2_init_refcount_extent_tree(&et
, ref_ci
, ref_root_bh
);
3469 *credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3475 brelse(ref_leaf_bh
);
3482 int ocfs2_refcount_cow_xattr(struct inode
*inode
,
3483 struct ocfs2_dinode
*di
,
3484 struct ocfs2_xattr_value_buf
*vb
,
3485 struct ocfs2_refcount_tree
*ref_tree
,
3486 struct buffer_head
*ref_root_bh
,
3487 u32 cpos
, u32 write_len
,
3488 struct ocfs2_post_refcount
*post
)
3491 struct ocfs2_xattr_value_root
*xv
= vb
->vb_xv
;
3492 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
3493 struct ocfs2_cow_context
*context
= NULL
;
3494 u32 cow_start
, cow_len
;
3496 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
3498 ret
= ocfs2_refcount_cal_cow_clusters(inode
, &xv
->xr_list
,
3499 cpos
, write_len
, UINT_MAX
,
3500 &cow_start
, &cow_len
);
3506 BUG_ON(cow_len
== 0);
3508 context
= kzalloc(sizeof(struct ocfs2_cow_context
), GFP_NOFS
);
3515 context
->inode
= inode
;
3516 context
->cow_start
= cow_start
;
3517 context
->cow_len
= cow_len
;
3518 context
->ref_tree
= ref_tree
;
3519 context
->ref_root_bh
= ref_root_bh
;;
3520 context
->cow_object
= xv
;
3522 context
->cow_duplicate_clusters
= ocfs2_duplicate_clusters_by_jbd
;
3523 /* We need the extra credits for duplicate_clusters by jbd. */
3524 context
->extra_credits
=
3525 ocfs2_clusters_to_blocks(inode
->i_sb
, 1) * cow_len
;
3526 context
->get_clusters
= ocfs2_xattr_value_get_clusters
;
3527 context
->post_refcount
= post
;
3529 ocfs2_init_xattr_value_extent_tree(&context
->data_et
,
3530 INODE_CACHE(inode
), vb
);
3532 ret
= ocfs2_replace_cow(context
);
3542 * Insert a new extent into refcount tree and mark a extent rec
3543 * as refcounted in the dinode tree.
3545 int ocfs2_add_refcount_flag(struct inode
*inode
,
3546 struct ocfs2_extent_tree
*data_et
,
3547 struct ocfs2_caching_info
*ref_ci
,
3548 struct buffer_head
*ref_root_bh
,
3549 u32 cpos
, u32 p_cluster
, u32 num_clusters
,
3550 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3554 int credits
= 1, ref_blocks
= 0;
3555 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3556 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3558 ret
= ocfs2_calc_refcount_meta_credits(inode
->i_sb
,
3559 ref_ci
, ref_root_bh
,
3560 p_cluster
, num_clusters
,
3561 &ref_blocks
, &credits
);
3567 mlog(0, "reserve new metadata %d, credits = %d\n",
3568 ref_blocks
, credits
);
3571 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
3572 ref_blocks
, &meta_ac
);
3579 handle
= ocfs2_start_trans(osb
, credits
);
3580 if (IS_ERR(handle
)) {
3581 ret
= PTR_ERR(handle
);
3586 ret
= ocfs2_mark_extent_refcounted(inode
, data_et
, handle
,
3587 cpos
, num_clusters
, p_cluster
,
3594 ret
= __ocfs2_increase_refcount(handle
, ref_ci
, ref_root_bh
,
3595 p_cluster
, num_clusters
,
3601 ocfs2_commit_trans(osb
, handle
);
3604 ocfs2_free_alloc_context(meta_ac
);
3608 static int ocfs2_change_ctime(struct inode
*inode
,
3609 struct buffer_head
*di_bh
)
3613 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3615 handle
= ocfs2_start_trans(OCFS2_SB(inode
->i_sb
),
3616 OCFS2_INODE_UPDATE_CREDITS
);
3617 if (IS_ERR(handle
)) {
3618 ret
= PTR_ERR(handle
);
3623 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
3624 OCFS2_JOURNAL_ACCESS_WRITE
);
3630 inode
->i_ctime
= CURRENT_TIME
;
3631 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
3632 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
3634 ocfs2_journal_dirty(handle
, di_bh
);
3637 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
3642 static int ocfs2_attach_refcount_tree(struct inode
*inode
,
3643 struct buffer_head
*di_bh
)
3645 int ret
, data_changed
= 0;
3646 struct buffer_head
*ref_root_bh
= NULL
;
3647 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
3648 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3649 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3650 struct ocfs2_refcount_tree
*ref_tree
;
3651 unsigned int ext_flags
;
3653 u32 cpos
, num_clusters
, clusters
, p_cluster
;
3654 struct ocfs2_cached_dealloc_ctxt dealloc
;
3655 struct ocfs2_extent_tree di_et
;
3657 ocfs2_init_dealloc_ctxt(&dealloc
);
3659 if (!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
)) {
3660 ret
= ocfs2_create_refcount_tree(inode
, di_bh
);
3667 BUG_ON(!di
->i_refcount_loc
);
3668 ret
= ocfs2_lock_refcount_tree(osb
,
3669 le64_to_cpu(di
->i_refcount_loc
), 1,
3670 &ref_tree
, &ref_root_bh
);
3676 ocfs2_init_dinode_extent_tree(&di_et
, INODE_CACHE(inode
), di_bh
);
3678 size
= i_size_read(inode
);
3679 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, size
);
3682 while (cpos
< clusters
) {
3683 ret
= ocfs2_get_clusters(inode
, cpos
, &p_cluster
,
3684 &num_clusters
, &ext_flags
);
3686 if (p_cluster
&& !(ext_flags
& OCFS2_EXT_REFCOUNTED
)) {
3687 ret
= ocfs2_add_refcount_flag(inode
, &di_et
,
3690 p_cluster
, num_clusters
,
3699 cpos
+= num_clusters
;
3703 ret
= ocfs2_change_ctime(inode
, di_bh
);
3709 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3710 brelse(ref_root_bh
);
3712 if (!ret
&& ocfs2_dealloc_has_cluster(&dealloc
)) {
3713 ocfs2_schedule_truncate_log_flush(osb
, 1);
3714 ocfs2_run_deallocs(osb
, &dealloc
);
3718 * Empty the extent map so that we may get the right extent
3719 * record from the disk.
3721 ocfs2_extent_map_trunc(inode
, 0);
3726 static int ocfs2_add_refcounted_extent(struct inode
*inode
,
3727 struct ocfs2_extent_tree
*et
,
3728 struct ocfs2_caching_info
*ref_ci
,
3729 struct buffer_head
*ref_root_bh
,
3730 u32 cpos
, u32 p_cluster
, u32 num_clusters
,
3731 unsigned int ext_flags
,
3732 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3737 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3738 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3740 ret
= ocfs2_lock_refcount_allocators(inode
->i_sb
,
3741 p_cluster
, num_clusters
,
3743 ref_root_bh
, &meta_ac
,
3750 handle
= ocfs2_start_trans(osb
, credits
);
3751 if (IS_ERR(handle
)) {
3752 ret
= PTR_ERR(handle
);
3757 ret
= ocfs2_insert_extent(handle
, et
, cpos
,
3758 cpu_to_le64(ocfs2_clusters_to_blocks(inode
->i_sb
,
3760 num_clusters
, ext_flags
, meta_ac
);
3766 ret
= __ocfs2_increase_refcount(handle
, ref_ci
, ref_root_bh
,
3767 p_cluster
, num_clusters
,
3773 ocfs2_commit_trans(osb
, handle
);
3776 ocfs2_free_alloc_context(meta_ac
);
3780 static int ocfs2_duplicate_extent_list(struct inode
*s_inode
,
3781 struct inode
*t_inode
,
3782 struct buffer_head
*t_bh
,
3783 struct ocfs2_caching_info
*ref_ci
,
3784 struct buffer_head
*ref_root_bh
,
3785 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3788 u32 p_cluster
, num_clusters
, clusters
, cpos
;
3790 unsigned int ext_flags
;
3791 struct ocfs2_extent_tree et
;
3793 ocfs2_init_dinode_extent_tree(&et
, INODE_CACHE(t_inode
), t_bh
);
3795 size
= i_size_read(s_inode
);
3796 clusters
= ocfs2_clusters_for_bytes(s_inode
->i_sb
, size
);
3799 while (cpos
< clusters
) {
3800 ret
= ocfs2_get_clusters(s_inode
, cpos
, &p_cluster
,
3801 &num_clusters
, &ext_flags
);
3804 ret
= ocfs2_add_refcounted_extent(t_inode
, &et
,
3805 ref_ci
, ref_root_bh
,
3816 cpos
+= num_clusters
;
3824 * change the new file's attributes to the src.
3826 * reflink creates a snapshot of a file, that means the attributes
3827 * must be identical except for three exceptions - nlink, ino, and ctime.
3829 static int ocfs2_complete_reflink(struct inode
*s_inode
,
3830 struct buffer_head
*s_bh
,
3831 struct inode
*t_inode
,
3832 struct buffer_head
*t_bh
)
3836 struct ocfs2_dinode
*s_di
= (struct ocfs2_dinode
*)s_bh
->b_data
;
3837 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)t_bh
->b_data
;
3838 loff_t size
= i_size_read(s_inode
);
3840 handle
= ocfs2_start_trans(OCFS2_SB(t_inode
->i_sb
),
3841 OCFS2_INODE_UPDATE_CREDITS
);
3842 if (IS_ERR(handle
)) {
3843 ret
= PTR_ERR(handle
);
3848 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(t_inode
), t_bh
,
3849 OCFS2_JOURNAL_ACCESS_WRITE
);
3855 spin_lock(&OCFS2_I(t_inode
)->ip_lock
);
3856 OCFS2_I(t_inode
)->ip_clusters
= OCFS2_I(s_inode
)->ip_clusters
;
3857 OCFS2_I(t_inode
)->ip_attr
= OCFS2_I(s_inode
)->ip_attr
;
3858 OCFS2_I(t_inode
)->ip_dyn_features
= OCFS2_I(s_inode
)->ip_dyn_features
;
3859 spin_unlock(&OCFS2_I(t_inode
)->ip_lock
);
3860 i_size_write(t_inode
, size
);
3862 di
->i_xattr_inline_size
= s_di
->i_xattr_inline_size
;
3863 di
->i_clusters
= s_di
->i_clusters
;
3864 di
->i_size
= s_di
->i_size
;
3865 di
->i_dyn_features
= s_di
->i_dyn_features
;
3866 di
->i_attr
= s_di
->i_attr
;
3867 di
->i_uid
= s_di
->i_uid
;
3868 di
->i_gid
= s_di
->i_gid
;
3869 di
->i_mode
= s_di
->i_mode
;
3873 * we want mtime to appear identical to the source and update ctime.
3875 t_inode
->i_ctime
= CURRENT_TIME
;
3877 di
->i_ctime
= cpu_to_le64(t_inode
->i_ctime
.tv_sec
);
3878 di
->i_ctime_nsec
= cpu_to_le32(t_inode
->i_ctime
.tv_nsec
);
3880 t_inode
->i_mtime
= s_inode
->i_mtime
;
3881 di
->i_mtime
= s_di
->i_mtime
;
3882 di
->i_mtime_nsec
= s_di
->i_mtime_nsec
;
3884 ocfs2_journal_dirty(handle
, t_bh
);
3887 ocfs2_commit_trans(OCFS2_SB(t_inode
->i_sb
), handle
);
3891 static int ocfs2_create_reflink_node(struct inode
*s_inode
,
3892 struct buffer_head
*s_bh
,
3893 struct inode
*t_inode
,
3894 struct buffer_head
*t_bh
)
3897 struct buffer_head
*ref_root_bh
= NULL
;
3898 struct ocfs2_cached_dealloc_ctxt dealloc
;
3899 struct ocfs2_super
*osb
= OCFS2_SB(s_inode
->i_sb
);
3900 struct ocfs2_refcount_block
*rb
;
3901 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)s_bh
->b_data
;
3902 struct ocfs2_refcount_tree
*ref_tree
;
3904 ocfs2_init_dealloc_ctxt(&dealloc
);
3906 ret
= ocfs2_set_refcount_tree(t_inode
, t_bh
,
3907 le64_to_cpu(di
->i_refcount_loc
));
3913 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
3914 1, &ref_tree
, &ref_root_bh
);
3919 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
3921 ret
= ocfs2_duplicate_extent_list(s_inode
, t_inode
, t_bh
,
3922 &ref_tree
->rf_ci
, ref_root_bh
,
3926 goto out_unlock_refcount
;
3929 ret
= ocfs2_complete_reflink(s_inode
, s_bh
, t_inode
, t_bh
);
3933 out_unlock_refcount
:
3934 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3935 brelse(ref_root_bh
);
3937 if (ocfs2_dealloc_has_cluster(&dealloc
)) {
3938 ocfs2_schedule_truncate_log_flush(osb
, 1);
3939 ocfs2_run_deallocs(osb
, &dealloc
);