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#include <linux/errno.h>
57#include <linux/kernel.h>
58#include <linux/fs.h>
59#include <linux/mount.h>
60#include <linux/mm.h>
61#include <linux/time.h>
62#include <linux/types.h>
63#include <linux/string.h>
64#include <linux/fcntl.h>
65#include <linux/stat.h>
66#include <linux/tty.h>
67#include <linux/file.h>
68#include <linux/slab.h>
69#include <linux/sysctl.h>
70#include <linux/init.h>
71#include <linux/module.h>
72#include <linux/proc_fs.h>
73#include <linux/security.h>
74#include <linux/sched.h>
75#include <linux/kmod.h>
76#include <linux/namei.h>
77#include <linux/capability.h>
78#include <linux/quotaops.h>
79#include "../internal.h"
80
81#include <linux/uaccess.h>
82
83
84
85
86
87
88
89
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
126static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_list_lock);
127static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_state_lock);
128__cacheline_aligned_in_smp DEFINE_SPINLOCK(dq_data_lock);
129EXPORT_SYMBOL(dq_data_lock);
130DEFINE_STATIC_SRCU(dquot_srcu);
131
132void __quota_error(struct super_block *sb, const char *func,
133 const char *fmt, ...)
134{
135 if (printk_ratelimit()) {
136 va_list args;
137 struct va_format vaf;
138
139 va_start(args, fmt);
140
141 vaf.fmt = fmt;
142 vaf.va = &args;
143
144 printk(KERN_ERR "Quota error (device %s): %s: %pV\n",
145 sb->s_id, func, &vaf);
146
147 va_end(args);
148 }
149}
150EXPORT_SYMBOL(__quota_error);
151
152#if defined(CONFIG_QUOTA_DEBUG) || defined(CONFIG_PRINT_QUOTA_WARNING)
153static char *quotatypes[] = INITQFNAMES;
154#endif
155static struct quota_format_type *quota_formats;
156static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
157
158
159static struct kmem_cache *dquot_cachep;
160
161int register_quota_format(struct quota_format_type *fmt)
162{
163 spin_lock(&dq_list_lock);
164 fmt->qf_next = quota_formats;
165 quota_formats = fmt;
166 spin_unlock(&dq_list_lock);
167 return 0;
168}
169EXPORT_SYMBOL(register_quota_format);
170
171void unregister_quota_format(struct quota_format_type *fmt)
172{
173 struct quota_format_type **actqf;
174
175 spin_lock(&dq_list_lock);
176 for (actqf = "a_formats; *actqf && *actqf != fmt;
177 actqf = &(*actqf)->qf_next)
178 ;
179 if (*actqf)
180 *actqf = (*actqf)->qf_next;
181 spin_unlock(&dq_list_lock);
182}
183EXPORT_SYMBOL(unregister_quota_format);
184
185static struct quota_format_type *find_quota_format(int id)
186{
187 struct quota_format_type *actqf;
188
189 spin_lock(&dq_list_lock);
190 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
191 actqf = actqf->qf_next)
192 ;
193 if (!actqf || !try_module_get(actqf->qf_owner)) {
194 int qm;
195
196 spin_unlock(&dq_list_lock);
197
198 for (qm = 0; module_names[qm].qm_fmt_id &&
199 module_names[qm].qm_fmt_id != id; qm++)
200 ;
201 if (!module_names[qm].qm_fmt_id ||
202 request_module(module_names[qm].qm_mod_name))
203 return NULL;
204
205 spin_lock(&dq_list_lock);
206 for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id;
207 actqf = actqf->qf_next)
208 ;
209 if (actqf && !try_module_get(actqf->qf_owner))
210 actqf = NULL;
211 }
212 spin_unlock(&dq_list_lock);
213 return actqf;
214}
215
216static void put_quota_format(struct quota_format_type *fmt)
217{
218 module_put(fmt->qf_owner);
219}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241static LIST_HEAD(inuse_list);
242static LIST_HEAD(free_dquots);
243static unsigned int dq_hash_bits, dq_hash_mask;
244static struct hlist_head *dquot_hash;
245
246struct dqstats dqstats;
247EXPORT_SYMBOL(dqstats);
248
249static qsize_t inode_get_rsv_space(struct inode *inode);
250static void __dquot_initialize(struct inode *inode, int type);
251
252static inline unsigned int
253hashfn(const struct super_block *sb, struct kqid qid)
254{
255 unsigned int id = from_kqid(&init_user_ns, qid);
256 int type = qid.type;
257 unsigned long tmp;
258
259 tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
260 return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
261}
262
263
264
265
266static inline void insert_dquot_hash(struct dquot *dquot)
267{
268 struct hlist_head *head;
269 head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id);
270 hlist_add_head(&dquot->dq_hash, head);
271}
272
273static inline void remove_dquot_hash(struct dquot *dquot)
274{
275 hlist_del_init(&dquot->dq_hash);
276}
277
278static struct dquot *find_dquot(unsigned int hashent, struct super_block *sb,
279 struct kqid qid)
280{
281 struct hlist_node *node;
282 struct dquot *dquot;
283
284 hlist_for_each (node, dquot_hash+hashent) {
285 dquot = hlist_entry(node, struct dquot, dq_hash);
286 if (dquot->dq_sb == sb && qid_eq(dquot->dq_id, qid))
287 return dquot;
288 }
289 return NULL;
290}
291
292
293static inline void put_dquot_last(struct dquot *dquot)
294{
295 list_add_tail(&dquot->dq_free, &free_dquots);
296 dqstats_inc(DQST_FREE_DQUOTS);
297}
298
299static inline void remove_free_dquot(struct dquot *dquot)
300{
301 if (list_empty(&dquot->dq_free))
302 return;
303 list_del_init(&dquot->dq_free);
304 dqstats_dec(DQST_FREE_DQUOTS);
305}
306
307static inline void put_inuse(struct dquot *dquot)
308{
309
310
311 list_add_tail(&dquot->dq_inuse, &inuse_list);
312 dqstats_inc(DQST_ALLOC_DQUOTS);
313}
314
315static inline void remove_inuse(struct dquot *dquot)
316{
317 dqstats_dec(DQST_ALLOC_DQUOTS);
318 list_del(&dquot->dq_inuse);
319}
320
321
322
323
324static void wait_on_dquot(struct dquot *dquot)
325{
326 mutex_lock(&dquot->dq_lock);
327 mutex_unlock(&dquot->dq_lock);
328}
329
330static inline int dquot_dirty(struct dquot *dquot)
331{
332 return test_bit(DQ_MOD_B, &dquot->dq_flags);
333}
334
335static inline int mark_dquot_dirty(struct dquot *dquot)
336{
337 return dquot->dq_sb->dq_op->mark_dirty(dquot);
338}
339
340
341int dquot_mark_dquot_dirty(struct dquot *dquot)
342{
343 int ret = 1;
344
345
346 if (test_bit(DQ_MOD_B, &dquot->dq_flags))
347 return 1;
348
349 spin_lock(&dq_list_lock);
350 if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags)) {
351 list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
352 info[dquot->dq_id.type].dqi_dirty_list);
353 ret = 0;
354 }
355 spin_unlock(&dq_list_lock);
356 return ret;
357}
358EXPORT_SYMBOL(dquot_mark_dquot_dirty);
359
360
361static inline int mark_all_dquot_dirty(struct dquot * const *dquot)
362{
363 int ret, err, cnt;
364
365 ret = err = 0;
366 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
367 if (dquot[cnt])
368
369 ret = mark_dquot_dirty(dquot[cnt]);
370 if (!err)
371 err = ret;
372 }
373 return err;
374}
375
376static inline void dqput_all(struct dquot **dquot)
377{
378 unsigned int cnt;
379
380 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
381 dqput(dquot[cnt]);
382}
383
384
385static inline int clear_dquot_dirty(struct dquot *dquot)
386{
387 if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
388 return 0;
389 list_del_init(&dquot->dq_dirty);
390 return 1;
391}
392
393void mark_info_dirty(struct super_block *sb, int type)
394{
395 set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
396}
397EXPORT_SYMBOL(mark_info_dirty);
398
399
400
401
402
403int dquot_acquire(struct dquot *dquot)
404{
405 int ret = 0, ret2 = 0;
406 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
407
408 mutex_lock(&dquot->dq_lock);
409 mutex_lock(&dqopt->dqio_mutex);
410 if (!test_bit(DQ_READ_B, &dquot->dq_flags))
411 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
412 if (ret < 0)
413 goto out_iolock;
414 set_bit(DQ_READ_B, &dquot->dq_flags);
415
416 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
417 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
418
419 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
420 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
421 dquot->dq_sb, dquot->dq_id.type);
422 }
423 if (ret < 0)
424 goto out_iolock;
425 if (ret2 < 0) {
426 ret = ret2;
427 goto out_iolock;
428 }
429 }
430 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
431out_iolock:
432 mutex_unlock(&dqopt->dqio_mutex);
433 mutex_unlock(&dquot->dq_lock);
434 return ret;
435}
436EXPORT_SYMBOL(dquot_acquire);
437
438
439
440
441int dquot_commit(struct dquot *dquot)
442{
443 int ret = 0;
444 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
445
446 mutex_lock(&dqopt->dqio_mutex);
447 spin_lock(&dq_list_lock);
448 if (!clear_dquot_dirty(dquot)) {
449 spin_unlock(&dq_list_lock);
450 goto out_sem;
451 }
452 spin_unlock(&dq_list_lock);
453
454
455 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
456 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
457 else
458 ret = -EIO;
459out_sem:
460 mutex_unlock(&dqopt->dqio_mutex);
461 return ret;
462}
463EXPORT_SYMBOL(dquot_commit);
464
465
466
467
468int dquot_release(struct dquot *dquot)
469{
470 int ret = 0, ret2 = 0;
471 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
472
473 mutex_lock(&dquot->dq_lock);
474
475 if (atomic_read(&dquot->dq_count) > 1)
476 goto out_dqlock;
477 mutex_lock(&dqopt->dqio_mutex);
478 if (dqopt->ops[dquot->dq_id.type]->release_dqblk) {
479 ret = dqopt->ops[dquot->dq_id.type]->release_dqblk(dquot);
480
481 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
482 ret2 = dqopt->ops[dquot->dq_id.type]->write_file_info(
483 dquot->dq_sb, dquot->dq_id.type);
484 }
485 if (ret >= 0)
486 ret = ret2;
487 }
488 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
489 mutex_unlock(&dqopt->dqio_mutex);
490out_dqlock:
491 mutex_unlock(&dquot->dq_lock);
492 return ret;
493}
494EXPORT_SYMBOL(dquot_release);
495
496void dquot_destroy(struct dquot *dquot)
497{
498 kmem_cache_free(dquot_cachep, dquot);
499}
500EXPORT_SYMBOL(dquot_destroy);
501
502static inline void do_destroy_dquot(struct dquot *dquot)
503{
504 dquot->dq_sb->dq_op->destroy_dquot(dquot);
505}
506
507
508
509
510
511
512
513static void invalidate_dquots(struct super_block *sb, int type)
514{
515 struct dquot *dquot, *tmp;
516
517restart:
518 spin_lock(&dq_list_lock);
519 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
520 if (dquot->dq_sb != sb)
521 continue;
522 if (dquot->dq_id.type != type)
523 continue;
524
525 if (atomic_read(&dquot->dq_count)) {
526 DEFINE_WAIT(wait);
527
528 atomic_inc(&dquot->dq_count);
529 prepare_to_wait(&dquot->dq_wait_unused, &wait,
530 TASK_UNINTERRUPTIBLE);
531 spin_unlock(&dq_list_lock);
532
533
534
535
536
537
538
539 if (atomic_read(&dquot->dq_count) > 1)
540 schedule();
541 finish_wait(&dquot->dq_wait_unused, &wait);
542 dqput(dquot);
543
544
545
546 goto restart;
547 }
548
549
550
551
552 remove_dquot_hash(dquot);
553 remove_free_dquot(dquot);
554 remove_inuse(dquot);
555 do_destroy_dquot(dquot);
556 }
557 spin_unlock(&dq_list_lock);
558}
559
560
561int dquot_scan_active(struct super_block *sb,
562 int (*fn)(struct dquot *dquot, unsigned long priv),
563 unsigned long priv)
564{
565 struct dquot *dquot, *old_dquot = NULL;
566 int ret = 0;
567
568 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
569 spin_lock(&dq_list_lock);
570 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
571 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
572 continue;
573 if (dquot->dq_sb != sb)
574 continue;
575
576 atomic_inc(&dquot->dq_count);
577 spin_unlock(&dq_list_lock);
578 dqstats_inc(DQST_LOOKUPS);
579 dqput(old_dquot);
580 old_dquot = dquot;
581
582
583
584
585
586 wait_on_dquot(dquot);
587 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
588 ret = fn(dquot, priv);
589 if (ret < 0)
590 goto out;
591 }
592 spin_lock(&dq_list_lock);
593
594
595 }
596 spin_unlock(&dq_list_lock);
597out:
598 dqput(old_dquot);
599 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
600 return ret;
601}
602EXPORT_SYMBOL(dquot_scan_active);
603
604
605int dquot_writeback_dquots(struct super_block *sb, int type)
606{
607 struct list_head *dirty;
608 struct dquot *dquot;
609 struct quota_info *dqopt = sb_dqopt(sb);
610 int cnt;
611 int err, ret = 0;
612
613 mutex_lock(&dqopt->dqonoff_mutex);
614 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
615 if (type != -1 && cnt != type)
616 continue;
617 if (!sb_has_quota_active(sb, cnt))
618 continue;
619 spin_lock(&dq_list_lock);
620 dirty = &dqopt->info[cnt].dqi_dirty_list;
621 while (!list_empty(dirty)) {
622 dquot = list_first_entry(dirty, struct dquot,
623 dq_dirty);
624
625 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
626 clear_dquot_dirty(dquot);
627 continue;
628 }
629
630
631
632 atomic_inc(&dquot->dq_count);
633 spin_unlock(&dq_list_lock);
634 dqstats_inc(DQST_LOOKUPS);
635 err = sb->dq_op->write_dquot(dquot);
636 if (!ret && err)
637 ret = err;
638 dqput(dquot);
639 spin_lock(&dq_list_lock);
640 }
641 spin_unlock(&dq_list_lock);
642 }
643
644 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
645 if ((cnt == type || type == -1) && sb_has_quota_active(sb, cnt)
646 && info_dirty(&dqopt->info[cnt]))
647 sb->dq_op->write_info(sb, cnt);
648 dqstats_inc(DQST_SYNCS);
649 mutex_unlock(&dqopt->dqonoff_mutex);
650
651 return ret;
652}
653EXPORT_SYMBOL(dquot_writeback_dquots);
654
655
656int dquot_quota_sync(struct super_block *sb, int type)
657{
658 struct quota_info *dqopt = sb_dqopt(sb);
659 int cnt;
660 int ret;
661
662 ret = dquot_writeback_dquots(sb, type);
663 if (ret)
664 return ret;
665 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
666 return 0;
667
668
669
670
671 if (sb->s_op->sync_fs)
672 sb->s_op->sync_fs(sb, 1);
673 sync_blockdev(sb->s_bdev);
674
675
676
677
678
679 mutex_lock(&dqopt->dqonoff_mutex);
680 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
681 if (type != -1 && cnt != type)
682 continue;
683 if (!sb_has_quota_active(sb, cnt))
684 continue;
685 mutex_lock(&dqopt->files[cnt]->i_mutex);
686 truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
687 mutex_unlock(&dqopt->files[cnt]->i_mutex);
688 }
689 mutex_unlock(&dqopt->dqonoff_mutex);
690
691 return 0;
692}
693EXPORT_SYMBOL(dquot_quota_sync);
694
695
696static void prune_dqcache(int count)
697{
698 struct list_head *head;
699 struct dquot *dquot;
700
701 head = free_dquots.prev;
702 while (head != &free_dquots && count) {
703 dquot = list_entry(head, struct dquot, dq_free);
704 remove_dquot_hash(dquot);
705 remove_free_dquot(dquot);
706 remove_inuse(dquot);
707 do_destroy_dquot(dquot);
708 count--;
709 head = free_dquots.prev;
710 }
711}
712
713
714
715
716
717static int shrink_dqcache_memory(struct shrinker *shrink,
718 struct shrink_control *sc)
719{
720 int nr = sc->nr_to_scan;
721
722 if (nr) {
723 spin_lock(&dq_list_lock);
724 prune_dqcache(nr);
725 spin_unlock(&dq_list_lock);
726 }
727 return ((unsigned)
728 percpu_counter_read_positive(&dqstats.counter[DQST_FREE_DQUOTS])
729 /100) * sysctl_vfs_cache_pressure;
730}
731
732static struct shrinker dqcache_shrinker = {
733 .shrink = shrink_dqcache_memory,
734 .seeks = DEFAULT_SEEKS,
735};
736
737
738
739
740void dqput(struct dquot *dquot)
741{
742 int ret;
743
744 if (!dquot)
745 return;
746#ifdef CONFIG_QUOTA_DEBUG
747 if (!atomic_read(&dquot->dq_count)) {
748 quota_error(dquot->dq_sb, "trying to free free dquot of %s %d",
749 quotatypes[dquot->dq_id.type],
750 from_kqid(&init_user_ns, dquot->dq_id));
751 BUG();
752 }
753#endif
754 dqstats_inc(DQST_DROPS);
755we_slept:
756 spin_lock(&dq_list_lock);
757 if (atomic_read(&dquot->dq_count) > 1) {
758
759 atomic_dec(&dquot->dq_count);
760
761 if (!sb_has_quota_active(dquot->dq_sb, dquot->dq_id.type) &&
762 atomic_read(&dquot->dq_count) == 1)
763 wake_up(&dquot->dq_wait_unused);
764 spin_unlock(&dq_list_lock);
765 return;
766 }
767
768 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
769 spin_unlock(&dq_list_lock);
770
771 ret = dquot->dq_sb->dq_op->write_dquot(dquot);
772 if (ret < 0) {
773 quota_error(dquot->dq_sb, "Can't write quota structure"
774 " (error %d). Quota may get out of sync!",
775 ret);
776
777
778
779
780 spin_lock(&dq_list_lock);
781 clear_dquot_dirty(dquot);
782 spin_unlock(&dq_list_lock);
783 }
784 goto we_slept;
785 }
786
787 clear_dquot_dirty(dquot);
788 if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
789 spin_unlock(&dq_list_lock);
790 dquot->dq_sb->dq_op->release_dquot(dquot);
791 goto we_slept;
792 }
793 atomic_dec(&dquot->dq_count);
794#ifdef CONFIG_QUOTA_DEBUG
795
796 BUG_ON(!list_empty(&dquot->dq_free));
797#endif
798 put_dquot_last(dquot);
799 spin_unlock(&dq_list_lock);
800}
801EXPORT_SYMBOL(dqput);
802
803struct dquot *dquot_alloc(struct super_block *sb, int type)
804{
805 return kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
806}
807EXPORT_SYMBOL(dquot_alloc);
808
809static struct dquot *get_empty_dquot(struct super_block *sb, int type)
810{
811 struct dquot *dquot;
812
813 dquot = sb->dq_op->alloc_dquot(sb, type);
814 if(!dquot)
815 return NULL;
816
817 mutex_init(&dquot->dq_lock);
818 INIT_LIST_HEAD(&dquot->dq_free);
819 INIT_LIST_HEAD(&dquot->dq_inuse);
820 INIT_HLIST_NODE(&dquot->dq_hash);
821 INIT_LIST_HEAD(&dquot->dq_dirty);
822 init_waitqueue_head(&dquot->dq_wait_unused);
823 dquot->dq_sb = sb;
824 dquot->dq_id = make_kqid_invalid(type);
825 atomic_set(&dquot->dq_count, 1);
826
827 return dquot;
828}
829
830
831
832
833
834
835
836
837
838struct dquot *dqget(struct super_block *sb, struct kqid qid)
839{
840 unsigned int hashent = hashfn(sb, qid);
841 struct dquot *dquot = NULL, *empty = NULL;
842
843 if (!qid_has_mapping(sb->s_user_ns, qid))
844 return ERR_PTR(-EINVAL);
845
846 if (!sb_has_quota_active(sb, qid.type))
847 return NULL;
848we_slept:
849 spin_lock(&dq_list_lock);
850 spin_lock(&dq_state_lock);
851 if (!sb_has_quota_active(sb, qid.type)) {
852 spin_unlock(&dq_state_lock);
853 spin_unlock(&dq_list_lock);
854 goto out;
855 }
856 spin_unlock(&dq_state_lock);
857
858 dquot = find_dquot(hashent, sb, qid);
859 if (!dquot) {
860 if (!empty) {
861 spin_unlock(&dq_list_lock);
862 empty = get_empty_dquot(sb, qid.type);
863 if (!empty)
864 schedule();
865 goto we_slept;
866 }
867 dquot = empty;
868 empty = NULL;
869 dquot->dq_id = qid;
870
871 put_inuse(dquot);
872
873 insert_dquot_hash(dquot);
874 spin_unlock(&dq_list_lock);
875 dqstats_inc(DQST_LOOKUPS);
876 } else {
877 if (!atomic_read(&dquot->dq_count))
878 remove_free_dquot(dquot);
879 atomic_inc(&dquot->dq_count);
880 spin_unlock(&dq_list_lock);
881 dqstats_inc(DQST_CACHE_HITS);
882 dqstats_inc(DQST_LOOKUPS);
883 }
884
885
886 wait_on_dquot(dquot);
887
888 if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) &&
889 sb->dq_op->acquire_dquot(dquot) < 0) {
890 dqput(dquot);
891 dquot = NULL;
892 goto out;
893 }
894#ifdef CONFIG_QUOTA_DEBUG
895 BUG_ON(!dquot->dq_sb);
896#endif
897out:
898 if (empty)
899 do_destroy_dquot(empty);
900
901 return dquot;
902}
903EXPORT_SYMBOL(dqget);
904
905static int dqinit_needed(struct inode *inode, int type)
906{
907 int cnt;
908
909 if (IS_NOQUOTA(inode))
910 return 0;
911 if (type != -1)
912 return !inode->i_dquot[type];
913 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
914 if (!inode->i_dquot[cnt])
915 return 1;
916 return 0;
917}
918
919
920static void add_dquot_ref(struct super_block *sb, int type)
921{
922 struct inode *inode, *old_inode = NULL;
923#ifdef CONFIG_QUOTA_DEBUG
924 int reserved = 0;
925#endif
926
927 spin_lock(&sb->s_inode_list_lock);
928 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
929 spin_lock(&inode->i_lock);
930 if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) ||
931 !atomic_read(&inode->i_writecount) ||
932 !dqinit_needed(inode, type)) {
933 spin_unlock(&inode->i_lock);
934 continue;
935 }
936 __iget(inode);
937 spin_unlock(&inode->i_lock);
938 spin_unlock(&sb->s_inode_list_lock);
939
940#ifdef CONFIG_QUOTA_DEBUG
941 if (unlikely(inode_get_rsv_space(inode) > 0))
942 reserved = 1;
943#endif
944 iput(old_inode);
945 __dquot_initialize(inode, type);
946
947
948
949
950
951
952
953
954
955 old_inode = inode;
956 spin_lock(&sb->s_inode_list_lock);
957 }
958 spin_unlock(&sb->s_inode_list_lock);
959 iput(old_inode);
960
961#ifdef CONFIG_QUOTA_DEBUG
962 if (reserved) {
963 quota_error(sb, "Writes happened before quota was turned on "
964 "thus quota information is probably inconsistent. "
965 "Please run quotacheck(8)");
966 }
967#endif
968}
969
970
971
972
973
974static void remove_inode_dquot_ref(struct inode *inode, int type,
975 struct list_head *tofree_head)
976{
977 struct dquot *dquot = inode->i_dquot[type];
978
979 inode->i_dquot[type] = NULL;
980 if (!dquot)
981 return;
982
983 if (list_empty(&dquot->dq_free)) {
984
985
986
987
988 spin_lock(&dq_list_lock);
989 list_add(&dquot->dq_free, tofree_head);
990 spin_unlock(&dq_list_lock);
991 } else {
992
993
994
995
996 dqput(dquot);
997 }
998}
999
1000
1001
1002
1003
1004
1005static void put_dquot_list(struct list_head *tofree_head)
1006{
1007 struct list_head *act_head;
1008 struct dquot *dquot;
1009
1010 act_head = tofree_head->next;
1011 while (act_head != tofree_head) {
1012 dquot = list_entry(act_head, struct dquot, dq_free);
1013 act_head = act_head->next;
1014
1015 list_del_init(&dquot->dq_free);
1016 dqput(dquot);
1017 }
1018}
1019
1020static void remove_dquot_ref(struct super_block *sb, int type,
1021 struct list_head *tofree_head)
1022{
1023 struct inode *inode;
1024 int reserved = 0;
1025
1026 spin_lock(&sb->s_inode_list_lock);
1027 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
1028
1029
1030
1031
1032
1033
1034 spin_lock(&dq_data_lock);
1035 if (!IS_NOQUOTA(inode)) {
1036 if (unlikely(inode_get_rsv_space(inode) > 0))
1037 reserved = 1;
1038 remove_inode_dquot_ref(inode, type, tofree_head);
1039 }
1040 spin_unlock(&dq_data_lock);
1041 }
1042 spin_unlock(&sb->s_inode_list_lock);
1043#ifdef CONFIG_QUOTA_DEBUG
1044 if (reserved) {
1045 printk(KERN_WARNING "VFS (%s): Writes happened after quota"
1046 " was disabled thus quota information is probably "
1047 "inconsistent. Please run quotacheck(8).\n", sb->s_id);
1048 }
1049#endif
1050}
1051
1052
1053static void drop_dquot_ref(struct super_block *sb, int type)
1054{
1055 LIST_HEAD(tofree_head);
1056
1057 if (sb->dq_op) {
1058 remove_dquot_ref(sb, type, &tofree_head);
1059 synchronize_srcu(&dquot_srcu);
1060 put_dquot_list(&tofree_head);
1061 }
1062}
1063
1064static inline void dquot_incr_inodes(struct dquot *dquot, qsize_t number)
1065{
1066 dquot->dq_dqb.dqb_curinodes += number;
1067}
1068
1069static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
1070{
1071 dquot->dq_dqb.dqb_curspace += number;
1072}
1073
1074static inline void dquot_resv_space(struct dquot *dquot, qsize_t number)
1075{
1076 dquot->dq_dqb.dqb_rsvspace += number;
1077}
1078
1079
1080
1081
1082static void dquot_claim_reserved_space(struct dquot *dquot, qsize_t number)
1083{
1084 if (dquot->dq_dqb.dqb_rsvspace < number) {
1085 WARN_ON_ONCE(1);
1086 number = dquot->dq_dqb.dqb_rsvspace;
1087 }
1088 dquot->dq_dqb.dqb_curspace += number;
1089 dquot->dq_dqb.dqb_rsvspace -= number;
1090}
1091
1092static void dquot_reclaim_reserved_space(struct dquot *dquot, qsize_t number)
1093{
1094 if (WARN_ON_ONCE(dquot->dq_dqb.dqb_curspace < number))
1095 number = dquot->dq_dqb.dqb_curspace;
1096 dquot->dq_dqb.dqb_rsvspace += number;
1097 dquot->dq_dqb.dqb_curspace -= number;
1098}
1099
1100static inline
1101void dquot_free_reserved_space(struct dquot *dquot, qsize_t number)
1102{
1103 if (dquot->dq_dqb.dqb_rsvspace >= number)
1104 dquot->dq_dqb.dqb_rsvspace -= number;
1105 else {
1106 WARN_ON_ONCE(1);
1107 dquot->dq_dqb.dqb_rsvspace = 0;
1108 }
1109}
1110
1111static void dquot_decr_inodes(struct dquot *dquot, qsize_t number)
1112{
1113 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1114 dquot->dq_dqb.dqb_curinodes >= number)
1115 dquot->dq_dqb.dqb_curinodes -= number;
1116 else
1117 dquot->dq_dqb.dqb_curinodes = 0;
1118 if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
1119 dquot->dq_dqb.dqb_itime = (time_t) 0;
1120 clear_bit(DQ_INODES_B, &dquot->dq_flags);
1121}
1122
1123static void dquot_decr_space(struct dquot *dquot, qsize_t number)
1124{
1125 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NEGATIVE_USAGE ||
1126 dquot->dq_dqb.dqb_curspace >= number)
1127 dquot->dq_dqb.dqb_curspace -= number;
1128 else
1129 dquot->dq_dqb.dqb_curspace = 0;
1130 if (dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1131 dquot->dq_dqb.dqb_btime = (time_t) 0;
1132 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
1133}
1134
1135struct dquot_warn {
1136 struct super_block *w_sb;
1137 struct kqid w_dq_id;
1138 short w_type;
1139};
1140
1141static int warning_issued(struct dquot *dquot, const int warntype)
1142{
1143 int flag = (warntype == QUOTA_NL_BHARDWARN ||
1144 warntype == QUOTA_NL_BSOFTLONGWARN) ? DQ_BLKS_B :
1145 ((warntype == QUOTA_NL_IHARDWARN ||
1146 warntype == QUOTA_NL_ISOFTLONGWARN) ? DQ_INODES_B : 0);
1147
1148 if (!flag)
1149 return 0;
1150 return test_and_set_bit(flag, &dquot->dq_flags);
1151}
1152
1153#ifdef CONFIG_PRINT_QUOTA_WARNING
1154static int flag_print_warnings = 1;
1155
1156static int need_print_warning(struct dquot_warn *warn)
1157{
1158 if (!flag_print_warnings)
1159 return 0;
1160
1161 switch (warn->w_dq_id.type) {
1162 case USRQUOTA:
1163 return uid_eq(current_fsuid(), warn->w_dq_id.uid);
1164 case GRPQUOTA:
1165 return in_group_p(warn->w_dq_id.gid);
1166 case PRJQUOTA:
1167 return 0;
1168 }
1169 return 0;
1170}
1171
1172
1173static void print_warning(struct dquot_warn *warn)
1174{
1175 char *msg = NULL;
1176 struct tty_struct *tty;
1177 int warntype = warn->w_type;
1178
1179 if (warntype == QUOTA_NL_IHARDBELOW ||
1180 warntype == QUOTA_NL_ISOFTBELOW ||
1181 warntype == QUOTA_NL_BHARDBELOW ||
1182 warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(warn))
1183 return;
1184
1185 tty = get_current_tty();
1186 if (!tty)
1187 return;
1188 tty_write_message(tty, warn->w_sb->s_id);
1189 if (warntype == QUOTA_NL_ISOFTWARN || warntype == QUOTA_NL_BSOFTWARN)
1190 tty_write_message(tty, ": warning, ");
1191 else
1192 tty_write_message(tty, ": write failed, ");
1193 tty_write_message(tty, quotatypes[warn->w_dq_id.type]);
1194 switch (warntype) {
1195 case QUOTA_NL_IHARDWARN:
1196 msg = " file limit reached.\r\n";
1197 break;
1198 case QUOTA_NL_ISOFTLONGWARN:
1199 msg = " file quota exceeded too long.\r\n";
1200 break;
1201 case QUOTA_NL_ISOFTWARN:
1202 msg = " file quota exceeded.\r\n";
1203 break;
1204 case QUOTA_NL_BHARDWARN:
1205 msg = " block limit reached.\r\n";
1206 break;
1207 case QUOTA_NL_BSOFTLONGWARN:
1208 msg = " block quota exceeded too long.\r\n";
1209 break;
1210 case QUOTA_NL_BSOFTWARN:
1211 msg = " block quota exceeded.\r\n";
1212 break;
1213 }
1214 tty_write_message(tty, msg);
1215 tty_kref_put(tty);
1216}
1217#endif
1218
1219static void prepare_warning(struct dquot_warn *warn, struct dquot *dquot,
1220 int warntype)
1221{
1222 if (warning_issued(dquot, warntype))
1223 return;
1224 warn->w_type = warntype;
1225 warn->w_sb = dquot->dq_sb;
1226 warn->w_dq_id = dquot->dq_id;
1227}
1228
1229
1230
1231
1232
1233
1234static void flush_warnings(struct dquot_warn *warn)
1235{
1236 int i;
1237
1238 for (i = 0; i < MAXQUOTAS; i++) {
1239 if (warn[i].w_type == QUOTA_NL_NOWARN)
1240 continue;
1241#ifdef CONFIG_PRINT_QUOTA_WARNING
1242 print_warning(&warn[i]);
1243#endif
1244 quota_send_warning(warn[i].w_dq_id,
1245 warn[i].w_sb->s_dev, warn[i].w_type);
1246 }
1247}
1248
1249static int ignore_hardlimit(struct dquot *dquot)
1250{
1251 struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
1252
1253 return capable(CAP_SYS_RESOURCE) &&
1254 (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD ||
1255 !(info->dqi_flags & V1_DQF_RSQUASH));
1256}
1257
1258
1259static int check_idq(struct dquot *dquot, qsize_t inodes,
1260 struct dquot_warn *warn)
1261{
1262 qsize_t newinodes = dquot->dq_dqb.dqb_curinodes + inodes;
1263
1264 if (!sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type) ||
1265 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1266 return 0;
1267
1268 if (dquot->dq_dqb.dqb_ihardlimit &&
1269 newinodes > dquot->dq_dqb.dqb_ihardlimit &&
1270 !ignore_hardlimit(dquot)) {
1271 prepare_warning(warn, dquot, QUOTA_NL_IHARDWARN);
1272 return -EDQUOT;
1273 }
1274
1275 if (dquot->dq_dqb.dqb_isoftlimit &&
1276 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1277 dquot->dq_dqb.dqb_itime &&
1278 get_seconds() >= dquot->dq_dqb.dqb_itime &&
1279 !ignore_hardlimit(dquot)) {
1280 prepare_warning(warn, dquot, QUOTA_NL_ISOFTLONGWARN);
1281 return -EDQUOT;
1282 }
1283
1284 if (dquot->dq_dqb.dqb_isoftlimit &&
1285 newinodes > dquot->dq_dqb.dqb_isoftlimit &&
1286 dquot->dq_dqb.dqb_itime == 0) {
1287 prepare_warning(warn, dquot, QUOTA_NL_ISOFTWARN);
1288 dquot->dq_dqb.dqb_itime = get_seconds() +
1289 sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type].dqi_igrace;
1290 }
1291
1292 return 0;
1293}
1294
1295
1296static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc,
1297 struct dquot_warn *warn)
1298{
1299 qsize_t tspace;
1300 struct super_block *sb = dquot->dq_sb;
1301
1302 if (!sb_has_quota_limits_enabled(sb, dquot->dq_id.type) ||
1303 test_bit(DQ_FAKE_B, &dquot->dq_flags))
1304 return 0;
1305
1306 tspace = dquot->dq_dqb.dqb_curspace + dquot->dq_dqb.dqb_rsvspace
1307 + space;
1308
1309 if (dquot->dq_dqb.dqb_bhardlimit &&
1310 tspace > dquot->dq_dqb.dqb_bhardlimit &&
1311 !ignore_hardlimit(dquot)) {
1312 if (!prealloc)
1313 prepare_warning(warn, dquot, QUOTA_NL_BHARDWARN);
1314 return -EDQUOT;
1315 }
1316
1317 if (dquot->dq_dqb.dqb_bsoftlimit &&
1318 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1319 dquot->dq_dqb.dqb_btime &&
1320 get_seconds() >= dquot->dq_dqb.dqb_btime &&
1321 !ignore_hardlimit(dquot)) {
1322 if (!prealloc)
1323 prepare_warning(warn, dquot, QUOTA_NL_BSOFTLONGWARN);
1324 return -EDQUOT;
1325 }
1326
1327 if (dquot->dq_dqb.dqb_bsoftlimit &&
1328 tspace > dquot->dq_dqb.dqb_bsoftlimit &&
1329 dquot->dq_dqb.dqb_btime == 0) {
1330 if (!prealloc) {
1331 prepare_warning(warn, dquot, QUOTA_NL_BSOFTWARN);
1332 dquot->dq_dqb.dqb_btime = get_seconds() +
1333 sb_dqopt(sb)->info[dquot->dq_id.type].dqi_bgrace;
1334 }
1335 else
1336
1337
1338
1339
1340 return -EDQUOT;
1341 }
1342
1343 return 0;
1344}
1345
1346static int info_idq_free(struct dquot *dquot, qsize_t inodes)
1347{
1348 qsize_t newinodes;
1349
1350 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1351 dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit ||
1352 !sb_has_quota_limits_enabled(dquot->dq_sb, dquot->dq_id.type))
1353 return QUOTA_NL_NOWARN;
1354
1355 newinodes = dquot->dq_dqb.dqb_curinodes - inodes;
1356 if (newinodes <= dquot->dq_dqb.dqb_isoftlimit)
1357 return QUOTA_NL_ISOFTBELOW;
1358 if (dquot->dq_dqb.dqb_curinodes >= dquot->dq_dqb.dqb_ihardlimit &&
1359 newinodes < dquot->dq_dqb.dqb_ihardlimit)
1360 return QUOTA_NL_IHARDBELOW;
1361 return QUOTA_NL_NOWARN;
1362}
1363
1364static int info_bdq_free(struct dquot *dquot, qsize_t space)
1365{
1366 if (test_bit(DQ_FAKE_B, &dquot->dq_flags) ||
1367 dquot->dq_dqb.dqb_curspace <= dquot->dq_dqb.dqb_bsoftlimit)
1368 return QUOTA_NL_NOWARN;
1369
1370 if (dquot->dq_dqb.dqb_curspace - space <= dquot->dq_dqb.dqb_bsoftlimit)
1371 return QUOTA_NL_BSOFTBELOW;
1372 if (dquot->dq_dqb.dqb_curspace >= dquot->dq_dqb.dqb_bhardlimit &&
1373 dquot->dq_dqb.dqb_curspace - space < dquot->dq_dqb.dqb_bhardlimit)
1374 return QUOTA_NL_BHARDBELOW;
1375 return QUOTA_NL_NOWARN;
1376}
1377
1378static int dquot_active(const struct inode *inode)
1379{
1380 struct super_block *sb = inode->i_sb;
1381
1382 if (IS_NOQUOTA(inode))
1383 return 0;
1384 return sb_any_quota_loaded(sb) & ~sb_any_quota_suspended(sb);
1385}
1386
1387
1388
1389
1390
1391
1392
1393static void __dquot_initialize(struct inode *inode, int type)
1394{
1395 int cnt, init_needed = 0;
1396 struct dquot *got[MAXQUOTAS];
1397 struct super_block *sb = inode->i_sb;
1398 qsize_t rsv;
1399
1400 if (!dquot_active(inode))
1401 return;
1402
1403
1404 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1405 struct kqid qid;
1406 got[cnt] = NULL;
1407 if (type != -1 && cnt != type)
1408 continue;
1409
1410
1411
1412
1413
1414 if (inode->i_dquot[cnt])
1415 continue;
1416 init_needed = 1;
1417
1418 switch (cnt) {
1419 case USRQUOTA:
1420 qid = make_kqid_uid(inode->i_uid);
1421 break;
1422 case GRPQUOTA:
1423 qid = make_kqid_gid(inode->i_gid);
1424 break;
1425 }
1426 got[cnt] = dqget(sb, qid);
1427 }
1428
1429
1430 if (!init_needed)
1431 return;
1432
1433 spin_lock(&dq_data_lock);
1434 if (IS_NOQUOTA(inode))
1435 goto out_err;
1436 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1437 if (type != -1 && cnt != type)
1438 continue;
1439
1440 if (!sb_has_quota_active(sb, cnt))
1441 continue;
1442
1443 if (!got[cnt])
1444 continue;
1445 if (!inode->i_dquot[cnt]) {
1446 inode->i_dquot[cnt] = got[cnt];
1447 got[cnt] = NULL;
1448
1449
1450
1451
1452 rsv = inode_get_rsv_space(inode);
1453 if (unlikely(rsv))
1454 dquot_resv_space(inode->i_dquot[cnt], rsv);
1455 }
1456 }
1457out_err:
1458 spin_unlock(&dq_data_lock);
1459
1460 dqput_all(got);
1461}
1462
1463void dquot_initialize(struct inode *inode)
1464{
1465 __dquot_initialize(inode, -1);
1466}
1467EXPORT_SYMBOL(dquot_initialize);
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477static void __dquot_drop(struct inode *inode)
1478{
1479 int cnt;
1480 struct dquot *put[MAXQUOTAS];
1481
1482 spin_lock(&dq_data_lock);
1483 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1484 put[cnt] = inode->i_dquot[cnt];
1485 inode->i_dquot[cnt] = NULL;
1486 }
1487 spin_unlock(&dq_data_lock);
1488 dqput_all(put);
1489}
1490
1491void dquot_drop(struct inode *inode)
1492{
1493 int cnt;
1494
1495 if (IS_NOQUOTA(inode))
1496 return;
1497
1498
1499
1500
1501
1502
1503
1504
1505 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1506 if (inode->i_dquot[cnt])
1507 break;
1508 }
1509
1510 if (cnt < MAXQUOTAS)
1511 __dquot_drop(inode);
1512}
1513EXPORT_SYMBOL(dquot_drop);
1514
1515
1516
1517
1518
1519static qsize_t *inode_reserved_space(struct inode * inode)
1520{
1521
1522
1523 BUG_ON(!inode->i_sb->dq_op->get_reserved_space);
1524 return inode->i_sb->dq_op->get_reserved_space(inode);
1525}
1526
1527void inode_add_rsv_space(struct inode *inode, qsize_t number)
1528{
1529 spin_lock(&inode->i_lock);
1530 *inode_reserved_space(inode) += number;
1531 spin_unlock(&inode->i_lock);
1532}
1533EXPORT_SYMBOL(inode_add_rsv_space);
1534
1535void inode_claim_rsv_space(struct inode *inode, qsize_t number)
1536{
1537 spin_lock(&inode->i_lock);
1538 *inode_reserved_space(inode) -= number;
1539 __inode_add_bytes(inode, number);
1540 spin_unlock(&inode->i_lock);
1541}
1542EXPORT_SYMBOL(inode_claim_rsv_space);
1543
1544void inode_reclaim_rsv_space(struct inode *inode, qsize_t number)
1545{
1546 spin_lock(&inode->i_lock);
1547 *inode_reserved_space(inode) += number;
1548 __inode_sub_bytes(inode, number);
1549 spin_unlock(&inode->i_lock);
1550}
1551EXPORT_SYMBOL(inode_reclaim_rsv_space);
1552
1553void inode_sub_rsv_space(struct inode *inode, qsize_t number)
1554{
1555 spin_lock(&inode->i_lock);
1556 *inode_reserved_space(inode) -= number;
1557 spin_unlock(&inode->i_lock);
1558}
1559EXPORT_SYMBOL(inode_sub_rsv_space);
1560
1561static qsize_t inode_get_rsv_space(struct inode *inode)
1562{
1563 qsize_t ret;
1564
1565 if (!inode->i_sb->dq_op->get_reserved_space)
1566 return 0;
1567 spin_lock(&inode->i_lock);
1568 ret = *inode_reserved_space(inode);
1569 spin_unlock(&inode->i_lock);
1570 return ret;
1571}
1572
1573static void inode_incr_space(struct inode *inode, qsize_t number,
1574 int reserve)
1575{
1576 if (reserve)
1577 inode_add_rsv_space(inode, number);
1578 else
1579 inode_add_bytes(inode, number);
1580}
1581
1582static void inode_decr_space(struct inode *inode, qsize_t number, int reserve)
1583{
1584 if (reserve)
1585 inode_sub_rsv_space(inode, number);
1586 else
1587 inode_sub_bytes(inode, number);
1588}
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603int __dquot_alloc_space(struct inode *inode, qsize_t number, int flags)
1604{
1605 int cnt, ret = 0, index;
1606 struct dquot_warn warn[MAXQUOTAS];
1607 struct dquot **dquots = inode->i_dquot;
1608 int reserve = flags & DQUOT_SPACE_RESERVE;
1609
1610 if (!dquot_active(inode)) {
1611 inode_incr_space(inode, number, reserve);
1612 goto out;
1613 }
1614
1615 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1616 warn[cnt].w_type = QUOTA_NL_NOWARN;
1617
1618 index = srcu_read_lock(&dquot_srcu);
1619 spin_lock(&dq_data_lock);
1620 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1621 if (!dquots[cnt])
1622 continue;
1623 ret = check_bdq(dquots[cnt], number,
1624 !(flags & DQUOT_SPACE_WARN), &warn[cnt]);
1625 if (ret && !(flags & DQUOT_SPACE_NOFAIL)) {
1626 spin_unlock(&dq_data_lock);
1627 goto out_flush_warn;
1628 }
1629 }
1630 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1631 if (!dquots[cnt])
1632 continue;
1633 if (reserve)
1634 dquot_resv_space(dquots[cnt], number);
1635 else
1636 dquot_incr_space(dquots[cnt], number);
1637 }
1638 inode_incr_space(inode, number, reserve);
1639 spin_unlock(&dq_data_lock);
1640
1641 if (reserve)
1642 goto out_flush_warn;
1643 mark_all_dquot_dirty(dquots);
1644out_flush_warn:
1645 srcu_read_unlock(&dquot_srcu, index);
1646 flush_warnings(warn);
1647out:
1648 return ret;
1649}
1650EXPORT_SYMBOL(__dquot_alloc_space);
1651
1652
1653
1654
1655int dquot_alloc_inode(const struct inode *inode)
1656{
1657 int cnt, ret = 0, index;
1658 struct dquot_warn warn[MAXQUOTAS];
1659 struct dquot * const *dquots = inode->i_dquot;
1660
1661 if (!dquot_active(inode))
1662 return 0;
1663 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1664 warn[cnt].w_type = QUOTA_NL_NOWARN;
1665
1666 index = srcu_read_lock(&dquot_srcu);
1667 spin_lock(&dq_data_lock);
1668 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1669 if (!dquots[cnt])
1670 continue;
1671 ret = check_idq(dquots[cnt], 1, &warn[cnt]);
1672 if (ret)
1673 goto warn_put_all;
1674 }
1675
1676 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1677 if (!dquots[cnt])
1678 continue;
1679 dquot_incr_inodes(dquots[cnt], 1);
1680 }
1681
1682warn_put_all:
1683 spin_unlock(&dq_data_lock);
1684 if (ret == 0)
1685 mark_all_dquot_dirty(dquots);
1686 srcu_read_unlock(&dquot_srcu, index);
1687 flush_warnings(warn);
1688 return ret;
1689}
1690EXPORT_SYMBOL(dquot_alloc_inode);
1691
1692
1693
1694
1695int dquot_claim_space_nodirty(struct inode *inode, qsize_t number)
1696{
1697 int cnt, index;
1698
1699 if (!dquot_active(inode)) {
1700 inode_claim_rsv_space(inode, number);
1701 return 0;
1702 }
1703
1704 index = srcu_read_lock(&dquot_srcu);
1705 spin_lock(&dq_data_lock);
1706
1707 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1708 if (inode->i_dquot[cnt])
1709 dquot_claim_reserved_space(inode->i_dquot[cnt],
1710 number);
1711 }
1712
1713 inode_claim_rsv_space(inode, number);
1714 spin_unlock(&dq_data_lock);
1715 mark_all_dquot_dirty(inode->i_dquot);
1716 srcu_read_unlock(&dquot_srcu, index);
1717 return 0;
1718}
1719EXPORT_SYMBOL(dquot_claim_space_nodirty);
1720
1721
1722
1723
1724void dquot_reclaim_space_nodirty(struct inode *inode, qsize_t number)
1725{
1726 int cnt, index;
1727
1728 if (!dquot_active(inode)) {
1729 inode_reclaim_rsv_space(inode, number);
1730 return;
1731 }
1732
1733 index = srcu_read_lock(&dquot_srcu);
1734 spin_lock(&dq_data_lock);
1735
1736 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1737 if (inode->i_dquot[cnt])
1738 dquot_reclaim_reserved_space(inode->i_dquot[cnt],
1739 number);
1740 }
1741
1742 inode_reclaim_rsv_space(inode, number);
1743 spin_unlock(&dq_data_lock);
1744 mark_all_dquot_dirty(inode->i_dquot);
1745 srcu_read_unlock(&dquot_srcu, index);
1746 return;
1747}
1748EXPORT_SYMBOL(dquot_reclaim_space_nodirty);
1749
1750
1751
1752
1753void __dquot_free_space(struct inode *inode, qsize_t number, int flags)
1754{
1755 unsigned int cnt;
1756 struct dquot_warn warn[MAXQUOTAS];
1757 struct dquot **dquots = inode->i_dquot;
1758 int reserve = flags & DQUOT_SPACE_RESERVE, index;
1759
1760 if (!dquot_active(inode)) {
1761 inode_decr_space(inode, number, reserve);
1762 return;
1763 }
1764
1765 index = srcu_read_lock(&dquot_srcu);
1766 spin_lock(&dq_data_lock);
1767 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1768 int wtype;
1769
1770 warn[cnt].w_type = QUOTA_NL_NOWARN;
1771 if (!dquots[cnt])
1772 continue;
1773 wtype = info_bdq_free(dquots[cnt], number);
1774 if (wtype != QUOTA_NL_NOWARN)
1775 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1776 if (reserve)
1777 dquot_free_reserved_space(dquots[cnt], number);
1778 else
1779 dquot_decr_space(dquots[cnt], number);
1780 }
1781 inode_decr_space(inode, number, reserve);
1782 spin_unlock(&dq_data_lock);
1783
1784 if (reserve)
1785 goto out_unlock;
1786 mark_all_dquot_dirty(dquots);
1787out_unlock:
1788 srcu_read_unlock(&dquot_srcu, index);
1789 flush_warnings(warn);
1790}
1791EXPORT_SYMBOL(__dquot_free_space);
1792
1793
1794
1795
1796void dquot_free_inode(const struct inode *inode)
1797{
1798 unsigned int cnt;
1799 struct dquot_warn warn[MAXQUOTAS];
1800 struct dquot * const *dquots = inode->i_dquot;
1801 int index;
1802
1803 if (!dquot_active(inode))
1804 return;
1805
1806 index = srcu_read_lock(&dquot_srcu);
1807 spin_lock(&dq_data_lock);
1808 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1809 int wtype;
1810
1811 warn[cnt].w_type = QUOTA_NL_NOWARN;
1812 if (!dquots[cnt])
1813 continue;
1814 wtype = info_idq_free(dquots[cnt], 1);
1815 if (wtype != QUOTA_NL_NOWARN)
1816 prepare_warning(&warn[cnt], dquots[cnt], wtype);
1817 dquot_decr_inodes(dquots[cnt], 1);
1818 }
1819 spin_unlock(&dq_data_lock);
1820 mark_all_dquot_dirty(dquots);
1821 srcu_read_unlock(&dquot_srcu, index);
1822 flush_warnings(warn);
1823}
1824EXPORT_SYMBOL(dquot_free_inode);
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838int __dquot_transfer(struct inode *inode, struct dquot **transfer_to)
1839{
1840 qsize_t space, cur_space;
1841 qsize_t rsv_space = 0;
1842 struct dquot *transfer_from[MAXQUOTAS] = {};
1843 int cnt, ret = 0;
1844 char is_valid[MAXQUOTAS] = {};
1845 struct dquot_warn warn_to[MAXQUOTAS];
1846 struct dquot_warn warn_from_inodes[MAXQUOTAS];
1847 struct dquot_warn warn_from_space[MAXQUOTAS];
1848
1849 if (IS_NOQUOTA(inode))
1850 return 0;
1851
1852 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1853 warn_to[cnt].w_type = QUOTA_NL_NOWARN;
1854 warn_from_inodes[cnt].w_type = QUOTA_NL_NOWARN;
1855 warn_from_space[cnt].w_type = QUOTA_NL_NOWARN;
1856 }
1857
1858 spin_lock(&dq_data_lock);
1859 if (IS_NOQUOTA(inode)) {
1860 spin_unlock(&dq_data_lock);
1861 return 0;
1862 }
1863 cur_space = inode_get_bytes(inode);
1864 rsv_space = inode_get_rsv_space(inode);
1865 space = cur_space + rsv_space;
1866
1867 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1868
1869
1870
1871 if (!transfer_to[cnt])
1872 continue;
1873
1874 if (!sb_has_quota_active(inode->i_sb, cnt))
1875 continue;
1876 is_valid[cnt] = 1;
1877 transfer_from[cnt] = inode->i_dquot[cnt];
1878 ret = check_idq(transfer_to[cnt], 1, &warn_to[cnt]);
1879 if (ret)
1880 goto over_quota;
1881 ret = check_bdq(transfer_to[cnt], space, 0, &warn_to[cnt]);
1882 if (ret)
1883 goto over_quota;
1884 }
1885
1886
1887
1888
1889 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
1890 if (!is_valid[cnt])
1891 continue;
1892
1893 if (transfer_from[cnt]) {
1894 int wtype;
1895 wtype = info_idq_free(transfer_from[cnt], 1);
1896 if (wtype != QUOTA_NL_NOWARN)
1897 prepare_warning(&warn_from_inodes[cnt],
1898 transfer_from[cnt], wtype);
1899 wtype = info_bdq_free(transfer_from[cnt], space);
1900 if (wtype != QUOTA_NL_NOWARN)
1901 prepare_warning(&warn_from_space[cnt],
1902 transfer_from[cnt], wtype);
1903 dquot_decr_inodes(transfer_from[cnt], 1);
1904 dquot_decr_space(transfer_from[cnt], cur_space);
1905 dquot_free_reserved_space(transfer_from[cnt],
1906 rsv_space);
1907 }
1908
1909 dquot_incr_inodes(transfer_to[cnt], 1);
1910 dquot_incr_space(transfer_to[cnt], cur_space);
1911 dquot_resv_space(transfer_to[cnt], rsv_space);
1912
1913 inode->i_dquot[cnt] = transfer_to[cnt];
1914 }
1915 spin_unlock(&dq_data_lock);
1916
1917 mark_all_dquot_dirty(transfer_from);
1918 mark_all_dquot_dirty(transfer_to);
1919 flush_warnings(warn_to);
1920 flush_warnings(warn_from_inodes);
1921 flush_warnings(warn_from_space);
1922
1923 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
1924 if (is_valid[cnt])
1925 transfer_to[cnt] = transfer_from[cnt];
1926 return 0;
1927over_quota:
1928 spin_unlock(&dq_data_lock);
1929 flush_warnings(warn_to);
1930 return ret;
1931}
1932EXPORT_SYMBOL(__dquot_transfer);
1933
1934
1935
1936
1937int dquot_transfer(struct inode *inode, struct iattr *iattr)
1938{
1939 struct dquot *transfer_to[MAXQUOTAS] = {};
1940 struct super_block *sb = inode->i_sb;
1941 int ret;
1942
1943 if (!dquot_active(inode))
1944 return 0;
1945
1946 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid))
1947 transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(iattr->ia_uid));
1948 if (iattr->ia_valid & ATTR_GID && !gid_eq(iattr->ia_gid, inode->i_gid))
1949 transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(iattr->ia_gid));
1950
1951 ret = __dquot_transfer(inode, transfer_to);
1952 dqput_all(transfer_to);
1953 return ret;
1954}
1955EXPORT_SYMBOL(dquot_transfer);
1956
1957
1958
1959
1960int dquot_commit_info(struct super_block *sb, int type)
1961{
1962 int ret;
1963 struct quota_info *dqopt = sb_dqopt(sb);
1964
1965 mutex_lock(&dqopt->dqio_mutex);
1966 ret = dqopt->ops[type]->write_file_info(sb, type);
1967 mutex_unlock(&dqopt->dqio_mutex);
1968 return ret;
1969}
1970EXPORT_SYMBOL(dquot_commit_info);
1971
1972
1973
1974
1975const struct dquot_operations dquot_operations = {
1976 .write_dquot = dquot_commit,
1977 .acquire_dquot = dquot_acquire,
1978 .release_dquot = dquot_release,
1979 .mark_dirty = dquot_mark_dquot_dirty,
1980 .write_info = dquot_commit_info,
1981 .alloc_dquot = dquot_alloc,
1982 .destroy_dquot = dquot_destroy,
1983};
1984EXPORT_SYMBOL(dquot_operations);
1985
1986
1987
1988
1989int dquot_file_open(struct inode *inode, struct file *file)
1990{
1991 int error;
1992
1993 error = generic_file_open(inode, file);
1994 if (!error && (file->f_mode & FMODE_WRITE))
1995 dquot_initialize(inode);
1996 return error;
1997}
1998EXPORT_SYMBOL(dquot_file_open);
1999
2000
2001
2002
2003int dquot_disable(struct super_block *sb, int type, unsigned int flags)
2004{
2005 int cnt, ret = 0;
2006 struct quota_info *dqopt = sb_dqopt(sb);
2007 struct inode *toputinode[MAXQUOTAS];
2008
2009
2010
2011 if ((flags & DQUOT_USAGE_ENABLED && !(flags & DQUOT_LIMITS_ENABLED))
2012 || (flags & DQUOT_SUSPENDED && flags & (DQUOT_LIMITS_ENABLED |
2013 DQUOT_USAGE_ENABLED)))
2014 return -EINVAL;
2015
2016
2017 mutex_lock(&dqopt->dqonoff_mutex);
2018
2019
2020
2021
2022
2023
2024 if (!sb_any_quota_loaded(sb)) {
2025 mutex_unlock(&dqopt->dqonoff_mutex);
2026 return 0;
2027 }
2028 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2029 toputinode[cnt] = NULL;
2030 if (type != -1 && cnt != type)
2031 continue;
2032 if (!sb_has_quota_loaded(sb, cnt))
2033 continue;
2034
2035 if (flags & DQUOT_SUSPENDED) {
2036 spin_lock(&dq_state_lock);
2037 dqopt->flags |=
2038 dquot_state_flag(DQUOT_SUSPENDED, cnt);
2039 spin_unlock(&dq_state_lock);
2040 } else {
2041 spin_lock(&dq_state_lock);
2042 dqopt->flags &= ~dquot_state_flag(flags, cnt);
2043
2044 if (!sb_has_quota_loaded(sb, cnt) &&
2045 sb_has_quota_suspended(sb, cnt)) {
2046 dqopt->flags &= ~dquot_state_flag(
2047 DQUOT_SUSPENDED, cnt);
2048 spin_unlock(&dq_state_lock);
2049 iput(dqopt->files[cnt]);
2050 dqopt->files[cnt] = NULL;
2051 continue;
2052 }
2053 spin_unlock(&dq_state_lock);
2054 }
2055
2056
2057 if (sb_has_quota_loaded(sb, cnt) && !(flags & DQUOT_SUSPENDED))
2058 continue;
2059
2060
2061 drop_dquot_ref(sb, cnt);
2062 invalidate_dquots(sb, cnt);
2063
2064
2065
2066
2067 if (info_dirty(&dqopt->info[cnt]))
2068 sb->dq_op->write_info(sb, cnt);
2069 if (dqopt->ops[cnt]->free_file_info)
2070 dqopt->ops[cnt]->free_file_info(sb, cnt);
2071 put_quota_format(dqopt->info[cnt].dqi_format);
2072
2073 toputinode[cnt] = dqopt->files[cnt];
2074 if (!sb_has_quota_loaded(sb, cnt))
2075 dqopt->files[cnt] = NULL;
2076 dqopt->info[cnt].dqi_flags = 0;
2077 dqopt->info[cnt].dqi_igrace = 0;
2078 dqopt->info[cnt].dqi_bgrace = 0;
2079 dqopt->ops[cnt] = NULL;
2080 }
2081 mutex_unlock(&dqopt->dqonoff_mutex);
2082
2083
2084 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2085 goto put_inodes;
2086
2087
2088
2089 if (sb->s_op->sync_fs)
2090 sb->s_op->sync_fs(sb, 1);
2091 sync_blockdev(sb->s_bdev);
2092
2093
2094
2095
2096
2097 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2098 if (toputinode[cnt]) {
2099 mutex_lock(&dqopt->dqonoff_mutex);
2100
2101
2102 if (!sb_has_quota_loaded(sb, cnt)) {
2103 mutex_lock(&toputinode[cnt]->i_mutex);
2104 toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
2105 S_NOATIME | S_NOQUOTA);
2106 truncate_inode_pages(&toputinode[cnt]->i_data,
2107 0);
2108 mutex_unlock(&toputinode[cnt]->i_mutex);
2109 mark_inode_dirty_sync(toputinode[cnt]);
2110 }
2111 mutex_unlock(&dqopt->dqonoff_mutex);
2112 }
2113 if (sb->s_bdev)
2114 invalidate_bdev(sb->s_bdev);
2115put_inodes:
2116 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2117 if (toputinode[cnt]) {
2118
2119
2120
2121
2122
2123
2124
2125 if (!(flags & DQUOT_SUSPENDED))
2126 iput(toputinode[cnt]);
2127 else if (!toputinode[cnt]->i_nlink)
2128 ret = -EBUSY;
2129 }
2130 return ret;
2131}
2132EXPORT_SYMBOL(dquot_disable);
2133
2134int dquot_quota_off(struct super_block *sb, int type)
2135{
2136 return dquot_disable(sb, type,
2137 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2138}
2139EXPORT_SYMBOL(dquot_quota_off);
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2150 unsigned int flags)
2151{
2152 struct quota_format_type *fmt = find_quota_format(format_id);
2153 struct super_block *sb = inode->i_sb;
2154 struct quota_info *dqopt = sb_dqopt(sb);
2155 int error;
2156 int oldflags = -1;
2157
2158 if (!fmt)
2159 return -ESRCH;
2160 if (!S_ISREG(inode->i_mode)) {
2161 error = -EACCES;
2162 goto out_fmt;
2163 }
2164 if (IS_RDONLY(inode)) {
2165 error = -EROFS;
2166 goto out_fmt;
2167 }
2168 if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
2169 error = -EINVAL;
2170 goto out_fmt;
2171 }
2172
2173 if (sb->s_user_ns != &init_user_ns) {
2174 error = -EINVAL;
2175 goto out_fmt;
2176 }
2177
2178 if (!(flags & DQUOT_USAGE_ENABLED)) {
2179 error = -EINVAL;
2180 goto out_fmt;
2181 }
2182
2183 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2184
2185
2186
2187
2188
2189
2190 sync_filesystem(sb);
2191 invalidate_bdev(sb->s_bdev);
2192 }
2193 mutex_lock(&dqopt->dqonoff_mutex);
2194 if (sb_has_quota_loaded(sb, type)) {
2195 error = -EBUSY;
2196 goto out_lock;
2197 }
2198
2199 if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2200
2201
2202
2203 mutex_lock(&inode->i_mutex);
2204 oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
2205 S_NOQUOTA);
2206 inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
2207 mutex_unlock(&inode->i_mutex);
2208
2209
2210
2211
2212 __dquot_drop(inode);
2213 }
2214
2215 error = -EIO;
2216 dqopt->files[type] = igrab(inode);
2217 if (!dqopt->files[type])
2218 goto out_lock;
2219 error = -EINVAL;
2220 if (!fmt->qf_ops->check_quota_file(sb, type))
2221 goto out_file_init;
2222
2223 dqopt->ops[type] = fmt->qf_ops;
2224 dqopt->info[type].dqi_format = fmt;
2225 dqopt->info[type].dqi_fmt_id = format_id;
2226 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
2227 mutex_lock(&dqopt->dqio_mutex);
2228 error = dqopt->ops[type]->read_file_info(sb, type);
2229 if (error < 0) {
2230 mutex_unlock(&dqopt->dqio_mutex);
2231 goto out_file_init;
2232 }
2233 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE)
2234 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
2235 mutex_unlock(&dqopt->dqio_mutex);
2236 spin_lock(&dq_state_lock);
2237 dqopt->flags |= dquot_state_flag(flags, type);
2238 spin_unlock(&dq_state_lock);
2239
2240 add_dquot_ref(sb, type);
2241 mutex_unlock(&dqopt->dqonoff_mutex);
2242
2243 return 0;
2244
2245out_file_init:
2246 dqopt->files[type] = NULL;
2247 iput(inode);
2248out_lock:
2249 if (oldflags != -1) {
2250 mutex_lock(&inode->i_mutex);
2251
2252
2253 inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
2254 inode->i_flags |= oldflags;
2255 mutex_unlock(&inode->i_mutex);
2256 }
2257 mutex_unlock(&dqopt->dqonoff_mutex);
2258out_fmt:
2259 put_quota_format(fmt);
2260
2261 return error;
2262}
2263
2264
2265int dquot_resume(struct super_block *sb, int type)
2266{
2267 struct quota_info *dqopt = sb_dqopt(sb);
2268 struct inode *inode;
2269 int ret = 0, cnt;
2270 unsigned int flags;
2271
2272 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2273 if (type != -1 && cnt != type)
2274 continue;
2275
2276 mutex_lock(&dqopt->dqonoff_mutex);
2277 if (!sb_has_quota_suspended(sb, cnt)) {
2278 mutex_unlock(&dqopt->dqonoff_mutex);
2279 continue;
2280 }
2281 inode = dqopt->files[cnt];
2282 dqopt->files[cnt] = NULL;
2283 spin_lock(&dq_state_lock);
2284 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
2285 DQUOT_LIMITS_ENABLED,
2286 cnt);
2287 dqopt->flags &= ~dquot_state_flag(DQUOT_STATE_FLAGS, cnt);
2288 spin_unlock(&dq_state_lock);
2289 mutex_unlock(&dqopt->dqonoff_mutex);
2290
2291 flags = dquot_generic_flag(flags, cnt);
2292 ret = vfs_load_quota_inode(inode, cnt,
2293 dqopt->info[cnt].dqi_fmt_id, flags);
2294 iput(inode);
2295 }
2296
2297 return ret;
2298}
2299EXPORT_SYMBOL(dquot_resume);
2300
2301int dquot_quota_on(struct super_block *sb, int type, int format_id,
2302 struct path *path)
2303{
2304 int error = security_quota_on(path->dentry);
2305 if (error)
2306 return error;
2307
2308 if (path->dentry->d_sb != sb)
2309 error = -EXDEV;
2310 else
2311 error = vfs_load_quota_inode(path->dentry->d_inode, type,
2312 format_id, DQUOT_USAGE_ENABLED |
2313 DQUOT_LIMITS_ENABLED);
2314 return error;
2315}
2316EXPORT_SYMBOL(dquot_quota_on);
2317
2318
2319
2320
2321
2322int dquot_enable(struct inode *inode, int type, int format_id,
2323 unsigned int flags)
2324{
2325 int ret = 0;
2326 struct super_block *sb = inode->i_sb;
2327 struct quota_info *dqopt = sb_dqopt(sb);
2328
2329
2330 BUG_ON(flags & DQUOT_SUSPENDED);
2331
2332 if (!flags)
2333 return 0;
2334
2335 if (sb_has_quota_loaded(sb, type)) {
2336 mutex_lock(&dqopt->dqonoff_mutex);
2337
2338 if (!sb_has_quota_loaded(sb, type)) {
2339 mutex_unlock(&dqopt->dqonoff_mutex);
2340 goto load_quota;
2341 }
2342 if (flags & DQUOT_USAGE_ENABLED &&
2343 sb_has_quota_usage_enabled(sb, type)) {
2344 ret = -EBUSY;
2345 goto out_lock;
2346 }
2347 if (flags & DQUOT_LIMITS_ENABLED &&
2348 sb_has_quota_limits_enabled(sb, type)) {
2349 ret = -EBUSY;
2350 goto out_lock;
2351 }
2352 spin_lock(&dq_state_lock);
2353 sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2354 spin_unlock(&dq_state_lock);
2355out_lock:
2356 mutex_unlock(&dqopt->dqonoff_mutex);
2357 return ret;
2358 }
2359
2360load_quota:
2361 return vfs_load_quota_inode(inode, type, format_id, flags);
2362}
2363EXPORT_SYMBOL(dquot_enable);
2364
2365
2366
2367
2368
2369int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
2370 int format_id, int type)
2371{
2372 struct dentry *dentry;
2373 int error;
2374
2375 mutex_lock(&sb->s_root->d_inode->i_mutex);
2376 dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
2377 mutex_unlock(&sb->s_root->d_inode->i_mutex);
2378 if (IS_ERR(dentry))
2379 return PTR_ERR(dentry);
2380
2381 if (!dentry->d_inode) {
2382 error = -ENOENT;
2383 goto out;
2384 }
2385
2386 error = security_quota_on(dentry);
2387 if (!error)
2388 error = vfs_load_quota_inode(dentry->d_inode, type, format_id,
2389 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
2390
2391out:
2392 dput(dentry);
2393 return error;
2394}
2395EXPORT_SYMBOL(dquot_quota_on_mount);
2396
2397static inline qsize_t qbtos(qsize_t blocks)
2398{
2399 return blocks << QIF_DQBLKSIZE_BITS;
2400}
2401
2402static inline qsize_t stoqb(qsize_t space)
2403{
2404 return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
2405}
2406
2407
2408static void do_get_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
2409{
2410 struct mem_dqblk *dm = &dquot->dq_dqb;
2411
2412 memset(di, 0, sizeof(*di));
2413 di->d_version = FS_DQUOT_VERSION;
2414 di->d_flags = dquot->dq_id.type == USRQUOTA ?
2415 FS_USER_QUOTA : FS_GROUP_QUOTA;
2416 di->d_id = from_kqid_munged(current_user_ns(), dquot->dq_id);
2417
2418 spin_lock(&dq_data_lock);
2419 di->d_blk_hardlimit = stoqb(dm->dqb_bhardlimit);
2420 di->d_blk_softlimit = stoqb(dm->dqb_bsoftlimit);
2421 di->d_ino_hardlimit = dm->dqb_ihardlimit;
2422 di->d_ino_softlimit = dm->dqb_isoftlimit;
2423 di->d_bcount = dm->dqb_curspace + dm->dqb_rsvspace;
2424 di->d_icount = dm->dqb_curinodes;
2425 di->d_btimer = dm->dqb_btime;
2426 di->d_itimer = dm->dqb_itime;
2427 spin_unlock(&dq_data_lock);
2428}
2429
2430int dquot_get_dqblk(struct super_block *sb, struct kqid qid,
2431 struct fs_disk_quota *di)
2432{
2433 struct dquot *dquot;
2434
2435 dquot = dqget(sb, qid);
2436 if (!dquot)
2437 return -ESRCH;
2438 do_get_dqblk(dquot, di);
2439 dqput(dquot);
2440
2441 return 0;
2442}
2443EXPORT_SYMBOL(dquot_get_dqblk);
2444
2445#define VFS_FS_DQ_MASK \
2446 (FS_DQ_BCOUNT | FS_DQ_BSOFT | FS_DQ_BHARD | \
2447 FS_DQ_ICOUNT | FS_DQ_ISOFT | FS_DQ_IHARD | \
2448 FS_DQ_BTIMER | FS_DQ_ITIMER)
2449
2450
2451static int do_set_dqblk(struct dquot *dquot, struct fs_disk_quota *di)
2452{
2453 struct mem_dqblk *dm = &dquot->dq_dqb;
2454 int check_blim = 0, check_ilim = 0;
2455 struct mem_dqinfo *dqi = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_id.type];
2456
2457 if (di->d_fieldmask & ~VFS_FS_DQ_MASK)
2458 return -EINVAL;
2459
2460 if (((di->d_fieldmask & FS_DQ_BSOFT) &&
2461 (di->d_blk_softlimit > dqi->dqi_maxblimit)) ||
2462 ((di->d_fieldmask & FS_DQ_BHARD) &&
2463 (di->d_blk_hardlimit > dqi->dqi_maxblimit)) ||
2464 ((di->d_fieldmask & FS_DQ_ISOFT) &&
2465 (di->d_ino_softlimit > dqi->dqi_maxilimit)) ||
2466 ((di->d_fieldmask & FS_DQ_IHARD) &&
2467 (di->d_ino_hardlimit > dqi->dqi_maxilimit)))
2468 return -ERANGE;
2469
2470 spin_lock(&dq_data_lock);
2471 if (di->d_fieldmask & FS_DQ_BCOUNT) {
2472 dm->dqb_curspace = di->d_bcount - dm->dqb_rsvspace;
2473 check_blim = 1;
2474 set_bit(DQ_LASTSET_B + QIF_SPACE_B, &dquot->dq_flags);
2475 }
2476
2477 if (di->d_fieldmask & FS_DQ_BSOFT)
2478 dm->dqb_bsoftlimit = qbtos(di->d_blk_softlimit);
2479 if (di->d_fieldmask & FS_DQ_BHARD)
2480 dm->dqb_bhardlimit = qbtos(di->d_blk_hardlimit);
2481 if (di->d_fieldmask & (FS_DQ_BSOFT | FS_DQ_BHARD)) {
2482 check_blim = 1;
2483 set_bit(DQ_LASTSET_B + QIF_BLIMITS_B, &dquot->dq_flags);
2484 }
2485
2486 if (di->d_fieldmask & FS_DQ_ICOUNT) {
2487 dm->dqb_curinodes = di->d_icount;
2488 check_ilim = 1;
2489 set_bit(DQ_LASTSET_B + QIF_INODES_B, &dquot->dq_flags);
2490 }
2491
2492 if (di->d_fieldmask & FS_DQ_ISOFT)
2493 dm->dqb_isoftlimit = di->d_ino_softlimit;
2494 if (di->d_fieldmask & FS_DQ_IHARD)
2495 dm->dqb_ihardlimit = di->d_ino_hardlimit;
2496 if (di->d_fieldmask & (FS_DQ_ISOFT | FS_DQ_IHARD)) {
2497 check_ilim = 1;
2498 set_bit(DQ_LASTSET_B + QIF_ILIMITS_B, &dquot->dq_flags);
2499 }
2500
2501 if (di->d_fieldmask & FS_DQ_BTIMER) {
2502 dm->dqb_btime = di->d_btimer;
2503 check_blim = 1;
2504 set_bit(DQ_LASTSET_B + QIF_BTIME_B, &dquot->dq_flags);
2505 }
2506
2507 if (di->d_fieldmask & FS_DQ_ITIMER) {
2508 dm->dqb_itime = di->d_itimer;
2509 check_ilim = 1;
2510 set_bit(DQ_LASTSET_B + QIF_ITIME_B, &dquot->dq_flags);
2511 }
2512
2513 if (check_blim) {
2514 if (!dm->dqb_bsoftlimit ||
2515 dm->dqb_curspace < dm->dqb_bsoftlimit) {
2516 dm->dqb_btime = 0;
2517 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
2518 } else if (!(di->d_fieldmask & FS_DQ_BTIMER))
2519
2520 dm->dqb_btime = get_seconds() + dqi->dqi_bgrace;
2521 }
2522 if (check_ilim) {
2523 if (!dm->dqb_isoftlimit ||
2524 dm->dqb_curinodes < dm->dqb_isoftlimit) {
2525 dm->dqb_itime = 0;
2526 clear_bit(DQ_INODES_B, &dquot->dq_flags);
2527 } else if (!(di->d_fieldmask & FS_DQ_ITIMER))
2528
2529 dm->dqb_itime = get_seconds() + dqi->dqi_igrace;
2530 }
2531 if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit ||
2532 dm->dqb_isoftlimit)
2533 clear_bit(DQ_FAKE_B, &dquot->dq_flags);
2534 else
2535 set_bit(DQ_FAKE_B, &dquot->dq_flags);
2536 spin_unlock(&dq_data_lock);
2537 mark_dquot_dirty(dquot);
2538
2539 return 0;
2540}
2541
2542int dquot_set_dqblk(struct super_block *sb, struct kqid qid,
2543 struct fs_disk_quota *di)
2544{
2545 struct dquot *dquot;
2546 int rc;
2547
2548 dquot = dqget(sb, qid);
2549 if (!dquot) {
2550 rc = -ESRCH;
2551 goto out;
2552 }
2553 rc = do_set_dqblk(dquot, di);
2554 dqput(dquot);
2555out:
2556 return rc;
2557}
2558EXPORT_SYMBOL(dquot_set_dqblk);
2559
2560
2561int dquot_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2562{
2563 struct mem_dqinfo *mi;
2564
2565 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2566 if (!sb_has_quota_active(sb, type)) {
2567 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2568 return -ESRCH;
2569 }
2570 mi = sb_dqopt(sb)->info + type;
2571 spin_lock(&dq_data_lock);
2572 ii->dqi_bgrace = mi->dqi_bgrace;
2573 ii->dqi_igrace = mi->dqi_igrace;
2574 ii->dqi_flags = mi->dqi_flags & DQF_GETINFO_MASK;
2575 ii->dqi_valid = IIF_ALL;
2576 spin_unlock(&dq_data_lock);
2577 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2578 return 0;
2579}
2580EXPORT_SYMBOL(dquot_get_dqinfo);
2581
2582
2583int dquot_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
2584{
2585 struct mem_dqinfo *mi;
2586 int err = 0;
2587
2588 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
2589 if (!sb_has_quota_active(sb, type)) {
2590 err = -ESRCH;
2591 goto out;
2592 }
2593 mi = sb_dqopt(sb)->info + type;
2594 spin_lock(&dq_data_lock);
2595 if (ii->dqi_valid & IIF_BGRACE)
2596 mi->dqi_bgrace = ii->dqi_bgrace;
2597 if (ii->dqi_valid & IIF_IGRACE)
2598 mi->dqi_igrace = ii->dqi_igrace;
2599 if (ii->dqi_valid & IIF_FLAGS)
2600 mi->dqi_flags = (mi->dqi_flags & ~DQF_SETINFO_MASK) |
2601 (ii->dqi_flags & DQF_SETINFO_MASK);
2602 spin_unlock(&dq_data_lock);
2603 mark_info_dirty(sb, type);
2604
2605 sb->dq_op->write_info(sb, type);
2606out:
2607 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
2608 return err;
2609}
2610EXPORT_SYMBOL(dquot_set_dqinfo);
2611
2612const struct quotactl_ops dquot_quotactl_ops = {
2613 .quota_on = dquot_quota_on,
2614 .quota_off = dquot_quota_off,
2615 .quota_sync = dquot_quota_sync,
2616 .get_info = dquot_get_dqinfo,
2617 .set_info = dquot_set_dqinfo,
2618 .get_dqblk = dquot_get_dqblk,
2619 .set_dqblk = dquot_set_dqblk
2620};
2621EXPORT_SYMBOL(dquot_quotactl_ops);
2622
2623static int do_proc_dqstats(struct ctl_table *table, int write,
2624 void __user *buffer, size_t *lenp, loff_t *ppos)
2625{
2626 unsigned int type = (int *)table->data - dqstats.stat;
2627
2628
2629 dqstats.stat[type] =
2630 percpu_counter_sum_positive(&dqstats.counter[type]);
2631 return proc_dointvec(table, write, buffer, lenp, ppos);
2632}
2633
2634static ctl_table fs_dqstats_table[] = {
2635 {
2636 .procname = "lookups",
2637 .data = &dqstats.stat[DQST_LOOKUPS],
2638 .maxlen = sizeof(int),
2639 .mode = 0444,
2640 .proc_handler = do_proc_dqstats,
2641 },
2642 {
2643 .procname = "drops",
2644 .data = &dqstats.stat[DQST_DROPS],
2645 .maxlen = sizeof(int),
2646 .mode = 0444,
2647 .proc_handler = do_proc_dqstats,
2648 },
2649 {
2650 .procname = "reads",
2651 .data = &dqstats.stat[DQST_READS],
2652 .maxlen = sizeof(int),
2653 .mode = 0444,
2654 .proc_handler = do_proc_dqstats,
2655 },
2656 {
2657 .procname = "writes",
2658 .data = &dqstats.stat[DQST_WRITES],
2659 .maxlen = sizeof(int),
2660 .mode = 0444,
2661 .proc_handler = do_proc_dqstats,
2662 },
2663 {
2664 .procname = "cache_hits",
2665 .data = &dqstats.stat[DQST_CACHE_HITS],
2666 .maxlen = sizeof(int),
2667 .mode = 0444,
2668 .proc_handler = do_proc_dqstats,
2669 },
2670 {
2671 .procname = "allocated_dquots",
2672 .data = &dqstats.stat[DQST_ALLOC_DQUOTS],
2673 .maxlen = sizeof(int),
2674 .mode = 0444,
2675 .proc_handler = do_proc_dqstats,
2676 },
2677 {
2678 .procname = "free_dquots",
2679 .data = &dqstats.stat[DQST_FREE_DQUOTS],
2680 .maxlen = sizeof(int),
2681 .mode = 0444,
2682 .proc_handler = do_proc_dqstats,
2683 },
2684 {
2685 .procname = "syncs",
2686 .data = &dqstats.stat[DQST_SYNCS],
2687 .maxlen = sizeof(int),
2688 .mode = 0444,
2689 .proc_handler = do_proc_dqstats,
2690 },
2691#ifdef CONFIG_PRINT_QUOTA_WARNING
2692 {
2693 .procname = "warnings",
2694 .data = &flag_print_warnings,
2695 .maxlen = sizeof(int),
2696 .mode = 0644,
2697 .proc_handler = proc_dointvec,
2698 },
2699#endif
2700 { },
2701};
2702
2703static ctl_table fs_table[] = {
2704 {
2705 .procname = "quota",
2706 .mode = 0555,
2707 .child = fs_dqstats_table,
2708 },
2709 { },
2710};
2711
2712static ctl_table sys_table[] = {
2713 {
2714 .procname = "fs",
2715 .mode = 0555,
2716 .child = fs_table,
2717 },
2718 { },
2719};
2720
2721static int __init dquot_init(void)
2722{
2723 int i, ret;
2724 unsigned long nr_hash, order;
2725
2726 printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
2727
2728 register_sysctl_table(sys_table);
2729
2730 dquot_cachep = kmem_cache_create("dquot",
2731 sizeof(struct dquot), sizeof(unsigned long) * 4,
2732 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
2733 SLAB_MEM_SPREAD|SLAB_PANIC),
2734 NULL);
2735
2736 order = 0;
2737 dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
2738 if (!dquot_hash)
2739 panic("Cannot create dquot hash table");
2740
2741 for (i = 0; i < _DQST_DQSTAT_LAST; i++) {
2742 ret = percpu_counter_init(&dqstats.counter[i], 0, GFP_KERNEL);
2743 if (ret)
2744 panic("Cannot create dquot stat counters");
2745 }
2746
2747
2748 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2749 dq_hash_bits = 0;
2750 do {
2751 dq_hash_bits++;
2752 } while (nr_hash >> dq_hash_bits);
2753 dq_hash_bits--;
2754
2755 nr_hash = 1UL << dq_hash_bits;
2756 dq_hash_mask = nr_hash - 1;
2757 for (i = 0; i < nr_hash; i++)
2758 INIT_HLIST_HEAD(dquot_hash + i);
2759
2760 printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
2761 nr_hash, order, (PAGE_SIZE << order));
2762
2763 register_shrinker(&dqcache_shrinker);
2764
2765 return 0;
2766}
2767module_init(dquot_init);
2768