ioctl.c 7.4 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 15
#include <linux/capability.h>
#include <linux/time.h>
#include <linux/compat.h>
#include <linux/smp_lock.h>
16
#include <linux/mount.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
		if (!S_ISDIR(inode->i_mode))
52
			flags &= ~EXT4_DIRSYNC_FL;
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
		handle = ext4_journal_start(inode, 1);
97
		if (IS_ERR(handle)) {
98 99
			err = PTR_ERR(handle);
			goto flags_out;
100 101
		}
		if (IS_SYNC(inode))
102
			ext4_handle_sync(handle);
103
		err = ext4_reserve_inode_write(handle, inode, &iloc);
104 105 106
		if (err)
			goto flags_err;

107 108
		flags = flags & EXT4_FL_USER_MODIFIABLE;
		flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
109 110
		ei->i_flags = flags;

111
		ext4_set_inode_flags(inode);
K
Kalpak Shah 已提交
112
		inode->i_ctime = ext4_current_time(inode);
113

114
		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
115
flags_err:
116
		ext4_journal_stop(handle);
117 118
		if (err)
			goto flags_out;
119

120 121
		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
			err = ext4_change_inode_journal_flag(inode, jflag);
122 123 124 125
		if (err)
			goto flags_out;
		if (migrate)
			err = ext4_ext_migrate(inode);
126
flags_out:
127
		mutex_unlock(&inode->i_mutex);
128
		mnt_drop_write(filp->f_path.mnt);
129 130
		return err;
	}
131 132
	case EXT4_IOC_GETVERSION:
	case EXT4_IOC_GETVERSION_OLD:
133
		return put_user(inode->i_generation, (int __user *) arg);
134 135
	case EXT4_IOC_SETVERSION:
	case EXT4_IOC_SETVERSION_OLD: {
136
		handle_t *handle;
137
		struct ext4_iloc iloc;
138 139 140
		__u32 generation;
		int err;

141
		if (!is_owner_or_cap(inode))
142
			return -EPERM;
143 144 145 146 147 148 149 150

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

152
		handle = ext4_journal_start(inode, 1);
153 154 155 156
		if (IS_ERR(handle)) {
			err = PTR_ERR(handle);
			goto setversion_out;
		}
157
		err = ext4_reserve_inode_write(handle, inode, &iloc);
158
		if (err == 0) {
K
Kalpak Shah 已提交
159
			inode->i_ctime = ext4_current_time(inode);
160
			inode->i_generation = generation;
161
			err = ext4_mark_iloc_dirty(handle, inode, &iloc);
162
		}
163
		ext4_journal_stop(handle);
164 165
setversion_out:
		mnt_drop_write(filp->f_path.mnt);
166 167
		return err;
	}
168
#ifdef CONFIG_JBD2_DEBUG
169
	case EXT4_IOC_WAIT_FOR_READONLY:
170 171 172 173 174 175 176 177 178 179 180 181 182
		/*
		 * 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);
183 184
			add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
			if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
185 186 187
				schedule();
				ret = 1;
			}
188
			remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
189 190 191
			return ret;
		}
#endif
192 193
	case EXT4_IOC_GROUP_EXTEND: {
		ext4_fsblk_t n_blocks_count;
194
		struct super_block *sb = inode->i_sb;
195
		int err, err2;
196 197 198 199 200 201 202

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

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

203 204 205 206
		err = mnt_want_write(filp->f_path.mnt);
		if (err)
			return err;

207
		err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
208
		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
209
		err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
210
		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
211 212
		if (err == 0)
			err = err2;
213
		mnt_drop_write(filp->f_path.mnt);
214 215 216

		return err;
	}
217 218
	case EXT4_IOC_GROUP_ADD: {
		struct ext4_new_group_data input;
219
		struct super_block *sb = inode->i_sb;
220
		int err, err2;
221 222 223 224

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

225
		if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
226 227 228
				sizeof(input)))
			return -EFAULT;

229 230 231 232
		err = mnt_want_write(filp->f_path.mnt);
		if (err)
			return err;

233
		err = ext4_group_add(sb, &input);
234
		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
235
		err2 = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
236
		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
237 238
		if (err == 0)
			err = err2;
239
		mnt_drop_write(filp->f_path.mnt);
240 241 242 243

		return err;
	}

244
	case EXT4_IOC_MIGRATE:
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	{
		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;
	}
265

266 267 268 269 270 271
	default:
		return -ENOTTY;
	}
}

#ifdef CONFIG_COMPAT
272
long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
273 274 275
{
	/* These are just misnamed, they actually get/put from/to user an int */
	switch (cmd) {
276 277
	case EXT4_IOC32_GETFLAGS:
		cmd = EXT4_IOC_GETFLAGS;
278
		break;
279 280
	case EXT4_IOC32_SETFLAGS:
		cmd = EXT4_IOC_SETFLAGS;
281
		break;
282 283
	case EXT4_IOC32_GETVERSION:
		cmd = EXT4_IOC_GETVERSION;
284
		break;
285 286
	case EXT4_IOC32_SETVERSION:
		cmd = EXT4_IOC_SETVERSION;
287
		break;
288 289
	case EXT4_IOC32_GROUP_EXTEND:
		cmd = EXT4_IOC_GROUP_EXTEND;
290
		break;
291 292
	case EXT4_IOC32_GETVERSION_OLD:
		cmd = EXT4_IOC_GETVERSION_OLD;
293
		break;
294 295
	case EXT4_IOC32_SETVERSION_OLD:
		cmd = EXT4_IOC_SETVERSION_OLD;
296
		break;
297
#ifdef CONFIG_JBD2_DEBUG
298 299
	case EXT4_IOC32_WAIT_FOR_READONLY:
		cmd = EXT4_IOC_WAIT_FOR_READONLY;
300 301
		break;
#endif
302 303
	case EXT4_IOC32_GETRSVSZ:
		cmd = EXT4_IOC_GETRSVSZ;
304
		break;
305 306
	case EXT4_IOC32_SETRSVSZ:
		cmd = EXT4_IOC_SETRSVSZ;
307
		break;
308
	case EXT4_IOC_GROUP_ADD:
309 310 311 312
		break;
	default:
		return -ENOIOCTLCMD;
	}
A
Andi Kleen 已提交
313
	return ext4_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
314 315
}
#endif