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/capability.h>
19 #include <linux/quotaops.h>
20 #include <linux/vs_context.h>
22 /* Check validity of generic quotactl commands */
23 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
25 if (type >= MAXQUOTAS)
27 if (!sb && cmd != Q_SYNC)
29 /* Is operation supported? */
30 if (sb && !sb->s_qcop)
37 if (!sb->s_qcop->quota_on)
41 if (!sb->s_qcop->quota_off)
45 if (!sb->s_qcop->set_info)
49 if (!sb->s_qcop->get_info)
53 if (!sb->s_qcop->set_dqblk)
57 if (!sb->s_qcop->get_dqblk)
61 if (sb && !sb->s_qcop->quota_sync)
68 /* Is quota turned on for commands which need it? */
76 /* This is just informative test so we are satisfied without a lock */
77 if (!sb_has_quota_enabled(sb, type))
81 /* Check privileges */
82 if (cmd == Q_GETQUOTA) {
83 if (((type == USRQUOTA && current->euid != id) ||
84 (type == GRPQUOTA && !in_egroup_p(id))) &&
85 !vx_capable(CAP_SYS_ADMIN, VXC_QUOTA_CTL))
88 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
89 if (!vx_capable(CAP_SYS_ADMIN, VXC_QUOTA_CTL))
95 /* Check validity of XFS Quota Manager commands */
96 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
98 if (type >= XQM_MAXQUOTAS)
109 if (!sb->s_qcop->set_xstate)
113 if (!sb->s_qcop->get_xstate)
117 if (!sb->s_qcop->set_xquota)
121 if (!sb->s_qcop->get_xquota)
125 if (!sb->s_qcop->quota_sync)
132 /* Check privileges */
133 if (cmd == Q_XGETQUOTA) {
134 if (((type == XQM_USRQUOTA && current->euid != id) ||
135 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
136 !vx_capable(CAP_SYS_ADMIN, VXC_QUOTA_CTL))
138 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
139 if (!vx_capable(CAP_SYS_ADMIN, VXC_QUOTA_CTL))
146 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
150 if (XQM_COMMAND(cmd))
151 error = xqm_quotactl_valid(sb, type, cmd, id);
153 error = generic_quotactl_valid(sb, type, cmd, id);
155 error = security_quotactl(cmd, type, id, sb);
159 static void quota_sync_sb(struct super_block *sb, int type)
162 struct inode *discard[MAXQUOTAS];
164 sb->s_qcop->quota_sync(sb, type);
165 /* This is not very clever (and fast) but currently I don't know about
166 * any other simple way of getting quota data to disk and we must get
167 * them there for userspace to be visible... */
168 if (sb->s_op->sync_fs)
169 sb->s_op->sync_fs(sb, 1);
170 sync_blockdev(sb->s_bdev);
172 /* Now when everything is written we can discard the pagecache so
173 * that userspace sees the changes. We need i_mutex and so we could
174 * not do it inside dqonoff_mutex. Moreover we need to be carefull
175 * about races with quotaoff() (that is the reason why we have own
176 * reference to inode). */
177 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
178 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
180 if (type != -1 && cnt != type)
182 if (!sb_has_quota_enabled(sb, cnt))
184 discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
186 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
187 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
189 mutex_lock(&discard[cnt]->i_mutex);
190 truncate_inode_pages(&discard[cnt]->i_data, 0);
191 mutex_unlock(&discard[cnt]->i_mutex);
197 void sync_dquots(struct super_block *sb, int type)
202 if (sb->s_qcop->quota_sync)
203 quota_sync_sb(sb, type);
209 list_for_each_entry(sb, &super_blocks, s_list) {
210 /* This test just improves performance so it needn't be reliable... */
211 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
212 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
213 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
218 spin_unlock(&sb_lock);
219 down_read(&sb->s_umount);
220 if (sb->s_root && sb->s_qcop->quota_sync)
221 quota_sync_sb(sb, type);
222 up_read(&sb->s_umount);
224 if (__put_super_and_need_restart(sb))
227 spin_unlock(&sb_lock);
230 /* Copy parameters and call proper function */
231 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
239 if (IS_ERR(pathname = getname(addr)))
240 return PTR_ERR(pathname);
241 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
246 return sb->s_qcop->quota_off(sb, type);
251 down_read(&sb_dqopt(sb)->dqptr_sem);
252 if (!sb_has_quota_enabled(sb, type)) {
253 up_read(&sb_dqopt(sb)->dqptr_sem);
256 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
257 up_read(&sb_dqopt(sb)->dqptr_sem);
258 if (copy_to_user(addr, &fmt, sizeof(fmt)))
263 struct if_dqinfo info;
265 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
267 if (copy_to_user(addr, &info, sizeof(info)))
272 struct if_dqinfo info;
274 if (copy_from_user(&info, addr, sizeof(info)))
276 return sb->s_qcop->set_info(sb, type, &info);
281 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
283 if (copy_to_user(addr, &idq, sizeof(idq)))
290 if (copy_from_user(&idq, addr, sizeof(idq)))
292 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
295 sync_dquots(sb, type);
303 if (copy_from_user(&flags, addr, sizeof(flags)))
305 return sb->s_qcop->set_xstate(sb, flags, cmd);
308 struct fs_quota_stat fqs;
310 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
312 if (copy_to_user(addr, &fqs, sizeof(fqs)))
317 struct fs_disk_quota fdq;
319 if (copy_from_user(&fdq, addr, sizeof(fdq)))
321 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
324 struct fs_disk_quota fdq;
326 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
328 if (copy_to_user(addr, &fdq, sizeof(fdq)))
333 return sb->s_qcop->quota_sync(sb, type);
334 /* We never reach here unless validity check is broken */
341 #if defined(CONFIG_BLK_DEV_VROOT) || defined(CONFIG_BLK_DEV_VROOT_MODULE)
343 #include <linux/vroot.h>
344 #include <linux/major.h>
345 #include <linux/module.h>
346 #include <linux/kallsyms.h>
347 #include <linux/vserver/debug.h>
349 static vroot_grb_func *vroot_get_real_bdev = NULL;
351 static spinlock_t vroot_grb_lock = SPIN_LOCK_UNLOCKED;
353 int register_vroot_grb(vroot_grb_func *func) {
356 spin_lock(&vroot_grb_lock);
357 if (!vroot_get_real_bdev) {
358 vroot_get_real_bdev = func;
361 spin_unlock(&vroot_grb_lock);
364 EXPORT_SYMBOL(register_vroot_grb);
366 int unregister_vroot_grb(vroot_grb_func *func) {
369 spin_lock(&vroot_grb_lock);
370 if (vroot_get_real_bdev) {
371 vroot_get_real_bdev = NULL;
374 spin_unlock(&vroot_grb_lock);
377 EXPORT_SYMBOL(unregister_vroot_grb);
382 * look up a superblock on which quota ops will be performed
383 * - use the name of a block device to find the superblock thereon
385 static inline struct super_block *quotactl_block(const char __user *special)
388 struct block_device *bdev;
389 struct super_block *sb;
390 char *tmp = getname(special);
393 return ERR_PTR(PTR_ERR(tmp));
394 bdev = lookup_bdev(tmp);
397 return ERR_PTR(PTR_ERR(bdev));
398 #if defined(CONFIG_BLK_DEV_VROOT) || defined(CONFIG_BLK_DEV_VROOT_MODULE)
399 if (bdev && bdev->bd_inode &&
400 imajor(bdev->bd_inode) == VROOT_MAJOR) {
401 struct block_device *bdnew = (void *)-EINVAL;
403 if (vroot_get_real_bdev)
404 bdnew = vroot_get_real_bdev(bdev);
406 vxdprintk(VXD_CBIT(misc, 0),
407 "vroot_get_real_bdev not set");
410 return ERR_PTR(PTR_ERR(bdnew));
414 sb = get_super(bdev);
417 return ERR_PTR(-ENODEV);
421 return ERR_PTR(-ENODEV);
426 * This is the system call interface. This communicates with
427 * the user-level programs. Currently this only supports diskquota
428 * calls. Maybe we need to add the process quotas etc. in the future,
429 * but we probably should use rlimits for that.
431 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
434 struct super_block *sb = NULL;
437 cmds = cmd >> SUBCMDSHIFT;
438 type = cmd & SUBCMDMASK;
440 if (cmds != Q_SYNC || special) {
441 sb = quotactl_block(special);
446 ret = check_quotactl_valid(sb, type, cmds, id);
448 ret = do_quotactl(sb, type, cmds, id, addr);