linux/fs/ext4/ioctl.c
<<
>>
Prefs
   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
  23long 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);
 131flags_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);
 142flags_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                if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
 161                                EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
 162                        ext4_warning(sb, "Setting inode version is not "
 163                                     "supported with metadata_csum enabled.");
 164                        return -ENOTTY;
 165                }
 166
 167                err = mnt_want_write_file(filp);
 168                if (err)
 169                        return err;
 170                if (get_user(generation, (int __user *) arg)) {
 171                        err = -EFAULT;
 172                        goto setversion_out;
 173                }
 174
 175                mutex_lock(&inode->i_mutex);
 176                handle = ext4_journal_start(inode, 1);
 177                if (IS_ERR(handle)) {
 178                        err = PTR_ERR(handle);
 179                        goto unlock_out;
 180                }
 181                err = ext4_reserve_inode_write(handle, inode, &iloc);
 182                if (err == 0) {
 183                        inode->i_ctime = ext4_current_time(inode);
 184                        inode->i_generation = generation;
 185                        err = ext4_mark_iloc_dirty(handle, inode, &iloc);
 186                }
 187                ext4_journal_stop(handle);
 188
 189unlock_out:
 190                mutex_unlock(&inode->i_mutex);
 191setversion_out:
 192                mnt_drop_write_file(filp);
 193                return err;
 194        }
 195        case EXT4_IOC_GROUP_EXTEND: {
 196                ext4_fsblk_t n_blocks_count;
 197                int err, err2=0;
 198
 199                err = ext4_resize_begin(sb);
 200                if (err)
 201                        return err;
 202
 203                if (get_user(n_blocks_count, (__u32 __user *)arg)) {
 204                        err = -EFAULT;
 205                        goto group_extend_out;
 206                }
 207
 208                if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
 209                               EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
 210                        ext4_msg(sb, KERN_ERR,
 211                                 "Online resizing not supported with bigalloc");
 212                        err = -EOPNOTSUPP;
 213                        goto group_extend_out;
 214                }
 215
 216                err = mnt_want_write_file(filp);
 217                if (err)
 218                        goto group_extend_out;
 219
 220                err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
 221                if (EXT4_SB(sb)->s_journal) {
 222                        jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
 223                        err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
 224                        jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
 225                }
 226                if (err == 0)
 227                        err = err2;
 228                mnt_drop_write_file(filp);
 229group_extend_out:
 230                ext4_resize_end(sb);
 231                return err;
 232        }
 233
 234        case EXT4_IOC_MOVE_EXT: {
 235                struct move_extent me;
 236                struct file *donor_filp;
 237                int err;
 238
 239                if (!(filp->f_mode & FMODE_READ) ||
 240                    !(filp->f_mode & FMODE_WRITE))
 241                        return -EBADF;
 242
 243                if (copy_from_user(&me,
 244                        (struct move_extent __user *)arg, sizeof(me)))
 245                        return -EFAULT;
 246                me.moved_len = 0;
 247
 248                donor_filp = fget(me.donor_fd);
 249                if (!donor_filp)
 250                        return -EBADF;
 251
 252                if (!(donor_filp->f_mode & FMODE_WRITE)) {
 253                        err = -EBADF;
 254                        goto mext_out;
 255                }
 256
 257                if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
 258                               EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
 259                        ext4_msg(sb, KERN_ERR,
 260                                 "Online defrag not supported with bigalloc");
 261                        return -EOPNOTSUPP;
 262                }
 263
 264                err = mnt_want_write_file(filp);
 265                if (err)
 266                        goto mext_out;
 267
 268                err = ext4_move_extents(filp, donor_filp, me.orig_start,
 269                                        me.donor_start, me.len, &me.moved_len);
 270                mnt_drop_write_file(filp);
 271
 272                if (copy_to_user((struct move_extent __user *)arg,
 273                                 &me, sizeof(me)))
 274                        err = -EFAULT;
 275mext_out:
 276                fput(donor_filp);
 277                return err;
 278        }
 279
 280        case EXT4_IOC_GROUP_ADD: {
 281                struct ext4_new_group_data input;
 282                int err, err2=0;
 283
 284                err = ext4_resize_begin(sb);
 285                if (err)
 286                        return err;
 287
 288                if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
 289                                sizeof(input))) {
 290                        err = -EFAULT;
 291                        goto group_add_out;
 292                }
 293
 294                if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
 295                               EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
 296                        ext4_msg(sb, KERN_ERR,
 297                                 "Online resizing not supported with bigalloc");
 298                        err = -EOPNOTSUPP;
 299                        goto group_add_out;
 300                }
 301
 302                err = mnt_want_write_file(filp);
 303                if (err)
 304                        goto group_add_out;
 305
 306                err = ext4_group_add(sb, &input);
 307                if (EXT4_SB(sb)->s_journal) {
 308                        jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
 309                        err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
 310                        jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
 311                }
 312                if (err == 0)
 313                        err = err2;
 314                mnt_drop_write_file(filp);
 315group_add_out:
 316                ext4_resize_end(sb);
 317                return err;
 318        }
 319
 320        case EXT4_IOC_MIGRATE:
 321        {
 322                int err;
 323                if (!inode_owner_or_capable(inode))
 324                        return -EACCES;
 325
 326                err = mnt_want_write_file(filp);
 327                if (err)
 328                        return err;
 329                /*
 330                 * inode_mutex prevent write and truncate on the file.
 331                 * Read still goes through. We take i_data_sem in
 332                 * ext4_ext_swap_inode_data before we switch the
 333                 * inode format to prevent read.
 334                 */
 335                mutex_lock(&(inode->i_mutex));
 336                err = ext4_ext_migrate(inode);
 337                mutex_unlock(&(inode->i_mutex));
 338                mnt_drop_write_file(filp);
 339                return err;
 340        }
 341
 342        case EXT4_IOC_ALLOC_DA_BLKS:
 343        {
 344                int err;
 345                if (!inode_owner_or_capable(inode))
 346                        return -EACCES;
 347
 348                err = mnt_want_write_file(filp);
 349                if (err)
 350                        return err;
 351                err = ext4_alloc_da_blocks(inode);
 352                mnt_drop_write_file(filp);
 353                return err;
 354        }
 355
 356        case EXT4_IOC_RESIZE_FS: {
 357                ext4_fsblk_t n_blocks_count;
 358                struct super_block *sb = inode->i_sb;
 359                int err = 0, err2 = 0;
 360
 361                if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
 362                               EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
 363                        ext4_msg(sb, KERN_ERR,
 364                                 "Online resizing not (yet) supported with bigalloc");
 365                        return -EOPNOTSUPP;
 366                }
 367
 368                if (EXT4_HAS_INCOMPAT_FEATURE(sb,
 369                               EXT4_FEATURE_INCOMPAT_META_BG)) {
 370                        ext4_msg(sb, KERN_ERR,
 371                                 "Online resizing not (yet) supported with meta_bg");
 372                        return -EOPNOTSUPP;
 373                }
 374
 375                if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
 376                                   sizeof(__u64))) {
 377                        return -EFAULT;
 378                }
 379
 380                if (n_blocks_count > MAX_32_NUM &&
 381                    !EXT4_HAS_INCOMPAT_FEATURE(sb,
 382                                               EXT4_FEATURE_INCOMPAT_64BIT)) {
 383                        ext4_msg(sb, KERN_ERR,
 384                                 "File system only supports 32-bit block numbers");
 385                        return -EOPNOTSUPP;
 386                }
 387
 388                err = ext4_resize_begin(sb);
 389                if (err)
 390                        return err;
 391
 392                err = mnt_want_write_file(filp);
 393                if (err)
 394                        goto resizefs_out;
 395
 396                err = ext4_resize_fs(sb, n_blocks_count);
 397                if (EXT4_SB(sb)->s_journal) {
 398                        jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
 399                        err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
 400                        jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
 401                }
 402                if (err == 0)
 403                        err = err2;
 404                mnt_drop_write_file(filp);
 405resizefs_out:
 406                ext4_resize_end(sb);
 407                return err;
 408        }
 409
 410        case FITRIM:
 411        {
 412                struct request_queue *q = bdev_get_queue(sb->s_bdev);
 413                struct fstrim_range range;
 414                int ret = 0;
 415
 416                if (!capable(CAP_SYS_ADMIN))
 417                        return -EPERM;
 418
 419                if (!blk_queue_discard(q))
 420                        return -EOPNOTSUPP;
 421
 422                if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
 423                               EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
 424                        ext4_msg(sb, KERN_ERR,
 425                                 "FITRIM not supported with bigalloc");
 426                        return -EOPNOTSUPP;
 427                }
 428
 429                if (copy_from_user(&range, (struct fstrim_range __user *)arg,
 430                    sizeof(range)))
 431                        return -EFAULT;
 432
 433                range.minlen = max((unsigned int)range.minlen,
 434                                   q->limits.discard_granularity);
 435                ret = ext4_trim_fs(sb, &range);
 436                if (ret < 0)
 437                        return ret;
 438
 439                if (copy_to_user((struct fstrim_range __user *)arg, &range,
 440                    sizeof(range)))
 441                        return -EFAULT;
 442
 443                return 0;
 444        }
 445
 446        default:
 447                return -ENOTTY;
 448        }
 449}
 450
 451#ifdef CONFIG_COMPAT
 452long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 453{
 454        /* These are just misnamed, they actually get/put from/to user an int */
 455        switch (cmd) {
 456        case EXT4_IOC32_GETFLAGS:
 457                cmd = EXT4_IOC_GETFLAGS;
 458                break;
 459        case EXT4_IOC32_SETFLAGS:
 460                cmd = EXT4_IOC_SETFLAGS;
 461                break;
 462        case EXT4_IOC32_GETVERSION:
 463                cmd = EXT4_IOC_GETVERSION;
 464                break;
 465        case EXT4_IOC32_SETVERSION:
 466                cmd = EXT4_IOC_SETVERSION;
 467                break;
 468        case EXT4_IOC32_GROUP_EXTEND:
 469                cmd = EXT4_IOC_GROUP_EXTEND;
 470                break;
 471        case EXT4_IOC32_GETVERSION_OLD:
 472                cmd = EXT4_IOC_GETVERSION_OLD;
 473                break;
 474        case EXT4_IOC32_SETVERSION_OLD:
 475                cmd = EXT4_IOC_SETVERSION_OLD;
 476                break;
 477        case EXT4_IOC32_GETRSVSZ:
 478                cmd = EXT4_IOC_GETRSVSZ;
 479                break;
 480        case EXT4_IOC32_SETRSVSZ:
 481                cmd = EXT4_IOC_SETRSVSZ;
 482                break;
 483        case EXT4_IOC32_GROUP_ADD: {
 484                struct compat_ext4_new_group_input __user *uinput;
 485                struct ext4_new_group_input input;
 486                mm_segment_t old_fs;
 487                int err;
 488
 489                uinput = compat_ptr(arg);
 490                err = get_user(input.group, &uinput->group);
 491                err |= get_user(input.block_bitmap, &uinput->block_bitmap);
 492                err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
 493                err |= get_user(input.inode_table, &uinput->inode_table);
 494                err |= get_user(input.blocks_count, &uinput->blocks_count);
 495                err |= get_user(input.reserved_blocks,
 496                                &uinput->reserved_blocks);
 497                if (err)
 498                        return -EFAULT;
 499                old_fs = get_fs();
 500                set_fs(KERNEL_DS);
 501                err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
 502                                 (unsigned long) &input);
 503                set_fs(old_fs);
 504                return err;
 505        }
 506        case EXT4_IOC_MOVE_EXT:
 507        case FITRIM:
 508        case EXT4_IOC_RESIZE_FS:
 509                break;
 510        default:
 511                return -ENOIOCTLCMD;
 512        }
 513        return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
 514}
 515#endif
 516
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.