blob: 3d31228082ea88ed8086e804430aa14d41c4583a [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>
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -050021#include <linux/writeback.h>
Steven Whitehouse86e931a2009-09-28 12:35:17 +010022#include <net/netlink.h>
23#include <net/genetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Christoph Hellwigc988afb2010-02-16 03:44:50 -050025static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
26 qid_t id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Christoph Hellwigc988afb2010-02-16 03:44:50 -050028 switch (cmd) {
29 /* these commands do not require any special privilegues */
30 case Q_GETFMT:
31 case Q_SYNC:
32 case Q_GETINFO:
33 case Q_XGETQSTAT:
34 case Q_XQUOTASYNC:
35 break;
36 /* allow to query information for dquots we "own" */
37 case Q_GETQUOTA:
38 case Q_XGETQUOTA:
39 if ((type == USRQUOTA && current_euid() == id) ||
40 (type == GRPQUOTA && in_egroup_p(id)))
41 break;
42 /*FALLTHROUGH*/
43 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (!capable(CAP_SYS_ADMIN))
45 return -EPERM;
46 }
47
Christoph Hellwigc988afb2010-02-16 03:44:50 -050048 return security_quotactl(cmd, type, id, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Christoph Hellwig850b2012009-04-27 16:43:54 +020051#ifdef CONFIG_QUOTA
52void sync_quota_sb(struct super_block *sb, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 int cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -050056 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
Christoph Hellwig850b2012009-04-27 16:43:54 +020057 return;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 sb->s_qcop->quota_sync(sb, type);
Jan Karaca785ec2008-09-30 17:53:37 +020060
61 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
62 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 /* This is not very clever (and fast) but currently I don't know about
64 * any other simple way of getting quota data to disk and we must get
65 * them there for userspace to be visible... */
66 if (sb->s_op->sync_fs)
67 sb->s_op->sync_fs(sb, 1);
68 sync_blockdev(sb->s_bdev);
69
Jan Kara79254092007-05-16 22:11:19 -070070 /*
71 * Now when everything is written we can discard the pagecache so
72 * that userspace sees the changes.
73 */
Ingo Molnard3be9152006-03-23 03:00:29 -080074 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (type != -1 && cnt != type)
77 continue;
Jan Karaf55abc02008-08-20 17:50:32 +020078 if (!sb_has_quota_active(sb, cnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 continue;
Jan Kara268157b2009-01-27 15:47:22 +010080 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
81 I_MUTEX_QUOTA);
Jan Kara79254092007-05-16 22:11:19 -070082 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
83 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Ingo Molnard3be9152006-03-23 03:00:29 -080085 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
Christoph Hellwig850b2012009-04-27 16:43:54 +020087#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Christoph Hellwig6ae09572010-02-16 03:44:49 -050089static int quota_sync_all(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Christoph Hellwig850b2012009-04-27 16:43:54 +020091 struct super_block *sb;
Jan Kara02a55ca2008-07-25 01:46:50 -070092 int cnt;
Christoph Hellwig6ae09572010-02-16 03:44:49 -050093 int ret;
94
95 if (type >= MAXQUOTAS)
96 return -EINVAL;
97 ret = security_quotactl(Q_SYNC, type, 0, NULL);
98 if (ret)
99 return ret;
Kirill Korotaev618f0632005-06-23 00:09:54 -0700100
Kirill Korotaev618f0632005-06-23 00:09:54 -0700101 spin_lock(&sb_lock);
102restart:
103 list_for_each_entry(sb, &super_blocks, s_list) {
Jan Kara268157b2009-01-27 15:47:22 +0100104 /* This test just improves performance so it needn't be
105 * reliable... */
Jan Kara02a55ca2008-07-25 01:46:50 -0700106 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
107 if (type != -1 && type != cnt)
108 continue;
Jan Karaf55abc02008-08-20 17:50:32 +0200109 if (!sb_has_quota_active(sb, cnt))
Jan Kara02a55ca2008-07-25 01:46:50 -0700110 continue;
111 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
Jan Kara268157b2009-01-27 15:47:22 +0100112 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
Jan Kara02a55ca2008-07-25 01:46:50 -0700113 continue;
114 break;
115 }
116 if (cnt == MAXQUOTAS)
Kirill Korotaev618f0632005-06-23 00:09:54 -0700117 continue;
118 sb->s_count++;
119 spin_unlock(&sb_lock);
120 down_read(&sb->s_umount);
Christoph Hellwig850b2012009-04-27 16:43:54 +0200121 if (sb->s_root)
122 sync_quota_sb(sb, type);
Kirill Korotaev618f0632005-06-23 00:09:54 -0700123 up_read(&sb->s_umount);
124 spin_lock(&sb_lock);
125 if (__put_super_and_need_restart(sb))
126 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
Kirill Korotaev618f0632005-06-23 00:09:54 -0700128 spin_unlock(&sb_lock);
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500129
130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500133static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
134 void __user *addr)
135{
136 char *pathname;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500137 int ret = -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500138
139 pathname = getname(addr);
140 if (IS_ERR(pathname))
141 return PTR_ERR(pathname);
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500142 if (sb->s_qcop->quota_on)
143 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500144 putname(pathname);
145 return ret;
146}
147
148static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
149{
150 __u32 fmt;
151
152 down_read(&sb_dqopt(sb)->dqptr_sem);
153 if (!sb_has_quota_active(sb, type)) {
154 up_read(&sb_dqopt(sb)->dqptr_sem);
155 return -ESRCH;
156 }
157 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
158 up_read(&sb_dqopt(sb)->dqptr_sem);
159 if (copy_to_user(addr, &fmt, sizeof(fmt)))
160 return -EFAULT;
161 return 0;
162}
163
164static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
165{
166 struct if_dqinfo info;
167 int ret;
168
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500169 if (!sb_has_quota_active(sb, type))
170 return -ESRCH;
171 if (!sb->s_qcop->get_info)
172 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500173 ret = sb->s_qcop->get_info(sb, type, &info);
174 if (!ret && copy_to_user(addr, &info, sizeof(info)))
175 return -EFAULT;
176 return ret;
177}
178
179static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
180{
181 struct if_dqinfo info;
182
183 if (copy_from_user(&info, addr, sizeof(info)))
184 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500185 if (!sb_has_quota_active(sb, type))
186 return -ESRCH;
187 if (!sb->s_qcop->set_info)
188 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500189 return sb->s_qcop->set_info(sb, type, &info);
190}
191
192static int quota_getquota(struct super_block *sb, int type, qid_t id,
193 void __user *addr)
194{
195 struct if_dqblk idq;
196 int ret;
197
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500198 if (!sb_has_quota_active(sb, type))
199 return -ESRCH;
200 if (!sb->s_qcop->get_dqblk)
201 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500202 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
203 if (ret)
204 return ret;
205 if (copy_to_user(addr, &idq, sizeof(idq)))
206 return -EFAULT;
207 return 0;
208}
209
210static int quota_setquota(struct super_block *sb, int type, qid_t id,
211 void __user *addr)
212{
213 struct if_dqblk idq;
214
215 if (copy_from_user(&idq, addr, sizeof(idq)))
216 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500217 if (!sb_has_quota_active(sb, type))
218 return -ESRCH;
219 if (!sb->s_qcop->set_dqblk)
220 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500221 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
222}
223
224static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
225{
226 __u32 flags;
227
228 if (copy_from_user(&flags, addr, sizeof(flags)))
229 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500230 if (!sb->s_qcop->set_xstate)
231 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500232 return sb->s_qcop->set_xstate(sb, flags, cmd);
233}
234
235static int quota_getxstate(struct super_block *sb, void __user *addr)
236{
237 struct fs_quota_stat fqs;
238 int ret;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500239
240 if (!sb->s_qcop->get_xstate)
241 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500242 ret = sb->s_qcop->get_xstate(sb, &fqs);
243 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
244 return -EFAULT;
245 return ret;
246}
247
248static int quota_setxquota(struct super_block *sb, int type, qid_t id,
249 void __user *addr)
250{
251 struct fs_disk_quota fdq;
252
253 if (copy_from_user(&fdq, addr, sizeof(fdq)))
254 return -EFAULT;
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500255 if (!sb->s_qcop->set_xquota)
256 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500257 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
258}
259
260static int quota_getxquota(struct super_block *sb, int type, qid_t id,
261 void __user *addr)
262{
263 struct fs_disk_quota fdq;
264 int ret;
265
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500266 if (!sb->s_qcop->get_xquota)
267 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500268 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
269 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
270 return -EFAULT;
271 return ret;
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/* Copy parameters and call proper function */
Jan Kara268157b2009-01-27 15:47:22 +0100275static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
276 void __user *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500278 int ret;
279
280 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
281 return -EINVAL;
282 if (!sb->s_qcop)
283 return -ENOSYS;
284
285 ret = check_quotactl_permission(sb, type, cmd, id);
286 if (ret < 0)
287 return ret;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 switch (cmd) {
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500290 case Q_QUOTAON:
291 return quota_quotaon(sb, type, cmd, id, addr);
292 case Q_QUOTAOFF:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500293 if (!sb->s_qcop->quota_off)
294 return -ENOSYS;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500295 return sb->s_qcop->quota_off(sb, type, 0);
296 case Q_GETFMT:
297 return quota_getfmt(sb, type, addr);
298 case Q_GETINFO:
299 return quota_getinfo(sb, type, addr);
300 case Q_SETINFO:
301 return quota_setinfo(sb, type, addr);
302 case Q_GETQUOTA:
303 return quota_getquota(sb, type, id, addr);
304 case Q_SETQUOTA:
305 return quota_setquota(sb, type, id, addr);
306 case Q_SYNC:
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500307 if (!sb->s_qcop->quota_sync)
308 return -ENOSYS;
309 sync_quota_sb(sb, type);
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500310 return 0;
311 case Q_XQUOTAON:
312 case Q_XQUOTAOFF:
313 case Q_XQUOTARM:
314 return quota_setxstate(sb, cmd, addr);
315 case Q_XGETQSTAT:
316 return quota_getxstate(sb, addr);
317 case Q_XSETQLIM:
318 return quota_setxquota(sb, type, id, addr);
319 case Q_XGETQUOTA:
320 return quota_getxquota(sb, type, id, addr);
321 case Q_XQUOTASYNC:
Christoph Hellwig8c4e4ac2010-02-16 03:44:51 -0500322 /* caller already holds s_umount */
323 if (sb->s_flags & MS_RDONLY)
324 return -EROFS;
325 writeback_inodes_sb(sb);
326 return 0;
Christoph Hellwigc411e5f2010-02-16 03:44:47 -0500327 default:
Christoph Hellwigf450d4f2010-02-16 03:44:48 -0500328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/*
David Howells93614012006-09-30 20:45:40 +0200333 * look up a superblock on which quota ops will be performed
334 * - use the name of a block device to find the superblock thereon
335 */
Jan Kara7a2435d2009-01-27 01:47:11 +0100336static struct super_block *quotactl_block(const char __user *special)
David Howells93614012006-09-30 20:45:40 +0200337{
338#ifdef CONFIG_BLOCK
339 struct block_device *bdev;
340 struct super_block *sb;
341 char *tmp = getname(special);
342
343 if (IS_ERR(tmp))
David Howellse231c2e2008-02-07 00:15:26 -0800344 return ERR_CAST(tmp);
David Howells93614012006-09-30 20:45:40 +0200345 bdev = lookup_bdev(tmp);
346 putname(tmp);
347 if (IS_ERR(bdev))
David Howellse231c2e2008-02-07 00:15:26 -0800348 return ERR_CAST(bdev);
David Howells93614012006-09-30 20:45:40 +0200349 sb = get_super(bdev);
350 bdput(bdev);
351 if (!sb)
352 return ERR_PTR(-ENODEV);
353
354 return sb;
355#else
356 return ERR_PTR(-ENODEV);
357#endif
358}
359
360/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * This is the system call interface. This communicates with
362 * the user-level programs. Currently this only supports diskquota
363 * calls. Maybe we need to add the process quotas etc. in the future,
364 * but we probably should use rlimits for that.
365 */
Heiko Carstens3cdad422009-01-14 14:14:22 +0100366SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
367 qid_t, id, void __user *, addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 uint cmds, type;
370 struct super_block *sb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int ret;
372
373 cmds = cmd >> SUBCMDSHIFT;
374 type = cmd & SUBCMDMASK;
375
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500376 /*
377 * As a special case Q_SYNC can be called without a specific device.
378 * It will iterate all superblocks that have quota enabled and call
379 * the sync action on each of them.
380 */
381 if (!special) {
382 if (cmds == Q_SYNC)
383 return quota_sync_all(type);
384 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500387 sb = quotactl_block(special);
388 if (IS_ERR(sb))
389 return PTR_ERR(sb);
390
Christoph Hellwigc988afb2010-02-16 03:44:50 -0500391 ret = do_quotactl(sb, type, cmds, id, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Christoph Hellwig6ae09572010-02-16 03:44:49 -0500393 drop_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return ret;
395}
Vasily Tarasovb7163952007-07-15 23:41:12 -0700396
Tony Luck7a6c8132007-07-27 15:35:43 -0700397#if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
Vasily Tarasovb7163952007-07-15 23:41:12 -0700398/*
399 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
400 * and is necessary due to alignment problems.
401 */
402struct compat_if_dqblk {
403 compat_u64 dqb_bhardlimit;
404 compat_u64 dqb_bsoftlimit;
405 compat_u64 dqb_curspace;
406 compat_u64 dqb_ihardlimit;
407 compat_u64 dqb_isoftlimit;
408 compat_u64 dqb_curinodes;
409 compat_u64 dqb_btime;
410 compat_u64 dqb_itime;
411 compat_uint_t dqb_valid;
412};
413
414/* XFS structures */
415struct compat_fs_qfilestat {
416 compat_u64 dqb_bhardlimit;
417 compat_u64 qfs_nblks;
418 compat_uint_t qfs_nextents;
419};
420
421struct compat_fs_quota_stat {
422 __s8 qs_version;
423 __u16 qs_flags;
424 __s8 qs_pad;
425 struct compat_fs_qfilestat qs_uquota;
426 struct compat_fs_qfilestat qs_gquota;
427 compat_uint_t qs_incoredqs;
428 compat_int_t qs_btimelimit;
429 compat_int_t qs_itimelimit;
430 compat_int_t qs_rtbtimelimit;
431 __u16 qs_bwarnlimit;
432 __u16 qs_iwarnlimit;
433};
434
435asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
436 qid_t id, void __user *addr)
437{
438 unsigned int cmds;
439 struct if_dqblk __user *dqblk;
440 struct compat_if_dqblk __user *compat_dqblk;
441 struct fs_quota_stat __user *fsqstat;
442 struct compat_fs_quota_stat __user *compat_fsqstat;
443 compat_uint_t data;
444 u16 xdata;
445 long ret;
446
447 cmds = cmd >> SUBCMDSHIFT;
448
449 switch (cmds) {
450 case Q_GETQUOTA:
451 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
452 compat_dqblk = addr;
453 ret = sys_quotactl(cmd, special, id, dqblk);
454 if (ret)
455 break;
456 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
457 get_user(data, &dqblk->dqb_valid) ||
458 put_user(data, &compat_dqblk->dqb_valid))
459 ret = -EFAULT;
460 break;
461 case Q_SETQUOTA:
462 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
463 compat_dqblk = addr;
464 ret = -EFAULT;
465 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
466 get_user(data, &compat_dqblk->dqb_valid) ||
467 put_user(data, &dqblk->dqb_valid))
468 break;
469 ret = sys_quotactl(cmd, special, id, dqblk);
470 break;
471 case Q_XGETQSTAT:
472 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
473 compat_fsqstat = addr;
474 ret = sys_quotactl(cmd, special, id, fsqstat);
475 if (ret)
476 break;
477 ret = -EFAULT;
478 /* Copying qs_version, qs_flags, qs_pad */
479 if (copy_in_user(compat_fsqstat, fsqstat,
480 offsetof(struct compat_fs_quota_stat, qs_uquota)))
481 break;
482 /* Copying qs_uquota */
483 if (copy_in_user(&compat_fsqstat->qs_uquota,
484 &fsqstat->qs_uquota,
485 sizeof(compat_fsqstat->qs_uquota)) ||
486 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
487 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
488 break;
489 /* Copying qs_gquota */
490 if (copy_in_user(&compat_fsqstat->qs_gquota,
491 &fsqstat->qs_gquota,
492 sizeof(compat_fsqstat->qs_gquota)) ||
493 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
494 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
495 break;
496 /* Copying the rest */
497 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
498 &fsqstat->qs_incoredqs,
499 sizeof(struct compat_fs_quota_stat) -
500 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
501 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
502 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
503 break;
504 ret = 0;
505 break;
506 default:
507 ret = sys_quotactl(cmd, special, id, addr);
508 }
509 return ret;
510}
511#endif
Steven Whitehouse86e931a2009-09-28 12:35:17 +0100512
513
514#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
515
516/* Netlink family structure for quota */
517static struct genl_family quota_genl_family = {
518 .id = GENL_ID_GENERATE,
519 .hdrsize = 0,
520 .name = "VFS_DQUOT",
521 .version = 1,
522 .maxattr = QUOTA_NL_A_MAX,
523};
524
525/**
526 * quota_send_warning - Send warning to userspace about exceeded quota
527 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
528 * @id: The user or group id of the quota that was exceeded
529 * @dev: The device on which the fs is mounted (sb->s_dev)
530 * @warntype: The type of the warning: QUOTA_NL_...
531 *
532 * This can be used by filesystems (including those which don't use
533 * dquot) to send a message to userspace relating to quota limits.
534 *
535 */
536
537void quota_send_warning(short type, unsigned int id, dev_t dev,
538 const char warntype)
539{
540 static atomic_t seq;
541 struct sk_buff *skb;
542 void *msg_head;
543 int ret;
544 int msg_size = 4 * nla_total_size(sizeof(u32)) +
545 2 * nla_total_size(sizeof(u64));
546
547 /* We have to allocate using GFP_NOFS as we are called from a
548 * filesystem performing write and thus further recursion into
549 * the fs to free some data could cause deadlocks. */
550 skb = genlmsg_new(msg_size, GFP_NOFS);
551 if (!skb) {
552 printk(KERN_ERR
553 "VFS: Not enough memory to send quota warning.\n");
554 return;
555 }
556 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
557 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
558 if (!msg_head) {
559 printk(KERN_ERR
560 "VFS: Cannot store netlink header in quota warning.\n");
561 goto err_out;
562 }
563 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
564 if (ret)
565 goto attr_err_out;
566 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
567 if (ret)
568 goto attr_err_out;
569 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
570 if (ret)
571 goto attr_err_out;
572 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
573 if (ret)
574 goto attr_err_out;
575 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
576 if (ret)
577 goto attr_err_out;
578 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
579 if (ret)
580 goto attr_err_out;
581 genlmsg_end(skb, msg_head);
582
583 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
584 return;
585attr_err_out:
586 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
587err_out:
588 kfree_skb(skb);
589}
590EXPORT_SYMBOL(quota_send_warning);
591
592static int __init quota_init(void)
593{
594 if (genl_register_family(&quota_genl_family) != 0)
595 printk(KERN_ERR
596 "VFS: Failed to create quota netlink interface.\n");
597 return 0;
598};
599
600module_init(quota_init);
601#endif
602