Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-10-08 Thread Liu Bo
On 10/03/2012 10:02 PM, Chris Mason wrote:
 On Tue, Sep 25, 2012 at 07:07:53PM -0600, Liu Bo wrote:
 On 09/26/2012 01:39 AM, Mitch Harder wrote:
 On Mon, Sep 17, 2012 at 4:58 AM, Liu Bo bo.li@oracle.com wrote:
 This comes from one of btrfs's project ideas,
 As we defragment files, we break any sharing from other snapshots.
 The balancing code will preserve the sharing, and defrag needs to grow this
 as well.

 Now we're able to fill the blank with this patch, in which we make full 
 use of
 backref walking stuff.

 Here is the basic idea,
 o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
 o  at endio, after we finish updating fs tree, we use backref walking to 
 find
all parents of the ranges and re-link them with the new COWed file 
 layout by
adding corresponding backrefs.

 Originally patch by Li Zefan l...@cn.fujitsu.com
 Signed-off-by: Liu Bo bo.li@oracle.com

 I'm hitting the WARN_ON in record_extent_backrefs() indicating a
 problem with the return value from iterate_inodes_from_logical().
 
 Me too.  It triggers reliably with mount -o autodefrag, and then crashes
 a in the next function ;)
 
 -chris
 

Hi Chris, Mitch,

I'm afraid that I may need a little more time to fix all bugs in it because 
there seems to be
some backref walking bugs mixed in, and at least 4 different crashes make it 
harder to address bugs.

I use an 1G random write fio job running in background, following by creating 
20 snapshots in background,
and mount -o autodefrag.

So if your crash is quite stable in one place, please let me know the steps.

thanks,
liubo


--
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


Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-10-08 Thread Chris Mason
On Mon, Oct 08, 2012 at 06:18:26AM -0600, Liu Bo wrote:
 On 10/03/2012 10:02 PM, Chris Mason wrote:
  On Tue, Sep 25, 2012 at 07:07:53PM -0600, Liu Bo wrote:
  On 09/26/2012 01:39 AM, Mitch Harder wrote:
  On Mon, Sep 17, 2012 at 4:58 AM, Liu Bo bo.li@oracle.com wrote:
  This comes from one of btrfs's project ideas,
  As we defragment files, we break any sharing from other snapshots.
  The balancing code will preserve the sharing, and defrag needs to grow 
  this
  as well.
 
  Now we're able to fill the blank with this patch, in which we make full 
  use of
  backref walking stuff.
 
  Here is the basic idea,
  o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
  o  at endio, after we finish updating fs tree, we use backref walking to 
  find
 all parents of the ranges and re-link them with the new COWed file 
  layout by
 adding corresponding backrefs.
 
  Originally patch by Li Zefan l...@cn.fujitsu.com
  Signed-off-by: Liu Bo bo.li@oracle.com
 
  I'm hitting the WARN_ON in record_extent_backrefs() indicating a
  problem with the return value from iterate_inodes_from_logical().
  
  Me too.  It triggers reliably with mount -o autodefrag, and then crashes
  a in the next function ;)
  
  -chris
  
 
 Hi Chris, Mitch,
 
 I'm afraid that I may need a little more time to fix all bugs in it because 
 there seems to be
 some backref walking bugs mixed in, and at least 4 different crashes make it 
 harder to address bugs.
 
 I use an 1G random write fio job running in background, following by creating 
 20 snapshots in background,
 and mount -o autodefrag.
 
 So if your crash is quite stable in one place, please let me know the steps.

I have a notmuch mail database.  I just receive mail with auto defrag on
and it crashes.  Chrome databases may do it as well.

If it helps, I have compression too.

-chris

