blob: 9fde5cd84f8dd4cb4137e49f6b52c9a08947abfe [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>
Vasily Tarasovb7163952007-07-15 23:41:12 -070013#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/security.h>
16#include <linux/syscalls.h>
17#include <linux/buffer_head.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018#include <linux/capability.h>
Adrian Bunkbe586ba2005-11-07 00:59:35 -080019#include <linux/quotaops.h>
Vasily Tarasovb7163952007-07-15 23:41:12 -070020#include <linux/types.h>
Steven Whitehouse86e931a2009-09-28 12:35:17 +010021#include <net/netlink.h>
22#include <net/genetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/* Check validity of generic quotactl commands */
Jan Kara268157b2009-01-27 15:47:22 +010025static int generic_quotactl_valid(struct super_block *sb, int type, int cmd,
26 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 if (type >= MAXQUOTAS)
29 return -EINVAL;
30 if (!sb && cmd != Q_SYNC)
31 return -ENODEV;
32 /* Is operation supported? */
33 if (sb && !sb->s_qcop)
34 return -ENOSYS;
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 /* Check privileges */
37 if (cmd == Q_GETQUOTA) {
David Howellsda9592e2008-11-14 10:39:05 +110038 if (((type == USRQUOTA && current_euid() != id) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 (type == GRPQUOTA && !in_egroup_p(id))) &&
40 !capable(CAP_SYS_ADMIN))
41 return -EPERM;
42 }
43 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
44 if (!capable(CAP_SYS_ADMIN))
45 return -EPERM;
46
47 return 0;
48}
49
50/* Check validity of XFS Quota Manager commands */
Jan Kara268157b2009-01-27 15:47:22 +010051static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd,
52 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 if (type >= XQM_MAXQUOTAS)
55 return -EINVAL;
56 if (!sb)
57 return -ENODEV;
58 if (!sb->s_qcop)
59 return -ENOSYS;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* Check privileges */
62 if (cmd == Q_XGETQUOTA) {
David Howellsda9592e2008-11-14 10:39:05 +110063 if (((type == XQM_USRQUOTA && current_euid() != id) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
65 !capable(CAP_SYS_ADMIN))
66 return -EPERM;
Nathan Scottde69e5f2005-11-03 13:53:34 +110067 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!capable(CAP_SYS_ADMIN))
69 return -EPERM;
70 }
71
72 return 0;
73}
74
Jan Kara268157b2009-01-27 15:47:22 +010075static int check_quotactl_valid(struct super_block *sb, int type, int cmd,
76 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 int error;
79
80 if (XQM_COMMAND(cmd))
81 error = xqm_quotactl_valid(sb, type, cmd, id);
82 else
83 error = generic_quotactl_valid(sb, type, cmd, id);
84 if (!error)
85 error = security_quotactl(cmd, type, id, sb);
86 return error;
87}
88
Christoph Hellwig850b2012009-04-27 16:43:54 +020089#ifdef CONFIG_QUOTA
90void sync_quota_sb(struct super_block *sb, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Christoph Hellwig850b2012009-04-27 16:43:54 +020094 if (!sb->s_qcop->quota_sync)
95 return;
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 sb->s_qcop->quota_sync(sb, type);
Jan Karaca785ec2008-09-30 17:53:37 +020098
99 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* This is not very clever (and fast) but currently I don't know about
102 * any other simple way of getting quota data to disk and we must get
103 * them there for userspace to be visible... */
104 if (sb->s_op->sync_fs)
105 sb->s_op->sync_fs(sb, 1);
106 sync_blockdev(sb->s_bdev);
107
Jan Kara79254092007-05-16 22:11:19 -0700108 /*
109 * Now when everything is written we can discard the pagecache so
110 * that userspace sees the changes.
111 */
Ingo Molnard3be9152006-03-23 03:00:29 -0800112 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (type != -1 && cnt != type)
115 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200116 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 continue;
Jan Kara268157b2009-01-27 15:47:22 +0100118 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
119 I_MUTEX_QUOTA);
Jan Kara79254092007-05-16 22:11:19 -0700120 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
121 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Ingo Molnard3be9152006-03-23 03:00:29 -0800123 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
Christoph Hellwig850b2012009-04-27 16:43:54 +0200125#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500127static int quota_sync_all(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Christoph Hellwig850b2012009-04-27 16:43:54 +0200129 struct super_block *sb;
Jan Kara02a55ca2008-07-25 01:46:50 -0700130 int cnt;
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500131 int ret;
132
133 if (type >= MAXQUOTAS)
134 return -EINVAL;
135 ret = security_quotactl(Q_SYNC, type, 0, NULL);
136 if (ret)
137 return ret;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700138
Kirill Korotaev618f0632005-06-23 00:09:54 -0700139 spin_lock(&sb_lock);
140restart:
141 list_for_each_entry(sb, &super_blocks, s_list) {
Jan Kara268157b2009-01-27 15:47:22 +0100142 /* This test just improves performance so it needn't be
143 * reliable... */
Jan Kara02a55ca2008-07-25 01:46:50 -0700144 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
145 if (type != -1 && type != cnt)
146 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200147 if (!sb_has_quota_active(sb, cnt))
Jan Kara02a55ca2008-07-25 01:46:50 -0700148 continue;
149 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
Jan Kara268157b2009-01-27 15:47:22 +0100150 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
Jan Kara02a55ca2008-07-25 01:46:50 -0700151 continue;
152 break;
153 }
154 if (cnt == MAXQUOTAS)
Kirill Korotaev618f0632005-06-23 00:09:54 -0700155 continue;
156 sb->s_count++;
157 spin_unlock(&sb_lock);
158 down_read(&sb->s_umount);
Christoph Hellwig850b2012009-04-27 16:43:54 +0200159 if (sb->s_root)
160 sync_quota_sb(sb, type);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700161 up_read(&sb->s_umount);
162 spin_lock(&sb_lock);
163 if (__put_super_and_need_restart(sb))
164 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Kirill Korotaev618f0632005-06-23 00:09:54 -0700166 spin_unlock(&sb_lock);
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500167
168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500171static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
172 void __user *addr)
173{
174 char *pathname;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500175 int ret = -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500176
177 pathname = getname(addr);
178 if (IS_ERR(pathname))
179 return PTR_ERR(pathname);
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500180 if (sb->s_qcop->quota_on)
181 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500182 putname(pathname);
183 return ret;
184}
185
186static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
187{
188 __u32 fmt;
189
190 down_read(&sb_dqopt(sb)->dqptr_sem);
191 if (!sb_has_quota_active(sb, type)) {
192 up_read(&sb_dqopt(sb)->dqptr_sem);
193 return -ESRCH;
194 }
195 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
196 up_read(&sb_dqopt(sb)->dqptr_sem);
197 if (copy_to_user(addr, &fmt, sizeof(fmt)))
198 return -EFAULT;
199 return 0;
200}
201
202static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
203{
204 struct if_dqinfo info;
205 int ret;
206
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500207 if (!sb_has_quota_active(sb, type))
208 return -ESRCH;
209 if (!sb->s_qcop->get_info)
210 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500211 ret = sb->s_qcop->get_info(sb, type, &info);
212 if (!ret && copy_to_user(addr, &info, sizeof(info)))
213 return -EFAULT;
214 return ret;
215}
216
217static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
218{
219 struct if_dqinfo info;
220
221 if (copy_from_user(&info, addr, sizeof(info)))
222 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500223 if (!sb_has_quota_active(sb, type))
224 return -ESRCH;
225 if (!sb->s_qcop->set_info)
226 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500227 return sb->s_qcop->set_info(sb, type, &info);
228}
229
230static int quota_getquota(struct super_block *sb, int type, qid_t id,
231 void __user *addr)
232{
233 struct if_dqblk idq;
234 int ret;
235
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500236 if (!sb_has_quota_active(sb, type))
237 return -ESRCH;
238 if (!sb->s_qcop->get_dqblk)
239 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500240 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
241 if (ret)
242 return ret;
243 if (copy_to_user(addr, &idq, sizeof(idq)))
244 return -EFAULT;
245 return 0;
246}
247
248static int quota_setquota(struct super_block *sb, int type, qid_t id,
249 void __user *addr)
250{
251 struct if_dqblk idq;
252
253 if (copy_from_user(&idq, addr, sizeof(idq)))
254 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500255 if (!sb_has_quota_active(sb, type))
256 return -ESRCH;
257 if (!sb->s_qcop->set_dqblk)
258 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500259 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
260}
261
262static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
263{
264 __u32 flags;
265
266 if (copy_from_user(&flags, addr, sizeof(flags)))
267 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500268 if (!sb->s_qcop->set_xstate)
269 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500270 return sb->s_qcop->set_xstate(sb, flags, cmd);
271}
272
273static int quota_getxstate(struct super_block *sb, void __user *addr)
274{
275 struct fs_quota_stat fqs;
276 int ret;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500277
278 if (!sb->s_qcop->get_xstate)
279 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500280 ret = sb->s_qcop->get_xstate(sb, &fqs);
281 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
282 return -EFAULT;
283 return ret;
284}
285
286static int quota_setxquota(struct super_block *sb, int type, qid_t id,
287 void __user *addr)
288{
289 struct fs_disk_quota fdq;
290
291 if (copy_from_user(&fdq, addr, sizeof(fdq)))
292 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500293 if (!sb->s_qcop->set_xquota)
294 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500295 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
296}
297
298static int quota_getxquota(struct super_block *sb, int type, qid_t id,
299 void __user *addr)
300{
301 struct fs_disk_quota fdq;
302 int ret;
303
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500304 if (!sb->s_qcop->get_xquota)
305 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500306 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
307 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
308 return -EFAULT;
309 return ret;
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* Copy parameters and call proper function */
Jan Kara268157b2009-01-27 15:47:22 +0100313static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
314 void __user *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 switch (cmd) {
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500317 case Q_QUOTAON:
318 return quota_quotaon(sb, type, cmd, id, addr);
319 case Q_QUOTAOFF:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500320 if (!sb->s_qcop->quota_off)
321 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500322 return sb->s_qcop->quota_off(sb, type, 0);
323 case Q_GETFMT:
324 return quota_getfmt(sb, type, addr);
325 case Q_GETINFO:
326 return quota_getinfo(sb, type, addr);
327 case Q_SETINFO:
328 return quota_setinfo(sb, type, addr);
329 case Q_GETQUOTA:
330 return quota_getquota(sb, type, id, addr);
331 case Q_SETQUOTA:
332 return quota_setquota(sb, type, id, addr);
333 case Q_SYNC:
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500334 if (!sb->s_qcop->quota_sync)
335 return -ENOSYS;
336 sync_quota_sb(sb, type);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500337 return 0;
338 case Q_XQUOTAON:
339 case Q_XQUOTAOFF:
340 case Q_XQUOTARM:
341 return quota_setxstate(sb, cmd, addr);
342 case Q_XGETQSTAT:
343 return quota_getxstate(sb, addr);
344 case Q_XSETQLIM:
345 return quota_setxquota(sb, type, id, addr);
346 case Q_XGETQUOTA:
347 return quota_getxquota(sb, type, id, addr);
348 case Q_XQUOTASYNC:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500349 if (!sb->s_qcop->quota_sync)
350 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500351 return sb->s_qcop->quota_sync(sb, type);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500352 default:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
David Howells93614012006-09-30 20:45:40 +0200358 * look up a superblock on which quota ops will be performed
359 * - use the name of a block device to find the superblock thereon
360 */
Jan Kara7a2435d2009-01-27 01:47:11 +0100361static struct super_block *quotactl_block(const char __user *special)
David Howells93614012006-09-30 20:45:40 +0200362{
363#ifdef CONFIG_BLOCK
364 struct block_device *bdev;
365 struct super_block *sb;
366 char *tmp = getname(special);
367
368 if (IS_ERR(tmp))
David Howellse231c2e2008-02-07 00:15:26 -0800369 return ERR_CAST(tmp);
David Howells93614012006-09-30 20:45:40 +0200370 bdev = lookup_bdev(tmp);
371 putname(tmp);
372 if (IS_ERR(bdev))
David Howellse231c2e2008-02-07 00:15:26 -0800373 return ERR_CAST(bdev);
David Howells93614012006-09-30 20:45:40 +0200374 sb = get_super(bdev);
375 bdput(bdev);
376 if (!sb)
377 return ERR_PTR(-ENODEV);
378
379 return sb;
380#else
381 return ERR_PTR(-ENODEV);
382#endif
383}
384
385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * This is the system call interface. This communicates with
387 * the user-level programs. Currently this only supports diskquota
388 * calls. Maybe we need to add the process quotas etc. in the future,
389 * but we probably should use rlimits for that.
390 */
Heiko Carstens3cdad422009-01-14 14:14:22 +0100391SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
392 qid_t, id, void __user *, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 uint cmds, type;
395 struct super_block *sb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int ret;
397
398 cmds = cmd >> SUBCMDSHIFT;
399 type = cmd & SUBCMDMASK;
400
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500401 /*
402 * As a special case Q_SYNC can be called without a specific device.
403 * It will iterate all superblocks that have quota enabled and call
404 * the sync action on each of them.
405 */
406 if (!special) {
407 if (cmds == Q_SYNC)
408 return quota_sync_all(type);
409 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500412 sb = quotactl_block(special);
413 if (IS_ERR(sb))
414 return PTR_ERR(sb);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ret = check_quotactl_valid(sb, type, cmds, id);
417 if (ret >= 0)
418 ret = do_quotactl(sb, type, cmds, id, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500420 drop_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return ret;
422}
Vasily Tarasovb7163952007-07-15 23:41:12 -0700423
Tony Luck7a6c8132007-07-27 15:35:43 -0700424#if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
Vasily Tarasovb7163952007-07-15 23:41:12 -0700425/*
426 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
427 * and is necessary due to alignment problems.
428 */
429struct compat_if_dqblk {
430 compat_u64 dqb_bhardlimit;
431 compat_u64 dqb_bsoftlimit;
432 compat_u64 dqb_curspace;
433 compat_u64 dqb_ihardlimit;
434 compat_u64 dqb_isoftlimit;
435 compat_u64 dqb_curinodes;
436 compat_u64 dqb_btime;
437 compat_u64 dqb_itime;
438 compat_uint_t dqb_valid;
439};
440
441/* XFS structures */
442struct compat_fs_qfilestat {
443 compat_u64 dqb_bhardlimit;
444 compat_u64 qfs_nblks;
445 compat_uint_t qfs_nextents;
446};
447
448struct compat_fs_quota_stat {
449 __s8 qs_version;
450 __u16 qs_flags;
451 __s8 qs_pad;
452 struct compat_fs_qfilestat qs_uquota;
453 struct compat_fs_qfilestat qs_gquota;
454 compat_uint_t qs_incoredqs;
455 compat_int_t qs_btimelimit;
456 compat_int_t qs_itimelimit;
457 compat_int_t qs_rtbtimelimit;
458 __u16 qs_bwarnlimit;
459 __u16 qs_iwarnlimit;
460};
461
462asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
463 qid_t id, void __user *addr)
464{
465 unsigned int cmds;
466 struct if_dqblk __user *dqblk;
467 struct compat_if_dqblk __user *compat_dqblk;
468 struct fs_quota_stat __user *fsqstat;
469 struct compat_fs_quota_stat __user *compat_fsqstat;
470 compat_uint_t data;
471 u16 xdata;
472 long ret;
473
474 cmds = cmd >> SUBCMDSHIFT;
475
476 switch (cmds) {
477 case Q_GETQUOTA:
478 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
479 compat_dqblk = addr;
480 ret = sys_quotactl(cmd, special, id, dqblk);
481 if (ret)
482 break;
483 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
484 get_user(data, &dqblk->dqb_valid) ||
485 put_user(data, &compat_dqblk->dqb_valid))
486 ret = -EFAULT;
487 break;
488 case Q_SETQUOTA:
489 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
490 compat_dqblk = addr;
491 ret = -EFAULT;
492 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
493 get_user(data, &compat_dqblk->dqb_valid) ||
494 put_user(data, &dqblk->dqb_valid))
495 break;
496 ret = sys_quotactl(cmd, special, id, dqblk);
497 break;
498 case Q_XGETQSTAT:
499 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
500 compat_fsqstat = addr;
501 ret = sys_quotactl(cmd, special, id, fsqstat);
502 if (ret)
503 break;
504 ret = -EFAULT;
505 /* Copying qs_version, qs_flags, qs_pad */
506 if (copy_in_user(compat_fsqstat, fsqstat,
507 offsetof(struct compat_fs_quota_stat, qs_uquota)))
508 break;
509 /* Copying qs_uquota */
510 if (copy_in_user(&compat_fsqstat->qs_uquota,
511 &fsqstat->qs_uquota,
512 sizeof(compat_fsqstat->qs_uquota)) ||
513 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
514 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
515 break;
516 /* Copying qs_gquota */
517 if (copy_in_user(&compat_fsqstat->qs_gquota,
518 &fsqstat->qs_gquota,
519 sizeof(compat_fsqstat->qs_gquota)) ||
520 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
521 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
522 break;
523 /* Copying the rest */
524 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
525 &fsqstat->qs_incoredqs,
526 sizeof(struct compat_fs_quota_stat) -
527 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
528 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
529 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
530 break;
531 ret = 0;
532 break;
533 default:
534 ret = sys_quotactl(cmd, special, id, addr);
535 }
536 return ret;
537}
538#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +0100539
540
541#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
542
543/* Netlink family structure for quota */
544static struct genl_family quota_genl_family = {
545 .id = GENL_ID_GENERATE,
546 .hdrsize = 0,
547 .name = "VFS_DQUOT",
548 .version = 1,
549 .maxattr = QUOTA_NL_A_MAX,
550};
551
552/**
553 * quota_send_warning - Send warning to userspace about exceeded quota
554 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
555 * @id: The user or group id of the quota that was exceeded
556 * @dev: The device on which the fs is mounted (sb->s_dev)
557 * @warntype: The type of the warning: QUOTA_NL_...
558 *
559 * This can be used by filesystems (including those which don't use
560 * dquot) to send a message to userspace relating to quota limits.
561 *
562 */
563
564void quota_send_warning(short type, unsigned int id, dev_t dev,
565 const char warntype)
566{
567 static atomic_t seq;
568 struct sk_buff *skb;
569 void *msg_head;
570 int ret;
571 int msg_size = 4 * nla_total_size(sizeof(u32)) +
572 2 * nla_total_size(sizeof(u64));
573
574 /* We have to allocate using GFP_NOFS as we are called from a
575 * filesystem performing write and thus further recursion into
576 * the fs to free some data could cause deadlocks. */
577 skb = genlmsg_new(msg_size, GFP_NOFS);
578 if (!skb) {
579 printk(KERN_ERR
580 "VFS: Not enough memory to send quota warning.\n");
581 return;
582 }
583 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
584 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
585 if (!msg_head) {
586 printk(KERN_ERR
587 "VFS: Cannot store netlink header in quota warning.\n");
588 goto err_out;
589 }
590 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
591 if (ret)
592 goto attr_err_out;
593 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
594 if (ret)
595 goto attr_err_out;
596 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
597 if (ret)
598 goto attr_err_out;
599 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
600 if (ret)
601 goto attr_err_out;
602 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
603 if (ret)
604 goto attr_err_out;
605 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
606 if (ret)
607 goto attr_err_out;
608 genlmsg_end(skb, msg_head);
609
610 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
611 return;
612attr_err_out:
613 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
614err_out:
615 kfree_skb(skb);
616}
617EXPORT_SYMBOL(quota_send_warning);
618
619static int __init quota_init(void)
620{
621 if (genl_register_family(&quota_genl_family) != 0)
622 printk(KERN_ERR
623 "VFS: Failed to create quota netlink interface.\n");
624 return 0;
625};
626
627module_init(quota_init);
628#endif
629