blob: 0edee31913d1fcccdc3b326aabeeb403fadcca75 [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'o18aded12018-10-02 18:21:19 -0400119 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) ||
120 IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) ||
121 ext4_has_inline_data(inode))
Theodore Ts'od8558a22014-02-17 20:44:36 -0500122 return -EINVAL;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400123
Theodore Ts'o18aded12018-10-02 18:21:19 -0400124 if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) ||
125 !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
Theodore Ts'od8558a22014-02-17 20:44:36 -0500126 return -EPERM;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400127
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400128 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
Theodore Ts'od8558a22014-02-17 20:44:36 -0500129 if (IS_ERR(inode_bl))
130 return PTR_ERR(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400131 ei_bl = EXT4_I(inode_bl);
132
133 filemap_flush(inode->i_mapping);
134 filemap_flush(inode_bl->i_mapping);
135
136 /* Protect orig inodes against a truncate and make sure,
137 * that only 1 swap_inode_boot_loader is running. */
J. Bruce Fields375e2892012-04-18 15:16:33 -0400138 lock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400139
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400140 /* Wait for all existing dio workers */
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400141 inode_dio_wait(inode);
142 inode_dio_wait(inode_bl);
143
Theodore Ts'o18aded12018-10-02 18:21:19 -0400144 truncate_inode_pages(&inode->i_data, 0);
145 truncate_inode_pages(&inode_bl->i_data, 0);
146
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400147 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
148 if (IS_ERR(handle)) {
149 err = -EINVAL;
Zheng Liu30d29b12014-02-12 11:48:31 -0500150 goto journal_err_out;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400151 }
152
153 /* Protect extent tree against block allocations via delalloc */
154 ext4_double_down_write_data_sem(inode, inode_bl);
155
156 if (inode_bl->i_nlink == 0) {
157 /* this inode has never been used as a BOOT_LOADER */
158 set_nlink(inode_bl, 1);
159 i_uid_write(inode_bl, 0);
160 i_gid_write(inode_bl, 0);
161 inode_bl->i_flags = 0;
162 ei_bl->i_flags = 0;
Jeff Laytonee73f9a2018-01-09 08:21:39 -0500163 inode_set_iversion(inode_bl, 1);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400164 i_size_write(inode_bl, 0);
165 inode_bl->i_mode = S_IFREG;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400166 if (ext4_has_feature_extents(sb)) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400167 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
168 ext4_ext_tree_init(handle, inode_bl);
169 } else
170 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
171 }
172
173 swap_inode_data(inode, inode_bl);
174
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500175 inode->i_ctime = inode_bl->i_ctime = current_time(inode);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400176
Theodore Ts'o23253062017-11-08 22:23:20 -0500177 inode->i_generation = prandom_u32();
178 inode_bl->i_generation = prandom_u32();
Theodore Ts'o18aded12018-10-02 18:21:19 -0400179 reset_inode_seed(inode);
180 reset_inode_seed(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400181
182 ext4_discard_preallocations(inode);
183
184 err = ext4_mark_inode_dirty(handle, inode);
185 if (err < 0) {
186 ext4_warning(inode->i_sb,
187 "couldn't mark inode #%lu dirty (err %d)",
188 inode->i_ino, err);
189 /* Revert all changes: */
190 swap_inode_data(inode, inode_bl);
Theodore Ts'o18aded12018-10-02 18:21:19 -0400191 ext4_mark_inode_dirty(handle, inode);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400192 } else {
193 err = ext4_mark_inode_dirty(handle, inode_bl);
194 if (err < 0) {
195 ext4_warning(inode_bl->i_sb,
196 "couldn't mark inode #%lu dirty (err %d)",
197 inode_bl->i_ino, err);
198 /* Revert all changes: */
199 swap_inode_data(inode, inode_bl);
200 ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o18aded12018-10-02 18:21:19 -0400201 ext4_mark_inode_dirty(handle, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400202 }
203 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400204 ext4_journal_stop(handle);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400205 ext4_double_up_write_data_sem(inode, inode_bl);
206
Zheng Liu30d29b12014-02-12 11:48:31 -0500207journal_err_out:
J. Bruce Fields375e2892012-04-18 15:16:33 -0400208 unlock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400209 iput(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400210 return err;
211}
212
Eric Biggersba679012016-12-01 11:55:51 -0500213#ifdef CONFIG_EXT4_FS_ENCRYPTION
Michael Halcrow9bd82122015-04-11 07:48:01 -0400214static int uuid_is_zero(__u8 u[16])
215{
216 int i;
217
218 for (i = 0; i < 16; i++)
219 if (u[i])
220 return 0;
221 return 1;
222}
Eric Biggersba679012016-12-01 11:55:51 -0500223#endif
Michael Halcrow9bd82122015-04-11 07:48:01 -0400224
Li Xi9b7365f2016-01-08 16:01:22 -0500225static int ext4_ioctl_setflags(struct inode *inode,
226 unsigned int flags)
227{
228 struct ext4_inode_info *ei = EXT4_I(inode);
229 handle_t *handle = NULL;
Anton Protopopovfdde3682016-02-11 23:57:21 -0500230 int err = -EPERM, migrate = 0;
Li Xi9b7365f2016-01-08 16:01:22 -0500231 struct ext4_iloc iloc;
232 unsigned int oldflags, mask, i;
233 unsigned int jflag;
234
235 /* Is it quota file? Do not allow user to mess with it */
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400236 if (ext4_is_quota_file(inode))
Li Xi9b7365f2016-01-08 16:01:22 -0500237 goto flags_out;
238
239 oldflags = ei->i_flags;
240
241 /* The JOURNAL_DATA flag is modifiable only by root */
242 jflag = flags & EXT4_JOURNAL_DATA_FL;
243
244 /*
245 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
246 * the relevant capability.
247 *
248 * This test looks nicer. Thanks to Pauline Middelink
249 */
250 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
251 if (!capable(CAP_LINUX_IMMUTABLE))
252 goto flags_out;
253 }
254
255 /*
256 * The JOURNAL_DATA flag can only be changed by
257 * the relevant capability.
258 */
259 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
260 if (!capable(CAP_SYS_RESOURCE))
261 goto flags_out;
262 }
263 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
264 migrate = 1;
265
266 if (flags & EXT4_EOFBLOCKS_FL) {
267 /* we don't support adding EOFBLOCKS flag */
268 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
269 err = -EOPNOTSUPP;
270 goto flags_out;
271 }
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500272 } else if (oldflags & EXT4_EOFBLOCKS_FL) {
273 err = ext4_truncate(inode);
274 if (err)
275 goto flags_out;
276 }
Li Xi9b7365f2016-01-08 16:01:22 -0500277
278 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
279 if (IS_ERR(handle)) {
280 err = PTR_ERR(handle);
281 goto flags_out;
282 }
283 if (IS_SYNC(inode))
284 ext4_handle_sync(handle);
285 err = ext4_reserve_inode_write(handle, inode, &iloc);
286 if (err)
287 goto flags_err;
288
289 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
290 if (!(mask & EXT4_FL_USER_MODIFIABLE))
291 continue;
Jan Karaf8011d92016-11-29 11:13:13 -0500292 /* These flags get special treatment later */
293 if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL)
294 continue;
Li Xi9b7365f2016-01-08 16:01:22 -0500295 if (mask & flags)
296 ext4_set_inode_flag(inode, i);
297 else
298 ext4_clear_inode_flag(inode, i);
299 }
300
301 ext4_set_inode_flags(inode);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500302 inode->i_ctime = current_time(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500303
304 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
305flags_err:
306 ext4_journal_stop(handle);
307 if (err)
308 goto flags_out;
309
Ross Zwislere9072d82017-10-12 11:54:08 -0400310 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
311 /*
312 * Changes to the journaling mode can cause unsafe changes to
313 * S_DAX if we are using the DAX mount option.
314 */
315 if (test_opt(inode->i_sb, DAX)) {
316 err = -EBUSY;
317 goto flags_out;
318 }
319
Li Xi9b7365f2016-01-08 16:01:22 -0500320 err = ext4_change_inode_journal_flag(inode, jflag);
Ross Zwislere9072d82017-10-12 11:54:08 -0400321 if (err)
322 goto flags_out;
323 }
Li Xi9b7365f2016-01-08 16:01:22 -0500324 if (migrate) {
325 if (flags & EXT4_EXTENTS_FL)
326 err = ext4_ext_migrate(inode);
327 else
328 err = ext4_ind_migrate(inode);
329 }
330
331flags_out:
332 return err;
333}
334
335#ifdef CONFIG_QUOTA
336static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
337{
338 struct inode *inode = file_inode(filp);
339 struct super_block *sb = inode->i_sb;
340 struct ext4_inode_info *ei = EXT4_I(inode);
341 int err, rc;
342 handle_t *handle;
343 kprojid_t kprojid;
344 struct ext4_iloc iloc;
345 struct ext4_inode *raw_inode;
Wang Shilong079788d2016-07-05 21:33:52 -0400346 struct dquot *transfer_to[MAXQUOTAS] = { };
Li Xi9b7365f2016-01-08 16:01:22 -0500347
Kaho Ng0b7b7772016-09-05 23:11:58 -0400348 if (!ext4_has_feature_project(sb)) {
Li Xi9b7365f2016-01-08 16:01:22 -0500349 if (projid != EXT4_DEF_PROJID)
350 return -EOPNOTSUPP;
351 else
352 return 0;
353 }
354
355 if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE)
356 return -EOPNOTSUPP;
357
358 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
359
360 if (projid_eq(kprojid, EXT4_I(inode)->i_projid))
361 return 0;
362
Li Xi9b7365f2016-01-08 16:01:22 -0500363 err = -EPERM;
Li Xi9b7365f2016-01-08 16:01:22 -0500364 /* Is it quota file? Do not allow user to mess with it */
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400365 if (ext4_is_quota_file(inode))
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400366 return err;
Li Xi9b7365f2016-01-08 16:01:22 -0500367
368 err = ext4_get_inode_loc(inode, &iloc);
369 if (err)
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400370 return err;
Li Xi9b7365f2016-01-08 16:01:22 -0500371
372 raw_inode = ext4_raw_inode(&iloc);
373 if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) {
Miao Xiec03b45b2017-08-06 01:00:49 -0400374 err = ext4_expand_extra_isize(inode,
375 EXT4_SB(sb)->s_want_extra_isize,
376 &iloc);
377 if (err)
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400378 return err;
Miao Xiec03b45b2017-08-06 01:00:49 -0400379 } else {
Li Xi9b7365f2016-01-08 16:01:22 -0500380 brelse(iloc.bh);
Li Xi9b7365f2016-01-08 16:01:22 -0500381 }
Li Xi9b7365f2016-01-08 16:01:22 -0500382
Wang Shilong182a79e2018-10-03 12:19:21 -0400383 err = dquot_initialize(inode);
384 if (err)
385 return err;
Li Xi9b7365f2016-01-08 16:01:22 -0500386
387 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
388 EXT4_QUOTA_INIT_BLOCKS(sb) +
389 EXT4_QUOTA_DEL_BLOCKS(sb) + 3);
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400390 if (IS_ERR(handle))
391 return PTR_ERR(handle);
Li Xi9b7365f2016-01-08 16:01:22 -0500392
393 err = ext4_reserve_inode_write(handle, inode, &iloc);
394 if (err)
395 goto out_stop;
396
Wang Shilong079788d2016-07-05 21:33:52 -0400397 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
398 if (!IS_ERR(transfer_to[PRJQUOTA])) {
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400399
400 /* __dquot_transfer() calls back ext4_get_inode_usage() which
401 * counts xattr inode references.
402 */
403 down_read(&EXT4_I(inode)->xattr_sem);
Wang Shilong079788d2016-07-05 21:33:52 -0400404 err = __dquot_transfer(inode, transfer_to);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -0400405 up_read(&EXT4_I(inode)->xattr_sem);
Wang Shilong079788d2016-07-05 21:33:52 -0400406 dqput(transfer_to[PRJQUOTA]);
407 if (err)
408 goto out_dirty;
Li Xi9b7365f2016-01-08 16:01:22 -0500409 }
Wang Shilong079788d2016-07-05 21:33:52 -0400410
Li Xi9b7365f2016-01-08 16:01:22 -0500411 EXT4_I(inode)->i_projid = kprojid;
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500412 inode->i_ctime = current_time(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500413out_dirty:
414 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
415 if (!err)
416 err = rc;
417out_stop:
418 ext4_journal_stop(handle);
Li Xi9b7365f2016-01-08 16:01:22 -0500419 return err;
420}
421#else
422static int ext4_ioctl_setproject(struct file *filp, __u32 projid)
423{
424 if (projid != EXT4_DEF_PROJID)
425 return -EOPNOTSUPP;
426 return 0;
427}
428#endif
429
430/* Transfer internal flags to xflags */
431static inline __u32 ext4_iflags_to_xflags(unsigned long iflags)
432{
433 __u32 xflags = 0;
434
435 if (iflags & EXT4_SYNC_FL)
436 xflags |= FS_XFLAG_SYNC;
437 if (iflags & EXT4_IMMUTABLE_FL)
438 xflags |= FS_XFLAG_IMMUTABLE;
439 if (iflags & EXT4_APPEND_FL)
440 xflags |= FS_XFLAG_APPEND;
441 if (iflags & EXT4_NODUMP_FL)
442 xflags |= FS_XFLAG_NODUMP;
443 if (iflags & EXT4_NOATIME_FL)
444 xflags |= FS_XFLAG_NOATIME;
445 if (iflags & EXT4_PROJINHERIT_FL)
446 xflags |= FS_XFLAG_PROJINHERIT;
447 return xflags;
448}
449
Jan Karad14e7682016-11-29 11:18:39 -0500450#define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
451 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
452 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
453
Li Xi9b7365f2016-01-08 16:01:22 -0500454/* Transfer xflags flags to internal */
455static inline unsigned long ext4_xflags_to_iflags(__u32 xflags)
456{
457 unsigned long iflags = 0;
458
459 if (xflags & FS_XFLAG_SYNC)
460 iflags |= EXT4_SYNC_FL;
461 if (xflags & FS_XFLAG_IMMUTABLE)
462 iflags |= EXT4_IMMUTABLE_FL;
463 if (xflags & FS_XFLAG_APPEND)
464 iflags |= EXT4_APPEND_FL;
465 if (xflags & FS_XFLAG_NODUMP)
466 iflags |= EXT4_NODUMP_FL;
467 if (xflags & FS_XFLAG_NOATIME)
468 iflags |= EXT4_NOATIME_FL;
469 if (xflags & FS_XFLAG_PROJINHERIT)
470 iflags |= EXT4_PROJINHERIT_FL;
471
472 return iflags;
473}
474
Eric Biggers1a20a6302017-04-30 00:40:44 -0400475static int ext4_shutdown(struct super_block *sb, unsigned long arg)
Theodore Ts'o783d9482017-02-05 19:47:14 -0500476{
477 struct ext4_sb_info *sbi = EXT4_SB(sb);
478 __u32 flags;
479
480 if (!capable(CAP_SYS_ADMIN))
481 return -EPERM;
482
483 if (get_user(flags, (__u32 __user *)arg))
484 return -EFAULT;
485
486 if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH)
487 return -EINVAL;
488
489 if (ext4_forced_shutdown(sbi))
490 return 0;
491
492 ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags);
Theodore Ts'occf0f322018-02-18 20:53:23 -0500493 trace_ext4_shutdown(sb, flags);
Theodore Ts'o783d9482017-02-05 19:47:14 -0500494
495 switch (flags) {
496 case EXT4_GOING_FLAGS_DEFAULT:
497 freeze_bdev(sb->s_bdev);
498 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
499 thaw_bdev(sb->s_bdev, sb);
500 break;
501 case EXT4_GOING_FLAGS_LOGFLUSH:
502 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
503 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) {
504 (void) ext4_force_commit(sb);
Theodore Ts'ofb7c0242018-02-18 23:45:18 -0500505 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
Theodore Ts'o783d9482017-02-05 19:47:14 -0500506 }
507 break;
508 case EXT4_GOING_FLAGS_NOLOGFLUSH:
509 set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags);
Theodore Ts'oa6d99462018-02-18 23:16:28 -0500510 if (sbi->s_journal && !is_journal_aborted(sbi->s_journal))
Theodore Ts'ofb7c0242018-02-18 23:45:18 -0500511 jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN);
Theodore Ts'o783d9482017-02-05 19:47:14 -0500512 break;
513 default:
514 return -EINVAL;
515 }
516 clear_opt(sb, DISCARD);
517 return 0;
518}
519
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -0400520struct getfsmap_info {
521 struct super_block *gi_sb;
522 struct fsmap_head __user *gi_data;
523 unsigned int gi_idx;
524 __u32 gi_last_flags;
525};
526
527static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv)
528{
529 struct getfsmap_info *info = priv;
530 struct fsmap fm;
531
532 trace_ext4_getfsmap_mapping(info->gi_sb, xfm);
533
534 info->gi_last_flags = xfm->fmr_flags;
535 ext4_fsmap_from_internal(info->gi_sb, &fm, xfm);
536 if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm,
537 sizeof(struct fsmap)))
538 return -EFAULT;
539
540 return 0;
541}
542
543static int ext4_ioc_getfsmap(struct super_block *sb,
544 struct fsmap_head __user *arg)
545{
546 struct getfsmap_info info = {0};
547 struct ext4_fsmap_head xhead = {0};
548 struct fsmap_head head;
549 bool aborted = false;
550 int error;
551
552 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
553 return -EFAULT;
554 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
555 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
556 sizeof(head.fmh_keys[0].fmr_reserved)) ||
557 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
558 sizeof(head.fmh_keys[1].fmr_reserved)))
559 return -EINVAL;
560 /*
561 * ext4 doesn't report file extents at all, so the only valid
562 * file offsets are the magic ones (all zeroes or all ones).
563 */
564 if (head.fmh_keys[0].fmr_offset ||
565 (head.fmh_keys[1].fmr_offset != 0 &&
566 head.fmh_keys[1].fmr_offset != -1ULL))
567 return -EINVAL;
568
569 xhead.fmh_iflags = head.fmh_iflags;
570 xhead.fmh_count = head.fmh_count;
571 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]);
572 ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]);
573
574 trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]);
575 trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]);
576
577 info.gi_sb = sb;
578 info.gi_data = arg;
579 error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info);
580 if (error == EXT4_QUERY_RANGE_ABORT) {
581 error = 0;
582 aborted = true;
583 } else if (error)
584 return error;
585
586 /* If we didn't abort, set the "last" flag in the last fmx */
587 if (!aborted && info.gi_idx) {
588 info.gi_last_flags |= FMR_OF_LAST;
589 if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags,
590 &info.gi_last_flags,
591 sizeof(info.gi_last_flags)))
592 return -EFAULT;
593 }
594
595 /* copy back header */
596 head.fmh_entries = xhead.fmh_entries;
597 head.fmh_oflags = xhead.fmh_oflags;
598 if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
599 return -EFAULT;
600
601 return 0;
602}
603
Al Viroe145b352017-09-29 21:39:59 -0400604static long ext4_ioctl_group_add(struct file *file,
605 struct ext4_new_group_data *input)
606{
607 struct super_block *sb = file_inode(file)->i_sb;
608 int err, err2=0;
609
610 err = ext4_resize_begin(sb);
611 if (err)
612 return err;
613
614 if (ext4_has_feature_bigalloc(sb)) {
615 ext4_msg(sb, KERN_ERR,
616 "Online resizing not supported with bigalloc");
617 err = -EOPNOTSUPP;
618 goto group_add_out;
619 }
620
621 err = mnt_want_write_file(file);
622 if (err)
623 goto group_add_out;
624
625 err = ext4_group_add(sb, input);
626 if (EXT4_SB(sb)->s_journal) {
627 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
628 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
629 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
630 }
631 if (err == 0)
632 err = err2;
633 mnt_drop_write_file(file);
634 if (!err && ext4_has_group_desc_csum(sb) &&
635 test_opt(sb, INIT_INODE_TABLE))
636 err = ext4_register_li_request(sb, input->group);
637group_add_out:
638 ext4_resize_end(sb);
639 return err;
640}
641
Wang Shilongdc7ac6c2018-10-03 10:33:32 -0400642static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa)
643{
644 /*
645 * Project Quota ID state is only allowed to change from within the init
646 * namespace. Enforce that restriction only if we are trying to change
647 * the quota ID state. Everything else is allowed in user namespaces.
648 */
649 if (current_user_ns() == &init_user_ns)
650 return 0;
651
652 if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid)
653 return -EINVAL;
654
655 if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) {
656 if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT))
657 return -EINVAL;
658 } else {
659 if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT)
660 return -EINVAL;
661 }
662
663 return 0;
664}
665
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400666long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700667{
Al Viro496ad9a2013-01-23 17:07:38 -0500668 struct inode *inode = file_inode(filp);
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400669 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700670 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700671 unsigned int flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400673 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674
675 switch (cmd) {
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -0400676 case FS_IOC_GETFSMAP:
677 return ext4_ioc_getfsmap(sb, (void __user *)arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700678 case EXT4_IOC_GETFLAGS:
679 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700680 return put_user(flags, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700681 case EXT4_IOC_SETFLAGS: {
Li Xi9b7365f2016-01-08 16:01:22 -0500682 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700684 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 return -EACCES;
686
687 if (get_user(flags, (int __user *) arg))
688 return -EFAULT;
689
Jan Karad14e7682016-11-29 11:18:39 -0500690 if (flags & ~EXT4_FL_USER_VISIBLE)
691 return -EOPNOTSUPP;
692 /*
693 * chattr(1) grabs flags via GETFLAGS, modifies the result and
694 * passes that to SETFLAGS. So we cannot easily make SETFLAGS
695 * more restrictive than just silently masking off visible but
696 * not settable flags as we always did.
697 */
698 flags &= EXT4_FL_USER_MODIFIABLE;
699 if (ext4_mask_flags(inode->i_mode, flags) != flags)
700 return -EOPNOTSUPP;
701
Al Viroa561be72011-11-23 11:57:51 -0500702 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800703 if (err)
704 return err;
705
Al Viro59551022016-01-22 15:40:57 -0500706 inode_lock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -0500707 err = ext4_ioctl_setflags(inode, flags);
Al Viro59551022016-01-22 15:40:57 -0500708 inode_unlock(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500709 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 return err;
711 }
Mingming Cao617ba132006-10-11 01:20:53 -0700712 case EXT4_IOC_GETVERSION:
713 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700715 case EXT4_IOC_SETVERSION:
716 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700718 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 __u32 generation;
720 int err;
721
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700722 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700723 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800724
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400725 if (ext4_has_metadata_csum(inode->i_sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -0400726 ext4_warning(sb, "Setting inode version is not "
727 "supported with metadata_csum enabled.");
728 return -ENOTTY;
729 }
730
Al Viroa561be72011-11-23 11:57:51 -0500731 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800732 if (err)
733 return err;
734 if (get_user(generation, (int __user *) arg)) {
735 err = -EFAULT;
736 goto setversion_out;
737 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738
Al Viro59551022016-01-22 15:40:57 -0500739 inode_lock(inode);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500740 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800741 if (IS_ERR(handle)) {
742 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100743 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800744 }
Mingming Cao617ba132006-10-11 01:20:53 -0700745 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746 if (err == 0) {
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -0500747 inode->i_ctime = current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700748 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700749 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750 }
Mingming Cao617ba132006-10-11 01:20:53 -0700751 ext4_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100752
753unlock_out:
Al Viro59551022016-01-22 15:40:57 -0500754 inode_unlock(inode);
Dave Hansen42a74f22008-02-15 14:37:46 -0800755setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500756 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757 return err;
758 }
Mingming Cao617ba132006-10-11 01:20:53 -0700759 case EXT4_IOC_GROUP_EXTEND: {
760 ext4_fsblk_t n_blocks_count;
Peng Taoac046f12009-07-13 09:30:17 -0400761 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400763 err = ext4_resize_begin(sb);
764 if (err)
765 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700766
Djalal Harouni014a1772012-01-04 17:09:52 -0500767 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
768 err = -EFAULT;
769 goto group_extend_out;
770 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400772 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400773 ext4_msg(sb, KERN_ERR,
774 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500775 err = -EOPNOTSUPP;
776 goto group_extend_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400777 }
778
Al Viroa561be72011-11-23 11:57:51 -0500779 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800780 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500781 goto group_extend_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800782
Mingming Cao617ba132006-10-11 01:20:53 -0700783 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Peng Taoac046f12009-07-13 09:30:17 -0400784 if (EXT4_SB(sb)->s_journal) {
785 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
786 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
787 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
788 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400789 if (err == 0)
790 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500791 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500792group_extend_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400793 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700794 return err;
795 }
Akira Fujita748de672009-06-17 19:24:03 -0400796
797 case EXT4_IOC_MOVE_EXT: {
798 struct move_extent me;
Al Viro2903ff02012-08-28 12:52:22 -0400799 struct fd donor;
800 int err;
Akira Fujita748de672009-06-17 19:24:03 -0400801
Akira Fujita4a585792009-12-06 23:38:31 -0500802 if (!(filp->f_mode & FMODE_READ) ||
803 !(filp->f_mode & FMODE_WRITE))
804 return -EBADF;
805
Akira Fujita748de672009-06-17 19:24:03 -0400806 if (copy_from_user(&me,
807 (struct move_extent __user *)arg, sizeof(me)))
808 return -EFAULT;
Akira Fujita4a585792009-12-06 23:38:31 -0500809 me.moved_len = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400810
Al Viro2903ff02012-08-28 12:52:22 -0400811 donor = fdget(me.donor_fd);
812 if (!donor.file)
Akira Fujita748de672009-06-17 19:24:03 -0400813 return -EBADF;
814
Al Viro2903ff02012-08-28 12:52:22 -0400815 if (!(donor.file->f_mode & FMODE_WRITE)) {
Akira Fujita4a585792009-12-06 23:38:31 -0500816 err = -EBADF;
817 goto mext_out;
Akira Fujita748de672009-06-17 19:24:03 -0400818 }
819
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400820 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400821 ext4_msg(sb, KERN_ERR,
822 "Online defrag not supported with bigalloc");
Al Viro399c9b82012-08-26 21:00:03 -0400823 err = -EOPNOTSUPP;
824 goto mext_out;
Ross Zwisler73f34a52016-02-26 15:19:49 -0800825 } else if (IS_DAX(inode)) {
826 ext4_msg(sb, KERN_ERR,
827 "Online defrag not supported with DAX");
828 err = -EOPNOTSUPP;
829 goto mext_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400830 }
831
Al Viroa561be72011-11-23 11:57:51 -0500832 err = mnt_want_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500833 if (err)
834 goto mext_out;
835
Al Viro2903ff02012-08-28 12:52:22 -0400836 err = ext4_move_extents(filp, donor.file, me.orig_start,
Akira Fujita748de672009-06-17 19:24:03 -0400837 me.donor_start, me.len, &me.moved_len);
Al Viro2a79f172011-12-09 08:06:57 -0500838 mnt_drop_write_file(filp);
Akira Fujita748de672009-06-17 19:24:03 -0400839
Theodore Ts'o60e66792010-05-17 07:00:00 -0400840 if (copy_to_user((struct move_extent __user *)arg,
Akira Fujitac437b272010-03-04 00:39:24 -0500841 &me, sizeof(me)))
Akira Fujita4a585792009-12-06 23:38:31 -0500842 err = -EFAULT;
843mext_out:
Al Viro2903ff02012-08-28 12:52:22 -0400844 fdput(donor);
Akira Fujita748de672009-06-17 19:24:03 -0400845 return err;
846 }
847
Mingming Cao617ba132006-10-11 01:20:53 -0700848 case EXT4_IOC_GROUP_ADD: {
849 struct ext4_new_group_data input;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850
Mingming Cao617ba132006-10-11 01:20:53 -0700851 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Al Viroe145b352017-09-29 21:39:59 -0400852 sizeof(input)))
853 return -EFAULT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854
Al Viroe145b352017-09-29 21:39:59 -0400855 return ext4_ioctl_group_add(filp, &input);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856 }
857
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500858 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400859 {
860 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700861 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400862 return -EACCES;
863
Al Viroa561be72011-11-23 11:57:51 -0500864 err = mnt_want_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400865 if (err)
866 return err;
867 /*
868 * inode_mutex prevent write and truncate on the file.
869 * Read still goes through. We take i_data_sem in
870 * ext4_ext_swap_inode_data before we switch the
871 * inode format to prevent read.
872 */
Al Viro59551022016-01-22 15:40:57 -0500873 inode_lock((inode));
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400874 err = ext4_ext_migrate(inode);
Al Viro59551022016-01-22 15:40:57 -0500875 inode_unlock((inode));
Al Viro2a79f172011-12-09 08:06:57 -0500876 mnt_drop_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400877 return err;
878 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500879
Theodore Ts'occd25062009-02-26 01:04:07 -0500880 case EXT4_IOC_ALLOC_DA_BLKS:
881 {
882 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700883 if (!inode_owner_or_capable(inode))
Theodore Ts'occd25062009-02-26 01:04:07 -0500884 return -EACCES;
885
Al Viroa561be72011-11-23 11:57:51 -0500886 err = mnt_want_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500887 if (err)
888 return err;
889 err = ext4_alloc_da_blocks(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500890 mnt_drop_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500891 return err;
892 }
893
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400894 case EXT4_IOC_SWAP_BOOT:
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400895 {
896 int err;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400897 if (!(filp->f_mode & FMODE_WRITE))
898 return -EBADF;
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400899 err = mnt_want_write_file(filp);
900 if (err)
901 return err;
902 err = swap_inode_boot_loader(sb, inode);
903 mnt_drop_write_file(filp);
904 return err;
905 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400906
Yongqiang Yang19c52462012-01-04 17:09:44 -0500907 case EXT4_IOC_RESIZE_FS: {
908 ext4_fsblk_t n_blocks_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500909 int err = 0, err2 = 0;
Theodore Ts'o7f511862013-01-13 08:41:45 -0500910 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500911
Yongqiang Yang19c52462012-01-04 17:09:44 -0500912 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
913 sizeof(__u64))) {
914 return -EFAULT;
915 }
916
Yongqiang Yang19c52462012-01-04 17:09:44 -0500917 err = ext4_resize_begin(sb);
918 if (err)
919 return err;
920
Al Viro8cae6f72012-07-19 11:19:07 +0400921 err = mnt_want_write_file(filp);
Yongqiang Yang19c52462012-01-04 17:09:44 -0500922 if (err)
923 goto resizefs_out;
924
925 err = ext4_resize_fs(sb, n_blocks_count);
926 if (EXT4_SB(sb)->s_journal) {
927 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
928 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
929 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
930 }
931 if (err == 0)
932 err = err2;
Al Viro8cae6f72012-07-19 11:19:07 +0400933 mnt_drop_write_file(filp);
Theodore Ts'o7f511862013-01-13 08:41:45 -0500934 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
935 ext4_has_group_desc_csum(sb) &&
936 test_opt(sb, INIT_INODE_TABLE))
937 err = ext4_register_li_request(sb, o_group);
938
Yongqiang Yang19c52462012-01-04 17:09:44 -0500939resizefs_out:
940 ext4_resize_end(sb);
941 return err;
942 }
943
Lukas Czernere681c042010-11-19 21:47:07 -0500944 case FITRIM:
945 {
Lukas Czerner41431792011-02-23 12:42:32 -0500946 struct request_queue *q = bdev_get_queue(sb->s_bdev);
Lukas Czernere681c042010-11-19 21:47:07 -0500947 struct fstrim_range range;
948 int ret = 0;
949
950 if (!capable(CAP_SYS_ADMIN))
951 return -EPERM;
952
Lukas Czerner41431792011-02-23 12:42:32 -0500953 if (!blk_queue_discard(q))
954 return -EOPNOTSUPP;
955
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400956 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czernere681c042010-11-19 21:47:07 -0500957 sizeof(range)))
958 return -EFAULT;
959
Lukas Czerner5c2ed622011-02-23 17:49:51 -0500960 range.minlen = max((unsigned int)range.minlen,
961 q->limits.discard_granularity);
Lukas Czernere681c042010-11-19 21:47:07 -0500962 ret = ext4_trim_fs(sb, &range);
963 if (ret < 0)
964 return ret;
965
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400966 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czernere681c042010-11-19 21:47:07 -0500967 sizeof(range)))
968 return -EFAULT;
969
970 return 0;
971 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400972 case EXT4_IOC_PRECACHE_EXTENTS:
973 return ext4_ext_precache(inode);
Lukas Czernere681c042010-11-19 21:47:07 -0500974
Eric Biggersdb717d82016-11-26 19:07:49 -0500975 case EXT4_IOC_SET_ENCRYPTION_POLICY:
Richard Weinberger9a200d02016-09-30 01:49:55 -0400976 if (!ext4_has_feature_encrypt(sb))
977 return -EOPNOTSUPP;
Eric Biggersdb717d82016-11-26 19:07:49 -0500978 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
Richard Weinberger9a200d02016-09-30 01:49:55 -0400979
Michael Halcrow9bd82122015-04-11 07:48:01 -0400980 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
Eric Biggersba679012016-12-01 11:55:51 -0500981#ifdef CONFIG_EXT4_FS_ENCRYPTION
Michael Halcrow9bd82122015-04-11 07:48:01 -0400982 int err, err2;
983 struct ext4_sb_info *sbi = EXT4_SB(sb);
984 handle_t *handle;
985
Eric Biggers35997d12016-12-01 11:54:18 -0500986 if (!ext4_has_feature_encrypt(sb))
Michael Halcrow9bd82122015-04-11 07:48:01 -0400987 return -EOPNOTSUPP;
988 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
989 err = mnt_want_write_file(filp);
990 if (err)
991 return err;
992 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
993 if (IS_ERR(handle)) {
994 err = PTR_ERR(handle);
995 goto pwsalt_err_exit;
996 }
997 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
998 if (err)
999 goto pwsalt_err_journal;
1000 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1001 err = ext4_handle_dirty_metadata(handle, NULL,
1002 sbi->s_sbh);
1003 pwsalt_err_journal:
1004 err2 = ext4_journal_stop(handle);
1005 if (err2 && !err)
1006 err = err2;
1007 pwsalt_err_exit:
1008 mnt_drop_write_file(filp);
1009 if (err)
1010 return err;
1011 }
Fabian Frederickb4ab9e22015-06-08 12:23:21 -04001012 if (copy_to_user((void __user *) arg,
1013 sbi->s_es->s_encrypt_pw_salt, 16))
Michael Halcrow9bd82122015-04-11 07:48:01 -04001014 return -EFAULT;
1015 return 0;
Eric Biggersba679012016-12-01 11:55:51 -05001016#else
1017 return -EOPNOTSUPP;
1018#endif
Michael Halcrow9bd82122015-04-11 07:48:01 -04001019 }
Eric Biggersdb717d82016-11-26 19:07:49 -05001020 case EXT4_IOC_GET_ENCRYPTION_POLICY:
1021 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
Michael Halcrow9bd82122015-04-11 07:48:01 -04001022
Li Xi9b7365f2016-01-08 16:01:22 -05001023 case EXT4_IOC_FSGETXATTR:
1024 {
1025 struct fsxattr fa;
1026
1027 memset(&fa, 0, sizeof(struct fsxattr));
Li Xi9b7365f2016-01-08 16:01:22 -05001028 fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE);
1029
Kaho Ng0b7b7772016-09-05 23:11:58 -04001030 if (ext4_has_feature_project(inode->i_sb)) {
Li Xi9b7365f2016-01-08 16:01:22 -05001031 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
1032 EXT4_I(inode)->i_projid);
1033 }
1034
1035 if (copy_to_user((struct fsxattr __user *)arg,
1036 &fa, sizeof(fa)))
1037 return -EFAULT;
1038 return 0;
1039 }
1040 case EXT4_IOC_FSSETXATTR:
1041 {
1042 struct fsxattr fa;
1043 int err;
1044
1045 if (copy_from_user(&fa, (struct fsxattr __user *)arg,
1046 sizeof(fa)))
1047 return -EFAULT;
1048
1049 /* Make sure caller has proper permission */
1050 if (!inode_owner_or_capable(inode))
1051 return -EACCES;
1052
Jan Karad14e7682016-11-29 11:18:39 -05001053 if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS)
1054 return -EOPNOTSUPP;
1055
1056 flags = ext4_xflags_to_iflags(fa.fsx_xflags);
1057 if (ext4_mask_flags(inode->i_mode, flags) != flags)
1058 return -EOPNOTSUPP;
1059
Li Xi9b7365f2016-01-08 16:01:22 -05001060 err = mnt_want_write_file(filp);
1061 if (err)
1062 return err;
1063
Al Viro59551022016-01-22 15:40:57 -05001064 inode_lock(inode);
Wang Shilongdc7ac6c2018-10-03 10:33:32 -04001065 err = ext4_ioctl_check_project(inode, &fa);
1066 if (err)
1067 goto out;
Li Xi9b7365f2016-01-08 16:01:22 -05001068 flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) |
1069 (flags & EXT4_FL_XFLAG_VISIBLE);
1070 err = ext4_ioctl_setflags(inode, flags);
Wang Shilongdc7ac6c2018-10-03 10:33:32 -04001071 if (err)
1072 goto out;
1073 err = ext4_ioctl_setproject(filp, fa.fsx_projid);
1074out:
Al Viro59551022016-01-22 15:40:57 -05001075 inode_unlock(inode);
Li Xi9b7365f2016-01-08 16:01:22 -05001076 mnt_drop_write_file(filp);
Wang Shilongdc7ac6c2018-10-03 10:33:32 -04001077 return err;
Li Xi9b7365f2016-01-08 16:01:22 -05001078 }
Theodore Ts'oe9be2ac2017-02-20 15:34:59 -05001079 case EXT4_IOC_SHUTDOWN:
1080 return ext4_shutdown(sb, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001081 default:
1082 return -ENOTTY;
1083 }
1084}
1085
1086#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07001087long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001088{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 /* These are just misnamed, they actually get/put from/to user an int */
1090 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -07001091 case EXT4_IOC32_GETFLAGS:
1092 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001094 case EXT4_IOC32_SETFLAGS:
1095 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001097 case EXT4_IOC32_GETVERSION:
1098 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001099 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001100 case EXT4_IOC32_SETVERSION:
1101 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001103 case EXT4_IOC32_GROUP_EXTEND:
1104 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001105 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001106 case EXT4_IOC32_GETVERSION_OLD:
1107 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001109 case EXT4_IOC32_SETVERSION_OLD:
1110 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001111 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001112 case EXT4_IOC32_GETRSVSZ:
1113 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001115 case EXT4_IOC32_SETRSVSZ:
1116 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 break;
Ben Hutchings4d92dc02010-05-17 06:00:00 -04001118 case EXT4_IOC32_GROUP_ADD: {
1119 struct compat_ext4_new_group_input __user *uinput;
Al Viroe145b352017-09-29 21:39:59 -04001120 struct ext4_new_group_data input;
Ben Hutchings4d92dc02010-05-17 06:00:00 -04001121 int err;
1122
1123 uinput = compat_ptr(arg);
1124 err = get_user(input.group, &uinput->group);
1125 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
1126 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
1127 err |= get_user(input.inode_table, &uinput->inode_table);
1128 err |= get_user(input.blocks_count, &uinput->blocks_count);
1129 err |= get_user(input.reserved_blocks,
1130 &uinput->reserved_blocks);
1131 if (err)
1132 return -EFAULT;
Al Viroe145b352017-09-29 21:39:59 -04001133 return ext4_ioctl_group_add(file, &input);
Ben Hutchings4d92dc02010-05-17 06:00:00 -04001134 }
Christian Borntraegerb684b2e2010-05-15 00:00:00 -04001135 case EXT4_IOC_MOVE_EXT:
Yongqiang Yang19c52462012-01-04 17:09:44 -05001136 case EXT4_IOC_RESIZE_FS:
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04001137 case EXT4_IOC_PRECACHE_EXTENTS:
Michael Halcrow9bd82122015-04-11 07:48:01 -04001138 case EXT4_IOC_SET_ENCRYPTION_POLICY:
1139 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
1140 case EXT4_IOC_GET_ENCRYPTION_POLICY:
Theodore Ts'oe9be2ac2017-02-20 15:34:59 -05001141 case EXT4_IOC_SHUTDOWN:
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -04001142 case FS_IOC_GETFSMAP:
Christian Borntraegerb684b2e2010-05-15 00:00:00 -04001143 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001144 default:
1145 return -ENOIOCTLCMD;
1146 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -04001147 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148}
1149#endif