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>
23 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
27 /* these commands do not require any special privilegues */
34 /* allow to query information for dquots we "own" */
37 if ((type == USRQUOTA && current_euid() == id) ||
38 (type == GRPQUOTA && in_egroup_p(id)))
42 if (!capable(CAP_SYS_ADMIN))
46 return security_quotactl(cmd, type, id, sb);
49 static int quota_sync_all(int type)
51 struct super_block *sb;
54 if (type >= MAXQUOTAS)
56 ret = security_quotactl(Q_SYNC, type, 0, NULL);
62 list_for_each_entry(sb, &super_blocks, s_list) {
63 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
67 spin_unlock(&sb_lock);
68 down_read(&sb->s_umount);
70 sb->s_qcop->quota_sync(sb, type, 1);
71 up_read(&sb->s_umount);
73 if (__put_super_and_need_restart(sb))
76 spin_unlock(&sb_lock);
81 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
87 pathname = getname(addr);
89 return PTR_ERR(pathname);
90 if (sb->s_qcop->quota_on)
91 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
96 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
100 down_read(&sb_dqopt(sb)->dqptr_sem);
101 if (!sb_has_quota_active(sb, type)) {
102 up_read(&sb_dqopt(sb)->dqptr_sem);
105 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
106 up_read(&sb_dqopt(sb)->dqptr_sem);
107 if (copy_to_user(addr, &fmt, sizeof(fmt)))
112 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
114 struct if_dqinfo info;
117 if (!sb_has_quota_active(sb, type))
119 if (!sb->s_qcop->get_info)
121 ret = sb->s_qcop->get_info(sb, type, &info);
122 if (!ret && copy_to_user(addr, &info, sizeof(info)))
127 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
129 struct if_dqinfo info;
131 if (copy_from_user(&info, addr, sizeof(info)))
133 if (!sb_has_quota_active(sb, type))
135 if (!sb->s_qcop->set_info)
137 return sb->s_qcop->set_info(sb, type, &info);
140 static int quota_getquota(struct super_block *sb, int type, qid_t id,
146 if (!sb_has_quota_active(sb, type))
148 if (!sb->s_qcop->get_dqblk)
150 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
153 if (copy_to_user(addr, &idq, sizeof(idq)))
158 static int quota_setquota(struct super_block *sb, int type, qid_t id,
163 if (copy_from_user(&idq, addr, sizeof(idq)))
165 if (!sb_has_quota_active(sb, type))
167 if (!sb->s_qcop->set_dqblk)
169 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
172 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
176 if (copy_from_user(&flags, addr, sizeof(flags)))
178 if (!sb->s_qcop->set_xstate)
180 return sb->s_qcop->set_xstate(sb, flags, cmd);
183 static int quota_getxstate(struct super_block *sb, void __user *addr)
185 struct fs_quota_stat fqs;
188 if (!sb->s_qcop->get_xstate)
190 ret = sb->s_qcop->get_xstate(sb, &fqs);
191 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
196 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
199 struct fs_disk_quota fdq;
201 if (copy_from_user(&fdq, addr, sizeof(fdq)))
203 if (!sb->s_qcop->set_xquota)
205 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
208 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
211 struct fs_disk_quota fdq;
214 if (!sb->s_qcop->get_xquota)
216 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
217 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
222 /* Copy parameters and call proper function */
223 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
228 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
233 ret = check_quotactl_permission(sb, type, cmd, id);
239 return quota_quotaon(sb, type, cmd, id, addr);
241 if (!sb->s_qcop->quota_off)
243 return sb->s_qcop->quota_off(sb, type, 0);
245 return quota_getfmt(sb, type, addr);
247 return quota_getinfo(sb, type, addr);
249 return quota_setinfo(sb, type, addr);
251 return quota_getquota(sb, type, id, addr);
253 return quota_setquota(sb, type, id, addr);
255 if (!sb->s_qcop->quota_sync)
257 return sb->s_qcop->quota_sync(sb, type, 1);
261 return quota_setxstate(sb, cmd, addr);
263 return quota_getxstate(sb, addr);
265 return quota_setxquota(sb, type, id, addr);
267 return quota_getxquota(sb, type, id, addr);
269 /* caller already holds s_umount */
270 if (sb->s_flags & MS_RDONLY)
272 writeback_inodes_sb(sb);
280 * look up a superblock on which quota ops will be performed
281 * - use the name of a block device to find the superblock thereon
283 static struct super_block *quotactl_block(const char __user *special)
286 struct block_device *bdev;
287 struct super_block *sb;
288 char *tmp = getname(special);
291 return ERR_CAST(tmp);
292 bdev = lookup_bdev(tmp);
295 return ERR_CAST(bdev);
296 sb = get_super(bdev);
299 return ERR_PTR(-ENODEV);
303 return ERR_PTR(-ENODEV);
308 * This is the system call interface. This communicates with
309 * the user-level programs. Currently this only supports diskquota
310 * calls. Maybe we need to add the process quotas etc. in the future,
311 * but we probably should use rlimits for that.
313 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
314 qid_t, id, void __user *, addr)
317 struct super_block *sb = NULL;
320 cmds = cmd >> SUBCMDSHIFT;
321 type = cmd & SUBCMDMASK;
324 * As a special case Q_SYNC can be called without a specific device.
325 * It will iterate all superblocks that have quota enabled and call
326 * the sync action on each of them.
330 return quota_sync_all(type);
334 sb = quotactl_block(special);
338 ret = do_quotactl(sb, type, cmds, id, addr);
344 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
346 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
347 * and is necessary due to alignment problems.
349 struct compat_if_dqblk {
350 compat_u64 dqb_bhardlimit;
351 compat_u64 dqb_bsoftlimit;
352 compat_u64 dqb_curspace;
353 compat_u64 dqb_ihardlimit;
354 compat_u64 dqb_isoftlimit;
355 compat_u64 dqb_curinodes;
356 compat_u64 dqb_btime;
357 compat_u64 dqb_itime;
358 compat_uint_t dqb_valid;
362 struct compat_fs_qfilestat {
363 compat_u64 dqb_bhardlimit;
364 compat_u64 qfs_nblks;
365 compat_uint_t qfs_nextents;
368 struct compat_fs_quota_stat {
372 struct compat_fs_qfilestat qs_uquota;
373 struct compat_fs_qfilestat qs_gquota;
374 compat_uint_t qs_incoredqs;
375 compat_int_t qs_btimelimit;
376 compat_int_t qs_itimelimit;
377 compat_int_t qs_rtbtimelimit;
382 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
383 qid_t id, void __user *addr)
386 struct if_dqblk __user *dqblk;
387 struct compat_if_dqblk __user *compat_dqblk;
388 struct fs_quota_stat __user *fsqstat;
389 struct compat_fs_quota_stat __user *compat_fsqstat;
394 cmds = cmd >> SUBCMDSHIFT;
398 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
400 ret = sys_quotactl(cmd, special, id, dqblk);
403 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
404 get_user(data, &dqblk->dqb_valid) ||
405 put_user(data, &compat_dqblk->dqb_valid))
409 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
412 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
413 get_user(data, &compat_dqblk->dqb_valid) ||
414 put_user(data, &dqblk->dqb_valid))
416 ret = sys_quotactl(cmd, special, id, dqblk);
419 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
420 compat_fsqstat = addr;
421 ret = sys_quotactl(cmd, special, id, fsqstat);
425 /* Copying qs_version, qs_flags, qs_pad */
426 if (copy_in_user(compat_fsqstat, fsqstat,
427 offsetof(struct compat_fs_quota_stat, qs_uquota)))
429 /* Copying qs_uquota */
430 if (copy_in_user(&compat_fsqstat->qs_uquota,
432 sizeof(compat_fsqstat->qs_uquota)) ||
433 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
434 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
436 /* Copying qs_gquota */
437 if (copy_in_user(&compat_fsqstat->qs_gquota,
439 sizeof(compat_fsqstat->qs_gquota)) ||
440 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
441 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
443 /* Copying the rest */
444 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
445 &fsqstat->qs_incoredqs,
446 sizeof(struct compat_fs_quota_stat) -
447 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
448 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
449 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
454 ret = sys_quotactl(cmd, special, id, addr);