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 * Try to remove refcount tree. The mechanism is:
931 * 1) Check whether i_clusters == 0, if no, exit.
932 * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
933 * 3) Check whether we have inline xattr stored outside, if yes, exit.
934 * 4) Remove the tree.
936 int ocfs2_try_remove_refcount_tree(struct inode
*inode
,
937 struct buffer_head
*di_bh
)
940 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
941 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
943 down_write(&oi
->ip_xattr_sem
);
944 down_write(&oi
->ip_alloc_sem
);
949 if ((oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
) && di
->i_xattr_loc
)
952 if (oi
->ip_dyn_features
& OCFS2_INLINE_XATTR_FL
&&
953 ocfs2_has_inline_xattr_value_outside(inode
, di
))
956 ret
= ocfs2_remove_refcount_tree(inode
, di_bh
);
960 up_write(&oi
->ip_alloc_sem
);
961 up_write(&oi
->ip_xattr_sem
);
966 * Given a cpos and len, try to find the refcount record which contains cpos.
967 * 1. If cpos can be found in one refcount record, return the record.
968 * 2. If cpos can't be found, return a fake record which start from cpos
969 * and end at a small value between cpos+len and start of the next record.
970 * This fake record has r_refcount = 0.
972 static int ocfs2_get_refcount_rec(struct ocfs2_caching_info
*ci
,
973 struct buffer_head
*ref_root_bh
,
974 u64 cpos
, unsigned int len
,
975 struct ocfs2_refcount_rec
*ret_rec
,
977 struct buffer_head
**ret_bh
)
979 int ret
= 0, i
, found
;
981 struct ocfs2_extent_list
*el
;
982 struct ocfs2_extent_rec
*tmp
, *rec
= NULL
;
983 struct ocfs2_extent_block
*eb
;
984 struct buffer_head
*eb_bh
= NULL
, *ref_leaf_bh
= NULL
;
985 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
986 struct ocfs2_refcount_block
*rb
=
987 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
989 if (!(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)) {
990 ocfs2_find_refcount_rec_in_rl(ci
, ref_root_bh
, cpos
, len
,
992 *ret_bh
= ref_root_bh
;
998 low_cpos
= cpos
& OCFS2_32BIT_POS_MASK
;
1000 if (el
->l_tree_depth
) {
1001 ret
= ocfs2_find_leaf(ci
, el
, low_cpos
, &eb_bh
);
1007 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
1010 if (el
->l_tree_depth
) {
1012 "refcount tree %llu has non zero tree "
1013 "depth in leaf btree tree block %llu\n",
1014 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1015 (unsigned long long)eb_bh
->b_blocknr
);
1022 for (i
= le16_to_cpu(el
->l_next_free_rec
) - 1; i
>= 0; i
--) {
1023 rec
= &el
->l_recs
[i
];
1025 if (le32_to_cpu(rec
->e_cpos
) <= low_cpos
) {
1031 /* adjust len when we have ocfs2_extent_rec after it. */
1032 if (found
&& i
< le16_to_cpu(el
->l_next_free_rec
) - 1) {
1033 tmp
= &el
->l_recs
[i
+1];
1035 if (le32_to_cpu(tmp
->e_cpos
) < cpos
+ len
)
1036 len
= le32_to_cpu(tmp
->e_cpos
) - cpos
;
1039 ret
= ocfs2_read_refcount_block(ci
, le64_to_cpu(rec
->e_blkno
),
1046 ocfs2_find_refcount_rec_in_rl(ci
, ref_leaf_bh
, cpos
, len
,
1048 *ret_bh
= ref_leaf_bh
;
1054 enum ocfs2_ref_rec_contig
{
1055 REF_CONTIG_NONE
= 0,
1058 REF_CONTIG_LEFTRIGHT
,
1061 static enum ocfs2_ref_rec_contig
1062 ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block
*rb
,
1065 if ((rb
->rf_records
.rl_recs
[index
].r_refcount
==
1066 rb
->rf_records
.rl_recs
[index
+ 1].r_refcount
) &&
1067 (le64_to_cpu(rb
->rf_records
.rl_recs
[index
].r_cpos
) +
1068 le32_to_cpu(rb
->rf_records
.rl_recs
[index
].r_clusters
) ==
1069 le64_to_cpu(rb
->rf_records
.rl_recs
[index
+ 1].r_cpos
)))
1070 return REF_CONTIG_RIGHT
;
1072 return REF_CONTIG_NONE
;
1075 static enum ocfs2_ref_rec_contig
1076 ocfs2_refcount_rec_contig(struct ocfs2_refcount_block
*rb
,
1079 enum ocfs2_ref_rec_contig ret
= REF_CONTIG_NONE
;
1081 if (index
< le16_to_cpu(rb
->rf_records
.rl_used
) - 1)
1082 ret
= ocfs2_refcount_rec_adjacent(rb
, index
);
1085 enum ocfs2_ref_rec_contig tmp
;
1087 tmp
= ocfs2_refcount_rec_adjacent(rb
, index
- 1);
1089 if (tmp
== REF_CONTIG_RIGHT
) {
1090 if (ret
== REF_CONTIG_RIGHT
)
1091 ret
= REF_CONTIG_LEFTRIGHT
;
1093 ret
= REF_CONTIG_LEFT
;
1100 static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block
*rb
,
1103 BUG_ON(rb
->rf_records
.rl_recs
[index
].r_refcount
!=
1104 rb
->rf_records
.rl_recs
[index
+1].r_refcount
);
1106 le32_add_cpu(&rb
->rf_records
.rl_recs
[index
].r_clusters
,
1107 le32_to_cpu(rb
->rf_records
.rl_recs
[index
+1].r_clusters
));
1109 if (index
< le16_to_cpu(rb
->rf_records
.rl_used
) - 2)
1110 memmove(&rb
->rf_records
.rl_recs
[index
+ 1],
1111 &rb
->rf_records
.rl_recs
[index
+ 2],
1112 sizeof(struct ocfs2_refcount_rec
) *
1113 (le16_to_cpu(rb
->rf_records
.rl_used
) - index
- 2));
1115 memset(&rb
->rf_records
.rl_recs
[le16_to_cpu(rb
->rf_records
.rl_used
) - 1],
1116 0, sizeof(struct ocfs2_refcount_rec
));
1117 le16_add_cpu(&rb
->rf_records
.rl_used
, -1);
1121 * Merge the refcount rec if we are contiguous with the adjacent recs.
1123 static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block
*rb
,
1126 enum ocfs2_ref_rec_contig contig
=
1127 ocfs2_refcount_rec_contig(rb
, index
);
1129 if (contig
== REF_CONTIG_NONE
)
1132 if (contig
== REF_CONTIG_LEFT
|| contig
== REF_CONTIG_LEFTRIGHT
) {
1137 ocfs2_rotate_refcount_rec_left(rb
, index
);
1139 if (contig
== REF_CONTIG_LEFTRIGHT
)
1140 ocfs2_rotate_refcount_rec_left(rb
, index
);
1144 * Change the refcount indexed by "index" in ref_bh.
1145 * If refcount reaches 0, remove it.
1147 static int ocfs2_change_refcount_rec(handle_t
*handle
,
1148 struct ocfs2_caching_info
*ci
,
1149 struct buffer_head
*ref_leaf_bh
,
1150 int index
, int change
)
1153 struct ocfs2_refcount_block
*rb
=
1154 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1155 struct ocfs2_refcount_list
*rl
= &rb
->rf_records
;
1156 struct ocfs2_refcount_rec
*rec
= &rl
->rl_recs
[index
];
1158 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1159 OCFS2_JOURNAL_ACCESS_WRITE
);
1165 mlog(0, "change index %d, old count %u, change %d\n", index
,
1166 le32_to_cpu(rec
->r_refcount
), change
);
1167 le32_add_cpu(&rec
->r_refcount
, change
);
1169 if (!rec
->r_refcount
) {
1170 if (index
!= le16_to_cpu(rl
->rl_used
) - 1) {
1171 memmove(rec
, rec
+ 1,
1172 (le16_to_cpu(rl
->rl_used
) - index
- 1) *
1173 sizeof(struct ocfs2_refcount_rec
));
1174 memset(&rl
->rl_recs
[le16_to_cpu(rl
->rl_used
) - 1],
1175 0, sizeof(struct ocfs2_refcount_rec
));
1178 le16_add_cpu(&rl
->rl_used
, -1);
1180 ocfs2_refcount_rec_merge(rb
, index
);
1182 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1189 static int ocfs2_expand_inline_ref_root(handle_t
*handle
,
1190 struct ocfs2_caching_info
*ci
,
1191 struct buffer_head
*ref_root_bh
,
1192 struct buffer_head
**ref_leaf_bh
,
1193 struct ocfs2_alloc_context
*meta_ac
)
1196 u16 suballoc_bit_start
;
1199 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1200 struct buffer_head
*new_bh
= NULL
;
1201 struct ocfs2_refcount_block
*new_rb
;
1202 struct ocfs2_refcount_block
*root_rb
=
1203 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1205 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
1206 OCFS2_JOURNAL_ACCESS_WRITE
);
1212 ret
= ocfs2_claim_metadata(OCFS2_SB(sb
), handle
, meta_ac
, 1,
1213 &suballoc_bit_start
, &num_got
,
1220 new_bh
= sb_getblk(sb
, blkno
);
1221 if (new_bh
== NULL
) {
1226 ocfs2_set_new_buffer_uptodate(ci
, new_bh
);
1228 ret
= ocfs2_journal_access_rb(handle
, ci
, new_bh
,
1229 OCFS2_JOURNAL_ACCESS_CREATE
);
1236 * Initialize ocfs2_refcount_block.
1237 * It should contain the same information as the old root.
1238 * so just memcpy it and change the corresponding field.
1240 memcpy(new_bh
->b_data
, ref_root_bh
->b_data
, sb
->s_blocksize
);
1242 new_rb
= (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1243 new_rb
->rf_suballoc_slot
= cpu_to_le16(OCFS2_SB(sb
)->slot_num
);
1244 new_rb
->rf_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1245 new_rb
->rf_blkno
= cpu_to_le64(blkno
);
1246 new_rb
->rf_cpos
= cpu_to_le32(0);
1247 new_rb
->rf_parent
= cpu_to_le64(ref_root_bh
->b_blocknr
);
1248 new_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL
);
1249 ocfs2_journal_dirty(handle
, new_bh
);
1251 /* Now change the root. */
1252 memset(&root_rb
->rf_list
, 0, sb
->s_blocksize
-
1253 offsetof(struct ocfs2_refcount_block
, rf_list
));
1254 root_rb
->rf_list
.l_count
= cpu_to_le16(ocfs2_extent_recs_per_rb(sb
));
1255 root_rb
->rf_clusters
= cpu_to_le32(1);
1256 root_rb
->rf_list
.l_next_free_rec
= cpu_to_le16(1);
1257 root_rb
->rf_list
.l_recs
[0].e_blkno
= cpu_to_le64(blkno
);
1258 root_rb
->rf_list
.l_recs
[0].e_leaf_clusters
= cpu_to_le16(1);
1259 root_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_TREE_FL
);
1261 ocfs2_journal_dirty(handle
, ref_root_bh
);
1263 mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno
,
1264 le16_to_cpu(new_rb
->rf_records
.rl_used
));
1266 *ref_leaf_bh
= new_bh
;
1273 static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec
*prev
,
1274 struct ocfs2_refcount_rec
*next
)
1276 if (ocfs2_get_ref_rec_low_cpos(prev
) + le32_to_cpu(prev
->r_clusters
) <=
1277 ocfs2_get_ref_rec_low_cpos(next
))
1283 static int cmp_refcount_rec_by_low_cpos(const void *a
, const void *b
)
1285 const struct ocfs2_refcount_rec
*l
= a
, *r
= b
;
1286 u32 l_cpos
= ocfs2_get_ref_rec_low_cpos(l
);
1287 u32 r_cpos
= ocfs2_get_ref_rec_low_cpos(r
);
1289 if (l_cpos
> r_cpos
)
1291 if (l_cpos
< r_cpos
)
1296 static int cmp_refcount_rec_by_cpos(const void *a
, const void *b
)
1298 const struct ocfs2_refcount_rec
*l
= a
, *r
= b
;
1299 u64 l_cpos
= le64_to_cpu(l
->r_cpos
);
1300 u64 r_cpos
= le64_to_cpu(r
->r_cpos
);
1302 if (l_cpos
> r_cpos
)
1304 if (l_cpos
< r_cpos
)
1309 static void swap_refcount_rec(void *a
, void *b
, int size
)
1311 struct ocfs2_refcount_rec
*l
= a
, *r
= b
, tmp
;
1313 tmp
= *(struct ocfs2_refcount_rec
*)l
;
1314 *(struct ocfs2_refcount_rec
*)l
=
1315 *(struct ocfs2_refcount_rec
*)r
;
1316 *(struct ocfs2_refcount_rec
*)r
= tmp
;
1320 * The refcount cpos are ordered by their 64bit cpos,
1321 * But we will use the low 32 bit to be the e_cpos in the b-tree.
1322 * So we need to make sure that this pos isn't intersected with others.
1324 * Note: The refcount block is already sorted by their low 32 bit cpos,
1325 * So just try the middle pos first, and we will exit when we find
1326 * the good position.
1328 static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list
*rl
,
1329 u32
*split_pos
, int *split_index
)
1331 int num_used
= le16_to_cpu(rl
->rl_used
);
1332 int delta
, middle
= num_used
/ 2;
1334 for (delta
= 0; delta
< middle
; delta
++) {
1335 /* Let's check delta earlier than middle */
1336 if (ocfs2_refcount_rec_no_intersect(
1337 &rl
->rl_recs
[middle
- delta
- 1],
1338 &rl
->rl_recs
[middle
- delta
])) {
1339 *split_index
= middle
- delta
;
1343 /* For even counts, don't walk off the end */
1344 if ((middle
+ delta
+ 1) == num_used
)
1347 /* Now try delta past middle */
1348 if (ocfs2_refcount_rec_no_intersect(
1349 &rl
->rl_recs
[middle
+ delta
],
1350 &rl
->rl_recs
[middle
+ delta
+ 1])) {
1351 *split_index
= middle
+ delta
+ 1;
1356 if (delta
>= middle
)
1359 *split_pos
= ocfs2_get_ref_rec_low_cpos(&rl
->rl_recs
[*split_index
]);
1363 static int ocfs2_divide_leaf_refcount_block(struct buffer_head
*ref_leaf_bh
,
1364 struct buffer_head
*new_bh
,
1367 int split_index
= 0, num_moved
, ret
;
1369 struct ocfs2_refcount_block
*rb
=
1370 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1371 struct ocfs2_refcount_list
*rl
= &rb
->rf_records
;
1372 struct ocfs2_refcount_block
*new_rb
=
1373 (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1374 struct ocfs2_refcount_list
*new_rl
= &new_rb
->rf_records
;
1376 mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
1377 (unsigned long long)ref_leaf_bh
->b_blocknr
,
1378 le32_to_cpu(rl
->rl_count
), le32_to_cpu(rl
->rl_used
));
1381 * XXX: Improvement later.
1382 * If we know all the high 32 bit cpos is the same, no need to sort.
1384 * In order to make the whole process safe, we do:
1385 * 1. sort the entries by their low 32 bit cpos first so that we can
1386 * find the split cpos easily.
1387 * 2. call ocfs2_insert_extent to insert the new refcount block.
1388 * 3. move the refcount rec to the new block.
1389 * 4. sort the entries by their 64 bit cpos.
1390 * 5. dirty the new_rb and rb.
1392 sort(&rl
->rl_recs
, le16_to_cpu(rl
->rl_used
),
1393 sizeof(struct ocfs2_refcount_rec
),
1394 cmp_refcount_rec_by_low_cpos
, swap_refcount_rec
);
1396 ret
= ocfs2_find_refcount_split_pos(rl
, &cpos
, &split_index
);
1402 new_rb
->rf_cpos
= cpu_to_le32(cpos
);
1404 /* move refcount records starting from split_index to the new block. */
1405 num_moved
= le16_to_cpu(rl
->rl_used
) - split_index
;
1406 memcpy(new_rl
->rl_recs
, &rl
->rl_recs
[split_index
],
1407 num_moved
* sizeof(struct ocfs2_refcount_rec
));
1409 /*ok, remove the entries we just moved over to the other block. */
1410 memset(&rl
->rl_recs
[split_index
], 0,
1411 num_moved
* sizeof(struct ocfs2_refcount_rec
));
1413 /* change old and new rl_used accordingly. */
1414 le16_add_cpu(&rl
->rl_used
, -num_moved
);
1415 new_rl
->rl_used
= cpu_to_le32(num_moved
);
1417 sort(&rl
->rl_recs
, le16_to_cpu(rl
->rl_used
),
1418 sizeof(struct ocfs2_refcount_rec
),
1419 cmp_refcount_rec_by_cpos
, swap_refcount_rec
);
1421 sort(&new_rl
->rl_recs
, le16_to_cpu(new_rl
->rl_used
),
1422 sizeof(struct ocfs2_refcount_rec
),
1423 cmp_refcount_rec_by_cpos
, swap_refcount_rec
);
1429 static int ocfs2_new_leaf_refcount_block(handle_t
*handle
,
1430 struct ocfs2_caching_info
*ci
,
1431 struct buffer_head
*ref_root_bh
,
1432 struct buffer_head
*ref_leaf_bh
,
1433 struct ocfs2_alloc_context
*meta_ac
)
1436 u16 suballoc_bit_start
;
1437 u32 num_got
, new_cpos
;
1439 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1440 struct ocfs2_refcount_block
*root_rb
=
1441 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1442 struct buffer_head
*new_bh
= NULL
;
1443 struct ocfs2_refcount_block
*new_rb
;
1444 struct ocfs2_extent_tree ref_et
;
1446 BUG_ON(!(le32_to_cpu(root_rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
));
1448 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
1449 OCFS2_JOURNAL_ACCESS_WRITE
);
1455 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1456 OCFS2_JOURNAL_ACCESS_WRITE
);
1462 ret
= ocfs2_claim_metadata(OCFS2_SB(sb
), handle
, meta_ac
, 1,
1463 &suballoc_bit_start
, &num_got
,
1470 new_bh
= sb_getblk(sb
, blkno
);
1471 if (new_bh
== NULL
) {
1476 ocfs2_set_new_buffer_uptodate(ci
, new_bh
);
1478 ret
= ocfs2_journal_access_rb(handle
, ci
, new_bh
,
1479 OCFS2_JOURNAL_ACCESS_CREATE
);
1485 /* Initialize ocfs2_refcount_block. */
1486 new_rb
= (struct ocfs2_refcount_block
*)new_bh
->b_data
;
1487 memset(new_rb
, 0, sb
->s_blocksize
);
1488 strcpy((void *)new_rb
, OCFS2_REFCOUNT_BLOCK_SIGNATURE
);
1489 new_rb
->rf_suballoc_slot
= cpu_to_le16(OCFS2_SB(sb
)->slot_num
);
1490 new_rb
->rf_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
1491 new_rb
->rf_fs_generation
= cpu_to_le32(OCFS2_SB(sb
)->fs_generation
);
1492 new_rb
->rf_blkno
= cpu_to_le64(blkno
);
1493 new_rb
->rf_parent
= cpu_to_le64(ref_root_bh
->b_blocknr
);
1494 new_rb
->rf_flags
= cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL
);
1495 new_rb
->rf_records
.rl_count
=
1496 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb
));
1497 new_rb
->rf_generation
= root_rb
->rf_generation
;
1499 ret
= ocfs2_divide_leaf_refcount_block(ref_leaf_bh
, new_bh
, &new_cpos
);
1505 ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1506 ocfs2_journal_dirty(handle
, new_bh
);
1508 ocfs2_init_refcount_extent_tree(&ref_et
, ci
, ref_root_bh
);
1510 mlog(0, "insert new leaf block %llu at %u\n",
1511 (unsigned long long)new_bh
->b_blocknr
, new_cpos
);
1513 /* Insert the new leaf block with the specific offset cpos. */
1514 ret
= ocfs2_insert_extent(handle
, &ref_et
, new_cpos
, new_bh
->b_blocknr
,
1524 static int ocfs2_expand_refcount_tree(handle_t
*handle
,
1525 struct ocfs2_caching_info
*ci
,
1526 struct buffer_head
*ref_root_bh
,
1527 struct buffer_head
*ref_leaf_bh
,
1528 struct ocfs2_alloc_context
*meta_ac
)
1531 struct buffer_head
*expand_bh
= NULL
;
1533 if (ref_root_bh
== ref_leaf_bh
) {
1535 * the old root bh hasn't been expanded to a b-tree,
1536 * so expand it first.
1538 ret
= ocfs2_expand_inline_ref_root(handle
, ci
, ref_root_bh
,
1539 &expand_bh
, meta_ac
);
1545 expand_bh
= ref_leaf_bh
;
1550 /* Now add a new refcount block into the tree.*/
1551 ret
= ocfs2_new_leaf_refcount_block(handle
, ci
, ref_root_bh
,
1552 expand_bh
, meta_ac
);
1561 * Adjust the extent rec in b-tree representing ref_leaf_bh.
1563 * Only called when we have inserted a new refcount rec at index 0
1564 * which means ocfs2_extent_rec.e_cpos may need some change.
1566 static int ocfs2_adjust_refcount_rec(handle_t
*handle
,
1567 struct ocfs2_caching_info
*ci
,
1568 struct buffer_head
*ref_root_bh
,
1569 struct buffer_head
*ref_leaf_bh
,
1570 struct ocfs2_refcount_rec
*rec
)
1573 u32 new_cpos
, old_cpos
;
1574 struct ocfs2_path
*path
= NULL
;
1575 struct ocfs2_extent_tree et
;
1576 struct ocfs2_refcount_block
*rb
=
1577 (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
1578 struct ocfs2_extent_list
*el
;
1580 if (!(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
))
1583 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1584 old_cpos
= le32_to_cpu(rb
->rf_cpos
);
1585 new_cpos
= le64_to_cpu(rec
->r_cpos
) & OCFS2_32BIT_POS_MASK
;
1586 if (old_cpos
<= new_cpos
)
1589 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
1591 path
= ocfs2_new_path_from_et(&et
);
1598 ret
= ocfs2_find_path(ci
, path
, old_cpos
);
1605 * 2 more credits, one for the leaf refcount block, one for
1606 * the extent block contains the extent rec.
1608 ret
= ocfs2_extend_trans(handle
, handle
->h_buffer_credits
+ 2);
1614 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1615 OCFS2_JOURNAL_ACCESS_WRITE
);
1621 ret
= ocfs2_journal_access_eb(handle
, ci
, path_leaf_bh(path
),
1622 OCFS2_JOURNAL_ACCESS_WRITE
);
1628 /* change the leaf extent block first. */
1629 el
= path_leaf_el(path
);
1631 for (i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
1632 if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) == old_cpos
)
1635 BUG_ON(i
== le16_to_cpu(el
->l_next_free_rec
));
1637 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1639 /* change the r_cpos in the leaf block. */
1640 rb
->rf_cpos
= cpu_to_le32(new_cpos
);
1642 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
1643 ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1646 ocfs2_free_path(path
);
1650 static int ocfs2_insert_refcount_rec(handle_t
*handle
,
1651 struct ocfs2_caching_info
*ci
,
1652 struct buffer_head
*ref_root_bh
,
1653 struct buffer_head
*ref_leaf_bh
,
1654 struct ocfs2_refcount_rec
*rec
,
1656 struct ocfs2_alloc_context
*meta_ac
)
1659 struct ocfs2_refcount_block
*rb
=
1660 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1661 struct ocfs2_refcount_list
*rf_list
= &rb
->rf_records
;
1662 struct buffer_head
*new_bh
= NULL
;
1664 BUG_ON(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
);
1666 if (rf_list
->rl_used
== rf_list
->rl_count
) {
1667 u64 cpos
= le64_to_cpu(rec
->r_cpos
);
1668 u32 len
= le32_to_cpu(rec
->r_clusters
);
1670 ret
= ocfs2_expand_refcount_tree(handle
, ci
, ref_root_bh
,
1671 ref_leaf_bh
, meta_ac
);
1677 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1678 cpos
, len
, NULL
, &index
,
1685 ref_leaf_bh
= new_bh
;
1686 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1687 rf_list
= &rb
->rf_records
;
1690 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1691 OCFS2_JOURNAL_ACCESS_WRITE
);
1697 if (index
< le16_to_cpu(rf_list
->rl_used
))
1698 memmove(&rf_list
->rl_recs
[index
+ 1],
1699 &rf_list
->rl_recs
[index
],
1700 (le16_to_cpu(rf_list
->rl_used
) - index
) *
1701 sizeof(struct ocfs2_refcount_rec
));
1703 mlog(0, "insert refcount record start %llu, len %u, count %u "
1704 "to leaf block %llu at index %d\n",
1705 (unsigned long long)le64_to_cpu(rec
->r_cpos
),
1706 le32_to_cpu(rec
->r_clusters
), le32_to_cpu(rec
->r_refcount
),
1707 (unsigned long long)ref_leaf_bh
->b_blocknr
, index
);
1709 rf_list
->rl_recs
[index
] = *rec
;
1711 le16_add_cpu(&rf_list
->rl_used
, 1);
1713 ocfs2_refcount_rec_merge(rb
, index
);
1715 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1722 ret
= ocfs2_adjust_refcount_rec(handle
, ci
,
1734 * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1735 * This is much simple than our b-tree code.
1736 * split_rec is the new refcount rec we want to insert.
1737 * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1738 * increase refcount or decrease a refcount to non-zero).
1739 * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1740 * rec( in case we decrease a refcount to zero).
1742 static int ocfs2_split_refcount_rec(handle_t
*handle
,
1743 struct ocfs2_caching_info
*ci
,
1744 struct buffer_head
*ref_root_bh
,
1745 struct buffer_head
*ref_leaf_bh
,
1746 struct ocfs2_refcount_rec
*split_rec
,
1748 struct ocfs2_alloc_context
*meta_ac
,
1749 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1753 struct ocfs2_refcount_block
*rb
=
1754 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1755 struct ocfs2_refcount_list
*rf_list
= &rb
->rf_records
;
1756 struct ocfs2_refcount_rec
*orig_rec
= &rf_list
->rl_recs
[index
];
1757 struct ocfs2_refcount_rec
*tail_rec
= NULL
;
1758 struct buffer_head
*new_bh
= NULL
;
1760 BUG_ON(le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
);
1762 mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
1763 le64_to_cpu(orig_rec
->r_cpos
), le32_to_cpu(orig_rec
->r_clusters
),
1764 le64_to_cpu(split_rec
->r_cpos
),
1765 le32_to_cpu(split_rec
->r_clusters
));
1768 * If we just need to split the header or tail clusters,
1769 * no more recs are needed, just split is OK.
1770 * Otherwise we at least need one new recs.
1772 if (!split_rec
->r_refcount
&&
1773 (split_rec
->r_cpos
== orig_rec
->r_cpos
||
1774 le64_to_cpu(split_rec
->r_cpos
) +
1775 le32_to_cpu(split_rec
->r_clusters
) ==
1776 le64_to_cpu(orig_rec
->r_cpos
) + le32_to_cpu(orig_rec
->r_clusters
)))
1782 * We need one more rec if we split in the middle and the new rec have
1783 * some refcount in it.
1785 if (split_rec
->r_refcount
&&
1786 (split_rec
->r_cpos
!= orig_rec
->r_cpos
&&
1787 le64_to_cpu(split_rec
->r_cpos
) +
1788 le32_to_cpu(split_rec
->r_clusters
) !=
1789 le64_to_cpu(orig_rec
->r_cpos
) + le32_to_cpu(orig_rec
->r_clusters
)))
1792 /* If the leaf block don't have enough record, expand it. */
1793 if (le16_to_cpu(rf_list
->rl_used
) + recs_need
> rf_list
->rl_count
) {
1794 struct ocfs2_refcount_rec tmp_rec
;
1795 u64 cpos
= le64_to_cpu(orig_rec
->r_cpos
);
1796 len
= le32_to_cpu(orig_rec
->r_clusters
);
1797 ret
= ocfs2_expand_refcount_tree(handle
, ci
, ref_root_bh
,
1798 ref_leaf_bh
, meta_ac
);
1805 * We have to re-get it since now cpos may be moved to
1806 * another leaf block.
1808 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1809 cpos
, len
, &tmp_rec
, &index
,
1816 ref_leaf_bh
= new_bh
;
1817 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
1818 rf_list
= &rb
->rf_records
;
1819 orig_rec
= &rf_list
->rl_recs
[index
];
1822 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_leaf_bh
,
1823 OCFS2_JOURNAL_ACCESS_WRITE
);
1830 * We have calculated out how many new records we need and store
1831 * in recs_need, so spare enough space first by moving the records
1832 * after "index" to the end.
1834 if (index
!= le16_to_cpu(rf_list
->rl_used
) - 1)
1835 memmove(&rf_list
->rl_recs
[index
+ 1 + recs_need
],
1836 &rf_list
->rl_recs
[index
+ 1],
1837 (le16_to_cpu(rf_list
->rl_used
) - index
- 1) *
1838 sizeof(struct ocfs2_refcount_rec
));
1840 len
= (le64_to_cpu(orig_rec
->r_cpos
) +
1841 le32_to_cpu(orig_rec
->r_clusters
)) -
1842 (le64_to_cpu(split_rec
->r_cpos
) +
1843 le32_to_cpu(split_rec
->r_clusters
));
1846 * If we have "len", the we will split in the tail and move it
1847 * to the end of the space we have just spared.
1850 tail_rec
= &rf_list
->rl_recs
[index
+ recs_need
];
1852 memcpy(tail_rec
, orig_rec
, sizeof(struct ocfs2_refcount_rec
));
1853 le64_add_cpu(&tail_rec
->r_cpos
,
1854 le32_to_cpu(tail_rec
->r_clusters
) - len
);
1855 tail_rec
->r_clusters
= le32_to_cpu(len
);
1859 * If the split pos isn't the same as the original one, we need to
1860 * split in the head.
1862 * Note: We have the chance that split_rec.r_refcount = 0,
1863 * recs_need = 0 and len > 0, which means we just cut the head from
1864 * the orig_rec and in that case we have done some modification in
1865 * orig_rec above, so the check for r_cpos is faked.
1867 if (split_rec
->r_cpos
!= orig_rec
->r_cpos
&& tail_rec
!= orig_rec
) {
1868 len
= le64_to_cpu(split_rec
->r_cpos
) -
1869 le64_to_cpu(orig_rec
->r_cpos
);
1870 orig_rec
->r_clusters
= cpu_to_le32(len
);
1874 le16_add_cpu(&rf_list
->rl_used
, recs_need
);
1876 if (split_rec
->r_refcount
) {
1877 rf_list
->rl_recs
[index
] = *split_rec
;
1878 mlog(0, "insert refcount record start %llu, len %u, count %u "
1879 "to leaf block %llu at index %d\n",
1880 (unsigned long long)le64_to_cpu(split_rec
->r_cpos
),
1881 le32_to_cpu(split_rec
->r_clusters
),
1882 le32_to_cpu(split_rec
->r_refcount
),
1883 (unsigned long long)ref_leaf_bh
->b_blocknr
, index
);
1885 ocfs2_refcount_rec_merge(rb
, index
);
1888 ret
= ocfs2_journal_dirty(handle
, ref_leaf_bh
);
1897 int ocfs2_increase_refcount(handle_t
*handle
,
1898 struct ocfs2_caching_info
*ci
,
1899 struct buffer_head
*ref_root_bh
,
1901 struct ocfs2_alloc_context
*meta_ac
,
1902 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1905 struct buffer_head
*ref_leaf_bh
= NULL
;
1906 struct ocfs2_refcount_rec rec
;
1907 unsigned int set_len
= 0;
1909 mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
1910 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
1911 (unsigned long long)cpos
, len
);
1914 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
1915 cpos
, len
, &rec
, &index
,
1922 set_len
= le32_to_cpu(rec
.r_clusters
);
1925 * Here we may meet with 3 situations:
1927 * 1. If we find an already existing record, and the length
1928 * is the same, cool, we just need to increase the r_refcount
1930 * 2. If we find a hole, just insert it with r_refcount = 1.
1931 * 3. If we are in the middle of one extent record, split
1934 if (rec
.r_refcount
&& le64_to_cpu(rec
.r_cpos
) == cpos
&&
1936 mlog(0, "increase refcount rec, start %llu, len %u, "
1937 "count %u\n", (unsigned long long)cpos
, set_len
,
1938 le32_to_cpu(rec
.r_refcount
));
1939 ret
= ocfs2_change_refcount_rec(handle
, ci
,
1940 ref_leaf_bh
, index
, 1);
1945 } else if (!rec
.r_refcount
) {
1946 rec
.r_refcount
= cpu_to_le32(1);
1948 mlog(0, "insert refcount rec, start %llu, len %u\n",
1949 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
1951 ret
= ocfs2_insert_refcount_rec(handle
, ci
, ref_root_bh
,
1953 &rec
, index
, meta_ac
);
1959 set_len
= min((u64
)(cpos
+ len
),
1960 le64_to_cpu(rec
.r_cpos
) + set_len
) - cpos
;
1961 rec
.r_cpos
= cpu_to_le64(cpos
);
1962 rec
.r_clusters
= cpu_to_le32(set_len
);
1963 le32_add_cpu(&rec
.r_refcount
, 1);
1965 mlog(0, "split refcount rec, start %llu, "
1966 "len %u, count %u\n",
1967 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
1968 set_len
, le32_to_cpu(rec
.r_refcount
));
1969 ret
= ocfs2_split_refcount_rec(handle
, ci
,
1970 ref_root_bh
, ref_leaf_bh
,
1981 brelse(ref_leaf_bh
);
1986 brelse(ref_leaf_bh
);
1990 static int ocfs2_remove_refcount_extent(handle_t
*handle
,
1991 struct ocfs2_caching_info
*ci
,
1992 struct buffer_head
*ref_root_bh
,
1993 struct buffer_head
*ref_leaf_bh
,
1994 struct ocfs2_alloc_context
*meta_ac
,
1995 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
1998 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
1999 struct ocfs2_refcount_block
*rb
=
2000 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
2001 struct ocfs2_extent_tree et
;
2003 BUG_ON(rb
->rf_records
.rl_used
);
2005 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
2006 ret
= ocfs2_remove_extent(handle
, &et
, le32_to_cpu(rb
->rf_cpos
),
2007 1, meta_ac
, dealloc
);
2013 ocfs2_remove_from_cache(ci
, ref_leaf_bh
);
2016 * add the freed block to the dealloc so that it will be freed
2017 * when we run dealloc.
2019 ret
= ocfs2_cache_block_dealloc(dealloc
, EXTENT_ALLOC_SYSTEM_INODE
,
2020 le16_to_cpu(rb
->rf_suballoc_slot
),
2021 le64_to_cpu(rb
->rf_blkno
),
2022 le16_to_cpu(rb
->rf_suballoc_bit
));
2028 ret
= ocfs2_journal_access_rb(handle
, ci
, ref_root_bh
,
2029 OCFS2_JOURNAL_ACCESS_WRITE
);
2035 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
2037 le32_add_cpu(&rb
->rf_clusters
, -1);
2040 * check whether we need to restore the root refcount block if
2041 * there is no leaf extent block at atll.
2043 if (!rb
->rf_list
.l_next_free_rec
) {
2044 BUG_ON(rb
->rf_clusters
);
2046 mlog(0, "reset refcount tree root %llu to be a record block.\n",
2047 (unsigned long long)ref_root_bh
->b_blocknr
);
2052 memset(&rb
->rf_records
, 0, sb
->s_blocksize
-
2053 offsetof(struct ocfs2_refcount_block
, rf_records
));
2054 rb
->rf_records
.rl_count
=
2055 cpu_to_le16(ocfs2_refcount_recs_per_rb(sb
));
2058 ocfs2_journal_dirty(handle
, ref_root_bh
);
2064 static int ocfs2_decrease_refcount_rec(handle_t
*handle
,
2065 struct ocfs2_caching_info
*ci
,
2066 struct buffer_head
*ref_root_bh
,
2067 struct buffer_head
*ref_leaf_bh
,
2068 int index
, u64 cpos
, unsigned int len
,
2069 struct ocfs2_alloc_context
*meta_ac
,
2070 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2073 struct ocfs2_refcount_block
*rb
=
2074 (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
2075 struct ocfs2_refcount_rec
*rec
= &rb
->rf_records
.rl_recs
[index
];
2077 BUG_ON(cpos
< le64_to_cpu(rec
->r_cpos
));
2079 le64_to_cpu(rec
->r_cpos
) + le32_to_cpu(rec
->r_clusters
));
2081 if (cpos
== le64_to_cpu(rec
->r_cpos
) &&
2082 len
== le32_to_cpu(rec
->r_clusters
))
2083 ret
= ocfs2_change_refcount_rec(handle
, ci
,
2084 ref_leaf_bh
, index
, -1);
2086 struct ocfs2_refcount_rec split
= *rec
;
2087 split
.r_cpos
= cpu_to_le64(cpos
);
2088 split
.r_clusters
= cpu_to_le32(len
);
2090 le32_add_cpu(&split
.r_refcount
, -1);
2092 mlog(0, "split refcount rec, start %llu, "
2093 "len %u, count %u, original start %llu, len %u\n",
2094 (unsigned long long)le64_to_cpu(split
.r_cpos
),
2095 len
, le32_to_cpu(split
.r_refcount
),
2096 (unsigned long long)le64_to_cpu(rec
->r_cpos
),
2097 le32_to_cpu(rec
->r_clusters
));
2098 ret
= ocfs2_split_refcount_rec(handle
, ci
,
2099 ref_root_bh
, ref_leaf_bh
,
2109 /* Remove the leaf refcount block if it contains no refcount record. */
2110 if (!rb
->rf_records
.rl_used
&& ref_leaf_bh
!= ref_root_bh
) {
2111 ret
= ocfs2_remove_refcount_extent(handle
, ci
, ref_root_bh
,
2112 ref_leaf_bh
, meta_ac
,
2122 static int __ocfs2_decrease_refcount(handle_t
*handle
,
2123 struct ocfs2_caching_info
*ci
,
2124 struct buffer_head
*ref_root_bh
,
2126 struct ocfs2_alloc_context
*meta_ac
,
2127 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2130 int ret
= 0, index
= 0;
2131 struct ocfs2_refcount_rec rec
;
2132 unsigned int r_count
= 0, r_len
;
2133 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
2134 struct buffer_head
*ref_leaf_bh
= NULL
;
2136 mlog(0, "Tree owner %llu, decrease refcount start %llu, "
2137 "len %u, delete %u\n",
2138 (unsigned long long)ocfs2_metadata_cache_owner(ci
),
2139 (unsigned long long)cpos
, len
, delete);
2142 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
2143 cpos
, len
, &rec
, &index
,
2150 r_count
= le32_to_cpu(rec
.r_refcount
);
2151 BUG_ON(r_count
== 0);
2153 BUG_ON(r_count
> 1);
2155 r_len
= min((u64
)(cpos
+ len
), le64_to_cpu(rec
.r_cpos
) +
2156 le32_to_cpu(rec
.r_clusters
)) - cpos
;
2158 ret
= ocfs2_decrease_refcount_rec(handle
, ci
, ref_root_bh
,
2167 if (le32_to_cpu(rec
.r_refcount
) == 1 && delete) {
2168 ret
= ocfs2_cache_cluster_dealloc(dealloc
,
2169 ocfs2_clusters_to_blocks(sb
, cpos
),
2179 brelse(ref_leaf_bh
);
2184 brelse(ref_leaf_bh
);
2188 /* Caller must hold refcount tree lock. */
2189 int ocfs2_decrease_refcount(struct inode
*inode
,
2190 handle_t
*handle
, u32 cpos
, u32 len
,
2191 struct ocfs2_alloc_context
*meta_ac
,
2192 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2197 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2198 struct buffer_head
*ref_root_bh
= NULL
;
2199 struct ocfs2_refcount_tree
*tree
;
2201 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
2203 ret
= ocfs2_get_refcount_block(inode
, &ref_blkno
);
2209 ret
= ocfs2_get_refcount_tree(OCFS2_SB(inode
->i_sb
), ref_blkno
, &tree
);
2215 ret
= ocfs2_read_refcount_block(&tree
->rf_ci
, tree
->rf_blkno
,
2222 ret
= __ocfs2_decrease_refcount(handle
, &tree
->rf_ci
, ref_root_bh
,
2223 cpos
, len
, meta_ac
, dealloc
, delete);
2227 brelse(ref_root_bh
);
2232 * Mark the already-existing extent at cpos as refcounted for len clusters.
2233 * This adds the refcount extent flag.
2235 * If the existing extent is larger than the request, initiate a
2236 * split. An attempt will be made at merging with adjacent extents.
2238 * The caller is responsible for passing down meta_ac if we'll need it.
2240 static int ocfs2_mark_extent_refcounted(struct inode
*inode
,
2241 struct ocfs2_extent_tree
*et
,
2242 handle_t
*handle
, u32 cpos
,
2244 struct ocfs2_alloc_context
*meta_ac
,
2245 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2249 mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
2250 inode
->i_ino
, cpos
, len
, phys
);
2252 if (!ocfs2_refcount_tree(OCFS2_SB(inode
->i_sb
))) {
2253 ocfs2_error(inode
->i_sb
, "Inode %lu want to use refcount "
2254 "tree, but the feature bit is not set in the "
2255 "super block.", inode
->i_ino
);
2260 ret
= ocfs2_change_extent_flag(handle
, et
, cpos
,
2261 len
, phys
, meta_ac
, dealloc
,
2262 OCFS2_EXT_REFCOUNTED
, 0);
2271 * Given some contiguous physical clusters, calculate what we need
2272 * for modifying their refcount.
2274 static int ocfs2_calc_refcount_meta_credits(struct super_block
*sb
,
2275 struct ocfs2_caching_info
*ci
,
2276 struct buffer_head
*ref_root_bh
,
2282 int ret
= 0, index
, ref_blocks
= 0, recs_add
= 0;
2283 u64 cpos
= start_cpos
;
2284 struct ocfs2_refcount_block
*rb
;
2285 struct ocfs2_refcount_rec rec
;
2286 struct buffer_head
*ref_leaf_bh
= NULL
, *prev_bh
= NULL
;
2289 mlog(0, "start_cpos %llu, clusters %u\n",
2290 (unsigned long long)start_cpos
, clusters
);
2292 ret
= ocfs2_get_refcount_rec(ci
, ref_root_bh
,
2293 cpos
, clusters
, &rec
,
2294 &index
, &ref_leaf_bh
);
2300 if (ref_leaf_bh
!= prev_bh
) {
2302 * Now we encounter a new leaf block, so calculate
2303 * whether we need to extend the old leaf.
2306 rb
= (struct ocfs2_refcount_block
*)
2309 if (le64_to_cpu(rb
->rf_records
.rl_used
) +
2311 le16_to_cpu(rb
->rf_records
.rl_count
))
2318 prev_bh
= ref_leaf_bh
;
2322 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
2324 mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
2325 "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
2326 recs_add
, (unsigned long long)cpos
, clusters
,
2327 (unsigned long long)le64_to_cpu(rec
.r_cpos
),
2328 le32_to_cpu(rec
.r_clusters
),
2329 le32_to_cpu(rec
.r_refcount
), index
);
2331 len
= min((u64
)cpos
+ clusters
, le64_to_cpu(rec
.r_cpos
) +
2332 le32_to_cpu(rec
.r_clusters
)) - cpos
;
2334 * If the refcount rec already exist, cool. We just need
2335 * to check whether there is a split. Otherwise we just need
2336 * to increase the refcount.
2337 * If we will insert one, increases recs_add.
2339 * We record all the records which will be inserted to the
2340 * same refcount block, so that we can tell exactly whether
2341 * we need a new refcount block or not.
2343 if (rec
.r_refcount
) {
2344 /* Check whether we need a split at the beginning. */
2345 if (cpos
== start_cpos
&&
2346 cpos
!= le64_to_cpu(rec
.r_cpos
))
2349 /* Check whether we need a split in the end. */
2350 if (cpos
+ clusters
< le64_to_cpu(rec
.r_cpos
) +
2351 le32_to_cpu(rec
.r_clusters
))
2356 brelse(ref_leaf_bh
);
2363 rb
= (struct ocfs2_refcount_block
*)prev_bh
->b_data
;
2365 if (le64_to_cpu(rb
->rf_records
.rl_used
) + recs_add
>
2366 le16_to_cpu(rb
->rf_records
.rl_count
))
2375 mlog(0, "we need ref_blocks %d\n", ref_blocks
);
2376 *meta_add
+= ref_blocks
;
2377 *credits
+= ref_blocks
;
2380 * So we may need ref_blocks to insert into the tree.
2381 * That also means we need to change the b-tree and add that number
2382 * of records since we never merge them.
2383 * We need one more block for expansion since the new created leaf
2384 * block is also full and needs split.
2386 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
2387 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
) {
2388 struct ocfs2_extent_tree et
;
2390 ocfs2_init_refcount_extent_tree(&et
, ci
, ref_root_bh
);
2391 *meta_add
+= ocfs2_extend_meta_needed(et
.et_root_el
);
2392 *credits
+= ocfs2_calc_extend_credits(sb
,
2396 *credits
+= OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
2401 brelse(ref_leaf_bh
);
2407 * For refcount tree, we will decrease some contiguous clusters
2408 * refcount count, so just go through it to see how many blocks
2409 * we gonna touch and whether we need to create new blocks.
2411 * Normally the refcount blocks store these refcount should be
2412 * continguous also, so that we can get the number easily.
2413 * As for meta_ac, we will at most add split 2 refcount record and
2414 * 2 more refcount block, so just check it in a rough way.
2416 * Caller must hold refcount tree lock.
2418 int ocfs2_prepare_refcount_change_for_del(struct inode
*inode
,
2419 struct buffer_head
*di_bh
,
2423 struct ocfs2_alloc_context
**meta_ac
)
2425 int ret
, ref_blocks
= 0;
2426 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
2427 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
2428 struct buffer_head
*ref_root_bh
= NULL
;
2429 struct ocfs2_refcount_tree
*tree
;
2430 u64 start_cpos
= ocfs2_blocks_to_clusters(inode
->i_sb
, phys_blkno
);
2432 if (!ocfs2_refcount_tree(OCFS2_SB(inode
->i_sb
))) {
2433 ocfs2_error(inode
->i_sb
, "Inode %lu want to use refcount "
2434 "tree, but the feature bit is not set in the "
2435 "super block.", inode
->i_ino
);
2440 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
2442 ret
= ocfs2_get_refcount_tree(OCFS2_SB(inode
->i_sb
),
2443 le64_to_cpu(di
->i_refcount_loc
), &tree
);
2449 ret
= ocfs2_read_refcount_block(&tree
->rf_ci
,
2450 le64_to_cpu(di
->i_refcount_loc
),
2457 ret
= ocfs2_calc_refcount_meta_credits(inode
->i_sb
,
2460 start_cpos
, clusters
,
2461 &ref_blocks
, credits
);
2467 mlog(0, "reserve new metadata %d, credits = %d\n",
2468 ref_blocks
, *credits
);
2471 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
2472 ref_blocks
, meta_ac
);
2478 brelse(ref_root_bh
);
2482 #define MAX_CONTIG_BYTES 1048576
2484 static inline unsigned int ocfs2_cow_contig_clusters(struct super_block
*sb
)
2486 return ocfs2_clusters_for_bytes(sb
, MAX_CONTIG_BYTES
);
2489 static inline unsigned int ocfs2_cow_contig_mask(struct super_block
*sb
)
2491 return ~(ocfs2_cow_contig_clusters(sb
) - 1);
2495 * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
2496 * find an offset (start + (n * contig_clusters)) that is closest to cpos
2497 * while still being less than or equal to it.
2499 * The goal is to break the extent at a multiple of contig_clusters.
2501 static inline unsigned int ocfs2_cow_align_start(struct super_block
*sb
,
2505 BUG_ON(start
> cpos
);
2507 return start
+ ((cpos
- start
) & ocfs2_cow_contig_mask(sb
));
2511 * Given a cluster count of len, pad it out so that it is a multiple
2512 * of contig_clusters.
2514 static inline unsigned int ocfs2_cow_align_length(struct super_block
*sb
,
2517 unsigned int padded
=
2518 (len
+ (ocfs2_cow_contig_clusters(sb
) - 1)) &
2519 ocfs2_cow_contig_mask(sb
);
2529 * Calculate out the start and number of virtual clusters we need to to CoW.
2531 * cpos is vitual start cluster position we want to do CoW in a
2532 * file and write_len is the cluster length.
2533 * max_cpos is the place where we want to stop CoW intentionally.
2535 * Normal we will start CoW from the beginning of extent record cotaining cpos.
2536 * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
2537 * get good I/O from the resulting extent tree.
2539 static int ocfs2_refcount_cal_cow_clusters(struct inode
*inode
,
2540 struct ocfs2_extent_list
*el
,
2548 int tree_height
= le16_to_cpu(el
->l_tree_depth
), i
;
2549 struct buffer_head
*eb_bh
= NULL
;
2550 struct ocfs2_extent_block
*eb
= NULL
;
2551 struct ocfs2_extent_rec
*rec
;
2552 unsigned int want_clusters
, rec_end
= 0;
2553 int contig_clusters
= ocfs2_cow_contig_clusters(inode
->i_sb
);
2556 BUG_ON(cpos
+ write_len
> max_cpos
);
2558 if (tree_height
> 0) {
2559 ret
= ocfs2_find_leaf(INODE_CACHE(inode
), el
, cpos
, &eb_bh
);
2565 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2568 if (el
->l_tree_depth
) {
2569 ocfs2_error(inode
->i_sb
,
2570 "Inode %lu has non zero tree depth in "
2571 "leaf block %llu\n", inode
->i_ino
,
2572 (unsigned long long)eb_bh
->b_blocknr
);
2579 for (i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
2580 rec
= &el
->l_recs
[i
];
2582 if (ocfs2_is_empty_extent(rec
)) {
2583 mlog_bug_on_msg(i
!= 0, "Inode %lu has empty record in "
2584 "index %d\n", inode
->i_ino
, i
);
2588 if (le32_to_cpu(rec
->e_cpos
) +
2589 le16_to_cpu(rec
->e_leaf_clusters
) <= cpos
)
2592 if (*cow_len
== 0) {
2594 * We should find a refcounted record in the
2597 BUG_ON(!(rec
->e_flags
& OCFS2_EXT_REFCOUNTED
));
2598 *cow_start
= le32_to_cpu(rec
->e_cpos
);
2602 * If we encounter a hole, a non-refcounted record or
2603 * pass the max_cpos, stop the search.
2605 if ((!(rec
->e_flags
& OCFS2_EXT_REFCOUNTED
)) ||
2606 (*cow_len
&& rec_end
!= le32_to_cpu(rec
->e_cpos
)) ||
2607 (max_cpos
<= le32_to_cpu(rec
->e_cpos
)))
2610 leaf_clusters
= le16_to_cpu(rec
->e_leaf_clusters
);
2611 rec_end
= le32_to_cpu(rec
->e_cpos
) + leaf_clusters
;
2612 if (rec_end
> max_cpos
) {
2614 leaf_clusters
= rec_end
- le32_to_cpu(rec
->e_cpos
);
2618 * How many clusters do we actually need from
2619 * this extent? First we see how many we actually
2620 * need to complete the write. If that's smaller
2621 * than contig_clusters, we try for contig_clusters.
2624 want_clusters
= write_len
;
2626 want_clusters
= (cpos
+ write_len
) -
2627 (*cow_start
+ *cow_len
);
2628 if (want_clusters
< contig_clusters
)
2629 want_clusters
= contig_clusters
;
2632 * If the write does not cover the whole extent, we
2633 * need to calculate how we're going to split the extent.
2634 * We try to do it on contig_clusters boundaries.
2636 * Any extent smaller than contig_clusters will be
2637 * CoWed in its entirety.
2639 if (leaf_clusters
<= contig_clusters
)
2640 *cow_len
+= leaf_clusters
;
2641 else if (*cow_len
|| (*cow_start
== cpos
)) {
2643 * This extent needs to be CoW'd from its
2644 * beginning, so all we have to do is compute
2645 * how many clusters to grab. We align
2646 * want_clusters to the edge of contig_clusters
2647 * to get better I/O.
2649 want_clusters
= ocfs2_cow_align_length(inode
->i_sb
,
2652 if (leaf_clusters
< want_clusters
)
2653 *cow_len
+= leaf_clusters
;
2655 *cow_len
+= want_clusters
;
2656 } else if ((*cow_start
+ contig_clusters
) >=
2657 (cpos
+ write_len
)) {
2659 * Breaking off contig_clusters at the front
2660 * of the extent will cover our write. That's
2663 *cow_len
= contig_clusters
;
2664 } else if ((rec_end
- cpos
) <= contig_clusters
) {
2666 * Breaking off contig_clusters at the tail of
2667 * this extent will cover cpos.
2669 *cow_start
= rec_end
- contig_clusters
;
2670 *cow_len
= contig_clusters
;
2671 } else if ((rec_end
- cpos
) <= want_clusters
) {
2673 * While we can't fit the entire write in this
2674 * extent, we know that the write goes from cpos
2675 * to the end of the extent. Break that off.
2676 * We try to break it at some multiple of
2677 * contig_clusters from the front of the extent.
2678 * Failing that (ie, cpos is within
2679 * contig_clusters of the front), we'll CoW the
2682 *cow_start
= ocfs2_cow_align_start(inode
->i_sb
,
2684 *cow_len
= rec_end
- *cow_start
;
2687 * Ok, the entire write lives in the middle of
2688 * this extent. Let's try to slice the extent up
2689 * nicely. Optimally, our CoW region starts at
2690 * m*contig_clusters from the beginning of the
2691 * extent and goes for n*contig_clusters,
2692 * covering the entire write.
2694 *cow_start
= ocfs2_cow_align_start(inode
->i_sb
,
2697 want_clusters
= (cpos
+ write_len
) - *cow_start
;
2698 want_clusters
= ocfs2_cow_align_length(inode
->i_sb
,
2700 if (*cow_start
+ want_clusters
<= rec_end
)
2701 *cow_len
= want_clusters
;
2703 *cow_len
= rec_end
- *cow_start
;
2706 /* Have we covered our entire write yet? */
2707 if ((*cow_start
+ *cow_len
) >= (cpos
+ write_len
))
2711 * If we reach the end of the extent block and don't get enough
2712 * clusters, continue with the next extent block if possible.
2714 if (i
+ 1 == le16_to_cpu(el
->l_next_free_rec
) &&
2715 eb
&& eb
->h_next_leaf_blk
) {
2719 ret
= ocfs2_read_extent_block(INODE_CACHE(inode
),
2720 le64_to_cpu(eb
->h_next_leaf_blk
),
2727 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
2739 * Prepare meta_ac, data_ac and calculate credits when we want to add some
2740 * num_clusters in data_tree "et" and change the refcount for the old
2741 * clusters(starting form p_cluster) in the refcount tree.
2744 * 1. since we may split the old tree, so we at most will need num_clusters + 2
2745 * more new leaf records.
2746 * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
2747 * just give data_ac = NULL.
2749 static int ocfs2_lock_refcount_allocators(struct super_block
*sb
,
2750 u32 p_cluster
, u32 num_clusters
,
2751 struct ocfs2_extent_tree
*et
,
2752 struct ocfs2_caching_info
*ref_ci
,
2753 struct buffer_head
*ref_root_bh
,
2754 struct ocfs2_alloc_context
**meta_ac
,
2755 struct ocfs2_alloc_context
**data_ac
,
2758 int ret
= 0, meta_add
= 0;
2759 int num_free_extents
= ocfs2_num_free_extents(OCFS2_SB(sb
), et
);
2761 if (num_free_extents
< 0) {
2762 ret
= num_free_extents
;
2767 if (num_free_extents
< num_clusters
+ 2)
2769 ocfs2_extend_meta_needed(et
->et_root_el
);
2771 *credits
+= ocfs2_calc_extend_credits(sb
, et
->et_root_el
,
2774 ret
= ocfs2_calc_refcount_meta_credits(sb
, ref_ci
, ref_root_bh
,
2775 p_cluster
, num_clusters
,
2776 &meta_add
, credits
);
2782 mlog(0, "reserve new metadata %d, clusters %u, credits = %d\n",
2783 meta_add
, num_clusters
, *credits
);
2784 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb
), meta_add
,
2792 ret
= ocfs2_reserve_clusters(OCFS2_SB(sb
), num_clusters
,
2801 ocfs2_free_alloc_context(*meta_ac
);
2809 static int ocfs2_clear_cow_buffer(handle_t
*handle
, struct buffer_head
*bh
)
2811 BUG_ON(buffer_dirty(bh
));
2813 clear_buffer_mapped(bh
);
2818 static int ocfs2_duplicate_clusters_by_page(handle_t
*handle
,
2819 struct ocfs2_cow_context
*context
,
2820 u32 cpos
, u32 old_cluster
,
2821 u32 new_cluster
, u32 new_len
)
2823 int ret
= 0, partial
;
2824 struct ocfs2_caching_info
*ci
= context
->data_et
.et_ci
;
2825 struct super_block
*sb
= ocfs2_metadata_cache_get_super(ci
);
2826 u64 new_block
= ocfs2_clusters_to_blocks(sb
, new_cluster
);
2829 unsigned int from
, to
;
2830 loff_t offset
, end
, map_end
;
2831 struct address_space
*mapping
= context
->inode
->i_mapping
;
2833 mlog(0, "old_cluster %u, new %u, len %u at offset %u\n", old_cluster
,
2834 new_cluster
, new_len
, cpos
);
2836 offset
= ((loff_t
)cpos
) << OCFS2_SB(sb
)->s_clustersize_bits
;
2837 end
= offset
+ (new_len
<< OCFS2_SB(sb
)->s_clustersize_bits
);
2839 while (offset
< end
) {
2840 page_index
= offset
>> PAGE_CACHE_SHIFT
;
2841 map_end
= (page_index
+ 1) << PAGE_CACHE_SHIFT
;
2845 /* from, to is the offset within the page. */
2846 from
= offset
& (PAGE_CACHE_SIZE
- 1);
2847 to
= PAGE_CACHE_SIZE
;
2848 if (map_end
& (PAGE_CACHE_SIZE
- 1))
2849 to
= map_end
& (PAGE_CACHE_SIZE
- 1);
2851 page
= grab_cache_page(mapping
, page_index
);
2853 /* This page can't be dirtied before we CoW it out. */
2854 BUG_ON(PageDirty(page
));
2856 if (!PageUptodate(page
)) {
2857 ret
= block_read_full_page(page
, ocfs2_get_block
);
2865 if (page_has_buffers(page
)) {
2866 ret
= walk_page_buffers(handle
, page_buffers(page
),
2868 ocfs2_clear_cow_buffer
);
2875 ocfs2_map_and_dirty_page(context
->inode
,
2877 page
, 0, &new_block
);
2878 mark_page_accessed(page
);
2881 page_cache_release(page
);
2891 static int ocfs2_duplicate_clusters_by_jbd(handle_t
*handle
,
2892 struct ocfs2_cow_context
*context
,
2893 u32 cpos
, u32 old_cluster
,
2894 u32 new_cluster
, u32 new_len
)
2897 struct super_block
*sb
= context
->inode
->i_sb
;
2898 struct ocfs2_caching_info
*ci
= context
->data_et
.et_ci
;
2899 int i
, blocks
= ocfs2_clusters_to_blocks(sb
, new_len
);
2900 u64 old_block
= ocfs2_clusters_to_blocks(sb
, old_cluster
);
2901 u64 new_block
= ocfs2_clusters_to_blocks(sb
, new_cluster
);
2902 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
2903 struct buffer_head
*old_bh
= NULL
;
2904 struct buffer_head
*new_bh
= NULL
;
2906 mlog(0, "old_cluster %u, new %u, len %u\n", old_cluster
,
2907 new_cluster
, new_len
);
2909 for (i
= 0; i
< blocks
; i
++, old_block
++, new_block
++) {
2910 new_bh
= sb_getblk(osb
->sb
, new_block
);
2911 if (new_bh
== NULL
) {
2917 ocfs2_set_new_buffer_uptodate(ci
, new_bh
);
2919 ret
= ocfs2_read_block(ci
, old_block
, &old_bh
, NULL
);
2925 ret
= ocfs2_journal_access(handle
, ci
, new_bh
,
2926 OCFS2_JOURNAL_ACCESS_CREATE
);
2932 memcpy(new_bh
->b_data
, old_bh
->b_data
, sb
->s_blocksize
);
2933 ret
= ocfs2_journal_dirty(handle
, new_bh
);
2950 static int ocfs2_clear_ext_refcount(handle_t
*handle
,
2951 struct ocfs2_extent_tree
*et
,
2952 u32 cpos
, u32 p_cluster
, u32 len
,
2953 unsigned int ext_flags
,
2954 struct ocfs2_alloc_context
*meta_ac
,
2955 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2958 struct ocfs2_extent_rec replace_rec
;
2959 struct ocfs2_path
*path
= NULL
;
2960 struct ocfs2_extent_list
*el
;
2961 struct super_block
*sb
= ocfs2_metadata_cache_get_super(et
->et_ci
);
2962 u64 ino
= ocfs2_metadata_cache_owner(et
->et_ci
);
2964 mlog(0, "inode %llu cpos %u, len %u, p_cluster %u, ext_flags %u\n",
2965 (unsigned long long)ino
, cpos
, len
, p_cluster
, ext_flags
);
2967 memset(&replace_rec
, 0, sizeof(replace_rec
));
2968 replace_rec
.e_cpos
= cpu_to_le32(cpos
);
2969 replace_rec
.e_leaf_clusters
= cpu_to_le16(len
);
2970 replace_rec
.e_blkno
= cpu_to_le64(ocfs2_clusters_to_blocks(sb
,
2972 replace_rec
.e_flags
= ext_flags
;
2973 replace_rec
.e_flags
&= ~OCFS2_EXT_REFCOUNTED
;
2975 path
= ocfs2_new_path_from_et(et
);
2982 ret
= ocfs2_find_path(et
->et_ci
, path
, cpos
);
2988 el
= path_leaf_el(path
);
2990 index
= ocfs2_search_extent_list(el
, cpos
);
2991 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
2993 "Inode %llu has an extent at cpos %u which can no "
2994 "longer be found.\n",
2995 (unsigned long long)ino
, cpos
);
3000 ret
= ocfs2_split_extent(handle
, et
, path
, index
,
3001 &replace_rec
, meta_ac
, dealloc
);
3006 ocfs2_free_path(path
);
3010 static int ocfs2_replace_clusters(handle_t
*handle
,
3011 struct ocfs2_cow_context
*context
,
3014 unsigned int ext_flags
)
3017 struct ocfs2_caching_info
*ci
= context
->data_et
.et_ci
;
3018 u64 ino
= ocfs2_metadata_cache_owner(ci
);
3020 mlog(0, "inode %llu, cpos %u, old %u, new %u, len %u, ext_flags %u\n",
3021 (unsigned long long)ino
, cpos
, old
, new, len
, ext_flags
);
3023 /*If the old clusters is unwritten, no need to duplicate. */
3024 if (!(ext_flags
& OCFS2_EXT_UNWRITTEN
)) {
3025 ret
= context
->cow_duplicate_clusters(handle
, context
, cpos
,
3033 ret
= ocfs2_clear_ext_refcount(handle
, &context
->data_et
,
3034 cpos
, new, len
, ext_flags
,
3035 context
->meta_ac
, &context
->dealloc
);
3042 static int ocfs2_cow_sync_writeback(struct super_block
*sb
,
3043 struct ocfs2_cow_context
*context
,
3044 u32 cpos
, u32 num_clusters
)
3047 loff_t offset
, end
, map_end
;
3051 if (ocfs2_should_order_data(context
->inode
))
3054 offset
= ((loff_t
)cpos
) << OCFS2_SB(sb
)->s_clustersize_bits
;
3055 end
= offset
+ (num_clusters
<< OCFS2_SB(sb
)->s_clustersize_bits
);
3057 ret
= filemap_fdatawrite_range(context
->inode
->i_mapping
,
3064 while (offset
< end
) {
3065 page_index
= offset
>> PAGE_CACHE_SHIFT
;
3066 map_end
= (page_index
+ 1) << PAGE_CACHE_SHIFT
;
3070 page
= grab_cache_page(context
->inode
->i_mapping
, page_index
);
3073 wait_on_page_writeback(page
);
3074 if (PageError(page
)) {
3078 mark_page_accessed(page
);
3081 page_cache_release(page
);
3091 static int ocfs2_di_get_clusters(struct ocfs2_cow_context
*context
,
3092 u32 v_cluster
, u32
*p_cluster
,
3094 unsigned int *extent_flags
)
3096 return ocfs2_get_clusters(context
->inode
, v_cluster
, p_cluster
,
3097 num_clusters
, extent_flags
);
3100 static int ocfs2_make_clusters_writable(struct super_block
*sb
,
3101 struct ocfs2_cow_context
*context
,
3102 u32 cpos
, u32 p_cluster
,
3103 u32 num_clusters
, unsigned int e_flags
)
3105 int ret
, delete, index
, credits
= 0;
3106 u32 new_bit
, new_len
;
3107 unsigned int set_len
;
3108 struct ocfs2_super
*osb
= OCFS2_SB(sb
);
3110 struct buffer_head
*ref_leaf_bh
= NULL
;
3111 struct ocfs2_caching_info
*ref_ci
= &context
->ref_tree
->rf_ci
;
3112 struct ocfs2_refcount_rec rec
;
3114 mlog(0, "cpos %u, p_cluster %u, num_clusters %u, e_flags %u\n",
3115 cpos
, p_cluster
, num_clusters
, e_flags
);
3117 ret
= ocfs2_lock_refcount_allocators(sb
, p_cluster
, num_clusters
,
3120 context
->ref_root_bh
,
3122 &context
->data_ac
, &credits
);
3128 if (context
->post_refcount
)
3129 credits
+= context
->post_refcount
->credits
;
3131 credits
+= context
->extra_credits
;
3132 handle
= ocfs2_start_trans(osb
, credits
);
3133 if (IS_ERR(handle
)) {
3134 ret
= PTR_ERR(handle
);
3139 while (num_clusters
) {
3140 ret
= ocfs2_get_refcount_rec(ref_ci
, context
->ref_root_bh
,
3141 p_cluster
, num_clusters
,
3142 &rec
, &index
, &ref_leaf_bh
);
3148 BUG_ON(!rec
.r_refcount
);
3149 set_len
= min((u64
)p_cluster
+ num_clusters
,
3150 le64_to_cpu(rec
.r_cpos
) +
3151 le32_to_cpu(rec
.r_clusters
)) - p_cluster
;
3154 * There are many different situation here.
3155 * 1. If refcount == 1, remove the flag and don't COW.
3156 * 2. If refcount > 1, allocate clusters.
3157 * Here we may not allocate r_len once at a time, so continue
3158 * until we reach num_clusters.
3160 if (le32_to_cpu(rec
.r_refcount
) == 1) {
3162 ret
= ocfs2_clear_ext_refcount(handle
,
3175 ret
= __ocfs2_claim_clusters(osb
, handle
,
3178 &new_bit
, &new_len
);
3184 ret
= ocfs2_replace_clusters(handle
, context
,
3185 cpos
, p_cluster
, new_bit
,
3194 ret
= __ocfs2_decrease_refcount(handle
, ref_ci
,
3195 context
->ref_root_bh
,
3198 &context
->dealloc
, delete);
3205 p_cluster
+= set_len
;
3206 num_clusters
-= set_len
;
3207 brelse(ref_leaf_bh
);
3211 /* handle any post_cow action. */
3212 if (context
->post_refcount
&& context
->post_refcount
->func
) {
3213 ret
= context
->post_refcount
->func(context
->inode
, handle
,
3214 context
->post_refcount
->para
);
3222 * Here we should write the new page out first if we are
3223 * in write-back mode.
3225 if (context
->get_clusters
== ocfs2_di_get_clusters
) {
3226 ret
= ocfs2_cow_sync_writeback(sb
, context
, cpos
, num_clusters
);
3232 ocfs2_commit_trans(osb
, handle
);
3235 if (context
->data_ac
) {
3236 ocfs2_free_alloc_context(context
->data_ac
);
3237 context
->data_ac
= NULL
;
3239 if (context
->meta_ac
) {
3240 ocfs2_free_alloc_context(context
->meta_ac
);
3241 context
->meta_ac
= NULL
;
3243 brelse(ref_leaf_bh
);
3248 static int ocfs2_replace_cow(struct ocfs2_cow_context
*context
)
3251 struct inode
*inode
= context
->inode
;
3252 u32 cow_start
= context
->cow_start
, cow_len
= context
->cow_len
;
3253 u32 p_cluster
, num_clusters
;
3254 unsigned int ext_flags
;
3255 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3257 if (!ocfs2_refcount_tree(OCFS2_SB(inode
->i_sb
))) {
3258 ocfs2_error(inode
->i_sb
, "Inode %lu want to use refcount "
3259 "tree, but the feature bit is not set in the "
3260 "super block.", inode
->i_ino
);
3264 ocfs2_init_dealloc_ctxt(&context
->dealloc
);
3267 ret
= context
->get_clusters(context
, cow_start
, &p_cluster
,
3268 &num_clusters
, &ext_flags
);
3274 BUG_ON(!(ext_flags
& OCFS2_EXT_REFCOUNTED
));
3276 if (cow_len
< num_clusters
)
3277 num_clusters
= cow_len
;
3279 ret
= ocfs2_make_clusters_writable(inode
->i_sb
, context
,
3280 cow_start
, p_cluster
,
3281 num_clusters
, ext_flags
);
3287 cow_len
-= num_clusters
;
3288 cow_start
+= num_clusters
;
3291 if (ocfs2_dealloc_has_cluster(&context
->dealloc
)) {
3292 ocfs2_schedule_truncate_log_flush(osb
, 1);
3293 ocfs2_run_deallocs(osb
, &context
->dealloc
);
3300 * Starting at cpos, try to CoW write_len clusters. Don't CoW
3301 * past max_cpos. This will stop when it runs into a hole or an
3302 * unrefcounted extent.
3304 static int ocfs2_refcount_cow_hunk(struct inode
*inode
,
3305 struct buffer_head
*di_bh
,
3306 u32 cpos
, u32 write_len
, u32 max_cpos
)
3309 u32 cow_start
= 0, cow_len
= 0;
3310 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
3311 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3312 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3313 struct buffer_head
*ref_root_bh
= NULL
;
3314 struct ocfs2_refcount_tree
*ref_tree
;
3315 struct ocfs2_cow_context
*context
= NULL
;
3317 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
3319 ret
= ocfs2_refcount_cal_cow_clusters(inode
, &di
->id2
.i_list
,
3320 cpos
, write_len
, max_cpos
,
3321 &cow_start
, &cow_len
);
3327 mlog(0, "CoW inode %lu, cpos %u, write_len %u, cow_start %u, "
3328 "cow_len %u\n", inode
->i_ino
,
3329 cpos
, write_len
, cow_start
, cow_len
);
3331 BUG_ON(cow_len
== 0);
3333 context
= kzalloc(sizeof(struct ocfs2_cow_context
), GFP_NOFS
);
3340 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
3341 1, &ref_tree
, &ref_root_bh
);
3347 context
->inode
= inode
;
3348 context
->cow_start
= cow_start
;
3349 context
->cow_len
= cow_len
;
3350 context
->ref_tree
= ref_tree
;
3351 context
->ref_root_bh
= ref_root_bh
;
3352 context
->cow_duplicate_clusters
= ocfs2_duplicate_clusters_by_page
;
3353 context
->get_clusters
= ocfs2_di_get_clusters
;
3355 ocfs2_init_dinode_extent_tree(&context
->data_et
,
3356 INODE_CACHE(inode
), di_bh
);
3358 ret
= ocfs2_replace_cow(context
);
3363 * truncate the extent map here since no matter whether we meet with
3364 * any error during the action, we shouldn't trust cached extent map
3367 ocfs2_extent_map_trunc(inode
, cow_start
);
3369 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3370 brelse(ref_root_bh
);
3377 * CoW any and all clusters between cpos and cpos+write_len.
3378 * Don't CoW past max_cpos. If this returns successfully, all
3379 * clusters between cpos and cpos+write_len are safe to modify.
3381 int ocfs2_refcount_cow(struct inode
*inode
,
3382 struct buffer_head
*di_bh
,
3383 u32 cpos
, u32 write_len
, u32 max_cpos
)
3386 u32 p_cluster
, num_clusters
;
3387 unsigned int ext_flags
;
3390 ret
= ocfs2_get_clusters(inode
, cpos
, &p_cluster
,
3391 &num_clusters
, &ext_flags
);
3397 if (write_len
< num_clusters
)
3398 num_clusters
= write_len
;
3400 if (ext_flags
& OCFS2_EXT_REFCOUNTED
) {
3401 ret
= ocfs2_refcount_cow_hunk(inode
, di_bh
, cpos
,
3402 num_clusters
, max_cpos
);
3409 write_len
-= num_clusters
;
3410 cpos
+= num_clusters
;
3416 static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context
*context
,
3417 u32 v_cluster
, u32
*p_cluster
,
3419 unsigned int *extent_flags
)
3421 struct inode
*inode
= context
->inode
;
3422 struct ocfs2_xattr_value_root
*xv
= context
->cow_object
;
3424 return ocfs2_xattr_get_clusters(inode
, v_cluster
, p_cluster
,
3425 num_clusters
, &xv
->xr_list
,
3430 * Given a xattr value root, calculate the most meta/credits we need for
3431 * refcount tree change if we truncate it to 0.
3433 int ocfs2_refcounted_xattr_delete_need(struct inode
*inode
,
3434 struct ocfs2_caching_info
*ref_ci
,
3435 struct buffer_head
*ref_root_bh
,
3436 struct ocfs2_xattr_value_root
*xv
,
3437 int *meta_add
, int *credits
)
3439 int ret
= 0, index
, ref_blocks
= 0;
3440 u32 p_cluster
, num_clusters
;
3441 u32 cpos
= 0, clusters
= le32_to_cpu(xv
->xr_clusters
);
3442 struct ocfs2_refcount_block
*rb
;
3443 struct ocfs2_refcount_rec rec
;
3444 struct buffer_head
*ref_leaf_bh
= NULL
;
3446 while (cpos
< clusters
) {
3447 ret
= ocfs2_xattr_get_clusters(inode
, cpos
, &p_cluster
,
3448 &num_clusters
, &xv
->xr_list
,
3455 cpos
+= num_clusters
;
3457 while (num_clusters
) {
3458 ret
= ocfs2_get_refcount_rec(ref_ci
, ref_root_bh
,
3459 p_cluster
, num_clusters
,
3467 BUG_ON(!rec
.r_refcount
);
3469 rb
= (struct ocfs2_refcount_block
*)ref_leaf_bh
->b_data
;
3472 * We really don't know whether the other clusters is in
3473 * this refcount block or not, so just take the worst
3474 * case that all the clusters are in this block and each
3475 * one will split a refcount rec, so totally we need
3476 * clusters * 2 new refcount rec.
3478 if (le64_to_cpu(rb
->rf_records
.rl_used
) + clusters
* 2 >
3479 le16_to_cpu(rb
->rf_records
.rl_count
))
3483 brelse(ref_leaf_bh
);
3486 if (num_clusters
<= le32_to_cpu(rec
.r_clusters
))
3489 num_clusters
-= le32_to_cpu(rec
.r_clusters
);
3490 p_cluster
+= num_clusters
;
3494 *meta_add
+= ref_blocks
;
3498 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
3499 if (le32_to_cpu(rb
->rf_flags
) & OCFS2_REFCOUNT_TREE_FL
)
3500 *credits
+= OCFS2_EXPAND_REFCOUNT_TREE_CREDITS
;
3502 struct ocfs2_extent_tree et
;
3504 ocfs2_init_refcount_extent_tree(&et
, ref_ci
, ref_root_bh
);
3505 *credits
+= ocfs2_calc_extend_credits(inode
->i_sb
,
3511 brelse(ref_leaf_bh
);
3518 int ocfs2_refcount_cow_xattr(struct inode
*inode
,
3519 struct ocfs2_dinode
*di
,
3520 struct ocfs2_xattr_value_buf
*vb
,
3521 struct ocfs2_refcount_tree
*ref_tree
,
3522 struct buffer_head
*ref_root_bh
,
3523 u32 cpos
, u32 write_len
,
3524 struct ocfs2_post_refcount
*post
)
3527 struct ocfs2_xattr_value_root
*xv
= vb
->vb_xv
;
3528 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
3529 struct ocfs2_cow_context
*context
= NULL
;
3530 u32 cow_start
, cow_len
;
3532 BUG_ON(!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
));
3534 ret
= ocfs2_refcount_cal_cow_clusters(inode
, &xv
->xr_list
,
3535 cpos
, write_len
, UINT_MAX
,
3536 &cow_start
, &cow_len
);
3542 BUG_ON(cow_len
== 0);
3544 context
= kzalloc(sizeof(struct ocfs2_cow_context
), GFP_NOFS
);
3551 context
->inode
= inode
;
3552 context
->cow_start
= cow_start
;
3553 context
->cow_len
= cow_len
;
3554 context
->ref_tree
= ref_tree
;
3555 context
->ref_root_bh
= ref_root_bh
;;
3556 context
->cow_object
= xv
;
3558 context
->cow_duplicate_clusters
= ocfs2_duplicate_clusters_by_jbd
;
3559 /* We need the extra credits for duplicate_clusters by jbd. */
3560 context
->extra_credits
=
3561 ocfs2_clusters_to_blocks(inode
->i_sb
, 1) * cow_len
;
3562 context
->get_clusters
= ocfs2_xattr_value_get_clusters
;
3563 context
->post_refcount
= post
;
3565 ocfs2_init_xattr_value_extent_tree(&context
->data_et
,
3566 INODE_CACHE(inode
), vb
);
3568 ret
= ocfs2_replace_cow(context
);
3578 * Insert a new extent into refcount tree and mark a extent rec
3579 * as refcounted in the dinode tree.
3581 int ocfs2_add_refcount_flag(struct inode
*inode
,
3582 struct ocfs2_extent_tree
*data_et
,
3583 struct ocfs2_caching_info
*ref_ci
,
3584 struct buffer_head
*ref_root_bh
,
3585 u32 cpos
, u32 p_cluster
, u32 num_clusters
,
3586 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3587 struct ocfs2_post_refcount
*post
)
3591 int credits
= 1, ref_blocks
= 0;
3592 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3593 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3595 ret
= ocfs2_calc_refcount_meta_credits(inode
->i_sb
,
3596 ref_ci
, ref_root_bh
,
3597 p_cluster
, num_clusters
,
3598 &ref_blocks
, &credits
);
3604 mlog(0, "reserve new metadata %d, credits = %d\n",
3605 ref_blocks
, credits
);
3608 ret
= ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode
->i_sb
),
3609 ref_blocks
, &meta_ac
);
3617 credits
+= post
->credits
;
3619 handle
= ocfs2_start_trans(osb
, credits
);
3620 if (IS_ERR(handle
)) {
3621 ret
= PTR_ERR(handle
);
3626 ret
= ocfs2_mark_extent_refcounted(inode
, data_et
, handle
,
3627 cpos
, num_clusters
, p_cluster
,
3634 ret
= ocfs2_increase_refcount(handle
, ref_ci
, ref_root_bh
,
3635 p_cluster
, num_clusters
,
3642 if (post
&& post
->func
) {
3643 ret
= post
->func(inode
, handle
, post
->para
);
3649 ocfs2_commit_trans(osb
, handle
);
3652 ocfs2_free_alloc_context(meta_ac
);
3656 static int ocfs2_change_ctime(struct inode
*inode
,
3657 struct buffer_head
*di_bh
)
3661 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3663 handle
= ocfs2_start_trans(OCFS2_SB(inode
->i_sb
),
3664 OCFS2_INODE_UPDATE_CREDITS
);
3665 if (IS_ERR(handle
)) {
3666 ret
= PTR_ERR(handle
);
3671 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(inode
), di_bh
,
3672 OCFS2_JOURNAL_ACCESS_WRITE
);
3678 inode
->i_ctime
= CURRENT_TIME
;
3679 di
->i_ctime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
3680 di
->i_ctime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
3682 ocfs2_journal_dirty(handle
, di_bh
);
3685 ocfs2_commit_trans(OCFS2_SB(inode
->i_sb
), handle
);
3690 static int ocfs2_attach_refcount_tree(struct inode
*inode
,
3691 struct buffer_head
*di_bh
)
3693 int ret
, data_changed
= 0;
3694 struct buffer_head
*ref_root_bh
= NULL
;
3695 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
3696 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
3697 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3698 struct ocfs2_refcount_tree
*ref_tree
;
3699 unsigned int ext_flags
;
3701 u32 cpos
, num_clusters
, clusters
, p_cluster
;
3702 struct ocfs2_cached_dealloc_ctxt dealloc
;
3703 struct ocfs2_extent_tree di_et
;
3705 ocfs2_init_dealloc_ctxt(&dealloc
);
3707 if (!(oi
->ip_dyn_features
& OCFS2_HAS_REFCOUNT_FL
)) {
3708 ret
= ocfs2_create_refcount_tree(inode
, di_bh
);
3715 BUG_ON(!di
->i_refcount_loc
);
3716 ret
= ocfs2_lock_refcount_tree(osb
,
3717 le64_to_cpu(di
->i_refcount_loc
), 1,
3718 &ref_tree
, &ref_root_bh
);
3724 ocfs2_init_dinode_extent_tree(&di_et
, INODE_CACHE(inode
), di_bh
);
3726 size
= i_size_read(inode
);
3727 clusters
= ocfs2_clusters_for_bytes(inode
->i_sb
, size
);
3730 while (cpos
< clusters
) {
3731 ret
= ocfs2_get_clusters(inode
, cpos
, &p_cluster
,
3732 &num_clusters
, &ext_flags
);
3734 if (p_cluster
&& !(ext_flags
& OCFS2_EXT_REFCOUNTED
)) {
3735 ret
= ocfs2_add_refcount_flag(inode
, &di_et
,
3738 p_cluster
, num_clusters
,
3747 cpos
+= num_clusters
;
3750 if (oi
->ip_dyn_features
& OCFS2_HAS_XATTR_FL
) {
3751 ret
= ocfs2_xattr_attach_refcount_tree(inode
, di_bh
,
3762 ret
= ocfs2_change_ctime(inode
, di_bh
);
3768 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3769 brelse(ref_root_bh
);
3771 if (!ret
&& ocfs2_dealloc_has_cluster(&dealloc
)) {
3772 ocfs2_schedule_truncate_log_flush(osb
, 1);
3773 ocfs2_run_deallocs(osb
, &dealloc
);
3777 * Empty the extent map so that we may get the right extent
3778 * record from the disk.
3780 ocfs2_extent_map_trunc(inode
, 0);
3785 static int ocfs2_add_refcounted_extent(struct inode
*inode
,
3786 struct ocfs2_extent_tree
*et
,
3787 struct ocfs2_caching_info
*ref_ci
,
3788 struct buffer_head
*ref_root_bh
,
3789 u32 cpos
, u32 p_cluster
, u32 num_clusters
,
3790 unsigned int ext_flags
,
3791 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3796 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
3797 struct ocfs2_alloc_context
*meta_ac
= NULL
;
3799 ret
= ocfs2_lock_refcount_allocators(inode
->i_sb
,
3800 p_cluster
, num_clusters
,
3802 ref_root_bh
, &meta_ac
,
3809 handle
= ocfs2_start_trans(osb
, credits
);
3810 if (IS_ERR(handle
)) {
3811 ret
= PTR_ERR(handle
);
3816 ret
= ocfs2_insert_extent(handle
, et
, cpos
,
3817 cpu_to_le64(ocfs2_clusters_to_blocks(inode
->i_sb
,
3819 num_clusters
, ext_flags
, meta_ac
);
3825 ret
= ocfs2_increase_refcount(handle
, ref_ci
, ref_root_bh
,
3826 p_cluster
, num_clusters
,
3832 ocfs2_commit_trans(osb
, handle
);
3835 ocfs2_free_alloc_context(meta_ac
);
3839 static int ocfs2_duplicate_extent_list(struct inode
*s_inode
,
3840 struct inode
*t_inode
,
3841 struct buffer_head
*t_bh
,
3842 struct ocfs2_caching_info
*ref_ci
,
3843 struct buffer_head
*ref_root_bh
,
3844 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
3847 u32 p_cluster
, num_clusters
, clusters
, cpos
;
3849 unsigned int ext_flags
;
3850 struct ocfs2_extent_tree et
;
3852 ocfs2_init_dinode_extent_tree(&et
, INODE_CACHE(t_inode
), t_bh
);
3854 size
= i_size_read(s_inode
);
3855 clusters
= ocfs2_clusters_for_bytes(s_inode
->i_sb
, size
);
3858 while (cpos
< clusters
) {
3859 ret
= ocfs2_get_clusters(s_inode
, cpos
, &p_cluster
,
3860 &num_clusters
, &ext_flags
);
3863 ret
= ocfs2_add_refcounted_extent(t_inode
, &et
,
3864 ref_ci
, ref_root_bh
,
3875 cpos
+= num_clusters
;
3883 * change the new file's attributes to the src.
3885 * reflink creates a snapshot of a file, that means the attributes
3886 * must be identical except for three exceptions - nlink, ino, and ctime.
3888 static int ocfs2_complete_reflink(struct inode
*s_inode
,
3889 struct buffer_head
*s_bh
,
3890 struct inode
*t_inode
,
3891 struct buffer_head
*t_bh
)
3895 struct ocfs2_dinode
*s_di
= (struct ocfs2_dinode
*)s_bh
->b_data
;
3896 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)t_bh
->b_data
;
3897 loff_t size
= i_size_read(s_inode
);
3899 handle
= ocfs2_start_trans(OCFS2_SB(t_inode
->i_sb
),
3900 OCFS2_INODE_UPDATE_CREDITS
);
3901 if (IS_ERR(handle
)) {
3902 ret
= PTR_ERR(handle
);
3907 ret
= ocfs2_journal_access_di(handle
, INODE_CACHE(t_inode
), t_bh
,
3908 OCFS2_JOURNAL_ACCESS_WRITE
);
3914 spin_lock(&OCFS2_I(t_inode
)->ip_lock
);
3915 OCFS2_I(t_inode
)->ip_clusters
= OCFS2_I(s_inode
)->ip_clusters
;
3916 OCFS2_I(t_inode
)->ip_attr
= OCFS2_I(s_inode
)->ip_attr
;
3917 OCFS2_I(t_inode
)->ip_dyn_features
= OCFS2_I(s_inode
)->ip_dyn_features
;
3918 spin_unlock(&OCFS2_I(t_inode
)->ip_lock
);
3919 i_size_write(t_inode
, size
);
3921 di
->i_xattr_inline_size
= s_di
->i_xattr_inline_size
;
3922 di
->i_clusters
= s_di
->i_clusters
;
3923 di
->i_size
= s_di
->i_size
;
3924 di
->i_dyn_features
= s_di
->i_dyn_features
;
3925 di
->i_attr
= s_di
->i_attr
;
3926 di
->i_uid
= s_di
->i_uid
;
3927 di
->i_gid
= s_di
->i_gid
;
3928 di
->i_mode
= s_di
->i_mode
;
3932 * we want mtime to appear identical to the source and update ctime.
3934 t_inode
->i_ctime
= CURRENT_TIME
;
3936 di
->i_ctime
= cpu_to_le64(t_inode
->i_ctime
.tv_sec
);
3937 di
->i_ctime_nsec
= cpu_to_le32(t_inode
->i_ctime
.tv_nsec
);
3939 t_inode
->i_mtime
= s_inode
->i_mtime
;
3940 di
->i_mtime
= s_di
->i_mtime
;
3941 di
->i_mtime_nsec
= s_di
->i_mtime_nsec
;
3943 ocfs2_journal_dirty(handle
, t_bh
);
3946 ocfs2_commit_trans(OCFS2_SB(t_inode
->i_sb
), handle
);
3950 static int ocfs2_create_reflink_node(struct inode
*s_inode
,
3951 struct buffer_head
*s_bh
,
3952 struct inode
*t_inode
,
3953 struct buffer_head
*t_bh
)
3956 struct buffer_head
*ref_root_bh
= NULL
;
3957 struct ocfs2_cached_dealloc_ctxt dealloc
;
3958 struct ocfs2_super
*osb
= OCFS2_SB(s_inode
->i_sb
);
3959 struct ocfs2_refcount_block
*rb
;
3960 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)s_bh
->b_data
;
3961 struct ocfs2_refcount_tree
*ref_tree
;
3963 ocfs2_init_dealloc_ctxt(&dealloc
);
3965 ret
= ocfs2_set_refcount_tree(t_inode
, t_bh
,
3966 le64_to_cpu(di
->i_refcount_loc
));
3972 ret
= ocfs2_lock_refcount_tree(osb
, le64_to_cpu(di
->i_refcount_loc
),
3973 1, &ref_tree
, &ref_root_bh
);
3978 rb
= (struct ocfs2_refcount_block
*)ref_root_bh
->b_data
;
3980 ret
= ocfs2_duplicate_extent_list(s_inode
, t_inode
, t_bh
,
3981 &ref_tree
->rf_ci
, ref_root_bh
,
3985 goto out_unlock_refcount
;
3988 ret
= ocfs2_complete_reflink(s_inode
, s_bh
, t_inode
, t_bh
);
3992 out_unlock_refcount
:
3993 ocfs2_unlock_refcount_tree(osb
, ref_tree
, 1);
3994 brelse(ref_root_bh
);
3996 if (ocfs2_dealloc_has_cluster(&dealloc
)) {
3997 ocfs2_schedule_truncate_log_flush(osb
, 1);
3998 ocfs2_run_deallocs(osb
, &dealloc
);