blob: c2324d5fe4ac149a5672bad13134d4775c815100 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext2/ioctl.c
3 *
4 * Copyright (C) 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 */
9
10#include "ext2.h"
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080011#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/time.h>
13#include <linux/sched.h>
David Howellse322ff02006-08-29 19:06:20 +010014#include <linux/compat.h>
15#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/current.h>
17#include <asm/uaccess.h>
18
19
20int ext2_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
21 unsigned long arg)
22{
23 struct ext2_inode_info *ei = EXT2_I(inode);
24 unsigned int flags;
Martin J. Bligha686cd82007-10-16 23:30:46 -070025 unsigned short rsv_window_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
28
29 switch (cmd) {
30 case EXT2_IOC_GETFLAGS:
Jan Kara4f99ed62007-05-08 00:31:04 -070031 ext2_get_inode_flags(ei);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
33 return put_user(flags, (int __user *) arg);
34 case EXT2_IOC_SETFLAGS: {
35 unsigned int oldflags;
36
37 if (IS_RDONLY(inode))
38 return -EROFS;
39
Satyam Sharma3bd858a2007-07-17 15:00:08 +053040 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return -EACCES;
42
43 if (get_user(flags, (int __user *) arg))
44 return -EFAULT;
45
46 if (!S_ISDIR(inode->i_mode))
47 flags &= ~EXT2_DIRSYNC_FL;
48
Andrew Morton93f210d2006-12-06 20:37:33 -080049 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 oldflags = ei->i_flags;
51
52 /*
53 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
54 * the relevant capability.
55 *
56 * This test looks nicer. Thanks to Pauline Middelink
57 */
58 if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
Andrew Morton93f210d2006-12-06 20:37:33 -080059 if (!capable(CAP_LINUX_IMMUTABLE)) {
60 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return -EPERM;
Andrew Morton93f210d2006-12-06 20:37:33 -080062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64
65 flags = flags & EXT2_FL_USER_MODIFIABLE;
66 flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
67 ei->i_flags = flags;
Andrew Morton93f210d2006-12-06 20:37:33 -080068 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 ext2_set_inode_flags(inode);
71 inode->i_ctime = CURRENT_TIME_SEC;
72 mark_inode_dirty(inode);
73 return 0;
74 }
75 case EXT2_IOC_GETVERSION:
76 return put_user(inode->i_generation, (int __user *) arg);
77 case EXT2_IOC_SETVERSION:
Satyam Sharma3bd858a2007-07-17 15:00:08 +053078 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return -EPERM;
80 if (IS_RDONLY(inode))
81 return -EROFS;
82 if (get_user(inode->i_generation, (int __user *) arg))
83 return -EFAULT;
84 inode->i_ctime = CURRENT_TIME_SEC;
85 mark_inode_dirty(inode);
86 return 0;
Martin J. Bligha686cd82007-10-16 23:30:46 -070087 case EXT2_IOC_GETRSVSZ:
88 if (test_opt(inode->i_sb, RESERVATION)
89 && S_ISREG(inode->i_mode)
90 && ei->i_block_alloc_info) {
91 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
92 return put_user(rsv_window_size, (int __user *)arg);
93 }
94 return -ENOTTY;
95 case EXT2_IOC_SETRSVSZ: {
96
97 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
98 return -ENOTTY;
99
100 if (IS_RDONLY(inode))
101 return -EROFS;
102
103 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
104 return -EACCES;
105
106 if (get_user(rsv_window_size, (int __user *)arg))
107 return -EFAULT;
108
109 if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
110 rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
111
112 /*
113 * need to allocate reservation structure for this inode
114 * before set the window size
115 */
116 /*
117 * XXX What lock should protect the rsv_goal_size?
118 * Accessed in ext2_get_block only. ext3 uses i_truncate.
119 */
120 mutex_lock(&ei->truncate_mutex);
121 if (!ei->i_block_alloc_info)
122 ext2_init_block_alloc_info(inode);
123
124 if (ei->i_block_alloc_info){
125 struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
126 rsv->rsv_goal_size = rsv_window_size;
127 }
128 mutex_unlock(&ei->truncate_mutex);
129 return 0;
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 default:
132 return -ENOTTY;
133 }
134}
David Howellse322ff02006-08-29 19:06:20 +0100135
136#ifdef CONFIG_COMPAT
137long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
138{
Josef "Jeff" Sipekc29c6932006-12-08 02:36:37 -0800139 struct inode *inode = file->f_path.dentry->d_inode;
David Howellse322ff02006-08-29 19:06:20 +0100140 int ret;
141
142 /* These are just misnamed, they actually get/put from/to user an int */
143 switch (cmd) {
144 case EXT2_IOC32_GETFLAGS:
145 cmd = EXT2_IOC_GETFLAGS;
146 break;
147 case EXT2_IOC32_SETFLAGS:
148 cmd = EXT2_IOC_SETFLAGS;
149 break;
150 case EXT2_IOC32_GETVERSION:
151 cmd = EXT2_IOC_GETVERSION;
152 break;
153 case EXT2_IOC32_SETVERSION:
154 cmd = EXT2_IOC_SETVERSION;
155 break;
156 default:
157 return -ENOIOCTLCMD;
158 }
159 lock_kernel();
160 ret = ext2_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
161 unlock_kernel();
162 return ret;
163}
164#endif