]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/ext2/ioctl.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / fs / ext2 / ioctl.c
CommitLineData
1da177e4
LT
1/*
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"
16f7e0fe 11#include <linux/capability.h>
1da177e4
LT
12#include <linux/time.h>
13#include <linux/sched.h>
e322ff07 14#include <linux/compat.h>
42a74f20 15#include <linux/mount.h>
1da177e4 16#include <asm/current.h>
7c0f6ba6 17#include <linux/uaccess.h>
1da177e4
LT
18
19
14f9f7b2 20long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1da177e4 21{
496ad9aa 22 struct inode *inode = file_inode(filp);
1da177e4
LT
23 struct ext2_inode_info *ei = EXT2_I(inode);
24 unsigned int flags;
a686cd89 25 unsigned short rsv_window_size;
42a74f20 26 int ret;
1da177e4
LT
27
28 ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
29
30 switch (cmd) {
31 case EXT2_IOC_GETFLAGS:
32 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
a561be71 37 ret = mnt_want_write_file(filp);
42a74f20
DH
38 if (ret)
39 return ret;
1da177e4 40
2e149670 41 if (!inode_owner_or_capable(inode)) {
42a74f20
DH
42 ret = -EACCES;
43 goto setflags_out;
44 }
1da177e4 45
42a74f20
DH
46 if (get_user(flags, (int __user *) arg)) {
47 ret = -EFAULT;
48 goto setflags_out;
49 }
1da177e4 50
ef8b6461 51 flags = ext2_mask_flags(inode->i_mode, flags);
1da177e4 52
5955102c 53 inode_lock(inode);
e47776a0
JK
54 /* Is it quota file? Do not allow user to mess with it */
55 if (IS_NOQUOTA(inode)) {
5955102c 56 inode_unlock(inode);
42a74f20
DH
57 ret = -EPERM;
58 goto setflags_out;
e47776a0 59 }
1da177e4
LT
60 oldflags = ei->i_flags;
61
62 /*
63 * The IMMUTABLE and APPEND_ONLY flags can only be changed by
64 * the relevant capability.
65 *
66 * This test looks nicer. Thanks to Pauline Middelink
67 */
68 if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
93f210dd 69 if (!capable(CAP_LINUX_IMMUTABLE)) {
5955102c 70 inode_unlock(inode);
42a74f20
DH
71 ret = -EPERM;
72 goto setflags_out;
93f210dd 73 }
1da177e4
LT
74 }
75
76 flags = flags & EXT2_FL_USER_MODIFIABLE;
77 flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
78 ei->i_flags = flags;
79
80 ext2_set_inode_flags(inode);
02027d42 81 inode->i_ctime = current_time(inode);
5955102c 82 inode_unlock(inode);
34b07840 83
1da177e4 84 mark_inode_dirty(inode);
42a74f20 85setflags_out:
2a79f17e 86 mnt_drop_write_file(filp);
42a74f20 87 return ret;
1da177e4
LT
88 }
89 case EXT2_IOC_GETVERSION:
90 return put_user(inode->i_generation, (int __user *) arg);
34b07840
DH
91 case EXT2_IOC_SETVERSION: {
92 __u32 generation;
93
2e149670 94 if (!inode_owner_or_capable(inode))
1da177e4 95 return -EPERM;
a561be71 96 ret = mnt_want_write_file(filp);
42a74f20
DH
97 if (ret)
98 return ret;
34b07840 99 if (get_user(generation, (int __user *) arg)) {
42a74f20 100 ret = -EFAULT;
34b07840 101 goto setversion_out;
42a74f20 102 }
34b07840 103
5955102c 104 inode_lock(inode);
02027d42 105 inode->i_ctime = current_time(inode);
34b07840 106 inode->i_generation = generation;
5955102c 107 inode_unlock(inode);
34b07840
DH
108
109 mark_inode_dirty(inode);
110setversion_out:
2a79f17e 111 mnt_drop_write_file(filp);
42a74f20 112 return ret;
34b07840 113 }
a686cd89
MB
114 case EXT2_IOC_GETRSVSZ:
115 if (test_opt(inode->i_sb, RESERVATION)
116 && S_ISREG(inode->i_mode)
117 && ei->i_block_alloc_info) {
118 rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
119 return put_user(rsv_window_size, (int __user *)arg);
120 }
121 return -ENOTTY;
122 case EXT2_IOC_SETRSVSZ: {
123
124 if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
125 return -ENOTTY;
126
2e149670 127 if (!inode_owner_or_capable(inode))
a686cd89
MB
128 return -EACCES;
129
130 if (get_user(rsv_window_size, (int __user *)arg))
131 return -EFAULT;
132
a561be71 133 ret = mnt_want_write_file(filp);
42a74f20
DH
134 if (ret)
135 return ret;
136
a686cd89
MB
137 if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
138 rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
139
140 /*
141 * need to allocate reservation structure for this inode
142 * before set the window size
143 */
144 /*
145 * XXX What lock should protect the rsv_goal_size?
146 * Accessed in ext2_get_block only. ext3 uses i_truncate.
147 */
148 mutex_lock(&ei->truncate_mutex);
149 if (!ei->i_block_alloc_info)
150 ext2_init_block_alloc_info(inode);
151
152 if (ei->i_block_alloc_info){
153 struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
154 rsv->rsv_goal_size = rsv_window_size;
155 }
156 mutex_unlock(&ei->truncate_mutex);
2a79f17e 157 mnt_drop_write_file(filp);
a686cd89
MB
158 return 0;
159 }
1da177e4
LT
160 default:
161 return -ENOTTY;
162 }
163}
e322ff07
DH
164
165#ifdef CONFIG_COMPAT
166long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
167{
e322ff07
DH
168 /* These are just misnamed, they actually get/put from/to user an int */
169 switch (cmd) {
170 case EXT2_IOC32_GETFLAGS:
171 cmd = EXT2_IOC_GETFLAGS;
172 break;
173 case EXT2_IOC32_SETFLAGS:
174 cmd = EXT2_IOC_SETFLAGS;
175 break;
176 case EXT2_IOC32_GETVERSION:
177 cmd = EXT2_IOC_GETVERSION;
178 break;
179 case EXT2_IOC32_SETVERSION:
180 cmd = EXT2_IOC_SETVERSION;
181 break;
182 default:
183 return -ENOIOCTLCMD;
184 }
14f9f7b2 185 return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
e322ff07
DH
186}
187#endif