]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ioctl.c
Use f_lock to protect f_flags
[net-next-2.6.git] / fs / ioctl.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/ioctl.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
1da177e4
LT
7#include <linux/syscalls.h>
8#include <linux/mm.h>
9#include <linux/smp_lock.h>
16f7e0fe 10#include <linux/capability.h>
1da177e4
LT
11#include <linux/file.h>
12#include <linux/fs.h>
13#include <linux/security.h>
14#include <linux/module.h>
c9845ff1 15#include <linux/uaccess.h>
68c9d702
JB
16#include <linux/writeback.h>
17#include <linux/buffer_head.h>
1da177e4 18
1da177e4
LT
19#include <asm/ioctls.h>
20
c4b929b8
MF
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
deb21db7
EZ
24/**
25 * vfs_ioctl - call filesystem specific ioctl methods
f6a4c8bd
CH
26 * @filp: open file to invoke ioctl method on
27 * @cmd: ioctl command to execute
28 * @arg: command-specific argument for ioctl
deb21db7
EZ
29 *
30 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
f6a4c8bd 31 * invokes filesystem specific ->ioctl method. If neither method exists,
deb21db7
EZ
32 * returns -ENOTTY.
33 *
34 * Returns 0 on success, -errno on error.
35 */
67cde595
AB
36static long vfs_ioctl(struct file *filp, unsigned int cmd,
37 unsigned long arg)
1da177e4
LT
38{
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;
64d67d21 49 } else if (filp->f_op->ioctl) {
1da177e4 50 lock_kernel();
64d67d21
AM
51 error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
52 filp, cmd, arg);
1da177e4
LT
53 unlock_kernel();
54 }
55
56 out:
57 return error;
58}
59
aa81a7c7
EZ
60static 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
c4b929b8
MF
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)
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
06270d5d
AB
229#ifdef CONFIG_BLOCK
230
68c9d702
JB
231#define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits)
232#define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits);
233
e9079cce
SW
234/**
235 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
68c9d702
JB
236 * @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
e9079cce
SW
246 * 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.
68c9d702 250 */
e9079cce
SW
251
252int __generic_block_fiemap(struct inode *inode,
253 struct fiemap_extent_info *fieinfo, u64 start,
254 u64 len, get_block_t *get_block)
68c9d702
JB
255{
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;
261 int ret = 0;
262
263 if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC)))
264 return ret;
265
266 start_blk = logical_to_blk(inode, start);
267
68c9d702
JB
268 length = (long long)min_t(u64, len, i_size_read(inode));
269 map_len = length;
270
271 do {
272 /*
273 * we set b_size to the total size we want so it will map as
274 * many contiguous blocks as possible at once
275 */
276 memset(&tmp, 0, sizeof(struct buffer_head));
277 tmp.b_size = map_len;
278
279 ret = get_block(inode, start_blk, &tmp, 0);
280 if (ret)
281 break;
282
283 /* HOLE */
284 if (!buffer_mapped(&tmp)) {
285 /*
286 * first hole after going past the EOF, this is our
287 * last extent
288 */
289 if (length <= 0) {
290 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
291 ret = fiemap_fill_next_extent(fieinfo, logical,
292 phys, size,
293 flags);
294 break;
295 }
296
297 length -= blk_to_logical(inode, 1);
298
299 /* if we have holes up to/past EOF then we're done */
300 if (length <= 0)
301 break;
302
303 start_blk++;
304 } else {
305 if (length <= 0 && size) {
306 ret = fiemap_fill_next_extent(fieinfo, logical,
307 phys, size,
308 flags);
309 if (ret)
310 break;
311 }
312
313 logical = blk_to_logical(inode, start_blk);
314 phys = blk_to_logical(inode, tmp.b_blocknr);
315 size = tmp.b_size;
316 flags = FIEMAP_EXTENT_MERGED;
317
318 length -= tmp.b_size;
319 start_blk += logical_to_blk(inode, size);
320
321 /*
322 * if we are past the EOF we need to loop again to see
323 * if there is a hole so we can mark this extent as the
324 * last one, and if not keep mapping things until we
325 * find a hole, or we run out of slots in the extent
326 * array
327 */
328 if (length <= 0)
329 continue;
330
331 ret = fiemap_fill_next_extent(fieinfo, logical, phys,
332 size, flags);
333 if (ret)
334 break;
335 }
336 cond_resched();
337 } while (1);
338
68c9d702
JB
339 /* if ret is 1 then we just hit the end of the extent array */
340 if (ret == 1)
341 ret = 0;
342
343 return ret;
344}
e9079cce
SW
345EXPORT_SYMBOL(__generic_block_fiemap);
346
347/**
348 * generic_block_fiemap - FIEMAP for block based inodes
349 * @inode: The inode to map
350 * @fieinfo: The mapping information
351 * @start: The initial block to map
352 * @len: The length of the extect to attempt to map
353 * @get_block: The block mapping function for the fs
354 *
355 * Calls __generic_block_fiemap to map the inode, after taking
356 * the inode's mutex lock.
357 */
358
359int generic_block_fiemap(struct inode *inode,
360 struct fiemap_extent_info *fieinfo, u64 start,
361 u64 len, get_block_t *get_block)
362{
363 int ret;
364 mutex_lock(&inode->i_mutex);
365 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
366 mutex_unlock(&inode->i_mutex);
367 return ret;
368}
68c9d702
JB
369EXPORT_SYMBOL(generic_block_fiemap);
370
06270d5d
AB
371#endif /* CONFIG_BLOCK */
372
1da177e4
LT
373static int file_ioctl(struct file *filp, unsigned int cmd,
374 unsigned long arg)
375{
c9845ff1 376 struct inode *inode = filp->f_path.dentry->d_inode;
1da177e4
LT
377 int __user *p = (int __user *)arg;
378
379 switch (cmd) {
c9845ff1 380 case FIBMAP:
aa81a7c7 381 return ioctl_fibmap(filp, p);
c4b929b8
MF
382 case FS_IOC_FIEMAP:
383 return ioctl_fiemap(filp, arg);
c9845ff1
EZ
384 case FIGETBSZ:
385 return put_user(inode->i_sb->s_blocksize, p);
386 case FIONREAD:
387 return put_user(i_size_read(inode) - filp->f_pos, p);
1da177e4
LT
388 }
389
deb21db7 390 return vfs_ioctl(filp, cmd, arg);
1da177e4
LT
391}
392
aa81a7c7
EZ
393static int ioctl_fionbio(struct file *filp, int __user *argp)
394{
395 unsigned int flag;
396 int on, error;
397
398 error = get_user(on, argp);
399 if (error)
400 return error;
401 flag = O_NONBLOCK;
402#ifdef __sparc__
403 /* SunOS compatibility item. */
404 if (O_NONBLOCK != O_NDELAY)
405 flag |= O_NDELAY;
406#endif
db1dd4d3 407 spin_lock(&filp->f_lock);
aa81a7c7
EZ
408 if (on)
409 filp->f_flags |= flag;
410 else
411 filp->f_flags &= ~flag;
db1dd4d3 412 spin_unlock(&filp->f_lock);
aa81a7c7
EZ
413 return error;
414}
415
416static int ioctl_fioasync(unsigned int fd, struct file *filp,
417 int __user *argp)
418{
419 unsigned int flag;
420 int on, error;
421
422 error = get_user(on, argp);
423 if (error)
424 return error;
425 flag = on ? FASYNC : 0;
426
427 /* Did FASYNC state change ? */
428 if ((flag ^ filp->f_flags) & FASYNC) {
218d11a8 429 if (filp->f_op && filp->f_op->fasync)
aa81a7c7 430 error = filp->f_op->fasync(fd, filp, on);
218d11a8 431 else
aa81a7c7
EZ
432 error = -ENOTTY;
433 }
434 if (error)
435 return error;
436
db1dd4d3 437 spin_lock(&filp->f_lock);
aa81a7c7
EZ
438 if (on)
439 filp->f_flags |= FASYNC;
440 else
441 filp->f_flags &= ~FASYNC;
db1dd4d3 442 spin_unlock(&filp->f_lock);
aa81a7c7
EZ
443 return error;
444}
445
fcccf502
TS
446static int ioctl_fsfreeze(struct file *filp)
447{
448 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
449
450 if (!capable(CAP_SYS_ADMIN))
451 return -EPERM;
452
453 /* If filesystem doesn't support freeze feature, return. */
454 if (sb->s_op->freeze_fs == NULL)
455 return -EOPNOTSUPP;
456
457 /* If a blockdevice-backed filesystem isn't specified, return. */
458 if (sb->s_bdev == NULL)
459 return -EINVAL;
460
461 /* Freeze */
462 sb = freeze_bdev(sb->s_bdev);
463 if (IS_ERR(sb))
464 return PTR_ERR(sb);
465 return 0;
466}
467
468static int ioctl_fsthaw(struct file *filp)
469{
470 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
471
472 if (!capable(CAP_SYS_ADMIN))
473 return -EPERM;
474
475 /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
476 if (sb->s_bdev == NULL)
477 return -EINVAL;
478
479 /* Thaw */
480 return thaw_bdev(sb->s_bdev, sb);
481}
482
1da177e4
LT
483/*
484 * When you add any new common ioctls to the switches above and below
485 * please update compat_sys_ioctl() too.
486 *
deb21db7 487 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
1da177e4
LT
488 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
489 */
deb21db7
EZ
490int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
491 unsigned long arg)
1da177e4 492{
aa81a7c7
EZ
493 int error = 0;
494 int __user *argp = (int __user *)arg;
1da177e4
LT
495
496 switch (cmd) {
c9845ff1
EZ
497 case FIOCLEX:
498 set_close_on_exec(fd, 1);
499 break;
1da177e4 500
c9845ff1
EZ
501 case FIONCLEX:
502 set_close_on_exec(fd, 0);
503 break;
1da177e4 504
c9845ff1 505 case FIONBIO:
aa81a7c7 506 error = ioctl_fionbio(filp, argp);
c9845ff1
EZ
507 break;
508
509 case FIOASYNC:
218d11a8
JC
510 /* BKL needed to avoid races tweaking f_flags */
511 lock_kernel();
aa81a7c7 512 error = ioctl_fioasync(fd, filp, argp);
218d11a8 513 unlock_kernel();
c9845ff1
EZ
514 break;
515
516 case FIOQSIZE:
517 if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
518 S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
519 S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
520 loff_t res =
521 inode_get_bytes(filp->f_path.dentry->d_inode);
522 error = copy_to_user((loff_t __user *)arg, &res,
523 sizeof(res)) ? -EFAULT : 0;
524 } else
525 error = -ENOTTY;
526 break;
fcccf502
TS
527
528 case FIFREEZE:
529 error = ioctl_fsfreeze(filp);
530 break;
531
532 case FITHAW:
533 error = ioctl_fsthaw(filp);
534 break;
535
c9845ff1
EZ
536 default:
537 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
538 error = file_ioctl(filp, cmd, arg);
539 else
deb21db7 540 error = vfs_ioctl(filp, cmd, arg);
c9845ff1 541 break;
1da177e4
LT
542 }
543 return error;
544}
545
a26eab24 546SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
1da177e4 547{
c9845ff1 548 struct file *filp;
1da177e4
LT
549 int error = -EBADF;
550 int fput_needed;
551
552 filp = fget_light(fd, &fput_needed);
553 if (!filp)
554 goto out;
555
556 error = security_file_ioctl(filp, cmd, arg);
557 if (error)
558 goto out_fput;
559
deb21db7 560 error = do_vfs_ioctl(filp, fd, cmd, arg);
1da177e4
LT
561 out_fput:
562 fput_light(filp, fput_needed);
563 out:
564 return error;
565}