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;
56 if (type >= MAXQUOTAS)
58 ret = security_quotactl(Q_SYNC, type, 0, NULL);
64 list_for_each_entry(sb, &super_blocks, s_list) {
65 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
69 spin_unlock(&sb_lock);
70 down_read(&sb->s_umount);
72 sb->s_qcop->quota_sync(sb, type, 1);
73 up_read(&sb->s_umount);
75 if (__put_super_and_need_restart(sb))
78 spin_unlock(&sb_lock);
83 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
89 pathname = getname(addr);
91 return PTR_ERR(pathname);
92 if (sb->s_qcop->quota_on)
93 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
98 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
102 down_read(&sb_dqopt(sb)->dqptr_sem);
103 if (!sb_has_quota_active(sb, type)) {
104 up_read(&sb_dqopt(sb)->dqptr_sem);
107 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
108 up_read(&sb_dqopt(sb)->dqptr_sem);
109 if (copy_to_user(addr, &fmt, sizeof(fmt)))
114 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
116 struct if_dqinfo info;
119 if (!sb_has_quota_active(sb, type))
121 if (!sb->s_qcop->get_info)
123 ret = sb->s_qcop->get_info(sb, type, &info);
124 if (!ret && copy_to_user(addr, &info, sizeof(info)))
129 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
131 struct if_dqinfo info;
133 if (copy_from_user(&info, addr, sizeof(info)))
135 if (!sb_has_quota_active(sb, type))
137 if (!sb->s_qcop->set_info)
139 return sb->s_qcop->set_info(sb, type, &info);
142 static int quota_getquota(struct super_block *sb, int type, qid_t id,
148 if (!sb_has_quota_active(sb, type))
150 if (!sb->s_qcop->get_dqblk)
152 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
155 if (copy_to_user(addr, &idq, sizeof(idq)))
160 static int quota_setquota(struct super_block *sb, int type, qid_t id,
165 if (copy_from_user(&idq, addr, sizeof(idq)))
167 if (!sb_has_quota_active(sb, type))
169 if (!sb->s_qcop->set_dqblk)
171 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
174 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
178 if (copy_from_user(&flags, addr, sizeof(flags)))
180 if (!sb->s_qcop->set_xstate)
182 return sb->s_qcop->set_xstate(sb, flags, cmd);
185 static int quota_getxstate(struct super_block *sb, void __user *addr)
187 struct fs_quota_stat fqs;
190 if (!sb->s_qcop->get_xstate)
192 ret = sb->s_qcop->get_xstate(sb, &fqs);
193 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
198 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
201 struct fs_disk_quota fdq;
203 if (copy_from_user(&fdq, addr, sizeof(fdq)))
205 if (!sb->s_qcop->set_xquota)
207 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
210 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
213 struct fs_disk_quota fdq;
216 if (!sb->s_qcop->get_xquota)
218 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
219 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
224 /* Copy parameters and call proper function */
225 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
230 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
235 ret = check_quotactl_permission(sb, type, cmd, id);
241 return quota_quotaon(sb, type, cmd, id, addr);
243 if (!sb->s_qcop->quota_off)
245 return sb->s_qcop->quota_off(sb, type, 0);
247 return quota_getfmt(sb, type, addr);
249 return quota_getinfo(sb, type, addr);
251 return quota_setinfo(sb, type, addr);
253 return quota_getquota(sb, type, id, addr);
255 return quota_setquota(sb, type, id, addr);
257 if (!sb->s_qcop->quota_sync)
259 return sb->s_qcop->quota_sync(sb, type, 1);
263 return quota_setxstate(sb, cmd, addr);
265 return quota_getxstate(sb, addr);
267 return quota_setxquota(sb, type, id, addr);
269 return quota_getxquota(sb, type, id, addr);
271 /* caller already holds s_umount */
272 if (sb->s_flags & MS_RDONLY)
274 writeback_inodes_sb(sb);
282 * look up a superblock on which quota ops will be performed
283 * - use the name of a block device to find the superblock thereon
285 static struct super_block *quotactl_block(const char __user *special)
288 struct block_device *bdev;
289 struct super_block *sb;
290 char *tmp = getname(special);
293 return ERR_CAST(tmp);
294 bdev = lookup_bdev(tmp);
297 return ERR_CAST(bdev);
298 sb = get_super(bdev);
301 return ERR_PTR(-ENODEV);
305 return ERR_PTR(-ENODEV);
310 * This is the system call interface. This communicates with
311 * the user-level programs. Currently this only supports diskquota
312 * calls. Maybe we need to add the process quotas etc. in the future,
313 * but we probably should use rlimits for that.
315 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
316 qid_t, id, void __user *, addr)
319 struct super_block *sb = NULL;
322 cmds = cmd >> SUBCMDSHIFT;
323 type = cmd & SUBCMDMASK;
326 * As a special case Q_SYNC can be called without a specific device.
327 * It will iterate all superblocks that have quota enabled and call
328 * the sync action on each of them.
332 return quota_sync_all(type);
336 sb = quotactl_block(special);
340 ret = do_quotactl(sb, type, cmds, id, addr);
346 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
348 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
349 * and is necessary due to alignment problems.
351 struct compat_if_dqblk {
352 compat_u64 dqb_bhardlimit;
353 compat_u64 dqb_bsoftlimit;
354 compat_u64 dqb_curspace;
355 compat_u64 dqb_ihardlimit;
356 compat_u64 dqb_isoftlimit;
357 compat_u64 dqb_curinodes;
358 compat_u64 dqb_btime;
359 compat_u64 dqb_itime;
360 compat_uint_t dqb_valid;
364 struct compat_fs_qfilestat {
365 compat_u64 dqb_bhardlimit;
366 compat_u64 qfs_nblks;
367 compat_uint_t qfs_nextents;
370 struct compat_fs_quota_stat {
374 struct compat_fs_qfilestat qs_uquota;
375 struct compat_fs_qfilestat qs_gquota;
376 compat_uint_t qs_incoredqs;
377 compat_int_t qs_btimelimit;
378 compat_int_t qs_itimelimit;
379 compat_int_t qs_rtbtimelimit;
384 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
385 qid_t id, void __user *addr)
388 struct if_dqblk __user *dqblk;
389 struct compat_if_dqblk __user *compat_dqblk;
390 struct fs_quota_stat __user *fsqstat;
391 struct compat_fs_quota_stat __user *compat_fsqstat;
396 cmds = cmd >> SUBCMDSHIFT;
400 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
402 ret = sys_quotactl(cmd, special, id, dqblk);
405 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
406 get_user(data, &dqblk->dqb_valid) ||
407 put_user(data, &compat_dqblk->dqb_valid))
411 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
414 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
415 get_user(data, &compat_dqblk->dqb_valid) ||
416 put_user(data, &dqblk->dqb_valid))
418 ret = sys_quotactl(cmd, special, id, dqblk);
421 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
422 compat_fsqstat = addr;
423 ret = sys_quotactl(cmd, special, id, fsqstat);
427 /* Copying qs_version, qs_flags, qs_pad */
428 if (copy_in_user(compat_fsqstat, fsqstat,
429 offsetof(struct compat_fs_quota_stat, qs_uquota)))
431 /* Copying qs_uquota */
432 if (copy_in_user(&compat_fsqstat->qs_uquota,
434 sizeof(compat_fsqstat->qs_uquota)) ||
435 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
436 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
438 /* Copying qs_gquota */
439 if (copy_in_user(&compat_fsqstat->qs_gquota,
441 sizeof(compat_fsqstat->qs_gquota)) ||
442 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
443 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
445 /* Copying the rest */
446 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
447 &fsqstat->qs_incoredqs,
448 sizeof(struct compat_fs_quota_stat) -
449 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
450 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
451 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
456 ret = sys_quotactl(cmd, special, id, addr);
463 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
465 /* Netlink family structure for quota */
466 static struct genl_family quota_genl_family = {
467 .id = GENL_ID_GENERATE,
471 .maxattr = QUOTA_NL_A_MAX,
475 * quota_send_warning - Send warning to userspace about exceeded quota
476 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
477 * @id: The user or group id of the quota that was exceeded
478 * @dev: The device on which the fs is mounted (sb->s_dev)
479 * @warntype: The type of the warning: QUOTA_NL_...
481 * This can be used by filesystems (including those which don't use
482 * dquot) to send a message to userspace relating to quota limits.
486 void quota_send_warning(short type, unsigned int id, dev_t dev,
493 int msg_size = 4 * nla_total_size(sizeof(u32)) +
494 2 * nla_total_size(sizeof(u64));
496 /* We have to allocate using GFP_NOFS as we are called from a
497 * filesystem performing write and thus further recursion into
498 * the fs to free some data could cause deadlocks. */
499 skb = genlmsg_new(msg_size, GFP_NOFS);
502 "VFS: Not enough memory to send quota warning.\n");
505 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
506 "a_genl_family, 0, QUOTA_NL_C_WARNING);
509 "VFS: Cannot store netlink header in quota warning.\n");
512 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
515 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
518 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
521 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
524 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
527 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
530 genlmsg_end(skb, msg_head);
532 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
535 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
539 EXPORT_SYMBOL(quota_send_warning);
541 static int __init quota_init(void)
543 if (genl_register_family("a_genl_family) != 0)
545 "VFS: Failed to create quota netlink interface.\n");
549 module_init(quota_init);