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>
20 /* Check validity of generic quotactl commands */
21 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
23 if (type >= MAXQUOTAS)
25 if (!sb && cmd != Q_SYNC)
27 /* Is operation supported? */
28 if (sb && !sb->s_qcop)
35 if (!sb->s_qcop->quota_on)
39 if (!sb->s_qcop->quota_off)
43 if (!sb->s_qcop->set_info)
47 if (!sb->s_qcop->get_info)
51 if (!sb->s_qcop->set_dqblk)
55 if (!sb->s_qcop->get_dqblk)
59 if (sb && !sb->s_qcop->quota_sync)
66 /* Is quota turned on for commands which need it? */
74 /* This is just informative test so we are satisfied without a lock */
75 if (!sb_has_quota_enabled(sb, type))
79 /* Check privileges */
80 if (cmd == Q_GETQUOTA) {
81 if (((type == USRQUOTA && current->euid != id) ||
82 (type == GRPQUOTA && !in_egroup_p(id))) &&
83 !capable(CAP_SYS_ADMIN))
86 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
87 if (!capable(CAP_SYS_ADMIN))
93 /* Check validity of XFS Quota Manager commands */
94 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
96 if (type >= XQM_MAXQUOTAS)
107 if (!sb->s_qcop->set_xstate)
111 if (!sb->s_qcop->get_xstate)
115 if (!sb->s_qcop->set_xquota)
119 if (!sb->s_qcop->get_xquota)
123 if (!sb->s_qcop->quota_sync)
130 /* Check privileges */
131 if (cmd == Q_XGETQUOTA) {
132 if (((type == XQM_USRQUOTA && current->euid != id) ||
133 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
134 !capable(CAP_SYS_ADMIN))
136 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
137 if (!capable(CAP_SYS_ADMIN))
144 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
148 if (XQM_COMMAND(cmd))
149 error = xqm_quotactl_valid(sb, type, cmd, id);
151 error = generic_quotactl_valid(sb, type, cmd, id);
153 error = security_quotactl(cmd, type, id, sb);
157 static void quota_sync_sb(struct super_block *sb, int type)
161 sb->s_qcop->quota_sync(sb, type);
162 /* This is not very clever (and fast) but currently I don't know about
163 * any other simple way of getting quota data to disk and we must get
164 * them there for userspace to be visible... */
165 if (sb->s_op->sync_fs)
166 sb->s_op->sync_fs(sb, 1);
167 sync_blockdev(sb->s_bdev);
170 * Now when everything is written we can discard the pagecache so
171 * that userspace sees the changes.
173 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
174 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
175 if (type != -1 && cnt != type)
177 if (!sb_has_quota_enabled(sb, cnt))
179 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex, I_MUTEX_QUOTA);
180 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
181 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
183 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
186 void sync_dquots(struct super_block *sb, int type)
191 if (sb->s_qcop->quota_sync)
192 quota_sync_sb(sb, type);
198 list_for_each_entry(sb, &super_blocks, s_list) {
199 /* This test just improves performance so it needn't be reliable... */
200 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
201 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
202 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
207 spin_unlock(&sb_lock);
208 down_read(&sb->s_umount);
209 if (sb->s_root && sb->s_qcop->quota_sync)
210 quota_sync_sb(sb, type);
211 up_read(&sb->s_umount);
213 if (__put_super_and_need_restart(sb))
216 spin_unlock(&sb_lock);
219 /* Copy parameters and call proper function */
220 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
228 if (IS_ERR(pathname = getname(addr)))
229 return PTR_ERR(pathname);
230 ret = sb->s_qcop->quota_on(sb, type, id, pathname);
235 return sb->s_qcop->quota_off(sb, type);
240 down_read(&sb_dqopt(sb)->dqptr_sem);
241 if (!sb_has_quota_enabled(sb, type)) {
242 up_read(&sb_dqopt(sb)->dqptr_sem);
245 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
246 up_read(&sb_dqopt(sb)->dqptr_sem);
247 if (copy_to_user(addr, &fmt, sizeof(fmt)))
252 struct if_dqinfo info;
254 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
256 if (copy_to_user(addr, &info, sizeof(info)))
261 struct if_dqinfo info;
263 if (copy_from_user(&info, addr, sizeof(info)))
265 return sb->s_qcop->set_info(sb, type, &info);
270 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
272 if (copy_to_user(addr, &idq, sizeof(idq)))
279 if (copy_from_user(&idq, addr, sizeof(idq)))
281 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
284 sync_dquots(sb, type);
292 if (copy_from_user(&flags, addr, sizeof(flags)))
294 return sb->s_qcop->set_xstate(sb, flags, cmd);
297 struct fs_quota_stat fqs;
299 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
301 if (copy_to_user(addr, &fqs, sizeof(fqs)))
306 struct fs_disk_quota fdq;
308 if (copy_from_user(&fdq, addr, sizeof(fdq)))
310 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
313 struct fs_disk_quota fdq;
315 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
317 if (copy_to_user(addr, &fdq, sizeof(fdq)))
322 return sb->s_qcop->quota_sync(sb, type);
323 /* We never reach here unless validity check is broken */
331 * look up a superblock on which quota ops will be performed
332 * - use the name of a block device to find the superblock thereon
334 static inline struct super_block *quotactl_block(const char __user *special)
337 struct block_device *bdev;
338 struct super_block *sb;
339 char *tmp = getname(special);
342 return ERR_PTR(PTR_ERR(tmp));
343 bdev = lookup_bdev(tmp);
346 return ERR_PTR(PTR_ERR(bdev));
347 sb = get_super(bdev);
350 return ERR_PTR(-ENODEV);
354 return ERR_PTR(-ENODEV);
359 * This is the system call interface. This communicates with
360 * the user-level programs. Currently this only supports diskquota
361 * calls. Maybe we need to add the process quotas etc. in the future,
362 * but we probably should use rlimits for that.
364 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
367 struct super_block *sb = NULL;
370 cmds = cmd >> SUBCMDSHIFT;
371 type = cmd & SUBCMDMASK;
373 if (cmds != Q_SYNC || special) {
374 sb = quotactl_block(special);
379 ret = check_quotactl_valid(sb, type, cmds, id);
381 ret = do_quotactl(sb, type, cmds, id, addr);