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