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/kernel.h>
14 #include <linux/security.h>
15 #include <linux/syscalls.h>
16 #include <linux/buffer_head.h>
17 #include <linux/capability.h>
18 #include <linux/quotaops.h>
19 #include <linux/types.h>
20 #include <linux/writeback.h>
22 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
26 /* these commands do not require any special privilegues */
33 /* allow to query information for dquots we "own" */
36 if ((type == USRQUOTA && current_euid() == id) ||
37 (type == GRPQUOTA && in_egroup_p(id)))
41 if (!capable(CAP_SYS_ADMIN))
45 return security_quotactl(cmd, type, id, sb);
48 static int quota_sync_all(int type)
50 struct super_block *sb;
53 if (type >= MAXQUOTAS)
55 ret = security_quotactl(Q_SYNC, type, 0, NULL);
61 list_for_each_entry(sb, &super_blocks, s_list) {
62 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
66 spin_unlock(&sb_lock);
67 down_read(&sb->s_umount);
69 sb->s_qcop->quota_sync(sb, type, 1);
70 up_read(&sb->s_umount);
72 if (__put_super_and_need_restart(sb))
75 spin_unlock(&sb_lock);
80 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
86 pathname = getname(addr);
88 return PTR_ERR(pathname);
89 if (sb->s_qcop->quota_on)
90 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
95 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
99 down_read(&sb_dqopt(sb)->dqptr_sem);
100 if (!sb_has_quota_active(sb, type)) {
101 up_read(&sb_dqopt(sb)->dqptr_sem);
104 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
105 up_read(&sb_dqopt(sb)->dqptr_sem);
106 if (copy_to_user(addr, &fmt, sizeof(fmt)))
111 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
113 struct if_dqinfo info;
116 if (!sb_has_quota_active(sb, type))
118 if (!sb->s_qcop->get_info)
120 ret = sb->s_qcop->get_info(sb, type, &info);
121 if (!ret && copy_to_user(addr, &info, sizeof(info)))
126 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
128 struct if_dqinfo info;
130 if (copy_from_user(&info, addr, sizeof(info)))
132 if (!sb_has_quota_active(sb, type))
134 if (!sb->s_qcop->set_info)
136 return sb->s_qcop->set_info(sb, type, &info);
139 static int quota_getquota(struct super_block *sb, int type, qid_t id,
145 if (!sb_has_quota_active(sb, type))
147 if (!sb->s_qcop->get_dqblk)
149 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
152 if (copy_to_user(addr, &idq, sizeof(idq)))
157 static int quota_setquota(struct super_block *sb, int type, qid_t id,
162 if (copy_from_user(&idq, addr, sizeof(idq)))
164 if (!sb_has_quota_active(sb, type))
166 if (!sb->s_qcop->set_dqblk)
168 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
171 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
175 if (copy_from_user(&flags, addr, sizeof(flags)))
177 if (!sb->s_qcop->set_xstate)
179 return sb->s_qcop->set_xstate(sb, flags, cmd);
182 static int quota_getxstate(struct super_block *sb, void __user *addr)
184 struct fs_quota_stat fqs;
187 if (!sb->s_qcop->get_xstate)
189 ret = sb->s_qcop->get_xstate(sb, &fqs);
190 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
195 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
198 struct fs_disk_quota fdq;
200 if (copy_from_user(&fdq, addr, sizeof(fdq)))
202 if (!sb->s_qcop->set_xquota)
204 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
207 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
210 struct fs_disk_quota fdq;
213 if (!sb->s_qcop->get_xquota)
215 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
216 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
221 /* Copy parameters and call proper function */
222 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
227 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
232 ret = check_quotactl_permission(sb, type, cmd, id);
238 return quota_quotaon(sb, type, cmd, id, addr);
240 if (!sb->s_qcop->quota_off)
242 return sb->s_qcop->quota_off(sb, type, 0);
244 return quota_getfmt(sb, type, addr);
246 return quota_getinfo(sb, type, addr);
248 return quota_setinfo(sb, type, addr);
250 return quota_getquota(sb, type, id, addr);
252 return quota_setquota(sb, type, id, addr);
254 if (!sb->s_qcop->quota_sync)
256 return sb->s_qcop->quota_sync(sb, type, 1);
260 return quota_setxstate(sb, cmd, addr);
262 return quota_getxstate(sb, addr);
264 return quota_setxquota(sb, type, id, addr);
266 return quota_getxquota(sb, type, id, addr);
268 /* caller already holds s_umount */
269 if (sb->s_flags & MS_RDONLY)
271 writeback_inodes_sb(sb);
279 * look up a superblock on which quota ops will be performed
280 * - use the name of a block device to find the superblock thereon
282 static struct super_block *quotactl_block(const char __user *special)
285 struct block_device *bdev;
286 struct super_block *sb;
287 char *tmp = getname(special);
290 return ERR_CAST(tmp);
291 bdev = lookup_bdev(tmp);
294 return ERR_CAST(bdev);
295 sb = get_super(bdev);
298 return ERR_PTR(-ENODEV);
302 return ERR_PTR(-ENODEV);
307 * This is the system call interface. This communicates with
308 * the user-level programs. Currently this only supports diskquota
309 * calls. Maybe we need to add the process quotas etc. in the future,
310 * but we probably should use rlimits for that.
312 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
313 qid_t, id, void __user *, addr)
316 struct super_block *sb = NULL;
319 cmds = cmd >> SUBCMDSHIFT;
320 type = cmd & SUBCMDMASK;
323 * As a special case Q_SYNC can be called without a specific device.
324 * It will iterate all superblocks that have quota enabled and call
325 * the sync action on each of them.
329 return quota_sync_all(type);
333 sb = quotactl_block(special);
337 ret = do_quotactl(sb, type, cmds, id, addr);