lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext4/ioctl.c |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | */ |
| 9 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/jbd2.h> |
| 12 | #include <linux/capability.h> |
| 13 | #include <linux/time.h> |
| 14 | #include <linux/compat.h> |
| 15 | #include <linux/mount.h> |
| 16 | #include <linux/file.h> |
| 17 | #include <asm/uaccess.h> |
| 18 | #include "ext4_jbd2.h" |
| 19 | #include "ext4.h" |
| 20 | |
| 21 | #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1) |
| 22 | |
| 23 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 24 | { |
| 25 | struct inode *inode = filp->f_dentry->d_inode; |
| 26 | struct super_block *sb = inode->i_sb; |
| 27 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 28 | unsigned int flags; |
| 29 | |
| 30 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
| 31 | |
| 32 | switch (cmd) { |
| 33 | case EXT4_IOC_GETFLAGS: |
| 34 | ext4_get_inode_flags(ei); |
| 35 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
| 36 | return put_user(flags, (int __user *) arg); |
| 37 | case EXT4_IOC_SETFLAGS: { |
| 38 | handle_t *handle = NULL; |
| 39 | int err, migrate = 0; |
| 40 | struct ext4_iloc iloc; |
| 41 | unsigned int oldflags, mask, i; |
| 42 | unsigned int jflag; |
| 43 | |
| 44 | if (!inode_owner_or_capable(inode)) |
| 45 | return -EACCES; |
| 46 | |
| 47 | if (get_user(flags, (int __user *) arg)) |
| 48 | return -EFAULT; |
| 49 | |
| 50 | err = mnt_want_write_file(filp); |
| 51 | if (err) |
| 52 | return err; |
| 53 | |
| 54 | flags = ext4_mask_flags(inode->i_mode, flags); |
| 55 | |
| 56 | err = -EPERM; |
| 57 | mutex_lock(&inode->i_mutex); |
| 58 | /* Is it quota file? Do not allow user to mess with it */ |
| 59 | if (IS_NOQUOTA(inode)) |
| 60 | goto flags_out; |
| 61 | |
| 62 | oldflags = ei->i_flags; |
| 63 | |
| 64 | /* The JOURNAL_DATA flag is modifiable only by root */ |
| 65 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
| 66 | |
| 67 | /* |
| 68 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by |
| 69 | * the relevant capability. |
| 70 | * |
| 71 | * This test looks nicer. Thanks to Pauline Middelink |
| 72 | */ |
| 73 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
| 74 | if (!capable(CAP_LINUX_IMMUTABLE)) |
| 75 | goto flags_out; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * The JOURNAL_DATA flag can only be changed by |
| 80 | * the relevant capability. |
| 81 | */ |
| 82 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
| 83 | if (!capable(CAP_SYS_RESOURCE)) |
| 84 | goto flags_out; |
| 85 | } |
| 86 | if (oldflags & EXT4_EXTENTS_FL) { |
| 87 | /* We don't support clearning extent flags */ |
| 88 | if (!(flags & EXT4_EXTENTS_FL)) { |
| 89 | err = -EOPNOTSUPP; |
| 90 | goto flags_out; |
| 91 | } |
| 92 | } else if (flags & EXT4_EXTENTS_FL) { |
| 93 | /* migrate the file */ |
| 94 | migrate = 1; |
| 95 | flags &= ~EXT4_EXTENTS_FL; |
| 96 | } |
| 97 | |
| 98 | if (flags & EXT4_EOFBLOCKS_FL) { |
| 99 | /* we don't support adding EOFBLOCKS flag */ |
| 100 | if (!(oldflags & EXT4_EOFBLOCKS_FL)) { |
| 101 | err = -EOPNOTSUPP; |
| 102 | goto flags_out; |
| 103 | } |
| 104 | } else if (oldflags & EXT4_EOFBLOCKS_FL) |
| 105 | ext4_truncate(inode); |
| 106 | |
| 107 | handle = ext4_journal_start(inode, 1); |
| 108 | if (IS_ERR(handle)) { |
| 109 | err = PTR_ERR(handle); |
| 110 | goto flags_out; |
| 111 | } |
| 112 | if (IS_SYNC(inode)) |
| 113 | ext4_handle_sync(handle); |
| 114 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
| 115 | if (err) |
| 116 | goto flags_err; |
| 117 | |
| 118 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { |
| 119 | if (!(mask & EXT4_FL_USER_MODIFIABLE)) |
| 120 | continue; |
| 121 | if (mask & flags) |
| 122 | ext4_set_inode_flag(inode, i); |
| 123 | else |
| 124 | ext4_clear_inode_flag(inode, i); |
| 125 | } |
| 126 | |
| 127 | ext4_set_inode_flags(inode); |
| 128 | inode->i_ctime = ext4_current_time(inode); |
| 129 | |
| 130 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
| 131 | flags_err: |
| 132 | ext4_journal_stop(handle); |
| 133 | if (err) |
| 134 | goto flags_out; |
| 135 | |
| 136 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) |
| 137 | err = ext4_change_inode_journal_flag(inode, jflag); |
| 138 | if (err) |
| 139 | goto flags_out; |
| 140 | if (migrate) |
| 141 | err = ext4_ext_migrate(inode); |
| 142 | flags_out: |
| 143 | mutex_unlock(&inode->i_mutex); |
| 144 | mnt_drop_write_file(filp); |
| 145 | return err; |
| 146 | } |
| 147 | case EXT4_IOC_GETVERSION: |
| 148 | case EXT4_IOC_GETVERSION_OLD: |
| 149 | return put_user(inode->i_generation, (int __user *) arg); |
| 150 | case EXT4_IOC_SETVERSION: |
| 151 | case EXT4_IOC_SETVERSION_OLD: { |
| 152 | handle_t *handle; |
| 153 | struct ext4_iloc iloc; |
| 154 | __u32 generation; |
| 155 | int err; |
| 156 | |
| 157 | if (!inode_owner_or_capable(inode)) |
| 158 | return -EPERM; |
| 159 | |
| 160 | err = mnt_want_write_file(filp); |
| 161 | if (err) |
| 162 | return err; |
| 163 | if (get_user(generation, (int __user *) arg)) { |
| 164 | err = -EFAULT; |
| 165 | goto setversion_out; |
| 166 | } |
| 167 | |
| 168 | mutex_lock(&inode->i_mutex); |
| 169 | handle = ext4_journal_start(inode, 1); |
| 170 | if (IS_ERR(handle)) { |
| 171 | err = PTR_ERR(handle); |
| 172 | goto unlock_out; |
| 173 | } |
| 174 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
| 175 | if (err == 0) { |
| 176 | inode->i_ctime = ext4_current_time(inode); |
| 177 | inode->i_generation = generation; |
| 178 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
| 179 | } |
| 180 | ext4_journal_stop(handle); |
| 181 | |
| 182 | unlock_out: |
| 183 | mutex_unlock(&inode->i_mutex); |
| 184 | setversion_out: |
| 185 | mnt_drop_write_file(filp); |
| 186 | return err; |
| 187 | } |
| 188 | case EXT4_IOC_GROUP_EXTEND: { |
| 189 | ext4_fsblk_t n_blocks_count; |
| 190 | int err, err2=0; |
| 191 | |
| 192 | err = ext4_resize_begin(sb); |
| 193 | if (err) |
| 194 | return err; |
| 195 | |
| 196 | if (get_user(n_blocks_count, (__u32 __user *)arg)) { |
| 197 | err = -EFAULT; |
| 198 | goto group_extend_out; |
| 199 | } |
| 200 | |
| 201 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 202 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 203 | ext4_msg(sb, KERN_ERR, |
| 204 | "Online resizing not supported with bigalloc"); |
| 205 | err = -EOPNOTSUPP; |
| 206 | goto group_extend_out; |
| 207 | } |
| 208 | |
| 209 | err = mnt_want_write_file(filp); |
| 210 | if (err) |
| 211 | goto group_extend_out; |
| 212 | |
| 213 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
| 214 | if (EXT4_SB(sb)->s_journal) { |
| 215 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 216 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 217 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 218 | } |
| 219 | if (err == 0) |
| 220 | err = err2; |
| 221 | mnt_drop_write_file(filp); |
| 222 | group_extend_out: |
| 223 | ext4_resize_end(sb); |
| 224 | return err; |
| 225 | } |
| 226 | |
| 227 | case EXT4_IOC_MOVE_EXT: { |
| 228 | struct move_extent me; |
| 229 | struct file *donor_filp; |
| 230 | int err; |
| 231 | |
| 232 | if (!(filp->f_mode & FMODE_READ) || |
| 233 | !(filp->f_mode & FMODE_WRITE)) |
| 234 | return -EBADF; |
| 235 | |
| 236 | if (copy_from_user(&me, |
| 237 | (struct move_extent __user *)arg, sizeof(me))) |
| 238 | return -EFAULT; |
| 239 | me.moved_len = 0; |
| 240 | |
| 241 | donor_filp = fget(me.donor_fd); |
| 242 | if (!donor_filp) |
| 243 | return -EBADF; |
| 244 | |
| 245 | if (!(donor_filp->f_mode & FMODE_WRITE)) { |
| 246 | err = -EBADF; |
| 247 | goto mext_out; |
| 248 | } |
| 249 | |
| 250 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 251 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 252 | ext4_msg(sb, KERN_ERR, |
| 253 | "Online defrag not supported with bigalloc"); |
| 254 | return -EOPNOTSUPP; |
| 255 | } |
| 256 | |
| 257 | err = mnt_want_write_file(filp); |
| 258 | if (err) |
| 259 | goto mext_out; |
| 260 | |
| 261 | err = ext4_move_extents(filp, donor_filp, me.orig_start, |
| 262 | me.donor_start, me.len, &me.moved_len); |
| 263 | mnt_drop_write_file(filp); |
| 264 | |
| 265 | if (copy_to_user((struct move_extent __user *)arg, |
| 266 | &me, sizeof(me))) |
| 267 | err = -EFAULT; |
| 268 | mext_out: |
| 269 | fput(donor_filp); |
| 270 | return err; |
| 271 | } |
| 272 | |
| 273 | case EXT4_IOC_GROUP_ADD: { |
| 274 | struct ext4_new_group_data input; |
| 275 | int err, err2=0; |
| 276 | |
| 277 | err = ext4_resize_begin(sb); |
| 278 | if (err) |
| 279 | return err; |
| 280 | |
| 281 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
| 282 | sizeof(input))) { |
| 283 | err = -EFAULT; |
| 284 | goto group_add_out; |
| 285 | } |
| 286 | |
| 287 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 288 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 289 | ext4_msg(sb, KERN_ERR, |
| 290 | "Online resizing not supported with bigalloc"); |
| 291 | err = -EOPNOTSUPP; |
| 292 | goto group_add_out; |
| 293 | } |
| 294 | |
| 295 | err = mnt_want_write_file(filp); |
| 296 | if (err) |
| 297 | goto group_add_out; |
| 298 | |
| 299 | err = ext4_group_add(sb, &input); |
| 300 | if (EXT4_SB(sb)->s_journal) { |
| 301 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 302 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 303 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 304 | } |
| 305 | if (err == 0) |
| 306 | err = err2; |
| 307 | mnt_drop_write_file(filp); |
| 308 | group_add_out: |
| 309 | ext4_resize_end(sb); |
| 310 | return err; |
| 311 | } |
| 312 | |
| 313 | case EXT4_IOC_MIGRATE: |
| 314 | { |
| 315 | int err; |
| 316 | if (!inode_owner_or_capable(inode)) |
| 317 | return -EACCES; |
| 318 | |
| 319 | err = mnt_want_write_file(filp); |
| 320 | if (err) |
| 321 | return err; |
| 322 | /* |
| 323 | * inode_mutex prevent write and truncate on the file. |
| 324 | * Read still goes through. We take i_data_sem in |
| 325 | * ext4_ext_swap_inode_data before we switch the |
| 326 | * inode format to prevent read. |
| 327 | */ |
| 328 | mutex_lock(&(inode->i_mutex)); |
| 329 | err = ext4_ext_migrate(inode); |
| 330 | mutex_unlock(&(inode->i_mutex)); |
| 331 | mnt_drop_write_file(filp); |
| 332 | return err; |
| 333 | } |
| 334 | |
| 335 | case EXT4_IOC_ALLOC_DA_BLKS: |
| 336 | { |
| 337 | int err; |
| 338 | if (!inode_owner_or_capable(inode)) |
| 339 | return -EACCES; |
| 340 | |
| 341 | err = mnt_want_write_file(filp); |
| 342 | if (err) |
| 343 | return err; |
| 344 | err = ext4_alloc_da_blocks(inode); |
| 345 | mnt_drop_write_file(filp); |
| 346 | return err; |
| 347 | } |
| 348 | |
| 349 | case EXT4_IOC_RESIZE_FS: { |
| 350 | ext4_fsblk_t n_blocks_count; |
| 351 | struct super_block *sb = inode->i_sb; |
| 352 | int err = 0, err2 = 0; |
| 353 | |
| 354 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 355 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 356 | ext4_msg(sb, KERN_ERR, |
| 357 | "Online resizing not (yet) supported with bigalloc"); |
| 358 | return -EOPNOTSUPP; |
| 359 | } |
| 360 | |
| 361 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, |
| 362 | EXT4_FEATURE_INCOMPAT_META_BG)) { |
| 363 | ext4_msg(sb, KERN_ERR, |
| 364 | "Online resizing not (yet) supported with meta_bg"); |
| 365 | return -EOPNOTSUPP; |
| 366 | } |
| 367 | |
| 368 | if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, |
| 369 | sizeof(__u64))) { |
| 370 | return -EFAULT; |
| 371 | } |
| 372 | |
| 373 | if (n_blocks_count > MAX_32_NUM && |
| 374 | !EXT4_HAS_INCOMPAT_FEATURE(sb, |
| 375 | EXT4_FEATURE_INCOMPAT_64BIT)) { |
| 376 | ext4_msg(sb, KERN_ERR, |
| 377 | "File system only supports 32-bit block numbers"); |
| 378 | return -EOPNOTSUPP; |
| 379 | } |
| 380 | |
| 381 | err = ext4_resize_begin(sb); |
| 382 | if (err) |
| 383 | return err; |
| 384 | |
| 385 | err = mnt_want_write(filp->f_path.mnt); |
| 386 | if (err) |
| 387 | goto resizefs_out; |
| 388 | |
| 389 | err = ext4_resize_fs(sb, n_blocks_count); |
| 390 | if (EXT4_SB(sb)->s_journal) { |
| 391 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
| 392 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); |
| 393 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); |
| 394 | } |
| 395 | if (err == 0) |
| 396 | err = err2; |
| 397 | mnt_drop_write(filp->f_path.mnt); |
| 398 | resizefs_out: |
| 399 | ext4_resize_end(sb); |
| 400 | return err; |
| 401 | } |
| 402 | |
| 403 | case FITRIM: |
| 404 | { |
| 405 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
| 406 | struct fstrim_range range; |
| 407 | int ret = 0; |
| 408 | |
| 409 | if (!capable(CAP_SYS_ADMIN)) |
| 410 | return -EPERM; |
| 411 | |
| 412 | if (!blk_queue_discard(q)) |
| 413 | return -EOPNOTSUPP; |
| 414 | |
| 415 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
| 416 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { |
| 417 | ext4_msg(sb, KERN_ERR, |
| 418 | "FITRIM not supported with bigalloc"); |
| 419 | return -EOPNOTSUPP; |
| 420 | } |
| 421 | |
| 422 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
| 423 | sizeof(range))) |
| 424 | return -EFAULT; |
| 425 | |
| 426 | range.minlen = max((unsigned int)range.minlen, |
| 427 | q->limits.discard_granularity); |
| 428 | ret = ext4_trim_fs(sb, &range); |
| 429 | if (ret < 0) |
| 430 | return ret; |
| 431 | |
| 432 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
| 433 | sizeof(range))) |
| 434 | return -EFAULT; |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | default: |
| 440 | return -ENOTTY; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | #ifdef CONFIG_COMPAT |
| 445 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 446 | { |
| 447 | /* These are just misnamed, they actually get/put from/to user an int */ |
| 448 | switch (cmd) { |
| 449 | case EXT4_IOC32_GETFLAGS: |
| 450 | cmd = EXT4_IOC_GETFLAGS; |
| 451 | break; |
| 452 | case EXT4_IOC32_SETFLAGS: |
| 453 | cmd = EXT4_IOC_SETFLAGS; |
| 454 | break; |
| 455 | case EXT4_IOC32_GETVERSION: |
| 456 | cmd = EXT4_IOC_GETVERSION; |
| 457 | break; |
| 458 | case EXT4_IOC32_SETVERSION: |
| 459 | cmd = EXT4_IOC_SETVERSION; |
| 460 | break; |
| 461 | case EXT4_IOC32_GROUP_EXTEND: |
| 462 | cmd = EXT4_IOC_GROUP_EXTEND; |
| 463 | break; |
| 464 | case EXT4_IOC32_GETVERSION_OLD: |
| 465 | cmd = EXT4_IOC_GETVERSION_OLD; |
| 466 | break; |
| 467 | case EXT4_IOC32_SETVERSION_OLD: |
| 468 | cmd = EXT4_IOC_SETVERSION_OLD; |
| 469 | break; |
| 470 | case EXT4_IOC32_GETRSVSZ: |
| 471 | cmd = EXT4_IOC_GETRSVSZ; |
| 472 | break; |
| 473 | case EXT4_IOC32_SETRSVSZ: |
| 474 | cmd = EXT4_IOC_SETRSVSZ; |
| 475 | break; |
| 476 | case EXT4_IOC32_GROUP_ADD: { |
| 477 | struct compat_ext4_new_group_input __user *uinput; |
| 478 | struct ext4_new_group_input input; |
| 479 | mm_segment_t old_fs; |
| 480 | int err; |
| 481 | |
| 482 | uinput = compat_ptr(arg); |
| 483 | err = get_user(input.group, &uinput->group); |
| 484 | err |= get_user(input.block_bitmap, &uinput->block_bitmap); |
| 485 | err |= get_user(input.inode_bitmap, &uinput->inode_bitmap); |
| 486 | err |= get_user(input.inode_table, &uinput->inode_table); |
| 487 | err |= get_user(input.blocks_count, &uinput->blocks_count); |
| 488 | err |= get_user(input.reserved_blocks, |
| 489 | &uinput->reserved_blocks); |
| 490 | if (err) |
| 491 | return -EFAULT; |
| 492 | old_fs = get_fs(); |
| 493 | set_fs(KERNEL_DS); |
| 494 | err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD, |
| 495 | (unsigned long) &input); |
| 496 | set_fs(old_fs); |
| 497 | return err; |
| 498 | } |
| 499 | case EXT4_IOC_MOVE_EXT: |
| 500 | case FITRIM: |
| 501 | case EXT4_IOC_RESIZE_FS: |
| 502 | break; |
| 503 | default: |
| 504 | return -ENOIOCTLCMD; |
| 505 | } |
| 506 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 507 | } |
| 508 | #endif |