blob: d6ee49dda4fd1a4bd61ab0002c46bf45effeca21 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
6 */
7
8#include <linux/fs.h>
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/security.h>
15#include <linux/syscalls.h>
16#include <linux/buffer_head.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080017#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080018#include <linux/quotaops.h>
Vasily Tarasovb7163952007-07-15 23:41:12 -070019#include <linux/types.h>
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -050020#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Christoph Hellwigc988afb2010-02-16 03:44:50 -050022static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
23 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Christoph Hellwigc988afb2010-02-16 03:44:50 -050025 switch (cmd) {
26 /* these commands do not require any special privilegues */
27 case Q_GETFMT:
28 case Q_SYNC:
29 case Q_GETINFO:
30 case Q_XGETQSTAT:
31 case Q_XQUOTASYNC:
32 break;
33 /* allow to query information for dquots we "own" */
34 case Q_GETQUOTA:
35 case Q_XGETQUOTA:
36 if ((type == USRQUOTA && current_euid() == id) ||
37 (type == GRPQUOTA && in_egroup_p(id)))
38 break;
39 /*FALLTHROUGH*/
40 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (!capable(CAP_SYS_ADMIN))
42 return -EPERM;
43 }
44
Christoph Hellwigc988afb2010-02-16 03:44:50 -050045 return security_quotactl(cmd, type, id, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Christoph Hellwig6ae09572010-02-16 03:44:49 -050048static int quota_sync_all(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Christoph Hellwig850b2012009-04-27 16:43:54 +020050 struct super_block *sb;
Christoph Hellwig6ae09572010-02-16 03:44:49 -050051 int ret;
52
53 if (type >= MAXQUOTAS)
54 return -EINVAL;
55 ret = security_quotactl(Q_SYNC, type, 0, NULL);
56 if (ret)
57 return ret;
Kirill Korotaev618f0632005-06-23 00:09:54 -070058
Kirill Korotaev618f0632005-06-23 00:09:54 -070059 spin_lock(&sb_lock);
60restart:
61 list_for_each_entry(sb, &super_blocks, s_list) {
Christoph Hellwig5fb324a2010-02-16 03:44:52 -050062 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
63 continue;
64
Kirill Korotaev618f0632005-06-23 00:09:54 -070065 sb->s_count++;
66 spin_unlock(&sb_lock);
67 down_read(&sb->s_umount);
Christoph Hellwig850b2012009-04-27 16:43:54 +020068 if (sb->s_root)
Christoph Hellwig5fb324a2010-02-16 03:44:52 -050069 sb->s_qcop->quota_sync(sb, type, 1);
Kirill Korotaev618f0632005-06-23 00:09:54 -070070 up_read(&sb->s_umount);
71 spin_lock(&sb_lock);
72 if (__put_super_and_need_restart(sb))
73 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
Kirill Korotaev618f0632005-06-23 00:09:54 -070075 spin_unlock(&sb_lock);
Christoph Hellwig6ae09572010-02-16 03:44:49 -050076
77 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Christoph Hellwigc411e5f2010-02-16 03:44:47 -050080static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
81 void __user *addr)
82{
83 char *pathname;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -050084 int ret = -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -050085
86 pathname = getname(addr);
87 if (IS_ERR(pathname))
88 return PTR_ERR(pathname);
Christoph Hellwigf450d4f2010-02-16 03:44:48 -050089 if (sb->s_qcop->quota_on)
90 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -050091 putname(pathname);
92 return ret;
93}
94
95static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
96{
97 __u32 fmt;
98
99 down_read(&sb_dqopt(sb)->dqptr_sem);
100 if (!sb_has_quota_active(sb, type)) {
101 up_read(&sb_dqopt(sb)->dqptr_sem);
102 return -ESRCH;
103 }
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)))
107 return -EFAULT;
108 return 0;
109}
110
111static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
112{
113 struct if_dqinfo info;
114 int ret;
115
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500116 if (!sb_has_quota_active(sb, type))
117 return -ESRCH;
118 if (!sb->s_qcop->get_info)
119 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500120 ret = sb->s_qcop->get_info(sb, type, &info);
121 if (!ret && copy_to_user(addr, &info, sizeof(info)))
122 return -EFAULT;
123 return ret;
124}
125
126static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
127{
128 struct if_dqinfo info;
129
130 if (copy_from_user(&info, addr, sizeof(info)))
131 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500132 if (!sb_has_quota_active(sb, type))
133 return -ESRCH;
134 if (!sb->s_qcop->set_info)
135 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500136 return sb->s_qcop->set_info(sb, type, &info);
137}
138
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400139static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src)
140{
141 dst->dqb_bhardlimit = src->d_blk_hardlimit;
142 dst->dqb_bsoftlimit = src->d_blk_softlimit;
143 dst->dqb_curspace = src->d_bcount;
144 dst->dqb_ihardlimit = src->d_ino_hardlimit;
145 dst->dqb_isoftlimit = src->d_ino_softlimit;
146 dst->dqb_curinodes = src->d_icount;
147 dst->dqb_btime = src->d_btimer;
148 dst->dqb_itime = src->d_itimer;
149 dst->dqb_valid = QIF_ALL;
150}
151
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500152static int quota_getquota(struct super_block *sb, int type, qid_t id,
153 void __user *addr)
154{
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400155 struct fs_disk_quota fdq;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500156 struct if_dqblk idq;
157 int ret;
158
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500159 if (!sb->s_qcop->get_dqblk)
160 return -ENOSYS;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400161 ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500162 if (ret)
163 return ret;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400164 copy_to_if_dqblk(&idq, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500165 if (copy_to_user(addr, &idq, sizeof(idq)))
166 return -EFAULT;
167 return 0;
168}
169
Christoph Hellwigc472b432010-05-06 17:05:17 -0400170static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src)
171{
172 dst->d_blk_hardlimit = src->dqb_bhardlimit;
173 dst->d_blk_softlimit = src->dqb_bsoftlimit;
174 dst->d_bcount = src->dqb_curspace;
175 dst->d_ino_hardlimit = src->dqb_ihardlimit;
176 dst->d_ino_softlimit = src->dqb_isoftlimit;
177 dst->d_icount = src->dqb_curinodes;
178 dst->d_btimer = src->dqb_btime;
179 dst->d_itimer = src->dqb_itime;
180
181 dst->d_fieldmask = 0;
182 if (src->dqb_valid & QIF_BLIMITS)
183 dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD;
184 if (src->dqb_valid & QIF_SPACE)
185 dst->d_fieldmask |= FS_DQ_BCOUNT;
186 if (src->dqb_valid & QIF_ILIMITS)
187 dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD;
188 if (src->dqb_valid & QIF_INODES)
189 dst->d_fieldmask |= FS_DQ_ICOUNT;
190 if (src->dqb_valid & QIF_BTIME)
191 dst->d_fieldmask |= FS_DQ_BTIMER;
192 if (src->dqb_valid & QIF_ITIME)
193 dst->d_fieldmask |= FS_DQ_ITIMER;
194}
195
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500196static int quota_setquota(struct super_block *sb, int type, qid_t id,
197 void __user *addr)
198{
Christoph Hellwigc472b432010-05-06 17:05:17 -0400199 struct fs_disk_quota fdq;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500200 struct if_dqblk idq;
201
202 if (copy_from_user(&idq, addr, sizeof(idq)))
203 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500204 if (!sb->s_qcop->set_dqblk)
205 return -ENOSYS;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400206 copy_from_if_dqblk(&fdq, &idq);
207 return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500208}
209
210static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
211{
212 __u32 flags;
213
214 if (copy_from_user(&flags, addr, sizeof(flags)))
215 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500216 if (!sb->s_qcop->set_xstate)
217 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500218 return sb->s_qcop->set_xstate(sb, flags, cmd);
219}
220
221static int quota_getxstate(struct super_block *sb, void __user *addr)
222{
223 struct fs_quota_stat fqs;
224 int ret;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500225
226 if (!sb->s_qcop->get_xstate)
227 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500228 ret = sb->s_qcop->get_xstate(sb, &fqs);
229 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
230 return -EFAULT;
231 return ret;
232}
233
234static int quota_setxquota(struct super_block *sb, int type, qid_t id,
235 void __user *addr)
236{
237 struct fs_disk_quota fdq;
238
239 if (copy_from_user(&fdq, addr, sizeof(fdq)))
240 return -EFAULT;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400241 if (!sb->s_qcop->set_dqblk)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500242 return -ENOSYS;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400243 return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500244}
245
246static int quota_getxquota(struct super_block *sb, int type, qid_t id,
247 void __user *addr)
248{
249 struct fs_disk_quota fdq;
250 int ret;
251
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400252 if (!sb->s_qcop->get_dqblk)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500253 return -ENOSYS;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400254 ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500255 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
256 return -EFAULT;
257 return ret;
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/* Copy parameters and call proper function */
Jan Kara268157b2009-01-27 15:47:22 +0100261static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
262 void __user *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500264 int ret;
265
266 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
267 return -EINVAL;
268 if (!sb->s_qcop)
269 return -ENOSYS;
270
271 ret = check_quotactl_permission(sb, type, cmd, id);
272 if (ret < 0)
273 return ret;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 switch (cmd) {
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500276 case Q_QUOTAON:
277 return quota_quotaon(sb, type, cmd, id, addr);
278 case Q_QUOTAOFF:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500279 if (!sb->s_qcop->quota_off)
280 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500281 return sb->s_qcop->quota_off(sb, type, 0);
282 case Q_GETFMT:
283 return quota_getfmt(sb, type, addr);
284 case Q_GETINFO:
285 return quota_getinfo(sb, type, addr);
286 case Q_SETINFO:
287 return quota_setinfo(sb, type, addr);
288 case Q_GETQUOTA:
289 return quota_getquota(sb, type, id, addr);
290 case Q_SETQUOTA:
291 return quota_setquota(sb, type, id, addr);
292 case Q_SYNC:
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500293 if (!sb->s_qcop->quota_sync)
294 return -ENOSYS;
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500295 return sb->s_qcop->quota_sync(sb, type, 1);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500296 case Q_XQUOTAON:
297 case Q_XQUOTAOFF:
298 case Q_XQUOTARM:
299 return quota_setxstate(sb, cmd, addr);
300 case Q_XGETQSTAT:
301 return quota_getxstate(sb, addr);
302 case Q_XSETQLIM:
303 return quota_setxquota(sb, type, id, addr);
304 case Q_XGETQUOTA:
305 return quota_getxquota(sb, type, id, addr);
306 case Q_XQUOTASYNC:
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -0500307 /* caller already holds s_umount */
308 if (sb->s_flags & MS_RDONLY)
309 return -EROFS;
310 writeback_inodes_sb(sb);
311 return 0;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500312 default:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317/*
David Howells93614012006-09-30 20:45:40 +0200318 * look up a superblock on which quota ops will be performed
319 * - use the name of a block device to find the superblock thereon
320 */
Jan Kara7a2435d2009-01-27 01:47:11 +0100321static struct super_block *quotactl_block(const char __user *special)
David Howells93614012006-09-30 20:45:40 +0200322{
323#ifdef CONFIG_BLOCK
324 struct block_device *bdev;
325 struct super_block *sb;
326 char *tmp = getname(special);
327
328 if (IS_ERR(tmp))
David Howellse231c2e2008-02-07 00:15:26 -0800329 return ERR_CAST(tmp);
David Howells93614012006-09-30 20:45:40 +0200330 bdev = lookup_bdev(tmp);
331 putname(tmp);
332 if (IS_ERR(bdev))
David Howellse231c2e2008-02-07 00:15:26 -0800333 return ERR_CAST(bdev);
David Howells93614012006-09-30 20:45:40 +0200334 sb = get_super(bdev);
335 bdput(bdev);
336 if (!sb)
337 return ERR_PTR(-ENODEV);
338
339 return sb;
340#else
341 return ERR_PTR(-ENODEV);
342#endif
343}
344
345/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * This is the system call interface. This communicates with
347 * the user-level programs. Currently this only supports diskquota
348 * calls. Maybe we need to add the process quotas etc. in the future,
349 * but we probably should use rlimits for that.
350 */
Heiko Carstens3cdad422009-01-14 14:14:22 +0100351SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
352 qid_t, id, void __user *, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 uint cmds, type;
355 struct super_block *sb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int ret;
357
358 cmds = cmd >> SUBCMDSHIFT;
359 type = cmd & SUBCMDMASK;
360
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500361 /*
362 * As a special case Q_SYNC can be called without a specific device.
363 * It will iterate all superblocks that have quota enabled and call
364 * the sync action on each of them.
365 */
366 if (!special) {
367 if (cmds == Q_SYNC)
368 return quota_sync_all(type);
369 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500372 sb = quotactl_block(special);
373 if (IS_ERR(sb))
374 return PTR_ERR(sb);
375
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500376 ret = do_quotactl(sb, type, cmds, id, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500378 drop_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return ret;
380}