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)
11 #include <linux/jbd.h>
12 #include <linux/capability.h>
13 #include <linux/ext3_fs.h>
14 #include <linux/ext3_jbd.h>
15 #include <linux/mount.h>
16 #include <linux/time.h>
17 #include <linux/compat.h>
18 #include <asm/uaccess.h>
20 long ext3_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
22 struct inode
*inode
= filp
->f_dentry
->d_inode
;
23 struct ext3_inode_info
*ei
= EXT3_I(inode
);
25 unsigned short rsv_window_size
;
27 ext3_debug ("cmd = %u, arg = %lu\n", cmd
, arg
);
30 case EXT3_IOC_GETFLAGS
:
31 ext3_get_inode_flags(ei
);
32 flags
= ei
->i_flags
& EXT3_FL_USER_VISIBLE
;
33 return put_user(flags
, (int __user
*) arg
);
34 case EXT3_IOC_SETFLAGS
: {
35 handle_t
*handle
= NULL
;
37 struct ext3_iloc iloc
;
38 unsigned int oldflags
;
41 if (!inode_owner_or_capable(inode
))
44 if (get_user(flags
, (int __user
*) arg
))
47 err
= mnt_want_write(filp
->f_path
.mnt
);
51 flags
= ext3_mask_flags(inode
->i_mode
, flags
);
53 mutex_lock(&inode
->i_mutex
);
55 /* Is it quota file? Do not allow user to mess with it */
57 if (IS_NOQUOTA(inode
))
60 oldflags
= ei
->i_flags
;
62 /* The JOURNAL_DATA flag is modifiable only by root */
63 jflag
= flags
& EXT3_JOURNAL_DATA_FL
;
66 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
67 * the relevant capability.
69 * This test looks nicer. Thanks to Pauline Middelink
71 if ((flags
^ oldflags
) & (EXT3_APPEND_FL
| EXT3_IMMUTABLE_FL
)) {
72 if (!capable(CAP_LINUX_IMMUTABLE
))
77 * The JOURNAL_DATA flag can only be changed by
78 * the relevant capability.
80 if ((jflag
^ oldflags
) & (EXT3_JOURNAL_DATA_FL
)) {
81 if (!capable(CAP_SYS_RESOURCE
))
85 handle
= ext3_journal_start(inode
, 1);
87 err
= PTR_ERR(handle
);
92 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
96 flags
= flags
& EXT3_FL_USER_MODIFIABLE
;
97 flags
|= oldflags
& ~EXT3_FL_USER_MODIFIABLE
;
100 ext3_set_inode_flags(inode
);
101 inode
->i_ctime
= CURRENT_TIME_SEC
;
103 err
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
105 ext3_journal_stop(handle
);
109 if ((jflag
^ oldflags
) & (EXT3_JOURNAL_DATA_FL
))
110 err
= ext3_change_inode_journal_flag(inode
, jflag
);
112 mutex_unlock(&inode
->i_mutex
);
113 mnt_drop_write(filp
->f_path
.mnt
);
116 case EXT3_IOC_GETVERSION
:
117 case EXT3_IOC_GETVERSION_OLD
:
118 return put_user(inode
->i_generation
, (int __user
*) arg
);
119 case EXT3_IOC_SETVERSION
:
120 case EXT3_IOC_SETVERSION_OLD
: {
122 struct ext3_iloc iloc
;
126 if (!inode_owner_or_capable(inode
))
129 err
= mnt_want_write(filp
->f_path
.mnt
);
132 if (get_user(generation
, (int __user
*) arg
)) {
137 handle
= ext3_journal_start(inode
, 1);
138 if (IS_ERR(handle
)) {
139 err
= PTR_ERR(handle
);
142 err
= ext3_reserve_inode_write(handle
, inode
, &iloc
);
144 inode
->i_ctime
= CURRENT_TIME_SEC
;
145 inode
->i_generation
= generation
;
146 err
= ext3_mark_iloc_dirty(handle
, inode
, &iloc
);
148 ext3_journal_stop(handle
);
150 mnt_drop_write(filp
->f_path
.mnt
);
153 case EXT3_IOC_GETRSVSZ
:
154 if (test_opt(inode
->i_sb
, RESERVATION
)
155 && S_ISREG(inode
->i_mode
)
156 && ei
->i_block_alloc_info
) {
157 rsv_window_size
= ei
->i_block_alloc_info
->rsv_window_node
.rsv_goal_size
;
158 return put_user(rsv_window_size
, (int __user
*)arg
);
161 case EXT3_IOC_SETRSVSZ
: {
164 if (!test_opt(inode
->i_sb
, RESERVATION
) ||!S_ISREG(inode
->i_mode
))
167 err
= mnt_want_write(filp
->f_path
.mnt
);
171 if (!inode_owner_or_capable(inode
)) {
176 if (get_user(rsv_window_size
, (int __user
*)arg
)) {
181 if (rsv_window_size
> EXT3_MAX_RESERVE_BLOCKS
)
182 rsv_window_size
= EXT3_MAX_RESERVE_BLOCKS
;
185 * need to allocate reservation structure for this inode
186 * before set the window size
188 mutex_lock(&ei
->truncate_mutex
);
189 if (!ei
->i_block_alloc_info
)
190 ext3_init_block_alloc_info(inode
);
192 if (ei
->i_block_alloc_info
){
193 struct ext3_reserve_window_node
*rsv
= &ei
->i_block_alloc_info
->rsv_window_node
;
194 rsv
->rsv_goal_size
= rsv_window_size
;
196 mutex_unlock(&ei
->truncate_mutex
);
198 mnt_drop_write(filp
->f_path
.mnt
);
201 case EXT3_IOC_GROUP_EXTEND
: {
202 ext3_fsblk_t n_blocks_count
;
203 struct super_block
*sb
= inode
->i_sb
;
206 if (!capable(CAP_SYS_RESOURCE
))
209 err
= mnt_want_write(filp
->f_path
.mnt
);
213 if (get_user(n_blocks_count
, (__u32 __user
*)arg
)) {
215 goto group_extend_out
;
217 err
= ext3_group_extend(sb
, EXT3_SB(sb
)->s_es
, n_blocks_count
);
218 journal_lock_updates(EXT3_SB(sb
)->s_journal
);
219 err2
= journal_flush(EXT3_SB(sb
)->s_journal
);
220 journal_unlock_updates(EXT3_SB(sb
)->s_journal
);
224 mnt_drop_write(filp
->f_path
.mnt
);
227 case EXT3_IOC_GROUP_ADD
: {
228 struct ext3_new_group_data input
;
229 struct super_block
*sb
= inode
->i_sb
;
232 if (!capable(CAP_SYS_RESOURCE
))
235 err
= mnt_want_write(filp
->f_path
.mnt
);
239 if (copy_from_user(&input
, (struct ext3_new_group_input __user
*)arg
,
245 err
= ext3_group_add(sb
, &input
);
246 journal_lock_updates(EXT3_SB(sb
)->s_journal
);
247 err2
= journal_flush(EXT3_SB(sb
)->s_journal
);
248 journal_unlock_updates(EXT3_SB(sb
)->s_journal
);
252 mnt_drop_write(filp
->f_path
.mnt
);
257 struct super_block
*sb
= inode
->i_sb
;
258 struct fstrim_range range
;
261 if (!capable(CAP_SYS_ADMIN
))
264 if (copy_from_user(&range
, (struct fstrim_range __user
*)arg
,
268 ret
= ext3_trim_fs(sb
, &range
);
272 if (copy_to_user((struct fstrim_range __user
*)arg
, &range
,
285 long ext3_compat_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
287 /* These are just misnamed, they actually get/put from/to user an int */
289 case EXT3_IOC32_GETFLAGS
:
290 cmd
= EXT3_IOC_GETFLAGS
;
292 case EXT3_IOC32_SETFLAGS
:
293 cmd
= EXT3_IOC_SETFLAGS
;
295 case EXT3_IOC32_GETVERSION
:
296 cmd
= EXT3_IOC_GETVERSION
;
298 case EXT3_IOC32_SETVERSION
:
299 cmd
= EXT3_IOC_SETVERSION
;
301 case EXT3_IOC32_GROUP_EXTEND
:
302 cmd
= EXT3_IOC_GROUP_EXTEND
;
304 case EXT3_IOC32_GETVERSION_OLD
:
305 cmd
= EXT3_IOC_GETVERSION_OLD
;
307 case EXT3_IOC32_SETVERSION_OLD
:
308 cmd
= EXT3_IOC_SETVERSION_OLD
;
310 #ifdef CONFIG_JBD_DEBUG
311 case EXT3_IOC32_WAIT_FOR_READONLY
:
312 cmd
= EXT3_IOC_WAIT_FOR_READONLY
;
315 case EXT3_IOC32_GETRSVSZ
:
316 cmd
= EXT3_IOC_GETRSVSZ
;
318 case EXT3_IOC32_SETRSVSZ
:
319 cmd
= EXT3_IOC_SETRSVSZ
;
321 case EXT3_IOC_GROUP_ADD
:
326 return ext3_ioctl(file
, cmd
, (unsigned long) compat_ptr(arg
));