Whamcloud - gitweb
Branch b1_6
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-fiemap-2.6-sles10.patch
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
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 @@ -266,6 +266,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.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;
30  };
31  
32 -
33  /*
34   * ioctl commands
35   */
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)
41  
42  /*
43   *  Mount options
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;
47  }
48 +extern int ext3_fiemap(struct inode *, struct file *, unsigned int,
49 +                      unsigned long);
50  
51  /*
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 {
58   */
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
65  
66  #define EXT_CONTINUE   0
67  #define EXT_BREAK      1
68 @@ -179,6 +180,25 @@ typedef int (*ext_prepare_callback)(stru
69  
70  #define EXT_MAX_BLOCK  0xffffffff
71  
72 +/*
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.
77 + * preallocated).
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).
88 + */
89 +#define        EXT_INIT_MAX_LEN        (1UL << 15)
90 +#define        EXT_UNINIT_MAX_LEN      (EXT_INIT_MAX_LEN - 1)
91  
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;
96  }
97  
98 +static inline int ext3_ext_is_uninitialized(struct ext3_extent *ext)
99 +{
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);
102 +}
103  
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
109 @@ -42,7 +42,7 @@
110  #include <linux/slab.h>
111  #include <linux/ext3_extents.h>
112  #include <asm/uaccess.h>
113 -
114 +#include "fiemap.h"
115  
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
119  
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);
125  
126                 if (err < 0)
127 @@ -2499,7 +2499,8 @@ int ext3_ext_calc_blockmap_metadata(stru
128  static int
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)
134  {
135         struct ext3_extent_buf *buf = (struct ext3_extent_buf *) tree->private;
136  
137 @@ -2524,7 +2525,7 @@ ext3_ext_store_extent_cb(struct ext3_ext
138  static int
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)
143  {
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, 
147         return err;
148  }
149  
150 +struct fiemap_internal {
151 +       struct fiemap           *fiemap_s;
152 +       struct fiemap_extent    fm_extent;
153 +       size_t                  tot_mapping_len;
154 +       char                    *cur_ext_ptr;
155 +       int                     current_extent;
156 +       int                     err;
157 +};
158 +
159 +/*
160 + * Callback function called for each extent to gather fiemap information.
161 + */
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)
165 +{
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;
172 +
173 +       /*
174 +        * ext3_ext_walk_space returns a hole for extents that have not been
175 +        * allocated yet.
176 +        */
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)
180 +               return EXT_BREAK;
181 +
182 +       /*
183 +        * We only need to return number of extents.
184 +        */
185 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_NUM_EXTENTS)
186 +               goto count_extents;
187 +
188 +       if (current_extent >= fiemap_s->fm_extent_count)
189 +               return EXT_BREAK;
190 +
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;
195 +
196 +       if (newex->ec_type == EXT3_EXT_CACHE_GAP)
197 +               fm_extent->fe_flags |= FIEMAP_EXTENT_HOLE;
198 +
199 +       if (ext3_ext_is_uninitialized(ex))
200 +               fm_extent->fe_flags |= (FIEMAP_EXTENT_DELALLOC |
201 +                                       FIEMAP_EXTENT_UNMAPPED);
202 +
203 +       /*
204 +        * Mark this fiemap_extent as FIEMAP_EXTENT_EOF if it's past the end
205 +        * of file.
206 +        */
207 +       if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
208 +                                                               inode->i_size)
209 +               fm_extent->fe_flags |= FIEMAP_EXTENT_EOF;
210 +
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);
214 +       } else {
215 +               fiemap_i->err = -EFAULT;
216 +               return EXT_BREAK;
217 +       }
218 +
219 +count_extents:
220 +       fiemap_i->current_extent++;
221 +
222 +       /*
223 +        * Stop if we are beyond requested mapping size but return complete last
224 +        * extent.
225 +        */
226 +       if ((u64)(newex->ec_block + newex->ec_len) << blksize_bits >=
227 +           fiemap_s->fm_length)
228 +               return EXT_BREAK;
229 +
230 +       return EXT_CONTINUE;
231 +}
232 +
233 +int ext3_fiemap(struct inode *inode, struct file *filp, unsigned int cmd,
234 +               unsigned long arg)
235 +{
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;
241 +       int err = 0;
242 +
243 +       if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
244 +               return -EOPNOTSUPP;
245 +
246 +       fiemap_s = kmalloc(sizeof(*fiemap_s), GFP_KERNEL);
247 +       if (fiemap_s == NULL)
248 +               return -ENOMEM;
249 +       if (copy_from_user(fiemap_s, (struct fiemap __user *)arg,
250 +                          sizeof(*fiemap_s)))
251 +               return -EFAULT;
252 +
253 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_INCOMPAT)
254 +               return -EOPNOTSUPP;
255 +
256 +       if (fiemap_s->fm_flags & FIEMAP_FLAG_SYNC)
257 +               ext3_sync_file(filp, filp->f_dentry, 1);
258 +
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;
265 +       fiemap_i.err = 0;
266 +       ext3_init_tree_desc(&tree, inode);
267 +       tree.private = &fiemap_i;
268 +
269 +       /*
270 +        * Walk the extent tree gathering extent information
271 +        */
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);
276 +       if (err)
277 +               return err;
278 +
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;
285 +       }
286 +       err = copy_to_user((void *)arg, fiemap_s, sizeof(*fiemap_s));
287 +
288 +       return err;
289 +}
290 +
291 +
292 +
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 ===================================================================
298 --- /dev/null
299 +++ linux-2.6.16.46-0.14/fs/ext3/fiemap.h
300 @@ -0,0 +1,49 @@
301 +/*
302 + * linux/fs/ext3/fiemap.h
303 + *
304 + * Copyright (C) 2007 Cluster File Systems, Inc
305 + *
306 + * Author: Kalpak Shah <kalpak@clusterfs.com>
307 + */
308 +
309 +#ifndef _LINUX_EXT3_FIEMAP_H
310 +#define _LINUX_EXT3_FIEMAP_H
311 +
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)*/
317 +};
318 +
319 +/*
320 + * fiemap is not ext3-specific and should be moved into fs.h eventually.
321 + */
322 +
323 +struct fiemap {
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) */
328 +       __u64   fm_unused;
329 +       struct fiemap_extent    fm_extents[0];
330 +};
331 +
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 */
336 +
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*/
348 +
349 +#endif /* _LINUX_EXT3_FIEMAP_H */