quota: move code from sync_quota_sb into vfs_quota_sync
[deliverable/linux.git] / fs / quota / quota.c
CommitLineData
1da177e4
LT
1/*
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>
b716395e 13#include <linux/compat.h>
1da177e4 14#include <linux/kernel.h>
1da177e4
LT
15#include <linux/security.h>
16#include <linux/syscalls.h>
17#include <linux/buffer_head.h>
16f7e0fe 18#include <linux/capability.h>
be586bab 19#include <linux/quotaops.h>
b716395e 20#include <linux/types.h>
8c4e4acd 21#include <linux/writeback.h>
86e931a3
SW
22#include <net/netlink.h>
23#include <net/genetlink.h>
1da177e4 24
c988afb5
CH
25static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
26 qid_t id)
1da177e4 27{
c988afb5
CH
28 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:
1da177e4
LT
44 if (!capable(CAP_SYS_ADMIN))
45 return -EPERM;
46 }
47
c988afb5 48 return security_quotactl(cmd, type, id, sb);
1da177e4
LT
49}
50
6ae09575 51static int quota_sync_all(int type)
1da177e4 52{
850b201b 53 struct super_block *sb;
02a55ca8 54 int cnt;
6ae09575
CH
55 int ret;
56
57 if (type >= MAXQUOTAS)
58 return -EINVAL;
59 ret = security_quotactl(Q_SYNC, type, 0, NULL);
60 if (ret)
61 return ret;
618f0636 62
618f0636
KK
63 spin_lock(&sb_lock);
64restart:
65 list_for_each_entry(sb, &super_blocks, s_list) {
5fb324ad
CH
66 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
67 continue;
68
268157ba
JK
69 /* This test just improves performance so it needn't be
70 * reliable... */
02a55ca8
JK
71 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
72 if (type != -1 && type != cnt)
73 continue;
f55abc0f 74 if (!sb_has_quota_active(sb, cnt))
02a55ca8
JK
75 continue;
76 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
268157ba 77 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
02a55ca8
JK
78 continue;
79 break;
80 }
81 if (cnt == MAXQUOTAS)
618f0636
KK
82 continue;
83 sb->s_count++;
84 spin_unlock(&sb_lock);
85 down_read(&sb->s_umount);
850b201b 86 if (sb->s_root)
5fb324ad 87 sb->s_qcop->quota_sync(sb, type, 1);
618f0636
KK
88 up_read(&sb->s_umount);
89 spin_lock(&sb_lock);
90 if (__put_super_and_need_restart(sb))
91 goto restart;
1da177e4 92 }
618f0636 93 spin_unlock(&sb_lock);
6ae09575
CH
94
95 return 0;
1da177e4
LT
96}
97
c411e5f6
CH
98static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
99 void __user *addr)
1da177e4 100{
c411e5f6 101 char *pathname;
f450d4fe 102 int ret = -ENOSYS;
1da177e4 103
c411e5f6
CH
104 pathname = getname(addr);
105 if (IS_ERR(pathname))
106 return PTR_ERR(pathname);
f450d4fe
CH
107 if (sb->s_qcop->quota_on)
108 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
c411e5f6
CH
109 putname(pathname);
110 return ret;
111}
1da177e4 112
c411e5f6
CH
113static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
114{
115 __u32 fmt;
1da177e4 116
c411e5f6
CH
117 down_read(&sb_dqopt(sb)->dqptr_sem);
118 if (!sb_has_quota_active(sb, type)) {
119 up_read(&sb_dqopt(sb)->dqptr_sem);
120 return -ESRCH;
121 }
122 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
123 up_read(&sb_dqopt(sb)->dqptr_sem);
124 if (copy_to_user(addr, &fmt, sizeof(fmt)))
125 return -EFAULT;
126 return 0;
127}
1da177e4 128
c411e5f6
CH
129static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
130{
131 struct if_dqinfo info;
132 int ret;
1da177e4 133
f450d4fe
CH
134 if (!sb_has_quota_active(sb, type))
135 return -ESRCH;
136 if (!sb->s_qcop->get_info)
137 return -ENOSYS;
c411e5f6
CH
138 ret = sb->s_qcop->get_info(sb, type, &info);
139 if (!ret && copy_to_user(addr, &info, sizeof(info)))
140 return -EFAULT;
141 return ret;
142}
1da177e4 143
c411e5f6
CH
144static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
145{
146 struct if_dqinfo info;
1da177e4 147
c411e5f6
CH
148 if (copy_from_user(&info, addr, sizeof(info)))
149 return -EFAULT;
f450d4fe
CH
150 if (!sb_has_quota_active(sb, type))
151 return -ESRCH;
152 if (!sb->s_qcop->set_info)
153 return -ENOSYS;
c411e5f6
CH
154 return sb->s_qcop->set_info(sb, type, &info);
155}
156
157static int quota_getquota(struct super_block *sb, int type, qid_t id,
158 void __user *addr)
159{
160 struct if_dqblk idq;
161 int ret;
162
f450d4fe
CH
163 if (!sb_has_quota_active(sb, type))
164 return -ESRCH;
165 if (!sb->s_qcop->get_dqblk)
166 return -ENOSYS;
c411e5f6
CH
167 ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
168 if (ret)
169 return ret;
170 if (copy_to_user(addr, &idq, sizeof(idq)))
171 return -EFAULT;
172 return 0;
173}
174
175static int quota_setquota(struct super_block *sb, int type, qid_t id,
176 void __user *addr)
177{
178 struct if_dqblk idq;
179
180 if (copy_from_user(&idq, addr, sizeof(idq)))
181 return -EFAULT;
f450d4fe
CH
182 if (!sb_has_quota_active(sb, type))
183 return -ESRCH;
184 if (!sb->s_qcop->set_dqblk)
185 return -ENOSYS;
c411e5f6
CH
186 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
187}
188
189static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
190{
191 __u32 flags;
192
193 if (copy_from_user(&flags, addr, sizeof(flags)))
194 return -EFAULT;
f450d4fe
CH
195 if (!sb->s_qcop->set_xstate)
196 return -ENOSYS;
c411e5f6
CH
197 return sb->s_qcop->set_xstate(sb, flags, cmd);
198}
199
200static int quota_getxstate(struct super_block *sb, void __user *addr)
201{
202 struct fs_quota_stat fqs;
203 int ret;
f450d4fe
CH
204
205 if (!sb->s_qcop->get_xstate)
206 return -ENOSYS;
c411e5f6
CH
207 ret = sb->s_qcop->get_xstate(sb, &fqs);
208 if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
209 return -EFAULT;
210 return ret;
211}
1da177e4 212
c411e5f6
CH
213static int quota_setxquota(struct super_block *sb, int type, qid_t id,
214 void __user *addr)
215{
216 struct fs_disk_quota fdq;
217
218 if (copy_from_user(&fdq, addr, sizeof(fdq)))
219 return -EFAULT;
f450d4fe
CH
220 if (!sb->s_qcop->set_xquota)
221 return -ENOSYS;
c411e5f6
CH
222 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
223}
224
225static int quota_getxquota(struct super_block *sb, int type, qid_t id,
226 void __user *addr)
227{
228 struct fs_disk_quota fdq;
229 int ret;
230
f450d4fe
CH
231 if (!sb->s_qcop->get_xquota)
232 return -ENOSYS;
c411e5f6
CH
233 ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
234 if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
235 return -EFAULT;
236 return ret;
237}
238
239/* Copy parameters and call proper function */
240static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
241 void __user *addr)
242{
c988afb5
CH
243 int ret;
244
245 if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
246 return -EINVAL;
247 if (!sb->s_qcop)
248 return -ENOSYS;
249
250 ret = check_quotactl_permission(sb, type, cmd, id);
251 if (ret < 0)
252 return ret;
253
c411e5f6
CH
254 switch (cmd) {
255 case Q_QUOTAON:
256 return quota_quotaon(sb, type, cmd, id, addr);
257 case Q_QUOTAOFF:
f450d4fe
CH
258 if (!sb->s_qcop->quota_off)
259 return -ENOSYS;
c411e5f6
CH
260 return sb->s_qcop->quota_off(sb, type, 0);
261 case Q_GETFMT:
262 return quota_getfmt(sb, type, addr);
263 case Q_GETINFO:
264 return quota_getinfo(sb, type, addr);
265 case Q_SETINFO:
266 return quota_setinfo(sb, type, addr);
267 case Q_GETQUOTA:
268 return quota_getquota(sb, type, id, addr);
269 case Q_SETQUOTA:
270 return quota_setquota(sb, type, id, addr);
271 case Q_SYNC:
6ae09575
CH
272 if (!sb->s_qcop->quota_sync)
273 return -ENOSYS;
5fb324ad 274 return sb->s_qcop->quota_sync(sb, type, 1);
c411e5f6
CH
275 case Q_XQUOTAON:
276 case Q_XQUOTAOFF:
277 case Q_XQUOTARM:
278 return quota_setxstate(sb, cmd, addr);
279 case Q_XGETQSTAT:
280 return quota_getxstate(sb, addr);
281 case Q_XSETQLIM:
282 return quota_setxquota(sb, type, id, addr);
283 case Q_XGETQUOTA:
284 return quota_getxquota(sb, type, id, addr);
285 case Q_XQUOTASYNC:
8c4e4acd
CH
286 /* caller already holds s_umount */
287 if (sb->s_flags & MS_RDONLY)
288 return -EROFS;
289 writeback_inodes_sb(sb);
290 return 0;
c411e5f6 291 default:
f450d4fe 292 return -EINVAL;
1da177e4 293 }
1da177e4
LT
294}
295
9361401e
DH
296/*
297 * look up a superblock on which quota ops will be performed
298 * - use the name of a block device to find the superblock thereon
299 */
7a2435d8 300static struct super_block *quotactl_block(const char __user *special)
9361401e
DH
301{
302#ifdef CONFIG_BLOCK
303 struct block_device *bdev;
304 struct super_block *sb;
305 char *tmp = getname(special);
306
307 if (IS_ERR(tmp))
e231c2ee 308 return ERR_CAST(tmp);
9361401e
DH
309 bdev = lookup_bdev(tmp);
310 putname(tmp);
311 if (IS_ERR(bdev))
e231c2ee 312 return ERR_CAST(bdev);
9361401e
DH
313 sb = get_super(bdev);
314 bdput(bdev);
315 if (!sb)
316 return ERR_PTR(-ENODEV);
317
318 return sb;
319#else
320 return ERR_PTR(-ENODEV);
321#endif
322}
323
1da177e4
LT
324/*
325 * This is the system call interface. This communicates with
326 * the user-level programs. Currently this only supports diskquota
327 * calls. Maybe we need to add the process quotas etc. in the future,
328 * but we probably should use rlimits for that.
329 */
3cdad428
HC
330SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
331 qid_t, id, void __user *, addr)
1da177e4
LT
332{
333 uint cmds, type;
334 struct super_block *sb = NULL;
1da177e4
LT
335 int ret;
336
337 cmds = cmd >> SUBCMDSHIFT;
338 type = cmd & SUBCMDMASK;
339
6ae09575
CH
340 /*
341 * As a special case Q_SYNC can be called without a specific device.
342 * It will iterate all superblocks that have quota enabled and call
343 * the sync action on each of them.
344 */
345 if (!special) {
346 if (cmds == Q_SYNC)
347 return quota_sync_all(type);
348 return -ENODEV;
1da177e4
LT
349 }
350
6ae09575
CH
351 sb = quotactl_block(special);
352 if (IS_ERR(sb))
353 return PTR_ERR(sb);
354
c988afb5 355 ret = do_quotactl(sb, type, cmds, id, addr);
1da177e4 356
6ae09575 357 drop_super(sb);
1da177e4
LT
358 return ret;
359}
b716395e 360
7a6c8135 361#if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
b716395e
VT
362/*
363 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
364 * and is necessary due to alignment problems.
365 */
366struct compat_if_dqblk {
367 compat_u64 dqb_bhardlimit;
368 compat_u64 dqb_bsoftlimit;
369 compat_u64 dqb_curspace;
370 compat_u64 dqb_ihardlimit;
371 compat_u64 dqb_isoftlimit;
372 compat_u64 dqb_curinodes;
373 compat_u64 dqb_btime;
374 compat_u64 dqb_itime;
375 compat_uint_t dqb_valid;
376};
377
378/* XFS structures */
379struct compat_fs_qfilestat {
380 compat_u64 dqb_bhardlimit;
381 compat_u64 qfs_nblks;
382 compat_uint_t qfs_nextents;
383};
384
385struct compat_fs_quota_stat {
386 __s8 qs_version;
387 __u16 qs_flags;
388 __s8 qs_pad;
389 struct compat_fs_qfilestat qs_uquota;
390 struct compat_fs_qfilestat qs_gquota;
391 compat_uint_t qs_incoredqs;
392 compat_int_t qs_btimelimit;
393 compat_int_t qs_itimelimit;
394 compat_int_t qs_rtbtimelimit;
395 __u16 qs_bwarnlimit;
396 __u16 qs_iwarnlimit;
397};
398
399asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
400 qid_t id, void __user *addr)
401{
402 unsigned int cmds;
403 struct if_dqblk __user *dqblk;
404 struct compat_if_dqblk __user *compat_dqblk;
405 struct fs_quota_stat __user *fsqstat;
406 struct compat_fs_quota_stat __user *compat_fsqstat;
407 compat_uint_t data;
408 u16 xdata;
409 long ret;
410
411 cmds = cmd >> SUBCMDSHIFT;
412
413 switch (cmds) {
414 case Q_GETQUOTA:
415 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
416 compat_dqblk = addr;
417 ret = sys_quotactl(cmd, special, id, dqblk);
418 if (ret)
419 break;
420 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
421 get_user(data, &dqblk->dqb_valid) ||
422 put_user(data, &compat_dqblk->dqb_valid))
423 ret = -EFAULT;
424 break;
425 case Q_SETQUOTA:
426 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
427 compat_dqblk = addr;
428 ret = -EFAULT;
429 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
430 get_user(data, &compat_dqblk->dqb_valid) ||
431 put_user(data, &dqblk->dqb_valid))
432 break;
433 ret = sys_quotactl(cmd, special, id, dqblk);
434 break;
435 case Q_XGETQSTAT:
436 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
437 compat_fsqstat = addr;
438 ret = sys_quotactl(cmd, special, id, fsqstat);
439 if (ret)
440 break;
441 ret = -EFAULT;
442 /* Copying qs_version, qs_flags, qs_pad */
443 if (copy_in_user(compat_fsqstat, fsqstat,
444 offsetof(struct compat_fs_quota_stat, qs_uquota)))
445 break;
446 /* Copying qs_uquota */
447 if (copy_in_user(&compat_fsqstat->qs_uquota,
448 &fsqstat->qs_uquota,
449 sizeof(compat_fsqstat->qs_uquota)) ||
450 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
451 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
452 break;
453 /* Copying qs_gquota */
454 if (copy_in_user(&compat_fsqstat->qs_gquota,
455 &fsqstat->qs_gquota,
456 sizeof(compat_fsqstat->qs_gquota)) ||
457 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
458 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
459 break;
460 /* Copying the rest */
461 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
462 &fsqstat->qs_incoredqs,
463 sizeof(struct compat_fs_quota_stat) -
464 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
465 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
466 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
467 break;
468 ret = 0;
469 break;
470 default:
471 ret = sys_quotactl(cmd, special, id, addr);
472 }
473 return ret;
474}
475#endif
86e931a3
SW
476
477
478#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
479
480/* Netlink family structure for quota */
481static struct genl_family quota_genl_family = {
482 .id = GENL_ID_GENERATE,
483 .hdrsize = 0,
484 .name = "VFS_DQUOT",
485 .version = 1,
486 .maxattr = QUOTA_NL_A_MAX,
487};
488
489/**
490 * quota_send_warning - Send warning to userspace about exceeded quota
491 * @type: The quota type: USRQQUOTA, GRPQUOTA,...
492 * @id: The user or group id of the quota that was exceeded
493 * @dev: The device on which the fs is mounted (sb->s_dev)
494 * @warntype: The type of the warning: QUOTA_NL_...
495 *
496 * This can be used by filesystems (including those which don't use
497 * dquot) to send a message to userspace relating to quota limits.
498 *
499 */
500
501void quota_send_warning(short type, unsigned int id, dev_t dev,
502 const char warntype)
503{
504 static atomic_t seq;
505 struct sk_buff *skb;
506 void *msg_head;
507 int ret;
508 int msg_size = 4 * nla_total_size(sizeof(u32)) +
509 2 * nla_total_size(sizeof(u64));
510
511 /* We have to allocate using GFP_NOFS as we are called from a
512 * filesystem performing write and thus further recursion into
513 * the fs to free some data could cause deadlocks. */
514 skb = genlmsg_new(msg_size, GFP_NOFS);
515 if (!skb) {
516 printk(KERN_ERR
517 "VFS: Not enough memory to send quota warning.\n");
518 return;
519 }
520 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
521 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
522 if (!msg_head) {
523 printk(KERN_ERR
524 "VFS: Cannot store netlink header in quota warning.\n");
525 goto err_out;
526 }
527 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, type);
528 if (ret)
529 goto attr_err_out;
530 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, id);
531 if (ret)
532 goto attr_err_out;
533 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
534 if (ret)
535 goto attr_err_out;
536 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
537 if (ret)
538 goto attr_err_out;
539 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
540 if (ret)
541 goto attr_err_out;
542 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current_uid());
543 if (ret)
544 goto attr_err_out;
545 genlmsg_end(skb, msg_head);
546
547 genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
548 return;
549attr_err_out:
550 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
551err_out:
552 kfree_skb(skb);
553}
554EXPORT_SYMBOL(quota_send_warning);
555
556static int __init quota_init(void)
557{
558 if (genl_register_family(&quota_genl_family) != 0)
559 printk(KERN_ERR
560 "VFS: Failed to create quota netlink interface.\n");
561 return 0;
562};
563
564module_init(quota_init);
565#endif
566
This page took 0.471003 seconds and 5 git commands to generate.