blob: d0efe302b1c16f035652252a3c123cab9e56e9ed [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
Christoph Hellwigc988afb2010-02-16 03:44:50 -050024static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
25 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Christoph Hellwigc988afb2010-02-16 03:44:50 -050027 switch (cmd) {
28 /* these commands do not require any special privilegues */
29 case Q_GETFMT:
30 case Q_SYNC:
31 case Q_GETINFO:
32 case Q_XGETQSTAT:
33 case Q_XQUOTASYNC:
34 break;
35 /* allow to query information for dquots we "own" */
36 case Q_GETQUOTA:
37 case Q_XGETQUOTA:
38 if ((type == USRQUOTA && current_euid() == id) ||
39 (type == GRPQUOTA && in_egroup_p(id)))
40 break;
41 /*FALLTHROUGH*/
42 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (!capable(CAP_SYS_ADMIN))
44 return -EPERM;
45 }
46
Christoph Hellwigc988afb2010-02-16 03:44:50 -050047 return security_quotactl(cmd, type, id, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Christoph Hellwig850b2012009-04-27 16:43:54 +020050#ifdef CONFIG_QUOTA
51void sync_quota_sb(struct super_block *sb, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Christoph Hellwig850b2012009-04-27 16:43:54 +020055 if (!sb->s_qcop->quota_sync)
56 return;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 sb->s_qcop->quota_sync(sb, type);
Jan Karaca785ec2008-09-30 17:53:37 +020059
60 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
61 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* This is not very clever (and fast) but currently I don't know about
63 * any other simple way of getting quota data to disk and we must get
64 * them there for userspace to be visible... */
65 if (sb->s_op->sync_fs)
66 sb->s_op->sync_fs(sb, 1);
67 sync_blockdev(sb->s_bdev);
68
Jan Kara79254092007-05-16 22:11:19 -070069 /*
70 * Now when everything is written we can discard the pagecache so
71 * that userspace sees the changes.
72 */
Ingo Molnard3be9152006-03-23 03:00:29 -080073 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (type != -1 && cnt != type)
76 continue;
Jan Karaf55abc02008-08-20 17:50:32 +020077 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 continue;
Jan Kara268157b2009-01-27 15:47:22 +010079 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
80 I_MUTEX_QUOTA);
Jan Kara79254092007-05-16 22:11:19 -070081 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
82 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
Ingo Molnard3be9152006-03-23 03:00:29 -080084 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
Christoph Hellwig850b2012009-04-27 16:43:54 +020086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Christoph Hellwig6ae09572010-02-16 03:44:49 -050088static int quota_sync_all(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Christoph Hellwig850b2012009-04-27 16:43:54 +020090 struct super_block *sb;
Jan Kara02a55ca2008-07-25 01:46:50 -070091 int cnt;
Christoph Hellwig6ae09572010-02-16 03:44:49 -050092 int ret;
93
94 if (type >= MAXQUOTAS)
95 return -EINVAL;
96 ret = security_quotactl(Q_SYNC, type, 0, NULL);
97 if (ret)
98 return ret;
Kirill Korotaev618f0632005-06-23 00:09:54 -070099
Kirill Korotaev618f0632005-06-23 00:09:54 -0700100 spin_lock(&sb_lock);
101restart:
102 list_for_each_entry(sb, &super_blocks, s_list) {
Jan Kara268157b2009-01-27 15:47:22 +0100103 /* This test just improves performance so it needn't be
104 * reliable... */
Jan Kara02a55ca2008-07-25 01:46:50 -0700105 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
106 if (type != -1 && type != cnt)
107 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200108 if (!sb_has_quota_active(sb, cnt))
Jan Kara02a55ca2008-07-25 01:46:50 -0700109 continue;
110 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
Jan Kara268157b2009-01-27 15:47:22 +0100111 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
Jan Kara02a55ca2008-07-25 01:46:50 -0700112 continue;
113 break;
114 }
115 if (cnt == MAXQUOTAS)
Kirill Korotaev618f0632005-06-23 00:09:54 -0700116 continue;
117 sb->s_count++;
118 spin_unlock(&sb_lock);
119 down_read(&sb->s_umount);
Christoph Hellwig850b2012009-04-27 16:43:54 +0200120 if (sb->s_root)
121 sync_quota_sb(sb, type);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700122 up_read(&sb->s_umount);
123 spin_lock(&sb_lock);
124 if (__put_super_and_need_restart(sb))
125 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
Kirill Korotaev618f0632005-06-23 00:09:54 -0700127 spin_unlock(&sb_lock);
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500128
129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500132static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
133 void __user *addr)
134{
135 char *pathname;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500136 int ret = -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500137
138 pathname = getname(addr);
139 if (IS_ERR(pathname))
140 return PTR_ERR(pathname);
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500141 if (sb->s_qcop->quota_on)
142 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500143 putname(pathname);
144 return ret;
145}
146
147static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
148{
149 __u32 fmt;
150
151 down_read(&sb_dqopt(sb)->dqptr_sem);
152 if (!sb_has_quota_active(sb, type)) {
153 up_read(&sb_dqopt(sb)->dqptr_sem);
154 return -ESRCH;
155 }
156 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
157 up_read(&sb_dqopt(sb)->dqptr_sem);
158 if (copy_to_user(addr, &fmt, sizeof(fmt)))
159 return -EFAULT;
160 return 0;
161}
162
163static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
164{
165 struct if_dqinfo info;
166 int ret;
167
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500168 if (!sb_has_quota_active(sb, type))
169 return -ESRCH;
170 if (!sb->s_qcop->get_info)
171 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500172 ret = sb->s_qcop->get_info(sb, type, &info);
173 if (!ret && copy_to_user(addr, &info, sizeof(info)))
174 return -EFAULT;
175 return ret;
176}
177
178static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
179{
180 struct if_dqinfo info;
181
182 if (copy_from_user(&info, addr, sizeof(info)))
183 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500184 if (!sb_has_quota_active(sb, type))
185 return -ESRCH;
186 if (!sb->s_qcop->set_info)
187 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500188 return sb->s_qcop->set_info(sb, type, &info);
189}
190
191static int quota_getquota(struct super_block *sb, int type, qid_t id,
192 void __user *addr)
193{
194 struct if_dqblk idq;
195 int ret;
196
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500197 if (!sb_has_quota_active(sb, type))
198 return -ESRCH;
199 if (!sb->s_qcop->get_dqblk)
200 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500201 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
202 if (ret)
203 return ret;
204 if (copy_to_user(addr, &idq, sizeof(idq)))
205 return -EFAULT;
206 return 0;
207}
208
209static int quota_setquota(struct super_block *sb, int type, qid_t id,
210 void __user *addr)
211{
212 struct if_dqblk idq;
213
214 if (copy_from_user(&idq, addr, sizeof(idq)))
215 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500216 if (!sb_has_quota_active(sb, type))
217 return -ESRCH;
218 if (!sb->s_qcop->set_dqblk)
219 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500220 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
221}
222
223static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
224{
225 __u32 flags;
226
227 if (copy_from_user(&flags, addr, sizeof(flags)))
228 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500229 if (!sb->s_qcop->set_xstate)
230 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500231 return sb->s_qcop->set_xstate(sb, flags, cmd);
232}
233
234static int quota_getxstate(struct super_block *sb, void __user *addr)
235{
236 struct fs_quota_stat fqs;
237 int ret;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500238
239 if (!sb->s_qcop->get_xstate)
240 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500241 ret = sb->s_qcop->get_xstate(sb, &fqs);
242 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
243 return -EFAULT;
244 return ret;
245}
246
247static int quota_setxquota(struct super_block *sb, int type, qid_t id,
248 void __user *addr)
249{
250 struct fs_disk_quota fdq;
251
252 if (copy_from_user(&fdq, addr, sizeof(fdq)))
253 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500254 if (!sb->s_qcop->set_xquota)
255 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500256 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
257}
258
259static int quota_getxquota(struct super_block *sb, int type, qid_t id,
260 void __user *addr)
261{
262 struct fs_disk_quota fdq;
263 int ret;
264
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500265 if (!sb->s_qcop->get_xquota)
266 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500267 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
268 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
269 return -EFAULT;
270 return ret;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/* Copy parameters and call proper function */
Jan Kara268157b2009-01-27 15:47:22 +0100274static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
275 void __user *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500277 int ret;
278
279 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
280 return -EINVAL;
281 if (!sb->s_qcop)
282 return -ENOSYS;
283
284 ret = check_quotactl_permission(sb, type, cmd, id);
285 if (ret < 0)
286 return ret;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 switch (cmd) {
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500289 case Q_QUOTAON:
290 return quota_quotaon(sb, type, cmd, id, addr);
291 case Q_QUOTAOFF:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500292 if (!sb->s_qcop->quota_off)
293 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500294 return sb->s_qcop->quota_off(sb, type, 0);
295 case Q_GETFMT:
296 return quota_getfmt(sb, type, addr);
297 case Q_GETINFO:
298 return quota_getinfo(sb, type, addr);
299 case Q_SETINFO:
300 return quota_setinfo(sb, type, addr);
301 case Q_GETQUOTA:
302 return quota_getquota(sb, type, id, addr);
303 case Q_SETQUOTA:
304 return quota_setquota(sb, type, id, addr);
305 case Q_SYNC:
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500306 if (!sb->s_qcop->quota_sync)
307 return -ENOSYS;
308 sync_quota_sb(sb, type);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500309 return 0;
310 case Q_XQUOTAON:
311 case Q_XQUOTAOFF:
312 case Q_XQUOTARM:
313 return quota_setxstate(sb, cmd, addr);
314 case Q_XGETQSTAT:
315 return quota_getxstate(sb, addr);
316 case Q_XSETQLIM:
317 return quota_setxquota(sb, type, id, addr);
318 case Q_XGETQUOTA:
319 return quota_getxquota(sb, type, id, addr);
320 case Q_XQUOTASYNC:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500321 if (!sb->s_qcop->quota_sync)
322 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500323 return sb->s_qcop->quota_sync(sb, type);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500324 default:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500325 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329/*
David Howells93614012006-09-30 20:45:40 +0200330 * look up a superblock on which quota ops will be performed
331 * - use the name of a block device to find the superblock thereon
332 */
Jan Kara7a2435d2009-01-27 01:47:11 +0100333static struct super_block *quotactl_block(const char __user *special)
David Howells93614012006-09-30 20:45:40 +0200334{
335#ifdef CONFIG_BLOCK
336 struct block_device *bdev;
337 struct super_block *sb;
338 char *tmp = getname(special);
339
340 if (IS_ERR(tmp))
David Howellse231c2e2008-02-07 00:15:26 -0800341 return ERR_CAST(tmp);
David Howells93614012006-09-30 20:45:40 +0200342 bdev = lookup_bdev(tmp);
343 putname(tmp);
344 if (IS_ERR(bdev))
David Howellse231c2e2008-02-07 00:15:26 -0800345 return ERR_CAST(bdev);
David Howells93614012006-09-30 20:45:40 +0200346 sb = get_super(bdev);
347 bdput(bdev);
348 if (!sb)
349 return ERR_PTR(-ENODEV);
350
351 return sb;
352#else
353 return ERR_PTR(-ENODEV);
354#endif
355}
356
357/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * This is the system call interface. This communicates with
359 * the user-level programs. Currently this only supports diskquota
360 * calls. Maybe we need to add the process quotas etc. in the future,
361 * but we probably should use rlimits for that.
362 */
Heiko Carstens3cdad422009-01-14 14:14:22 +0100363SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
364 qid_t, id, void __user *, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 uint cmds, type;
367 struct super_block *sb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int ret;
369
370 cmds = cmd >> SUBCMDSHIFT;
371 type = cmd & SUBCMDMASK;
372
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500373 /*
374 * As a special case Q_SYNC can be called without a specific device.
375 * It will iterate all superblocks that have quota enabled and call
376 * the sync action on each of them.
377 */
378 if (!special) {
379 if (cmds == Q_SYNC)
380 return quota_sync_all(type);
381 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500384 sb = quotactl_block(special);
385 if (IS_ERR(sb))
386 return PTR_ERR(sb);
387
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500388 ret = do_quotactl(sb, type, cmds, id, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500390 drop_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return ret;
392}
Vasily Tarasovb7163952007-07-15 23:41:12 -0700393
Tony Luck7a6c8132007-07-27 15:35:43 -0700394#if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
Vasily Tarasovb7163952007-07-15 23:41:12 -0700395/*
396 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
397 * and is necessary due to alignment problems.
398 */
399struct compat_if_dqblk {
400 compat_u64 dqb_bhardlimit;
401 compat_u64 dqb_bsoftlimit;
402 compat_u64 dqb_curspace;
403 compat_u64 dqb_ihardlimit;
404 compat_u64 dqb_isoftlimit;
405 compat_u64 dqb_curinodes;
406 compat_u64 dqb_btime;
407 compat_u64 dqb_itime;
408 compat_uint_t dqb_valid;
409};
410
411/* XFS structures */
412struct compat_fs_qfilestat {
413 compat_u64 dqb_bhardlimit;
414 compat_u64 qfs_nblks;
415 compat_uint_t qfs_nextents;
416};
417
418struct compat_fs_quota_stat {
419 __s8 qs_version;
420 __u16 qs_flags;
421 __s8 qs_pad;
422 struct compat_fs_qfilestat qs_uquota;
423 struct compat_fs_qfilestat qs_gquota;
424 compat_uint_t qs_incoredqs;
425 compat_int_t qs_btimelimit;
426 compat_int_t qs_itimelimit;
427 compat_int_t qs_rtbtimelimit;
428 __u16 qs_bwarnlimit;
429 __u16 qs_iwarnlimit;
430};
431
432asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
433 qid_t id, void __user *addr)
434{
435 unsigned int cmds;
436 struct if_dqblk __user *dqblk;
437 struct compat_if_dqblk __user *compat_dqblk;
438 struct fs_quota_stat __user *fsqstat;
439 struct compat_fs_quota_stat __user *compat_fsqstat;
440 compat_uint_t data;
441 u16 xdata;
442 long ret;
443
444 cmds = cmd >> SUBCMDSHIFT;
445
446 switch (cmds) {
447 case Q_GETQUOTA:
448 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
449 compat_dqblk = addr;
450 ret = sys_quotactl(cmd, special, id, dqblk);
451 if (ret)
452 break;
453 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
454 get_user(data, &dqblk->dqb_valid) ||
455 put_user(data, &compat_dqblk->dqb_valid))
456 ret = -EFAULT;
457 break;
458 case Q_SETQUOTA:
459 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
460 compat_dqblk = addr;
461 ret = -EFAULT;
462 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
463 get_user(data, &compat_dqblk->dqb_valid) ||
464 put_user(data, &dqblk->dqb_valid))
465 break;
466 ret = sys_quotactl(cmd, special, id, dqblk);
467 break;
468 case Q_XGETQSTAT:
469 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
470 compat_fsqstat = addr;
471 ret = sys_quotactl(cmd, special, id, fsqstat);
472 if (ret)
473 break;
474 ret = -EFAULT;
475 /* Copying qs_version, qs_flags, qs_pad */
476 if (copy_in_user(compat_fsqstat, fsqstat,
477 offsetof(struct compat_fs_quota_stat, qs_uquota)))
478 break;
479 /* Copying qs_uquota */
480 if (copy_in_user(&compat_fsqstat->qs_uquota,
481 &fsqstat->qs_uquota,
482 sizeof(compat_fsqstat->qs_uquota)) ||
483 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
484 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
485 break;
486 /* Copying qs_gquota */
487 if (copy_in_user(&compat_fsqstat->qs_gquota,
488 &fsqstat->qs_gquota,
489 sizeof(compat_fsqstat->qs_gquota)) ||
490 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
491 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
492 break;
493 /* Copying the rest */
494 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
495 &fsqstat->qs_incoredqs,
496 sizeof(struct compat_fs_quota_stat) -
497 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
498 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
499 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
500 break;
501 ret = 0;
502 break;
503 default:
504 ret = sys_quotactl(cmd, special, id, addr);
505 }
506 return ret;
507}
508#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +0100509
510
511#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
512
513/* Netlink family structure for quota */
514static struct genl_family quota_genl_family = {
515 .id = GENL_ID_GENERATE,
516 .hdrsize = 0,
517 .name = "VFS_DQUOT",
518 .version = 1,
519 .maxattr = QUOTA_NL_A_MAX,
520};
521
522/**
523 * quota_send_warning - Send warning to userspace about exceeded quota
524 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
525 * @id: The user or group id of the quota that was exceeded
526 * @dev: The device on which the fs is mounted (sb->s_dev)
527 * @warntype: The type of the warning: QUOTA_NL_...
528 *
529 * This can be used by filesystems (including those which don't use
530 * dquot) to send a message to userspace relating to quota limits.
531 *
532 */
533
534void quota_send_warning(short type, unsigned int id, dev_t dev,
535 const char warntype)
536{
537 static atomic_t seq;
538 struct sk_buff *skb;
539 void *msg_head;
540 int ret;
541 int msg_size = 4 * nla_total_size(sizeof(u32)) +
542 2 * nla_total_size(sizeof(u64));
543
544 /* We have to allocate using GFP_NOFS as we are called from a
545 * filesystem performing write and thus further recursion into
546 * the fs to free some data could cause deadlocks. */
547 skb = genlmsg_new(msg_size, GFP_NOFS);
548 if (!skb) {
549 printk(KERN_ERR
550 "VFS: Not enough memory to send quota warning.\n");
551 return;
552 }
553 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
554 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
555 if (!msg_head) {
556 printk(KERN_ERR
557 "VFS: Cannot store netlink header in quota warning.\n");
558 goto err_out;
559 }
560 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
561 if (ret)
562 goto attr_err_out;
563 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
564 if (ret)
565 goto attr_err_out;
566 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
567 if (ret)
568 goto attr_err_out;
569 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
570 if (ret)
571 goto attr_err_out;
572 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
573 if (ret)
574 goto attr_err_out;
575 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
576 if (ret)
577 goto attr_err_out;
578 genlmsg_end(skb, msg_head);
579
580 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
581 return;
582attr_err_out:
583 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
584err_out:
585 kfree_skb(skb);
586}
587EXPORT_SYMBOL(quota_send_warning);
588
589static int __init quota_init(void)
590{
591 if (genl_register_family(&quota_genl_family) != 0)
592 printk(KERN_ERR
593 "VFS: Failed to create quota netlink interface.\n");
594 return 0;
595};
596
597module_init(quota_init);
598#endif
599