ioctl.c 8.8 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 ext4_inode_info *ei = EXT4_I(inode);
25 26
	unsigned int flags;

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

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

41
		if (!is_owner_or_cap(inode))
42 43 44 45 46
			return -EACCES;

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

47 48 49 50
		err = mnt_want_write(filp->f_path.mnt);
		if (err)
			return err;

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

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

59 60 61
		oldflags = ei->i_flags;

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

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

		/*
		 * The JOURNAL_DATA flag can only be changed by
		 * the relevant capability.
		 */
79
		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
80 81
			if (!capable(CAP_SYS_RESOURCE))
				goto flags_out;
82
		}
83 84 85 86 87 88 89 90 91 92 93
		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;
		}
94

95 96 97 98 99 100 101 102 103
		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);

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

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

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

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

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

149
		if (!is_owner_or_cap(inode))
150
			return -EPERM;
151 152 153 154 155 156 157 158

		err = mnt_want_write(filp->f_path.mnt);
		if (err)
			return err;
		if (get_user(generation, (int __user *) arg)) {
			err = -EFAULT;
			goto setversion_out;
		}
159

160
		handle = ext4_journal_start(inode, 1);
161 162 163 164
		if (IS_ERR(handle)) {
			err = PTR_ERR(handle);
			goto setversion_out;
		}
165
		err = ext4_reserve_inode_write(handle, inode, &iloc);
166
		if (err == 0) {
K
Kalpak Shah 已提交
167
			inode->i_ctime = ext4_current_time(inode);
168
			inode->i_generation = generation;
169
			err = ext4_mark_iloc_dirty(handle, inode, &iloc);
170
		}
171
		ext4_journal_stop(handle);
172 173
setversion_out:
		mnt_drop_write(filp->f_path.mnt);
174 175
		return err;
	}
176
#ifdef CONFIG_JBD2_DEBUG
177
	case EXT4_IOC_WAIT_FOR_READONLY:
178 179 180 181 182 183 184 185 186 187 188 189 190
		/*
		 * This is racy - by the time we're woken up and running,
		 * the superblock could be released.  And the module could
		 * have been unloaded.  So sue me.
		 *
		 * Returns 1 if it slept, else zero.
		 */
		{
			struct super_block *sb = inode->i_sb;
			DECLARE_WAITQUEUE(wait, current);
			int ret = 0;

			set_current_state(TASK_INTERRUPTIBLE);
191 192
			add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
			if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
193 194 195
				schedule();
				ret = 1;
			}
196
			remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
197 198 199
			return ret;
		}
#endif
200 201
	case EXT4_IOC_GROUP_EXTEND: {
		ext4_fsblk_t n_blocks_count;
202
		struct super_block *sb = inode->i_sb;
203
		int err, err2=0;
204 205 206 207 208 209 210

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

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

211 212 213 214
		err = mnt_want_write(filp->f_path.mnt);
		if (err)
			return err;

215
		err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
216 217 218 219 220
		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);
		}
221 222
		if (err == 0)
			err = err2;
223
		mnt_drop_write(filp->f_path.mnt);
224 225 226

		return err;
	}
227 228 229 230 231 232

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

233 234 235 236
		if (!(filp->f_mode & FMODE_READ) ||
		    !(filp->f_mode & FMODE_WRITE))
			return -EBADF;

237 238 239
		if (copy_from_user(&me,
			(struct move_extent __user *)arg, sizeof(me)))
			return -EFAULT;
240
		me.moved_len = 0;
241 242 243 244 245

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

246 247 248
		if (!(donor_filp->f_mode & FMODE_WRITE)) {
			err = -EBADF;
			goto mext_out;
249 250
		}

251 252 253 254
		err = mnt_want_write(filp->f_path.mnt);
		if (err)
			goto mext_out;

255 256
		err = ext4_move_extents(filp, donor_filp, me.orig_start,
					me.donor_start, me.len, &me.moved_len);
257 258 259
		mnt_drop_write(filp->f_path.mnt);
		if (me.moved_len > 0)
			file_remove_suid(donor_filp);
260

261 262
		if (copy_to_user((struct move_extent __user *)arg, 
				 &me, sizeof(me)))
263 264 265
			err = -EFAULT;
mext_out:
		fput(donor_filp);
266 267 268
		return err;
	}

269 270
	case EXT4_IOC_GROUP_ADD: {
		struct ext4_new_group_data input;
271
		struct super_block *sb = inode->i_sb;
272
		int err, err2=0;
273 274 275 276

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

277
		if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
278 279 280
				sizeof(input)))
			return -EFAULT;

281 282 283 284
		err = mnt_want_write(filp->f_path.mnt);
		if (err)
			return err;

285
		err = ext4_group_add(sb, &input);
286 287 288 289 290
		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);
		}
291 292
		if (err == 0)
			err = err2;
293
		mnt_drop_write(filp->f_path.mnt);
294 295 296 297

		return err;
	}

298
	case EXT4_IOC_MIGRATE:
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	{
		int err;
		if (!is_owner_or_cap(inode))
			return -EACCES;

		err = mnt_want_write(filp->f_path.mnt);
		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 323 324 325 326 327 328 329 330 331 332 333
	case EXT4_IOC_ALLOC_DA_BLKS:
	{
		int err;
		if (!is_owner_or_cap(inode))
			return -EACCES;

		err = mnt_want_write(filp->f_path.mnt);
		if (err)
			return err;
		err = ext4_alloc_da_blocks(inode);
		mnt_drop_write(filp->f_path.mnt);
		return err;
	}

334 335 336 337 338 339
	default:
		return -ENOTTY;
	}
}

#ifdef CONFIG_COMPAT
340
long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
341 342 343
{
	/* These are just misnamed, they actually get/put from/to user an int */
	switch (cmd) {
344 345
	case EXT4_IOC32_GETFLAGS:
		cmd = EXT4_IOC_GETFLAGS;
346
		break;
347 348
	case EXT4_IOC32_SETFLAGS:
		cmd = EXT4_IOC_SETFLAGS;
349
		break;
350 351
	case EXT4_IOC32_GETVERSION:
		cmd = EXT4_IOC_GETVERSION;
352
		break;
353 354
	case EXT4_IOC32_SETVERSION:
		cmd = EXT4_IOC_SETVERSION;
355
		break;
356 357
	case EXT4_IOC32_GROUP_EXTEND:
		cmd = EXT4_IOC_GROUP_EXTEND;
358
		break;
359 360
	case EXT4_IOC32_GETVERSION_OLD:
		cmd = EXT4_IOC_GETVERSION_OLD;
361
		break;
362 363
	case EXT4_IOC32_SETVERSION_OLD:
		cmd = EXT4_IOC_SETVERSION_OLD;
364
		break;
365
#ifdef CONFIG_JBD2_DEBUG
366 367
	case EXT4_IOC32_WAIT_FOR_READONLY:
		cmd = EXT4_IOC_WAIT_FOR_READONLY;
368 369
		break;
#endif
370 371
	case EXT4_IOC32_GETRSVSZ:
		cmd = EXT4_IOC_GETRSVSZ;
372
		break;
373 374
	case EXT4_IOC32_SETRSVSZ:
		cmd = EXT4_IOC_SETRSVSZ;
375
		break;
376
	case EXT4_IOC_GROUP_ADD:
377
		break;
378 379
	case EXT4_IOC_MOVE_EXT:
		break;
380 381 382
	default:
		return -ENOIOCTLCMD;
	}
A
Andi Kleen 已提交
383
	return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
384 385
}
#endif