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>
21 #include <net/netlink.h>
22 #include <net/genetlink.h>
24 /* Check validity of generic quotactl commands */
25 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd,
28 if (type >= MAXQUOTAS)
30 if (!sb && cmd != Q_SYNC)
32 /* Is operation supported? */
33 if (sb && !sb->s_qcop)
40 if (!sb->s_qcop->quota_on)
44 if (!sb->s_qcop->quota_off)
48 if (!sb->s_qcop->set_info)
52 if (!sb->s_qcop->get_info)
56 if (!sb->s_qcop->set_dqblk)
60 if (!sb->s_qcop->get_dqblk)
64 if (sb && !sb->s_qcop->quota_sync)
71 /* Is quota turned on for commands which need it? */
78 /* This is just an informative test so we are satisfied
80 if (!sb_has_quota_active(sb, type))
84 /* Check privileges */
85 if (cmd == Q_GETQUOTA) {
86 if (((type == USRQUOTA && current_euid() != id) ||
87 (type == GRPQUOTA && !in_egroup_p(id))) &&
88 !capable(CAP_SYS_ADMIN))
91 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
92 if (!capable(CAP_SYS_ADMIN))
98 /* Check validity of XFS Quota Manager commands */
99 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd,
102 if (type >= XQM_MAXQUOTAS)
113 if (!sb->s_qcop->set_xstate)
117 if (!sb->s_qcop->get_xstate)
121 if (!sb->s_qcop->set_xquota)
125 if (!sb->s_qcop->get_xquota)
129 if (!sb->s_qcop->quota_sync)
136 /* Check privileges */
137 if (cmd == Q_XGETQUOTA) {
138 if (((type == XQM_USRQUOTA && current_euid() != id) ||
139 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
140 !capable(CAP_SYS_ADMIN))
142 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
143 if (!capable(CAP_SYS_ADMIN))
150 static int check_quotactl_valid(struct super_block *sb, int type, int cmd,
155 if (XQM_COMMAND(cmd))
156 error = xqm_quotactl_valid(sb, type, cmd, id);
158 error = generic_quotactl_valid(sb, type, cmd, id);
160 error = security_quotactl(cmd, type, id, sb);
165 void sync_quota_sb(struct super_block *sb, int type)
169 if (!sb->s_qcop->quota_sync)
172 sb->s_qcop->quota_sync(sb, type);
174 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
176 /* This is not very clever (and fast) but currently I don't know about
177 * any other simple way of getting quota data to disk and we must get
178 * them there for userspace to be visible... */
179 if (sb->s_op->sync_fs)
180 sb->s_op->sync_fs(sb, 1);
181 sync_blockdev(sb->s_bdev);
184 * Now when everything is written we can discard the pagecache so
185 * that userspace sees the changes.
187 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
188 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
189 if (type != -1 && cnt != type)
191 if (!sb_has_quota_active(sb, cnt))
193 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
195 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
196 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
198 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
202 static void sync_dquots(int type)
204 struct super_block *sb;
209 list_for_each_entry(sb, &super_blocks, s_list) {
210 /* This test just improves performance so it needn't be
212 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
213 if (type != -1 && type != cnt)
215 if (!sb_has_quota_active(sb, cnt))
217 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
218 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
222 if (cnt == MAXQUOTAS)
225 spin_unlock(&sb_lock);
226 down_read(&sb->s_umount);
228 sync_quota_sb(sb, type);
229 up_read(&sb->s_umount);
231 if (__put_super_and_need_restart(sb))
234 spin_unlock(&sb_lock);
237 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
243 pathname = getname(addr);
244 if (IS_ERR(pathname))
245 return PTR_ERR(pathname);
246 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
251 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
255 down_read(&sb_dqopt(sb)->dqptr_sem);
256 if (!sb_has_quota_active(sb, type)) {
257 up_read(&sb_dqopt(sb)->dqptr_sem);
260 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
261 up_read(&sb_dqopt(sb)->dqptr_sem);
262 if (copy_to_user(addr, &fmt, sizeof(fmt)))
267 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
269 struct if_dqinfo info;
272 ret = sb->s_qcop->get_info(sb, type, &info);
273 if (!ret && copy_to_user(addr, &info, sizeof(info)))
278 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
280 struct if_dqinfo info;
282 if (copy_from_user(&info, addr, sizeof(info)))
284 return sb->s_qcop->set_info(sb, type, &info);
287 static int quota_getquota(struct super_block *sb, int type, qid_t id,
293 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
296 if (copy_to_user(addr, &idq, sizeof(idq)))
301 static int quota_setquota(struct super_block *sb, int type, qid_t id,
306 if (copy_from_user(&idq, addr, sizeof(idq)))
308 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
311 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
315 if (copy_from_user(&flags, addr, sizeof(flags)))
317 return sb->s_qcop->set_xstate(sb, flags, cmd);
320 static int quota_getxstate(struct super_block *sb, void __user *addr)
322 struct fs_quota_stat fqs;
325 ret = sb->s_qcop->get_xstate(sb, &fqs);
326 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
331 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
334 struct fs_disk_quota fdq;
336 if (copy_from_user(&fdq, addr, sizeof(fdq)))
338 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
341 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
344 struct fs_disk_quota fdq;
347 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
348 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
353 /* Copy parameters and call proper function */
354 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
359 return quota_quotaon(sb, type, cmd, id, addr);
361 return sb->s_qcop->quota_off(sb, type, 0);
363 return quota_getfmt(sb, type, addr);
365 return quota_getinfo(sb, type, addr);
367 return quota_setinfo(sb, type, addr);
369 return quota_getquota(sb, type, id, addr);
371 return quota_setquota(sb, type, id, addr);
374 sync_quota_sb(sb, type);
381 return quota_setxstate(sb, cmd, addr);
383 return quota_getxstate(sb, addr);
385 return quota_setxquota(sb, type, id, addr);
387 return quota_getxquota(sb, type, id, addr);
389 return sb->s_qcop->quota_sync(sb, type);
390 /* We never reach here unless validity check is broken */
399 * look up a superblock on which quota ops will be performed
400 * - use the name of a block device to find the superblock thereon
402 static struct super_block *quotactl_block(const char __user *special)
405 struct block_device *bdev;
406 struct super_block *sb;
407 char *tmp = getname(special);
410 return ERR_CAST(tmp);
411 bdev = lookup_bdev(tmp);
414 return ERR_CAST(bdev);
415 sb = get_super(bdev);
418 return ERR_PTR(-ENODEV);
422 return ERR_PTR(-ENODEV);
427 * This is the system call interface. This communicates with
428 * the user-level programs. Currently this only supports diskquota
429 * calls. Maybe we need to add the process quotas etc. in the future,
430 * but we probably should use rlimits for that.
432 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
433 qid_t, id, void __user *, addr)
436 struct super_block *sb = NULL;
439 cmds = cmd >> SUBCMDSHIFT;
440 type = cmd & SUBCMDMASK;
442 if (cmds != Q_SYNC || special) {
443 sb = quotactl_block(special);
448 ret = check_quotactl_valid(sb, type, cmds, id);
450 ret = do_quotactl(sb, type, cmds, id, addr);
457 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
459 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
460 * and is necessary due to alignment problems.
462 struct compat_if_dqblk {
463 compat_u64 dqb_bhardlimit;
464 compat_u64 dqb_bsoftlimit;
465 compat_u64 dqb_curspace;
466 compat_u64 dqb_ihardlimit;
467 compat_u64 dqb_isoftlimit;
468 compat_u64 dqb_curinodes;
469 compat_u64 dqb_btime;
470 compat_u64 dqb_itime;
471 compat_uint_t dqb_valid;
475 struct compat_fs_qfilestat {
476 compat_u64 dqb_bhardlimit;
477 compat_u64 qfs_nblks;
478 compat_uint_t qfs_nextents;
481 struct compat_fs_quota_stat {
485 struct compat_fs_qfilestat qs_uquota;
486 struct compat_fs_qfilestat qs_gquota;
487 compat_uint_t qs_incoredqs;
488 compat_int_t qs_btimelimit;
489 compat_int_t qs_itimelimit;
490 compat_int_t qs_rtbtimelimit;
495 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
496 qid_t id, void __user *addr)
499 struct if_dqblk __user *dqblk;
500 struct compat_if_dqblk __user *compat_dqblk;
501 struct fs_quota_stat __user *fsqstat;
502 struct compat_fs_quota_stat __user *compat_fsqstat;
507 cmds = cmd >> SUBCMDSHIFT;
511 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
513 ret = sys_quotactl(cmd, special, id, dqblk);
516 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
517 get_user(data, &dqblk->dqb_valid) ||
518 put_user(data, &compat_dqblk->dqb_valid))
522 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
525 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
526 get_user(data, &compat_dqblk->dqb_valid) ||
527 put_user(data, &dqblk->dqb_valid))
529 ret = sys_quotactl(cmd, special, id, dqblk);
532 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
533 compat_fsqstat = addr;
534 ret = sys_quotactl(cmd, special, id, fsqstat);
538 /* Copying qs_version, qs_flags, qs_pad */
539 if (copy_in_user(compat_fsqstat, fsqstat,
540 offsetof(struct compat_fs_quota_stat, qs_uquota)))
542 /* Copying qs_uquota */
543 if (copy_in_user(&compat_fsqstat->qs_uquota,
545 sizeof(compat_fsqstat->qs_uquota)) ||
546 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
547 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
549 /* Copying qs_gquota */
550 if (copy_in_user(&compat_fsqstat->qs_gquota,
552 sizeof(compat_fsqstat->qs_gquota)) ||
553 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
554 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
556 /* Copying the rest */
557 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
558 &fsqstat->qs_incoredqs,
559 sizeof(struct compat_fs_quota_stat) -
560 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
561 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
562 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
567 ret = sys_quotactl(cmd, special, id, addr);
574 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
576 /* Netlink family structure for quota */
577 static struct genl_family quota_genl_family = {
578 .id = GENL_ID_GENERATE,
582 .maxattr = QUOTA_NL_A_MAX,
586 * quota_send_warning - Send warning to userspace about exceeded quota
587 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
588 * @id: The user or group id of the quota that was exceeded
589 * @dev: The device on which the fs is mounted (sb->s_dev)
590 * @warntype: The type of the warning: QUOTA_NL_...
592 * This can be used by filesystems (including those which don't use
593 * dquot) to send a message to userspace relating to quota limits.
597 void quota_send_warning(short type, unsigned int id, dev_t dev,
604 int msg_size = 4 * nla_total_size(sizeof(u32)) +
605 2 * nla_total_size(sizeof(u64));
607 /* We have to allocate using GFP_NOFS as we are called from a
608 * filesystem performing write and thus further recursion into
609 * the fs to free some data could cause deadlocks. */
610 skb = genlmsg_new(msg_size, GFP_NOFS);
613 "VFS: Not enough memory to send quota warning.\n");
616 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
617 "a_genl_family, 0, QUOTA_NL_C_WARNING);
620 "VFS: Cannot store netlink header in quota warning.\n");
623 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
626 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
629 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
632 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
635 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
638 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
641 genlmsg_end(skb, msg_head);
643 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
646 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
650 EXPORT_SYMBOL(quota_send_warning);
652 static int __init quota_init(void)
654 if (genl_register_family("a_genl_family) != 0)
656 "VFS: Failed to create quota netlink interface.\n");
660 module_init(quota_init);