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 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
28 /* these commands do not require any special privilegues */
35 /* allow to query information for dquots we "own" */
38 if ((type == USRQUOTA && current_euid() == id) ||
39 (type == GRPQUOTA && in_egroup_p(id)))
43 if (!capable(CAP_SYS_ADMIN))
47 return security_quotactl(cmd, type, id, sb);
51 void sync_quota_sb(struct super_block *sb, int type)
55 if (!sb->s_qcop->quota_sync)
58 sb->s_qcop->quota_sync(sb, type);
60 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
62 /* This is not very clever (and fast) but currently I don't know about
63 * any other simple way of getting quota data to disk and we must get
64 * them there for userspace to be visible... */
65 if (sb->s_op->sync_fs)
66 sb->s_op->sync_fs(sb, 1);
67 sync_blockdev(sb->s_bdev);
70 * Now when everything is written we can discard the pagecache so
71 * that userspace sees the changes.
73 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
74 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
75 if (type != -1 && cnt != type)
77 if (!sb_has_quota_active(sb, cnt))
79 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
81 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
82 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
84 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
88 static int quota_sync_all(int type)
90 struct super_block *sb;
94 if (type >= MAXQUOTAS)
96 ret = security_quotactl(Q_SYNC, type, 0, NULL);
102 list_for_each_entry(sb, &super_blocks, s_list) {
103 /* This test just improves performance so it needn't be
105 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
106 if (type != -1 && type != cnt)
108 if (!sb_has_quota_active(sb, cnt))
110 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
111 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
115 if (cnt == MAXQUOTAS)
118 spin_unlock(&sb_lock);
119 down_read(&sb->s_umount);
121 sync_quota_sb(sb, type);
122 up_read(&sb->s_umount);
124 if (__put_super_and_need_restart(sb))
127 spin_unlock(&sb_lock);
132 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
138 pathname = getname(addr);
139 if (IS_ERR(pathname))
140 return PTR_ERR(pathname);
141 if (sb->s_qcop->quota_on)
142 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
147 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
151 down_read(&sb_dqopt(sb)->dqptr_sem);
152 if (!sb_has_quota_active(sb, type)) {
153 up_read(&sb_dqopt(sb)->dqptr_sem);
156 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
157 up_read(&sb_dqopt(sb)->dqptr_sem);
158 if (copy_to_user(addr, &fmt, sizeof(fmt)))
163 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
165 struct if_dqinfo info;
168 if (!sb_has_quota_active(sb, type))
170 if (!sb->s_qcop->get_info)
172 ret = sb->s_qcop->get_info(sb, type, &info);
173 if (!ret && copy_to_user(addr, &info, sizeof(info)))
178 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
180 struct if_dqinfo info;
182 if (copy_from_user(&info, addr, sizeof(info)))
184 if (!sb_has_quota_active(sb, type))
186 if (!sb->s_qcop->set_info)
188 return sb->s_qcop->set_info(sb, type, &info);
191 static int quota_getquota(struct super_block *sb, int type, qid_t id,
197 if (!sb_has_quota_active(sb, type))
199 if (!sb->s_qcop->get_dqblk)
201 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
204 if (copy_to_user(addr, &idq, sizeof(idq)))
209 static int quota_setquota(struct super_block *sb, int type, qid_t id,
214 if (copy_from_user(&idq, addr, sizeof(idq)))
216 if (!sb_has_quota_active(sb, type))
218 if (!sb->s_qcop->set_dqblk)
220 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
223 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
227 if (copy_from_user(&flags, addr, sizeof(flags)))
229 if (!sb->s_qcop->set_xstate)
231 return sb->s_qcop->set_xstate(sb, flags, cmd);
234 static int quota_getxstate(struct super_block *sb, void __user *addr)
236 struct fs_quota_stat fqs;
239 if (!sb->s_qcop->get_xstate)
241 ret = sb->s_qcop->get_xstate(sb, &fqs);
242 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
247 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
250 struct fs_disk_quota fdq;
252 if (copy_from_user(&fdq, addr, sizeof(fdq)))
254 if (!sb->s_qcop->set_xquota)
256 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
259 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
262 struct fs_disk_quota fdq;
265 if (!sb->s_qcop->get_xquota)
267 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
268 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
273 /* Copy parameters and call proper function */
274 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
279 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
284 ret = check_quotactl_permission(sb, type, cmd, id);
290 return quota_quotaon(sb, type, cmd, id, addr);
292 if (!sb->s_qcop->quota_off)
294 return sb->s_qcop->quota_off(sb, type, 0);
296 return quota_getfmt(sb, type, addr);
298 return quota_getinfo(sb, type, addr);
300 return quota_setinfo(sb, type, addr);
302 return quota_getquota(sb, type, id, addr);
304 return quota_setquota(sb, type, id, addr);
306 if (!sb->s_qcop->quota_sync)
308 sync_quota_sb(sb, type);
313 return quota_setxstate(sb, cmd, addr);
315 return quota_getxstate(sb, addr);
317 return quota_setxquota(sb, type, id, addr);
319 return quota_getxquota(sb, type, id, addr);
321 if (!sb->s_qcop->quota_sync)
323 return sb->s_qcop->quota_sync(sb, type);
330 * look up a superblock on which quota ops will be performed
331 * - use the name of a block device to find the superblock thereon
333 static struct super_block *quotactl_block(const char __user *special)
336 struct block_device *bdev;
337 struct super_block *sb;
338 char *tmp = getname(special);
341 return ERR_CAST(tmp);
342 bdev = lookup_bdev(tmp);
345 return ERR_CAST(bdev);
346 sb = get_super(bdev);
349 return ERR_PTR(-ENODEV);
353 return ERR_PTR(-ENODEV);
358 * This is the system call interface. This communicates with
359 * the user-level programs. Currently this only supports diskquota
360 * calls. Maybe we need to add the process quotas etc. in the future,
361 * but we probably should use rlimits for that.
363 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
364 qid_t, id, void __user *, addr)
367 struct super_block *sb = NULL;
370 cmds = cmd >> SUBCMDSHIFT;
371 type = cmd & SUBCMDMASK;
374 * As a special case Q_SYNC can be called without a specific device.
375 * It will iterate all superblocks that have quota enabled and call
376 * the sync action on each of them.
380 return quota_sync_all(type);
384 sb = quotactl_block(special);
388 ret = do_quotactl(sb, type, cmds, id, addr);
394 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
396 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
397 * and is necessary due to alignment problems.
399 struct compat_if_dqblk {
400 compat_u64 dqb_bhardlimit;
401 compat_u64 dqb_bsoftlimit;
402 compat_u64 dqb_curspace;
403 compat_u64 dqb_ihardlimit;
404 compat_u64 dqb_isoftlimit;
405 compat_u64 dqb_curinodes;
406 compat_u64 dqb_btime;
407 compat_u64 dqb_itime;
408 compat_uint_t dqb_valid;
412 struct compat_fs_qfilestat {
413 compat_u64 dqb_bhardlimit;
414 compat_u64 qfs_nblks;
415 compat_uint_t qfs_nextents;
418 struct compat_fs_quota_stat {
422 struct compat_fs_qfilestat qs_uquota;
423 struct compat_fs_qfilestat qs_gquota;
424 compat_uint_t qs_incoredqs;
425 compat_int_t qs_btimelimit;
426 compat_int_t qs_itimelimit;
427 compat_int_t qs_rtbtimelimit;
432 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
433 qid_t id, void __user *addr)
436 struct if_dqblk __user *dqblk;
437 struct compat_if_dqblk __user *compat_dqblk;
438 struct fs_quota_stat __user *fsqstat;
439 struct compat_fs_quota_stat __user *compat_fsqstat;
444 cmds = cmd >> SUBCMDSHIFT;
448 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
450 ret = sys_quotactl(cmd, special, id, dqblk);
453 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
454 get_user(data, &dqblk->dqb_valid) ||
455 put_user(data, &compat_dqblk->dqb_valid))
459 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
462 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
463 get_user(data, &compat_dqblk->dqb_valid) ||
464 put_user(data, &dqblk->dqb_valid))
466 ret = sys_quotactl(cmd, special, id, dqblk);
469 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
470 compat_fsqstat = addr;
471 ret = sys_quotactl(cmd, special, id, fsqstat);
475 /* Copying qs_version, qs_flags, qs_pad */
476 if (copy_in_user(compat_fsqstat, fsqstat,
477 offsetof(struct compat_fs_quota_stat, qs_uquota)))
479 /* Copying qs_uquota */
480 if (copy_in_user(&compat_fsqstat->qs_uquota,
482 sizeof(compat_fsqstat->qs_uquota)) ||
483 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
484 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
486 /* Copying qs_gquota */
487 if (copy_in_user(&compat_fsqstat->qs_gquota,
489 sizeof(compat_fsqstat->qs_gquota)) ||
490 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
491 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
493 /* Copying the rest */
494 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
495 &fsqstat->qs_incoredqs,
496 sizeof(struct compat_fs_quota_stat) -
497 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
498 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
499 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
504 ret = sys_quotactl(cmd, special, id, addr);
511 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
513 /* Netlink family structure for quota */
514 static struct genl_family quota_genl_family = {
515 .id = GENL_ID_GENERATE,
519 .maxattr = QUOTA_NL_A_MAX,
523 * quota_send_warning - Send warning to userspace about exceeded quota
524 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
525 * @id: The user or group id of the quota that was exceeded
526 * @dev: The device on which the fs is mounted (sb->s_dev)
527 * @warntype: The type of the warning: QUOTA_NL_...
529 * This can be used by filesystems (including those which don't use
530 * dquot) to send a message to userspace relating to quota limits.
534 void quota_send_warning(short type, unsigned int id, dev_t dev,
541 int msg_size = 4 * nla_total_size(sizeof(u32)) +
542 2 * nla_total_size(sizeof(u64));
544 /* We have to allocate using GFP_NOFS as we are called from a
545 * filesystem performing write and thus further recursion into
546 * the fs to free some data could cause deadlocks. */
547 skb = genlmsg_new(msg_size, GFP_NOFS);
550 "VFS: Not enough memory to send quota warning.\n");
553 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
554 "a_genl_family, 0, QUOTA_NL_C_WARNING);
557 "VFS: Cannot store netlink header in quota warning.\n");
560 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
563 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
566 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
569 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
572 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
575 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
578 genlmsg_end(skb, msg_head);
580 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
583 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
587 EXPORT_SYMBOL(quota_send_warning);
589 static int __init quota_init(void)
591 if (genl_register_family("a_genl_family) != 0)
593 "VFS: Failed to create quota netlink interface.\n");
597 module_init(quota_init);