Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
ac27a0ec | 2 | /* |
617ba13b | 3 | * linux/fs/ext4/ioctl.c |
ac27a0ec DK |
4 | * |
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> | |
ac27a0ec | 12 | #include <linux/capability.h> |
ac27a0ec DK |
13 | #include <linux/time.h> |
14 | #include <linux/compat.h> | |
42a74f20 | 15 | #include <linux/mount.h> |
748de673 | 16 | #include <linux/file.h> |
9b7365fc | 17 | #include <linux/quotaops.h> |
23253068 | 18 | #include <linux/random.h> |
8da4b8c4 | 19 | #include <linux/uuid.h> |
7c0f6ba6 | 20 | #include <linux/uaccess.h> |
783d9485 | 21 | #include <linux/delay.h> |
ee73f9a5 | 22 | #include <linux/iversion.h> |
3dcf5451 CH |
23 | #include "ext4_jbd2.h" |
24 | #include "ext4.h" | |
0c9ec4be DW |
25 | #include <linux/fsmap.h> |
26 | #include "fsmap.h" | |
27 | #include <trace/events/ext4.h> | |
ac27a0ec | 28 | |
393d1d1d DTB |
29 | /** |
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 | */ | |
37 | static void memswap(void *a, void *b, size_t len) | |
38 | { | |
39 | unsigned char *ap, *bp; | |
393d1d1d DTB |
40 | |
41 | ap = (unsigned char *)a; | |
42 | bp = (unsigned char *)b; | |
43 | while (len-- > 0) { | |
4b7e2db5 | 44 | swap(*ap, *bp); |
393d1d1d DTB |
45 | 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 | */ | |
61 | static 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; | |
abdc644e | 66 | unsigned long tmp; |
393d1d1d DTB |
67 | |
68 | ei1 = EXT4_I(inode1); | |
69 | ei2 = EXT4_I(inode2); | |
70 | ||
9c5d58fb | 71 | swap(inode1->i_version, inode2->i_version); |
9c5d58fb JL |
72 | swap(inode1->i_atime, inode2->i_atime); |
73 | swap(inode1->i_mtime, inode2->i_mtime); | |
393d1d1d DTB |
74 | |
75 | memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data)); | |
abdc644e | 76 | tmp = ei1->i_flags & EXT4_FL_SHOULD_SWAP; |
77 | ei1->i_flags = (ei2->i_flags & EXT4_FL_SHOULD_SWAP) | | |
78 | (ei1->i_flags & ~EXT4_FL_SHOULD_SWAP); | |
79 | ei2->i_flags = tmp | (ei2->i_flags & ~EXT4_FL_SHOULD_SWAP); | |
9c5d58fb | 80 | swap(ei1->i_disksize, ei2->i_disksize); |
cde2d7a7 TT |
81 | ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS); |
82 | ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS); | |
393d1d1d DTB |
83 | |
84 | isize = i_size_read(inode1); | |
85 | i_size_write(inode1, i_size_read(inode2)); | |
86 | i_size_write(inode2, isize); | |
87 | } | |
88 | ||
8016e29f | 89 | void ext4_reset_inode_seed(struct inode *inode) |
18aded17 TT |
90 | { |
91 | struct ext4_inode_info *ei = EXT4_I(inode); | |
92 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
93 | __le32 inum = cpu_to_le32(inode->i_ino); | |
94 | __le32 gen = cpu_to_le32(inode->i_generation); | |
95 | __u32 csum; | |
96 | ||
97 | if (!ext4_has_metadata_csum(inode->i_sb)) | |
98 | return; | |
99 | ||
100 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, sizeof(inum)); | |
101 | ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, sizeof(gen)); | |
102 | } | |
103 | ||
393d1d1d DTB |
104 | /** |
105 | * Swap the information from the given @inode and the inode | |
106 | * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other | |
107 | * important fields of the inodes. | |
108 | * | |
109 | * @sb: the super block of the filesystem | |
110 | * @inode: the inode to swap with EXT4_BOOT_LOADER_INO | |
111 | * | |
112 | */ | |
113 | static long swap_inode_boot_loader(struct super_block *sb, | |
114 | struct inode *inode) | |
115 | { | |
116 | handle_t *handle; | |
117 | int err; | |
118 | struct inode *inode_bl; | |
393d1d1d | 119 | struct ext4_inode_info *ei_bl; |
aa507b5f | 120 | qsize_t size, size_bl, diff; |
121 | blkcnt_t blocks; | |
122 | unsigned short bytes; | |
393d1d1d | 123 | |
8a363970 | 124 | inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO, EXT4_IGET_SPECIAL); |
d8558a29 TT |
125 | if (IS_ERR(inode_bl)) |
126 | return PTR_ERR(inode_bl); | |
393d1d1d DTB |
127 | ei_bl = EXT4_I(inode_bl); |
128 | ||
393d1d1d DTB |
129 | /* Protect orig inodes against a truncate and make sure, |
130 | * that only 1 swap_inode_boot_loader is running. */ | |
375e289e | 131 | lock_two_nondirectories(inode, inode_bl); |
393d1d1d | 132 | |
67a11611 | 133 | if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode) || |
134 | IS_SWAPFILE(inode) || IS_ENCRYPTED(inode) || | |
6e589291 | 135 | (EXT4_I(inode)->i_flags & EXT4_JOURNAL_DATA_FL) || |
67a11611 | 136 | ext4_has_inline_data(inode)) { |
137 | err = -EINVAL; | |
138 | goto journal_err_out; | |
139 | } | |
140 | ||
141 | if (IS_RDONLY(inode) || IS_APPEND(inode) || IS_IMMUTABLE(inode) || | |
142 | !inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) { | |
143 | err = -EPERM; | |
144 | goto journal_err_out; | |
145 | } | |
146 | ||
a46c68a3 | 147 | down_write(&EXT4_I(inode)->i_mmap_sem); |
148 | err = filemap_write_and_wait(inode->i_mapping); | |
149 | if (err) | |
150 | goto err_out; | |
151 | ||
152 | err = filemap_write_and_wait(inode_bl->i_mapping); | |
153 | if (err) | |
154 | goto err_out; | |
155 | ||
393d1d1d | 156 | /* Wait for all existing dio workers */ |
393d1d1d DTB |
157 | inode_dio_wait(inode); |
158 | inode_dio_wait(inode_bl); | |
159 | ||
18aded17 TT |
160 | truncate_inode_pages(&inode->i_data, 0); |
161 | truncate_inode_pages(&inode_bl->i_data, 0); | |
162 | ||
393d1d1d DTB |
163 | handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2); |
164 | if (IS_ERR(handle)) { | |
165 | err = -EINVAL; | |
a46c68a3 | 166 | goto err_out; |
393d1d1d | 167 | } |
aa75f4d3 | 168 | ext4_fc_start_ineligible(sb, EXT4_FC_REASON_SWAP_BOOT); |
393d1d1d DTB |
169 | |
170 | /* Protect extent tree against block allocations via delalloc */ | |
171 | ext4_double_down_write_data_sem(inode, inode_bl); | |
172 | ||
173 | if (inode_bl->i_nlink == 0) { | |
174 | /* this inode has never been used as a BOOT_LOADER */ | |
175 | set_nlink(inode_bl, 1); | |
176 | i_uid_write(inode_bl, 0); | |
177 | i_gid_write(inode_bl, 0); | |
178 | inode_bl->i_flags = 0; | |
179 | ei_bl->i_flags = 0; | |
ee73f9a5 | 180 | inode_set_iversion(inode_bl, 1); |
393d1d1d DTB |
181 | i_size_write(inode_bl, 0); |
182 | inode_bl->i_mode = S_IFREG; | |
e2b911c5 | 183 | if (ext4_has_feature_extents(sb)) { |
393d1d1d DTB |
184 | ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS); |
185 | ext4_ext_tree_init(handle, inode_bl); | |
186 | } else | |
187 | memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data)); | |
188 | } | |
189 | ||
aa507b5f | 190 | err = dquot_initialize(inode); |
191 | if (err) | |
192 | goto err_out1; | |
193 | ||
194 | size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes; | |
195 | size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes; | |
196 | diff = size - size_bl; | |
393d1d1d DTB |
197 | swap_inode_data(inode, inode_bl); |
198 | ||
eeca7ea1 | 199 | inode->i_ctime = inode_bl->i_ctime = current_time(inode); |
393d1d1d | 200 | |
23253068 TT |
201 | inode->i_generation = prandom_u32(); |
202 | inode_bl->i_generation = prandom_u32(); | |
8016e29f HS |
203 | ext4_reset_inode_seed(inode); |
204 | ext4_reset_inode_seed(inode_bl); | |
393d1d1d | 205 | |
27bc446e | 206 | ext4_discard_preallocations(inode, 0); |
393d1d1d DTB |
207 | |
208 | err = ext4_mark_inode_dirty(handle, inode); | |
209 | if (err < 0) { | |
aa507b5f | 210 | /* No need to update quota information. */ |
393d1d1d DTB |
211 | ext4_warning(inode->i_sb, |
212 | "couldn't mark inode #%lu dirty (err %d)", | |
213 | inode->i_ino, err); | |
214 | /* Revert all changes: */ | |
215 | swap_inode_data(inode, inode_bl); | |
18aded17 | 216 | ext4_mark_inode_dirty(handle, inode); |
aa507b5f | 217 | goto err_out1; |
218 | } | |
219 | ||
220 | blocks = inode_bl->i_blocks; | |
221 | bytes = inode_bl->i_bytes; | |
222 | inode_bl->i_blocks = inode->i_blocks; | |
223 | inode_bl->i_bytes = inode->i_bytes; | |
224 | err = ext4_mark_inode_dirty(handle, inode_bl); | |
225 | if (err < 0) { | |
226 | /* No need to update quota information. */ | |
227 | ext4_warning(inode_bl->i_sb, | |
228 | "couldn't mark inode #%lu dirty (err %d)", | |
229 | inode_bl->i_ino, err); | |
230 | goto revert; | |
231 | } | |
232 | ||
233 | /* Bootloader inode should not be counted into quota information. */ | |
234 | if (diff > 0) | |
235 | dquot_free_space(inode, diff); | |
236 | else | |
237 | err = dquot_alloc_space(inode, -1 * diff); | |
238 | ||
239 | if (err < 0) { | |
240 | revert: | |
241 | /* Revert all changes: */ | |
242 | inode_bl->i_blocks = blocks; | |
243 | inode_bl->i_bytes = bytes; | |
244 | swap_inode_data(inode, inode_bl); | |
245 | ext4_mark_inode_dirty(handle, inode); | |
246 | ext4_mark_inode_dirty(handle, inode_bl); | |
393d1d1d | 247 | } |
aa507b5f | 248 | |
249 | err_out1: | |
393d1d1d | 250 | ext4_journal_stop(handle); |
aa75f4d3 | 251 | ext4_fc_stop_ineligible(sb); |
393d1d1d DTB |
252 | ext4_double_up_write_data_sem(inode, inode_bl); |
253 | ||
a46c68a3 | 254 | err_out: |
255 | up_write(&EXT4_I(inode)->i_mmap_sem); | |
30d29b11 | 256 | journal_err_out: |
375e289e | 257 | unlock_two_nondirectories(inode, inode_bl); |
393d1d1d | 258 | iput(inode_bl); |
393d1d1d DTB |
259 | return err; |
260 | } | |
261 | ||
643fa961 | 262 | #ifdef CONFIG_FS_ENCRYPTION |
9bd8212f MH |
263 | static int uuid_is_zero(__u8 u[16]) |
264 | { | |
265 | int i; | |
266 | ||
267 | for (i = 0; i < 16; i++) | |
268 | if (u[i]) | |
269 | return 0; | |
270 | return 1; | |
271 | } | |
ba679017 | 272 | #endif |
9bd8212f | 273 | |
2e538403 DW |
274 | /* |
275 | * If immutable is set and we are not clearing it, we're not allowed to change | |
276 | * anything else in the inode. Don't error out if we're only trying to set | |
277 | * immutable on an immutable file. | |
278 | */ | |
279 | static int ext4_ioctl_check_immutable(struct inode *inode, __u32 new_projid, | |
280 | unsigned int flags) | |
281 | { | |
282 | struct ext4_inode_info *ei = EXT4_I(inode); | |
283 | unsigned int oldflags = ei->i_flags; | |
284 | ||
285 | if (!(oldflags & EXT4_IMMUTABLE_FL) || !(flags & EXT4_IMMUTABLE_FL)) | |
286 | return 0; | |
287 | ||
288 | if ((oldflags & ~EXT4_IMMUTABLE_FL) != (flags & ~EXT4_IMMUTABLE_FL)) | |
289 | return -EPERM; | |
290 | if (ext4_has_feature_project(inode->i_sb) && | |
291 | __kprojid_val(ei->i_projid) != new_projid) | |
292 | return -EPERM; | |
293 | ||
294 | return 0; | |
295 | } | |
296 | ||
b383a73f IW |
297 | static void ext4_dax_dontcache(struct inode *inode, unsigned int flags) |
298 | { | |
299 | struct ext4_inode_info *ei = EXT4_I(inode); | |
300 | ||
301 | if (S_ISDIR(inode->i_mode)) | |
302 | return; | |
303 | ||
304 | if (test_opt2(inode->i_sb, DAX_NEVER) || | |
305 | test_opt(inode->i_sb, DAX_ALWAYS)) | |
306 | return; | |
307 | ||
308 | if ((ei->i_flags ^ flags) & EXT4_DAX_FL) | |
309 | d_mark_dontcache(inode); | |
310 | } | |
311 | ||
312 | static bool dax_compatible(struct inode *inode, unsigned int oldflags, | |
313 | unsigned int flags) | |
314 | { | |
315 | if (flags & EXT4_DAX_FL) { | |
316 | if ((oldflags & EXT4_DAX_MUT_EXCL) || | |
317 | ext4_test_inode_state(inode, | |
318 | EXT4_STATE_VERITY_IN_PROGRESS)) { | |
319 | return false; | |
320 | } | |
321 | } | |
322 | ||
323 | if ((flags & EXT4_DAX_MUT_EXCL) && (oldflags & EXT4_DAX_FL)) | |
324 | return false; | |
325 | ||
326 | return true; | |
327 | } | |
328 | ||
9b7365fc LX |
329 | static int ext4_ioctl_setflags(struct inode *inode, |
330 | unsigned int flags) | |
331 | { | |
332 | struct ext4_inode_info *ei = EXT4_I(inode); | |
333 | handle_t *handle = NULL; | |
fdde368e | 334 | int err = -EPERM, migrate = 0; |
9b7365fc LX |
335 | struct ext4_iloc iloc; |
336 | unsigned int oldflags, mask, i; | |
b886ee3e | 337 | struct super_block *sb = inode->i_sb; |
9b7365fc LX |
338 | |
339 | /* Is it quota file? Do not allow user to mess with it */ | |
02749a4c | 340 | if (ext4_is_quota_file(inode)) |
9b7365fc LX |
341 | goto flags_out; |
342 | ||
343 | oldflags = ei->i_flags; | |
344 | ||
5aca2842 DW |
345 | err = vfs_ioc_setflags_prepare(inode, oldflags, flags); |
346 | if (err) | |
347 | goto flags_out; | |
9b7365fc LX |
348 | |
349 | /* | |
350 | * The JOURNAL_DATA flag can only be changed by | |
351 | * the relevant capability. | |
352 | */ | |
fcebc794 | 353 | if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
9b7365fc LX |
354 | if (!capable(CAP_SYS_RESOURCE)) |
355 | goto flags_out; | |
356 | } | |
b383a73f IW |
357 | |
358 | if (!dax_compatible(inode, oldflags, flags)) { | |
359 | err = -EOPNOTSUPP; | |
360 | goto flags_out; | |
361 | } | |
362 | ||
9b7365fc LX |
363 | if ((flags ^ oldflags) & EXT4_EXTENTS_FL) |
364 | migrate = 1; | |
365 | ||
b886ee3e GKB |
366 | if ((flags ^ oldflags) & EXT4_CASEFOLD_FL) { |
367 | if (!ext4_has_feature_casefold(sb)) { | |
368 | err = -EOPNOTSUPP; | |
369 | goto flags_out; | |
370 | } | |
371 | ||
372 | if (!S_ISDIR(inode->i_mode)) { | |
373 | err = -ENOTDIR; | |
374 | goto flags_out; | |
375 | } | |
376 | ||
377 | if (!ext4_empty_dir(inode)) { | |
378 | err = -ENOTEMPTY; | |
379 | goto flags_out; | |
380 | } | |
381 | } | |
382 | ||
2e538403 DW |
383 | /* |
384 | * Wait for all pending directio and then flush all the dirty pages | |
385 | * for this file. The flush marks all the pages readonly, so any | |
386 | * subsequent attempt to write to the file (particularly mmap pages) | |
387 | * will come through the filesystem and fail. | |
388 | */ | |
389 | if (S_ISREG(inode->i_mode) && !IS_IMMUTABLE(inode) && | |
390 | (flags & EXT4_IMMUTABLE_FL)) { | |
391 | inode_dio_wait(inode); | |
392 | err = filemap_write_and_wait(inode->i_mapping); | |
393 | if (err) | |
394 | goto flags_out; | |
395 | } | |
396 | ||
9b7365fc LX |
397 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
398 | if (IS_ERR(handle)) { | |
399 | err = PTR_ERR(handle); | |
400 | goto flags_out; | |
401 | } | |
402 | if (IS_SYNC(inode)) | |
403 | ext4_handle_sync(handle); | |
404 | err = ext4_reserve_inode_write(handle, inode, &iloc); | |
405 | if (err) | |
406 | goto flags_err; | |
407 | ||
b383a73f IW |
408 | ext4_dax_dontcache(inode, flags); |
409 | ||
9b7365fc LX |
410 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { |
411 | if (!(mask & EXT4_FL_USER_MODIFIABLE)) | |
412 | continue; | |
f8011d93 JK |
413 | /* These flags get special treatment later */ |
414 | if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL) | |
415 | continue; | |
9b7365fc LX |
416 | if (mask & flags) |
417 | ext4_set_inode_flag(inode, i); | |
418 | else | |
419 | ext4_clear_inode_flag(inode, i); | |
420 | } | |
421 | ||
043546e4 IW |
422 | ext4_set_inode_flags(inode, false); |
423 | ||
eeca7ea1 | 424 | inode->i_ctime = current_time(inode); |
9b7365fc LX |
425 | |
426 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); | |
427 | flags_err: | |
428 | ext4_journal_stop(handle); | |
429 | if (err) | |
430 | goto flags_out; | |
431 | ||
fcebc794 | 432 | if ((flags ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
e9072d85 RZ |
433 | /* |
434 | * Changes to the journaling mode can cause unsafe changes to | |
ff694ab6 | 435 | * S_DAX if the inode is DAX |
e9072d85 | 436 | */ |
ff694ab6 | 437 | if (IS_DAX(inode)) { |
e9072d85 RZ |
438 | err = -EBUSY; |
439 | goto flags_out; | |
440 | } | |
441 | ||
fcebc794 IW |
442 | err = ext4_change_inode_journal_flag(inode, |
443 | flags & EXT4_JOURNAL_DATA_FL); | |
e9072d85 RZ |
444 | if (err) |
445 | goto flags_out; | |
446 | } | |
9b7365fc LX |
447 | if (migrate) { |
448 | if (flags & EXT4_EXTENTS_FL) | |
449 | err = ext4_ext_migrate(inode); | |
450 | else | |
451 | err = ext4_ind_migrate(inode); | |
452 | } | |
453 | ||
454 | flags_out: | |
455 | return err; | |
456 | } | |
457 | ||
458 | #ifdef CONFIG_QUOTA | |
459 | static int ext4_ioctl_setproject(struct file *filp, __u32 projid) | |
460 | { | |
461 | struct inode *inode = file_inode(filp); | |
462 | struct super_block *sb = inode->i_sb; | |
463 | struct ext4_inode_info *ei = EXT4_I(inode); | |
464 | int err, rc; | |
465 | handle_t *handle; | |
466 | kprojid_t kprojid; | |
467 | struct ext4_iloc iloc; | |
468 | struct ext4_inode *raw_inode; | |
079788d0 | 469 | struct dquot *transfer_to[MAXQUOTAS] = { }; |
9b7365fc | 470 | |
0b7b7779 | 471 | if (!ext4_has_feature_project(sb)) { |
9b7365fc LX |
472 | if (projid != EXT4_DEF_PROJID) |
473 | return -EOPNOTSUPP; | |
474 | else | |
475 | return 0; | |
476 | } | |
477 | ||
478 | if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE) | |
479 | return -EOPNOTSUPP; | |
480 | ||
481 | kprojid = make_kprojid(&init_user_ns, (projid_t)projid); | |
482 | ||
483 | if (projid_eq(kprojid, EXT4_I(inode)->i_projid)) | |
484 | return 0; | |
485 | ||
9b7365fc | 486 | err = -EPERM; |
9b7365fc | 487 | /* Is it quota file? Do not allow user to mess with it */ |
02749a4c | 488 | if (ext4_is_quota_file(inode)) |
dc7ac6c4 | 489 | return err; |
9b7365fc LX |
490 | |
491 | err = ext4_get_inode_loc(inode, &iloc); | |
492 | if (err) | |
dc7ac6c4 | 493 | return err; |
9b7365fc LX |
494 | |
495 | raw_inode = ext4_raw_inode(&iloc); | |
496 | if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) { | |
c03b45b8 MX |
497 | err = ext4_expand_extra_isize(inode, |
498 | EXT4_SB(sb)->s_want_extra_isize, | |
499 | &iloc); | |
500 | if (err) | |
dc7ac6c4 | 501 | return err; |
c03b45b8 | 502 | } else { |
9b7365fc | 503 | brelse(iloc.bh); |
9b7365fc | 504 | } |
9b7365fc | 505 | |
182a79e0 WS |
506 | err = dquot_initialize(inode); |
507 | if (err) | |
508 | return err; | |
9b7365fc LX |
509 | |
510 | handle = ext4_journal_start(inode, EXT4_HT_QUOTA, | |
511 | EXT4_QUOTA_INIT_BLOCKS(sb) + | |
512 | EXT4_QUOTA_DEL_BLOCKS(sb) + 3); | |
dc7ac6c4 WS |
513 | if (IS_ERR(handle)) |
514 | return PTR_ERR(handle); | |
9b7365fc LX |
515 | |
516 | err = ext4_reserve_inode_write(handle, inode, &iloc); | |
517 | if (err) | |
518 | goto out_stop; | |
519 | ||
079788d0 WS |
520 | transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); |
521 | if (!IS_ERR(transfer_to[PRJQUOTA])) { | |
7a9ca53a TE |
522 | |
523 | /* __dquot_transfer() calls back ext4_get_inode_usage() which | |
524 | * counts xattr inode references. | |
525 | */ | |
526 | down_read(&EXT4_I(inode)->xattr_sem); | |
079788d0 | 527 | err = __dquot_transfer(inode, transfer_to); |
7a9ca53a | 528 | up_read(&EXT4_I(inode)->xattr_sem); |
079788d0 WS |
529 | dqput(transfer_to[PRJQUOTA]); |
530 | if (err) | |
531 | goto out_dirty; | |
9b7365fc | 532 | } |
079788d0 | 533 | |
9b7365fc | 534 | EXT4_I(inode)->i_projid = kprojid; |
eeca7ea1 | 535 | inode->i_ctime = current_time(inode); |
9b7365fc LX |
536 | out_dirty: |
537 | rc = ext4_mark_iloc_dirty(handle, inode, &iloc); | |
538 | if (!err) | |
539 | err = rc; | |
540 | out_stop: | |
541 | ext4_journal_stop(handle); | |
9b7365fc LX |
542 | return err; |
543 | } | |
544 | #else | |
545 | static int ext4_ioctl_setproject(struct file *filp, __u32 projid) | |
546 | { | |
547 | if (projid != EXT4_DEF_PROJID) | |
548 | return -EOPNOTSUPP; | |
549 | return 0; | |
550 | } | |
551 | #endif | |
552 | ||
553 | /* Transfer internal flags to xflags */ | |
554 | static inline __u32 ext4_iflags_to_xflags(unsigned long iflags) | |
555 | { | |
556 | __u32 xflags = 0; | |
557 | ||
558 | if (iflags & EXT4_SYNC_FL) | |
559 | xflags |= FS_XFLAG_SYNC; | |
560 | if (iflags & EXT4_IMMUTABLE_FL) | |
561 | xflags |= FS_XFLAG_IMMUTABLE; | |
562 | if (iflags & EXT4_APPEND_FL) | |
563 | xflags |= FS_XFLAG_APPEND; | |
564 | if (iflags & EXT4_NODUMP_FL) | |
565 | xflags |= FS_XFLAG_NODUMP; | |
566 | if (iflags & EXT4_NOATIME_FL) | |
567 | xflags |= FS_XFLAG_NOATIME; | |
568 | if (iflags & EXT4_PROJINHERIT_FL) | |
569 | xflags |= FS_XFLAG_PROJINHERIT; | |
b383a73f IW |
570 | if (iflags & EXT4_DAX_FL) |
571 | xflags |= FS_XFLAG_DAX; | |
9b7365fc LX |
572 | return xflags; |
573 | } | |
574 | ||
d14e7683 JK |
575 | #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \ |
576 | FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \ | |
b383a73f IW |
577 | FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT | \ |
578 | FS_XFLAG_DAX) | |
d14e7683 | 579 | |
9b7365fc LX |
580 | /* Transfer xflags flags to internal */ |
581 | static inline unsigned long ext4_xflags_to_iflags(__u32 xflags) | |
582 | { | |
583 | unsigned long iflags = 0; | |
584 | ||
585 | if (xflags & FS_XFLAG_SYNC) | |
586 | iflags |= EXT4_SYNC_FL; | |
587 | if (xflags & FS_XFLAG_IMMUTABLE) | |
588 | iflags |= EXT4_IMMUTABLE_FL; | |
589 | if (xflags & FS_XFLAG_APPEND) | |
590 | iflags |= EXT4_APPEND_FL; | |
591 | if (xflags & FS_XFLAG_NODUMP) | |
592 | iflags |= EXT4_NODUMP_FL; | |
593 | if (xflags & FS_XFLAG_NOATIME) | |
594 | iflags |= EXT4_NOATIME_FL; | |
595 | if (xflags & FS_XFLAG_PROJINHERIT) | |
596 | iflags |= EXT4_PROJINHERIT_FL; | |
b383a73f IW |
597 | if (xflags & FS_XFLAG_DAX) |
598 | iflags |= EXT4_DAX_FL; | |
9b7365fc LX |
599 | |
600 | return iflags; | |
601 | } | |
602 | ||
1a20a630 | 603 | static int ext4_shutdown(struct super_block *sb, unsigned long arg) |
783d9485 TT |
604 | { |
605 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
606 | __u32 flags; | |
607 | ||
608 | if (!capable(CAP_SYS_ADMIN)) | |
609 | return -EPERM; | |
610 | ||
611 | if (get_user(flags, (__u32 __user *)arg)) | |
612 | return -EFAULT; | |
613 | ||
614 | if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH) | |
615 | return -EINVAL; | |
616 | ||
617 | if (ext4_forced_shutdown(sbi)) | |
618 | return 0; | |
619 | ||
620 | ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags); | |
ccf0f32a | 621 | trace_ext4_shutdown(sb, flags); |
783d9485 TT |
622 | |
623 | switch (flags) { | |
624 | case EXT4_GOING_FLAGS_DEFAULT: | |
625 | freeze_bdev(sb->s_bdev); | |
626 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); | |
040f04bd | 627 | thaw_bdev(sb->s_bdev); |
783d9485 TT |
628 | break; |
629 | case EXT4_GOING_FLAGS_LOGFLUSH: | |
630 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); | |
631 | if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) { | |
632 | (void) ext4_force_commit(sb); | |
fb7c0244 | 633 | jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN); |
783d9485 TT |
634 | } |
635 | break; | |
636 | case EXT4_GOING_FLAGS_NOLOGFLUSH: | |
637 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); | |
a6d9946b | 638 | if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) |
fb7c0244 | 639 | jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN); |
783d9485 TT |
640 | break; |
641 | default: | |
642 | return -EINVAL; | |
643 | } | |
644 | clear_opt(sb, DISCARD); | |
645 | return 0; | |
646 | } | |
647 | ||
0c9ec4be DW |
648 | struct getfsmap_info { |
649 | struct super_block *gi_sb; | |
650 | struct fsmap_head __user *gi_data; | |
651 | unsigned int gi_idx; | |
652 | __u32 gi_last_flags; | |
653 | }; | |
654 | ||
655 | static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv) | |
656 | { | |
657 | struct getfsmap_info *info = priv; | |
658 | struct fsmap fm; | |
659 | ||
660 | trace_ext4_getfsmap_mapping(info->gi_sb, xfm); | |
661 | ||
662 | info->gi_last_flags = xfm->fmr_flags; | |
663 | ext4_fsmap_from_internal(info->gi_sb, &fm, xfm); | |
664 | if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm, | |
665 | sizeof(struct fsmap))) | |
666 | return -EFAULT; | |
667 | ||
668 | return 0; | |
669 | } | |
670 | ||
671 | static int ext4_ioc_getfsmap(struct super_block *sb, | |
672 | struct fsmap_head __user *arg) | |
673 | { | |
0ba33fac | 674 | struct getfsmap_info info = { NULL }; |
0c9ec4be DW |
675 | struct ext4_fsmap_head xhead = {0}; |
676 | struct fsmap_head head; | |
677 | bool aborted = false; | |
678 | int error; | |
679 | ||
680 | if (copy_from_user(&head, arg, sizeof(struct fsmap_head))) | |
681 | return -EFAULT; | |
682 | if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) || | |
683 | memchr_inv(head.fmh_keys[0].fmr_reserved, 0, | |
684 | sizeof(head.fmh_keys[0].fmr_reserved)) || | |
685 | memchr_inv(head.fmh_keys[1].fmr_reserved, 0, | |
686 | sizeof(head.fmh_keys[1].fmr_reserved))) | |
687 | return -EINVAL; | |
688 | /* | |
689 | * ext4 doesn't report file extents at all, so the only valid | |
690 | * file offsets are the magic ones (all zeroes or all ones). | |
691 | */ | |
692 | if (head.fmh_keys[0].fmr_offset || | |
693 | (head.fmh_keys[1].fmr_offset != 0 && | |
694 | head.fmh_keys[1].fmr_offset != -1ULL)) | |
695 | return -EINVAL; | |
696 | ||
697 | xhead.fmh_iflags = head.fmh_iflags; | |
698 | xhead.fmh_count = head.fmh_count; | |
699 | ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]); | |
700 | ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]); | |
701 | ||
702 | trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]); | |
703 | trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]); | |
704 | ||
705 | info.gi_sb = sb; | |
706 | info.gi_data = arg; | |
707 | error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info); | |
708 | if (error == EXT4_QUERY_RANGE_ABORT) { | |
709 | error = 0; | |
710 | aborted = true; | |
711 | } else if (error) | |
712 | return error; | |
713 | ||
714 | /* If we didn't abort, set the "last" flag in the last fmx */ | |
715 | if (!aborted && info.gi_idx) { | |
716 | info.gi_last_flags |= FMR_OF_LAST; | |
717 | if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags, | |
718 | &info.gi_last_flags, | |
719 | sizeof(info.gi_last_flags))) | |
720 | return -EFAULT; | |
721 | } | |
722 | ||
723 | /* copy back header */ | |
724 | head.fmh_entries = xhead.fmh_entries; | |
725 | head.fmh_oflags = xhead.fmh_oflags; | |
726 | if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) | |
727 | return -EFAULT; | |
728 | ||
729 | return 0; | |
730 | } | |
731 | ||
e145b35b AV |
732 | static long ext4_ioctl_group_add(struct file *file, |
733 | struct ext4_new_group_data *input) | |
734 | { | |
735 | struct super_block *sb = file_inode(file)->i_sb; | |
736 | int err, err2=0; | |
737 | ||
738 | err = ext4_resize_begin(sb); | |
739 | if (err) | |
740 | return err; | |
741 | ||
742 | if (ext4_has_feature_bigalloc(sb)) { | |
743 | ext4_msg(sb, KERN_ERR, | |
744 | "Online resizing not supported with bigalloc"); | |
745 | err = -EOPNOTSUPP; | |
746 | goto group_add_out; | |
747 | } | |
748 | ||
749 | err = mnt_want_write_file(file); | |
750 | if (err) | |
751 | goto group_add_out; | |
752 | ||
753 | err = ext4_group_add(sb, input); | |
754 | if (EXT4_SB(sb)->s_journal) { | |
755 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
756 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
757 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
758 | } | |
759 | if (err == 0) | |
760 | err = err2; | |
761 | mnt_drop_write_file(file); | |
762 | if (!err && ext4_has_group_desc_csum(sb) && | |
763 | test_opt(sb, INIT_INODE_TABLE)) | |
764 | err = ext4_register_li_request(sb, input->group); | |
765 | group_add_out: | |
766 | ext4_resize_end(sb); | |
767 | return err; | |
768 | } | |
769 | ||
7b0e492e | 770 | static void ext4_fill_fsxattr(struct inode *inode, struct fsxattr *fa) |
dc7ac6c4 | 771 | { |
7b0e492e | 772 | struct ext4_inode_info *ei = EXT4_I(inode); |
dc7ac6c4 | 773 | |
7b0e492e DW |
774 | simple_fill_fsxattr(fa, ext4_iflags_to_xflags(ei->i_flags & |
775 | EXT4_FL_USER_VISIBLE)); | |
dc7ac6c4 | 776 | |
7b0e492e DW |
777 | if (ext4_has_feature_project(inode->i_sb)) |
778 | fa->fsx_projid = from_kprojid(&init_user_ns, ei->i_projid); | |
dc7ac6c4 WS |
779 | } |
780 | ||
bb5835ed TT |
781 | /* So that the fiemap access checks can't overflow on 32 bit machines. */ |
782 | #define FIEMAP_MAX_EXTENTS (UINT_MAX / sizeof(struct fiemap_extent)) | |
783 | ||
784 | static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg) | |
785 | { | |
786 | struct fiemap fiemap; | |
787 | struct fiemap __user *ufiemap = (struct fiemap __user *) arg; | |
788 | struct fiemap_extent_info fieinfo = { 0, }; | |
789 | struct inode *inode = file_inode(filp); | |
bb5835ed TT |
790 | int error; |
791 | ||
792 | if (copy_from_user(&fiemap, ufiemap, sizeof(fiemap))) | |
793 | return -EFAULT; | |
794 | ||
795 | if (fiemap.fm_extent_count > FIEMAP_MAX_EXTENTS) | |
796 | return -EINVAL; | |
797 | ||
bb5835ed TT |
798 | fieinfo.fi_flags = fiemap.fm_flags; |
799 | fieinfo.fi_extents_max = fiemap.fm_extent_count; | |
800 | fieinfo.fi_extents_start = ufiemap->fm_extents; | |
801 | ||
328e24ae CH |
802 | error = ext4_get_es_cache(inode, &fieinfo, fiemap.fm_start, |
803 | fiemap.fm_length); | |
bb5835ed TT |
804 | fiemap.fm_flags = fieinfo.fi_flags; |
805 | fiemap.fm_mapped_extents = fieinfo.fi_extents_mapped; | |
806 | if (copy_to_user(ufiemap, &fiemap, sizeof(fiemap))) | |
807 | error = -EFAULT; | |
808 | ||
809 | return error; | |
810 | } | |
811 | ||
aa75f4d3 | 812 | static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
ac27a0ec | 813 | { |
496ad9aa | 814 | struct inode *inode = file_inode(filp); |
bab08ab9 | 815 | struct super_block *sb = inode->i_sb; |
617ba13b | 816 | struct ext4_inode_info *ei = EXT4_I(inode); |
ac27a0ec | 817 | unsigned int flags; |
ac27a0ec | 818 | |
af5bc92d | 819 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
ac27a0ec DK |
820 | |
821 | switch (cmd) { | |
0c9ec4be DW |
822 | case FS_IOC_GETFSMAP: |
823 | return ext4_ioc_getfsmap(sb, (void __user *)arg); | |
cb29a02d | 824 | case FS_IOC_GETFLAGS: |
617ba13b | 825 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
7ddf79a1 WS |
826 | if (S_ISREG(inode->i_mode)) |
827 | flags &= ~EXT4_PROJINHERIT_FL; | |
ac27a0ec | 828 | return put_user(flags, (int __user *) arg); |
cb29a02d | 829 | case FS_IOC_SETFLAGS: { |
9b7365fc | 830 | int err; |
ac27a0ec | 831 | |
2e149670 | 832 | if (!inode_owner_or_capable(inode)) |
ac27a0ec DK |
833 | return -EACCES; |
834 | ||
835 | if (get_user(flags, (int __user *) arg)) | |
836 | return -EFAULT; | |
837 | ||
d14e7683 JK |
838 | if (flags & ~EXT4_FL_USER_VISIBLE) |
839 | return -EOPNOTSUPP; | |
840 | /* | |
841 | * chattr(1) grabs flags via GETFLAGS, modifies the result and | |
842 | * passes that to SETFLAGS. So we cannot easily make SETFLAGS | |
843 | * more restrictive than just silently masking off visible but | |
844 | * not settable flags as we always did. | |
845 | */ | |
846 | flags &= EXT4_FL_USER_MODIFIABLE; | |
847 | if (ext4_mask_flags(inode->i_mode, flags) != flags) | |
848 | return -EOPNOTSUPP; | |
849 | ||
a561be71 | 850 | err = mnt_want_write_file(filp); |
42a74f20 DH |
851 | if (err) |
852 | return err; | |
853 | ||
5955102c | 854 | inode_lock(inode); |
2e538403 DW |
855 | err = ext4_ioctl_check_immutable(inode, |
856 | from_kprojid(&init_user_ns, ei->i_projid), | |
857 | flags); | |
858 | if (!err) | |
859 | err = ext4_ioctl_setflags(inode, flags); | |
5955102c | 860 | inode_unlock(inode); |
2a79f17e | 861 | mnt_drop_write_file(filp); |
ac27a0ec DK |
862 | return err; |
863 | } | |
617ba13b MC |
864 | case EXT4_IOC_GETVERSION: |
865 | case EXT4_IOC_GETVERSION_OLD: | |
ac27a0ec | 866 | return put_user(inode->i_generation, (int __user *) arg); |
617ba13b MC |
867 | case EXT4_IOC_SETVERSION: |
868 | case EXT4_IOC_SETVERSION_OLD: { | |
ac27a0ec | 869 | handle_t *handle; |
617ba13b | 870 | struct ext4_iloc iloc; |
ac27a0ec DK |
871 | __u32 generation; |
872 | int err; | |
873 | ||
2e149670 | 874 | if (!inode_owner_or_capable(inode)) |
ac27a0ec | 875 | return -EPERM; |
42a74f20 | 876 | |
9aa5d32b | 877 | if (ext4_has_metadata_csum(inode->i_sb)) { |
814525f4 DW |
878 | ext4_warning(sb, "Setting inode version is not " |
879 | "supported with metadata_csum enabled."); | |
880 | return -ENOTTY; | |
881 | } | |
882 | ||
a561be71 | 883 | err = mnt_want_write_file(filp); |
42a74f20 DH |
884 | if (err) |
885 | return err; | |
886 | if (get_user(generation, (int __user *) arg)) { | |
887 | err = -EFAULT; | |
888 | goto setversion_out; | |
889 | } | |
ac27a0ec | 890 | |
5955102c | 891 | inode_lock(inode); |
9924a92a | 892 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
42a74f20 DH |
893 | if (IS_ERR(handle)) { |
894 | err = PTR_ERR(handle); | |
6c2155b9 | 895 | goto unlock_out; |
42a74f20 | 896 | } |
617ba13b | 897 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec | 898 | if (err == 0) { |
eeca7ea1 | 899 | inode->i_ctime = current_time(inode); |
ac27a0ec | 900 | inode->i_generation = generation; |
617ba13b | 901 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 902 | } |
617ba13b | 903 | ext4_journal_stop(handle); |
6c2155b9 DH |
904 | |
905 | unlock_out: | |
5955102c | 906 | inode_unlock(inode); |
42a74f20 | 907 | setversion_out: |
2a79f17e | 908 | mnt_drop_write_file(filp); |
ac27a0ec DK |
909 | return err; |
910 | } | |
617ba13b MC |
911 | case EXT4_IOC_GROUP_EXTEND: { |
912 | ext4_fsblk_t n_blocks_count; | |
ac046f1d | 913 | int err, err2=0; |
ac27a0ec | 914 | |
8f82f840 YY |
915 | err = ext4_resize_begin(sb); |
916 | if (err) | |
917 | return err; | |
ac27a0ec | 918 | |
014a1770 DH |
919 | if (get_user(n_blocks_count, (__u32 __user *)arg)) { |
920 | err = -EFAULT; | |
921 | goto group_extend_out; | |
922 | } | |
ac27a0ec | 923 | |
e2b911c5 | 924 | if (ext4_has_feature_bigalloc(sb)) { |
bab08ab9 TT |
925 | ext4_msg(sb, KERN_ERR, |
926 | "Online resizing not supported with bigalloc"); | |
014a1770 DH |
927 | err = -EOPNOTSUPP; |
928 | goto group_extend_out; | |
bab08ab9 TT |
929 | } |
930 | ||
a561be71 | 931 | err = mnt_want_write_file(filp); |
42a74f20 | 932 | if (err) |
014a1770 | 933 | goto group_extend_out; |
42a74f20 | 934 | |
617ba13b | 935 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
ac046f1d PT |
936 | if (EXT4_SB(sb)->s_journal) { |
937 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
938 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
939 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
940 | } | |
7ffe1ea8 HK |
941 | if (err == 0) |
942 | err = err2; | |
2a79f17e | 943 | mnt_drop_write_file(filp); |
014a1770 | 944 | group_extend_out: |
8f82f840 | 945 | ext4_resize_end(sb); |
ac27a0ec DK |
946 | return err; |
947 | } | |
748de673 AF |
948 | |
949 | case EXT4_IOC_MOVE_EXT: { | |
950 | struct move_extent me; | |
2903ff01 AV |
951 | struct fd donor; |
952 | int err; | |
748de673 | 953 | |
4a58579b AF |
954 | if (!(filp->f_mode & FMODE_READ) || |
955 | !(filp->f_mode & FMODE_WRITE)) | |
956 | return -EBADF; | |
957 | ||
748de673 AF |
958 | if (copy_from_user(&me, |
959 | (struct move_extent __user *)arg, sizeof(me))) | |
960 | return -EFAULT; | |
4a58579b | 961 | me.moved_len = 0; |
748de673 | 962 | |
2903ff01 AV |
963 | donor = fdget(me.donor_fd); |
964 | if (!donor.file) | |
748de673 AF |
965 | return -EBADF; |
966 | ||
2903ff01 | 967 | if (!(donor.file->f_mode & FMODE_WRITE)) { |
4a58579b AF |
968 | err = -EBADF; |
969 | goto mext_out; | |
748de673 AF |
970 | } |
971 | ||
e2b911c5 | 972 | if (ext4_has_feature_bigalloc(sb)) { |
bab08ab9 TT |
973 | ext4_msg(sb, KERN_ERR, |
974 | "Online defrag not supported with bigalloc"); | |
399c9b86 AV |
975 | err = -EOPNOTSUPP; |
976 | goto mext_out; | |
73f34a5e RZ |
977 | } else if (IS_DAX(inode)) { |
978 | ext4_msg(sb, KERN_ERR, | |
979 | "Online defrag not supported with DAX"); | |
980 | err = -EOPNOTSUPP; | |
981 | goto mext_out; | |
bab08ab9 TT |
982 | } |
983 | ||
a561be71 | 984 | err = mnt_want_write_file(filp); |
4a58579b AF |
985 | if (err) |
986 | goto mext_out; | |
987 | ||
2903ff01 | 988 | err = ext4_move_extents(filp, donor.file, me.orig_start, |
748de673 | 989 | me.donor_start, me.len, &me.moved_len); |
2a79f17e | 990 | mnt_drop_write_file(filp); |
748de673 | 991 | |
60e6679e | 992 | if (copy_to_user((struct move_extent __user *)arg, |
c437b273 | 993 | &me, sizeof(me))) |
4a58579b AF |
994 | err = -EFAULT; |
995 | mext_out: | |
2903ff01 | 996 | fdput(donor); |
748de673 AF |
997 | return err; |
998 | } | |
999 | ||
617ba13b MC |
1000 | case EXT4_IOC_GROUP_ADD: { |
1001 | struct ext4_new_group_data input; | |
ac27a0ec | 1002 | |
617ba13b | 1003 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
e145b35b AV |
1004 | sizeof(input))) |
1005 | return -EFAULT; | |
42a74f20 | 1006 | |
e145b35b | 1007 | return ext4_ioctl_group_add(filp, &input); |
ac27a0ec DK |
1008 | } |
1009 | ||
c14c6fd5 | 1010 | case EXT4_IOC_MIGRATE: |
2a43a878 AK |
1011 | { |
1012 | int err; | |
2e149670 | 1013 | if (!inode_owner_or_capable(inode)) |
2a43a878 AK |
1014 | return -EACCES; |
1015 | ||
a561be71 | 1016 | err = mnt_want_write_file(filp); |
2a43a878 AK |
1017 | if (err) |
1018 | return err; | |
1019 | /* | |
1020 | * inode_mutex prevent write and truncate on the file. | |
1021 | * Read still goes through. We take i_data_sem in | |
1022 | * ext4_ext_swap_inode_data before we switch the | |
1023 | * inode format to prevent read. | |
1024 | */ | |
5955102c | 1025 | inode_lock((inode)); |
2a43a878 | 1026 | err = ext4_ext_migrate(inode); |
5955102c | 1027 | inode_unlock((inode)); |
2a79f17e | 1028 | mnt_drop_write_file(filp); |
2a43a878 AK |
1029 | return err; |
1030 | } | |
c14c6fd5 | 1031 | |
ccd2506b TT |
1032 | case EXT4_IOC_ALLOC_DA_BLKS: |
1033 | { | |
1034 | int err; | |
2e149670 | 1035 | if (!inode_owner_or_capable(inode)) |
ccd2506b TT |
1036 | return -EACCES; |
1037 | ||
a561be71 | 1038 | err = mnt_want_write_file(filp); |
ccd2506b TT |
1039 | if (err) |
1040 | return err; | |
1041 | err = ext4_alloc_da_blocks(inode); | |
2a79f17e | 1042 | mnt_drop_write_file(filp); |
ccd2506b TT |
1043 | return err; |
1044 | } | |
1045 | ||
393d1d1d | 1046 | case EXT4_IOC_SWAP_BOOT: |
3e67cfad DM |
1047 | { |
1048 | int err; | |
393d1d1d DTB |
1049 | if (!(filp->f_mode & FMODE_WRITE)) |
1050 | return -EBADF; | |
3e67cfad DM |
1051 | err = mnt_want_write_file(filp); |
1052 | if (err) | |
1053 | return err; | |
1054 | err = swap_inode_boot_loader(sb, inode); | |
1055 | mnt_drop_write_file(filp); | |
1056 | return err; | |
1057 | } | |
393d1d1d | 1058 | |
19c5246d YY |
1059 | case EXT4_IOC_RESIZE_FS: { |
1060 | ext4_fsblk_t n_blocks_count; | |
19c5246d | 1061 | int err = 0, err2 = 0; |
7f511862 | 1062 | ext4_group_t o_group = EXT4_SB(sb)->s_groups_count; |
19c5246d | 1063 | |
19c5246d YY |
1064 | if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, |
1065 | sizeof(__u64))) { | |
1066 | return -EFAULT; | |
1067 | } | |
1068 | ||
19c5246d YY |
1069 | err = ext4_resize_begin(sb); |
1070 | if (err) | |
1071 | return err; | |
1072 | ||
8cae6f71 | 1073 | err = mnt_want_write_file(filp); |
19c5246d YY |
1074 | if (err) |
1075 | goto resizefs_out; | |
1076 | ||
1077 | err = ext4_resize_fs(sb, n_blocks_count); | |
1078 | if (EXT4_SB(sb)->s_journal) { | |
aa75f4d3 | 1079 | ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_RESIZE); |
19c5246d YY |
1080 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); |
1081 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
1082 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
1083 | } | |
1084 | if (err == 0) | |
1085 | err = err2; | |
8cae6f71 | 1086 | mnt_drop_write_file(filp); |
310a997f | 1087 | if (!err && (o_group < EXT4_SB(sb)->s_groups_count) && |
7f511862 TT |
1088 | ext4_has_group_desc_csum(sb) && |
1089 | test_opt(sb, INIT_INODE_TABLE)) | |
1090 | err = ext4_register_li_request(sb, o_group); | |
1091 | ||
19c5246d YY |
1092 | resizefs_out: |
1093 | ext4_resize_end(sb); | |
1094 | return err; | |
1095 | } | |
1096 | ||
e681c047 LC |
1097 | case FITRIM: |
1098 | { | |
41431792 | 1099 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
e681c047 LC |
1100 | struct fstrim_range range; |
1101 | int ret = 0; | |
1102 | ||
1103 | if (!capable(CAP_SYS_ADMIN)) | |
1104 | return -EPERM; | |
1105 | ||
41431792 LC |
1106 | if (!blk_queue_discard(q)) |
1107 | return -EOPNOTSUPP; | |
1108 | ||
18915b58 DW |
1109 | /* |
1110 | * We haven't replayed the journal, so we cannot use our | |
1111 | * block-bitmap-guided storage zapping commands. | |
1112 | */ | |
1113 | if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) | |
1114 | return -EROFS; | |
1115 | ||
e6705f7c | 1116 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
e681c047 LC |
1117 | sizeof(range))) |
1118 | return -EFAULT; | |
1119 | ||
5c2ed62f LC |
1120 | range.minlen = max((unsigned int)range.minlen, |
1121 | q->limits.discard_granularity); | |
e681c047 LC |
1122 | ret = ext4_trim_fs(sb, &range); |
1123 | if (ret < 0) | |
1124 | return ret; | |
1125 | ||
e6705f7c | 1126 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
e681c047 LC |
1127 | sizeof(range))) |
1128 | return -EFAULT; | |
1129 | ||
1130 | return 0; | |
1131 | } | |
7869a4a6 TT |
1132 | case EXT4_IOC_PRECACHE_EXTENTS: |
1133 | return ext4_ext_precache(inode); | |
9bd8212f | 1134 | |
cb29a02d | 1135 | case FS_IOC_SET_ENCRYPTION_POLICY: |
9a200d07 RW |
1136 | if (!ext4_has_feature_encrypt(sb)) |
1137 | return -EOPNOTSUPP; | |
db717d8e | 1138 | return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); |
9a200d07 | 1139 | |
cb29a02d | 1140 | case FS_IOC_GET_ENCRYPTION_PWSALT: { |
643fa961 | 1141 | #ifdef CONFIG_FS_ENCRYPTION |
9bd8212f MH |
1142 | int err, err2; |
1143 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
1144 | handle_t *handle; | |
1145 | ||
35997d1c | 1146 | if (!ext4_has_feature_encrypt(sb)) |
9bd8212f MH |
1147 | return -EOPNOTSUPP; |
1148 | if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { | |
1149 | err = mnt_want_write_file(filp); | |
1150 | if (err) | |
1151 | return err; | |
1152 | handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); | |
1153 | if (IS_ERR(handle)) { | |
1154 | err = PTR_ERR(handle); | |
1155 | goto pwsalt_err_exit; | |
1156 | } | |
1157 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); | |
1158 | if (err) | |
1159 | goto pwsalt_err_journal; | |
1160 | generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); | |
1161 | err = ext4_handle_dirty_metadata(handle, NULL, | |
1162 | sbi->s_sbh); | |
1163 | pwsalt_err_journal: | |
1164 | err2 = ext4_journal_stop(handle); | |
1165 | if (err2 && !err) | |
1166 | err = err2; | |
1167 | pwsalt_err_exit: | |
1168 | mnt_drop_write_file(filp); | |
1169 | if (err) | |
1170 | return err; | |
1171 | } | |
b4ab9e29 FF |
1172 | if (copy_to_user((void __user *) arg, |
1173 | sbi->s_es->s_encrypt_pw_salt, 16)) | |
9bd8212f MH |
1174 | return -EFAULT; |
1175 | return 0; | |
ba679017 EB |
1176 | #else |
1177 | return -EOPNOTSUPP; | |
1178 | #endif | |
9bd8212f | 1179 | } |
cb29a02d | 1180 | case FS_IOC_GET_ENCRYPTION_POLICY: |
0642ea24 CY |
1181 | if (!ext4_has_feature_encrypt(sb)) |
1182 | return -EOPNOTSUPP; | |
db717d8e | 1183 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); |
9bd8212f | 1184 | |
29b3692e EB |
1185 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: |
1186 | if (!ext4_has_feature_encrypt(sb)) | |
1187 | return -EOPNOTSUPP; | |
1188 | return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg); | |
1189 | ||
1190 | case FS_IOC_ADD_ENCRYPTION_KEY: | |
1191 | if (!ext4_has_feature_encrypt(sb)) | |
1192 | return -EOPNOTSUPP; | |
1193 | return fscrypt_ioctl_add_key(filp, (void __user *)arg); | |
1194 | ||
1195 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | |
1196 | if (!ext4_has_feature_encrypt(sb)) | |
1197 | return -EOPNOTSUPP; | |
1198 | return fscrypt_ioctl_remove_key(filp, (void __user *)arg); | |
1199 | ||
1200 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | |
1201 | if (!ext4_has_feature_encrypt(sb)) | |
1202 | return -EOPNOTSUPP; | |
1203 | return fscrypt_ioctl_remove_key_all_users(filp, | |
1204 | (void __user *)arg); | |
1205 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | |
1206 | if (!ext4_has_feature_encrypt(sb)) | |
1207 | return -EOPNOTSUPP; | |
1208 | return fscrypt_ioctl_get_key_status(filp, (void __user *)arg); | |
1209 | ||
7ec9f3b4 EB |
1210 | case FS_IOC_GET_ENCRYPTION_NONCE: |
1211 | if (!ext4_has_feature_encrypt(sb)) | |
1212 | return -EOPNOTSUPP; | |
1213 | return fscrypt_ioctl_get_nonce(filp, (void __user *)arg); | |
1214 | ||
b0c013e2 TT |
1215 | case EXT4_IOC_CLEAR_ES_CACHE: |
1216 | { | |
1217 | if (!inode_owner_or_capable(inode)) | |
1218 | return -EACCES; | |
1219 | ext4_clear_inode_es(inode); | |
1220 | return 0; | |
1221 | } | |
1222 | ||
1ad3ea6e TT |
1223 | case EXT4_IOC_GETSTATE: |
1224 | { | |
1225 | __u32 state = 0; | |
1226 | ||
1227 | if (ext4_test_inode_state(inode, EXT4_STATE_EXT_PRECACHED)) | |
1228 | state |= EXT4_STATE_FLAG_EXT_PRECACHED; | |
1229 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) | |
1230 | state |= EXT4_STATE_FLAG_NEW; | |
1231 | if (ext4_test_inode_state(inode, EXT4_STATE_NEWENTRY)) | |
1232 | state |= EXT4_STATE_FLAG_NEWENTRY; | |
1233 | if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) | |
1234 | state |= EXT4_STATE_FLAG_DA_ALLOC_CLOSE; | |
1235 | ||
1236 | return put_user(state, (__u32 __user *) arg); | |
1237 | } | |
1238 | ||
bb5835ed TT |
1239 | case EXT4_IOC_GET_ES_CACHE: |
1240 | return ext4_ioctl_get_es_cache(filp, arg); | |
1241 | ||
cb29a02d | 1242 | case FS_IOC_FSGETXATTR: |
9b7365fc LX |
1243 | { |
1244 | struct fsxattr fa; | |
1245 | ||
7b0e492e | 1246 | ext4_fill_fsxattr(inode, &fa); |
9b7365fc LX |
1247 | |
1248 | if (copy_to_user((struct fsxattr __user *)arg, | |
1249 | &fa, sizeof(fa))) | |
1250 | return -EFAULT; | |
1251 | return 0; | |
1252 | } | |
cb29a02d | 1253 | case FS_IOC_FSSETXATTR: |
9b7365fc | 1254 | { |
7b0e492e | 1255 | struct fsxattr fa, old_fa; |
9b7365fc LX |
1256 | int err; |
1257 | ||
1258 | if (copy_from_user(&fa, (struct fsxattr __user *)arg, | |
1259 | sizeof(fa))) | |
1260 | return -EFAULT; | |
1261 | ||
1262 | /* Make sure caller has proper permission */ | |
1263 | if (!inode_owner_or_capable(inode)) | |
1264 | return -EACCES; | |
1265 | ||
d14e7683 JK |
1266 | if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS) |
1267 | return -EOPNOTSUPP; | |
1268 | ||
1269 | flags = ext4_xflags_to_iflags(fa.fsx_xflags); | |
1270 | if (ext4_mask_flags(inode->i_mode, flags) != flags) | |
1271 | return -EOPNOTSUPP; | |
1272 | ||
9b7365fc LX |
1273 | err = mnt_want_write_file(filp); |
1274 | if (err) | |
1275 | return err; | |
1276 | ||
5955102c | 1277 | inode_lock(inode); |
7b0e492e | 1278 | ext4_fill_fsxattr(inode, &old_fa); |
7b0e492e | 1279 | err = vfs_ioc_fssetxattr_check(inode, &old_fa, &fa); |
dc7ac6c4 WS |
1280 | if (err) |
1281 | goto out; | |
9b7365fc LX |
1282 | flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) | |
1283 | (flags & EXT4_FL_XFLAG_VISIBLE); | |
2e538403 DW |
1284 | err = ext4_ioctl_check_immutable(inode, fa.fsx_projid, flags); |
1285 | if (err) | |
1286 | goto out; | |
9b7365fc | 1287 | err = ext4_ioctl_setflags(inode, flags); |
9b7365fc | 1288 | if (err) |
dc7ac6c4 | 1289 | goto out; |
9b7365fc | 1290 | err = ext4_ioctl_setproject(filp, fa.fsx_projid); |
dc7ac6c4 WS |
1291 | out: |
1292 | inode_unlock(inode); | |
1293 | mnt_drop_write_file(filp); | |
1294 | return err; | |
9b7365fc | 1295 | } |
e9be2ac7 TT |
1296 | case EXT4_IOC_SHUTDOWN: |
1297 | return ext4_shutdown(sb, arg); | |
c93d8f88 EB |
1298 | |
1299 | case FS_IOC_ENABLE_VERITY: | |
1300 | if (!ext4_has_feature_verity(sb)) | |
1301 | return -EOPNOTSUPP; | |
1302 | return fsverity_ioctl_enable(filp, (const void __user *)arg); | |
1303 | ||
1304 | case FS_IOC_MEASURE_VERITY: | |
1305 | if (!ext4_has_feature_verity(sb)) | |
1306 | return -EOPNOTSUPP; | |
1307 | return fsverity_ioctl_measure(filp, (void __user *)arg); | |
1308 | ||
ac27a0ec DK |
1309 | default: |
1310 | return -ENOTTY; | |
1311 | } | |
1312 | } | |
1313 | ||
aa75f4d3 HS |
1314 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
1315 | { | |
1316 | long ret; | |
1317 | ||
1318 | ext4_fc_start_update(file_inode(filp)); | |
1319 | ret = __ext4_ioctl(filp, cmd, arg); | |
1320 | ext4_fc_stop_update(file_inode(filp)); | |
1321 | ||
1322 | return ret; | |
1323 | } | |
1324 | ||
ac27a0ec | 1325 | #ifdef CONFIG_COMPAT |
617ba13b | 1326 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ac27a0ec | 1327 | { |
ac27a0ec DK |
1328 | /* These are just misnamed, they actually get/put from/to user an int */ |
1329 | switch (cmd) { | |
cb29a02d EB |
1330 | case FS_IOC32_GETFLAGS: |
1331 | cmd = FS_IOC_GETFLAGS; | |
ac27a0ec | 1332 | break; |
cb29a02d EB |
1333 | case FS_IOC32_SETFLAGS: |
1334 | cmd = FS_IOC_SETFLAGS; | |
ac27a0ec | 1335 | break; |
617ba13b MC |
1336 | case EXT4_IOC32_GETVERSION: |
1337 | cmd = EXT4_IOC_GETVERSION; | |
ac27a0ec | 1338 | break; |
617ba13b MC |
1339 | case EXT4_IOC32_SETVERSION: |
1340 | cmd = EXT4_IOC_SETVERSION; | |
ac27a0ec | 1341 | break; |
617ba13b MC |
1342 | case EXT4_IOC32_GROUP_EXTEND: |
1343 | cmd = EXT4_IOC_GROUP_EXTEND; | |
ac27a0ec | 1344 | break; |
617ba13b MC |
1345 | case EXT4_IOC32_GETVERSION_OLD: |
1346 | cmd = EXT4_IOC_GETVERSION_OLD; | |
ac27a0ec | 1347 | break; |
617ba13b MC |
1348 | case EXT4_IOC32_SETVERSION_OLD: |
1349 | cmd = EXT4_IOC_SETVERSION_OLD; | |
ac27a0ec | 1350 | break; |
617ba13b MC |
1351 | case EXT4_IOC32_GETRSVSZ: |
1352 | cmd = EXT4_IOC_GETRSVSZ; | |
ac27a0ec | 1353 | break; |
617ba13b MC |
1354 | case EXT4_IOC32_SETRSVSZ: |
1355 | cmd = EXT4_IOC_SETRSVSZ; | |
ac27a0ec | 1356 | break; |
4d92dc0f BH |
1357 | case EXT4_IOC32_GROUP_ADD: { |
1358 | struct compat_ext4_new_group_input __user *uinput; | |
e145b35b | 1359 | struct ext4_new_group_data input; |
4d92dc0f BH |
1360 | int err; |
1361 | ||
1362 | uinput = compat_ptr(arg); | |
1363 | err = get_user(input.group, &uinput->group); | |
1364 | err |= get_user(input.block_bitmap, &uinput->block_bitmap); | |
1365 | err |= get_user(input.inode_bitmap, &uinput->inode_bitmap); | |
1366 | err |= get_user(input.inode_table, &uinput->inode_table); | |
1367 | err |= get_user(input.blocks_count, &uinput->blocks_count); | |
1368 | err |= get_user(input.reserved_blocks, | |
1369 | &uinput->reserved_blocks); | |
1370 | if (err) | |
1371 | return -EFAULT; | |
e145b35b | 1372 | return ext4_ioctl_group_add(file, &input); |
4d92dc0f | 1373 | } |
b684b2ee | 1374 | case EXT4_IOC_MOVE_EXT: |
19c5246d | 1375 | case EXT4_IOC_RESIZE_FS: |
314999dc | 1376 | case FITRIM: |
7869a4a6 | 1377 | case EXT4_IOC_PRECACHE_EXTENTS: |
cb29a02d EB |
1378 | case FS_IOC_SET_ENCRYPTION_POLICY: |
1379 | case FS_IOC_GET_ENCRYPTION_PWSALT: | |
1380 | case FS_IOC_GET_ENCRYPTION_POLICY: | |
29b3692e EB |
1381 | case FS_IOC_GET_ENCRYPTION_POLICY_EX: |
1382 | case FS_IOC_ADD_ENCRYPTION_KEY: | |
1383 | case FS_IOC_REMOVE_ENCRYPTION_KEY: | |
1384 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS: | |
1385 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS: | |
7ec9f3b4 | 1386 | case FS_IOC_GET_ENCRYPTION_NONCE: |
e9be2ac7 | 1387 | case EXT4_IOC_SHUTDOWN: |
0c9ec4be | 1388 | case FS_IOC_GETFSMAP: |
c93d8f88 EB |
1389 | case FS_IOC_ENABLE_VERITY: |
1390 | case FS_IOC_MEASURE_VERITY: | |
b0c013e2 | 1391 | case EXT4_IOC_CLEAR_ES_CACHE: |
1ad3ea6e | 1392 | case EXT4_IOC_GETSTATE: |
bb5835ed | 1393 | case EXT4_IOC_GET_ES_CACHE: |
cb29a02d EB |
1394 | case FS_IOC_FSGETXATTR: |
1395 | case FS_IOC_FSSETXATTR: | |
b684b2ee | 1396 | break; |
ac27a0ec DK |
1397 | default: |
1398 | return -ENOIOCTLCMD; | |
1399 | } | |
5cdd7b2d | 1400 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
ac27a0ec DK |
1401 | } |
1402 | #endif |