blob: 2d140a713861badfb1c9b936c683805dab30853a [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{
Jeff Layton5aa98b72009-09-18 13:05:50 -0700165 u64 maxbytes = (u64) sb->s_maxbytes;
166
Mark Fashehc4b929b2008-10-08 19:44:18 -0400167 *new_len = len;
168
169 if (len == 0)
170 return -EINVAL;
171
Jeff Layton5aa98b72009-09-18 13:05:50 -0700172 if (start > maxbytes)
Mark Fashehc4b929b2008-10-08 19:44:18 -0400173 return -EFBIG;
174
175 /*
176 * Shrink request scope to what the fs can actually handle.
177 */
Jeff Layton5aa98b72009-09-18 13:05:50 -0700178 if (len > maxbytes || (maxbytes - len) < start)
179 *new_len = maxbytes - start;
Mark Fashehc4b929b2008-10-08 19:44:18 -0400180
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 Bacik3a3076f2010-04-23 12:17:17 -0400231static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
232{
233 return (offset >> inode->i_blkbits);
234}
235
236static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
237{
238 return (blk << inode->i_blkbits);
239}
Josef Bacik68c9d702008-10-03 17:32:43 -0400240
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100241/**
242 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
Josef Bacik3a3076f2010-04-23 12:17:17 -0400243 * @inode: the inode to map
244 * @fieinfo: the fiemap info struct that will be passed back to userspace
245 * @start: where to start mapping in the inode
246 * @len: how much space to map
247 * @get_block: the fs's get_block function
Josef Bacik68c9d702008-10-03 17:32:43 -0400248 *
249 * This does FIEMAP for block based inodes. Basically it will just loop
250 * through get_block until we hit the number of extents we want to map, or we
251 * go past the end of the file and hit a hole.
252 *
253 * If it is possible to have data blocks beyond a hole past @inode->i_size, then
254 * please do not use this function, it will stop at the first unmapped block
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100255 * beyond i_size.
256 *
257 * If you use this function directly, you need to do your own locking. Use
258 * generic_block_fiemap if you want the locking done for you.
Josef Bacik68c9d702008-10-03 17:32:43 -0400259 */
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100260
261int __generic_block_fiemap(struct inode *inode,
Josef Bacik3a3076f2010-04-23 12:17:17 -0400262 struct fiemap_extent_info *fieinfo, loff_t start,
263 loff_t len, get_block_t *get_block)
Josef Bacik68c9d702008-10-03 17:32:43 -0400264{
Josef Bacik3a3076f2010-04-23 12:17:17 -0400265 struct buffer_head map_bh;
266 sector_t start_blk, last_blk;
267 loff_t isize = i_size_read(inode);
Josef Bacik68c9d702008-10-03 17:32:43 -0400268 u64 logical = 0, phys = 0, size = 0;
269 u32 flags = FIEMAP_EXTENT_MERGED;
Josef Bacik3a3076f2010-04-23 12:17:17 -0400270 bool past_eof = false, whole_file = false;
271 int ret = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400272
Josef Bacik3a3076f2010-04-23 12:17:17 -0400273 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
274 if (ret)
Josef Bacik68c9d702008-10-03 17:32:43 -0400275 return ret;
276
Josef Bacik3a3076f2010-04-23 12:17:17 -0400277 /*
278 * Either the i_mutex or other appropriate locking needs to be held
279 * since we expect isize to not change at all through the duration of
280 * this call.
281 */
282 if (len >= isize) {
283 whole_file = true;
284 len = isize;
285 }
286
Josef Bacik68c9d702008-10-03 17:32:43 -0400287 start_blk = logical_to_blk(inode, start);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400288 last_blk = logical_to_blk(inode, start + len - 1);
Josef Bacik68c9d702008-10-03 17:32:43 -0400289
290 do {
291 /*
292 * we set b_size to the total size we want so it will map as
293 * many contiguous blocks as possible at once
294 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400295 memset(&map_bh, 0, sizeof(struct buffer_head));
296 map_bh.b_size = len;
Josef Bacik68c9d702008-10-03 17:32:43 -0400297
Josef Bacik3a3076f2010-04-23 12:17:17 -0400298 ret = get_block(inode, start_blk, &map_bh, 0);
Josef Bacik68c9d702008-10-03 17:32:43 -0400299 if (ret)
300 break;
301
302 /* HOLE */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400303 if (!buffer_mapped(&map_bh)) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700304 start_blk++;
305
306 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400307 * We want to handle the case where there is an
Josef Bacikdf3935f2009-05-06 16:02:53 -0700308 * allocated block at the front of the file, and then
309 * nothing but holes up to the end of the file properly,
310 * to make sure that extent at the front gets properly
311 * marked with FIEMAP_EXTENT_LAST
312 */
313 if (!past_eof &&
Josef Bacik3a3076f2010-04-23 12:17:17 -0400314 blk_to_logical(inode, start_blk) >= isize)
Josef Bacikdf3935f2009-05-06 16:02:53 -0700315 past_eof = 1;
316
Josef Bacik68c9d702008-10-03 17:32:43 -0400317 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400318 * First hole after going past the EOF, this is our
Josef Bacik68c9d702008-10-03 17:32:43 -0400319 * last extent
320 */
Josef Bacikdf3935f2009-05-06 16:02:53 -0700321 if (past_eof && size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400322 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
323 ret = fiemap_fill_next_extent(fieinfo, logical,
324 phys, size,
325 flags);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400326 } else if (size) {
327 ret = fiemap_fill_next_extent(fieinfo, logical,
328 phys, size, flags);
329 size = 0;
Josef Bacik68c9d702008-10-03 17:32:43 -0400330 }
331
Josef Bacik68c9d702008-10-03 17:32:43 -0400332 /* if we have holes up to/past EOF then we're done */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400333 if (start_blk > last_blk || past_eof || ret)
Josef Bacik68c9d702008-10-03 17:32:43 -0400334 break;
Josef Bacik68c9d702008-10-03 17:32:43 -0400335 } else {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700336 /*
Josef Bacik3a3076f2010-04-23 12:17:17 -0400337 * We have gone over the length of what we wanted to
Josef Bacikdf3935f2009-05-06 16:02:53 -0700338 * map, and it wasn't the entire file, so add the extent
339 * we got last time and exit.
340 *
341 * This is for the case where say we want to map all the
342 * way up to the second to the last block in a file, but
343 * the last block is a hole, making the second to last
344 * block FIEMAP_EXTENT_LAST. In this case we want to
345 * see if there is a hole after the second to last block
346 * so we can mark it properly. If we found data after
347 * we exceeded the length we were requesting, then we
348 * are good to go, just add the extent to the fieinfo
349 * and break
350 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400351 if (start_blk > last_blk && !whole_file) {
Josef Bacikdf3935f2009-05-06 16:02:53 -0700352 ret = fiemap_fill_next_extent(fieinfo, logical,
353 phys, size,
354 flags);
355 break;
356 }
357
358 /*
359 * if size != 0 then we know we already have an extent
360 * to add, so add it.
361 */
362 if (size) {
Josef Bacik68c9d702008-10-03 17:32:43 -0400363 ret = fiemap_fill_next_extent(fieinfo, logical,
364 phys, size,
365 flags);
366 if (ret)
367 break;
368 }
369
370 logical = blk_to_logical(inode, start_blk);
Josef Bacik3a3076f2010-04-23 12:17:17 -0400371 phys = blk_to_logical(inode, map_bh.b_blocknr);
372 size = map_bh.b_size;
Josef Bacik68c9d702008-10-03 17:32:43 -0400373 flags = FIEMAP_EXTENT_MERGED;
374
Josef Bacik68c9d702008-10-03 17:32:43 -0400375 start_blk += logical_to_blk(inode, size);
376
377 /*
Josef Bacikdf3935f2009-05-06 16:02:53 -0700378 * If we are past the EOF, then we need to make sure as
379 * soon as we find a hole that the last extent we found
380 * is marked with FIEMAP_EXTENT_LAST
Josef Bacik68c9d702008-10-03 17:32:43 -0400381 */
Josef Bacik3a3076f2010-04-23 12:17:17 -0400382 if (!past_eof && logical + size >= isize)
383 past_eof = true;
Josef Bacik68c9d702008-10-03 17:32:43 -0400384 }
385 cond_resched();
386 } while (1);
387
Josef Bacik3a3076f2010-04-23 12:17:17 -0400388 /* If ret is 1 then we just hit the end of the extent array */
Josef Bacik68c9d702008-10-03 17:32:43 -0400389 if (ret == 1)
390 ret = 0;
391
392 return ret;
393}
Steven Whitehousee9079cc2008-10-14 14:43:29 +0100394EXPORT_SYMBOL(__generic_block_fiemap);
395
396/**
397 * generic_block_fiemap - FIEMAP for block based inodes
398 * @inode: The inode to map
399 * @fieinfo: The mapping information
400 * @start: The initial block to map
401 * @len: The length of the extect to attempt to map
402 * @get_block: The block mapping function for the fs
403 *
404 * Calls __generic_block_fiemap to map the inode, after taking
405 * the inode's mutex lock.
406 */
407
408int generic_block_fiemap(struct inode *inode,
409 struct fiemap_extent_info *fieinfo, u64 start,
410 u64 len, get_block_t *get_block)
411{
412 int ret;
413 mutex_lock(&inode->i_mutex);
414 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
415 mutex_unlock(&inode->i_mutex);
416 return ret;
417}
Josef Bacik68c9d702008-10-03 17:32:43 -0400418EXPORT_SYMBOL(generic_block_fiemap);
419
Adrian Bunk06270d52008-10-12 07:15:19 +0300420#endif /* CONFIG_BLOCK */
421
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400422/*
423 * This provides compatibility with legacy XFS pre-allocation ioctls
424 * which predate the fallocate syscall.
425 *
426 * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
427 * are used here, rest are ignored.
428 */
429int ioctl_preallocate(struct file *filp, void __user *argp)
430{
431 struct inode *inode = filp->f_path.dentry->d_inode;
432 struct space_resv sr;
433
434 if (copy_from_user(&sr, argp, sizeof(sr)))
435 return -EFAULT;
436
437 switch (sr.l_whence) {
438 case SEEK_SET:
439 break;
440 case SEEK_CUR:
441 sr.l_start += filp->f_pos;
442 break;
443 case SEEK_END:
444 sr.l_start += i_size_read(inode);
445 break;
446 default:
447 return -EINVAL;
448 }
449
450 return do_fallocate(filp, FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453static int file_ioctl(struct file *filp, unsigned int cmd,
454 unsigned long arg)
455{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800456 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int __user *p = (int __user *)arg;
458
459 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800460 case FIBMAP:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800461 return ioctl_fibmap(filp, p);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800462 case FIONREAD:
463 return put_user(i_size_read(inode) - filp->f_pos, p);
Ankit Jain3e63cbb2009-06-19 14:28:07 -0400464 case FS_IOC_RESVSP:
465 case FS_IOC_RESVSP64:
466 return ioctl_preallocate(filp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
Erez Zadokdeb21db2008-02-07 00:13:25 -0800469 return vfs_ioctl(filp, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800472static int ioctl_fionbio(struct file *filp, int __user *argp)
473{
474 unsigned int flag;
475 int on, error;
476
477 error = get_user(on, argp);
478 if (error)
479 return error;
480 flag = O_NONBLOCK;
481#ifdef __sparc__
482 /* SunOS compatibility item. */
483 if (O_NONBLOCK != O_NDELAY)
484 flag |= O_NDELAY;
485#endif
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700486 spin_lock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800487 if (on)
488 filp->f_flags |= flag;
489 else
490 filp->f_flags &= ~flag;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -0700491 spin_unlock(&filp->f_lock);
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800492 return error;
493}
494
495static int ioctl_fioasync(unsigned int fd, struct file *filp,
496 int __user *argp)
497{
498 unsigned int flag;
499 int on, error;
500
501 error = get_user(on, argp);
502 if (error)
503 return error;
504 flag = on ? FASYNC : 0;
505
506 /* Did FASYNC state change ? */
507 if ((flag ^ filp->f_flags) & FASYNC) {
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700508 if (filp->f_op && filp->f_op->fasync)
Jonathan Corbet76398422009-02-01 14:26:59 -0700509 /* fasync() adjusts filp->f_flags */
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800510 error = filp->f_op->fasync(fd, filp, on);
Jonathan Corbet218d11a2008-12-05 16:12:48 -0700511 else
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800512 error = -ENOTTY;
513 }
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700514 return error < 0 ? error : 0;
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800515}
516
Takashi Satofcccf502009-01-09 16:40:59 -0800517static int ioctl_fsfreeze(struct file *filp)
518{
519 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
520
521 if (!capable(CAP_SYS_ADMIN))
522 return -EPERM;
523
524 /* If filesystem doesn't support freeze feature, return. */
525 if (sb->s_op->freeze_fs == NULL)
526 return -EOPNOTSUPP;
527
Takashi Satofcccf502009-01-09 16:40:59 -0800528 /* Freeze */
Josef Bacik18e9e512010-03-23 10:34:56 -0400529 return freeze_super(sb);
Takashi Satofcccf502009-01-09 16:40:59 -0800530}
531
532static int ioctl_fsthaw(struct file *filp)
533{
534 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
535
536 if (!capable(CAP_SYS_ADMIN))
537 return -EPERM;
538
Takashi Satofcccf502009-01-09 16:40:59 -0800539 /* Thaw */
Josef Bacik18e9e512010-03-23 10:34:56 -0400540 return thaw_super(sb);
Takashi Satofcccf502009-01-09 16:40:59 -0800541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
544 * When you add any new common ioctls to the switches above and below
545 * please update compat_sys_ioctl() too.
546 *
Erez Zadokdeb21db2008-02-07 00:13:25 -0800547 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
549 */
Erez Zadokdeb21db2008-02-07 00:13:25 -0800550int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
551 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800553 int error = 0;
554 int __user *argp = (int __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 switch (cmd) {
Erez Zadokc9845ff2008-02-07 00:13:23 -0800557 case FIOCLEX:
558 set_close_on_exec(fd, 1);
559 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Erez Zadokc9845ff2008-02-07 00:13:23 -0800561 case FIONCLEX:
562 set_close_on_exec(fd, 0);
563 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Erez Zadokc9845ff2008-02-07 00:13:23 -0800565 case FIONBIO:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800566 error = ioctl_fionbio(filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800567 break;
568
569 case FIOASYNC:
Erez Zadokaa81a7c2008-02-07 00:13:25 -0800570 error = ioctl_fioasync(fd, filp, argp);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800571 break;
572
573 case FIOQSIZE:
574 if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
575 S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
576 S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
577 loff_t res =
578 inode_get_bytes(filp->f_path.dentry->d_inode);
579 error = copy_to_user((loff_t __user *)arg, &res,
580 sizeof(res)) ? -EFAULT : 0;
581 } else
582 error = -ENOTTY;
583 break;
Takashi Satofcccf502009-01-09 16:40:59 -0800584
585 case FIFREEZE:
586 error = ioctl_fsfreeze(filp);
587 break;
588
589 case FITHAW:
590 error = ioctl_fsthaw(filp);
591 break;
592
Aneesh Kumar K.V19ba0552009-05-13 18:12:05 -0400593 case FS_IOC_FIEMAP:
594 return ioctl_fiemap(filp, arg);
595
596 case FIGETBSZ:
597 {
598 struct inode *inode = filp->f_path.dentry->d_inode;
599 int __user *p = (int __user *)arg;
600 return put_user(inode->i_sb->s_blocksize, p);
601 }
602
Erez Zadokc9845ff2008-02-07 00:13:23 -0800603 default:
604 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
605 error = file_ioctl(filp, cmd, arg);
606 else
Erez Zadokdeb21db2008-02-07 00:13:25 -0800607 error = vfs_ioctl(filp, cmd, arg);
Erez Zadokc9845ff2008-02-07 00:13:23 -0800608 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 return error;
611}
612
Heiko Carstensa26eab22009-01-14 14:14:17 +0100613SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Erez Zadokc9845ff2008-02-07 00:13:23 -0800615 struct file *filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 int error = -EBADF;
617 int fput_needed;
618
619 filp = fget_light(fd, &fput_needed);
620 if (!filp)
621 goto out;
622
623 error = security_file_ioctl(filp, cmd, arg);
624 if (error)
625 goto out_fput;
626
Erez Zadokdeb21db2008-02-07 00:13:25 -0800627 error = do_vfs_ioctl(filp, fd, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 out_fput:
629 fput_light(filp, fput_needed);
630 out:
631 return error;
632}