ioctl.c 10.1 KB
Newer Older
1
/*
2
 * linux/fs/ext4/ioctl.c
3 4 5 6 7 8 9 10
 *
 * Copyright (C) 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 */

#include <linux/fs.h>
11
#include <linux/jbd2.h>
12 13 14
#include <linux/capability.h>
#include <linux/time.h>
#include <linux/compat.h>
15
#include <linux/mount.h>
16
#include <linux/file.h>
17
#include <asm/uaccess.h>
18 19
#include "ext4_jbd2.h"
#include "ext4.h"
20

A
Andi Kleen 已提交
21
long ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
22
{
A
Andi Kleen 已提交
23
	struct inode *inode = filp->f_dentry->d_inode;
24
	struct super_block *sb = inode->i_sb;
25
	struct ext4_inode_info *ei = EXT4_I(inode);
26 27
	unsigned int flags;

28
	ext4_debug("cmd = %u, arg = %lu\n", cmd, arg);
29 30

	switch (cmd) {
31
	case EXT4_IOC_GETFLAGS:
32
		ext4_get_inode_flags(ei);
33
		flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
34
		return put_user(flags, (int __user *) arg);
35
	case EXT4_IOC_SETFLAGS: {
36
		handle_t *handle = NULL;
37
		int err, migrate = 0;
38
		struct ext4_iloc iloc;
39 40 41
		unsigned int oldflags;
		unsigned int jflag;

42
		if (!inode_owner_or_capable(inode))
43 44 45 46 47
			return -EACCES;

		if (get_user(flags, (int __user *) arg))
			return -EFAULT;

48
		err = mnt_want_write_file(filp);
49 50 51
		if (err)
			return err;

52
		flags = ext4_mask_flags(inode->i_mode, flags);
53

54
		err = -EPERM;
55
		mutex_lock(&inode->i_mutex);
56
		/* Is it quota file? Do not allow user to mess with it */
57 58 59
		if (IS_NOQUOTA(inode))
			goto flags_out;

60 61 62
		oldflags = ei->i_flags;

		/* The JOURNAL_DATA flag is modifiable only by root */
63
		jflag = flags & EXT4_JOURNAL_DATA_FL;
64 65 66 67 68 69 70

		/*
		 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
		 * the relevant capability.
		 *
		 * This test looks nicer. Thanks to Pauline Middelink
		 */
71
		if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
72 73
			if (!capable(CAP_LINUX_IMMUTABLE))
				goto flags_out;
74 75 76 77 78 79
		}

		/*
		 * The JOURNAL_DATA flag can only be changed by
		 * the relevant capability.
		 */
80
		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
81 82
			if (!capable(CAP_SYS_RESOURCE))
				goto flags_out;
83
		}
84 85 86 87 88 89 90 91 92 93 94
		if (oldflags & EXT4_EXTENTS_FL) {
			/* We don't support clearning extent flags */
			if (!(flags & EXT4_EXTENTS_FL)) {
				err = -EOPNOTSUPP;
				goto flags_out;
			}
		} else if (flags & EXT4_EXTENTS_FL) {
			/* migrate the file */
			migrate = 1;
			flags &= ~EXT4_EXTENTS_FL;
		}
95

96 97 98 99 100 101 102 103 104
		if (flags & EXT4_EOFBLOCKS_FL) {
			/* we don't support adding EOFBLOCKS flag */
			if (!(oldflags & EXT4_EOFBLOCKS_FL)) {
				err = -EOPNOTSUPP;
				goto flags_out;
			}
		} else if (oldflags & EXT4_EOFBLOCKS_FL)
			ext4_truncate(inode);

105
		handle = ext4_journal_start(inode, 1);
106
		if (IS_ERR(handle)) {
107 108
			err = PTR_ERR(handle);
			goto flags_out;
109 110
		}
		if (IS_SYNC(inode))
111
			ext4_handle_sync(handle);
112
		err = ext4_reserve_inode_write(handle, inode, &iloc);
113 114 115
		if (err)
			goto flags_err;

116 117
		flags = flags & EXT4_FL_USER_MODIFIABLE;
		flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
118 119
		ei->i_flags = flags;

120
		ext4_set_inode_flags(inode);
K
Kalpak Shah 已提交
121
		inode->i_ctime = ext4_current_time(inode);
122

123
		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
124
flags_err:
125
		ext4_journal_stop(handle);
126 127
		if (err)
			goto flags_out;
128

129 130
		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
			err = ext4_change_inode_journal_flag(inode, jflag);
131 132 133 134
		if (err)
			goto flags_out;
		if (migrate)
			err = ext4_ext_migrate(inode);
135
flags_out:
136
		mutex_unlock(&inode->i_mutex);
137
		mnt_drop_write(filp->f_path.mnt);
138 139
		return err;
	}
140 141
	case EXT4_IOC_GETVERSION:
	case EXT4_IOC_GETVERSION_OLD:
