Whamcloud - gitweb
b=19872
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-fiemap-2.6-sles10.patch
1 A large part of this code is from the generic VFS code in fs/ioctl.c in the
2 upstream kernel.
3
4 Index: linux-2.6.16.60-0.37/fs/ext3/ioctl.c
5 ===================================================================
6 --- linux-2.6.16.60-0.37.orig/fs/ext3/ioctl.c
7 +++ linux-2.6.16.60-0.37/fs/ext3/ioctl.c
8 @@ -15,7 +15,161 @@
9  #include <linux/time.h>
10  #include <asm/uaccess.h>
11  #include <linux/namei.h>
12 +#include "fiemap.h"
13  
14 +/* So that the fiemap access checks can't overflow on 32 bit machines. */
15 +#define FIEMAP_MAX_EXTENTS     (UINT_MAX / sizeof(struct fiemap_extent))
16 +
17 +/**
18 + * fiemap_fill_next_extent - Fiemap helper function
19 + * @fieinfo:   Fiemap context passed into ->fiemap
20 + * @logical:   Extent logical start offset, in bytes
21 + * @phys:      Extent physical start offset, in bytes
22 + * @len:       Extent length, in bytes
23 + * @flags:     FIEMAP_EXTENT flags that describe this extent
24 + * @lun:       LUN on which this extent resides
25 + *
26 + * Called from file system ->fiemap callback. Will populate extent
27 + * info as passed in via arguments and copy to user memory. On
28 + * success, extent count on fieinfo is incremented.
29 + *
30 + * Returns 0 on success, -errno on error, 1 if this was the last
31 + * extent that will fit in user array.
32 + */
33 +#define SET_UNKNOWN_FLAGS      (FIEMAP_EXTENT_DELALLOC)
34 +#define SET_NO_DIRECT_FLAGS    (FIEMAP_EXTENT_DATA_ENCRYPTED   \
35 +                               |FIEMAP_EXTENT_NET)
36 +#define SET_NO_UNMOUNTED_IO_FLAGS       (FIEMAP_EXTENT_DATA_ENCRYPTED)
37 +#define SET_NOT_ALIGNED_FLAGS  (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
38 +int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
39 +                           u64 phys, u64 len, u32 flags, dev_t dev)
40 +{
41 +       struct fiemap_extent extent = { 0 };
42 +       struct fiemap_extent *dest = fieinfo->fi_extents_start;
43 +
44 +       /* only count the extents */
45 +       if (fieinfo->fi_extents_max == 0) {
46 +               fieinfo->fi_extents_mapped++;
47 +               return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
48 +       }
49 +
50 +       if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
51 +               return 1;
52 +
53 +       if (flags & SET_UNKNOWN_FLAGS)
54 +               flags |= FIEMAP_EXTENT_UNKNOWN;
55 +       if (flags & SET_NO_DIRECT_FLAGS)
56 +               flags |= FIEMAP_EXTENT_NO_DIRECT;
57 +       if (flags & SET_NOT_ALIGNED_FLAGS)
58 +               flags |= FIEMAP_EXTENT_NOT_ALIGNED;
59 +       if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
60 +               flags |= FIEMAP_EXTENT_ENCODED;
61 +
62 +       extent.fe_logical = logical;
63 +       extent.fe_physical = phys;
64 +       extent.fe_length = len;
65 +       extent.fe_flags = flags;
66 +       extent.fe_device = new_encode_dev(dev);
67 +
68 +       dest += fieinfo->fi_extents_mapped;
69 +       if (copy_to_user(dest, &extent, sizeof(extent)))
70 +               return -EFAULT;
71 +
72 +       fieinfo->fi_extents_mapped++;
73 +       if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
74 +               return 1;
75 +
76 +       return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
77 +}
78 +
79 +static int fiemap_check_ranges(struct super_block *sb,
80 +                              u64 start, u64 len, u64 *new_len)
81 +{
82 +       *new_len = len;
83 +
84 +       if (len == 0)
85 +               return -EINVAL;
86 +
87 +       if (start > sb->s_maxbytes)
88 +               return -EFBIG;
89 +
90 +       /*
91 +        * Shrink request scope to what the fs can actually handle.
92 +        */
93 +       if ((len > sb->s_maxbytes) ||
94 +           (sb->s_maxbytes - len) < start)
95 +               *new_len = sb->s_maxbytes - start;
96 +
97 +       return 0;
98 +}
99 +
100 +/*
101 + * fiemap_check_flags - check validity of requested flags for fiemap
102 + * @fieinfo:   Fiemap context passed into ->fiemap
103 + * @fs_flags:  Set of fiemap flags that the file system understands
104 + *
105 + * Called from file system ->fiemap callback. This will compute the
106 + * intersection of valid fiemap flags and those that the fs supports. That
107 + * value is then compared against the user supplied flags. In case of bad user
108 + * flags, the invalid values will be written into the fieinfo structure, and
109 + * -EBADR is returned, which tells ioctl_fiemap() to return those values to
110 + * userspace. For this reason, a return code of -EBADR should be preserved.
111 + *
112 + * Returns 0 on success, -EBADR on bad flags.
113 +*/
114 +int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
115 +{
116 +       u32 incompat_flags;
117 +
118 +       incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
119 +       if (incompat_flags) {
120 +               fieinfo->fi_flags = incompat_flags;
121 +               return -EBADR;
122 +       }
123 +
124 +       return 0;
125 +}
126 +
127 +int ioctl_fiemap(struct inode *inode, struct file *filp, unsigned long arg)
128 +{
129 +       struct fiemap fiemap;
130 +       u64 len;
131 +       struct fiemap_extent_info fieinfo = {0, };
132 +       struct super_block *sb = inode->i_sb;
133 +       int error = 0;
134 +
135 +       if (copy_from_user(&fiemap, (struct fiemap __user *) arg,
136 +                          sizeof(struct fiemap)))
137 +                return -EFAULT;
138 +
139 +       if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
140 +               return -EINVAL;
141 +
142 +       error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
143 +                                   &len);
144 +       if (error)
145 +               return error;
146 +
147 +       fieinfo.fi_flags = fiemap.fm_flags;
148 +       fieinfo.fi_extents_max = fiemap.fm_extent_count;
149 +       fieinfo.fi_extents_start = (struct fiemap_extent *)(arg + sizeof(fiemap));
150 +
151 +       if (fiemap.fm_extent_count != 0 &&
152 +           !access_ok(VERIFY_WRITE, (void *)arg,
153 +                      offsetof(typeof(fiemap), fm_extents[fiemap.fm_extent_count])))
154 +               return -EFAULT;
155 +
156 +       if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
157 +               filemap_write_and_wait(inode->i_mapping);
158 +
159 +       error = ext3_fiemap(inode, &fieinfo, fiemap.fm_start, len);
160 +       fiemap.fm_flags = fieinfo.fi_flags;
161 +       fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
162 +       if (copy_to_user((char *)arg, &fiemap, sizeof(fiemap)))
163 +               error = -EFAULT;
164 +
165 +       return error;
166 +}
167  
168  int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
169                 unsigned long arg)
170 @@ -262,7 +416,9 @@ flags_err:
171  
172                 return err;
173         }
174 -
175 +       case EXT3_IOC_FIEMAP: {
176 +               return ioctl_fiemap(inode, filp, arg);
177 +       }
178  
179         default:
180                 return -ENOTTY;
181 Index: linux-2.6.16.60-0.37/include/linux/ext3_fs.h
182 ===================================================================
183 --- linux-2.6.16.60-0.37.orig/include/linux/ext3_fs.h
184 +++ linux-2.6.16.60-0.37/include/linux/ext3_fs.h
185 @@ -261,15 +261,19 @@ struct ext3_new_group_data {
186  #define        EXT3_IOC_SETFLAGS               _IOW('f', 2, long)
187  #define        EXT3_IOC_GETVERSION             _IOR('f', 3, long)
188  #define        EXT3_IOC_SETVERSION             _IOW('f', 4, long)
189 +#define EXT3_IOC_GETRSVSZ              _IOR('f', 5, long)
190 +#define EXT3_IOC_SETRSVSZ              _IOW('f', 6, long)
191  #define EXT3_IOC_GROUP_EXTEND          _IOW('f', 7, unsigned long)
192  #define EXT3_IOC_GROUP_ADD             _IOW('f', 8,struct ext3_new_group_input)
193 +#define EXT3_IOC_FIEMAP                        _IOWR('f', 11, struct fiemap)
194  #define        EXT3_IOC_GETVERSION_OLD         _IOR('v', 1, long)
195  #define        EXT3_IOC_SETVERSION_OLD         _IOW('v', 2, long)
196  #ifdef CONFIG_JBD_DEBUG
197  #define EXT3_IOC_WAIT_FOR_READONLY     _IOR('f', 99, long)
198  #endif
199 -#define EXT3_IOC_GETRSVSZ              _IOR('f', 5, long)
200 -#define EXT3_IOC_SETRSVSZ              _IOW('f', 6, long)
201 +
202 +/* FIEMAP flags supported by ext3 */
203 +#define EXT3_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC)
204  
205  /*
206   *  Mount options
207 @@ -997,6 +1001,9 @@ extern int ext3_writepage_trans_blocks(s
208  /* ioctl.c */
209  extern int ext3_ioctl (struct inode *, struct file *, unsigned int,
210                        unsigned long);
211 +struct fiemap_extent_info;
212 +extern int ext3_fiemap(struct inode *, struct fiemap_extent_info *, __u64,
213 +                      __u64);
214  
215  /* namei.c */
216  extern int ext3_orphan_add(handle_t *, struct inode *);
217 Index: linux-2.6.16.60-0.37/include/linux/ext3_extents.h
218 ===================================================================
219 --- linux-2.6.16.60-0.37.orig/include/linux/ext3_extents.h
220 +++ linux-2.6.16.60-0.37/include/linux/ext3_extents.h
221 @@ -170,7 +170,10 @@ struct ext3_extents_helpers {
222   */
223  typedef int (*ext_prepare_callback)(struct ext3_extents_tree *,
224                                     struct ext3_ext_path *,
225 -                                   struct ext3_ext_cache *);
226 +                                   struct ext3_ext_cache *,
227 +                                   struct ext3_extent *);
228 +
229 +#define HAVE_EXT_PREPARE_CB_EXTENT
230  
231  #define EXT_CONTINUE   0
232  #define EXT_BREAK      1
233 @@ -180,6 +183,25 @@ typedef int (*ext_prepare_callback)(stru
234  #define EXT_MAX_BLOCK  0xffffffff
235  #define EXT_UNSET_BLOCK 1
236  
237 +/*
238 + * EXT_INIT_MAX_LEN is the maximum number of blocks we can have in an
239 + * initialized extent. This is 2^15 and not (2^16 - 1), since we use the
240 + * MSB of ee_len field in the extent datastructure to signify if this
241 + * particular extent is an initialized extent or an uninitialized (i.e.
242 + * preallocated).
243 + * EXT_UNINIT_MAX_LEN is the maximum number of blocks we can have in an
244 + * uninitialized extent.
245 + * If ee_len is <= 0x8000, it is an initialized extent. Otherwise, it is an
246 + * uninitialized one. In other words, if MSB of ee_len is set, it is an
247 + * uninitialized extent with only one special scenario when ee_len = 0x8000.
248 + * In this case we can not have an uninitialized extent of zero length and
249 + * thus we make it as a special case of initialized extent with 0x8000 length.
250 + * This way we get better extent-to-group alignment for initialized extents.
251 + * Hence, the maximum number of blocks we can have in an *initialized*
252 + * extent is 2^15 (32768) and in an *uninitialized* extent is 2^15-1 (32767).
253 + */
254 +#define        EXT_INIT_MAX_LEN        (1UL << 15)
255 +#define        EXT_UNINIT_MAX_LEN      (EXT_INIT_MAX_LEN - 1)
256  
257  #define EXT_FIRST_EXTENT(__hdr__) \
258         ((struct ext3_extent *) (((char *) (__hdr__)) +         \
259 @@ -243,6 +265,12 @@ struct ext3_extent_tree_stats {
260         int leaf_num;
261  };
262  
263 +static inline int ext3_ext_is_uninitialized(struct ext3_extent *ext)
264 +{
265 +       /* Extent with ee_len of 0x8000 is treated as an initialized extent */
266 +       return (le16_to_cpu(ext->ee_len) > EXT_INIT_MAX_LEN);
267 +}
268 +
269  extern int ext3_ext_search_left(struct ext3_extents_tree *, struct ext3_ext_path *, unsigned long *, unsigned long *);
270  extern int ext3_ext_search_right(struct ext3_extents_tree *, struct ext3_ext_path *, unsigned long *, unsigned long *);
271  extern void ext3_init_tree_desc(struct ext3_extents_tree *, struct inode *);
272 Index: linux-2.6.16.60-0.37/fs/ext3/extents.c
273 ===================================================================
274 --- linux-2.6.16.60-0.37.orig/fs/ext3/extents.c
275 +++ linux-2.6.16.60-0.37/fs/ext3/extents.c
276 @@ -41,6 +41,7 @@
277  #include <linux/string.h>
278  #include <linux/slab.h>
279  #include <linux/ext3_extents.h>
280 +#include "fiemap.h"
281  #include <asm/uaccess.h>
282  
283  
284 @@ -1488,7 +1489,7 @@ int ext3_ext_walk_space(struct ext3_exte
285  
286                 EXT_ASSERT(cbex.ec_len > 0);
287                 EXT_ASSERT(path[depth].p_hdr);
288 -               err = func(tree, path, &cbex);
289 +               err = func(tree, path, &cbex, ex);
290                 ext3_ext_drop_refs(path);
291  
292                 if (err < 0)
293 @@ -2502,7 +2503,109 @@ int ext3_ext_calc_blockmap_metadata(stru
294         ext3_init_tree_desc(&tree, inode);
295         return ext3_ext_calc_metadata_amount(&tree, blocks);
296  }
297 -       
298 +
299 +/*
300 + * Callback function called for each extent to gather FIEMAP information.
301 + */
302 +int ext3_ext_fiemap_cb(struct ext3_extents_tree *tree,
303 +                      struct ext3_ext_path *path, struct ext3_ext_cache *newex,
304 +                      struct ext3_extent *ex)
305 +{
306 +       struct fiemap_extent_info *fieinfo = (struct fiemap_extent_info *)tree->private;
307 +       struct inode *inode = tree->inode;
308 +       unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
309 +       __u64   logical;
310 +       __u64   physical;
311 +       __u64   length;
312 +       __u32   flags = 0;
313 +       int     error;
314 +
315 +       logical =  (__u64)newex->ec_block << blksize_bits;
316 +
317 +       if (newex->ec_type == EXT3_EXT_CACHE_GAP) {
318 +               pgoff_t offset;
319 +               struct page *page;
320 +               struct buffer_head *bh = NULL;
321 +
322 +               offset = logical >> PAGE_SHIFT;
323 +               page = find_get_page(inode->i_mapping, offset);
324 +               if (!page || !page_has_buffers(page))
325 +                       return EXT_CONTINUE;
326 +
327 +               bh = page_buffers(page);
328 +
329 +               if (!bh)
330 +                       return EXT_CONTINUE;
331 +
332 +               if (buffer_delay(bh)) {
333 +                       flags |= FIEMAP_EXTENT_DELALLOC;
334 +                       page_cache_release(page);
335 +               } else {
336 +                       page_cache_release(page);
337 +                       return EXT_CONTINUE;
338 +               }
339 +       }
340 +
341 +       physical = (__u64)newex->ec_start << blksize_bits;
342 +       length =   (__u64)newex->ec_len << blksize_bits;
343 +
344 +       if (ex && ext3_ext_is_uninitialized(ex))
345 +               flags |= FIEMAP_EXTENT_UNWRITTEN;
346 +
347 +       /*
348 +        * If this extent reaches EXT_MAX_BLOCK, it must be last.
349 +        *
350 +        * Or if ext3_ext_next_allocated_block is EXT_MAX_BLOCK,
351 +        * this indicates no more allocated blocks.
352 +        *
353 +        * XXX this might miss a single-block extent at EXT_MAX_BLOCK
354 +        */
355 +       if (ext3_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
356 +           newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK)
357 +               flags |= FIEMAP_EXTENT_LAST;
358 +
359 +       error = fiemap_fill_next_extent(fieinfo, logical, physical,
360 +                                       length, flags, inode->i_sb->s_dev);
361 +       if (error < 0)
362 +               return error;
363 +       if (error == 1)
364 +               return EXT_BREAK;
365 +
366 +       return EXT_CONTINUE;
367 +}
368 +
369 +int ext3_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
370 +               __u64 start, __u64 len)
371 +{
372 +       struct ext3_extents_tree tree;
373 +       unsigned long start_blk;
374 +       unsigned long len_blks;
375 +       int error = 0;
376 +
377 +       if (!(EXT3_I(inode)->i_flags & EXT3_EXTENTS_FL))
378 +               return -EOPNOTSUPP;
379 +
380 +       if (fiemap_check_flags(fieinfo, EXT3_FIEMAP_FLAGS_COMPAT))
381 +               return -EBADR;
382 +
383 +       start_blk = start >> inode->i_sb->s_blocksize_bits;
384 +       len_blks = (len + inode->i_sb->s_blocksize - 1) >> inode->i_sb->s_blocksize_bits;
385 +
386 +       ext3_init_tree_desc(&tree, inode);
387 +       tree.private = fieinfo;
388 +
389 +       /*
390 +        * Walk the extent tree gathering extent information.
391 +        * ext3_ext_fiemap_cb will push extents back to user.
392 +        */
393 +       down(&EXT3_I(inode)->truncate_sem);
394 +       error = ext3_ext_walk_space(&tree, start_blk, len_blks,
395 +                                   ext3_ext_fiemap_cb);
396 +       up(&EXT3_I(inode)->truncate_sem);
397 +
398 +       return error;
399 +}
400 +
401  EXPORT_SYMBOL(ext3_init_tree_desc);
402  EXPORT_SYMBOL(ext3_mark_inode_dirty);
403  EXPORT_SYMBOL(ext3_ext_invalidate_cache);
404 Index: linux-2.6.16.60-0.37/fs/ext3/fiemap.h
405 ===================================================================
406 --- /dev/null
407 +++ linux-2.6.16.60-0.37/fs/ext3/fiemap.h
408 @@ -0,0 +1,85 @@
409 +/*
410 + * FIEMAP ioctl infrastructure.
411 + *
412 + * Copyright 2008 Sun Microsystems, Inc.
413 + *
414 + * Author: Kalpak Shah <kalpak.shah@sun.com>
415 + *         Andreas Dilger <adilger@sun.com>
416 + */
417 +
418 +#ifndef _LINUX_EXT3_FIEMAP_H
419 +#define _LINUX_EXT3_FIEMAP_H
420 +
421 +struct fiemap_extent {
422 +       __u64 fe_logical;  /* logical offset in bytes for the start of
423 +                           * the extent from the beginning of the file */
424 +       __u64 fe_physical; /* physical offset in bytes for the start
425 +                           * of the extent from the beginning of the disk */
426 +       __u64 fe_length;   /* length in bytes for this extent */
427 +       __u64 fe_reserved64[2];
428 +       __u32 fe_flags;    /* FIEMAP_EXTENT_* flags for this extent */
429 +       __u32 fe_device;   /* device number for this extent */
430 +       __u32 fe_reserved[2];
431 +};
432 +
433 +struct fiemap {
434 +       __u64 fm_start;  /* logical offset (inclusive) at
435 +                                * which to start mapping (in) */
436 +       __u64 fm_length;        /* logical length of mapping which
437 +                                * userspace wants (in) */
438 +       __u32 fm_flags;  /* FIEMAP_FLAG_* flags for request (in/out) */
439 +       __u32 fm_mapped_extents;/* number of extents that were mapped (out) */
440 +       __u32 fm_extent_count;  /* size of fm_extents array (in) */
441 +       __u32 fm_reserved;
442 +       struct fiemap_extent fm_extents[0]; /* array of mapped extents (out) */
443 +};
444 +
445 +/*
446 + * FIEMAP helper definition.
447 + */
448 +struct fiemap_extent_info {
449 +       unsigned int    fi_flags;               /* Flags as passed from user */
450 +       unsigned int    fi_extents_mapped;      /* Number of mapped extents */
451 +       unsigned int    fi_extents_max;         /* Size of fiemap_extent array*/
452 +       struct fiemap_extent *fi_extents_start; /* Start of fiemap_extent array */
453 +};
454 +
455 +int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags);
456 +int fiemap_fill_next_extent(struct fiemap_extent_info *info, u64 logical,
457 +                           u64 phys, u64 len, u32 flags, u32 lun);
458 +
459 +#define        FIEMAP_MAX_OFFSET       (~0ULL)
460 +
461 +#define        FIEMAP_FLAG_SYNC        0x00000001 /* sync file data before map */
462 +#define        FIEMAP_FLAG_XATTR       0x00000002 /* map extended attribute tree */
463 +
464 +/* ldiskfs only supports FLAG_SYNC flag currently */
465 +#define FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
466 +
467 +#define FIEMAP_EXTENT_LAST             0x00000001 /* Last extent in file. */
468 +#define FIEMAP_EXTENT_UNKNOWN          0x00000002 /* Data location unknown. */
469 +#define FIEMAP_EXTENT_DELALLOC         0x00000004 /* Location still pending.
470 +                                                   * Sets EXTENT_UNKNOWN. */
471 +#define FIEMAP_EXTENT_ENCODED          0x00000008 /* Data can not be read
472 +                                                   * while fs is unmounted */
473 +#define FIEMAP_EXTENT_DATA_ENCRYPTED   0x00000080 /* Data is encrypted by fs.
474 +                                                   * Sets EXTENT_NO_DIRECT. */
475 +#define FIEMAP_EXTENT_NOT_ALIGNED      0x00000100 /* Extent offsets may not be
476 +                                                   * block aligned. */
477 +#define FIEMAP_EXTENT_DATA_INLINE      0x00000200 /* Data mixed with metadata.
478 +                                                   * Sets EXTENT_NOT_ALIGNED.*/
479 +#define FIEMAP_EXTENT_DATA_TAIL                0x00000400 /* Multiple files in block.
480 +                                                   * Sets EXTENT_NOT_ALIGNED.*/
481 +#define FIEMAP_EXTENT_UNWRITTEN                0x00000800 /* Space allocated, but
482 +                                                   * no data (i.e. zero). */
483 +#define FIEMAP_EXTENT_MERGED           0x00001000 /* File does not natively
484 +                                                   * support extents. Result
485 +                                                   * merged for efficiency. */
486 +
487 +/* Lustre specific flags - use a high bit, don't conflict with upstream flag */
488 +#define FIEMAP_EXTENT_NO_DIRECT                0x40000000 /* Data mapping undefined */
489 +#define FIEMAP_EXTENT_NET              0x80000000 /* Data stored remotely.
490 +                                                   * Sets NO_DIRECT flag */
491 +
492 +#endif /* _LINUX_EXT3_FIEMAP_H */
493 +