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>
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)
22 if (!sb && cmd != Q_SYNC)
24 /* Is operation supported? */
25 if (sb && !sb->s_qcop)
32 if (!sb->s_qcop->quota_on)
36 if (!sb->s_qcop->quota_off)
40 if (!sb->s_qcop->set_info)
44 if (!sb->s_qcop->get_info)
48 if (!sb->s_qcop->set_dqblk)
52 if (!sb->s_qcop->get_dqblk)
56 if (sb && !sb->s_qcop->quota_sync)
62 if (!sb->s_qcop->set_xstate)
66 if (!sb->s_qcop->get_xstate)
70 if (!sb->s_qcop->set_xquota)
74 if (!sb->s_qcop->get_xquota)
81 /* Is quota turned on for commands which need it? */
89 /* This is just informative test so we are satisfied without a lock */
90 if (!sb_has_quota_enabled(sb, type))
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))
100 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO && cmd != Q_XGETQSTAT)
101 if (!capable(CAP_SYS_ADMIN))
104 return security_quotactl (cmd, type, id, sb);
107 static struct super_block *get_super_to_sync(int type)
109 struct list_head *head;
114 list_for_each(head, &super_blocks) {
115 struct super_block *sb = list_entry(head, struct super_block, s_list);
117 /* This test just improves performance so it needn't be reliable... */
118 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
119 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
120 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
125 spin_unlock(&sb_lock);
126 down_read(&sb->s_umount);
133 spin_unlock(&sb_lock);
137 void sync_dquots(struct super_block *sb, int type)
140 if (sb->s_qcop->quota_sync)
141 sb->s_qcop->quota_sync(sb, type);
144 while ((sb = get_super_to_sync(type)) != 0) {
145 if (sb->s_qcop->quota_sync)
146 sb->s_qcop->quota_sync(sb, type);
152 /* Copy parameters and call proper function */
153 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
161 if (IS_ERR(pathname = getname(addr)))
162 return PTR_ERR(pathname);
163 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
168 return sb->s_qcop->quota_off(sb, type);
173 down_read(&sb_dqopt(sb)->dqptr_sem);
174 if (!sb_has_quota_enabled(sb, type)) {
175 up_read(&sb_dqopt(sb)->dqptr_sem);
178 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
179 up_read(&sb_dqopt(sb)->dqptr_sem);
180 if (copy_to_user(addr, &fmt, sizeof(fmt)))
185 struct if_dqinfo info;
187 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
189 if (copy_to_user(addr, &info, sizeof(info)))
194 struct if_dqinfo info;
196 if (copy_from_user(&info, addr, sizeof(info)))
198 return sb->s_qcop->set_info(sb, type, &info);
203 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
205 if (copy_to_user(addr, &idq, sizeof(idq)))
212 if (copy_from_user(&idq, addr, sizeof(idq)))
214 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
217 sync_dquots(sb, type);
225 if (copy_from_user(&flags, addr, sizeof(flags)))
227 return sb->s_qcop->set_xstate(sb, flags, cmd);
230 struct fs_quota_stat fqs;
232 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
234 if (copy_to_user(addr, &fqs, sizeof(fqs)))
239 struct fs_disk_quota fdq;
241 if (copy_from_user(&fdq, addr, sizeof(fdq)))
243 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
246 struct fs_disk_quota fdq;
248 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
250 if (copy_to_user(addr, &fdq, sizeof(fdq)))
254 /* We never reach here unless validity check is broken */
262 * This is the system call interface. This communicates with
263 * the user-level programs. Currently this only supports diskquota
264 * calls. Maybe we need to add the process quotas etc. in the future,
265 * but we probably should use rlimits for that.
267 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
270 struct super_block *sb = NULL;
271 struct block_device *bdev;
275 cmds = cmd >> SUBCMDSHIFT;
276 type = cmd & SUBCMDMASK;
278 if (cmds != Q_SYNC || special) {
279 tmp = getname(special);
282 bdev = lookup_bdev(tmp);
285 return PTR_ERR(bdev);
286 sb = get_super(bdev);
292 ret = check_quotactl_valid(sb, type, cmds, id);
294 ret = do_quotactl(sb, type, cmds, id, addr);