Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a996031c authored by Eric Sandeen's avatar Eric Sandeen Committed by Theodore Ts'o
Browse files

delay capable() check in ext4_has_free_blocks()



As reported by Eric Paris, the capable() check in ext4_has_free_blocks()
sometimes causes SELinux denials.

We can rearrange the logic so that we only try to use the root-reserved
blocks when necessary, and even then we can move the capable() test
to last, to avoid the check most of the time.

Signed-off-by: default avatarEric Sandeen <sandeen@redhat.com>
Reviewed-by: default avatarMingming Cao <cmm@us.ibm.com>
Signed-off-by: default avatar"Theodore Ts'o" <tytso@mit.edu>
parent 8c3bf8a0
Loading
Loading
Loading
Loading
+14 −12
Original line number Diff line number Diff line
@@ -599,17 +599,12 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
 */
int ext4_has_free_blocks(struct ext4_sb_info *sbi, s64 nblocks)
{
	s64 free_blocks, dirty_blocks;
	s64 root_blocks = 0;
	s64 free_blocks, dirty_blocks, root_blocks;
	struct percpu_counter *fbc = &sbi->s_freeblocks_counter;
	struct percpu_counter *dbc = &sbi->s_dirtyblocks_counter;

	free_blocks  = percpu_counter_read_positive(fbc);
	dirty_blocks = percpu_counter_read_positive(dbc);

	if (!capable(CAP_SYS_RESOURCE) &&
		sbi->s_resuid != current->fsuid &&
		(sbi->s_resgid == 0 || !in_group_p(sbi->s_resgid)))
	root_blocks = ext4_r_blocks_count(sbi->s_es);

	if (free_blocks - (nblocks + root_blocks + dirty_blocks) <
@@ -623,15 +618,22 @@ int ext4_has_free_blocks(struct ext4_sb_info *sbi, s64 nblocks)
		}
	}
	/* Check whether we have space after
	 * accounting for current dirty blocks
	 * accounting for current dirty blocks & root reserved blocks.
	 */
	if (free_blocks < ((root_blocks + nblocks) + dirty_blocks))
		/* we don't have free space */
		return 0;
	if (free_blocks >= ((root_blocks + nblocks) + dirty_blocks))
		return 1;

	/* Hm, nope.  Are (enough) root reserved blocks available? */
	if (sbi->s_resuid == current->fsuid ||
	    ((sbi->s_resgid != 0) && in_group_p(sbi->s_resgid)) ||
	    capable(CAP_SYS_RESOURCE)) {
		if (free_blocks >= (nblocks + dirty_blocks))
			return 1;
	}

	return 0;
}

int ext4_claim_free_blocks(struct ext4_sb_info *sbi,
						s64 nblocks)
{