blob: 4d7fdc4443b9cae0966f692aa5304c90d41938dc [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
36 switch (cmd) {
37 case Q_GETFMT:
38 break;
39 case Q_QUOTAON:
40 if (!sb->s_qcop->quota_on)
41 return -ENOSYS;
42 break;
43 case Q_QUOTAOFF:
44 if (!sb->s_qcop->quota_off)
45 return -ENOSYS;
46 break;
47 case Q_SETINFO:
48 if (!sb->s_qcop->set_info)
49 return -ENOSYS;
50 break;
51 case Q_GETINFO:
52 if (!sb->s_qcop->get_info)
53 return -ENOSYS;
54 break;
55 case Q_SETQUOTA:
56 if (!sb->s_qcop->set_dqblk)
57 return -ENOSYS;
58 break;
59 case Q_GETQUOTA:
60 if (!sb->s_qcop->get_dqblk)
61 return -ENOSYS;
62 break;
63 case Q_SYNC:
64 if (sb && !sb->s_qcop->quota_sync)
65 return -ENOSYS;
66 break;
67 default:
68 return -EINVAL;
69 }
70
71 /* Is quota turned on for commands which need it? */
72 switch (cmd) {
73 case Q_GETFMT:
74 case Q_GETINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 case Q_SETINFO:
76 case Q_SETQUOTA:
77 case Q_GETQUOTA:
Jan Kara268157b2009-01-27 15:47:22 +010078 /* This is just an informative test so we are satisfied
79 * without the lock */
Jan Karaf55abc02008-08-20 17:50:32 +020080 if (!sb_has_quota_active(sb, type))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -ESRCH;
82 }
83
84 /* Check privileges */
85 if (cmd == Q_GETQUOTA) {
David Howellsda9592e2008-11-14 10:39:05 +110086 if (((type == USRQUOTA && current_euid() != id) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 (type == GRPQUOTA && !in_egroup_p(id))) &&
88 !capable(CAP_SYS_ADMIN))
89 return -EPERM;
90 }
91 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
92 if (!capable(CAP_SYS_ADMIN))
93 return -EPERM;
94
95 return 0;
96}
97
98/* Check validity of XFS Quota Manager commands */
Jan Kara268157b2009-01-27 15:47:22 +010099static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd,
100 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 if (type >= XQM_MAXQUOTAS)
103 return -EINVAL;
104 if (!sb)
105 return -ENODEV;
106 if (!sb->s_qcop)
107 return -ENOSYS;
108
109 switch (cmd) {
110 case Q_XQUOTAON:
111 case Q_XQUOTAOFF:
112 case Q_XQUOTARM:
113 if (!sb->s_qcop->set_xstate)
114 return -ENOSYS;
115 break;
116 case Q_XGETQSTAT:
117 if (!sb->s_qcop->get_xstate)
118 return -ENOSYS;
119 break;
120 case Q_XSETQLIM:
121 if (!sb->s_qcop->set_xquota)
122 return -ENOSYS;
123 break;
124 case Q_XGETQUOTA:
125 if (!sb->s_qcop->get_xquota)
126 return -ENOSYS;
127 break;
Nathan Scottde69e5f2005-11-03 13:53:34 +1100128 case Q_XQUOTASYNC:
129 if (!sb->s_qcop->quota_sync)
130 return -ENOSYS;
131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 default:
133 return -EINVAL;
134 }
135
136 /* Check privileges */
137 if (cmd == Q_XGETQUOTA) {
David Howellsda9592e2008-11-14 10:39:05 +1100138 if (((type == XQM_USRQUOTA && current_euid() != id) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
140 !capable(CAP_SYS_ADMIN))
141 return -EPERM;
Nathan Scottde69e5f2005-11-03 13:53:34 +1100142 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!capable(CAP_SYS_ADMIN))
144 return -EPERM;
145 }
146
147 return 0;
148}
149
Jan Kara268157b2009-01-27 15:47:22 +0100150static int check_quotactl_valid(struct super_block *sb, int type, int cmd,
151 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int error;
154
155 if (XQM_COMMAND(cmd))
156 error = xqm_quotactl_valid(sb, type, cmd, id);
157 else
158 error = generic_quotactl_valid(sb, type, cmd, id);
159 if (!error)
160 error = security_quotactl(cmd, type, id, sb);
161 return error;
162}
163
Christoph Hellwig850b2012009-04-27 16:43:54 +0200164#ifdef CONFIG_QUOTA
165void sync_quota_sb(struct super_block *sb, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Christoph Hellwig850b2012009-04-27 16:43:54 +0200169 if (!sb->s_qcop->quota_sync)
170 return;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sb->s_qcop->quota_sync(sb, type);
Jan Karaca785ec2008-09-30 17:53:37 +0200173
174 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* This is not very clever (and fast) but currently I don't know about
177 * any other simple way of getting quota data to disk and we must get
178 * them there for userspace to be visible... */
179 if (sb->s_op->sync_fs)
180 sb->s_op->sync_fs(sb, 1);
181 sync_blockdev(sb->s_bdev);
182
Jan Kara79254092007-05-16 22:11:19 -0700183 /*
184 * Now when everything is written we can discard the pagecache so
185 * that userspace sees the changes.
186 */
Ingo Molnard3be9152006-03-23 03:00:29 -0800187 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (type != -1 && cnt != type)
190 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200191 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 continue;
Jan Kara268157b2009-01-27 15:47:22 +0100193 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
194 I_MUTEX_QUOTA);
Jan Kara79254092007-05-16 22:11:19 -0700195 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
196 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Ingo Molnard3be9152006-03-23 03:00:29 -0800198 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
Christoph Hellwig850b2012009-04-27 16:43:54 +0200200#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Christoph Hellwig850b2012009-04-27 16:43:54 +0200202static void sync_dquots(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Christoph Hellwig850b2012009-04-27 16:43:54 +0200204 struct super_block *sb;
Jan Kara02a55ca2008-07-25 01:46:50 -0700205 int cnt;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700206
Kirill Korotaev618f0632005-06-23 00:09:54 -0700207 spin_lock(&sb_lock);
208restart:
209 list_for_each_entry(sb, &super_blocks, s_list) {
Jan Kara268157b2009-01-27 15:47:22 +0100210 /* This test just improves performance so it needn't be
211 * reliable... */
Jan Kara02a55ca2008-07-25 01:46:50 -0700212 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
213 if (type != -1 && type != cnt)
214 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200215 if (!sb_has_quota_active(sb, cnt))
Jan Kara02a55ca2008-07-25 01:46:50 -0700216 continue;
217 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
Jan Kara268157b2009-01-27 15:47:22 +0100218 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
Jan Kara02a55ca2008-07-25 01:46:50 -0700219 continue;
220 break;
221 }
222 if (cnt == MAXQUOTAS)
Kirill Korotaev618f0632005-06-23 00:09:54 -0700223 continue;
224 sb->s_count++;
225 spin_unlock(&sb_lock);
226 down_read(&sb->s_umount);
Christoph Hellwig850b2012009-04-27 16:43:54 +0200227 if (sb->s_root)
228 sync_quota_sb(sb, type);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700229 up_read(&sb->s_umount);
230 spin_lock(&sb_lock);
231 if (__put_super_and_need_restart(sb))
232 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Kirill Korotaev618f0632005-06-23 00:09:54 -0700234 spin_unlock(&sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500237static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
238 void __user *addr)
239{
240 char *pathname;
241 int ret;
242
243 pathname = getname(addr);
244 if (IS_ERR(pathname))
245 return PTR_ERR(pathname);
246 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
247 putname(pathname);
248 return ret;
249}
250
251static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
252{
253 __u32 fmt;
254
255 down_read(&sb_dqopt(sb)->dqptr_sem);
256 if (!sb_has_quota_active(sb, type)) {
257 up_read(&sb_dqopt(sb)->dqptr_sem);
258 return -ESRCH;
259 }
260 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
261 up_read(&sb_dqopt(sb)->dqptr_sem);
262 if (copy_to_user(addr, &fmt, sizeof(fmt)))
263 return -EFAULT;
264 return 0;
265}
266
267static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
268{
269 struct if_dqinfo info;
270 int ret;
271
272 ret = sb->s_qcop->get_info(sb, type, &info);
273 if (!ret && copy_to_user(addr, &info, sizeof(info)))
274 return -EFAULT;
275 return ret;
276}
277
278static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
279{
280 struct if_dqinfo info;
281
282 if (copy_from_user(&info, addr, sizeof(info)))
283 return -EFAULT;
284 return sb->s_qcop->set_info(sb, type, &info);
285}
286
287static int quota_getquota(struct super_block *sb, int type, qid_t id,
288 void __user *addr)
289{
290 struct if_dqblk idq;
291 int ret;
292
293 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
294 if (ret)
295 return ret;
296 if (copy_to_user(addr, &idq, sizeof(idq)))
297 return -EFAULT;
298 return 0;
299}
300
301static int quota_setquota(struct super_block *sb, int type, qid_t id,
302 void __user *addr)
303{
304 struct if_dqblk idq;
305
306 if (copy_from_user(&idq, addr, sizeof(idq)))
307 return -EFAULT;
308 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
309}
310
311static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
312{
313 __u32 flags;
314
315 if (copy_from_user(&flags, addr, sizeof(flags)))
316 return -EFAULT;
317 return sb->s_qcop->set_xstate(sb, flags, cmd);
318}
319
320static int quota_getxstate(struct super_block *sb, void __user *addr)
321{
322 struct fs_quota_stat fqs;
323 int ret;
324
325 ret = sb->s_qcop->get_xstate(sb, &fqs);
326 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
327 return -EFAULT;
328 return ret;
329}
330
331static int quota_setxquota(struct super_block *sb, int type, qid_t id,
332 void __user *addr)
333{
334 struct fs_disk_quota fdq;
335
336 if (copy_from_user(&fdq, addr, sizeof(fdq)))
337 return -EFAULT;
338 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
339}
340
341static int quota_getxquota(struct super_block *sb, int type, qid_t id,
342 void __user *addr)
343{
344 struct fs_disk_quota fdq;
345 int ret;
346
347 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
348 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
349 return -EFAULT;
350 return ret;
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/* Copy parameters and call proper function */
Jan Kara268157b2009-01-27 15:47:22 +0100354static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
355 void __user *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 switch (cmd) {
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500358 case Q_QUOTAON:
359 return quota_quotaon(sb, type, cmd, id, addr);
360 case Q_QUOTAOFF:
361 return sb->s_qcop->quota_off(sb, type, 0);
362 case Q_GETFMT:
363 return quota_getfmt(sb, type, addr);
364 case Q_GETINFO:
365 return quota_getinfo(sb, type, addr);
366 case Q_SETINFO:
367 return quota_setinfo(sb, type, addr);
368 case Q_GETQUOTA:
369 return quota_getquota(sb, type, id, addr);
370 case Q_SETQUOTA:
371 return quota_setquota(sb, type, id, addr);
372 case Q_SYNC:
373 if (sb)
374 sync_quota_sb(sb, type);
375 else
376 sync_dquots(type);
377 return 0;
378 case Q_XQUOTAON:
379 case Q_XQUOTAOFF:
380 case Q_XQUOTARM:
381 return quota_setxstate(sb, cmd, addr);
382 case Q_XGETQSTAT:
383 return quota_getxstate(sb, addr);
384 case Q_XSETQLIM:
385 return quota_setxquota(sb, type, id, addr);
386 case Q_XGETQUOTA:
387 return quota_getxquota(sb, type, id, addr);
388 case Q_XQUOTASYNC:
389 return sb->s_qcop->quota_sync(sb, type);
390 /* We never reach here unless validity check is broken */
391 default:
392 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return 0;
396}
397
398/*
David Howells93614012006-09-30 20:45:40 +0200399 * look up a superblock on which quota ops will be performed
400 * - use the name of a block device to find the superblock thereon
401 */
Jan Kara7a2435d2009-01-27 01:47:11 +0100402static struct super_block *quotactl_block(const char __user *special)
David Howells93614012006-09-30 20:45:40 +0200403{
404#ifdef CONFIG_BLOCK
405 struct block_device *bdev;
406 struct super_block *sb;
407 char *tmp = getname(special);
408
409 if (IS_ERR(tmp))
David Howellse231c2e2008-02-07 00:15:26 -0800410 return ERR_CAST(tmp);
David Howells93614012006-09-30 20:45:40 +0200411 bdev = lookup_bdev(tmp);
412 putname(tmp);
413 if (IS_ERR(bdev))
David Howellse231c2e2008-02-07 00:15:26 -0800414 return ERR_CAST(bdev);
David Howells93614012006-09-30 20:45:40 +0200415 sb = get_super(bdev);
416 bdput(bdev);
417 if (!sb)
418 return ERR_PTR(-ENODEV);
419
420 return sb;
421#else
422 return ERR_PTR(-ENODEV);
423#endif
424}
425
426/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * This is the system call interface. This communicates with
428 * the user-level programs. Currently this only supports diskquota
429 * calls. Maybe we need to add the process quotas etc. in the future,
430 * but we probably should use rlimits for that.
431 */
Heiko Carstens3cdad422009-01-14 14:14:22 +0100432SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
433 qid_t, id, void __user *, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 uint cmds, type;
436 struct super_block *sb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int ret;
438
439 cmds = cmd >> SUBCMDSHIFT;
440 type = cmd & SUBCMDMASK;
441
442 if (cmds != Q_SYNC || special) {
David Howells93614012006-09-30 20:45:40 +0200443 sb = quotactl_block(special);
444 if (IS_ERR(sb))
445 return PTR_ERR(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
448 ret = check_quotactl_valid(sb, type, cmds, id);
449 if (ret >= 0)
450 ret = do_quotactl(sb, type, cmds, id, addr);
451 if (sb)
452 drop_super(sb);
453
454 return ret;
455}
Vasily Tarasovb7163952007-07-15 23:41:12 -0700456
Tony Luck7a6c8132007-07-27 15:35:43 -0700457#if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
Vasily Tarasovb7163952007-07-15 23:41:12 -0700458/*
459 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
460 * and is necessary due to alignment problems.
461 */
462struct compat_if_dqblk {
463 compat_u64 dqb_bhardlimit;
464 compat_u64 dqb_bsoftlimit;
465 compat_u64 dqb_curspace;
466 compat_u64 dqb_ihardlimit;
467 compat_u64 dqb_isoftlimit;
468 compat_u64 dqb_curinodes;
469 compat_u64 dqb_btime;
470 compat_u64 dqb_itime;
471 compat_uint_t dqb_valid;
472};
473
474/* XFS structures */
475struct compat_fs_qfilestat {
476 compat_u64 dqb_bhardlimit;
477 compat_u64 qfs_nblks;
478 compat_uint_t qfs_nextents;
479};
480
481struct compat_fs_quota_stat {
482 __s8 qs_version;
483 __u16 qs_flags;
484 __s8 qs_pad;
485 struct compat_fs_qfilestat qs_uquota;
486 struct compat_fs_qfilestat qs_gquota;
487 compat_uint_t qs_incoredqs;
488 compat_int_t qs_btimelimit;
489 compat_int_t qs_itimelimit;
490 compat_int_t qs_rtbtimelimit;
491 __u16 qs_bwarnlimit;
492 __u16 qs_iwarnlimit;
493};
494
495asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
496 qid_t id, void __user *addr)
497{
498 unsigned int cmds;
499 struct if_dqblk __user *dqblk;
500 struct compat_if_dqblk __user *compat_dqblk;
501 struct fs_quota_stat __user *fsqstat;
502 struct compat_fs_quota_stat __user *compat_fsqstat;
503 compat_uint_t data;
504 u16 xdata;
505 long ret;
506
507 cmds = cmd >> SUBCMDSHIFT;
508
509 switch (cmds) {
510 case Q_GETQUOTA:
511 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
512 compat_dqblk = addr;
513 ret = sys_quotactl(cmd, special, id, dqblk);
514 if (ret)
515 break;
516 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
517 get_user(data, &dqblk->dqb_valid) ||
518 put_user(data, &compat_dqblk->dqb_valid))
519 ret = -EFAULT;
520 break;
521 case Q_SETQUOTA:
522 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
523 compat_dqblk = addr;
524 ret = -EFAULT;
525 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
526 get_user(data, &compat_dqblk->dqb_valid) ||
527 put_user(data, &dqblk->dqb_valid))
528 break;
529 ret = sys_quotactl(cmd, special, id, dqblk);
530 break;
531 case Q_XGETQSTAT:
532 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
533 compat_fsqstat = addr;
534 ret = sys_quotactl(cmd, special, id, fsqstat);
535 if (ret)
536 break;
537 ret = -EFAULT;
538 /* Copying qs_version, qs_flags, qs_pad */
539 if (copy_in_user(compat_fsqstat, fsqstat,
540 offsetof(struct compat_fs_quota_stat, qs_uquota)))
541 break;
542 /* Copying qs_uquota */
543 if (copy_in_user(&compat_fsqstat->qs_uquota,
544 &fsqstat->qs_uquota,
545 sizeof(compat_fsqstat->qs_uquota)) ||
546 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
547 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
548 break;
549 /* Copying qs_gquota */
550 if (copy_in_user(&compat_fsqstat->qs_gquota,
551 &fsqstat->qs_gquota,
552 sizeof(compat_fsqstat->qs_gquota)) ||
553 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
554 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
555 break;
556 /* Copying the rest */
557 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
558 &fsqstat->qs_incoredqs,
559 sizeof(struct compat_fs_quota_stat) -
560 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
561 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
562 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
563 break;
564 ret = 0;
565 break;
566 default:
567 ret = sys_quotactl(cmd, special, id, addr);
568 }
569 return ret;
570}
571#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +0100572
573
574#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
575
576/* Netlink family structure for quota */
577static struct genl_family quota_genl_family = {
578 .id = GENL_ID_GENERATE,
579 .hdrsize = 0,
580 .name = "VFS_DQUOT",
581 .version = 1,
582 .maxattr = QUOTA_NL_A_MAX,
583};
584
585/**
586 * quota_send_warning - Send warning to userspace about exceeded quota
587 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
588 * @id: The user or group id of the quota that was exceeded
589 * @dev: The device on which the fs is mounted (sb->s_dev)
590 * @warntype: The type of the warning: QUOTA_NL_...
591 *
592 * This can be used by filesystems (including those which don't use
593 * dquot) to send a message to userspace relating to quota limits.
594 *
595 */
596
597void quota_send_warning(short type, unsigned int id, dev_t dev,
598 const char warntype)
599{
600 static atomic_t seq;
601 struct sk_buff *skb;
602 void *msg_head;
603 int ret;
604 int msg_size = 4 * nla_total_size(sizeof(u32)) +
605 2 * nla_total_size(sizeof(u64));
606
607 /* We have to allocate using GFP_NOFS as we are called from a
608 * filesystem performing write and thus further recursion into
609 * the fs to free some data could cause deadlocks. */
610 skb = genlmsg_new(msg_size, GFP_NOFS);
611 if (!skb) {
612 printk(KERN_ERR
613 "VFS: Not enough memory to send quota warning.\n");
614 return;
615 }
616 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
617 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
618 if (!msg_head) {
619 printk(KERN_ERR
620 "VFS: Cannot store netlink header in quota warning.\n");
621 goto err_out;
622 }
623 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
624 if (ret)
625 goto attr_err_out;
626 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
627 if (ret)
628 goto attr_err_out;
629 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
630 if (ret)
631 goto attr_err_out;
632 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
633 if (ret)
634 goto attr_err_out;
635 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
636 if (ret)
637 goto attr_err_out;
638 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
639 if (ret)
640 goto attr_err_out;
641 genlmsg_end(skb, msg_head);
642
643 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
644 return;
645attr_err_out:
646 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
647err_out:
648 kfree_skb(skb);
649}
650EXPORT_SYMBOL(quota_send_warning);
651
652static int __init quota_init(void)
653{
654 if (genl_register_family(&quota_genl_family) != 0)
655 printk(KERN_ERR
656 "VFS: Failed to create quota netlink interface.\n");
657 return 0;
658};
659
660module_init(quota_init);
661#endif
662