--
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


Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-10-04 Thread Liu Bo
On 10/03/2012 10:02 PM, Chris Mason wrote:
 On Tue, Sep 25, 2012 at 07:07:53PM -0600, Liu Bo wrote:
 On 09/26/2012 01:39 AM, Mitch Harder wrote:
 On Mon, Sep 17, 2012 at 4:58 AM, Liu Bo bo.li@oracle.com wrote:
 This comes from one of btrfs's project ideas,
 As we defragment files, we break any sharing from other snapshots.
 The balancing code will preserve the sharing, and defrag needs to grow this
 as well.

 Now we're able to fill the blank with this patch, in which we make full 
 use of
 backref walking stuff.

 Here is the basic idea,
 o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
 o  at endio, after we finish updating fs tree, we use backref walking to 
 find
all parents of the ranges and re-link them with the new COWed file 
 layout by
adding corresponding backrefs.

 Originally patch by Li Zefan l...@cn.fujitsu.com
 Signed-off-by: Liu Bo bo.li@oracle.com

 I'm hitting the WARN_ON in record_extent_backrefs() indicating a
 problem with the return value from iterate_inodes_from_logical().
 
 Me too.  It triggers reliably with mount -o autodefrag, and then crashes
 a in the next function ;)
 
 -chris
 

Good news, I'm starting hitting the crash (a NULL pointer crash) ;)

thanks,
liubo
--
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


Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-10-04 Thread Mitch Harder
On Thu, Oct 4, 2012 at 9:22 AM, Liu Bo bo.li@oracle.com wrote:
 On 10/03/2012 10:02 PM, Chris Mason wrote:
 On Tue, Sep 25, 2012 at 07:07:53PM -0600, Liu Bo wrote:
 On 09/26/2012 01:39 AM, Mitch Harder wrote:
 On Mon, Sep 17, 2012 at 4:58 AM, Liu Bo bo.li@oracle.com wrote:
 This comes from one of btrfs's project ideas,
 As we defragment files, we break any sharing from other snapshots.
 The balancing code will preserve the sharing, and defrag needs to grow 
 this
 as well.

 Now we're able to fill the blank with this patch, in which we make full 
 use of
 backref walking stuff.

 Here is the basic idea,
 o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
 o  at endio, after we finish updating fs tree, we use backref walking to 
 find
all parents of the ranges and re-link them with the new COWed file 
 layout by
adding corresponding backrefs.

 Originally patch by Li Zefan l...@cn.fujitsu.com
 Signed-off-by: Liu Bo bo.li@oracle.com

 I'm hitting the WARN_ON in record_extent_backrefs() indicating a
 problem with the return value from iterate_inodes_from_logical().

 Me too.  It triggers reliably with mount -o autodefrag, and then crashes
 a in the next function ;)

 -chris


 Good news, I'm starting hitting the crash (a NULL pointer crash) ;)

 thanks,
 liubo

I'm also starting to hit this crash while balancing a test partition.

I guess this isn't surprising since both autodefrag and balancing make
use of relocation.
--
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


Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-10-03 Thread Chris Mason
On Tue, Sep 25, 2012 at 07:07:53PM -0600, Liu Bo wrote:
 On 09/26/2012 01:39 AM, Mitch Harder wrote:
  On Mon, Sep 17, 2012 at 4:58 AM, Liu Bo bo.li@oracle.com wrote:
  This comes from one of btrfs's project ideas,
  As we defragment files, we break any sharing from other snapshots.
  The balancing code will preserve the sharing, and defrag needs to grow this
  as well.
 
  Now we're able to fill the blank with this patch, in which we make full 
  use of
  backref walking stuff.
 
  Here is the basic idea,
  o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
  o  at endio, after we finish updating fs tree, we use backref walking to 
  find
 all parents of the ranges and re-link them with the new COWed file 
  layout by
 adding corresponding backrefs.
 
  Originally patch by Li Zefan l...@cn.fujitsu.com
  Signed-off-by: Liu Bo bo.li@oracle.com
  
  I'm hitting the WARN_ON in record_extent_backrefs() indicating a
  problem with the return value from iterate_inodes_from_logical().

Me too.  It triggers reliably with mount -o autodefrag, and then crashes
a in the next function ;)

