2 * linux/fs/ext3/ioctl.c
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)
10 #include <linux/mount.h>
11 #include <linux/compat.h>
12 #include <asm/uaccess.h>
15 long ext3_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
17 struct inode
*inode
= file_inode(filp
);
18 struct ext3_inode_info
*ei
= EXT3_I(inode
);
20 unsigned short rsv_window_size
;
22 ext3_debug ("cmd = %u, arg = %lu\n", cmd
, arg
);
25 case EXT3_IOC_GETFLAGS
:
26 ext3_get_inode_flags(ei
);
27 flags
= ei
->i_flags
& EXT3_FL_USER_VISIBLE
;
28 return put_user(flags
, (int __user
*) arg
);
29 case EXT3_IOC_SETFLAGS
: {
30 handle_t
*handle
= NULL
;
32 struct ext3_iloc iloc
;
33 unsigned int oldflags
;
36 if (!inode_owner_or_capable(inode
))
39 if (get_user(flags
, (int __user
*) arg
))
42 err
= mnt_want_write_file(filp
);
46 flags
= ext3_mask_flags(inode
->i_mode
, flags
);
48 mutex_lock(&inode
->i_mutex
);
50 /* Is it quota file? Do not allow user to mess with it */
52 if (IS_NOQUOTA(inode
))
55 oldflags
= ei
->i_flags
;
57 /* The JOURNAL_DATA flag is modifiable only by root */
58 jflag
= flags
& EXT3_JOURNAL_DATA_FL
;
61 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
62 * the relevant capability.
64 * This test looks nicer. Thanks to Pauline Middelink
66 if ((flags
^ oldflags
) & (EXT3_APPEND_FL
| EXT3_IMMUTABLE_FL
)) {
67 if (!capable(CAP_LINUX_IMMUTABLE
))
72 * The JOURNAL_DATA flag can only be changed by
73 * the relevant capability.
75 if ((jflag
^ oldflags
) & (EXT3_JOURNAL_DATA_FL
)) {
76 if (!capable(CAP_SYS_RESOURCE
))
80 handle
= ext3_journal_start(inode
, 1);
82 err
= PTR_ERR(handle
);
87 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
91 flags
= flags
& EXT3_FL_USER_MODIFIABLE
;
92 flags
|= oldflags
& ~EXT3_FL_USER_MODIFIABLE
;
95 ext3_set_inode_flags(inode
);
96 inode
->i_ctime
= CURRENT_TIME_SEC
;
98 err
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
100 ext3_journal_stop(handle
);
104 if ((jflag
^ oldflags
) & (EXT3_JOURNAL_DATA_FL
))
105 err
= ext3_change_inode_journal_flag(inode
, jflag
);
107 mutex_unlock(&inode
->i_mutex
);
108 mnt_drop_write_file(filp
);
111 case EXT3_IOC_GETVERSION
:
112 case EXT3_IOC_GETVERSION_OLD
:
113 return put_user(inode
->i_generation
, (int __user
*) arg
);
114 case EXT3_IOC_SETVERSION
:
115 case EXT3_IOC_SETVERSION_OLD
: {
117 struct ext3_iloc iloc
;
121 if (!inode_owner_or_capable(inode
))
124 err
= mnt_want_write_file(filp
);
127 if (get_user(generation
, (int __user
*) arg
)) {
132 mutex_lock(&inode
->i_mutex
);
133 handle
= ext3_journal_start(inode
, 1);
134 if (IS_ERR(handle
)) {
135 err
= PTR_ERR(handle
);
138 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
140 inode
->i_ctime
= CURRENT_TIME_SEC
;
141 inode
->i_generation
= generation
;
142 err
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
144 ext3_journal_stop(handle
);
147 mutex_unlock(&inode
->i_mutex
);
149 mnt_drop_write_file(filp
);
152 case EXT3_IOC_GETRSVSZ
:
153 if (test_opt(inode
->i_sb
, RESERVATION
)
154 && S_ISREG(inode
->i_mode
)
155 && ei
->i_block_alloc_info
) {
156 rsv_window_size
= ei
->i_block_alloc_info
->rsv_window_node
.rsv_goal_size
;
157 return put_user(rsv_window_size
, (int __user
*)arg
);
160 case EXT3_IOC_SETRSVSZ
: {
163 if (!test_opt(inode
->i_sb
, RESERVATION
) ||!S_ISREG(inode
->i_mode
))
166 err
= mnt_want_write_file(filp
);
170 if (!inode_owner_or_capable(inode
)) {
175 if (get_user(rsv_window_size
, (int __user
*)arg
)) {
180 if (rsv_window_size
> EXT3_MAX_RESERVE_BLOCKS
)
181 rsv_window_size
= EXT3_MAX_RESERVE_BLOCKS
;
184 * need to allocate reservation structure for this inode
185 * before set the window size
187 mutex_lock(&ei
->truncate_mutex
);
188 if (!ei
->i_block_alloc_info
)
189 ext3_init_block_alloc_info(inode
);
191 if (ei
->i_block_alloc_info
){
192 struct ext3_reserve_window_node
*rsv
= &ei
->i_block_alloc_info
->rsv_window_node
;
193 rsv
->rsv_goal_size
= rsv_window_size
;
195 mutex_unlock(&ei
->truncate_mutex
);
197 mnt_drop_write_file(filp
);
200 case EXT3_IOC_GROUP_EXTEND
: {
201 ext3_fsblk_t n_blocks_count
;
202 struct super_block
*sb
= inode
->i_sb
;
205 if (!capable(CAP_SYS_RESOURCE
))
208 err
= mnt_want_write_file(filp
);
212 if (get_user(n_blocks_count
, (__u32 __user
*)arg
)) {
214 goto group_extend_out
;
216 err
= ext3_group_extend(sb
, EXT3_SB(sb
)->s_es
, n_blocks_count
);
217 journal_lock_updates(EXT3_SB(sb
)->s_journal
);
218 err2
= journal_flush(EXT3_SB(sb
)->s_journal
);
219 journal_unlock_updates(EXT3_SB(sb
)->s_journal
);
223 mnt_drop_write_file(filp
);
226 case EXT3_IOC_GROUP_ADD
: {
227 struct ext3_new_group_data input
;
228 struct super_block
*sb
= inode
->i_sb
;
231 if (!capable(CAP_SYS_RESOURCE
))
234 err
= mnt_want_write_file(filp
);
238 if (copy_from_user(&input
, (struct ext3_new_group_input __user
*)arg
,
244 err
= ext3_group_add(sb
, &input
);
245 journal_lock_updates(EXT3_SB(sb
)->s_journal
);
246 err2
= journal_flush(EXT3_SB(sb
)->s_journal
);
247 journal_unlock_updates(EXT3_SB(sb
)->s_journal
);
251 mnt_drop_write_file(filp
);
256 struct super_block
*sb
= inode
->i_sb
;
257 struct fstrim_range range
;
260 if (!capable(CAP_SYS_ADMIN
))
263 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
267 ret
= ext3_trim_fs(sb
, &range
);
271 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
284 long ext3_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
286 /* These are just misnamed, they actually get/put from/to user an int */
288 case EXT3_IOC32_GETFLAGS
:
289 cmd
= EXT3_IOC_GETFLAGS
;
291 case EXT3_IOC32_SETFLAGS
:
292 cmd
= EXT3_IOC_SETFLAGS
;
294 case EXT3_IOC32_GETVERSION
:
295 cmd
= EXT3_IOC_GETVERSION
;
297 case EXT3_IOC32_SETVERSION
:
298 cmd
= EXT3_IOC_SETVERSION
;
300 case EXT3_IOC32_GROUP_EXTEND
:
301 cmd
= EXT3_IOC_GROUP_EXTEND
;
303 case EXT3_IOC32_GETVERSION_OLD
:
304 cmd
= EXT3_IOC_GETVERSION_OLD
;
306 case EXT3_IOC32_SETVERSION_OLD
:
307 cmd
= EXT3_IOC_SETVERSION_OLD
;
309 #ifdef CONFIG_JBD_DEBUG
310 case EXT3_IOC32_WAIT_FOR_READONLY
:
311 cmd
= EXT3_IOC_WAIT_FOR_READONLY
;
314 case EXT3_IOC32_GETRSVSZ
:
315 cmd
= EXT3_IOC_GETRSVSZ
;
317 case EXT3_IOC32_SETRSVSZ
:
318 cmd
= EXT3_IOC_SETRSVSZ
;
320 case EXT3_IOC_GROUP_ADD
:
325 return ext3_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));