blob: 23a4766f66783a7a1637b064e6dadf7fd99b7b93 [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/ioctl.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
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>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070011#include <linux/capability.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070012#include <linux/time.h>
13#include <linux/compat.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080014#include <linux/mount.h>
Akira Fujita748de672009-06-17 19:24:03 -040015#include <linux/file.h>
Li Xi9b7365f2016-01-08 16:01:22 -050016#include <linux/quotaops.h>
Theodore Ts'o23253062017-11-08 22:23:20 -050017#include <linux/random.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070018#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Theodore Ts'o783d9482017-02-05 19:47:14 -050020#include <linux/delay.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040021#include "ext4_jbd2.h"
22#include "ext4.h"
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -040023#include <linux/fsmap.h>
24#include "fsmap.h"
25#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070026
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040027/**
28 * Swap memory between @a and @b for @len bytes.
29 *
30 * @a: pointer to first memory area
31 * @b: pointer to second memory area
32 * @len: number of bytes to swap
33 *
34 */
35static void memswap(void *a, void *b, size_t len)
36{
37 unsigned char *ap, *bp;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040038
39 ap = (unsigned char *)a;
40 bp = (unsigned char *)b;
41 while (len-- > 0) {
Fabian Frederick4b7e2db2015-06-12 23:46:33 -040042 swap(*ap, *bp);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040043 ap++;
44 bp++;
45 }
46}
47
48/**
49 * Swap i_data and associated attributes between @inode1 and @inode2.
50 * This function is used for the primary swap between inode1 and inode2
51 * and also to revert this primary swap in case of errors.
52 *
53 * Therefore you have to make sure, that calling this method twice
54 * will revert all changes.
55 *
56 * @inode1: pointer to first inode
57 * @inode2: pointer to second inode
58 */
59static void swap_inode_data(struct inode *inode1, struct inode *inode2)
60{
61 loff_t isize;
62 struct ext4_inode_info *ei1;
63 struct ext4_inode_info *ei2;
64
65 ei1 = EXT4_I(inode1);
66 ei2 = EXT4_I(inode2);
67
Jeff Layton9c5d58f2017-07-31 00:55:34 -040068 swap(inode1->i_flags, inode2->i_flags);
69 swap(inode1->i_version, inode2->i_version);
70 swap(inode1->i_blocks, inode2->i_blocks);
71 swap(inode1->i_bytes, inode2->i_bytes);
72 swap(inode1->i_atime, inode2->i_atime);
73 swap(inode1->i_mtime, inode2->i_mtime);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040074
75 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
Jeff Layton9c5d58f2017-07-31 00:55:34 -040076 swap(ei1->i_flags, ei2->i_flags);
77 swap(ei1->i_disksize, ei2->i_disksize);
Theodore Ts'ocde2d7a2013-08-12 09:29:30 -040078 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
79 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040080
81 isize = i_size_read(inode1);
82 i_size_write(inode1, i_size_read(inode2));
83 i_size_write(inode2, isize);
84}
85
86/**
87 * Swap the information from the given @inode and the inode
88 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
89 * important fields of the inodes.
90 *
91 * @sb: the super block of the filesystem
92 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
93 *
94 */
95static long swap_inode_boot_loader(struct super_block *sb,
96 struct inode *inode)
97{
98 handle_t *handle;
99 int err;
100 struct inode *inode_bl;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400101 struct ext4_inode_info *ei_bl;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400102
Theodore Ts'od8558a22014-02-17 20:44:36 -0500103 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
104 return -EINVAL;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400105
Theodore Ts'od8558a22014-02-17 20:44:36 -0500106 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
107 return -EPERM;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400108
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400109 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
Theodore Ts'od8558a22014-02-17 20:44:36 -0500110 if (IS_ERR(inode_bl))
111 return PTR_ERR(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400112 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. */
J. Bruce Fields375e2892012-04-18 15:16:33 -0400119 lock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400120
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;
Zheng Liu30d29b12014-02-12 11:48:31 -0500133 goto journal_err_out;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400134 }
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;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400149 if (ext4_has_feature_extents(sb)) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400150 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
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500158 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400159
Theodore Ts'o23253062017-11-08 22:23:20 -0500160 inode->i_generation = prandom_u32();
161 inode_bl->i_generation = prandom_u32();
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400162
163 ext4_discard_preallocations(inode);
164
165 err = ext4_mark_inode_dirty(handle, inode);
166 if (err < 0) {
167 ext4_warning(inode->i_sb,
168 "couldn't mark inode #%lu dirty (err %d)",
169 inode->i_ino, err);
170 /* Revert all changes: */
171 swap_inode_data(inode, inode_bl);
172 } else {
173 err = ext4_mark_inode_dirty(handle, inode_bl);
174 if (err < 0) {
175 ext4_warning(inode_bl->i_sb,
176 "couldn't mark inode #%lu dirty (err %d)",
177 inode_bl->i_ino, err);
178 /* Revert all changes: */
179 swap_inode_data(inode, inode_bl);
180 ext4_mark_inode_dirty(handle, inode);
181 }
182 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400183 ext4_journal_stop(handle);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400184 ext4_double_up_write_data_sem(inode, inode_bl);
185
Zheng Liu30d29b12014-02-12 11:48:31 -0500186journal_err_out:
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400187 ext4_inode_resume_unlocked_dio(inode);
188 ext4_inode_resume_unlocked_dio(inode_bl);
J. Bruce Fields375e2892012-04-18 15:16:33 -0400189 unlock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400190 iput(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400191 return err;
192}
193
Eric Biggersba679012016-12-01 11:55:51 -0500194#ifdef CONFIG_EXT4_FS_ENCRYPTION
Michael Halcrow9bd82122015-04-11 07:48:01 -0400195static int uuid_is_zero(__u8 u[16])
196{
197 int i;
198
199 for (i = 0; i < 16; i++)
200 if (u[i])
201 return 0;
202 return 1;
203}
Eric Biggersba679012016-12-01 11:55:51 -0500204#endif
Michael Halcrow9bd82122015-04-11 07:48:01 -0400205
Li Xi9b7365f2016-01-08 16:01:22 -0500206static int ext4_ioctl_setflags(struct inode *inode,
207 unsigned int flags)
208{
209 struct ext4_inode_info *ei = EXT4_I(inode);
210 handle_t *handle = NULL;
Anton Protopopovfdde3682016-02-11 23:57:21 -0500211 int err = -EPERM, migrate = 0;
Li Xi9b7365f2016-01-08 16:01:22 -0500212 struct ext4_iloc iloc;
213 unsigned int oldflags, mask, i;
214 unsigned int jflag;
215
216 /* Is it quota file? Do not allow user to mess with it */
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400217 if (ext4_is_quota_file(inode))
Li Xi9b7365f2016-01-08 16:01:22 -0500218 goto flags_out;
219
220 oldflags = ei->i_flags;
221
222 /* The JOURNAL_DATA flag is modifiable only by root */
223 jflag = flags & EXT4_JOURNAL_DATA_FL;
224
225 /*
226 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
227 * the relevant capability.
228 *
229 * This test looks nicer. Thanks to Pauline Middelink
230 */
231 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
232 if (!capable(CAP_LINUX_IMMUTABLE))
233 goto flags_out;
234 }
235
236 /*
237 * The JOURNAL_DATA flag can only be changed by
238 * the relevant capability.
239 */
240 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
241 if (!capable(CAP_SYS_RESOURCE))
242 goto flags_out;
243 }
244 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
245 migrate = 1;
246
247 if (flags & EXT4_EOFBLOCKS_FL) {
248 /* we don't support adding EOFBLOCKS flag */
249 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
250 err = -EOPNOTSUPP;
251 goto flags_out;
252 }
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500253 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
254 err = ext4_truncate(inode);
255 if (err)
256 goto flags_out;
257 }
Li Xi9b7365f2016-01-08 16:01:22 -0500258
259 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
260 if (IS_ERR(handle)) {
261 err = PTR_ERR(handle);
262 goto flags_out;
263 }
264 if (IS_SYNC(inode))
265 ext4_handle_sync(handle);
266 err = ext4_reserve_inode_write(handle, inode, &iloc);
267 if (err)
268 goto flags_err;
269
270 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
271 if (!(mask & EXT4_FL_USER_MODIFIABLE))
272 continue;
Jan Karaf8011d92016-11-29 11:13:13 -0500273 /* These flags get special treatment later */
274 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
275 continue;
Li Xi9b7365f2016-01-08 16:01:22 -0500276 if (mask & flags)
277 ext4_set_inode_flag(inode, i);
278 else
279 ext4_clear_inode_flag(inode, i);
280 }
281
282 ext4_set_inode_flags(inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500283 inode->i_ctime = current_time(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500284
285 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
286flags_err:
287 ext4_journal_stop(handle);
288 if (err)
289 goto flags_out;
290
Ross Zwislere9072d82017-10-12 11:54:08 -0400291 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
292 /*
293 * Changes to the journaling mode can cause unsafe changes to
294 * S_DAX if we are using the DAX mount option.
295 */
296 if (test_opt(inode->i_sb, DAX)) {
297 err = -EBUSY;
298 goto flags_out;
299 }
300
Li Xi9b7365f2016-01-08 16:01:22 -0500301 err = ext4_change_inode_journal_flag(inode, jflag);
Ross Zwislere9072d82017-10-12 11:54:08 -0400302 if (err)
303 goto flags_out;
304 }
Li Xi9b7365f2016-01-08 16:01:22 -0500305 if (migrate) {
306 if (flags & EXT4_EXTENTS_FL)
307 err = ext4_ext_migrate(inode);
308 else
309 err = ext4_ind_migrate(inode);
310 }
311
312flags_out:
313 return err;
314}
315
316#ifdef CONFIG_QUOTA
317static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
318{
319 struct inode *inode = file_inode(filp);
320 struct super_block *sb = inode->i_sb;
321 struct ext4_inode_info *ei = EXT4_I(inode);
322 int err, rc;
323 handle_t *handle;
324 kprojid_t kprojid;
325 struct ext4_iloc iloc;
326 struct ext4_inode *raw_inode;
Wang Shilong079788d2016-07-05 21:33:52 -0400327 struct dquot *transfer_to[MAXQUOTAS] = { };
Li Xi9b7365f2016-01-08 16:01:22 -0500328
Kaho Ng0b7b7772016-09-05 23:11:58 -0400329 if (!ext4_has_feature_project(sb)) {
Li Xi9b7365f2016-01-08 16:01:22 -0500330 if (projid != EXT4_DEF_PROJID)
331 return -EOPNOTSUPP;
332 else
333 return 0;
334 }
335
336 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
337 return -EOPNOTSUPP;
338
339 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
340
341 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
342 return 0;
343
344 err = mnt_want_write_file(filp);
345 if (err)
346 return err;
347
348 err = -EPERM;
Al Viro59551022016-01-22 15:40:57 -0500349 inode_lock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500350 /* Is it quota file? Do not allow user to mess with it */
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400351 if (ext4_is_quota_file(inode))
Li Xi9b7365f2016-01-08 16:01:22 -0500352 goto out_unlock;
353
354 err = ext4_get_inode_loc(inode, &iloc);
355 if (err)
356 goto out_unlock;
357
358 raw_inode = ext4_raw_inode(&iloc);
359 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
Miao Xiec03b45b2017-08-06 01:00:49 -0400360 err = ext4_expand_extra_isize(inode,
361 EXT4_SB(sb)->s_want_extra_isize,
362 &iloc);
363 if (err)
364 goto out_unlock;
365 } else {
Li Xi9b7365f2016-01-08 16:01:22 -0500366 brelse(iloc.bh);
Li Xi9b7365f2016-01-08 16:01:22 -0500367 }
Li Xi9b7365f2016-01-08 16:01:22 -0500368
369 dquot_initialize(inode);
370
371 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
372 EXT4_QUOTA_INIT_BLOCKS(sb) +
373 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
374 if (IS_ERR(handle)) {
375 err = PTR_ERR(handle);
376 goto out_unlock;
377 }
378
379 err = ext4_reserve_inode_write(handle, inode, &iloc);
380 if (err)
381 goto out_stop;
382
Wang Shilong079788d2016-07-05 21:33:52 -0400383 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
384 if (!IS_ERR(transfer_to[PRJQUOTA])) {
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400385
386 /* __dquot_transfer() calls back ext4_get_inode_usage() which
387 * counts xattr inode references.
388 */
389 down_read(&EXT4_I(inode)->xattr_sem);
Wang Shilong079788d2016-07-05 21:33:52 -0400390 err = __dquot_transfer(inode, transfer_to);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400391 up_read(&EXT4_I(inode)->xattr_sem);
Wang Shilong079788d2016-07-05 21:33:52 -0400392 dqput(transfer_to[PRJQUOTA]);
393 if (err)
394 goto out_dirty;
Li Xi9b7365f2016-01-08 16:01:22 -0500395 }
Wang Shilong079788d2016-07-05 21:33:52 -0400396
Li Xi9b7365f2016-01-08 16:01:22 -0500397 EXT4_I(inode)->i_projid = kprojid;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500398 inode->i_ctime = current_time(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500399out_dirty:
400 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
401 if (!err)
402 err = rc;
403out_stop:
404 ext4_journal_stop(handle);
405out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500406 inode_unlock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500407 mnt_drop_write_file(filp);
408 return err;
409}
410#else
411static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
412{
413 if (projid != EXT4_DEF_PROJID)
414 return -EOPNOTSUPP;
415 return 0;
416}
417#endif
418
419/* Transfer internal flags to xflags */
420static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
421{
422 __u32 xflags = 0;
423
424 if (iflags & EXT4_SYNC_FL)
425 xflags |= FS_XFLAG_SYNC;
426 if (iflags & EXT4_IMMUTABLE_FL)
427 xflags |= FS_XFLAG_IMMUTABLE;
428 if (iflags & EXT4_APPEND_FL)
429 xflags |= FS_XFLAG_APPEND;
430 if (iflags & EXT4_NODUMP_FL)
431 xflags |= FS_XFLAG_NODUMP;
432 if (iflags & EXT4_NOATIME_FL)
433 xflags |= FS_XFLAG_NOATIME;
434 if (iflags & EXT4_PROJINHERIT_FL)
435 xflags |= FS_XFLAG_PROJINHERIT;
436 return xflags;
437}
438
Jan Karad14e7682016-11-29 11:18:39 -0500439#define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
440 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
441 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
442
Li Xi9b7365f2016-01-08 16:01:22 -0500443/* Transfer xflags flags to internal */
444static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
445{
446 unsigned long iflags = 0;
447
448 if (xflags & FS_XFLAG_SYNC)
449 iflags |= EXT4_SYNC_FL;
450 if (xflags & FS_XFLAG_IMMUTABLE)
451 iflags |= EXT4_IMMUTABLE_FL;
452 if (xflags & FS_XFLAG_APPEND)
453 iflags |= EXT4_APPEND_FL;
454 if (xflags & FS_XFLAG_NODUMP)
455 iflags |= EXT4_NODUMP_FL;
456 if (xflags & FS_XFLAG_NOATIME)
457 iflags |= EXT4_NOATIME_FL;
458 if (xflags & FS_XFLAG_PROJINHERIT)
459 iflags |= EXT4_PROJINHERIT_FL;
460
461 return iflags;
462}
463
Eric Biggers1a20a6302017-04-30 00:40:44 -0400464static int ext4_shutdown(struct super_block *sb, unsigned long arg)
Theodore Ts'o783d9482017-02-05 19:47:14 -0500465{
466 struct ext4_sb_info *sbi = EXT4_SB(sb);
467 __u32 flags;
468
469 if (!capable(CAP_SYS_ADMIN))
470 return -EPERM;
471
472 if (get_user(flags, (__u32 __user *)arg))
473 return -EFAULT;
474
475 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
476 return -EINVAL;
477
478 if (ext4_forced_shutdown(sbi))
479 return 0;
480
481 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
482
483 switch (flags) {
484 case EXT4_GOING_FLAGS_DEFAULT:
485 freeze_bdev(sb->s_bdev);
486 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
487 thaw_bdev(sb->s_bdev, sb);
488 break;
489 case EXT4_GOING_FLAGS_LOGFLUSH:
490 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
491 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
492 (void) ext4_force_commit(sb);
493 jbd2_journal_abort(sbi->s_journal, 0);
494 }
495 break;
496 case EXT4_GOING_FLAGS_NOLOGFLUSH:
497 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
498 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
499 msleep(100);
500 jbd2_journal_abort(sbi->s_journal, 0);
501 }
502 break;
503 default:
504 return -EINVAL;
505 }
506 clear_opt(sb, DISCARD);
507 return 0;
508}
509
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -0400510struct getfsmap_info {
511 struct super_block *gi_sb;
512 struct fsmap_head __user *gi_data;
513 unsigned int gi_idx;
514 __u32 gi_last_flags;
515};
516
517static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
518{
519 struct getfsmap_info *info = priv;
520 struct fsmap fm;
521
522 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
523
524 info->gi_last_flags = xfm->fmr_flags;
525 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
526 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
527 sizeof(struct fsmap)))
528 return -EFAULT;
529
530 return 0;
531}
532
533static int ext4_ioc_getfsmap(struct super_block *sb,
534 struct fsmap_head __user *arg)
535{
536 struct getfsmap_info info = {0};
537 struct ext4_fsmap_head xhead = {0};
538 struct fsmap_head head;
539 bool aborted = false;
540 int error;
541
542 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
543 return -EFAULT;
544 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
545 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
546 sizeof(head.fmh_keys[0].fmr_reserved)) ||
547 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
548 sizeof(head.fmh_keys[1].fmr_reserved)))
549 return -EINVAL;
550 /*
551 * ext4 doesn't report file extents at all, so the only valid
552 * file offsets are the magic ones (all zeroes or all ones).
553 */
554 if (head.fmh_keys[0].fmr_offset ||
555 (head.fmh_keys[1].fmr_offset != 0 &&
556 head.fmh_keys[1].fmr_offset != -1ULL))
557 return -EINVAL;
558
559 xhead.fmh_iflags = head.fmh_iflags;
560 xhead.fmh_count = head.fmh_count;
561 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
562 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
563
564 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
565 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
566
567 info.gi_sb = sb;
568 info.gi_data = arg;
569 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
570 if (error == EXT4_QUERY_RANGE_ABORT) {
571 error = 0;
572 aborted = true;
573 } else if (error)
574 return error;
575
576 /* If we didn't abort, set the "last" flag in the last fmx */
577 if (!aborted && info.gi_idx) {
578 info.gi_last_flags |= FMR_OF_LAST;
579 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
580 &info.gi_last_flags,
581 sizeof(info.gi_last_flags)))
582 return -EFAULT;
583 }
584
585 /* copy back header */
586 head.fmh_entries = xhead.fmh_entries;
587 head.fmh_oflags = xhead.fmh_oflags;
588 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
589 return -EFAULT;
590
591 return 0;
592}
593
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400594long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700595{
Al Viro496ad9a2013-01-23 17:07:38 -0500596 struct inode *inode = file_inode(filp);
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400597 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700598 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700599 unsigned int flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400601 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700602
603 switch (cmd) {
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -0400604 case FS_IOC_GETFSMAP:
605 return ext4_ioc_getfsmap(sb, (void __user *)arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700606 case EXT4_IOC_GETFLAGS:
607 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608 return put_user(flags, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700609 case EXT4_IOC_SETFLAGS: {
Li Xi9b7365f2016-01-08 16:01:22 -0500610 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700612 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700613 return -EACCES;
614
615 if (get_user(flags, (int __user *) arg))
616 return -EFAULT;
617
Jan Karad14e7682016-11-29 11:18:39 -0500618 if (flags & ~EXT4_FL_USER_VISIBLE)
619 return -EOPNOTSUPP;
620 /*
621 * chattr(1) grabs flags via GETFLAGS, modifies the result and
622 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
623 * more restrictive than just silently masking off visible but
624 * not settable flags as we always did.
625 */
626 flags &= EXT4_FL_USER_MODIFIABLE;
627 if (ext4_mask_flags(inode->i_mode, flags) != flags)
628 return -EOPNOTSUPP;
629
Al Viroa561be72011-11-23 11:57:51 -0500630 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800631 if (err)
632 return err;
633
Al Viro59551022016-01-22 15:40:57 -0500634 inode_lock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500635 err = ext4_ioctl_setflags(inode, flags);
Al Viro59551022016-01-22 15:40:57 -0500636 inode_unlock(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500637 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638 return err;
639 }
Mingming Cao617ba132006-10-11 01:20:53 -0700640 case EXT4_IOC_GETVERSION:
641 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700642 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700643 case EXT4_IOC_SETVERSION:
644 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700645 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700646 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647 __u32 generation;
648 int err;
649
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700650 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800652
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400653 if (ext4_has_metadata_csum(inode->i_sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -0400654 ext4_warning(sb, "Setting inode version is not "
655 "supported with metadata_csum enabled.");
656 return -ENOTTY;
657 }
658
Al Viroa561be72011-11-23 11:57:51 -0500659 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800660 if (err)
661 return err;
662 if (get_user(generation, (int __user *) arg)) {
663 err = -EFAULT;
664 goto setversion_out;
665 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700666
Al Viro59551022016-01-22 15:40:57 -0500667 inode_lock(inode);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500668 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800669 if (IS_ERR(handle)) {
670 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100671 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800672 }
Mingming Cao617ba132006-10-11 01:20:53 -0700673 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674 if (err == 0) {
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500675 inode->i_ctime = current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700677 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700678 }
Mingming Cao617ba132006-10-11 01:20:53 -0700679 ext4_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100680
681unlock_out:
Al Viro59551022016-01-22 15:40:57 -0500682 inode_unlock(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800683setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500684 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 return err;
686 }
Mingming Cao617ba132006-10-11 01:20:53 -0700687 case EXT4_IOC_GROUP_EXTEND: {
688 ext4_fsblk_t n_blocks_count;
Peng Taoac046f12009-07-13 09:30:17 -0400689 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700690
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400691 err = ext4_resize_begin(sb);
692 if (err)
693 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700694
Djalal Harouni014a1772012-01-04 17:09:52 -0500695 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
696 err = -EFAULT;
697 goto group_extend_out;
698 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400700 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400701 ext4_msg(sb, KERN_ERR,
702 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500703 err = -EOPNOTSUPP;
704 goto group_extend_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400705 }
706
Al Viroa561be72011-11-23 11:57:51 -0500707 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800708 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500709 goto group_extend_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800710
Mingming Cao617ba132006-10-11 01:20:53 -0700711 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Peng Taoac046f12009-07-13 09:30:17 -0400712 if (EXT4_SB(sb)->s_journal) {
713 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
714 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
715 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
716 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400717 if (err == 0)
718 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500719 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500720group_extend_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400721 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 return err;
723 }
Akira Fujita748de672009-06-17 19:24:03 -0400724
725 case EXT4_IOC_MOVE_EXT: {
726 struct move_extent me;
Al Viro2903ff02012-08-28 12:52:22 -0400727 struct fd donor;
728 int err;
Akira Fujita748de672009-06-17 19:24:03 -0400729
Akira Fujita4a585792009-12-06 23:38:31 -0500730 if (!(filp->f_mode & FMODE_READ) ||
731 !(filp->f_mode & FMODE_WRITE))
732 return -EBADF;
733
Akira Fujita748de672009-06-17 19:24:03 -0400734 if (copy_from_user(&me,
735 (struct move_extent __user *)arg, sizeof(me)))
736 return -EFAULT;
Akira Fujita4a585792009-12-06 23:38:31 -0500737 me.moved_len = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400738
Al Viro2903ff02012-08-28 12:52:22 -0400739 donor = fdget(me.donor_fd);
740 if (!donor.file)
Akira Fujita748de672009-06-17 19:24:03 -0400741 return -EBADF;
742
Al Viro2903ff02012-08-28 12:52:22 -0400743 if (!(donor.file->f_mode & FMODE_WRITE)) {
Akira Fujita4a585792009-12-06 23:38:31 -0500744 err = -EBADF;
745 goto mext_out;
Akira Fujita748de672009-06-17 19:24:03 -0400746 }
747
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400748 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400749 ext4_msg(sb, KERN_ERR,
750 "Online defrag not supported with bigalloc");
Al Viro399c9b82012-08-26 21:00:03 -0400751 err = -EOPNOTSUPP;
752 goto mext_out;
Ross Zwisler73f34a52016-02-26 15:19:49 -0800753 } else if (IS_DAX(inode)) {
754 ext4_msg(sb, KERN_ERR,
755 "Online defrag not supported with DAX");
756 err = -EOPNOTSUPP;
757 goto mext_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400758 }
759
Al Viroa561be72011-11-23 11:57:51 -0500760 err = mnt_want_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500761 if (err)
762 goto mext_out;
763
Al Viro2903ff02012-08-28 12:52:22 -0400764 err = ext4_move_extents(filp, donor.file, me.orig_start,
Akira Fujita748de672009-06-17 19:24:03 -0400765 me.donor_start, me.len, &me.moved_len);
Al Viro2a79f172011-12-09 08:06:57 -0500766 mnt_drop_write_file(filp);
Akira Fujita748de672009-06-17 19:24:03 -0400767
Theodore Ts'o60e66792010-05-17 07:00:00 -0400768 if (copy_to_user((struct move_extent __user *)arg,
Akira Fujitac437b272010-03-04 00:39:24 -0500769 &me, sizeof(me)))
Akira Fujita4a585792009-12-06 23:38:31 -0500770 err = -EFAULT;
771mext_out:
Al Viro2903ff02012-08-28 12:52:22 -0400772 fdput(donor);
Akira Fujita748de672009-06-17 19:24:03 -0400773 return err;
774 }
775
Mingming Cao617ba132006-10-11 01:20:53 -0700776 case EXT4_IOC_GROUP_ADD: {
777 struct ext4_new_group_data input;
Peng Taoac046f12009-07-13 09:30:17 -0400778 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400780 err = ext4_resize_begin(sb);
781 if (err)
782 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783
Mingming Cao617ba132006-10-11 01:20:53 -0700784 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Djalal Harouni014a1772012-01-04 17:09:52 -0500785 sizeof(input))) {
786 err = -EFAULT;
787 goto group_add_out;
788 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400790 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400791 ext4_msg(sb, KERN_ERR,
792 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500793 err = -EOPNOTSUPP;
794 goto group_add_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400795 }
796
Al Viroa561be72011-11-23 11:57:51 -0500797 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800798 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500799 goto group_add_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800800
Mingming Cao617ba132006-10-11 01:20:53 -0700801 err = ext4_group_add(sb, &input);
Peng Taoac046f12009-07-13 09:30:17 -0400802 if (EXT4_SB(sb)->s_journal) {
803 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
804 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
805 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
806 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400807 if (err == 0)
808 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500809 mnt_drop_write_file(filp);
Theodore Ts'o7f511862013-01-13 08:41:45 -0500810 if (!err && ext4_has_group_desc_csum(sb) &&
811 test_opt(sb, INIT_INODE_TABLE))
812 err = ext4_register_li_request(sb, input.group);
Djalal Harouni014a1772012-01-04 17:09:52 -0500813group_add_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400814 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700815 return err;
816 }
817
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500818 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400819 {
820 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700821 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400822 return -EACCES;
823
Al Viroa561be72011-11-23 11:57:51 -0500824 err = mnt_want_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400825 if (err)
826 return err;
827 /*
828 * inode_mutex prevent write and truncate on the file.
829 * Read still goes through. We take i_data_sem in
830 * ext4_ext_swap_inode_data before we switch the
831 * inode format to prevent read.
832 */
Al Viro59551022016-01-22 15:40:57 -0500833 inode_lock((inode));
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400834 err = ext4_ext_migrate(inode);
Al Viro59551022016-01-22 15:40:57 -0500835 inode_unlock((inode));
Al Viro2a79f172011-12-09 08:06:57 -0500836 mnt_drop_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400837 return err;
838 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500839
Theodore Ts'occd25062009-02-26 01:04:07 -0500840 case EXT4_IOC_ALLOC_DA_BLKS:
841 {
842 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700843 if (!inode_owner_or_capable(inode))
Theodore Ts'occd25062009-02-26 01:04:07 -0500844 return -EACCES;
845
Al Viroa561be72011-11-23 11:57:51 -0500846 err = mnt_want_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500847 if (err)
848 return err;
849 err = ext4_alloc_da_blocks(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500850 mnt_drop_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500851 return err;
852 }
853
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400854 case EXT4_IOC_SWAP_BOOT:
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400855 {
856 int err;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400857 if (!(filp->f_mode & FMODE_WRITE))
858 return -EBADF;
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400859 err = mnt_want_write_file(filp);
860 if (err)
861 return err;
862 err = swap_inode_boot_loader(sb, inode);
863 mnt_drop_write_file(filp);
864 return err;
865 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400866
Yongqiang Yang19c52462012-01-04 17:09:44 -0500867 case EXT4_IOC_RESIZE_FS: {
868 ext4_fsblk_t n_blocks_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500869 int err = 0, err2 = 0;
Theodore Ts'o7f511862013-01-13 08:41:45 -0500870 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500871
Yongqiang Yang19c52462012-01-04 17:09:44 -0500872 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
873 sizeof(__u64))) {
874 return -EFAULT;
875 }
876
Yongqiang Yang19c52462012-01-04 17:09:44 -0500877 err = ext4_resize_begin(sb);
878 if (err)
879 return err;
880
Al Viro8cae6f72012-07-19 11:19:07 +0400881 err = mnt_want_write_file(filp);
Yongqiang Yang19c52462012-01-04 17:09:44 -0500882 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;
Al Viro8cae6f72012-07-19 11:19:07 +0400893 mnt_drop_write_file(filp);
Theodore Ts'o7f511862013-01-13 08:41:45 -0500894 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
Yongqiang Yang19c52462012-01-04 17:09:44 -0500899resizefs_out:
900 ext4_resize_end(sb);
901 return err;
902 }
903
Lukas Czernere681c042010-11-19 21:47:07 -0500904 case FITRIM:
905 {
Lukas Czerner41431792011-02-23 12:42:32 -0500906 struct request_queue *q = bdev_get_queue(sb->s_bdev);
Lukas Czernere681c042010-11-19 21:47:07 -0500907 struct fstrim_range range;
908 int ret = 0;
909
910 if (!capable(CAP_SYS_ADMIN))
911 return -EPERM;
912
Lukas Czerner41431792011-02-23 12:42:32 -0500913 if (!blk_queue_discard(q))
914 return -EOPNOTSUPP;
915
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400916 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czernere681c042010-11-19 21:47:07 -0500917 sizeof(range)))
918 return -EFAULT;
919
Lukas Czerner5c2ed622011-02-23 17:49:51 -0500920 range.minlen = max((unsigned int)range.minlen,
921 q->limits.discard_granularity);
Lukas Czernere681c042010-11-19 21:47:07 -0500922 ret = ext4_trim_fs(sb, &range);
923 if (ret < 0)
924 return ret;
925
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400926 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czernere681c042010-11-19 21:47:07 -0500927 sizeof(range)))
928 return -EFAULT;
929
930 return 0;
931 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400932 case EXT4_IOC_PRECACHE_EXTENTS:
933 return ext4_ext_precache(inode);
Lukas Czernere681c042010-11-19 21:47:07 -0500934
Eric Biggersdb717d82016-11-26 19:07:49 -0500935 case EXT4_IOC_SET_ENCRYPTION_POLICY:
Richard Weinberger9a200d02016-09-30 01:49:55 -0400936 if (!ext4_has_feature_encrypt(sb))
937 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -0500938 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Richard Weinberger9a200d02016-09-30 01:49:55 -0400939
Michael Halcrow9bd82122015-04-11 07:48:01 -0400940 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
Eric Biggersba679012016-12-01 11:55:51 -0500941#ifdef CONFIG_EXT4_FS_ENCRYPTION
Michael Halcrow9bd82122015-04-11 07:48:01 -0400942 int err, err2;
943 struct ext4_sb_info *sbi = EXT4_SB(sb);
944 handle_t *handle;
945
Eric Biggers35997d12016-12-01 11:54:18 -0500946 if (!ext4_has_feature_encrypt(sb))
Michael Halcrow9bd82122015-04-11 07:48:01 -0400947 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 }
Fabian Frederickb4ab9e22015-06-08 12:23:21 -0400972 if (copy_to_user((void __user *) arg,
973 sbi->s_es->s_encrypt_pw_salt, 16))
Michael Halcrow9bd82122015-04-11 07:48:01 -0400974 return -EFAULT;
975 return 0;
Eric Biggersba679012016-12-01 11:55:51 -0500976#else
977 return -EOPNOTSUPP;
978#endif
Michael Halcrow9bd82122015-04-11 07:48:01 -0400979 }
Eric Biggersdb717d82016-11-26 19:07:49 -0500980 case EXT4_IOC_GET_ENCRYPTION_POLICY:
981 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Michael Halcrow9bd82122015-04-11 07:48:01 -0400982
Li Xi9b7365f2016-01-08 16:01:22 -0500983 case EXT4_IOC_FSGETXATTR:
984 {
985 struct fsxattr fa;
986
987 memset(&fa, 0, sizeof(struct fsxattr));
Li Xi9b7365f2016-01-08 16:01:22 -0500988 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
989
Kaho Ng0b7b7772016-09-05 23:11:58 -0400990 if (ext4_has_feature_project(inode->i_sb)) {
Li Xi9b7365f2016-01-08 16:01:22 -0500991 fa.fsx_projid = (__u32)from_kprojid(&init_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
Jan Karad14e7682016-11-29 11:18:39 -05001013 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
Li Xi9b7365f2016-01-08 16:01:22 -05001020 err = mnt_want_write_file(filp);
1021 if (err)
1022 return err;
1023
Al Viro59551022016-01-22 15:40:57 -05001024 inode_lock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -05001025 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1026 (flags & EXT4_FL_XFLAG_VISIBLE);
1027 err = ext4_ioctl_setflags(inode, flags);
Al Viro59551022016-01-22 15:40:57 -05001028 inode_unlock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -05001029 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 }
Theodore Ts'oe9be2ac2017-02-20 15:34:59 -05001039 case EXT4_IOC_SHUTDOWN:
1040 return ext4_shutdown(sb, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041 default:
1042 return -ENOTTY;
1043 }
1044}
1045
1046#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07001047long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001048{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001049 /* These are just misnamed, they actually get/put from/to user an int */
1050 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -07001051 case EXT4_IOC32_GETFLAGS:
1052 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001054 case EXT4_IOC32_SETFLAGS:
1055 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001057 case EXT4_IOC32_GETVERSION:
1058 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001059 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001060 case EXT4_IOC32_SETVERSION:
1061 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001063 case EXT4_IOC32_GROUP_EXTEND:
1064 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001065 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001066 case EXT4_IOC32_GETVERSION_OLD:
1067 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001068 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001069 case EXT4_IOC32_SETVERSION_OLD:
1070 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001072 case EXT4_IOC32_GETRSVSZ:
1073 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001074 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001075 case EXT4_IOC32_SETRSVSZ:
1076 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001077 break;
Ben Hutchings4d92dc02010-05-17 06:00:00 -04001078 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 }
Christian Borntraegerb684b2e2010-05-15 00:00:00 -04001101 case EXT4_IOC_MOVE_EXT:
Yongqiang Yang19c52462012-01-04 17:09:44 -05001102 case EXT4_IOC_RESIZE_FS:
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001103 case EXT4_IOC_PRECACHE_EXTENTS:
Michael Halcrow9bd82122015-04-11 07:48:01 -04001104 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1105 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1106 case EXT4_IOC_GET_ENCRYPTION_POLICY:
Theodore Ts'oe9be2ac2017-02-20 15:34:59 -05001107 case EXT4_IOC_SHUTDOWN:
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -04001108 case FS_IOC_GETFSMAP:
Christian Borntraegerb684b2e2010-05-15 00:00:00 -04001109 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001110 default:
1111 return -ENOIOCTLCMD;
1112 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -04001113 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114}
1115#endif