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 quotactl */
22 static int check_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)
66 if (!sb->s_qcop->set_xstate)
70 if (!sb->s_qcop->get_xstate)
74 if (!sb->s_qcop->set_xquota)
78 if (!sb->s_qcop->get_xquota)
85 /* Is quota turned on for commands which need it? */
93 /* This is just informative test so we are satisfied without a lock */
94 if (!sb_has_quota_enabled(sb, type))
97 /* Check privileges */
98 if (cmd == Q_GETQUOTA || cmd == Q_XGETQUOTA) {
99 if (((type == USRQUOTA && current->euid != id) ||
100 (type == GRPQUOTA && !in_egroup_p(id))) &&
101 !capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_QUOTA_CTL))
104 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO && cmd != Q_XGETQSTAT)
105 if (!capable(CAP_SYS_ADMIN) && !vx_ccaps(VXC_QUOTA_CTL))
108 return security_quotactl (cmd, type, id, sb);
111 static struct super_block *get_super_to_sync(int type)
113 struct list_head *head;
118 list_for_each(head, &super_blocks) {
119 struct super_block *sb = list_entry(head, struct super_block, s_list);
121 /* This test just improves performance so it needn't be reliable... */
122 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
123 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
124 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
129 spin_unlock(&sb_lock);
130 down_read(&sb->s_umount);
137 spin_unlock(&sb_lock);
141 static void quota_sync_sb(struct super_block *sb, int type)
144 struct inode *discard[MAXQUOTAS];
146 sb->s_qcop->quota_sync(sb, type);
147 /* This is not very clever (and fast) but currently I don't know about
148 * any other simple way of getting quota data to disk and we must get
149 * them there for userspace to be visible... */
150 if (sb->s_op->sync_fs)
151 sb->s_op->sync_fs(sb, 1);
152 sync_blockdev(sb->s_bdev);
154 /* Now when everything is written we can discard the pagecache so
155 * that userspace sees the changes. We need i_sem and so we could
156 * not do it inside dqonoff_sem. Moreover we need to be carefull
157 * about races with quotaoff() (that is the reason why we have own
158 * reference to inode). */
159 down(&sb_dqopt(sb)->dqonoff_sem);
160 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
162 if (type != -1 && cnt != type)
164 if (!sb_has_quota_enabled(sb, cnt))
166 discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
168 up(&sb_dqopt(sb)->dqonoff_sem);
169 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
171 down(&discard[cnt]->i_sem);
172 truncate_inode_pages(&discard[cnt]->i_data, 0);
173 up(&discard[cnt]->i_sem);
179 void sync_dquots(struct super_block *sb, int type)
182 if (sb->s_qcop->quota_sync)
183 quota_sync_sb(sb, type);
186 while ((sb = get_super_to_sync(type)) != NULL) {
187 if (sb->s_qcop->quota_sync)
188 quota_sync_sb(sb, type);
194 /* Copy parameters and call proper function */
195 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
203 if (IS_ERR(pathname = getname(addr)))
204 return PTR_ERR(pathname);
205 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
210 return sb->s_qcop->quota_off(sb, type);
215 down_read(&sb_dqopt(sb)->dqptr_sem);
216 if (!sb_has_quota_enabled(sb, type)) {
217 up_read(&sb_dqopt(sb)->dqptr_sem);
220 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
221 up_read(&sb_dqopt(sb)->dqptr_sem);
222 if (copy_to_user(addr, &fmt, sizeof(fmt)))
227 struct if_dqinfo info;
229 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
231 if (copy_to_user(addr, &info, sizeof(info)))
236 struct if_dqinfo info;
238 if (copy_from_user(&info, addr, sizeof(info)))
240 return sb->s_qcop->set_info(sb, type, &info);
245 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
247 if (copy_to_user(addr, &idq, sizeof(idq)))
254 if (copy_from_user(&idq, addr, sizeof(idq)))
256 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
259 sync_dquots(sb, type);
267 if (copy_from_user(&flags, addr, sizeof(flags)))
269 return sb->s_qcop->set_xstate(sb, flags, cmd);
272 struct fs_quota_stat fqs;
274 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
276 if (copy_to_user(addr, &fqs, sizeof(fqs)))
281 struct fs_disk_quota fdq;
283 if (copy_from_user(&fdq, addr, sizeof(fdq)))
285 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
288 struct fs_disk_quota fdq;
290 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
292 if (copy_to_user(addr, &fdq, sizeof(fdq)))
296 /* We never reach here unless validity check is broken */
303 #ifdef CONFIG_BLK_DEV_VROOT
304 extern struct block_device *vroot_get_real_bdev(struct block_device *);
308 * This is the system call interface. This communicates with
309 * the user-level programs. Currently this only supports diskquota
310 * calls. Maybe we need to add the process quotas etc. in the future,
311 * but we probably should use rlimits for that.
313 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
316 struct super_block *sb = NULL;
317 struct block_device *bdev;
321 cmds = cmd >> SUBCMDSHIFT;
322 type = cmd & SUBCMDMASK;
324 if (cmds != Q_SYNC || special) {
325 tmp = getname(special);
328 bdev = lookup_bdev(tmp);
331 return PTR_ERR(bdev);
332 #ifdef CONFIG_BLK_DEV_VROOT
333 printk(KERN_INFO "bdev=%p, gendisk=%p inode=%p[%d,%d]\n",
334 bdev, bdev?bdev->bd_disk:0, bdev->bd_inode,
335 imajor(bdev->bd_inode), iminor(bdev->bd_inode));
337 if (bdev && bdev->bd_inode &&
338 imajor(bdev->bd_inode) == VROOT_MAJOR) {
339 struct block_device *bdnew =
340 vroot_get_real_bdev(bdev);
344 return PTR_ERR(bdnew);
348 sb = get_super(bdev);
354 ret = check_quotactl_valid(sb, type, cmds, id);
356 ret = do_quotactl(sb, type, cmds, id, addr);