1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58#include <linux/errno.h>
59#include <linux/kernel.h>
60#include <linux/fs.h>
61#include <linux/mount.h>
62#include <linux/mm.h>
63#include <linux/time.h>
64#include <linux/types.h>
65#include <linux/string.h>
66#include <linux/fcntl.h>
67#include <linux/stat.h>
68#include <linux/tty.h>
69#include <linux/file.h>
70#include <linux/slab.h>
71#include <linux/sysctl.h>
72#include <linux/init.h>
73#include <linux/module.h>
74#include <linux/proc_fs.h>
75#include <linux/security.h>
76#include <linux/kmod.h>
77#include <linux/namei.h>
78#include <linux/buffer_head.h>
79#include <linux/capability.h>
80#include <linux/quotaops.h>
81#include <linux/writeback.h>
82#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
83#include <net/netlink.h>
84#include <net/genetlink.h>
85#endif
86
87#include <asm/uaccess.h>
88
89#define __DQUOT_PARANOIA
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130static DEFINE_SPINLOCK(dq_list_lock);
131DEFINE_SPINLOCK(dq_data_lock);
132
133static char *quotatypes[] = INITQFNAMES;
134static struct quota_format_type *quota_formats;
135static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
136
137
138static struct kmem_cache *dquot_cachep;
139
140int register_quota_format(struct quota_format_type *fmt)
141{
142 spin_lock(&dq_list_lock);
143 fmt->qf_next = quota_formats;
144 quota_formats = fmt;
145 spin_unlock(&dq_list_lock);
146 return 0;
147}
148
149void unregister_quota_format(struct quota_format_type *fmt)
150{
151 struct quota_format_type **actqf;
152
153 spin_lock(&dq_list_lock);
154 for (actqf = "a_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
155 if (*actqf)
156 *actqf = (*actqf)->qf_next;
157 spin_unlock(&dq_list_lock);
158}
159
160static struct quota_format_type *find_quota_format(int id)
161{
162 struct quota_format_type *actqf;
163
164 spin_lock(&dq_list_lock);
165 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
166 if (!actqf || !try_module_get(actqf->qf_owner)) {
167 int qm;
168
169 spin_unlock(&dq_list_lock);
170
171 for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
172 if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
173 return NULL;
174
175 spin_lock(&dq_list_lock);
176 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
177 if (actqf && !try_module_get(actqf->qf_owner))
178 actqf = NULL;
179 }
180 spin_unlock(&dq_list_lock);
181 return actqf;
182}
183
184static void put_quota_format(struct quota_format_type *fmt)
185{
186 module_put(fmt->qf_owner);
187}
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209static LIST_HEAD(inuse_list);
210static LIST_HEAD(free_dquots);
211static unsigned int dq_hash_bits, dq_hash_mask;
212static struct hlist_head *dquot_hash;
213
214struct dqstats dqstats;
215
216static void dqput(struct dquot *dquot);
217
218static inline unsigned int
219hashfn(const struct super_block *sb, unsigned int id, int type)
220{
221 unsigned long tmp;
222
223 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
224 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
225}
226
227
228
229
230static inline void insert_dquot_hash(struct dquot *dquot)
231{
232 struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
233 hlist_add_head(&dquot->dq_hash, head);
234}
235
236static inline void remove_dquot_hash(struct dquot *dquot)
237{
238 hlist_del_init(&dquot->dq_hash);
239}
240
241static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
242{
243 struct hlist_node *node;
244 struct dquot *dquot;
245
246 hlist_for_each (node, dquot_hash+hashent) {
247 dquot = hlist_entry(node, struct dquot, dq_hash);
248 if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
249 return dquot;
250 }
251 return NODQUOT;
252}
253
254
255static inline void put_dquot_last(struct dquot *dquot)
256{
257 list_add_tail(&dquot->dq_free, &free_dquots);
258 dqstats.free_dquots++;
259}
260
261static inline void remove_free_dquot(struct dquot *dquot)
262{
263 if (list_empty(&dquot->dq_free))
264 return;
265 list_del_init(&dquot->dq_free);
266 dqstats.free_dquots--;
267}
268
269static inline void put_inuse(struct dquot *dquot)
270{
271
272
273 list_add_tail(&dquot->dq_inuse, &inuse_list);
274 dqstats.allocated_dquots++;
275}
276
277static inline void remove_inuse(struct dquot *dquot)
278{
279 dqstats.allocated_dquots--;
280 list_del(&dquot->dq_inuse);
281}
282
283
284
285
286static void wait_on_dquot(struct dquot *dquot)
287{
288 mutex_lock(&dquot->dq_lock);
289 mutex_unlock(&dquot->dq_lock);
290}
291
292#define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
293
294int dquot_mark_dquot_dirty(struct dquot *dquot)
295{
296 spin_lock(&dq_list_lock);
297 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
298 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
299 info[dquot->dq_type].dqi_dirty_list);
300 spin_unlock(&dq_list_lock);
301 return 0;
302}
303
304
305static inline int clear_dquot_dirty(struct dquot *dquot)
306{
307 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
308 return 0;
309 list_del_init(&dquot->dq_dirty);
310 return 1;
311}
312
313void mark_info_dirty(struct super_block *sb, int type)
314{
315 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
316}
317EXPORT_SYMBOL(mark_info_dirty);
318
319
320
321
322
323int dquot_acquire(struct dquot *dquot)
324{
325 int ret = 0, ret2 = 0;
326 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
327
328 mutex_lock(&dquot->dq_lock);
329 mutex_lock(&dqopt->dqio_mutex);
330 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
331 ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
332 if (ret < 0)
333 goto out_iolock;
334 set_bit(DQ_READ_B, &dquot->dq_flags);
335
336 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
337 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
338
339 if (info_dirty(&dqopt->info[dquot->dq_type]))
340 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
341 if (ret < 0)
342 goto out_iolock;
343 if (ret2 < 0) {
344 ret = ret2;
345 goto out_iolock;
346 }
347 }
348 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
349out_iolock:
350 mutex_unlock(&dqopt->dqio_mutex);
351 mutex_unlock(&dquot->dq_lock);
352 return ret;
353}
354
355
356
357
358int dquot_commit(struct dquot *dquot)
359{
360 int ret = 0, ret2 = 0;
361 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
362
363 mutex_lock(&dqopt->dqio_mutex);
364 spin_lock(&dq_list_lock);
365 if (!clear_dquot_dirty(dquot)) {
366 spin_unlock(&dq_list_lock);
367 goto out_sem;
368 }
369 spin_unlock(&dq_list_lock);
370
371
372 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
373 ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
374 if (info_dirty(&dqopt->info[dquot->dq_type]))
375 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
376 if (ret >= 0)
377 ret = ret2;
378 }
379out_sem:
380 mutex_unlock(&dqopt->dqio_mutex);
381 return ret;
382}
383
384
385
386
387int dquot_release(struct dquot *dquot)
388{
389 int ret = 0, ret2 = 0;
390 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
391
392 mutex_lock(&dquot->dq_lock);
393
394 if (atomic_read(&dquot->dq_count) > 1)
395 goto out_dqlock;
396 mutex_lock(&dqopt->dqio_mutex);
397 if (dqopt->ops[dquot->dq_type]->release_dqblk) {
398 ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
399
400 if (info_dirty(&dqopt->info[dquot->dq_type]))
401 ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
402 if (ret >= 0)
403 ret = ret2;
404 }
405 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
406 mutex_unlock(&dqopt->dqio_mutex);
407out_dqlock:
408 mutex_unlock(&dquot->dq_lock);
409 return ret;
410}
411
412
413
414
415
416
417
418static void invalidate_dquots(struct super_block *sb, int type)
419{
420 struct dquot *dquot, *tmp;
421
422restart:
423 spin_lock(&dq_list_lock);
424 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
425 if (dquot->dq_sb != sb)
426 continue;
427 if (dquot->dq_type != type)
428 continue;
429
430 if (atomic_read(&dquot->dq_count)) {
431 DEFINE_WAIT(wait);
432
433 atomic_inc(&dquot->dq_count);
434 prepare_to_wait(&dquot->dq_wait_unused, &wait,
435 TASK_UNINTERRUPTIBLE);
436 spin_unlock(&dq_list_lock);
437
438
439
440
441
442
443
444 if (atomic_read(&dquot->dq_count) > 1)
445 schedule();
446 finish_wait(&dquot->dq_wait_unused, &wait);
447 dqput(dquot);
448
449
450
451 goto restart;
452 }
453
454
455
456
457 remove_dquot_hash(dquot);
458 remove_free_dquot(dquot);
459 remove_inuse(dquot);
460 kmem_cache_free(dquot_cachep, dquot);
461 }
462 spin_unlock(&dq_list_lock);
463}
464
465int vfs_quota_sync(struct super_block *sb, int type)
466{
467 struct list_head *dirty;
468 struct dquot *dquot;
469 struct quota_info *dqopt = sb_dqopt(sb);
470 int cnt;
471
472 mutex_lock(&dqopt->dqonoff_mutex);
473 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
474 if (type != -1 && cnt != type)
475 continue;
476 if (!sb_has_quota_enabled(sb, cnt))
477 continue;
478 spin_lock(&dq_list_lock);
479 dirty = &dqopt->info[cnt].dqi_dirty_list;
480 while (!list_empty(dirty)) {
481 dquot = list_first_entry(dirty, struct dquot, dq_dirty);
482
483 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
484 clear_dquot_dirty(dquot);
485 continue;
486 }
487
488
489
490 atomic_inc(&dquot->dq_count);
491 dqstats.lookups++;
492 spin_unlock(&dq_list_lock);
493 sb->dq_op->write_dquot(dquot);
494 dqput(dquot);
495 spin_lock(&dq_list_lock);
496 }
497 spin_unlock(&dq_list_lock);
498 }
499
500 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
501 if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
502 && info_dirty(&dqopt->info[cnt]))
503 sb->dq_op->write_info(sb, cnt);
504 spin_lock(&dq_list_lock);
505 dqstats.syncs++;
506 spin_unlock(&dq_list_lock);
507 mutex_unlock(&dqopt->dqonoff_mutex);
508
509 return 0;
510}
511
512
513static void prune_dqcache(int count)
514{
515 struct list_head *head;
516 struct dquot *dquot;
517
518 head = free_dquots.prev;
519 while (head != &free_dquots && count) {
520 dquot = list_entry(head, struct dquot, dq_free);
521 remove_dquot_hash(dquot);
522 remove_free_dquot(dquot);
523 remove_inuse(dquot);
524 kmem_cache_free(dquot_cachep, dquot);
525 count--;
526 head = free_dquots.prev;
527 }
528}
529
530
531
532
533
534
535static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
536{
537 if (nr) {
538 spin_lock(&dq_list_lock);
539 prune_dqcache(nr);
540 spin_unlock(&dq_list_lock);
541 }
542 return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
543}
544
545static struct shrinker dqcache_shrinker = {
546 .shrink = shrink_dqcache_memory,
547 .seeks = DEFAULT_SEEKS,
548};
549
550
551
552
553
554
555static void dqput(struct dquot *dquot)
556{
557 if (!dquot)
558 return;
559#ifdef __DQUOT_PARANOIA
560 if (!atomic_read(&dquot->dq_count)) {
561 printk("VFS: dqput: trying to free free dquot\n");
562 printk("VFS: device %s, dquot of %s %d\n",
563 dquot->dq_sb->s_id,
564 quotatypes[dquot->dq_type],
565 dquot->dq_id);
566 BUG();
567 }
568#endif
569
570 spin_lock(&dq_list_lock);
571 dqstats.drops++;
572 spin_unlock(&dq_list_lock);
573we_slept:
574 spin_lock(&dq_list_lock);
575 if (atomic_read(&dquot->dq_count) > 1) {
576
577 atomic_dec(&dquot->dq_count);
578
579 if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
580 atomic_read(&dquot->dq_count) == 1)
581 wake_up(&dquot->dq_wait_unused);
582 spin_unlock(&dq_list_lock);
583 return;
584 }
585
586 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
587 spin_unlock(&dq_list_lock);
588
589 dquot->dq_sb->dq_op->write_dquot(dquot);
590 goto we_slept;
591 }
592
593 clear_dquot_dirty(dquot);
594 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
595 spin_unlock(&dq_list_lock);
596 dquot->dq_sb->dq_op->release_dquot(dquot);
597 goto we_slept;
598 }
599 atomic_dec(&dquot->dq_count);
600#ifdef __DQUOT_PARANOIA
601
602 BUG_ON(!list_empty(&dquot->dq_free));
603#endif
604 put_dquot_last(dquot);
605 spin_unlock(&dq_list_lock);
606}
607
608static struct dquot *get_empty_dquot(struct super_block *sb, int type)
609{
610 struct dquot *dquot;
611
612 dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
613 if(!dquot)
614 return NODQUOT;
615
616 mutex_init(&dquot->dq_lock);
617 INIT_LIST_HEAD(&dquot->dq_free);
618 INIT_LIST_HEAD(&dquot->dq_inuse);
619 INIT_HLIST_NODE(&dquot->dq_hash);
620 INIT_LIST_HEAD(&dquot->dq_dirty);
621 init_waitqueue_head(&dquot->dq_wait_unused);
622 dquot->dq_sb = sb;
623 dquot->dq_type = type;
624 atomic_set(&dquot->dq_count, 1);
625
626 return dquot;
627}
628
629
630
631
632
633static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
634{
635 unsigned int hashent = hashfn(sb, id, type);
636 struct dquot *dquot, *empty = NODQUOT;
637
638 if (!sb_has_quota_enabled(sb, type))
639 return NODQUOT;
640we_slept:
641 spin_lock(&dq_list_lock);
642 if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
643 if (empty == NODQUOT) {
644 spin_unlock(&dq_list_lock);
645 if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
646 schedule();
647 goto we_slept;
648 }
649 dquot = empty;
650 dquot->dq_id = id;
651
652 put_inuse(dquot);
653
654 insert_dquot_hash(dquot);
655 dqstats.lookups++;
656 spin_unlock(&dq_list_lock);
657 } else {
658 if (!atomic_read(&dquot->dq_count))
659 remove_free_dquot(dquot);
660 atomic_inc(&dquot->dq_count);
661 dqstats.cache_hits++;
662 dqstats.lookups++;
663 spin_unlock(&dq_list_lock);
664 if (empty)
665 kmem_cache_free(dquot_cachep, empty);
666 }
667
668
669 wait_on_dquot(dquot);
670
671 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
672 dqput(dquot);
673 return NODQUOT;
674 }
675#ifdef __DQUOT_PARANOIA
676 BUG_ON(!dquot->dq_sb);
677#endif
678
679 return dquot;
680}
681
682static int dqinit_needed(struct inode *inode, int type)
683{
684 int cnt;
685
686 if (IS_NOQUOTA(inode))
687 return 0;
688 if (type != -1)
689 return inode->i_dquot[type] == NODQUOT;
690 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
691 if (inode->i_dquot[cnt] == NODQUOT)
692 return 1;
693 return 0;
694}
695
696
697static void add_dquot_ref(struct super_block *sb, int type)
698{
699 struct inode *inode;
700
701restart:
702 spin_lock(&inode_lock);
703 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
704 if (!atomic_read(&inode->i_writecount))
705 continue;
706 if (!dqinit_needed(inode, type))
707 continue;
708 if (inode->i_state & (I_FREEING|I_WILL_FREE))
709 continue;
710
711 __iget(inode);
712 spin_unlock(&inode_lock);
713
714 sb->dq_op->initialize(inode, type);
715 iput(inode);
716
717 goto restart;
718 }
719 spin_unlock(&inode_lock);
720}
721
722
723static inline int dqput_blocks(struct dquot *dquot)
724{
725 if (atomic_read(&dquot->dq_count) <= 1)
726 return 1;
727 return 0;
728}
729
730
731
732static int remove_inode_dquot_ref(struct inode *inode, int type,
733 struct list_head *tofree_head)
734{
735 struct dquot *dquot = inode->i_dquot[type];
736
737 inode->i_dquot[type] = NODQUOT;
738 if (dquot != NODQUOT) {
739 if (dqput_blocks(dquot)) {
740#ifdef __DQUOT_PARANOIA
741 if (atomic_read(&dquot->dq_count) != 1)
742 printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
743#endif
744 spin_lock(&dq_list_lock);
745 list_add(&dquot->dq_free, tofree_head);
746 spin_unlock(&dq_list_lock);
747 return 1;
748 }
749 else
750 dqput(dquot);
751 }
752 return 0;
753}
754
755
756
757static void put_dquot_list(struct list_head *tofree_head)
758{
759 struct list_head *act_head;
760 struct dquot *dquot;
761
762 act_head = tofree_head->next;
763
764 while (act_head != tofree_head) {
765 dquot = list_entry(act_head, struct dquot, dq_free);
766 act_head = act_head->next;
767 list_del_init(&dquot->dq_free);
768 dqput(dquot);
769 }
770}
771
772static void remove_dquot_ref(struct super_block *sb, int type,
773 struct list_head *tofree_head)
774{
775 struct inode *inode;
776
777 spin_lock(&inode_lock);
778 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
779 if (!IS_NOQUOTA(inode))
780 remove_inode_dquot_ref(inode, type, tofree_head);
781 }
782 spin_unlock(&inode_lock);
783}
784
785
786static void drop_dquot_ref(struct super_block *sb, int type)
787{
788 LIST_HEAD(tofree_head);
789
790 if (sb->dq_op) {
791 down_write(&sb_dqopt(sb)->dqptr_sem);
792 remove_dquot_ref(sb, type, &tofree_head);
793 up_write(&sb_dqopt(sb)->dqptr_sem);
794 put_dquot_list(&tofree_head);
795 }
796}
797
798static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
799{
800 dquot->dq_dqb.dqb_curinodes += number;
801}
802
803static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
804{
805 dquot->dq_dqb.dqb_curspace += number;
806}
807
808static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
809{
810 if (dquot->dq_dqb.dqb_curinodes > number)
811 dquot->dq_dqb.dqb_curinodes -= number;
812 else
813 dquot->dq_dqb.dqb_curinodes = 0;
814 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
815 dquot->dq_dqb.dqb_itime = (time_t) 0;
816 clear_bit(DQ_INODES_B, &dquot->dq_flags);
817}
818
819static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
820{
821 if (dquot->dq_dqb.dqb_curspace > number)
822 dquot->dq_dqb.dqb_curspace -= number;
823 else
824 dquot->dq_dqb.dqb_curspace = 0;
825 if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
826 dquot->dq_dqb.dqb_btime = (time_t) 0;
827 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
828}
829
830static int warning_issued(struct dquot *dquot, const int warntype)
831{
832 int flag = (warntype == QUOTA_NL_BHARDWARN ||
833 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
834 ((warntype == QUOTA_NL_IHARDWARN ||
835 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
836
837 if (!flag)
838 return 0;
839 return test_and_set_bit(flag, &dquot->dq_flags);
840}
841
842#ifdef CONFIG_PRINT_QUOTA_WARNING
843static int flag_print_warnings = 1;
844
845static inline int need_print_warning(struct dquot *dquot)
846{
847 if (!flag_print_warnings)
848 return 0;
849
850 switch (dquot->dq_type) {
851 case USRQUOTA:
852 return current->fsuid == dquot->dq_id;
853 case GRPQUOTA:
854 return in_group_p(dquot->dq_id);
855 }
856 return 0;
857}
858
859
860static void print_warning(struct dquot *dquot, const int warntype)
861{
862 char *msg = NULL;
863 struct tty_struct *tty;
864
865 if (!need_print_warning(dquot))
866 return;
867
868 mutex_lock(&tty_mutex);
869 tty = get_current_tty();
870 if (!tty)
871 goto out_lock;
872 tty_write_message(tty, dquot->dq_sb->s_id);
873 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
874 tty_write_message(tty, ": warning, ");
875 else
876 tty_write_message(tty, ": write failed, ");
877 tty_write_message(tty, quotatypes[dquot->dq_type]);
878 switch (warntype) {
879 case QUOTA_NL_IHARDWARN:
880 msg = " file limit reached.\r\n";
881 break;
882 case QUOTA_NL_ISOFTLONGWARN:
883 msg = " file quota exceeded too long.\r\n";
884 break;
885 case QUOTA_NL_ISOFTWARN:
886 msg = " file quota exceeded.\r\n";
887 break;
888 case QUOTA_NL_BHARDWARN:
889 msg = " block limit reached.\r\n";
890 break;
891 case QUOTA_NL_BSOFTLONGWARN:
892 msg = " block quota exceeded too long.\r\n";
893 break;
894 case QUOTA_NL_BSOFTWARN:
895 msg = " block quota exceeded.\r\n";
896 break;
897 }
898 tty_write_message(tty, msg);
899out_lock:
900 mutex_unlock(&tty_mutex);
901}
902#endif
903
904#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
905
906
907static struct genl_family quota_genl_family = {
908 .id = GENL_ID_GENERATE,
909 .hdrsize = 0,
910 .name = "VFS_DQUOT",
911 .version = 1,
912 .maxattr = QUOTA_NL_A_MAX,
913};
914
915
916static void send_warning(const struct dquot *dquot, const char warntype)
917{
918 static atomic_t seq;
919 struct sk_buff *skb;
920 void *msg_head;
921 int ret;
922 int msg_size = 4 * nla_total_size(sizeof(u32)) +
923 2 * nla_total_size(sizeof(u64));
924
925
926
927
928 skb = genlmsg_new(msg_size, GFP_NOFS);
929 if (!skb) {
930 printk(KERN_ERR
931 "VFS: Not enough memory to send quota warning.\n");
932 return;
933 }
934 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
935 "a_genl_family, 0, QUOTA_NL_C_WARNING);
936 if (!msg_head) {
937 printk(KERN_ERR
938 "VFS: Cannot store netlink header in quota warning.\n");
939 goto err_out;
940 }
941 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, dquot->dq_type);
942 if (ret)
943 goto attr_err_out;
944 ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, dquot->dq_id);
945 if (ret)
946 goto attr_err_out;
947 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
948 if (ret)
949 goto attr_err_out;
950 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR,
951 MAJOR(dquot->dq_sb->s_dev));
952 if (ret)
953 goto attr_err_out;
954 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR,
955 MINOR(dquot->dq_sb->s_dev));
956 if (ret)
957 goto attr_err_out;
958 ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, current->user->uid);
959 if (ret)
960 goto attr_err_out;
961 genlmsg_end(skb, msg_head);
962
963 ret = genlmsg_multicast(skb, 0, quota_genl_family.id, GFP_NOFS);
964 if (ret < 0 && ret != -ESRCH)
965 printk(KERN_ERR
966 "VFS: Failed to send notification message: %d\n", ret);
967 return;
968attr_err_out:
969 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
970err_out:
971 kfree_skb(skb);
972}
973#endif
974
975static inline void flush_warnings(struct dquot * const *dquots, char *warntype)
976{
977 int i;
978
979 for (i = 0; i < MAXQUOTAS; i++)
980 if (dquots[i] != NODQUOT && warntype[i] != QUOTA_NL_NOWARN &&
981 !warning_issued(dquots[i], warntype[i])) {
982#ifdef CONFIG_PRINT_QUOTA_WARNING
983 print_warning(dquots[i], warntype[i]);
984#endif
985#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
986 send_warning(dquots[i], warntype[i]);
987#endif
988 }
989}
990
991static inline char ignore_hardlimit(struct dquot *dquot)
992{
993 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
994
995 return capable(CAP_SYS_RESOURCE) &&
996 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
997}
998
999
1000static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
1001{
1002 *warntype = QUOTA_NL_NOWARN;
1003 if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
1004 return QUOTA_OK;
1005
1006 if (dquot->dq_dqb.dqb_ihardlimit &&
1007 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
1008 !ignore_hardlimit(dquot)) {
1009 *warntype = QUOTA_NL_IHARDWARN;
1010 return NO_QUOTA;
1011 }
1012
1013 if (dquot->dq_dqb.dqb_isoftlimit &&
1014 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1015 dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
1016 !ignore_hardlimit(dquot)) {
1017 *warntype = QUOTA_NL_ISOFTLONGWARN;
1018 return NO_QUOTA;
1019 }
1020
1021 if (dquot->dq_dqb.dqb_isoftlimit &&
1022 (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
1023 dquot->dq_dqb.dqb_itime == 0) {
1024 *warntype = QUOTA_NL_ISOFTWARN;
1025 dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1026 }
1027
1028 return QUOTA_OK;
1029}
1030
1031
1032static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
1033{
1034 *warntype = QUOTA_NL_NOWARN;
1035 if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
1036 return QUOTA_OK;
1037
1038 if (dquot->dq_dqb.dqb_bhardlimit &&
1039 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
1040 !ignore_hardlimit(dquot)) {
1041 if (!prealloc)
1042 *warntype = QUOTA_NL_BHARDWARN;
1043 return NO_QUOTA;
1044 }
1045
1046 if (dquot->dq_dqb.dqb_bsoftlimit &&
1047 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
1048 dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
1049 !ignore_hardlimit(dquot)) {
1050 if (!prealloc)
1051 *warntype = QUOTA_NL_BSOFTLONGWARN;
1052 return NO_QUOTA;
1053 }
1054
1055 if (dquot->dq_dqb.dqb_bsoftlimit &&
1056 toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
1057 dquot->dq_dqb.dqb_btime == 0) {
1058 if (!prealloc) {
1059 *warntype = QUOTA_NL_BSOFTWARN;
1060 dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1061 }
1062 else
1063
1064
1065
1066
1067 return NO_QUOTA;
1068 }
1069
1070 return QUOTA_OK;
1071}
1072
1073
1074
1075
1076
1077int dquot_initialize(struct inode *inode, int type)
1078{
1079 unsigned int id = 0;
1080 int cnt, ret = 0;
1081
1082
1083
1084 if (IS_NOQUOTA(inode))
1085 return 0;
1086 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1087
1088 if (IS_NOQUOTA(inode))
1089 goto out_err;
1090 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1091 if (type != -1 && cnt != type)
1092 continue;
1093 if (inode->i_dquot[cnt] == NODQUOT) {
1094 switch (cnt) {
1095 case USRQUOTA:
1096 id = inode->i_uid;
1097 break;
1098 case GRPQUOTA:
1099 id = inode->i_gid;
1100 break;
1101 }
1102 inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
1103 }
1104 }
1105out_err:
1106 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1107 return ret;
1108}
1109
1110
1111
1112
1113
1114int dquot_drop(struct inode *inode)
1115{
1116 int cnt;
1117
1118 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1119 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1120 if (inode->i_dquot[cnt] != NODQUOT) {
1121 dqput(inode->i_dquot[cnt]);
1122 inode->i_dquot[cnt] = NODQUOT;
1123 }
1124 }
1125 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1126 return 0;
1127}
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
1142{
1143 int cnt, ret = NO_QUOTA;
1144 char warntype[MAXQUOTAS];
1145
1146
1147
1148 if (IS_NOQUOTA(inode)) {
1149out_add:
1150 inode_add_bytes(inode, number);
1151 return QUOTA_OK;
1152 }
1153 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1154 warntype[cnt] = QUOTA_NL_NOWARN;
1155
1156 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1157 if (IS_NOQUOTA(inode)) {
1158 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1159 goto out_add;
1160 }
1161 spin_lock(&dq_data_lock);
1162 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1163 if (inode->i_dquot[cnt] == NODQUOT)
1164 continue;
1165 if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
1166 goto warn_put_all;
1167 }
1168 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1169 if (inode->i_dquot[cnt] == NODQUOT)
1170 continue;
1171 dquot_incr_space(inode->i_dquot[cnt], number);
1172 }
1173 inode_add_bytes(inode, number);
1174 ret = QUOTA_OK;
1175warn_put_all:
1176 spin_unlock(&dq_data_lock);
1177 if (ret == QUOTA_OK)
1178
1179 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1180 if (inode->i_dquot[cnt])
1181 mark_dquot_dirty(inode->i_dquot[cnt]);
1182 flush_warnings(inode->i_dquot, warntype);
1183 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1184 return ret;
1185}
1186
1187
1188
1189
1190int dquot_alloc_inode(const struct inode *inode, unsigned long number)
1191{
1192 int cnt, ret = NO_QUOTA;
1193 char warntype[MAXQUOTAS];
1194
1195
1196
1197 if (IS_NOQUOTA(inode))
1198 return QUOTA_OK;
1199 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1200 warntype[cnt] = QUOTA_NL_NOWARN;
1201 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1202 if (IS_NOQUOTA(inode)) {
1203 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1204 return QUOTA_OK;
1205 }
1206 spin_lock(&dq_data_lock);
1207 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1208 if (inode->i_dquot[cnt] == NODQUOT)
1209 continue;
1210 if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
1211 goto warn_put_all;
1212 }
1213
1214 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1215 if (inode->i_dquot[cnt] == NODQUOT)
1216 continue;
1217 dquot_incr_inodes(inode->i_dquot[cnt], number);
1218 }
1219 ret = QUOTA_OK;
1220warn_put_all:
1221 spin_unlock(&dq_data_lock);
1222 if (ret == QUOTA_OK)
1223
1224 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1225 if (inode->i_dquot[cnt])
1226 mark_dquot_dirty(inode->i_dquot[cnt]);
1227 flush_warnings(inode->i_dquot, warntype);
1228 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1229 return ret;
1230}
1231
1232
1233
1234
1235int dquot_free_space(struct inode *inode, qsize_t number)
1236{
1237 unsigned int cnt;
1238
1239
1240
1241 if (IS_NOQUOTA(inode)) {
1242out_sub:
1243 inode_sub_bytes(inode, number);
1244 return QUOTA_OK;
1245 }
1246 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1247
1248 if (IS_NOQUOTA(inode)) {
1249 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1250 goto out_sub;
1251 }
1252 spin_lock(&dq_data_lock);
1253 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1254 if (inode->i_dquot[cnt] == NODQUOT)
1255 continue;
1256 dquot_decr_space(inode->i_dquot[cnt], number);
1257 }
1258 inode_sub_bytes(inode, number);
1259 spin_unlock(&dq_data_lock);
1260
1261 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1262 if (inode->i_dquot[cnt])
1263 mark_dquot_dirty(inode->i_dquot[cnt]);
1264 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1265 return QUOTA_OK;
1266}
1267
1268
1269
1270
1271int dquot_free_inode(const struct inode *inode, unsigned long number)
1272{
1273 unsigned int cnt;
1274
1275
1276
1277 if (IS_NOQUOTA(inode))
1278 return QUOTA_OK;
1279 down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1280
1281 if (IS_NOQUOTA(inode)) {
1282 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1283 return QUOTA_OK;
1284 }
1285 spin_lock(&dq_data_lock);
1286 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1287 if (inode->i_dquot[cnt] == NODQUOT)
1288 continue;
1289 dquot_decr_inodes(inode->i_dquot[cnt], number);
1290 }
1291 spin_unlock(&dq_data_lock);
1292
1293 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1294 if (inode->i_dquot[cnt])
1295 mark_dquot_dirty(inode->i_dquot[cnt]);
1296 up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1297 return QUOTA_OK;
1298}
1299
1300
1301
1302
1303
1304
1305
1306int dquot_transfer(struct inode *inode, struct iattr *iattr)
1307{
1308 qsize_t space;
1309 struct dquot *transfer_from[MAXQUOTAS];
1310 struct dquot *transfer_to[MAXQUOTAS];
1311 int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
1312 chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
1313 char warntype[MAXQUOTAS];
1314
1315
1316
1317 if (IS_NOQUOTA(inode))
1318 return QUOTA_OK;
1319
1320 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1321 transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
1322 warntype[cnt] = QUOTA_NL_NOWARN;
1323 }
1324 down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1325
1326 if (IS_NOQUOTA(inode)) {
1327 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1328 return QUOTA_OK;
1329 }
1330
1331
1332
1333 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1334 switch (cnt) {
1335 case USRQUOTA:
1336 if (!chuid)
1337 continue;
1338 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
1339 break;
1340 case GRPQUOTA:
1341 if (!chgid)
1342 continue;
1343 transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
1344 break;
1345 }
1346 }
1347 spin_lock(&dq_data_lock);
1348 space = inode_get_bytes(inode);
1349
1350 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1351 if (transfer_to[cnt] == NODQUOT)
1352 continue;
1353 transfer_from[cnt] = inode->i_dquot[cnt];
1354 if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
1355 check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
1356 goto warn_put_all;
1357 }
1358
1359
1360
1361
1362 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1363
1364
1365
1366 if (transfer_to[cnt] == NODQUOT)
1367 continue;
1368
1369
1370 if (transfer_from[cnt]) {
1371 dquot_decr_inodes(transfer_from[cnt], 1);
1372 dquot_decr_space(transfer_from[cnt], space);
1373 }
1374
1375 dquot_incr_inodes(transfer_to[cnt], 1);
1376 dquot_incr_space(transfer_to[cnt], space);
1377
1378 inode->i_dquot[cnt] = transfer_to[cnt];
1379 }
1380 ret = QUOTA_OK;
1381warn_put_all:
1382 spin_unlock(&dq_data_lock);
1383
1384 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1385 if (transfer_from[cnt])
1386 mark_dquot_dirty(transfer_from[cnt]);
1387 if (transfer_to[cnt])
1388 mark_dquot_dirty(transfer_to[cnt]);
1389 }
1390 flush_warnings(transfer_to, warntype);
1391
1392 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1393 if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
1394 dqput(transfer_from[cnt]);
1395 if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
1396 dqput(transfer_to[cnt]);
1397 }
1398 up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1399 return ret;
1400}
1401
1402
1403
1404
1405int dquot_commit_info(struct super_block *sb, int type)
1406{
1407 int ret;
1408 struct quota_info *dqopt = sb_dqopt(sb);
1409
1410 mutex_lock(&dqopt->dqio_mutex);
1411 ret = dqopt->ops[type]->write_file_info(sb, type);
1412 mutex_unlock(&dqopt->dqio_mutex);
1413 return ret;
1414}
1415
1416
1417
1418
1419struct dquot_operations dquot_operations = {
1420 .initialize = dquot_initialize,
1421 .drop = dquot_drop,
1422 .alloc_space = dquot_alloc_space,
1423 .alloc_inode = dquot_alloc_inode,
1424 .free_space = dquot_free_space,
1425 .free_inode = dquot_free_inode,
1426 .transfer = dquot_transfer,
1427 .write_dquot = dquot_commit,
1428 .acquire_dquot = dquot_acquire,
1429 .release_dquot = dquot_release,
1430 .mark_dirty = dquot_mark_dquot_dirty,
1431 .write_info = dquot_commit_info
1432};
1433
1434static inline void set_enable_flags(struct quota_info *dqopt, int type)
1435{
1436 switch (type) {
1437 case USRQUOTA:
1438 dqopt->flags |= DQUOT_USR_ENABLED;
1439 break;
1440 case GRPQUOTA:
1441 dqopt->flags |= DQUOT_GRP_ENABLED;
1442 break;
1443 }
1444}
1445
1446static inline void reset_enable_flags(struct quota_info *dqopt, int type)
1447{
1448 switch (type) {
1449 case USRQUOTA:
1450 dqopt->flags &= ~DQUOT_USR_ENABLED;
1451 break;
1452 case GRPQUOTA:
1453 dqopt->flags &= ~DQUOT_GRP_ENABLED;
1454 break;
1455 }
1456}
1457
1458
1459
1460
1461int vfs_quota_off(struct super_block *sb, int type)
1462{
1463 int cnt;
1464 struct quota_info *dqopt = sb_dqopt(sb);
1465 struct inode *toputinode[MAXQUOTAS];
1466
1467
1468 mutex_lock(&dqopt->dqonoff_mutex);
1469 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1470 toputinode[cnt] = NULL;
1471 if (type != -1 && cnt != type)
1472 continue;
1473 if (!sb_has_quota_enabled(sb, cnt))
1474 continue;
1475 reset_enable_flags(dqopt, cnt);
1476
1477
1478 drop_dquot_ref(sb, cnt);
1479 invalidate_dquots(sb, cnt);
1480
1481
1482
1483
1484 if (info_dirty(&dqopt->info[cnt]))
1485 sb->dq_op->write_info(sb, cnt);
1486 if (dqopt->ops[cnt]->free_file_info)
1487 dqopt->ops[cnt]->free_file_info(sb, cnt);
1488 put_quota_format(dqopt->info[cnt].dqi_format);
1489
1490 toputinode[cnt] = dqopt->files[cnt];
1491 dqopt->files[cnt] = NULL;
1492 dqopt->info[cnt].dqi_flags = 0;
1493 dqopt->info[cnt].dqi_igrace = 0;
1494 dqopt->info[cnt].dqi_bgrace = 0;
1495 dqopt->ops[cnt] = NULL;
1496 }
1497 mutex_unlock(&dqopt->dqonoff_mutex);
1498
1499
1500 if (sb->s_op->sync_fs)
1501 sb->s_op->sync_fs(sb, 1);
1502 sync_blockdev(sb->s_bdev);
1503
1504
1505
1506
1507
1508 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1509 if (toputinode[cnt]) {
1510 mutex_lock(&dqopt->dqonoff_mutex);
1511
1512
1513 if (!sb_has_quota_enabled(sb, cnt)) {
1514 mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
1515 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
1516 S_NOATIME | S_NOQUOTA);
1517 truncate_inode_pages(&toputinode[cnt]->i_data, 0);
1518 mutex_unlock(&toputinode[cnt]->i_mutex);
1519 mark_inode_dirty(toputinode[cnt]);
1520 iput(toputinode[cnt]);
1521 }
1522 mutex_unlock(&dqopt->dqonoff_mutex);
1523 }
1524 if (sb->s_bdev)
1525 invalidate_bdev(sb->s_bdev);
1526 return 0;
1527}
1528
1529
1530
1531
1532
1533
1534static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
1535{
1536 struct quota_format_type *fmt = find_quota_format(format_id);
1537 struct super_block *sb = inode->i_sb;
1538 struct quota_info *dqopt = sb_dqopt(sb);
1539 int error;
1540 int oldflags = -1;
1541
1542 if (!fmt)
1543 return -ESRCH;
1544 if (!S_ISREG(inode->i_mode)) {
1545 error = -EACCES;
1546 goto out_fmt;
1547 }
1548 if (IS_RDONLY(inode)) {
1549 error = -EROFS;
1550 goto out_fmt;
1551 }
1552 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
1553 error = -EINVAL;
1554 goto out_fmt;
1555 }
1556
1557
1558
1559 write_inode_now(inode, 1);
1560
1561 invalidate_bdev(sb->s_bdev);
1562 mutex_lock(&inode->i_mutex);
1563 mutex_lock(&dqopt->dqonoff_mutex);
1564 if (sb_has_quota_enabled(sb, type)) {
1565 error = -EBUSY;
1566 goto out_lock;
1567 }
1568
1569
1570
1571 down_write(&dqopt->dqptr_sem);
1572 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
1573 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
1574 up_write(&dqopt->dqptr_sem);
1575 sb->dq_op->drop(inode);
1576
1577 error = -EIO;
1578 dqopt->files[type] = igrab(inode);
1579 if (!dqopt->files[type])
1580 goto out_lock;
1581 error = -EINVAL;
1582 if (!fmt->qf_ops->check_quota_file(sb, type))
1583 goto out_file_init;
1584
1585 dqopt->ops[type] = fmt->qf_ops;
1586 dqopt->info[type].dqi_format = fmt;
1587 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
1588 mutex_lock(&dqopt->dqio_mutex);
1589 if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
1590 mutex_unlock(&dqopt->dqio_mutex);
1591 goto out_file_init;
1592 }
1593 mutex_unlock(&dqopt->dqio_mutex);
1594 mutex_unlock(&inode->i_mutex);
1595 set_enable_flags(dqopt, type);
1596
1597 add_dquot_ref(sb, type);
1598 mutex_unlock(&dqopt->dqonoff_mutex);
1599
1600 return 0;
1601
1602out_file_init:
1603 dqopt->files[type] = NULL;
1604 iput(inode);
1605out_lock:
1606 mutex_unlock(&dqopt->dqonoff_mutex);
1607 if (oldflags != -1) {
1608 down_write(&dqopt->dqptr_sem);
1609
1610
1611 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
1612 inode->i_flags |= oldflags;
1613 up_write(&dqopt->dqptr_sem);
1614 }
1615 mutex_unlock(&inode->i_mutex);
1616out_fmt:
1617 put_quota_format(fmt);
1618
1619 return error;
1620}
1621
1622
1623int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
1624{
1625 struct nameidata nd;
1626 int error;
1627
1628 error = path_lookup(path, LOOKUP_FOLLOW, &nd);
1629 if (error < 0)
1630 return error;
1631 error = security_quota_on(nd.dentry);
1632 if (error)
1633 goto out_path;
1634
1635 if (nd.mnt->mnt_sb != sb)
1636 error = -EXDEV;
1637 else
1638 error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
1639out_path:
1640 path_release(&nd);
1641 return error;
1642}
1643
1644
1645
1646
1647
1648int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
1649 int format_id, int type)
1650{
1651 struct dentry *dentry;
1652 int error;
1653
1654 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
1655 if (IS_ERR(dentry))
1656 return PTR_ERR(dentry);
1657
1658 if (!dentry->d_inode) {
1659 error = -ENOENT;
1660 goto out;
1661 }
1662
1663 error = security_quota_on(dentry);
1664 if (!error)
1665 error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
1666
1667out:
1668 dput(dentry);
1669 return error;
1670}
1671
1672
1673static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
1674{
1675 struct mem_dqblk *dm = &dquot->dq_dqb;
1676
1677 spin_lock(&dq_data_lock);
1678 di->dqb_bhardlimit = dm->dqb_bhardlimit;
1679 di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
1680 di->dqb_curspace = dm->dqb_curspace;
1681 di->dqb_ihardlimit = dm->dqb_ihardlimit;
1682 di->dqb_isoftlimit = dm->dqb_isoftlimit;
1683 di->dqb_curinodes = dm->dqb_curinodes;
1684 di->dqb_btime = dm->dqb_btime;
1685 di->dqb_itime = dm->dqb_itime;
1686 di->dqb_valid = QIF_ALL;
1687 spin_unlock(&dq_data_lock);
1688}
1689
1690int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1691{
1692 struct dquot *dquot;
1693
1694 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1695 if (!(dquot = dqget(sb, id, type))) {
1696 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1697 return -ESRCH;
1698 }
1699 do_get_dqblk(dquot, di);
1700 dqput(dquot);
1701 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1702 return 0;
1703}
1704
1705
1706static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
1707{
1708 struct mem_dqblk *dm = &dquot->dq_dqb;
1709 int check_blim = 0, check_ilim = 0;
1710
1711 spin_lock(&dq_data_lock);
1712 if (di->dqb_valid & QIF_SPACE) {
1713 dm->dqb_curspace = di->dqb_curspace;
1714 check_blim = 1;
1715 }
1716 if (di->dqb_valid & QIF_BLIMITS) {
1717 dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
1718 dm->dqb_bhardlimit = di->dqb_bhardlimit;
1719 check_blim = 1;
1720 }
1721 if (di->dqb_valid & QIF_INODES) {
1722 dm->dqb_curinodes = di->dqb_curinodes;
1723 check_ilim = 1;
1724 }
1725 if (di->dqb_valid & QIF_ILIMITS) {
1726 dm->dqb_isoftlimit = di->dqb_isoftlimit;
1727 dm->dqb_ihardlimit = di->dqb_ihardlimit;
1728 check_ilim = 1;
1729 }
1730 if (di->dqb_valid & QIF_BTIME)
1731 dm->dqb_btime = di->dqb_btime;
1732 if (di->dqb_valid & QIF_ITIME)
1733 dm->dqb_itime = di->dqb_itime;
1734
1735 if (check_blim) {
1736 if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
1737 dm->dqb_btime = 0;
1738 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1739 }
1740 else if (!(di->dqb_valid & QIF_BTIME))
1741 dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
1742 }
1743 if (check_ilim) {
1744 if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
1745 dm->dqb_itime = 0;
1746 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1747 }
1748 else if (!(di->dqb_valid & QIF_ITIME))
1749 dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
1750 }
1751 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
1752 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
1753 else
1754 set_bit(DQ_FAKE_B, &dquot->dq_flags);
1755 spin_unlock(&dq_data_lock);
1756 mark_dquot_dirty(dquot);
1757}
1758
1759int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
1760{
1761 struct dquot *dquot;
1762
1763 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1764 if (!(dquot = dqget(sb, id, type))) {
1765 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1766 return -ESRCH;
1767 }
1768 do_set_dqblk(dquot, di);
1769 dqput(dquot);
1770 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1771 return 0;
1772}
1773
1774
1775int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1776{
1777 struct mem_dqinfo *mi;
1778
1779 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1780 if (!sb_has_quota_enabled(sb, type)) {
1781 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1782 return -ESRCH;
1783 }
1784 mi = sb_dqopt(sb)->info + type;
1785 spin_lock(&dq_data_lock);
1786 ii->dqi_bgrace = mi->dqi_bgrace;
1787 ii->dqi_igrace = mi->dqi_igrace;
1788 ii->dqi_flags = mi->dqi_flags & DQF_MASK;
1789 ii->dqi_valid = IIF_ALL;
1790 spin_unlock(&dq_data_lock);
1791 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1792 return 0;
1793}
1794
1795
1796int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
1797{
1798 struct mem_dqinfo *mi;
1799
1800 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
1801 if (!sb_has_quota_enabled(sb, type)) {
1802 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1803 return -ESRCH;
1804 }
1805 mi = sb_dqopt(sb)->info + type;
1806 spin_lock(&dq_data_lock);
1807 if (ii->dqi_valid & IIF_BGRACE)
1808 mi->dqi_bgrace = ii->dqi_bgrace;
1809 if (ii->dqi_valid & IIF_IGRACE)
1810 mi->dqi_igrace = ii->dqi_igrace;
1811 if (ii->dqi_valid & IIF_FLAGS)
1812 mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
1813 spin_unlock(&dq_data_lock);
1814 mark_info_dirty(sb, type);
1815
1816 sb->dq_op->write_info(sb, type);
1817 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
1818 return 0;
1819}
1820
1821struct quotactl_ops vfs_quotactl_ops = {
1822 .quota_on = vfs_quota_on,
1823 .quota_off = vfs_quota_off,
1824 .quota_sync = vfs_quota_sync,
1825 .get_info = vfs_get_dqinfo,
1826 .set_info = vfs_set_dqinfo,
1827 .get_dqblk = vfs_get_dqblk,
1828 .set_dqblk = vfs_set_dqblk
1829};
1830
1831static ctl_table fs_dqstats_table[] = {
1832 {
1833 .ctl_name = FS_DQ_LOOKUPS,
1834 .procname = "lookups",
1835 .data = &dqstats.lookups,
1836 .maxlen = sizeof(int),
1837 .mode = 0444,
1838 .proc_handler = &proc_dointvec,
1839 },
1840 {
1841 .ctl_name = FS_DQ_DROPS,
1842 .procname = "drops",
1843 .data = &dqstats.drops,
1844 .maxlen = sizeof(int),
1845 .mode = 0444,
1846 .proc_handler = &proc_dointvec,
1847 },
1848 {
1849 .ctl_name = FS_DQ_READS,
1850 .procname = "reads",
1851 .data = &dqstats.reads,
1852 .maxlen = sizeof(int),
1853 .mode = 0444,
1854 .proc_handler = &proc_dointvec,
1855 },
1856 {
1857 .ctl_name = FS_DQ_WRITES,
1858 .procname = "writes",
1859 .data = &dqstats.writes,
1860 .maxlen = sizeof(int),
1861 .mode = 0444,
1862 .proc_handler = &proc_dointvec,
1863 },
1864 {
1865 .ctl_name = FS_DQ_CACHE_HITS,
1866 .procname = "cache_hits",
1867 .data = &dqstats.cache_hits,
1868 .maxlen = sizeof(int),
1869 .mode = 0444,
1870 .proc_handler = &proc_dointvec,
1871 },
1872 {
1873 .ctl_name = FS_DQ_ALLOCATED,
1874 .procname = "allocated_dquots",
1875 .data = &dqstats.allocated_dquots,
1876 .maxlen = sizeof(int),
1877 .mode = 0444,
1878 .proc_handler = &proc_dointvec,
1879 },
1880 {
1881 .ctl_name = FS_DQ_FREE,
1882 .procname = "free_dquots",
1883 .data = &dqstats.free_dquots,
1884 .maxlen = sizeof(int),
1885 .mode = 0444,
1886 .proc_handler = &proc_dointvec,
1887 },
1888 {
1889 .ctl_name = FS_DQ_SYNCS,
1890 .procname = "syncs",
1891 .data = &dqstats.syncs,
1892 .maxlen = sizeof(int),
1893 .mode = 0444,
1894 .proc_handler = &proc_dointvec,
1895 },
1896#ifdef CONFIG_PRINT_QUOTA_WARNING
1897 {
1898 .ctl_name = FS_DQ_WARNINGS,
1899 .procname = "warnings",
1900 .data = &flag_print_warnings,
1901 .maxlen = sizeof(int),
1902 .mode = 0644,
1903 .proc_handler = &proc_dointvec,
1904 },
1905#endif
1906 { .ctl_name = 0 },
1907};
1908
1909static ctl_table fs_table[] = {
1910 {
1911 .ctl_name = FS_DQSTATS,
1912 .procname = "quota",
1913 .mode = 0555,
1914 .child = fs_dqstats_table,
1915 },
1916 { .ctl_name = 0 },
1917};
1918
1919static ctl_table sys_table[] = {
1920 {
1921 .ctl_name = CTL_FS,
1922 .procname = "fs",
1923 .mode = 0555,
1924 .child = fs_table,
1925 },
1926 { .ctl_name = 0 },
1927};
1928
1929static int __init dquot_init(void)
1930{
1931 int i;
1932 unsigned long nr_hash, order;
1933
1934 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
1935
1936 register_sysctl_table(sys_table);
1937
1938 dquot_cachep = kmem_cache_create("dquot",
1939 sizeof(struct dquot), sizeof(unsigned long) * 4,
1940 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1941 SLAB_MEM_SPREAD|SLAB_PANIC),
1942 NULL);
1943
1944 order = 0;
1945 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
1946 if (!dquot_hash)
1947 panic("Cannot create dquot hash table");
1948
1949
1950 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
1951 dq_hash_bits = 0;
1952 do {
1953 dq_hash_bits++;
1954 } while (nr_hash >> dq_hash_bits);
1955 dq_hash_bits--;
1956
1957 nr_hash = 1UL << dq_hash_bits;
1958 dq_hash_mask = nr_hash - 1;
1959 for (i = 0; i < nr_hash; i++)
1960 INIT_HLIST_HEAD(dquot_hash + i);
1961
1962 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
1963 nr_hash, order, (PAGE_SIZE << order));
1964
1965 register_shrinker(&dqcache_shrinker);
1966
1967#ifdef CONFIG_QUOTA_NETLINK_INTERFACE
1968 if (genl_register_family("a_genl_family) != 0)
1969 printk(KERN_ERR "VFS: Failed to create quota netlink interface.\n");
1970#endif
1971
1972 return 0;
1973}
1974module_init(dquot_init);
1975
1976EXPORT_SYMBOL(register_quota_format);
1977EXPORT_SYMBOL(unregister_quota_format);
1978EXPORT_SYMBOL(dqstats);
1979EXPORT_SYMBOL(dq_data_lock);
1980EXPORT_SYMBOL(vfs_quota_on);
1981EXPORT_SYMBOL(vfs_quota_on_mount);
1982EXPORT_SYMBOL(vfs_quota_off);
1983EXPORT_SYMBOL(vfs_quota_sync);
1984EXPORT_SYMBOL(vfs_get_dqinfo);
1985EXPORT_SYMBOL(vfs_set_dqinfo);
1986EXPORT_SYMBOL(vfs_get_dqblk);
1987EXPORT_SYMBOL(vfs_set_dqblk);
1988EXPORT_SYMBOL(dquot_commit);
1989EXPORT_SYMBOL(dquot_commit_info);
1990EXPORT_SYMBOL(dquot_acquire);
1991EXPORT_SYMBOL(dquot_release);
1992EXPORT_SYMBOL(dquot_mark_dquot_dirty);
1993EXPORT_SYMBOL(dquot_initialize);
1994EXPORT_SYMBOL(dquot_drop);
1995EXPORT_SYMBOL(dquot_alloc_space);
1996EXPORT_SYMBOL(dquot_alloc_inode);
1997EXPORT_SYMBOL(dquot_free_space);
1998EXPORT_SYMBOL(dquot_free_inode);
1999EXPORT_SYMBOL(dquot_transfer);
2000