]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/ext4/ioctl.c
x86/msr: Add definitions for new speculation control MSRs
[mirror_ubuntu-artful-kernel.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 #include <linux/fsmap.h>
23 #include "fsmap.h"
24 #include <trace/events/ext4.h>
25
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 */
34 static void memswap(void *a, void *b, size_t len)
35 {
36 unsigned char *ap, *bp;
37
38 ap = (unsigned char *)a;
39 bp = (unsigned char *)b;
40 while (len-- > 0) {
41 swap(*ap, *bp);
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 */
58 static 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
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);
73
74 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
75 swap(ei1->i_flags, ei2->i_flags);
76 swap(ei1->i_disksize, ei2->i_disksize);
77 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
78 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
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 */
94 static 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;
100 struct ext4_inode_info *ei_bl;
101 struct ext4_sb_info *sbi = EXT4_SB(sb);
102
103 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
104 return -EINVAL;
105
106 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
107 return -EPERM;
108
109 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
110 if (IS_ERR(inode_bl))
111 return PTR_ERR(inode_bl);
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. */
119 lock_two_nondirectories(inode, inode_bl);
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;
133 goto journal_err_out;
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;
149 if (ext4_has_feature_extents(sb)) {
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
158 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
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 }
185 ext4_journal_stop(handle);
186 ext4_double_up_write_data_sem(inode, inode_bl);
187
188 journal_err_out:
189 ext4_inode_resume_unlocked_dio(inode);
190 ext4_inode_resume_unlocked_dio(inode_bl);
191 unlock_two_nondirectories(inode, inode_bl);
192 iput(inode_bl);
193 return err;
194 }
195
196 #ifdef CONFIG_EXT4_FS_ENCRYPTION
197 static 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 }
206 #endif
207
208 static 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;
213 int err = -EPERM, migrate = 0;
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 */
219 if (ext4_is_quota_file(inode))
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)) {
243 if (!ns_capable(inode->i_sb->s_user_ns, CAP_SYS_RESOURCE))
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 }
255 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
256 err = ext4_truncate(inode);
257 if (err)
258 goto flags_out;
259 }
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;
275 /* These flags get special treatment later */
276 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
277 continue;
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);
285 inode->i_ctime = current_time(inode);
286
287 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
288 flags_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
304 flags_out:
305 return err;
306 }
307
308 #ifdef CONFIG_QUOTA
309 static 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;
319 struct dquot *transfer_to[MAXQUOTAS] = { };
320
321 if (!ext4_has_feature_project(sb)) {
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
331 kprojid = make_kprojid(sb->s_user_ns, (projid_t)projid);
332
333 if (!projid_valid(kprojid))
334 return -EOVERFLOW;
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;
343 inode_lock(inode);
344 /* Is it quota file? Do not allow user to mess with it */
345 if (ext4_is_quota_file(inode))
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)) {
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 {
360 brelse(iloc.bh);
361 }
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
377 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
378 if (!IS_ERR(transfer_to[PRJQUOTA])) {
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);
384 err = __dquot_transfer(inode, transfer_to);
385 up_read(&EXT4_I(inode)->xattr_sem);
386 dqput(transfer_to[PRJQUOTA]);
387 if (err)
388 goto out_dirty;
389 }
390
391 EXT4_I(inode)->i_projid = kprojid;
392 inode->i_ctime = current_time(inode);
393 out_dirty:
394 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
395 if (!err)
396 err = rc;
397 out_stop:
398 ext4_journal_stop(handle);
399 out_unlock:
400 inode_unlock(inode);
401 mnt_drop_write_file(filp);
402 return err;
403 }
404 #else
405 static 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 */
414 static 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
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
437 /* Transfer xflags flags to internal */
438 static 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
458 static int ext4_shutdown(struct super_block *sb, unsigned long arg)
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
504 struct 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
511 static 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
527 static 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
588 long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
589 {
590 struct inode *inode = file_inode(filp);
591 struct super_block *sb = inode->i_sb;
592 struct ext4_inode_info *ei = EXT4_I(inode);
593 unsigned int flags;
594
595 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
596
597 switch (cmd) {
598 case FS_IOC_GETFSMAP:
599 return ext4_ioc_getfsmap(sb, (void __user *)arg);
600 case EXT4_IOC_GETFLAGS:
601 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
602 return put_user(flags, (int __user *) arg);
603 case EXT4_IOC_SETFLAGS: {
604 int err;
605
606 if (!inode_owner_or_capable(inode))
607 return -EACCES;
608
609 if (get_user(flags, (int __user *) arg))
610 return -EFAULT;
611
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
624 err = mnt_want_write_file(filp);
625 if (err)
626 return err;
627
628 inode_lock(inode);
629 err = ext4_ioctl_setflags(inode, flags);
630 inode_unlock(inode);
631 mnt_drop_write_file(filp);
632 return err;
633 }
634 case EXT4_IOC_GETVERSION:
635 case EXT4_IOC_GETVERSION_OLD:
636 return put_user(inode->i_generation, (int __user *) arg);
637 case EXT4_IOC_SETVERSION:
638 case EXT4_IOC_SETVERSION_OLD: {
639 handle_t *handle;
640 struct ext4_iloc iloc;
641 __u32 generation;
642 int err;
643
644 if (!inode_owner_or_capable(inode))
645 return -EPERM;
646
647 if (ext4_has_metadata_csum(inode->i_sb)) {
648 ext4_warning(sb, "Setting inode version is not "
649 "supported with metadata_csum enabled.");
650 return -ENOTTY;
651 }
652
653 err = mnt_want_write_file(filp);
654 if (err)
655 return err;
656 if (get_user(generation, (int __user *) arg)) {
657 err = -EFAULT;
658 goto setversion_out;
659 }
660
661 inode_lock(inode);
662 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
663 if (IS_ERR(handle)) {
664 err = PTR_ERR(handle);
665 goto unlock_out;
666 }
667 err = ext4_reserve_inode_write(handle, inode, &iloc);
668 if (err == 0) {
669 inode->i_ctime = current_time(inode);
670 inode->i_generation = generation;
671 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
672 }
673 ext4_journal_stop(handle);
674
675 unlock_out:
676 inode_unlock(inode);
677 setversion_out:
678 mnt_drop_write_file(filp);
679 return err;
680 }
681 case EXT4_IOC_GROUP_EXTEND: {
682 ext4_fsblk_t n_blocks_count;
683 int err, err2=0;
684
685 err = ext4_resize_begin(sb);
686 if (err)
687 return err;
688
689 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
690 err = -EFAULT;
691 goto group_extend_out;
692 }
693
694 if (ext4_has_feature_bigalloc(sb)) {
695 ext4_msg(sb, KERN_ERR,
696 "Online resizing not supported with bigalloc");
697 err = -EOPNOTSUPP;
698 goto group_extend_out;
699 }
700
701 err = mnt_want_write_file(filp);
702 if (err)
703 goto group_extend_out;
704
705 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
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 }
711 if (err == 0)
712 err = err2;
713 mnt_drop_write_file(filp);
714 group_extend_out:
715 ext4_resize_end(sb);
716 return err;
717 }
718
719 case EXT4_IOC_MOVE_EXT: {
720 struct move_extent me;
721 struct fd donor;
722 int err;
723
724 if (!(filp->f_mode & FMODE_READ) ||
725 !(filp->f_mode & FMODE_WRITE))
726 return -EBADF;
727
728 if (copy_from_user(&me,
729 (struct move_extent __user *)arg, sizeof(me)))
730 return -EFAULT;
731 me.moved_len = 0;
732
733 donor = fdget(me.donor_fd);
734 if (!donor.file)
735 return -EBADF;
736
737 if (!(donor.file->f_mode & FMODE_WRITE)) {
738 err = -EBADF;
739 goto mext_out;
740 }
741
742 if (ext4_has_feature_bigalloc(sb)) {
743 ext4_msg(sb, KERN_ERR,
744 "Online defrag not supported with bigalloc");
745 err = -EOPNOTSUPP;
746 goto mext_out;
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;
752 }
753
754 err = mnt_want_write_file(filp);
755 if (err)
756 goto mext_out;
757
758 err = ext4_move_extents(filp, donor.file, me.orig_start,
759 me.donor_start, me.len, &me.moved_len);
760 mnt_drop_write_file(filp);
761
762 if (copy_to_user((struct move_extent __user *)arg,
763 &me, sizeof(me)))
764 err = -EFAULT;
765 mext_out:
766 fdput(donor);
767 return err;
768 }
769
770 case EXT4_IOC_GROUP_ADD: {
771 struct ext4_new_group_data input;
772 int err, err2=0;
773
774 err = ext4_resize_begin(sb);
775 if (err)
776 return err;
777
778 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
779 sizeof(input))) {
780 err = -EFAULT;
781 goto group_add_out;
782 }
783
784 if (ext4_has_feature_bigalloc(sb)) {
785 ext4_msg(sb, KERN_ERR,
786 "Online resizing not supported with bigalloc");
787 err = -EOPNOTSUPP;
788 goto group_add_out;
789 }
790
791 err = mnt_want_write_file(filp);
792 if (err)
793 goto group_add_out;
794
795 err = ext4_group_add(sb, &input);
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 }
801 if (err == 0)
802 err = err2;
803 mnt_drop_write_file(filp);
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);
807 group_add_out:
808 ext4_resize_end(sb);
809 return err;
810 }
811
812 case EXT4_IOC_MIGRATE:
813 {
814 int err;
815 if (!inode_owner_or_capable(inode))
816 return -EACCES;
817
818 err = mnt_want_write_file(filp);
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 */
827 inode_lock((inode));
828 err = ext4_ext_migrate(inode);
829 inode_unlock((inode));
830 mnt_drop_write_file(filp);
831 return err;
832 }
833
834 case EXT4_IOC_ALLOC_DA_BLKS:
835 {
836 int err;
837 if (!inode_owner_or_capable(inode))
838 return -EACCES;
839
840 err = mnt_want_write_file(filp);
841 if (err)
842 return err;
843 err = ext4_alloc_da_blocks(inode);
844 mnt_drop_write_file(filp);
845 return err;
846 }
847
848 case EXT4_IOC_SWAP_BOOT:
849 {
850 int err;
851 if (!(filp->f_mode & FMODE_WRITE))
852 return -EBADF;
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 }
860
861 case EXT4_IOC_RESIZE_FS: {
862 ext4_fsblk_t n_blocks_count;
863 int err = 0, err2 = 0;
864 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
865
866 if (ext4_has_feature_bigalloc(sb)) {
867 ext4_msg(sb, KERN_ERR,
868 "Online resizing not (yet) supported with bigalloc");
869 return -EOPNOTSUPP;
870 }
871
872 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
873 sizeof(__u64))) {
874 return -EFAULT;
875 }
876
877 err = ext4_resize_begin(sb);
878 if (err)
879 return err;
880
881 err = mnt_want_write_file(filp);
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;
893 mnt_drop_write_file(filp);
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
899 resizefs_out:
900 ext4_resize_end(sb);
901 return err;
902 }
903
904 case FITRIM:
905 {
906 struct request_queue *q = bdev_get_queue(sb->s_bdev);
907 struct fstrim_range range;
908 int ret = 0;
909
910 if (!ns_capable(sb->s_user_ns, CAP_SYS_ADMIN))
911 return -EPERM;
912
913 if (!blk_queue_discard(q))
914 return -EOPNOTSUPP;
915
916 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
917 sizeof(range)))
918 return -EFAULT;
919
920 range.minlen = max((unsigned int)range.minlen,
921 q->limits.discard_granularity);
922 ret = ext4_trim_fs(sb, &range);
923 if (ret < 0)
924 return ret;
925
926 if (copy_to_user((struct fstrim_range __user *)arg, &range,
927 sizeof(range)))
928 return -EFAULT;
929
930 return 0;
931 }
932 case EXT4_IOC_PRECACHE_EXTENTS:
933 return ext4_ext_precache(inode);
934
935 case EXT4_IOC_SET_ENCRYPTION_POLICY:
936 if (!ext4_has_feature_encrypt(sb))
937 return -EOPNOTSUPP;
938 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
939
940 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
941 #ifdef CONFIG_EXT4_FS_ENCRYPTION
942 int err, err2;
943 struct ext4_sb_info *sbi = EXT4_SB(sb);
944 handle_t *handle;
945
946 if (!ext4_has_feature_encrypt(sb))
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 }
972 if (copy_to_user((void __user *) arg,
973 sbi->s_es->s_encrypt_pw_salt, 16))
974 return -EFAULT;
975 return 0;
976 #else
977 return -EOPNOTSUPP;
978 #endif
979 }
980 case EXT4_IOC_GET_ENCRYPTION_POLICY:
981 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
982
983 case EXT4_IOC_FSGETXATTR:
984 {
985 struct fsxattr fa;
986
987 memset(&fa, 0, sizeof(struct fsxattr));
988 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
989
990 if (ext4_has_feature_project(inode->i_sb)) {
991 fa.fsx_projid = (__u32)from_kprojid_munged(sb->s_user_ns,
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
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
1020 err = mnt_want_write_file(filp);
1021 if (err)
1022 return err;
1023
1024 inode_lock(inode);
1025 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1026 (flags & EXT4_FL_XFLAG_VISIBLE);
1027 err = ext4_ioctl_setflags(inode, flags);
1028 inode_unlock(inode);
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 }
1039 case EXT4_IOC_SHUTDOWN:
1040 return ext4_shutdown(sb, arg);
1041 default:
1042 return -ENOTTY;
1043 }
1044 }
1045
1046 #ifdef CONFIG_COMPAT
1047 long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1048 {
1049 /* These are just misnamed, they actually get/put from/to user an int */
1050 switch (cmd) {
1051 case EXT4_IOC32_GETFLAGS:
1052 cmd = EXT4_IOC_GETFLAGS;
1053 break;
1054 case EXT4_IOC32_SETFLAGS:
1055 cmd = EXT4_IOC_SETFLAGS;
1056 break;
1057 case EXT4_IOC32_GETVERSION:
1058 cmd = EXT4_IOC_GETVERSION;
1059 break;
1060 case EXT4_IOC32_SETVERSION:
1061 cmd = EXT4_IOC_SETVERSION;
1062 break;
1063 case EXT4_IOC32_GROUP_EXTEND:
1064 cmd = EXT4_IOC_GROUP_EXTEND;
1065 break;
1066 case EXT4_IOC32_GETVERSION_OLD:
1067 cmd = EXT4_IOC_GETVERSION_OLD;
1068 break;
1069 case EXT4_IOC32_SETVERSION_OLD:
1070 cmd = EXT4_IOC_SETVERSION_OLD;
1071 break;
1072 case EXT4_IOC32_GETRSVSZ:
1073 cmd = EXT4_IOC_GETRSVSZ;
1074 break;
1075 case EXT4_IOC32_SETRSVSZ:
1076 cmd = EXT4_IOC_SETRSVSZ;
1077 break;
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 }
1101 case EXT4_IOC_MOVE_EXT:
1102 case EXT4_IOC_RESIZE_FS:
1103 case EXT4_IOC_PRECACHE_EXTENTS:
1104 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1105 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1106 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1107 case EXT4_IOC_SHUTDOWN:
1108 case FS_IOC_GETFSMAP:
1109 break;
1110 default:
1111 return -ENOIOCTLCMD;
1112 }
1113 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1114 }
1115 #endif