142
		return put_user(inode->i_generation, (int __user *) arg);
143 144
	case EXT4_IOC_SETVERSION:
	case EXT4_IOC_SETVERSION_OLD: {
145
		handle_t *handle;
146
		struct ext4_iloc iloc;
147 148 149
		__u32 generation;
		int err;

150
		if (!inode_owner_or_capable(inode))
151
			return -EPERM;
152

153
		err = mnt_want_write_file(filp);
154 155 156 157 158 159
		if (err)
			return err;
		if (get_user(generation, (int __user *) arg)) {
			err = -EFAULT;
			goto setversion_out;
		}
160

161
		handle = ext4_journal_start(inode, 1);
162 163 164 165
		if (IS_ERR(handle)) {
			err = PTR_ERR(handle);
			goto setversion_out;
		}
166
		err = ext4_reserve_inode_write(handle, inode, &iloc);
167
		if (err == 0) {
K
Kalpak Shah 已提交
168
			inode->i_ctime = ext4_current_time(inode);
169
			inode->i_generation = generation;
170
			err = ext4_mark_iloc_dirty(handle, inode, &iloc);
171
		}
172
		ext4_journal_stop(handle);
173 174
setversion_out:
		mnt_drop_write(filp->f_path.mnt);
175 176
		return err;
	}
177 178
	case EXT4_IOC_GROUP_EXTEND: {
		ext4_fsblk_t n_blocks_count;
179
		int err, err2=0;
180

181 182 183
		err = ext4_resize_begin(sb);
		if (err)
			return err;
184 185 186 187

		if (get_user(n_blocks_count, (__u32 __user *)arg))
			return -EFAULT;

188 189 190 191 192 193 194
		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
			       EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
			ext4_msg(sb, KERN_ERR,
				 "Online resizing not supported with bigalloc");
			return -EOPNOTSUPP;
		}

195
		err = mnt_want_write_file(filp);
196 197 198
		if (err)
			return err;

199
		err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
200 201 202 203 204
		if (EXT4_SB(sb)->s_journal) {
			jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
			err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
			jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
		}
205 206
		if (err == 0)
			err = err2;
207
		mnt_drop_write(filp->f_path.mnt);
208
		ext4_resize_end(sb);
209 210 211

		return err;
	}
212 213 214 215 216 217

	case EXT4_IOC_MOVE_EXT: {
		struct move_extent me;
		struct file *donor_filp;
		int err;

218 219 220 221
		if (!(filp->f_mode & FMODE_READ) ||
		    !(filp->f_mode & FMODE_WRITE))
			return -EBADF;

222 223 224
		if (copy_from_user(&me,
			(struct move_extent __user *)arg, sizeof(me)))
			return -EFAULT;
225
		me.moved_len = 0;
226 227 228 229 230

		donor_filp = fget(me.donor_fd);
		if (!donor_filp)
			return -EBADF;

231 232 233
		if (!(donor_filp->f_mode & FMODE_WRITE)) {
			err = -EBADF;
			goto mext_out;
234 235
		}

236 237 238 239 240 241 242
		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
			       EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
			ext4_msg(sb, KERN_ERR,
				 "Online defrag not supported with bigalloc");
			return -EOPNOTSUPP;
		}

243
		err = mnt_want_write_file(filp);
244 245 246
		if (err)
			goto mext_out;

247 248
		err = ext4_move_extents(filp, donor_filp, me.orig_start,
					me.donor_start, me.len, &me.moved_len);
249 250 251
		mnt_drop_write(filp->f_path.mnt);
		if (me.moved_len > 0)
			file_remove_suid(donor_filp);
252

253
		if (copy_to_user((struct move_extent __user *)arg,
254
				 &me, sizeof(me)))
255 256 257
			err = -EFAULT;
mext_out:
		fput(donor_filp);
258 259 260
		return err;
	}

261 262
	case EXT4_IOC_GROUP_ADD: {
		struct ext4_new_group_data input;
263
		int err, err2=0;
264

265 266 267
		err = ext4_resize_begin(sb);
		if (err)
			return err;
268

269
		if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
270 271 272
				sizeof(input)))
			return -EFAULT;

273 274 275 276 277 278 279
		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
			       EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
			ext4_msg(sb, KERN_ERR,
				 "Online resizing not supported with bigalloc");
			return -EOPNOTSUPP;
		}

280
		err = mnt_want_write_file(filp);
281 282 283
		if (err)
			return err;

284
		err = ext4_group_add(sb, &input);
285 286 287 288 289
		if (EXT4_SB(sb)->s_journal) {
			jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
			err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
			jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
		}
290 291
		if (err == 0)
			err = err2;
292
		mnt_drop_write(filp->f_path.mnt);
293
		ext4_resize_end(sb);
294 295 296 297

		return err;
	}

298
	case EXT4_IOC_MIGRATE:
