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