1 A large part of this code is from the generic VFS code in fs/ioctl.c in the
4 Index: linux-2.6.18-53.1.14/fs/ext3/ioctl.c
5 ===================================================================
6 --- linux-2.6.18-53.1.14.orig/fs/ext3/ioctl.c
7 +++ linux-2.6.18-53.1.14/fs/ext3/ioctl.c
9 #include <linux/time.h>
10 #include <asm/uaccess.h>
11 #include <linux/namei.h>
14 +/* So that the fiemap access checks can't overflow on 32 bit machines. */
15 +#define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
18 + * fiemap_fill_next_extent - Fiemap helper function
19 + * @fieinfo: Fiemap context passed into ->fiemap
20 + * @logical: Extent logical start offset, in bytes
21 + * @phys: Extent physical start offset, in bytes
22 + * @len: Extent length, in bytes
23 + * @flags: FIEMAP_EXTENT flags that describe this extent
24 + * @lun: LUN on which this extent resides
26 + * Called from file system ->fiemap callback. Will populate extent
27 + * info as passed in via arguments and copy to user memory. On
28 + * success, extent count on fieinfo is incremented.
30 + * Returns 0 on success, -errno on error, 1 if this was the last
31 + * extent that will fit in user array.
33 +#define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
34 +#define SET_NO_DIRECT_FLAGS (FIEMAP_EXTENT_DATA_COMPRESSED \
35 + |FIEMAP_EXTENT_DATA_ENCRYPTED \
37 +#define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
38 +int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
39 + u64 phys, u64 len, u32 flags, dev_t dev)
41 + struct fiemap_extent extent = { 0 };
42 + struct fiemap_extent *dest = fieinfo->fi_extents_start;
44 + /* only count the extents */
45 + if (fieinfo->fi_extents_max == 0) {
46 + fieinfo->fi_extents_mapped++;
47 + return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
50 + if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
53 + if (flags & SET_UNKNOWN_FLAGS)
54 + flags |= FIEMAP_EXTENT_UNKNOWN;
55 + if (flags & SET_NO_DIRECT_FLAGS)
56 + flags |= FIEMAP_EXTENT_NO_DIRECT;
57 + if (flags & SET_NOT_ALIGNED_FLAGS)
58 + flags |= FIEMAP_EXTENT_NOT_ALIGNED;
60 + extent.fe_logical = logical;
61 + extent.fe_physical = phys;
62 + extent.fe_length = len;
63 + extent.fe_flags = flags;
64 + extent.fe_device = new_encode_dev(dev);
66 + dest += fieinfo->fi_extents_mapped;
67 + if (copy_to_user(dest, &extent, sizeof(extent)))
70 + fieinfo->fi_extents_mapped++;
71 + if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
74 + return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
77 +static int fiemap_check_ranges(struct super_block *sb,
78 + u64 start, u64 len, u64 *new_len)
85 + if (start > sb->s_maxbytes)
89 + * Shrink request scope to what the fs can actually handle.
91 + if ((len > sb->s_maxbytes) ||
92 + (sb->s_maxbytes - len) < start)
93 + *new_len = sb->s_maxbytes - start;
99 + * fiemap_check_flags - check validity of requested flags for fiemap
100 + * @fieinfo: Fiemap context passed into ->fiemap
101 + * @fs_flags: Set of fiemap flags that the file system understands
103 + * Called from file system ->fiemap callback. This will compute the
104 + * intersection of valid fiemap flags and those that the fs supports. That
105 + * value is then compared against the user supplied flags. In case of bad user
106 + * flags, the invalid values will be written into the fieinfo structure, and
107 + * -EBADR is returned, which tells ioctl_fiemap() to return those values to
108 + * userspace. For this reason, a return code of -EBADR should be preserved.
110 + * Returns 0 on success, -EBADR on bad flags.
112 +int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
114 + u32 incompat_flags;
116 + incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
117 + if (incompat_flags) {
118 + fieinfo->fi_flags = incompat_flags;
125 +int ioctl_fiemap(struct inode *inode, struct file *filp, unsigned long arg)
127 + struct fiemap fiemap;
129 + struct fiemap_extent_info fieinfo = {0, };
130 + struct super_block *sb = inode->i_sb;
133 + if (copy_from_user(&fiemap, (struct fiemap __user *) arg,
134 + sizeof(struct fiemap)))
137 + if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
140 + error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
145 + fieinfo.fi_flags = fiemap.fm_flags;
146 + fieinfo.fi_extents_max = fiemap.fm_extent_count;
147 + fieinfo.fi_extents_start = (struct fiemap_extent *)(arg + sizeof(fiemap));
149 + if (fiemap.fm_extent_count != 0 &&
150 + !access_ok(VERIFY_WRITE, (void *)arg,
151 + offsetof(typeof(fiemap), fm_extents[fiemap.fm_extent_count])))
154 + if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
155 + filemap_write_and_wait(inode->i_mapping);
157 + error = ext3_fiemap(inode, &fieinfo, fiemap.fm_start, len);
158 + fiemap.fm_flags = fieinfo.fi_flags;
159 + fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
160 + if (copy_to_user((char *)arg, &fiemap, sizeof(fiemap)))
166 int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
168 @@ -272,7 +424,9 @@ flags_err:
173 + case EXT3_IOC_FIEMAP: {
174 + return ioctl_fiemap(inode, filp, arg);
179 Index: linux-2.6.18-53.1.14/include/linux/ext3_fs.h
180 ===================================================================
181 --- linux-2.6.18-53.1.14.orig/include/linux/ext3_fs.h
182 +++ linux-2.6.18-53.1.14/include/linux/ext3_fs.h
183 @@ -257,15 +257,19 @@ struct ext3_new_group_data {
184 #define EXT3_IOC_SETFLAGS _IOW('f', 2, long)
185 #define EXT3_IOC_GETVERSION _IOR('f', 3, long)
186 #define EXT3_IOC_SETVERSION _IOW('f', 4, long)
187 +#define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
188 +#define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
189 #define EXT3_IOC_GROUP_EXTEND _IOW('f', 7, unsigned long)
190 #define EXT3_IOC_GROUP_ADD _IOW('f', 8,struct ext3_new_group_input)
191 +#define EXT3_IOC_FIEMAP _IOWR('f', 10, struct fiemap)
192 #define EXT3_IOC_GETVERSION_OLD _IOR('v', 1, long)
193 #define EXT3_IOC_SETVERSION_OLD _IOW('v', 2, long)
194 #ifdef CONFIG_JBD_DEBUG
195 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
197 -#define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
198 -#define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
200 +/* FIEMAP flags supported by ext3 */
201 +#define EXT3_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC)
205 @@ -1040,6 +1044,9 @@ extern int ext3_block_truncate_page(hand
207 extern int ext3_ioctl (struct inode *, struct file *, unsigned int,
209 +struct fiemap_extent_info;
210 +extern int ext3_fiemap(struct inode *, struct fiemap_extent_info *, __u64,
214 extern int ext3_orphan_add(handle_t *, struct inode *);
215 @@ -1117,7 +1124,6 @@ ext3_get_blocks_wrap(handle_t *handle, s
220 #endif /* __KERNEL__ */
222 /* EXT3_IOC_CREATE_INUM at bottom of file (visible to kernel and user). */
223 Index: linux-2.6.18-53.1.14/include/linux/ext3_extents.h
224 ===================================================================
225 --- linux-2.6.18-53.1.14.orig/include/linux/ext3_extents.h
226 +++ linux-2.6.18-53.1.14/include/linux/ext3_extents.h
227 @@ -142,8 +142,10 @@ struct ext3_ext_path {
228 * callback must return valid extent (passed or newly created)
230 typedef int (*ext_prepare_callback)(struct inode *, struct ext3_ext_path *,
231 - struct ext3_ext_cache *,
233 + struct ext3_ext_cache *,
234 + struct ext3_extent *, void *);
236 +#define HAVE_EXT_PREPARE_CB_EXTENT
238 #define EXT_CONTINUE 0
240 @@ -152,6 +154,26 @@ typedef int (*ext_prepare_callback)(stru
242 #define EXT_MAX_BLOCK 0xffffffff
245 + * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
246 + * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
247 + * MSB of ee_len field in the extent datastructure to signify if this
248 + * particular extent is an initialized extent or an uninitialized (i.e.
250 + * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
251 + * uninitialized extent.
252 + * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
253 + * uninitialized one. In other words, if MSB of ee_len is set, it is an
254 + * uninitialized extent with only one special scenario when ee_len = 0x8000.
255 + * In this case we can not have an uninitialized extent of zero length and
256 + * thus we make it as a special case of initialized extent with 0x8000 length.
257 + * This way we get better extent-to-group alignment for initialized extents.
258 + * Hence, the maximum number of blocks we can have in an *initialized*
259 + * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
261 +#define EXT_INIT_MAX_LEN (1UL << 15)
262 +#define EXT_UNINIT_MAX_LEN (EXT_INIT_MAX_LEN - 1)
264 #define EXT_FLAGS_CLR_UNKNOWN 0x7 /* Flags cleared on modification */
265 #define EXT_HDR_GEN_BITS 24
266 #define EXT_HDR_GEN_MASK ((1 << EXT_HDR_GEN_BITS) - 1)
267 @@ -219,6 +241,12 @@ ext3_ext_invalidate_cache(struct inode *
268 EXT3_I(inode)->i_cached_extent.ec_type = EXT3_EXT_CACHE_NO;
271 +static inline int ext3_ext_is_uninitialized(struct ext3_extent *ext)
273 + /* Extent with ee_len of 0x8000 is treated as an initialized extent */
274 + return (le16_to_cpu(ext->ee_len) > EXT_INIT_MAX_LEN);
277 extern int ext3_ext_search_left(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
278 extern int ext3_ext_search_right(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
279 extern int ext3_extent_tree_init(handle_t *, struct inode *);
280 Index: linux-2.6.18-53.1.14/fs/ext3/extents.c
281 ===================================================================
282 --- linux-2.6.18-53.1.14.orig/fs/ext3/extents.c
283 +++ linux-2.6.18-53.1.14/fs/ext3/extents.c
285 #include <linux/string.h>
286 #include <linux/slab.h>
287 #include <linux/ext3_extents.h>
289 #include <asm/uaccess.h>
292 @@ -1481,7 +1482,7 @@ int ext3_ext_walk_space(struct inode *in
295 BUG_ON(cbex.ec_len == 0);
296 - err = func(inode, path, &cbex, cbdata);
297 + err = func(inode, path, &cbex, ex, cbdata);
298 ext3_ext_drop_refs(path);
301 @@ -2296,6 +2297,103 @@ int ext3_ext_writepage_trans_blocks(stru
306 + * Callback function called for each extent to gather FIEMAP information.
308 +int ext3_ext_fiemap_cb(struct inode *inode, struct ext3_ext_path *path,
309 + struct ext3_ext_cache *newex, struct ext3_extent *ex,
312 + struct fiemap_extent_info *fieinfo = data;
313 + unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
320 + logical = (__u64)newex->ec_block << blksize_bits;
322 + if (newex->ec_type == EXT3_EXT_CACHE_GAP) {
325 + struct buffer_head *bh = NULL;
327 + offset = logical >> PAGE_SHIFT;
328 + page = find_get_page(inode->i_mapping, offset);
329 + if (!page || !page_has_buffers(page))
330 + return EXT_CONTINUE;
332 + bh = page_buffers(page);
335 + return EXT_CONTINUE;
337 + if (buffer_delay(bh)) {
338 + flags |= FIEMAP_EXTENT_DELALLOC;
339 + page_cache_release(page);
341 + page_cache_release(page);
342 + return EXT_CONTINUE;
346 + physical = (__u64)newex->ec_start << blksize_bits;
347 + length = (__u64)newex->ec_len << blksize_bits;
349 + if (ex && ext3_ext_is_uninitialized(ex))
350 + flags |= FIEMAP_EXTENT_UNWRITTEN;
353 + * If this extent reaches EXT_MAX_BLOCK, it must be last.
355 + * Or if ext3_ext_next_allocated_block is EXT_MAX_BLOCK,
356 + * this indicates no more allocated blocks.
358 + * XXX this might miss a single-block extent at EXT_MAX_BLOCK
360 + if (logical + length - 1 == EXT_MAX_BLOCK ||
361 + ext3_ext_next_allocated_block(path) == EXT_MAX_BLOCK)
362 + flags |= FIEMAP_EXTENT_LAST;
364 + error = fiemap_fill_next_extent(fieinfo, logical, physical,
365 + length, flags, inode->i_sb->s_dev);
371 + return EXT_CONTINUE;
374 +int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
375 + __u64 start, __u64 len)
377 + ext3_fsblk_t start_blk;
378 + ext3_fsblk_t len_blks;
381 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
382 + return -EOPNOTSUPP;
384 + if (fiemap_check_flags(fieinfo, EXT3_FIEMAP_FLAGS_COMPAT))
387 + start_blk = start >> inode->i_sb->s_blocksize_bits;
388 + len_blks = (len + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
391 + * Walk the extent tree gathering extent information.
392 + * ext3_ext_fiemap_cb will push extents back to user.
394 + mutex_lock(&EXT3_I(inode)->truncate_mutex);
395 + error = ext3_ext_walk_space(inode, start_blk, len_blks,
396 + ext3_ext_fiemap_cb, fieinfo);
397 + mutex_unlock(&EXT3_I(inode)->truncate_mutex);
402 EXPORT_SYMBOL(ext3_mark_inode_dirty);
403 EXPORT_SYMBOL(ext3_ext_invalidate_cache);
404 EXPORT_SYMBOL(ext3_ext_insert_extent);
405 Index: linux-2.6.18-53.1.14/fs/ext3/fiemap.h
406 ===================================================================
408 +++ linux-2.6.18-53.1.14/fs/ext3/fiemap.h
411 + * FIEMAP ioctl infrastructure.
413 + * Copyright 2008 Sun Microsystems, Inc.
415 + * Author: Kalpak Shah <kalpak.shah@sun.com>
416 + * Andreas Dilger <adilger@sun.com>
419 +#ifndef _LINUX_EXT3_FIEMAP_H
420 +#define _LINUX_EXT3_FIEMAP_H
422 +struct fiemap_extent {
423 + __u64 fe_logical; /* logical offset in bytes for the start of
424 + * the extent from the beginning of the file */
425 + __u64 fe_physical; /* physical offset in bytes for the start
426 + * of the extent from the beginning of the disk */
427 + __u64 fe_length; /* length in bytes for this extent */
428 + __u32 fe_flags; /* FIEMAP_EXTENT_* flags for this extent */
429 + __u32 fe_device; /* device number for this extent */
433 + __u64 fm_start; /* logical offset (inclusive) at
434 + * which to start mapping (in) */
435 + __u64 fm_length; /* logical length of mapping which
436 + * userspace wants (in) */
437 + __u32 fm_flags; /* FIEMAP_FLAG_* flags for request (in/out) */
438 + __u32 fm_mapped_extents;/* number of extents that were mapped (out) */
439 + __u32 fm_extent_count; /* size of fm_extents array (in) */
441 + struct fiemap_extent fm_extents[0]; /* array of mapped extents (out) */
445 + * FIEMAP helper definition.
447 +struct fiemap_extent_info {
448 + unsigned int fi_flags; /* Flags as passed from user */
449 + unsigned int fi_extents_mapped; /* Number of mapped extents */
450 + unsigned int fi_extents_max; /* Size of fiemap_extent array*/
451 + struct fiemap_extent *fi_extents_start; /* Start of fiemap_extent array */
454 +int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags);
455 +int fiemap_fill_next_extent(struct fiemap_extent_info *info, u64 logical,
456 + u64 phys, u64 len, u32 flags, u32 lun);
458 +#define FIEMAP_MAX_OFFSET (~0ULL)
460 +#define FIEMAP_FLAG_SYNC 0x00000001 /* sync file data before map */
461 +#define FIEMAP_FLAG_XATTR 0x00000002 /* map extended attribute tree */
463 +/* ldiskfs only supports FLAG_SYNC flag currently */
464 +#define FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
467 +#define FIEMAP_EXTENT_LAST 0x00000001 /* Last extent in file. */
468 +#define FIEMAP_EXTENT_UNKNOWN 0x00000002 /* Data location unknown. */
469 +#define FIEMAP_EXTENT_DELALLOC 0x00000004 /* Location still pending.
470 + * Sets EXTENT_UNKNOWN. */
471 +#define FIEMAP_EXTENT_NO_DIRECT 0x00000008 /* Data mapping undefined */
472 +#define FIEMAP_EXTENT_SECONDARY 0x00000010 /* Data copied offline. May
473 + * set EXTENT_NO_DIRECT. */
474 +#define FIEMAP_EXTENT_NET 0x00000020 /* Data stored remotely.
475 + * Sets EXTENT_NO_DIRECT. */
476 +#define FIEMAP_EXTENT_DATA_COMPRESSED 0x00000040 /* Data is compressed by fs.
477 + * Sets EXTENT_NO_DIRECT. */
478 +#define FIEMAP_EXTENT_DATA_ENCRYPTED 0x00000080 /* Data is encrypted by fs.
479 + * Sets EXTENT_NO_DIRECT. */
480 +#define FIEMAP_EXTENT_NOT_ALIGNED 0x00000100 /* Extent offsets may not be
481 + * block aligned. */
482 +#define FIEMAP_EXTENT_DATA_INLINE 0x00000200 /* Data mixed with metadata.
483 + * Sets EXTENT_NOT_ALIGNED.*/
484 +#define FIEMAP_EXTENT_DATA_TAIL 0x00000400 /* Multiple files in block.
485 + * Sets EXTENT_NOT_ALIGNED.*/
486 +#define FIEMAP_EXTENT_UNWRITTEN 0x00000800 /* Space allocated, but
487 + * no data (i.e. zero). */
488 +#define FIEMAP_EXTENT_MERGED 0x00001000 /* File does not natively
489 + * support extents. Result
490 + * merged for efficiency. */
492 +#endif /* _LINUX_EXT3_FIEMAP_H */