blob: 7ce85824211b924ff6f2eebd2786c5b569371bea [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/ioctl.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
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>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070011#include <linux/capability.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070012#include <linux/time.h>
13#include <linux/compat.h>
Dave Hansen42a74f22008-02-15 14:37:46 -080014#include <linux/mount.h>
Akira Fujita748de672009-06-17 19:24:03 -040015#include <linux/file.h>
Michael Halcrow9bd82122015-04-11 07:48:01 -040016#include <linux/random.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070017#include <asm/uaccess.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040018#include "ext4_jbd2.h"
19#include "ext4.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070020
Yongqiang Yang19c52462012-01-04 17:09:44 -050021#define MAX_32_NUM ((((unsigned long long) 1) << 32) - 1)
22
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040023/**
24 * Swap memory between @a and @b for @len bytes.
25 *
26 * @a: pointer to first memory area
27 * @b: pointer to second memory area
28 * @len: number of bytes to swap
29 *
30 */
31static void memswap(void *a, void *b, size_t len)
32{
33 unsigned char *ap, *bp;
34 unsigned char tmp;
35
36 ap = (unsigned char *)a;
37 bp = (unsigned char *)b;
38 while (len-- > 0) {
39 tmp = *ap;
40 *ap = *bp;
41 *bp = tmp;
42 ap++;
43 bp++;
44 }
45}
46
47/**
48 * Swap i_data and associated attributes between @inode1 and @inode2.
49 * This function is used for the primary swap between inode1 and inode2
50 * and also to revert this primary swap in case of errors.
51 *
52 * Therefore you have to make sure, that calling this method twice
53 * will revert all changes.
54 *
55 * @inode1: pointer to first inode
56 * @inode2: pointer to second inode
57 */
58static void swap_inode_data(struct inode *inode1, struct inode *inode2)
59{
60 loff_t isize;
61 struct ext4_inode_info *ei1;
62 struct ext4_inode_info *ei2;
63
64 ei1 = EXT4_I(inode1);
65 ei2 = EXT4_I(inode2);
66
67 memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
68 memswap(&inode1->i_version, &inode2->i_version,
69 sizeof(inode1->i_version));
70 memswap(&inode1->i_blocks, &inode2->i_blocks,
71 sizeof(inode1->i_blocks));
72 memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
73 memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
74 memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
75
76 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
77 memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
78 memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
Theodore Ts'ocde2d7a2013-08-12 09:29:30 -040079 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
80 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040081
82 isize = i_size_read(inode1);
83 i_size_write(inode1, i_size_read(inode2));
84 i_size_write(inode2, isize);
85}
86
87/**
88 * Swap the information from the given @inode and the inode
89 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
90 * important fields of the inodes.
91 *
92 * @sb: the super block of the filesystem
93 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
94 *
95 */
96static long swap_inode_boot_loader(struct super_block *sb,
97 struct inode *inode)
98{
99 handle_t *handle;
100 int err;
101 struct inode *inode_bl;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400102 struct ext4_inode_info *ei_bl;
jon ernstd7092ae2014-01-11 13:26:56 -0500103 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400104
Theodore Ts'od8558a22014-02-17 20:44:36 -0500105 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
106 return -EINVAL;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400107
Theodore Ts'od8558a22014-02-17 20:44:36 -0500108 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
109 return -EPERM;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400110
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400111 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
Theodore Ts'od8558a22014-02-17 20:44:36 -0500112 if (IS_ERR(inode_bl))
113 return PTR_ERR(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400114 ei_bl = EXT4_I(inode_bl);
115
116 filemap_flush(inode->i_mapping);
117 filemap_flush(inode_bl->i_mapping);
118
119 /* Protect orig inodes against a truncate and make sure,
120 * that only 1 swap_inode_boot_loader is running. */
J. Bruce Fields375e2892012-04-18 15:16:33 -0400121 lock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400122
123 truncate_inode_pages(&inode->i_data, 0);
124 truncate_inode_pages(&inode_bl->i_data, 0);
125
126 /* Wait for all existing dio workers */
127 ext4_inode_block_unlocked_dio(inode);
128 ext4_inode_block_unlocked_dio(inode_bl);
129 inode_dio_wait(inode);
130 inode_dio_wait(inode_bl);
131
132 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
133 if (IS_ERR(handle)) {
134 err = -EINVAL;
Zheng Liu30d29b12014-02-12 11:48:31 -0500135 goto journal_err_out;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400136 }
137
138 /* Protect extent tree against block allocations via delalloc */
139 ext4_double_down_write_data_sem(inode, inode_bl);
140
141 if (inode_bl->i_nlink == 0) {
142 /* this inode has never been used as a BOOT_LOADER */
143 set_nlink(inode_bl, 1);
144 i_uid_write(inode_bl, 0);
145 i_gid_write(inode_bl, 0);
146 inode_bl->i_flags = 0;
147 ei_bl->i_flags = 0;
148 inode_bl->i_version = 1;
149 i_size_write(inode_bl, 0);
150 inode_bl->i_mode = S_IFREG;
151 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
152 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
153 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
154 ext4_ext_tree_init(handle, inode_bl);
155 } else
156 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
157 }
158
159 swap_inode_data(inode, inode_bl);
160
161 inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
162
163 spin_lock(&sbi->s_next_gen_lock);
164 inode->i_generation = sbi->s_next_generation++;
165 inode_bl->i_generation = sbi->s_next_generation++;
166 spin_unlock(&sbi->s_next_gen_lock);
167
168 ext4_discard_preallocations(inode);
169
170 err = ext4_mark_inode_dirty(handle, inode);
171 if (err < 0) {
172 ext4_warning(inode->i_sb,
173 "couldn't mark inode #%lu dirty (err %d)",
174 inode->i_ino, err);
175 /* Revert all changes: */
176 swap_inode_data(inode, inode_bl);
177 } else {
178 err = ext4_mark_inode_dirty(handle, inode_bl);
179 if (err < 0) {
180 ext4_warning(inode_bl->i_sb,
181 "couldn't mark inode #%lu dirty (err %d)",
182 inode_bl->i_ino, err);
183 /* Revert all changes: */
184 swap_inode_data(inode, inode_bl);
185 ext4_mark_inode_dirty(handle, inode);
186 }
187 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400188 ext4_journal_stop(handle);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400189 ext4_double_up_write_data_sem(inode, inode_bl);
190
Zheng Liu30d29b12014-02-12 11:48:31 -0500191journal_err_out:
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400192 ext4_inode_resume_unlocked_dio(inode);
193 ext4_inode_resume_unlocked_dio(inode_bl);
J. Bruce Fields375e2892012-04-18 15:16:33 -0400194 unlock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400195 iput(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400196 return err;
197}
198
Michael Halcrow9bd82122015-04-11 07:48:01 -0400199static int uuid_is_zero(__u8 u[16])
200{
201 int i;
202
203 for (i = 0; i < 16; i++)
204 if (u[i])
205 return 0;
206 return 1;
207}
208
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400209long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210{
Al Viro496ad9a2013-01-23 17:07:38 -0500211 struct inode *inode = file_inode(filp);
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400212 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700213 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700214 unsigned int flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400216 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217
218 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700219 case EXT4_IOC_GETFLAGS:
Jan Karaff9ddf72007-07-18 09:24:20 -0400220 ext4_get_inode_flags(ei);
Mingming Cao617ba132006-10-11 01:20:53 -0700221 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222 return put_user(flags, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700223 case EXT4_IOC_SETFLAGS: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224 handle_t *handle = NULL;
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400225 int err, migrate = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700226 struct ext4_iloc iloc;
Theodore Ts'o79906962012-05-31 23:46:01 -0400227 unsigned int oldflags, mask, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228 unsigned int jflag;
229
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700230 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 return -EACCES;
232
233 if (get_user(flags, (int __user *) arg))
234 return -EFAULT;
235
Al Viroa561be72011-11-23 11:57:51 -0500236 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800237 if (err)
238 return err;
239
Duane Griffin2dc6b0d2009-02-15 18:09:20 -0500240 flags = ext4_mask_flags(inode->i_mode, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241
Dave Hansen42a74f22008-02-15 14:37:46 -0800242 err = -EPERM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700243 mutex_lock(&inode->i_mutex);
Jan Karae47776a2007-11-14 16:58:56 -0800244 /* Is it quota file? Do not allow user to mess with it */
Dave Hansen42a74f22008-02-15 14:37:46 -0800245 if (IS_NOQUOTA(inode))
246 goto flags_out;
247
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248 oldflags = ei->i_flags;
249
250 /* The JOURNAL_DATA flag is modifiable only by root */
Mingming Cao617ba132006-10-11 01:20:53 -0700251 jflag = flags & EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252
253 /*
254 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
255 * the relevant capability.
256 *
257 * This test looks nicer. Thanks to Pauline Middelink
258 */
Mingming Cao617ba132006-10-11 01:20:53 -0700259 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800260 if (!capable(CAP_LINUX_IMMUTABLE))
261 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 }
263
264 /*
265 * The JOURNAL_DATA flag can only be changed by
266 * the relevant capability.
267 */
Mingming Cao617ba132006-10-11 01:20:53 -0700268 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800269 if (!capable(CAP_SYS_RESOURCE))
270 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700271 }
Theodore Ts'o996bb9f2013-04-03 22:04:52 -0400272 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400273 migrate = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700274
Jiaying Zhangc8d46e42010-02-24 09:52:53 -0500275 if (flags & EXT4_EOFBLOCKS_FL) {
276 /* we don't support adding EOFBLOCKS flag */
277 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
278 err = -EOPNOTSUPP;
279 goto flags_out;
280 }
281 } else if (oldflags & EXT4_EOFBLOCKS_FL)
282 ext4_truncate(inode);
283
Theodore Ts'o9924a922013-02-08 21:59:22 -0500284 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 if (IS_ERR(handle)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800286 err = PTR_ERR(handle);
287 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 }
289 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500290 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700291 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 if (err)
293 goto flags_err;
294
Theodore Ts'o79906962012-05-31 23:46:01 -0400295 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
296 if (!(mask & EXT4_FL_USER_MODIFIABLE))
297 continue;
298 if (mask & flags)
299 ext4_set_inode_flag(inode, i);
300 else
301 ext4_clear_inode_flag(inode, i);
302 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303
Mingming Cao617ba132006-10-11 01:20:53 -0700304 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400305 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306
Mingming Cao617ba132006-10-11 01:20:53 -0700307 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700309 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800310 if (err)
311 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312
Mingming Cao617ba132006-10-11 01:20:53 -0700313 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
314 err = ext4_change_inode_journal_flag(inode, jflag);
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400315 if (err)
316 goto flags_out;
Theodore Ts'o996bb9f2013-04-03 22:04:52 -0400317 if (migrate) {
318 if (flags & EXT4_EXTENTS_FL)
319 err = ext4_ext_migrate(inode);
320 else
321 err = ext4_ind_migrate(inode);
322 }
323
Dave Hansen42a74f22008-02-15 14:37:46 -0800324flags_out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700325 mutex_unlock(&inode->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -0500326 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700327 return err;
328 }
Mingming Cao617ba132006-10-11 01:20:53 -0700329 case EXT4_IOC_GETVERSION:
330 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700332 case EXT4_IOC_SETVERSION:
333 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700334 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700335 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 __u32 generation;
337 int err;
338
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700339 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800341
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400342 if (ext4_has_metadata_csum(inode->i_sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -0400343 ext4_warning(sb, "Setting inode version is not "
344 "supported with metadata_csum enabled.");
345 return -ENOTTY;
346 }
347
Al Viroa561be72011-11-23 11:57:51 -0500348 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800349 if (err)
350 return err;
351 if (get_user(generation, (int __user *) arg)) {
352 err = -EFAULT;
353 goto setversion_out;
354 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100356 mutex_lock(&inode->i_mutex);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500357 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800358 if (IS_ERR(handle)) {
359 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100360 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800361 }
Mingming Cao617ba132006-10-11 01:20:53 -0700362 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400364 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700366 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700367 }
Mingming Cao617ba132006-10-11 01:20:53 -0700368 ext4_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100369
370unlock_out:
371 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800372setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500373 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700374 return err;
375 }
Mingming Cao617ba132006-10-11 01:20:53 -0700376 case EXT4_IOC_GROUP_EXTEND: {
377 ext4_fsblk_t n_blocks_count;
Peng Taoac046f12009-07-13 09:30:17 -0400378 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700379
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400380 err = ext4_resize_begin(sb);
381 if (err)
382 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700383
Djalal Harouni014a1772012-01-04 17:09:52 -0500384 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
385 err = -EFAULT;
386 goto group_extend_out;
387 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700388
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400389 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
390 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
391 ext4_msg(sb, KERN_ERR,
392 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500393 err = -EOPNOTSUPP;
394 goto group_extend_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400395 }
396
Al Viroa561be72011-11-23 11:57:51 -0500397 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800398 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500399 goto group_extend_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800400
Mingming Cao617ba132006-10-11 01:20:53 -0700401 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Peng Taoac046f12009-07-13 09:30:17 -0400402 if (EXT4_SB(sb)->s_journal) {
403 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
404 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
405 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
406 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400407 if (err == 0)
408 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500409 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500410group_extend_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400411 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700412 return err;
413 }
Akira Fujita748de672009-06-17 19:24:03 -0400414
415 case EXT4_IOC_MOVE_EXT: {
416 struct move_extent me;
Al Viro2903ff02012-08-28 12:52:22 -0400417 struct fd donor;
418 int err;
Akira Fujita748de672009-06-17 19:24:03 -0400419
Akira Fujita4a585792009-12-06 23:38:31 -0500420 if (!(filp->f_mode & FMODE_READ) ||
421 !(filp->f_mode & FMODE_WRITE))
422 return -EBADF;
423
Akira Fujita748de672009-06-17 19:24:03 -0400424 if (copy_from_user(&me,
425 (struct move_extent __user *)arg, sizeof(me)))
426 return -EFAULT;
Akira Fujita4a585792009-12-06 23:38:31 -0500427 me.moved_len = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400428
Al Viro2903ff02012-08-28 12:52:22 -0400429 donor = fdget(me.donor_fd);
430 if (!donor.file)
Akira Fujita748de672009-06-17 19:24:03 -0400431 return -EBADF;
432
Al Viro2903ff02012-08-28 12:52:22 -0400433 if (!(donor.file->f_mode & FMODE_WRITE)) {
Akira Fujita4a585792009-12-06 23:38:31 -0500434 err = -EBADF;
435 goto mext_out;
Akira Fujita748de672009-06-17 19:24:03 -0400436 }
437
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400438 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
439 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
440 ext4_msg(sb, KERN_ERR,
441 "Online defrag not supported with bigalloc");
Al Viro399c9b82012-08-26 21:00:03 -0400442 err = -EOPNOTSUPP;
443 goto mext_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400444 }
445
Al Viroa561be72011-11-23 11:57:51 -0500446 err = mnt_want_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500447 if (err)
448 goto mext_out;
449
Al Viro2903ff02012-08-28 12:52:22 -0400450 err = ext4_move_extents(filp, donor.file, me.orig_start,
Akira Fujita748de672009-06-17 19:24:03 -0400451 me.donor_start, me.len, &me.moved_len);
Al Viro2a79f172011-12-09 08:06:57 -0500452 mnt_drop_write_file(filp);
Akira Fujita748de672009-06-17 19:24:03 -0400453
Theodore Ts'o60e66792010-05-17 07:00:00 -0400454 if (copy_to_user((struct move_extent __user *)arg,
Akira Fujitac437b272010-03-04 00:39:24 -0500455 &me, sizeof(me)))
Akira Fujita4a585792009-12-06 23:38:31 -0500456 err = -EFAULT;
457mext_out:
Al Viro2903ff02012-08-28 12:52:22 -0400458 fdput(donor);
Akira Fujita748de672009-06-17 19:24:03 -0400459 return err;
460 }
461
Mingming Cao617ba132006-10-11 01:20:53 -0700462 case EXT4_IOC_GROUP_ADD: {
463 struct ext4_new_group_data input;
Peng Taoac046f12009-07-13 09:30:17 -0400464 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700465
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400466 err = ext4_resize_begin(sb);
467 if (err)
468 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469
Mingming Cao617ba132006-10-11 01:20:53 -0700470 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Djalal Harouni014a1772012-01-04 17:09:52 -0500471 sizeof(input))) {
472 err = -EFAULT;
473 goto group_add_out;
474 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400476 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
477 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
478 ext4_msg(sb, KERN_ERR,
479 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500480 err = -EOPNOTSUPP;
481 goto group_add_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400482 }
483
Al Viroa561be72011-11-23 11:57:51 -0500484 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800485 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500486 goto group_add_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800487
Mingming Cao617ba132006-10-11 01:20:53 -0700488 err = ext4_group_add(sb, &input);
Peng Taoac046f12009-07-13 09:30:17 -0400489 if (EXT4_SB(sb)->s_journal) {
490 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
491 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
492 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
493 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400494 if (err == 0)
495 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500496 mnt_drop_write_file(filp);
Theodore Ts'o7f511862013-01-13 08:41:45 -0500497 if (!err && ext4_has_group_desc_csum(sb) &&
498 test_opt(sb, INIT_INODE_TABLE))
499 err = ext4_register_li_request(sb, input.group);
Djalal Harouni014a1772012-01-04 17:09:52 -0500500group_add_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400501 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502 return err;
503 }
504
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500505 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400506 {
507 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700508 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400509 return -EACCES;
510
Al Viroa561be72011-11-23 11:57:51 -0500511 err = mnt_want_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400512 if (err)
513 return err;
514 /*
515 * inode_mutex prevent write and truncate on the file.
516 * Read still goes through. We take i_data_sem in
517 * ext4_ext_swap_inode_data before we switch the
518 * inode format to prevent read.
519 */
520 mutex_lock(&(inode->i_mutex));
521 err = ext4_ext_migrate(inode);
522 mutex_unlock(&(inode->i_mutex));
Al Viro2a79f172011-12-09 08:06:57 -0500523 mnt_drop_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400524 return err;
525 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500526
Theodore Ts'occd25062009-02-26 01:04:07 -0500527 case EXT4_IOC_ALLOC_DA_BLKS:
528 {
529 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700530 if (!inode_owner_or_capable(inode))
Theodore Ts'occd25062009-02-26 01:04:07 -0500531 return -EACCES;
532
Al Viroa561be72011-11-23 11:57:51 -0500533 err = mnt_want_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500534 if (err)
535 return err;
536 err = ext4_alloc_da_blocks(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500537 mnt_drop_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500538 return err;
539 }
540
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400541 case EXT4_IOC_SWAP_BOOT:
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400542 {
543 int err;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400544 if (!(filp->f_mode & FMODE_WRITE))
545 return -EBADF;
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400546 err = mnt_want_write_file(filp);
547 if (err)
548 return err;
549 err = swap_inode_boot_loader(sb, inode);
550 mnt_drop_write_file(filp);
551 return err;
552 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400553
Yongqiang Yang19c52462012-01-04 17:09:44 -0500554 case EXT4_IOC_RESIZE_FS: {
555 ext4_fsblk_t n_blocks_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500556 int err = 0, err2 = 0;
Theodore Ts'o7f511862013-01-13 08:41:45 -0500557 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500558
559 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
560 EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
561 ext4_msg(sb, KERN_ERR,
562 "Online resizing not (yet) supported with bigalloc");
563 return -EOPNOTSUPP;
564 }
565
Yongqiang Yang19c52462012-01-04 17:09:44 -0500566 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
567 sizeof(__u64))) {
568 return -EFAULT;
569 }
570
Yongqiang Yang19c52462012-01-04 17:09:44 -0500571 err = ext4_resize_begin(sb);
572 if (err)
573 return err;
574
Al Viro8cae6f72012-07-19 11:19:07 +0400575 err = mnt_want_write_file(filp);
Yongqiang Yang19c52462012-01-04 17:09:44 -0500576 if (err)
577 goto resizefs_out;
578
579 err = ext4_resize_fs(sb, n_blocks_count);
580 if (EXT4_SB(sb)->s_journal) {
581 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
582 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
583 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
584 }
585 if (err == 0)
586 err = err2;
Al Viro8cae6f72012-07-19 11:19:07 +0400587 mnt_drop_write_file(filp);
Theodore Ts'o7f511862013-01-13 08:41:45 -0500588 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
589 ext4_has_group_desc_csum(sb) &&
590 test_opt(sb, INIT_INODE_TABLE))
591 err = ext4_register_li_request(sb, o_group);
592
Yongqiang Yang19c52462012-01-04 17:09:44 -0500593resizefs_out:
594 ext4_resize_end(sb);
595 return err;
596 }
597
Lukas Czernere681c042010-11-19 21:47:07 -0500598 case FITRIM:
599 {
Lukas Czerner41431792011-02-23 12:42:32 -0500600 struct request_queue *q = bdev_get_queue(sb->s_bdev);
Lukas Czernere681c042010-11-19 21:47:07 -0500601 struct fstrim_range range;
602 int ret = 0;
603
604 if (!capable(CAP_SYS_ADMIN))
605 return -EPERM;
606
Lukas Czerner41431792011-02-23 12:42:32 -0500607 if (!blk_queue_discard(q))
608 return -EOPNOTSUPP;
609
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400610 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czernere681c042010-11-19 21:47:07 -0500611 sizeof(range)))
612 return -EFAULT;
613
Lukas Czerner5c2ed622011-02-23 17:49:51 -0500614 range.minlen = max((unsigned int)range.minlen,
615 q->limits.discard_granularity);
Lukas Czernere681c042010-11-19 21:47:07 -0500616 ret = ext4_trim_fs(sb, &range);
617 if (ret < 0)
618 return ret;
619
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400620 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czernere681c042010-11-19 21:47:07 -0500621 sizeof(range)))
622 return -EFAULT;
623
624 return 0;
625 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400626 case EXT4_IOC_PRECACHE_EXTENTS:
627 return ext4_ext_precache(inode);
Michael Halcrow9bd82122015-04-11 07:48:01 -0400628 case EXT4_IOC_SET_ENCRYPTION_POLICY: {
629#ifdef CONFIG_EXT4_FS_ENCRYPTION
630 struct ext4_encryption_policy policy;
631 int err = 0;
Lukas Czernere681c042010-11-19 21:47:07 -0500632
Michael Halcrow9bd82122015-04-11 07:48:01 -0400633 if (copy_from_user(&policy,
634 (struct ext4_encryption_policy __user *)arg,
635 sizeof(policy))) {
636 err = -EFAULT;
637 goto encryption_policy_out;
638 }
639
640 err = ext4_process_policy(&policy, inode);
641encryption_policy_out:
642 return err;
643#else
644 return -EOPNOTSUPP;
645#endif
646 }
647 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
648 int err, err2;
649 struct ext4_sb_info *sbi = EXT4_SB(sb);
650 handle_t *handle;
651
652 if (!ext4_sb_has_crypto(sb))
653 return -EOPNOTSUPP;
654 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
655 err = mnt_want_write_file(filp);
656 if (err)
657 return err;
658 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
659 if (IS_ERR(handle)) {
660 err = PTR_ERR(handle);
661 goto pwsalt_err_exit;
662 }
663 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
664 if (err)
665 goto pwsalt_err_journal;
666 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
667 err = ext4_handle_dirty_metadata(handle, NULL,
668 sbi->s_sbh);
669 pwsalt_err_journal:
670 err2 = ext4_journal_stop(handle);
671 if (err2 && !err)
672 err = err2;
673 pwsalt_err_exit:
674 mnt_drop_write_file(filp);
675 if (err)
676 return err;
677 }
Fabian Frederickb4ab9e22015-06-08 12:23:21 -0400678 if (copy_to_user((void __user *) arg,
679 sbi->s_es->s_encrypt_pw_salt, 16))
Michael Halcrow9bd82122015-04-11 07:48:01 -0400680 return -EFAULT;
681 return 0;
682 }
683 case EXT4_IOC_GET_ENCRYPTION_POLICY: {
684#ifdef CONFIG_EXT4_FS_ENCRYPTION
685 struct ext4_encryption_policy policy;
686 int err = 0;
687
688 if (!ext4_encrypted_inode(inode))
689 return -ENOENT;
690 err = ext4_get_policy(inode, &policy);
691 if (err)
692 return err;
Fabian Frederickb4ab9e22015-06-08 12:23:21 -0400693 if (copy_to_user((void __user *)arg, &policy, sizeof(policy)))
Michael Halcrow9bd82122015-04-11 07:48:01 -0400694 return -EFAULT;
695 return 0;
696#else
697 return -EOPNOTSUPP;
698#endif
699 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 default:
701 return -ENOTTY;
702 }
703}
704
705#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700706long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 /* These are just misnamed, they actually get/put from/to user an int */
709 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700710 case EXT4_IOC32_GETFLAGS:
711 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700712 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700713 case EXT4_IOC32_SETFLAGS:
714 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700716 case EXT4_IOC32_GETVERSION:
717 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700719 case EXT4_IOC32_SETVERSION:
720 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700722 case EXT4_IOC32_GROUP_EXTEND:
723 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700725 case EXT4_IOC32_GETVERSION_OLD:
726 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700728 case EXT4_IOC32_SETVERSION_OLD:
729 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700731 case EXT4_IOC32_GETRSVSZ:
732 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700734 case EXT4_IOC32_SETRSVSZ:
735 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700736 break;
Ben Hutchings4d92dc02010-05-17 06:00:00 -0400737 case EXT4_IOC32_GROUP_ADD: {
738 struct compat_ext4_new_group_input __user *uinput;
739 struct ext4_new_group_input input;
740 mm_segment_t old_fs;
741 int err;
742
743 uinput = compat_ptr(arg);
744 err = get_user(input.group, &uinput->group);
745 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
746 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
747 err |= get_user(input.inode_table, &uinput->inode_table);
748 err |= get_user(input.blocks_count, &uinput->blocks_count);
749 err |= get_user(input.reserved_blocks,
750 &uinput->reserved_blocks);
751 if (err)
752 return -EFAULT;
753 old_fs = get_fs();
754 set_fs(KERNEL_DS);
755 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
756 (unsigned long) &input);
757 set_fs(old_fs);
758 return err;
759 }
Christian Borntraegerb684b2e2010-05-15 00:00:00 -0400760 case EXT4_IOC_MOVE_EXT:
Tao Maa56e69c2011-03-20 23:16:58 -0400761 case FITRIM:
Yongqiang Yang19c52462012-01-04 17:09:44 -0500762 case EXT4_IOC_RESIZE_FS:
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400763 case EXT4_IOC_PRECACHE_EXTENTS:
Michael Halcrow9bd82122015-04-11 07:48:01 -0400764 case EXT4_IOC_SET_ENCRYPTION_POLICY:
765 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
766 case EXT4_IOC_GET_ENCRYPTION_POLICY:
Christian Borntraegerb684b2e2010-05-15 00:00:00 -0400767 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768 default:
769 return -ENOIOCTLCMD;
770 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400771 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772}
773#endif