-chris
--
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


Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-09-25 Thread Mitch Harder
On Mon, Sep 17, 2012 at 4:58 AM, Liu Bo bo.li@oracle.com wrote:
 This comes from one of btrfs's project ideas,
 As we defragment files, we break any sharing from other snapshots.
 The balancing code will preserve the sharing, and defrag needs to grow this
 as well.

 Now we're able to fill the blank with this patch, in which we make full use of
 backref walking stuff.

 Here is the basic idea,
 o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
 o  at endio, after we finish updating fs tree, we use backref walking to find
all parents of the ranges and re-link them with the new COWed file layout 
 by
adding corresponding backrefs.

 Originally patch by Li Zefan l...@cn.fujitsu.com
 Signed-off-by: Liu Bo bo.li@oracle.com

I'm hitting the WARN_ON in record_extent_backrefs() indicating a
problem with the return value from iterate_inodes_from_logical().

[ 6865.184782] [ cut here ]
[ 6865.184819] WARNING: at fs/btrfs/inode.c:2062
record_extent_backrefs+0xe5/0xe7 [btrfs]()
[ 6865.184823] Hardware name: OptiPlex 745
[ 6865.184825] Modules linked in: lpc_ich mfd_core xts gf128mul cryptd
aes_x86_64 sha256_generic btrfs libcrc32c
[ 6865.184841] Pid: 4239, comm: btrfs-endio-wri Not tainted 3.5.4-git-local+ #1
[ 6865.184844] Call Trace:
[ 6865.184856]  [81031d6a] warn_slowpath_common+0x74/0xa2
[ 6865.184862]  [81031db2] warn_slowpath_null+0x1a/0x1c
[ 6865.184884]  [a003356b] record_extent_backrefs+0xe5/0xe7 [btrfs]
[ 6865.184908]  [a003cf3a] btrfs_finish_ordered_io+0x131/0xa4b [btrfs]
[ 6865.184930]  [a003d869] finish_ordered_fn+0x15/0x17 [btrfs]
[ 6865.184951]  [a005882f] worker_loop+0x145/0x516 [btrfs]
[ 6865.184959]  [81059727] ? __wake_up_common+0x54/0x84
[ 6865.184983]  [a00586ea] ? btrfs_queue_worker+0x2d3/0x2d3 [btrfs]
[ 6865.184989]  [810516bb] kthread+0x93/0x98
[ 6865.184996]  [817d7934] kernel_thread_helper+0x4/0x10
[ 6865.185001]  [81051628] ? kthread_freezable_should_stop+0x6a/0x6a
[ 6865.185021]  [817d7930] ? gs_change+0xb/0xb
[ 6865.185025] ---[ end trace 26cc0e186efc79d8 ]---


I'm testing a 3.5.4 kernel merged with 3.6_rc patchset as well as the
send_recv patches and most of the btrfs-next patches.

I'm running into this issue when mounting with autodefrag, and running
some snapshot tests.

This may be related to a problem elsewhere, because I've been
encountering other backref issues even before testing this patch.
--
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


Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-09-25 Thread Liu Bo
On 09/26/2012 01:39 AM, Mitch Harder wrote:
 On Mon, Sep 17, 2012 at 4:58 AM, Liu Bo bo.li@oracle.com wrote:
 This comes from one of btrfs's project ideas,
 As we defragment files, we break any sharing from other snapshots.
 The balancing code will preserve the sharing, and defrag needs to grow this
 as well.

 Now we're able to fill the blank with this patch, in which we make full use 
 of
 backref walking stuff.

 Here is the basic idea,
 o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
 o  at endio, after we finish updating fs tree, we use backref walking to find
all parents of the ranges and re-link them with the new COWed file layout 
 by
