Whamcloud - gitweb
b=10555
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-fiemap-2.6.18-vanilla.patch
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
5 @@ -15,7 +15,7 @@
6  #include <linux/time.h>
7  #include <asm/uaccess.h>
8  #include <linux/namei.h>
9 -
10 +#include "fiemap.h"
11  
12  int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
13                 unsigned long arg)
14 @@ -272,6 +272,9 @@ flags_err:
15  
16                 return err;
17         }
18 +       case EXT3_IOC_FIEMAP: {
19 +               return ext3_fiemap(inode, filp, cmd, arg);
20 +       }
21  
22  
23         default:
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;
30  };
31  
32 -
33  /*
34   * ioctl commands
35   */
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)
50  #endif
51 -#define EXT3_IOC_GETRSVSZ              _IOR('f', 5, long)
52 -#define EXT3_IOC_SETRSVSZ              _IOW('f', 6, long)
53  
54  /*
55   *  Mount options
56 @@ -1117,6 +1117,8 @@ ext3_get_blocks_wrap(handle_t *handle, s
57                 bh->b_size = (ret << inode->i_blkbits);
58         return ret;
59  }
60 +extern int ext3_fiemap(struct inode *, struct file *, unsigned int,
61 +                      unsigned long);
62  
63  
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,8 @@ struct ext3_ext_path {
70   * callback must return valid extent (passed or newly created)
71   */
72  typedef int (*ext_prepare_callback)(struct inode *, struct ext3_ext_path *,
73 -                                       struct ext3_ext_cache *,
74 -                                       void *);
75 +                                   struct ext3_ext_cache *,
76 +                                   struct ext3_extent *, void *);
77  
78  #define EXT_CONTINUE   0
79  #define EXT_BREAK      1
80 @@ -152,6 +152,26 @@ typedef int (*ext_prepare_callback)(stru
81  
82  #define EXT_MAX_BLOCK  0xffffffff
83  
84 +/*
85 + * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
86 + * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
87 + * MSB of ee_len field in the extent datastructure to signify if this
88 + * particular extent is an initialized extent or an uninitialized (i.e.
89 + * preallocated).
90 + * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
91 + * uninitialized extent.
92 + * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
93 + * uninitialized one. In other words, if MSB of ee_len is set, it is an
94 + * uninitialized extent with only one special scenario when ee_len = 0x8000.
95 + * In this case we can not have an uninitialized extent of zero length and
96 + * thus we make it as a special case of initialized extent with 0x8000 length.
97 + * This way we get better extent-to-group alignment for initialized extents.
98 + * Hence, the maximum number of blocks we can have in an *initialized*
99 + * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
100 + */
101 +#define EXT_INIT_MAX_LEN       (1UL << 15)
102 +#define EXT_UNINIT_MAX_LEN     (EXT_INIT_MAX_LEN - 1)
103 +
104  #define EXT_FLAGS_CLR_UNKNOWN  0x7  /* Flags cleared on modification */
105  #define EXT_HDR_GEN_BITS       24
106  #define EXT_HDR_GEN_MASK       ((1 << EXT_HDR_GEN_BITS) - 1)
107 @@ -219,6 +239,13 @@ ext3_ext_invalidate_cache(struct inode *
108         EXT3_I(inode)->i_cached_extent.ec_type = EXT3_EXT_CACHE_NO;
109  }
110  
111 +static inline int ext3_ext_is_uninitialized(struct ext3_extent *ext)
112 +{
113 +       /* Extent with ee_len of 0x8000 is treated as an initialized extent */
114 +       return (le16_to_cpu(ext->ee_len) > EXT_INIT_MAX_LEN);
115 +}
116 +
117 +
118  extern int ext3_ext_search_left(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
119  extern int ext3_ext_search_right(struct inode *, struct ext3_ext_path *, unsigned long *, unsigned long *);
120  extern int ext3_extent_tree_init(handle_t *, struct inode *);
121 Index: linux-2.6.18/fs/ext3/extents.c
122 ===================================================================
123 --- linux-2.6.18.orig/fs/ext3/extents.c
124 +++ linux-2.6.18/fs/ext3/extents.c
125 @@ -42,7 +42,7 @@
126  #include <linux/slab.h>
127  #include <linux/ext3_extents.h>
128  #include <asm/uaccess.h>
129 -
130 +#include "fiemap.h"
131  
132  static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
133  {
134 @@ -1477,7 +1477,7 @@ int ext3_ext_walk_space(struct inode *in
135                 }
136  
137                 BUG_ON(cbex.ec_len == 0);
138 -               err = func(inode, path, &cbex, cbdata);
139 +               err = func(inode, path, &cbex, ex, cbdata);
140                 ext3_ext_drop_refs(path);
141  
142                 if (err < 0)
143 @@ -2289,6 +2289,143 @@ int ext3_ext_writepage_trans_blocks(stru
144         return needed;
145  }
146  
147 +struct fiemap_internal {
148 +       struct fiemap           *fiemap_s;
149 +       struct fiemap_extent    fm_extent;
150 +       size_t                  tot_mapping_len;
151 +       char                    *cur_ext_ptr;
152 +       int                     current_extent;
153 +       int                     err;
154 +};
155 +
156 +/*
157 + * Callback function called for each extent to gather fiemap information.
158 + */
159 +int ext3_ext_fiemap_cb(struct inode *inode, struct ext3_ext_path *path,
160 +                      struct ext3_ext_cache *newex, struct ext3_extent *ex,
161 +                      void *data)
162 +{
163 +       struct fiemap_internal *fiemap_i = data;
164 +       struct fiemap *fiemap_s = fiemap_i->fiemap_s;
165 +       struct fiemap_extent *fm_extent = &fiemap_i->fm_extent;
166 +       int current_extent = fiemap_i->current_extent;
167 +       unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
168 +
169 +       /*
170 +        * ext3_ext_walk_space returns a hole for extents that have not been
171 +        * allocated yet.
172 +        */
173 +       if (((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
174 +            inode->i_size) && !ext3_ext_is_uninitialized(ex) &&
175 +           newex->ec_type == EXT3_EXT_CACHE_GAP)
176 +               return EXT_BREAK;
177 +
178 +       /*
179 +        * We only need to return number of extents.
180 +        */
181 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)
182 +               goto count_extents;
183 +
184 +       if (current_extent >= fiemap_s->fm_extent_count)
185 +               return EXT_BREAK;
186 +
187 +       memset(fm_extent, 0, sizeof(*fm_extent));
188 +       fm_extent->fe_offset = (__u64)newex->ec_start << blksize_bits;
189 +       fm_extent->fe_length = (__u64)newex->ec_len << blksize_bits;
190 +       fiemap_i->tot_mapping_len += fm_extent->fe_length;
191 +
192 +       if (newex->ec_type == EXT3_EXT_CACHE_GAP)
193 +               fm_extent->fe_flags |= FIEMAP_EXTENT_HOLE;
194 +
195 +       if (ext3_ext_is_uninitialized(ex))
196 +               fm_extent->fe_flags |= (FIEMAP_EXTENT_DELALLOC |
197 +                                       FIEMAP_EXTENT_UNMAPPED);
198 +
199 +       /*
200 +        * Mark this fiemap_extent as FIEMAP_EXTENT_EOF if it's past the end
201 +        * of file.
202 +        */
203 +       if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
204 +                                                               inode->i_size)
205 +               fm_extent->fe_flags |= FIEMAP_EXTENT_EOF;
206 +
207 +       if (!copy_to_user(fiemap_i->cur_ext_ptr, fm_extent,
208 +                         sizeof(struct fiemap_extent))) {
209 +               fiemap_i->cur_ext_ptr += sizeof(struct fiemap_extent);
210 +       } else {
211 +               fiemap_i->err = -EFAULT;
212 +               return EXT_BREAK;
213 +       }
214 +
215 +count_extents:
216 +       fiemap_i->current_extent++;
217 +
218 +       /*
219 +        * Stop if we are beyond requested mapping size but return complete last
220 +        * extent.
221 +        */
222 +       if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
223 +           fiemap_s->fm_length)
224 +               return EXT_BREAK;
225 +
226 +       return EXT_CONTINUE;
227 +}
228 +
229 +int ext3_fiemap(struct inode *inode, struct file *filp, unsigned int cmd,
230 +               unsigned long arg)
231 +{
232 +       struct fiemap *fiemap_s;
233 +       struct fiemap_internal fiemap_i;
234 +       struct fiemap_extent *last_extent;
235 +       ext3_fsblk_t start_blk;
236 +       int err = 0;
237 +
238 +       if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
239 +               return -EOPNOTSUPP;
240 +
241 +       fiemap_s = kmalloc(sizeof(*fiemap_s), GFP_KERNEL);
242 +       if (fiemap_s == NULL)
243 +               return -ENOMEM;
244 +       if (copy_from_user(fiemap_s, (struct fiemap __user *)arg,
245 +                          sizeof(*fiemap_s)))
246 +               return -EFAULT;
247 +
248 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_INCOMPAT)
249 +               return -EOPNOTSUPP;
250 +
251 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_SYNC)
252 +               ext3_sync_file(filp, filp->f_dentry, 1);
253 +
254 +       start_blk = (fiemap_s->fm_start + inode->i_sb->s_blocksize - 1) >>
255 +                                               inode->i_sb->s_blocksize_bits;
256 +       fiemap_i.fiemap_s = fiemap_s;
257 +       fiemap_i.tot_mapping_len = 0;
258 +       fiemap_i.cur_ext_ptr = (char *)(arg + sizeof(*fiemap_s));
259 +       fiemap_i.current_extent = 0;
260 +       fiemap_i.err = 0;
261 +
262 +       /*
263 +        * Walk the extent tree gathering extent information
264 +        */
265 +       mutex_lock(&EXT3_I(inode)->truncate_mutex);
266 +       err = ext3_ext_walk_space(inode, start_blk , EXT_MAX_BLOCK - start_blk,
267 +                                 (void *)ext3_ext_fiemap_cb, &fiemap_i);
268 +       mutex_unlock(&EXT3_I(inode)->truncate_mutex);
269 +       if (err)
270 +               return err;
271 +
272 +       fiemap_s->fm_extent_count = fiemap_i.current_extent;
273 +       fiemap_s->fm_length = fiemap_i.tot_mapping_len;
274 +       if (fiemap_i.current_extent != 0 &&
275 +           !(fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)) {
276 +               last_extent = &fiemap_i.fm_extent;
277 +               last_extent->fe_flags |= FIEMAP_EXTENT_LAST;
278 +       }
279 +       err = copy_to_user((void *)arg, fiemap_s, sizeof(*fiemap_s));
280 +
281 +       return err;
282 +}
283 +
284  EXPORT_SYMBOL(ext3_mark_inode_dirty);
285  EXPORT_SYMBOL(ext3_ext_invalidate_cache);
286  EXPORT_SYMBOL(ext3_ext_insert_extent);
287 Index: linux-2.6.18/fs/ext3/fiemap.h
288 ===================================================================
289 --- /dev/null
290 +++ linux-2.6.18/fs/ext3/fiemap.h
291 @@ -0,0 +1,49 @@
292 +/*
293 + * linux/fs/ext3/fiemap.h
294 + *
295 + * Copyright (C) 2007 Cluster File Systems, Inc
296 + *
297 + * Author: Kalpak Shah <kalpak@clusterfs.com>
298 + */
299 +
300 +#ifndef _LINUX_EXT3_FIEMAP_H
301 +#define _LINUX_EXT3_FIEMAP_H
302 +
303 +struct fiemap_extent {
304 +       __u64   fe_offset; /* offset in bytes for the start of the extent */
305 +       __u64   fe_length; /* length in bytes for the extent */
306 +       __u32   fe_flags;  /* returned FIEMAP_EXTENT_* flags for the extent */
307 +       __u32   fe_lun;    /* logical device number for extent (starting at 0)*/
308 +};
309 +
310 +/*
311 + * fiemap is not ext3-specific and should be moved into fs.h eventually.
312 + */
313 +
314 +struct fiemap {
315 +       __u64   fm_start;        /* logical starting byte offset (in/out) */
316 +       __u64   fm_length;       /* logical length of map (in/out) */
317 +       __u32   fm_flags;        /* FIEMAP_FLAG_* flags for request (in/out) */
318 +       __u32   fm_extent_count; /* number of extents in fm_extents (in/out) */
319 +       __u64   fm_unused;
320 +       struct fiemap_extent    fm_extents[0];
321 +};
322 +
323 +#define        FIEMAP_FLAG_SYNC        0x00000001 /* sync file data before map */
324 +#define        FIEMAP_FLAG_HSM_READ    0x00000002 /* get data from HSM before map */
325 +#define        FIEMAP_FLAG_NUM_EXTENTS 0x00000004 /* return only number of extents */
326 +#define        FIEMAP_FLAG_INCOMPAT    0xff000000 /* error for unknown flags in here */
327 +
328 +#define        FIEMAP_EXTENT_HOLE      0x00000001 /* has no data or space allocation */
329 +#define        FIEMAP_EXTENT_UNWRITTEN 0x00000002 /* space allocated, but no data */
330 +#define        FIEMAP_EXTENT_UNMAPPED  0x00000004 /* has data but no space allocation*/
331 +#define        FIEMAP_EXTENT_ERROR     0x00000008 /* mapping error, errno in fe_start*/
332 +#define        FIEMAP_EXTENT_NO_DIRECT 0x00000010 /* cannot access data directly */
333 +#define        FIEMAP_EXTENT_LAST      0x00000020 /* last extent in the file */
334 +#define        FIEMAP_EXTENT_DELALLOC  0x00000040 /* has data but not yet written,
335 +                                           * must have EXTENT_UNKNOWN set */
336 +#define        FIEMAP_EXTENT_SECONDARY 0x00000080 /* data (also) in secondary storage,
337 +                                           * not in primary if EXTENT_UNKNOWN*/
338 +#define        FIEMAP_EXTENT_EOF       0x00000100 /* if fm_start+fm_len is beyond EOF*/
339 +
340 +#endif /* _LINUX_EXT3_FIEMAP_H */