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 <linux/writeback.h>
22 #include <net/netlink.h>
23 #include <net/genetlink.h>
25 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
29 /* these commands do not require any special privilegues */
36 /* allow to query information for dquots we "own" */
39 if ((type == USRQUOTA && current_euid() == id) ||
40 (type == GRPQUOTA && in_egroup_p(id)))
44 if (!capable(CAP_SYS_ADMIN))
48 return security_quotactl(cmd, type, id, sb);
52 void sync_quota_sb(struct super_block *sb, int type)
56 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
59 sb->s_qcop->quota_sync(sb, type);
61 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
63 /* This is not very clever (and fast) but currently I don't know about
64 * any other simple way of getting quota data to disk and we must get
65 * them there for userspace to be visible... */
66 if (sb->s_op->sync_fs)
67 sb->s_op->sync_fs(sb, 1);
68 sync_blockdev(sb->s_bdev);
71 * Now when everything is written we can discard the pagecache so
72 * that userspace sees the changes.
74 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
75 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
76 if (type != -1 && cnt != type)
78 if (!sb_has_quota_active(sb, cnt))
80 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
82 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
83 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
85 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
89 static int quota_sync_all(int type)
91 struct super_block *sb;
95 if (type >= MAXQUOTAS)
97 ret = security_quotactl(Q_SYNC, type, 0, NULL);
103 list_for_each_entry(sb, &super_blocks, s_list) {
104 /* This test just improves performance so it needn't be
106 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
107 if (type != -1 && type != cnt)
109 if (!sb_has_quota_active(sb, cnt))
111 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
112 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
116 if (cnt == MAXQUOTAS)
119 spin_unlock(&sb_lock);
120 down_read(&sb->s_umount);
122 sync_quota_sb(sb, type);
123 up_read(&sb->s_umount);
125 if (__put_super_and_need_restart(sb))
128 spin_unlock(&sb_lock);
133 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
139 pathname = getname(addr);
140 if (IS_ERR(pathname))
141 return PTR_ERR(pathname);
142 if (sb->s_qcop->quota_on)
143 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
148 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
152 down_read(&sb_dqopt(sb)->dqptr_sem);
153 if (!sb_has_quota_active(sb, type)) {
154 up_read(&sb_dqopt(sb)->dqptr_sem);
157 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
158 up_read(&sb_dqopt(sb)->dqptr_sem);
159 if (copy_to_user(addr, &fmt, sizeof(fmt)))
164 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
166 struct if_dqinfo info;
169 if (!sb_has_quota_active(sb, type))
171 if (!sb->s_qcop->get_info)
173 ret = sb->s_qcop->get_info(sb, type, &info);
174 if (!ret && copy_to_user(addr, &info, sizeof(info)))
179 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
181 struct if_dqinfo info;
183 if (copy_from_user(&info, addr, sizeof(info)))
185 if (!sb_has_quota_active(sb, type))
187 if (!sb->s_qcop->set_info)
189 return sb->s_qcop->set_info(sb, type, &info);
192 static int quota_getquota(struct super_block *sb, int type, qid_t id,
198 if (!sb_has_quota_active(sb, type))
200 if (!sb->s_qcop->get_dqblk)
202 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
205 if (copy_to_user(addr, &idq, sizeof(idq)))
210 static int quota_setquota(struct super_block *sb, int type, qid_t id,
215 if (copy_from_user(&idq, addr, sizeof(idq)))
217 if (!sb_has_quota_active(sb, type))
219 if (!sb->s_qcop->set_dqblk)
221 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
224 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
228 if (copy_from_user(&flags, addr, sizeof(flags)))
230 if (!sb->s_qcop->set_xstate)
232 return sb->s_qcop->set_xstate(sb, flags, cmd);
235 static int quota_getxstate(struct super_block *sb, void __user *addr)
237 struct fs_quota_stat fqs;
240 if (!sb->s_qcop->get_xstate)
242 ret = sb->s_qcop->get_xstate(sb, &fqs);
243 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
248 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
251 struct fs_disk_quota fdq;
253 if (copy_from_user(&fdq, addr, sizeof(fdq)))
255 if (!sb->s_qcop->set_xquota)
257 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
260 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
263 struct fs_disk_quota fdq;
266 if (!sb->s_qcop->get_xquota)
268 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
269 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
274 /* Copy parameters and call proper function */
275 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
280 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
285 ret = check_quotactl_permission(sb, type, cmd, id);
291 return quota_quotaon(sb, type, cmd, id, addr);
293 if (!sb->s_qcop->quota_off)
295 return sb->s_qcop->quota_off(sb, type, 0);
297 return quota_getfmt(sb, type, addr);
299 return quota_getinfo(sb, type, addr);
301 return quota_setinfo(sb, type, addr);
303 return quota_getquota(sb, type, id, addr);
305 return quota_setquota(sb, type, id, addr);
307 if (!sb->s_qcop->quota_sync)
309 sync_quota_sb(sb, type);
314 return quota_setxstate(sb, cmd, addr);
316 return quota_getxstate(sb, addr);
318 return quota_setxquota(sb, type, id, addr);
320 return quota_getxquota(sb, type, id, addr);
322 /* caller already holds s_umount */
323 if (sb->s_flags & MS_RDONLY)
325 writeback_inodes_sb(sb);
333 * look up a superblock on which quota ops will be performed
334 * - use the name of a block device to find the superblock thereon
336 static struct super_block *quotactl_block(const char __user *special)
339 struct block_device *bdev;
340 struct super_block *sb;
341 char *tmp = getname(special);
344 return ERR_CAST(tmp);
345 bdev = lookup_bdev(tmp);
348 return ERR_CAST(bdev);
349 sb = get_super(bdev);
352 return ERR_PTR(-ENODEV);
356 return ERR_PTR(-ENODEV);
361 * This is the system call interface. This communicates with
362 * the user-level programs. Currently this only supports diskquota
363 * calls. Maybe we need to add the process quotas etc. in the future,
364 * but we probably should use rlimits for that.
366 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
367 qid_t, id, void __user *, addr)
370 struct super_block *sb = NULL;
373 cmds = cmd >> SUBCMDSHIFT;
374 type = cmd & SUBCMDMASK;
377 * As a special case Q_SYNC can be called without a specific device.
378 * It will iterate all superblocks that have quota enabled and call
379 * the sync action on each of them.
383 return quota_sync_all(type);
387 sb = quotactl_block(special);
391 ret = do_quotactl(sb, type, cmds, id, addr);
397 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
399 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
400 * and is necessary due to alignment problems.
402 struct compat_if_dqblk {
403 compat_u64 dqb_bhardlimit;
404 compat_u64 dqb_bsoftlimit;
405 compat_u64 dqb_curspace;
406 compat_u64 dqb_ihardlimit;
407 compat_u64 dqb_isoftlimit;
408 compat_u64 dqb_curinodes;
409 compat_u64 dqb_btime;
410 compat_u64 dqb_itime;
411 compat_uint_t dqb_valid;
415 struct compat_fs_qfilestat {
416 compat_u64 dqb_bhardlimit;
417 compat_u64 qfs_nblks;
418 compat_uint_t qfs_nextents;
421 struct compat_fs_quota_stat {
425 struct compat_fs_qfilestat qs_uquota;
426 struct compat_fs_qfilestat qs_gquota;
427 compat_uint_t qs_incoredqs;
428 compat_int_t qs_btimelimit;
429 compat_int_t qs_itimelimit;
430 compat_int_t qs_rtbtimelimit;
435 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
436 qid_t id, void __user *addr)
439 struct if_dqblk __user *dqblk;
440 struct compat_if_dqblk __user *compat_dqblk;
441 struct fs_quota_stat __user *fsqstat;
442 struct compat_fs_quota_stat __user *compat_fsqstat;
447 cmds = cmd >> SUBCMDSHIFT;
451 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
453 ret = sys_quotactl(cmd, special, id, dqblk);
456 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
457 get_user(data, &dqblk->dqb_valid) ||
458 put_user(data, &compat_dqblk->dqb_valid))
462 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
465 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
466 get_user(data, &compat_dqblk->dqb_valid) ||
467 put_user(data, &dqblk->dqb_valid))
469 ret = sys_quotactl(cmd, special, id, dqblk);
472 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
473 compat_fsqstat = addr;
474 ret = sys_quotactl(cmd, special, id, fsqstat);
478 /* Copying qs_version, qs_flags, qs_pad */
479 if (copy_in_user(compat_fsqstat, fsqstat,
480 offsetof(struct compat_fs_quota_stat, qs_uquota)))
482 /* Copying qs_uquota */
483 if (copy_in_user(&compat_fsqstat->qs_uquota,
485 sizeof(compat_fsqstat->qs_uquota)) ||
486 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
487 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
489 /* Copying qs_gquota */
490 if (copy_in_user(&compat_fsqstat->qs_gquota,
492 sizeof(compat_fsqstat->qs_gquota)) ||
493 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
494 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
496 /* Copying the rest */
497 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
498 &fsqstat->qs_incoredqs,
499 sizeof(struct compat_fs_quota_stat) -
500 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
501 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
502 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
507 ret = sys_quotactl(cmd, special, id, addr);
514 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
516 /* Netlink family structure for quota */
517 static struct genl_family quota_genl_family = {
518 .id = GENL_ID_GENERATE,
522 .maxattr = QUOTA_NL_A_MAX,
526 * quota_send_warning - Send warning to userspace about exceeded quota
527 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
528 * @id: The user or group id of the quota that was exceeded
529 * @dev: The device on which the fs is mounted (sb->s_dev)
530 * @warntype: The type of the warning: QUOTA_NL_...
532 * This can be used by filesystems (including those which don't use
533 * dquot) to send a message to userspace relating to quota limits.
537 void quota_send_warning(short type, unsigned int id, dev_t dev,
544 int msg_size = 4 * nla_total_size(sizeof(u32)) +
545 2 * nla_total_size(sizeof(u64));
547 /* We have to allocate using GFP_NOFS as we are called from a
548 * filesystem performing write and thus further recursion into
549 * the fs to free some data could cause deadlocks. */
550 skb = genlmsg_new(msg_size, GFP_NOFS);
553 "VFS: Not enough memory to send quota warning.\n");
556 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
557 "a_genl_family, 0, QUOTA_NL_C_WARNING);
560 "VFS: Cannot store netlink header in quota warning.\n");
563 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
566 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
569 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
572 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
575 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
578 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
581 genlmsg_end(skb, msg_head);
583 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
586 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
590 EXPORT_SYMBOL(quota_send_warning);
592 static int __init quota_init(void)
594 if (genl_register_family("a_genl_family) != 0)
596 "VFS: Failed to create quota netlink interface.\n");
600 module_init(quota_init);