Re: [PATCH v10 17/21] btrfs: dedupe: Introduce interfaces to resume and cleanup dedupe info

2016-06-03 Thread Josef Bacik

On 04/01/2016 02:35 AM, Qu Wenruo wrote:

Since we will introduce a new on-disk based dedupe method, introduce new
interfaces to resume previous dedupe setup.

And since we introduce a new tree for status, also add disable handler
for it.

Signed-off-by: Wang Xiaoguang 
Signed-off-by: Qu Wenruo 
---
 fs/btrfs/dedupe.c  | 197 -
 fs/btrfs/dedupe.h  |  13 
 fs/btrfs/disk-io.c |  25 ++-
 fs/btrfs/disk-io.h |   1 +
 4 files changed, 232 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/dedupe.c b/fs/btrfs/dedupe.c
index cfb7fea..a274c1c 100644
--- a/fs/btrfs/dedupe.c
+++ b/fs/btrfs/dedupe.c
@@ -21,6 +21,8 @@
 #include "transaction.h"
 #include "delayed-ref.h"
 #include "qgroup.h"
+#include "disk-io.h"
+#include "locking.h"

 struct inmem_hash {
struct rb_node hash_node;
@@ -102,10 +104,69 @@ static int init_dedupe_info(struct btrfs_dedupe_info 
**ret_info, u16 type,
return 0;
 }

+static int init_dedupe_tree(struct btrfs_fs_info *fs_info,
+   struct btrfs_dedupe_info *dedupe_info)
+{
+   struct btrfs_root *dedupe_root;
+   struct btrfs_key key;
+   struct btrfs_path *path;
+   struct btrfs_dedupe_status_item *status;
+   struct btrfs_trans_handle *trans;
+   int ret;
+
+   path = btrfs_alloc_path();
+   if (!path)
+   return -ENOMEM;
+
+   trans = btrfs_start_transaction(fs_info->tree_root, 2);
+   if (IS_ERR(trans)) {
+   ret = PTR_ERR(trans);
+   goto out;
+   }
+   dedupe_root = btrfs_create_tree(trans, fs_info,
+  BTRFS_DEDUPE_TREE_OBJECTID);
+   if (IS_ERR(dedupe_root)) {
+   ret = PTR_ERR(dedupe_root);
+   btrfs_abort_transaction(trans, fs_info->tree_root, ret);
+   goto out;
+   }
+   dedupe_info->dedupe_root = dedupe_root;
+
+   key.objectid = 0;
+   key.type = BTRFS_DEDUPE_STATUS_ITEM_KEY;
+   key.offset = 0;
+
+   ret = btrfs_insert_empty_item(trans, dedupe_root, path, ,
+ sizeof(*status));
+   if (ret < 0) {
+   btrfs_abort_transaction(trans, fs_info->tree_root, ret);
+   goto out;
+   }
+
+   status = btrfs_item_ptr(path->nodes[0], path->slots[0],
+   struct btrfs_dedupe_status_item);
+   btrfs_set_dedupe_status_blocksize(path->nodes[0], status,
+dedupe_info->blocksize);
+   btrfs_set_dedupe_status_limit(path->nodes[0], status,
+   dedupe_info->limit_nr);
+   btrfs_set_dedupe_status_hash_type(path->nodes[0], status,
+   dedupe_info->hash_type);
+   btrfs_set_dedupe_status_backend(path->nodes[0], status,
+   dedupe_info->backend);
+   btrfs_mark_buffer_dirty(path->nodes[0]);
+out:
+   btrfs_free_path(path);
+   if (ret == 0)
+   btrfs_commit_transaction(trans, fs_info->tree_root);


Still need to call btrfs_end_transaction() if we aborted to clean things 
up.  Thanks,


Josef
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v10 17/21] btrfs: dedupe: Introduce interfaces to resume and cleanup dedupe info

2016-04-01 Thread Qu Wenruo
Since we will introduce a new on-disk based dedupe method, introduce new
interfaces to resume previous dedupe setup.

And since we introduce a new tree for status, also add disable handler
for it.

Signed-off-by: Wang Xiaoguang 
Signed-off-by: Qu Wenruo 
---
 fs/btrfs/dedupe.c  | 197 -
 fs/btrfs/dedupe.h  |  13 
 fs/btrfs/disk-io.c |  25 ++-
 fs/btrfs/disk-io.h |   1 +
 4 files changed, 232 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/dedupe.c b/fs/btrfs/dedupe.c
index cfb7fea..a274c1c 100644
--- a/fs/btrfs/dedupe.c
+++ b/fs/btrfs/dedupe.c
@@ -21,6 +21,8 @@
 #include "transaction.h"
 #include "delayed-ref.h"
 #include "qgroup.h"
+#include "disk-io.h"
+#include "locking.h"
 
 struct inmem_hash {
struct rb_node hash_node;
@@ -102,10 +104,69 @@ static int init_dedupe_info(struct btrfs_dedupe_info 
**ret_info, u16 type,
return 0;
 }
 
+static int init_dedupe_tree(struct btrfs_fs_info *fs_info,
+   struct btrfs_dedupe_info *dedupe_info)
+{
+   struct btrfs_root *dedupe_root;
+   struct btrfs_key key;
+   struct btrfs_path *path;
+   struct btrfs_dedupe_status_item *status;
+   struct btrfs_trans_handle *trans;
+   int ret;
+
+   path = btrfs_alloc_path();
+   if (!path)
+   return -ENOMEM;
+
+   trans = btrfs_start_transaction(fs_info->tree_root, 2);
+   if (IS_ERR(trans)) {
+   ret = PTR_ERR(trans);
+   goto out;
+   }
+   dedupe_root = btrfs_create_tree(trans, fs_info,
+  BTRFS_DEDUPE_TREE_OBJECTID);
+   if (IS_ERR(dedupe_root)) {
+   ret = PTR_ERR(dedupe_root);
+   btrfs_abort_transaction(trans, fs_info->tree_root, ret);
+   goto out;
+   }
+   dedupe_info->dedupe_root = dedupe_root;
+
+   key.objectid = 0;
+   key.type = BTRFS_DEDUPE_STATUS_ITEM_KEY;
+   key.offset = 0;
+
+   ret = btrfs_insert_empty_item(trans, dedupe_root, path, ,
+ sizeof(*status));
+   if (ret < 0) {
+   btrfs_abort_transaction(trans, fs_info->tree_root, ret);
+   goto out;
+   }
+
+   status = btrfs_item_ptr(path->nodes[0], path->slots[0],
+   struct btrfs_dedupe_status_item);
+   btrfs_set_dedupe_status_blocksize(path->nodes[0], status,
+dedupe_info->blocksize);
+   btrfs_set_dedupe_status_limit(path->nodes[0], status,
+   dedupe_info->limit_nr);
+   btrfs_set_dedupe_status_hash_type(path->nodes[0], status,
+   dedupe_info->hash_type);
+   btrfs_set_dedupe_status_backend(path->nodes[0], status,
+   dedupe_info->backend);
+   btrfs_mark_buffer_dirty(path->nodes[0]);
+out:
+   btrfs_free_path(path);
+   if (ret == 0)
+   btrfs_commit_transaction(trans, fs_info->tree_root);
+   return ret;
+}
+
 static int check_dedupe_parameter(struct btrfs_fs_info *fs_info, u16 hash_type,
  u16 backend, u64 blocksize, u64 limit_nr,
  u64 limit_mem, u64 *ret_limit)
 {
+   u64 compat_ro_flag = btrfs_super_compat_ro_flags(fs_info->super_copy);
+
if (blocksize > BTRFS_DEDUPE_BLOCKSIZE_MAX ||
blocksize < BTRFS_DEDUPE_BLOCKSIZE_MIN ||
blocksize < fs_info->tree_root->sectorsize ||
@@ -140,8 +201,12 @@ static int check_dedupe_parameter(struct btrfs_fs_info 
*fs_info, u16 hash_type,
*ret_limit = min(tmp, limit_nr);
}
}
-   if (backend == BTRFS_DEDUPE_BACKEND_ONDISK)
+   if (backend == BTRFS_DEDUPE_BACKEND_ONDISK) {
+   /* Ondisk backend must use RO compat feature */
+   if (!(compat_ro_flag & BTRFS_FEATURE_COMPAT_RO_DEDUPE))
+   return -EOPNOTSUPP;
*ret_limit = 0;
+   }
return 0;
 }
 
@@ -150,11 +215,16 @@ int btrfs_dedupe_enable(struct btrfs_fs_info *fs_info, 
u16 type, u16 backend,
 {
struct btrfs_dedupe_info *dedupe_info;
u64 limit = 0;
+   u64 compat_ro_flag = btrfs_super_compat_ro_flags(fs_info->super_copy);
+   int create_tree;
int ret = 0;
 
/* only one limit is accepted for enable*/
if (limit_nr && limit_mem)
return -EINVAL;
+   /* enable and disable may modify ondisk data, so block RO fs*/
+   if (fs_info->sb->s_flags & MS_RDONLY)
+   return -EROFS;
 
ret = check_dedupe_parameter(fs_info, type, backend, blocksize,
 limit_nr, limit_mem, );
@@ -179,9 +249,19 @@ int btrfs_dedupe_enable(struct btrfs_fs_info *fs_info, u16 
type, u16 backend,
}
 
 enable:
+   create_tree = compat_ro_flag &