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/quotaops.h>
20 /* Check validity of generic quotactl commands */
21 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
23 if (type >= MAXQUOTAS)
25 if (!sb && cmd != Q_SYNC)
27 /* Is operation supported? */
28 if (sb && !sb->s_qcop)
35 if (!sb->s_qcop->quota_on)
39 if (!sb->s_qcop->quota_off)
43 if (!sb->s_qcop->set_info)
47 if (!sb->s_qcop->get_info)
51 if (!sb->s_qcop->set_dqblk)
55 if (!sb->s_qcop->get_dqblk)
59 if (sb && !sb->s_qcop->quota_sync)
66 /* Is quota turned on for commands which need it? */
74 /* This is just informative test so we are satisfied without a lock */
75 if (!sb_has_quota_enabled(sb, type))
79 /* Check privileges */
80 if (cmd == Q_GETQUOTA) {
81 if (((type == USRQUOTA && current->euid != id) ||
82 (type == GRPQUOTA && !in_egroup_p(id))) &&
83 !capable(CAP_SYS_ADMIN))
86 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
87 if (!capable(CAP_SYS_ADMIN))
93 /* Check validity of XFS Quota Manager commands */
94 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
96 if (type >= XQM_MAXQUOTAS)
107 if (!sb->s_qcop->set_xstate)
111 if (!sb->s_qcop->get_xstate)
115 if (!sb->s_qcop->set_xquota)
119 if (!sb->s_qcop->get_xquota)
123 if (!sb->s_qcop->quota_sync)
130 /* Check privileges */
131 if (cmd == Q_XGETQUOTA) {
132 if (((type == XQM_USRQUOTA && current->euid != id) ||
133 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
134 !capable(CAP_SYS_ADMIN))
136 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
137 if (!capable(CAP_SYS_ADMIN))
144 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
148 if (XQM_COMMAND(cmd))
149 error = xqm_quotactl_valid(sb, type, cmd, id);
151 error = generic_quotactl_valid(sb, type, cmd, id);
153 error = security_quotactl(cmd, type, id, sb);
157 static void quota_sync_sb(struct super_block *sb, int type)
160 struct inode *discard[MAXQUOTAS];
162 sb->s_qcop->quota_sync(sb, type);
163 /* This is not very clever (and fast) but currently I don't know about
164 * any other simple way of getting quota data to disk and we must get
165 * them there for userspace to be visible... */
166 if (sb->s_op->sync_fs)
167 sb->s_op->sync_fs(sb, 1);
168 sync_blockdev(sb->s_bdev);
170 /* Now when everything is written we can discard the pagecache so
171 * that userspace sees the changes. We need i_sem and so we could
172 * not do it inside dqonoff_sem. Moreover we need to be carefull
173 * about races with quotaoff() (that is the reason why we have own
174 * reference to inode). */
175 down(&sb_dqopt(sb)->dqonoff_sem);
176 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
178 if (type != -1 && cnt != type)
180 if (!sb_has_quota_enabled(sb, cnt))
182 discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
184 up(&sb_dqopt(sb)->dqonoff_sem);
185 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
187 down(&discard[cnt]->i_sem);
188 truncate_inode_pages(&discard[cnt]->i_data, 0);
189 up(&discard[cnt]->i_sem);
195 void sync_dquots(struct super_block *sb, int type)
200 if (sb->s_qcop->quota_sync)
201 quota_sync_sb(sb, type);
207 list_for_each_entry(sb, &super_blocks, s_list) {
208 /* This test just improves performance so it needn't be reliable... */
209 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
210 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
211 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
216 spin_unlock(&sb_lock);
217 down_read(&sb->s_umount);
218 if (sb->s_root && sb->s_qcop->quota_sync)
219 quota_sync_sb(sb, type);
220 up_read(&sb->s_umount);
222 if (__put_super_and_need_restart(sb))
225 spin_unlock(&sb_lock);
228 /* Copy parameters and call proper function */
229 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
237 if (IS_ERR(pathname = getname(addr)))
238 return PTR_ERR(pathname);
239 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
244 return sb->s_qcop->quota_off(sb, type);
249 down_read(&sb_dqopt(sb)->dqptr_sem);
250 if (!sb_has_quota_enabled(sb, type)) {
251 up_read(&sb_dqopt(sb)->dqptr_sem);
254 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
255 up_read(&sb_dqopt(sb)->dqptr_sem);
256 if (copy_to_user(addr, &fmt, sizeof(fmt)))
261 struct if_dqinfo info;
263 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
265 if (copy_to_user(addr, &info, sizeof(info)))
270 struct if_dqinfo info;
272 if (copy_from_user(&info, addr, sizeof(info)))
274 return sb->s_qcop->set_info(sb, type, &info);
279 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
281 if (copy_to_user(addr, &idq, sizeof(idq)))
288 if (copy_from_user(&idq, addr, sizeof(idq)))
290 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
293 sync_dquots(sb, type);
301 if (copy_from_user(&flags, addr, sizeof(flags)))
303 return sb->s_qcop->set_xstate(sb, flags, cmd);
306 struct fs_quota_stat fqs;
308 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
310 if (copy_to_user(addr, &fqs, sizeof(fqs)))
315 struct fs_disk_quota fdq;
317 if (copy_from_user(&fdq, addr, sizeof(fdq)))
319 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
322 struct fs_disk_quota fdq;
324 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
326 if (copy_to_user(addr, &fdq, sizeof(fdq)))
331 return sb->s_qcop->quota_sync(sb, type);
332 /* We never reach here unless validity check is broken */
340 * This is the system call interface. This communicates with
341 * the user-level programs. Currently this only supports diskquota
342 * calls. Maybe we need to add the process quotas etc. in the future,
343 * but we probably should use rlimits for that.
345 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
348 struct super_block *sb = NULL;
349 struct block_device *bdev;
353 cmds = cmd >> SUBCMDSHIFT;
354 type = cmd & SUBCMDMASK;
356 if (cmds != Q_SYNC || special) {
357 tmp = getname(special);
360 bdev = lookup_bdev(tmp);
363 return PTR_ERR(bdev);
364 sb = get_super(bdev);
370 ret = check_quotactl_valid(sb, type, cmds, id);
372 ret = do_quotactl(sb, type, cmds, id, addr);