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> | |
dab291af | 11 | #include <linux/jbd2.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> |
ac27a0ec | 17 | #include <asm/uaccess.h> |
3dcf5451 CH |
18 | #include "ext4_jbd2.h" |
19 | #include "ext4.h" | |
393d1d1d | 20 | #include "ext4_extents.h" |
ac27a0ec | 21 | |
19c5246d YY |
22 | #define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1) |
23 | ||
393d1d1d DTB |
24 | /** |
25 | * Swap memory between @a and @b for @len bytes. | |
26 | * | |
27 | * @a: pointer to first memory area | |
28 | * @b: pointer to second memory area | |
29 | * @len: number of bytes to swap | |
30 | * | |
31 | */ | |
32 | static void memswap(void *a, void *b, size_t len) | |
33 | { | |
34 | unsigned char *ap, *bp; | |
35 | unsigned char tmp; | |
36 | ||
37 | ap = (unsigned char *)a; | |
38 | bp = (unsigned char *)b; | |
39 | while (len-- > 0) { | |
40 | tmp = *ap; | |
41 | *ap = *bp; | |
42 | *bp = tmp; | |
43 | ap++; | |
44 | bp++; | |
45 | } | |
46 | } | |
47 | ||
48 | /** | |
49 | * Swap i_data and associated attributes between @inode1 and @inode2. | |
50 | * This function is used for the primary swap between inode1 and inode2 | |
51 | * and also to revert this primary swap in case of errors. | |
52 | * | |
53 | * Therefore you have to make sure, that calling this method twice | |
54 | * will revert all changes. | |
55 | * | |
56 | * @inode1: pointer to first inode | |
57 | * @inode2: pointer to second inode | |
58 | */ | |
59 | static void swap_inode_data(struct inode *inode1, struct inode *inode2) | |
60 | { | |
61 | loff_t isize; | |
62 | struct ext4_inode_info *ei1; | |
63 | struct ext4_inode_info *ei2; | |
64 | ||
65 | ei1 = EXT4_I(inode1); | |
66 | ei2 = EXT4_I(inode2); | |
67 | ||
68 | memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags)); | |
69 | memswap(&inode1->i_version, &inode2->i_version, | |
70 | sizeof(inode1->i_version)); | |
71 | memswap(&inode1->i_blocks, &inode2->i_blocks, | |
72 | sizeof(inode1->i_blocks)); | |
73 | memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes)); | |
74 | memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime)); | |
75 | memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime)); | |
76 | ||
77 | memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data)); | |
78 | memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags)); | |
79 | memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize)); | |
cde2d7a7 TT |
80 | ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS); |
81 | ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS); | |
82 | ext4_es_lru_del(inode1); | |
83 | ext4_es_lru_del(inode2); | |
393d1d1d DTB |
84 | |
85 | isize = i_size_read(inode1); | |
86 | i_size_write(inode1, i_size_read(inode2)); | |
87 | i_size_write(inode2, isize); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Swap the information from the given @inode and the inode | |
92 | * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other | |
93 | * important fields of the inodes. | |
94 | * | |
95 | * @sb: the super block of the filesystem | |
96 | * @inode: the inode to swap with EXT4_BOOT_LOADER_INO | |
97 | * | |
98 | */ | |
99 | static long swap_inode_boot_loader(struct super_block *sb, | |
100 | struct inode *inode) | |
101 | { | |
102 | handle_t *handle; | |
103 | int err; | |
104 | struct inode *inode_bl; | |
105 | struct ext4_inode_info *ei; | |
106 | struct ext4_inode_info *ei_bl; | |
107 | struct ext4_sb_info *sbi; | |
108 | ||
109 | if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode)) { | |
110 | err = -EINVAL; | |
111 | goto swap_boot_out; | |
112 | } | |
113 | ||
114 | if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN)) { | |
115 | err = -EPERM; | |
116 | goto swap_boot_out; | |
117 | } | |
118 | ||
119 | sbi = EXT4_SB(sb); | |
120 | ei = EXT4_I(inode); | |
121 | ||
122 | inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO); | |
123 | if (IS_ERR(inode_bl)) { | |
124 | err = PTR_ERR(inode_bl); | |
125 | goto swap_boot_out; | |
126 | } | |
127 | ei_bl = EXT4_I(inode_bl); | |
128 | ||
129 | filemap_flush(inode->i_mapping); | |
130 | filemap_flush(inode_bl->i_mapping); | |
131 | ||
132 | /* Protect orig inodes against a truncate and make sure, | |
133 | * that only 1 swap_inode_boot_loader is running. */ | |
134 | ext4_inode_double_lock(inode, inode_bl); | |
135 | ||
136 | truncate_inode_pages(&inode->i_data, 0); | |
137 | truncate_inode_pages(&inode_bl->i_data, 0); | |
138 | ||
139 | /* Wait for all existing dio workers */ | |
140 | ext4_inode_block_unlocked_dio(inode); | |
141 | ext4_inode_block_unlocked_dio(inode_bl); | |
142 | inode_dio_wait(inode); | |
143 | inode_dio_wait(inode_bl); | |
144 | ||
145 | handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2); | |
146 | if (IS_ERR(handle)) { | |
147 | err = -EINVAL; | |
148 | goto swap_boot_out; | |
149 | } | |
150 | ||
151 | /* Protect extent tree against block allocations via delalloc */ | |
152 | ext4_double_down_write_data_sem(inode, inode_bl); | |
153 | ||
154 | if (inode_bl->i_nlink == 0) { | |
155 | /* this inode has never been used as a BOOT_LOADER */ | |
156 | set_nlink(inode_bl, 1); | |
157 | i_uid_write(inode_bl, 0); | |
158 | i_gid_write(inode_bl, 0); | |
159 | inode_bl->i_flags = 0; | |
160 | ei_bl->i_flags = 0; | |
161 | inode_bl->i_version = 1; | |
162 | i_size_write(inode_bl, 0); | |
163 | inode_bl->i_mode = S_IFREG; | |
164 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, | |
165 | EXT4_FEATURE_INCOMPAT_EXTENTS)) { | |
166 | ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS); | |
167 | ext4_ext_tree_init(handle, inode_bl); | |
168 | } else | |
169 | memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data)); | |
170 | } | |
171 | ||
172 | swap_inode_data(inode, inode_bl); | |
173 | ||
174 | inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode); | |
175 | ||
176 | spin_lock(&sbi->s_next_gen_lock); | |
177 | inode->i_generation = sbi->s_next_generation++; | |
178 | inode_bl->i_generation = sbi->s_next_generation++; | |
179 | spin_unlock(&sbi->s_next_gen_lock); | |
180 | ||
181 | ext4_discard_preallocations(inode); | |
182 | ||
183 | err = ext4_mark_inode_dirty(handle, inode); | |
184 | if (err < 0) { | |
185 | ext4_warning(inode->i_sb, | |
186 | "couldn't mark inode #%lu dirty (err %d)", | |
187 | inode->i_ino, err); | |
188 | /* Revert all changes: */ | |
189 | swap_inode_data(inode, inode_bl); | |
190 | } else { | |
191 | err = ext4_mark_inode_dirty(handle, inode_bl); | |
192 | if (err < 0) { | |
193 | ext4_warning(inode_bl->i_sb, | |
194 | "couldn't mark inode #%lu dirty (err %d)", | |
195 | inode_bl->i_ino, err); | |
196 | /* Revert all changes: */ | |
197 | swap_inode_data(inode, inode_bl); | |
198 | ext4_mark_inode_dirty(handle, inode); | |
199 | } | |
200 | } | |
201 | ||
202 | ext4_journal_stop(handle); | |
203 | ||
204 | ext4_double_up_write_data_sem(inode, inode_bl); | |
205 | ||
206 | ext4_inode_resume_unlocked_dio(inode); | |
207 | ext4_inode_resume_unlocked_dio(inode_bl); | |
208 | ||
209 | ext4_inode_double_unlock(inode, inode_bl); | |
210 | ||
211 | iput(inode_bl); | |
212 | ||
213 | swap_boot_out: | |
214 | return err; | |
215 | } | |
216 | ||
5cdd7b2d | 217 | long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
ac27a0ec | 218 | { |
496ad9aa | 219 | struct inode *inode = file_inode(filp); |
bab08ab9 | 220 | struct super_block *sb = inode->i_sb; |
617ba13b | 221 | struct ext4_inode_info *ei = EXT4_I(inode); |
ac27a0ec | 222 | unsigned int flags; |
ac27a0ec | 223 | |
af5bc92d | 224 | ext4_debug("cmd = %u, arg = %lu\n", cmd, arg); |
ac27a0ec DK |
225 | |
226 | switch (cmd) { | |
617ba13b | 227 | case EXT4_IOC_GETFLAGS: |
ff9ddf7e | 228 | ext4_get_inode_flags(ei); |
617ba13b | 229 | flags = ei->i_flags & EXT4_FL_USER_VISIBLE; |
ac27a0ec | 230 | return put_user(flags, (int __user *) arg); |
617ba13b | 231 | case EXT4_IOC_SETFLAGS: { |
ac27a0ec | 232 | handle_t *handle = NULL; |
4db46fc2 | 233 | int err, migrate = 0; |
617ba13b | 234 | struct ext4_iloc iloc; |
79906964 | 235 | unsigned int oldflags, mask, i; |
ac27a0ec DK |
236 | unsigned int jflag; |
237 | ||
2e149670 | 238 | if (!inode_owner_or_capable(inode)) |
ac27a0ec DK |
239 | return -EACCES; |
240 | ||
241 | if (get_user(flags, (int __user *) arg)) | |
242 | return -EFAULT; | |
243 | ||
a561be71 | 244 | err = mnt_want_write_file(filp); |
42a74f20 DH |
245 | if (err) |
246 | return err; | |
247 | ||
2dc6b0d4 | 248 | flags = ext4_mask_flags(inode->i_mode, flags); |
ac27a0ec | 249 | |
42a74f20 | 250 | err = -EPERM; |
ac27a0ec | 251 | mutex_lock(&inode->i_mutex); |
e47776a0 | 252 | /* Is it quota file? Do not allow user to mess with it */ |
42a74f20 DH |
253 | if (IS_NOQUOTA(inode)) |
254 | goto flags_out; | |
255 | ||
ac27a0ec DK |
256 | oldflags = ei->i_flags; |
257 | ||
258 | /* The JOURNAL_DATA flag is modifiable only by root */ | |
617ba13b | 259 | jflag = flags & EXT4_JOURNAL_DATA_FL; |
ac27a0ec DK |
260 | |
261 | /* | |
262 | * The IMMUTABLE and APPEND_ONLY flags can only be changed by | |
263 | * the relevant capability. | |
264 | * | |
265 | * This test looks nicer. Thanks to Pauline Middelink | |
266 | */ | |
617ba13b | 267 | if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) { |
42a74f20 DH |
268 | if (!capable(CAP_LINUX_IMMUTABLE)) |
269 | goto flags_out; | |
ac27a0ec DK |
270 | } |
271 | ||
272 | /* | |
273 | * The JOURNAL_DATA flag can only be changed by | |
274 | * the relevant capability. | |
275 | */ | |
617ba13b | 276 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) { |
42a74f20 DH |
277 | if (!capable(CAP_SYS_RESOURCE)) |
278 | goto flags_out; | |
ac27a0ec | 279 | } |
996bb9fd | 280 | if ((flags ^ oldflags) & EXT4_EXTENTS_FL) |
4db46fc2 | 281 | migrate = 1; |
ac27a0ec | 282 | |
c8d46e41 JZ |
283 | if (flags & EXT4_EOFBLOCKS_FL) { |
284 | /* we don't support adding EOFBLOCKS flag */ | |
285 | if (!(oldflags & EXT4_EOFBLOCKS_FL)) { | |
286 | err = -EOPNOTSUPP; | |
287 | goto flags_out; | |
288 | } | |
289 | } else if (oldflags & EXT4_EOFBLOCKS_FL) | |
290 | ext4_truncate(inode); | |
291 | ||
9924a92a | 292 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
ac27a0ec | 293 | if (IS_ERR(handle)) { |
42a74f20 DH |
294 | err = PTR_ERR(handle); |
295 | goto flags_out; | |
ac27a0ec DK |
296 | } |
297 | if (IS_SYNC(inode)) | |
0390131b | 298 | ext4_handle_sync(handle); |
617ba13b | 299 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec DK |
300 | if (err) |
301 | goto flags_err; | |
302 | ||
79906964 TT |
303 | for (i = 0, mask = 1; i < 32; i++, mask <<= 1) { |
304 | if (!(mask & EXT4_FL_USER_MODIFIABLE)) | |
305 | continue; | |
306 | if (mask & flags) | |
307 | ext4_set_inode_flag(inode, i); | |
308 | else | |
309 | ext4_clear_inode_flag(inode, i); | |
310 | } | |
ac27a0ec | 311 | |
617ba13b | 312 | ext4_set_inode_flags(inode); |
ef7f3835 | 313 | inode->i_ctime = ext4_current_time(inode); |
ac27a0ec | 314 | |
617ba13b | 315 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 316 | flags_err: |
617ba13b | 317 | ext4_journal_stop(handle); |
42a74f20 DH |
318 | if (err) |
319 | goto flags_out; | |
ac27a0ec | 320 | |
617ba13b MC |
321 | if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) |
322 | err = ext4_change_inode_journal_flag(inode, jflag); | |
4db46fc2 AK |
323 | if (err) |
324 | goto flags_out; | |
996bb9fd TT |
325 | if (migrate) { |
326 | if (flags & EXT4_EXTENTS_FL) | |
327 | err = ext4_ext_migrate(inode); | |
328 | else | |
329 | err = ext4_ind_migrate(inode); | |
330 | } | |
331 | ||
42a74f20 | 332 | flags_out: |
ac27a0ec | 333 | mutex_unlock(&inode->i_mutex); |
2a79f17e | 334 | mnt_drop_write_file(filp); |
ac27a0ec DK |
335 | return err; |
336 | } | |
617ba13b MC |
337 | case EXT4_IOC_GETVERSION: |
338 | case EXT4_IOC_GETVERSION_OLD: | |
ac27a0ec | 339 | return put_user(inode->i_generation, (int __user *) arg); |
617ba13b MC |
340 | case EXT4_IOC_SETVERSION: |
341 | case EXT4_IOC_SETVERSION_OLD: { | |
ac27a0ec | 342 | handle_t *handle; |
617ba13b | 343 | struct ext4_iloc iloc; |
ac27a0ec DK |
344 | __u32 generation; |
345 | int err; | |
346 | ||
2e149670 | 347 | if (!inode_owner_or_capable(inode)) |
ac27a0ec | 348 | return -EPERM; |
42a74f20 | 349 | |
814525f4 DW |
350 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
351 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { | |
352 | ext4_warning(sb, "Setting inode version is not " | |
353 | "supported with metadata_csum enabled."); | |
354 | return -ENOTTY; | |
355 | } | |
356 | ||
a561be71 | 357 | err = mnt_want_write_file(filp); |
42a74f20 DH |
358 | if (err) |
359 | return err; | |
360 | if (get_user(generation, (int __user *) arg)) { | |
361 | err = -EFAULT; | |
362 | goto setversion_out; | |
363 | } | |
ac27a0ec | 364 | |
6c2155b9 | 365 | mutex_lock(&inode->i_mutex); |
9924a92a | 366 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
42a74f20 DH |
367 | if (IS_ERR(handle)) { |
368 | err = PTR_ERR(handle); | |
6c2155b9 | 369 | goto unlock_out; |
42a74f20 | 370 | } |
617ba13b | 371 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
ac27a0ec | 372 | if (err == 0) { |
ef7f3835 | 373 | inode->i_ctime = ext4_current_time(inode); |
ac27a0ec | 374 | inode->i_generation = generation; |
617ba13b | 375 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec | 376 | } |
617ba13b | 377 | ext4_journal_stop(handle); |
6c2155b9 DH |
378 | |
379 | unlock_out: | |
380 | mutex_unlock(&inode->i_mutex); | |
42a74f20 | 381 | setversion_out: |
2a79f17e | 382 | mnt_drop_write_file(filp); |
ac27a0ec DK |
383 | return err; |
384 | } | |
617ba13b MC |
385 | case EXT4_IOC_GROUP_EXTEND: { |
386 | ext4_fsblk_t n_blocks_count; | |
ac046f1d | 387 | int err, err2=0; |
ac27a0ec | 388 | |
8f82f840 YY |
389 | err = ext4_resize_begin(sb); |
390 | if (err) | |
391 | return err; | |
ac27a0ec | 392 | |
014a1770 DH |
393 | if (get_user(n_blocks_count, (__u32 __user *)arg)) { |
394 | err = -EFAULT; | |
395 | goto group_extend_out; | |
396 | } | |
ac27a0ec | 397 | |
bab08ab9 TT |
398 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
399 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { | |
400 | ext4_msg(sb, KERN_ERR, | |
401 | "Online resizing not supported with bigalloc"); | |
014a1770 DH |
402 | err = -EOPNOTSUPP; |
403 | goto group_extend_out; | |
bab08ab9 TT |
404 | } |
405 | ||
a561be71 | 406 | err = mnt_want_write_file(filp); |
42a74f20 | 407 | if (err) |
014a1770 | 408 | goto group_extend_out; |
42a74f20 | 409 | |
617ba13b | 410 | err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count); |
ac046f1d PT |
411 | if (EXT4_SB(sb)->s_journal) { |
412 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
413 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
414 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
415 | } | |
7ffe1ea8 HK |
416 | if (err == 0) |
417 | err = err2; | |
2a79f17e | 418 | mnt_drop_write_file(filp); |
014a1770 | 419 | group_extend_out: |
8f82f840 | 420 | ext4_resize_end(sb); |
ac27a0ec DK |
421 | return err; |
422 | } | |
748de673 AF |
423 | |
424 | case EXT4_IOC_MOVE_EXT: { | |
425 | struct move_extent me; | |
2903ff01 AV |
426 | struct fd donor; |
427 | int err; | |
748de673 | 428 | |
4a58579b AF |
429 | if (!(filp->f_mode & FMODE_READ) || |
430 | !(filp->f_mode & FMODE_WRITE)) | |
431 | return -EBADF; | |
432 | ||
748de673 AF |
433 | if (copy_from_user(&me, |
434 | (struct move_extent __user *)arg, sizeof(me))) | |
435 | return -EFAULT; | |
4a58579b | 436 | me.moved_len = 0; |
748de673 | 437 | |
2903ff01 AV |
438 | donor = fdget(me.donor_fd); |
439 | if (!donor.file) | |
748de673 AF |
440 | return -EBADF; |
441 | ||
2903ff01 | 442 | if (!(donor.file->f_mode & FMODE_WRITE)) { |
4a58579b AF |
443 | err = -EBADF; |
444 | goto mext_out; | |
748de673 AF |
445 | } |
446 | ||
bab08ab9 TT |
447 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
448 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { | |
449 | ext4_msg(sb, KERN_ERR, | |
450 | "Online defrag not supported with bigalloc"); | |
399c9b86 AV |
451 | err = -EOPNOTSUPP; |
452 | goto mext_out; | |
bab08ab9 TT |
453 | } |
454 | ||
a561be71 | 455 | err = mnt_want_write_file(filp); |
4a58579b AF |
456 | if (err) |
457 | goto mext_out; | |
458 | ||
2903ff01 | 459 | err = ext4_move_extents(filp, donor.file, me.orig_start, |
748de673 | 460 | me.donor_start, me.len, &me.moved_len); |
2a79f17e | 461 | mnt_drop_write_file(filp); |
748de673 | 462 | |
60e6679e | 463 | if (copy_to_user((struct move_extent __user *)arg, |
c437b273 | 464 | &me, sizeof(me))) |
4a58579b AF |
465 | err = -EFAULT; |
466 | mext_out: | |
2903ff01 | 467 | fdput(donor); |
748de673 AF |
468 | return err; |
469 | } | |
470 | ||
617ba13b MC |
471 | case EXT4_IOC_GROUP_ADD: { |
472 | struct ext4_new_group_data input; | |
ac046f1d | 473 | int err, err2=0; |
ac27a0ec | 474 | |
8f82f840 YY |
475 | err = ext4_resize_begin(sb); |
476 | if (err) | |
477 | return err; | |
ac27a0ec | 478 | |
617ba13b | 479 | if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg, |
014a1770 DH |
480 | sizeof(input))) { |
481 | err = -EFAULT; | |
482 | goto group_add_out; | |
483 | } | |
ac27a0ec | 484 | |
bab08ab9 TT |
485 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, |
486 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { | |
487 | ext4_msg(sb, KERN_ERR, | |
488 | "Online resizing not supported with bigalloc"); | |
014a1770 DH |
489 | err = -EOPNOTSUPP; |
490 | goto group_add_out; | |
bab08ab9 TT |
491 | } |
492 | ||
a561be71 | 493 | err = mnt_want_write_file(filp); |
42a74f20 | 494 | if (err) |
014a1770 | 495 | goto group_add_out; |
42a74f20 | 496 | |
617ba13b | 497 | err = ext4_group_add(sb, &input); |
ac046f1d PT |
498 | if (EXT4_SB(sb)->s_journal) { |
499 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
500 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
501 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
502 | } | |
7ffe1ea8 HK |
503 | if (err == 0) |
504 | err = err2; | |
2a79f17e | 505 | mnt_drop_write_file(filp); |
7f511862 TT |
506 | if (!err && ext4_has_group_desc_csum(sb) && |
507 | test_opt(sb, INIT_INODE_TABLE)) | |
508 | err = ext4_register_li_request(sb, input.group); | |
014a1770 | 509 | group_add_out: |
8f82f840 | 510 | ext4_resize_end(sb); |
ac27a0ec DK |
511 | return err; |
512 | } | |
513 | ||
c14c6fd5 | 514 | case EXT4_IOC_MIGRATE: |
2a43a878 AK |
515 | { |
516 | int err; | |
2e149670 | 517 | if (!inode_owner_or_capable(inode)) |
2a43a878 AK |
518 | return -EACCES; |
519 | ||
a561be71 | 520 | err = mnt_want_write_file(filp); |
2a43a878 AK |
521 | if (err) |
522 | return err; | |
523 | /* | |
524 | * inode_mutex prevent write and truncate on the file. | |
525 | * Read still goes through. We take i_data_sem in | |
526 | * ext4_ext_swap_inode_data before we switch the | |
527 | * inode format to prevent read. | |
528 | */ | |
529 | mutex_lock(&(inode->i_mutex)); | |
530 | err = ext4_ext_migrate(inode); | |
531 | mutex_unlock(&(inode->i_mutex)); | |
2a79f17e | 532 | mnt_drop_write_file(filp); |
2a43a878 AK |
533 | return err; |
534 | } | |
c14c6fd5 | 535 | |
ccd2506b TT |
536 | case EXT4_IOC_ALLOC_DA_BLKS: |
537 | { | |
538 | int err; | |
2e149670 | 539 | if (!inode_owner_or_capable(inode)) |
ccd2506b TT |
540 | return -EACCES; |
541 | ||
a561be71 | 542 | err = mnt_want_write_file(filp); |
ccd2506b TT |
543 | if (err) |
544 | return err; | |
545 | err = ext4_alloc_da_blocks(inode); | |
2a79f17e | 546 | mnt_drop_write_file(filp); |
ccd2506b TT |
547 | return err; |
548 | } | |
549 | ||
393d1d1d DTB |
550 | case EXT4_IOC_SWAP_BOOT: |
551 | if (!(filp->f_mode & FMODE_WRITE)) | |
552 | return -EBADF; | |
553 | return swap_inode_boot_loader(sb, inode); | |
554 | ||
19c5246d YY |
555 | case EXT4_IOC_RESIZE_FS: { |
556 | ext4_fsblk_t n_blocks_count; | |
19c5246d | 557 | int err = 0, err2 = 0; |
7f511862 | 558 | ext4_group_t o_group = EXT4_SB(sb)->s_groups_count; |
19c5246d YY |
559 | |
560 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | |
561 | EXT4_FEATURE_RO_COMPAT_BIGALLOC)) { | |
562 | ext4_msg(sb, KERN_ERR, | |
563 | "Online resizing not (yet) supported with bigalloc"); | |
564 | return -EOPNOTSUPP; | |
565 | } | |
566 | ||
19c5246d YY |
567 | if (copy_from_user(&n_blocks_count, (__u64 __user *)arg, |
568 | sizeof(__u64))) { | |
569 | return -EFAULT; | |
570 | } | |
571 | ||
19c5246d YY |
572 | err = ext4_resize_begin(sb); |
573 | if (err) | |
574 | return err; | |
575 | ||
8cae6f71 | 576 | err = mnt_want_write_file(filp); |
19c5246d YY |
577 | if (err) |
578 | goto resizefs_out; | |
579 | ||
580 | err = ext4_resize_fs(sb, n_blocks_count); | |
581 | if (EXT4_SB(sb)->s_journal) { | |
582 | jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal); | |
583 | err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal); | |
584 | jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal); | |
585 | } | |
586 | if (err == 0) | |
587 | err = err2; | |
8cae6f71 | 588 | mnt_drop_write_file(filp); |
7f511862 TT |
589 | if (!err && (o_group > EXT4_SB(sb)->s_groups_count) && |
590 | ext4_has_group_desc_csum(sb) && | |
591 | test_opt(sb, INIT_INODE_TABLE)) | |
592 | err = ext4_register_li_request(sb, o_group); | |
593 | ||
19c5246d YY |
594 | resizefs_out: |
595 | ext4_resize_end(sb); | |
596 | return err; | |
597 | } | |
598 | ||
e681c047 LC |
599 | case FITRIM: |
600 | { | |
41431792 | 601 | struct request_queue *q = bdev_get_queue(sb->s_bdev); |
e681c047 LC |
602 | struct fstrim_range range; |
603 | int ret = 0; | |
604 | ||
605 | if (!capable(CAP_SYS_ADMIN)) | |
606 | return -EPERM; | |
607 | ||
41431792 LC |
608 | if (!blk_queue_discard(q)) |
609 | return -EOPNOTSUPP; | |
610 | ||
e6705f7c | 611 | if (copy_from_user(&range, (struct fstrim_range __user *)arg, |
e681c047 LC |
612 | sizeof(range))) |
613 | return -EFAULT; | |
614 | ||
5c2ed62f LC |
615 | range.minlen = max((unsigned int)range.minlen, |
616 | q->limits.discard_granularity); | |
e681c047 LC |
617 | ret = ext4_trim_fs(sb, &range); |
618 | if (ret < 0) | |
619 | return ret; | |
620 | ||
e6705f7c | 621 | if (copy_to_user((struct fstrim_range __user *)arg, &range, |
e681c047 LC |
622 | sizeof(range))) |
623 | return -EFAULT; | |
624 | ||
625 | return 0; | |
626 | } | |
627 | ||
ac27a0ec DK |
628 | default: |
629 | return -ENOTTY; | |
630 | } | |
631 | } | |
632 | ||
633 | #ifdef CONFIG_COMPAT | |
617ba13b | 634 | long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
ac27a0ec | 635 | { |
ac27a0ec DK |
636 | /* These are just misnamed, they actually get/put from/to user an int */ |
637 | switch (cmd) { | |
617ba13b MC |
638 | case EXT4_IOC32_GETFLAGS: |
639 | cmd = EXT4_IOC_GETFLAGS; | |
ac27a0ec | 640 | break; |
617ba13b MC |
641 | case EXT4_IOC32_SETFLAGS: |
642 | cmd = EXT4_IOC_SETFLAGS; | |
ac27a0ec | 643 | break; |
617ba13b MC |
644 | case EXT4_IOC32_GETVERSION: |
645 | cmd = EXT4_IOC_GETVERSION; | |
ac27a0ec | 646 | break; |
617ba13b MC |
647 | case EXT4_IOC32_SETVERSION: |
648 | cmd = EXT4_IOC_SETVERSION; | |
ac27a0ec | 649 | break; |
617ba13b MC |
650 | case EXT4_IOC32_GROUP_EXTEND: |
651 | cmd = EXT4_IOC_GROUP_EXTEND; | |
ac27a0ec | 652 | break; |
617ba13b MC |
653 | case EXT4_IOC32_GETVERSION_OLD: |
654 | cmd = EXT4_IOC_GETVERSION_OLD; | |
ac27a0ec | 655 | break; |
617ba13b MC |
656 | case EXT4_IOC32_SETVERSION_OLD: |
657 | cmd = EXT4_IOC_SETVERSION_OLD; | |
ac27a0ec | 658 | break; |
617ba13b MC |
659 | case EXT4_IOC32_GETRSVSZ: |
660 | cmd = EXT4_IOC_GETRSVSZ; | |
ac27a0ec | 661 | break; |
617ba13b MC |
662 | case EXT4_IOC32_SETRSVSZ: |
663 | cmd = EXT4_IOC_SETRSVSZ; | |
ac27a0ec | 664 | break; |
4d92dc0f BH |
665 | case EXT4_IOC32_GROUP_ADD: { |
666 | struct compat_ext4_new_group_input __user *uinput; | |
667 | struct ext4_new_group_input input; | |
668 | mm_segment_t old_fs; | |
669 | int err; | |
670 | ||
671 | uinput = compat_ptr(arg); | |
672 | err = get_user(input.group, &uinput->group); | |
673 | err |= get_user(input.block_bitmap, &uinput->block_bitmap); | |
674 | err |= get_user(input.inode_bitmap, &uinput->inode_bitmap); | |
675 | err |= get_user(input.inode_table, &uinput->inode_table); | |
676 | err |= get_user(input.blocks_count, &uinput->blocks_count); | |
677 | err |= get_user(input.reserved_blocks, | |
678 | &uinput->reserved_blocks); | |
679 | if (err) | |
680 | return -EFAULT; | |
681 | old_fs = get_fs(); | |
682 | set_fs(KERNEL_DS); | |
683 | err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD, | |
684 | (unsigned long) &input); | |
685 | set_fs(old_fs); | |
686 | return err; | |
687 | } | |
b684b2ee | 688 | case EXT4_IOC_MOVE_EXT: |
a56e69c2 | 689 | case FITRIM: |
19c5246d | 690 | case EXT4_IOC_RESIZE_FS: |
b684b2ee | 691 | break; |
ac27a0ec DK |
692 | default: |
693 | return -ENOIOCTLCMD; | |
694 | } | |
5cdd7b2d | 695 | return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
ac27a0ec DK |
696 | } |
697 | #endif |