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