blob: 82d9c42b8bac951f54fe0a09298eb90a0627f667 [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;
73 lock_kernel();
74 res = mapping->a_ops->bmap(mapping, block);
75 unlock_kernel();
76 return put_user(res, p);
77}
78
Mark Fashehc4b929b2008-10-08 19:44:18 -040079/**
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)
97int 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}
134EXPORT_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 */
150int 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}
161EXPORT_SYMBOL(fiemap_check_flags);
162
163static 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
184static 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
Adrian Bunk06270d52008-10-12 07:15:19 +0300229#ifdef CONFIG_BLOCK
230
Josef Bacik68c9d702008-10-03 17:32:43 -0400231#define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits)
232#define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits);
233
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100234/**
235 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
Josef Bacik68c9d702008-10-03 17:32:43 -0400236 * @inode - the inode to map
237 * @arg - the pointer to userspace where we copy everything to
238 * @get_block - the fs's get_block function
239 *
240 * This does FIEMAP for block based inodes. Basically it will just loop
241 * through get_block until we hit the number of extents we want to map, or we
242 * go past the end of the file and hit a hole.
243 *
244 * If it is possible to have data blocks beyond a hole past @inode->i_size, then
245 * please do not use this function, it will stop at the first unmapped block
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100246 * beyond i_size.
247 *
248 * If you use this function directly, you need to do your own locking. Use
249 * generic_block_fiemap if you want the locking done for you.
Josef Bacik68c9d702008-10-03 17:32:43 -0400250 */
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100251
252int __generic_block_fiemap(struct inode *inode,
253 struct fiemap_extent_info *fieinfo, u64 start,
254 u64 len, get_block_t *get_block)
Josef Bacik68c9d702008-10-03 17:32:43 -0400255{
256 struct buffer_head tmp;
257 unsigned int start_blk;
258 long long length = 0, map_len = 0;
259 u64 logical = 0, phys = 0, size = 0;
260 u32 flags = FIEMAP_EXTENT_MERGED;
Josef Bacikdf3935f2009-05-06 16:02:53 -0700261 int ret = 0, past_eof = 0, whole_file = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400262
263 if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC)))
264 return ret;
265
266 start_blk = logical_to_blk(inode, start);
267
Josef Bacik68c9d702008-10-03 17:32:43 -0400268 length = (long long)min_t(u64, len, i_size_read(inode));
Josef Bacikdf3935f2009-05-06 16:02:53 -0700269 if (length < len)
270 whole_file = 1;
271
Josef Bacik68c9d702008-10-03 17:32:43 -0400272 map_len = length;
273
274 do {
275 /*
276 * we set b_size to the total size we want so it will map as
277 * many contiguous blocks as possible at once
278 */
279 memset(&tmp, 0, sizeof(struct buffer_head));
280 tmp.b_size = map_len;
281
282 ret = get_block(inode, start_blk, &tmp, 0);
283 if (ret)
284 break;
285
286 /* HOLE */
287 if (!buffer_mapped(&tmp)) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700288 length -= blk_to_logical(inode, 1);
289 start_blk++;
290
291 /*
292 * we want to handle the case where there is an
293 * allocated block at the front of the file, and then
294 * nothing but holes up to the end of the file properly,
295 * to make sure that extent at the front gets properly
296 * marked with FIEMAP_EXTENT_LAST
297 */
298 if (!past_eof &&
299 blk_to_logical(inode, start_blk) >=
300 blk_to_logical(inode, 0)+i_size_read(inode))
301 past_eof = 1;
302
Josef Bacik68c9d702008-10-03 17:32:43 -0400303 /*
304 * first hole after going past the EOF, this is our
305 * last extent
306 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700307 if (past_eof && size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400308 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
309 ret = fiemap_fill_next_extent(fieinfo, logical,
310 phys, size,
311 flags);
312 break;
313 }
314
Josef Bacik68c9d702008-10-03 17:32:43 -0400315 /* if we have holes up to/past EOF then we're done */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700316 if (length <= 0 || past_eof)
Josef Bacik68c9d702008-10-03 17:32:43 -0400317 break;
Josef Bacik68c9d702008-10-03 17:32:43 -0400318 } else {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700319 /*
320 * we have gone over the length of what we wanted to
321 * map, and it wasn't the entire file, so add the extent
322 * we got last time and exit.
323 *
324 * This is for the case where say we want to map all the
325 * way up to the second to the last block in a file, but
326 * the last block is a hole, making the second to last
327 * block FIEMAP_EXTENT_LAST. In this case we want to
328 * see if there is a hole after the second to last block
329 * so we can mark it properly. If we found data after
330 * we exceeded the length we were requesting, then we
331 * are good to go, just add the extent to the fieinfo
332 * and break
333 */
334 if (length <= 0 && !whole_file) {
335 ret = fiemap_fill_next_extent(fieinfo, logical,
336 phys, size,
337 flags);
338 break;
339 }
340
341 /*
342 * if size != 0 then we know we already have an extent
343 * to add, so add it.
344 */
345 if (size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400346 ret = fiemap_fill_next_extent(fieinfo, logical,
347 phys, size,
348 flags);
349 if (ret)
350 break;
351 }
352
353 logical = blk_to_logical(inode, start_blk);
354 phys = blk_to_logical(inode, tmp.b_blocknr);
355 size = tmp.b_size;
356 flags = FIEMAP_EXTENT_MERGED;
357
358 length -= tmp.b_size;
359 start_blk += logical_to_blk(inode, size);
360
361 /*
Josef Bacikdf3935f2009-05-06 16:02:53 -0700362 * If we are past the EOF, then we need to make sure as
363 * soon as we find a hole that the last extent we found
364 * is marked with FIEMAP_EXTENT_LAST
Josef Bacik68c9d702008-10-03 17:32:43 -0400365 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700366 if (!past_eof &&
367 logical+size >=
368 blk_to_logical(inode, 0)+i_size_read(inode))
369 past_eof = 1;
Josef Bacik68c9d702008-10-03 17:32:43 -0400370 }
371 cond_resched();
372 } while (1);
373
Josef Bacik68c9d702008-10-03 17:32:43 -0400374 /* if ret is 1 then we just hit the end of the extent array */
375 if (ret == 1)
376 ret = 0;
377
378 return ret;
379}
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100380EXPORT_SYMBOL(__generic_block_fiemap);
381
382/**
383 * generic_block_fiemap - FIEMAP for block based inodes
384 * @inode: The inode to map
385 * @fieinfo: The mapping information
386 * @start: The initial block to map
387 * @len: The length of the extect to attempt to map
388 * @get_block: The block mapping function for the fs
389 *
390 * Calls __generic_block_fiemap to map the inode, after taking
391 * the inode's mutex lock.
392 */
393
394int generic_block_fiemap(struct inode *inode,
395 struct fiemap_extent_info *fieinfo, u64 start,
396 u64 len, get_block_t *get_block)
397{
398 int ret;
399 mutex_lock(&inode->i_mutex);
400 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
401 mutex_unlock(&inode->i_mutex);
402 return ret;
403}
Josef Bacik68c9d702008-10-03 17:32:43 -0400404EXPORT_SYMBOL(generic_block_fiemap);
405
Adrian Bunk06270d52008-10-12 07:15:19 +0300406#endif /* CONFIG_BLOCK */
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static int file_ioctl(struct file *filp, unsigned int cmd,
409 unsigned long arg)
410{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800411 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 int __user *p = (int __user *)arg;
413
414 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800415 case FIBMAP:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800416 return ioctl_fibmap(filp, p);
Mark Fashehc4b929b2008-10-08 19:44:18 -0400417 case FS_IOC_FIEMAP:
418 return ioctl_fiemap(filp, arg);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800419 case FIGETBSZ:
420 return put_user(inode->i_sb->s_blocksize, p);
421 case FIONREAD:
422 return put_user(i_size_read(inode) - filp->f_pos, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Erez Zadokdeb21db2008-02-07 00:13:25 -0800425 return vfs_ioctl(filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800428static int ioctl_fionbio(struct file *filp, int __user *argp)
429{
430 unsigned int flag;
431 int on, error;
432
433 error = get_user(on, argp);
434 if (error)
435 return error;
436 flag = O_NONBLOCK;
437#ifdef __sparc__
438 /* SunOS compatibility item. */
439 if (O_NONBLOCK != O_NDELAY)
440 flag |= O_NDELAY;
441#endif
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700442 spin_lock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800443 if (on)
444 filp->f_flags |= flag;
445 else
446 filp->f_flags &= ~flag;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700447 spin_unlock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800448 return error;
449}
450
451static int ioctl_fioasync(unsigned int fd, struct file *filp,
452 int __user *argp)
453{
454 unsigned int flag;
455 int on, error;
456
457 error = get_user(on, argp);
458 if (error)
459 return error;
460 flag = on ? FASYNC : 0;
461
462 /* Did FASYNC state change ? */
463 if ((flag ^ filp->f_flags) & FASYNC) {
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700464 if (filp->f_op && filp->f_op->fasync)
Jonathan Corbet76398422009-02-01 14:26:59 -0700465 /* fasync() adjusts filp->f_flags */
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800466 error = filp->f_op->fasync(fd, filp, on);
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700467 else
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800468 error = -ENOTTY;
469 }
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700470 return error < 0 ? error : 0;
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800471}
472
Takashi Satofcccf502009-01-09 16:40:59 -0800473static int ioctl_fsfreeze(struct file *filp)
474{
475 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
476
477 if (!capable(CAP_SYS_ADMIN))
478 return -EPERM;
479
480 /* If filesystem doesn't support freeze feature, return. */
481 if (sb->s_op->freeze_fs == NULL)
482 return -EOPNOTSUPP;
483
484 /* If a blockdevice-backed filesystem isn't specified, return. */
485 if (sb->s_bdev == NULL)
486 return -EINVAL;
487
488 /* Freeze */
489 sb = freeze_bdev(sb->s_bdev);
490 if (IS_ERR(sb))
491 return PTR_ERR(sb);
492 return 0;
493}
494
495static int ioctl_fsthaw(struct file *filp)
496{
497 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
498
499 if (!capable(CAP_SYS_ADMIN))
500 return -EPERM;
501
502 /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
503 if (sb->s_bdev == NULL)
504 return -EINVAL;
505
506 /* Thaw */
507 return thaw_bdev(sb->s_bdev, sb);
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/*
511 * When you add any new common ioctls to the switches above and below
512 * please update compat_sys_ioctl() too.
513 *
Erez Zadokdeb21db2008-02-07 00:13:25 -0800514 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
516 */
Erez Zadokdeb21db2008-02-07 00:13:25 -0800517int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
518 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800520 int error = 0;
521 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800524 case FIOCLEX:
525 set_close_on_exec(fd, 1);
526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Erez Zadokc9845ff2008-02-07 00:13:23 -0800528 case FIONCLEX:
529 set_close_on_exec(fd, 0);
530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Erez Zadokc9845ff2008-02-07 00:13:23 -0800532 case FIONBIO:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800533 error = ioctl_fionbio(filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800534 break;
535
536 case FIOASYNC:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800537 error = ioctl_fioasync(fd, filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800538 break;
539
540 case FIOQSIZE:
541 if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
542 S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
543 S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
544 loff_t res =
545 inode_get_bytes(filp->f_path.dentry->d_inode);
546 error = copy_to_user((loff_t __user *)arg, &res,
547 sizeof(res)) ? -EFAULT : 0;
548 } else
549 error = -ENOTTY;
550 break;
Takashi Satofcccf502009-01-09 16:40:59 -0800551
552 case FIFREEZE:
553 error = ioctl_fsfreeze(filp);
554 break;
555
556 case FITHAW:
557 error = ioctl_fsthaw(filp);
558 break;
559
Erez Zadokc9845ff2008-02-07 00:13:23 -0800560 default:
561 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
562 error = file_ioctl(filp, cmd, arg);
563 else
Erez Zadokdeb21db2008-02-07 00:13:25 -0800564 error = vfs_ioctl(filp, cmd, arg);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 return error;
568}
569
Heiko Carstensa26eab22009-01-14 14:14:17 +0100570SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800572 struct file *filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int error = -EBADF;
574 int fput_needed;
575
576 filp = fget_light(fd, &fput_needed);
577 if (!filp)
578 goto out;
579
580 error = security_file_ioctl(filp, cmd, arg);
581 if (error)
582 goto out_fput;
583
Erez Zadokdeb21db2008-02-07 00:13:25 -0800584 error = do_vfs_ioctl(filp, fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 out_fput:
586 fput_light(filp, fput_needed);
587 out:
588 return error;
589}