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.
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>
16 #include <linux/syscalls.h>
17 #include <linux/buffer_head.h>
18 #include <linux/major.h>
19 #include <linux/blkdev.h>
21 /* Check validity of generic quotactl commands */
22 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
24 if (type >= MAXQUOTAS)
26 if (!sb && cmd != Q_SYNC)
28 /* Is operation supported? */
29 if (sb && !sb->s_qcop)
36 if (!sb->s_qcop->quota_on)
40 if (!sb->s_qcop->quota_off)
44 if (!sb->s_qcop->set_info)
48 if (!sb->s_qcop->get_info)
52 if (!sb->s_qcop->set_dqblk)
56 if (!sb->s_qcop->get_dqblk)
60 if (sb && !sb->s_qcop->quota_sync)
67 /* Is quota turned on for commands which need it? */
75 /* This is just informative test so we are satisfied without a lock */
76 if (!sb_has_quota_enabled(sb, type))
80 /* Check privileges */
81 if (cmd == Q_GETQUOTA) {
82 if (((type == USRQUOTA && current->euid != id) ||
83 (type == GRPQUOTA && !in_egroup_p(id))) &&
84 !capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_QUOTA_CTL))
87 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
88 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_QUOTA_CTL))
94 /* Check validity of XFS Quota Manager commands */
95 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
97 if (type >= XQM_MAXQUOTAS)
108 if (!sb->s_qcop->set_xstate)
112 if (!sb->s_qcop->get_xstate)
116 if (!sb->s_qcop->set_xquota)
120 if (!sb->s_qcop->get_xquota)
127 /* Check privileges */
128 if (cmd == Q_XGETQUOTA) {
129 if (((type == XQM_USRQUOTA && current->euid != id) ||
130 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
131 !capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_QUOTA_CTL))
133 } else if (cmd != Q_XGETQSTAT) {
134 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_QUOTA_CTL))
141 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
145 if (XQM_COMMAND(cmd))
146 error = xqm_quotactl_valid(sb, type, cmd, id);
148 error = generic_quotactl_valid(sb, type, cmd, id);
150 error = security_quotactl(cmd, type, id, sb);
154 static struct super_block *get_super_to_sync(int type)
156 struct list_head *head;
161 list_for_each(head, &super_blocks) {
162 struct super_block *sb = list_entry(head, struct super_block, s_list);
164 /* This test just improves performance so it needn't be reliable... */
165 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
166 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
167 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
172 spin_unlock(&sb_lock);
173 down_read(&sb->s_umount);
180 spin_unlock(&sb_lock);
184 static void quota_sync_sb(struct super_block *sb, int type)
187 struct inode *discard[MAXQUOTAS];
189 sb->s_qcop->quota_sync(sb, type);
190 /* This is not very clever (and fast) but currently I don't know about
191 * any other simple way of getting quota data to disk and we must get
192 * them there for userspace to be visible... */
193 if (sb->s_op->sync_fs)
194 sb->s_op->sync_fs(sb, 1);
195 sync_blockdev(sb->s_bdev);
197 /* Now when everything is written we can discard the pagecache so
198 * that userspace sees the changes. We need i_sem and so we could
199 * not do it inside dqonoff_sem. Moreover we need to be carefull
200 * about races with quotaoff() (that is the reason why we have own
201 * reference to inode). */
202 down(&sb_dqopt(sb)->dqonoff_sem);
203 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
205 if (type != -1 && cnt != type)
207 if (!sb_has_quota_enabled(sb, cnt))
209 discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
211 up(&sb_dqopt(sb)->dqonoff_sem);
212 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
214 down(&discard[cnt]->i_sem);
215 truncate_inode_pages(&discard[cnt]->i_data, 0);
216 up(&discard[cnt]->i_sem);
222 void sync_dquots(struct super_block *sb, int type)
225 if (sb->s_qcop->quota_sync)
226 quota_sync_sb(sb, type);
229 while ((sb = get_super_to_sync(type)) != NULL) {
230 if (sb->s_qcop->quota_sync)
231 quota_sync_sb(sb, type);
237 /* Copy parameters and call proper function */
238 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
246 if (IS_ERR(pathname = getname(addr)))
247 return PTR_ERR(pathname);
248 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
253 return sb->s_qcop->quota_off(sb, type);
258 down_read(&sb_dqopt(sb)->dqptr_sem);
259 if (!sb_has_quota_enabled(sb, type)) {
260 up_read(&sb_dqopt(sb)->dqptr_sem);
263 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
264 up_read(&sb_dqopt(sb)->dqptr_sem);
265 if (copy_to_user(addr, &fmt, sizeof(fmt)))
270 struct if_dqinfo info;
272 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
274 if (copy_to_user(addr, &info, sizeof(info)))
279 struct if_dqinfo info;
281 if (copy_from_user(&info, addr, sizeof(info)))
283 return sb->s_qcop->set_info(sb, type, &info);
288 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
290 if (copy_to_user(addr, &idq, sizeof(idq)))
297 if (copy_from_user(&idq, addr, sizeof(idq)))
299 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
302 sync_dquots(sb, type);
310 if (copy_from_user(&flags, addr, sizeof(flags)))
312 return sb->s_qcop->set_xstate(sb, flags, cmd);
315 struct fs_quota_stat fqs;
317 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
319 if (copy_to_user(addr, &fqs, sizeof(fqs)))
324 struct fs_disk_quota fdq;
326 if (copy_from_user(&fdq, addr, sizeof(fdq)))
328 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
331 struct fs_disk_quota fdq;
333 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
335 if (copy_to_user(addr, &fdq, sizeof(fdq)))
339 /* We never reach here unless validity check is broken */
346 #ifdef CONFIG_BLK_DEV_VROOT
347 extern struct block_device *vroot_get_real_bdev(struct block_device *);
351 * This is the system call interface. This communicates with
352 * the user-level programs. Currently this only supports diskquota
353 * calls. Maybe we need to add the process quotas etc. in the future,
354 * but we probably should use rlimits for that.
356 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
359 struct super_block *sb = NULL;
360 struct block_device *bdev;
364 cmds = cmd >> SUBCMDSHIFT;
365 type = cmd & SUBCMDMASK;
367 if (cmds != Q_SYNC || special) {
368 tmp = getname(special);
371 bdev = lookup_bdev(tmp);
374 return PTR_ERR(bdev);
375 #ifdef CONFIG_BLK_DEV_VROOT
376 if (bdev && bdev->bd_inode &&
377 imajor(bdev->bd_inode) == VROOT_MAJOR) {
378 struct block_device *bdnew =
379 vroot_get_real_bdev(bdev);
383 return PTR_ERR(bdnew);
387 sb = get_super(bdev);
393 ret = check_quotactl_valid(sb, type, cmds, id);
395 ret = do_quotactl(sb, type, cmds, id, addr);