]>
Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/ioctl.c |
ac27a0ec DK |
3 | * |
4 | * Copyright (C) 1993, 1994, 1995 | |
5 | * Remy Card (card@masi.ibp.fr) | |
6 | * Laboratoire MASI - Institut Blaise Pascal | |
7 | * Universite Pierre et Marie Curie (Paris VI) | |
8 | */ | |
9 | ||
10 | #include <linux/fs.h> | |
ac27a0ec | 11 | #include <linux/capability.h> |
ac27a0ec DK |
12 | #include <linux/time.h> |
13 | #include <linux/compat.h> | |
42a74f20 | 14 | #include <linux/mount.h> |
748de673 | 15 | #include <linux/file.h> |
9b7365fc | 16 | #include <linux/quotaops.h> |
8da4b8c4 | 17 | #include <linux/uuid.h> |
ac27a0ec | 18 | #include <asm/uaccess.h> |
3dcf5451 CH |
19 | #include "ext4_jbd2.h" |
20 | #include "ext4.h" | |
ac27a0ec | 21 | |
393d1d1d DTB |
22 | /** |
23 | * Swap memory between @a and @b for @len bytes. | |
24 | * | |
25 | * @a: pointer to first memory area | |
26 | * @b: pointer to second memory area | |
27 | * @len: number of bytes to swap | |
28 | * | |
29 | */ | |
30 | static void memswap(void *a, void *b, size_t len) | |
31 | { | |
32 | unsigned char *ap, *bp; | |
393d1d1d DTB |
33 | |
34 | ap = (unsigned char *)a; | |
35 | bp = (unsigned char *)b; | |
36 | while (len-- > 0) { | |
4b7e2db5 | 37 | swap(*ap, *bp); |
393d1d1d DTB |
38 | ap++; |
39 | bp++; | |
40 | } | |
41 | } | |
42 | ||
43 | /** | |
44 | * Swap i_data and associated attributes between @inode1 and @inode2. | |
45 | * This function is used for the primary swap between inode1 and inode2 | |
46 | * and also to revert this primary swap in case of errors. | |
47 | * | |
48 | * Therefore you have to make sure, that calling this method twice | |
49 | * will revert all changes. | |
50 | * | |
51 | * @inode1: pointer to first inode | |
52 | * @inode2: pointer to second inode | |
53 | */ | |
54 | static void swap_inode_data(struct inode *inode1, struct inode *inode2) | |
55 | { | |
56 | loff_t isize; | |
57 | struct ext4_inode_info *ei1; | |
58 | struct ext4_inode_info *ei2; | |
59 | ||
60 | ei1 = EXT4_I(inode1); | |
61 | ei2 = EXT4_I(inode2); | |
62 | ||
63 | memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags)); | |
64 | memswap(&inode1->i_version, &inode2->i_version, | |
65 | sizeof(inode1->i_version)); | |
66 | memswap(&inode1->i_blocks, &inode2->i_blocks, | |
67 | sizeof(inode1->i_blocks)); | |
68 | memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes)); | |
69 | memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime)); | |
70 | memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime)); | |
71 | ||
72 | memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data)); | |
73 | memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags)); | |
74 | memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize)); | |
cde2d7a7 TT |
75 | ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS); |
76 | ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS); | |
393d1d1d DTB |
77 | |
78 | isize = i_size_read(inode1); | |
79 | i_size_write(inode1, i_size_read(inode2)); | |
80 | i_size_write(inode2, isize); | |
81 | } | |
82 | ||
83 | /** | |
84 | * Swap the information from the given @inode and the inode | |
85 | * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other | |
86 | * important fields of the inodes. | |
87 | * | |
88 | * @sb: the super block of the filesystem | |
89 | * @inode: the inode to swap with EXT4_BOOT_LOADER_INO | |
90 | * | |
91 | */ | |
92 | static long swap_inode_boot_loader(struct super_block *sb, | |
93 | struct inode *inode) | |
94 | { | |
95 | handle_t *handle; | |
96 | int err; | |
97 | struct inode *inode_bl; | |
393d1d1d | 98 | struct ext4_inode_info *ei_bl; |
d7092ae2 | 99 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
393d1d1d | 100 | |
d8558a29 TT |
101 | if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode)) |
102 | return -EINVAL; | |
393d1d1d | 103 | |
d8558a29 TT |
104 | if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) |
105 | return -EPERM; | |
393d1d1d | 106 | |
393d1d1d | 107 | inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO); |
d8558a29 TT |
108 | if (IS_ERR(inode_bl)) |
109 | return PTR_ERR(inode_bl); | |
393d1d1d DTB |
110 | ei_bl = EXT4_I(inode_bl); |
111 | ||
112 | filemap_flush(inode->i_mapping); | |
113 | filemap_flush(inode_bl->i_mapping); | |
114 | ||
115 | /* Protect orig inodes against a truncate and make sure, | |
116 | * that only 1 swap_inode_boot_loader is running. */ | |
375e289e | 117 | lock_two_nondirectories(inode, inode_bl); |
393d1d1d DTB |
118 | |
119 | truncate_inode_pages(&inode->i_data, 0); | |
120 | truncate_inode_pages(&inode_bl->i_data, 0); | |
121 | ||
122 | /* Wait for all existing dio workers */ | |
123 | ext4_inode_block_unlocked_dio(inode); | |
124 | ext4_inode_block_unlocked_dio(inode_bl); | |
125 | inode_dio_wait(inode); | |
126 | inode_dio_wait(inode_bl); | |
127 | ||
128 | handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2); | |
129 | if (IS_ERR(handle)) { | |
130 | err = -EINVAL; | |
30d29b11 | 131 | goto journal_err_out; |
393d1d1d DTB |
132 | } |
133 | ||
134 | /* Protect extent tree against block allocations via delalloc */ | |
135 | ext4_double_down_write_data_sem(inode, inode_bl); | |
136 | ||
137 | if (inode_bl->i_nlink == 0) { | |
138 | /* this inode has never been used as a BOOT_LOADER */ | |
139 | set_nlink(inode_bl, 1); | |
140 | i_uid_write(inode_bl, 0); | |
141 | i_gid_write(inode_bl, 0); | |
142 | inode_bl->i_flags = 0; | |
143 | ei_bl->i_flags = 0; | |
144 | inode_bl->i_version = 1; | |
145 | i_size_write(inode_bl, 0); | |
146 | inode_bl->i_mode = S_IFREG; | |
e2b911c5 | 147 | if (ext4_has_feature_extents(sb)) { |
393d1d1d DTB |
148 | ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS); |
149 | ext4_ext_tree_init(handle, inode_bl); | |
150 | } else | |
151 | memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data)); | |
152 | } | |
153 | ||
154 | swap_inode_data(inode, inode_bl); | |
155 | ||
eeca7ea1 | 156 | inode->i_ctime = inode_bl->i_ctime = current_time(inode); |
393d1d1d DTB |
157 | |
158 | spin_lock(&sbi->s_next_gen_lock); | |
159 | inode->i_generation = sbi->s_next_generation++; | |
160 | inode_bl->i_generation = sbi->s_next_generation++; | |
161 | spin_unlock(&sbi->s_next_gen_lock); | |
162 | ||
163 | ext4_discard_preallocations(inode); | |
164 | ||
165 | err = ext4_mark_inode_dirty(handle, inode); | |
166 | if (err < 0) { | |
167 | ext4_warning(inode->i_sb, | |
168 | "couldn't mark inode #%lu dirty (err %d)", | |
169 | inode->i_ino, err); | |
170 | /* Revert all changes: */ | |
171 | swap_inode_data(inode, inode_bl); | |
172 | } else { | |
173 | err = ext4_mark_inode_dirty(handle, inode_bl); | |
174 | if (err < 0) { | |
175 | ext4_warning(inode_bl->i_sb, | |
176 | "couldn't mark inode #%lu dirty (err %d)", | |
177 | inode_bl->i_ino, err); | |
178 | /* Revert all changes: */ | |
179 | swap_inode_data(inode, inode_bl); | |
180 | ext4_mark_inode_dirty(handle, inode); | |
181 | } | |
182 | } | |
393d1d1d | 183 | ext4_journal_stop(handle); |
393d1d1d DTB |
184 | ext4_double_up_write_data_sem(inode, inode_bl); |
185 | ||
30d29b11 | 186 | journal_err_out: |
393d1d1d DTB |
187 | ext4_inode_resume_unlocked_dio(inode); |
188 | ext4_inode_resume_unlocked_dio(inode_bl); | |
375e289e | 189 | unlock_two_nondirectories(inode, inode_bl); |
393d1d1d | 190 | iput(inode_bl); |
393d1d1d DTB |
191 | return err; |
192 | } | |
193 | ||
9bd8212f MH |
194 | static int uuid_is_zero(__u8 u[16]) |
195 | { | |
196 | int i; | |
197 | ||
198 | for (i = 0; i < 16; i++) | |
199 | if (u[i]) | |
200 | return 0; | |
201 | return 1; | |
202 | } | |
203 | ||
9b7365fc LX |
204 | static int ext4_ioctl_setflags(struct inode *inode, |
205 | unsigned int flags) | |
206 | { | |
207 | struct ext4_inode_info *ei = EXT4_I(inode); | |
208 | handle_t *handle = NULL; | |
fdde368e | 209 | int err = -EPERM, migrate = 0; |
9b7365fc LX |
210 | struct ext4_iloc iloc; |
211 | unsigned int oldflags, mask, i; | |
212 | unsigned int jflag; | |
213 | ||
214 | /* Is it quota file? Do not allow user to mess with it */ | |
215 | if (IS_NOQUOTA(inode)) | |
216 | goto flags_out; | |
217 | ||
218 | oldflags = ei->i_flags; | |
219 | ||
220 | /* The JOURNAL_DATA flag is modifiable only by root */ | |
221 | jflag = flags & EXT4_JOURNAL_DATA_FL; | |
222 | ||
223 | /* | |
224 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by | |
225 | * the relevant capability. | |
226 | * | |
227 | * This test looks nicer. Thanks to Pauline Middelink | |
228 | */ | |
229 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { | |
230 | if (!capable(CAP_LINUX_IMMUTABLE)) | |
231 | goto flags_out; | |
232 | } | |
233 | ||
234 | /* | |
235 | * The JOURNAL_DATA flag can only be changed by | |
236 | * the relevant capability. | |
237 | */ | |
238 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { | |
239 | if (!capable(CAP_SYS_RESOURCE)) | |
240 | goto flags_out; | |
241 | } | |
242 | if ((flags ^ oldflags) & EXT4_EXTENTS_FL) | |
243 | migrate = 1; | |
244 | ||
245 | if (flags & EXT4_EOFBLOCKS_FL) { | |
246 | /* we don't support adding EOFBLOCKS flag */ | |
247 | if (!(oldflags & EXT4_EOFBLOCKS_FL)) { | |
248 | err = -EOPNOTSUPP; | |
249 | goto flags_out; | |
250 | } | |
2c98eb5e TT |
251 | } else if (oldflags & EXT4_EOFBLOCKS_FL) { |
252 | err = ext4_truncate(inode); | |
253 | if (err) | |
254 | goto flags_out; | |
255 | } | |
9b7365fc LX |
256 | |
257 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); | |
258 | if (IS_ERR(handle)) { | |
259 | err = PTR_ERR(handle); | |
260 | goto flags_out; | |
261 | } | |
262 | if (IS_SYNC(inode)) | |
263 | ext4_handle_sync(handle); | |
264 | err = ext4_reserve_inode_write(handle, inode, &iloc); | |
265 | if (err) | |
266 | goto flags_err; | |
267 | ||
268 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { | |
269 | if (!(mask & EXT4_FL_USER_MODIFIABLE)) | |
270 | continue; | |
f8011d93 JK |
271 | /* These flags get special treatment later */ |
272 | if (mask == EXT4_JOURNAL_DATA_FL || mask == EXT4_EXTENTS_FL) | |
273 | continue; | |
9b7365fc LX |
274 | if (mask & flags) |
275 | ext4_set_inode_flag(inode, i); | |
276 | else | |
277 | ext4_clear_inode_flag(inode, i); | |
278 | } | |
279 | ||
280 | ext4_set_inode_flags(inode); | |
eeca7ea1 | 281 | inode->i_ctime = current_time(inode); |
9b7365fc LX |
282 | |
283 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); | |
284 | flags_err: | |
285 | ext4_journal_stop(handle); | |
286 | if (err) | |
287 | goto flags_out; | |
288 | ||
289 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) | |
290 | err = ext4_change_inode_journal_flag(inode, jflag); | |
291 | if (err) | |
292 | goto flags_out; | |
293 | if (migrate) { | |
294 | if (flags & EXT4_EXTENTS_FL) | |
295 | err = ext4_ext_migrate(inode); | |
296 | else | |
297 | err = ext4_ind_migrate(inode); | |
298 | } | |
299 | ||
300 | flags_out: | |
301 | return err; | |
302 | } | |
303 | ||
304 | #ifdef CONFIG_QUOTA | |
305 | static int ext4_ioctl_setproject(struct file *filp, __u32 projid) | |
306 | { | |
307 | struct inode *inode = file_inode(filp); | |
308 | struct super_block *sb = inode->i_sb; | |
309 | struct ext4_inode_info *ei = EXT4_I(inode); | |
310 | int err, rc; | |
311 | handle_t *handle; | |
312 | kprojid_t kprojid; | |
313 | struct ext4_iloc iloc; | |
314 | struct ext4_inode *raw_inode; | |
079788d0 | 315 | struct dquot *transfer_to[MAXQUOTAS] = { }; |
9b7365fc | 316 | |
0b7b7779 | 317 | if (!ext4_has_feature_project(sb)) { |
9b7365fc LX |
318 | if (projid != EXT4_DEF_PROJID) |
319 | return -EOPNOTSUPP; | |
320 | else | |
321 | return 0; | |
322 | } | |
323 | ||
324 | if (EXT4_INODE_SIZE(sb) <= EXT4_GOOD_OLD_INODE_SIZE) | |
325 | return -EOPNOTSUPP; | |
326 | ||
327 | kprojid = make_kprojid(&init_user_ns, (projid_t)projid); | |
328 | ||
329 | if (projid_eq(kprojid, EXT4_I(inode)->i_projid)) | |
330 | return 0; | |
331 | ||
332 | err = mnt_want_write_file(filp); | |
333 | if (err) | |
334 | return err; | |
335 | ||
336 | err = -EPERM; | |
5955102c | 337 | inode_lock(inode); |
9b7365fc LX |
338 | /* Is it quota file? Do not allow user to mess with it */ |
339 | if (IS_NOQUOTA(inode)) | |
340 | goto out_unlock; | |
341 | ||
342 | err = ext4_get_inode_loc(inode, &iloc); | |
343 | if (err) | |
344 | goto out_unlock; | |
345 | ||
346 | raw_inode = ext4_raw_inode(&iloc); | |
347 | if (!EXT4_FITS_IN_INODE(raw_inode, ei, i_projid)) { | |
348 | err = -EOVERFLOW; | |
349 | brelse(iloc.bh); | |
350 | goto out_unlock; | |
351 | } | |
352 | brelse(iloc.bh); | |
353 | ||
354 | dquot_initialize(inode); | |
355 | ||
356 | handle = ext4_journal_start(inode, EXT4_HT_QUOTA, | |
357 | EXT4_QUOTA_INIT_BLOCKS(sb) + | |
358 | EXT4_QUOTA_DEL_BLOCKS(sb) + 3); | |
359 | if (IS_ERR(handle)) { | |
360 | err = PTR_ERR(handle); | |
361 | goto out_unlock; | |
362 | } | |
363 | ||
364 | err = ext4_reserve_inode_write(handle, inode, &iloc); | |
365 | if (err) | |
366 | goto out_stop; | |
367 | ||
079788d0 WS |
368 | transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid)); |
369 | if (!IS_ERR(transfer_to[PRJQUOTA])) { | |
370 | err = __dquot_transfer(inode, transfer_to); | |
371 | dqput(transfer_to[PRJQUOTA]); | |
372 | if (err) | |
373 | goto out_dirty; | |
9b7365fc | 374 | } |
079788d0 | 375 | |
9b7365fc | 376 | EXT4_I(inode)->i_projid = kprojid; |
eeca7ea1 | 377 | inode->i_ctime = current_time(inode); |
9b7365fc LX |
378 | out_dirty: |
379 | rc = ext4_mark_iloc_dirty(handle, inode, &iloc); | |
380 | if (!err) | |
381 | err = rc; | |
382 | out_stop: | |
383 | ext4_journal_stop(handle); | |
384 | out_unlock: | |
5955102c | 385 | inode_unlock(inode); |
9b7365fc LX |
386 | mnt_drop_write_file(filp); |
387 | return err; | |
388 | } | |
389 | #else | |
390 | static int ext4_ioctl_setproject(struct file *filp, __u32 projid) | |
391 | { | |
392 | if (projid != EXT4_DEF_PROJID) | |
393 | return -EOPNOTSUPP; | |
394 | return 0; | |
395 | } | |
396 | #endif | |
397 | ||
398 | /* Transfer internal flags to xflags */ | |
399 | static inline __u32 ext4_iflags_to_xflags(unsigned long iflags) | |
400 | { | |
401 | __u32 xflags = 0; | |
402 | ||
403 | if (iflags & EXT4_SYNC_FL) | |
404 | xflags |= FS_XFLAG_SYNC; | |
405 | if (iflags & EXT4_IMMUTABLE_FL) | |
406 | xflags |= FS_XFLAG_IMMUTABLE; | |
407 | if (iflags & EXT4_APPEND_FL) | |
408 | xflags |= FS_XFLAG_APPEND; | |
409 | if (iflags & EXT4_NODUMP_FL) | |
410 | xflags |= FS_XFLAG_NODUMP; | |
411 | if (iflags & EXT4_NOATIME_FL) | |
412 | xflags |= FS_XFLAG_NOATIME; | |
413 | if (iflags & EXT4_PROJINHERIT_FL) | |
414 | xflags |= FS_XFLAG_PROJINHERIT; | |
415 | return xflags; | |
416 | } | |
417 | ||
d14e7683 JK |
418 | #define EXT4_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \ |
419 | FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \ | |
420 | FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT) | |
421 | ||
9b7365fc LX |
422 | /* Transfer xflags flags to internal */ |
423 | static inline unsigned long ext4_xflags_to_iflags(__u32 xflags) | |
424 | { | |
425 | unsigned long iflags = 0; | |
426 | ||
427 | if (xflags & FS_XFLAG_SYNC) | |
428 | iflags |= EXT4_SYNC_FL; | |
429 | if (xflags & FS_XFLAG_IMMUTABLE) | |
430 | iflags |= EXT4_IMMUTABLE_FL; | |
431 | if (xflags & FS_XFLAG_APPEND) | |
432 | iflags |= EXT4_APPEND_FL; | |
433 | if (xflags & FS_XFLAG_NODUMP) | |
434 | iflags |= EXT4_NODUMP_FL; | |
435 | if (xflags & FS_XFLAG_NOATIME) | |
436 | iflags |= EXT4_NOATIME_FL; | |
437 | if (xflags & FS_XFLAG_PROJINHERIT) | |
438 | iflags |= EXT4_PROJINHERIT_FL; | |
439 | ||
440 | return iflags; | |
441 | } | |
442 | ||
5cdd7b2d | 443 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
ac27a0ec | 444 | { |
496ad9aa | 445 | struct inode *inode = file_inode(filp); |
bab08ab9 | 446 | struct super_block *sb = inode->i_sb; |
617ba13b | 447 | struct ext4_inode_info *ei = EXT4_I(inode); |
ac27a0ec | 448 | unsigned int flags; |
ac27a0ec | 449 | |
af5bc92d | 450 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
ac27a0ec DK |
451 | |
452 | switch (cmd) { | |
617ba13b | 453 | case EXT4_IOC_GETFLAGS: |
ff9ddf7e | 454 | ext4_get_inode_flags(ei); |
617ba13b | 455 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
ac27a0ec | 456 | return put_user(flags, (int __user *) arg); |
617ba13b | 457 | case EXT4_IOC_SETFLAGS: { |
9b7365fc | 458 | int err; |
ac27a0ec | 459 | |
2e149670 | 460 | if (!inode_owner_or_capable(inode)) |
ac27a0ec DK |
461 | return -EACCES; |
462 | ||
463 | if (get_user(flags, (int __user *) arg)) | |
464 | return -EFAULT; | |
465 | ||
d14e7683 JK |
466 | if (flags & ~EXT4_FL_USER_VISIBLE) |
467 | return -EOPNOTSUPP; | |
468 | /* | |
469 | * chattr(1) grabs flags via GETFLAGS, modifies the result and | |
470 | * passes that to SETFLAGS. So we cannot easily make SETFLAGS | |
471 | * more restrictive than just silently masking off visible but | |
472 | * not settable flags as we always did. | |
473 | */ | |
474 | flags &= EXT4_FL_USER_MODIFIABLE; | |
475 | if (ext4_mask_flags(inode->i_mode, flags) != flags) | |
476 | return -EOPNOTSUPP; | |
477 | ||
a561be71 | 478 | err = mnt_want_write_file(filp); |
42a74f20 DH |
479 | if (err) |
480 | return err; | |
481 | ||
5955102c | 482 | inode_lock(inode); |
9b7365fc | 483 | err = ext4_ioctl_setflags(inode, flags); |
5955102c | 484 | inode_unlock(inode); |
2a79f17e | 485 | mnt_drop_write_file(filp); |
ac27a0ec DK |
486 | return err; |
487 | } | |
617ba13b MC |
488 | case EXT4_IOC_GETVERSION: |
489 | case EXT4_IOC_GETVERSION_OLD: | |
ac27a0ec | 490 | return put_user(inode->i_generation, (int __user *) arg); |
617ba13b MC |
491 | case EXT4_IOC_SETVERSION: |
492 | case EXT4_IOC_SETVERSION_OLD: { | |
ac27a0ec | 493 | handle_t *handle; |
617ba13b | 494 | struct ext4_iloc iloc; |
ac27a0ec DK |
495 | __u32 generation; |
496 | int err; | |
497 | ||
2e149670 | 498 | if (!inode_owner_or_capable(inode)) |
ac27a0ec | 499 | return -EPERM; |
42a74f20 | 500 | |
9aa5d32b | 501 | if (ext4_has_metadata_csum(inode->i_sb)) { |
814525f4 DW |
502 | ext4_warning(sb, "Setting inode version is not " |
503 | "supported with metadata_csum enabled."); | |
504 | return -ENOTTY; | |
505 | } | |
506 | ||
a561be71 | 507 | err = mnt_want_write_file(filp); |
42a74f20 DH |
508 | if (err) |
509 | return err; | |
510 | if (get_user(generation, (int __user *) arg)) { | |
511 | err = -EFAULT; | |
512 | goto setversion_out; | |
513 | } | |
ac27a0ec | 514 | |
5955102c | 515 | inode_lock(inode); |
9924a92a | 516 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
42a74f20 DH |
517 | if (IS_ERR(handle)) { |
518 | err = PTR_ERR(handle); | |
6c2155b9 | 519 | goto unlock_out; |
42a74f20 | 520 | } |
617ba13b | 521 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec | 522 | if (err == 0) { |
eeca7ea1 | 523 | inode->i_ctime = current_time(inode); |
ac27a0ec | 524 | inode->i_generation = generation; |
617ba13b | 525 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 526 | } |
617ba13b | 527 | ext4_journal_stop(handle); |
6c2155b9 DH |
528 | |
529 | unlock_out: | |
5955102c | 530 | inode_unlock(inode); |
42a74f20 | 531 | setversion_out: |
2a79f17e | 532 | mnt_drop_write_file(filp); |
ac27a0ec DK |
533 | return err; |
534 | } | |
617ba13b MC |
535 | case EXT4_IOC_GROUP_EXTEND: { |
536 | ext4_fsblk_t n_blocks_count; | |
ac046f1d | 537 | int err, err2=0; |
ac27a0ec | 538 | |
8f82f840 YY |
539 | err = ext4_resize_begin(sb); |
540 | if (err) | |
541 | return err; | |
ac27a0ec | 542 | |
014a1770 DH |
543 | if (get_user(n_blocks_count, (__u32 __user *)arg)) { |
544 | err = -EFAULT; | |
545 | goto group_extend_out; | |
546 | } | |
ac27a0ec | 547 | |
e2b911c5 | 548 | if (ext4_has_feature_bigalloc(sb)) { |
bab08ab9 TT |
549 | ext4_msg(sb, KERN_ERR, |
550 | "Online resizing not supported with bigalloc"); | |
014a1770 DH |
551 | err = -EOPNOTSUPP; |
552 | goto group_extend_out; | |
bab08ab9 TT |
553 | } |
554 | ||
a561be71 | 555 | err = mnt_want_write_file(filp); |
42a74f20 | 556 | if (err) |
014a1770 | 557 | goto group_extend_out; |
42a74f20 | 558 | |
617ba13b | 559 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
ac046f1d PT |
560 | if (EXT4_SB(sb)->s_journal) { |
561 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
562 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
563 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
564 | } | |
7ffe1ea8 HK |
565 | if (err == 0) |
566 | err = err2; | |
2a79f17e | 567 | mnt_drop_write_file(filp); |
014a1770 | 568 | group_extend_out: |
8f82f840 | 569 | ext4_resize_end(sb); |
ac27a0ec DK |
570 | return err; |
571 | } | |
748de673 AF |
572 | |
573 | case EXT4_IOC_MOVE_EXT: { | |
574 | struct move_extent me; | |
2903ff01 AV |
575 | struct fd donor; |
576 | int err; | |
748de673 | 577 | |
4a58579b AF |
578 | if (!(filp->f_mode & FMODE_READ) || |
579 | !(filp->f_mode & FMODE_WRITE)) | |
580 | return -EBADF; | |
581 | ||
748de673 AF |
582 | if (copy_from_user(&me, |
583 | (struct move_extent __user *)arg, sizeof(me))) | |
584 | return -EFAULT; | |
4a58579b | 585 | me.moved_len = 0; |
748de673 | 586 | |
2903ff01 AV |
587 | donor = fdget(me.donor_fd); |
588 | if (!donor.file) | |
748de673 AF |
589 | return -EBADF; |
590 | ||
2903ff01 | 591 | if (!(donor.file->f_mode & FMODE_WRITE)) { |
4a58579b AF |
592 | err = -EBADF; |
593 | goto mext_out; | |
748de673 AF |
594 | } |
595 | ||
e2b911c5 | 596 | if (ext4_has_feature_bigalloc(sb)) { |
bab08ab9 TT |
597 | ext4_msg(sb, KERN_ERR, |
598 | "Online defrag not supported with bigalloc"); | |
399c9b86 AV |
599 | err = -EOPNOTSUPP; |
600 | goto mext_out; | |
73f34a5e RZ |
601 | } else if (IS_DAX(inode)) { |
602 | ext4_msg(sb, KERN_ERR, | |
603 | "Online defrag not supported with DAX"); | |
604 | err = -EOPNOTSUPP; | |
605 | goto mext_out; | |
bab08ab9 TT |
606 | } |
607 | ||
a561be71 | 608 | err = mnt_want_write_file(filp); |
4a58579b AF |
609 | if (err) |
610 | goto mext_out; | |
611 | ||
2903ff01 | 612 | err = ext4_move_extents(filp, donor.file, me.orig_start, |
748de673 | 613 | me.donor_start, me.len, &me.moved_len); |
2a79f17e | 614 | mnt_drop_write_file(filp); |
748de673 | 615 | |
60e6679e | 616 | if (copy_to_user((struct move_extent __user *)arg, |
c437b273 | 617 | &me, sizeof(me))) |
4a58579b AF |
618 | err = -EFAULT; |
619 | mext_out: | |
2903ff01 | 620 | fdput(donor); |
748de673 AF |
621 | return err; |
622 | } | |
623 | ||
617ba13b MC |
624 | case EXT4_IOC_GROUP_ADD: { |
625 | struct ext4_new_group_data input; | |
ac046f1d | 626 | int err, err2=0; |
ac27a0ec | 627 | |
8f82f840 YY |
628 | err = ext4_resize_begin(sb); |
629 | if (err) | |
630 | return err; | |
ac27a0ec | 631 | |
617ba13b | 632 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
014a1770 DH |
633 | sizeof(input))) { |
634 | err = -EFAULT; | |
635 | goto group_add_out; | |
636 | } | |
ac27a0ec | 637 | |
e2b911c5 | 638 | if (ext4_has_feature_bigalloc(sb)) { |
bab08ab9 TT |
639 | ext4_msg(sb, KERN_ERR, |
640 | "Online resizing not supported with bigalloc"); | |
014a1770 DH |
641 | err = -EOPNOTSUPP; |
642 | goto group_add_out; | |
bab08ab9 TT |
643 | } |
644 | ||
a561be71 | 645 | err = mnt_want_write_file(filp); |
42a74f20 | 646 | if (err) |
014a1770 | 647 | goto group_add_out; |
42a74f20 | 648 | |
617ba13b | 649 | err = ext4_group_add(sb, &input); |
ac046f1d PT |
650 | if (EXT4_SB(sb)->s_journal) { |
651 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
652 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
653 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
654 | } | |
7ffe1ea8 HK |
655 | if (err == 0) |
656 | err = err2; | |
2a79f17e | 657 | mnt_drop_write_file(filp); |
7f511862 TT |
658 | if (!err && ext4_has_group_desc_csum(sb) && |
659 | test_opt(sb, INIT_INODE_TABLE)) | |
660 | err = ext4_register_li_request(sb, input.group); | |
014a1770 | 661 | group_add_out: |
8f82f840 | 662 | ext4_resize_end(sb); |
ac27a0ec DK |
663 | return err; |
664 | } | |
665 | ||
c14c6fd5 | 666 | case EXT4_IOC_MIGRATE: |
2a43a878 AK |
667 | { |
668 | int err; | |
2e149670 | 669 | if (!inode_owner_or_capable(inode)) |
2a43a878 AK |
670 | return -EACCES; |
671 | ||
a561be71 | 672 | err = mnt_want_write_file(filp); |
2a43a878 AK |
673 | if (err) |
674 | return err; | |
675 | /* | |
676 | * inode_mutex prevent write and truncate on the file. | |
677 | * Read still goes through. We take i_data_sem in | |
678 | * ext4_ext_swap_inode_data before we switch the | |
679 | * inode format to prevent read. | |
680 | */ | |
5955102c | 681 | inode_lock((inode)); |
2a43a878 | 682 | err = ext4_ext_migrate(inode); |
5955102c | 683 | inode_unlock((inode)); |
2a79f17e | 684 | mnt_drop_write_file(filp); |
2a43a878 AK |
685 | return err; |
686 | } | |
c14c6fd5 | 687 | |
ccd2506b TT |
688 | case EXT4_IOC_ALLOC_DA_BLKS: |
689 | { | |
690 | int err; | |
2e149670 | 691 | if (!inode_owner_or_capable(inode)) |
ccd2506b TT |
692 | return -EACCES; |
693 | ||
a561be71 | 694 | err = mnt_want_write_file(filp); |
ccd2506b TT |
695 | if (err) |
696 | return err; | |
697 | err = ext4_alloc_da_blocks(inode); | |
2a79f17e | 698 | mnt_drop_write_file(filp); |
ccd2506b TT |
699 | return err; |
700 | } | |
701 | ||
393d1d1d | 702 | case EXT4_IOC_SWAP_BOOT: |
3e67cfad DM |
703 | { |
704 | int err; | |
393d1d1d DTB |
705 | if (!(filp->f_mode & FMODE_WRITE)) |
706 | return -EBADF; | |
3e67cfad DM |
707 | err = mnt_want_write_file(filp); |
708 | if (err) | |
709 | return err; | |
710 | err = swap_inode_boot_loader(sb, inode); | |
711 | mnt_drop_write_file(filp); | |
712 | return err; | |
713 | } | |
393d1d1d | 714 | |
19c5246d YY |
715 | case EXT4_IOC_RESIZE_FS: { |
716 | ext4_fsblk_t n_blocks_count; | |
19c5246d | 717 | int err = 0, err2 = 0; |
7f511862 | 718 | ext4_group_t o_group = EXT4_SB(sb)->s_groups_count; |
19c5246d | 719 | |
e2b911c5 | 720 | if (ext4_has_feature_bigalloc(sb)) { |
19c5246d YY |
721 | ext4_msg(sb, KERN_ERR, |
722 | "Online resizing not (yet) supported with bigalloc"); | |
723 | return -EOPNOTSUPP; | |
724 | } | |
725 | ||
19c5246d YY |
726 | if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, |
727 | sizeof(__u64))) { | |
728 | return -EFAULT; | |
729 | } | |
730 | ||
19c5246d YY |
731 | err = ext4_resize_begin(sb); |
732 | if (err) | |
733 | return err; | |
734 | ||
8cae6f71 | 735 | err = mnt_want_write_file(filp); |
19c5246d YY |
736 | if (err) |
737 | goto resizefs_out; | |
738 | ||
739 | err = ext4_resize_fs(sb, n_blocks_count); | |
740 | if (EXT4_SB(sb)->s_journal) { | |
741 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
742 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
743 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
744 | } | |
745 | if (err == 0) | |
746 | err = err2; | |
8cae6f71 | 747 | mnt_drop_write_file(filp); |
7f511862 TT |
748 | if (!err && (o_group > EXT4_SB(sb)->s_groups_count) && |
749 | ext4_has_group_desc_csum(sb) && | |
750 | test_opt(sb, INIT_INODE_TABLE)) | |
751 | err = ext4_register_li_request(sb, o_group); | |
752 | ||
19c5246d YY |
753 | resizefs_out: |
754 | ext4_resize_end(sb); | |
755 | return err; | |
756 | } | |
757 | ||
e681c047 LC |
758 | case FITRIM: |
759 | { | |
41431792 | 760 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
e681c047 LC |
761 | struct fstrim_range range; |
762 | int ret = 0; | |
763 | ||
764 | if (!capable(CAP_SYS_ADMIN)) | |
765 | return -EPERM; | |
766 | ||
41431792 LC |
767 | if (!blk_queue_discard(q)) |
768 | return -EOPNOTSUPP; | |
769 | ||
e6705f7c | 770 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
e681c047 LC |
771 | sizeof(range))) |
772 | return -EFAULT; | |
773 | ||
5c2ed62f LC |
774 | range.minlen = max((unsigned int)range.minlen, |
775 | q->limits.discard_granularity); | |
e681c047 LC |
776 | ret = ext4_trim_fs(sb, &range); |
777 | if (ret < 0) | |
778 | return ret; | |
779 | ||
e6705f7c | 780 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
e681c047 LC |
781 | sizeof(range))) |
782 | return -EFAULT; | |
783 | ||
784 | return 0; | |
785 | } | |
7869a4a6 TT |
786 | case EXT4_IOC_PRECACHE_EXTENTS: |
787 | return ext4_ext_precache(inode); | |
9bd8212f MH |
788 | case EXT4_IOC_SET_ENCRYPTION_POLICY: { |
789 | #ifdef CONFIG_EXT4_FS_ENCRYPTION | |
a7550b30 | 790 | struct fscrypt_policy policy; |
9bd8212f | 791 | |
9a200d07 RW |
792 | if (!ext4_has_feature_encrypt(sb)) |
793 | return -EOPNOTSUPP; | |
794 | ||
9bd8212f | 795 | if (copy_from_user(&policy, |
a7550b30 JK |
796 | (struct fscrypt_policy __user *)arg, |
797 | sizeof(policy))) | |
798 | return -EFAULT; | |
ba63f23d | 799 | return fscrypt_process_policy(filp, &policy); |
9bd8212f MH |
800 | #else |
801 | return -EOPNOTSUPP; | |
802 | #endif | |
803 | } | |
804 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: { | |
805 | int err, err2; | |
806 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
807 | handle_t *handle; | |
808 | ||
809 | if (!ext4_sb_has_crypto(sb)) | |
810 | return -EOPNOTSUPP; | |
811 | if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) { | |
812 | err = mnt_want_write_file(filp); | |
813 | if (err) | |
814 | return err; | |
815 | handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1); | |
816 | if (IS_ERR(handle)) { | |
817 | err = PTR_ERR(handle); | |
818 | goto pwsalt_err_exit; | |
819 | } | |
820 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); | |
821 | if (err) | |
822 | goto pwsalt_err_journal; | |
823 | generate_random_uuid(sbi->s_es->s_encrypt_pw_salt); | |
824 | err = ext4_handle_dirty_metadata(handle, NULL, | |
825 | sbi->s_sbh); | |
826 | pwsalt_err_journal: | |
827 | err2 = ext4_journal_stop(handle); | |
828 | if (err2 && !err) | |
829 | err = err2; | |
830 | pwsalt_err_exit: | |
831 | mnt_drop_write_file(filp); | |
832 | if (err) | |
833 | return err; | |
834 | } | |
b4ab9e29 FF |
835 | if (copy_to_user((void __user *) arg, |
836 | sbi->s_es->s_encrypt_pw_salt, 16)) | |
9bd8212f MH |
837 | return -EFAULT; |
838 | return 0; | |
839 | } | |
840 | case EXT4_IOC_GET_ENCRYPTION_POLICY: { | |
841 | #ifdef CONFIG_EXT4_FS_ENCRYPTION | |
a7550b30 | 842 | struct fscrypt_policy policy; |
9bd8212f MH |
843 | int err = 0; |
844 | ||
845 | if (!ext4_encrypted_inode(inode)) | |
846 | return -ENOENT; | |
a7550b30 | 847 | err = fscrypt_get_policy(inode, &policy); |
9bd8212f MH |
848 | if (err) |
849 | return err; | |
b4ab9e29 | 850 | if (copy_to_user((void __user *)arg, &policy, sizeof(policy))) |
9bd8212f MH |
851 | return -EFAULT; |
852 | return 0; | |
853 | #else | |
854 | return -EOPNOTSUPP; | |
855 | #endif | |
856 | } | |
9b7365fc LX |
857 | case EXT4_IOC_FSGETXATTR: |
858 | { | |
859 | struct fsxattr fa; | |
860 | ||
861 | memset(&fa, 0, sizeof(struct fsxattr)); | |
862 | ext4_get_inode_flags(ei); | |
863 | fa.fsx_xflags = ext4_iflags_to_xflags(ei->i_flags & EXT4_FL_USER_VISIBLE); | |
864 | ||
0b7b7779 | 865 | if (ext4_has_feature_project(inode->i_sb)) { |
9b7365fc LX |
866 | fa.fsx_projid = (__u32)from_kprojid(&init_user_ns, |
867 | EXT4_I(inode)->i_projid); | |
868 | } | |
869 | ||
870 | if (copy_to_user((struct fsxattr __user *)arg, | |
871 | &fa, sizeof(fa))) | |
872 | return -EFAULT; | |
873 | return 0; | |
874 | } | |
875 | case EXT4_IOC_FSSETXATTR: | |
876 | { | |
877 | struct fsxattr fa; | |
878 | int err; | |
879 | ||
880 | if (copy_from_user(&fa, (struct fsxattr __user *)arg, | |
881 | sizeof(fa))) | |
882 | return -EFAULT; | |
883 | ||
884 | /* Make sure caller has proper permission */ | |
885 | if (!inode_owner_or_capable(inode)) | |
886 | return -EACCES; | |
887 | ||
d14e7683 JK |
888 | if (fa.fsx_xflags & ~EXT4_SUPPORTED_FS_XFLAGS) |
889 | return -EOPNOTSUPP; | |
890 | ||
891 | flags = ext4_xflags_to_iflags(fa.fsx_xflags); | |
892 | if (ext4_mask_flags(inode->i_mode, flags) != flags) | |
893 | return -EOPNOTSUPP; | |
894 | ||
9b7365fc LX |
895 | err = mnt_want_write_file(filp); |
896 | if (err) | |
897 | return err; | |
898 | ||
5955102c | 899 | inode_lock(inode); |
9b7365fc LX |
900 | flags = (ei->i_flags & ~EXT4_FL_XFLAG_VISIBLE) | |
901 | (flags & EXT4_FL_XFLAG_VISIBLE); | |
902 | err = ext4_ioctl_setflags(inode, flags); | |
5955102c | 903 | inode_unlock(inode); |
9b7365fc LX |
904 | mnt_drop_write_file(filp); |
905 | if (err) | |
906 | return err; | |
907 | ||
908 | err = ext4_ioctl_setproject(filp, fa.fsx_projid); | |
909 | if (err) | |
910 | return err; | |
911 | ||
912 | return 0; | |
913 | } | |
ac27a0ec DK |
914 | default: |
915 | return -ENOTTY; | |
916 | } | |
917 | } | |
918 | ||
919 | #ifdef CONFIG_COMPAT | |
617ba13b | 920 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ac27a0ec | 921 | { |
ac27a0ec DK |
922 | /* These are just misnamed, they actually get/put from/to user an int */ |
923 | switch (cmd) { | |
617ba13b MC |
924 | case EXT4_IOC32_GETFLAGS: |
925 | cmd = EXT4_IOC_GETFLAGS; | |
ac27a0ec | 926 | break; |
617ba13b MC |
927 | case EXT4_IOC32_SETFLAGS: |
928 | cmd = EXT4_IOC_SETFLAGS; | |
ac27a0ec | 929 | break; |
617ba13b MC |
930 | case EXT4_IOC32_GETVERSION: |
931 | cmd = EXT4_IOC_GETVERSION; | |
ac27a0ec | 932 | break; |
617ba13b MC |
933 | case EXT4_IOC32_SETVERSION: |
934 | cmd = EXT4_IOC_SETVERSION; | |
ac27a0ec | 935 | break; |
617ba13b MC |
936 | case EXT4_IOC32_GROUP_EXTEND: |
937 | cmd = EXT4_IOC_GROUP_EXTEND; | |
ac27a0ec | 938 | break; |
617ba13b MC |
939 | case EXT4_IOC32_GETVERSION_OLD: |
940 | cmd = EXT4_IOC_GETVERSION_OLD; | |
ac27a0ec | 941 | break; |
617ba13b MC |
942 | case EXT4_IOC32_SETVERSION_OLD: |
943 | cmd = EXT4_IOC_SETVERSION_OLD; | |
ac27a0ec | 944 | break; |
617ba13b MC |
945 | case EXT4_IOC32_GETRSVSZ: |
946 | cmd = EXT4_IOC_GETRSVSZ; | |
ac27a0ec | 947 | break; |
617ba13b MC |
948 | case EXT4_IOC32_SETRSVSZ: |
949 | cmd = EXT4_IOC_SETRSVSZ; | |
ac27a0ec | 950 | break; |
4d92dc0f BH |
951 | case EXT4_IOC32_GROUP_ADD: { |
952 | struct compat_ext4_new_group_input __user *uinput; | |
953 | struct ext4_new_group_input input; | |
954 | mm_segment_t old_fs; | |
955 | int err; | |
956 | ||
957 | uinput = compat_ptr(arg); | |
958 | err = get_user(input.group, &uinput->group); | |
959 | err |= get_user(input.block_bitmap, &uinput->block_bitmap); | |
960 | err |= get_user(input.inode_bitmap, &uinput->inode_bitmap); | |
961 | err |= get_user(input.inode_table, &uinput->inode_table); | |
962 | err |= get_user(input.blocks_count, &uinput->blocks_count); | |
963 | err |= get_user(input.reserved_blocks, | |
964 | &uinput->reserved_blocks); | |
965 | if (err) | |
966 | return -EFAULT; | |
967 | old_fs = get_fs(); | |
968 | set_fs(KERNEL_DS); | |
969 | err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD, | |
970 | (unsigned long) &input); | |
971 | set_fs(old_fs); | |
972 | return err; | |
973 | } | |
b684b2ee | 974 | case EXT4_IOC_MOVE_EXT: |
19c5246d | 975 | case EXT4_IOC_RESIZE_FS: |
7869a4a6 | 976 | case EXT4_IOC_PRECACHE_EXTENTS: |
9bd8212f MH |
977 | case EXT4_IOC_SET_ENCRYPTION_POLICY: |
978 | case EXT4_IOC_GET_ENCRYPTION_PWSALT: | |
979 | case EXT4_IOC_GET_ENCRYPTION_POLICY: | |
b684b2ee | 980 | break; |
ac27a0ec DK |
981 | default: |
982 | return -ENOIOCTLCMD; | |
983 | } | |
5cdd7b2d | 984 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
ac27a0ec DK |
985 | } |
986 | #endif |