adding corresponding backrefs.

 Originally patch by Li Zefan l...@cn.fujitsu.com
 Signed-off-by: Liu Bo bo.li@oracle.com
 
 I'm hitting the WARN_ON in record_extent_backrefs() indicating a
 problem with the return value from iterate_inodes_from_logical().
 
 [ 6865.184782] [ cut here ]
 [ 6865.184819] WARNING: at fs/btrfs/inode.c:2062
 record_extent_backrefs+0xe5/0xe7 [btrfs]()
 [ 6865.184823] Hardware name: OptiPlex 745
 [ 6865.184825] Modules linked in: lpc_ich mfd_core xts gf128mul cryptd
 aes_x86_64 sha256_generic btrfs libcrc32c
 [ 6865.184841] Pid: 4239, comm: btrfs-endio-wri Not tainted 3.5.4-git-local+ 
 #1
 [ 6865.184844] Call Trace:
 [ 6865.184856]  [81031d6a] warn_slowpath_common+0x74/0xa2
 [ 6865.184862]  [81031db2] warn_slowpath_null+0x1a/0x1c
 [ 6865.184884]  [a003356b] record_extent_backrefs+0xe5/0xe7 [btrfs]
 [ 6865.184908]  [a003cf3a] btrfs_finish_ordered_io+0x131/0xa4b 
 [btrfs]
 [ 6865.184930]  [a003d869] finish_ordered_fn+0x15/0x17 [btrfs]
 [ 6865.184951]  [a005882f] worker_loop+0x145/0x516 [btrfs]
 [ 6865.184959]  [81059727] ? __wake_up_common+0x54/0x84
 [ 6865.184983]  [a00586ea] ? btrfs_queue_worker+0x2d3/0x2d3 [btrfs]
 [ 6865.184989]  [810516bb] kthread+0x93/0x98
 [ 6865.184996]  [817d7934] kernel_thread_helper+0x4/0x10
 [ 6865.185001]  [81051628] ? kthread_freezable_should_stop+0x6a/0x6a
 [ 6865.185021]  [817d7930] ? gs_change+0xb/0xb
 [ 6865.185025] ---[ end trace 26cc0e186efc79d8 ]---
 
 
 I'm testing a 3.5.4 kernel merged with 3.6_rc patchset as well as the
 send_recv patches and most of the btrfs-next patches.
 
 I'm running into this issue when mounting with autodefrag, and running
 some snapshot tests.
 
 This may be related to a problem elsewhere, because I've been
 encountering other backref issues even before testing this patch.
 

Oh, will look into it, thanks for the report.

thanks,
liubo

--
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


Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-09-18 Thread Josef Bacik
On Mon, Sep 17, 2012 at 06:23:21PM -0600, Liu Bo wrote:
 On 09/18/2012 01:15 AM, Josef Bacik wrote:
  On Mon, Sep 17, 2012 at 03:58:56AM -0600, Liu Bo wrote:
  This comes from one of btrfs's project ideas,
  As we defragment files, we break any sharing from other snapshots.
  The balancing code will preserve the sharing, and defrag needs to grow this
  as well.
 
  Now we're able to fill the blank with this patch, in which we make full 
  use of
  backref walking stuff.
 
  Here is the basic idea,
  o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
  o  at endio, after we finish updating fs tree, we use backref walking to 
  find
 all parents of the ranges and re-link them with the new COWed file 
  layout by
 adding corresponding backrefs.
 
  Originally patch by Li Zefan l...@cn.fujitsu.com
  Signed-off-by: Liu Bo bo.li@oracle.com
  
  I was trying to fixup the rejects on this patch when I noticed there were no
  tabs, only spaces.  Thats not going to work and now I have to go back and 
  make
  sure none of your other patches did this.  Thanks,
  
  Josef
  
 
 I'm quite confused about this, my local part and the email part I received 
 show it is well formed.
 
 There is no spaces, and every time before I send them out, I use 
 checkpatch.pl and make sure
 checkpatch.pl does not complain about anything:
 
 $ ./scripts/checkpatch.pl ~/Desktop/0002-Btrfs-snapshot-aware-defrag.patch 
 total: 0 errors, 0 warnings, 647 lines checked
 
 /home/liubo/Desktop/0002-Btrfs-snapshot-aware-defrag.patch has no obvious 
 style problems and is ready for submission.
 
 
 Anyway, I don't know where goes wrong but I'm attaching it for you.
 

