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 void quota_sync_one(struct super_block *sb, void *arg)
50 if (sb->s_qcop && sb->s_qcop->quota_sync)
51 sb->s_qcop->quota_sync(sb, *(int *)arg, 1);
54 static int quota_sync_all(int type)
58 if (type >= MAXQUOTAS)
60 ret = security_quotactl(Q_SYNC, type, 0, NULL);
62 iterate_supers(quota_sync_one, &type);
66 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
72 pathname = getname(addr);
74 return PTR_ERR(pathname);
75 if (sb->s_qcop->quota_on)
76 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
81 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
85 down_read(&sb_dqopt(sb)->dqptr_sem);
86 if (!sb_has_quota_active(sb, type)) {
87 up_read(&sb_dqopt(sb)->dqptr_sem);
90 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
91 up_read(&sb_dqopt(sb)->dqptr_sem);
92 if (copy_to_user(addr, &fmt, sizeof(fmt)))
97 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
99 struct if_dqinfo info;
102 if (!sb->s_qcop->get_info)
104 ret = sb->s_qcop->get_info(sb, type, &info);
105 if (!ret && copy_to_user(addr, &info, sizeof(info)))
110 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
112 struct if_dqinfo info;
114 if (copy_from_user(&info, addr, sizeof(info)))
116 if (!sb->s_qcop->set_info)
118 return sb->s_qcop->set_info(sb, type, &info);
121 static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src)
123 dst->dqb_bhardlimit = src->d_blk_hardlimit;
124 dst->dqb_bsoftlimit = src->d_blk_softlimit;
125 dst->dqb_curspace = src->d_bcount;
126 dst->dqb_ihardlimit = src->d_ino_hardlimit;
127 dst->dqb_isoftlimit = src->d_ino_softlimit;
128 dst->dqb_curinodes = src->d_icount;
129 dst->dqb_btime = src->d_btimer;
130 dst->dqb_itime = src->d_itimer;
131 dst->dqb_valid = QIF_ALL;
134 static int quota_getquota(struct super_block *sb, int type, qid_t id,
137 struct fs_disk_quota fdq;
141 if (!sb->s_qcop->get_dqblk)
143 ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
146 copy_to_if_dqblk(&idq, &fdq);
147 if (copy_to_user(addr, &idq, sizeof(idq)))
152 static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src)
154 dst->d_blk_hardlimit = src->dqb_bhardlimit;
155 dst->d_blk_softlimit = src->dqb_bsoftlimit;
156 dst->d_bcount = src->dqb_curspace;
157 dst->d_ino_hardlimit = src->dqb_ihardlimit;
158 dst->d_ino_softlimit = src->dqb_isoftlimit;
159 dst->d_icount = src->dqb_curinodes;
160 dst->d_btimer = src->dqb_btime;
161 dst->d_itimer = src->dqb_itime;
163 dst->d_fieldmask = 0;
164 if (src->dqb_valid & QIF_BLIMITS)
165 dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD;
166 if (src->dqb_valid & QIF_SPACE)
167 dst->d_fieldmask |= FS_DQ_BCOUNT;
168 if (src->dqb_valid & QIF_ILIMITS)
169 dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD;
170 if (src->dqb_valid & QIF_INODES)
171 dst->d_fieldmask |= FS_DQ_ICOUNT;
172 if (src->dqb_valid & QIF_BTIME)
173 dst->d_fieldmask |= FS_DQ_BTIMER;
174 if (src->dqb_valid & QIF_ITIME)
175 dst->d_fieldmask |= FS_DQ_ITIMER;
178 static int quota_setquota(struct super_block *sb, int type, qid_t id,
181 struct fs_disk_quota fdq;
184 if (copy_from_user(&idq, addr, sizeof(idq)))
186 if (!sb->s_qcop->set_dqblk)
188 copy_from_if_dqblk(&fdq, &idq);
189 return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
192 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
196 if (copy_from_user(&flags, addr, sizeof(flags)))
198 if (!sb->s_qcop->set_xstate)
200 return sb->s_qcop->set_xstate(sb, flags, cmd);
203 static int quota_getxstate(struct super_block *sb, void __user *addr)
205 struct fs_quota_stat fqs;
208 if (!sb->s_qcop->get_xstate)
210 ret = sb->s_qcop->get_xstate(sb, &fqs);
211 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
216 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
219 struct fs_disk_quota fdq;
221 if (copy_from_user(&fdq, addr, sizeof(fdq)))
223 if (!sb->s_qcop->set_dqblk)
225 return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
228 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
231 struct fs_disk_quota fdq;
234 if (!sb->s_qcop->get_dqblk)
236 ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
237 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
242 /* Copy parameters and call proper function */
243 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
248 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
253 ret = check_quotactl_permission(sb, type, cmd, id);
259 return quota_quotaon(sb, type, cmd, id, addr);
261 if (!sb->s_qcop->quota_off)
263 return sb->s_qcop->quota_off(sb, type, 0);
265 return quota_getfmt(sb, type, addr);
267 return quota_getinfo(sb, type, addr);
269 return quota_setinfo(sb, type, addr);
271 return quota_getquota(sb, type, id, addr);
273 return quota_setquota(sb, type, id, addr);
275 if (!sb->s_qcop->quota_sync)
277 return sb->s_qcop->quota_sync(sb, type, 1);
281 return quota_setxstate(sb, cmd, addr);
283 return quota_getxstate(sb, addr);
285 return quota_setxquota(sb, type, id, addr);
287 return quota_getxquota(sb, type, id, addr);
289 /* caller already holds s_umount */
290 if (sb->s_flags & MS_RDONLY)
292 writeback_inodes_sb(sb);
300 * look up a superblock on which quota ops will be performed
301 * - use the name of a block device to find the superblock thereon
303 static struct super_block *quotactl_block(const char __user *special)
306 struct block_device *bdev;
307 struct super_block *sb;
308 char *tmp = getname(special);
311 return ERR_CAST(tmp);
312 bdev = lookup_bdev(tmp);
315 return ERR_CAST(bdev);
316 sb = get_super(bdev);
319 return ERR_PTR(-ENODEV);
323 return ERR_PTR(-ENODEV);
328 * This is the system call interface. This communicates with
329 * the user-level programs. Currently this only supports diskquota
330 * calls. Maybe we need to add the process quotas etc. in the future,
331 * but we probably should use rlimits for that.
333 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
334 qid_t, id, void __user *, addr)
337 struct super_block *sb = NULL;
340 cmds = cmd >> SUBCMDSHIFT;
341 type = cmd & SUBCMDMASK;
344 * As a special case Q_SYNC can be called without a specific device.
345 * It will iterate all superblocks that have quota enabled and call
346 * the sync action on each of them.
350 return quota_sync_all(type);
354 sb = quotactl_block(special);
358 ret = do_quotactl(sb, type, cmds, id, addr);