Whamcloud - gitweb
Branch b1_8
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext4-fiemap-2.6-rhel5.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.18-128.1.6/fs/ext4/ioctl.c
5 ===================================================================
6 --- linux-2.6.18-128.1.6.orig/fs/ext4/ioctl.c
7 +++ linux-2.6.18-128.1.6/fs/ext4/ioctl.c
8 @@ -17,6 +17,162 @@
9  #include "ext4_jbd2.h"
10  #include "ext4.h"
11  
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 = ext4_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  long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
169  {
170         struct inode *inode = filp->f_dentry->d_inode;
171 @@ -257,6 +413,10 @@ flags_err:
172         case EXT4_IOC_MIGRATE:
173                 return ext4_ext_migrate(inode, filp, cmd, arg);
174  
175 +       case EXT4_IOC_FIEMAP: {
176 +               return ioctl_fiemap(inode, filp, arg);
177 +       }
178 +
179         default:
180                 return -ENOTTY;
181         }
182 Index: linux-2.6.18-128.1.6/fs/ext4/ext4.h
183 ===================================================================
184 --- linux-2.6.18-128.1.6.orig/fs/ext4/ext4.h
185 +++ linux-2.6.18-128.1.6/fs/ext4/ext4.h
186 @@ -300,6 +300,7 @@ struct ext4_new_group_data {
187  #define EXT4_IOC_GROUP_EXTEND          _IOW('f', 7, unsigned long)
188  #define EXT4_IOC_GROUP_ADD             _IOW('f', 8, struct ext4_new_group_input)
189  #define EXT4_IOC_MIGRATE               _IO('f', 9)
190 +#define EXT4_IOC_FIEMAP                        _IOWR('f', 11, struct fiemap)
191   /* note ioctl 11 reserved for filesystem-independent FIEMAP ioctl */
192  
193  /*
194 @@ -317,6 +318,8 @@ struct ext4_new_group_data {
195  #define EXT4_IOC32_GETVERSION_OLD      FS_IOC32_GETVERSION
196  #define EXT4_IOC32_SETVERSION_OLD      FS_IOC32_SETVERSION
197  
198 +/* FIEMAP flags supported by ext4 */
199 +#define EXT4_FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC)
200  
201  /*
202   *  Mount options
203 @@ -1117,6 +1120,9 @@ extern int ext4_page_mkwrite(struct vm_a
204  /* ioctl.c */
205  extern long ext4_ioctl(struct file *, unsigned int, unsigned long);
206  extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long);
207 +struct fiemap_extent_info;
208 +extern int ext4_fiemap(struct inode *, struct fiemap_extent_info *, __u64,
209 +                      __u64);
210  
211  /* migrate.c */
212  extern int ext4_ext_migrate(struct inode *, struct file *, unsigned int,
213 Index: linux-2.6.18-128.1.6/fs/ext4/fiemap.h
214 ===================================================================
215 --- /dev/null
216 +++ linux-2.6.18-128.1.6/fs/ext4/fiemap.h
217 @@ -0,0 +1,85 @@
218 +/*
219 + * FIEMAP ioctl infrastructure.
220 + *
221 + * Copyright 2008 Sun Microsystems, Inc
222 + *
223 + * Author: Kalpak Shah <kalpak.shah@sun.com>
224 + *      Andreas Dilger <adilger@sun.com>
225 + */
226 +
227 +#ifndef _LINUX_EXT4_FIEMAP_H
228 +#define _LINUX_EXT4_FIEMAP_H
229 +
230 +struct fiemap_extent {
231 +       __u64 fe_logical;  /* logical offset in bytes for the start of
232 +                           * the extent from the beginning of the file */
233 +       __u64 fe_physical; /* physical offset in bytes for the start
234 +                           * of the extent from the beginning of the disk */
235 +       __u64 fe_length;   /* length in bytes for this extent */
236 +       __u64 fe_reserved64[2];
237 +       __u32 fe_flags;    /* FIEMAP_EXTENT_* flags for this extent */
238 +       __u32 fe_device;   /* device number for this extent */
239 +       __u32 fe_reserved[2];
240 +};
241 +
242 +struct fiemap {
243 +       __u64 fm_start;  /* logical offset (inclusive) at
244 +                                * which to start mapping (in) */
245 +       __u64 fm_length;        /* logical length of mapping which
246 +                                * userspace wants (in) */
247 +       __u32 fm_flags;  /* FIEMAP_FLAG_* flags for request (in/out) */
248 +       __u32 fm_mapped_extents;/* number of extents that were mapped (out) */
249 +       __u32 fm_extent_count;  /* size of fm_extents array (in) */
250 +       __u32 fm_reserved;
251 +       struct fiemap_extent fm_extents[0]; /* array of mapped extents (out) */
252 +};
253 +
254 +/*
255 + * FIEMAP helper definition.
256 + */
257 +struct fiemap_extent_info {
258 +       unsigned int    fi_flags;               /* Flags as passed from user */
259 +       unsigned int    fi_extents_mapped;      /* Number of mapped extents */
260 +       unsigned int    fi_extents_max;         /* Size of fiemap_extent array*/
261 +       struct fiemap_extent *fi_extents_start; /* Start of fiemap_extent array */
262 +};
263 +
264 +int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags);
265 +int fiemap_fill_next_extent(struct fiemap_extent_info *info, u64 logical,
266 +                           u64 phys, u64 len, u32 flags, u32 lun);
267 +
268 +#define        FIEMAP_MAX_OFFSET       (~0ULL)
269 +
270 +#define        FIEMAP_FLAG_SYNC        0x00000001 /* sync file data before map */
271 +#define        FIEMAP_FLAG_XATTR       0x00000002 /* map extended attribute tree */
272 +
273 +/* ldiskfs only supports FLAG_SYNC flag currently */
274 +#define FIEMAP_FLAGS_COMPAT (FIEMAP_FLAG_SYNC | FIEMAP_FLAG_XATTR)
275 +
276 +#define FIEMAP_EXTENT_LAST             0x00000001 /* Last extent in file. */
277 +#define FIEMAP_EXTENT_UNKNOWN          0x00000002 /* Data location unknown. */
278 +#define FIEMAP_EXTENT_DELALLOC         0x00000004 /* Location still pending.
279 +                                                   * Sets EXTENT_UNKNOWN. */
280 +#define FIEMAP_EXTENT_ENCODED          0x00000008 /* Data can not be read
281 +                                                   * while fs is unmounted */
282 +#define FIEMAP_EXTENT_DATA_ENCRYPTED   0x00000080 /* Data is encrypted by fs.
283 +                                                   * Sets EXTENT_NO_DIRECT. */
284 +#define FIEMAP_EXTENT_NOT_ALIGNED      0x00000100 /* Extent offsets may not be
285 +                                                   * block aligned. */
286 +#define FIEMAP_EXTENT_DATA_INLINE      0x00000200 /* Data mixed with metadata.
287 +                                                   * Sets EXTENT_NOT_ALIGNED.*/
288 +#define FIEMAP_EXTENT_DATA_TAIL                0x00000400 /* Multiple files in block.
289 +                                                   * Sets EXTENT_NOT_ALIGNED.*/
290 +#define FIEMAP_EXTENT_UNWRITTEN                0x00000800 /* Space allocated, but
291 +                                                   * no data (i.e. zero). */
292 +#define FIEMAP_EXTENT_MERGED           0x00001000 /* File does not natively
293 +                                                   * support extents. Result
294 +                                                   * merged for efficiency. */
295 +
296 +/* Lustre specific flags - use a high bit, don't conflict with upstream flag */
297 +#define FIEMAP_EXTENT_NO_DIRECT                0x40000000 /* Data mapping undefined */
298 +#define FIEMAP_EXTENT_NET              0x80000000 /* Data stored remotely.
299 +                                                   * Sets NO_DIRECT flag */
300 +
301 +#endif /* _LINUX_EXT4_FIEMAP_H */
302 +