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