Merge tag 'drm-coc-for-v4.12-rc1' of git://people.freedesktop.org/~airlied/linux
[linux-2.6-microblaze.git] / fs / ext4 / ioctl.c
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/capability.h>
12 #include <linux/time.h>
13 #include <linux/compat.h>
14 #include <linux/mount.h>
15 #include <linux/file.h>
16 #include <linux/quotaops.h>
17 #include <linux/uuid.h>
18 #include <linux/uaccess.h>
19 #include <linux/delay.h>
20 #include "ext4_jbd2.h"
21 #include "ext4.h"
22
23 /**
24  * Swap memory between @a and @b for @len bytes.
25  *
26  * @a:          pointer to first memory area
27  * @b:          pointer to second memory area
28  * @len:        number of bytes to swap
29  *
30  */
31 static void memswap(void *a, void *b, size_t len)
32 {
33         unsigned char *ap, *bp;
34
35         ap = (unsigned char *)a;
36         bp = (unsigned char *)b;
37         while (len-- > 0) {
38                 swap(*ap, *bp);
39                 ap++;
40                 bp++;
41         }
42 }
43
44 /**
45  * Swap i_data and associated attributes between @inode1 and @inode2.
46  * This function is used for the primary swap between inode1 and inode2
47  * and also to revert this primary swap in case of errors.
48  *
49  * Therefore you have to make sure, that calling this method twice
50  * will revert all changes.
51  *
52  * @inode1:     pointer to first inode
53  * @inode2:     pointer to second inode
54  */
55 static void swap_inode_data(struct inode *inode1, struct inode *inode2)
56 {
57         loff_t isize;
58         struct ext4_inode_info *ei1;
59         struct ext4_inode_info *ei2;
60
61         ei1 = EXT4_I(inode1);
62         ei2 = EXT4_I(inode2);
63
64         memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
65         memswap(&inode1->i_version, &inode2->i_version,
66                   sizeof(inode1->i_version));
67         memswap(&inode1->i_blocks, &inode2->i_blocks,
68                   sizeof(inode1->i_blocks));
69         memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
70         memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
71         memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
72
73         memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
74         memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
75         memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
76         ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
77         ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
78
79         isize = i_size_read(inode1);
80         i_size_write(inode1, i_size_read(inode2));
81         i_size_write(inode2, isize);
82 }
83
84 /**
85  * Swap the information from the given @inode and the inode
86  * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
87  * important fields of the inodes.
88  *
89  * @sb:         the super block of the filesystem
90  * @inode:      the inode to swap with EXT4_BOOT_LOADER_INO
91  *
92  */
93 static long swap_inode_boot_loader(struct super_block *sb,
94                                 struct inode *inode)
95 {
96         handle_t *handle;
97         int err;
98         struct inode *inode_bl;
99         struct ext4_inode_info *ei_bl;
100         struct ext4_sb_info *sbi = EXT4_SB(sb);
101
102         if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
103                 return -EINVAL;
104
105         if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
106                 return -EPERM;
107
108         inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
109         if (IS_ERR(inode_bl))
110                 return PTR_ERR(inode_bl);
111         ei_bl = EXT4_I(inode_bl);
112
113         filemap_flush(inode->i_mapping);
114         filemap_flush(inode_bl->i_mapping);
115
116         /* Protect orig inodes against a truncate and make sure,
117          * that only 1 swap_inode_boot_loader is running. */
118         lock_two_nondirectories(inode, inode_bl);
119
120         truncate_inode_pages(&inode->i_data, 0);
121         truncate_inode_pages(&inode_bl->i_data, 0);
122
123         /* Wait for all existing dio workers */
124         ext4_inode_block_unlocked_dio(inode);
125         ext4_inode_block_unlocked_dio(inode_bl);
126         inode_dio_wait(inode);
127         inode_dio_wait(inode_bl);
128
129         handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
130         if (IS_ERR(handle)) {
131                 err = -EINVAL;
132                 goto journal_err_out;
133         }
134
135         /* Protect extent tree against block allocations via delalloc */
136         ext4_double_down_write_data_sem(inode, inode_bl);
137
138         if (inode_bl->i_nlink == 0) {
139                 /* this inode has never been used as a BOOT_LOADER */
140                 set_nlink(inode_bl, 1);
141                 i_uid_write(inode_bl, 0);
142                 i_gid_write(inode_bl, 0);
143                 inode_bl->i_flags = 0;
144                 ei_bl->i_flags = 0;
145                 inode_bl->i_version = 1;
146                 i_size_write(inode_bl, 0);
147                 inode_bl->i_mode = S_IFREG;
148                 if (ext4_has_feature_extents(sb)) {
149                         ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
150                         ext4_ext_tree_init(handle, inode_bl);
151                 } else
152                         memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
153         }
154
155         swap_inode_data(inode, inode_bl);
156
157         inode->i_ctime = inode_bl->i_ctime = current_time(inode);
158
159         spin_lock(&sbi->s_next_gen_lock);
160         inode->i_generation = sbi->s_next_generation++;
161         inode_bl->i_generation = sbi->s_next_generation++;
162         spin_unlock(&sbi->s_next_gen_lock);
163
164         ext4_discard_preallocations(inode);
165
166         err = ext4_mark_inode_dirty(handle, inode);
167         if (err < 0) {
168                 ext4_warning(inode->i_sb,
169                         "couldn't mark inode #%lu dirty (err %d)",
170                         inode->i_ino, err);
171                 /* Revert all changes: */
172                 swap_inode_data(inode, inode_bl);
173         } else {
174                 err = ext4_mark_inode_dirty(handle, inode_bl);
175                 if (err < 0) {
176                         ext4_warning(inode_bl->i_sb,
177                                 "couldn't mark inode #%lu dirty (err %d)",
178                                 inode_bl->i_ino, err);
179                         /* Revert all changes: */
180                         swap_inode_data(inode, inode_bl);
181                         ext4_mark_inode_dirty(handle, inode);
182                 }
183         }
184         ext4_journal_stop(handle);
185         ext4_double_up_write_data_sem(inode, inode_bl);
186
187 journal_err_out:
188         ext4_inode_resume_unlocked_dio(inode);
189         ext4_inode_resume_unlocked_dio(inode_bl);
190         unlock_two_nondirectories(inode, inode_bl);
191         iput(inode_bl);
192         return err;
193 }
194
195 #ifdef CONFIG_EXT4_FS_ENCRYPTION
196 static int uuid_is_zero(__u8 u[16])
197 {
198         int     i;
199
200         for (i = 0; i < 16; i++)
201                 if (u[i])
202                         return 0;
203         return 1;
204 }
205 #endif
206
207 static int ext4_ioctl_setflags(struct inode *inode,
208                                unsigned int flags)
209 {
210         struct ext4_inode_info *ei = EXT4_I(inode);
211         handle_t *handle = NULL;
212         int err = -EPERM, migrate = 0;
213         struct ext4_iloc iloc;
214         unsigned int oldflags, mask, i;
215         unsigned int jflag;
216
217         /* Is it quota file? Do not allow user to mess with it */
218         if (IS_NOQUOTA(inode))
219                 goto flags_out;
220
221         oldflags = ei->i_flags;
222
223         /* The JOURNAL_DATA flag is modifiable only by root */
224         jflag = flags & EXT4_JOURNAL_DATA_FL;
225
226         /*
227          * The IMMUTABLE and APPEND_ONLY flags can only be changed by
228          * the relevant capability.
229          *
230          * This test looks nicer. Thanks to Pauline Middelink
231          */
232         if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
233                 if (!capable(CAP_LINUX_IMMUTABLE))
234                         goto flags_out;
235         }
236
237         /*
238          * The JOURNAL_DATA flag can only be changed by
239          * the relevant capability.
240          */
241         if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
242                 if (!capable(CAP_SYS_RESOURCE))
243                         goto flags_out;
244         }
245         if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
246                 migrate = 1;
247
248         if (flags & EXT4_EOFBLOCKS_FL) {
249                 /* we don't support adding EOFBLOCKS flag */
250                 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
251                         err = -EOPNOTSUPP;
252                         goto flags_out;
253                 }
254         } else if (oldflags & EXT4_EOFBLOCKS_FL) {
255                 err = ext4_truncate(inode);
256                 if (err)
257                         goto flags_out;
258         }
259
260         handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
261         if (IS_ERR(handle)) {
262                 err = PTR_ERR(handle);
263                 goto flags_out;
264         }
265         if (IS_SYNC(inode))
266                 ext4_handle_sync(handle);
267         err = ext4_reserve_inode_write(handle, inode, &iloc);
268         if (err)
269                 goto flags_err;
270
271         for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
272                 if (!(mask & EXT4_FL_USER_MODIFIABLE))
273                         continue;
274                 /* These flags get special treatment later */
275                 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
276                         continue;
277                 if (mask & flags)
278                         ext4_set_inode_flag(inode, i);
279                 else
280                         ext4_clear_inode_flag(inode, i);
281         }
282
283         ext4_set_inode_flags(inode);
284         inode->i_ctime = current_time(inode);
285
286         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
287 flags_err:
288         ext4_journal_stop(handle);
289         if (err)
290                 goto flags_out;
291
292         if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
293                 err = ext4_change_inode_journal_flag(inode, jflag);
294         if (err)
295                 goto flags_out;
296         if (migrate) {
297                 if (flags & EXT4_EXTENTS_FL)
298                         err = ext4_ext_migrate(inode);
299                 else
300                         err = ext4_ind_migrate(inode);
301         }
302
303 flags_out:
304         return err;
305 }
306
307 #ifdef CONFIG_QUOTA
308 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
309 {
310         struct inode *inode = file_inode(filp);
311         struct super_block *sb = inode->i_sb;
312         struct ext4_inode_info *ei = EXT4_I(inode);
313         int err, rc;
314         handle_t *handle;
315         kprojid_t kprojid;
316         struct ext4_iloc iloc;
317         struct ext4_inode *raw_inode;
318         struct dquot *transfer_to[MAXQUOTAS] = { };
319
320         if (!ext4_has_feature_project(sb)) {
321                 if (projid != EXT4_DEF_PROJID)
322                         return -EOPNOTSUPP;
323                 else
324                         return 0;
325         }
326
327         if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
328                 return -EOPNOTSUPP;
329
330         kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
331
332         if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
333                 return 0;
334
335         err = mnt_want_write_file(filp);
336         if (err)
337                 return err;
338
339         err = -EPERM;
340         inode_lock(inode);
341         /* Is it quota file? Do not allow user to mess with it */
342         if (IS_NOQUOTA(inode))
343                 goto out_unlock;
344
345         err = ext4_get_inode_loc(inode, &iloc);
346         if (err)
347                 goto out_unlock;
348
349         raw_inode = ext4_raw_inode(&iloc);
350         if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
351                 err = -EOVERFLOW;
352                 brelse(iloc.bh);
353                 goto out_unlock;
354         }
355         brelse(iloc.bh);
356
357         dquot_initialize(inode);
358
359         handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
360                 EXT4_QUOTA_INIT_BLOCKS(sb) +
361                 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
362         if (IS_ERR(handle)) {
363                 err = PTR_ERR(handle);
364                 goto out_unlock;
365         }
366
367         err = ext4_reserve_inode_write(handle, inode, &iloc);
368         if (err)
369                 goto out_stop;
370
371         transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
372         if (!IS_ERR(transfer_to[PRJQUOTA])) {
373                 err = __dquot_transfer(inode, transfer_to);
374                 dqput(transfer_to[PRJQUOTA]);
375                 if (err)
376                         goto out_dirty;
377         }
378
379         EXT4_I(inode)->i_projid = kprojid;
380         inode->i_ctime = current_time(inode);
381 out_dirty:
382         rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
383         if (!err)
384                 err = rc;
385 out_stop:
386         ext4_journal_stop(handle);
387 out_unlock:
388         inode_unlock(inode);
389         mnt_drop_write_file(filp);
390         return err;
391 }
392 #else
393 static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
394 {
395         if (projid != EXT4_DEF_PROJID)
396                 return -EOPNOTSUPP;
397         return 0;
398 }
399 #endif
400
401 /* Transfer internal flags to xflags */
402 static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
403 {
404         __u32 xflags = 0;
405
406         if (iflags & EXT4_SYNC_FL)
407                 xflags |= FS_XFLAG_SYNC;
408         if (iflags & EXT4_IMMUTABLE_FL)
409                 xflags |= FS_XFLAG_IMMUTABLE;
410         if (iflags & EXT4_APPEND_FL)
411                 xflags |= FS_XFLAG_APPEND;
412         if (iflags & EXT4_NODUMP_FL)
413                 xflags |= FS_XFLAG_NODUMP;
414         if (iflags & EXT4_NOATIME_FL)
415                 xflags |= FS_XFLAG_NOATIME;
416         if (iflags & EXT4_PROJINHERIT_FL)
417                 xflags |= FS_XFLAG_PROJINHERIT;
418         return xflags;
419 }
420
421 #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
422                                   FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
423                                   FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
424
425 /* Transfer xflags flags to internal */
426 static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
427 {
428         unsigned long iflags = 0;
429
430         if (xflags & FS_XFLAG_SYNC)
431                 iflags |= EXT4_SYNC_FL;
432         if (xflags & FS_XFLAG_IMMUTABLE)
433                 iflags |= EXT4_IMMUTABLE_FL;
434         if (xflags & FS_XFLAG_APPEND)
435                 iflags |= EXT4_APPEND_FL;
436         if (xflags & FS_XFLAG_NODUMP)
437                 iflags |= EXT4_NODUMP_FL;
438         if (xflags & FS_XFLAG_NOATIME)
439                 iflags |= EXT4_NOATIME_FL;
440         if (xflags & FS_XFLAG_PROJINHERIT)
441                 iflags |= EXT4_PROJINHERIT_FL;
442
443         return iflags;
444 }
445
446 int ext4_shutdown(struct super_block *sb, unsigned long arg)
447 {
448         struct ext4_sb_info *sbi = EXT4_SB(sb);
449         __u32 flags;
450
451         if (!capable(CAP_SYS_ADMIN))
452                 return -EPERM;
453
454         if (get_user(flags, (__u32 __user *)arg))
455                 return -EFAULT;
456
457         if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
458                 return -EINVAL;
459
460         if (ext4_forced_shutdown(sbi))
461                 return 0;
462
463         ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
464
465         switch (flags) {
466         case EXT4_GOING_FLAGS_DEFAULT:
467                 freeze_bdev(sb->s_bdev);
468                 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
469                 thaw_bdev(sb->s_bdev, sb);
470                 break;
471         case EXT4_GOING_FLAGS_LOGFLUSH:
472                 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
473                 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
474                         (void) ext4_force_commit(sb);
475                         jbd2_journal_abort(sbi->s_journal, 0);
476                 }
477                 break;
478         case EXT4_GOING_FLAGS_NOLOGFLUSH:
479                 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
480                 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
481                         msleep(100);
482                         jbd2_journal_abort(sbi->s_journal, 0);
483                 }
484                 break;
485         default:
486                 return -EINVAL;
487         }
488         clear_opt(sb, DISCARD);
489         return 0;
490 }
491
492 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
493 {
494         struct inode *inode = file_inode(filp);
495         struct super_block *sb = inode->i_sb;
496         struct ext4_inode_info *ei = EXT4_I(inode);
497         unsigned int flags;
498
499         ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
500
501         switch (cmd) {
502         case EXT4_IOC_GETFLAGS:
503                 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
504                 return put_user(flags, (int __user *) arg);
505         case EXT4_IOC_SETFLAGS: {
506                 int err;
507
508                 if (!inode_owner_or_capable(inode))
509                         return -EACCES;
510
511                 if (get_user(flags, (int __user *) arg))
512                         return -EFAULT;
513
514                 if (flags & ~EXT4_FL_USER_VISIBLE)
515                         return -EOPNOTSUPP;
516                 /*
517                  * chattr(1) grabs flags via GETFLAGS, modifies the result and
518                  * passes that to SETFLAGS. So we cannot easily make SETFLAGS
519                  * more restrictive than just silently masking off visible but
520                  * not settable flags as we always did.
521                  */
522                 flags &= EXT4_FL_USER_MODIFIABLE;
523                 if (ext4_mask_flags(inode->i_mode, flags) != flags)
524                         return -EOPNOTSUPP;
525
526                 err = mnt_want_write_file(filp);
527                 if (err)
528                         return err;
529
530                 inode_lock(inode);
531                 err = ext4_ioctl_setflags(inode, flags);
532                 inode_unlock(inode);
533                 mnt_drop_write_file(filp);
534                 return err;
535         }
536         case EXT4_IOC_GETVERSION:
537         case EXT4_IOC_GETVERSION_OLD:
538                 return put_user(inode->i_generation, (int __user *) arg);
539         case EXT4_IOC_SETVERSION:
540         case EXT4_IOC_SETVERSION_OLD: {
541                 handle_t *handle;
542                 struct ext4_iloc iloc;
543                 __u32 generation;
544                 int err;
545
546                 if (!inode_owner_or_capable(inode))
547                         return -EPERM;
548
549                 if (ext4_has_metadata_csum(inode->i_sb)) {
550                         ext4_warning(sb, "Setting inode version is not "
551                                      "supported with metadata_csum enabled.");
552                         return -ENOTTY;
553                 }
554
555                 err = mnt_want_write_file(filp);
556                 if (err)
557                         return err;
558                 if (get_user(generation, (int __user *) arg)) {
559                         err = -EFAULT;
560                         goto setversion_out;
561                 }
562
563                 inode_lock(inode);
564                 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
565                 if (IS_ERR(handle)) {
566                         err = PTR_ERR(handle);
567                         goto unlock_out;
568                 }
569                 err = ext4_reserve_inode_write(handle, inode, &iloc);
570                 if (err == 0) {
571                         inode->i_ctime = current_time(inode);
572                         inode->i_generation = generation;
573                         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
574                 }
575                 ext4_journal_stop(handle);
576
577 unlock_out:
578                 inode_unlock(inode);
579 setversion_out:
580                 mnt_drop_write_file(filp);
581                 return err;
582         }
583         case EXT4_IOC_GROUP_EXTEND: {
584                 ext4_fsblk_t n_blocks_count;
585                 int err, err2=0;
586
587                 err = ext4_resize_begin(sb);
588                 if (err)
589                         return err;
590
591                 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
592                         err = -EFAULT;
593                         goto group_extend_out;
594                 }
595
596                 if (ext4_has_feature_bigalloc(sb)) {
597                         ext4_msg(sb, KERN_ERR,
598                                  "Online resizing not supported with bigalloc");
599                         err = -EOPNOTSUPP;
600                         goto group_extend_out;
601                 }
602
603                 err = mnt_want_write_file(filp);
604                 if (err)
605                         goto group_extend_out;
606
607                 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
608                 if (EXT4_SB(sb)->s_journal) {
609                         jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
610                         err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
611                         jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
612                 }
613                 if (err == 0)
614                         err = err2;
615                 mnt_drop_write_file(filp);
616 group_extend_out:
617                 ext4_resize_end(sb);
618                 return err;
619         }
620
621         case EXT4_IOC_MOVE_EXT: {
622                 struct move_extent me;
623                 struct fd donor;
624                 int err;
625
626                 if (!(filp->f_mode & FMODE_READ) ||
627                     !(filp->f_mode & FMODE_WRITE))
628                         return -EBADF;
629
630                 if (copy_from_user(&me,
631                         (struct move_extent __user *)arg, sizeof(me)))
632                         return -EFAULT;
633                 me.moved_len = 0;
634
635                 donor = fdget(me.donor_fd);
636                 if (!donor.file)
637                         return -EBADF;
638
639                 if (!(donor.file->f_mode & FMODE_WRITE)) {
640                         err = -EBADF;
641                         goto mext_out;
642                 }
643
644                 if (ext4_has_feature_bigalloc(sb)) {
645                         ext4_msg(sb, KERN_ERR,
646                                  "Online defrag not supported with bigalloc");
647                         err = -EOPNOTSUPP;
648                         goto mext_out;
649                 } else if (IS_DAX(inode)) {
650                         ext4_msg(sb, KERN_ERR,
651                                  "Online defrag not supported with DAX");
652                         err = -EOPNOTSUPP;
653                         goto mext_out;
654                 }
655
656                 err = mnt_want_write_file(filp);
657                 if (err)
658                         goto mext_out;
659
660                 err = ext4_move_extents(filp, donor.file, me.orig_start,
661                                         me.donor_start, me.len, &me.moved_len);
662                 mnt_drop_write_file(filp);
663
664                 if (copy_to_user((struct move_extent __user *)arg,
665                                  &me, sizeof(me)))
666                         err = -EFAULT;
667 mext_out:
668                 fdput(donor);
669                 return err;
670         }
671
672         case EXT4_IOC_GROUP_ADD: {
673                 struct ext4_new_group_data input;
674                 int err, err2=0;
675
676                 err = ext4_resize_begin(sb);
677                 if (err)
678                         return err;
679
680                 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
681                                 sizeof(input))) {
682                         err = -EFAULT;
683                         goto group_add_out;
684                 }
685
686                 if (ext4_has_feature_bigalloc(sb)) {
687                         ext4_msg(sb, KERN_ERR,
688                                  "Online resizing not supported with bigalloc");
689                         err = -EOPNOTSUPP;
690                         goto group_add_out;
691                 }
692
693                 err = mnt_want_write_file(filp);
694                 if (err)
695                         goto group_add_out;
696
697                 err = ext4_group_add(sb, &input);
698                 if (EXT4_SB(sb)->s_journal) {
699                         jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
700                         err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
701                         jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
702                 }
703                 if (err == 0)
704                         err = err2;
705                 mnt_drop_write_file(filp);
706                 if (!err && ext4_has_group_desc_csum(sb) &&
707                     test_opt(sb, INIT_INODE_TABLE))
708                         err = ext4_register_li_request(sb, input.group);
709 group_add_out:
710                 ext4_resize_end(sb);
711                 return err;
712         }
713
714         case EXT4_IOC_MIGRATE:
715         {
716                 int err;
717                 if (!inode_owner_or_capable(inode))
718                         return -EACCES;
719
720                 err = mnt_want_write_file(filp);
721                 if (err)
722                         return err;
723                 /*
724                  * inode_mutex prevent write and truncate on the file.
725                  * Read still goes through. We take i_data_sem in
726                  * ext4_ext_swap_inode_data before we switch the
727                  * inode format to prevent read.
728                  */
729                 inode_lock((inode));
730                 err = ext4_ext_migrate(inode);
731                 inode_unlock((inode));
732                 mnt_drop_write_file(filp);
733                 return err;
734         }
735
736         case EXT4_IOC_ALLOC_DA_BLKS:
737         {
738                 int err;
739                 if (!inode_owner_or_capable(inode))
740                         return -EACCES;
741
742                 err = mnt_want_write_file(filp);
743                 if (err)
744                         return err;
745                 err = ext4_alloc_da_blocks(inode);
746                 mnt_drop_write_file(filp);
747                 return err;
748         }
749
750         case EXT4_IOC_SWAP_BOOT:
751         {
752                 int err;
753                 if (!(filp->f_mode & FMODE_WRITE))
754                         return -EBADF;
755                 err = mnt_want_write_file(filp);
756                 if (err)
757                         return err;
758                 err = swap_inode_boot_loader(sb, inode);
759                 mnt_drop_write_file(filp);
760                 return err;
761         }
762
763         case EXT4_IOC_RESIZE_FS: {
764                 ext4_fsblk_t n_blocks_count;
765                 int err = 0, err2 = 0;
766                 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
767
768                 if (ext4_has_feature_bigalloc(sb)) {
769                         ext4_msg(sb, KERN_ERR,
770                                  "Online resizing not (yet) supported with bigalloc");
771                         return -EOPNOTSUPP;
772                 }
773
774                 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
775                                    sizeof(__u64))) {
776                         return -EFAULT;
777                 }
778
779                 err = ext4_resize_begin(sb);
780                 if (err)
781                         return err;
782
783                 err = mnt_want_write_file(filp);
784                 if (err)
785                         goto resizefs_out;
786
787                 err = ext4_resize_fs(sb, n_blocks_count);
788                 if (EXT4_SB(sb)->s_journal) {
789                         jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
790                         err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
791                         jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
792                 }
793                 if (err == 0)
794                         err = err2;
795                 mnt_drop_write_file(filp);
796                 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
797                     ext4_has_group_desc_csum(sb) &&
798                     test_opt(sb, INIT_INODE_TABLE))
799                         err = ext4_register_li_request(sb, o_group);
800
801 resizefs_out:
802                 ext4_resize_end(sb);
803                 return err;
804         }
805
806         case FITRIM:
807         {
808                 struct request_queue *q = bdev_get_queue(sb->s_bdev);
809                 struct fstrim_range range;
810                 int ret = 0;
811
812                 if (!capable(CAP_SYS_ADMIN))
813                         return -EPERM;
814
815                 if (!blk_queue_discard(q))
816                         return -EOPNOTSUPP;
817
818                 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
819                     sizeof(range)))
820                         return -EFAULT;
821
822                 range.minlen = max((unsigned int)range.minlen,
823                                    q->limits.discard_granularity);
824                 ret = ext4_trim_fs(sb, &range);
825                 if (ret < 0)
826                         return ret;
827
828                 if (copy_to_user((struct fstrim_range __user *)arg, &range,
829                     sizeof(range)))
830                         return -EFAULT;
831
832                 return 0;
833         }
834         case EXT4_IOC_PRECACHE_EXTENTS:
835                 return ext4_ext_precache(inode);
836
837         case EXT4_IOC_SET_ENCRYPTION_POLICY:
838                 if (!ext4_has_feature_encrypt(sb))
839                         return -EOPNOTSUPP;
840                 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
841
842         case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
843 #ifdef CONFIG_EXT4_FS_ENCRYPTION
844                 int err, err2;
845                 struct ext4_sb_info *sbi = EXT4_SB(sb);
846                 handle_t *handle;
847
848                 if (!ext4_has_feature_encrypt(sb))
849                         return -EOPNOTSUPP;
850                 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
851                         err = mnt_want_write_file(filp);
852                         if (err)
853                                 return err;
854                         handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
855                         if (IS_ERR(handle)) {
856                                 err = PTR_ERR(handle);
857                                 goto pwsalt_err_exit;
858                         }
859                         err = ext4_journal_get_write_access(handle, sbi->s_sbh);
860                         if (err)
861                                 goto pwsalt_err_journal;
862                         generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
863                         err = ext4_handle_dirty_metadata(handle, NULL,
864                                                          sbi->s_sbh);
865                 pwsalt_err_journal:
866                         err2 = ext4_journal_stop(handle);
867                         if (err2 && !err)
868                                 err = err2;
869                 pwsalt_err_exit:
870                         mnt_drop_write_file(filp);
871                         if (err)
872                                 return err;
873                 }
874                 if (copy_to_user((void __user *) arg,
875                                  sbi->s_es->s_encrypt_pw_salt, 16))
876                         return -EFAULT;
877                 return 0;
878 #else
879                 return -EOPNOTSUPP;
880 #endif
881         }
882         case EXT4_IOC_GET_ENCRYPTION_POLICY:
883                 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
884
885         case EXT4_IOC_FSGETXATTR:
886         {
887                 struct fsxattr fa;
888
889                 memset(&fa, 0, sizeof(struct fsxattr));
890                 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
891
892                 if (ext4_has_feature_project(inode->i_sb)) {
893                         fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
894                                 EXT4_I(inode)->i_projid);
895                 }
896
897                 if (copy_to_user((struct fsxattr __user *)arg,
898                                  &fa, sizeof(fa)))
899                         return -EFAULT;
900                 return 0;
901         }
902         case EXT4_IOC_FSSETXATTR:
903         {
904                 struct fsxattr fa;
905                 int err;
906
907                 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
908                                    sizeof(fa)))
909                         return -EFAULT;
910
911                 /* Make sure caller has proper permission */
912                 if (!inode_owner_or_capable(inode))
913                         return -EACCES;
914
915                 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
916                         return -EOPNOTSUPP;
917
918                 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
919                 if (ext4_mask_flags(inode->i_mode, flags) != flags)
920                         return -EOPNOTSUPP;
921
922                 err = mnt_want_write_file(filp);
923                 if (err)
924                         return err;
925
926                 inode_lock(inode);
927                 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
928                          (flags & EXT4_FL_XFLAG_VISIBLE);
929                 err = ext4_ioctl_setflags(inode, flags);
930                 inode_unlock(inode);
931                 mnt_drop_write_file(filp);
932                 if (err)
933                         return err;
934
935                 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
936                 if (err)
937                         return err;
938
939                 return 0;
940         }
941         case EXT4_IOC_SHUTDOWN:
942                 return ext4_shutdown(sb, arg);
943         default:
944                 return -ENOTTY;
945         }
946 }
947
948 #ifdef CONFIG_COMPAT
949 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
950 {
951         /* These are just misnamed, they actually get/put from/to user an int */
952         switch (cmd) {
953         case EXT4_IOC32_GETFLAGS:
954                 cmd = EXT4_IOC_GETFLAGS;
955                 break;
956         case EXT4_IOC32_SETFLAGS:
957                 cmd = EXT4_IOC_SETFLAGS;
958                 break;
959         case EXT4_IOC32_GETVERSION:
960                 cmd = EXT4_IOC_GETVERSION;
961                 break;
962         case EXT4_IOC32_SETVERSION:
963                 cmd = EXT4_IOC_SETVERSION;
964                 break;
965         case EXT4_IOC32_GROUP_EXTEND:
966                 cmd = EXT4_IOC_GROUP_EXTEND;
967                 break;
968         case EXT4_IOC32_GETVERSION_OLD:
969                 cmd = EXT4_IOC_GETVERSION_OLD;
970                 break;
971         case EXT4_IOC32_SETVERSION_OLD:
972                 cmd = EXT4_IOC_SETVERSION_OLD;
973                 break;
974         case EXT4_IOC32_GETRSVSZ:
975                 cmd = EXT4_IOC_GETRSVSZ;
976                 break;
977         case EXT4_IOC32_SETRSVSZ:
978                 cmd = EXT4_IOC_SETRSVSZ;
979                 break;
980         case EXT4_IOC32_GROUP_ADD: {
981                 struct compat_ext4_new_group_input __user *uinput;
982                 struct ext4_new_group_input input;
983                 mm_segment_t old_fs;
984                 int err;
985
986                 uinput = compat_ptr(arg);
987                 err = get_user(input.group, &uinput->group);
988                 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
989                 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
990                 err |= get_user(input.inode_table, &uinput->inode_table);
991                 err |= get_user(input.blocks_count, &uinput->blocks_count);
992                 err |= get_user(input.reserved_blocks,
993                                 &uinput->reserved_blocks);
994                 if (err)
995                         return -EFAULT;
996                 old_fs = get_fs();
997                 set_fs(KERNEL_DS);
998                 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
999                                  (unsigned long) &input);
1000                 set_fs(old_fs);
1001                 return err;
1002         }
1003         case EXT4_IOC_MOVE_EXT:
1004         case EXT4_IOC_RESIZE_FS:
1005         case EXT4_IOC_PRECACHE_EXTENTS:
1006         case EXT4_IOC_SET_ENCRYPTION_POLICY:
1007         case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1008         case EXT4_IOC_GET_ENCRYPTION_POLICY:
1009         case EXT4_IOC_SHUTDOWN:
1010                 break;
1011         default:
1012                 return -ENOIOCTLCMD;
1013         }
1014         return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1015 }
1016 #endif