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