xref: /openbmc/linux/fs/ioctl.c (revision 6189f1b0)
1 /*
2  *  linux/fs/ioctl.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6 
7 #include <linux/syscalls.h>
8 #include <linux/mm.h>
9 #include <linux/capability.h>
10 #include <linux/file.h>
11 #include <linux/fs.h>
12 #include <linux/security.h>
13 #include <linux/export.h>
14 #include <linux/uaccess.h>
15 #include <linux/writeback.h>
16 #include <linux/buffer_head.h>
17 #include <linux/falloc.h>
18 
19 #include <asm/ioctls.h>
20 
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 
24 /**
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
29  *
30  * Invokes filesystem specific ->unlocked_ioctl, if one exists; otherwise
31  * returns -ENOTTY.
32  *
33  * Returns 0 on success, -errno on error.
34  */
35 static long vfs_ioctl(struct file *filp, unsigned int cmd,
36 		      unsigned long arg)
37 {
38 	int error = -ENOTTY;
39 
40 	if (!filp->f_op->unlocked_ioctl)
41 		goto out;
42 
43 	error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
44 	if (error == -ENOIOCTLCMD)
45 		error = -ENOTTY;
46  out:
47 	return error;
48 }
49 
50 static int ioctl_fibmap(struct file *filp, int __user *p)
51 {
52 	struct address_space *mapping = filp->f_mapping;
53 	int res, block;
54 
55 	/* do we support this mess? */
56 	if (!mapping->a_ops->bmap)
57 		return -EINVAL;
58 	if (!capable(CAP_SYS_RAWIO))
59 		return -EPERM;
60 	res = get_user(block, p);
61 	if (res)
62 		return res;
63 	res = mapping->a_ops->bmap(mapping, block);
64 	return put_user(res, p);
65 }
66 
67 /**
68  * fiemap_fill_next_extent - Fiemap helper function
69  * @fieinfo:	Fiemap context passed into ->fiemap
70  * @logical:	Extent logical start offset, in bytes
71  * @phys:	Extent physical start offset, in bytes
72  * @len:	Extent length, in bytes
73  * @flags:	FIEMAP_EXTENT flags that describe this extent
74  *
75  * Called from file system ->fiemap callback. Will populate extent
76  * info as passed in via arguments and copy to user memory. On
77  * success, extent count on fieinfo is incremented.
78  *
79  * Returns 0 on success, -errno on error, 1 if this was the last
80  * extent that will fit in user array.
81  */
82 #define SET_UNKNOWN_FLAGS	(FIEMAP_EXTENT_DELALLOC)
83 #define SET_NO_UNMOUNTED_IO_FLAGS	(FIEMAP_EXTENT_DATA_ENCRYPTED)
84 #define SET_NOT_ALIGNED_FLAGS	(FIEMAP_EXTENT_DATA_TAIL|FIEMAP_EXTENT_DATA_INLINE)
85 int fiemap_fill_next_extent(struct fiemap_extent_info *fieinfo, u64 logical,
86 			    u64 phys, u64 len, u32 flags)
87 {
88 	struct fiemap_extent extent;
89 	struct fiemap_extent __user *dest = fieinfo->fi_extents_start;
90 
91 	/* only count the extents */
92 	if (fieinfo->fi_extents_max == 0) {
93 		fieinfo->fi_extents_mapped++;
94 		return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
95 	}
96 
97 	if (fieinfo->fi_extents_mapped >= fieinfo->fi_extents_max)
98 		return 1;
99 
100 	if (flags & SET_UNKNOWN_FLAGS)
101 		flags |= FIEMAP_EXTENT_UNKNOWN;
102 	if (flags & SET_NO_UNMOUNTED_IO_FLAGS)
103 		flags |= FIEMAP_EXTENT_ENCODED;
104 	if (flags & SET_NOT_ALIGNED_FLAGS)
105 		flags |= FIEMAP_EXTENT_NOT_ALIGNED;
106 
107 	memset(&extent, 0, sizeof(extent));
108 	extent.fe_logical = logical;
109 	extent.fe_physical = phys;
110 	extent.fe_length = len;
111 	extent.fe_flags = flags;
112 
113 	dest += fieinfo->fi_extents_mapped;
114 	if (copy_to_user(dest, &extent, sizeof(extent)))
115 		return -EFAULT;
116 
117 	fieinfo->fi_extents_mapped++;
118 	if (fieinfo->fi_extents_mapped == fieinfo->fi_extents_max)
119 		return 1;
120 	return (flags & FIEMAP_EXTENT_LAST) ? 1 : 0;
121 }
122 EXPORT_SYMBOL(fiemap_fill_next_extent);
123 
124 /**
125  * fiemap_check_flags - check validity of requested flags for fiemap
126  * @fieinfo:	Fiemap context passed into ->fiemap
127  * @fs_flags:	Set of fiemap flags that the file system understands
128  *
129  * Called from file system ->fiemap callback. This will compute the
130  * intersection of valid fiemap flags and those that the fs supports. That
131  * value is then compared against the user supplied flags. In case of bad user
132  * flags, the invalid values will be written into the fieinfo structure, and
133  * -EBADR is returned, which tells ioctl_fiemap() to return those values to
134  * userspace. For this reason, a return code of -EBADR should be preserved.
135  *
136  * Returns 0 on success, -EBADR on bad flags.
137  */
138 int fiemap_check_flags(struct fiemap_extent_info *fieinfo, u32 fs_flags)
139 {
140 	u32 incompat_flags;
141 
142 	incompat_flags = fieinfo->fi_flags & ~(FIEMAP_FLAGS_COMPAT & fs_flags);
143 	if (incompat_flags) {
144 		fieinfo->fi_flags = incompat_flags;
145 		return -EBADR;
146 	}
147 	return 0;
148 }
149 EXPORT_SYMBOL(fiemap_check_flags);
150 
151 static int fiemap_check_ranges(struct super_block *sb,
152 			       u64 start, u64 len, u64 *new_len)
153 {
154 	u64 maxbytes = (u64) sb->s_maxbytes;
155 
156 	*new_len = len;
157 
158 	if (len == 0)
159 		return -EINVAL;
160 
161 	if (start > maxbytes)
162 		return -EFBIG;
163 
164 	/*
165 	 * Shrink request scope to what the fs can actually handle.
166 	 */
167 	if (len > maxbytes || (maxbytes - len) < start)
168 		*new_len = maxbytes - start;
169 
170 	return 0;
171 }
172 
173 static int ioctl_fiemap(struct file *filp, unsigned long arg)
174 {
175 	struct fiemap fiemap;
176 	struct fiemap __user *ufiemap = (struct fiemap __user *) arg;
177 	struct fiemap_extent_info fieinfo = { 0, };
178 	struct inode *inode = file_inode(filp);
179 	struct super_block *sb = inode->i_sb;
180 	u64 len;
181 	int error;
182 
183 	if (!inode->i_op->fiemap)
184 		return -EOPNOTSUPP;
185 
186 	if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap)))
187 		return -EFAULT;
188 
189 	if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS)
190 		return -EINVAL;
191 
192 	error = fiemap_check_ranges(sb, fiemap.fm_start, fiemap.fm_length,
193 				    &len);
194 	if (error)
195 		return error;
196 
197 	fieinfo.fi_flags = fiemap.fm_flags;
198 	fieinfo.fi_extents_max = fiemap.fm_extent_count;
199 	fieinfo.fi_extents_start = ufiemap->fm_extents;
200 
201 	if (fiemap.fm_extent_count != 0 &&
202 	    !access_ok(VERIFY_WRITE, fieinfo.fi_extents_start,
203 		       fieinfo.fi_extents_max * sizeof(struct fiemap_extent)))
204 		return -EFAULT;
205 
206 	if (fieinfo.fi_flags & FIEMAP_FLAG_SYNC)
207 		filemap_write_and_wait(inode->i_mapping);
208 
209 	error = inode->i_op->fiemap(inode, &fieinfo, fiemap.fm_start, len);
210 	fiemap.fm_flags = fieinfo.fi_flags;
211 	fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped;
212 	if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap)))
213 		error = -EFAULT;
214 
215 	return error;
216 }
217 
218 #ifdef CONFIG_BLOCK
219 
220 static inline sector_t logical_to_blk(struct inode *inode, loff_t offset)
221 {
222 	return (offset >> inode->i_blkbits);
223 }
224 
225 static inline loff_t blk_to_logical(struct inode *inode, sector_t blk)
226 {
227 	return (blk << inode->i_blkbits);
228 }
229 
230 /**
231  * __generic_block_fiemap - FIEMAP for block based inodes (no locking)
232  * @inode: the inode to map
233  * @fieinfo: the fiemap info struct that will be passed back to userspace
234  * @start: where to start mapping in the inode
235  * @len: how much space to map
236  * @get_block: the fs's get_block function
237  *
238  * This does FIEMAP for block based inodes.  Basically it will just loop
239  * through get_block until we hit the number of extents we want to map, or we
240  * go past the end of the file and hit a hole.
241  *
242  * If it is possible to have data blocks beyond a hole past @inode->i_size, then
243  * please do not use this function, it will stop at the first unmapped block
244  * beyond i_size.
245  *
246  * If you use this function directly, you need to do your own locking. Use
247  * generic_block_fiemap if you want the locking done for you.
248  */
249 
250 int __generic_block_fiemap(struct inode *inode,
251 			   struct fiemap_extent_info *fieinfo, loff_t start,
252 			   loff_t len, get_block_t *get_block)
253 {
254 	struct buffer_head map_bh;
255 	sector_t start_blk, last_blk;
256 	loff_t isize = i_size_read(inode);
257 	u64 logical = 0, phys = 0, size = 0;
258 	u32 flags = FIEMAP_EXTENT_MERGED;
259 	bool past_eof = false, whole_file = false;
260 	int ret = 0;
261 
262 	ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
263 	if (ret)
264 		return ret;
265 
266 	/*
267 	 * Either the i_mutex or other appropriate locking needs to be held
268 	 * since we expect isize to not change at all through the duration of
269 	 * this call.
270 	 */
271 	if (len >= isize) {
272 		whole_file = true;
273 		len = isize;
274 	}
275 
276 	/*
277 	 * Some filesystems can't deal with being asked to map less than
278 	 * blocksize, so make sure our len is at least block length.
279 	 */
280 	if (logical_to_blk(inode, len) == 0)
281 		len = blk_to_logical(inode, 1);
282 
283 	start_blk = logical_to_blk(inode, start);
284 	last_blk = logical_to_blk(inode, start + len - 1);
285 
286 	do {
287 		/*
288 		 * we set b_size to the total size we want so it will map as
289 		 * many contiguous blocks as possible at once
290 		 */
291 		memset(&map_bh, 0, sizeof(struct buffer_head));
292 		map_bh.b_size = len;
293 
294 		ret = get_block(inode, start_blk, &map_bh, 0);
295 		if (ret)
296 			break;
297 
298 		/* HOLE */
299 		if (!buffer_mapped(&map_bh)) {
300 			start_blk++;
301 
302 			/*
303 			 * We want to handle the case where there is an
304 			 * allocated block at the front of the file, and then
305 			 * nothing but holes up to the end of the file properly,
306 			 * to make sure that extent at the front gets properly
307 			 * marked with FIEMAP_EXTENT_LAST
308 			 */
309 			if (!past_eof &&
310 			    blk_to_logical(inode, start_blk) >= isize)
311 				past_eof = 1;
312 
313 			/*
314 			 * First hole after going past the EOF, this is our
315 			 * last extent
316 			 */
317 			if (past_eof && size) {
318 				flags = FIEMAP_EXTENT_MERGED|FIEMAP_EXTENT_LAST;
319 				ret = fiemap_fill_next_extent(fieinfo, logical,
320 							      phys, size,
321 							      flags);
322 			} else if (size) {
323 				ret = fiemap_fill_next_extent(fieinfo, logical,
324 							      phys, size, flags);
325 				size = 0;
326 			}
327 
328 			/* if we have holes up to/past EOF then we're done */
329 			if (start_blk > last_blk || past_eof || ret)
330 				break;
331 		} else {
332 			/*
333 			 * We have gone over the length of what we wanted to
334 			 * map, and it wasn't the entire file, so add the extent
335 			 * we got last time and exit.
336 			 *
337 			 * This is for the case where say we want to map all the
338 			 * way up to the second to the last block in a file, but
339 			 * the last block is a hole, making the second to last
340 			 * block FIEMAP_EXTENT_LAST.  In this case we want to
341 			 * see if there is a hole after the second to last block
342 			 * so we can mark it properly.  If we found data after
343 			 * we exceeded the length we were requesting, then we
344 			 * are good to go, just add the extent to the fieinfo
345 			 * and break
346 			 */
347 			if (start_blk > last_blk && !whole_file) {
348 				ret = fiemap_fill_next_extent(fieinfo, logical,
349 							      phys, size,
350 							      flags);
351 				break;
352 			}
353 
354 			/*
355 			 * if size != 0 then we know we already have an extent
356 			 * to add, so add it.
357 			 */
358 			if (size) {
359 				ret = fiemap_fill_next_extent(fieinfo, logical,
360 							      phys, size,
361 							      flags);
362 				if (ret)
363 					break;
364 			}
365 
366 			logical = blk_to_logical(inode, start_blk);
367 			phys = blk_to_logical(inode, map_bh.b_blocknr);
368 			size = map_bh.b_size;
369 			flags = FIEMAP_EXTENT_MERGED;
370 
371 			start_blk += logical_to_blk(inode, size);
372 
373 			/*
374 			 * If we are past the EOF, then we need to make sure as
375 			 * soon as we find a hole that the last extent we found
376 			 * is marked with FIEMAP_EXTENT_LAST
377 			 */
378 			if (!past_eof && logical + size >= isize)
379 				past_eof = true;
380 		}
381 		cond_resched();
382 		if (fatal_signal_pending(current)) {
383 			ret = -EINTR;
384 			break;
385 		}
386 
387 	} while (1);
388 
389 	/* If ret is 1 then we just hit the end of the extent array */
390 	if (ret == 1)
391 		ret = 0;
392 
393 	return ret;
394 }
395 EXPORT_SYMBOL(__generic_block_fiemap);
396 
397 /**
398  * generic_block_fiemap - FIEMAP for block based inodes
399  * @inode: The inode to map
400  * @fieinfo: The mapping information
401  * @start: The initial block to map
402  * @len: The length of the extect to attempt to map
403  * @get_block: The block mapping function for the fs
404  *
405  * Calls __generic_block_fiemap to map the inode, after taking
406  * the inode's mutex lock.
407  */
408 
409 int generic_block_fiemap(struct inode *inode,
410 			 struct fiemap_extent_info *fieinfo, u64 start,
411 			 u64 len, get_block_t *get_block)
412 {
413 	int ret;
414 	mutex_lock(&inode->i_mutex);
415 	ret = __generic_block_fiemap(inode, fieinfo, start, len, get_block);
416 	mutex_unlock(&inode->i_mutex);
417 	return ret;
418 }
419 EXPORT_SYMBOL(generic_block_fiemap);
420 
421 #endif  /*  CONFIG_BLOCK  */
422 
423 /*
424  * This provides compatibility with legacy XFS pre-allocation ioctls
425  * which predate the fallocate syscall.
426  *
427  * Only the l_start, l_len and l_whence fields of the 'struct space_resv'
428  * are used here, rest are ignored.
429  */
430 int ioctl_preallocate(struct file *filp, void __user *argp)
431 {
432 	struct inode *inode = file_inode(filp);
433 	struct space_resv sr;
434 
435 	if (copy_from_user(&sr, argp, sizeof(sr)))
436 		return -EFAULT;
437 
438 	switch (sr.l_whence) {
439 	case SEEK_SET:
440 		break;
441 	case SEEK_CUR:
442 		sr.l_start += filp->f_pos;
443 		break;
444 	case SEEK_END:
445 		sr.l_start += i_size_read(inode);
446 		break;
447 	default:
448 		return -EINVAL;
449 	}
450 
451 	return vfs_fallocate(filp, FALLOC_FL_KEEP_SIZE, sr.l_start, sr.l_len);
452 }
453 
454 static int file_ioctl(struct file *filp, unsigned int cmd,
455 		unsigned long arg)
456 {
457 	struct inode *inode = file_inode(filp);
458 	int __user *p = (int __user *)arg;
459 
460 	switch (cmd) {
461 	case FIBMAP:
462 		return ioctl_fibmap(filp, p);
463 	case FIONREAD:
464 		return put_user(i_size_read(inode) - filp->f_pos, p);
465 	case FS_IOC_RESVSP:
466 	case FS_IOC_RESVSP64:
467 		return ioctl_preallocate(filp, p);
468 	}
469 
470 	return vfs_ioctl(filp, cmd, arg);
471 }
472 
473 static int ioctl_fionbio(struct file *filp, int __user *argp)
474 {
475 	unsigned int flag;
476 	int on, error;
477 
478 	error = get_user(on, argp);
479 	if (error)
480 		return error;
481 	flag = O_NONBLOCK;
482 #ifdef __sparc__
483 	/* SunOS compatibility item. */
484 	if (O_NONBLOCK != O_NDELAY)
485 		flag |= O_NDELAY;
486 #endif
487 	spin_lock(&filp->f_lock);
488 	if (on)
489 		filp->f_flags |= flag;
490 	else
491 		filp->f_flags &= ~flag;
492 	spin_unlock(&filp->f_lock);
493 	return error;
494 }
495 
496 static int ioctl_fioasync(unsigned int fd, struct file *filp,
497 			  int __user *argp)
498 {
499 	unsigned int flag;
500 	int on, error;
501 
502 	error = get_user(on, argp);
503 	if (error)
504 		return error;
505 	flag = on ? FASYNC : 0;
506 
507 	/* Did FASYNC state change ? */
508 	if ((flag ^ filp->f_flags) & FASYNC) {
509 		if (filp->f_op->fasync)
510 			/* fasync() adjusts filp->f_flags */
511 			error = filp->f_op->fasync(fd, filp, on);
512 		else
513 			error = -ENOTTY;
514 	}
515 	return error < 0 ? error : 0;
516 }
517 
518 static int ioctl_fsfreeze(struct file *filp)
519 {
520 	struct super_block *sb = file_inode(filp)->i_sb;
521 
522 	if (!capable(CAP_SYS_ADMIN))
523 		return -EPERM;
524 
525 	/* If filesystem doesn't support freeze feature, return. */
526 	if (sb->s_op->freeze_fs == NULL && sb->s_op->freeze_super == NULL)
527 		return -EOPNOTSUPP;
528 
529 	/* Freeze */
530 	if (sb->s_op->freeze_super)
531 		return sb->s_op->freeze_super(sb);
532 	return freeze_super(sb);
533 }
534 
535 static int ioctl_fsthaw(struct file *filp)
536 {
537 	struct super_block *sb = file_inode(filp)->i_sb;
538 
539 	if (!capable(CAP_SYS_ADMIN))
540 		return -EPERM;
541 
542 	/* Thaw */
543 	if (sb->s_op->thaw_super)
544 		return sb->s_op->thaw_super(sb);
545 	return thaw_super(sb);
546 }
547 
548 /*
549  * When you add any new common ioctls to the switches above and below
550  * please update compat_sys_ioctl() too.
551  *
552  * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
553  * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
554  */
555 int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd,
556 	     unsigned long arg)
557 {
558 	int error = 0;
559 	int __user *argp = (int __user *)arg;
560 	struct inode *inode = file_inode(filp);
561 
562 	switch (cmd) {
563 	case FIOCLEX:
564 		set_close_on_exec(fd, 1);
565 		break;
566 
567 	case FIONCLEX:
568 		set_close_on_exec(fd, 0);
569 		break;
570 
571 	case FIONBIO:
572 		error = ioctl_fionbio(filp, argp);
573 		break;
574 
575 	case FIOASYNC:
576 		error = ioctl_fioasync(fd, filp, argp);
577 		break;
578 
579 	case FIOQSIZE:
580 		if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode) ||
581 		    S_ISLNK(inode->i_mode)) {
582 			loff_t res = inode_get_bytes(inode);
583 			error = copy_to_user(argp, &res, sizeof(res)) ?
584 					-EFAULT : 0;
585 		} else
586 			error = -ENOTTY;
587 		break;
588 
589 	case FIFREEZE:
590 		error = ioctl_fsfreeze(filp);
591 		break;
592 
593 	case FITHAW:
594 		error = ioctl_fsthaw(filp);
595 		break;
596 
597 	case FS_IOC_FIEMAP:
598 		return ioctl_fiemap(filp, arg);
599 
600 	case FIGETBSZ:
601 		return put_user(inode->i_sb->s_blocksize, argp);
602 
603 	default:
604 		if (S_ISREG(inode->i_mode))
605 			error = file_ioctl(filp, cmd, arg);
606 		else
607 			error = vfs_ioctl(filp, cmd, arg);
608 		break;
609 	}
610 	return error;
611 }
612 
613 SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
614 {
615 	int error;
616 	struct fd f = fdget(fd);
617 
618 	if (!f.file)
619 		return -EBADF;
620 	error = security_file_ioctl(f.file, cmd, arg);
621 	if (!error)
622 		error = do_vfs_ioctl(f.file, fd, cmd, arg);
623 	fdput(f);
624 	return error;
625 }
626