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
#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

		mutex_lock(&inode->i_mutex);
54 55 56 57 58
		/* Is it quota file? Do not allow user to mess with it */
		if (IS_NOQUOTA(inode)) {
			mutex_unlock(&inode->i_mutex);
			return -EPERM;
		}
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 73 74 75 76 77 78 79 80
			if (!capable(CAP_LINUX_IMMUTABLE)) {
				mutex_unlock(&inode->i_mutex);
				return -EPERM;
			}
		}

		/*
		 * The JOURNAL_DATA flag can only be changed by
		 * the relevant capability.
		 */
81
		if ((jflag ^ oldflags) & (EXT4_JOURNAL_DATA_FL)) {
82 83 84 85 86 87 88
			if (!capable(CAP_SYS_RESOURCE)) {
				mutex_unlock(&inode->i_mutex);
				return -EPERM;
			}
		}


89
		handle = ext4_journal_start(inode, 1);
90 91 92 93 94 95
		if (IS_ERR(handle)) {
			mutex_unlock(&inode->i_mutex);
			return PTR_ERR(handle);
		}
		if (IS_SYNC(inode))
			handle->h_sync = 1;
96
		err = ext4_reserve_inode_write(handle, inode, &iloc);
97 98 99
		if (err)
			goto flags_err;

100 101
		flags = flags & EXT4_FL_USER_MODIFIABLE;
		flags |= oldflags & ~EXT4_FL_USER_MODIFIABLE;
102 103
		ei->i_flags = flags;

104
		ext4_set_inode_flags(inode);
K
Kalpak Shah 已提交
105
		inode->i_ctime = ext4_current_time(inode);
106

107
		err = ext4_mark_iloc_dirty(handle, inode, &iloc);
108
flags_err:
109
		ext4_journal_stop(handle);
110 111 112 113 114
		if (err) {
			mutex_unlock(&inode->i_mutex);
			return err;
		}

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

130
		if (!is_owner_or_cap(inode))
131 132 133 134 135 136
			return -EPERM;
		if (IS_RDONLY(inode))
			return -EROFS;
		if (get_user(generation, (int __user *) arg))
			return -EFAULT;

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

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

		if (IS_RDONLY(inode))
			return -EROFS;

189
		if (!is_owner_or_cap(inode))
190 191 192 193 194
			return -EACCES;

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

195 196
		if (rsv_window_size > EXT4_MAX_RESERVE_BLOCKS)
			rsv_window_size = EXT4_MAX_RESERVE_BLOCKS;
197 198 199 200 201

		/*
		 * need to allocate reservation structure for this inode
		 * before set the window size
		 */
202
		down_write(&ei->i_data_sem);
203
		if (!ei->i_block_alloc_info)
204
			ext4_init_block_alloc_info(inode);
205 206

		if (ei->i_block_alloc_info){
207
			struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
208 209
			rsv->rsv_goal_size = rsv_window_size;
		}
210
		up_write(&ei->i_data_sem);
211 212
		return 0;
	}
213 214
	case EXT4_IOC_GROUP_EXTEND: {
		ext4_fsblk_t n_blocks_count;
215 216 217 218 219 220 221 222 223 224 225 226
		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;

227
		err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
228 229 230
		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);
231 232 233

		return err;
	}
234 235
	case EXT4_IOC_GROUP_ADD: {
		struct ext4_new_group_data input;
236 237 238 239 240 241 242 243 244
		struct super_block *sb = inode->i_sb;
		int err;

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

		if (IS_RDONLY(inode))
			return -EROFS;

245
		if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
246 247 248
				sizeof(input)))
			return -EFAULT;

249
		err = ext4_group_add(sb, &input);
250 251 252
		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);
253 254 255 256 257 258 259 260 261 262

		return err;
	}

	default:
		return -ENOTTY;
	}
}

#ifdef CONFIG_COMPAT
263
long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
264
{
265
	struct inode *inode = file->f_path.dentry->d_inode;
266 267 268 269
	int ret;

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