hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/quota/dquot.c
....@@ -9,7 +9,7 @@
99 * on the Melbourne quota system as used on BSD derived systems. The internal
1010 * implementation is based on one of the several variants of the LINUX
1111 * inode-subsystem with added complexity of the diskquota system.
12
- *
12
+ *
1313 * Author: Marco van Wieringen <mvw@planets.elm.net>
1414 *
1515 * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
....@@ -51,7 +51,7 @@
5151 * Added journalled quota support, fix lock inversion problems
5252 * Jan Kara, <jack@suse.cz>, 2003,2004
5353 *
54
- * (C) Copyright 1994 - 1997 Marco van Wieringen
54
+ * (C) Copyright 1994 - 1997 Marco van Wieringen
5555 */
5656
5757 #include <linux/errno.h>
....@@ -78,6 +78,8 @@
7878 #include <linux/namei.h>
7979 #include <linux/capability.h>
8080 #include <linux/quotaops.h>
81
+#include <linux/blkdev.h>
82
+#include <linux/sched/mm.h>
8183 #include "../internal.h" /* ugh */
8284
8385 #include <linux/uaccess.h>
....@@ -197,7 +199,7 @@
197199 int qm;
198200
199201 spin_unlock(&dq_list_lock);
200
-
202
+
201203 for (qm = 0; module_names[qm].qm_fmt_id &&
202204 module_names[qm].qm_fmt_id != id; qm++)
203205 ;
....@@ -223,18 +225,32 @@
223225
224226 /*
225227 * Dquot List Management:
226
- * The quota code uses three lists for dquot management: the inuse_list,
227
- * free_dquots, and dquot_hash[] array. A single dquot structure may be
228
- * on all three lists, depending on its current state.
228
+ * The quota code uses five lists for dquot management: the inuse_list,
229
+ * releasing_dquots, free_dquots, dqi_dirty_list, and dquot_hash[] array.
230
+ * A single dquot structure may be on some of those lists, depending on
231
+ * its current state.
229232 *
230233 * All dquots are placed to the end of inuse_list when first created, and this
231234 * list is used for invalidate operation, which must look at every dquot.
235
+ *
236
+ * When the last reference of a dquot will be dropped, the dquot will be
237
+ * added to releasing_dquots. We'd then queue work item which would call
238
+ * synchronize_srcu() and after that perform the final cleanup of all the
239
+ * dquots on the list. Both releasing_dquots and free_dquots use the
240
+ * dq_free list_head in the dquot struct. When a dquot is removed from
241
+ * releasing_dquots, a reference count is always subtracted, and if
242
+ * dq_count == 0 at that point, the dquot will be added to the free_dquots.
232243 *
233244 * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
234245 * and this list is searched whenever we need an available dquot. Dquots are
235246 * removed from the list as soon as they are used again, and
236247 * dqstats.free_dquots gives the number of dquots on the list. When
237248 * dquot is invalidated it's completely released from memory.
249
+ *
250
+ * Dirty dquots are added to the dqi_dirty_list of quota_info when mark
251
+ * dirtied, and this list is searched when writing dirty dquots back to
252
+ * quota file. Note that some filesystems do dirty dquot tracking on their
253
+ * own (e.g. in a journal) and thus don't use dqi_dirty_list.
238254 *
239255 * Dquots with a specific identity (device, type and id) are placed on
240256 * one of the dquot_hash[] hash chains. The provides an efficient search
....@@ -243,6 +259,7 @@
243259
244260 static LIST_HEAD(inuse_list);
245261 static LIST_HEAD(free_dquots);
262
+static LIST_HEAD(releasing_dquots);
246263 static unsigned int dq_hash_bits, dq_hash_mask;
247264 static struct hlist_head *dquot_hash;
248265
....@@ -252,6 +269,9 @@
252269 static qsize_t inode_get_rsv_space(struct inode *inode);
253270 static qsize_t __inode_get_rsv_space(struct inode *inode);
254271 static int __dquot_initialize(struct inode *inode, int type);
272
+
273
+static void quota_release_workfn(struct work_struct *work);
274
+static DECLARE_DELAYED_WORK(quota_release_work, quota_release_workfn);
255275
256276 static inline unsigned int
257277 hashfn(const struct super_block *sb, struct kqid qid)
....@@ -300,12 +320,18 @@
300320 dqstats_inc(DQST_FREE_DQUOTS);
301321 }
302322
323
+static inline void put_releasing_dquots(struct dquot *dquot)
324
+{
325
+ list_add_tail(&dquot->dq_free, &releasing_dquots);
326
+}
327
+
303328 static inline void remove_free_dquot(struct dquot *dquot)
304329 {
305330 if (list_empty(&dquot->dq_free))
306331 return;
307332 list_del_init(&dquot->dq_free);
308
- dqstats_dec(DQST_FREE_DQUOTS);
333
+ if (!atomic_read(&dquot->dq_count))
334
+ dqstats_dec(DQST_FREE_DQUOTS);
309335 }
310336
311337 static inline void put_inuse(struct dquot *dquot)
....@@ -331,6 +357,11 @@
331357 mutex_unlock(&dquot->dq_lock);
332358 }
333359
360
+static inline int dquot_active(struct dquot *dquot)
361
+{
362
+ return test_bit(DQ_ACTIVE_B, &dquot->dq_flags);
363
+}
364
+
334365 static inline int dquot_dirty(struct dquot *dquot)
335366 {
336367 return test_bit(DQ_MOD_B, &dquot->dq_flags);
....@@ -346,14 +377,14 @@
346377 {
347378 int ret = 1;
348379
349
- if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
380
+ if (!dquot_active(dquot))
350381 return 0;
351382
352383 if (sb_dqopt(dquot->dq_sb)->flags & DQUOT_NOLIST_DIRTY)
353384 return test_and_set_bit(DQ_MOD_B, &dquot->dq_flags);
354385
355386 /* If quota is dirty already, we don't have to acquire dq_list_lock */
356
- if (test_bit(DQ_MOD_B, &dquot->dq_flags))
387
+ if (dquot_dirty(dquot))
357388 return 1;
358389
359390 spin_lock(&dq_list_lock);
....@@ -421,18 +452,21 @@
421452 int dquot_acquire(struct dquot *dquot)
422453 {
423454 int ret = 0, ret2 = 0;
455
+ unsigned int memalloc;
424456 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
425457
426458 mutex_lock(&dquot->dq_lock);
427
- if (!test_bit(DQ_READ_B, &dquot->dq_flags))
459
+ memalloc = memalloc_nofs_save();
460
+ if (!test_bit(DQ_READ_B, &dquot->dq_flags)) {
428461 ret = dqopt->ops[dquot->dq_id.type]->read_dqblk(dquot);
429
- if (ret < 0)
430
- goto out_iolock;
462
+ if (ret < 0)
463
+ goto out_iolock;
464
+ }
431465 /* Make sure flags update is visible after dquot has been filled */
432466 smp_mb__before_atomic();
433467 set_bit(DQ_READ_B, &dquot->dq_flags);
434468 /* Instantiate dquot if needed */
435
- if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
469
+ if (!dquot_active(dquot) && !dquot->dq_off) {
436470 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
437471 /* Write the info if needed */
438472 if (info_dirty(&dqopt->info[dquot->dq_id.type])) {
....@@ -453,6 +487,7 @@
453487 smp_mb__before_atomic();
454488 set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
455489 out_iolock:
490
+ memalloc_nofs_restore(memalloc);
456491 mutex_unlock(&dquot->dq_lock);
457492 return ret;
458493 }
....@@ -464,18 +499,21 @@
464499 int dquot_commit(struct dquot *dquot)
465500 {
466501 int ret = 0;
502
+ unsigned int memalloc;
467503 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
468504
469505 mutex_lock(&dquot->dq_lock);
506
+ memalloc = memalloc_nofs_save();
470507 if (!clear_dquot_dirty(dquot))
471508 goto out_lock;
472509 /* Inactive dquot can be only if there was error during read/init
473510 * => we have better not writing it */
474
- if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
511
+ if (dquot_active(dquot))
475512 ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk(dquot);
476513 else
477514 ret = -EIO;
478515 out_lock:
516
+ memalloc_nofs_restore(memalloc);
479517 mutex_unlock(&dquot->dq_lock);
480518 return ret;
481519 }
....@@ -487,9 +525,11 @@
487525 int dquot_release(struct dquot *dquot)
488526 {
489527 int ret = 0, ret2 = 0;
528
+ unsigned int memalloc;
490529 struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
491530
492531 mutex_lock(&dquot->dq_lock);
532
+ memalloc = memalloc_nofs_save();
493533 /* Check whether we are not racing with some other dqget() */
494534 if (dquot_is_busy(dquot))
495535 goto out_dqlock;
....@@ -505,6 +545,7 @@
505545 }
506546 clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
507547 out_dqlock:
548
+ memalloc_nofs_restore(memalloc);
508549 mutex_unlock(&dquot->dq_lock);
509550 return ret;
510551 }
....@@ -532,6 +573,8 @@
532573 struct dquot *dquot, *tmp;
533574
534575 restart:
576
+ flush_delayed_work(&quota_release_work);
577
+
535578 spin_lock(&dq_list_lock);
536579 list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
537580 if (dquot->dq_sb != sb)
....@@ -540,7 +583,13 @@
540583 continue;
541584 /* Wait for dquot users */
542585 if (atomic_read(&dquot->dq_count)) {
543
- dqgrab(dquot);
586
+ /* dquot in releasing_dquots, flush and retry */
587
+ if (!list_empty(&dquot->dq_free)) {
588
+ spin_unlock(&dq_list_lock);
589
+ goto restart;
590
+ }
591
+
592
+ atomic_inc(&dquot->dq_count);
544593 spin_unlock(&dq_list_lock);
545594 /*
546595 * Once dqput() wakes us up, we know it's time to free
....@@ -582,14 +631,13 @@
582631
583632 spin_lock(&dq_list_lock);
584633 list_for_each_entry(dquot, &inuse_list, dq_inuse) {
585
- if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags))
634
+ if (!dquot_active(dquot))
586635 continue;
587636 if (dquot->dq_sb != sb)
588637 continue;
589638 /* Now we have active dquot so we can just increase use count */
590639 atomic_inc(&dquot->dq_count);
591640 spin_unlock(&dq_list_lock);
592
- dqstats_inc(DQST_LOOKUPS);
593641 dqput(old_dquot);
594642 old_dquot = dquot;
595643 /*
....@@ -598,7 +646,7 @@
598646 * outstanding call and recheck the DQ_ACTIVE_B after that.
599647 */
600648 wait_on_dquot(dquot);
601
- if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
649
+ if (dquot_active(dquot)) {
602650 ret = fn(dquot, priv);
603651 if (ret < 0)
604652 goto out;
....@@ -613,6 +661,18 @@
613661 return ret;
614662 }
615663 EXPORT_SYMBOL(dquot_scan_active);
664
+
665
+static inline int dquot_write_dquot(struct dquot *dquot)
666
+{
667
+ int ret = dquot->dq_sb->dq_op->write_dquot(dquot);
668
+ if (ret < 0) {
669
+ quota_error(dquot->dq_sb, "Can't write quota structure "
670
+ "(error %d). Quota may get out of sync!", ret);
671
+ /* Clear dirty bit anyway to avoid infinite loop. */
672
+ clear_dquot_dirty(dquot);
673
+ }
674
+ return ret;
675
+}
616676
617677 /* Write all dquot structures to quota files */
618678 int dquot_writeback_dquots(struct super_block *sb, int type)
....@@ -637,24 +697,16 @@
637697 dquot = list_first_entry(&dirty, struct dquot,
638698 dq_dirty);
639699
640
- WARN_ON(!test_bit(DQ_ACTIVE_B, &dquot->dq_flags));
700
+ WARN_ON(!dquot_active(dquot));
641701
642702 /* Now we have active dquot from which someone is
643703 * holding reference so we can safely just increase
644704 * use count */
645705 dqgrab(dquot);
646706 spin_unlock(&dq_list_lock);
647
- dqstats_inc(DQST_LOOKUPS);
648
- err = sb->dq_op->write_dquot(dquot);
649
- if (err) {
650
- /*
651
- * Clear dirty bit anyway to avoid infinite
652
- * loop here.
653
- */
654
- clear_dquot_dirty(dquot);
655
- if (!ret)
656
- ret = err;
657
- }
707
+ err = dquot_write_dquot(dquot);
708
+ if (err && !ret)
709
+ ret = err;
658710 dqput(dquot);
659711 spin_lock(&dq_list_lock);
660712 }
....@@ -748,12 +800,53 @@
748800 };
749801
750802 /*
803
+ * Safely release dquot and put reference to dquot.
804
+ */
805
+static void quota_release_workfn(struct work_struct *work)
806
+{
807
+ struct dquot *dquot;
808
+ struct list_head rls_head;
809
+
810
+ spin_lock(&dq_list_lock);
811
+ /* Exchange the list head to avoid livelock. */
812
+ list_replace_init(&releasing_dquots, &rls_head);
813
+ spin_unlock(&dq_list_lock);
814
+
815
+restart:
816
+ synchronize_srcu(&dquot_srcu);
817
+ spin_lock(&dq_list_lock);
818
+ while (!list_empty(&rls_head)) {
819
+ dquot = list_first_entry(&rls_head, struct dquot, dq_free);
820
+ /* Dquot got used again? */
821
+ if (atomic_read(&dquot->dq_count) > 1) {
822
+ remove_free_dquot(dquot);
823
+ atomic_dec(&dquot->dq_count);
824
+ continue;
825
+ }
826
+ if (dquot_dirty(dquot)) {
827
+ spin_unlock(&dq_list_lock);
828
+ /* Commit dquot before releasing */
829
+ dquot_write_dquot(dquot);
830
+ goto restart;
831
+ }
832
+ if (dquot_active(dquot)) {
833
+ spin_unlock(&dq_list_lock);
834
+ dquot->dq_sb->dq_op->release_dquot(dquot);
835
+ goto restart;
836
+ }
837
+ /* Dquot is inactive and clean, now move it to free list */
838
+ remove_free_dquot(dquot);
839
+ atomic_dec(&dquot->dq_count);
840
+ put_dquot_last(dquot);
841
+ }
842
+ spin_unlock(&dq_list_lock);
843
+}
844
+
845
+/*
751846 * Put reference to dquot
752847 */
753848 void dqput(struct dquot *dquot)
754849 {
755
- int ret;
756
-
757850 if (!dquot)
758851 return;
759852 #ifdef CONFIG_QUOTA_DEBUG
....@@ -765,7 +858,7 @@
765858 }
766859 #endif
767860 dqstats_inc(DQST_DROPS);
768
-we_slept:
861
+
769862 spin_lock(&dq_list_lock);
770863 if (atomic_read(&dquot->dq_count) > 1) {
771864 /* We have more than one user... nothing to do */
....@@ -777,35 +870,15 @@
777870 spin_unlock(&dq_list_lock);
778871 return;
779872 }
873
+
780874 /* Need to release dquot? */
781
- if (dquot_dirty(dquot)) {
782
- spin_unlock(&dq_list_lock);
783
- /* Commit dquot before releasing */
784
- ret = dquot->dq_sb->dq_op->write_dquot(dquot);
785
- if (ret < 0) {
786
- quota_error(dquot->dq_sb, "Can't write quota structure"
787
- " (error %d). Quota may get out of sync!",
788
- ret);
789
- /*
790
- * We clear dirty bit anyway, so that we avoid
791
- * infinite loop here
792
- */
793
- clear_dquot_dirty(dquot);
794
- }
795
- goto we_slept;
796
- }
797
- if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
798
- spin_unlock(&dq_list_lock);
799
- dquot->dq_sb->dq_op->release_dquot(dquot);
800
- goto we_slept;
801
- }
802
- atomic_dec(&dquot->dq_count);
803875 #ifdef CONFIG_QUOTA_DEBUG
804876 /* sanity check */
805877 BUG_ON(!list_empty(&dquot->dq_free));
806878 #endif
807
- put_dquot_last(dquot);
879
+ put_releasing_dquots(dquot);
808880 spin_unlock(&dq_list_lock);
881
+ queue_delayed_work(system_unbound_wq, &quota_release_work, 1);
809882 }
810883 EXPORT_SYMBOL(dqput);
811884
....@@ -895,7 +968,7 @@
895968 * already finished or it will be canceled due to dq_count > 1 test */
896969 wait_on_dquot(dquot);
897970 /* Read the dquot / allocate space in quota file */
898
- if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
971
+ if (!dquot_active(dquot)) {
899972 int err;
900973
901974 err = sb->dq_op->acquire_dquot(dquot);
....@@ -1056,7 +1129,9 @@
10561129 struct list_head *tofree_head)
10571130 {
10581131 struct inode *inode;
1132
+#ifdef CONFIG_QUOTA_DEBUG
10591133 int reserved = 0;
1134
+#endif
10601135
10611136 spin_lock(&sb->s_inode_list_lock);
10621137 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
....@@ -1068,8 +1143,10 @@
10681143 */
10691144 spin_lock(&dq_data_lock);
10701145 if (!IS_NOQUOTA(inode)) {
1146
+#ifdef CONFIG_QUOTA_DEBUG
10711147 if (unlikely(inode_get_rsv_space(inode) > 0))
10721148 reserved = 1;
1149
+#endif
10731150 remove_inode_dquot_ref(inode, type, tofree_head);
10741151 }
10751152 spin_unlock(&dq_data_lock);
....@@ -1408,7 +1485,7 @@
14081485 return QUOTA_NL_NOWARN;
14091486 }
14101487
1411
-static int dquot_active(const struct inode *inode)
1488
+static int inode_quota_active(const struct inode *inode)
14121489 {
14131490 struct super_block *sb = inode->i_sb;
14141491
....@@ -1431,7 +1508,7 @@
14311508 qsize_t rsv;
14321509 int ret = 0;
14331510
1434
- if (!dquot_active(inode))
1511
+ if (!inode_quota_active(inode))
14351512 return 0;
14361513
14371514 dquots = i_dquot(inode);
....@@ -1539,7 +1616,7 @@
15391616 struct dquot **dquots;
15401617 int i;
15411618
1542
- if (!dquot_active(inode))
1619
+ if (!inode_quota_active(inode))
15431620 return false;
15441621
15451622 dquots = i_dquot(inode);
....@@ -1650,7 +1727,7 @@
16501727 int reserve = flags & DQUOT_SPACE_RESERVE;
16511728 struct dquot **dquots;
16521729
1653
- if (!dquot_active(inode)) {
1730
+ if (!inode_quota_active(inode)) {
16541731 if (reserve) {
16551732 spin_lock(&inode->i_lock);
16561733 *inode_reserved_space(inode) += number;
....@@ -1670,7 +1747,7 @@
16701747 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
16711748 if (!dquots[cnt])
16721749 continue;
1673
- if (flags & DQUOT_SPACE_RESERVE) {
1750
+ if (reserve) {
16741751 ret = dquot_add_space(dquots[cnt], 0, number, flags,
16751752 &warn[cnt]);
16761753 } else {
....@@ -1683,13 +1760,11 @@
16831760 if (!dquots[cnt])
16841761 continue;
16851762 spin_lock(&dquots[cnt]->dq_dqb_lock);
1686
- if (flags & DQUOT_SPACE_RESERVE) {
1687
- dquots[cnt]->dq_dqb.dqb_rsvspace -=
1688
- number;
1689
- } else {
1690
- dquots[cnt]->dq_dqb.dqb_curspace -=
1691
- number;
1692
- }
1763
+ if (reserve)
1764
+ dquot_free_reserved_space(dquots[cnt],
1765
+ number);
1766
+ else
1767
+ dquot_decr_space(dquots[cnt], number);
16931768 spin_unlock(&dquots[cnt]->dq_dqb_lock);
16941769 }
16951770 spin_unlock(&inode->i_lock);
....@@ -1722,7 +1797,7 @@
17221797 struct dquot_warn warn[MAXQUOTAS];
17231798 struct dquot * const *dquots;
17241799
1725
- if (!dquot_active(inode))
1800
+ if (!inode_quota_active(inode))
17261801 return 0;
17271802 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
17281803 warn[cnt].w_type = QUOTA_NL_NOWARN;
....@@ -1740,7 +1815,7 @@
17401815 continue;
17411816 /* Back out changes we already did */
17421817 spin_lock(&dquots[cnt]->dq_dqb_lock);
1743
- dquots[cnt]->dq_dqb.dqb_curinodes--;
1818
+ dquot_decr_inodes(dquots[cnt], 1);
17441819 spin_unlock(&dquots[cnt]->dq_dqb_lock);
17451820 }
17461821 goto warn_put_all;
....@@ -1765,7 +1840,7 @@
17651840 struct dquot **dquots;
17661841 int cnt, index;
17671842
1768
- if (!dquot_active(inode)) {
1843
+ if (!inode_quota_active(inode)) {
17691844 spin_lock(&inode->i_lock);
17701845 *inode_reserved_space(inode) -= number;
17711846 __inode_add_bytes(inode, number);
....@@ -1807,7 +1882,7 @@
18071882 struct dquot **dquots;
18081883 int cnt, index;
18091884
1810
- if (!dquot_active(inode)) {
1885
+ if (!inode_quota_active(inode)) {
18111886 spin_lock(&inode->i_lock);
18121887 *inode_reserved_space(inode) += number;
18131888 __inode_sub_bytes(inode, number);
....@@ -1851,7 +1926,7 @@
18511926 struct dquot **dquots;
18521927 int reserve = flags & DQUOT_SPACE_RESERVE, index;
18531928
1854
- if (!dquot_active(inode)) {
1929
+ if (!inode_quota_active(inode)) {
18551930 if (reserve) {
18561931 spin_lock(&inode->i_lock);
18571932 *inode_reserved_space(inode) -= number;
....@@ -1906,7 +1981,7 @@
19061981 struct dquot * const *dquots;
19071982 int index;
19081983
1909
- if (!dquot_active(inode))
1984
+ if (!inode_quota_active(inode))
19101985 return;
19111986
19121987 dquots = i_dquot(inode);
....@@ -2077,7 +2152,7 @@
20772152 struct super_block *sb = inode->i_sb;
20782153 int ret;
20792154
2080
- if (!dquot_active(inode))
2155
+ if (!inode_quota_active(inode))
20812156 return 0;
20822157
20832158 if (iattr->ia_valid & ATTR_UID && !uid_eq(iattr->ia_uid, inode->i_uid)){
....@@ -2161,14 +2236,29 @@
21612236 }
21622237 EXPORT_SYMBOL(dquot_file_open);
21632238
2239
+static void vfs_cleanup_quota_inode(struct super_block *sb, int type)
2240
+{
2241
+ struct quota_info *dqopt = sb_dqopt(sb);
2242
+ struct inode *inode = dqopt->files[type];
2243
+
2244
+ if (!inode)
2245
+ return;
2246
+ if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2247
+ inode_lock(inode);
2248
+ inode->i_flags &= ~S_NOQUOTA;
2249
+ inode_unlock(inode);
2250
+ }
2251
+ dqopt->files[type] = NULL;
2252
+ iput(inode);
2253
+}
2254
+
21642255 /*
21652256 * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
21662257 */
21672258 int dquot_disable(struct super_block *sb, int type, unsigned int flags)
21682259 {
2169
- int cnt, ret = 0;
2260
+ int cnt;
21702261 struct quota_info *dqopt = sb_dqopt(sb);
2171
- struct inode *toputinode[MAXQUOTAS];
21722262
21732263 /* s_umount should be held in exclusive mode */
21742264 if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
....@@ -2190,7 +2280,6 @@
21902280 return 0;
21912281
21922282 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
2193
- toputinode[cnt] = NULL;
21942283 if (type != -1 && cnt != type)
21952284 continue;
21962285 if (!sb_has_quota_loaded(sb, cnt))
....@@ -2210,8 +2299,7 @@
22102299 dqopt->flags &= ~dquot_state_flag(
22112300 DQUOT_SUSPENDED, cnt);
22122301 spin_unlock(&dq_state_lock);
2213
- iput(dqopt->files[cnt]);
2214
- dqopt->files[cnt] = NULL;
2302
+ vfs_cleanup_quota_inode(sb, cnt);
22152303 continue;
22162304 }
22172305 spin_unlock(&dq_state_lock);
....@@ -2233,10 +2321,6 @@
22332321 if (dqopt->ops[cnt]->free_file_info)
22342322 dqopt->ops[cnt]->free_file_info(sb, cnt);
22352323 put_quota_format(dqopt->info[cnt].dqi_format);
2236
-
2237
- toputinode[cnt] = dqopt->files[cnt];
2238
- if (!sb_has_quota_loaded(sb, cnt))
2239
- dqopt->files[cnt] = NULL;
22402324 dqopt->info[cnt].dqi_flags = 0;
22412325 dqopt->info[cnt].dqi_igrace = 0;
22422326 dqopt->info[cnt].dqi_bgrace = 0;
....@@ -2258,32 +2342,22 @@
22582342 * must also discard the blockdev buffers so that we see the
22592343 * changes done by userspace on the next quotaon() */
22602344 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2261
- /* This can happen when suspending quotas on remount-ro... */
2262
- if (toputinode[cnt] && !sb_has_quota_loaded(sb, cnt)) {
2263
- inode_lock(toputinode[cnt]);
2264
- toputinode[cnt]->i_flags &= ~S_NOQUOTA;
2265
- truncate_inode_pages(&toputinode[cnt]->i_data, 0);
2266
- inode_unlock(toputinode[cnt]);
2267
- mark_inode_dirty_sync(toputinode[cnt]);
2345
+ if (!sb_has_quota_loaded(sb, cnt) && dqopt->files[cnt]) {
2346
+ inode_lock(dqopt->files[cnt]);
2347
+ truncate_inode_pages(&dqopt->files[cnt]->i_data, 0);
2348
+ inode_unlock(dqopt->files[cnt]);
22682349 }
22692350 if (sb->s_bdev)
22702351 invalidate_bdev(sb->s_bdev);
22712352 put_inodes:
2353
+ /* We are done when suspending quotas */
2354
+ if (flags & DQUOT_SUSPENDED)
2355
+ return 0;
2356
+
22722357 for (cnt = 0; cnt < MAXQUOTAS; cnt++)
2273
- if (toputinode[cnt]) {
2274
- /* On remount RO, we keep the inode pointer so that we
2275
- * can reenable quota on the subsequent remount RW. We
2276
- * have to check 'flags' variable and not use sb_has_
2277
- * function because another quotaon / quotaoff could
2278
- * change global state before we got here. We refuse
2279
- * to suspend quotas when there is pending delete on
2280
- * the quota file... */
2281
- if (!(flags & DQUOT_SUSPENDED))
2282
- iput(toputinode[cnt]);
2283
- else if (!toputinode[cnt]->i_nlink)
2284
- ret = -EBUSY;
2285
- }
2286
- return ret;
2358
+ if (!sb_has_quota_loaded(sb, cnt))
2359
+ vfs_cleanup_quota_inode(sb, cnt);
2360
+ return 0;
22872361 }
22882362 EXPORT_SYMBOL(dquot_disable);
22892363
....@@ -2298,28 +2372,54 @@
22982372 * Turn quotas on on a device
22992373 */
23002374
2301
-/*
2302
- * Helper function to turn quotas on when we already have the inode of
2303
- * quota file and no quota information is loaded.
2304
- */
2305
-static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
2375
+static int vfs_setup_quota_inode(struct inode *inode, int type)
2376
+{
2377
+ struct super_block *sb = inode->i_sb;
2378
+ struct quota_info *dqopt = sb_dqopt(sb);
2379
+
2380
+ if (is_bad_inode(inode))
2381
+ return -EUCLEAN;
2382
+ if (!S_ISREG(inode->i_mode))
2383
+ return -EACCES;
2384
+ if (IS_RDONLY(inode))
2385
+ return -EROFS;
2386
+ if (sb_has_quota_loaded(sb, type))
2387
+ return -EBUSY;
2388
+
2389
+ dqopt->files[type] = igrab(inode);
2390
+ if (!dqopt->files[type])
2391
+ return -EIO;
2392
+ if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2393
+ /* We don't want quota and atime on quota files (deadlocks
2394
+ * possible) Also nobody should write to the file - we use
2395
+ * special IO operations which ignore the immutable bit. */
2396
+ inode_lock(inode);
2397
+ inode->i_flags |= S_NOQUOTA;
2398
+ inode_unlock(inode);
2399
+ /*
2400
+ * When S_NOQUOTA is set, remove dquot references as no more
2401
+ * references can be added
2402
+ */
2403
+ __dquot_drop(inode);
2404
+ }
2405
+ return 0;
2406
+}
2407
+
2408
+int dquot_load_quota_sb(struct super_block *sb, int type, int format_id,
23062409 unsigned int flags)
23072410 {
23082411 struct quota_format_type *fmt = find_quota_format(format_id);
2309
- struct super_block *sb = inode->i_sb;
23102412 struct quota_info *dqopt = sb_dqopt(sb);
23112413 int error;
23122414
2415
+ /* Just unsuspend quotas? */
2416
+ BUG_ON(flags & DQUOT_SUSPENDED);
2417
+ /* s_umount should be held in exclusive mode */
2418
+ if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2419
+ up_read(&sb->s_umount);
2420
+
23132421 if (!fmt)
23142422 return -ESRCH;
2315
- if (!S_ISREG(inode->i_mode)) {
2316
- error = -EACCES;
2317
- goto out_fmt;
2318
- }
2319
- if (IS_RDONLY(inode)) {
2320
- error = -EROFS;
2321
- goto out_fmt;
2322
- }
23232423 if (!sb->s_op->quota_write || !sb->s_op->quota_read ||
23242424 (type == PRJQUOTA && sb->dq_op->get_projid == NULL)) {
23252425 error = -EINVAL;
....@@ -2351,27 +2451,9 @@
23512451 invalidate_bdev(sb->s_bdev);
23522452 }
23532453
2354
- if (!(dqopt->flags & DQUOT_QUOTA_SYS_FILE)) {
2355
- /* We don't want quota and atime on quota files (deadlocks
2356
- * possible) Also nobody should write to the file - we use
2357
- * special IO operations which ignore the immutable bit. */
2358
- inode_lock(inode);
2359
- inode->i_flags |= S_NOQUOTA;
2360
- inode_unlock(inode);
2361
- /*
2362
- * When S_NOQUOTA is set, remove dquot references as no more
2363
- * references can be added
2364
- */
2365
- __dquot_drop(inode);
2366
- }
2367
-
2368
- error = -EIO;
2369
- dqopt->files[type] = igrab(inode);
2370
- if (!dqopt->files[type])
2371
- goto out_file_flags;
23722454 error = -EINVAL;
23732455 if (!fmt->qf_ops->check_quota_file(sb, type))
2374
- goto out_file_init;
2456
+ goto out_fmt;
23752457
23762458 dqopt->ops[type] = fmt->qf_ops;
23772459 dqopt->info[type].dqi_format = fmt;
....@@ -2379,7 +2461,7 @@
23792461 INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
23802462 error = dqopt->ops[type]->read_file_info(sb, type);
23812463 if (error < 0)
2382
- goto out_file_init;
2464
+ goto out_fmt;
23832465 if (dqopt->flags & DQUOT_QUOTA_SYS_FILE) {
23842466 spin_lock(&dq_data_lock);
23852467 dqopt->info[type].dqi_flags |= DQF_SYS_FILE;
....@@ -2391,27 +2473,40 @@
23912473
23922474 error = add_dquot_ref(sb, type);
23932475 if (error)
2394
- dquot_disable(sb, type, flags);
2476
+ dquot_disable(sb, type,
2477
+ DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
23952478
23962479 return error;
2397
-out_file_init:
2398
- dqopt->files[type] = NULL;
2399
- iput(inode);
2400
-out_file_flags:
2401
- inode_lock(inode);
2402
- inode->i_flags &= ~S_NOQUOTA;
2403
- inode_unlock(inode);
24042480 out_fmt:
24052481 put_quota_format(fmt);
24062482
2407
- return error;
2483
+ return error;
24082484 }
2485
+EXPORT_SYMBOL(dquot_load_quota_sb);
2486
+
2487
+/*
2488
+ * More powerful function for turning on quotas on given quota inode allowing
2489
+ * setting of individual quota flags
2490
+ */
2491
+int dquot_load_quota_inode(struct inode *inode, int type, int format_id,
2492
+ unsigned int flags)
2493
+{
2494
+ int err;
2495
+
2496
+ err = vfs_setup_quota_inode(inode, type);
2497
+ if (err < 0)
2498
+ return err;
2499
+ err = dquot_load_quota_sb(inode->i_sb, type, format_id, flags);
2500
+ if (err < 0)
2501
+ vfs_cleanup_quota_inode(inode->i_sb, type);
2502
+ return err;
2503
+}
2504
+EXPORT_SYMBOL(dquot_load_quota_inode);
24092505
24102506 /* Reenable quotas on remount RW */
24112507 int dquot_resume(struct super_block *sb, int type)
24122508 {
24132509 struct quota_info *dqopt = sb_dqopt(sb);
2414
- struct inode *inode;
24152510 int ret = 0, cnt;
24162511 unsigned int flags;
24172512
....@@ -2425,8 +2520,6 @@
24252520 if (!sb_has_quota_suspended(sb, cnt))
24262521 continue;
24272522
2428
- inode = dqopt->files[cnt];
2429
- dqopt->files[cnt] = NULL;
24302523 spin_lock(&dq_state_lock);
24312524 flags = dqopt->flags & dquot_state_flag(DQUOT_USAGE_ENABLED |
24322525 DQUOT_LIMITS_ENABLED,
....@@ -2435,9 +2528,10 @@
24352528 spin_unlock(&dq_state_lock);
24362529
24372530 flags = dquot_generic_flag(flags, cnt);
2438
- ret = vfs_load_quota_inode(inode, cnt,
2439
- dqopt->info[cnt].dqi_fmt_id, flags);
2440
- iput(inode);
2531
+ ret = dquot_load_quota_sb(sb, cnt, dqopt->info[cnt].dqi_fmt_id,
2532
+ flags);
2533
+ if (ret < 0)
2534
+ vfs_cleanup_quota_inode(sb, cnt);
24412535 }
24422536
24432537 return ret;
....@@ -2454,47 +2548,12 @@
24542548 if (path->dentry->d_sb != sb)
24552549 error = -EXDEV;
24562550 else
2457
- error = vfs_load_quota_inode(d_inode(path->dentry), type,
2551
+ error = dquot_load_quota_inode(d_inode(path->dentry), type,
24582552 format_id, DQUOT_USAGE_ENABLED |
24592553 DQUOT_LIMITS_ENABLED);
24602554 return error;
24612555 }
24622556 EXPORT_SYMBOL(dquot_quota_on);
2463
-
2464
-/*
2465
- * More powerful function for turning on quotas allowing setting
2466
- * of individual quota flags
2467
- */
2468
-int dquot_enable(struct inode *inode, int type, int format_id,
2469
- unsigned int flags)
2470
-{
2471
- struct super_block *sb = inode->i_sb;
2472
-
2473
- /* Just unsuspend quotas? */
2474
- BUG_ON(flags & DQUOT_SUSPENDED);
2475
- /* s_umount should be held in exclusive mode */
2476
- if (WARN_ON_ONCE(down_read_trylock(&sb->s_umount)))
2477
- up_read(&sb->s_umount);
2478
-
2479
- if (!flags)
2480
- return 0;
2481
- /* Just updating flags needed? */
2482
- if (sb_has_quota_loaded(sb, type)) {
2483
- if (flags & DQUOT_USAGE_ENABLED &&
2484
- sb_has_quota_usage_enabled(sb, type))
2485
- return -EBUSY;
2486
- if (flags & DQUOT_LIMITS_ENABLED &&
2487
- sb_has_quota_limits_enabled(sb, type))
2488
- return -EBUSY;
2489
- spin_lock(&dq_state_lock);
2490
- sb_dqopt(sb)->flags |= dquot_state_flag(flags, type);
2491
- spin_unlock(&dq_state_lock);
2492
- return 0;
2493
- }
2494
-
2495
- return vfs_load_quota_inode(inode, type, format_id, flags);
2496
-}
2497
-EXPORT_SYMBOL(dquot_enable);
24982557
24992558 /*
25002559 * This function is used when filesystem needs to initialize quotas
....@@ -2506,21 +2565,15 @@
25062565 struct dentry *dentry;
25072566 int error;
25082567
2509
- dentry = lookup_one_len_unlocked(qf_name, sb->s_root, strlen(qf_name));
2568
+ dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
25102569 if (IS_ERR(dentry))
25112570 return PTR_ERR(dentry);
25122571
2513
- if (d_really_is_negative(dentry)) {
2514
- error = -ENOENT;
2515
- goto out;
2516
- }
2517
-
25182572 error = security_quota_on(dentry);
25192573 if (!error)
2520
- error = vfs_load_quota_inode(d_inode(dentry), type, format_id,
2574
+ error = dquot_load_quota_inode(d_inode(dentry), type, format_id,
25212575 DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
25222576
2523
-out:
25242577 dput(dentry);
25252578 return error;
25262579 }
....@@ -2542,13 +2595,17 @@
25422595 if (!(flags & qtype_enforce_flag(type)))
25432596 continue;
25442597 /* Can't enforce without accounting */
2545
- if (!sb_has_quota_usage_enabled(sb, type))
2546
- return -EINVAL;
2547
- ret = dquot_enable(dqopt->files[type], type,
2548
- dqopt->info[type].dqi_fmt_id,
2549
- DQUOT_LIMITS_ENABLED);
2550
- if (ret < 0)
2598
+ if (!sb_has_quota_usage_enabled(sb, type)) {
2599
+ ret = -EINVAL;
25512600 goto out_err;
2601
+ }
2602
+ if (sb_has_quota_limits_enabled(sb, type)) {
2603
+ ret = -EBUSY;
2604
+ goto out_err;
2605
+ }
2606
+ spin_lock(&dq_state_lock);
2607
+ dqopt->flags |= dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2608
+ spin_unlock(&dq_state_lock);
25522609 }
25532610 return 0;
25542611 out_err:
....@@ -2598,10 +2655,12 @@
25982655 out_err:
25992656 /* Backout enforcement disabling we already did */
26002657 for (type--; type >= 0; type--) {
2601
- if (flags & qtype_enforce_flag(type))
2602
- dquot_enable(dqopt->files[type], type,
2603
- dqopt->info[type].dqi_fmt_id,
2604
- DQUOT_LIMITS_ENABLED);
2658
+ if (flags & qtype_enforce_flag(type)) {
2659
+ spin_lock(&dq_state_lock);
2660
+ dqopt->flags |=
2661
+ dquot_state_flag(DQUOT_LIMITS_ENABLED, type);
2662
+ spin_unlock(&dq_state_lock);
2663
+ }
26052664 }
26062665 return ret;
26072666 }
....@@ -2730,7 +2789,7 @@
27302789
27312790 if (check_blim) {
27322791 if (!dm->dqb_bsoftlimit ||
2733
- dm->dqb_curspace + dm->dqb_rsvspace < dm->dqb_bsoftlimit) {
2792
+ dm->dqb_curspace + dm->dqb_rsvspace <= dm->dqb_bsoftlimit) {
27342793 dm->dqb_btime = 0;
27352794 clear_bit(DQ_BLKS_B, &dquot->dq_flags);
27362795 } else if (!(di->d_fieldmask & QC_SPC_TIMER))
....@@ -2739,7 +2798,7 @@
27392798 }
27402799 if (check_ilim) {
27412800 if (!dm->dqb_isoftlimit ||
2742
- dm->dqb_curinodes < dm->dqb_isoftlimit) {
2801
+ dm->dqb_curinodes <= dm->dqb_isoftlimit) {
27432802 dm->dqb_itime = 0;
27442803 clear_bit(DQ_INODES_B, &dquot->dq_flags);
27452804 } else if (!(di->d_fieldmask & QC_INO_TIMER))
....@@ -2782,7 +2841,7 @@
27822841 struct qc_type_state *tstate;
27832842 struct quota_info *dqopt = sb_dqopt(sb);
27842843 int type;
2785
-
2844
+
27862845 memset(state, 0, sizeof(*state));
27872846 for (type = 0; type < MAXQUOTAS; type++) {
27882847 if (!sb_has_quota_active(sb, type))
....@@ -2799,8 +2858,10 @@
27992858 tstate->flags |= QCI_LIMITS_ENFORCED;
28002859 tstate->spc_timelimit = mi->dqi_bgrace;
28012860 tstate->ino_timelimit = mi->dqi_igrace;
2802
- tstate->ino = dqopt->files[type]->i_ino;
2803
- tstate->blocks = dqopt->files[type]->i_blocks;
2861
+ if (dqopt->files[type]) {
2862
+ tstate->ino = dqopt->files[type]->i_ino;
2863
+ tstate->blocks = dqopt->files[type]->i_blocks;
2864
+ }
28042865 tstate->nextents = 1; /* We don't know... */
28052866 spin_unlock(&dq_data_lock);
28062867 }
....@@ -2857,7 +2918,7 @@
28572918 EXPORT_SYMBOL(dquot_quotactl_sysfile_ops);
28582919
28592920 static int do_proc_dqstats(struct ctl_table *table, int write,
2860
- void __user *buffer, size_t *lenp, loff_t *ppos)
2921
+ void *buffer, size_t *lenp, loff_t *ppos)
28612922 {
28622923 unsigned int type = (unsigned long *)table->data - dqstats.stat;
28632924 s64 value = percpu_counter_sum(&dqstats.counter[type]);
....@@ -2987,11 +3048,7 @@
29873048
29883049 /* Find power-of-two hlist_heads which can fit into allocation */
29893050 nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
2990
- dq_hash_bits = 0;
2991
- do {
2992
- dq_hash_bits++;
2993
- } while (nr_hash >> dq_hash_bits);
2994
- dq_hash_bits--;
3051
+ dq_hash_bits = ilog2(nr_hash);
29953052
29963053 nr_hash = 1UL << dq_hash_bits;
29973054 dq_hash_mask = nr_hash - 1;