blob: 5612880fcbe7d7436f3579c7c1add7360170e407 [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>
Ankit Jain3e63cbb2009-06-19 14:28:07 -040018#include <linux/falloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/ioctls.h>
21
Mark Fashehc4b929b2008-10-08 19:44:18 -040022/* So that the fiemap access checks can't overflow on 32 bit machines. */
23#define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
24
Erez Zadokdeb21db2008-02-07 00:13:25 -080025/**
26 * vfs_ioctl - call filesystem specific ioctl methods
Christoph Hellwigf6a4c8b2008-02-09 00:10:16 -080027 * @filp: open file to invoke ioctl method on
28 * @cmd: ioctl command to execute
29 * @arg: command-specific argument for ioctl
Erez Zadokdeb21db2008-02-07 00:13:25 -080030 *
31 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
Christoph Hellwigf6a4c8b2008-02-09 00:10:16 -080032 * invokes filesystem specific ->ioctl method. If neither method exists,
Erez Zadokdeb21db2008-02-07 00:13:25 -080033 * returns -ENOTTY.
34 *
35 * Returns 0 on success, -errno on error.
36 */
Adrian Bunk67cde592008-04-29 00:58:55 -070037static long vfs_ioctl(struct file *filp, unsigned int cmd,
38 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 int error = -ENOTTY;
41
42 if (!filp->f_op)
43 goto out;
44
45 if (filp->f_op->unlocked_ioctl) {
46 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
47 if (error == -ENOIOCTLCMD)
48 error = -EINVAL;
49 goto out;
Andrew Morton64d67d22007-07-15 23:41:02 -070050 } else if (filp->f_op->ioctl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 lock_kernel();
Andrew Morton64d67d22007-07-15 23:41:02 -070052 error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
53 filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 unlock_kernel();
55 }
56
57 out:
58 return error;
59}
60
Erez Zadokaa81a7c2008-02-07 00:13:25 -080061static int ioctl_fibmap(struct file *filp, int __user *p)
62{
63 struct address_space *mapping = filp->f_mapping;
64 int res, block;
65
66 /* do we support this mess? */
67 if (!mapping->a_ops->bmap)
68 return -EINVAL;
69 if (!capable(CAP_SYS_RAWIO))
70 return -EPERM;
71 res = get_user(block, p);
72 if (res)
73 return res;
Erez Zadokaa81a7c2008-02-07 00:13:25 -080074 res = mapping->a_ops->bmap(mapping, block);
Erez Zadokaa81a7c2008-02-07 00:13:25 -080075 return put_user(res, p);
76}
77
Mark Fashehc4b929b2008-10-08 19:44:18 -040078/**
79 * fiemap_fill_next_extent - Fiemap helper function
80 * @fieinfo: Fiemap context passed into ->fiemap
81 * @logical: Extent logical start offset, in bytes
82 * @phys: Extent physical start offset, in bytes
83 * @len: Extent length, in bytes
84 * @flags: FIEMAP_EXTENT flags that describe this extent
85 *
86 * Called from file system ->fiemap callback. Will populate extent
87 * info as passed in via arguments and copy to user memory. On
88 * success, extent count on fieinfo is incremented.
89 *
90 * Returns 0 on success, -errno on error, 1 if this was the last
91 * extent that will fit in user array.
92 */
93#define SET_UNKNOWN_FLAGS (FIEMAP_EXTENT_DELALLOC)
94#define SET_NO_UNMOUNTED_IO_FLAGS (FIEMAP_EXTENT_DATA_ENCRYPTED)
95#define SET_NOT_ALIGNED_FLAGS (FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
96int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
97 u64 phys, u64 len, u32 flags)
98{
99 struct fiemap_extent extent;
100 struct fiemap_extent *dest = fieinfo->fi_extents_start;
101
102 /* only count the extents */
103 if (fieinfo->fi_extents_max == 0) {
104 fieinfo->fi_extents_mapped++;
105 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
106 }
107
108 if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
109 return 1;
110
111 if (flags & SET_UNKNOWN_FLAGS)
112 flags |= FIEMAP_EXTENT_UNKNOWN;
113 if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
114 flags |= FIEMAP_EXTENT_ENCODED;
115 if (flags & SET_NOT_ALIGNED_FLAGS)
116 flags |= FIEMAP_EXTENT_NOT_ALIGNED;
117
118 memset(&extent, 0, sizeof(extent));
119 extent.fe_logical = logical;
120 extent.fe_physical = phys;
121 extent.fe_length = len;
122 extent.fe_flags = flags;
123
124 dest += fieinfo->fi_extents_mapped;
125 if (copy_to_user(dest, &extent, sizeof(extent)))
126 return -EFAULT;
127
128 fieinfo->fi_extents_mapped++;
129 if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
130 return 1;
131 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
132}
133EXPORT_SYMBOL(fiemap_fill_next_extent);
134
135/**
136 * fiemap_check_flags - check validity of requested flags for fiemap
137 * @fieinfo: Fiemap context passed into ->fiemap
138 * @fs_flags: Set of fiemap flags that the file system understands
139 *
140 * Called from file system ->fiemap callback. This will compute the
141 * intersection of valid fiemap flags and those that the fs supports. That
142 * value is then compared against the user supplied flags. In case of bad user
143 * flags, the invalid values will be written into the fieinfo structure, and
144 * -EBADR is returned, which tells ioctl_fiemap() to return those values to
145 * userspace. For this reason, a return code of -EBADR should be preserved.
146 *
147 * Returns 0 on success, -EBADR on bad flags.
148 */
149int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
150{
151 u32 incompat_flags;
152
153 incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
154 if (incompat_flags) {
155 fieinfo->fi_flags = incompat_flags;
156 return -EBADR;
157 }
158 return 0;
159}
160EXPORT_SYMBOL(fiemap_check_flags);
161
162static int fiemap_check_ranges(struct super_block *sb,
163 u64 start, u64 len, u64 *new_len)
164{
165 *new_len = len;
166
167 if (len == 0)
168 return -EINVAL;
169
170 if (start > sb->s_maxbytes)
171 return -EFBIG;
172
173 /*
174 * Shrink request scope to what the fs can actually handle.
175 */
176 if ((len > sb->s_maxbytes) ||
177 (sb->s_maxbytes - len) < start)
178 *new_len = sb->s_maxbytes - start;
179
180 return 0;
181}
182
183static int ioctl_fiemap(struct file *filp, unsigned long arg)
184{
185 struct fiemap fiemap;
186 struct fiemap_extent_info fieinfo = { 0, };
187 struct inode *inode = filp->f_path.dentry->d_inode;
188 struct super_block *sb = inode->i_sb;
189 u64 len;
190 int error;
191
192 if (!inode->i_op->fiemap)
193 return -EOPNOTSUPP;
194
195 if (copy_from_user(&fiemap, (struct fiemap __user *)arg,
196 sizeof(struct fiemap)))
197 return -EFAULT;
198
199 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
200 return -EINVAL;
201
202 error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
203 &len);
204 if (error)
205 return error;
206
207 fieinfo.fi_flags = fiemap.fm_flags;
208 fieinfo.fi_extents_max = fiemap.fm_extent_count;
209 fieinfo.fi_extents_start = (struct fiemap_extent *)(arg + sizeof(fiemap));
210
211 if (fiemap.fm_extent_count != 0 &&
212 !access_ok(VERIFY_WRITE, fieinfo.fi_extents_start,
213 fieinfo.fi_extents_max * sizeof(struct fiemap_extent)))
214 return -EFAULT;
215
216 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
217 filemap_write_and_wait(inode->i_mapping);
218
219 error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len);
220 fiemap.fm_flags = fieinfo.fi_flags;
221 fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
222 if (copy_to_user((char *)arg, &fiemap, sizeof(fiemap)))
223 error = -EFAULT;
224
225 return error;
226}
227
Adrian Bunk06270d52008-10-12 07:15:19 +0300228#ifdef CONFIG_BLOCK
229
Josef Bacik68c9d702008-10-03 17:32:43 -0400230#define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits)
231#define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits);
232
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100233/**
234 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
Josef Bacik68c9d702008-10-03 17:32:43 -0400235 * @inode - the inode to map
236 * @arg - the pointer to userspace where we copy everything to
237 * @get_block - the fs's get_block function
238 *
239 * This does FIEMAP for block based inodes. Basically it will just loop
240 * through get_block until we hit the number of extents we want to map, or we
241 * go past the end of the file and hit a hole.
242 *
243 * If it is possible to have data blocks beyond a hole past @inode->i_size, then
244 * please do not use this function, it will stop at the first unmapped block
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100245 * beyond i_size.
246 *
247 * If you use this function directly, you need to do your own locking. Use
248 * generic_block_fiemap if you want the locking done for you.
Josef Bacik68c9d702008-10-03 17:32:43 -0400249 */
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100250
251int __generic_block_fiemap(struct inode *inode,
252 struct fiemap_extent_info *fieinfo, u64 start,
253 u64 len, get_block_t *get_block)
Josef Bacik68c9d702008-10-03 17:32:43 -0400254{
255 struct buffer_head tmp;
256 unsigned int start_blk;
257 long long length = 0, map_len = 0;
258 u64 logical = 0, phys = 0, size = 0;
259 u32 flags = FIEMAP_EXTENT_MERGED;
Josef Bacikdf3935f2009-05-06 16:02:53 -0700260 int ret = 0, past_eof = 0, whole_file = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400261
262 if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC)))
263 return ret;
264
265 start_blk = logical_to_blk(inode, start);
266
Josef Bacik68c9d702008-10-03 17:32:43 -0400267 length = (long long)min_t(u64, len, i_size_read(inode));
Josef Bacikdf3935f2009-05-06 16:02:53 -0700268 if (length < len)
269 whole_file = 1;
270
Josef Bacik68c9d702008-10-03 17:32:43 -0400271 map_len = length;
272
273 do {
274 /*
275 * we set b_size to the total size we want so it will map as
276 * many contiguous blocks as possible at once
277 */
278 memset(&tmp, 0, sizeof(struct buffer_head));
279 tmp.b_size = map_len;
280
281 ret = get_block(inode, start_blk, &tmp, 0);
282 if (ret)
283 break;
284
285 /* HOLE */
286 if (!buffer_mapped(&tmp)) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700287 length -= blk_to_logical(inode, 1);
288 start_blk++;
289
290 /*
291 * we want to handle the case where there is an
292 * allocated block at the front of the file, and then
293 * nothing but holes up to the end of the file properly,
294 * to make sure that extent at the front gets properly
295 * marked with FIEMAP_EXTENT_LAST
296 */
297 if (!past_eof &&
298 blk_to_logical(inode, start_blk) >=
299 blk_to_logical(inode, 0)+i_size_read(inode))
300 past_eof = 1;
301
Josef Bacik68c9d702008-10-03 17:32:43 -0400302 /*
303 * first hole after going past the EOF, this is our
304 * last extent
305 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700306 if (past_eof && size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400307 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
308 ret = fiemap_fill_next_extent(fieinfo, logical,
309 phys, size,
310 flags);
311 break;
312 }
313
Josef Bacik68c9d702008-10-03 17:32:43 -0400314 /* if we have holes up to/past EOF then we're done */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700315 if (length <= 0 || past_eof)
Josef Bacik68c9d702008-10-03 17:32:43 -0400316 break;
Josef Bacik68c9d702008-10-03 17:32:43 -0400317 } else {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700318 /*
319 * we have gone over the length of what we wanted to
320 * map, and it wasn't the entire file, so add the extent
321 * we got last time and exit.
322 *
323 * This is for the case where say we want to map all the
324 * way up to the second to the last block in a file, but
325 * the last block is a hole, making the second to last
326 * block FIEMAP_EXTENT_LAST. In this case we want to
327 * see if there is a hole after the second to last block
328 * so we can mark it properly. If we found data after
329 * we exceeded the length we were requesting, then we
330 * are good to go, just add the extent to the fieinfo
331 * and break
332 */
333 if (length <= 0 && !whole_file) {
334 ret = fiemap_fill_next_extent(fieinfo, logical,
335 phys, size,
336 flags);
337 break;
338 }
339
340 /*
341 * if size != 0 then we know we already have an extent
342 * to add, so add it.
343 */
344 if (size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400345 ret = fiemap_fill_next_extent(fieinfo, logical,
346 phys, size,
347 flags);
348 if (ret)
349 break;
350 }
351
352 logical = blk_to_logical(inode, start_blk);
353 phys = blk_to_logical(inode, tmp.b_blocknr);
354 size = tmp.b_size;
355 flags = FIEMAP_EXTENT_MERGED;
356
357 length -= tmp.b_size;
358 start_blk += logical_to_blk(inode, size);
359
360 /*
Josef Bacikdf3935f2009-05-06 16:02:53 -0700361 * If we are past the EOF, then we need to make sure as
362 * soon as we find a hole that the last extent we found
363 * is marked with FIEMAP_EXTENT_LAST
Josef Bacik68c9d702008-10-03 17:32:43 -0400364 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700365 if (!past_eof &&
366 logical+size >=
367 blk_to_logical(inode, 0)+i_size_read(inode))
368 past_eof = 1;
Josef Bacik68c9d702008-10-03 17:32:43 -0400369 }
370 cond_resched();
371 } while (1);
372
Josef Bacik68c9d702008-10-03 17:32:43 -0400373 /* if ret is 1 then we just hit the end of the extent array */
374 if (ret == 1)
375 ret = 0;
376
377 return ret;
378}
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100379EXPORT_SYMBOL(__generic_block_fiemap);
380
381/**
382 * generic_block_fiemap - FIEMAP for block based inodes
383 * @inode: The inode to map
384 * @fieinfo: The mapping information
385 * @start: The initial block to map
386 * @len: The length of the extect to attempt to map
387 * @get_block: The block mapping function for the fs
388 *
389 * Calls __generic_block_fiemap to map the inode, after taking
390 * the inode's mutex lock.
391 */
392
393int generic_block_fiemap(struct inode *inode,
394 struct fiemap_extent_info *fieinfo, u64 start,
395 u64 len, get_block_t *get_block)
396{
397 int ret;
398 mutex_lock(&inode->i_mutex);
399 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
400 mutex_unlock(&inode->i_mutex);
401 return ret;
402}
Josef Bacik68c9d702008-10-03 17:32:43 -0400403EXPORT_SYMBOL(generic_block_fiemap);
404
Adrian Bunk06270d52008-10-12 07:15:19 +0300405#endif /* CONFIG_BLOCK */
406
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400407/*
408 * This provides compatibility with legacy XFS pre-allocation ioctls
409 * which predate the fallocate syscall.
410 *
411 * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
412 * are used here, rest are ignored.
413 */
414int ioctl_preallocate(struct file *filp, void __user *argp)
415{
416 struct inode *inode = filp->f_path.dentry->d_inode;
417 struct space_resv sr;
418
419 if (copy_from_user(&sr, argp, sizeof(sr)))
420 return -EFAULT;
421
422 switch (sr.l_whence) {
423 case SEEK_SET:
424 break;
425 case SEEK_CUR:
426 sr.l_start += filp->f_pos;
427 break;
428 case SEEK_END:
429 sr.l_start += i_size_read(inode);
430 break;
431 default:
432 return -EINVAL;
433 }
434
435 return do_fallocate(filp, FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
436}
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static int file_ioctl(struct file *filp, unsigned int cmd,
439 unsigned long arg)
440{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800441 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int __user *p = (int __user *)arg;
443
444 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800445 case FIBMAP:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800446 return ioctl_fibmap(filp, p);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800447 case FIONREAD:
448 return put_user(i_size_read(inode) - filp->f_pos, p);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400449 case FS_IOC_RESVSP:
450 case FS_IOC_RESVSP64:
451 return ioctl_preallocate(filp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Erez Zadokdeb21db2008-02-07 00:13:25 -0800454 return vfs_ioctl(filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800457static int ioctl_fionbio(struct file *filp, int __user *argp)
458{
459 unsigned int flag;
460 int on, error;
461
462 error = get_user(on, argp);
463 if (error)
464 return error;
465 flag = O_NONBLOCK;
466#ifdef __sparc__
467 /* SunOS compatibility item. */
468 if (O_NONBLOCK != O_NDELAY)
469 flag |= O_NDELAY;
470#endif
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700471 spin_lock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800472 if (on)
473 filp->f_flags |= flag;
474 else
475 filp->f_flags &= ~flag;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700476 spin_unlock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800477 return error;
478}
479
480static int ioctl_fioasync(unsigned int fd, struct file *filp,
481 int __user *argp)
482{
483 unsigned int flag;
484 int on, error;
485
486 error = get_user(on, argp);
487 if (error)
488 return error;
489 flag = on ? FASYNC : 0;
490
491 /* Did FASYNC state change ? */
492 if ((flag ^ filp->f_flags) & FASYNC) {
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700493 if (filp->f_op && filp->f_op->fasync)
Jonathan Corbet76398422009-02-01 14:26:59 -0700494 /* fasync() adjusts filp->f_flags */
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800495 error = filp->f_op->fasync(fd, filp, on);
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700496 else
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800497 error = -ENOTTY;
498 }
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700499 return error < 0 ? error : 0;
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800500}
501
Takashi Satofcccf502009-01-09 16:40:59 -0800502static int ioctl_fsfreeze(struct file *filp)
503{
504 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
505
506 if (!capable(CAP_SYS_ADMIN))
507 return -EPERM;
508
509 /* If filesystem doesn't support freeze feature, return. */
510 if (sb->s_op->freeze_fs == NULL)
511 return -EOPNOTSUPP;
512
513 /* If a blockdevice-backed filesystem isn't specified, return. */
514 if (sb->s_bdev == NULL)
515 return -EINVAL;
516
517 /* Freeze */
518 sb = freeze_bdev(sb->s_bdev);
519 if (IS_ERR(sb))
520 return PTR_ERR(sb);
521 return 0;
522}
523
524static int ioctl_fsthaw(struct file *filp)
525{
526 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
527
528 if (!capable(CAP_SYS_ADMIN))
529 return -EPERM;
530
531 /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
532 if (sb->s_bdev == NULL)
533 return -EINVAL;
534
535 /* Thaw */
536 return thaw_bdev(sb->s_bdev, sb);
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/*
540 * When you add any new common ioctls to the switches above and below
541 * please update compat_sys_ioctl() too.
542 *
Erez Zadokdeb21db2008-02-07 00:13:25 -0800543 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
545 */
Erez Zadokdeb21db2008-02-07 00:13:25 -0800546int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
547 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800549 int error = 0;
550 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800553 case FIOCLEX:
554 set_close_on_exec(fd, 1);
555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Erez Zadokc9845ff2008-02-07 00:13:23 -0800557 case FIONCLEX:
558 set_close_on_exec(fd, 0);
559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Erez Zadokc9845ff2008-02-07 00:13:23 -0800561 case FIONBIO:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800562 error = ioctl_fionbio(filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800563 break;
564
565 case FIOASYNC:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800566 error = ioctl_fioasync(fd, filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800567 break;
568
569 case FIOQSIZE:
570 if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
571 S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
572 S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
573 loff_t res =
574 inode_get_bytes(filp->f_path.dentry->d_inode);
575 error = copy_to_user((loff_t __user *)arg, &res,
576 sizeof(res)) ? -EFAULT : 0;
577 } else
578 error = -ENOTTY;
579 break;
Takashi Satofcccf502009-01-09 16:40:59 -0800580
581 case FIFREEZE:
582 error = ioctl_fsfreeze(filp);
583 break;
584
585 case FITHAW:
586 error = ioctl_fsthaw(filp);
587 break;
588
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400589 case FS_IOC_FIEMAP:
590 return ioctl_fiemap(filp, arg);
591
592 case FIGETBSZ:
593 {
594 struct inode *inode = filp->f_path.dentry->d_inode;
595 int __user *p = (int __user *)arg;
596 return put_user(inode->i_sb->s_blocksize, p);
597 }
598
Erez Zadokc9845ff2008-02-07 00:13:23 -0800599 default:
600 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
601 error = file_ioctl(filp, cmd, arg);
602 else
Erez Zadokdeb21db2008-02-07 00:13:25 -0800603 error = vfs_ioctl(filp, cmd, arg);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800604 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 return error;
607}
608
Heiko Carstensa26eab22009-01-14 14:14:17 +0100609SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800611 struct file *filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int error = -EBADF;
613 int fput_needed;
614
615 filp = fget_light(fd, &fput_needed);
616 if (!filp)
617 goto out;
618
619 error = security_file_ioctl(filp, cmd, arg);
620 if (error)
621 goto out_fput;
622
Erez Zadokdeb21db2008-02-07 00:13:25 -0800623 error = do_vfs_ioctl(filp, fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 out_fput:
625 fput_light(filp, fput_needed);
626 out:
627 return error;
628}