Whamcloud - gitweb
b=16098
[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,9 @@ 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 +#define HAVE_EXT_PREPARE_CB_EXTENT
78  
79  #define EXT_CONTINUE   0
80  #define EXT_BREAK      1
81 @@ -152,6 +152,26 @@ typedef int (*ext_prepare_callback)(stru
82  
83  #define EXT_MAX_BLOCK  0xffffffff
84  
85 +/*
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.
90 + * preallocated).
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).
101 + */
102 +#define EXT_INIT_MAX_LEN       (1UL << 15)
103 +#define EXT_UNINIT_MAX_LEN     (EXT_INIT_MAX_LEN - 1)
104 +
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;
110  }
111  
112 +static inline int ext3_ext_is_uninitialized(struct ext3_extent *ext)
113 +{
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);
116 +}
117 +
118 +
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
126 @@ -42,7 +42,7 @@
127  #include <linux/slab.h>
128  #include <linux/ext3_extents.h>
129  #include <asm/uaccess.h>
130 -
131 +#include "fiemap.h"
132  
133  static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
134  {
135 @@ -1477,7 +1477,7 @@ int ext3_ext_walk_space(struct inode *in
136                 }
137  
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);
142  
143                 if (err < 0)
144 @@ -2289,6 +2289,143 @@ int ext3_ext_writepage_trans_blocks(stru
145         return needed;
146  }
147  
148 +struct fiemap_internal {
149 +       struct fiemap           *fiemap_s;
150 +       struct fiemap_extent    fm_extent;
151 +       size_t                  tot_mapping_len;
152 +       char                    *cur_ext_ptr;
153 +       int                     current_extent;
154 +       int                     err;
155 +};
156 +
157 +/*
158 + * Callback function called for each extent to gather fiemap information.
159 + */
160 +int ext3_ext_fiemap_cb(struct inode *inode, struct ext3_ext_path *path,
161 +                      struct ext3_ext_cache *newex, struct ext3_extent *ex,
162 +                      void *data)
163 +{
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;
169 +
170 +       /*
171 +        * ext3_ext_walk_space returns a hole for extents that have not been
172 +        * allocated yet.
173 +        */
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)
177 +               return EXT_BREAK;
178 +
179 +       /*
180 +        * We only need to return number of extents.
181 +        */
182 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)
183 +               goto count_extents;
184 +
185 +       if (current_extent >= fiemap_s->fm_extent_count)
186 +               return EXT_BREAK;
187 +
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;
192 +
193 +       if (newex->ec_type == EXT3_EXT_CACHE_GAP)
194 +               fm_extent->fe_flags |= FIEMAP_EXTENT_HOLE;
195 +
196 +       if (ext3_ext_is_uninitialized(ex))
197 +               fm_extent->fe_flags |= (FIEMAP_EXTENT_DELALLOC |
198 +                                       FIEMAP_EXTENT_UNMAPPED);
199 +
200 +       /*
201 +        * Mark this fiemap_extent as FIEMAP_EXTENT_EOF if it's past the end
202 +        * of file.
203 +        */
204 +       if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
205 +                                                               inode->i_size)
206 +               fm_extent->fe_flags |= FIEMAP_EXTENT_EOF;
207 +
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);
211 +       } else {
212 +               fiemap_i->err = -EFAULT;
213 +               return EXT_BREAK;
214 +       }
215 +
216 +count_extents:
217 +       fiemap_i->current_extent++;
218 +
219 +       /*
220 +        * Stop if we are beyond requested mapping size but return complete last
221 +        * extent.
222 +        */
223 +       if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
224 +           fiemap_s->fm_length)
225 +               return EXT_BREAK;
226 +
227 +       return EXT_CONTINUE;
228 +}
229 +
230 +int ext3_fiemap(struct inode *inode, struct file *filp, unsigned int cmd,
231 +               unsigned long arg)
232 +{
233 +       struct fiemap *fiemap_s;
234 +       struct fiemap_internal fiemap_i;
235 +       struct fiemap_extent *last_extent;
236 +       ext3_fsblk_t start_blk;
237 +       int err = 0;
238 +
239 +       if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
240 +               return -EOPNOTSUPP;
241 +
242 +       fiemap_s = kmalloc(sizeof(*fiemap_s), GFP_KERNEL);
243 +       if (fiemap_s == NULL)
244 +               return -ENOMEM;
245 +       if (copy_from_user(fiemap_s, (struct fiemap __user *)arg,
246 +                          sizeof(*fiemap_s)))
247 +               return -EFAULT;
248 +
249 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_INCOMPAT)
250 +               return -EOPNOTSUPP;
251 +
252 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_SYNC)
253 +               ext3_sync_file(filp, filp->f_dentry, 1);
254 +
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;
261 +       fiemap_i.err = 0;
262 +
263 +       /*
264 +        * Walk the extent tree gathering extent information
265 +        */
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);
270 +       if (err)
271 +               return err;
272 +
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;
279 +       }
280 +       err = copy_to_user((void *)arg, fiemap_s, sizeof(*fiemap_s));
281 +
282 +       return err;
283 +}
284 +
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 ===================================================================
290 --- /dev/null
291 +++ linux-2.6.18/fs/ext3/fiemap.h
292 @@ -0,0 +1,49 @@
293 +/*
294 + * linux/fs/ext3/fiemap.h
295 + *
296 + * Copyright 2008 Sun Microsystems, Inc.
297 + *
298 + * Author: Kalpak Shah <kalpak@clusterfs.com>
299 + */
300 +
301 +#ifndef _LINUX_EXT3_FIEMAP_H
302 +#define _LINUX_EXT3_FIEMAP_H
303 +
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)*/
309 +};
310 +
311 +/*
312 + * fiemap is not ext3-specific and should be moved into fs.h eventually.
313 + */
314 +
315 +struct fiemap {
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) */
320 +       __u64   fm_unused;
321 +       struct fiemap_extent    fm_extents[0];
322 +};
323 +
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 */
328 +
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*/
340 +
341 +#endif /* _LINUX_EXT3_FIEMAP_H */