4d7fdc4443b9cae0966f692aa5304c90d41938dc
[safe/jmp/linux-2.6] / fs / quota / quota.c
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>
13 #include <linux/compat.h>
14 #include <linux/kernel.h>
15 #include <linux/security.h>
16 #include <linux/syscalls.h>
17 #include <linux/buffer_head.h>
18 #include <linux/capability.h>
19 #include <linux/quotaops.h>
20 #include <linux/types.h>
21 #include <net/netlink.h>
22 #include <net/genetlink.h>
23
24 /* Check validity of generic quotactl commands */
25 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd,
26                                   qid_t id)
27 {
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:
75                 case Q_SETINFO:
76                 case Q_SETQUOTA:
77                 case Q_GETQUOTA:
78                         /* This is just an informative test so we are satisfied
79                          * without the lock */
80                         if (!sb_has_quota_active(sb, type))
81                                 return -ESRCH;
82         }
83
84         /* Check privileges */
85         if (cmd == Q_GETQUOTA) {
86                 if (((type == USRQUOTA && current_euid() != id) ||
87                      (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 */
99 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd,
100                               qid_t id)
101 {
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;
128                 case Q_XQUOTASYNC:
129                         if (!sb->s_qcop->quota_sync)
130                                 return -ENOSYS;
131                         break;
132                 default:
133                         return -EINVAL;
134         }
135
136         /* Check privileges */
137         if (cmd == Q_XGETQUOTA) {
138                 if (((type == XQM_USRQUOTA && current_euid() != id) ||
139                      (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
140                      !capable(CAP_SYS_ADMIN))
141                         return -EPERM;
142         } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
143                 if (!capable(CAP_SYS_ADMIN))
144                         return -EPERM;
145         }
146
147         return 0;
148 }
149
150 static int check_quotactl_valid(struct super_block *sb, int type, int cmd,
151                                 qid_t id)
152 {
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
164 #ifdef CONFIG_QUOTA
165 void sync_quota_sb(struct super_block *sb, int type)
166 {
167         int cnt;
168
169         if (!sb->s_qcop->quota_sync)
170                 return;
171
172         sb->s_qcop->quota_sync(sb, type);
173
174         if (sb_dqopt(sb)->flags & DQUOT_QUOTA_SYS_FILE)
175                 return;
176         /* 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
183         /*
184          * Now when everything is written we can discard the pagecache so
185          * that userspace sees the changes.
186          */
187         mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
188         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
189                 if (type != -1 && cnt != type)
190                         continue;
191                 if (!sb_has_quota_active(sb, cnt))
192                         continue;
193                 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex,
194                                   I_MUTEX_QUOTA);
195                 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
196                 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
197         }
198         mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
199 }
200 #endif
201
202 static void sync_dquots(int type)
203 {
204         struct super_block *sb;
205         int cnt;
206
207         spin_lock(&sb_lock);
208 restart:
209         list_for_each_entry(sb, &super_blocks, s_list) {
210                 /* This test just improves performance so it needn't be
211                  * reliable... */
212                 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
213                         if (type != -1 && type != cnt)
214                                 continue;
215                         if (!sb_has_quota_active(sb, cnt))
216                                 continue;
217                         if (!info_dirty(&sb_dqopt(sb)->info[cnt]) &&
218                            list_empty(&sb_dqopt(sb)->info[cnt].dqi_dirty_list))
219                                 continue;
220                         break;
221                 }
222                 if (cnt == MAXQUOTAS)
223                         continue;
224                 sb->s_count++;
225                 spin_unlock(&sb_lock);
226                 down_read(&sb->s_umount);
227                 if (sb->s_root)
228                         sync_quota_sb(sb, type);
229                 up_read(&sb->s_umount);
230                 spin_lock(&sb_lock);
231                 if (__put_super_and_need_restart(sb))
232                         goto restart;
233         }
234         spin_unlock(&sb_lock);
235 }
236
237 static 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
251 static 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
267 static 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
278 static 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
287 static 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
301 static 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
311 static 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
320 static 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
331 static 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
341 static 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
353 /* Copy parameters and call proper function */
354 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
355                        void __user *addr)
356 {
357         switch (cmd) {
358         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();
393         }
394
395         return 0;
396 }
397
398 /*
399  * 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  */
402 static struct super_block *quotactl_block(const char __user *special)
403 {
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))
410                 return ERR_CAST(tmp);
411         bdev = lookup_bdev(tmp);
412         putname(tmp);
413         if (IS_ERR(bdev))
414                 return ERR_CAST(bdev);
415         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 /*
427  * 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  */
432 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
433                 qid_t, id, void __user *, addr)
434 {
435         uint cmds, type;
436         struct super_block *sb = NULL;
437         int ret;
438
439         cmds = cmd >> SUBCMDSHIFT;
440         type = cmd & SUBCMDMASK;
441
442         if (cmds != Q_SYNC || special) {
443                 sb = quotactl_block(special);
444                 if (IS_ERR(sb))
445                         return PTR_ERR(sb);
446         }
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 }
456
457 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
458 /*
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  */
462 struct 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 */
475 struct compat_fs_qfilestat {
476         compat_u64 dqb_bhardlimit;
477         compat_u64 qfs_nblks;
478         compat_uint_t qfs_nextents;
479 };
480
481 struct 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
495 asmlinkage 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
572
573
574 #ifdef CONFIG_QUOTA_NETLINK_INTERFACE
575
576 /* Netlink family structure for quota */
577 static 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
597 void 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;
645 attr_err_out:
646         printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
647 err_out:
648         kfree_skb(skb);
649 }
650 EXPORT_SYMBOL(quota_send_warning);
651
652 static 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
660 module_init(quota_init);
661 #endif
662