Skip to contentSkip to conversation

ext4: prevent ext4_quota_write() from failing due to ENOSPC

PATCH In review Theodore Ts'o 2 participants 5 messages Jun 21, 2015 – Jun 26, 2015

Subsystems (3)
lore
Share & tools
lore

Theodore Ts'o

author maintainer
#0 lore raw

PATCH 1/1 ext4: prevent ext4_quota_write() from failing due to ENOSPC

In order to prevent quota block tracking to be inaccurate when ext4_quota_write() fails with ENOSPC, we make two changes. The quota file can now use the reserved block (since the quota file is arguably file system metadata), and ext4_quota_write() now uses ext4_should_retry_alloc() to retry the block allocation after a commit has completed and released some blocks for allocation.

This fixes failures of xfstests generic/270:

Quota error (device vdc): write_blk: dquota write failed
Quota error (device vdc): qtree_write_dquot: Error -28 occurred while creating quota

---
 fs/ext4/extents.c  |  2 ++
 fs/ext4/indirect.c |  2 ++
 fs/ext4/inode.c    | 10 +++++-----
 fs/ext4/namei.c    |  2 +-
 fs/ext4/super.c    |  8 +++++++-
 5 files changed, 17 insertions(+), 7 deletions(-)
fs/ext4/extents.c +2 -0
index 338473b..a1a54f5 100644
@@ -4456,6 +4456,8 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
 		ar.flags |= EXT4_MB_HINT_NOPREALLOC;
 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
 		ar.flags |= EXT4_MB_DELALLOC_RESERVED;
+	if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
+		ar.flags |= EXT4_MB_USE_RESERVED;
 	newblock = ext4_mb_new_blocks(handle, &ar, &err);
 	if (!newblock)
 		goto out2;
fs/ext4/indirect.c +2 -0
index 9588240..9962d57 100644
@@ -576,6 +576,8 @@ int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
 		ar.flags = EXT4_MB_HINT_DATA;
 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
 		ar.flags |= EXT4_MB_DELALLOC_RESERVED;
+	if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
+		ar.flags |= EXT4_MB_USE_RESERVED;
 
 	ar.goal = ext4_find_goal(inode, map->m_lblk, partial);
 
