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>
19 /* Check validity of quotactl */
20 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
22 if (type >= MAXQUOTAS)
24 if (!sb && cmd != Q_SYNC)
26 /* Is operation supported? */
27 if (sb && !sb->s_qcop)
34 if (!sb->s_qcop->quota_on)
38 if (!sb->s_qcop->quota_off)
42 if (!sb->s_qcop->set_info)
46 if (!sb->s_qcop->get_info)
50 if (!sb->s_qcop->set_dqblk)
54 if (!sb->s_qcop->get_dqblk)
58 if (sb && !sb->s_qcop->quota_sync)
64 if (!sb->s_qcop->set_xstate)
68 if (!sb->s_qcop->get_xstate)
72 if (!sb->s_qcop->set_xquota)
76 if (!sb->s_qcop->get_xquota)
83 /* Is quota turned on for commands which need it? */
91 /* This is just informative test so we are satisfied without a lock */
92 if (!sb_has_quota_enabled(sb, type))
95 /* Check privileges */
96 if (cmd == Q_GETQUOTA || cmd == Q_XGETQUOTA) {
97 if (((type == USRQUOTA && current->euid != id) ||
98 (type == GRPQUOTA && !in_egroup_p(id))) &&
99 !capable(CAP_SYS_ADMIN))
102 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO && cmd != Q_XGETQSTAT)
103 if (!capable(CAP_SYS_ADMIN))
106 return security_quotactl (cmd, type, id, sb);
109 static struct super_block *get_super_to_sync(int type)
111 struct list_head *head;
116 list_for_each(head, &super_blocks) {
117 struct super_block *sb = list_entry(head, struct super_block, s_list);
119 /* This test just improves performance so it needn't be reliable... */
120 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
121 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
122 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
127 spin_unlock(&sb_lock);
128 down_read(&sb->s_umount);
135 spin_unlock(&sb_lock);
139 static void quota_sync_sb(struct super_block *sb, int type)
142 struct inode *discard[MAXQUOTAS];
144 sb->s_qcop->quota_sync(sb, type);
145 /* This is not very clever (and fast) but currently I don't know about
146 * any other simple way of getting quota data to disk and we must get
147 * them there for userspace to be visible... */
148 if (sb->s_op->sync_fs)
149 sb->s_op->sync_fs(sb, 1);
150 sync_blockdev(sb->s_bdev);
152 /* Now when everything is written we can discard the pagecache so
153 * that userspace sees the changes. We need i_sem and so we could
154 * not do it inside dqonoff_sem. Moreover we need to be carefull
155 * about races with quotaoff() (that is the reason why we have own
156 * reference to inode). */
157 down(&sb_dqopt(sb)->dqonoff_sem);
158 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
160 if (type != -1 && cnt != type)
162 if (!sb_has_quota_enabled(sb, cnt))
164 discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
166 up(&sb_dqopt(sb)->dqonoff_sem);
167 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
169 down(&discard[cnt]->i_sem);
170 truncate_inode_pages(&discard[cnt]->i_data, 0);
171 up(&discard[cnt]->i_sem);
177 void sync_dquots(struct super_block *sb, int type)
180 if (sb->s_qcop->quota_sync)
181 quota_sync_sb(sb, type);
184 while ((sb = get_super_to_sync(type)) != NULL) {
185 if (sb->s_qcop->quota_sync)
186 quota_sync_sb(sb, type);
192 /* Copy parameters and call proper function */
193 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
201 if (IS_ERR(pathname = getname(addr)))
202 return PTR_ERR(pathname);
203 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
208 return sb->s_qcop->quota_off(sb, type);
213 down_read(&sb_dqopt(sb)->dqptr_sem);
214 if (!sb_has_quota_enabled(sb, type)) {
215 up_read(&sb_dqopt(sb)->dqptr_sem);
218 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
219 up_read(&sb_dqopt(sb)->dqptr_sem);
220 if (copy_to_user(addr, &fmt, sizeof(fmt)))
225 struct if_dqinfo info;
227 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
229 if (copy_to_user(addr, &info, sizeof(info)))
234 struct if_dqinfo info;
236 if (copy_from_user(&info, addr, sizeof(info)))
238 return sb->s_qcop->set_info(sb, type, &info);
243 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
245 if (copy_to_user(addr, &idq, sizeof(idq)))
252 if (copy_from_user(&idq, addr, sizeof(idq)))
254 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
257 sync_dquots(sb, type);
265 if (copy_from_user(&flags, addr, sizeof(flags)))
267 return sb->s_qcop->set_xstate(sb, flags, cmd);
270 struct fs_quota_stat fqs;
272 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
274 if (copy_to_user(addr, &fqs, sizeof(fqs)))
279 struct fs_disk_quota fdq;
281 if (copy_from_user(&fdq, addr, sizeof(fdq)))
283 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
286 struct fs_disk_quota fdq;
288 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
290 if (copy_to_user(addr, &fdq, sizeof(fdq)))
294 /* We never reach here unless validity check is broken */
302 * This is the system call interface. This communicates with
303 * the user-level programs. Currently this only supports diskquota
304 * calls. Maybe we need to add the process quotas etc. in the future,
305 * but we probably should use rlimits for that.
307 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
310 struct super_block *sb = NULL;
311 struct block_device *bdev;
315 cmds = cmd >> SUBCMDSHIFT;
316 type = cmd & SUBCMDMASK;
318 if (cmds != Q_SYNC || special) {
319 tmp = getname(special);
322 bdev = lookup_bdev(tmp);
325 return PTR_ERR(bdev);
326 sb = get_super(bdev);
332 ret = check_quotactl_valid(sb, type, cmds, id);
334 ret = do_quotactl(sb, type, cmds, id, addr);