Skip to content

Commit fe09e16

Browse files
Liu BoChris Mason
authored andcommitted
Btrfs: export btrfs space shared info to userspace
Similar to ocfs2, btrfs also supports that extents can be shared by different inodes, and there are some userspace tools requesting for this kind of 'space shared infomation'.[1] ocfs2 uses flag FIEMAP_EXTENT_SHARED, so does btrfs. [1]: http://thr3ads.net/ocfs2-devel/2010/09/489052-PATCH-3-3-shared-du-using-fiemap-to-figure-up-the-shared-extents-per-file-and-the-footprint-in Reviewed-by: David Sterba <dsterba@suse.cz> Signed-off-by: Liu Bo <bo.li.liu@oracle.com> Signed-off-by: Josef Bacik <jbacik@fusionio.com> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
1 parent 7451432 commit fe09e16

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

‎fs/btrfs/extent_io.c‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "check-integrity.h"
2121
#include "locking.h"
2222
#include "rcu-string.h"
23+
#include "backref.h"
2324

2425
static struct kmem_cache *extent_state_cache;
2526
static struct kmem_cache *extent_buffer_cache;
@@ -4062,6 +4063,19 @@ static struct extent_map *get_extent_skip_holes(struct inode *inode,
40624063
return NULL;
40634064
}
40644065

4066+
static noinline int count_ext_ref(u64 inum, u64 offset, u64 root_id, void *ctx)
4067+
{
4068+
unsigned long cnt = *((unsigned long *)ctx);
4069+
4070+
cnt++;
4071+
*((unsigned long *)ctx) = cnt;
4072+
4073+
/* Now we're sure that the extent is shared. */
4074+
if (cnt > 1)
4075+
return 1;
4076+
return 0;
4077+
}
4078+
40654079
int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
40664080
__u64 start, __u64 len, get_extent_t *get_extent)
40674081
{
@@ -4128,7 +4142,7 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
41284142
last = found_key.offset;
41294143
last_for_get_extent = last + 1;
41304144
}
4131-
btrfs_free_path(path);
4145+
btrfs_release_path(path);
41324146

41334147
/*
41344148
* we might have some extents allocated but more delalloc past those
@@ -4198,7 +4212,24 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
41984212
flags |= (FIEMAP_EXTENT_DELALLOC |
41994213
FIEMAP_EXTENT_UNKNOWN);
42004214
} else {
4215+
unsigned long ref_cnt = 0;
4216+
42014217
disko = em->block_start + offset_in_extent;
4218+
4219+
/*
4220+
* As btrfs supports shared space, this information
4221+
* can be exported to userspace tools via
4222+
* flag FIEMAP_EXTENT_SHARED.
4223+
*/
4224+
ret = iterate_inodes_from_logical(
4225+
em->block_start,
4226+
BTRFS_I(inode)->root->fs_info,
4227+
path, count_ext_ref, &ref_cnt);
4228+
if (ret < 0 && ret != -ENOENT)
4229+
goto out_free;
4230+
4231+
if (ref_cnt > 1)
4232+
flags |= FIEMAP_EXTENT_SHARED;
42024233
}
42034234
if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
42044235
flags |= FIEMAP_EXTENT_ENCODED;
@@ -4230,6 +4261,7 @@ int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
42304261
out_free:
42314262
free_extent_map(em);
42324263
out:
4264+
btrfs_free_path(path);
42334265
unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
42344266
&cached_state, GFP_NOFS);
42354267
return ret;

0 commit comments

Comments
 (0)