fs/ext4/inode.c +5 -5
index 263a46c..e8a67b8 100644
@@ -731,18 +731,18 @@ int ext4_get_block(struct inode *inode, sector_t iblock,
  * `handle' can be NULL if create is zero
  */
 struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
-				ext4_lblk_t block, int create)
+				ext4_lblk_t block, int map_flags)
 {
 	struct ext4_map_blocks map;
 	struct buffer_head *bh;
+	int create = map_flags & EXT4_GET_BLOCKS_CREATE;
 	int err;
 
 	J_ASSERT(handle != NULL || create == 0);
 
 	map.m_lblk = block;
 	map.m_len = 1;
-	err = ext4_map_blocks(handle, inode, &map,
-			      create ? EXT4_GET_BLOCKS_CREATE : 0);
+	err = ext4_map_blocks(handle, inode, &map, map_flags);
 
 	if (err == 0)
 		return create ? ERR_PTR(-ENOSPC) : NULL;
@@ -788,11 +788,11 @@ errout:
 }
 
 struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
-			       ext4_lblk_t block, int create)
+			       ext4_lblk_t block, int map_flags)
 {
 	struct buffer_head *bh;
 
-	bh = ext4_getblk(handle, inode, block, create);
+	bh = ext4_getblk(handle, inode, block, map_flags);
 	if (IS_ERR(bh))
 		return bh;
 	if (!bh || buffer_uptodate(bh))
fs/ext4/namei.c +1 -1
index 5e7676f..e230b31 100644
@@ -61,7 +61,7 @@ static struct buffer_head *ext4_append(handle_t *handle,
 
 	*block = inode->i_size >> inode->i_sb->s_blocksize_bits;
 
-	bh = ext4_bread(handle, inode, *block, 1);
+	bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE);
 	if (IS_ERR(bh))
 		return bh;
 	inode->i_size += inode->i_sb->s_blocksize;
fs/ext4/super.c +7 -1
index 2858ac0..bd4df9d 100644
@@ -5438,6 +5438,7 @@ static ssize_t ext4_quota_write(struct super_block *sb, int type,
 	struct inode *inode = sb_dqopt(sb)->files[type];
 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
 	int err, offset = off & (sb->s_blocksize - 1);
+	int retries = 0;
 	struct buffer_head *bh;
 	handle_t *handle = journal_current_handle();
 
@@ -5458,7 +5459,12 @@ static ssize_t ext4_quota_write(struct super_block *sb, int type,
 		return -EIO;
 	}
 
-	bh = ext4_bread(handle, inode, blk, 1);
+	do {
+		bh = ext4_bread(handle, inode, blk,
+				EXT4_GET_BLOCKS_CREATE |
+				EXT4_GET_BLOCKS_METADATA_NOFAIL);
+	} while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
+		 ext4_should_retry_alloc(inode->i_sb, &retries));
 	if (IS_ERR(bh))
 		return PTR_ERR(bh);
 	if (!bh)
Signature · 4 lines
2.3.0

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
author maintainer
#1 lore raw
Quoted 7 lines · Theodore Ts'o

In order to prevent quota block tracking to be inaccurate when ext4_quota_write() fails with ENOSPC, we make two changes. The quota file can now use the reserved block (since the quota file is arguably file system metadata), and ext4_quota_write() now uses ext4_should_retry_alloc() to retry the block allocation after a commit has completed and released some blocks for allocation.

Quoted 6 lines

This fixes failures of xfstests generic/270:

Quota error (device vdc): write_blk: dquota write failed
Quota error (device vdc): qtree_write_dquot: Error -28 occurred while creating quota

This patch significantly reduces the incidence of generic/270 failing, but it doesn't completely eliminate them, especially when using a 1k block size. This makes sense, because what this generic/270 does is to chown files to random uid's using an ENOSPC hitter as an antagonist. This means the quota system needs to allocate a huge number of blocks, and with a 1k block, it's doing 4x the number allocations.

Something we could do that would help this situation is if there was an interface in the quota system that initialized the quota records for a particular uid or gid, which we could call from ext4_setattr() or ext4_open() *before* we actually need to allocate a block quota record for the file.

(We do check the error return from dquot_transfer() and abort if it fails, but either the allocation qtree_write_dquot() isn't getting perculated all the way up to dquot_transfer(), or the quota mismatch is taking place because we're doing the chown on an empty file, and then when we subsequently write to the file, that's when the quota record is created and by the time we try to resolve the quota file update, it's too late to back out the delayed allocation write.)

Jan, does that make sense to you as a way to solve this issue?

- Ted

P.S. BTW, have you had a chance to take a look at the various competing project quota patches and do you have an opinion about their disposition? Project quota hasn't been high priority for me, to be honest, but there are people who seem to care about it, and I've been waiting to see what you think about the patchsets from Li Xi and Konstantin Khlebnikov. Thanks!!

reviewer
#2 lore raw
Quoted 23 lines · Ted Tso
Quoted 7 lines · Theodore Ts'o

In order to prevent quota block tracking to be inaccurate when ext4_quota_write() fails with ENOSPC, we make two changes. The quota file can now use the reserved block (since the quota file is arguably file system metadata), and ext4_quota_write() now uses ext4_should_retry_alloc() to retry the block allocation after a commit has completed and released some blocks for allocation.

Quoted 6 lines

This fixes failures of xfstests generic/270:

Quota error (device vdc): write_blk: dquota write failed
Quota error (device vdc): qtree_write_dquot: Error -28 occurred while creating quota

This patch significantly reduces the incidence of generic/270 failing, but it doesn't completely eliminate them, especially when using a 1k block size. This makes sense, because what this generic/270 does is to chown files to random uid's using an ENOSPC hitter as an antagonist. This means the quota system needs to allocate a huge number of blocks, and with a 1k block, it's doing 4x the number allocations.

Quoted 5 lines

Something we could do that would help this situation is if there was an interface in the quota system that initialized the quota records for a particular uid or gid, which we could call from ext4_setattr() or ext4_open() *before* we actually need to allocate a block quota record for the file.

So quota record is actually first written from dquot_acquire() function which gets called (through ->acquire_dquot() callback) when we get the first reference to a quota structure in dqget() (called from dquot_initialize() / dquot_transfer()). So the problem really is in the lack of error propagation from dqget() up through dquot_initialize() / dquot_transfer() to the filesystem.

Doing the propagation for dquot_transfer() should be pretty easy since that already reports other errors. With dquot_initialize() it is more difficult since that doesn't return any errors currently so we have to add error handling into filesystems to the inode creation path.

I'll look into this.

Quoted 6 lines

P.S. BTW, have you had a chance to take a look at the various competing project quota patches and do you have an opinion about their disposition? Project quota hasn't been high priority for me, to be honest, but there are people who seem to care about it, and I've been waiting to see what you think about the patchsets from Li Xi and Konstantin Khlebnikov. Thanks!!

Yes, patches from Li Xi look good to me and I think I've added my Reviewed-by tags to all of them. The only missing piece was that Dave wanted Li Xi to run xfstests for ext4 & xfs with them and Li Xi got somehow stuck with that.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
reviewer
#3 lore raw
Quoted 37 lines · Jan Kara
Quoted 23 lines · Ted Tso
Quoted 7 lines · Theodore Ts'o

In order to prevent quota block tracking to be inaccurate when ext4_quota_write() fails with ENOSPC, we make two changes. The quota file can now use the reserved block (since the quota file is arguably file system metadata), and ext4_quota_write() now uses ext4_should_retry_alloc() to retry the block allocation after a commit has completed and released some blocks for allocation.

Quoted 6 lines

This fixes failures of xfstests generic/270:

Quota error (device vdc): write_blk: dquota write failed
Quota error (device vdc): qtree_write_dquot: Error -28 occurred while creating quota

This patch significantly reduces the incidence of generic/270 failing, but it doesn't completely eliminate them, especially when using a 1k block size. This makes sense, because what this generic/270 does is to chown files to random uid's using an ENOSPC hitter as an antagonist. This means the quota system needs to allocate a huge number of blocks, and with a 1k block, it's doing 4x the number allocations.

Quoted 5 lines

Something we could do that would help this situation is if there was an interface in the quota system that initialized the quota records for a particular uid or gid, which we could call from ext4_setattr() or ext4_open() *before* we actually need to allocate a block quota record for the file.

So quota record is actually first written from dquot_acquire() function which gets called (through ->acquire_dquot() callback) when we get the first reference to a quota structure in dqget() (called from dquot_initialize() / dquot_transfer()). So the problem really is in the lack of error propagation from dqget() up through dquot_initialize() / dquot_transfer() to the filesystem.

Quoted 6 lines

Doing the propagation for dquot_transfer() should be pretty easy since that already reports other errors. With dquot_initialize() it is more difficult since that doesn't return any errors currently so we have to add error handling into filesystems to the inode creation path.

I'll look into this.

The attached patch propagates errors from quota creation up into the caller so now chown will return ENOSPC... The patch seems to work for me. Can you check whether it fixes the failures you are seeing? If yes, I'll merge the patch through my tree.

Missing is still handling of errors from dquot_initialize() in filesystems. I'll add that on top if this patch works for you.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Quoted 1 line

From 727cbcbb7776efb7185fd8835c79737b4387dcae Mon Sep 17 00:00:00 2001

From: Jan Kara <[email protected]>
Date: Wed, 24 Jun 2015 18:07:02 +0200
Subject: [PATCH] quota: Propagate error from ->acquire_dquot()

Currently when some error happened in ->acquire_dquot(), dqget() just returned NULL. That was indistinguishable from a case when e.g. someone run quotaoff and so was generally silently ignored. However ->acquire_dquot() can fail because of ENOSPC or EIO in which case user should better know. So propagate error up from ->acquire_dquot properly.

---
 fs/ocfs2/file.c          |  8 ++---
 fs/ocfs2/quota_local.c   |  4 +--
 fs/quota/dquot.c         | 88 ++++++++++++++++++++++++++++++++++--------------
 include/linux/quotaops.h |  2 +-
 4 files changed, 70 insertions(+), 32 deletions(-)
fs/ocfs2/file.c +4 -4
index d8b670cbd909..474176b35ffb 100644
@@ -1208,8 +1208,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
 		    && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
 		    OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
 			transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(attr->ia_uid));
-			if (!transfer_to[USRQUOTA]) {
-				status = -ESRCH;
+			if (IS_ERR(transfer_to[USRQUOTA])) {
+				status = PTR_ERR(transfer_to[USRQUOTA]);
 				goto bail_unlock;
 			}
 		}
@@ -1217,8 +1217,8 @@ int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
 		    && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
 		    OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
 			transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(attr->ia_gid));
-			if (!transfer_to[GRPQUOTA]) {
-				status = -ESRCH;
+			if (IS_ERR(transfer_to[GRPQUOTA])) {
+				status = PTR_ERR(transfer_to[GRPQUOTA]);
 				goto bail_unlock;
 			}
 		}
fs/ocfs2/quota_local.c +2 -2
index 3d0b63d34225..bb07004df72a 100644
@@ -499,8 +499,8 @@ static int ocfs2_recover_local_quota_file(struct inode *lqinode,
 			dquot = dqget(sb,
 				      make_kqid(&init_user_ns, type,
 						le64_to_cpu(dqblk->dqb_id)));
-			if (!dquot) {
-				status = -EIO;
+			if (IS_ERR(dquot)) {
+				status = PTR_ERR(dquot);
 				mlog(ML_ERROR, "Failed to get quota structure "
 				     "for id %u, type %d. Cannot finish quota "
 				     "file recovery.\n",
fs/quota/dquot.c +63 -25

183 lines not on this page · raw message

include/linux/quotaops.h +1 -1
index 77ca6601ff25..06c9ea8971fb 100644
@@ -43,7 +43,7 @@ void inode_claim_rsv_space(struct inode *inode, qsize_t number);
 void inode_sub_rsv_space(struct inode *inode, qsize_t number);
 void inode_reclaim_rsv_space(struct inode *inode, qsize_t number);
 
-void dquot_initialize(struct inode *inode);
+int dquot_initialize(struct inode *inode);
 void dquot_drop(struct inode *inode);
 struct dquot *dqget(struct super_block *sb, struct kqid qid);
 static inline struct dquot *dqgrab(struct dquot *dquot)
Signature · 1 line
2.1.4
author maintainer
#4 lore raw
Quoted 5 lines · Jan Kara

The attached patch propagates errors from quota creation up into the caller so now chown will return ENOSPC... The patch seems to work for me. Can you check whether it fixes the failures you are seeing? If yes, I'll merge the patch through my tree.

I ran "kvm-xfstests -C 5 generic/270" with this patch applied and I'm not seeing any failures. So this seems to solve the issue!

- Ted