blob: 5e872fd40e5e38655435fbcf91802244c9ce8959 [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;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040034
35 ap = (unsigned char *)a;
36 bp = (unsigned char *)b;
37 while (len-- > 0) {
Fabian Frederick4b7e2db2015-06-12 23:46:33 -040038 swap(*ap, *bp);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040039 ap++;
40 bp++;
41 }
42}
43
44/**
45 * Swap i_data and associated attributes between @inode1 and @inode2.
46 * This function is used for the primary swap between inode1 and inode2
47 * and also to revert this primary swap in case of errors.
48 *
49 * Therefore you have to make sure, that calling this method twice
50 * will revert all changes.
51 *
52 * @inode1: pointer to first inode
53 * @inode2: pointer to second inode
54 */
55static void swap_inode_data(struct inode *inode1, struct inode *inode2)
56{
57 loff_t isize;
58 struct ext4_inode_info *ei1;
59 struct ext4_inode_info *ei2;
60
61 ei1 = EXT4_I(inode1);
62 ei2 = EXT4_I(inode2);
63
64 memswap(&inode1->i_flags, &inode2->i_flags, sizeof(inode1->i_flags));
65 memswap(&inode1->i_version, &inode2->i_version,
66 sizeof(inode1->i_version));
67 memswap(&inode1->i_blocks, &inode2->i_blocks,
68 sizeof(inode1->i_blocks));
69 memswap(&inode1->i_bytes, &inode2->i_bytes, sizeof(inode1->i_bytes));
70 memswap(&inode1->i_atime, &inode2->i_atime, sizeof(inode1->i_atime));
71 memswap(&inode1->i_mtime, &inode2->i_mtime, sizeof(inode1->i_mtime));
72
73 memswap(ei1->i_data, ei2->i_data, sizeof(ei1->i_data));
74 memswap(&ei1->i_flags, &ei2->i_flags, sizeof(ei1->i_flags));
75 memswap(&ei1->i_disksize, &ei2->i_disksize, sizeof(ei1->i_disksize));
Theodore Ts'ocde2d7a2013-08-12 09:29:30 -040076 ext4_es_remove_extent(inode1, 0, EXT_MAX_BLOCKS);
77 ext4_es_remove_extent(inode2, 0, EXT_MAX_BLOCKS);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040078
79 isize = i_size_read(inode1);
80 i_size_write(inode1, i_size_read(inode2));
81 i_size_write(inode2, isize);
82}
83
84/**
85 * Swap the information from the given @inode and the inode
86 * EXT4_BOOT_LOADER_INO. It will basically swap i_data and all other
87 * important fields of the inodes.
88 *
89 * @sb: the super block of the filesystem
90 * @inode: the inode to swap with EXT4_BOOT_LOADER_INO
91 *
92 */
93static long swap_inode_boot_loader(struct super_block *sb,
94 struct inode *inode)
95{
96 handle_t *handle;
97 int err;
98 struct inode *inode_bl;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -040099 struct ext4_inode_info *ei_bl;
jon ernstd7092ae2014-01-11 13:26:56 -0500100 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400101
Theodore Ts'od8558a22014-02-17 20:44:36 -0500102 if (inode->i_nlink != 1 || !S_ISREG(inode->i_mode))
103 return -EINVAL;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400104
Theodore Ts'od8558a22014-02-17 20:44:36 -0500105 if (!inode_owner_or_capable(inode) || !capable(CAP_SYS_ADMIN))
106 return -EPERM;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400107
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400108 inode_bl = ext4_iget(sb, EXT4_BOOT_LOADER_INO);
Theodore Ts'od8558a22014-02-17 20:44:36 -0500109 if (IS_ERR(inode_bl))
110 return PTR_ERR(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400111 ei_bl = EXT4_I(inode_bl);
112
113 filemap_flush(inode->i_mapping);
114 filemap_flush(inode_bl->i_mapping);
115
116 /* Protect orig inodes against a truncate and make sure,
117 * that only 1 swap_inode_boot_loader is running. */
J. Bruce Fields375e2892012-04-18 15:16:33 -0400118 lock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400119
120 truncate_inode_pages(&inode->i_data, 0);
121 truncate_inode_pages(&inode_bl->i_data, 0);
122
123 /* Wait for all existing dio workers */
124 ext4_inode_block_unlocked_dio(inode);
125 ext4_inode_block_unlocked_dio(inode_bl);
126 inode_dio_wait(inode);
127 inode_dio_wait(inode_bl);
128
129 handle = ext4_journal_start(inode_bl, EXT4_HT_MOVE_EXTENTS, 2);
130 if (IS_ERR(handle)) {
131 err = -EINVAL;
Zheng Liu30d29b12014-02-12 11:48:31 -0500132 goto journal_err_out;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400133 }
134
135 /* Protect extent tree against block allocations via delalloc */
136 ext4_double_down_write_data_sem(inode, inode_bl);
137
138 if (inode_bl->i_nlink == 0) {
139 /* this inode has never been used as a BOOT_LOADER */
140 set_nlink(inode_bl, 1);
141 i_uid_write(inode_bl, 0);
142 i_gid_write(inode_bl, 0);
143 inode_bl->i_flags = 0;
144 ei_bl->i_flags = 0;
145 inode_bl->i_version = 1;
146 i_size_write(inode_bl, 0);
147 inode_bl->i_mode = S_IFREG;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400148 if (ext4_has_feature_extents(sb)) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400149 ext4_set_inode_flag(inode_bl, EXT4_INODE_EXTENTS);
150 ext4_ext_tree_init(handle, inode_bl);
151 } else
152 memset(ei_bl->i_data, 0, sizeof(ei_bl->i_data));
153 }
154
155 swap_inode_data(inode, inode_bl);
156
157 inode->i_ctime = inode_bl->i_ctime = ext4_current_time(inode);
158
159 spin_lock(&sbi->s_next_gen_lock);
160 inode->i_generation = sbi->s_next_generation++;
161 inode_bl->i_generation = sbi->s_next_generation++;
162 spin_unlock(&sbi->s_next_gen_lock);
163
164 ext4_discard_preallocations(inode);
165
166 err = ext4_mark_inode_dirty(handle, inode);
167 if (err < 0) {
168 ext4_warning(inode->i_sb,
169 "couldn't mark inode #%lu dirty (err %d)",
170 inode->i_ino, err);
171 /* Revert all changes: */
172 swap_inode_data(inode, inode_bl);
173 } else {
174 err = ext4_mark_inode_dirty(handle, inode_bl);
175 if (err < 0) {
176 ext4_warning(inode_bl->i_sb,
177 "couldn't mark inode #%lu dirty (err %d)",
178 inode_bl->i_ino, err);
179 /* Revert all changes: */
180 swap_inode_data(inode, inode_bl);
181 ext4_mark_inode_dirty(handle, inode);
182 }
183 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400184 ext4_journal_stop(handle);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400185 ext4_double_up_write_data_sem(inode, inode_bl);
186
Zheng Liu30d29b12014-02-12 11:48:31 -0500187journal_err_out:
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400188 ext4_inode_resume_unlocked_dio(inode);
189 ext4_inode_resume_unlocked_dio(inode_bl);
J. Bruce Fields375e2892012-04-18 15:16:33 -0400190 unlock_two_nondirectories(inode, inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400191 iput(inode_bl);
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400192 return err;
193}
194
Michael Halcrow9bd82122015-04-11 07:48:01 -0400195static int uuid_is_zero(__u8 u[16])
196{
197 int i;
198
199 for (i = 0; i < 16; i++)
200 if (u[i])
201 return 0;
202 return 1;
203}
204
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400205long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700206{
Al Viro496ad9a2013-01-23 17:07:38 -0500207 struct inode *inode = file_inode(filp);
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400208 struct super_block *sb = inode->i_sb;
Mingming Cao617ba132006-10-11 01:20:53 -0700209 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 unsigned int flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700211
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400212 ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213
214 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700215 case EXT4_IOC_GETFLAGS:
Jan Karaff9ddf72007-07-18 09:24:20 -0400216 ext4_get_inode_flags(ei);
Mingming Cao617ba132006-10-11 01:20:53 -0700217 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700218 return put_user(flags, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700219 case EXT4_IOC_SETFLAGS: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220 handle_t *handle = NULL;
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400221 int err, migrate = 0;
Mingming Cao617ba132006-10-11 01:20:53 -0700222 struct ext4_iloc iloc;
Theodore Ts'o79906962012-05-31 23:46:01 -0400223 unsigned int oldflags, mask, i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224 unsigned int jflag;
225
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700226 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227 return -EACCES;
228
229 if (get_user(flags, (int __user *) arg))
230 return -EFAULT;
231
Al Viroa561be72011-11-23 11:57:51 -0500232 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800233 if (err)
234 return err;
235
Duane Griffin2dc6b0d2009-02-15 18:09:20 -0500236 flags = ext4_mask_flags(inode->i_mode, flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237
Dave Hansen42a74f22008-02-15 14:37:46 -0800238 err = -EPERM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239 mutex_lock(&inode->i_mutex);
Jan Karae47776a2007-11-14 16:58:56 -0800240 /* Is it quota file? Do not allow user to mess with it */
Dave Hansen42a74f22008-02-15 14:37:46 -0800241 if (IS_NOQUOTA(inode))
242 goto flags_out;
243
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 oldflags = ei->i_flags;
245
246 /* The JOURNAL_DATA flag is modifiable only by root */
Mingming Cao617ba132006-10-11 01:20:53 -0700247 jflag = flags & EXT4_JOURNAL_DATA_FL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248
249 /*
250 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
251 * the relevant capability.
252 *
253 * This test looks nicer. Thanks to Pauline Middelink
254 */
Mingming Cao617ba132006-10-11 01:20:53 -0700255 if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800256 if (!capable(CAP_LINUX_IMMUTABLE))
257 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258 }
259
260 /*
261 * The JOURNAL_DATA flag can only be changed by
262 * the relevant capability.
263 */
Mingming Cao617ba132006-10-11 01:20:53 -0700264 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800265 if (!capable(CAP_SYS_RESOURCE))
266 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267 }
Theodore Ts'o996bb9f2013-04-03 22:04:52 -0400268 if ((flags ^ oldflags) & EXT4_EXTENTS_FL)
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400269 migrate = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270
Jiaying Zhangc8d46e42010-02-24 09:52:53 -0500271 if (flags & EXT4_EOFBLOCKS_FL) {
272 /* we don't support adding EOFBLOCKS flag */
273 if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
274 err = -EOPNOTSUPP;
275 goto flags_out;
276 }
277 } else if (oldflags & EXT4_EOFBLOCKS_FL)
278 ext4_truncate(inode);
279
Theodore Ts'o9924a922013-02-08 21:59:22 -0500280 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 if (IS_ERR(handle)) {
Dave Hansen42a74f22008-02-15 14:37:46 -0800282 err = PTR_ERR(handle);
283 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 }
285 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500286 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -0700287 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 if (err)
289 goto flags_err;
290
Theodore Ts'o79906962012-05-31 23:46:01 -0400291 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) {
292 if (!(mask & EXT4_FL_USER_MODIFIABLE))
293 continue;
294 if (mask & flags)
295 ext4_set_inode_flag(inode, i);
296 else
297 ext4_clear_inode_flag(inode, i);
298 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299
Mingming Cao617ba132006-10-11 01:20:53 -0700300 ext4_set_inode_flags(inode);
Kalpak Shahef7f3832007-07-18 09:15:20 -0400301 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302
Mingming Cao617ba132006-10-11 01:20:53 -0700303 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304flags_err:
Mingming Cao617ba132006-10-11 01:20:53 -0700305 ext4_journal_stop(handle);
Dave Hansen42a74f22008-02-15 14:37:46 -0800306 if (err)
307 goto flags_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308
Mingming Cao617ba132006-10-11 01:20:53 -0700309 if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
310 err = ext4_change_inode_journal_flag(inode, jflag);
Aneesh Kumar K.V4db46fc2008-10-08 23:34:06 -0400311 if (err)
312 goto flags_out;
Theodore Ts'o996bb9f2013-04-03 22:04:52 -0400313 if (migrate) {
314 if (flags & EXT4_EXTENTS_FL)
315 err = ext4_ext_migrate(inode);
316 else
317 err = ext4_ind_migrate(inode);
318 }
319
Dave Hansen42a74f22008-02-15 14:37:46 -0800320flags_out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 mutex_unlock(&inode->i_mutex);
Al Viro2a79f172011-12-09 08:06:57 -0500322 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 return err;
324 }
Mingming Cao617ba132006-10-11 01:20:53 -0700325 case EXT4_IOC_GETVERSION:
326 case EXT4_IOC_GETVERSION_OLD:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700327 return put_user(inode->i_generation, (int __user *) arg);
Mingming Cao617ba132006-10-11 01:20:53 -0700328 case EXT4_IOC_SETVERSION:
329 case EXT4_IOC_SETVERSION_OLD: {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700330 handle_t *handle;
Mingming Cao617ba132006-10-11 01:20:53 -0700331 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700332 __u32 generation;
333 int err;
334
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700335 if (!inode_owner_or_capable(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336 return -EPERM;
Dave Hansen42a74f22008-02-15 14:37:46 -0800337
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400338 if (ext4_has_metadata_csum(inode->i_sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -0400339 ext4_warning(sb, "Setting inode version is not "
340 "supported with metadata_csum enabled.");
341 return -ENOTTY;
342 }
343
Al Viroa561be72011-11-23 11:57:51 -0500344 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800345 if (err)
346 return err;
347 if (get_user(generation, (int __user *) arg)) {
348 err = -EFAULT;
349 goto setversion_out;
350 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100352 mutex_lock(&inode->i_mutex);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500353 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Hansen42a74f22008-02-15 14:37:46 -0800354 if (IS_ERR(handle)) {
355 err = PTR_ERR(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100356 goto unlock_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800357 }
Mingming Cao617ba132006-10-11 01:20:53 -0700358 err = ext4_reserve_inode_write(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700359 if (err == 0) {
Kalpak Shahef7f3832007-07-18 09:15:20 -0400360 inode->i_ctime = ext4_current_time(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 inode->i_generation = generation;
Mingming Cao617ba132006-10-11 01:20:53 -0700362 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363 }
Mingming Cao617ba132006-10-11 01:20:53 -0700364 ext4_journal_stop(handle);
Djalal Harouni6c2155b2012-01-03 02:31:52 +0100365
366unlock_out:
367 mutex_unlock(&inode->i_mutex);
Dave Hansen42a74f22008-02-15 14:37:46 -0800368setversion_out:
Al Viro2a79f172011-12-09 08:06:57 -0500369 mnt_drop_write_file(filp);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370 return err;
371 }
Mingming Cao617ba132006-10-11 01:20:53 -0700372 case EXT4_IOC_GROUP_EXTEND: {
373 ext4_fsblk_t n_blocks_count;
Peng Taoac046f12009-07-13 09:30:17 -0400374 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400376 err = ext4_resize_begin(sb);
377 if (err)
378 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700379
Djalal Harouni014a1772012-01-04 17:09:52 -0500380 if (get_user(n_blocks_count, (__u32 __user *)arg)) {
381 err = -EFAULT;
382 goto group_extend_out;
383 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700384
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400385 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400386 ext4_msg(sb, KERN_ERR,
387 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500388 err = -EOPNOTSUPP;
389 goto group_extend_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400390 }
391
Al Viroa561be72011-11-23 11:57:51 -0500392 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800393 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500394 goto group_extend_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800395
Mingming Cao617ba132006-10-11 01:20:53 -0700396 err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
Peng Taoac046f12009-07-13 09:30:17 -0400397 if (EXT4_SB(sb)->s_journal) {
398 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
399 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
400 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
401 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400402 if (err == 0)
403 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500404 mnt_drop_write_file(filp);
Djalal Harouni014a1772012-01-04 17:09:52 -0500405group_extend_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400406 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407 return err;
408 }
Akira Fujita748de672009-06-17 19:24:03 -0400409
410 case EXT4_IOC_MOVE_EXT: {
411 struct move_extent me;
Al Viro2903ff02012-08-28 12:52:22 -0400412 struct fd donor;
413 int err;
Akira Fujita748de672009-06-17 19:24:03 -0400414
Akira Fujita4a585792009-12-06 23:38:31 -0500415 if (!(filp->f_mode & FMODE_READ) ||
416 !(filp->f_mode & FMODE_WRITE))
417 return -EBADF;
418
Akira Fujita748de672009-06-17 19:24:03 -0400419 if (copy_from_user(&me,
420 (struct move_extent __user *)arg, sizeof(me)))
421 return -EFAULT;
Akira Fujita4a585792009-12-06 23:38:31 -0500422 me.moved_len = 0;
Akira Fujita748de672009-06-17 19:24:03 -0400423
Al Viro2903ff02012-08-28 12:52:22 -0400424 donor = fdget(me.donor_fd);
425 if (!donor.file)
Akira Fujita748de672009-06-17 19:24:03 -0400426 return -EBADF;
427
Al Viro2903ff02012-08-28 12:52:22 -0400428 if (!(donor.file->f_mode & FMODE_WRITE)) {
Akira Fujita4a585792009-12-06 23:38:31 -0500429 err = -EBADF;
430 goto mext_out;
Akira Fujita748de672009-06-17 19:24:03 -0400431 }
432
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400433 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400434 ext4_msg(sb, KERN_ERR,
435 "Online defrag not supported with bigalloc");
Al Viro399c9b82012-08-26 21:00:03 -0400436 err = -EOPNOTSUPP;
437 goto mext_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400438 }
439
Al Viroa561be72011-11-23 11:57:51 -0500440 err = mnt_want_write_file(filp);
Akira Fujita4a585792009-12-06 23:38:31 -0500441 if (err)
442 goto mext_out;
443
Al Viro2903ff02012-08-28 12:52:22 -0400444 err = ext4_move_extents(filp, donor.file, me.orig_start,
Akira Fujita748de672009-06-17 19:24:03 -0400445 me.donor_start, me.len, &me.moved_len);
Al Viro2a79f172011-12-09 08:06:57 -0500446 mnt_drop_write_file(filp);
Akira Fujita748de672009-06-17 19:24:03 -0400447
Theodore Ts'o60e66792010-05-17 07:00:00 -0400448 if (copy_to_user((struct move_extent __user *)arg,
Akira Fujitac437b272010-03-04 00:39:24 -0500449 &me, sizeof(me)))
Akira Fujita4a585792009-12-06 23:38:31 -0500450 err = -EFAULT;
451mext_out:
Al Viro2903ff02012-08-28 12:52:22 -0400452 fdput(donor);
Akira Fujita748de672009-06-17 19:24:03 -0400453 return err;
454 }
455
Mingming Cao617ba132006-10-11 01:20:53 -0700456 case EXT4_IOC_GROUP_ADD: {
457 struct ext4_new_group_data input;
Peng Taoac046f12009-07-13 09:30:17 -0400458 int err, err2=0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400460 err = ext4_resize_begin(sb);
461 if (err)
462 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700463
Mingming Cao617ba132006-10-11 01:20:53 -0700464 if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
Djalal Harouni014a1772012-01-04 17:09:52 -0500465 sizeof(input))) {
466 err = -EFAULT;
467 goto group_add_out;
468 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700469
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400470 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400471 ext4_msg(sb, KERN_ERR,
472 "Online resizing not supported with bigalloc");
Djalal Harouni014a1772012-01-04 17:09:52 -0500473 err = -EOPNOTSUPP;
474 goto group_add_out;
Theodore Ts'obab08ab2011-09-09 18:36:51 -0400475 }
476
Al Viroa561be72011-11-23 11:57:51 -0500477 err = mnt_want_write_file(filp);
Dave Hansen42a74f22008-02-15 14:37:46 -0800478 if (err)
Djalal Harouni014a1772012-01-04 17:09:52 -0500479 goto group_add_out;
Dave Hansen42a74f22008-02-15 14:37:46 -0800480
Mingming Cao617ba132006-10-11 01:20:53 -0700481 err = ext4_group_add(sb, &input);
Peng Taoac046f12009-07-13 09:30:17 -0400482 if (EXT4_SB(sb)->s_journal) {
483 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
484 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
485 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
486 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -0400487 if (err == 0)
488 err = err2;
Al Viro2a79f172011-12-09 08:06:57 -0500489 mnt_drop_write_file(filp);
Theodore Ts'o7f511862013-01-13 08:41:45 -0500490 if (!err && ext4_has_group_desc_csum(sb) &&
491 test_opt(sb, INIT_INODE_TABLE))
492 err = ext4_register_li_request(sb, input.group);
Djalal Harouni014a1772012-01-04 17:09:52 -0500493group_add_out:
Yongqiang Yang8f82f842011-07-26 21:35:44 -0400494 ext4_resize_end(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700495 return err;
496 }
497
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500498 case EXT4_IOC_MIGRATE:
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400499 {
500 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700501 if (!inode_owner_or_capable(inode))
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400502 return -EACCES;
503
Al Viroa561be72011-11-23 11:57:51 -0500504 err = mnt_want_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400505 if (err)
506 return err;
507 /*
508 * inode_mutex prevent write and truncate on the file.
509 * Read still goes through. We take i_data_sem in
510 * ext4_ext_swap_inode_data before we switch the
511 * inode format to prevent read.
512 */
513 mutex_lock(&(inode->i_mutex));
514 err = ext4_ext_migrate(inode);
515 mutex_unlock(&(inode->i_mutex));
Al Viro2a79f172011-12-09 08:06:57 -0500516 mnt_drop_write_file(filp);
Aneesh Kumar K.V2a43a872008-09-13 12:52:26 -0400517 return err;
518 }
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -0500519
Theodore Ts'occd25062009-02-26 01:04:07 -0500520 case EXT4_IOC_ALLOC_DA_BLKS:
521 {
522 int err;
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700523 if (!inode_owner_or_capable(inode))
Theodore Ts'occd25062009-02-26 01:04:07 -0500524 return -EACCES;
525
Al Viroa561be72011-11-23 11:57:51 -0500526 err = mnt_want_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500527 if (err)
528 return err;
529 err = ext4_alloc_da_blocks(inode);
Al Viro2a79f172011-12-09 08:06:57 -0500530 mnt_drop_write_file(filp);
Theodore Ts'occd25062009-02-26 01:04:07 -0500531 return err;
532 }
533
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400534 case EXT4_IOC_SWAP_BOOT:
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400535 {
536 int err;
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400537 if (!(filp->f_mode & FMODE_WRITE))
538 return -EBADF;
Dmitry Monakhov3e67cfad22014-10-03 12:47:23 -0400539 err = mnt_want_write_file(filp);
540 if (err)
541 return err;
542 err = swap_inode_boot_loader(sb, inode);
543 mnt_drop_write_file(filp);
544 return err;
545 }
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -0400546
Yongqiang Yang19c52462012-01-04 17:09:44 -0500547 case EXT4_IOC_RESIZE_FS: {
548 ext4_fsblk_t n_blocks_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500549 int err = 0, err2 = 0;
Theodore Ts'o7f511862013-01-13 08:41:45 -0500550 ext4_group_t o_group = EXT4_SB(sb)->s_groups_count;
Yongqiang Yang19c52462012-01-04 17:09:44 -0500551
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400552 if (ext4_has_feature_bigalloc(sb)) {
Yongqiang Yang19c52462012-01-04 17:09:44 -0500553 ext4_msg(sb, KERN_ERR,
554 "Online resizing not (yet) supported with bigalloc");
555 return -EOPNOTSUPP;
556 }
557
Yongqiang Yang19c52462012-01-04 17:09:44 -0500558 if (copy_from_user(&n_blocks_count, (__u64 __user *)arg,
559 sizeof(__u64))) {
560 return -EFAULT;
561 }
562
Yongqiang Yang19c52462012-01-04 17:09:44 -0500563 err = ext4_resize_begin(sb);
564 if (err)
565 return err;
566
Al Viro8cae6f72012-07-19 11:19:07 +0400567 err = mnt_want_write_file(filp);
Yongqiang Yang19c52462012-01-04 17:09:44 -0500568 if (err)
569 goto resizefs_out;
570
571 err = ext4_resize_fs(sb, n_blocks_count);
572 if (EXT4_SB(sb)->s_journal) {
573 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
574 err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
575 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
576 }
577 if (err == 0)
578 err = err2;
Al Viro8cae6f72012-07-19 11:19:07 +0400579 mnt_drop_write_file(filp);
Theodore Ts'o7f511862013-01-13 08:41:45 -0500580 if (!err && (o_group > EXT4_SB(sb)->s_groups_count) &&
581 ext4_has_group_desc_csum(sb) &&
582 test_opt(sb, INIT_INODE_TABLE))
583 err = ext4_register_li_request(sb, o_group);
584
Yongqiang Yang19c52462012-01-04 17:09:44 -0500585resizefs_out:
586 ext4_resize_end(sb);
587 return err;
588 }
589
Lukas Czernere681c042010-11-19 21:47:07 -0500590 case FITRIM:
591 {
Lukas Czerner41431792011-02-23 12:42:32 -0500592 struct request_queue *q = bdev_get_queue(sb->s_bdev);
Lukas Czernere681c042010-11-19 21:47:07 -0500593 struct fstrim_range range;
594 int ret = 0;
595
596 if (!capable(CAP_SYS_ADMIN))
597 return -EPERM;
598
Lukas Czerner41431792011-02-23 12:42:32 -0500599 if (!blk_queue_discard(q))
600 return -EOPNOTSUPP;
601
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400602 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
Lukas Czernere681c042010-11-19 21:47:07 -0500603 sizeof(range)))
604 return -EFAULT;
605
Lukas Czerner5c2ed622011-02-23 17:49:51 -0500606 range.minlen = max((unsigned int)range.minlen,
607 q->limits.discard_granularity);
Lukas Czernere681c042010-11-19 21:47:07 -0500608 ret = ext4_trim_fs(sb, &range);
609 if (ret < 0)
610 return ret;
611
H Hartley Sweetene6705f72011-10-18 10:59:51 -0400612 if (copy_to_user((struct fstrim_range __user *)arg, &range,
Lukas Czernere681c042010-11-19 21:47:07 -0500613 sizeof(range)))
614 return -EFAULT;
615
616 return 0;
617 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400618 case EXT4_IOC_PRECACHE_EXTENTS:
619 return ext4_ext_precache(inode);
Michael Halcrow9bd82122015-04-11 07:48:01 -0400620 case EXT4_IOC_SET_ENCRYPTION_POLICY: {
621#ifdef CONFIG_EXT4_FS_ENCRYPTION
622 struct ext4_encryption_policy policy;
623 int err = 0;
Lukas Czernere681c042010-11-19 21:47:07 -0500624
Michael Halcrow9bd82122015-04-11 07:48:01 -0400625 if (copy_from_user(&policy,
626 (struct ext4_encryption_policy __user *)arg,
627 sizeof(policy))) {
628 err = -EFAULT;
629 goto encryption_policy_out;
630 }
631
632 err = ext4_process_policy(&policy, inode);
633encryption_policy_out:
634 return err;
635#else
636 return -EOPNOTSUPP;
637#endif
638 }
639 case EXT4_IOC_GET_ENCRYPTION_PWSALT: {
640 int err, err2;
641 struct ext4_sb_info *sbi = EXT4_SB(sb);
642 handle_t *handle;
643
644 if (!ext4_sb_has_crypto(sb))
645 return -EOPNOTSUPP;
646 if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
647 err = mnt_want_write_file(filp);
648 if (err)
649 return err;
650 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
651 if (IS_ERR(handle)) {
652 err = PTR_ERR(handle);
653 goto pwsalt_err_exit;
654 }
655 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
656 if (err)
657 goto pwsalt_err_journal;
658 generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
659 err = ext4_handle_dirty_metadata(handle, NULL,
660 sbi->s_sbh);
661 pwsalt_err_journal:
662 err2 = ext4_journal_stop(handle);
663 if (err2 && !err)
664 err = err2;
665 pwsalt_err_exit:
666 mnt_drop_write_file(filp);
667 if (err)
668 return err;
669 }
Fabian Frederickb4ab9e22015-06-08 12:23:21 -0400670 if (copy_to_user((void __user *) arg,
671 sbi->s_es->s_encrypt_pw_salt, 16))
Michael Halcrow9bd82122015-04-11 07:48:01 -0400672 return -EFAULT;
673 return 0;
674 }
675 case EXT4_IOC_GET_ENCRYPTION_POLICY: {
676#ifdef CONFIG_EXT4_FS_ENCRYPTION
677 struct ext4_encryption_policy policy;
678 int err = 0;
679
680 if (!ext4_encrypted_inode(inode))
681 return -ENOENT;
682 err = ext4_get_policy(inode, &policy);
683 if (err)
684 return err;
Fabian Frederickb4ab9e22015-06-08 12:23:21 -0400685 if (copy_to_user((void __user *)arg, &policy, sizeof(policy)))
Michael Halcrow9bd82122015-04-11 07:48:01 -0400686 return -EFAULT;
687 return 0;
688#else
689 return -EOPNOTSUPP;
690#endif
691 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 default:
693 return -ENOTTY;
694 }
695}
696
697#ifdef CONFIG_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -0700698long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 /* These are just misnamed, they actually get/put from/to user an int */
701 switch (cmd) {
Mingming Cao617ba132006-10-11 01:20:53 -0700702 case EXT4_IOC32_GETFLAGS:
703 cmd = EXT4_IOC_GETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700705 case EXT4_IOC32_SETFLAGS:
706 cmd = EXT4_IOC_SETFLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700708 case EXT4_IOC32_GETVERSION:
709 cmd = EXT4_IOC_GETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700710 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700711 case EXT4_IOC32_SETVERSION:
712 cmd = EXT4_IOC_SETVERSION;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700714 case EXT4_IOC32_GROUP_EXTEND:
715 cmd = EXT4_IOC_GROUP_EXTEND;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700717 case EXT4_IOC32_GETVERSION_OLD:
718 cmd = EXT4_IOC_GETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700720 case EXT4_IOC32_SETVERSION_OLD:
721 cmd = EXT4_IOC_SETVERSION_OLD;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700723 case EXT4_IOC32_GETRSVSZ:
724 cmd = EXT4_IOC_GETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700726 case EXT4_IOC32_SETRSVSZ:
727 cmd = EXT4_IOC_SETRSVSZ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 break;
Ben Hutchings4d92dc02010-05-17 06:00:00 -0400729 case EXT4_IOC32_GROUP_ADD: {
730 struct compat_ext4_new_group_input __user *uinput;
731 struct ext4_new_group_input input;
732 mm_segment_t old_fs;
733 int err;
734
735 uinput = compat_ptr(arg);
736 err = get_user(input.group, &uinput->group);
737 err |= get_user(input.block_bitmap, &uinput->block_bitmap);
738 err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
739 err |= get_user(input.inode_table, &uinput->inode_table);
740 err |= get_user(input.blocks_count, &uinput->blocks_count);
741 err |= get_user(input.reserved_blocks,
742 &uinput->reserved_blocks);
743 if (err)
744 return -EFAULT;
745 old_fs = get_fs();
746 set_fs(KERNEL_DS);
747 err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
748 (unsigned long) &input);
749 set_fs(old_fs);
750 return err;
751 }
Christian Borntraegerb684b2e2010-05-15 00:00:00 -0400752 case EXT4_IOC_MOVE_EXT:
Yongqiang Yang19c52462012-01-04 17:09:44 -0500753 case EXT4_IOC_RESIZE_FS:
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400754 case EXT4_IOC_PRECACHE_EXTENTS:
Michael Halcrow9bd82122015-04-11 07:48:01 -0400755 case EXT4_IOC_SET_ENCRYPTION_POLICY:
756 case EXT4_IOC_GET_ENCRYPTION_PWSALT:
757 case EXT4_IOC_GET_ENCRYPTION_POLICY:
Christian Borntraegerb684b2e2010-05-15 00:00:00 -0400758 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759 default:
760 return -ENOIOCTLCMD;
761 }
Andi Kleen5cdd7b22008-04-29 22:03:54 -0400762 return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700763}
764#endif