aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ocfs2/refcounttree.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ocfs2/refcounttree.c')
-rw-r--r--fs/ocfs2/refcounttree.c174
1 files changed, 131 insertions, 43 deletions
diff --git a/fs/ocfs2/refcounttree.c b/fs/ocfs2/refcounttree.c
index 3a0df7a1b810..bd96f6c7877e 100644
--- a/fs/ocfs2/refcounttree.c
+++ b/fs/ocfs2/refcounttree.c
@@ -37,7 +37,6 @@
37 37
38#include <linux/bio.h> 38#include <linux/bio.h>
39#include <linux/blkdev.h> 39#include <linux/blkdev.h>
40#include <linux/gfp.h>
41#include <linux/slab.h> 40#include <linux/slab.h>
42#include <linux/writeback.h> 41#include <linux/writeback.h>
43#include <linux/pagevec.h> 42#include <linux/pagevec.h>
@@ -276,7 +275,7 @@ static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
276 spin_unlock(&osb->osb_lock); 275 spin_unlock(&osb->osb_lock);
277} 276}
278 277
279void ocfs2_kref_remove_refcount_tree(struct kref *kref) 278static void ocfs2_kref_remove_refcount_tree(struct kref *kref)
280{ 279{
281 struct ocfs2_refcount_tree *tree = 280 struct ocfs2_refcount_tree *tree =
282 container_of(kref, struct ocfs2_refcount_tree, rf_getcnt); 281 container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
@@ -524,23 +523,6 @@ out:
524 return ret; 523 return ret;
525} 524}
526 525
527int ocfs2_lock_refcount_tree_by_inode(struct inode *inode, int rw,
528 struct ocfs2_refcount_tree **ret_tree,
529 struct buffer_head **ref_bh)
530{
531 int ret;
532 u64 ref_blkno;
533
534 ret = ocfs2_get_refcount_block(inode, &ref_blkno);
535 if (ret) {
536 mlog_errno(ret);
537 return ret;
538 }
539
540 return ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno,
541 rw, ret_tree, ref_bh);
542}
543
544void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb, 526void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
545 struct ocfs2_refcount_tree *tree, int rw) 527 struct ocfs2_refcount_tree *tree, int rw)
546{ 528{
@@ -643,7 +625,7 @@ static int ocfs2_create_refcount_tree(struct inode *inode,
643 rb = (struct ocfs2_refcount_block *)new_bh->b_data; 625 rb = (struct ocfs2_refcount_block *)new_bh->b_data;
644 memset(rb, 0, inode->i_sb->s_blocksize); 626 memset(rb, 0, inode->i_sb->s_blocksize);
645 strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE); 627 strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
646 rb->rf_suballoc_slot = cpu_to_le16(osb->slot_num); 628 rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
647 rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 629 rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
648 rb->rf_fs_generation = cpu_to_le32(osb->fs_generation); 630 rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
649 rb->rf_blkno = cpu_to_le64(first_blkno); 631 rb->rf_blkno = cpu_to_le64(first_blkno);
@@ -969,6 +951,103 @@ out:
969} 951}
970 952
971/* 953/*
954 * Find the end range for a leaf refcount block indicated by
955 * el->l_recs[index].e_blkno.
956 */
957static int ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info *ci,
958 struct buffer_head *ref_root_bh,
959 struct ocfs2_extent_block *eb,
960 struct ocfs2_extent_list *el,
961 int index, u32 *cpos_end)
962{
963 int ret, i, subtree_root;
964 u32 cpos;
965 u64 blkno;
966 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
967 struct ocfs2_path *left_path = NULL, *right_path = NULL;
968 struct ocfs2_extent_tree et;
969 struct ocfs2_extent_list *tmp_el;
970
971 if (index < le16_to_cpu(el->l_next_free_rec) - 1) {
972 /*
973 * We have a extent rec after index, so just use the e_cpos
974 * of the next extent rec.
975 */
976 *cpos_end = le32_to_cpu(el->l_recs[index+1].e_cpos);
977 return 0;
978 }
979
980 if (!eb || (eb && !eb->h_next_leaf_blk)) {
981 /*
982 * We are the last extent rec, so any high cpos should
983 * be stored in this leaf refcount block.
984 */
985 *cpos_end = UINT_MAX;
986 return 0;
987 }
988
989 /*
990 * If the extent block isn't the last one, we have to find
991 * the subtree root between this extent block and the next
992 * leaf extent block and get the corresponding e_cpos from
993 * the subroot. Otherwise we may corrupt the b-tree.
994 */
995 ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
996
997 left_path = ocfs2_new_path_from_et(&et);
998 if (!left_path) {
999 ret = -ENOMEM;
1000 mlog_errno(ret);
1001 goto out;
1002 }
1003
1004 cpos = le32_to_cpu(eb->h_list.l_recs[index].e_cpos);
1005 ret = ocfs2_find_path(ci, left_path, cpos);
1006 if (ret) {
1007 mlog_errno(ret);
1008 goto out;
1009 }
1010
1011 right_path = ocfs2_new_path_from_path(left_path);
1012 if (!right_path) {
1013 ret = -ENOMEM;
1014 mlog_errno(ret);
1015 goto out;
1016 }
1017
1018 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, &cpos);
1019 if (ret) {
1020 mlog_errno(ret);
1021 goto out;
1022 }
1023
1024 ret = ocfs2_find_path(ci, right_path, cpos);
1025 if (ret) {
1026 mlog_errno(ret);
1027 goto out;
1028 }
1029
1030 subtree_root = ocfs2_find_subtree_root(&et, left_path,
1031 right_path);
1032
1033 tmp_el = left_path->p_node[subtree_root].el;
1034 blkno = left_path->p_node[subtree_root+1].bh->b_blocknr;
1035 for (i = 0; i < le32_to_cpu(tmp_el->l_next_free_rec); i++) {
1036 if (le64_to_cpu(tmp_el->l_recs[i].e_blkno) == blkno) {
1037 *cpos_end = le32_to_cpu(tmp_el->l_recs[i+1].e_cpos);
1038 break;
1039 }
1040 }
1041
1042 BUG_ON(i == le32_to_cpu(tmp_el->l_next_free_rec));
1043
1044out:
1045 ocfs2_free_path(left_path);
1046 ocfs2_free_path(right_path);
1047 return ret;
1048}
1049
1050/*
972 * Given a cpos and len, try to find the refcount record which contains cpos. 1051 * Given a cpos and len, try to find the refcount record which contains cpos.
973 * 1. If cpos can be found in one refcount record, return the record. 1052 * 1. If cpos can be found in one refcount record, return the record.
974 * 2. If cpos can't be found, return a fake record which start from cpos 1053 * 2. If cpos can't be found, return a fake record which start from cpos
@@ -983,10 +1062,10 @@ static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
983 struct buffer_head **ret_bh) 1062 struct buffer_head **ret_bh)
984{ 1063{
985 int ret = 0, i, found; 1064 int ret = 0, i, found;
986 u32 low_cpos; 1065 u32 low_cpos, uninitialized_var(cpos_end);
987 struct ocfs2_extent_list *el; 1066 struct ocfs2_extent_list *el;
988 struct ocfs2_extent_rec *tmp, *rec = NULL; 1067 struct ocfs2_extent_rec *rec = NULL;
989 struct ocfs2_extent_block *eb; 1068 struct ocfs2_extent_block *eb = NULL;
990 struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL; 1069 struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
991 struct super_block *sb = ocfs2_metadata_cache_get_super(ci); 1070 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
992 struct ocfs2_refcount_block *rb = 1071 struct ocfs2_refcount_block *rb =
@@ -1034,12 +1113,16 @@ static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
1034 } 1113 }
1035 } 1114 }
1036 1115
1037 /* adjust len when we have ocfs2_extent_rec after it. */ 1116 if (found) {
1038 if (found && i < le16_to_cpu(el->l_next_free_rec) - 1) { 1117 ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh,
1039 tmp = &el->l_recs[i+1]; 1118 eb, el, i, &cpos_end);
1119 if (ret) {
1120 mlog_errno(ret);
1121 goto out;
1122 }
1040 1123
1041 if (le32_to_cpu(tmp->e_cpos) < cpos + len) 1124 if (cpos_end < low_cpos + len)
1042 len = le32_to_cpu(tmp->e_cpos) - cpos; 1125 len = cpos_end - low_cpos;
1043 } 1126 }
1044 1127
1045 ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno), 1128 ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
@@ -1246,7 +1329,7 @@ static int ocfs2_expand_inline_ref_root(handle_t *handle,
1246 memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize); 1329 memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
1247 1330
1248 new_rb = (struct ocfs2_refcount_block *)new_bh->b_data; 1331 new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1249 new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num); 1332 new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1250 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1333 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1251 new_rb->rf_blkno = cpu_to_le64(blkno); 1334 new_rb->rf_blkno = cpu_to_le64(blkno);
1252 new_rb->rf_cpos = cpu_to_le32(0); 1335 new_rb->rf_cpos = cpu_to_le32(0);
@@ -1418,7 +1501,7 @@ static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
1418 1501
1419 /* change old and new rl_used accordingly. */ 1502 /* change old and new rl_used accordingly. */
1420 le16_add_cpu(&rl->rl_used, -num_moved); 1503 le16_add_cpu(&rl->rl_used, -num_moved);
1421 new_rl->rl_used = cpu_to_le32(num_moved); 1504 new_rl->rl_used = cpu_to_le16(num_moved);
1422 1505
1423 sort(&rl->rl_recs, le16_to_cpu(rl->rl_used), 1506 sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1424 sizeof(struct ocfs2_refcount_rec), 1507 sizeof(struct ocfs2_refcount_rec),
@@ -1492,7 +1575,7 @@ static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1492 new_rb = (struct ocfs2_refcount_block *)new_bh->b_data; 1575 new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1493 memset(new_rb, 0, sb->s_blocksize); 1576 memset(new_rb, 0, sb->s_blocksize);
1494 strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE); 1577 strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1495 new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num); 1578 new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1496 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start); 1579 new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1497 new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 1580 new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1498 new_rb->rf_blkno = cpu_to_le64(blkno); 1581 new_rb->rf_blkno = cpu_to_le64(blkno);
@@ -1797,7 +1880,8 @@ static int ocfs2_split_refcount_rec(handle_t *handle,
1797 recs_need++; 1880 recs_need++;
1798 1881
1799 /* If the leaf block don't have enough record, expand it. */ 1882 /* If the leaf block don't have enough record, expand it. */
1800 if (le16_to_cpu(rf_list->rl_used) + recs_need > rf_list->rl_count) { 1883 if (le16_to_cpu(rf_list->rl_used) + recs_need >
1884 le16_to_cpu(rf_list->rl_count)) {
1801 struct ocfs2_refcount_rec tmp_rec; 1885 struct ocfs2_refcount_rec tmp_rec;
1802 u64 cpos = le64_to_cpu(orig_rec->r_cpos); 1886 u64 cpos = le64_to_cpu(orig_rec->r_cpos);
1803 len = le32_to_cpu(orig_rec->r_clusters); 1887 len = le32_to_cpu(orig_rec->r_clusters);
@@ -1859,7 +1943,7 @@ static int ocfs2_split_refcount_rec(handle_t *handle,
1859 memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec)); 1943 memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
1860 le64_add_cpu(&tail_rec->r_cpos, 1944 le64_add_cpu(&tail_rec->r_cpos,
1861 le32_to_cpu(tail_rec->r_clusters) - len); 1945 le32_to_cpu(tail_rec->r_clusters) - len);
1862 tail_rec->r_clusters = le32_to_cpu(len); 1946 tail_rec->r_clusters = cpu_to_le32(len);
1863 } 1947 }
1864 1948
1865 /* 1949 /*
@@ -2431,7 +2515,7 @@ out:
2431 * we gonna touch and whether we need to create new blocks. 2515 * we gonna touch and whether we need to create new blocks.
2432 * 2516 *
2433 * Normally the refcount blocks store these refcount should be 2517 * Normally the refcount blocks store these refcount should be
2434 * continguous also, so that we can get the number easily. 2518 * contiguous also, so that we can get the number easily.
2435 * As for meta_ac, we will at most add split 2 refcount record and 2519 * As for meta_ac, we will at most add split 2 refcount record and
2436 * 2 more refcount block, so just check it in a rough way. 2520 * 2 more refcount block, so just check it in a rough way.
2437 * 2521 *
@@ -2860,7 +2944,7 @@ static int ocfs2_duplicate_clusters_by_page(handle_t *handle,
2860 2944
2861 while (offset < end) { 2945 while (offset < end) {
2862 page_index = offset >> PAGE_CACHE_SHIFT; 2946 page_index = offset >> PAGE_CACHE_SHIFT;
2863 map_end = (page_index + 1) << PAGE_CACHE_SHIFT; 2947 map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT;
2864 if (map_end > end) 2948 if (map_end > end)
2865 map_end = end; 2949 map_end = end;
2866 2950
@@ -2872,8 +2956,12 @@ static int ocfs2_duplicate_clusters_by_page(handle_t *handle,
2872 2956
2873 page = grab_cache_page(mapping, page_index); 2957 page = grab_cache_page(mapping, page_index);
2874 2958
2875 /* This page can't be dirtied before we CoW it out. */ 2959 /*
2876 BUG_ON(PageDirty(page)); 2960 * In case PAGE_CACHE_SIZE <= CLUSTER_SIZE, This page
2961 * can't be dirtied before we CoW it out.
2962 */
2963 if (PAGE_CACHE_SIZE <= OCFS2_SB(sb)->s_clustersize)
2964 BUG_ON(PageDirty(page));
2877 2965
2878 if (!PageUptodate(page)) { 2966 if (!PageUptodate(page)) {
2879 ret = block_read_full_page(page, ocfs2_get_block); 2967 ret = block_read_full_page(page, ocfs2_get_block);
@@ -3085,7 +3173,7 @@ static int ocfs2_cow_sync_writeback(struct super_block *sb,
3085 3173
3086 while (offset < end) { 3174 while (offset < end) {
3087 page_index = offset >> PAGE_CACHE_SHIFT; 3175 page_index = offset >> PAGE_CACHE_SHIFT;
3088 map_end = (page_index + 1) << PAGE_CACHE_SHIFT; 3176 map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT;
3089 if (map_end > end) 3177 if (map_end > end)
3090 map_end = end; 3178 map_end = end;
3091 3179
@@ -3840,8 +3928,7 @@ static int ocfs2_add_refcounted_extent(struct inode *inode,
3840 } 3928 }
3841 3929
3842 ret = ocfs2_insert_extent(handle, et, cpos, 3930 ret = ocfs2_insert_extent(handle, et, cpos,
3843 cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb, 3931 ocfs2_clusters_to_blocks(inode->i_sb, p_cluster),
3844 p_cluster)),
3845 num_clusters, ext_flags, meta_ac); 3932 num_clusters, ext_flags, meta_ac);
3846 if (ret) { 3933 if (ret) {
3847 mlog_errno(ret); 3934 mlog_errno(ret);
@@ -3987,6 +4074,7 @@ static int ocfs2_complete_reflink(struct inode *s_inode,
3987 OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features; 4074 OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
3988 spin_unlock(&OCFS2_I(t_inode)->ip_lock); 4075 spin_unlock(&OCFS2_I(t_inode)->ip_lock);
3989 i_size_write(t_inode, size); 4076 i_size_write(t_inode, size);
4077 t_inode->i_blocks = s_inode->i_blocks;
3990 4078
3991 di->i_xattr_inline_size = s_di->i_xattr_inline_size; 4079 di->i_xattr_inline_size = s_di->i_xattr_inline_size;
3992 di->i_clusters = s_di->i_clusters; 4080 di->i_clusters = s_di->i_clusters;
@@ -4253,8 +4341,8 @@ static int ocfs2_user_path_parent(const char __user *path,
4253 * @new_dentry: target dentry 4341 * @new_dentry: target dentry
4254 * @preserve: if true, preserve all file attributes 4342 * @preserve: if true, preserve all file attributes
4255 */ 4343 */
4256int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir, 4344static int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir,
4257 struct dentry *new_dentry, bool preserve) 4345 struct dentry *new_dentry, bool preserve)
4258{ 4346{
4259 struct inode *inode = old_dentry->d_inode; 4347 struct inode *inode = old_dentry->d_inode;
4260 int error; 4348 int error;
@@ -4302,7 +4390,7 @@ int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir,
4302 } 4390 }
4303 4391
4304 mutex_lock(&inode->i_mutex); 4392 mutex_lock(&inode->i_mutex);
4305 vfs_dq_init(dir); 4393 dquot_initialize(dir);
4306 error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve); 4394 error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve);
4307 mutex_unlock(&inode->i_mutex); 4395 mutex_unlock(&inode->i_mutex);
4308 if (!error) 4396 if (!error)