Yeah I don't know wtf is going on here, that patch turned out fine.  I blame
exchange ;).  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 2/2 v3] Btrfs: snapshot-aware defrag

2012-09-17 Thread Liu Bo
This comes from one of btrfs's project ideas,
As we defragment files, we break any sharing from other snapshots.
The balancing code will preserve the sharing, and defrag needs to grow this
as well.

Now we're able to fill the blank with this patch, in which we make full use of
backref walking stuff.

Here is the basic idea,
o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
o  at endio, after we finish updating fs tree, we use backref walking to find
   all parents of the ranges and re-link them with the new COWed file layout by
   adding corresponding backrefs.

Originally patch by Li Zefan l...@cn.fujitsu.com
Signed-off-by: Liu Bo bo.li@oracle.com
---
Changes since v2:
- adopt better names for local structures.
- add proper reschedule phrase
- better error handling
- minor cleanups
(Thanks, David)

 fs/btrfs/inode.c |  617 ++
 1 files changed, 617 insertions(+), 0 deletions(-)

diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 55857eb..8278aa2 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -54,6 +54,7 @@
 #include locking.h
 #include free-space-cache.h
 #include inode-map.h
+#include backref.h
 
 struct btrfs_iget_args {
u64 ino;
@@ -1846,6 +1847,608 @@ out:
return ret;
 }
 
