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 31
	case EXT4_IOC_GETFLAGS:
		flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
32
		return put_user(flags, (int __user *) arg);
33
	case EXT4_IOC_SETFLAGS: {
34 35
		handle_t *handle = NULL;
		int err;
36
		struct ext4_iloc iloc;
37 38 39 40 41 42 43 44 45 46 47 48 49
		unsigned int oldflags;
		unsigned int jflag;

		if (IS_RDONLY(inode))
			return -EROFS;

		if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
			return -EACCES;

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

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

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

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

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

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


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

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

98
		ext4_set_inode_flags(inode);
99 100
		inode->i_ctime = CURRENT_TIME_SEC;

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

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

		if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
			return -EPERM;
		if (IS_RDONLY(inode))
			return -EROFS;
		if (get_user(generation, (int __user *) arg))
			return -EFAULT;

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

		if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
			return -EACCES;

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

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

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

		if (ei->i_block_alloc_info){
201
			struct ext4_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
202 203 204 205 206
			rsv->rsv_goal_size = rsv_window_size;
		}
		mutex_unlock(&ei->truncate_mutex);
		return 0;
	}
207 208
	case EXT4_IOC_GROUP_EXTEND: {
		ext4_fsblk_t n_blocks_count;
209 210 211 212 213 214 215 216 217 218 219 220
		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;

221
		err = ext4_group_extend(sb, EXT4_SB(sb)->s_es, n_blocks_count);
222 223 224
		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);
225 226 227

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

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

		if (IS_RDONLY(inode))
			return -EROFS;

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

243
		err = ext4_group_add(sb, &input);
244 245 246
		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);
247 248 249 250 251 252 253 254 255 256

		return err;
	}

	default:
		return -ENOTTY;
	}
}

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

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