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)
36 /* Check privileges */
37 if (cmd == Q_GETQUOTA) {
38 if (((type == USRQUOTA && current_euid() != id) ||
39 (type == GRPQUOTA && !in_egroup_p(id))) &&
40 !capable(CAP_SYS_ADMIN))
43 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
44 if (!capable(CAP_SYS_ADMIN))
50 /* Check validity of XFS Quota Manager commands */
51 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd,
54 if (type >= XQM_MAXQUOTAS)
61 /* Check privileges */
62 if (cmd == Q_XGETQUOTA) {
63 if (((type == XQM_USRQUOTA && current_euid() != id) ||
64 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
65 !capable(CAP_SYS_ADMIN))
67 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
68 if (!capable(CAP_SYS_ADMIN))
75 static int check_quotactl_valid(struct super_block *sb, int type, int cmd,
81 error = xqm_quotactl_valid(sb, type, cmd, id);
83 error = generic_quotactl_valid(sb, type, cmd, id);
85 error = security_quotactl(cmd, type, id, sb);
90 void sync_quota_sb(struct super_block *sb, int type)
94 if (!sb->s_qcop->quota_sync)
97 sb->s_qcop->quota_sync(sb, type);
99 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
101 /* This is not very clever (and fast) but currently I don't know about
102 * any other simple way of getting quota data to disk and we must get
103 * them there for userspace to be visible... */
104 if (sb->s_op->sync_fs)
105 sb->s_op->sync_fs(sb, 1);
106 sync_blockdev(sb->s_bdev);
109 * Now when everything is written we can discard the pagecache so
110 * that userspace sees the changes.
112 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
113 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
114 if (type != -1 && cnt != type)
116 if (!sb_has_quota_active(sb, cnt))
118 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
120 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
121 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
123 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
127 static int quota_sync_all(int type)
129 struct super_block *sb;
133 if (type >= MAXQUOTAS)
135 ret = security_quotactl(Q_SYNC, type, 0, NULL);
141 list_for_each_entry(sb, &super_blocks, s_list) {
142 /* This test just improves performance so it needn't be
144 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
145 if (type != -1 && type != cnt)
147 if (!sb_has_quota_active(sb, cnt))
149 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
150 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
154 if (cnt == MAXQUOTAS)
157 spin_unlock(&sb_lock);
158 down_read(&sb->s_umount);
160 sync_quota_sb(sb, type);
161 up_read(&sb->s_umount);
163 if (__put_super_and_need_restart(sb))
166 spin_unlock(&sb_lock);
171 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
177 pathname = getname(addr);
178 if (IS_ERR(pathname))
179 return PTR_ERR(pathname);
180 if (sb->s_qcop->quota_on)
181 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
186 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
190 down_read(&sb_dqopt(sb)->dqptr_sem);
191 if (!sb_has_quota_active(sb, type)) {
192 up_read(&sb_dqopt(sb)->dqptr_sem);
195 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
196 up_read(&sb_dqopt(sb)->dqptr_sem);
197 if (copy_to_user(addr, &fmt, sizeof(fmt)))
202 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
204 struct if_dqinfo info;
207 if (!sb_has_quota_active(sb, type))
209 if (!sb->s_qcop->get_info)
211 ret = sb->s_qcop->get_info(sb, type, &info);
212 if (!ret && copy_to_user(addr, &info, sizeof(info)))
217 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
219 struct if_dqinfo info;
221 if (copy_from_user(&info, addr, sizeof(info)))
223 if (!sb_has_quota_active(sb, type))
225 if (!sb->s_qcop->set_info)
227 return sb->s_qcop->set_info(sb, type, &info);
230 static int quota_getquota(struct super_block *sb, int type, qid_t id,
236 if (!sb_has_quota_active(sb, type))
238 if (!sb->s_qcop->get_dqblk)
240 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
243 if (copy_to_user(addr, &idq, sizeof(idq)))
248 static int quota_setquota(struct super_block *sb, int type, qid_t id,
253 if (copy_from_user(&idq, addr, sizeof(idq)))
255 if (!sb_has_quota_active(sb, type))
257 if (!sb->s_qcop->set_dqblk)
259 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
262 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
266 if (copy_from_user(&flags, addr, sizeof(flags)))
268 if (!sb->s_qcop->set_xstate)
270 return sb->s_qcop->set_xstate(sb, flags, cmd);
273 static int quota_getxstate(struct super_block *sb, void __user *addr)
275 struct fs_quota_stat fqs;
278 if (!sb->s_qcop->get_xstate)
280 ret = sb->s_qcop->get_xstate(sb, &fqs);
281 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
286 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
289 struct fs_disk_quota fdq;
291 if (copy_from_user(&fdq, addr, sizeof(fdq)))
293 if (!sb->s_qcop->set_xquota)
295 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
298 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
301 struct fs_disk_quota fdq;
304 if (!sb->s_qcop->get_xquota)
306 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
307 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
312 /* Copy parameters and call proper function */
313 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
318 return quota_quotaon(sb, type, cmd, id, addr);
320 if (!sb->s_qcop->quota_off)
322 return sb->s_qcop->quota_off(sb, type, 0);
324 return quota_getfmt(sb, type, addr);
326 return quota_getinfo(sb, type, addr);
328 return quota_setinfo(sb, type, addr);
330 return quota_getquota(sb, type, id, addr);
332 return quota_setquota(sb, type, id, addr);
334 if (!sb->s_qcop->quota_sync)
336 sync_quota_sb(sb, type);
341 return quota_setxstate(sb, cmd, addr);
343 return quota_getxstate(sb, addr);
345 return quota_setxquota(sb, type, id, addr);
347 return quota_getxquota(sb, type, id, addr);
349 if (!sb->s_qcop->quota_sync)
351 return sb->s_qcop->quota_sync(sb, type);
358 * look up a superblock on which quota ops will be performed
359 * - use the name of a block device to find the superblock thereon
361 static struct super_block *quotactl_block(const char __user *special)
364 struct block_device *bdev;
365 struct super_block *sb;
366 char *tmp = getname(special);
369 return ERR_CAST(tmp);
370 bdev = lookup_bdev(tmp);
373 return ERR_CAST(bdev);
374 sb = get_super(bdev);
377 return ERR_PTR(-ENODEV);
381 return ERR_PTR(-ENODEV);
386 * This is the system call interface. This communicates with
387 * the user-level programs. Currently this only supports diskquota
388 * calls. Maybe we need to add the process quotas etc. in the future,
389 * but we probably should use rlimits for that.
391 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
392 qid_t, id, void __user *, addr)
395 struct super_block *sb = NULL;
398 cmds = cmd >> SUBCMDSHIFT;
399 type = cmd & SUBCMDMASK;
402 * As a special case Q_SYNC can be called without a specific device.
403 * It will iterate all superblocks that have quota enabled and call
404 * the sync action on each of them.
408 return quota_sync_all(type);
412 sb = quotactl_block(special);
416 ret = check_quotactl_valid(sb, type, cmds, id);
418 ret = do_quotactl(sb, type, cmds, id, addr);
424 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
426 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
427 * and is necessary due to alignment problems.
429 struct compat_if_dqblk {
430 compat_u64 dqb_bhardlimit;
431 compat_u64 dqb_bsoftlimit;
432 compat_u64 dqb_curspace;
433 compat_u64 dqb_ihardlimit;
434 compat_u64 dqb_isoftlimit;
435 compat_u64 dqb_curinodes;
436 compat_u64 dqb_btime;
437 compat_u64 dqb_itime;
438 compat_uint_t dqb_valid;
442 struct compat_fs_qfilestat {
443 compat_u64 dqb_bhardlimit;
444 compat_u64 qfs_nblks;
445 compat_uint_t qfs_nextents;
448 struct compat_fs_quota_stat {
452 struct compat_fs_qfilestat qs_uquota;
453 struct compat_fs_qfilestat qs_gquota;
454 compat_uint_t qs_incoredqs;
455 compat_int_t qs_btimelimit;
456 compat_int_t qs_itimelimit;
457 compat_int_t qs_rtbtimelimit;
462 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
463 qid_t id, void __user *addr)
466 struct if_dqblk __user *dqblk;
467 struct compat_if_dqblk __user *compat_dqblk;
468 struct fs_quota_stat __user *fsqstat;
469 struct compat_fs_quota_stat __user *compat_fsqstat;
474 cmds = cmd >> SUBCMDSHIFT;
478 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
480 ret = sys_quotactl(cmd, special, id, dqblk);
483 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
484 get_user(data, &dqblk->dqb_valid) ||
485 put_user(data, &compat_dqblk->dqb_valid))
489 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
492 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
493 get_user(data, &compat_dqblk->dqb_valid) ||
494 put_user(data, &dqblk->dqb_valid))
496 ret = sys_quotactl(cmd, special, id, dqblk);
499 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
500 compat_fsqstat = addr;
501 ret = sys_quotactl(cmd, special, id, fsqstat);
505 /* Copying qs_version, qs_flags, qs_pad */
506 if (copy_in_user(compat_fsqstat, fsqstat,
507 offsetof(struct compat_fs_quota_stat, qs_uquota)))
509 /* Copying qs_uquota */
510 if (copy_in_user(&compat_fsqstat->qs_uquota,
512 sizeof(compat_fsqstat->qs_uquota)) ||
513 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
514 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
516 /* Copying qs_gquota */
517 if (copy_in_user(&compat_fsqstat->qs_gquota,
519 sizeof(compat_fsqstat->qs_gquota)) ||
520 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
521 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
523 /* Copying the rest */
524 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
525 &fsqstat->qs_incoredqs,
526 sizeof(struct compat_fs_quota_stat) -
527 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
528 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
529 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
534 ret = sys_quotactl(cmd, special, id, addr);
541 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
543 /* Netlink family structure for quota */
544 static struct genl_family quota_genl_family = {
545 .id = GENL_ID_GENERATE,
549 .maxattr = QUOTA_NL_A_MAX,
553 * quota_send_warning - Send warning to userspace about exceeded quota
554 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
555 * @id: The user or group id of the quota that was exceeded
556 * @dev: The device on which the fs is mounted (sb->s_dev)
557 * @warntype: The type of the warning: QUOTA_NL_...
559 * This can be used by filesystems (including those which don't use
560 * dquot) to send a message to userspace relating to quota limits.
564 void quota_send_warning(short type, unsigned int id, dev_t dev,
571 int msg_size = 4 * nla_total_size(sizeof(u32)) +
572 2 * nla_total_size(sizeof(u64));
574 /* We have to allocate using GFP_NOFS as we are called from a
575 * filesystem performing write and thus further recursion into
576 * the fs to free some data could cause deadlocks. */
577 skb = genlmsg_new(msg_size, GFP_NOFS);
580 "VFS: Not enough memory to send quota warning.\n");
583 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
584 "a_genl_family, 0, QUOTA_NL_C_WARNING);
587 "VFS: Cannot store netlink header in quota warning.\n");
590 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
593 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
596 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
599 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
602 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
605 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
608 genlmsg_end(skb, msg_head);
610 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
613 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
617 EXPORT_SYMBOL(quota_send_warning);
619 static int __init quota_init(void)
621 if (genl_register_family("a_genl_family) != 0)
623 "VFS: Failed to create quota netlink interface.\n");
627 module_init(quota_init);