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>
19 #include <asm/ioctls.h>
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))
25 * vfs_ioctl - call filesystem specific ioctl methods
26 * @filp: open file to invoke ioctl method on
27 * @cmd: ioctl command to execute
28 * @arg: command-specific argument for ioctl
30 * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
31 * invokes filesystem specific ->ioctl method. If neither method exists,
34 * Returns 0 on success, -errno on error.
36 static long vfs_ioctl(struct file *filp, unsigned int cmd,
44 if (filp->f_op->unlocked_ioctl) {
45 error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
46 if (error == -ENOIOCTLCMD)
49 } else if (filp->f_op->ioctl) {
51 error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
60 static int ioctl_fibmap(struct file *filp, int __user *p)
62 struct address_space *mapping = filp->f_mapping;
65 /* do we support this mess? */
66 if (!mapping->a_ops->bmap)
68 if (!capable(CAP_SYS_RAWIO))
70 res = get_user(block, p);
74 res = mapping->a_ops->bmap(mapping, block);
76 return put_user(res, p);
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
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.
91 * Returns 0 on success, -errno on error, 1 if this was the last
92 * extent that will fit in user array.
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)
97 int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
98 u64 phys, u64 len, u32 flags)
100 struct fiemap_extent extent;
101 struct fiemap_extent *dest = fieinfo->fi_extents_start;
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;
109 if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
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;
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;
125 dest += fieinfo->fi_extents_mapped;
126 if (copy_to_user(dest, &extent, sizeof(extent)))
129 fieinfo->fi_extents_mapped++;
130 if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
132 return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
134 EXPORT_SYMBOL(fiemap_fill_next_extent);
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
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.
148 * Returns 0 on success, -EBADR on bad flags.
150 int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
154 incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
155 if (incompat_flags) {
156 fieinfo->fi_flags = incompat_flags;
161 EXPORT_SYMBOL(fiemap_check_flags);
163 static int fiemap_check_ranges(struct super_block *sb,
164 u64 start, u64 len, u64 *new_len)
171 if (start > sb->s_maxbytes)
175 * Shrink request scope to what the fs can actually handle.
177 if ((len > sb->s_maxbytes) ||
178 (sb->s_maxbytes - len) < start)
179 *new_len = sb->s_maxbytes - start;
184 static int ioctl_fiemap(struct file *filp, unsigned long arg)
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;
193 if (!inode->i_op->fiemap)
196 if (copy_from_user(&fiemap, (struct fiemap __user *)arg,
197 sizeof(struct fiemap)))
200 if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
203 error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
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));
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)))
217 if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
218 filemap_write_and_wait(inode->i_mapping);
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)))
231 #define blk_to_logical(inode, blk) (blk << (inode)->i_blkbits)
232 #define logical_to_blk(inode, offset) (offset >> (inode)->i_blkbits);
235 * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
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
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.
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
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.
252 int __generic_block_fiemap(struct inode *inode,
253 struct fiemap_extent_info *fieinfo, u64 start,
254 u64 len, get_block_t *get_block)
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;
263 if ((ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC)))
266 start_blk = logical_to_blk(inode, start);
268 length = (long long)min_t(u64, len, i_size_read(inode));
273 * we set b_size to the total size we want so it will map as
274 * many contiguous blocks as possible at once
276 memset(&tmp, 0, sizeof(struct buffer_head));
277 tmp.b_size = map_len;
279 ret = get_block(inode, start_blk, &tmp, 0);
284 if (!buffer_mapped(&tmp)) {
286 * first hole after going past the EOF, this is our
290 flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
291 ret = fiemap_fill_next_extent(fieinfo, logical,
297 length -= blk_to_logical(inode, 1);
299 /* if we have holes up to/past EOF then we're done */
305 if (length <= 0 && size) {
306 ret = fiemap_fill_next_extent(fieinfo, logical,
313 logical = blk_to_logical(inode, start_blk);
314 phys = blk_to_logical(inode, tmp.b_blocknr);
316 flags = FIEMAP_EXTENT_MERGED;
318 length -= tmp.b_size;
319 start_blk += logical_to_blk(inode, size);
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
331 ret = fiemap_fill_next_extent(fieinfo, logical, phys,
339 /* if ret is 1 then we just hit the end of the extent array */
345 EXPORT_SYMBOL(__generic_block_fiemap);
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
355 * Calls __generic_block_fiemap to map the inode, after taking
356 * the inode's mutex lock.
359 int generic_block_fiemap(struct inode *inode,
360 struct fiemap_extent_info *fieinfo, u64 start,
361 u64 len, get_block_t *get_block)
364 mutex_lock(&inode->i_mutex);
365 ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
366 mutex_unlock(&inode->i_mutex);
369 EXPORT_SYMBOL(generic_block_fiemap);
371 #endif /* CONFIG_BLOCK */
373 static int file_ioctl(struct file *filp, unsigned int cmd,
376 struct inode *inode = filp->f_path.dentry->d_inode;
377 int __user *p = (int __user *)arg;
381 return ioctl_fibmap(filp, p);
383 return ioctl_fiemap(filp, arg);
385 return put_user(inode->i_sb->s_blocksize, p);
387 return put_user(i_size_read(inode) - filp->f_pos, p);
390 return vfs_ioctl(filp, cmd, arg);
393 static int ioctl_fionbio(struct file *filp, int __user *argp)
398 error = get_user(on, argp);
403 /* SunOS compatibility item. */
404 if (O_NONBLOCK != O_NDELAY)
408 filp->f_flags |= flag;
410 filp->f_flags &= ~flag;
414 static int ioctl_fioasync(unsigned int fd, struct file *filp,
420 error = get_user(on, argp);
423 flag = on ? FASYNC : 0;
425 /* Did FASYNC state change ? */
426 if ((flag ^ filp->f_flags) & FASYNC) {
427 if (filp->f_op && filp->f_op->fasync)
428 error = filp->f_op->fasync(fd, filp, on);
436 filp->f_flags |= FASYNC;
438 filp->f_flags &= ~FASYNC;
442 static int ioctl_fsfreeze(struct file *filp)
444 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
446 if (!capable(CAP_SYS_ADMIN))
449 /* If filesystem doesn't support freeze feature, return. */
450 if (sb->s_op->freeze_fs == NULL)
453 /* If a blockdevice-backed filesystem isn't specified, return. */
454 if (sb->s_bdev == NULL)
458 sb = freeze_bdev(sb->s_bdev);
464 static int ioctl_fsthaw(struct file *filp)
466 struct super_block *sb = filp->f_path.dentry->d_inode->i_sb;
468 if (!capable(CAP_SYS_ADMIN))
471 /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */
472 if (sb->s_bdev == NULL)
476 return thaw_bdev(sb->s_bdev, sb);
480 * When you add any new common ioctls to the switches above and below
481 * please update compat_sys_ioctl() too.
483 * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
484 * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
486 int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
490 int __user *argp = (int __user *)arg;
494 set_close_on_exec(fd, 1);
498 set_close_on_exec(fd, 0);
502 /* BKL needed to avoid races tweaking f_flags */
504 error = ioctl_fionbio(filp, argp);
509 /* BKL needed to avoid races tweaking f_flags */
511 error = ioctl_fioasync(fd, filp, argp);
516 if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
517 S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
518 S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
520 inode_get_bytes(filp->f_path.dentry->d_inode);
521 error = copy_to_user((loff_t __user *)arg, &res,
522 sizeof(res)) ? -EFAULT : 0;
528 error = ioctl_fsfreeze(filp);
532 error = ioctl_fsthaw(filp);
536 if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
537 error = file_ioctl(filp, cmd, arg);
539 error = vfs_ioctl(filp, cmd, arg);
545 SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
551 filp = fget_light(fd, &fput_needed);
555 error = security_file_ioctl(filp, cmd, arg);
559 error = do_vfs_ioctl(filp, fd, cmd, arg);
561 fput_light(filp, fput_needed);