ioctl.c 7.3 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
#include <linux/capability.h>
13
#include <linux/ext4_fs.h>
14
#include <linux/ext4_jbd2.h>
15 16 17 18 19
#include <linux/time.h>
#include <linux/compat.h>
#include <linux/smp_lock.h>
#include <asm/uaccess.h>

20
int ext4_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
21 22
		unsigned long arg)
{
23
	struct ext4_inode_info *ei = EXT4_I(inode);
24 25 26
	unsigned int flags;
	unsigned short rsv_window_size;

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 36
		handle_t *handle = NULL;
		int err;
37
		struct ext4_iloc iloc;
38 39 40 41 42 43
		unsigned int oldflags;
		unsigned int jflag;

		if (IS_RDONLY(inode))
			return -EROFS;

44
		if (!is_owner_or_cap(inode))
45 46 47 48 49 50
			return -EACCES;

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

		if (!S_ISDIR(inode->i_mode))
51
			flags &= ~EXT4_DIRSYNC_FL;
52 53 54 55 56

		mutex_lock(&inode->i_mutex);
		oldflags = ei->i_flags;

		/* The JOURNAL_DATA flag is modifiable only by root */
57
		jflag = flags & EXT4_JOURNAL_DATA_FL;
58 59 60 61 62 63 64

		/*
		 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
		 * the relevant capability.
		 *
		 * This test looks nicer. Thanks to Pauline Middelink
		 */
65
		if ((flags ^ oldflags) & (EXT4_APPEND_FL | EXT4_IMMUTABLE_FL)) {
66 67 68 69 70 71 72 73 74 75
			if (!capable(CAP_LINUX_IMMUTABLE)) {
				mutex_unlock(&inode->i_mutex);
				return -EPERM;
			}
		}

		/*
		 * The JOURNAL_DATA flag can only be changed by
		 * the relevant capability.
		 */
76
		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
77 78 79 80 81 82 83
			if (!capable(CAP_SYS_RESOURCE)) {
				mutex_unlock(&inode->i_mutex);
				return -EPERM;
			}
		}


84
		handle = ext4_journal_start(inode, 1);
85 86 87 88 89 90
		if (IS_ERR(handle)) {
			mutex_unlock(&inode->i_mutex);
			return PTR_ERR(handle);
		}
		if (IS_SYNC(inode))
			handle->h_sync = 1;
91
		err = ext4_reserve_inode_write(handle, inode, &iloc);
92 93 94
		if (err)
			goto flags_err;

95 96
		flags = flags & EXT4_FL_USER_MODIFIABLE;
		flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
97 98
		ei->i_flags = flags;

99
		ext4_set_inode_flags(inode);
K
Kalpak Shah 已提交
100
		inode->i_ctime = ext4_current_time(inode);
101

102
		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
103
flags_err:
104
		ext4_journal_stop(handle);
105 106 107 108 109
		if (err) {
			mutex_unlock(&inode->i_mutex);
			return err;
		}

110 111
		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL))
			err = ext4_change_inode_journal_flag(inode, jflag);
112 113 114
		mutex_unlock(&inode->i_mutex);
		return err;
	}
115 116
	case EXT4_IOC_GETVERSION:
	case EXT4_IOC_GETVERSION_OLD:
117
		return put_user(inode->i_generation, (int __user *) arg);
118 119
	case EXT4_IOC_SETVERSION:
	case EXT4_IOC_SETVERSION_OLD: {
120
		handle_t *handle;
121
		struct ext4_iloc iloc;
122 123 124
		__u32 generation;
		int err;

125
		if (!is_owner_or_cap(inode))
126 127 128 129 130 131
			return -EPERM;
		if (IS_RDONLY(inode))
			return -EROFS;
		if (get_user(generation, (int __user *) arg))
			return -EFAULT;

132
		handle = ext4_journal_start(inode, 1);
133 134
		if (IS_ERR(handle))
			return PTR_ERR(handle);
135
		err = ext4_reserve_inode_write(handle, inode, &iloc);
136
		if (err == 0) {
K
Kalpak Shah 已提交
137
			inode->i_ctime = ext4_current_time(inode);
138
			inode->i_generation = generation;
139
			err = ext4_mark_iloc_dirty(handle, inode, &iloc);
140
		}
141
		ext4_journal_stop(handle);
142 143
		return err;
	}
144
#ifdef CONFIG_JBD2_DEBUG
145
	case EXT4_IOC_WAIT_FOR_READONLY:
