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 | ||
18aded17 TT |
89 | static void reset_inode_seed(struct inode *inode) |
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 DTB |
167 | } |
168 | ||
169 | /* Protect extent tree against block allocations via delalloc */ | |
170 | ext4_double_down_write_data_sem(inode, inode_bl); | |
171 | ||
172 | if (inode_bl->i_nlink == 0) { | |
173 | /* this inode has never been used as a BOOT_LOADER */ | |
174 | set_nlink(inode_bl, 1); | |
175 | i_uid_write(inode_bl, 0); | |
176 | i_gid_write(inode_bl, 0); | |
177 | inode_bl->i_flags = 0; | |
178 | ei_bl->i_flags = 0; | |
ee73f9a5 | 179 | inode_set_iversion(inode_bl, 1); |
393d1d1d DTB |
180 | i_size_write(inode_bl, 0); |
181 | inode_bl->i_mode = S_IFREG; | |
e2b911c5 | 182 | if (ext4_has_feature_extents(sb)) { |
393d1d1d DTB |
183 | ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS); |
184 | ext4_ext_tree_init(handle, inode_bl); | |
185 | } else | |
186 | memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data)); | |
187 | } | |
188 | ||
aa507b5f | 189 | err = dquot_initialize(inode); |
190 | if (err) | |
191 | goto err_out1; | |
192 | ||
193 | size = (qsize_t)(inode->i_blocks) * (1 << 9) + inode->i_bytes; | |
194 | size_bl = (qsize_t)(inode_bl->i_blocks) * (1 << 9) + inode_bl->i_bytes; | |
195 | diff = size - size_bl; | |
393d1d1d DTB |
196 | swap_inode_data(inode, inode_bl); |
197 | ||
eeca7ea1 | 198 | inode->i_ctime = inode_bl->i_ctime = current_time(inode); |
393d1d1d | 199 | |
23253068 TT |
200 | inode->i_generation = prandom_u32(); |
201 | inode_bl->i_generation = prandom_u32(); | |
18aded17 TT |
202 | reset_inode_seed(inode); |
203 | reset_inode_seed(inode_bl); | |
393d1d1d DTB |
204 | |
205 | ext4_discard_preallocations(inode); | |
206 | ||
207 | err = ext4_mark_inode_dirty(handle, inode); | |
208 | if (err < 0) { | |
aa507b5f | 209 | /* No need to update quota information. */ |
393d1d1d DTB |
210 | ext4_warning(inode->i_sb, |
211 | "couldn't mark inode #%lu dirty (err %d)", | |
212 | inode->i_ino, err); | |
213 | /* Revert all changes: */ | |
214 | swap_inode_data(inode, inode_bl); | |
18aded17 | 215 | ext4_mark_inode_dirty(handle, inode); |
aa507b5f | 216 | goto err_out1; |
217 | } | |
218 | ||
219 | blocks = inode_bl->i_blocks; | |
220 | bytes = inode_bl->i_bytes; | |
221 | inode_bl->i_blocks = inode->i_blocks; | |
222 | inode_bl->i_bytes = inode->i_bytes; | |
223 | err = ext4_mark_inode_dirty(handle, inode_bl); | |
224 | if (err < 0) { | |
225 | /* No need to update quota information. */ | |
226 | ext4_warning(inode_bl->i_sb, | |
227 | "couldn't mark inode #%lu dirty (err %d)", | |
228 | inode_bl->i_ino, err); | |
229 | goto revert; | |
230 | } | |
231 | ||
232 | /* Bootloader inode should not be counted into quota information. */ | |
233 | if (diff > 0) | |
234 | dquot_free_space(inode, diff); | |
235 | else | |
236 | err = dquot_alloc_space(inode, -1 * diff); | |
237 | ||
238 | if (err < 0) { | |
239 | revert: | |
240 | /* Revert all changes: */ | |
241 | inode_bl->i_blocks = blocks; | |
242 | inode_bl->i_bytes = bytes; | |
243 | swap_inode_data(inode, inode_bl); | |
244 | ext4_mark_inode_dirty(handle, inode); | |
245 | ext4_mark_inode_dirty(handle, inode_bl); | |
393d1d1d | 246 | } |
aa507b5f | 247 | |
248 | err_out1: | |
393d1d1d | 249 | ext4_journal_stop(handle); |
393d1d1d DTB |
250 | ext4_double_up_write_data_sem(inode, inode_bl); |
251 | ||
a46c68a3 | 252 | err_out: |
253 | up_write(&EXT4_I(inode)->i_mmap_sem); | |
30d29b11 | 254 | journal_err_out: |
375e289e | 255 | unlock_two_nondirectories(inode, inode_bl); |
393d1d1d | 256 | iput(inode_bl); |
393d1d1d DTB |
257 | return err; |
258 | } | |
259 | ||
ba679017 | 260 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
9bd8212f MH |
261 | static int uuid_is_zero(__u8 u[16]) |
262 | { | |
263 | int i; | |
264 | ||
265 | for (i = 0; i < 16; i++) | |
266 | if (u[i]) | |
267 | return 0; | |
268 | return 1; | |
269 | } | |
ba679017 | 270 | #endif |
9bd8212f | 271 | |
9b7365fc LX |
272 | static int ext4_ioctl_setflags(struct inode *inode, |
273 | unsigned int flags) | |
274 | { | |
275 | struct ext4_inode_info *ei = EXT4_I(inode); | |
276 | handle_t *handle = NULL; | |
fdde368e | 277 | int err = -EPERM, migrate = 0; |
9b7365fc LX |
278 | struct ext4_iloc iloc; |
279 | unsigned int oldflags, mask, i; | |
280 | unsigned int jflag; | |
281 | ||
282 | /* Is it quota file? Do not allow user to mess with it */ | |
02749a4c | 283 | if (ext4_is_quota_file(inode)) |
9b7365fc LX |
284 | goto flags_out; |
285 | ||
286 | oldflags = ei->i_flags; | |
287 | ||
288 | /* The JOURNAL_DATA flag is modifiable only by root */ | |
289 | jflag = flags & EXT4_JOURNAL_DATA_FL; | |
290 | ||
291 | /* | |
292 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by | |
293 | * the relevant capability. | |
294 | * | |
295 | * This test looks nicer. Thanks to Pauline Middelink | |
296 | */ | |
297 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { | |
298 | if (!capable(CAP_LINUX_IMMUTABLE)) | |
299 | goto flags_out; | |
300 | } | |
301 | ||
302 | /* | |
303 | * The JOURNAL_DATA flag can only be changed by | |
304 | * the relevant capability. | |
305 | */ | |
306 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { | |
307 | if (!capable(CAP_SYS_RESOURCE)) | |
308 | goto flags_out; | |
309 | } | |
310 | if ((flags ^ oldflags) & EXT4_EXTENTS_FL) | |
311 | migrate = 1; | |
312 | ||
313 | if (flags & EXT4_EOFBLOCKS_FL) { | |
314 | /* we don't support adding EOFBLOCKS flag */ | |
315 | if (!(oldflags & EXT4_EOFBLOCKS_FL)) { | |
316 | err = -EOPNOTSUPP; | |
317 | goto flags_out; | |
318 | } | |
2c98eb5e TT |
319 | } else if (oldflags & EXT4_EOFBLOCKS_FL) { |
320 | err = ext4_truncate(inode); | |
321 | if (err) | |
322 | goto flags_out; | |
323 | } | |
9b7365fc LX |
324 | |
325 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); | |
326 | if (IS_ERR(handle)) { | |
327 | err = PTR_ERR(handle); | |
328 | goto flags_out; | |
329 | } | |
330 | if (IS_SYNC(inode)) | |
331 | ext4_handle_sync(handle); | |
332 | err = ext4_reserve_inode_write(handle, inode, &iloc); | |
333 | if (err) | |
334 | goto flags_err; | |
335 | ||
336 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { | |
337 | if (!(mask & EXT4_FL_USER_MODIFIABLE)) | |
338 | continue; | |
f8011d93 JK |
339 | /* These flags get special treatment later */ |
340 | if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL) | |
341 | continue; | |
9b7365fc LX |
342 | if (mask & flags) |
343 | ext4_set_inode_flag(inode, i); | |
344 | else | |
345 | ext4_clear_inode_flag(inode, i); | |
346 | } | |
347 | ||
348 | ext4_set_inode_flags(inode); | |
eeca7ea1 | 349 | inode->i_ctime = current_time(inode); |
9b7365fc LX |
350 | |
351 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); | |
352 | flags_err: | |
353 | ext4_journal_stop(handle); | |
354 | if (err) | |
355 | goto flags_out; | |
356 | ||
e9072d85 RZ |
357 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
358 | /* | |
359 | * Changes to the journaling mode can cause unsafe changes to | |
360 | * S_DAX if we are using the DAX mount option. | |
361 | */ | |
362 | if (test_opt(inode->i_sb, DAX)) { | |
363 | err = -EBUSY; | |
364 | goto flags_out; | |
365 | } | |
366 | ||
9b7365fc | 367 | err = ext4_change_inode_journal_flag(inode, jflag); |
e9072d85 RZ |
368 | if (err) |
369 | goto flags_out; | |
370 | } | |
9b7365fc LX |
371 | if (migrate) { |
372 | if (flags & EXT4_EXTENTS_FL) | |
373 | err = ext4_ext_migrate(inode); | |
374 | else | |
375 | err = ext4_ind_migrate(inode); | |
376 | } | |
377 | ||
378 | flags_out: | |
379 | return err; | |
380 | } | |
381 | ||
382 | #ifdef CONFIG_QUOTA | |
383 | static int ext4_ioctl_setproject(struct file *filp, __u32 projid) | |
384 | { | |
385 | struct inode *inode = file_inode(filp); | |
386 | struct super_block *sb = inode->i_sb; | |
387 | struct ext4_inode_info *ei = EXT4_I(inode); | |
388 | int err, rc; | |
389 | handle_t *handle; | |
390 | kprojid_t kprojid; | |
391 | struct ext4_iloc iloc; | |
392 | struct ext4_inode *raw_inode; | |
079788d0 | 393 | struct dquot *transfer_to[MAXQUOTAS] = { }; |
9b7365fc | 394 | |
0b7b7779 | 395 | if (!ext4_has_feature_project(sb)) { |
9b7365fc LX |
396 | if (projid != EXT4_DEF_PROJID) |
397 | return -EOPNOTSUPP; | |
398 | else | |
399 | return 0; | |
400 | } | |
401 | ||
402 | if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE) | |
403 | return -EOPNOTSUPP; | |
404 | ||
405 | kprojid = make_kprojid(&init_user_ns, (projid_t)projid); | |
406 | ||
407 | if (projid_eq(kprojid, EXT4_I(inode)->i_projid)) | |
408 | return 0; | |
409 | ||
9b7365fc | 410 | err = -EPERM; |
9b7365fc | 411 | /* Is it quota file? Do not allow user to mess with it */ |
02749a4c | 412 | if (ext4_is_quota_file(inode)) |
dc7ac6c4 | 413 | return err; |
9b7365fc LX |
414 | |
415 | err = ext4_get_inode_loc(inode, &iloc); | |
416 | if (err) | |
dc7ac6c4 | 417 | return err; |
9b7365fc LX |
418 | |
419 | raw_inode = ext4_raw_inode(&iloc); | |
420 | if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) { | |
c03b45b8 MX |
421 | err = ext4_expand_extra_isize(inode, |
422 | EXT4_SB(sb)->s_want_extra_isize, | |
423 | &iloc); | |
424 | if (err) | |
dc7ac6c4 | 425 | return err; |
c03b45b8 | 426 | } else { |
9b7365fc | 427 | brelse(iloc.bh); |
9b7365fc | 428 | } |
9b7365fc | 429 | |
182a79e0 WS |
430 | err = dquot_initialize(inode); |
431 | if (err) | |
432 | return err; | |
9b7365fc LX |
433 | |
434 | handle = ext4_journal_start(inode, EXT4_HT_QUOTA, | |
435 | EXT4_QUOTA_INIT_BLOCKS(sb) + | |
436 | EXT4_QUOTA_DEL_BLOCKS(sb) + 3); | |
dc7ac6c4 WS |
437 | if (IS_ERR(handle)) |
438 | return PTR_ERR(handle); | |
9b7365fc LX |
439 | |
440 | err = ext4_reserve_inode_write(handle, inode, &iloc); | |
441 | if (err) | |
442 | goto out_stop; | |
443 | ||
079788d0 WS |
444 | transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); |
445 | if (!IS_ERR(transfer_to[PRJQUOTA])) { | |
7a9ca53a TE |
446 | |
447 | /* __dquot_transfer() calls back ext4_get_inode_usage() which | |
448 | * counts xattr inode references. | |
449 | */ | |
450 | down_read(&EXT4_I(inode)->xattr_sem); | |
079788d0 | 451 | err = __dquot_transfer(inode, transfer_to); |
7a9ca53a | 452 | up_read(&EXT4_I(inode)->xattr_sem); |
079788d0 WS |
453 | dqput(transfer_to[PRJQUOTA]); |
454 | if (err) | |
455 | goto out_dirty; | |
9b7365fc | 456 | } |
079788d0 | 457 | |
9b7365fc | 458 | EXT4_I(inode)->i_projid = kprojid; |
eeca7ea1 | 459 | inode->i_ctime = current_time(inode); |
9b7365fc LX |
460 | out_dirty: |
461 | rc = ext4_mark_iloc_dirty(handle, inode, &iloc); | |
462 | if (!err) | |
463 | err = rc; | |
464 | out_stop: | |
465 | ext4_journal_stop(handle); | |
9b7365fc LX |
466 | return err; |
467 | } | |
468 | #else | |
469 | static int ext4_ioctl_setproject(struct file *filp, __u32 projid) | |
470 | { | |
471 | if (projid != EXT4_DEF_PROJID) | |
472 | return -EOPNOTSUPP; | |
473 | return 0; | |
474 | } | |
475 | #endif | |
476 | ||
477 | /* Transfer internal flags to xflags */ | |
478 | static inline __u32 ext4_iflags_to_xflags(unsigned long iflags) | |
479 | { | |
480 | __u32 xflags = 0; | |
481 | ||
482 | if (iflags & EXT4_SYNC_FL) | |
483 | xflags |= FS_XFLAG_SYNC; | |
484 | if (iflags & EXT4_IMMUTABLE_FL) | |
485 | xflags |= FS_XFLAG_IMMUTABLE; | |
486 | if (iflags & EXT4_APPEND_FL) | |
487 | xflags |= FS_XFLAG_APPEND; | |
488 | if (iflags & EXT4_NODUMP_FL) | |
489 | xflags |= FS_XFLAG_NODUMP; | |
490 | if (iflags & EXT4_NOATIME_FL) | |
491 | xflags |= FS_XFLAG_NOATIME; | |
492 | if (iflags & EXT4_PROJINHERIT_FL) | |
493 | xflags |= FS_XFLAG_PROJINHERIT; | |
494 | return xflags; | |
495 | } | |
496 | ||
d14e7683 JK |
497 | #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \ |
498 | FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \ | |
499 | FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT) | |
500 | ||
9b7365fc LX |
501 | /* Transfer xflags flags to internal */ |
502 | static inline unsigned long ext4_xflags_to_iflags(__u32 xflags) | |
503 | { | |
504 | unsigned long iflags = 0; | |
505 | ||
506 | if (xflags & FS_XFLAG_SYNC) | |
507 | iflags |= EXT4_SYNC_FL; | |
508 | if (xflags & FS_XFLAG_IMMUTABLE) | |
509 | iflags |= EXT4_IMMUTABLE_FL; | |
510 | if (xflags & FS_XFLAG_APPEND) | |
511 | iflags |= EXT4_APPEND_FL; | |
512 | if (xflags & FS_XFLAG_NODUMP) | |
513 | iflags |= EXT4_NODUMP_FL; | |
514 | if (xflags & FS_XFLAG_NOATIME) | |
515 | iflags |= EXT4_NOATIME_FL; | |
516 | if (xflags & FS_XFLAG_PROJINHERIT) | |
517 | iflags |= EXT4_PROJINHERIT_FL; | |
518 | ||
519 | return iflags; | |
520 | } | |
521 | ||
1a20a630 | 522 | static int ext4_shutdown(struct super_block *sb, unsigned long arg) |
783d9485 TT |
523 | { |
524 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
525 | __u32 flags; | |
526 | ||
527 | if (!capable(CAP_SYS_ADMIN)) | |
528 | return -EPERM; | |
529 | ||
530 | if (get_user(flags, (__u32 __user *)arg)) | |
531 | return -EFAULT; | |
532 | ||
533 | if (flags > EXT4_GOING_FLAGS_NOLOGFLUSH) | |
534 | return -EINVAL; | |
535 | ||
536 | if (ext4_forced_shutdown(sbi)) | |
537 | return 0; | |
538 | ||
539 | ext4_msg(sb, KERN_ALERT, "shut down requested (%d)", flags); | |
ccf0f32a | 540 | trace_ext4_shutdown(sb, flags); |
783d9485 TT |
541 | |
542 | switch (flags) { | |
543 | case EXT4_GOING_FLAGS_DEFAULT: | |
544 | freeze_bdev(sb->s_bdev); | |
545 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); | |
546 | thaw_bdev(sb->s_bdev, sb); | |
547 | break; | |
548 | case EXT4_GOING_FLAGS_LOGFLUSH: | |
549 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); | |
550 | if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) { | |
551 | (void) ext4_force_commit(sb); | |
fb7c0244 | 552 | jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN); |
783d9485 TT |
553 | } |
554 | break; | |
555 | case EXT4_GOING_FLAGS_NOLOGFLUSH: | |
556 | set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); | |
a6d9946b | 557 | if (sbi->s_journal && !is_journal_aborted(sbi->s_journal)) |
fb7c0244 | 558 | jbd2_journal_abort(sbi->s_journal, -ESHUTDOWN); |
783d9485 TT |
559 | break; |
560 | default: | |
561 | return -EINVAL; | |
562 | } | |
563 | clear_opt(sb, DISCARD); | |
564 | return 0; | |
565 | } | |
566 | ||
0c9ec4be DW |
567 | struct getfsmap_info { |
568 | struct super_block *gi_sb; | |
569 | struct fsmap_head __user *gi_data; | |
570 | unsigned int gi_idx; | |
571 | __u32 gi_last_flags; | |
572 | }; | |
573 | ||
574 | static int ext4_getfsmap_format(struct ext4_fsmap *xfm, void *priv) | |
575 | { | |
576 | struct getfsmap_info *info = priv; | |
577 | struct fsmap fm; | |
578 | ||
579 | trace_ext4_getfsmap_mapping(info->gi_sb, xfm); | |
580 | ||
581 | info->gi_last_flags = xfm->fmr_flags; | |
582 | ext4_fsmap_from_internal(info->gi_sb, &fm, xfm); | |
583 | if (copy_to_user(&info->gi_data->fmh_recs[info->gi_idx++], &fm, | |
584 | sizeof(struct fsmap))) | |
585 | return -EFAULT; | |
586 | ||
587 | return 0; | |
588 | } | |
589 | ||
590 | static int ext4_ioc_getfsmap(struct super_block *sb, | |
591 | struct fsmap_head __user *arg) | |
592 | { | |
593 | struct getfsmap_info info = {0}; | |
594 | struct ext4_fsmap_head xhead = {0}; | |
595 | struct fsmap_head head; | |
596 | bool aborted = false; | |
597 | int error; | |
598 | ||
599 | if (copy_from_user(&head, arg, sizeof(struct fsmap_head))) | |
600 | return -EFAULT; | |
601 | if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) || | |
602 | memchr_inv(head.fmh_keys[0].fmr_reserved, 0, | |
603 | sizeof(head.fmh_keys[0].fmr_reserved)) || | |
604 | memchr_inv(head.fmh_keys[1].fmr_reserved, 0, | |
605 | sizeof(head.fmh_keys[1].fmr_reserved))) | |
606 | return -EINVAL; | |
607 | /* | |
608 | * ext4 doesn't report file extents at all, so the only valid | |
609 | * file offsets are the magic ones (all zeroes or all ones). | |
610 | */ | |
611 | if (head.fmh_keys[0].fmr_offset || | |
612 | (head.fmh_keys[1].fmr_offset != 0 && | |
613 | head.fmh_keys[1].fmr_offset != -1ULL)) | |
614 | return -EINVAL; | |
615 | ||
616 | xhead.fmh_iflags = head.fmh_iflags; | |
617 | xhead.fmh_count = head.fmh_count; | |
618 | ext4_fsmap_to_internal(sb, &xhead.fmh_keys[0], &head.fmh_keys[0]); | |
619 | ext4_fsmap_to_internal(sb, &xhead.fmh_keys[1], &head.fmh_keys[1]); | |
620 | ||
621 | trace_ext4_getfsmap_low_key(sb, &xhead.fmh_keys[0]); | |
622 | trace_ext4_getfsmap_high_key(sb, &xhead.fmh_keys[1]); | |
623 | ||
624 | info.gi_sb = sb; | |
625 | info.gi_data = arg; | |
626 | error = ext4_getfsmap(sb, &xhead, ext4_getfsmap_format, &info); | |
627 | if (error == EXT4_QUERY_RANGE_ABORT) { | |
628 | error = 0; | |
629 | aborted = true; | |
630 | } else if (error) | |
631 | return error; | |
632 | ||
633 | /* If we didn't abort, set the "last" flag in the last fmx */ | |
634 | if (!aborted && info.gi_idx) { | |
635 | info.gi_last_flags |= FMR_OF_LAST; | |
636 | if (copy_to_user(&info.gi_data->fmh_recs[info.gi_idx - 1].fmr_flags, | |
637 | &info.gi_last_flags, | |
638 | sizeof(info.gi_last_flags))) | |
639 | return -EFAULT; | |
640 | } | |
641 | ||
642 | /* copy back header */ | |
643 | head.fmh_entries = xhead.fmh_entries; | |
644 | head.fmh_oflags = xhead.fmh_oflags; | |
645 | if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) | |
646 | return -EFAULT; | |
647 | ||
648 | return 0; | |
649 | } | |
650 | ||
e145b35b AV |
651 | static long ext4_ioctl_group_add(struct file *file, |
652 | struct ext4_new_group_data *input) | |
653 | { | |
654 | struct super_block *sb = file_inode(file)->i_sb; | |
655 | int err, err2=0; | |
656 | ||
657 | err = ext4_resize_begin(sb); | |
658 | if (err) | |
659 | return err; | |
660 | ||
661 | if (ext4_has_feature_bigalloc(sb)) { | |
662 | ext4_msg(sb, KERN_ERR, | |
663 | "Online resizing not supported with bigalloc"); | |
664 | err = -EOPNOTSUPP; | |
665 | goto group_add_out; | |
666 | } | |
667 | ||
668 | err = mnt_want_write_file(file); | |
669 | if (err) | |
670 | goto group_add_out; | |
671 | ||
672 | err = ext4_group_add(sb, input); | |
673 | if (EXT4_SB(sb)->s_journal) { | |
674 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
675 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
676 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
677 | } | |
678 | if (err == 0) | |
679 | err = err2; | |
680 | mnt_drop_write_file(file); | |
681 | if (!err && ext4_has_group_desc_csum(sb) && | |
682 | test_opt(sb, INIT_INODE_TABLE)) | |
683 | err = ext4_register_li_request(sb, input->group); | |
684 | group_add_out: | |
685 | ext4_resize_end(sb); | |
686 | return err; | |
687 | } | |
688 | ||
dc7ac6c4 WS |
689 | static int ext4_ioctl_check_project(struct inode *inode, struct fsxattr *fa) |
690 | { | |
691 | /* | |
692 | * Project Quota ID state is only allowed to change from within the init | |
693 | * namespace. Enforce that restriction only if we are trying to change | |
694 | * the quota ID state. Everything else is allowed in user namespaces. | |
695 | */ | |
696 | if (current_user_ns() == &init_user_ns) | |
697 | return 0; | |
698 | ||
699 | if (__kprojid_val(EXT4_I(inode)->i_projid) != fa->fsx_projid) | |
700 | return -EINVAL; | |
701 | ||
702 | if (ext4_test_inode_flag(inode, EXT4_INODE_PROJINHERIT)) { | |
703 | if (!(fa->fsx_xflags & FS_XFLAG_PROJINHERIT)) | |
704 | return -EINVAL; | |
705 | } else { | |
706 | if (fa->fsx_xflags & FS_XFLAG_PROJINHERIT) | |
707 | return -EINVAL; | |
708 | } | |
709 | ||
710 | return 0; | |
711 | } | |
712 | ||
5cdd7b2d | 713 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
ac27a0ec | 714 | { |
496ad9aa | 715 | struct inode *inode = file_inode(filp); |
bab08ab9 | 716 | struct super_block *sb = inode->i_sb; |
617ba13b | 717 | struct ext4_inode_info *ei = EXT4_I(inode); |
ac27a0ec | 718 | unsigned int flags; |
ac27a0ec | 719 | |
af5bc92d | 720 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
ac27a0ec DK |
721 | |
722 | switch (cmd) { | |
0c9ec4be DW |
723 | case FS_IOC_GETFSMAP: |
724 | return ext4_ioc_getfsmap(sb, (void __user *)arg); | |
617ba13b MC |
725 | case EXT4_IOC_GETFLAGS: |
726 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; | |
ac27a0ec | 727 | return put_user(flags, (int __user *) arg); |
617ba13b | 728 | case EXT4_IOC_SETFLAGS: { |
9b7365fc | 729 | int err; |
ac27a0ec | 730 | |
2e149670 | 731 | if (!inode_owner_or_capable(inode)) |
ac27a0ec DK |
732 | return -EACCES; |
733 | ||
734 | if (get_user(flags, (int __user *) arg)) | |
735 | return -EFAULT; | |
736 | ||
d14e7683 JK |
737 | if (flags & ~EXT4_FL_USER_VISIBLE) |
738 | return -EOPNOTSUPP; | |
739 | /* | |
740 | * chattr(1) grabs flags via GETFLAGS, modifies the result and | |
741 | * passes that to SETFLAGS. So we cannot easily make SETFLAGS | |
742 | * more restrictive than just silently masking off visible but | |
743 | * not settable flags as we always did. | |
744 | */ | |
745 | flags &= EXT4_FL_USER_MODIFIABLE; | |
746 | if (ext4_mask_flags(inode->i_mode, flags) != flags) | |
747 | return -EOPNOTSUPP; | |
748 | ||
a561be71 | 749 | err = mnt_want_write_file(filp); |
42a74f20 DH |
750 | if (err) |
751 | return err; | |
752 | ||
5955102c | 753 | inode_lock(inode); |
9b7365fc | 754 | err = ext4_ioctl_setflags(inode, flags); |
5955102c | 755 | inode_unlock(inode); |
2a79f17e | 756 | mnt_drop_write_file(filp); |
ac27a0ec DK |
757 | return err; |
758 | } | |
617ba13b MC |
759 | case EXT4_IOC_GETVERSION: |
760 | case EXT4_IOC_GETVERSION_OLD: | |
ac27a0ec | 761 | return put_user(inode->i_generation, (int __user *) arg); |
617ba13b MC |
762 | case EXT4_IOC_SETVERSION: |
763 | case EXT4_IOC_SETVERSION_OLD: { | |
ac27a0ec | 764 | handle_t *handle; |
617ba13b | 765 | struct ext4_iloc iloc; |
ac27a0ec DK |
766 | __u32 generation; |
767 | int err; | |
768 | ||
2e149670 | 769 | if (!inode_owner_or_capable(inode)) |
ac27a0ec | 770 | return -EPERM; |
42a74f20 | 771 | |
9aa5d32b | 772 | if (ext4_has_metadata_csum(inode->i_sb)) { |
814525f4 DW |
773 | ext4_warning(sb, "Setting inode version is not " |
774 | "supported with metadata_csum enabled."); | |
775 | return -ENOTTY; | |
776 | } | |
777 | ||
a561be71 | 778 | err = mnt_want_write_file(filp); |
42a74f20 DH |
779 | if (err) |
780 | return err; | |
781 | if (get_user(generation, (int __user *) arg)) { | |
782 | err = -EFAULT; | |
783 | goto setversion_out; | |
784 | } | |
ac27a0ec | 785 | |
5955102c | 786 | inode_lock(inode); |
9924a92a | 787 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
42a74f20 DH |
788 | if (IS_ERR(handle)) { |
789 | err = PTR_ERR(handle); | |
6c2155b9 | 790 | goto unlock_out; |
42a74f20 | 791 | } |
617ba13b | 792 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec | 793 | if (err == 0) { |
eeca7ea1 | 794 | inode->i_ctime = current_time(inode); |
ac27a0ec | 795 | inode->i_generation = generation; |
617ba13b | 796 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 797 | } |
617ba13b | 798 | ext4_journal_stop(handle); |
6c2155b9 DH |
799 | |
800 | unlock_out: | |
5955102c | 801 | inode_unlock(inode); |
42a74f20 | 802 | setversion_out: |
2a79f17e | 803 | mnt_drop_write_file(filp); |
ac27a0ec DK |
804 | return err; |
805 | } | |
617ba13b MC |
806 | case EXT4_IOC_GROUP_EXTEND: { |
807 | ext4_fsblk_t n_blocks_count; | |
ac046f1d | 808 | int err, err2=0; |
ac27a0ec | 809 | |
8f82f840 YY |
810 | err = ext4_resize_begin(sb); |
811 | if (err) | |
812 | return err; | |
ac27a0ec | 813 | |
014a1770 DH |
814 | if (get_user(n_blocks_count, (__u32 __user *)arg)) { |
815 | err = -EFAULT; | |
816 | goto group_extend_out; | |
817 | } | |
ac27a0ec | 818 | |
e2b911c5 | 819 | if (ext4_has_feature_bigalloc(sb)) { |
bab08ab9 TT |
820 | ext4_msg(sb, KERN_ERR, |
821 | "Online resizing not supported with bigalloc"); | |
014a1770 DH |
822 | err = -EOPNOTSUPP; |
823 | goto group_extend_out; | |
bab08ab9 TT |
824 | } |
825 | ||
a561be71 | 826 | err = mnt_want_write_file(filp); |
42a74f20 | 827 | if (err) |
014a1770 | 828 | goto group_extend_out; |
42a74f20 | 829 | |
617ba13b | 830 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
ac046f1d PT |
831 | if (EXT4_SB(sb)->s_journal) { |
832 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
833 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
834 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
835 | } | |
7ffe1ea8 HK |
836 | if (err == 0) |
837 | err = err2; | |
2a79f17e | 838 | mnt_drop_write_file(filp); |
014a1770 | 839 | group_extend_out: |
8f82f840 | 840 | ext4_resize_end(sb); |
ac27a0ec DK |
841 | return err; |
842 | } | |
748de673 AF |
843 | |
844 | case EXT4_IOC_MOVE_EXT: { | |
845 | struct move_extent me; | |
2903ff01 AV |
846 | struct fd donor; |
847 | int err; | |
748de673 | 848 | |
4a58579b AF |
849 | if (!(filp->f_mode & FMODE_READ) || |
850 | !(filp->f_mode & FMODE_WRITE)) | |
851 | return -EBADF; | |
852 | ||
748de673 AF |
853 | if (copy_from_user(&me, |
854 | (struct move_extent __user *)arg, sizeof(me))) | |
855 | return -EFAULT; | |
4a58579b | 856 | me.moved_len = 0; |
748de673 | 857 | |
2903ff01 AV |
858 | donor = fdget(me.donor_fd); |
859 | if (!donor.file) | |
748de673 AF |
860 | return -EBADF; |
861 | ||
2903ff01 | 862 | if (!(donor.file->f_mode & FMODE_WRITE)) { |
4a58579b AF |
863 | err = -EBADF; |
864 | goto mext_out; | |
748de673 AF |
865 | } |
866 | ||
e2b911c5 | 867 | if (ext4_has_feature_bigalloc(sb)) { |
bab08ab9 TT |
868 | ext4_msg(sb, KERN_ERR, |
869 | "Online defrag not supported with bigalloc"); | |
399c9b86 AV |
870 | err = -EOPNOTSUPP; |
871 | goto mext_out; | |
73f34a5e RZ |
872 | } else if (IS_DAX(inode)) { |
873 | ext4_msg(sb, KERN_ERR, | |
874 | "Online defrag not supported with DAX"); | |
875 | err = -EOPNOTSUPP; | |
876 | goto mext_out; | |
bab08ab9 TT |
877 | } |
878 | ||
a561be71 | 879 | err = mnt_want_write_file(filp); |
4a58579b AF |
880 | if (err) |
881 | goto mext_out; | |
882 | ||
2903ff01 | 883 | err = ext4_move_extents(filp, donor.file, me.orig_start, |
748de673 | 884 | me.donor_start, me.len, &me.moved_len); |
2a79f17e | 885 | mnt_drop_write_file(filp); |
748de673 | 886 | |
60e6679e | 887 | if (copy_to_user((struct move_extent __user *)arg, |
c437b273 | 888 | &me, sizeof(me))) |
4a58579b AF |
889 | err = -EFAULT; |
890 | mext_out: | |
2903ff01 | 891 | fdput(donor); |
748de673 AF |
892 | return err; |
893 | } | |
894 | ||
617ba13b MC |
895 | case EXT4_IOC_GROUP_ADD: { |
896 | struct ext4_new_group_data input; | |
ac27a0ec | 897 | |
617ba13b | 898 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
e145b35b AV |
899 | sizeof(input))) |
900 | return -EFAULT; | |
42a74f20 | 901 | |
e145b35b | 902 | return ext4_ioctl_group_add(filp, &input); |
ac27a0ec DK |
903 | } |
904 | ||
c14c6fd5 | 905 | case EXT4_IOC_MIGRATE: |
2a43a878 AK |
906 | { |
907 | int err; | |
2e149670 | 908 | if (!inode_owner_or_capable(inode)) |
2a43a878 AK |
909 | return -EACCES; |
910 | ||
a561be71 | 911 | err = mnt_want_write_file(filp); |
2a43a878 AK |
912 | if (err) |
913 | return err; | |
914 | /* | |
915 | * inode_mutex prevent write and truncate on the file. | |
916 | * Read still goes through. We take i_data_sem in | |
917 | * ext4_ext_swap_inode_data before we switch the | |
918 | * inode format to prevent read. | |
919 | */ | |
5955102c | 920 | inode_lock((inode)); |
2a43a878 | 921 | err = ext4_ext_migrate(inode); |
5955102c | 922 | inode_unlock((inode)); |
2a79f17e | 923 | mnt_drop_write_file(filp); |
2a43a878 AK |
924 | return err; |
925 | } | |
c14c6fd5 | 926 | |
ccd2506b TT |
927 | case EXT4_IOC_ALLOC_DA_BLKS: |
928 | { | |
929 | int err; | |
2e149670 | 930 | if (!inode_owner_or_capable(inode)) |
ccd2506b TT |
931 | return -EACCES; |
932 | ||
a561be71 | 933 | err = mnt_want_write_file(filp); |
ccd2506b TT |
934 | if (err) |
935 | return err; | |
936 | err = ext4_alloc_da_blocks(inode); | |
2a79f17e | 937 | mnt_drop_write_file(filp); |
ccd2506b TT |
938 | return err; |
939 | } | |
940 | ||
393d1d1d | 941 | case EXT4_IOC_SWAP_BOOT: |
3e67cfad DM |
942 | { |
943 | int err; | |
393d1d1d DTB |
944 | if (!(filp->f_mode & FMODE_WRITE)) |
945 | return -EBADF; | |
3e67cfad DM |
946 | err = mnt_want_write_file(filp); |
947 | if (err) | |
948 | return err; | |
949 | err = swap_inode_boot_loader(sb, inode); | |
950 | mnt_drop_write_file(filp); | |
951 | return err; | |
952 | } | |
393d1d1d | 953 | |
19c5246d YY |
954 | case EXT4_IOC_RESIZE_FS: { |
955 | ext4_fsblk_t n_blocks_count; | |
19c5246d | 956 | int err = 0, err2 = 0; |
7f511862 | 957 | ext4_group_t o_group = EXT4_SB(sb)->s_groups_count; |
19c5246d | 958 | |
19c5246d YY |
959 | if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, |
960 | sizeof(__u64))) { | |
961 | return -EFAULT; | |
962 | } | |
963 | ||
19c5246d YY |
964 | err = ext4_resize_begin(sb); |
965 | if (err) | |
966 | return err; | |
967 | ||
8cae6f71 | 968 | err = mnt_want_write_file(filp); |
19c5246d YY |
969 | if (err) |
970 | goto resizefs_out; | |
971 | ||
972 | err = ext4_resize_fs(sb, n_blocks_count); | |
973 | if (EXT4_SB(sb)->s_journal) { | |
974 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
975 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
976 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
977 | } | |
978 | if (err == 0) | |
979 | err = err2; | |
8cae6f71 | 980 | mnt_drop_write_file(filp); |
7f511862 TT |
981 | if (!err && (o_group > EXT4_SB(sb)->s_groups_count) && |
982 | ext4_has_group_desc_csum(sb) && | |
983 | test_opt(sb, INIT_INODE_TABLE)) | |
984 | err = ext4_register_li_request(sb, o_group); | |
985 | ||
19c5246d YY |
986 | resizefs_out: |
987 | ext4_resize_end(sb); | |
988 | return err; | |
989 | } | |
990 | ||
e681c047 LC |
991 | case FITRIM: |
992 | { | |
41431792 | 993 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
e681c047 LC |
994 | struct fstrim_range range; |
995 | int ret = 0; | |
996 | ||
997 | if (!capable(CAP_SYS_ADMIN)) | |
998 | return -EPERM; | |
999 | ||
41431792 LC |
1000 | if (!blk_queue_discard(q)) |
1001 | return -EOPNOTSUPP; | |
1002 | ||
18915b58 DW |
1003 | /* |
1004 | * We haven't replayed the journal, so we cannot use our | |
1005 | * block-bitmap-guided storage zapping commands. | |
1006 | */ | |
1007 | if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) | |
1008 | return -EROFS; | |
1009 | ||
e6705f7c | 1010 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
e681c047 LC |
1011 | sizeof(range))) |
1012 | return -EFAULT; | |
1013 | ||
5c2ed62f LC |
1014 | range.minlen = max((unsigned int)range.minlen, |
1015 | q->limits.discard_granularity); | |
e681c047 LC |
1016 | ret = ext4_trim_fs(sb, &range); |
1017 | if (ret < 0) | |
1018 | return ret; | |
1019 | ||
e6705f7c | 1020 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
e681c047 LC |
1021 | sizeof(range))) |
1022 | return -EFAULT; | |
1023 | ||
1024 | return 0; | |
1025 | } | |
7869a4a6 TT |
1026 | case EXT4_IOC_PRECACHE_EXTENTS: |
1027 | return ext4_ext_precache(inode); | |
9bd8212f | 1028 | |
db717d8e | 1029 | case EXT4_IOC_SET_ENCRYPTION_POLICY: |
9a200d07 RW |
1030 | if (!ext4_has_feature_encrypt(sb)) |
1031 | return -EOPNOTSUPP; | |
db717d8e | 1032 | return fscrypt_ioctl_set_policy(filp, (const void __user *)arg); |
9a200d07 | 1033 | |
9bd8212f | 1034 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: { |
ba679017 | 1035 | #ifdef CONFIG_EXT4_FS_ENCRYPTION |
9bd8212f MH |
1036 | int err, err2; |
1037 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
1038 | handle_t *handle; | |
1039 | ||
35997d1c | 1040 | if (!ext4_has_feature_encrypt(sb)) |
9bd8212f MH |
1041 | return -EOPNOTSUPP; |
1042 | if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { | |
1043 | err = mnt_want_write_file(filp); | |
1044 | if (err) | |
1045 | return err; | |
1046 | handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); | |
1047 | if (IS_ERR(handle)) { | |
1048 | err = PTR_ERR(handle); | |
1049 | goto pwsalt_err_exit; | |
1050 | } | |
1051 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); | |
1052 | if (err) | |
1053 | goto pwsalt_err_journal; | |
1054 | generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); | |
1055 | err = ext4_handle_dirty_metadata(handle, NULL, | |
1056 | sbi->s_sbh); | |
1057 | pwsalt_err_journal: | |
1058 | err2 = ext4_journal_stop(handle); | |
1059 | if (err2 && !err) | |
1060 | err = err2; | |
1061 | pwsalt_err_exit: | |
1062 | mnt_drop_write_file(filp); | |
1063 | if (err) | |
1064 | return err; | |
1065 | } | |
b4ab9e29 FF |
1066 | if (copy_to_user((void __user *) arg, |
1067 | sbi->s_es->s_encrypt_pw_salt, 16)) | |
9bd8212f MH |
1068 | return -EFAULT; |
1069 | return 0; | |
ba679017 EB |
1070 | #else |
1071 | return -EOPNOTSUPP; | |
1072 | #endif | |
9bd8212f | 1073 | } |
db717d8e EB |
1074 | case EXT4_IOC_GET_ENCRYPTION_POLICY: |
1075 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg); | |
9bd8212f | 1076 | |
9b7365fc LX |
1077 | case EXT4_IOC_FSGETXATTR: |
1078 | { | |
1079 | struct fsxattr fa; | |
1080 | ||
1081 | memset(&fa, 0, sizeof(struct fsxattr)); | |
9b7365fc LX |
1082 | fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE); |
1083 | ||
0b7b7779 | 1084 | if (ext4_has_feature_project(inode->i_sb)) { |
9b7365fc LX |
1085 | fa.fsx_projid = (__u32)from_kprojid(&init_user_ns, |
1086 | EXT4_I(inode)->i_projid); | |
1087 | } | |
1088 | ||
1089 | if (copy_to_user((struct fsxattr __user *)arg, | |
1090 | &fa, sizeof(fa))) | |
1091 | return -EFAULT; | |
1092 | return 0; | |
1093 | } | |
1094 | case EXT4_IOC_FSSETXATTR: | |
1095 | { | |
1096 | struct fsxattr fa; | |
1097 | int err; | |
1098 | ||
1099 | if (copy_from_user(&fa, (struct fsxattr __user *)arg, | |
1100 | sizeof(fa))) | |
1101 | return -EFAULT; | |
1102 | ||
1103 | /* Make sure caller has proper permission */ | |
1104 | if (!inode_owner_or_capable(inode)) | |
1105 | return -EACCES; | |
1106 | ||
d14e7683 JK |
1107 | if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS) |
1108 | return -EOPNOTSUPP; | |
1109 | ||
1110 | flags = ext4_xflags_to_iflags(fa.fsx_xflags); | |
1111 | if (ext4_mask_flags(inode->i_mode, flags) != flags) | |
1112 | return -EOPNOTSUPP; | |
1113 | ||
9b7365fc LX |
1114 | err = mnt_want_write_file(filp); |
1115 | if (err) | |
1116 | return err; | |
1117 | ||
5955102c | 1118 | inode_lock(inode); |
dc7ac6c4 WS |
1119 | err = ext4_ioctl_check_project(inode, &fa); |
1120 | if (err) | |
1121 | goto out; | |
9b7365fc LX |
1122 | flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) | |
1123 | (flags & EXT4_FL_XFLAG_VISIBLE); | |
1124 | err = ext4_ioctl_setflags(inode, flags); | |
9b7365fc | 1125 | if (err) |
dc7ac6c4 | 1126 | goto out; |
9b7365fc | 1127 | err = ext4_ioctl_setproject(filp, fa.fsx_projid); |
dc7ac6c4 WS |
1128 | out: |
1129 | inode_unlock(inode); | |
1130 | mnt_drop_write_file(filp); | |
1131 | return err; | |
9b7365fc | 1132 | } |
e9be2ac7 TT |
1133 | case EXT4_IOC_SHUTDOWN: |
1134 | return ext4_shutdown(sb, arg); | |
ac27a0ec DK |
1135 | default: |
1136 | return -ENOTTY; | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | #ifdef CONFIG_COMPAT | |
617ba13b | 1141 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ac27a0ec | 1142 | { |
ac27a0ec DK |
1143 | /* These are just misnamed, they actually get/put from/to user an int */ |
1144 | switch (cmd) { | |
617ba13b MC |
1145 | case EXT4_IOC32_GETFLAGS: |
1146 | cmd = EXT4_IOC_GETFLAGS; | |
ac27a0ec | 1147 | break; |
617ba13b MC |
1148 | case EXT4_IOC32_SETFLAGS: |
1149 | cmd = EXT4_IOC_SETFLAGS; | |
ac27a0ec | 1150 | break; |
617ba13b MC |
1151 | case EXT4_IOC32_GETVERSION: |
1152 | cmd = EXT4_IOC_GETVERSION; | |
ac27a0ec | 1153 | break; |
617ba13b MC |
1154 | case EXT4_IOC32_SETVERSION: |
1155 | cmd = EXT4_IOC_SETVERSION; | |
ac27a0ec | 1156 | break; |
617ba13b MC |
1157 | case EXT4_IOC32_GROUP_EXTEND: |
1158 | cmd = EXT4_IOC_GROUP_EXTEND; | |
ac27a0ec | 1159 | break; |
617ba13b MC |
1160 | case EXT4_IOC32_GETVERSION_OLD: |
1161 | cmd = EXT4_IOC_GETVERSION_OLD; | |
ac27a0ec | 1162 | break; |
617ba13b MC |
1163 | case EXT4_IOC32_SETVERSION_OLD: |
1164 | cmd = EXT4_IOC_SETVERSION_OLD; | |
ac27a0ec | 1165 | break; |
617ba13b MC |
1166 | case EXT4_IOC32_GETRSVSZ: |
1167 | cmd = EXT4_IOC_GETRSVSZ; | |
ac27a0ec | 1168 | break; |
617ba13b MC |
1169 | case EXT4_IOC32_SETRSVSZ: |
1170 | cmd = EXT4_IOC_SETRSVSZ; | |
ac27a0ec | 1171 | break; |
4d92dc0f BH |
1172 | case EXT4_IOC32_GROUP_ADD: { |
1173 | struct compat_ext4_new_group_input __user *uinput; | |
e145b35b | 1174 | struct ext4_new_group_data input; |
4d92dc0f BH |
1175 | int err; |
1176 | ||
1177 | uinput = compat_ptr(arg); | |
1178 | err = get_user(input.group, &uinput->group); | |
1179 | err |= get_user(input.block_bitmap, &uinput->block_bitmap); | |
1180 | err |= get_user(input.inode_bitmap, &uinput->inode_bitmap); | |
1181 | err |= get_user(input.inode_table, &uinput->inode_table); | |
1182 | err |= get_user(input.blocks_count, &uinput->blocks_count); | |
1183 | err |= get_user(input.reserved_blocks, | |
1184 | &uinput->reserved_blocks); | |
1185 | if (err) | |
1186 | return -EFAULT; | |
e145b35b | 1187 | return ext4_ioctl_group_add(file, &input); |
4d92dc0f | 1188 | } |
b684b2ee | 1189 | case EXT4_IOC_MOVE_EXT: |
19c5246d | 1190 | case EXT4_IOC_RESIZE_FS: |
7869a4a6 | 1191 | case EXT4_IOC_PRECACHE_EXTENTS: |
9bd8212f MH |
1192 | case EXT4_IOC_SET_ENCRYPTION_POLICY: |
1193 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: | |
1194 | case EXT4_IOC_GET_ENCRYPTION_POLICY: | |
e9be2ac7 | 1195 | case EXT4_IOC_SHUTDOWN: |
0c9ec4be | 1196 | case FS_IOC_GETFSMAP: |
b684b2ee | 1197 | break; |
ac27a0ec DK |
1198 | default: |
1199 | return -ENOIOCTLCMD; | |
1200 | } | |
5cdd7b2d | 1201 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
ac27a0ec DK |
1202 | } |
1203 | #endif |