1 Index: linux-2.6.18/fs/ext3/ioctl.c
2 ===================================================================
3 --- linux-2.6.18.orig/fs/ext3/ioctl.c
4 +++ linux-2.6.18/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 @@ -272,6 +272,9 @@ flags_err:
18 + case EXT3_IOC_FIEMAP: {
19 + return ext3_fiemap(inode, filp, cmd, arg);
24 Index: linux-2.6.18/include/linux/ext3_fs.h
25 ===================================================================
26 --- linux-2.6.18.orig/include/linux/ext3_fs.h
27 +++ linux-2.6.18/include/linux/ext3_fs.h
28 @@ -249,7 +249,6 @@ struct ext3_new_group_data {
29 __u32 free_blocks_count;
36 @@ -257,15 +256,16 @@ struct ext3_new_group_data {
37 #define EXT3_IOC_SETFLAGS _IOW('f', 2, long)
38 #define EXT3_IOC_GETVERSION _IOR('f', 3, long)
39 #define EXT3_IOC_SETVERSION _IOW('f', 4, long)
40 -#define EXT3_IOC_GROUP_EXTEND _IOW('f', 7, unsigned long)
41 +#define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
42 +#define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
43 +#define EXT3_IOC_GROUP_EXTEND _IOW('f', 7, unsigned long)
44 #define EXT3_IOC_GROUP_ADD _IOW('f', 8,struct ext3_new_group_input)
45 +#define EXT3_IOC_FIEMAP _IOWR('f', 10, struct fiemap)
46 #define EXT3_IOC_GETVERSION_OLD _IOR('v', 1, long)
47 #define EXT3_IOC_SETVERSION_OLD _IOW('v', 2, long)
48 #ifdef CONFIG_JBD_DEBUG
49 #define EXT3_IOC_WAIT_FOR_READONLY _IOR('f', 99, long)
51 -#define EXT3_IOC_GETRSVSZ _IOR('f', 5, long)
52 -#define EXT3_IOC_SETRSVSZ _IOW('f', 6, long)
56 @@ -1117,6 +1117,8 @@ ext3_get_blocks_wrap(handle_t *handle, s
57 bh->b_size = (ret << inode->i_blkbits);
60 +extern int ext3_fiemap(struct inode *, struct file *, unsigned int,
64 #endif /* __KERNEL__ */
65 Index: linux-2.6.18/include/linux/ext3_extents.h
66 ===================================================================
67 --- linux-2.6.18.orig/include/linux/ext3_extents.h
68 +++ linux-2.6.18/include/linux/ext3_extents.h
69 @@ -142,8 +142,9 @@ struct ext3_ext_path {
70 * callback must return valid extent (passed or newly created)
72 typedef int (*ext_prepare_callback)(struct inode *, struct ext3_ext_path *,
73 - struct ext3_ext_cache *,
75 + struct ext3_ext_cache *,
76 + struct ext3_extent *, void *);
77 +#define HAVE_EXT_PREPARE_CB_EXTENT
79 #define EXT_CONTINUE 0
81 @@ -152,6 +152,26 @@ typedef int (*ext_prepare_callback)(stru
83 #define EXT_MAX_BLOCK 0xffffffff
86 + * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
87 + * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
88 + * MSB of ee_len field in the extent datastructure to signify if this
89 + * particular extent is an initialized extent or an uninitialized (i.e.
91 + * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
92 + * uninitialized extent.
93 + * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
94 + * uninitialized one. In other words, if MSB of ee_len is set, it is an
95 + * uninitialized extent with only one special scenario when ee_len = 0x8000.
96 + * In this case we can not have an uninitialized extent of zero length and
97 + * thus we make it as a special case of initialized extent with 0x8000 length.
98 + * This way we get better extent-to-group alignment for initialized extents.
99 + * Hence, the maximum number of blocks we can have in an *initialized*
100 + * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
102 +#define EXT_INIT_MAX_LEN (1UL << 15)
103 +#define EXT_UNINIT_MAX_LEN (EXT_INIT_MAX_LEN - 1)
105 #define EXT_FLAGS_CLR_UNKNOWN 0x7 /* Flags cleared on modification */
106 #define EXT_HDR_GEN_BITS 24
107 #define EXT_HDR_GEN_MASK ((1 << EXT_HDR_GEN_BITS) - 1)
108 @@ -219,6 +239,13 @@ ext3_ext_invalidate_cache(struct inode *
109 EXT3_I(inode)->i_cached_extent.ec_type = EXT3_EXT_CACHE_NO;
112 +static inline int ext3_ext_is_uninitialized(struct ext3_extent *ext)
114 + /* Extent with ee_len of 0x8000 is treated as an initialized extent */
115 + return (le16_to_cpu(ext->ee_len) > EXT_INIT_MAX_LEN);
119 extern int ext3_ext_search_left(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
120 extern int ext3_ext_search_right(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
121 extern int ext3_extent_tree_init(handle_t *, struct inode *);
122 Index: linux-2.6.18/fs/ext3/extents.c
123 ===================================================================
124 --- linux-2.6.18.orig/fs/ext3/extents.c
125 +++ linux-2.6.18/fs/ext3/extents.c
127 #include <linux/slab.h>
128 #include <linux/ext3_extents.h>
129 #include <asm/uaccess.h>
133 static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
135 @@ -1477,7 +1477,7 @@ int ext3_ext_walk_space(struct inode *in
138 BUG_ON(cbex.ec_len == 0);
139 - err = func(inode, path, &cbex, cbdata);
140 + err = func(inode, path, &cbex, ex, cbdata);
141 ext3_ext_drop_refs(path);
144 @@ -2289,6 +2289,143 @@ int ext3_ext_writepage_trans_blocks(stru
148 +struct fiemap_internal {
149 + struct fiemap *fiemap_s;
150 + struct fiemap_extent fm_extent;
151 + size_t tot_mapping_len;
153 + int current_extent;
158 + * Callback function called for each extent to gather fiemap information.
160 +int ext3_ext_fiemap_cb(struct inode *inode, struct ext3_ext_path *path,
161 + struct ext3_ext_cache *newex, struct ext3_extent *ex,
164 + struct fiemap_internal *fiemap_i = data;
165 + struct fiemap *fiemap_s = fiemap_i->fiemap_s;
166 + struct fiemap_extent *fm_extent = &fiemap_i->fm_extent;
167 + int current_extent = fiemap_i->current_extent;
168 + unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
171 + * ext3_ext_walk_space returns a hole for extents that have not been
174 + if (((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
175 + inode->i_size) && !ext3_ext_is_uninitialized(ex) &&
176 + newex->ec_type == EXT3_EXT_CACHE_GAP)
180 + * We only need to return number of extents.
182 + if (fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)
183 + goto count_extents;
185 + if (current_extent >= fiemap_s->fm_extent_count)
188 + memset(fm_extent, 0, sizeof(*fm_extent));
189 + fm_extent->fe_offset = (__u64)newex->ec_start << blksize_bits;
190 + fm_extent->fe_length = (__u64)newex->ec_len << blksize_bits;
191 + fiemap_i->tot_mapping_len += fm_extent->fe_length;
193 + if (newex->ec_type == EXT3_EXT_CACHE_GAP)
194 + fm_extent->fe_flags |= FIEMAP_EXTENT_HOLE;
196 + if (ext3_ext_is_uninitialized(ex))
197 + fm_extent->fe_flags |= (FIEMAP_EXTENT_DELALLOC |
198 + FIEMAP_EXTENT_UNMAPPED);
201 + * Mark this fiemap_extent as FIEMAP_EXTENT_EOF if it's past the end
204 + if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
206 + fm_extent->fe_flags |= FIEMAP_EXTENT_EOF;
208 + if (!copy_to_user(fiemap_i->cur_ext_ptr, fm_extent,
209 + sizeof(struct fiemap_extent))) {
210 + fiemap_i->cur_ext_ptr += sizeof(struct fiemap_extent);
212 + fiemap_i->err = -EFAULT;
217 + fiemap_i->current_extent++;
220 + * Stop if we are beyond requested mapping size but return complete last
223 + if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
224 + fiemap_s->fm_length)
227 + return EXT_CONTINUE;
230 +int ext3_fiemap(struct inode *inode, struct file *filp, unsigned int cmd,
233 + struct fiemap *fiemap_s;
234 + struct fiemap_internal fiemap_i;
235 + struct fiemap_extent *last_extent;
236 + ext3_fsblk_t start_blk;
239 + if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
240 + return -EOPNOTSUPP;
242 + fiemap_s = kmalloc(sizeof(*fiemap_s), GFP_KERNEL);
243 + if (fiemap_s == NULL)
245 + if (copy_from_user(fiemap_s, (struct fiemap __user *)arg,
246 + sizeof(*fiemap_s)))
249 + if (fiemap_s->fm_flags & FIEMAP_FLAG_INCOMPAT)
250 + return -EOPNOTSUPP;
252 + if (fiemap_s->fm_flags & FIEMAP_FLAG_SYNC)
253 + ext3_sync_file(filp, filp->f_dentry, 1);
255 + start_blk = (fiemap_s->fm_start + inode->i_sb->s_blocksize - 1) >>
256 + inode->i_sb->s_blocksize_bits;
257 + fiemap_i.fiemap_s = fiemap_s;
258 + fiemap_i.tot_mapping_len = 0;
259 + fiemap_i.cur_ext_ptr = (char *)(arg + sizeof(*fiemap_s));
260 + fiemap_i.current_extent = 0;
264 + * Walk the extent tree gathering extent information
266 + mutex_lock(&EXT3_I(inode)->truncate_mutex);
267 + err = ext3_ext_walk_space(inode, start_blk , EXT_MAX_BLOCK - start_blk,
268 + ext3_ext_fiemap_cb, &fiemap_i);
269 + mutex_unlock(&EXT3_I(inode)->truncate_mutex);
273 + fiemap_s->fm_extent_count = fiemap_i.current_extent;
274 + fiemap_s->fm_length = fiemap_i.tot_mapping_len;
275 + if (fiemap_i.current_extent != 0 &&
276 + !(fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)) {
277 + last_extent = &fiemap_i.fm_extent;
278 + last_extent->fe_flags |= FIEMAP_EXTENT_LAST;
280 + err = copy_to_user((void *)arg, fiemap_s, sizeof(*fiemap_s));
285 EXPORT_SYMBOL(ext3_mark_inode_dirty);
286 EXPORT_SYMBOL(ext3_ext_invalidate_cache);
287 EXPORT_SYMBOL(ext3_ext_insert_extent);
288 Index: linux-2.6.18/fs/ext3/fiemap.h
289 ===================================================================
291 +++ linux-2.6.18/fs/ext3/fiemap.h
294 + * linux/fs/ext3/fiemap.h
296 + * Copyright 2008 Sun Microsystems, Inc.
298 + * Author: Kalpak Shah <kalpak@clusterfs.com>
301 +#ifndef _LINUX_EXT3_FIEMAP_H
302 +#define _LINUX_EXT3_FIEMAP_H
304 +struct fiemap_extent {
305 + __u64 fe_offset; /* offset in bytes for the start of the extent */
306 + __u64 fe_length; /* length in bytes for the extent */
307 + __u32 fe_flags; /* returned FIEMAP_EXTENT_* flags for the extent */
308 + __u32 fe_lun; /* logical device number for extent (starting at 0)*/
312 + * fiemap is not ext3-specific and should be moved into fs.h eventually.
316 + __u64 fm_start; /* logical starting byte offset (in/out) */
317 + __u64 fm_length; /* logical length of map (in/out) */
318 + __u32 fm_flags; /* FIEMAP_FLAG_* flags for request (in/out) */
319 + __u32 fm_extent_count; /* number of extents in fm_extents (in/out) */
321 + struct fiemap_extent fm_extents[0];
324 +#define FIEMAP_FLAG_SYNC 0x00000001 /* sync file data before map */
325 +#define FIEMAP_FLAG_HSM_READ 0x00000002 /* get data from HSM before map */
326 +#define FIEMAP_FLAG_NUM_EXTENTS 0x00000004 /* return only number of extents */
327 +#define FIEMAP_FLAG_INCOMPAT 0xff000000 /* error for unknown flags in here */
329 +#define FIEMAP_EXTENT_HOLE 0x00000001 /* has no data or space allocation */
330 +#define FIEMAP_EXTENT_UNWRITTEN 0x00000002 /* space allocated, but no data */
331 +#define FIEMAP_EXTENT_UNMAPPED 0x00000004 /* has data but no space allocation*/
332 +#define FIEMAP_EXTENT_ERROR 0x00000008 /* mapping error, errno in fe_start*/
333 +#define FIEMAP_EXTENT_NO_DIRECT 0x00000010 /* cannot access data directly */
334 +#define FIEMAP_EXTENT_LAST 0x00000020 /* last extent in the file */
335 +#define FIEMAP_EXTENT_DELALLOC 0x00000040 /* has data but not yet written,
336 + * must have EXTENT_UNKNOWN set */
337 +#define FIEMAP_EXTENT_SECONDARY 0x00000080 /* data (also) in secondary storage,
338 + * not in primary if EXTENT_UNKNOWN*/
339 +#define FIEMAP_EXTENT_EOF 0x00000100 /* if fm_start+fm_len is beyond EOF*/
341 +#endif /* _LINUX_EXT3_FIEMAP_H */