Add ext4-printk-throttling patch
[ext4-patch-queue.git] / ext4-fiemap.patch
blobf2af39d369b40513b0c8aee8dfd36e7da2b766b0
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>
9 ---
10 fs/ext4/ext4.h | 2 +
11 fs/ext4/ext4_extents.h | 15 +++
12 fs/ext4/extents.c | 248 ++++++++++++++++++++++++++++++++++++++++++++++++
13 fs/ext4/file.c | 4 +
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
38 +/*
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)
43 + */
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
49 +#define EXT_BREAK 1
50 +#define EXT_REPEAT 2
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
67 @@ -40,6 +40,7 @@
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:
76 return err;
79 +int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
80 + ext4_lblk_t num, ext_prepare_callback func,
81 + void *cbdata)
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) {
94 + num = last - block;
95 + /* find extent for this block */
96 + path = ext4_ext_find_extent(inode, block, path);
97 + if (IS_ERR(path)) {
98 + err = PTR_ERR(path);
99 + path = NULL;
100 + break;
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);
108 + exists = 0;
109 + if (!ex) {
110 + /* there is no extent yet, so try to allocate
111 + * all requested space */
112 + start = block;
113 + end = block + num;
114 + } else if (le32_to_cpu(ex->ee_block) > block) {
115 + /* need to allocate space before found extent */
116 + start = block;
117 + end = le32_to_cpu(ex->ee_block);
118 + if (block + num < end)
119 + end = block + num;
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 */
123 + start = block;
124 + end = block + num;
125 + if (end >= next)
126 + end = next;
127 + } else if (block >= le32_to_cpu(ex->ee_block)) {
128 + /*
129 + * some part of requested space is covered
130 + * by found extent
131 + */
132 + start = block;
133 + end = le32_to_cpu(ex->ee_block)
134 + + ext4_ext_get_actual_len(ex);
135 + if (block + num < end)
136 + end = block + num;
137 + exists = 1;
138 + } else {
139 + BUG();
141 + BUG_ON(end <= start);
143 + if (!exists) {
144 + cbex.ec_block = start;
145 + cbex.ec_len = end - start;
146 + cbex.ec_start = 0;
147 + cbex.ec_type = EXT4_EXT_CACHE_GAP;
148 + } else {
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);
159 + if (err < 0)
160 + break;
162 + if (err == EXT_REPEAT)
163 + continue;
164 + else if (err == EXT_BREAK) {
165 + err = 0;
166 + break;
169 + if (ext_depth(inode) != depth) {
170 + /* depth was changed. we have to realloc path */
171 + kfree(path);
172 + path = NULL;
175 + block = cbex.ec_block + cbex.ec_len;
178 + if (path) {
179 + ext4_ext_drop_refs(path);
180 + kfree(path);
183 + return err;
186 static void
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.
196 + */
197 +int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
198 + struct ext4_ext_cache *newex, struct ext4_extent *ex,
199 + void *data)
201 + struct fiemap_extent_info *fieinfo = data;
202 + unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
203 + __u64 logical;
204 + __u64 physical;
205 + __u64 length;
206 + __u32 flags = 0;
207 + int error;
209 + logical = (__u64)newex->ec_block << blksize_bits;
211 + if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
212 + pgoff_t offset;
213 + struct page *page;
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);
223 + if (!bh)
224 + return EXT_CONTINUE;
226 + if (buffer_delay(bh)) {
227 + flags |= FIEMAP_EXTENT_DELALLOC;
228 + page_cache_release(page);
229 + } else {
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;
241 + /*
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
248 + */
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);
255 + if (error < 0)
256 + return error;
257 + if (error == 1)
258 + return EXT_BREAK;
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;
269 + __u64 length;
270 + __u32 flags = FIEMAP_EXTENT_LAST;
271 + int blockbits = inode->i_sb->s_blocksize_bits;
272 + int error = 0;
274 + /* in-inode? */
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);
280 + if (error)
281 + return error;
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;
293 + if (physical)
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;
304 + int error = 0;
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,
309 + ext4_get_block);
311 + if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
312 + return -EBADR;
314 + if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
315 + error = ext4_xattr_fiemap(inode, fieinfo);
316 + } else {
317 + start_blk = start >> inode->i_sb->s_blocksize_bits;
318 + len_blks = len >> inode->i_sb->s_blocksize_bits;
320 + /*
321 + * Walk the extent tree gathering extent information.
322 + * ext4_ext_fiemap_cb will push extents back to user.
323 + */
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);
330 + return error;
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
338 return 0;
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_
348 #endif
349 .permission = ext4_permission,
350 .fallocate = ext4_fallocate,
351 + .fiemap = ext4_fiemap,