146 147 148 149 150 151 152 153 154 155 156 157 158
		/*
		 * 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);
159 160
			add_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
			if (timer_pending(&EXT4_SB(sb)->turn_ro_timer)) {
161 162 163
				schedule();
				ret = 1;
			}
164
			remove_wait_queue(&EXT4_SB(sb)->ro_wait_queue, &wait);
165 166 167
			return ret;
		}
#endif
168
	case EXT4_IOC_GETRSVSZ:
169 170 171 172 173 174 175
		if (test_opt(inode->i_sb, RESERVATION)
			&& S_ISREG(inode->i_mode)
			&& ei->i_block_alloc_info) {
			rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
			return put_user(rsv_window_size, (int __user *)arg);
		}
		return -ENOTTY;
176
	case EXT4_IOC_SETRSVSZ: {
177 178 179 180 181 182 183

		if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
			return -ENOTTY;

		if (IS_RDONLY(inode))
			return -EROFS;

184
		if (!is_owner_or_cap(inode))
185 186 187 188 189
			return -EACCES;

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

190 191
		if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
			rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
192 193 194 195 196 197 198

		/*
		 * need to allocate reservation structure for this inode
		 * before set the window size
		 */
		mutex_lock(&ei->truncate_mutex);
		if (!ei->i_block_alloc_info)
199
			ext4_init_block_alloc_info(inode);
200 201

		if (ei->i_block_alloc_info){
202
			struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
203 204 205 206 207
			rsv->rsv_goal_size = rsv_window_size;
		}
		mutex_unlock(&ei->truncate_mutex);
		return 0;
	}
208 209
	case EXT4_IOC_GROUP_EXTEND: {
		ext4_fsblk_t n_blocks_count;
210 211 212 213 214 215 216 217 218 219 220 221
		struct super_block *sb = inode->i_sb;
		int err;

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

		if (IS_RDONLY(inode))
			return -EROFS;

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

222
		err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
223 224 225
		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
		jbd2_journal_flush(EXT4_SB(sb)->s_journal);
		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
226 227 228

		return err;
	}
229 230
	case EXT4_IOC_GROUP_ADD: {
		struct ext4_new_group_data input;
231 232 233 234 235 236 237 238 239
		struct super_block *sb = inode->i_sb;
		int err;

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

		if (IS_RDONLY(inode))
			return -EROFS;

240
		if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
241 242 243
				sizeof(input)))
			return -EFAULT;

244
		err = ext4_group_add(sb, &input);
245 246 247
		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
		jbd2_journal_flush(EXT4_SB(sb)->s_journal);
		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
248 249 250 251 252 253 254 255 256 257

		return err;
	}

	default:
		return -ENOTTY;
	}
}

#ifdef CONFIG_COMPAT
258
long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
259
{
260
	struct inode *inode = file->f_path.dentry->d_inode;
261 262 263 264
	int ret;

	/* These are just misnamed, they actually get/put from/to user an int */
	switch (cmd) {
265 266
	case EXT4_IOC32_GETFLAGS:
		cmd = EXT4_IOC_GETFLAGS;
267
		break;
268 269
	case EXT4_IOC32_SETFLAGS:
		cmd = EXT4_IOC_SETFLAGS;
270
		break;
271 272
	case EXT4_IOC32_GETVERSION:
		cmd = EXT4_IOC_GETVERSION;
273
		break;
274 275
	case EXT4_IOC32_SETVERSION:
		cmd = EXT4_IOC_SETVERSION;
276
		break;
277 278
	case EXT4_IOC32_GROUP_EXTEND:
		cmd = EXT4_IOC_GROUP_EXTEND;
279
		break;
280 281
	case EXT4_IOC32_GETVERSION_OLD:
		cmd = EXT4_IOC_GETVERSION_OLD;
282
		break;
283 284
	case EXT4_IOC32_SETVERSION_OLD:
		cmd = EXT4_IOC_SETVERSION_OLD;
285
		break;
286
#ifdef CONFIG_JBD2_DEBUG
287 288
	case EXT4_IOC32_WAIT_FOR_READONLY:
		cmd = EXT4_IOC_WAIT_FOR_READONLY;
289 290
		break;
#endif
291 292
	case EXT4_IOC32_GETRSVSZ:
		cmd = EXT4_IOC_GETRSVSZ;
293
		break;
294 295
	case EXT4_IOC32_SETRSVSZ:
		cmd = EXT4_IOC_SETRSVSZ;
296
		break;
297
	case EXT4_IOC_GROUP_ADD:
298 299 300 301 302
		break;
	default:
		return -ENOIOCTLCMD;
	}
	lock_kernel();
303
	ret = ext4_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
304 305 306 307
	unlock_kernel();
	return ret;
}
#endif