1 From: Eric Sandeen <sandeen@redhat.com>
3 Hook ext4 to the vfs fiemap interface.
5 ext4_ext_walk_space() was reinstated to be used for iterating over file
6 extents with a callback; it is used by the ext4 fiemap implementation.
8 Signed-off-by: Eric Sandeen <sandeen@redhat.com>
11 fs/ext4/ext4_extents.h | 15 +++
12 fs/ext4/extents.c | 248 ++++++++++++++++++++++++++++++++++++++++++++++++
14 fs/ext4/inode.c | 2 +-
15 5 files changed, 270 insertions(+), 1 deletions(-)
17 Index: linux-2.6/fs/ext4/ext4.h
18 ===================================================================
19 --- linux-2.6.orig/fs/ext4/ext4.h 2008-07-14 16:51:40.000000000 -0500
20 +++ linux-2.6/fs/ext4/ext4.h 2008-07-14 16:56:12.038353463 -0500
21 @@ -1127,6 +1127,8 @@ struct buffer_head *ext4_getblk(handle_t
22 ext4_lblk_t, int, int *);
23 struct buffer_head *ext4_bread(handle_t *, struct inode *,
24 ext4_lblk_t, int, int *);
25 +int ext4_get_block(struct inode *inode, sector_t iblock,
26 + struct buffer_head *bh_result, int create);
27 int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
28 ext4_lblk_t iblock, unsigned long maxblocks,
29 struct buffer_head *bh_result,
30 Index: linux-2.6/fs/ext4/ext4_extents.h
31 ===================================================================
32 --- linux-2.6.orig/fs/ext4/ext4_extents.h 2008-07-14 16:51:41.000000000 -0500
33 +++ linux-2.6/fs/ext4/ext4_extents.h 2008-07-14 16:56:12.040354241 -0500
34 @@ -124,6 +124,19 @@ struct ext4_ext_path {
35 #define EXT4_EXT_CACHE_GAP 1
36 #define EXT4_EXT_CACHE_EXTENT 2
39 + * to be called by ext4_ext_walk_space()
40 + * negative retcode - error
41 + * positive retcode - signal for ext4_ext_walk_space(), see below
42 + * callback must return valid extent (passed or newly created)
44 +typedef int (*ext_prepare_callback)(struct inode *, struct ext4_ext_path *,
45 + struct ext4_ext_cache *,
46 + struct ext4_extent *, void *);
48 +#define EXT_CONTINUE 0
52 #define EXT_MAX_BLOCK 0xffffffff
54 @@ -222,6 +235,8 @@ extern int ext4_ext_try_to_merge(struct
55 struct ext4_extent *);
56 extern unsigned int ext4_ext_check_overlap(struct inode *, struct ext4_extent *, struct ext4_ext_path *);
57 extern int ext4_ext_insert_extent(handle_t *, struct inode *, struct ext4_ext_path *, struct ext4_extent *);
58 +extern int ext4_ext_walk_space(struct inode *, ext4_lblk_t, ext4_lblk_t,
59 + ext_prepare_callback, void *);
60 extern struct ext4_ext_path *ext4_ext_find_extent(struct inode *, ext4_lblk_t,
61 struct ext4_ext_path *);
62 extern int ext4_ext_search_left(struct inode *, struct ext4_ext_path *,
63 Index: linux-2.6/fs/ext4/extents.c
64 ===================================================================
65 --- linux-2.6.orig/fs/ext4/extents.c 2008-07-14 16:51:41.000000000 -0500
66 +++ linux-2.6/fs/ext4/extents.c 2008-07-14 16:56:12.044353533 -0500
68 #include <linux/slab.h>
69 #include <linux/falloc.h>
70 #include <asm/uaccess.h>
71 +#include <linux/fiemap.h>
72 #include "ext4_jbd2.h"
73 #include "ext4_extents.h"
75 @@ -1656,6 +1657,113 @@ cleanup:
79 +int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
80 + ext4_lblk_t num, ext_prepare_callback func,
83 + struct ext4_ext_path *path = NULL;
84 + struct ext4_ext_cache cbex;
85 + struct ext4_extent *ex;
86 + ext4_lblk_t next, start = 0, end = 0;
87 + ext4_lblk_t last = block + num;
88 + int depth, exists, err = 0;
90 + BUG_ON(func == NULL);
91 + BUG_ON(inode == NULL);
93 + while (block < last && block != EXT_MAX_BLOCK) {
95 + /* find extent for this block */
96 + path = ext4_ext_find_extent(inode, block, path);
98 + err = PTR_ERR(path);
103 + depth = ext_depth(inode);
104 + BUG_ON(path[depth].p_hdr == NULL);
105 + ex = path[depth].p_ext;
106 + next = ext4_ext_next_allocated_block(path);
110 + /* there is no extent yet, so try to allocate
111 + * all requested space */
114 + } else if (le32_to_cpu(ex->ee_block) > block) {
115 + /* need to allocate space before found extent */
117 + end = le32_to_cpu(ex->ee_block);
118 + if (block + num < end)
120 + } else if (block >= le32_to_cpu(ex->ee_block)
121 + + ext4_ext_get_actual_len(ex)) {
122 + /* need to allocate space after found extent */
127 + } else if (block >= le32_to_cpu(ex->ee_block)) {
129 + * some part of requested space is covered
133 + end = le32_to_cpu(ex->ee_block)
134 + + ext4_ext_get_actual_len(ex);
135 + if (block + num < end)
141 + BUG_ON(end <= start);
144 + cbex.ec_block = start;
145 + cbex.ec_len = end - start;
147 + cbex.ec_type = EXT4_EXT_CACHE_GAP;
149 + cbex.ec_block = le32_to_cpu(ex->ee_block);
150 + cbex.ec_len = ext4_ext_get_actual_len(ex);
151 + cbex.ec_start = ext_pblock(ex);
152 + cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
155 + BUG_ON(cbex.ec_len == 0);
156 + err = func(inode, path, &cbex, ex, cbdata);
157 + ext4_ext_drop_refs(path);
162 + if (err == EXT_REPEAT)
164 + else if (err == EXT_BREAK) {
169 + if (ext_depth(inode) != depth) {
170 + /* depth was changed. we have to realloc path */
175 + block = cbex.ec_block + cbex.ec_len;
179 + ext4_ext_drop_refs(path);
187 ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
188 __u32 len, ext4_fsblk_t start, int type)
189 @@ -3011,3 +3119,143 @@ retry:
190 mutex_unlock(&inode->i_mutex);
191 return ret > 0 ? ret2 : ret;
195 + * Callback function called for each extent to gather FIEMAP information.
197 +int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
198 + struct ext4_ext_cache *newex, struct ext4_extent *ex,
201 + struct fiemap_extent_info *fieinfo = data;
202 + unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
209 + logical = (__u64)newex->ec_block << blksize_bits;
211 + if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
214 + struct buffer_head *bh = NULL;
216 + offset = logical >> PAGE_SHIFT;
217 + page = find_get_page(inode->i_mapping, offset);
218 + if (!page || !page_has_buffers(page))
219 + return EXT_CONTINUE;
221 + bh = page_buffers(page);
224 + return EXT_CONTINUE;
226 + if (buffer_delay(bh)) {
227 + flags |= FIEMAP_EXTENT_DELALLOC;
228 + page_cache_release(page);
230 + page_cache_release(page);
231 + return EXT_CONTINUE;
235 + physical = (__u64)newex->ec_start << blksize_bits;
236 + length = (__u64)newex->ec_len << blksize_bits;
238 + if (ex && ext4_ext_is_uninitialized(ex))
239 + flags |= FIEMAP_EXTENT_UNWRITTEN;
242 + * If this extent reaches EXT_MAX_BLOCK, it must be last.
244 + * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
245 + * this also indicates no more allocated blocks.
247 + * XXX this might miss a single-block extent at EXT_MAX_BLOCK
249 + if (logical + length - 1 == EXT_MAX_BLOCK ||
250 + ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK)
251 + flags |= FIEMAP_EXTENT_LAST;
253 + error = fiemap_fill_next_extent(fieinfo, logical, physical,
254 + length, flags, inode->i_sb->s_dev);
260 + return EXT_CONTINUE;
263 +/* fiemap flags we can handle specified here */
264 +#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
266 +int ext4_xattr_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo)
268 + __u64 physical = 0;
270 + __u32 flags = FIEMAP_EXTENT_LAST;
271 + int blockbits = inode->i_sb->s_blocksize_bits;
275 + if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
276 + struct ext4_iloc iloc;
277 + int offset; /* offset of xattr in inode */
279 + error = ext4_get_inode_loc(inode, &iloc);
282 + physical = iloc.bh->b_blocknr << blockbits;
283 + offset = EXT4_GOOD_OLD_INODE_SIZE +
284 + EXT4_I(inode)->i_extra_isize;
285 + physical += offset;
286 + length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
287 + flags |= FIEMAP_EXTENT_DATA_INLINE;
288 + } else { /* external block */
289 + physical = EXT4_I(inode)->i_file_acl << blockbits;
290 + length = inode->i_sb->s_blocksize;
294 + error = fiemap_fill_next_extent(fieinfo, 0, physical,
295 + length, flags, inode->i_sb->s_dev);
296 + return (error < 0 ? error : 0);
299 +int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
300 + __u64 start, __u64 len)
302 + ext4_lblk_t start_blk;
303 + ext4_lblk_t len_blks;
306 + /* fallback to generic here if not in extents fmt */
307 + if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
308 + return generic_block_fiemap(inode, fieinfo, start, len,
311 + if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
314 + if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
315 + error = ext4_xattr_fiemap(inode, fieinfo);
317 + start_blk = start >> inode->i_sb->s_blocksize_bits;
318 + len_blks = len >> inode->i_sb->s_blocksize_bits;
321 + * Walk the extent tree gathering extent information.
322 + * ext4_ext_fiemap_cb will push extents back to user.
324 + down_write(&EXT4_I(inode)->i_data_sem);
325 + error = ext4_ext_walk_space(inode, start_blk, len_blks,
326 + ext4_ext_fiemap_cb, fieinfo);
327 + up_write(&EXT4_I(inode)->i_data_sem);
333 Index: linux-2.6/fs/ext4/file.c
334 ===================================================================
335 --- linux-2.6.orig/fs/ext4/file.c 2008-07-14 16:51:40.000000000 -0500
336 +++ linux-2.6/fs/ext4/file.c 2008-07-14 16:56:12.066353577 -0500
337 @@ -140,6 +140,9 @@ static int ext4_file_mmap(struct file *f
341 +extern int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
342 + __u64 start, __u64 len);
344 const struct file_operations ext4_file_operations = {
345 .llseek = generic_file_llseek,
346 .read = do_sync_read,
347 @@ -170,5 +173,6 @@ const struct inode_operations ext4_file_
349 .permission = ext4_permission,
350 .fallocate = ext4_fallocate,
351 + .fiemap = ext4_fiemap,