1 Index: linux-2.6.16.46-0.14/fs/ext3/ioctl.c
2 ===================================================================
3 --- linux-2.6.16.46-0.14.orig/fs/ext3/ioctl.c
4 +++ linux-2.6.16.46-0.14/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 @@ -266,6 +266,9 @@ flags_err:
18 + case EXT3_IOC_FIEMAP: {
19 + return ext3_fiemap(inode, filp, cmd, arg);
24 Index: linux-2.6.16.46-0.14/include/linux/ext3_fs.h
25 ===================================================================
26 --- linux-2.6.16.46-0.14.orig/include/linux/ext3_fs.h
27 +++ linux-2.6.16.46-0.14/include/linux/ext3_fs.h
28 @@ -252,7 +252,6 @@ struct ext3_new_group_data {
29 __u32 free_blocks_count;
36 @@ -272,6 +271,7 @@ struct ext3_new_group_data {
37 #define EXT3_IOC_GET_EXTENTS _IOR('f', 7, long)
38 #define EXT3_IOC_GET_TREE_DEPTH _IOR('f', 8, long)
39 #define EXT3_IOC_GET_TREE_STATS _IOR('f', 9, long)
40 +#define EXT3_IOC_FIEMAP _IOWR('f', 10, struct fiemap)
44 @@ -853,6 +853,8 @@ static inline struct timespec ext3_curre
45 return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ?
46 current_fs_time(inode->i_sb) : CURRENT_TIME_SEC;
48 +extern int ext3_fiemap(struct inode *, struct file *, unsigned int,
52 * This structure is stuffed into the struct file's private_data field
53 Index: linux-2.6.16.46-0.14/include/linux/ext3_extents.h
54 ===================================================================
55 --- linux-2.6.16.46-0.14.orig/include/linux/ext3_extents.h
56 +++ linux-2.6.16.46-0.14/include/linux/ext3_extents.h
57 @@ -170,7 +170,9 @@ struct ext3_extents_helpers {
59 typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
60 struct ext3_ext_path *,
61 - struct ext3_ext_cache *);
62 + struct ext3_ext_cache *,
63 + struct ext3_extent *);
64 +#define HAVE_EXT_PREPARE_CB_EXTENT
66 #define EXT_CONTINUE 0
68 @@ -179,6 +180,25 @@ typedef int (*ext_prepare_callback)(stru
70 #define EXT_MAX_BLOCK 0xffffffff
73 + * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
74 + * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
75 + * MSB of ee_len field in the extent datastructure to signify if this
76 + * particular extent is an initialized extent or an uninitialized (i.e.
78 + * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
79 + * uninitialized extent.
80 + * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
81 + * uninitialized one. In other words, if MSB of ee_len is set, it is an
82 + * uninitialized extent with only one special scenario when ee_len = 0x8000.
83 + * In this case we can not have an uninitialized extent of zero length and
84 + * thus we make it as a special case of initialized extent with 0x8000 length.
85 + * This way we get better extent-to-group alignment for initialized extents.
86 + * Hence, the maximum number of blocks we can have in an *initialized*
87 + * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
89 +#define EXT_INIT_MAX_LEN (1UL << 15)
90 +#define EXT_UNINIT_MAX_LEN (EXT_INIT_MAX_LEN - 1)
92 #define EXT_FIRST_EXTENT(__hdr__) \
93 ((struct ext3_extent *) (((char *) (__hdr__)) + \
94 @@ -260,5 +280,10 @@ ext3_ext_invalidate_cache(struct ext3_ex
95 tree->cex->ec_type = EXT3_EXT_CACHE_NO;
98 +static inline int ext3_ext_is_uninitialized(struct ext3_extent *ext)
100 + /* Extent with ee_len of 0x8000 is treated as an initialized extent */
101 + return (le16_to_cpu(ext->ee_len) > EXT_INIT_MAX_LEN);
104 #endif /* _LINUX_EXT3_EXTENTS */
105 Index: linux-2.6.16.46-0.14/fs/ext3/extents.c
106 ===================================================================
107 --- linux-2.6.16.46-0.14.orig/fs/ext3/extents.c
108 +++ linux-2.6.16.46-0.14/fs/ext3/extents.c
110 #include <linux/slab.h>
111 #include <linux/ext3_extents.h>
112 #include <asm/uaccess.h>
116 static int __ext3_ext_check_header(const char *function, struct inode *inode,
117 struct ext3_extent_header *eh, int depth,
118 @@ -1484,7 +1484,7 @@ int ext3_ext_walk_space(struct ext3_exte
120 EXT_ASSERT(cbex.ec_len > 0);
121 EXT_ASSERT(path[depth].p_hdr);
122 - err = func(tree, path, &cbex);
123 + err = func(tree, path, &cbex, ex);
124 ext3_ext_drop_refs(path);
127 @@ -2499,7 +2499,8 @@ int ext3_ext_calc_blockmap_metadata(stru
129 ext3_ext_store_extent_cb(struct ext3_extents_tree *tree,
130 struct ext3_ext_path *path,
131 - struct ext3_ext_cache *newex)
132 + struct ext3_ext_cache *newex,
133 + struct ext3_extent *extent)
135 struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
137 @@ -2524,7 +2525,7 @@ ext3_ext_store_extent_cb(struct ext3_ext
139 ext3_ext_collect_stats_cb(struct ext3_extents_tree *tree,
140 struct ext3_ext_path *path,
141 - struct ext3_ext_cache *ex)
142 + struct ext3_ext_cache *ex, struct ext3_extent *extent)
144 struct ext3_extent_tree_stats *buf =
145 (struct ext3_extent_tree_stats *) tree->private;
146 @@ -2591,6 +2592,149 @@ int ext3_ext_ioctl(struct inode *inode,
150 +struct fiemap_internal {
151 + struct fiemap *fiemap_s;
152 + struct fiemap_extent fm_extent;
153 + size_t tot_mapping_len;
155 + int current_extent;
160 + * Callback function called for each extent to gather fiemap information.
162 +int ext3_ext_fiemap_cb(struct ext3_extents_tree *tree,
163 + struct ext3_ext_path *path, struct ext3_ext_cache *newex,
164 + struct ext3_extent *ex)
166 + struct fiemap_internal *fiemap_i = (struct fiemap_internal *)tree->private;
167 + struct fiemap *fiemap_s = fiemap_i->fiemap_s;
168 + struct fiemap_extent *fm_extent = &fiemap_i->fm_extent;
169 + int current_extent = fiemap_i->current_extent;
170 + struct inode *inode = tree->inode;
171 + unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
174 + * ext3_ext_walk_space returns a hole for extents that have not been
177 + if (((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
178 + inode->i_size) && !ext3_ext_is_uninitialized(ex) &&
179 + newex->ec_type == EXT3_EXT_CACHE_GAP)
183 + * We only need to return number of extents.
185 + if (fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)
186 + goto count_extents;
188 + if (current_extent >= fiemap_s->fm_extent_count)
191 + memset(fm_extent, 0, sizeof(*fm_extent));
192 + fm_extent->fe_offset = (__u64)newex->ec_start << blksize_bits;
193 + fm_extent->fe_length = (__u64)newex->ec_len << blksize_bits;
194 + fiemap_i->tot_mapping_len += fm_extent->fe_length;
196 + if (newex->ec_type == EXT3_EXT_CACHE_GAP)
197 + fm_extent->fe_flags |= FIEMAP_EXTENT_HOLE;
199 + if (ext3_ext_is_uninitialized(ex))
200 + fm_extent->fe_flags |= (FIEMAP_EXTENT_DELALLOC |
201 + FIEMAP_EXTENT_UNMAPPED);
204 + * Mark this fiemap_extent as FIEMAP_EXTENT_EOF if it's past the end
207 + if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
209 + fm_extent->fe_flags |= FIEMAP_EXTENT_EOF;
211 + if (!copy_to_user(fiemap_i->cur_ext_ptr, fm_extent,
212 + sizeof(struct fiemap_extent))) {
213 + fiemap_i->cur_ext_ptr += sizeof(struct fiemap_extent);
215 + fiemap_i->err = -EFAULT;
220 + fiemap_i->current_extent++;
223 + * Stop if we are beyond requested mapping size but return complete last
226 + if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
227 + fiemap_s->fm_length)
230 + return EXT_CONTINUE;
233 +int ext3_fiemap(struct inode *inode, struct file *filp, unsigned int cmd,
236 + struct fiemap *fiemap_s;
237 + struct fiemap_internal fiemap_i;
238 + struct fiemap_extent *last_extent;
239 + unsigned long start_blk;
240 + struct ext3_extents_tree tree;
243 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
244 + return -EOPNOTSUPP;
246 + fiemap_s = kmalloc(sizeof(*fiemap_s), GFP_KERNEL);
247 + if (fiemap_s == NULL)
249 + if (copy_from_user(fiemap_s, (struct fiemap __user *)arg,
250 + sizeof(*fiemap_s)))
253 + if (fiemap_s->fm_flags & FIEMAP_FLAG_INCOMPAT)
254 + return -EOPNOTSUPP;
256 + if (fiemap_s->fm_flags & FIEMAP_FLAG_SYNC)
257 + ext3_sync_file(filp, filp->f_dentry, 1);
259 + start_blk = (fiemap_s->fm_start + inode->i_sb->s_blocksize - 1) >>
260 + inode->i_sb->s_blocksize_bits;
261 + fiemap_i.fiemap_s = fiemap_s;
262 + fiemap_i.tot_mapping_len = 0;
263 + fiemap_i.cur_ext_ptr = (char *)(arg + sizeof(*fiemap_s));
264 + fiemap_i.current_extent = 0;
266 + ext3_init_tree_desc(&tree, inode);
267 + tree.private = &fiemap_i;
270 + * Walk the extent tree gathering extent information
272 + down(&EXT3_I(inode)->truncate_sem);
273 + err = ext3_ext_walk_space(&tree, start_blk , EXT_MAX_BLOCK - start_blk,
274 + ext3_ext_fiemap_cb);
275 + up(&EXT3_I(inode)->truncate_sem);
279 + fiemap_s->fm_extent_count = fiemap_i.current_extent;
280 + fiemap_s->fm_length = fiemap_i.tot_mapping_len;
281 + if (fiemap_i.current_extent != 0 &&
282 + !(fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)) {
283 + last_extent = &fiemap_i.fm_extent;
284 + last_extent->fe_flags |= FIEMAP_EXTENT_LAST;
286 + err = copy_to_user((void *)arg, fiemap_s, sizeof(*fiemap_s));
293 EXPORT_SYMBOL(ext3_init_tree_desc);
294 EXPORT_SYMBOL(ext3_mark_inode_dirty);
295 EXPORT_SYMBOL(ext3_ext_invalidate_cache);
296 Index: linux-2.6.16.46-0.14/fs/ext3/fiemap.h
297 ===================================================================
299 +++ linux-2.6.16.46-0.14/fs/ext3/fiemap.h
302 + * linux/fs/ext3/fiemap.h
304 + * Copyright (C) 2007 Cluster File Systems, Inc
306 + * Author: Kalpak Shah <kalpak@clusterfs.com>
309 +#ifndef _LINUX_EXT3_FIEMAP_H
310 +#define _LINUX_EXT3_FIEMAP_H
312 +struct fiemap_extent {
313 + __u64 fe_offset; /* offset in bytes for the start of the extent */
314 + __u64 fe_length; /* length in bytes for the extent */
315 + __u32 fe_flags; /* returned FIEMAP_EXTENT_* flags for the extent */
316 + __u32 fe_lun; /* logical device number for extent (starting at 0)*/
320 + * fiemap is not ext3-specific and should be moved into fs.h eventually.
324 + __u64 fm_start; /* logical starting byte offset (in/out) */
325 + __u64 fm_length; /* logical length of map (in/out) */
326 + __u32 fm_flags; /* FIEMAP_FLAG_* flags for request (in/out) */
327 + __u32 fm_extent_count; /* number of extents in fm_extents (in/out) */
329 + struct fiemap_extent fm_extents[0];
332 +#define FIEMAP_FLAG_SYNC 0x00000001 /* sync file data before map */
333 +#define FIEMAP_FLAG_HSM_READ 0x00000002 /* get data from HSM before map */
334 +#define FIEMAP_FLAG_NUM_EXTENTS 0x00000004 /* return only number of extents */
335 +#define FIEMAP_FLAG_INCOMPAT 0xff000000 /* error for unknown flags in here */
337 +#define FIEMAP_EXTENT_HOLE 0x00000001 /* has no data or space allocation */
338 +#define FIEMAP_EXTENT_UNWRITTEN 0x00000002 /* space allocated, but no data */
339 +#define FIEMAP_EXTENT_UNMAPPED 0x00000004 /* has data but no space allocation*/
340 +#define FIEMAP_EXTENT_ERROR 0x00000008 /* mapping error, errno in fe_start*/
341 +#define FIEMAP_EXTENT_NO_DIRECT 0x00000010 /* cannot access data directly */
342 +#define FIEMAP_EXTENT_LAST 0x00000020 /* last extent in the file */
343 +#define FIEMAP_EXTENT_DELALLOC 0x00000040 /* has data but not yet written,
344 + * must have EXTENT_UNKNOWN set */
345 +#define FIEMAP_EXTENT_SECONDARY 0x00000080 /* data (also) in secondary storage,
346 + * not in primary if EXTENT_UNKNOWN*/
347 +#define FIEMAP_EXTENT_EOF 0x00000100 /* if fm_start+fm_len is beyond EOF*/
349 +#endif /* _LINUX_EXT3_FIEMAP_H */