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>
18 /* Check validity of quotactl */
19 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
21 if (type >= MAXQUOTAS)
23 if (!sb && cmd != Q_SYNC)
25 /* Is operation supported? */
26 if (sb && !sb->s_qcop)
33 if (!sb->s_qcop->quota_on)
37 if (!sb->s_qcop->quota_off)
41 if (!sb->s_qcop->set_info)
45 if (!sb->s_qcop->get_info)
49 if (!sb->s_qcop->set_dqblk)
53 if (!sb->s_qcop->get_dqblk)
57 if (sb && !sb->s_qcop->quota_sync)
63 if (!sb->s_qcop->set_xstate)
67 if (!sb->s_qcop->get_xstate)
71 if (!sb->s_qcop->set_xquota)
75 if (!sb->s_qcop->get_xquota)
82 /* Is quota turned on for commands which need it? */
90 /* This is just informative test so we are satisfied without a lock */
91 if (!sb_has_quota_enabled(sb, type))
94 /* Check privileges */
95 if (cmd == Q_GETQUOTA || cmd == Q_XGETQUOTA) {
96 if (((type == USRQUOTA && current->euid != id) ||
97 (type == GRPQUOTA && !in_egroup_p(id))) &&
98 !capable(CAP_SYS_ADMIN))
101 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO && cmd != Q_XGETQSTAT)
102 if (!capable(CAP_SYS_ADMIN))
105 return security_quotactl (cmd, type, id, sb);
108 static struct super_block *get_super_to_sync(int type)
110 struct list_head *head;
115 list_for_each(head, &super_blocks) {
116 struct super_block *sb = list_entry(head, struct super_block, s_list);
118 /* This test just improves performance so it needn't be reliable... */
119 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
120 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
121 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
126 spin_unlock(&sb_lock);
127 down_read(&sb->s_umount);
134 spin_unlock(&sb_lock);
138 void sync_dquots(struct super_block *sb, int type)
141 if (sb->s_qcop->quota_sync)
142 sb->s_qcop->quota_sync(sb, type);
145 while ((sb = get_super_to_sync(type)) != 0) {
146 if (sb->s_qcop->quota_sync)
147 sb->s_qcop->quota_sync(sb, type);
153 /* Copy parameters and call proper function */
154 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
162 if (IS_ERR(pathname = getname(addr)))
163 return PTR_ERR(pathname);
164 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
169 return sb->s_qcop->quota_off(sb, type);
174 down_read(&sb_dqopt(sb)->dqptr_sem);
175 if (!sb_has_quota_enabled(sb, type)) {
176 up_read(&sb_dqopt(sb)->dqptr_sem);
179 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
180 up_read(&sb_dqopt(sb)->dqptr_sem);
181 if (copy_to_user(addr, &fmt, sizeof(fmt)))
186 struct if_dqinfo info;
188 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
190 if (copy_to_user(addr, &info, sizeof(info)))
195 struct if_dqinfo info;
197 if (copy_from_user(&info, addr, sizeof(info)))
199 return sb->s_qcop->set_info(sb, type, &info);
204 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
206 if (copy_to_user(addr, &idq, sizeof(idq)))
213 if (copy_from_user(&idq, addr, sizeof(idq)))
215 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
218 sync_dquots(sb, type);
226 if (copy_from_user(&flags, addr, sizeof(flags)))
228 return sb->s_qcop->set_xstate(sb, flags, cmd);
231 struct fs_quota_stat fqs;
233 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
235 if (copy_to_user(addr, &fqs, sizeof(fqs)))
240 struct fs_disk_quota fdq;
242 if (copy_from_user(&fdq, addr, sizeof(fdq)))
244 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
247 struct fs_disk_quota fdq;
249 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
251 if (copy_to_user(addr, &fdq, sizeof(fdq)))
255 /* We never reach here unless validity check is broken */
263 * This is the system call interface. This communicates with
264 * the user-level programs. Currently this only supports diskquota
265 * calls. Maybe we need to add the process quotas etc. in the future,
266 * but we probably should use rlimits for that.
268 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
271 struct super_block *sb = NULL;
272 struct block_device *bdev;
276 cmds = cmd >> SUBCMDSHIFT;
277 type = cmd & SUBCMDMASK;
279 if (cmds != Q_SYNC || special) {
280 tmp = getname(special);
283 bdev = lookup_bdev(tmp);
286 return PTR_ERR(bdev);
287 sb = get_super(bdev);
293 ret = check_quotactl_valid(sb, type, cmds, id);
295 ret = do_quotactl(sb, type, cmds, id, addr);