blob: 597e8b617f92f32e40331c12e0ad19adbb81e0ef [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/ioctl.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 */
10
11#include <linux/fs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070012#include <linux/capability.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070013#include <linux/time.h>
14#include <linux/compat.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080015#include <linux/mount.h>
Akira Fujita748de672009-06-17 19:24:03 -040016#include <linux/file.h>
Li Xi9b7365f2016-01-08 16:01:22 -050017#include <linux/quotaops.h>
Theodore Ts'o23253062017-11-08 22:23:20 -050018#include <linux/random.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070019#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h>
Theodore Ts'o783d9482017-02-05 19:47:14 -050021#include <linux/delay.h>
Jeff Laytonee73f9a2018-01-09 08:21:39 -050022#include <linux/iversion.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040023#include "ext4_jbd2.h"
24#include "ext4.h"
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -040025#include <linux/fsmap.h>
26#include "fsmap.h"
27#include <trace/events/ext4.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070028
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040029/**
30 * Swap memory between @a and @b for @len bytes.
31 *
32 * @a: pointer to first memory area
33 * @b: pointer to second memory area
34 * @len: number of bytes to swap
35 *
36 */
37static void memswap(void *a, void *b, size_t len)
38{
39 unsigned char *ap, *bp;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040040
41 ap = (unsigned char *)a;
42 bp = (unsigned char *)b;
43 while (len-- > 0) {
Fabian Frederick4b7e2db2015-06-12 23:46:33 -040044 swap(*ap, *bp);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040045 ap++;
46 bp++;
47 }
48}
49
50/**
51 * Swap i_data and associated attributes between @inode1 and @inode2.
52 * This function is used for the primary swap between inode1 and inode2
53 * and also to revert this primary swap in case of errors.
54 *
55 * Therefore you have to make sure, that calling this method twice
56 * will revert all changes.
57 *
58 * @inode1: pointer to first inode
59 * @inode2: pointer to second inode
60 */
61static void swap_inode_data(struct inode *inode1, struct inode *inode2)
62{
63 loff_t isize;
64 struct ext4_inode_info *ei1;
65 struct ext4_inode_info *ei2;
66
67 ei1 = EXT4_I(inode1);
68 ei2 = EXT4_I(inode2);
69
Jeff Layton9c5d58f2017-07-31 00:55:34 -040070 swap(inode1->i_version, inode2->i_version);
71 swap(inode1->i_blocks, inode2->i_blocks);
72 swap(inode1->i_bytes, inode2->i_bytes);
73 swap(inode1->i_atime, inode2->i_atime);
74 swap(inode1->i_mtime, inode2->i_mtime);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040075
76 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
Jeff Layton9c5d58f2017-07-31 00:55:34 -040077 swap(ei1->i_flags, ei2->i_flags);
78 swap(ei1->i_disksize, ei2->i_disksize);
Theodore Ts'ocde2d7a2013-08-12 09:29:30 -040079 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
80 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040081
82 isize = i_size_read(inode1);
83 i_size_write(inode1, i_size_read(inode2));
84 i_size_write(inode2, isize);
85}
86
Theodore Ts'o18aded12018-10-02 18:21:19 -040087static void reset_inode_seed(struct inode *inode)
88{
89 struct ext4_inode_info *ei = EXT4_I(inode);
90 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
91 __le32 inum = cpu_to_le32(inode->i_ino);
92 __le32 gen = cpu_to_le32(inode->i_generation);
93 __u32 csum;
94
95 if (!ext4_has_metadata_csum(inode->i_sb))
96 return;
97
98 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum));
99 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen));
100}
101
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400102/**
103 * Swap the information from the given @inode and the inode
104 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
105 * important fields of the inodes.
106 *
107 * @sb: the super block of the filesystem
108 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
109 *
110 */
111static long swap_inode_boot_loader(struct super_block *sb,
112 struct inode *inode)
113{
114 handle_t *handle;
115 int err;
116 struct inode *inode_bl;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400117 struct ext4_inode_info *ei_bl;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400118
Theodore Ts'o8a363972018-12-19 12:29:13 -0500119 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL);
Theodore Ts'od8558a22014-02-17 20:44:36 -0500120 if (IS_ERR(inode_bl))
121 return PTR_ERR(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400122 ei_bl = EXT4_I(inode_bl);
123
124 filemap_flush(inode->i_mapping);
125 filemap_flush(inode_bl->i_mapping);
126
127 /* Protect orig inodes against a truncate and make sure,
128 * that only 1 swap_inode_boot_loader is running. */
J. Bruce Fields375e2892012-04-18 15:16:33 -0400129 lock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400130
yangerkun67a11612019-02-11 00:02:05 -0500131 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
132 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
133 ext4_has_inline_data(inode)) {
134 err = -EINVAL;
135 goto journal_err_out;
136 }
137
138 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
139 !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) {
140 err = -EPERM;
141 goto journal_err_out;
142 }
143
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400144 /* Wait for all existing dio workers */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400145 inode_dio_wait(inode);
146 inode_dio_wait(inode_bl);
147
Theodore Ts'o18aded12018-10-02 18:21:19 -0400148 truncate_inode_pages(&inode->i_data, 0);
149 truncate_inode_pages(&inode_bl->i_data, 0);
150
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400151 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
152 if (IS_ERR(handle)) {
153 err = -EINVAL;
Zheng Liu30d29b12014-02-12 11:48:31 -0500154 goto journal_err_out;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400155 }
156
157 /* Protect extent tree against block allocations via delalloc */
158 ext4_double_down_write_data_sem(inode, inode_bl);
159
160 if (inode_bl->i_nlink == 0) {
161 /* this inode has never been used as a BOOT_LOADER */
162 set_nlink(inode_bl, 1);
163 i_uid_write(inode_bl, 0);
164 i_gid_write(inode_bl, 0);
165 inode_bl->i_flags = 0;
166 ei_bl->i_flags = 0;
Jeff Laytonee73f9a2018-01-09 08:21:39 -0500167 inode_set_iversion(inode_bl, 1);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400168 i_size_write(inode_bl, 0);
169 inode_bl->i_mode = S_IFREG;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400170 if (ext4_has_feature_extents(sb)) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400171 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
172 ext4_ext_tree_init(handle, inode_bl);
173 } else
174 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
175 }
176
177 swap_inode_data(inode, inode_bl);
178
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500179 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400180
Theodore Ts'o23253062017-11-08 22:23:20 -0500181 inode->i_generation = prandom_u32();
182 inode_bl->i_generation = prandom_u32();
Theodore Ts'o18aded12018-10-02 18:21:19 -0400183 reset_inode_seed(inode);
184 reset_inode_seed(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400185
186 ext4_discard_preallocations(inode);
187
188 err = ext4_mark_inode_dirty(handle, inode);
189 if (err < 0) {
190 ext4_warning(inode->i_sb,
191 "couldn't mark inode #%lu dirty (err %d)",
192 inode->i_ino, err);
193 /* Revert all changes: */
194 swap_inode_data(inode, inode_bl);
Theodore Ts'o18aded12018-10-02 18:21:19 -0400195 ext4_mark_inode_dirty(handle, inode);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400196 } else {
197 err = ext4_mark_inode_dirty(handle, inode_bl);
198 if (err < 0) {
199 ext4_warning(inode_bl->i_sb,
200 "couldn't mark inode #%lu dirty (err %d)",
201 inode_bl->i_ino, err);
202 /* Revert all changes: */
203 swap_inode_data(inode, inode_bl);
204 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o18aded12018-10-02 18:21:19 -0400205 ext4_mark_inode_dirty(handle, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400206 }
207 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400208 ext4_journal_stop(handle);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400209 ext4_double_up_write_data_sem(inode, inode_bl);
210
Zheng Liu30d29b12014-02-12 11:48:31 -0500211journal_err_out:
J. Bruce Fields375e2892012-04-18 15:16:33 -0400212 unlock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400213 iput(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400214 return err;
215}
216
Eric Biggersba679012016-12-01 11:55:51 -0500217#ifdef CONFIG_EXT4_FS_ENCRYPTION
Michael Halcrow9bd82122015-04-11 07:48:01 -0400218static int uuid_is_zero(__u8 u[16])
219{
220 int i;
221
222 for (i = 0; i < 16; i++)
223 if (u[i])
224 return 0;
225 return 1;
226}
Eric Biggersba679012016-12-01 11:55:51 -0500227#endif
Michael Halcrow9bd82122015-04-11 07:48:01 -0400228
Li Xi9b7365f2016-01-08 16:01:22 -0500229static int ext4_ioctl_setflags(struct inode *inode,
230 unsigned int flags)
231{
232 struct ext4_inode_info *ei = EXT4_I(inode);
233 handle_t *handle = NULL;
Anton Protopopovfdde3682016-02-11 23:57:21 -0500234 int err = -EPERM, migrate = 0;
Li Xi9b7365f2016-01-08 16:01:22 -0500235 struct ext4_iloc iloc;
236 unsigned int oldflags, mask, i;
237 unsigned int jflag;
238
239 /* Is it quota file? Do not allow user to mess with it */
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400240 if (ext4_is_quota_file(inode))
Li Xi9b7365f2016-01-08 16:01:22 -0500241 goto flags_out;
242
243 oldflags = ei->i_flags;
244
245 /* The JOURNAL_DATA flag is modifiable only by root */
246 jflag = flags & EXT4_JOURNAL_DATA_FL;
247
248 /*
249 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
250 * the relevant capability.
251 *
252 * This test looks nicer. Thanks to Pauline Middelink
253 */
254 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
255 if (!capable(CAP_LINUX_IMMUTABLE))
256 goto flags_out;
257 }
258
259 /*
260 * The JOURNAL_DATA flag can only be changed by
261 * the relevant capability.
262 */
263 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
264 if (!capable(CAP_SYS_RESOURCE))
265 goto flags_out;
266 }
267 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
268 migrate = 1;
269
270 if (flags & EXT4_EOFBLOCKS_FL) {
271 /* we don't support adding EOFBLOCKS flag */
272 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
273 err = -EOPNOTSUPP;
274 goto flags_out;
275 }
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500276 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
277 err = ext4_truncate(inode);
278 if (err)
279 goto flags_out;
280 }
Li Xi9b7365f2016-01-08 16:01:22 -0500281
282 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
283 if (IS_ERR(handle)) {
284 err = PTR_ERR(handle);
285 goto flags_out;
286 }
287 if (IS_SYNC(inode))
288 ext4_handle_sync(handle);
289 err = ext4_reserve_inode_write(handle, inode, &iloc);
290 if (err)
291 goto flags_err;
292
293 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
294 if (!(mask & EXT4_FL_USER_MODIFIABLE))
295 continue;
Jan Karaf8011d92016-11-29 11:13:13 -0500296 /* These flags get special treatment later */
297 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
298 continue;
Li Xi9b7365f2016-01-08 16:01:22 -0500299 if (mask & flags)
300 ext4_set_inode_flag(inode, i);
301 else
302 ext4_clear_inode_flag(inode, i);
303 }
304
305 ext4_set_inode_flags(inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500306 inode->i_ctime = current_time(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500307
308 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
309flags_err:
310 ext4_journal_stop(handle);
311 if (err)
312 goto flags_out;
313
Ross Zwislere9072d82017-10-12 11:54:08 -0400314 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
315 /*
316 * Changes to the journaling mode can cause unsafe changes to
317 * S_DAX if we are using the DAX mount option.
318 */
319 if (test_opt(inode->i_sb, DAX)) {
320 err = -EBUSY;
321 goto flags_out;
322 }
323
Li Xi9b7365f2016-01-08 16:01:22 -0500324 err = ext4_change_inode_journal_flag(inode, jflag);
Ross Zwislere9072d82017-10-12 11:54:08 -0400325 if (err)
326 goto flags_out;
327 }
Li Xi9b7365f2016-01-08 16:01:22 -0500328 if (migrate) {
329 if (flags & EXT4_EXTENTS_FL)
330 err = ext4_ext_migrate(inode);
331 else
332 err = ext4_ind_migrate(inode);
333 }
334
335flags_out:
336 return err;
337}
338
339#ifdef CONFIG_QUOTA
340static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
341{
342 struct inode *inode = file_inode(filp);
343 struct super_block *sb = inode->i_sb;
344 struct ext4_inode_info *ei = EXT4_I(inode);
345 int err, rc;
346 handle_t *handle;
347 kprojid_t kprojid;
348 struct ext4_iloc iloc;
349 struct ext4_inode *raw_inode;
Wang Shilong079788d2016-07-05 21:33:52 -0400350 struct dquot *transfer_to[MAXQUOTAS] = { };
Li Xi9b7365f2016-01-08 16:01:22 -0500351
Kaho Ng0b7b7772016-09-05 23:11:58 -0400352 if (!ext4_has_feature_project(sb)) {
Li Xi9b7365f2016-01-08 16:01:22 -0500353 if (projid != EXT4_DEF_PROJID)
354 return -EOPNOTSUPP;
355 else
356 return 0;
357 }
358
359 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
360 return -EOPNOTSUPP;
361
362 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
363
364 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
365 return 0;
366
Li Xi9b7365f2016-01-08 16:01:22 -0500367 err = -EPERM;
Li Xi9b7365f2016-01-08 16:01:22 -0500368 /* Is it quota file? Do not allow user to mess with it */
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400369 if (ext4_is_quota_file(inode))
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400370 return err;
Li Xi9b7365f2016-01-08 16:01:22 -0500371
372 err = ext4_get_inode_loc(inode, &iloc);
373 if (err)
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400374 return err;
Li Xi9b7365f2016-01-08 16:01:22 -0500375
376 raw_inode = ext4_raw_inode(&iloc);
377 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
Miao Xiec03b45b2017-08-06 01:00:49 -0400378 err = ext4_expand_extra_isize(inode,
379 EXT4_SB(sb)->s_want_extra_isize,
380 &iloc);
381 if (err)
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400382 return err;
Miao Xiec03b45b2017-08-06 01:00:49 -0400383 } else {
Li Xi9b7365f2016-01-08 16:01:22 -0500384 brelse(iloc.bh);
Li Xi9b7365f2016-01-08 16:01:22 -0500385 }
Li Xi9b7365f2016-01-08 16:01:22 -0500386
Wang Shilong182a79e2018-10-03 12:19:21 -0400387 err = dquot_initialize(inode);
388 if (err)
389 return err;
Li Xi9b7365f2016-01-08 16:01:22 -0500390
391 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
392 EXT4_QUOTA_INIT_BLOCKS(sb) +
393 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400394 if (IS_ERR(handle))
395 return PTR_ERR(handle);
Li Xi9b7365f2016-01-08 16:01:22 -0500396
397 err = ext4_reserve_inode_write(handle, inode, &iloc);
398 if (err)
399 goto out_stop;
400
Wang Shilong079788d2016-07-05 21:33:52 -0400401 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
402 if (!IS_ERR(transfer_to[PRJQUOTA])) {
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400403
404 /* __dquot_transfer() calls back ext4_get_inode_usage() which
405 * counts xattr inode references.
406 */
407 down_read(&EXT4_I(inode)->xattr_sem);
Wang Shilong079788d2016-07-05 21:33:52 -0400408 err = __dquot_transfer(inode, transfer_to);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400409 up_read(&EXT4_I(inode)->xattr_sem);
Wang Shilong079788d2016-07-05 21:33:52 -0400410 dqput(transfer_to[PRJQUOTA]);
411 if (err)
412 goto out_dirty;
Li Xi9b7365f2016-01-08 16:01:22 -0500413 }
Wang Shilong079788d2016-07-05 21:33:52 -0400414
Li Xi9b7365f2016-01-08 16:01:22 -0500415 EXT4_I(inode)->i_projid = kprojid;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500416 inode->i_ctime = current_time(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500417out_dirty:
418 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
419 if (!err)
420 err = rc;
421out_stop:
422 ext4_journal_stop(handle);
Li Xi9b7365f2016-01-08 16:01:22 -0500423 return err;
424}
425#else
426static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
427{
428 if (projid != EXT4_DEF_PROJID)
429 return -EOPNOTSUPP;
430 return 0;
431}
432#endif
433
434/* Transfer internal flags to xflags */
435static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
436{
437 __u32 xflags = 0;
438
439 if (iflags & EXT4_SYNC_FL)
440 xflags |= FS_XFLAG_SYNC;
441 if (iflags & EXT4_IMMUTABLE_FL)
442 xflags |= FS_XFLAG_IMMUTABLE;
443 if (iflags & EXT4_APPEND_FL)
444 xflags |= FS_XFLAG_APPEND;
445 if (iflags & EXT4_NODUMP_FL)
446 xflags |= FS_XFLAG_NODUMP;
447 if (iflags & EXT4_NOATIME_FL)
448 xflags |= FS_XFLAG_NOATIME;
449 if (iflags & EXT4_PROJINHERIT_FL)
450 xflags |= FS_XFLAG_PROJINHERIT;
451 return xflags;
452}
453
Jan Karad14e7682016-11-29 11:18:39 -0500454#define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
455 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
456 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
457
Li Xi9b7365f2016-01-08 16:01:22 -0500458/* Transfer xflags flags to internal */
459static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
460{
461 unsigned long iflags = 0;
462
463 if (xflags & FS_XFLAG_SYNC)
464 iflags |= EXT4_SYNC_FL;
465 if (xflags & FS_XFLAG_IMMUTABLE)
466 iflags |= EXT4_IMMUTABLE_FL;
467 if (xflags & FS_XFLAG_APPEND)
468 iflags |= EXT4_APPEND_FL;
469 if (xflags & FS_XFLAG_NODUMP)
470 iflags |= EXT4_NODUMP_FL;
471 if (xflags & FS_XFLAG_NOATIME)
472 iflags |= EXT4_NOATIME_FL;
473 if (xflags & FS_XFLAG_PROJINHERIT)
474 iflags |= EXT4_PROJINHERIT_FL;
475
476 return iflags;
477}
478
Eric Biggers1a20a6302017-04-30 00:40:44 -0400479static int ext4_shutdown(struct super_block *sb, unsigned long arg)
Theodore Ts'o783d9482017-02-05 19:47:14 -0500480{
481 struct ext4_sb_info *sbi = EXT4_SB(sb);
482 __u32 flags;
483
484 if (!capable(CAP_SYS_ADMIN))
485 return -EPERM;
486
487 if (get_user(flags, (__u32 __user *)arg))
488 return -EFAULT;
489
490 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
491 return -EINVAL;
492
493 if (ext4_forced_shutdown(sbi))
494 return 0;
495
496 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
Theodore Ts'occf0f322018-02-18 20:53:23 -0500497 trace_ext4_shutdown(sb, flags);
Theodore Ts'o783d9482017-02-05 19:47:14 -0500498
499 switch (flags) {
500 case EXT4_GOING_FLAGS_DEFAULT:
501 freeze_bdev(sb->s_bdev);
502 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
503 thaw_bdev(sb->s_bdev, sb);
504 break;
505 case EXT4_GOING_FLAGS_LOGFLUSH:
506 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
507 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
508 (void) ext4_force_commit(sb);
Theodore Ts'ofb7c0242018-02-18 23:45:18 -0500509 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
Theodore Ts'o783d9482017-02-05 19:47:14 -0500510 }
511 break;
512 case EXT4_GOING_FLAGS_NOLOGFLUSH:
513 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
Theodore Ts'oa6d99462018-02-18 23:16:28 -0500514 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
Theodore Ts'ofb7c0242018-02-18 23:45:18 -0500515 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
Theodore Ts'o783d9482017-02-05 19:47:14 -0500516 break;
517 default:
518 return -EINVAL;
519 }
520 clear_opt(sb, DISCARD);
521 return 0;
522}
523
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -0400524struct getfsmap_info {
525 struct super_block *gi_sb;
526 struct fsmap_head __user *gi_data;
527 unsigned int gi_idx;
528 __u32 gi_last_flags;
529};
530
531static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
532{
533 struct getfsmap_info *info = priv;
534 struct fsmap fm;
535
536 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
537
538 info->gi_last_flags = xfm->fmr_flags;
539 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
540 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
541 sizeof(struct fsmap)))
542 return -EFAULT;
543
544 return 0;
545}
546
547static int ext4_ioc_getfsmap(struct super_block *sb,
548 struct fsmap_head __user *arg)
549{
550 struct getfsmap_info info = {0};
551 struct ext4_fsmap_head xhead = {0};
552 struct fsmap_head head;
553 bool aborted = false;
554 int error;
555
556 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
557 return -EFAULT;
558 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
559 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
560 sizeof(head.fmh_keys[0].fmr_reserved)) ||
561 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
562 sizeof(head.fmh_keys[1].fmr_reserved)))
563 return -EINVAL;
564 /*
565 * ext4 doesn't report file extents at all, so the only valid
566 * file offsets are the magic ones (all zeroes or all ones).
567 */
568 if (head.fmh_keys[0].fmr_offset ||
569 (head.fmh_keys[1].fmr_offset != 0 &&
570 head.fmh_keys[1].fmr_offset != -1ULL))
571 return -EINVAL;
572
573 xhead.fmh_iflags = head.fmh_iflags;
574 xhead.fmh_count = head.fmh_count;
575 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
576 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
577
578 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
579 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
580
581 info.gi_sb = sb;
582 info.gi_data = arg;
583 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
584 if (error == EXT4_QUERY_RANGE_ABORT) {
585 error = 0;
586 aborted = true;
587 } else if (error)
588 return error;
589
590 /* If we didn't abort, set the "last" flag in the last fmx */
591 if (!aborted && info.gi_idx) {
592 info.gi_last_flags |= FMR_OF_LAST;
593 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
594 &info.gi_last_flags,
595 sizeof(info.gi_last_flags)))
596 return -EFAULT;
597 }
598
599 /* copy back header */
600 head.fmh_entries = xhead.fmh_entries;
601 head.fmh_oflags = xhead.fmh_oflags;
602 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
603 return -EFAULT;
604
605 return 0;
606}
607
Al Viroe145b352017-09-29 21:39:59 -0400608static long ext4_ioctl_group_add(struct file *file,
609 struct ext4_new_group_data *input)
610{
611 struct super_block *sb = file_inode(file)->i_sb;
612 int err, err2=0;
613
614 err = ext4_resize_begin(sb);
615 if (err)
616 return err;
617
618 if (ext4_has_feature_bigalloc(sb)) {
619 ext4_msg(sb, KERN_ERR,
620 "Online resizing not supported with bigalloc");
621 err = -EOPNOTSUPP;
622 goto group_add_out;
623 }
624
625 err = mnt_want_write_file(file);
626 if (err)
627 goto group_add_out;
628
629 err = ext4_group_add(sb, input);
630 if (EXT4_SB(sb)->s_journal) {
631 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
632 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
633 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
634 }
635 if (err == 0)
636 err = err2;
637 mnt_drop_write_file(file);
638 if (!err && ext4_has_group_desc_csum(sb) &&
639 test_opt(sb, INIT_INODE_TABLE))
640 err = ext4_register_li_request(sb, input->group);
641group_add_out:
642 ext4_resize_end(sb);
643 return err;
644}
645
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400646static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
647{
648 /*
649 * Project Quota ID state is only allowed to change from within the init
650 * namespace. Enforce that restriction only if we are trying to change
651 * the quota ID state. Everything else is allowed in user namespaces.
652 */
653 if (current_user_ns() == &init_user_ns)
654 return 0;
655
656 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
657 return -EINVAL;
658
659 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
660 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
661 return -EINVAL;
662 } else {
663 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
664 return -EINVAL;
665 }
666
667 return 0;
668}
669
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400670long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671{
Al Viro496ad9a2013-01-23 17:07:38 -0500672 struct inode *inode = file_inode(filp);
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400673 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700674 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 unsigned int flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400677 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700678
679 switch (cmd) {
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -0400680 case FS_IOC_GETFSMAP:
681 return ext4_ioc_getfsmap(sb, (void __user *)arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700682 case EXT4_IOC_GETFLAGS:
683 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700684 return put_user(flags, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700685 case EXT4_IOC_SETFLAGS: {
Li Xi9b7365f2016-01-08 16:01:22 -0500686 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700688 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700689 return -EACCES;
690
691 if (get_user(flags, (int __user *) arg))
692 return -EFAULT;
693
Jan Karad14e7682016-11-29 11:18:39 -0500694 if (flags & ~EXT4_FL_USER_VISIBLE)
695 return -EOPNOTSUPP;
696 /*
697 * chattr(1) grabs flags via GETFLAGS, modifies the result and
698 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
699 * more restrictive than just silently masking off visible but
700 * not settable flags as we always did.
701 */
702 flags &= EXT4_FL_USER_MODIFIABLE;
703 if (ext4_mask_flags(inode->i_mode, flags) != flags)
704 return -EOPNOTSUPP;
705
Al Viroa561be72011-11-23 11:57:51 -0500706 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800707 if (err)
708 return err;
709
Al Viro59551022016-01-22 15:40:57 -0500710 inode_lock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500711 err = ext4_ioctl_setflags(inode, flags);
Al Viro59551022016-01-22 15:40:57 -0500712 inode_unlock(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500713 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 return err;
715 }
Mingming Cao617ba132006-10-11 01:20:53 -0700716 case EXT4_IOC_GETVERSION:
717 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700719 case EXT4_IOC_SETVERSION:
720 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700722 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700723 __u32 generation;
724 int err;
725
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700726 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800728
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400729 if (ext4_has_metadata_csum(inode->i_sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -0400730 ext4_warning(sb, "Setting inode version is not "
731 "supported with metadata_csum enabled.");
732 return -ENOTTY;
733 }
734
Al Viroa561be72011-11-23 11:57:51 -0500735 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800736 if (err)
737 return err;
738 if (get_user(generation, (int __user *) arg)) {
739 err = -EFAULT;
740 goto setversion_out;
741 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742
Al Viro59551022016-01-22 15:40:57 -0500743 inode_lock(inode);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500744 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800745 if (IS_ERR(handle)) {
746 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100747 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800748 }
Mingming Cao617ba132006-10-11 01:20:53 -0700749 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750 if (err == 0) {
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500751 inode->i_ctime = current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700753 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 }
Mingming Cao617ba132006-10-11 01:20:53 -0700755 ext4_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100756
757unlock_out:
Al Viro59551022016-01-22 15:40:57 -0500758 inode_unlock(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800759setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500760 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 return err;
762 }
Mingming Cao617ba132006-10-11 01:20:53 -0700763 case EXT4_IOC_GROUP_EXTEND: {
764 ext4_fsblk_t n_blocks_count;
Peng Taoac046f12009-07-13 09:30:17 -0400765 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700766
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400767 err = ext4_resize_begin(sb);
768 if (err)
769 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770
Djalal Harouni014a1772012-01-04 17:09:52 -0500771 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
772 err = -EFAULT;
773 goto group_extend_out;
774 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400776 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400777 ext4_msg(sb, KERN_ERR,
778 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500779 err = -EOPNOTSUPP;
780 goto group_extend_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400781 }
782
Al Viroa561be72011-11-23 11:57:51 -0500783 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800784 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500785 goto group_extend_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800786
Mingming Cao617ba132006-10-11 01:20:53 -0700787 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Peng Taoac046f12009-07-13 09:30:17 -0400788 if (EXT4_SB(sb)->s_journal) {
789 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
790 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
791 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
792 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400793 if (err == 0)
794 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500795 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500796group_extend_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400797 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798 return err;
799 }
Akira Fujita748de672009-06-17 19:24:03 -0400800
801 case EXT4_IOC_MOVE_EXT: {
802 struct move_extent me;
Al Viro2903ff02012-08-28 12:52:22 -0400803 struct fd donor;
804 int err;
Akira Fujita748de672009-06-17 19:24:03 -0400805
Akira Fujita4a585792009-12-06 23:38:31 -0500806 if (!(filp->f_mode & FMODE_READ) ||
807 !(filp->f_mode & FMODE_WRITE))
808 return -EBADF;
809
Akira Fujita748de672009-06-17 19:24:03 -0400810 if (copy_from_user(&me,
811 (struct move_extent __user *)arg, sizeof(me)))
812 return -EFAULT;
Akira Fujita4a585792009-12-06 23:38:31 -0500813 me.moved_len = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400814
Al Viro2903ff02012-08-28 12:52:22 -0400815 donor = fdget(me.donor_fd);
816 if (!donor.file)
Akira Fujita748de672009-06-17 19:24:03 -0400817 return -EBADF;
818
Al Viro2903ff02012-08-28 12:52:22 -0400819 if (!(donor.file->f_mode & FMODE_WRITE)) {
Akira Fujita4a585792009-12-06 23:38:31 -0500820 err = -EBADF;
821 goto mext_out;
Akira Fujita748de672009-06-17 19:24:03 -0400822 }
823
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400824 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400825 ext4_msg(sb, KERN_ERR,
826 "Online defrag not supported with bigalloc");
Al Viro399c9b82012-08-26 21:00:03 -0400827 err = -EOPNOTSUPP;
828 goto mext_out;
Ross Zwisler73f34a52016-02-26 15:19:49 -0800829 } else if (IS_DAX(inode)) {
830 ext4_msg(sb, KERN_ERR,
831 "Online defrag not supported with DAX");
832 err = -EOPNOTSUPP;
833 goto mext_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400834 }
835
Al Viroa561be72011-11-23 11:57:51 -0500836 err = mnt_want_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500837 if (err)
838 goto mext_out;
839
Al Viro2903ff02012-08-28 12:52:22 -0400840 err = ext4_move_extents(filp, donor.file, me.orig_start,
Akira Fujita748de672009-06-17 19:24:03 -0400841 me.donor_start, me.len, &me.moved_len);
Al Viro2a79f172011-12-09 08:06:57 -0500842 mnt_drop_write_file(filp);
Akira Fujita748de672009-06-17 19:24:03 -0400843
Theodore Ts'o60e66792010-05-17 07:00:00 -0400844 if (copy_to_user((struct move_extent __user *)arg,
Akira Fujitac437b272010-03-04 00:39:24 -0500845 &me, sizeof(me)))
Akira Fujita4a585792009-12-06 23:38:31 -0500846 err = -EFAULT;
847mext_out:
Al Viro2903ff02012-08-28 12:52:22 -0400848 fdput(donor);
Akira Fujita748de672009-06-17 19:24:03 -0400849 return err;
850 }
851
Mingming Cao617ba132006-10-11 01:20:53 -0700852 case EXT4_IOC_GROUP_ADD: {
853 struct ext4_new_group_data input;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854
Mingming Cao617ba132006-10-11 01:20:53 -0700855 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Al Viroe145b352017-09-29 21:39:59 -0400856 sizeof(input)))
857 return -EFAULT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858
Al Viroe145b352017-09-29 21:39:59 -0400859 return ext4_ioctl_group_add(filp, &input);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 }
861
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500862 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400863 {
864 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700865 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400866 return -EACCES;
867
Al Viroa561be72011-11-23 11:57:51 -0500868 err = mnt_want_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400869 if (err)
870 return err;
871 /*
872 * inode_mutex prevent write and truncate on the file.
873 * Read still goes through. We take i_data_sem in
874 * ext4_ext_swap_inode_data before we switch the
875 * inode format to prevent read.
876 */
Al Viro59551022016-01-22 15:40:57 -0500877 inode_lock((inode));
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400878 err = ext4_ext_migrate(inode);
Al Viro59551022016-01-22 15:40:57 -0500879 inode_unlock((inode));
Al Viro2a79f172011-12-09 08:06:57 -0500880 mnt_drop_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400881 return err;
882 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500883
Theodore Ts'occd25062009-02-26 01:04:07 -0500884 case EXT4_IOC_ALLOC_DA_BLKS:
885 {
886 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700887 if (!inode_owner_or_capable(inode))
Theodore Ts'occd25062009-02-26 01:04:07 -0500888 return -EACCES;
889
Al Viroa561be72011-11-23 11:57:51 -0500890 err = mnt_want_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500891 if (err)
892 return err;
893 err = ext4_alloc_da_blocks(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500894 mnt_drop_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500895 return err;
896 }
897
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400898 case EXT4_IOC_SWAP_BOOT:
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400899 {
900 int err;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400901 if (!(filp->f_mode & FMODE_WRITE))
902 return -EBADF;
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400903 err = mnt_want_write_file(filp);
904 if (err)
905 return err;
906 err = swap_inode_boot_loader(sb, inode);
907 mnt_drop_write_file(filp);
908 return err;
909 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400910
Yongqiang Yang19c52462012-01-04 17:09:44 -0500911 case EXT4_IOC_RESIZE_FS: {
912 ext4_fsblk_t n_blocks_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500913 int err = 0, err2 = 0;
Theodore Ts'o7f511862013-01-13 08:41:45 -0500914 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500915
Yongqiang Yang19c52462012-01-04 17:09:44 -0500916 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
917 sizeof(__u64))) {
918 return -EFAULT;
919 }
920
Yongqiang Yang19c52462012-01-04 17:09:44 -0500921 err = ext4_resize_begin(sb);
922 if (err)
923 return err;
924
Al Viro8cae6f72012-07-19 11:19:07 +0400925 err = mnt_want_write_file(filp);
Yongqiang Yang19c52462012-01-04 17:09:44 -0500926 if (err)
927 goto resizefs_out;
928
929 err = ext4_resize_fs(sb, n_blocks_count);
930 if (EXT4_SB(sb)->s_journal) {
931 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
932 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
933 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
934 }
935 if (err == 0)
936 err = err2;
Al Viro8cae6f72012-07-19 11:19:07 +0400937 mnt_drop_write_file(filp);
Theodore Ts'o7f511862013-01-13 08:41:45 -0500938 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
939 ext4_has_group_desc_csum(sb) &&
940 test_opt(sb, INIT_INODE_TABLE))
941 err = ext4_register_li_request(sb, o_group);
942
Yongqiang Yang19c52462012-01-04 17:09:44 -0500943resizefs_out:
944 ext4_resize_end(sb);
945 return err;
946 }
947
Lukas Czernere681c042010-11-19 21:47:07 -0500948 case FITRIM:
949 {
Lukas Czerner41431792011-02-23 12:42:32 -0500950 struct request_queue *q = bdev_get_queue(sb->s_bdev);
Lukas Czernere681c042010-11-19 21:47:07 -0500951 struct fstrim_range range;
952 int ret = 0;
953
954 if (!capable(CAP_SYS_ADMIN))
955 return -EPERM;
956
Lukas Czerner41431792011-02-23 12:42:32 -0500957 if (!blk_queue_discard(q))
958 return -EOPNOTSUPP;
959
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400960 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czernere681c042010-11-19 21:47:07 -0500961 sizeof(range)))
962 return -EFAULT;
963
Lukas Czerner5c2ed622011-02-23 17:49:51 -0500964 range.minlen = max((unsigned int)range.minlen,
965 q->limits.discard_granularity);
Lukas Czernere681c042010-11-19 21:47:07 -0500966 ret = ext4_trim_fs(sb, &range);
967 if (ret < 0)
968 return ret;
969
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400970 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czernere681c042010-11-19 21:47:07 -0500971 sizeof(range)))
972 return -EFAULT;
973
974 return 0;
975 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400976 case EXT4_IOC_PRECACHE_EXTENTS:
977 return ext4_ext_precache(inode);
Lukas Czernere681c042010-11-19 21:47:07 -0500978
Eric Biggersdb717d82016-11-26 19:07:49 -0500979 case EXT4_IOC_SET_ENCRYPTION_POLICY:
Richard Weinberger9a200d02016-09-30 01:49:55 -0400980 if (!ext4_has_feature_encrypt(sb))
981 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -0500982 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Richard Weinberger9a200d02016-09-30 01:49:55 -0400983
Michael Halcrow9bd82122015-04-11 07:48:01 -0400984 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
Eric Biggersba679012016-12-01 11:55:51 -0500985#ifdef CONFIG_EXT4_FS_ENCRYPTION
Michael Halcrow9bd82122015-04-11 07:48:01 -0400986 int err, err2;
987 struct ext4_sb_info *sbi = EXT4_SB(sb);
988 handle_t *handle;
989
Eric Biggers35997d12016-12-01 11:54:18 -0500990 if (!ext4_has_feature_encrypt(sb))
Michael Halcrow9bd82122015-04-11 07:48:01 -0400991 return -EOPNOTSUPP;
992 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
993 err = mnt_want_write_file(filp);
994 if (err)
995 return err;
996 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
997 if (IS_ERR(handle)) {
998 err = PTR_ERR(handle);
999 goto pwsalt_err_exit;
1000 }
1001 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
1002 if (err)
1003 goto pwsalt_err_journal;
1004 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1005 err = ext4_handle_dirty_metadata(handle, NULL,
1006 sbi->s_sbh);
1007 pwsalt_err_journal:
1008 err2 = ext4_journal_stop(handle);
1009 if (err2 && !err)
1010 err = err2;
1011 pwsalt_err_exit:
1012 mnt_drop_write_file(filp);
1013 if (err)
1014 return err;
1015 }
Fabian Frederickb4ab9e22015-06-08 12:23:21 -04001016 if (copy_to_user((void __user *) arg,
1017 sbi->s_es->s_encrypt_pw_salt, 16))
Michael Halcrow9bd82122015-04-11 07:48:01 -04001018 return -EFAULT;
1019 return 0;
Eric Biggersba679012016-12-01 11:55:51 -05001020#else
1021 return -EOPNOTSUPP;
1022#endif
Michael Halcrow9bd82122015-04-11 07:48:01 -04001023 }
Eric Biggersdb717d82016-11-26 19:07:49 -05001024 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1025 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Michael Halcrow9bd82122015-04-11 07:48:01 -04001026
Li Xi9b7365f2016-01-08 16:01:22 -05001027 case EXT4_IOC_FSGETXATTR:
1028 {
1029 struct fsxattr fa;
1030
1031 memset(&fa, 0, sizeof(struct fsxattr));
Li Xi9b7365f2016-01-08 16:01:22 -05001032 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1033
Kaho Ng0b7b7772016-09-05 23:11:58 -04001034 if (ext4_has_feature_project(inode->i_sb)) {
Li Xi9b7365f2016-01-08 16:01:22 -05001035 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1036 EXT4_I(inode)->i_projid);
1037 }
1038
1039 if (copy_to_user((struct fsxattr __user *)arg,
1040 &fa, sizeof(fa)))
1041 return -EFAULT;
1042 return 0;
1043 }
1044 case EXT4_IOC_FSSETXATTR:
1045 {
1046 struct fsxattr fa;
1047 int err;
1048
1049 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1050 sizeof(fa)))
1051 return -EFAULT;
1052
1053 /* Make sure caller has proper permission */
1054 if (!inode_owner_or_capable(inode))
1055 return -EACCES;
1056
Jan Karad14e7682016-11-29 11:18:39 -05001057 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1058 return -EOPNOTSUPP;
1059
1060 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1061 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1062 return -EOPNOTSUPP;
1063
Li Xi9b7365f2016-01-08 16:01:22 -05001064 err = mnt_want_write_file(filp);
1065 if (err)
1066 return err;
1067
Al Viro59551022016-01-22 15:40:57 -05001068 inode_lock(inode);
Wang Shilongdc7ac6c2018-10-03 10:33:32 -04001069 err = ext4_ioctl_check_project(inode, &fa);
1070 if (err)
1071 goto out;
Li Xi9b7365f2016-01-08 16:01:22 -05001072 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1073 (flags & EXT4_FL_XFLAG_VISIBLE);
1074 err = ext4_ioctl_setflags(inode, flags);
Wang Shilongdc7ac6c2018-10-03 10:33:32 -04001075 if (err)
1076 goto out;
1077 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1078out:
Al Viro59551022016-01-22 15:40:57 -05001079 inode_unlock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -05001080 mnt_drop_write_file(filp);
Wang Shilongdc7ac6c2018-10-03 10:33:32 -04001081 return err;
Li Xi9b7365f2016-01-08 16:01:22 -05001082 }
Theodore Ts'oe9be2ac2017-02-20 15:34:59 -05001083 case EXT4_IOC_SHUTDOWN:
1084 return ext4_shutdown(sb, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001085 default:
1086 return -ENOTTY;
1087 }
1088}
1089
1090#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07001091long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001092{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093 /* These are just misnamed, they actually get/put from/to user an int */
1094 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -07001095 case EXT4_IOC32_GETFLAGS:
1096 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001097 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001098 case EXT4_IOC32_SETFLAGS:
1099 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001100 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001101 case EXT4_IOC32_GETVERSION:
1102 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001103 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001104 case EXT4_IOC32_SETVERSION:
1105 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001107 case EXT4_IOC32_GROUP_EXTEND:
1108 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001109 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001110 case EXT4_IOC32_GETVERSION_OLD:
1111 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001112 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001113 case EXT4_IOC32_SETVERSION_OLD:
1114 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001116 case EXT4_IOC32_GETRSVSZ:
1117 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001119 case EXT4_IOC32_SETRSVSZ:
1120 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 break;
Ben Hutchings4d92dc02010-05-17 06:00:00 -04001122 case EXT4_IOC32_GROUP_ADD: {
1123 struct compat_ext4_new_group_input __user *uinput;
Al Viroe145b352017-09-29 21:39:59 -04001124 struct ext4_new_group_data input;
Ben Hutchings4d92dc02010-05-17 06:00:00 -04001125 int err;
1126
1127 uinput = compat_ptr(arg);
1128 err = get_user(input.group, &uinput->group);
1129 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1130 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1131 err |= get_user(input.inode_table, &uinput->inode_table);
1132 err |= get_user(input.blocks_count, &uinput->blocks_count);
1133 err |= get_user(input.reserved_blocks,
1134 &uinput->reserved_blocks);
1135 if (err)
1136 return -EFAULT;
Al Viroe145b352017-09-29 21:39:59 -04001137 return ext4_ioctl_group_add(file, &input);
Ben Hutchings4d92dc02010-05-17 06:00:00 -04001138 }
Christian Borntraegerb684b2e2010-05-15 00:00:00 -04001139 case EXT4_IOC_MOVE_EXT:
Yongqiang Yang19c52462012-01-04 17:09:44 -05001140 case EXT4_IOC_RESIZE_FS:
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001141 case EXT4_IOC_PRECACHE_EXTENTS:
Michael Halcrow9bd82122015-04-11 07:48:01 -04001142 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1143 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1144 case EXT4_IOC_GET_ENCRYPTION_POLICY:
Theodore Ts'oe9be2ac2017-02-20 15:34:59 -05001145 case EXT4_IOC_SHUTDOWN:
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -04001146 case FS_IOC_GETFSMAP:
Christian Borntraegerb684b2e2010-05-15 00:00:00 -04001147 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 default:
1149 return -ENOIOCTLCMD;
1150 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -04001151 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152}
1153#endif