[PATCH] Add a driver for the Technisat Skystar2 DVB card
[linux-2.6/history.git] / fs / quota.c
blobce929f581b53e17ab5fb09cdc6a7bf4f2e9c05b3
1 /*
2 * Quota code necessary even when VFS quota support is not compiled
3 * into the kernel. The interesting stuff is over in dquot.c, here
4 * we have symbols for initial quotactl(2) handling, the sysctl(2)
5 * variables, etc - things needed even when quota support disabled.
6 */
8 #include <linux/fs.h>
9 #include <linux/namei.h>
10 #include <linux/slab.h>
11 #include <asm/current.h>
12 #include <asm/uaccess.h>
13 #include <linux/kernel.h>
14 #include <linux/smp_lock.h>
15 #include <linux/security.h>
17 /* Check validity of quotactl */
18 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
20 if (type >= MAXQUOTAS)
21 return -EINVAL;
22 if (!sb && cmd != Q_SYNC)
23 return -ENODEV;
24 /* Is operation supported? */
25 if (sb && !sb->s_qcop)
26 return -ENOSYS;
28 switch (cmd) {
29 case Q_GETFMT:
30 break;
31 case Q_QUOTAON:
32 if (!sb->s_qcop->quota_on)
33 return -ENOSYS;
34 break;
35 case Q_QUOTAOFF:
36 if (!sb->s_qcop->quota_off)
37 return -ENOSYS;
38 break;
39 case Q_SETINFO:
40 if (!sb->s_qcop->set_info)
41 return -ENOSYS;
42 break;
43 case Q_GETINFO:
44 if (!sb->s_qcop->get_info)
45 return -ENOSYS;
46 break;
47 case Q_SETQUOTA:
48 if (!sb->s_qcop->set_dqblk)
49 return -ENOSYS;
50 break;
51 case Q_GETQUOTA:
52 if (!sb->s_qcop->get_dqblk)
53 return -ENOSYS;
54 break;
55 case Q_SYNC:
56 if (sb && !sb->s_qcop->quota_sync)
57 return -ENOSYS;
58 break;
59 case Q_XQUOTAON:
60 case Q_XQUOTAOFF:
61 case Q_XQUOTARM:
62 if (!sb->s_qcop->set_xstate)
63 return -ENOSYS;
64 break;
65 case Q_XGETQSTAT:
66 if (!sb->s_qcop->get_xstate)
67 return -ENOSYS;
68 break;
69 case Q_XSETQLIM:
70 if (!sb->s_qcop->set_xquota)
71 return -ENOSYS;
72 break;
73 case Q_XGETQUOTA:
74 if (!sb->s_qcop->get_xquota)
75 return -ENOSYS;
76 break;
77 default:
78 return -EINVAL;
81 /* Is quota turned on for commands which need it? */
82 switch (cmd) {
83 case Q_GETFMT:
84 case Q_GETINFO:
85 case Q_QUOTAOFF:
86 case Q_SETINFO:
87 case Q_SETQUOTA:
88 case Q_GETQUOTA:
89 /* This is just informative test so we are satisfied without a lock */
90 if (!sb_has_quota_enabled(sb, type))
91 return -ESRCH;
93 /* Check privileges */
94 if (cmd == Q_GETQUOTA || cmd == Q_XGETQUOTA) {
95 if (((type == USRQUOTA && current->euid != id) ||
96 (type == GRPQUOTA && !in_egroup_p(id))) &&
97 !capable(CAP_SYS_ADMIN))
98 return -EPERM;
100 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO && cmd != Q_XGETQSTAT)
101 if (!capable(CAP_SYS_ADMIN))
102 return -EPERM;
104 return security_quotactl (cmd, type, id, sb);
107 static struct super_block *get_super_to_sync(int type)
109 struct list_head *head;
110 int cnt, dirty;
112 restart:
113 spin_lock(&sb_lock);
114 list_for_each(head, &super_blocks) {
115 struct super_block *sb = list_entry(head, struct super_block, s_list);
117 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
118 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
119 && info_any_dquot_dirty(&sb_dqopt(sb)->info[cnt]))
120 dirty = 1;
121 if (!dirty)
122 continue;
123 sb->s_count++;
124 spin_unlock(&sb_lock);
125 down_read(&sb->s_umount);
126 if (!sb->s_root) {
127 drop_super(sb);
128 goto restart;
130 return sb;
132 spin_unlock(&sb_lock);
133 return NULL;
136 void sync_dquots(struct super_block *sb, int type)
138 if (sb) {
139 if (sb->s_qcop->quota_sync)
140 sb->s_qcop->quota_sync(sb, type);
142 else {
143 while ((sb = get_super_to_sync(type))) {
144 if (sb->s_qcop->quota_sync)
145 sb->s_qcop->quota_sync(sb, type);
146 drop_super(sb);
151 /* Copy parameters and call proper function */
152 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, caddr_t addr)
154 int ret;
156 switch (cmd) {
157 case Q_QUOTAON: {
158 char *pathname;
160 if (IS_ERR(pathname = getname(addr)))
161 return PTR_ERR(pathname);
162 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
163 putname(pathname);
164 return ret;
166 case Q_QUOTAOFF:
167 return sb->s_qcop->quota_off(sb, type);
169 case Q_GETFMT: {
170 __u32 fmt;
172 down_read(&sb_dqopt(sb)->dqptr_sem);
173 if (!sb_has_quota_enabled(sb, type)) {
174 up_read(&sb_dqopt(sb)->dqptr_sem);
175 return -ESRCH;
177 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
178 up_read(&sb_dqopt(sb)->dqptr_sem);
179 if (copy_to_user(addr, &fmt, sizeof(fmt)))
180 return -EFAULT;
181 return 0;
183 case Q_GETINFO: {
184 struct if_dqinfo info;
186 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
187 return ret;
188 if (copy_to_user(addr, &info, sizeof(info)))
189 return -EFAULT;
190 return 0;
192 case Q_SETINFO: {
193 struct if_dqinfo info;
195 if (copy_from_user(&info, addr, sizeof(info)))
196 return -EFAULT;
197 return sb->s_qcop->set_info(sb, type, &info);
199 case Q_GETQUOTA: {
200 struct if_dqblk idq;
202 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
203 return ret;
204 if (copy_to_user(addr, &idq, sizeof(idq)))
205 return -EFAULT;
206 return 0;
208 case Q_SETQUOTA: {
209 struct if_dqblk idq;
211 if (copy_from_user(&idq, addr, sizeof(idq)))
212 return -EFAULT;
213 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
215 case Q_SYNC:
216 sync_dquots(sb, type);
217 return 0;
219 case Q_XQUOTAON:
220 case Q_XQUOTAOFF:
221 case Q_XQUOTARM: {
222 __u32 flags;
224 if (copy_from_user(&flags, addr, sizeof(flags)))
225 return -EFAULT;
226 return sb->s_qcop->set_xstate(sb, flags, cmd);
228 case Q_XGETQSTAT: {
229 struct fs_quota_stat fqs;
231 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
232 return ret;
233 if (copy_to_user(addr, &fqs, sizeof(fqs)))
234 return -EFAULT;
235 return 0;
237 case Q_XSETQLIM: {
238 struct fs_disk_quota fdq;
240 if (copy_from_user(&fdq, addr, sizeof(fdq)))
241 return -EFAULT;
242 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
244 case Q_XGETQUOTA: {
245 struct fs_disk_quota fdq;
247 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
248 return ret;
249 if (copy_to_user(addr, &fdq, sizeof(fdq)))
250 return -EFAULT;
251 return 0;
253 /* We never reach here unless validity check is broken */
254 default:
255 BUG();
257 return 0;
261 * This is the system call interface. This communicates with
262 * the user-level programs. Currently this only supports diskquota
263 * calls. Maybe we need to add the process quotas etc. in the future,
264 * but we probably should use rlimits for that.
266 asmlinkage long sys_quotactl(unsigned int cmd, const char *special, qid_t id, caddr_t addr)
268 uint cmds, type;
269 struct super_block *sb = NULL;
270 struct block_device *bdev;
271 char *tmp;
272 int ret;
274 cmds = cmd >> SUBCMDSHIFT;
275 type = cmd & SUBCMDMASK;
277 if (cmds != Q_SYNC || special) {
278 tmp = getname(special);
279 if (IS_ERR(tmp))
280 return PTR_ERR(tmp);
281 bdev = lookup_bdev(tmp);
282 putname(tmp);
283 if (IS_ERR(bdev))
284 return PTR_ERR(bdev);
285 sb = get_super(bdev);
286 bdput(bdev);
287 if (!sb)
288 return -ENODEV;
291 ret = check_quotactl_valid(sb, type, cmds, id);
292 if (ret >= 0)
293 ret = do_quotactl(sb, type, cmds, id, addr);
294 if (sb)
295 drop_super(sb);
297 return ret;