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/compat.h>
14 #include <linux/kernel.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/types.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? */
75 /* This is just informative test so we are satisfied without a lock */
76 if (!sb_has_quota_enabled(sb, type))
80 /* Check privileges */
81 if (cmd == Q_GETQUOTA) {
82 if (((type == USRQUOTA && current->euid != id) ||
83 (type == GRPQUOTA && !in_egroup_p(id))) &&
84 !capable(CAP_SYS_ADMIN))
87 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
88 if (!capable(CAP_SYS_ADMIN))
94 /* Check validity of XFS Quota Manager commands */
95 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
97 if (type >= XQM_MAXQUOTAS)
108 if (!sb->s_qcop->set_xstate)
112 if (!sb->s_qcop->get_xstate)
116 if (!sb->s_qcop->set_xquota)
120 if (!sb->s_qcop->get_xquota)
124 if (!sb->s_qcop->quota_sync)
131 /* Check privileges */
132 if (cmd == Q_XGETQUOTA) {
133 if (((type == XQM_USRQUOTA && current->euid != id) ||
134 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
135 !capable(CAP_SYS_ADMIN))
137 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
138 if (!capable(CAP_SYS_ADMIN))
145 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
149 if (XQM_COMMAND(cmd))
150 error = xqm_quotactl_valid(sb, type, cmd, id);
152 error = generic_quotactl_valid(sb, type, cmd, id);
154 error = security_quotactl(cmd, type, id, sb);
158 static void quota_sync_sb(struct super_block *sb, int type)
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);
171 * Now when everything is written we can discard the pagecache so
172 * that userspace sees the changes.
174 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
175 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
176 if (type != -1 && cnt != type)
178 if (!sb_has_quota_enabled(sb, cnt))
180 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex, I_MUTEX_QUOTA);
181 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
182 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
184 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
187 void sync_dquots(struct super_block *sb, int type)
192 if (sb->s_qcop->quota_sync)
193 quota_sync_sb(sb, type);
199 list_for_each_entry(sb, &super_blocks, s_list) {
200 /* This test just improves performance so it needn't be reliable... */
201 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
202 if (type != -1 && type != cnt)
204 if (!sb_has_quota_enabled(sb, cnt))
206 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
207 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
211 if (cnt == MAXQUOTAS)
214 spin_unlock(&sb_lock);
215 down_read(&sb->s_umount);
216 if (sb->s_root && sb->s_qcop->quota_sync)
217 quota_sync_sb(sb, type);
218 up_read(&sb->s_umount);
220 if (__put_super_and_need_restart(sb))
223 spin_unlock(&sb_lock);
226 /* Copy parameters and call proper function */
227 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
235 if (IS_ERR(pathname = getname(addr)))
236 return PTR_ERR(pathname);
237 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
242 return sb->s_qcop->quota_off(sb, type, 0);
247 down_read(&sb_dqopt(sb)->dqptr_sem);
248 if (!sb_has_quota_enabled(sb, type)) {
249 up_read(&sb_dqopt(sb)->dqptr_sem);
252 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
253 up_read(&sb_dqopt(sb)->dqptr_sem);
254 if (copy_to_user(addr, &fmt, sizeof(fmt)))
259 struct if_dqinfo info;
261 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
263 if (copy_to_user(addr, &info, sizeof(info)))
268 struct if_dqinfo info;
270 if (copy_from_user(&info, addr, sizeof(info)))
272 return sb->s_qcop->set_info(sb, type, &info);
277 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
279 if (copy_to_user(addr, &idq, sizeof(idq)))
286 if (copy_from_user(&idq, addr, sizeof(idq)))
288 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
291 sync_dquots(sb, type);
299 if (copy_from_user(&flags, addr, sizeof(flags)))
301 return sb->s_qcop->set_xstate(sb, flags, cmd);
304 struct fs_quota_stat fqs;
306 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
308 if (copy_to_user(addr, &fqs, sizeof(fqs)))
313 struct fs_disk_quota fdq;
315 if (copy_from_user(&fdq, addr, sizeof(fdq)))
317 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
320 struct fs_disk_quota fdq;
322 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
324 if (copy_to_user(addr, &fdq, sizeof(fdq)))
329 return sb->s_qcop->quota_sync(sb, type);
330 /* We never reach here unless validity check is broken */
338 * look up a superblock on which quota ops will be performed
339 * - use the name of a block device to find the superblock thereon
341 static inline struct super_block *quotactl_block(const char __user *special)
344 struct block_device *bdev;
345 struct super_block *sb;
346 char *tmp = getname(special);
349 return ERR_CAST(tmp);
350 bdev = lookup_bdev(tmp);
353 return ERR_CAST(bdev);
354 sb = get_super(bdev);
357 return ERR_PTR(-ENODEV);
361 return ERR_PTR(-ENODEV);
366 * This is the system call interface. This communicates with
367 * the user-level programs. Currently this only supports diskquota
368 * calls. Maybe we need to add the process quotas etc. in the future,
369 * but we probably should use rlimits for that.
371 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
374 struct super_block *sb = NULL;
377 cmds = cmd >> SUBCMDSHIFT;
378 type = cmd & SUBCMDMASK;
380 if (cmds != Q_SYNC || special) {
381 sb = quotactl_block(special);
386 ret = check_quotactl_valid(sb, type, cmds, id);
388 ret = do_quotactl(sb, type, cmds, id, addr);
395 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
397 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
398 * and is necessary due to alignment problems.
400 struct compat_if_dqblk {
401 compat_u64 dqb_bhardlimit;
402 compat_u64 dqb_bsoftlimit;
403 compat_u64 dqb_curspace;
404 compat_u64 dqb_ihardlimit;
405 compat_u64 dqb_isoftlimit;
406 compat_u64 dqb_curinodes;
407 compat_u64 dqb_btime;
408 compat_u64 dqb_itime;
409 compat_uint_t dqb_valid;
413 struct compat_fs_qfilestat {
414 compat_u64 dqb_bhardlimit;
415 compat_u64 qfs_nblks;
416 compat_uint_t qfs_nextents;
419 struct compat_fs_quota_stat {
423 struct compat_fs_qfilestat qs_uquota;
424 struct compat_fs_qfilestat qs_gquota;
425 compat_uint_t qs_incoredqs;
426 compat_int_t qs_btimelimit;
427 compat_int_t qs_itimelimit;
428 compat_int_t qs_rtbtimelimit;
433 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
434 qid_t id, void __user *addr)
437 struct if_dqblk __user *dqblk;
438 struct compat_if_dqblk __user *compat_dqblk;
439 struct fs_quota_stat __user *fsqstat;
440 struct compat_fs_quota_stat __user *compat_fsqstat;
445 cmds = cmd >> SUBCMDSHIFT;
449 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
451 ret = sys_quotactl(cmd, special, id, dqblk);
454 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
455 get_user(data, &dqblk->dqb_valid) ||
456 put_user(data, &compat_dqblk->dqb_valid))
460 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
463 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
464 get_user(data, &compat_dqblk->dqb_valid) ||
465 put_user(data, &dqblk->dqb_valid))
467 ret = sys_quotactl(cmd, special, id, dqblk);
470 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
471 compat_fsqstat = addr;
472 ret = sys_quotactl(cmd, special, id, fsqstat);
476 /* Copying qs_version, qs_flags, qs_pad */
477 if (copy_in_user(compat_fsqstat, fsqstat,
478 offsetof(struct compat_fs_quota_stat, qs_uquota)))
480 /* Copying qs_uquota */
481 if (copy_in_user(&compat_fsqstat->qs_uquota,
483 sizeof(compat_fsqstat->qs_uquota)) ||
484 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
485 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
487 /* Copying qs_gquota */
488 if (copy_in_user(&compat_fsqstat->qs_gquota,
490 sizeof(compat_fsqstat->qs_gquota)) ||
491 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
492 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
494 /* Copying the rest */
495 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
496 &fsqstat->qs_incoredqs,
497 sizeof(struct compat_fs_quota_stat) -
498 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
499 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
500 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
505 ret = sys_quotactl(cmd, special, id, addr);