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