blob: 001f8d3118f27329d89131feef43c7ba0c0b1a85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ioctl.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/syscalls.h>
8#include <linux/mm.h>
9#include <linux/smp_lock.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080010#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/file.h>
12#include <linux/fs.h>
13#include <linux/security.h>
14#include <linux/module.h>
Erez Zadokc9845ff2008-02-07 00:13:23 -080015#include <linux/uaccess.h>
Josef Bacik68c9d702008-10-03 17:32:43 -040016#include <linux/writeback.h>
17#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/ioctls.h>
20
Mark Fashehc4b929b2008-10-08 19:44:18 -040021/* 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
Erez Zadokdeb21db2008-02-07 00:13:25 -080024/**
25 * vfs_ioctl - call filesystem specific ioctl methods
Christoph Hellwigf6a4c8b2008-02-09 00:10:16 -080026 * @filp: open file to invoke ioctl method on
27 * @cmd: ioctl command to execute
28 * @arg: command-specific argument for ioctl
Erez Zadokdeb21db2008-02-07 00:13:25 -080029 *
30 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
Christoph Hellwigf6a4c8b2008-02-09 00:10:16 -080031 * invokes filesystem specific ->ioctl method. If neither method exists,
Erez Zadokdeb21db2008-02-07 00:13:25 -080032 * returns -ENOTTY.
33 *
34 * Returns 0 on success, -errno on error.
35 */
Adrian Bunk67cde592008-04-29 00:58:55 -070036static long vfs_ioctl(struct file *filp, unsigned int cmd,
37 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 int error = -ENOTTY;
40
41 if (!filp->f_op)
42 goto out;
43
44 if (filp->f_op->unlocked_ioctl) {
45 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
46 if (error == -ENOIOCTLCMD)
47 error = -EINVAL;
48 goto out;
Andrew Morton64d67d22007-07-15 23:41:02 -070049 } else if (filp->f_op->ioctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 lock_kernel();
Andrew Morton64d67d22007-07-15 23:41:02 -070051 error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
52 filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 unlock_kernel();
54 }
55
56 out:
57 return error;
58}
59
Erez Zadokaa81a7c2008-02-07 00:13:25 -080060static int ioctl_fibmap(struct file *filp, int __user *p)
61{
62 struct address_space *mapping = filp->f_mapping;
63 int res, block;
64
65 /* do we support this mess? */
66 if (!mapping->a_ops->bmap)
67 return -EINVAL;
68 if (!capable(CAP_SYS_RAWIO))
69 return -EPERM;
70 res = get_user(block, p);
71 if (res)
72 return res;
Erez Zadokaa81a7c2008-02-07 00:13:25 -080073 res = mapping->a_ops->bmap(mapping, block);
Erez Zadokaa81a7c2008-02-07 00:13:25 -080074 return put_user(res, p);
75}
76
Mark Fashehc4b929b2008-10-08 19:44:18 -040077/**
78 * fiemap_fill_next_extent - Fiemap helper function
79 * @fieinfo: Fiemap context passed into ->fiemap
80 * @logical: Extent logical start offset, in bytes
81 * @phys: Extent physical start offset, in bytes
82 * @len: Extent length, in bytes
83 * @flags: FIEMAP_EXTENT flags that describe this extent
84 *
85 * Called from file system ->fiemap callback. Will populate extent
86 * info as passed in via arguments and copy to user memory. On
87 * success, extent count on fieinfo is incremented.
88 *
89 * Returns 0 on success, -errno on error, 1 if this was the last
90 * extent that will fit in user array.
91 */
92#define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
93#define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED)
94#define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
95int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
96 u64 phys, u64 len, u32 flags)
97{
98 struct fiemap_extent extent;
99 struct fiemap_extent *dest = fieinfo->fi_extents_start;
100
101 /* only count the extents */
102 if (fieinfo->fi_extents_max == 0) {
103 fieinfo->fi_extents_mapped++;
104 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
105 }
106
107 if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
108 return 1;
109
110 if (flags & SET_UNKNOWN_FLAGS)
111 flags |= FIEMAP_EXTENT_UNKNOWN;
112 if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
113 flags |= FIEMAP_EXTENT_ENCODED;
114 if (flags & SET_NOT_ALIGNED_FLAGS)
115 flags |= FIEMAP_EXTENT_NOT_ALIGNED;
116
117 memset(&extent, 0, sizeof(extent));
118 extent.fe_logical = logical;
119 extent.fe_physical = phys;
120 extent.fe_length = len;
121 extent.fe_flags = flags;
122
123 dest += fieinfo->fi_extents_mapped;
124 if (copy_to_user(dest, &extent, sizeof(extent)))
125 return -EFAULT;
126
127 fieinfo->fi_extents_mapped++;
128 if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
129 return 1;
130 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
131}
132EXPORT_SYMBOL(fiemap_fill_next_extent);
133
134/**
135 * fiemap_check_flags - check validity of requested flags for fiemap
136 * @fieinfo: Fiemap context passed into ->fiemap
137 * @fs_flags: Set of fiemap flags that the file system understands
138 *
139 * Called from file system ->fiemap callback. This will compute the
140 * intersection of valid fiemap flags and those that the fs supports. That
141 * value is then compared against the user supplied flags. In case of bad user
142 * flags, the invalid values will be written into the fieinfo structure, and
143 * -EBADR is returned, which tells ioctl_fiemap() to return those values to
144 * userspace. For this reason, a return code of -EBADR should be preserved.
145 *
146 * Returns 0 on success, -EBADR on bad flags.
147 */
148int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
149{
150 u32 incompat_flags;
151
152 incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
153 if (incompat_flags) {
154 fieinfo->fi_flags = incompat_flags;
155 return -EBADR;
156 }
157 return 0;
158}
159EXPORT_SYMBOL(fiemap_check_flags);
160
161static int fiemap_check_ranges(struct super_block *sb,
162 u64 start, u64 len, u64 *new_len)
163{
164 *new_len = len;
165
166 if (len == 0)
167 return -EINVAL;
168
169 if (start > sb->s_maxbytes)
170 return -EFBIG;
171
172 /*
173 * Shrink request scope to what the fs can actually handle.
174 */
175 if ((len > sb->s_maxbytes) ||
176 (sb->s_maxbytes - len) < start)
177 *new_len = sb->s_maxbytes - start;
178
179 return 0;
180}
181
182static int ioctl_fiemap(struct file *filp, unsigned long arg)
183{
184 struct fiemap fiemap;
185 struct fiemap_extent_info fieinfo = { 0, };
186 struct inode *inode = filp->f_path.dentry->d_inode;
187 struct super_block *sb = inode->i_sb;
188 u64 len;
189 int error;
190
191 if (!inode->i_op->fiemap)
192 return -EOPNOTSUPP;
193
194 if (copy_from_user(&fiemap, (struct fiemap __user *)arg,
195 sizeof(struct fiemap)))
196 return -EFAULT;
197
198 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
199 return -EINVAL;
200
201 error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
202 &len);
203 if (error)
204 return error;
205
206 fieinfo.fi_flags = fiemap.fm_flags;
207 fieinfo.fi_extents_max = fiemap.fm_extent_count;
208 fieinfo.fi_extents_start = (struct fiemap_extent *)(arg + sizeof(fiemap));
209
210 if (fiemap.fm_extent_count != 0 &&
211 !access_ok(VERIFY_WRITE, fieinfo.fi_extents_start,
212 fieinfo.fi_extents_max * sizeof(struct fiemap_extent)))
213 return -EFAULT;
214
215 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
216 filemap_write_and_wait(inode->i_mapping);
217
218 error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len);
219 fiemap.fm_flags = fieinfo.fi_flags;
220 fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
221 if (copy_to_user((char *)arg, &fiemap, sizeof(fiemap)))
222 error = -EFAULT;
223
224 return error;
225}
226
Adrian Bunk06270d52008-10-12 07:15:19 +0300227#ifdef CONFIG_BLOCK
228
Josef Bacik68c9d702008-10-03 17:32:43 -0400229#define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits)
230#define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits);
231
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100232/**
233 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
Josef Bacik68c9d702008-10-03 17:32:43 -0400234 * @inode - the inode to map
235 * @arg - the pointer to userspace where we copy everything to
236 * @get_block - the fs's get_block function
237 *
238 * This does FIEMAP for block based inodes. Basically it will just loop
239 * through get_block until we hit the number of extents we want to map, or we
240 * go past the end of the file and hit a hole.
241 *
242 * If it is possible to have data blocks beyond a hole past @inode->i_size, then
243 * please do not use this function, it will stop at the first unmapped block
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100244 * beyond i_size.
245 *
246 * If you use this function directly, you need to do your own locking. Use
247 * generic_block_fiemap if you want the locking done for you.
Josef Bacik68c9d702008-10-03 17:32:43 -0400248 */
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100249
250int __generic_block_fiemap(struct inode *inode,
251 struct fiemap_extent_info *fieinfo, u64 start,
252 u64 len, get_block_t *get_block)
Josef Bacik68c9d702008-10-03 17:32:43 -0400253{
254 struct buffer_head tmp;
255 unsigned int start_blk;
256 long long length = 0, map_len = 0;
257 u64 logical = 0, phys = 0, size = 0;
258 u32 flags = FIEMAP_EXTENT_MERGED;
Josef Bacikdf3935f2009-05-06 16:02:53 -0700259 int ret = 0, past_eof = 0, whole_file = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400260
261 if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC)))
262 return ret;
263
264 start_blk = logical_to_blk(inode, start);
265
Josef Bacik68c9d702008-10-03 17:32:43 -0400266 length = (long long)min_t(u64, len, i_size_read(inode));
Josef Bacikdf3935f2009-05-06 16:02:53 -0700267 if (length < len)
268 whole_file = 1;
269
Josef Bacik68c9d702008-10-03 17:32:43 -0400270 map_len = length;
271
272 do {
273 /*
274 * we set b_size to the total size we want so it will map as
275 * many contiguous blocks as possible at once
276 */
277 memset(&tmp, 0, sizeof(struct buffer_head));
278 tmp.b_size = map_len;
279
280 ret = get_block(inode, start_blk, &tmp, 0);
281 if (ret)
282 break;
283
284 /* HOLE */
285 if (!buffer_mapped(&tmp)) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700286 length -= blk_to_logical(inode, 1);
287 start_blk++;
288
289 /*
290 * we want to handle the case where there is an
291 * allocated block at the front of the file, and then
292 * nothing but holes up to the end of the file properly,
293 * to make sure that extent at the front gets properly
294 * marked with FIEMAP_EXTENT_LAST
295 */
296 if (!past_eof &&
297 blk_to_logical(inode, start_blk) >=
298 blk_to_logical(inode, 0)+i_size_read(inode))
299 past_eof = 1;
300
Josef Bacik68c9d702008-10-03 17:32:43 -0400301 /*
302 * first hole after going past the EOF, this is our
303 * last extent
304 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700305 if (past_eof && size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400306 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
307 ret = fiemap_fill_next_extent(fieinfo, logical,
308 phys, size,
309 flags);
310 break;
311 }
312
Josef Bacik68c9d702008-10-03 17:32:43 -0400313 /* if we have holes up to/past EOF then we're done */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700314 if (length <= 0 || past_eof)
Josef Bacik68c9d702008-10-03 17:32:43 -0400315 break;
Josef Bacik68c9d702008-10-03 17:32:43 -0400316 } else {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700317 /*
318 * we have gone over the length of what we wanted to
319 * map, and it wasn't the entire file, so add the extent
320 * we got last time and exit.
321 *
322 * This is for the case where say we want to map all the
323 * way up to the second to the last block in a file, but
324 * the last block is a hole, making the second to last
325 * block FIEMAP_EXTENT_LAST. In this case we want to
326 * see if there is a hole after the second to last block
327 * so we can mark it properly. If we found data after
328 * we exceeded the length we were requesting, then we
329 * are good to go, just add the extent to the fieinfo
330 * and break
331 */
332 if (length <= 0 && !whole_file) {
333 ret = fiemap_fill_next_extent(fieinfo, logical,
334 phys, size,
335 flags);
336 break;
337 }
338
339 /*
340 * if size != 0 then we know we already have an extent
341 * to add, so add it.
342 */
343 if (size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400344 ret = fiemap_fill_next_extent(fieinfo, logical,
345 phys, size,
346 flags);
347 if (ret)
348 break;
349 }
350
351 logical = blk_to_logical(inode, start_blk);
352 phys = blk_to_logical(inode, tmp.b_blocknr);
353 size = tmp.b_size;
354 flags = FIEMAP_EXTENT_MERGED;
355
356 length -= tmp.b_size;
357 start_blk += logical_to_blk(inode, size);
358
359 /*
Josef Bacikdf3935f2009-05-06 16:02:53 -0700360 * If we are past the EOF, then we need to make sure as
361 * soon as we find a hole that the last extent we found
362 * is marked with FIEMAP_EXTENT_LAST
Josef Bacik68c9d702008-10-03 17:32:43 -0400363 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700364 if (!past_eof &&
365 logical+size >=
366 blk_to_logical(inode, 0)+i_size_read(inode))
367 past_eof = 1;
Josef Bacik68c9d702008-10-03 17:32:43 -0400368 }
369 cond_resched();
370 } while (1);
371
Josef Bacik68c9d702008-10-03 17:32:43 -0400372 /* if ret is 1 then we just hit the end of the extent array */
373 if (ret == 1)
374 ret = 0;
375
376 return ret;
377}
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100378EXPORT_SYMBOL(__generic_block_fiemap);
379
380/**
381 * generic_block_fiemap - FIEMAP for block based inodes
382 * @inode: The inode to map
383 * @fieinfo: The mapping information
384 * @start: The initial block to map
385 * @len: The length of the extect to attempt to map
386 * @get_block: The block mapping function for the fs
387 *
388 * Calls __generic_block_fiemap to map the inode, after taking
389 * the inode's mutex lock.
390 */
391
392int generic_block_fiemap(struct inode *inode,
393 struct fiemap_extent_info *fieinfo, u64 start,
394 u64 len, get_block_t *get_block)
395{
396 int ret;
397 mutex_lock(&inode->i_mutex);
398 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
399 mutex_unlock(&inode->i_mutex);
400 return ret;
401}
Josef Bacik68c9d702008-10-03 17:32:43 -0400402EXPORT_SYMBOL(generic_block_fiemap);
403
Adrian Bunk06270d52008-10-12 07:15:19 +0300404#endif /* CONFIG_BLOCK */
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static int file_ioctl(struct file *filp, unsigned int cmd,
407 unsigned long arg)
408{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800409 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int __user *p = (int __user *)arg;
411
412 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800413 case FIBMAP:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800414 return ioctl_fibmap(filp, p);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800415 case FIONREAD:
416 return put_user(i_size_read(inode) - filp->f_pos, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Erez Zadokdeb21db2008-02-07 00:13:25 -0800419 return vfs_ioctl(filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800422static int ioctl_fionbio(struct file *filp, int __user *argp)
423{
424 unsigned int flag;
425 int on, error;
426
427 error = get_user(on, argp);
428 if (error)
429 return error;
430 flag = O_NONBLOCK;
431#ifdef __sparc__
432 /* SunOS compatibility item. */
433 if (O_NONBLOCK != O_NDELAY)
434 flag |= O_NDELAY;
435#endif
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700436 spin_lock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800437 if (on)
438 filp->f_flags |= flag;
439 else
440 filp->f_flags &= ~flag;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700441 spin_unlock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800442 return error;
443}
444
445static int ioctl_fioasync(unsigned int fd, struct file *filp,
446 int __user *argp)
447{
448 unsigned int flag;
449 int on, error;
450
451 error = get_user(on, argp);
452 if (error)
453 return error;
454 flag = on ? FASYNC : 0;
455
456 /* Did FASYNC state change ? */
457 if ((flag ^ filp->f_flags) & FASYNC) {
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700458 if (filp->f_op && filp->f_op->fasync)
Jonathan Corbet76398422009-02-01 14:26:59 -0700459 /* fasync() adjusts filp->f_flags */
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800460 error = filp->f_op->fasync(fd, filp, on);
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700461 else
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800462 error = -ENOTTY;
463 }
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700464 return error < 0 ? error : 0;
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800465}
466
Takashi Satofcccf502009-01-09 16:40:59 -0800467static int ioctl_fsfreeze(struct file *filp)
468{
469 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
470
471 if (!capable(CAP_SYS_ADMIN))
472 return -EPERM;
473
474 /* If filesystem doesn't support freeze feature, return. */
475 if (sb->s_op->freeze_fs == NULL)
476 return -EOPNOTSUPP;
477
478 /* If a blockdevice-backed filesystem isn't specified, return. */
479 if (sb->s_bdev == NULL)
480 return -EINVAL;
481
482 /* Freeze */
483 sb = freeze_bdev(sb->s_bdev);
484 if (IS_ERR(sb))
485 return PTR_ERR(sb);
486 return 0;
487}
488
489static int ioctl_fsthaw(struct file *filp)
490{
491 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
492
493 if (!capable(CAP_SYS_ADMIN))
494 return -EPERM;
495
496 /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
497 if (sb->s_bdev == NULL)
498 return -EINVAL;
499
500 /* Thaw */
501 return thaw_bdev(sb->s_bdev, sb);
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/*
505 * When you add any new common ioctls to the switches above and below
506 * please update compat_sys_ioctl() too.
507 *
Erez Zadokdeb21db2008-02-07 00:13:25 -0800508 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
510 */
Erez Zadokdeb21db2008-02-07 00:13:25 -0800511int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
512 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800514 int error = 0;
515 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800518 case FIOCLEX:
519 set_close_on_exec(fd, 1);
520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Erez Zadokc9845ff2008-02-07 00:13:23 -0800522 case FIONCLEX:
523 set_close_on_exec(fd, 0);
524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Erez Zadokc9845ff2008-02-07 00:13:23 -0800526 case FIONBIO:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800527 error = ioctl_fionbio(filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800528 break;
529
530 case FIOASYNC:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800531 error = ioctl_fioasync(fd, filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800532 break;
533
534 case FIOQSIZE:
535 if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
536 S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
537 S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
538 loff_t res =
539 inode_get_bytes(filp->f_path.dentry->d_inode);
540 error = copy_to_user((loff_t __user *)arg, &res,
541 sizeof(res)) ? -EFAULT : 0;
542 } else
543 error = -ENOTTY;
544 break;
Takashi Satofcccf502009-01-09 16:40:59 -0800545
546 case FIFREEZE:
547 error = ioctl_fsfreeze(filp);
548 break;
549
550 case FITHAW:
551 error = ioctl_fsthaw(filp);
552 break;
553
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400554 case FS_IOC_FIEMAP:
555 return ioctl_fiemap(filp, arg);
556
557 case FIGETBSZ:
558 {
559 struct inode *inode = filp->f_path.dentry->d_inode;
560 int __user *p = (int __user *)arg;
561 return put_user(inode->i_sb->s_blocksize, p);
562 }
563
Erez Zadokc9845ff2008-02-07 00:13:23 -0800564 default:
565 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
566 error = file_ioctl(filp, cmd, arg);
567 else
Erez Zadokdeb21db2008-02-07 00:13:25 -0800568 error = vfs_ioctl(filp, cmd, arg);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571 return error;
572}
573
Heiko Carstensa26eab22009-01-14 14:14:17 +0100574SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800576 struct file *filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int error = -EBADF;
578 int fput_needed;
579
580 filp = fget_light(fd, &fput_needed);
581 if (!filp)
582 goto out;
583
584 error = security_file_ioctl(filp, cmd, arg);
585 if (error)
586 goto out_fput;
587
Erez Zadokdeb21db2008-02-07 00:13:25 -0800588 error = do_vfs_ioctl(filp, fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 out_fput:
590 fput_light(filp, fput_needed);
591 out:
592 return error;
593}