+/* snapshot-aware defrag */
+struct sa_defrag_extent_backref {
+   struct rb_node node;
+   struct old_sa_defrag_extent *old;
+   u64 root_id;
+   u64 inum;
+   u64 file_pos;
+   u64 extent_offset;
+   u64 num_bytes;
+   u64 generation;
+};
+
+struct old_sa_defrag_extent {
+   struct list_head list;
+   struct new_sa_defrag_extent *new;
+
+   u64 extent_offset;
+   u64 bytenr;
+   u64 offset;
+   u64 len;
+   int count;
+};
+
+struct new_sa_defrag_extent {
+   struct rb_root root;
+   struct list_head head;
+   struct btrfs_path *path;
+   struct inode *inode;
+   u64 file_pos;
+   u64 len;
+   u64 bytenr;
+   u64 disk_len;
+   u8 compress_type;
+};
+
+static int backref_comp(struct sa_defrag_extent_backref *b1,
+   struct sa_defrag_extent_backref *b2)
+{
+   if (b1-root_id  b2-root_id)
+   return -1;
+   else if (b1-root_id  b2-root_id)
+   return 1;
+
+   if (b1-inum  b2-inum)
+   return -1;
+   else if (b1-inum  b2-inum)
+   return 1;
+
+   if (b1-file_pos  b2-file_pos)
+   return -1;
+   else if (b1-file_pos  b2-file_pos)
+   return 1;
+
+   WARN_ON(1);
+   return 0;
+}
+
+static void backref_insert(struct rb_root *root,
+  struct sa_defrag_extent_backref *backref)
+{
+   struct rb_node **p = root-rb_node;
+   struct rb_node *parent = NULL;
+   struct sa_defrag_extent_backref *entry;
+   int ret;
+
+   while (*p) {
+   parent = *p;
+   entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
+
+   ret = backref_comp(backref, entry);
+   if (ret  0)
+   p = (*p)-rb_left;
+   else if (ret  0)
+   p = (*p)-rb_right;
+   else
+   BUG_ON(1);
+   }
+
+   rb_link_node(backref-node, parent, p);
+   rb_insert_color(backref-node, root);
+}
+
+/*
+ * Note the backref might has changed, and in this case we just return 0.
+ */
+static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
+  void *ctx)
+{
+   struct btrfs_file_extent_item *extent;
+   struct btrfs_fs_info *fs_info;
+   struct old_sa_defrag_extent *old = ctx;
+   struct new_sa_defrag_extent *new = old-new;
+   struct btrfs_path *path = new-path;
+   struct btrfs_key key;
+   struct btrfs_root *root;
+   struct sa_defrag_extent_backref *backref;
+   struct extent_buffer *leaf;
+   struct inode *inode = new-inode;
+   int slot;
+   int ret;
+   u64 extent_offset;
+   u64 num_bytes;
+
+   if (BTRFS_I(inode)-root-root_key.objectid == root_id 
+   inum == btrfs_ino(inode))
+   return 0;
+
+   key.objectid = root_id;
+   key.type = BTRFS_ROOT_ITEM_KEY;
+   key.offset = (u64)-1;
+
+   fs_info = BTRFS_I(inode)-root-fs_info;
+   root = btrfs_read_fs_root_no_name(fs_info, key);
+   if (IS_ERR(root)) {
+   if (PTR_ERR(root) == -ENOENT)
+   return 0;
+   WARN_ON(1);
+   pr_debug(inum=%llu, offset=%llu, root_id=%llu\n,
+inum, offset, root_id);
+   return PTR_ERR(root);
+   }
+
+   key.objectid = inum;
+   key.type = BTRFS_EXTENT_DATA_KEY;
+   if (offset  (u64)-1  32)
+   key.offset = 0;
+   else
+   key.offset = offset;
+
+   ret = 

Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-09-17 Thread Liu Bo
Please only push this one since the first one remains unchanged, I also posted 
it for
others to better review.

thanks,
liubo

On 09/17/2012 05:58 PM, Liu Bo wrote:
 This comes from one of btrfs's project ideas,
 As we defragment files, we break any sharing from other snapshots.
 The balancing code will preserve the sharing, and defrag needs to grow this
 as well.
 
 Now we're able to fill the blank with this patch, in which we make full use of
 backref walking stuff.
 
 Here is the basic idea,
 o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
 o  at endio, after we finish updating fs tree, we use backref walking to find
all parents of the ranges and re-link them with the new COWed file layout 
 by
adding corresponding backrefs.
 
 Originally patch by Li Zefan l...@cn.fujitsu.com
 Signed-off-by: Liu Bo bo.li@oracle.com
 ---
 Changes since v2:
 - adopt better names for local structures.
 - add proper reschedule phrase
 - better error handling
 - minor cleanups
   (Thanks, David)
 
  fs/btrfs/inode.c |  617 
 ++
  1 files changed, 617 insertions(+), 0 deletions(-)
 
 diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
 index 55857eb..8278aa2 100644
 --- a/fs/btrfs/inode.c
 +++ b/fs/btrfs/inode.c
 @@ -54,6 +54,7 @@
  #include locking.h
  #include free-space-cache.h
  #include inode-map.h
 +#include backref.h
  
  struct btrfs_iget_args {
   u64 ino;
 @@ -1846,6 +1847,608 @@ out:
   return ret;
  }
  
 +/* snapshot-aware defrag */
 +struct sa_defrag_extent_backref {
 + struct rb_node node;
 + struct old_sa_defrag_extent *old;
 + u64 root_id;
 + u64 inum;
 + u64 file_pos;
 + u64 extent_offset;
 + u64 num_bytes;
 + u64 generation;
 +};
 +
 +struct old_sa_defrag_extent {
 + struct list_head list;
 + struct new_sa_defrag_extent *new;
 +
 + u64 extent_offset;
 + u64 bytenr;
 + u64 offset;
 + u64 len;
 + int count;
 +};
 +
 +struct new_sa_defrag_extent {
 + struct rb_root root;
 + struct list_head head;
 + struct btrfs_path *path;
 + struct inode *inode;
 + u64 file_pos;
 + u64 len;
 + u64 bytenr;
 + u64 disk_len;
 + u8 compress_type;
 +};
 +
 +static int backref_comp(struct sa_defrag_extent_backref *b1,
 + struct sa_defrag_extent_backref *b2)
 +{
 + if (b1-root_id  b2-root_id)
 + return -1;
 + else if (b1-root_id  b2-root_id)
 + return 1;
 +
 + if (b1-inum  b2-inum)
 + return -1;
 + else if (b1-inum  b2-inum)
 + return 1;
 +
 + if (b1-file_pos  b2-file_pos)
 + return -1;
 + else if (b1-file_pos  b2-file_pos)
 + return 1;
 +
 + WARN_ON(1);
 + return 0;
 +}
 +
 +static void backref_insert(struct rb_root *root,
 +struct sa_defrag_extent_backref *backref)
 +{
 + struct rb_node **p = root-rb_node;
 + struct rb_node *parent = NULL;
 + struct sa_defrag_extent_backref *entry;
 + int ret;
 +
 + while (*p) {
 + parent = *p;
 + entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
 +
 + ret = backref_comp(backref, entry);
 + if (ret  0)
 + p = (*p)-rb_left;
 + else if (ret  0)
 + p = (*p)-rb_right;
 + else
 + BUG_ON(1);
 + }
 +
 + rb_link_node(backref-node, parent, p);
 + rb_insert_color(backref-node, root);
 +}
 +
 +/*
 + * Note the backref might has changed, and in this case we just return 0.
 + */
 +static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
 +void *ctx)
 +{
 + struct btrfs_file_extent_item *extent;
 + struct btrfs_fs_info *fs_info;
 + struct old_sa_defrag_extent *old = ctx;
 + struct new_sa_defrag_extent *new = old-new;
 + struct btrfs_path *path = new-path;
 + struct btrfs_key key;
 + struct btrfs_root *root;
 + struct sa_defrag_extent_backref *backref;
 + struct extent_buffer *leaf;
 + struct inode *inode = new-inode;
 + int slot;
 + int ret;
 + u64 extent_offset;
 + u64 num_bytes;
 +
 + if (BTRFS_I(inode)-root-root_key.objectid == root_id 
 + inum == btrfs_ino(inode))
 + return 0;
 +
 + key.objectid = root_id;
 + key.type = BTRFS_ROOT_ITEM_KEY;
 + key.offset = (u64)-1;
 +
 + fs_info = BTRFS_I(inode)-root-fs_info;
 + root = btrfs_read_fs_root_no_name(fs_info, key);
 + if (IS_ERR(root)) {
 + if (PTR_ERR(root) == -ENOENT)
 + return 0;
 + WARN_ON(1);
 + pr_debug(inum=%llu, offset=%llu, root_id=%llu\n,
 +  inum, offset, root_id);
 + return PTR_ERR(root);
 + }
 +
 + key.objectid = inum;
 + key.type = 

Re: [PATCH 2/2 v3] Btrfs: snapshot-aware defrag

2012-09-17 Thread Josef Bacik
On Mon, Sep 17, 2012 at 03:58:56AM -0600, Liu Bo wrote:
 This comes from one of btrfs's project ideas,
 As we defragment files, we break any sharing from other snapshots.
 The balancing code will preserve the sharing, and defrag needs to grow this
 as well.
 
 Now we're able to fill the blank with this patch, in which we make full use of
 backref walking stuff.
 
 Here is the basic idea,
 o  set the writeback ranges started by defragment with flag EXTENT_DEFRAG
 o  at endio, after we finish updating fs tree, we use backref walking to find
all parents of the ranges and re-link them with the new COWed file layout 
 by
adding corresponding backrefs.
 
 Originally patch by Li Zefan l...@cn.fujitsu.com
 Signed-off-by: Liu Bo bo.li@oracle.com

I was trying to fixup the rejects on this patch when I noticed there were no
tabs, only spaces.  Thats not going to work and now I have to go back and make
sure none of your other patches did this.  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