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);
51 static int quota_sync_all(int type)
53 struct super_block *sb;
57 if (type >= MAXQUOTAS)
59 ret = security_quotactl(Q_SYNC, type, 0, NULL);
65 list_for_each_entry(sb, &super_blocks, s_list) {
66 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
69 /* This test just improves performance so it needn't be
71 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
72 if (type != -1 && type != cnt)
74 if (!sb_has_quota_active(sb, cnt))
76 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
77 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
84 spin_unlock(&sb_lock);
85 down_read(&sb->s_umount);
87 sb->s_qcop->quota_sync(sb, type, 1);
88 up_read(&sb->s_umount);
90 if (__put_super_and_need_restart(sb))
93 spin_unlock(&sb_lock);
98 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
104 pathname = getname(addr);
105 if (IS_ERR(pathname))
106 return PTR_ERR(pathname);
107 if (sb->s_qcop->quota_on)
108 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
113 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
117 down_read(&sb_dqopt(sb)->dqptr_sem);
118 if (!sb_has_quota_active(sb, type)) {
119 up_read(&sb_dqopt(sb)->dqptr_sem);
122 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
123 up_read(&sb_dqopt(sb)->dqptr_sem);
124 if (copy_to_user(addr, &fmt, sizeof(fmt)))
129 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
131 struct if_dqinfo info;
134 if (!sb_has_quota_active(sb, type))
136 if (!sb->s_qcop->get_info)
138 ret = sb->s_qcop->get_info(sb, type, &info);
139 if (!ret && copy_to_user(addr, &info, sizeof(info)))
144 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
146 struct if_dqinfo info;
148 if (copy_from_user(&info, addr, sizeof(info)))
150 if (!sb_has_quota_active(sb, type))
152 if (!sb->s_qcop->set_info)
154 return sb->s_qcop->set_info(sb, type, &info);
157 static int quota_getquota(struct super_block *sb, int type, qid_t id,
163 if (!sb_has_quota_active(sb, type))
165 if (!sb->s_qcop->get_dqblk)
167 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
170 if (copy_to_user(addr, &idq, sizeof(idq)))
175 static int quota_setquota(struct super_block *sb, int type, qid_t id,
180 if (copy_from_user(&idq, addr, sizeof(idq)))
182 if (!sb_has_quota_active(sb, type))
184 if (!sb->s_qcop->set_dqblk)
186 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
189 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
193 if (copy_from_user(&flags, addr, sizeof(flags)))
195 if (!sb->s_qcop->set_xstate)
197 return sb->s_qcop->set_xstate(sb, flags, cmd);
200 static int quota_getxstate(struct super_block *sb, void __user *addr)
202 struct fs_quota_stat fqs;
205 if (!sb->s_qcop->get_xstate)
207 ret = sb->s_qcop->get_xstate(sb, &fqs);
208 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
213 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
216 struct fs_disk_quota fdq;
218 if (copy_from_user(&fdq, addr, sizeof(fdq)))
220 if (!sb->s_qcop->set_xquota)
222 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
225 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
228 struct fs_disk_quota fdq;
231 if (!sb->s_qcop->get_xquota)
233 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
234 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
239 /* Copy parameters and call proper function */
240 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
245 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
250 ret = check_quotactl_permission(sb, type, cmd, id);
256 return quota_quotaon(sb, type, cmd, id, addr);
258 if (!sb->s_qcop->quota_off)
260 return sb->s_qcop->quota_off(sb, type, 0);
262 return quota_getfmt(sb, type, addr);
264 return quota_getinfo(sb, type, addr);
266 return quota_setinfo(sb, type, addr);
268 return quota_getquota(sb, type, id, addr);
270 return quota_setquota(sb, type, id, addr);
272 if (!sb->s_qcop->quota_sync)
274 return sb->s_qcop->quota_sync(sb, type, 1);
278 return quota_setxstate(sb, cmd, addr);
280 return quota_getxstate(sb, addr);
282 return quota_setxquota(sb, type, id, addr);
284 return quota_getxquota(sb, type, id, addr);
286 /* caller already holds s_umount */
287 if (sb->s_flags & MS_RDONLY)
289 writeback_inodes_sb(sb);
297 * look up a superblock on which quota ops will be performed
298 * - use the name of a block device to find the superblock thereon
300 static struct super_block *quotactl_block(const char __user *special)
303 struct block_device *bdev;
304 struct super_block *sb;
305 char *tmp = getname(special);
308 return ERR_CAST(tmp);
309 bdev = lookup_bdev(tmp);
312 return ERR_CAST(bdev);
313 sb = get_super(bdev);
316 return ERR_PTR(-ENODEV);
320 return ERR_PTR(-ENODEV);
325 * This is the system call interface. This communicates with
326 * the user-level programs. Currently this only supports diskquota
327 * calls. Maybe we need to add the process quotas etc. in the future,
328 * but we probably should use rlimits for that.
330 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
331 qid_t, id, void __user *, addr)
334 struct super_block *sb = NULL;
337 cmds = cmd >> SUBCMDSHIFT;
338 type = cmd & SUBCMDMASK;
341 * As a special case Q_SYNC can be called without a specific device.
342 * It will iterate all superblocks that have quota enabled and call
343 * the sync action on each of them.
347 return quota_sync_all(type);
351 sb = quotactl_block(special);
355 ret = do_quotactl(sb, type, cmds, id, addr);
361 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
363 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
364 * and is necessary due to alignment problems.
366 struct compat_if_dqblk {
367 compat_u64 dqb_bhardlimit;
368 compat_u64 dqb_bsoftlimit;
369 compat_u64 dqb_curspace;
370 compat_u64 dqb_ihardlimit;
371 compat_u64 dqb_isoftlimit;
372 compat_u64 dqb_curinodes;
373 compat_u64 dqb_btime;
374 compat_u64 dqb_itime;
375 compat_uint_t dqb_valid;
379 struct compat_fs_qfilestat {
380 compat_u64 dqb_bhardlimit;
381 compat_u64 qfs_nblks;
382 compat_uint_t qfs_nextents;
385 struct compat_fs_quota_stat {
389 struct compat_fs_qfilestat qs_uquota;
390 struct compat_fs_qfilestat qs_gquota;
391 compat_uint_t qs_incoredqs;
392 compat_int_t qs_btimelimit;
393 compat_int_t qs_itimelimit;
394 compat_int_t qs_rtbtimelimit;
399 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
400 qid_t id, void __user *addr)
403 struct if_dqblk __user *dqblk;
404 struct compat_if_dqblk __user *compat_dqblk;
405 struct fs_quota_stat __user *fsqstat;
406 struct compat_fs_quota_stat __user *compat_fsqstat;
411 cmds = cmd >> SUBCMDSHIFT;
415 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
417 ret = sys_quotactl(cmd, special, id, dqblk);
420 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
421 get_user(data, &dqblk->dqb_valid) ||
422 put_user(data, &compat_dqblk->dqb_valid))
426 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
429 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
430 get_user(data, &compat_dqblk->dqb_valid) ||
431 put_user(data, &dqblk->dqb_valid))
433 ret = sys_quotactl(cmd, special, id, dqblk);
436 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
437 compat_fsqstat = addr;
438 ret = sys_quotactl(cmd, special, id, fsqstat);
442 /* Copying qs_version, qs_flags, qs_pad */
443 if (copy_in_user(compat_fsqstat, fsqstat,
444 offsetof(struct compat_fs_quota_stat, qs_uquota)))
446 /* Copying qs_uquota */
447 if (copy_in_user(&compat_fsqstat->qs_uquota,
449 sizeof(compat_fsqstat->qs_uquota)) ||
450 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
451 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
453 /* Copying qs_gquota */
454 if (copy_in_user(&compat_fsqstat->qs_gquota,
456 sizeof(compat_fsqstat->qs_gquota)) ||
457 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
458 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
460 /* Copying the rest */
461 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
462 &fsqstat->qs_incoredqs,
463 sizeof(struct compat_fs_quota_stat) -
464 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
465 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
466 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
471 ret = sys_quotactl(cmd, special, id, addr);
478 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
480 /* Netlink family structure for quota */
481 static struct genl_family quota_genl_family = {
482 .id = GENL_ID_GENERATE,
486 .maxattr = QUOTA_NL_A_MAX,
490 * quota_send_warning - Send warning to userspace about exceeded quota
491 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
492 * @id: The user or group id of the quota that was exceeded
493 * @dev: The device on which the fs is mounted (sb->s_dev)
494 * @warntype: The type of the warning: QUOTA_NL_...
496 * This can be used by filesystems (including those which don't use
497 * dquot) to send a message to userspace relating to quota limits.
501 void quota_send_warning(short type, unsigned int id, dev_t dev,
508 int msg_size = 4 * nla_total_size(sizeof(u32)) +
509 2 * nla_total_size(sizeof(u64));
511 /* We have to allocate using GFP_NOFS as we are called from a
512 * filesystem performing write and thus further recursion into
513 * the fs to free some data could cause deadlocks. */
514 skb = genlmsg_new(msg_size, GFP_NOFS);
517 "VFS: Not enough memory to send quota warning.\n");
520 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
521 "a_genl_family, 0, QUOTA_NL_C_WARNING);
524 "VFS: Cannot store netlink header in quota warning.\n");
527 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
530 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
533 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
536 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
539 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
542 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
545 genlmsg_end(skb, msg_head);
547 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
550 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
554 EXPORT_SYMBOL(quota_send_warning);
556 static int __init quota_init(void)
558 if (genl_register_family("a_genl_family) != 0)
560 "VFS: Failed to create quota netlink interface.\n");
564 module_init(quota_init);