forked from Ninals-GitHub/Learning-Ext2-Filesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathme2fs_ioctl.c
310 lines (265 loc) · 7.29 KB
/
me2fs_ioctl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/********************************************************************************
File : me2fs_ioctl.c
Description : Operations of me2fs ioctl
*********************************************************************************/
#include <linux/mount.h>
#include <linux/compat.h>
#include "me2fs.h"
#include "me2fs_util.h"
#include "me2fs_inode.h"
#include "me2fs_block.h"
/*
==================================================================================
Prototype Statement
==================================================================================
*/
/*
==================================================================================
DEFINES
==================================================================================
*/
/*
==================================================================================
Management
==================================================================================
*/
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< Open Functions >
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsIoctl
Input :struct file *filp
< vfs file object >
unsigned int cmd
< command for ioctl >
unsigned long arg
< user buffer >
Output :void
Return :long
< result >
Description :i/o contorl of file
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
long me2fsIoctl( struct file *filp, unsigned int cmd, unsigned long arg )
{
struct inode *inode;
struct me2fs_inode_info *mei;
unsigned int flags;
int ret;
unsigned int oldflags;
__u32 generation;
unsigned short rsv_window_size;
DBGPRINT( "<ME2FS>ioctl cmd = %u, arg = %lu\n", cmd, arg );
inode = file_inode( filp );
mei = ME2FS_I( inode );
switch( cmd )
{
case EXT2_IOC_GETFLAGS:
/* get flags: set me2fs inode flags to vfs inode flags */
me2fsSetMe2fsInodeFlags( mei );
flags = mei->i_flags & EXT2_FL_USER_VISIBLE;
return( put_user( flags, ( int __user* )arg ) );
case EXT2_IOC_SETFLAGS:
if( ( ret = mnt_want_write_file( filp ) ) )
{
return( ret );
}
if( !inode_owner_or_capable( inode ) )
{
ret = -EACCES;
mnt_drop_write_file( filp );
return( ret );
}
if( get_user( flags, ( int __user* )arg ) )
{
ret = -EFAULT;
mnt_drop_write_file( filp );
return( ret );
}
if( S_ISDIR( inode->i_mode ) )
{
/* use user flags, so do nothing */
}
else if( S_ISREG( inode->i_mode ) )
{
flags = flags & EXT2_REG_FLMASK;
}
else
{
flags = flags & EXT2_OTHER_FLMASK;
}
mutex_lock( &inode->i_mutex );
{
if( IS_NOQUOTA( inode ) )
{
mutex_unlock( &inode->i_mutex );
ret = -EPERM;
mnt_drop_write_file( filp );
return( ret );
}
oldflags = mei->i_flags;
/* ---------------------------------------------------------------- */
/* IMMUTABLE and APPEND_ONLY flags can only be changed by the */
/* relevant capability. */
/* ---------------------------------------------------------------- */
if( ( flags ^ oldflags ) & ( EXT2_APPEND_FL | EXT2_IMMUTABLE_FL ) )
{
if( !capable( CAP_LINUX_IMMUTABLE ) )
{
mutex_unlock( &inode->i_mutex );
ret = -EPERM;
mnt_drop_write_file( filp );
return( ret );
}
}
flags &= EXT2_FL_USER_MODIFIABLE;
flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
mei->i_flags = flags;
me2fsSetVfsInodeFlags( inode );
inode->i_ctime = CURRENT_TIME_SEC;
}
mutex_unlock( &inode->i_mutex );
mark_inode_dirty( inode );
mnt_drop_write_file( filp );
return( ret );
case EXT2_IOC_GETVERSION:
return( put_user( inode->i_generation, ( int __user* )arg ) );
case EXT2_IOC_SETVERSION:
if( !inode_owner_or_capable( inode ) )
{
return( -EPERM );
}
if( ( ret = mnt_want_write_file( filp ) ) )
{
return( ret );
}
if( get_user( generation, ( int __user* )arg ) )
{
ret = -EFAULT;
mnt_drop_write_file( filp );
return( ret );
}
mutex_lock( &inode->i_mutex );
{
inode->i_ctime = CURRENT_TIME_SEC;
inode->i_generation = generation;
}
mutex_unlock( &inode->i_mutex );
mark_inode_dirty( inode );
mnt_drop_write_file( filp );
return( ret );
case EXT2_IOC_GETRSVSZ:
if( ( ME2FS_SB( inode->i_sb )->s_mount_opt & EXT2_MOUNT_RESERVATION ) &&
( S_ISREG( inode->i_mode ) ) &&
( mei->i_block_alloc_info ) )
{
rsv_window_size =
mei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
return( put_user( rsv_window_size, ( int __user* )arg ) );
}
return( -ENOTTY );
case EXT2_IOC_SETRSVSZ:
if( !( ME2FS_SB( inode->i_sb )->s_mount_opt & EXT2_MOUNT_RESERVATION ) ||
!S_ISREG( inode->i_mode ) )
{
return( -ENOTTY );
}
if( get_user( rsv_window_size, ( int __user* )arg ) )
{
return( -EFAULT );
}
if( ( ret = mnt_want_write_file( filp ) ) )
{
return( ret );
}
if( EXT2_MAX_RESERVE_BLOCKS < rsv_window_size )
{
rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
}
/* -------------------------------------------------------------------- */
/* need to allocate reservation structure for this inode before set */
/* the window size */
/* -------------------------------------------------------------------- */
mutex_lock( &mei->truncate_mutex );
{
if( !mei->i_block_alloc_info )
{
me2fsInitBlockAllocInfo( inode );
}
if( mei->i_block_alloc_info )
{
struct ext2_reserve_window_node *rsv;
rsv = &mei->i_block_alloc_info->rsv_window_node;
rsv->rsv_goal_size = rsv_window_size;
}
}
mutex_unlock( &mei->truncate_mutex );
mnt_drop_write_file( filp );
return( 0 );
default:
break;
}
return( -ENOTTY );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :me2fsCompatIoctl
Input :struct file *filp
< vfs file object >
unsigned int cmd
< command for ioctl >
unsigned long arg
< user buffer >
Output :void
Return :long
< result >
Description :32 bit compatible ioctl
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
long me2fsCompatIoctl( struct file *file, unsigned int cmd, unsigned long arg )
{
switch( cmd )
{
case EXT2_IOC32_GETFLAGS:
cmd = EXT2_IOC_GETFLAGS;
break;
case EXT2_IOC32_SETFLAGS:
cmd = EXT2_IOC_SETFLAGS;
break;
case EXT2_IOC32_GETVERSION:
cmd = EXT2_IOC_GETVERSION;
break;
case EXT2_IOC32_SETVERSION:
cmd = EXT2_IOC_SETVERSION;
break;
default:
return( -ENOIOCTLCMD );
}
return( me2fsIoctl( file, cmd, ( unsigned long )compat_ptr( arg ) ) );
}
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Function :void
Input :void
Output :void
Return :void
Description :void
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
< Local Functions >
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
/*
==================================================================================
Function :void
Input :void
Output :void
Return :void
Description :void
==================================================================================
*/