blob: 7a9bedeb1d571e1cd713f5c6fcb26547c9f46813 [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>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080016#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080017#include <linux/quotaops.h>
Vasily Tarasovb7163952007-07-15 23:41:12 -070018#include <linux/types.h>
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -050019#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Christoph Hellwigc988afb2010-02-16 03:44:50 -050021static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
22 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
Christoph Hellwigc988afb2010-02-16 03:44:50 -050024 switch (cmd) {
25 /* these commands do not require any special privilegues */
26 case Q_GETFMT:
27 case Q_SYNC:
28 case Q_GETINFO:
29 case Q_XGETQSTAT:
30 case Q_XQUOTASYNC:
31 break;
32 /* allow to query information for dquots we "own" */
33 case Q_GETQUOTA:
34 case Q_XGETQUOTA:
35 if ((type == USRQUOTA && current_euid() == id) ||
36 (type == GRPQUOTA && in_egroup_p(id)))
37 break;
38 /*FALLTHROUGH*/
39 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (!capable(CAP_SYS_ADMIN))
41 return -EPERM;
42 }
43
Christoph Hellwigc988afb2010-02-16 03:44:50 -050044 return security_quotactl(cmd, type, id, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Al Viro01a05b32010-03-23 06:06:58 -040047static void quota_sync_one(struct super_block *sb, void *arg)
48{
49 if (sb->s_qcop && sb->s_qcop->quota_sync)
50 sb->s_qcop->quota_sync(sb, *(int *)arg, 1);
51}
52
Christoph Hellwig6ae09572010-02-16 03:44:49 -050053static int quota_sync_all(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Christoph Hellwig6ae09572010-02-16 03:44:49 -050055 int ret;
56
57 if (type >= MAXQUOTAS)
58 return -EINVAL;
59 ret = security_quotactl(Q_SYNC, type, 0, NULL);
Al Viro01a05b32010-03-23 06:06:58 -040060 if (!ret)
61 iterate_supers(quota_sync_one, &type);
62 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Christoph Hellwigc411e5f2010-02-16 03:44:47 -050065static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
Jan Karaf00c9e42010-09-15 17:38:58 +020066 struct path *path)
Christoph Hellwigc411e5f2010-02-16 03:44:47 -050067{
Jan Karaf00c9e42010-09-15 17:38:58 +020068 if (!sb->s_qcop->quota_on && !sb->s_qcop->quota_on_meta)
69 return -ENOSYS;
70 if (sb->s_qcop->quota_on_meta)
71 return sb->s_qcop->quota_on_meta(sb, type, id);
72 if (IS_ERR(path))
73 return PTR_ERR(path);
74 return sb->s_qcop->quota_on(sb, type, id, path);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -050075}
76
77static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
78{
79 __u32 fmt;
80
81 down_read(&sb_dqopt(sb)->dqptr_sem);
82 if (!sb_has_quota_active(sb, type)) {
83 up_read(&sb_dqopt(sb)->dqptr_sem);
84 return -ESRCH;
85 }
86 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
87 up_read(&sb_dqopt(sb)->dqptr_sem);
88 if (copy_to_user(addr, &fmt, sizeof(fmt)))
89 return -EFAULT;
90 return 0;
91}
92
93static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
94{
95 struct if_dqinfo info;
96 int ret;
97
Christoph Hellwigf450d4f2010-02-16 03:44:48 -050098 if (!sb->s_qcop->get_info)
99 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500100 ret = sb->s_qcop->get_info(sb, type, &info);
101 if (!ret && copy_to_user(addr, &info, sizeof(info)))
102 return -EFAULT;
103 return ret;
104}
105
106static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
107{
108 struct if_dqinfo info;
109
110 if (copy_from_user(&info, addr, sizeof(info)))
111 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500112 if (!sb->s_qcop->set_info)
113 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500114 return sb->s_qcop->set_info(sb, type, &info);
115}
116
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400117static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src)
118{
119 dst->dqb_bhardlimit = src->d_blk_hardlimit;
120 dst->dqb_bsoftlimit = src->d_blk_softlimit;
121 dst->dqb_curspace = src->d_bcount;
122 dst->dqb_ihardlimit = src->d_ino_hardlimit;
123 dst->dqb_isoftlimit = src->d_ino_softlimit;
124 dst->dqb_curinodes = src->d_icount;
125 dst->dqb_btime = src->d_btimer;
126 dst->dqb_itime = src->d_itimer;
127 dst->dqb_valid = QIF_ALL;
128}
129
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500130static int quota_getquota(struct super_block *sb, int type, qid_t id,
131 void __user *addr)
132{
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400133 struct fs_disk_quota fdq;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500134 struct if_dqblk idq;
135 int ret;
136
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500137 if (!sb->s_qcop->get_dqblk)
138 return -ENOSYS;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400139 ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500140 if (ret)
141 return ret;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400142 copy_to_if_dqblk(&idq, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500143 if (copy_to_user(addr, &idq, sizeof(idq)))
144 return -EFAULT;
145 return 0;
146}
147
Christoph Hellwigc472b432010-05-06 17:05:17 -0400148static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src)
149{
150 dst->d_blk_hardlimit = src->dqb_bhardlimit;
151 dst->d_blk_softlimit = src->dqb_bsoftlimit;
152 dst->d_bcount = src->dqb_curspace;
153 dst->d_ino_hardlimit = src->dqb_ihardlimit;
154 dst->d_ino_softlimit = src->dqb_isoftlimit;
155 dst->d_icount = src->dqb_curinodes;
156 dst->d_btimer = src->dqb_btime;
157 dst->d_itimer = src->dqb_itime;
158
159 dst->d_fieldmask = 0;
160 if (src->dqb_valid & QIF_BLIMITS)
161 dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD;
162 if (src->dqb_valid & QIF_SPACE)
163 dst->d_fieldmask |= FS_DQ_BCOUNT;
164 if (src->dqb_valid & QIF_ILIMITS)
165 dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD;
166 if (src->dqb_valid & QIF_INODES)
167 dst->d_fieldmask |= FS_DQ_ICOUNT;
168 if (src->dqb_valid & QIF_BTIME)
169 dst->d_fieldmask |= FS_DQ_BTIMER;
170 if (src->dqb_valid & QIF_ITIME)
171 dst->d_fieldmask |= FS_DQ_ITIMER;
172}
173
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500174static int quota_setquota(struct super_block *sb, int type, qid_t id,
175 void __user *addr)
176{
Christoph Hellwigc472b432010-05-06 17:05:17 -0400177 struct fs_disk_quota fdq;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500178 struct if_dqblk idq;
179
180 if (copy_from_user(&idq, addr, sizeof(idq)))
181 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500182 if (!sb->s_qcop->set_dqblk)
183 return -ENOSYS;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400184 copy_from_if_dqblk(&fdq, &idq);
185 return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500186}
187
188static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
189{
190 __u32 flags;
191
192 if (copy_from_user(&flags, addr, sizeof(flags)))
193 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500194 if (!sb->s_qcop->set_xstate)
195 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500196 return sb->s_qcop->set_xstate(sb, flags, cmd);
197}
198
199static int quota_getxstate(struct super_block *sb, void __user *addr)
200{
201 struct fs_quota_stat fqs;
202 int ret;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500203
204 if (!sb->s_qcop->get_xstate)
205 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500206 ret = sb->s_qcop->get_xstate(sb, &fqs);
207 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
208 return -EFAULT;
209 return ret;
210}
211
212static int quota_setxquota(struct super_block *sb, int type, qid_t id,
213 void __user *addr)
214{
215 struct fs_disk_quota fdq;
216
217 if (copy_from_user(&fdq, addr, sizeof(fdq)))
218 return -EFAULT;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400219 if (!sb->s_qcop->set_dqblk)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500220 return -ENOSYS;
Christoph Hellwigc472b432010-05-06 17:05:17 -0400221 return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500222}
223
224static int quota_getxquota(struct super_block *sb, int type, qid_t id,
225 void __user *addr)
226{
227 struct fs_disk_quota fdq;
228 int ret;
229
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400230 if (!sb->s_qcop->get_dqblk)
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500231 return -ENOSYS;
Christoph Hellwigb9b2dd32010-05-06 17:04:58 -0400232 ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500233 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
234 return -EFAULT;
235 return ret;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/* Copy parameters and call proper function */
Jan Kara268157b2009-01-27 15:47:22 +0100239static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
Jan Karaf00c9e42010-09-15 17:38:58 +0200240 void __user *addr, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500242 int ret;
243
244 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
245 return -EINVAL;
246 if (!sb->s_qcop)
247 return -ENOSYS;
248
249 ret = check_quotactl_permission(sb, type, cmd, id);
250 if (ret < 0)
251 return ret;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 switch (cmd) {
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500254 case Q_QUOTAON:
Jan Karaf00c9e42010-09-15 17:38:58 +0200255 return quota_quotaon(sb, type, cmd, id, path);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500256 case Q_QUOTAOFF:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500257 if (!sb->s_qcop->quota_off)
258 return -ENOSYS;
Christoph Hellwig307ae182010-05-19 07:16:43 -0400259 return sb->s_qcop->quota_off(sb, type);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500260 case Q_GETFMT:
261 return quota_getfmt(sb, type, addr);
262 case Q_GETINFO:
263 return quota_getinfo(sb, type, addr);
264 case Q_SETINFO:
265 return quota_setinfo(sb, type, addr);
266 case Q_GETQUOTA:
267 return quota_getquota(sb, type, id, addr);
268 case Q_SETQUOTA:
269 return quota_setquota(sb, type, id, addr);
270 case Q_SYNC:
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500271 if (!sb->s_qcop->quota_sync)
272 return -ENOSYS;
Christoph Hellwig5fb324a2010-02-16 03:44:52 -0500273 return sb->s_qcop->quota_sync(sb, type, 1);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500274 case Q_XQUOTAON:
275 case Q_XQUOTAOFF:
276 case Q_XQUOTARM:
277 return quota_setxstate(sb, cmd, addr);
278 case Q_XGETQSTAT:
279 return quota_getxstate(sb, addr);
280 case Q_XSETQLIM:
281 return quota_setxquota(sb, type, id, addr);
282 case Q_XGETQUOTA:
283 return quota_getxquota(sb, type, id, addr);
284 case Q_XQUOTASYNC:
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -0500285 if (sb->s_flags & MS_RDONLY)
286 return -EROFS;
Christoph Hellwig4b217ed2012-02-20 02:28:18 +0000287 /* XFS quotas are fully coherent now, making this call a noop */
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -0500288 return 0;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500289 default:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500290 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*
David Howells93614012006-09-30 20:45:40 +0200295 * look up a superblock on which quota ops will be performed
296 * - use the name of a block device to find the superblock thereon
297 */
Jan Kara7a2435d2009-01-27 01:47:11 +0100298static struct super_block *quotactl_block(const char __user *special)
David Howells93614012006-09-30 20:45:40 +0200299{
300#ifdef CONFIG_BLOCK
301 struct block_device *bdev;
302 struct super_block *sb;
303 char *tmp = getname(special);
304
305 if (IS_ERR(tmp))
David Howellse231c2e2008-02-07 00:15:26 -0800306 return ERR_CAST(tmp);
David Howells93614012006-09-30 20:45:40 +0200307 bdev = lookup_bdev(tmp);
308 putname(tmp);
309 if (IS_ERR(bdev))
David Howellse231c2e2008-02-07 00:15:26 -0800310 return ERR_CAST(bdev);
David Howells93614012006-09-30 20:45:40 +0200311 sb = get_super(bdev);
312 bdput(bdev);
313 if (!sb)
314 return ERR_PTR(-ENODEV);
315
316 return sb;
317#else
318 return ERR_PTR(-ENODEV);
319#endif
320}
321
322/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * This is the system call interface. This communicates with
324 * the user-level programs. Currently this only supports diskquota
325 * calls. Maybe we need to add the process quotas etc. in the future,
326 * but we probably should use rlimits for that.
327 */
Heiko Carstens3cdad422009-01-14 14:14:22 +0100328SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
329 qid_t, id, void __user *, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 uint cmds, type;
332 struct super_block *sb = NULL;
Jan Karaf00c9e42010-09-15 17:38:58 +0200333 struct path path, *pathp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int ret;
335
336 cmds = cmd >> SUBCMDSHIFT;
337 type = cmd & SUBCMDMASK;
338
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500339 /*
340 * As a special case Q_SYNC can be called without a specific device.
341 * It will iterate all superblocks that have quota enabled and call
342 * the sync action on each of them.
343 */
344 if (!special) {
345 if (cmds == Q_SYNC)
346 return quota_sync_all(type);
347 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
Jan Karaf00c9e42010-09-15 17:38:58 +0200350 /*
351 * Path for quotaon has to be resolved before grabbing superblock
352 * because that gets s_umount sem which is also possibly needed by path
353 * resolution (think about autofs) and thus deadlocks could arise.
354 */
355 if (cmds == Q_QUOTAON) {
Trond Myklebust815d4052011-09-26 20:36:09 -0400356 ret = user_path_at(AT_FDCWD, addr, LOOKUP_FOLLOW|LOOKUP_AUTOMOUNT, &path);
Jan Karaf00c9e42010-09-15 17:38:58 +0200357 if (ret)
358 pathp = ERR_PTR(ret);
359 else
360 pathp = &path;
361 }
362
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500363 sb = quotactl_block(special);
Jan Kara0aaa6182011-10-10 18:32:06 +0200364 if (IS_ERR(sb)) {
365 ret = PTR_ERR(sb);
366 goto out;
367 }
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500368
Jan Karaf00c9e42010-09-15 17:38:58 +0200369 ret = do_quotactl(sb, type, cmds, id, addr, pathp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500371 drop_super(sb);
Jan Kara0aaa6182011-10-10 18:32:06 +0200372out:
Jan Karaf00c9e42010-09-15 17:38:58 +0200373 if (pathp && !IS_ERR(pathp))
374 path_put(pathp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return ret;
376}