quota: simplify permission checking
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
86e931a3
SW
21#include <net/netlink.h>
22#include <net/genetlink.h>
1da177e4 23
c988afb5
CH
24static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
25 qid_t id)
1da177e4 26{
c988afb5
CH
27 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:
1da177e4
LT
43 if (!capable(CAP_SYS_ADMIN))
44 return -EPERM;
45 }
46
c988afb5 47 return security_quotactl(cmd, type, id, sb);
1da177e4
LT
48}
49
850b201b
CH
50#ifdef CONFIG_QUOTA
51void sync_quota_sb(struct super_block *sb, int type)
1da177e4
LT
52{
53 int cnt;
1da177e4 54
850b201b
CH
55 if (!sb->s_qcop->quota_sync)
56 return;
57
1da177e4 58 sb->s_qcop->quota_sync(sb, type);
ca785ec6
JK
59
60 if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
61 return;
1da177e4
LT
62 /* 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
7925409e
JK
69 /*
70 * Now when everything is written we can discard the pagecache so
71 * that userspace sees the changes.
72 */
d3be915f 73 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4 74 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1da177e4
LT
75 if (type != -1 && cnt != type)
76 continue;
f55abc0f 77 if (!sb_has_quota_active(sb, cnt))
1da177e4 78 continue;
268157ba
JK
79 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
80 I_MUTEX_QUOTA);
7925409e
JK
81 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
82 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
1da177e4 83 }
d3be915f 84 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1da177e4 85}
850b201b 86#endif
1da177e4 87
6ae09575 88static int quota_sync_all(int type)
1da177e4 89{
850b201b 90 struct super_block *sb;
02a55ca8 91 int cnt;
6ae09575
CH
92 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;
618f0636 99
618f0636
KK
100 spin_lock(&sb_lock);
101restart:
102 list_for_each_entry(sb, &super_blocks, s_list) {
268157ba
JK
103 /* This test just improves performance so it needn't be
104 * reliable... */
02a55ca8
JK
105 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
106 if (type != -1 && type != cnt)
107 continue;
f55abc0f 108 if (!sb_has_quota_active(sb, cnt))
02a55ca8
JK
109 continue;
110 if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
268157ba 111 list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
02a55ca8
JK
112 continue;
113 break;
114 }
115 if (cnt == MAXQUOTAS)
618f0636
KK
116 continue;
117 sb->s_count++;
118 spin_unlock(&sb_lock);
119 down_read(&sb->s_umount);
850b201b
CH
120 if (sb->s_root)
121 sync_quota_sb(sb, type);
618f0636
KK
122 up_read(&sb->s_umount);
123 spin_lock(&sb_lock);
124 if (__put_super_and_need_restart(sb))
125 goto restart;
1da177e4 126 }
618f0636 127 spin_unlock(&sb_lock);
6ae09575
CH
128
129 return 0;
1da177e4
LT
130}
131
c411e5f6
CH
132static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
133 void __user *addr)
1da177e4 134{
c411e5f6 135 char *pathname;
f450d4fe 136 int ret = -ENOSYS;
1da177e4 137
c411e5f6
CH
138 pathname = getname(addr);
139 if (IS_ERR(pathname))
140 return PTR_ERR(pathname);
f450d4fe
CH
141 if (sb->s_qcop->quota_on)
142 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
c411e5f6
CH
143 putname(pathname);
144 return ret;
145}
1da177e4 146
c411e5f6
CH
147static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
148{
149 __u32 fmt;
1da177e4 150
c411e5f6
CH
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}
1da177e4 162
c411e5f6
CH
163static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
164{
165 struct if_dqinfo info;
166 int ret;
1da177e4 167
f450d4fe
CH
168 if (!sb_has_quota_active(sb, type))
169 return -ESRCH;
170 if (!sb->s_qcop->get_info)
171 return -ENOSYS;
c411e5f6
CH
172 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}
1da177e4 177
c411e5f6
CH
178static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
179{
180 struct if_dqinfo info;
1da177e4 181
c411e5f6
CH
182 if (copy_from_user(&info, addr, sizeof(info)))
183 return -EFAULT;
f450d4fe
CH
184 if (!sb_has_quota_active(sb, type))
185 return -ESRCH;
186 if (!sb->s_qcop->set_info)
187 return -ENOSYS;
c411e5f6
CH
188 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
f450d4fe
CH
197 if (!sb_has_quota_active(sb, type))
198 return -ESRCH;
199 if (!sb->s_qcop->get_dqblk)
200 return -ENOSYS;
c411e5f6
CH
201 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;
f450d4fe
CH
216 if (!sb_has_quota_active(sb, type))
217 return -ESRCH;
218 if (!sb->s_qcop->set_dqblk)
219 return -ENOSYS;
c411e5f6
CH
220 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;
f450d4fe
CH
229 if (!sb->s_qcop->set_xstate)
230 return -ENOSYS;
c411e5f6
CH
231 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;
f450d4fe
CH
238
239 if (!sb->s_qcop->get_xstate)
240 return -ENOSYS;
c411e5f6
CH
241 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}
1da177e4 246
c411e5f6
CH
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;
f450d4fe
CH
254 if (!sb->s_qcop->set_xquota)
255 return -ENOSYS;
c411e5f6
CH
256 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
f450d4fe
CH
265 if (!sb->s_qcop->get_xquota)
266 return -ENOSYS;
c411e5f6
CH
267 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
273/* Copy parameters and call proper function */
274static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
275 void __user *addr)
276{
c988afb5
CH
277 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
c411e5f6
CH
288 switch (cmd) {
289 case Q_QUOTAON:
290 return quota_quotaon(sb, type, cmd, id, addr);
291 case Q_QUOTAOFF:
f450d4fe
CH
292 if (!sb->s_qcop->quota_off)
293 return -ENOSYS;
c411e5f6
CH
294 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:
6ae09575
CH
306 if (!sb->s_qcop->quota_sync)
307 return -ENOSYS;
308 sync_quota_sb(sb, type);
c411e5f6
CH
309 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:
f450d4fe
CH
321 if (!sb->s_qcop->quota_sync)
322 return -ENOSYS;
c411e5f6 323 return sb->s_qcop->quota_sync(sb, type);
c411e5f6 324 default:
f450d4fe 325 return -EINVAL;
1da177e4 326 }
1da177e4
LT
327}
328
9361401e
DH
329/*
330 * 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 */
7a2435d8 333static struct super_block *quotactl_block(const char __user *special)
9361401e
DH
334{
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))
e231c2ee 341 return ERR_CAST(tmp);
9361401e
DH
342 bdev = lookup_bdev(tmp);
343 putname(tmp);
344 if (IS_ERR(bdev))
e231c2ee 345 return ERR_CAST(bdev);
9361401e
DH
346 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
1da177e4
LT
357/*
358 * 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 */
3cdad428
HC
363SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
364 qid_t, id, void __user *, addr)
1da177e4
LT
365{
366 uint cmds, type;
367 struct super_block *sb = NULL;
1da177e4
LT
368 int ret;
369
370 cmds = cmd >> SUBCMDSHIFT;
371 type = cmd & SUBCMDMASK;
372
6ae09575
CH
373 /*
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;
1da177e4
LT
382 }
383
6ae09575
CH
384 sb = quotactl_block(special);
385 if (IS_ERR(sb))
386 return PTR_ERR(sb);
387
c988afb5 388 ret = do_quotactl(sb, type, cmds, id, addr);
1da177e4 389
6ae09575 390 drop_super(sb);
1da177e4
LT
391 return ret;
392}
b716395e 393
7a6c8135 394#if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
b716395e
VT
395/*
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
86e931a3
SW
509
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