fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / 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/kernel.h>
14 #include <linux/smp_lock.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/vs_context.h>
21
22 /* Check validity of generic quotactl commands */
23 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
24 {
25         if (type >= MAXQUOTAS)
26                 return -EINVAL;
27         if (!sb && cmd != Q_SYNC)
28                 return -ENODEV;
29         /* Is operation supported? */
30         if (sb && !sb->s_qcop)
31                 return -ENOSYS;
32
33         switch (cmd) {
34                 case Q_GETFMT:
35                         break;
36                 case Q_QUOTAON:
37                         if (!sb->s_qcop->quota_on)
38                                 return -ENOSYS;
39                         break;
40                 case Q_QUOTAOFF:
41                         if (!sb->s_qcop->quota_off)
42                                 return -ENOSYS;
43                         break;
44                 case Q_SETINFO:
45                         if (!sb->s_qcop->set_info)
46                                 return -ENOSYS;
47                         break;
48                 case Q_GETINFO:
49                         if (!sb->s_qcop->get_info)
50                                 return -ENOSYS;
51                         break;
52                 case Q_SETQUOTA:
53                         if (!sb->s_qcop->set_dqblk)
54                                 return -ENOSYS;
55                         break;
56                 case Q_GETQUOTA:
57                         if (!sb->s_qcop->get_dqblk)
58                                 return -ENOSYS;
59                         break;
60                 case Q_SYNC:
61                         if (sb && !sb->s_qcop->quota_sync)
62                                 return -ENOSYS;
63                         break;
64                 default:
65                         return -EINVAL;
66         }
67
68         /* Is quota turned on for commands which need it? */
69         switch (cmd) {
70                 case Q_GETFMT:
71                 case Q_GETINFO:
72                 case Q_QUOTAOFF:
73                 case Q_SETINFO:
74                 case Q_SETQUOTA:
75                 case Q_GETQUOTA:
76                         /* This is just informative test so we are satisfied without a lock */
77                         if (!sb_has_quota_enabled(sb, type))
78                                 return -ESRCH;
79         }
80
81         /* Check privileges */
82         if (cmd == Q_GETQUOTA) {
83                 if (((type == USRQUOTA && current->euid != id) ||
84                      (type == GRPQUOTA && !in_egroup_p(id))) &&
85                     !vx_capable(CAP_SYS_ADMIN, VXC_QUOTA_CTL))
86                         return -EPERM;
87         }
88         else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
89                 if (!vx_capable(CAP_SYS_ADMIN, VXC_QUOTA_CTL))
90                         return -EPERM;
91
92         return 0;
93 }
94
95 /* Check validity of XFS Quota Manager commands */
96 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
97 {
98         if (type >= XQM_MAXQUOTAS)
99                 return -EINVAL;
100         if (!sb)
101                 return -ENODEV;
102         if (!sb->s_qcop)
103                 return -ENOSYS;
104
105         switch (cmd) {
106                 case Q_XQUOTAON:
107                 case Q_XQUOTAOFF:
108                 case Q_XQUOTARM:
109                         if (!sb->s_qcop->set_xstate)
110                                 return -ENOSYS;
111                         break;
112                 case Q_XGETQSTAT:
113                         if (!sb->s_qcop->get_xstate)
114                                 return -ENOSYS;
115                         break;
116                 case Q_XSETQLIM:
117                         if (!sb->s_qcop->set_xquota)
118                                 return -ENOSYS;
119                         break;
120                 case Q_XGETQUOTA:
121                         if (!sb->s_qcop->get_xquota)
122                                 return -ENOSYS;
123                         break;
124                 case Q_XQUOTASYNC:
125                         if (!sb->s_qcop->quota_sync)
126                                 return -ENOSYS;
127                         break;
128                 default:
129                         return -EINVAL;
130         }
131
132         /* Check privileges */
133         if (cmd == Q_XGETQUOTA) {
134                 if (((type == XQM_USRQUOTA && current->euid != id) ||
135                      (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
136                      !vx_capable(CAP_SYS_ADMIN, VXC_QUOTA_CTL))
137                         return -EPERM;
138         } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
139                 if (!vx_capable(CAP_SYS_ADMIN, VXC_QUOTA_CTL))
140                         return -EPERM;
141         }
142
143         return 0;
144 }
145
146 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
147 {
148         int error;
149
150         if (XQM_COMMAND(cmd))
151                 error = xqm_quotactl_valid(sb, type, cmd, id);
152         else
153                 error = generic_quotactl_valid(sb, type, cmd, id);
154         if (!error)
155                 error = security_quotactl(cmd, type, id, sb);
156         return error;
157 }
158
159 static void quota_sync_sb(struct super_block *sb, int type)
160 {
161         int cnt;
162         struct inode *discard[MAXQUOTAS];
163
164         sb->s_qcop->quota_sync(sb, type);
165         /* This is not very clever (and fast) but currently I don't know about
166          * any other simple way of getting quota data to disk and we must get
167          * them there for userspace to be visible... */
168         if (sb->s_op->sync_fs)
169                 sb->s_op->sync_fs(sb, 1);
170         sync_blockdev(sb->s_bdev);
171
172         /* Now when everything is written we can discard the pagecache so
173          * that userspace sees the changes. We need i_mutex and so we could
174          * not do it inside dqonoff_mutex. Moreover we need to be carefull
175          * about races with quotaoff() (that is the reason why we have own
176          * reference to inode). */
177         mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
178         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
179                 discard[cnt] = NULL;
180                 if (type != -1 && cnt != type)
181                         continue;
182                 if (!sb_has_quota_enabled(sb, cnt))
183                         continue;
184                 discard[cnt] = igrab(sb_dqopt(sb)->files[cnt]);
185         }
186         mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
187         for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
188                 if (discard[cnt]) {
189                         mutex_lock(&discard[cnt]->i_mutex);
190                         truncate_inode_pages(&discard[cnt]->i_data, 0);
191                         mutex_unlock(&discard[cnt]->i_mutex);
192                         iput(discard[cnt]);
193                 }
194         }
195 }
196
197 void sync_dquots(struct super_block *sb, int type)
198 {
199         int cnt, dirty;
200
201         if (sb) {
202                 if (sb->s_qcop->quota_sync)
203                         quota_sync_sb(sb, type);
204                 return;
205         }
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 reliable... */
211                 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
212                         if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
213                             && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
214                                 dirty = 1;
215                 if (!dirty)
216                         continue;
217                 sb->s_count++;
218                 spin_unlock(&sb_lock);
219                 down_read(&sb->s_umount);
220                 if (sb->s_root && sb->s_qcop->quota_sync)
221                         quota_sync_sb(sb, type);
222                 up_read(&sb->s_umount);
223                 spin_lock(&sb_lock);
224                 if (__put_super_and_need_restart(sb))
225                         goto restart;
226         }
227         spin_unlock(&sb_lock);
228 }
229
230 /* Copy parameters and call proper function */
231 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
232 {
233         int ret;
234
235         switch (cmd) {
236                 case Q_QUOTAON: {
237                         char *pathname;
238
239                         if (IS_ERR(pathname = getname(addr)))
240                                 return PTR_ERR(pathname);
241                         ret = sb->s_qcop->quota_on(sb, type, id, pathname);
242                         putname(pathname);
243                         return ret;
244                 }
245                 case Q_QUOTAOFF:
246                         return sb->s_qcop->quota_off(sb, type);
247
248                 case Q_GETFMT: {
249                         __u32 fmt;
250
251                         down_read(&sb_dqopt(sb)->dqptr_sem);
252                         if (!sb_has_quota_enabled(sb, type)) {
253                                 up_read(&sb_dqopt(sb)->dqptr_sem);
254                                 return -ESRCH;
255                         }
256                         fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
257                         up_read(&sb_dqopt(sb)->dqptr_sem);
258                         if (copy_to_user(addr, &fmt, sizeof(fmt)))
259                                 return -EFAULT;
260                         return 0;
261                 }
262                 case Q_GETINFO: {
263                         struct if_dqinfo info;
264
265                         if ((ret = sb->s_qcop->get_info(sb, type, &info)))
266                                 return ret;
267                         if (copy_to_user(addr, &info, sizeof(info)))
268                                 return -EFAULT;
269                         return 0;
270                 }
271                 case Q_SETINFO: {
272                         struct if_dqinfo info;
273
274                         if (copy_from_user(&info, addr, sizeof(info)))
275                                 return -EFAULT;
276                         return sb->s_qcop->set_info(sb, type, &info);
277                 }
278                 case Q_GETQUOTA: {
279                         struct if_dqblk idq;
280
281                         if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
282                                 return ret;
283                         if (copy_to_user(addr, &idq, sizeof(idq)))
284                                 return -EFAULT;
285                         return 0;
286                 }
287                 case Q_SETQUOTA: {
288                         struct if_dqblk idq;
289
290                         if (copy_from_user(&idq, addr, sizeof(idq)))
291                                 return -EFAULT;
292                         return sb->s_qcop->set_dqblk(sb, type, id, &idq);
293                 }
294                 case Q_SYNC:
295                         sync_dquots(sb, type);
296                         return 0;
297
298                 case Q_XQUOTAON:
299                 case Q_XQUOTAOFF:
300                 case Q_XQUOTARM: {
301                         __u32 flags;
302
303                         if (copy_from_user(&flags, addr, sizeof(flags)))
304                                 return -EFAULT;
305                         return sb->s_qcop->set_xstate(sb, flags, cmd);
306                 }
307                 case Q_XGETQSTAT: {
308                         struct fs_quota_stat fqs;
309                 
310                         if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
311                                 return ret;
312                         if (copy_to_user(addr, &fqs, sizeof(fqs)))
313                                 return -EFAULT;
314                         return 0;
315                 }
316                 case Q_XSETQLIM: {
317                         struct fs_disk_quota fdq;
318
319                         if (copy_from_user(&fdq, addr, sizeof(fdq)))
320                                 return -EFAULT;
321                        return sb->s_qcop->set_xquota(sb, type, id, &fdq);
322                 }
323                 case Q_XGETQUOTA: {
324                         struct fs_disk_quota fdq;
325
326                         if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
327                                 return ret;
328                         if (copy_to_user(addr, &fdq, sizeof(fdq)))
329                                 return -EFAULT;
330                         return 0;
331                 }
332                 case Q_XQUOTASYNC:
333                         return sb->s_qcop->quota_sync(sb, type);
334                 /* We never reach here unless validity check is broken */
335                 default:
336                         BUG();
337         }
338         return 0;
339 }
340
341 #if defined(CONFIG_BLK_DEV_VROOT) || defined(CONFIG_BLK_DEV_VROOT_MODULE)
342
343 #include <linux/vroot.h>
344 #include <linux/major.h>
345 #include <linux/module.h>
346 #include <linux/kallsyms.h>
347 #include <linux/vserver/debug.h>
348
349 static vroot_grb_func *vroot_get_real_bdev = NULL;
350
351 static spinlock_t vroot_grb_lock = SPIN_LOCK_UNLOCKED;
352
353 int register_vroot_grb(vroot_grb_func *func) {
354         int ret = -EBUSY;
355
356         spin_lock(&vroot_grb_lock);
357         if (!vroot_get_real_bdev) {
358                 vroot_get_real_bdev = func;
359                 ret = 0;
360         }
361         spin_unlock(&vroot_grb_lock);
362         return ret;
363 }
364 EXPORT_SYMBOL(register_vroot_grb);
365
366 int unregister_vroot_grb(vroot_grb_func *func) {
367         int ret = -EINVAL;
368
369         spin_lock(&vroot_grb_lock);
370         if (vroot_get_real_bdev) {
371                 vroot_get_real_bdev = NULL;
372                 ret = 0;
373         }
374         spin_unlock(&vroot_grb_lock);
375         return ret;
376 }
377 EXPORT_SYMBOL(unregister_vroot_grb);
378
379 #endif
380
381 /*
382  * look up a superblock on which quota ops will be performed
383  * - use the name of a block device to find the superblock thereon
384  */
385 static inline struct super_block *quotactl_block(const char __user *special)
386 {
387 #ifdef CONFIG_BLOCK
388         struct block_device *bdev;
389         struct super_block *sb;
390         char *tmp = getname(special);
391
392         if (IS_ERR(tmp))
393                 return ERR_PTR(PTR_ERR(tmp));
394         bdev = lookup_bdev(tmp);
395         putname(tmp);
396         if (IS_ERR(bdev))
397                 return ERR_PTR(PTR_ERR(bdev));
398 #if defined(CONFIG_BLK_DEV_VROOT) || defined(CONFIG_BLK_DEV_VROOT_MODULE)
399         if (bdev && bdev->bd_inode &&
400                         imajor(bdev->bd_inode) == VROOT_MAJOR) {
401                 struct block_device *bdnew = (void *)-EINVAL;
402
403                 if (vroot_get_real_bdev)
404                         bdnew = vroot_get_real_bdev(bdev);
405                 else
406                         vxdprintk(VXD_CBIT(misc, 0),
407                                         "vroot_get_real_bdev not set");
408                 bdput(bdev);
409                 if (IS_ERR(bdnew))
410                         return ERR_PTR(PTR_ERR(bdnew));
411                 bdev = bdnew;
412         }
413 #endif
414         sb = get_super(bdev);
415         bdput(bdev);
416         if (!sb)
417                 return ERR_PTR(-ENODEV);
418
419         return sb;
420 #else
421         return ERR_PTR(-ENODEV);
422 #endif
423 }
424
425 /*
426  * This is the system call interface. This communicates with
427  * the user-level programs. Currently this only supports diskquota
428  * calls. Maybe we need to add the process quotas etc. in the future,
429  * but we probably should use rlimits for that.
430  */
431 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
432 {
433         uint cmds, type;
434         struct super_block *sb = NULL;
435         int ret;
436
437         cmds = cmd >> SUBCMDSHIFT;
438         type = cmd & SUBCMDMASK;
439
440         if (cmds != Q_SYNC || special) {
441                 sb = quotactl_block(special);
442                 if (IS_ERR(sb))
443                         return PTR_ERR(sb);
444         }
445
446         ret = check_quotactl_valid(sb, type, cmds, id);
447         if (ret >= 0)
448                 ret = do_quotactl(sb, type, cmds, id, addr);
449         if (sb)
450                 drop_super(sb);
451
452         return ret;
453 }