diff options
Diffstat (limited to 'fs/ioctl.c')
-rw-r--r-- | fs/ioctl.c | 273 |
1 files changed, 273 insertions, 0 deletions
diff --git a/fs/ioctl.c b/fs/ioctl.c index 7db32b3382d3..33a6b7ecb8b8 100644 --- a/fs/ioctl.c +++ b/fs/ioctl.c | |||
@@ -13,9 +13,14 @@ | |||
13 | #include <linux/security.h> | 13 | #include <linux/security.h> |
14 | #include <linux/module.h> | 14 | #include <linux/module.h> |
15 | #include <linux/uaccess.h> | 15 | #include <linux/uaccess.h> |
16 | #include <linux/writeback.h> | ||
17 | #include <linux/buffer_head.h> | ||
16 | 18 | ||
17 | #include <asm/ioctls.h> | 19 | #include <asm/ioctls.h> |
18 | 20 | ||
21 | /* So that the fiemap access checks can't overflow on 32 bit machines. */ | ||
22 | #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent)) | ||
23 | |||
19 | /** | 24 | /** |
20 | * vfs_ioctl - call filesystem specific ioctl methods | 25 | * vfs_ioctl - call filesystem specific ioctl methods |
21 | * @filp: open file to invoke ioctl method on | 26 | * @filp: open file to invoke ioctl method on |
@@ -71,6 +76,272 @@ static int ioctl_fibmap(struct file *filp, int __user *p) | |||
71 | return put_user(res, p); | 76 | return put_user(res, p); |
72 | } | 77 | } |
73 | 78 | ||
79 | /** | ||
80 | * fiemap_fill_next_extent - Fiemap helper function | ||
81 | * @fieinfo: Fiemap context passed into ->fiemap | ||
82 | * @logical: Extent logical start offset, in bytes | ||
83 | * @phys: Extent physical start offset, in bytes | ||
84 | * @len: Extent length, in bytes | ||
85 | * @flags: FIEMAP_EXTENT flags that describe this extent | ||
86 | * | ||
87 | * Called from file system ->fiemap callback. Will populate extent | ||
88 | * info as passed in via arguments and copy to user memory. On | ||
89 | * success, extent count on fieinfo is incremented. | ||
90 | * | ||
91 | * Returns 0 on success, -errno on error, 1 if this was the last | ||
92 | * extent that will fit in user array. | ||
93 | */ | ||
94 | #define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC) | ||
95 | #define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED) | ||
96 | #define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE) | ||
97 | int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical, | ||
98 | u64 phys, u64 len, u32 flags) | ||
99 | { | ||
100 | struct fiemap_extent extent; | ||
101 | struct fiemap_extent *dest = fieinfo->fi_extents_start; | ||
102 | |||
103 | /* only count the extents */ | ||
104 | if (fieinfo->fi_extents_max == 0) { | ||
105 | fieinfo->fi_extents_mapped++; | ||
106 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; | ||
107 | } | ||
108 | |||
109 | if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max) | ||
110 | return 1; | ||
111 | |||
112 | if (flags & SET_UNKNOWN_FLAGS) | ||
113 | flags |= FIEMAP_EXTENT_UNKNOWN; | ||
114 | if (flags & SET_NO_UNMOUNTED_IO_FLAGS) | ||
115 | flags |= FIEMAP_EXTENT_ENCODED; | ||
116 | if (flags & SET_NOT_ALIGNED_FLAGS) | ||
117 | flags |= FIEMAP_EXTENT_NOT_ALIGNED; | ||
118 | |||
119 | memset(&extent, 0, sizeof(extent)); | ||
120 | extent.fe_logical = logical; | ||
121 | extent.fe_physical = phys; | ||
122 | extent.fe_length = len; | ||
123 | extent.fe_flags = flags; | ||
124 | |||
125 | dest += fieinfo->fi_extents_mapped; | ||
126 | if (copy_to_user(dest, &extent, sizeof(extent))) | ||
127 | return -EFAULT; | ||
128 | |||
129 | fieinfo->fi_extents_mapped++; | ||
130 | if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max) | ||
131 | return 1; | ||
132 | return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0; | ||
133 | } | ||
134 | EXPORT_SYMBOL(fiemap_fill_next_extent); | ||
135 | |||
136 | /** | ||
137 | * fiemap_check_flags - check validity of requested flags for fiemap | ||
138 | * @fieinfo: Fiemap context passed into ->fiemap | ||
139 | * @fs_flags: Set of fiemap flags that the file system understands | ||
140 | * | ||
141 | * Called from file system ->fiemap callback. This will compute the | ||
142 | * intersection of valid fiemap flags and those that the fs supports. That | ||
143 | * value is then compared against the user supplied flags. In case of bad user | ||
144 | * flags, the invalid values will be written into the fieinfo structure, and | ||
145 | * -EBADR is returned, which tells ioctl_fiemap() to return those values to | ||
146 | * userspace. For this reason, a return code of -EBADR should be preserved. | ||
147 | * | ||
148 | * Returns 0 on success, -EBADR on bad flags. | ||
149 | */ | ||
150 | int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags) | ||
151 | { | ||
152 | u32 incompat_flags; | ||
153 | |||
154 | incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags); | ||
155 | if (incompat_flags) { | ||
156 | fieinfo->fi_flags = incompat_flags; | ||
157 | return -EBADR; | ||
158 | } | ||
159 | return 0; | ||
160 | } | ||
161 | EXPORT_SYMBOL(fiemap_check_flags); | ||
162 | |||
163 | static int fiemap_check_ranges(struct super_block *sb, | ||
164 | u64 start, u64 len, u64 *new_len) | ||
165 | { | ||
166 | *new_len = len; | ||
167 | |||
168 | if (len == 0) | ||
169 | return -EINVAL; | ||
170 | |||
171 | if (start > sb->s_maxbytes) | ||
172 | return -EFBIG; | ||
173 | |||
174 | /* | ||
175 | * Shrink request scope to what the fs can actually handle. | ||
176 | */ | ||
177 | if ((len > sb->s_maxbytes) || | ||
178 | (sb->s_maxbytes - len) < start) | ||
179 | *new_len = sb->s_maxbytes - start; | ||
180 | |||
181 | return 0; | ||
182 | } | ||
183 | |||
184 | static int ioctl_fiemap(struct file *filp, unsigned long arg) | ||
185 | { | ||
186 | struct fiemap fiemap; | ||
187 | struct fiemap_extent_info fieinfo = { 0, }; | ||
188 | struct inode *inode = filp->f_path.dentry->d_inode; | ||
189 | struct super_block *sb = inode->i_sb; | ||
190 | u64 len; | ||
191 | int error; | ||
192 | |||
193 | if (!inode->i_op->fiemap) | ||
194 | return -EOPNOTSUPP; | ||
195 | |||
196 | if (copy_from_user(&fiemap, (struct fiemap __user *)arg, | ||
197 | sizeof(struct fiemap))) | ||
198 | return -EFAULT; | ||
199 | |||
200 | if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS) | ||
201 | return -EINVAL; | ||
202 | |||
203 | error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length, | ||
204 | &len); | ||
205 | if (error) | ||
206 | return error; | ||
207 | |||
208 | fieinfo.fi_flags = fiemap.fm_flags; | ||
209 | fieinfo.fi_extents_max = fiemap.fm_extent_count; | ||
210 | fieinfo.fi_extents_start = (struct fiemap_extent *)(arg + sizeof(fiemap)); | ||
211 | |||
212 | if (fiemap.fm_extent_count != 0 && | ||
213 | !access_ok(VERIFY_WRITE, fieinfo.fi_extents_start, | ||
214 | fieinfo.fi_extents_max * sizeof(struct fiemap_extent))) | ||
215 | return -EFAULT; | ||
216 | |||
217 | if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC) | ||
218 | filemap_write_and_wait(inode->i_mapping); | ||
219 | |||
220 | error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len); | ||
221 | fiemap.fm_flags = fieinfo.fi_flags; | ||
222 | fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped; | ||
223 | if (copy_to_user((char *)arg, &fiemap, sizeof(fiemap))) | ||
224 | error = -EFAULT; | ||
225 | |||
226 | return error; | ||
227 | } | ||
228 | |||
229 | #define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits) | ||
230 | #define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits); | ||
231 | |||
232 | /* | ||
233 | * @inode - the inode to map | ||
234 | * @arg - the pointer to userspace where we copy everything to | ||
235 | * @get_block - the fs's get_block function | ||
236 | * | ||
237 | * This does FIEMAP for block based inodes. Basically it will just loop | ||
238 | * through get_block until we hit the number of extents we want to map, or we | ||
239 | * go past the end of the file and hit a hole. | ||
240 | * | ||
241 | * If it is possible to have data blocks beyond a hole past @inode->i_size, then | ||
242 | * please do not use this function, it will stop at the first unmapped block | ||
243 | * beyond i_size | ||
244 | */ | ||
245 | int generic_block_fiemap(struct inode *inode, | ||
246 | struct fiemap_extent_info *fieinfo, u64 start, | ||
247 | u64 len, get_block_t *get_block) | ||
248 | { | ||
249 | struct buffer_head tmp; | ||
250 | unsigned int start_blk; | ||
251 | long long length = 0, map_len = 0; | ||
252 | u64 logical = 0, phys = 0, size = 0; | ||
253 | u32 flags = FIEMAP_EXTENT_MERGED; | ||
254 | int ret = 0; | ||
255 | |||
256 | if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC))) | ||
257 | return ret; | ||
258 | |||
259 | start_blk = logical_to_blk(inode, start); | ||
260 | |||
261 | /* guard against change */ | ||
262 | mutex_lock(&inode->i_mutex); | ||
263 | |||
264 | length = (long long)min_t(u64, len, i_size_read(inode)); | ||
265 | map_len = length; | ||
266 | |||
267 | do { | ||
268 | /* | ||
269 | * we set b_size to the total size we want so it will map as | ||
270 | * many contiguous blocks as possible at once | ||
271 | */ | ||
272 | memset(&tmp, 0, sizeof(struct buffer_head)); | ||
273 | tmp.b_size = map_len; | ||
274 | |||
275 | ret = get_block(inode, start_blk, &tmp, 0); | ||
276 | if (ret) | ||
277 | break; | ||
278 | |||
279 | /* HOLE */ | ||
280 | if (!buffer_mapped(&tmp)) { | ||
281 | /* | ||
282 | * first hole after going past the EOF, this is our | ||
283 | * last extent | ||
284 | */ | ||
285 | if (length <= 0) { | ||
286 | flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST; | ||
287 | ret = fiemap_fill_next_extent(fieinfo, logical, | ||
288 | phys, size, | ||
289 | flags); | ||
290 | break; | ||
291 | } | ||
292 | |||
293 | length -= blk_to_logical(inode, 1); | ||
294 | |||
295 | /* if we have holes up to/past EOF then we're done */ | ||
296 | if (length <= 0) | ||
297 | break; | ||
298 | |||
299 | start_blk++; | ||
300 | } else { | ||
301 | if (length <= 0 && size) { | ||
302 | ret = fiemap_fill_next_extent(fieinfo, logical, | ||
303 | phys, size, | ||
304 | flags); | ||
305 | if (ret) | ||
306 | break; | ||
307 | } | ||
308 | |||
309 | logical = blk_to_logical(inode, start_blk); | ||
310 | phys = blk_to_logical(inode, tmp.b_blocknr); | ||
311 | size = tmp.b_size; | ||
312 | flags = FIEMAP_EXTENT_MERGED; | ||
313 | |||
314 | length -= tmp.b_size; | ||
315 | start_blk += logical_to_blk(inode, size); | ||
316 | |||
317 | /* | ||
318 | * if we are past the EOF we need to loop again to see | ||
319 | * if there is a hole so we can mark this extent as the | ||
320 | * last one, and if not keep mapping things until we | ||
321 | * find a hole, or we run out of slots in the extent | ||
322 | * array | ||
323 | */ | ||
324 | if (length <= 0) | ||
325 | continue; | ||
326 | |||
327 | ret = fiemap_fill_next_extent(fieinfo, logical, phys, | ||
328 | size, flags); | ||
329 | if (ret) | ||
330 | break; | ||
331 | } | ||
332 | cond_resched(); | ||
333 | } while (1); | ||
334 | |||
335 | mutex_unlock(&inode->i_mutex); | ||
336 | |||
337 | /* if ret is 1 then we just hit the end of the extent array */ | ||
338 | if (ret == 1) | ||
339 | ret = 0; | ||
340 | |||
341 | return ret; | ||
342 | } | ||
343 | EXPORT_SYMBOL(generic_block_fiemap); | ||
344 | |||
74 | static int file_ioctl(struct file *filp, unsigned int cmd, | 345 | static int file_ioctl(struct file *filp, unsigned int cmd, |
75 | unsigned long arg) | 346 | unsigned long arg) |
76 | { | 347 | { |
@@ -80,6 +351,8 @@ static int file_ioctl(struct file *filp, unsigned int cmd, | |||
80 | switch (cmd) { | 351 | switch (cmd) { |
81 | case FIBMAP: | 352 | case FIBMAP: |
82 | return ioctl_fibmap(filp, p); | 353 | return ioctl_fibmap(filp, p); |
354 | case FS_IOC_FIEMAP: | ||
355 | return ioctl_fiemap(filp, arg); | ||
83 | case FIGETBSZ: | 356 | case FIGETBSZ: |
84 | return put_user(inode->i_sb->s_blocksize, p); | 357 | return put_user(inode->i_sb->s_blocksize, p); |
85 | case FIONREAD: | 358 | case FIONREAD: |