Skip to content

Commit 26245c9

Browse files
committed
quota: Cleanup S_NOQUOTA handling
Cleanup handling of S_NOQUOTA inode flag and document it a bit. The flag does not have to be set under dqptr_sem. Only functions modifying inode's dquot pointers have to check the flag under dqptr_sem before going forward with the modification. This way we are sure that we cannot add new dquot pointers to the inode which is just becoming a quota file. The good thing about this cleanup is that there are no more places in quota code which enforce i_mutex vs. dqptr_sem lock ordering (in particular that dqptr_sem -> i_mutex of quota file). This should silence some (false) lockdep warnings with ext4 + quota and generally make life of some filesystems easier. Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 3a5b27b commit 26245c9

1 file changed

Lines changed: 11 additions & 36 deletions

File tree

‎fs/quota/dquot.c‎

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,13 @@
100100
*
101101
* Any operation working on dquots via inode pointers must hold dqptr_sem. If
102102
* operation is just reading pointers from inode (or not using them at all) the
103-
* read lock is enough. If pointers are altered function must hold write lock
104-
* (these locking rules also apply for S_NOQUOTA flag in the inode - note that
105-
* for altering the flag i_mutex is also needed).
103+
* read lock is enough. If pointers are altered function must hold write lock.
104+
* Special care needs to be taken about S_NOQUOTA inode flag (marking that
105+
* inode is a quota file). Functions adding pointers from inode to dquots have
106+
* to check this flag under dqptr_sem and then (if S_NOQUOTA is not set) they
107+
* have to do all pointer modifications before dropping dqptr_sem. This makes
108+
* sure they cannot race with quotaon which first sets S_NOQUOTA flag and
109+
* then drops all pointers to dquots from an inode.
106110
*
107111
* Each dquot has its dq_lock mutex. Locked dquots might not be referenced
108112
* from inodes (dquot_alloc_space() and such don't check the dq_lock).
@@ -1275,7 +1279,6 @@ int dquot_initialize(struct inode *inode, int type)
12751279
}
12761280

12771281
down_write(&sb_dqopt(sb)->dqptr_sem);
1278-
/* Having dqptr_sem we know NOQUOTA flags can't be altered... */
12791282
if (IS_NOQUOTA(inode))
12801283
goto out_err;
12811284
for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
@@ -1431,11 +1434,6 @@ int __dquot_alloc_space(struct inode *inode, qsize_t number,
14311434
}
14321435

14331436
down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1434-
if (IS_NOQUOTA(inode)) {
1435-
inode_incr_space(inode, number, reserve);
1436-
goto out_unlock;
1437-
}
1438-
14391437
for (cnt = 0; cnt < MAXQUOTAS; cnt++)
14401438
warntype[cnt] = QUOTA_NL_NOWARN;
14411439

@@ -1466,7 +1464,6 @@ int __dquot_alloc_space(struct inode *inode, qsize_t number,
14661464
mark_all_dquot_dirty(inode->i_dquot);
14671465
out_flush_warn:
14681466
flush_warnings(inode->i_dquot, warntype);
1469-
out_unlock:
14701467
up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
14711468
out:
14721469
return ret;
@@ -1499,10 +1496,6 @@ int dquot_alloc_inode(const struct inode *inode, qsize_t number)
14991496
for (cnt = 0; cnt < MAXQUOTAS; cnt++)
15001497
warntype[cnt] = QUOTA_NL_NOWARN;
15011498
down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1502-
if (IS_NOQUOTA(inode)) {
1503-
up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1504-
return QUOTA_OK;
1505-
}
15061499
spin_lock(&dq_data_lock);
15071500
for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
15081501
if (!inode->i_dquot[cnt])
@@ -1539,12 +1532,6 @@ int dquot_claim_space(struct inode *inode, qsize_t number)
15391532
}
15401533

15411534
down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1542-
if (IS_NOQUOTA(inode)) {
1543-
up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1544-
inode_claim_rsv_space(inode, number);
1545-
goto out;
1546-
}
1547-
15481535
spin_lock(&dq_data_lock);
15491536
/* Claim reserved quotas to allocated quotas */
15501537
for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
@@ -1573,17 +1560,11 @@ int __dquot_free_space(struct inode *inode, qsize_t number, int reserve)
15731560
/* First test before acquiring mutex - solves deadlocks when we
15741561
* re-enter the quota code and are already holding the mutex */
15751562
if (IS_NOQUOTA(inode)) {
1576-
out_sub:
15771563
inode_decr_space(inode, number, reserve);
15781564
return QUOTA_OK;
15791565
}
15801566

15811567
down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1582-
/* Now recheck reliably when holding dqptr_sem */
1583-
if (IS_NOQUOTA(inode)) {
1584-
up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1585-
goto out_sub;
1586-
}
15871568
spin_lock(&dq_data_lock);
15881569
for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
15891570
if (!inode->i_dquot[cnt])
@@ -1636,11 +1617,6 @@ int dquot_free_inode(const struct inode *inode, qsize_t number)
16361617
return QUOTA_OK;
16371618

16381619
down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1639-
/* Now recheck reliably when holding dqptr_sem */
1640-
if (IS_NOQUOTA(inode)) {
1641-
up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
1642-
return QUOTA_OK;
1643-
}
16441620
spin_lock(&dq_data_lock);
16451621
for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
16461622
if (!inode->i_dquot[cnt])
@@ -1692,7 +1668,6 @@ int dquot_transfer(struct inode *inode, struct iattr *iattr)
16921668
GRPQUOTA);
16931669

16941670
down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
1695-
/* Now recheck reliably when holding dqptr_sem */
16961671
if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
16971672
up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
16981673
goto put_all;
@@ -2010,13 +1985,15 @@ static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
20101985
/* We don't want quota and atime on quota files (deadlocks
20111986
* possible) Also nobody should write to the file - we use
20121987
* special IO operations which ignore the immutable bit. */
2013-
down_write(&dqopt->dqptr_sem);
20141988
mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
20151989
oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE |
20161990
S_NOQUOTA);
20171991
inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
20181992
mutex_unlock(&inode->i_mutex);
2019-
up_write(&dqopt->dqptr_sem);
1993+
/*
1994+
* When S_NOQUOTA is set, remove dquot references as no more
1995+
* references can be added
1996+
*/
20201997
sb->dq_op->drop(inode);
20211998
}
20221999

@@ -2053,14 +2030,12 @@ static int vfs_load_quota_inode(struct inode *inode, int type, int format_id,
20532030
iput(inode);
20542031
out_lock:
20552032
if (oldflags != -1) {
2056-
down_write(&dqopt->dqptr_sem);
20572033
mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
20582034
/* Set the flags back (in the case of accidental quotaon()
20592035
* on a wrong file we don't want to mess up the flags) */
20602036
inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
20612037
inode->i_flags |= oldflags;
20622038
mutex_unlock(&inode->i_mutex);
2063-
up_write(&dqopt->dqptr_sem);
20642039
}
20652040
mutex_unlock(&dqopt->dqonoff_mutex);
20662041
out_fmt:

0 commit comments

Comments
 (0)