1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext2/ioctl.c
5 * Copyright (C) 1993, 1994, 1995
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 #include <linux/capability.h>
13 #include <linux/time.h>
14 #include <linux/sched.h>
15 #include <linux/compat.h>
16 #include <linux/mount.h>
17 #include <asm/current.h>
18 #include <linux/uaccess.h>
21 long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
23 struct inode *inode = file_inode(filp);
24 struct ext2_inode_info *ei = EXT2_I(inode);
26 unsigned short rsv_window_size;
29 ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
32 case EXT2_IOC_GETFLAGS:
33 flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
34 return put_user(flags, (int __user *) arg);
35 case EXT2_IOC_SETFLAGS: {
36 unsigned int oldflags;
38 ret = mnt_want_write_file(filp);
42 if (!inode_owner_or_capable(inode)) {
47 if (get_user(flags, (int __user *) arg)) {
52 flags = ext2_mask_flags(inode->i_mode, flags);
55 /* Is it quota file? Do not allow user to mess with it */
56 if (IS_NOQUOTA(inode)) {
61 oldflags = ei->i_flags;
63 ret = vfs_ioc_setflags_prepare(inode, oldflags, flags);
69 flags = flags & EXT2_FL_USER_MODIFIABLE;
70 flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
73 ext2_set_inode_flags(inode);
74 inode->i_ctime = current_time(inode);
77 mark_inode_dirty(inode);
79 mnt_drop_write_file(filp);
82 case EXT2_IOC_GETVERSION:
83 return put_user(inode->i_generation, (int __user *) arg);
84 case EXT2_IOC_SETVERSION: {
87 if (!inode_owner_or_capable(inode))
89 ret = mnt_want_write_file(filp);
92 if (get_user(generation, (int __user *) arg)) {
98 inode->i_ctime = current_time(inode);
99 inode->i_generation = generation;
102 mark_inode_dirty(inode);
104 mnt_drop_write_file(filp);
107 case EXT2_IOC_GETRSVSZ:
108 if (test_opt(inode->i_sb, RESERVATION)
109 && S_ISREG(inode->i_mode)
110 && ei->i_block_alloc_info) {
111 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
112 return put_user(rsv_window_size, (int __user *)arg);
115 case EXT2_IOC_SETRSVSZ: {
117 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
120 if (!inode_owner_or_capable(inode))
123 if (get_user(rsv_window_size, (int __user *)arg))
126 ret = mnt_want_write_file(filp);
130 if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
131 rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
134 * need to allocate reservation structure for this inode
135 * before set the window size
138 * XXX What lock should protect the rsv_goal_size?
139 * Accessed in ext2_get_block only. ext3 uses i_truncate.
141 mutex_lock(&ei->truncate_mutex);
142 if (!ei->i_block_alloc_info)
143 ext2_init_block_alloc_info(inode);
145 if (ei->i_block_alloc_info){
146 struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
147 rsv->rsv_goal_size = rsv_window_size;
152 mutex_unlock(&ei->truncate_mutex);
153 mnt_drop_write_file(filp);
162 long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
164 /* These are just misnamed, they actually get/put from/to user an int */
166 case EXT2_IOC32_GETFLAGS:
167 cmd = EXT2_IOC_GETFLAGS;
169 case EXT2_IOC32_SETFLAGS:
170 cmd = EXT2_IOC_SETFLAGS;
172 case EXT2_IOC32_GETVERSION:
173 cmd = EXT2_IOC_GETVERSION;
175 case EXT2_IOC32_SETVERSION:
176 cmd = EXT2_IOC_SETVERSION;
181 return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));