299 300
	{
		int err;
301
		if (!inode_owner_or_capable(inode))
302 303
			return -EACCES;

304
		err = mnt_want_write_file(filp);
305 306 307 308 309 310 311 312 313 314 315 316 317 318
		if (err)
			return err;
		/*
		 * inode_mutex prevent write and truncate on the file.
		 * Read still goes through. We take i_data_sem in
		 * ext4_ext_swap_inode_data before we switch the
		 * inode format to prevent read.
		 */
		mutex_lock(&(inode->i_mutex));
		err = ext4_ext_migrate(inode);
		mutex_unlock(&(inode->i_mutex));
		mnt_drop_write(filp->f_path.mnt);
		return err;
	}
319

320 321 322
	case EXT4_IOC_ALLOC_DA_BLKS:
	{
		int err;
323
		if (!inode_owner_or_capable(inode))
324 325
			return -EACCES;

326
		err = mnt_want_write_file(filp);
327 328 329 330 331 332 333
		if (err)
			return err;
		err = ext4_alloc_da_blocks(inode);
		mnt_drop_write(filp->f_path.mnt);
		return err;
	}

334 335
	case FITRIM:
	{
336
		struct request_queue *q = bdev_get_queue(sb->s_bdev);
337 338 339 340 341 342
		struct fstrim_range range;
		int ret = 0;

		if (!capable(CAP_SYS_ADMIN))
			return -EPERM;

343 344 345
		if (!blk_queue_discard(q))
			return -EOPNOTSUPP;

346 347 348 349 350 351 352
		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
			       EXT4_FEATURE_RO_COMPAT_BIGALLOC)) {
			ext4_msg(sb, KERN_ERR,
				 "FITRIM not supported with bigalloc");
			return -EOPNOTSUPP;
		}

353
		if (copy_from_user(&range, (struct fstrim_range __user *)arg,
354 355 356
		    sizeof(range)))
			return -EFAULT;

357 358
		range.minlen = max((unsigned int)range.minlen,
				   q->limits.discard_granularity);
359 360 361 362
		ret = ext4_trim_fs(sb, &range);
		if (ret < 0)
			return ret;

363
		if (copy_to_user((struct fstrim_range __user *)arg, &range,
364 365 366 367 368 369
		    sizeof(range)))
			return -EFAULT;

		return 0;
	}

370 371 372 373 374 375
	default:
		return -ENOTTY;
	}
}

#ifdef CONFIG_COMPAT
376
long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
377 378 379
{
	/* These are just misnamed, they actually get/put from/to user an int */
	switch (cmd) {
380 381
	case EXT4_IOC32_GETFLAGS:
		cmd = EXT4_IOC_GETFLAGS;
382
		break;
383 384
	case EXT4_IOC32_SETFLAGS:
		cmd = EXT4_IOC_SETFLAGS;
385
		break;
386 387
	case EXT4_IOC32_GETVERSION:
		cmd = EXT4_IOC_GETVERSION;
388
		break;
389 390
	case EXT4_IOC32_SETVERSION:
		cmd = EXT4_IOC_SETVERSION;
391
		break;
392 393
	case EXT4_IOC32_GROUP_EXTEND:
		cmd = EXT4_IOC_GROUP_EXTEND;
394
		break;
395 396
	case EXT4_IOC32_GETVERSION_OLD:
		cmd = EXT4_IOC_GETVERSION_OLD;
397
		break;
398 399
	case EXT4_IOC32_SETVERSION_OLD:
		cmd = EXT4_IOC_SETVERSION_OLD;
400
		break;
401 402
	case EXT4_IOC32_GETRSVSZ:
		cmd = EXT4_IOC_GETRSVSZ;
403
		break;
404 405
	case EXT4_IOC32_SETRSVSZ:
		cmd = EXT4_IOC_SETRSVSZ;
406
		break;
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	case EXT4_IOC32_GROUP_ADD: {
		struct compat_ext4_new_group_input __user *uinput;
		struct ext4_new_group_input input;
		mm_segment_t old_fs;
		int err;

		uinput = compat_ptr(arg);
		err = get_user(input.group, &uinput->group);
		err |= get_user(input.block_bitmap, &uinput->block_bitmap);
		err |= get_user(input.inode_bitmap, &uinput->inode_bitmap);
		err |= get_user(input.inode_table, &uinput->inode_table);
		err |= get_user(input.blocks_count, &uinput->blocks_count);
		err |= get_user(input.reserved_blocks,
				&uinput->reserved_blocks);
		if (err)
			return -EFAULT;
		old_fs = get_fs();
		set_fs(KERNEL_DS);
		err = ext4_ioctl(file, EXT4_IOC_GROUP_ADD,
				 (unsigned long) &input);
		set_fs(old_fs);
		return err;
	}
430
	case EXT4_IOC_MOVE_EXT:
T
Tao Ma 已提交
431
	case FITRIM:
432
		break;
433 434 435
	default:
		return -ENOIOCTLCMD;
	}
A
Andi Kleen 已提交
436
	return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
437 438
}
#endif