40 #include <linux/slab.h>
65 u32 v_cluster,
u32 *p_cluster,
67 unsigned int *extent_flags);
71 u32 new_cluster,
u32 new_len);
80 static int ocfs2_validate_refcount_block(
struct super_block *
sb,
81 struct buffer_head *bh)
87 trace_ocfs2_validate_refcount_block((
unsigned long long)bh->b_blocknr);
89 BUG_ON(!buffer_uptodate(bh));
98 mlog(
ML_ERROR,
"Checksum failed for refcount block %llu\n",
99 (
unsigned long long)bh->b_blocknr);
106 "Refcount block #%llu has bad signature %.*s",
107 (
unsigned long long)bh->b_blocknr, 7,
114 "Refcount block #%llu has an invalid rf_blkno "
116 (
unsigned long long)bh->b_blocknr,
123 "Refcount block #%llu has an invalid "
124 "rf_fs_generation of #%u",
125 (
unsigned long long)bh->b_blocknr,
135 struct buffer_head **bh)
138 struct buffer_head *
tmp = *bh;
140 rc = ocfs2_read_block(ci, rb_blkno, &tmp,
141 ocfs2_validate_refcount_block);
194 .co_owner = ocfs2_refcount_cache_owner,
195 .co_get_super = ocfs2_refcount_cache_get_super,
196 .co_cache_lock = ocfs2_refcount_cache_lock,
197 .co_cache_unlock = ocfs2_refcount_cache_unlock,
198 .co_io_lock = ocfs2_refcount_cache_io_lock,
199 .co_io_unlock = ocfs2_refcount_cache_io_unlock,
223 static void ocfs2_insert_refcount_tree(
struct ocfs2_super *osb,
237 if (rf_blkno < tmp->rf_blkno)
243 mlog(
ML_ERROR,
"Duplicate refcount block %llu found!\n",
244 (
unsigned long long)rf_blkno);
249 rb_link_node(&new->rf_node, parent, p);
262 ocfs2_erase_refcount_tree_from_list_no_lock(
struct ocfs2_super *osb,
270 static void ocfs2_erase_refcount_tree_from_list(
struct ocfs2_super *osb,
274 ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
278 static void ocfs2_kref_remove_refcount_tree(
struct kref *
kref)
283 ocfs2_free_refcount_tree(tree);
295 kref_put(&tree->
rf_getcnt, ocfs2_kref_remove_refcount_tree);
307 static inline void ocfs2_init_refcount_tree_lock(
struct ocfs2_super *osb,
313 rf_blkno, generation);
317 ocfs2_allocate_refcount_tree(
struct ocfs2_super *osb,
u64 rf_blkno)
326 kref_init(&new->rf_getcnt);
327 ocfs2_init_refcount_tree_ci(
new, osb->
sb);
332 static int ocfs2_get_refcount_tree(
struct ocfs2_super *osb,
u64 rf_blkno,
337 struct buffer_head *ref_root_bh =
NULL;
345 tree = ocfs2_find_refcount_tree(osb, rf_blkno);
351 new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
365 ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
375 ocfs2_init_refcount_tree_lock(osb,
new, rf_blkno,
380 tree = ocfs2_find_refcount_tree(osb, rf_blkno);
384 ocfs2_insert_refcount_tree(osb,
new);
397 ocfs2_free_refcount_tree(
new);
403 static int ocfs2_get_refcount_block(
struct inode *
inode,
u64 *ref_blkno)
406 struct buffer_head *di_bh =
NULL;
424 static int __ocfs2_lock_refcount_tree(
struct ocfs2_super *osb,
453 u64 ref_blkno,
int rw,
455 struct buffer_head **ref_bh)
457 int ret, delete_tree = 0;
459 struct buffer_head *ref_root_bh =
NULL;
463 ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
469 ocfs2_refcount_tree_get(tree);
471 ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
474 ocfs2_refcount_tree_put(tree);
478 ret = ocfs2_read_refcount_block(&tree->
rf_ci, tree->
rf_blkno,
483 ocfs2_refcount_tree_put(tree);
499 ocfs2_erase_refcount_tree_from_list(osb, tree);
510 ocfs2_refcount_tree_put(tree);
518 *ref_bh = ref_root_bh;
535 ocfs2_refcount_tree_put(tree);
547 trace_ocfs2_purge_refcount_trees(
548 (
unsigned long long) tree->
rf_blkno);
551 ocfs2_free_refcount_tree(tree);
559 static int ocfs2_create_refcount_tree(
struct inode *inode,
560 struct buffer_head *di_bh)
568 struct buffer_head *new_bh =
NULL;
571 u16 suballoc_bit_start;
573 u64 suballoc_loc, first_blkno;
577 trace_ocfs2_create_refcount_tree(
578 (
unsigned long long)OCFS2_I(inode)->ip_blkno);
587 if (IS_ERR(handle)) {
588 ret = PTR_ERR(handle);
601 &suballoc_bit_start, &num_got,
608 new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
615 new_bh = sb_getblk(inode->
i_sb, first_blkno);
643 spin_lock(&oi->ip_lock);
647 spin_unlock(&oi->ip_lock);
649 trace_ocfs2_create_refcount_tree_blkno((
unsigned long long)first_blkno);
658 ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
662 tree = ocfs2_find_refcount_tree(osb, first_blkno);
672 ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
673 ocfs2_insert_refcount_tree(osb, new_tree);
677 ocfs2_refcount_tree_put(tree);
695 static int ocfs2_set_refcount_tree(
struct inode *inode,
696 struct buffer_head *di_bh,
700 handle_t *handle =
NULL;
704 struct buffer_head *ref_root_bh =
NULL;
711 &ref_tree, &ref_root_bh);
718 if (IS_ERR(handle)) {
719 ret = PTR_ERR(handle);
743 spin_lock(&oi->ip_lock);
747 spin_unlock(&oi->ip_lock);
761 int ret, delete_tree = 0;
762 handle_t *handle =
NULL;
767 struct inode *alloc_inode =
NULL;
768 struct buffer_head *alloc_bh =
NULL;
769 struct buffer_head *blk_bh =
NULL;
797 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
819 if (IS_ERR(handle)) {
820 ret = PTR_ERR(handle);
839 spin_lock(&oi->ip_lock);
843 spin_unlock(&oi->ip_lock);
851 ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
853 alloc_bh, bit, bg_blkno, 1);
873 ocfs2_refcount_tree_put(ref_tree);
880 struct buffer_head *ref_leaf_bh,
881 u64 cpos,
unsigned int len,
929 struct buffer_head *di_bh)
962 struct buffer_head *ref_root_bh,
965 int index,
u32 *cpos_end)
967 int ret,
i, subtree_root;
1037 tmp_el = left_path->
p_node[subtree_root].el;
1038 blkno = left_path->
p_node[subtree_root+1].bh->b_blocknr;
1062 struct buffer_head *ref_root_bh,
1063 u64 cpos,
unsigned int len,
1066 struct buffer_head **ret_bh)
1068 int ret = 0,
i, found;
1073 struct buffer_head *eb_bh =
NULL, *ref_leaf_bh =
NULL;
1079 ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
1081 *ret_bh = ref_root_bh;
1082 get_bh(ref_root_bh);
1101 "refcount tree %llu has non zero tree "
1102 "depth in leaf btree tree block %llu\n",
1104 (
unsigned long long)eb_bh->b_blocknr);
1121 ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh,
1122 eb, el, i, &cpos_end);
1128 if (cpos_end < low_cpos + len)
1129 len = cpos_end - low_cpos;
1139 ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
1141 *ret_bh = ref_leaf_bh;
1158 if ((rb->
rf_records.rl_recs[index].r_refcount ==
1159 rb->
rf_records.rl_recs[index + 1].r_refcount) &&
1175 ret = ocfs2_refcount_rec_adjacent(rb, index);
1180 tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
1199 le32_add_cpu(&rb->
rf_records.rl_recs[index].r_clusters,
1220 ocfs2_refcount_rec_contig(rb, index);
1230 ocfs2_rotate_refcount_rec_left(rb, index);
1233 ocfs2_rotate_refcount_rec_left(rb, index);
1240 static int ocfs2_change_refcount_rec(handle_t *handle,
1242 struct buffer_head *ref_leaf_bh,
1243 int index,
int merge,
int change)
1258 trace_ocfs2_change_refcount_rec(
1261 le32_add_cpu(&rec->r_refcount, change);
1263 if (!rec->r_refcount) {
1272 le16_add_cpu(&rl->rl_used, -1);
1274 ocfs2_refcount_rec_merge(rb, index);
1281 static int ocfs2_expand_inline_ref_root(handle_t *handle,
1283 struct buffer_head *ref_root_bh,
1284 struct buffer_head **ref_leaf_bh,
1288 u16 suballoc_bit_start;
1290 u64 suballoc_loc, blkno;
1292 struct buffer_head *new_bh =
NULL;
1305 &suballoc_bit_start, &num_got,
1312 new_bh = sb_getblk(sb, blkno);
1313 if (new_bh ==
NULL) {
1356 trace_ocfs2_expand_inline_ref_root((
unsigned long long)blkno,
1359 *ref_leaf_bh = new_bh;
1370 ocfs2_get_ref_rec_low_cpos(next))
1376 static int cmp_refcount_rec_by_low_cpos(
const void *
a,
const void *
b)
1379 u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
1380 u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
1382 if (l_cpos > r_cpos)
1384 if (l_cpos < r_cpos)
1389 static int cmp_refcount_rec_by_cpos(
const void *a,
const void *b)
1395 if (l_cpos > r_cpos)
1397 if (l_cpos < r_cpos)
1402 static void swap_refcount_rec(
void *a,
void *b,
int size)
1422 u32 *split_pos,
int *split_index)
1425 int delta, middle = num_used / 2;
1427 for (delta = 0; delta < middle; delta++) {
1429 if (ocfs2_refcount_rec_no_intersect(
1430 &rl->
rl_recs[middle - delta - 1],
1431 &rl->
rl_recs[middle - delta])) {
1432 *split_index = middle -
delta;
1437 if ((middle + delta + 1) == num_used)
1441 if (ocfs2_refcount_rec_no_intersect(
1443 &rl->
rl_recs[middle + delta + 1])) {
1444 *split_index = middle + delta + 1;
1449 if (delta >= middle)
1452 *split_pos = ocfs2_get_ref_rec_low_cpos(&rl->
rl_recs[*split_index]);
1456 static int ocfs2_divide_leaf_refcount_block(
struct buffer_head *ref_leaf_bh,
1457 struct buffer_head *new_bh,
1460 int split_index = 0, num_moved,
ret;
1469 trace_ocfs2_divide_leaf_refcount_block(
1470 (
unsigned long long)ref_leaf_bh->b_blocknr,
1487 cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
1489 ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
1507 le16_add_cpu(&rl->
rl_used, -num_moved);
1512 cmp_refcount_rec_by_cpos, swap_refcount_rec);
1516 cmp_refcount_rec_by_cpos, swap_refcount_rec);
1522 static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1524 struct buffer_head *ref_root_bh,
1525 struct buffer_head *ref_leaf_bh,
1529 u16 suballoc_bit_start;
1530 u32 num_got, new_cpos;
1531 u64 suballoc_loc, blkno;
1535 struct buffer_head *new_bh =
NULL;
1556 &suballoc_bit_start, &num_got,
1563 new_bh = sb_getblk(sb, blkno);
1564 if (new_bh ==
NULL) {
1593 ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
1604 trace_ocfs2_new_leaf_refcount_block(
1605 (
unsigned long long)new_bh->b_blocknr, new_cpos);
1618 static int ocfs2_expand_refcount_tree(handle_t *handle,
1620 struct buffer_head *ref_root_bh,
1621 struct buffer_head *ref_leaf_bh,
1625 struct buffer_head *expand_bh =
NULL;
1627 if (ref_root_bh == ref_leaf_bh) {
1632 ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
1633 &expand_bh, meta_ac);
1639 expand_bh = ref_leaf_bh;
1645 ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
1646 expand_bh, meta_ac);
1660 static int ocfs2_adjust_refcount_rec(handle_t *handle,
1662 struct buffer_head *ref_root_bh,
1663 struct buffer_head *ref_leaf_bh,
1667 u32 new_cpos, old_cpos;
1680 if (old_cpos <= new_cpos)
1744 static int ocfs2_insert_refcount_rec(handle_t *handle,
1746 struct buffer_head *ref_root_bh,
1747 struct buffer_head *ref_leaf_bh,
1749 int index,
int merge,
1756 struct buffer_head *new_bh =
NULL;
1764 ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1765 ref_leaf_bh, meta_ac);
1771 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1772 cpos, len,
NULL, &index,
1779 ref_leaf_bh = new_bh;
1797 trace_ocfs2_insert_refcount_rec(
1798 (
unsigned long long)ref_leaf_bh->b_blocknr, index,
1804 le16_add_cpu(&
rf_list->rl_used, 1);
1807 ocfs2_refcount_rec_merge(rb, index);
1812 ret = ocfs2_adjust_refcount_rec(handle, ci,
1832 static int ocfs2_split_refcount_rec(handle_t *handle,
1834 struct buffer_head *ref_root_bh,
1835 struct buffer_head *ref_leaf_bh,
1837 int index,
int merge,
1848 struct buffer_head *new_bh =
NULL;
1852 trace_ocfs2_split_refcount_rec(
le64_to_cpu(orig_rec->r_cpos),
1865 (split_rec->
r_cpos == orig_rec->r_cpos ||
1878 (split_rec->
r_cpos != orig_rec->r_cpos &&
1890 ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1891 ref_leaf_bh, meta_ac);
1901 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1902 cpos, len, &tmp_rec, &index,
1909 ref_leaf_bh = new_bh;
1912 orig_rec = &
rf_list->rl_recs[index];
1943 tail_rec = &
rf_list->rl_recs[index + recs_need];
1946 le64_add_cpu(&tail_rec->r_cpos,
1960 if (split_rec->
r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
1967 le16_add_cpu(&
rf_list->rl_used, recs_need);
1971 trace_ocfs2_split_refcount_rec_insert(
1972 (
unsigned long long)ref_leaf_bh->b_blocknr, index,
1978 ocfs2_refcount_rec_merge(rb, index);
1988 static int __ocfs2_increase_refcount(handle_t *handle,
1990 struct buffer_head *ref_root_bh,
1991 u64 cpos,
u32 len,
int merge,
1996 struct buffer_head *ref_leaf_bh =
NULL;
1998 unsigned int set_len = 0;
2000 trace_ocfs2_increase_refcount_begin(
2002 (
unsigned long long)cpos, len);
2005 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2006 cpos, len, &rec, &index,
2027 trace_ocfs2_increase_refcount_change(
2028 (
unsigned long long)cpos, set_len,
2030 ret = ocfs2_change_refcount_rec(handle, ci,
2040 trace_ocfs2_increase_refcount_insert(
2043 ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
2052 set_len =
min((
u64)(cpos + len),
2058 trace_ocfs2_increase_refcount_split(
2061 ret = ocfs2_split_refcount_rec(handle, ci,
2062 ref_root_bh, ref_leaf_bh,
2073 brelse(ref_leaf_bh);
2078 brelse(ref_leaf_bh);
2082 static int ocfs2_remove_refcount_extent(handle_t *handle,
2084 struct buffer_head *ref_root_bh,
2085 struct buffer_head *ref_leaf_bh,
2097 trace_ocfs2_remove_refcount_extent(
2099 (
unsigned long long)ref_leaf_bh->b_blocknr,
2104 1, meta_ac, dealloc);
2141 if (!rb->
rf_list.l_next_free_rec) {
2144 trace_ocfs2_restore_refcount_block(
2145 (
unsigned long long)ref_root_bh->b_blocknr);
2164 struct buffer_head *ref_root_bh,
2169 return __ocfs2_increase_refcount(handle, ci, ref_root_bh,
2174 static int ocfs2_decrease_refcount_rec(handle_t *handle,
2176 struct buffer_head *ref_root_bh,
2177 struct buffer_head *ref_leaf_bh,
2178 int index,
u64 cpos,
unsigned int len,
2191 trace_ocfs2_decrease_refcount_rec(
2193 (
unsigned long long)cpos, len);
2197 ret = ocfs2_change_refcount_rec(handle, ci,
2198 ref_leaf_bh, index, 1, -1);
2206 ret = ocfs2_split_refcount_rec(handle, ci,
2207 ref_root_bh, ref_leaf_bh,
2218 if (!rb->
rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
2219 ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
2220 ref_leaf_bh, meta_ac,
2230 static int __ocfs2_decrease_refcount(handle_t *handle,
2232 struct buffer_head *ref_root_bh,
2238 int ret = 0, index = 0;
2240 unsigned int r_count = 0, r_len;
2242 struct buffer_head *ref_leaf_bh =
NULL;
2244 trace_ocfs2_decrease_refcount(
2246 (
unsigned long long)cpos, len,
delete);
2249 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2250 cpos, len, &rec, &index,
2265 ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
2276 ocfs2_clusters_to_blocks(sb, cpos),
2286 brelse(ref_leaf_bh);
2291 brelse(ref_leaf_bh);
2297 handle_t *handle,
u32 cpos,
u32 len,
2305 struct buffer_head *ref_root_bh =
NULL;
2310 ret = ocfs2_get_refcount_block(inode, &ref_blkno);
2316 ret = ocfs2_get_refcount_tree(
OCFS2_SB(inode->
i_sb), ref_blkno, &tree);
2322 ret = ocfs2_read_refcount_block(&tree->
rf_ci, tree->
rf_blkno,
2329 ret = __ocfs2_decrease_refcount(handle, &tree->
rf_ci, ref_root_bh,
2330 cpos, len, meta_ac, dealloc,
delete);
2334 brelse(ref_root_bh);
2347 static int ocfs2_mark_extent_refcounted(
struct inode *inode,
2349 handle_t *handle,
u32 cpos,
2356 trace_ocfs2_mark_extent_refcounted(OCFS2_I(inode)->ip_blkno,
2361 "tree, but the feature bit is not set in the "
2362 "super block.", inode->
i_ino);
2368 len, phys, meta_ac, dealloc,
2381 static int ocfs2_calc_refcount_meta_credits(
struct super_block *sb,
2383 struct buffer_head *ref_root_bh,
2389 int ret = 0,
index, ref_blocks = 0, recs_add = 0;
2390 u64 cpos = start_cpos;
2393 struct buffer_head *ref_leaf_bh =
NULL, *prev_bh =
NULL;
2397 ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2398 cpos, clusters, &rec,
2399 &index, &ref_leaf_bh);
2405 if (ref_leaf_bh != prev_bh) {
2423 prev_bh = ref_leaf_bh;
2429 trace_ocfs2_calc_refcount_meta_credits_iterate(
2430 recs_add, (
unsigned long long)cpos, clusters,
2459 if (cpos == start_cpos &&
2470 brelse(ref_leaf_bh);
2489 *meta_add += ref_blocks;
2490 *credits += ref_blocks;
2504 *meta_add += ocfs2_extend_meta_needed(et.
et_root_el);
2505 *credits += ocfs2_calc_extend_credits(sb,
2515 trace_ocfs2_calc_refcount_meta_credits(
2516 (
unsigned long long)start_cpos, clusters,
2517 *meta_add, *credits);
2518 brelse(ref_leaf_bh);
2544 struct buffer_head *ref_root_bh =
NULL;
2546 u64 start_cpos = ocfs2_blocks_to_clusters(inode->
i_sb, phys_blkno);
2550 "tree, but the feature bit is not set in the "
2551 "super block.", inode->
i_ino);
2559 refcount_loc, &tree);
2565 ret = ocfs2_read_refcount_block(&tree->
rf_ci, refcount_loc,
2572 ret = ocfs2_calc_refcount_meta_credits(inode->
i_sb,
2575 start_cpos, clusters,
2576 ref_blocks, credits);
2582 trace_ocfs2_prepare_refcount_change_for_del(*ref_blocks, *credits);
2585 brelse(ref_root_bh);
2589 #define MAX_CONTIG_BYTES 1048576
2591 static inline unsigned int ocfs2_cow_contig_clusters(
struct super_block *sb)
2596 static inline unsigned int ocfs2_cow_contig_mask(
struct super_block *sb)
2598 return ~(ocfs2_cow_contig_clusters(sb) - 1);
2608 static inline unsigned int ocfs2_cow_align_start(
struct super_block *sb,
2614 return start + ((cpos -
start) & ocfs2_cow_contig_mask(sb));
2621 static inline unsigned int ocfs2_cow_align_length(
struct super_block *sb,
2624 unsigned int padded =
2625 (len + (ocfs2_cow_contig_clusters(sb) - 1)) &
2626 ocfs2_cow_contig_mask(sb);
2646 static int ocfs2_refcount_cal_cow_clusters(
struct inode *inode,
2656 struct buffer_head *eb_bh =
NULL;
2659 unsigned int want_clusters, rec_end = 0;
2660 int contig_clusters = ocfs2_cow_contig_clusters(inode->
i_sb);
2663 BUG_ON(cpos + write_len > max_cpos);
2665 if (tree_height > 0) {
2677 "Inode %lu has non zero tree depth in "
2678 "leaf block %llu\n", inode->
i_ino,
2679 (
unsigned long long)eb_bh->b_blocknr);
2689 if (ocfs2_is_empty_extent(rec)) {
2691 "index %d\n", inode->
i_ino, i);
2699 if (*cow_len == 0) {
2719 if (rec_end > max_cpos) {
2731 want_clusters = write_len;
2733 want_clusters = (cpos + write_len) -
2734 (*cow_start + *cow_len);
2735 if (want_clusters < contig_clusters)
2736 want_clusters = contig_clusters;
2746 if (leaf_clusters <= contig_clusters)
2747 *cow_len += leaf_clusters;
2748 else if (*cow_len || (*cow_start == cpos)) {
2756 want_clusters = ocfs2_cow_align_length(inode->
i_sb,
2759 if (leaf_clusters < want_clusters)
2760 *cow_len += leaf_clusters;
2762 *cow_len += want_clusters;
2763 }
else if ((*cow_start + contig_clusters) >=
2764 (cpos + write_len)) {
2770 *cow_len = contig_clusters;
2771 }
else if ((rec_end - cpos) <= contig_clusters) {
2776 *cow_start = rec_end - contig_clusters;
2777 *cow_len = contig_clusters;
2778 }
else if ((rec_end - cpos) <= want_clusters) {
2789 *cow_start = ocfs2_cow_align_start(inode->
i_sb,
2791 *cow_len = rec_end - *cow_start;
2801 *cow_start = ocfs2_cow_align_start(inode->
i_sb,
2804 want_clusters = (cpos + write_len) - *cow_start;
2805 want_clusters = ocfs2_cow_align_length(inode->
i_sb,
2807 if (*cow_start + want_clusters <= rec_end)
2808 *cow_len = want_clusters;
2810 *cow_len = rec_end - *cow_start;
2814 if ((*cow_start + *cow_len) >= (cpos + write_len))
2856 static int ocfs2_lock_refcount_allocators(
struct super_block *sb,
2857 u32 p_cluster,
u32 num_clusters,
2860 struct buffer_head *ref_root_bh,
2865 int ret = 0, meta_add = 0;
2868 if (num_free_extents < 0) {
2869 ret = num_free_extents;
2874 if (num_free_extents < num_clusters + 2)
2878 *credits += ocfs2_calc_extend_credits(sb, et->
et_root_el,
2881 ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
2882 p_cluster, num_clusters,
2883 &meta_add, credits);
2889 trace_ocfs2_lock_refcount_allocators(meta_add, *credits);
2915 static int ocfs2_clear_cow_buffer(handle_t *handle,
struct buffer_head *bh)
2917 BUG_ON(buffer_dirty(bh));
2919 clear_buffer_mapped(bh);
2926 u32 cpos,
u32 old_cluster,
2927 u32 new_cluster,
u32 new_len)
2929 int ret = 0, partial;
2930 struct inode *inode = file->
f_path.dentry->d_inode;
2933 u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
2936 unsigned int from, to, readahead_pages;
2940 trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster,
2941 new_cluster, new_len);
2944 (ocfs2_cow_contig_clusters(sb) <<
2946 offset = ((loff_t)cpos) <<
OCFS2_SB(sb)->s_clustersize_bits;
2947 end = offset + (new_len <<
OCFS2_SB(sb)->s_clustersize_bits);
2952 if (end > i_size_read(inode))
2953 end = i_size_read(inode);
2955 while (offset < end) {
2976 if (PageReadahead(page)) {
2983 if (!PageUptodate(page)) {
2992 if (page_has_buffers(page)) {
2995 ocfs2_clear_cow_buffer);
3003 page, 0, &new_block);
3019 u32 cpos,
u32 old_cluster,
3020 u32 new_cluster,
u32 new_len)
3023 struct inode *inode = file->
f_path.dentry->d_inode;
3026 int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
3027 u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
3028 u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
3030 struct buffer_head *old_bh =
NULL;
3031 struct buffer_head *new_bh =
NULL;
3033 trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster,
3034 new_cluster, new_len);
3036 for (i = 0; i < blocks; i++, old_block++, new_block++) {
3037 new_bh = sb_getblk(osb->
sb, new_block);
3038 if (new_bh ==
NULL) {
3046 ret = ocfs2_read_block(ci, old_block, &old_bh,
NULL);
3073 static int ocfs2_clear_ext_refcount(handle_t *handle,
3087 trace_ocfs2_clear_ext_refcount((
unsigned long long)ino,
3088 cpos, len, p_cluster, ext_flags);
3090 memset(&replace_rec, 0,
sizeof(replace_rec));
3093 replace_rec.e_blkno =
cpu_to_le64(ocfs2_clusters_to_blocks(sb,
3116 "Inode %llu has an extent at cpos %u which can no "
3117 "longer be found.\n",
3118 (
unsigned long long)ino, cpos);
3124 &replace_rec, meta_ac, dealloc);
3133 static int ocfs2_replace_clusters(handle_t *handle,
3137 unsigned int ext_flags)
3143 trace_ocfs2_replace_clusters((
unsigned long long)ino,
3144 cpos, old,
new, len, ext_flags);
3149 cpos, old,
new, len);
3156 ret = ocfs2_clear_ext_refcount(handle, &context->
data_et,
3157 cpos,
new, len, ext_flags,
3166 struct inode *inode,
3167 u32 cpos,
u32 num_clusters)
3174 if (ocfs2_should_order_data(inode))
3177 offset = ((loff_t)cpos) <<
OCFS2_SB(sb)->s_clustersize_bits;
3178 end = offset + (num_clusters <<
OCFS2_SB(sb)->s_clustersize_bits);
3187 while (offset < end) {
3197 wait_on_page_writeback(page);
3198 if (PageError(page)) {
3216 u32 v_cluster,
u32 *p_cluster,
3218 unsigned int *extent_flags)
3221 num_clusters, extent_flags);
3224 static int ocfs2_make_clusters_writable(
struct super_block *sb,
3227 u32 num_clusters,
unsigned int e_flags)
3229 int ret,
delete,
index, credits = 0;
3230 u32 new_bit, new_len, orig_num_clusters;
3231 unsigned int set_len;
3234 struct buffer_head *ref_leaf_bh =
NULL;
3238 trace_ocfs2_make_clusters_writable(cpos, p_cluster,
3239 num_clusters, e_flags);
3241 ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
3257 if (IS_ERR(handle)) {
3258 ret = PTR_ERR(handle);
3263 orig_num_clusters = num_clusters;
3265 while (num_clusters) {
3266 ret = ocfs2_get_refcount_rec(ref_ci, context->
ref_root_bh,
3267 p_cluster, num_clusters,
3268 &rec, &index, &ref_leaf_bh);
3275 set_len =
min((
u64)p_cluster + num_clusters,
3288 ret = ocfs2_clear_ext_refcount(handle,
3304 &new_bit, &new_len);
3310 ret = ocfs2_replace_clusters(handle, context,
3311 cpos, p_cluster, new_bit,
3320 ret = __ocfs2_decrease_refcount(handle, ref_ci,
3331 p_cluster += set_len;
3332 num_clusters -= set_len;
3333 brelse(ref_leaf_bh);
3370 brelse(ref_leaf_bh);
3378 struct inode *inode = context->
inode;
3380 u32 p_cluster, num_clusters;
3386 "tree, but the feature bit is not set in the "
3387 "super block.", inode->
i_ino);
3391 ocfs2_init_dealloc_ctxt(&context->
dealloc);
3394 ret = context->
get_clusters(context, cow_start, &p_cluster,
3395 &num_clusters, &ext_flags);
3403 if (cow_len < num_clusters)
3404 num_clusters = cow_len;
3406 ret = ocfs2_make_clusters_writable(inode->
i_sb, context,
3407 cow_start, p_cluster,
3408 num_clusters, ext_flags);
3414 cow_len -= num_clusters;
3415 cow_start += num_clusters;
3418 if (ocfs2_dealloc_has_cluster(&context->
dealloc)) {
3426 static void ocfs2_readahead_for_cow(
struct inode *inode,
3433 int cs_bits =
OCFS2_SB(inode->
i_sb)->s_clustersize_bits;
3453 static int ocfs2_refcount_cow_hunk(
struct inode *inode,
3455 struct buffer_head *di_bh,
3459 u32 cow_start = 0, cow_len = 0;
3463 struct buffer_head *ref_root_bh =
NULL;
3469 ret = ocfs2_refcount_cal_cow_clusters(inode, &di->
id2.
i_list,
3470 cpos, write_len, max_cpos,
3471 &cow_start, &cow_len);
3477 trace_ocfs2_refcount_cow_hunk(OCFS2_I(inode)->ip_blkno,
3478 cpos, write_len, max_cpos,
3479 cow_start, cow_len);
3483 ocfs2_readahead_for_cow(inode, file, cow_start, cow_len);
3493 1, &ref_tree, &ref_root_bh);
3509 INODE_CACHE(inode), di_bh);
3511 ret = ocfs2_replace_cow(context);
3523 brelse(ref_root_bh);
3536 struct buffer_head *di_bh,
3540 u32 p_cluster, num_clusters;
3545 &num_clusters, &ext_flags);
3551 if (write_len < num_clusters)
3552 num_clusters = write_len;
3555 ret = ocfs2_refcount_cow_hunk(inode, file, di_bh, cpos,
3556 num_clusters, max_cpos);
3563 write_len -= num_clusters;
3564 cpos += num_clusters;
3571 u32 v_cluster,
u32 *p_cluster,
3573 unsigned int *extent_flags)
3575 struct inode *inode = context->
inode;
3589 struct buffer_head *ref_root_bh,
3591 int *meta_add,
int *credits)
3593 int ret = 0,
index, ref_blocks = 0;
3594 u32 p_cluster, num_clusters;
3598 struct buffer_head *ref_leaf_bh =
NULL;
3600 while (cpos < clusters) {
3609 cpos += num_clusters;
3611 while (num_clusters) {
3612 ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
3613 p_cluster, num_clusters,
3637 brelse(ref_leaf_bh);
3644 p_cluster += num_clusters;
3648 *meta_add += ref_blocks;
3659 *credits += ocfs2_calc_extend_credits(inode->
i_sb,
3665 brelse(ref_leaf_bh);
3676 struct buffer_head *ref_root_bh,
3688 ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->
xr_list,
3690 &cow_start, &cow_len);
3715 ocfs2_clusters_to_blocks(inode->
i_sb, 1) *
cow_len;
3716 context->
get_clusters = ocfs2_xattr_value_get_clusters;
3720 INODE_CACHE(inode), vb);
3722 ret = ocfs2_replace_cow(context);
3738 struct buffer_head *ref_root_bh,
3739 u32 cpos,
u32 p_cluster,
u32 num_clusters,
3745 int credits = 1, ref_blocks = 0;
3749 ret = ocfs2_calc_refcount_meta_credits(inode->
i_sb,
3750 ref_ci, ref_root_bh,
3751 p_cluster, num_clusters,
3752 &ref_blocks, &credits);
3758 trace_ocfs2_add_refcount_flag(ref_blocks, credits);
3762 ref_blocks, &meta_ac);
3773 if (IS_ERR(handle)) {
3774 ret = PTR_ERR(handle);
3779 ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
3780 cpos, num_clusters, p_cluster,
3787 ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3788 p_cluster, num_clusters, 0,
3795 if (post && post->
func) {
3796 ret = post->
func(inode, handle, post->
para);
3809 static int ocfs2_change_ctime(
struct inode *inode,
3810 struct buffer_head *di_bh)
3818 if (IS_ERR(handle)) {
3819 ret = PTR_ERR(handle);
3843 static int ocfs2_attach_refcount_tree(
struct inode *inode,
3844 struct buffer_head *di_bh)
3846 int ret, data_changed = 0;
3847 struct buffer_head *ref_root_bh =
NULL;
3854 u32 cpos, num_clusters, clusters, p_cluster;
3858 ocfs2_init_dealloc_ctxt(&dealloc);
3861 ret = ocfs2_create_refcount_tree(inode, di_bh);
3871 &ref_tree, &ref_root_bh);
3882 size = i_size_read(inode);
3883 clusters = ocfs2_clusters_for_bytes(inode->
i_sb, size);
3886 while (cpos < clusters) {
3888 &num_clusters, &ext_flags);
3894 p_cluster, num_clusters,
3903 cpos += num_clusters;
3919 ret = ocfs2_change_ctime(inode, di_bh);
3926 brelse(ref_root_bh);
3928 if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
3942 static int ocfs2_add_refcounted_extent(
struct inode *inode,
3945 struct buffer_head *ref_root_bh,
3946 u32 cpos,
u32 p_cluster,
u32 num_clusters,
3947 unsigned int ext_flags,
3956 ret = ocfs2_lock_refcount_allocators(inode->
i_sb,
3957 p_cluster, num_clusters,
3959 ref_root_bh, &meta_ac,
3967 if (IS_ERR(handle)) {
3968 ret = PTR_ERR(handle);
3974 ocfs2_clusters_to_blocks(inode->
i_sb, p_cluster),
3975 num_clusters, ext_flags, meta_ac);
3982 p_cluster, num_clusters,
3995 static int ocfs2_duplicate_inline_data(
struct inode *s_inode,
3996 struct buffer_head *s_bh,
3997 struct inode *t_inode,
3998 struct buffer_head *t_bh)
4009 if (IS_ERR(handle)) {
4010 ret = PTR_ERR(handle);
4022 t_di->id2.i_data.id_count = s_di->
id2.
i_data.id_count;
4025 spin_lock(&OCFS2_I(t_inode)->ip_lock);
4027 t_di->i_dyn_features =
cpu_to_le16(OCFS2_I(t_inode)->ip_dyn_features);
4028 spin_unlock(&OCFS2_I(t_inode)->ip_lock);
4038 static int ocfs2_duplicate_extent_list(
struct inode *s_inode,
4039 struct inode *t_inode,
4040 struct buffer_head *t_bh,
4042 struct buffer_head *ref_root_bh,
4046 u32 p_cluster, num_clusters, clusters, cpos;
4053 size = i_size_read(s_inode);
4054 clusters = ocfs2_clusters_for_bytes(s_inode->
i_sb, size);
4057 while (cpos < clusters) {
4059 &num_clusters, &ext_flags);
4062 ret = ocfs2_add_refcounted_extent(t_inode, &et,
4063 ref_ci, ref_root_bh,
4074 cpos += num_clusters;
4087 static int ocfs2_complete_reflink(
struct inode *s_inode,
4088 struct buffer_head *s_bh,
4089 struct inode *t_inode,
4090 struct buffer_head *t_bh,
4097 loff_t size = i_size_read(s_inode);
4101 if (IS_ERR(handle)) {
4102 ret = PTR_ERR(handle);
4114 spin_lock(&OCFS2_I(t_inode)->ip_lock);
4115 OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
4116 OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
4117 OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
4118 spin_unlock(&OCFS2_I(t_inode)->ip_lock);
4119 i_size_write(t_inode, size);
4158 static int ocfs2_create_reflink_node(
struct inode *s_inode,
4159 struct buffer_head *s_bh,
4160 struct inode *t_inode,
4161 struct buffer_head *t_bh,
4165 struct buffer_head *ref_root_bh =
NULL;
4172 ocfs2_init_dealloc_ctxt(&dealloc);
4174 ret = ocfs2_set_refcount_tree(t_inode, t_bh,
4182 ret = ocfs2_duplicate_inline_data(s_inode, s_bh,
4190 1, &ref_tree, &ref_root_bh);
4197 ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
4198 &ref_tree->
rf_ci, ref_root_bh,
4202 goto out_unlock_refcount;
4205 out_unlock_refcount:
4207 brelse(ref_root_bh);
4209 if (ocfs2_dealloc_has_cluster(&dealloc)) {
4217 static int __ocfs2_reflink(
struct dentry *old_dentry,
4218 struct buffer_head *old_bh,
4223 struct inode *inode = old_dentry->
d_inode;
4224 struct buffer_head *new_bh =
NULL;
4238 ret = ocfs2_attach_refcount_tree(inode, old_bh);
4252 ret = ocfs2_create_reflink_node(inode, old_bh,
4253 new_inode, new_bh, preserve);
4269 ret = ocfs2_complete_reflink(inode, old_bh,
4270 new_inode, new_bh, preserve);
4288 static int ocfs2_reflink(
struct dentry *old_dentry,
struct inode *dir,
4289 struct dentry *new_dentry,
bool preserve)
4292 struct inode *inode = old_dentry->
d_inode;
4293 struct buffer_head *old_bh =
NULL;
4294 struct inode *new_orphan_inode =
NULL;
4314 error = __ocfs2_reflink(old_dentry, old_bh,
4315 new_orphan_inode, preserve);
4316 up_write(&OCFS2_I(inode)->ip_alloc_sem);
4317 up_write(&OCFS2_I(inode)->ip_xattr_sem);
4342 if (new_orphan_inode) {
4349 iput(new_orphan_inode);
4362 static inline int ocfs2_may_create(
struct inode *dir,
struct dentry *
child)
4379 static int ocfs2_vfs_reflink(
struct dentry *old_dentry,
struct inode *dir,
4380 struct dentry *new_dentry,
bool preserve)
4382 struct inode *inode = old_dentry->
d_inode;
4388 error = ocfs2_may_create(dir, new_dentry);
4429 error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve);
4432 fsnotify_create(dir, new_dentry);
4439 const char __user *oldname,
4440 const char __user *newname,
4443 struct dentry *new_dentry;
4444 struct path old_path, new_path;
4457 error = PTR_ERR(new_dentry);
4458 if (IS_ERR(new_dentry)) {
4464 if (old_path.
mnt != new_path.
mnt) {
4469 error = ocfs2_vfs_reflink(old_path.
dentry,
4470 new_path.
dentry->d_inode,
4471 new_dentry, preserve);