4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/syscalls.h>
9 #include <linux/smp_lock.h>
10 #include <linux/capability.h>
11 #include <linux/file.h>
13 #include <linux/security.h>
14 #include <linux/module.h>
15 #include <linux/uaccess.h>
16 #include <linux/writeback.h>
17 #include <linux/buffer_head.h>
18 #include <linux/falloc.h>
20 #include <asm/ioctls.h>
22 /* So that the fiemap access checks can't overflow on 32 bit machines. */
23 #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent))
26 * vfs_ioctl - call filesystem specific ioctl methods
27 * @filp: open file to invoke ioctl method on
28 * @cmd: ioctl command to execute
29 * @arg: command-specific argument for ioctl
31 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
32 * invokes filesystem specific ->ioctl method. If neither method exists,
35 * Returns 0 on success, -errno on error.
37 static long vfs_ioctl(struct file
*filp
, unsigned int cmd
,
45 if (filp
->f_op
->unlocked_ioctl
) {
46 error
= filp
->f_op
->unlocked_ioctl(filp
, cmd
, arg
);
47 if (error
== -ENOIOCTLCMD
)
50 } else if (filp
->f_op
->ioctl
) {
52 error
= filp
->f_op
->ioctl(filp
->f_path
.dentry
->d_inode
,
61 static int ioctl_fibmap(struct file
*filp
, int __user
*p
)
63 struct address_space
*mapping
= filp
->f_mapping
;
66 /* do we support this mess? */
67 if (!mapping
->a_ops
->bmap
)
69 if (!capable(CAP_SYS_RAWIO
))
71 res
= get_user(block
, p
);
74 res
= mapping
->a_ops
->bmap(mapping
, block
);
75 return put_user(res
, p
);
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
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.
90 * Returns 0 on success, -errno on error, 1 if this was the last
91 * extent that will fit in user array.
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)
96 int fiemap_fill_next_extent(struct fiemap_extent_info
*fieinfo
, u64 logical
,
97 u64 phys
, u64 len
, u32 flags
)
99 struct fiemap_extent extent
;
100 struct fiemap_extent
*dest
= fieinfo
->fi_extents_start
;
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;
108 if (fieinfo
->fi_extents_mapped
>= fieinfo
->fi_extents_max
)
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
;
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
;
124 dest
+= fieinfo
->fi_extents_mapped
;
125 if (copy_to_user(dest
, &extent
, sizeof(extent
)))
128 fieinfo
->fi_extents_mapped
++;
129 if (fieinfo
->fi_extents_mapped
== fieinfo
->fi_extents_max
)
131 return (flags
& FIEMAP_EXTENT_LAST
) ? 1 : 0;
133 EXPORT_SYMBOL(fiemap_fill_next_extent
);
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
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.
147 * Returns 0 on success, -EBADR on bad flags.
149 int fiemap_check_flags(struct fiemap_extent_info
*fieinfo
, u32 fs_flags
)
153 incompat_flags
= fieinfo
->fi_flags
& ~(FIEMAP_FLAGS_COMPAT
& fs_flags
);
154 if (incompat_flags
) {
155 fieinfo
->fi_flags
= incompat_flags
;
160 EXPORT_SYMBOL(fiemap_check_flags
);
162 static int fiemap_check_ranges(struct super_block
*sb
,
163 u64 start
, u64 len
, u64
*new_len
)
170 if (start
> sb
->s_maxbytes
)
174 * Shrink request scope to what the fs can actually handle.
176 if ((len
> sb
->s_maxbytes
) ||
177 (sb
->s_maxbytes
- len
) < start
)
178 *new_len
= sb
->s_maxbytes
- start
;
183 static int ioctl_fiemap(struct file
*filp
, unsigned long arg
)
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
;
192 if (!inode
->i_op
->fiemap
)
195 if (copy_from_user(&fiemap
, (struct fiemap __user
*)arg
,
196 sizeof(struct fiemap
)))
199 if (fiemap
.fm_extent_count
> FIEMAP_MAX_EXTENTS
)
202 error
= fiemap_check_ranges(sb
, fiemap
.fm_start
, fiemap
.fm_length
,
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
));
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
)))
216 if (fieinfo
.fi_flags
& FIEMAP_FLAG_SYNC
)
217 filemap_write_and_wait(inode
->i_mapping
);
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
)))
230 #define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits)
231 #define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits);
234 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
235 * @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
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.
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
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.
251 int __generic_block_fiemap(struct inode
*inode
,
252 struct fiemap_extent_info
*fieinfo
, u64 start
,
253 u64 len
, get_block_t
*get_block
)
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
;
260 int ret
= 0, past_eof
= 0, whole_file
= 0;
262 if ((ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
)))
265 start_blk
= logical_to_blk(inode
, start
);
267 length
= (long long)min_t(u64
, len
, i_size_read(inode
));
275 * we set b_size to the total size we want so it will map as
276 * many contiguous blocks as possible at once
278 memset(&tmp
, 0, sizeof(struct buffer_head
));
279 tmp
.b_size
= map_len
;
281 ret
= get_block(inode
, start_blk
, &tmp
, 0);
286 if (!buffer_mapped(&tmp
)) {
287 length
-= blk_to_logical(inode
, 1);
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
298 blk_to_logical(inode
, start_blk
) >=
299 blk_to_logical(inode
, 0)+i_size_read(inode
))
303 * first hole after going past the EOF, this is our
306 if (past_eof
&& size
) {
307 flags
= FIEMAP_EXTENT_MERGED
|FIEMAP_EXTENT_LAST
;
308 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
314 /* if we have holes up to/past EOF then we're done */
315 if (length
<= 0 || past_eof
)
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.
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
333 if (length
<= 0 && !whole_file
) {
334 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
341 * if size != 0 then we know we already have an extent
345 ret
= fiemap_fill_next_extent(fieinfo
, logical
,
352 logical
= blk_to_logical(inode
, start_blk
);
353 phys
= blk_to_logical(inode
, tmp
.b_blocknr
);
355 flags
= FIEMAP_EXTENT_MERGED
;
357 length
-= tmp
.b_size
;
358 start_blk
+= logical_to_blk(inode
, size
);
361 * 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
367 blk_to_logical(inode
, 0)+i_size_read(inode
))
373 /* if ret is 1 then we just hit the end of the extent array */
379 EXPORT_SYMBOL(__generic_block_fiemap
);
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
389 * Calls __generic_block_fiemap to map the inode, after taking
390 * the inode's mutex lock.
393 int generic_block_fiemap(struct inode
*inode
,
394 struct fiemap_extent_info
*fieinfo
, u64 start
,
395 u64 len
, get_block_t
*get_block
)
398 mutex_lock(&inode
->i_mutex
);
399 ret
= __generic_block_fiemap(inode
, fieinfo
, start
, len
, get_block
);
400 mutex_unlock(&inode
->i_mutex
);
403 EXPORT_SYMBOL(generic_block_fiemap
);
405 #endif /* CONFIG_BLOCK */
408 * This provides compatibility with legacy XFS pre-allocation ioctls
409 * which predate the fallocate syscall.
411 * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
412 * are used here, rest are ignored.
414 int ioctl_preallocate(struct file
*filp
, void __user
*argp
)
416 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
417 struct space_resv sr
;
419 if (copy_from_user(&sr
, argp
, sizeof(sr
)))
422 switch (sr
.l_whence
) {
426 sr
.l_start
+= filp
->f_pos
;
429 sr
.l_start
+= i_size_read(inode
);
435 return do_fallocate(filp
, FALLOC_FL_KEEP_SIZE
, sr
.l_start
, sr
.l_len
);
438 static int file_ioctl(struct file
*filp
, unsigned int cmd
,
441 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
442 int __user
*p
= (int __user
*)arg
;
446 return ioctl_fibmap(filp
, p
);
448 return put_user(i_size_read(inode
) - filp
->f_pos
, p
);
450 case FS_IOC_RESVSP64
:
451 return ioctl_preallocate(filp
, p
);
454 return vfs_ioctl(filp
, cmd
, arg
);
457 static int ioctl_fionbio(struct file
*filp
, int __user
*argp
)
462 error
= get_user(on
, argp
);
467 /* SunOS compatibility item. */
468 if (O_NONBLOCK
!= O_NDELAY
)
471 spin_lock(&filp
->f_lock
);
473 filp
->f_flags
|= flag
;
475 filp
->f_flags
&= ~flag
;
476 spin_unlock(&filp
->f_lock
);
480 static int ioctl_fioasync(unsigned int fd
, struct file
*filp
,
486 error
= get_user(on
, argp
);
489 flag
= on
? FASYNC
: 0;
491 /* Did FASYNC state change ? */
492 if ((flag
^ filp
->f_flags
) & FASYNC
) {
493 if (filp
->f_op
&& filp
->f_op
->fasync
)
494 /* fasync() adjusts filp->f_flags */
495 error
= filp
->f_op
->fasync(fd
, filp
, on
);
499 return error
< 0 ? error
: 0;
502 static int ioctl_fsfreeze(struct file
*filp
)
504 struct super_block
*sb
= filp
->f_path
.dentry
->d_inode
->i_sb
;
506 if (!capable(CAP_SYS_ADMIN
))
509 /* If filesystem doesn't support freeze feature, return. */
510 if (sb
->s_op
->freeze_fs
== NULL
)
513 /* If a blockdevice-backed filesystem isn't specified, return. */
514 if (sb
->s_bdev
== NULL
)
518 sb
= freeze_bdev(sb
->s_bdev
);
524 static int ioctl_fsthaw(struct file
*filp
)
526 struct super_block
*sb
= filp
->f_path
.dentry
->d_inode
->i_sb
;
528 if (!capable(CAP_SYS_ADMIN
))
531 /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
532 if (sb
->s_bdev
== NULL
)
536 return thaw_bdev(sb
->s_bdev
, sb
);
540 * When you add any new common ioctls to the switches above and below
541 * please update compat_sys_ioctl() too.
543 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
544 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
546 int do_vfs_ioctl(struct file
*filp
, unsigned int fd
, unsigned int cmd
,
550 int __user
*argp
= (int __user
*)arg
;
554 set_close_on_exec(fd
, 1);
558 set_close_on_exec(fd
, 0);
562 error
= ioctl_fionbio(filp
, argp
);
566 error
= ioctl_fioasync(fd
, filp
, argp
);
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
)) {
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;
582 error
= ioctl_fsfreeze(filp
);
586 error
= ioctl_fsthaw(filp
);
590 return ioctl_fiemap(filp
, arg
);
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
);
600 if (S_ISREG(filp
->f_path
.dentry
->d_inode
->i_mode
))
601 error
= file_ioctl(filp
, cmd
, arg
);
603 error
= vfs_ioctl(filp
, cmd
, arg
);
609 SYSCALL_DEFINE3(ioctl
, unsigned int, fd
, unsigned int, cmd
, unsigned long, arg
)
615 filp
= fget_light(fd
, &fput_needed
);
619 error
= security_file_ioctl(filp
, cmd
, arg
);
623 error
= do_vfs_ioctl(filp
, fd
, cmd
, arg
);
625 fput_light(filp
, fput_needed
);