1 Index: linux-2.6.9-67.0.15/fs/ext3/ioctl.c
2 ===================================================================
3 --- linux-2.6.9-67.0.15.orig/fs/ext3/ioctl.c
4 +++ linux-2.6.9-67.0.15/fs/ext3/ioctl.c
6 #include <linux/time.h>
7 #include <asm/uaccess.h>
8 #include <linux/namei.h>
12 int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
14 @@ -244,6 +244,9 @@ flags_err:
18 + case EXT3_IOC_FIEMAP: {
19 + return ext3_fiemap(inode, filp, cmd, arg);
24 Index: linux-2.6.9-67.0.15/include/linux/ext3_fs.h
25 ===================================================================
26 --- linux-2.6.9-67.0.15.orig/include/linux/ext3_fs.h
27 +++ linux-2.6.9-67.0.15/include/linux/ext3_fs.h
28 @@ -251,7 +251,6 @@ struct ext3_new_group_data {
29 __u32 free_blocks_count;
36 @@ -268,6 +267,8 @@ struct ext3_new_group_data {
38 #define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
39 #define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
40 +#define EXT3_IOC_FIEMAP _IOWR('f', 10, struct fiemap)
44 * Structure of an inode on the disk
45 @@ -813,6 +814,8 @@ static inline struct timespec ext3_curre
46 return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
47 current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
49 +extern int ext3_fiemap(struct inode *, struct file *, unsigned int,
53 * This structure is stuffed into the struct file's private_data field
54 Index: linux-2.6.9-67.0.15/include/linux/ext3_extents.h
55 ===================================================================
56 --- linux-2.6.9-67.0.15.orig/include/linux/ext3_extents.h
57 +++ linux-2.6.9-67.0.15/include/linux/ext3_extents.h
58 @@ -170,7 +170,9 @@ struct ext3_extents_helpers {
60 typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
61 struct ext3_ext_path *,
62 - struct ext3_ext_cache *);
63 + struct ext3_ext_cache *,
64 + struct ext3_extent *);
65 +#define HAVE_EXT_PREPARE_CB_EXTENT
67 #define EXT_CONTINUE 0
69 @@ -179,6 +181,25 @@ typedef int (*ext_prepare_callback)(stru
71 #define EXT_MAX_BLOCK 0xffffffff
74 + * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
75 + * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
76 + * MSB of ee_len field in the extent datastructure to signify if this
77 + * particular extent is an initialized extent or an uninitialized (i.e.
79 + * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
80 + * uninitialized extent.
81 + * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
82 + * uninitialized one. In other words, if MSB of ee_len is set, it is an
83 + * uninitialized extent with only one special scenario when ee_len = 0x8000.
84 + * In this case we can not have an uninitialized extent of zero length and
85 + * thus we make it as a special case of initialized extent with 0x8000 length.
86 + * This way we get better extent-to-group alignment for initialized extents.
87 + * Hence, the maximum number of blocks we can have in an *initialized*
88 + * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
90 +#define EXT_INIT_MAX_LEN (1UL << 15)
91 +#define EXT_UNINIT_MAX_LEN (EXT_INIT_MAX_LEN - 1)
93 #define EXT_FIRST_EXTENT(__hdr__) \
94 ((struct ext3_extent *) (((char *) (__hdr__)) + \
95 @@ -223,6 +244,11 @@ typedef int (*ext_prepare_callback)(stru
96 BUG_ON((path)[0].p_depth != depth); \
99 +static inline int ext3_ext_is_uninitialized(struct ext3_extent *ext)
101 + /* Extent with ee_len of 0x8000 is treated as an initialized extent */
102 + return (le16_to_cpu(ext->ee_len) > EXT_INIT_MAX_LEN);
106 * this structure is used to gather extents from the tree via ioctl
107 Index: linux-2.6.9-67.0.15/fs/ext3/extents.c
108 ===================================================================
109 --- linux-2.6.9-67.0.15.orig/fs/ext3/extents.c
110 +++ linux-2.6.9-67.0.15/fs/ext3/extents.c
112 #include <linux/slab.h>
113 #include <linux/ext3_extents.h>
114 #include <asm/uaccess.h>
118 static int __ext3_ext_check_header(const char *function, int line, struct inode *inode,
119 struct ext3_extent_header *eh, int depth,
120 @@ -1489,7 +1489,7 @@ int ext3_ext_walk_space(struct ext3_exte
122 EXT_ASSERT(cbex.ec_len > 0);
123 EXT_ASSERT(path[depth].p_hdr);
124 - err = func(tree, path, &cbex);
125 + err = func(tree, path, &cbex, ex);
126 ext3_ext_drop_refs(path);
129 @@ -2503,7 +2503,148 @@ int ext3_ext_calc_blockmap_metadata(stru
130 ext3_init_tree_desc(&tree, inode);
131 return ext3_ext_calc_metadata_amount(&tree, blocks);
135 +struct fiemap_internal {
136 + struct fiemap *fiemap_s;
137 + struct fiemap_extent fm_extent;
138 + size_t tot_mapping_len;
140 + int current_extent;
145 + * Callback function called for each extent to gather fiemap information.
147 +int ext3_ext_fiemap_cb(struct ext3_extents_tree *tree,
148 + struct ext3_ext_path *path, struct ext3_ext_cache *newex,
149 + struct ext3_extent *ex)
151 + struct fiemap_internal *fiemap_i = (struct fiemap_internal *)tree->private;
152 + struct fiemap *fiemap_s = fiemap_i->fiemap_s;
153 + struct fiemap_extent *fm_extent = &fiemap_i->fm_extent;
154 + int current_extent = fiemap_i->current_extent;
155 + struct inode *inode = tree->inode;
156 + unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
159 + * ext3_ext_walk_space returns a hole for extents that have not been
162 + if (((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
163 + inode->i_size) && !ext3_ext_is_uninitialized(ex) &&
164 + newex->ec_type == EXT3_EXT_CACHE_GAP)
168 + * We only need to return number of extents.
170 + if (fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)
171 + goto count_extents;
173 + if (current_extent >= fiemap_s->fm_extent_count)
176 + memset(fm_extent, 0, sizeof(*fm_extent));
177 + fm_extent->fe_offset = (__u64)newex->ec_start << blksize_bits;
178 + fm_extent->fe_length = (__u64)newex->ec_len << blksize_bits;
179 + fiemap_i->tot_mapping_len += fm_extent->fe_length;
181 + if (newex->ec_type == EXT3_EXT_CACHE_GAP)
182 + fm_extent->fe_flags |= FIEMAP_EXTENT_HOLE;
184 + if (ext3_ext_is_uninitialized(ex))
185 + fm_extent->fe_flags |= (FIEMAP_EXTENT_DELALLOC |
186 + FIEMAP_EXTENT_UNMAPPED);
189 + * Mark this fiemap_extent as FIEMAP_EXTENT_EOF if it's past the end
192 + if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
194 + fm_extent->fe_flags |= FIEMAP_EXTENT_EOF;
196 + if (!copy_to_user(fiemap_i->cur_ext_ptr, fm_extent,
197 + sizeof(struct fiemap_extent))) {
198 + fiemap_i->cur_ext_ptr += sizeof(struct fiemap_extent);
200 + fiemap_i->err = -EFAULT;
205 + fiemap_i->current_extent++;
208 + * Stop if we are beyond requested mapping size but return complete last
211 + if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
212 + fiemap_s->fm_length)
215 + return EXT_CONTINUE;
218 +int ext3_fiemap(struct inode *inode, struct file *filp, unsigned int cmd,
221 + struct fiemap *fiemap_s;
222 + struct fiemap_internal fiemap_i;
223 + struct fiemap_extent *last_extent;
224 + unsigned long start_blk;
225 + struct ext3_extents_tree tree;
228 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
229 + return -EOPNOTSUPP;
231 + fiemap_s = kmalloc(sizeof(*fiemap_s), GFP_KERNEL);
232 + if (fiemap_s == NULL)
234 + if (copy_from_user(fiemap_s, (struct fiemap __user *)arg,
235 + sizeof(*fiemap_s)))
238 + if (fiemap_s->fm_flags & FIEMAP_FLAG_INCOMPAT)
239 + return -EOPNOTSUPP;
241 + if (fiemap_s->fm_flags & FIEMAP_FLAG_SYNC)
242 + ext3_sync_file(filp, filp->f_dentry, 1);
244 + start_blk = (fiemap_s->fm_start + inode->i_sb->s_blocksize - 1) >>
245 + inode->i_sb->s_blocksize_bits;
246 + fiemap_i.fiemap_s = fiemap_s;
247 + fiemap_i.tot_mapping_len = 0;
248 + fiemap_i.cur_ext_ptr = (char *)(arg + sizeof(*fiemap_s));
249 + fiemap_i.current_extent = 0;
251 + ext3_init_tree_desc(&tree, inode);
252 + tree.private = &fiemap_i;
255 + * Walk the extent tree gathering extent information
257 + down(&EXT3_I(inode)->truncate_sem);
258 + err = ext3_ext_walk_space(&tree, start_blk , EXT_MAX_BLOCK - start_blk,
259 + ext3_ext_fiemap_cb);
260 + up(&EXT3_I(inode)->truncate_sem);
264 + fiemap_s->fm_extent_count = fiemap_i.current_extent;
265 + fiemap_s->fm_length = fiemap_i.tot_mapping_len;
266 + if (fiemap_i.current_extent != 0 &&
267 + !(fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)) {
268 + last_extent = &fiemap_i.fm_extent;
269 + last_extent->fe_flags |= FIEMAP_EXTENT_LAST;
271 + err = copy_to_user((void *)arg, fiemap_s, sizeof(*fiemap_s));
276 EXPORT_SYMBOL(ext3_init_tree_desc);
277 EXPORT_SYMBOL(ext3_mark_inode_dirty);
278 EXPORT_SYMBOL(ext3_ext_invalidate_cache);
279 Index: linux-2.6.9-67.0.15/fs/ext3/fiemap.h
280 ===================================================================
282 +++ linux-2.6.9-67.0.15/fs/ext3/fiemap.h
285 + * linux/fs/ext3/fiemap.h
287 + * Copyright 2008 Sun Microsystems, Inc.
289 + * Author: Kalpak Shah <kalpak@clusterfs.com>
292 +#ifndef _LINUX_EXT3_FIEMAP_H
293 +#define _LINUX_EXT3_FIEMAP_H
295 +struct fiemap_extent {
296 + __u64 fe_offset; /* offset in bytes for the start of the extent */
297 + __u64 fe_length; /* length in bytes for the extent */
298 + __u32 fe_flags; /* returned FIEMAP_EXTENT_* flags for the extent */
299 + __u32 fe_lun; /* logical device number for extent (starting at 0)*/
303 + * fiemap is not ext3-specific and should be moved into fs.h eventually.
307 + __u64 fm_start; /* logical starting byte offset (in/out) */
308 + __u64 fm_length; /* logical length of map (in/out) */
309 + __u32 fm_flags; /* FIEMAP_FLAG_* flags for request (in/out) */
310 + __u32 fm_extent_count; /* number of extents in fm_extents (in/out) */
312 + struct fiemap_extent fm_extents[0];
315 +#define FIEMAP_FLAG_SYNC 0x00000001 /* sync file data before map */
316 +#define FIEMAP_FLAG_HSM_READ 0x00000002 /* get data from HSM before map */
317 +#define FIEMAP_FLAG_NUM_EXTENTS 0x00000004 /* return only number of extents */
318 +#define FIEMAP_FLAG_INCOMPAT 0xff000000 /* error for unknown flags in here */
320 +#define FIEMAP_EXTENT_HOLE 0x00000001 /* has no data or space allocation */
321 +#define FIEMAP_EXTENT_UNWRITTEN 0x00000002 /* space allocated, but no data */
322 +#define FIEMAP_EXTENT_UNMAPPED 0x00000004 /* has data but no space allocation*/
323 +#define FIEMAP_EXTENT_ERROR 0x00000008 /* mapping error, errno in fe_start*/
324 +#define FIEMAP_EXTENT_NO_DIRECT 0x00000010 /* cannot access data directly */
325 +#define FIEMAP_EXTENT_LAST 0x00000020 /* last extent in the file */
326 +#define FIEMAP_EXTENT_DELALLOC 0x00000040 /* has data but not yet written,
327 + * must have EXTENT_UNKNOWN set */
328 +#define FIEMAP_EXTENT_SECONDARY 0x00000080 /* data (also) in secondary storage,
329 + * not in primary if EXTENT_UNKNOWN*/
330 +#define FIEMAP_EXTENT_EOF 0x00000100 /* if fm_start+fm_len is beyond EOF*/
332 +#endif /* _LINUX_EXT3_FIEMAP_H */