Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ext4: change fast symlink test to not rely on i_blocks

ext4_inode_info->i_data is the storage area for 4 types of data:

a) Extents data
b) Inline data
c) Block map
d) Fast symlink data (symlink length < 60)

Extents data case is positively identified by EXT4_INODE_EXTENTS flag.
Inline data case is also obvious because of EXT4_INODE_INLINE_DATA
flag.

Distinguishing c) and d) however requires additional logic. This
currently relies on i_blocks count. After subtracting external xattr
block from i_blocks, if it is greater than 0 then we know that some
data blocks exist, so there must be a block map.

This logic got broken after ea_inode feature was added. That feature
charges the data blocks of external xattr inodes to the referencing
inode and so adds them to the i_blocks. To fix this, we could subtract
ea_inode blocks by iterating through all xattr entries and then check
whether remaining i_blocks count is zero. Besides being complicated,
this won't change the fact that the current way of distinguishing
between c) and d) is fragile.

The alternative solution is to test whether i_size is less than 60 to
determine fast symlink case. ext4_symlink() uses the same test to decide
whether to store the symlink in i_data. There is one caveat to address
before this can work though.

If an inode's i_nlink is zero during eviction, its i_size is set to
zero and its data is truncated. If system crashes before inode is removed
from the orphan list, next boot orphan cleanup may find the inode with
zero i_size. So, a symlink that had its data stored in a block may now
appear to be a fast symlink. The solution used in this patch is to treat
i_size = 0 as a non-fast symlink case. A zero sized symlink is not legal
so the only time this can happen is the mentioned scenario. This is also
logically correct because a i_size = 0 symlink has no data stored in
i_data.

Suggested-by: Andreas Dilger <adilger@dilger.ca>
Signed-off-by: Tahsin Erdogan <tahsin@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Andreas Dilger <adilger@dilger.ca>

authored by

Tahsin Erdogan and committed by
Theodore Ts'o
407cd7fb 63136858

+13 -7
+13 -7
fs/ext4/inode.c
··· 144 144 145 145 /* 146 146 * Test whether an inode is a fast symlink. 147 + * A fast symlink has its symlink data stored in ext4_inode_info->i_data. 147 148 */ 148 149 int ext4_inode_is_fast_symlink(struct inode *inode) 149 150 { 150 - int ea_blocks = EXT4_I(inode)->i_file_acl ? 151 - EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0; 152 - 153 - if (ext4_has_inline_data(inode)) 154 - return 0; 155 - 156 - return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); 151 + return S_ISLNK(inode->i_mode) && inode->i_size && 152 + (inode->i_size < EXT4_N_BLOCKS * 4); 157 153 } 158 154 159 155 /* ··· 257 261 258 262 if (IS_SYNC(inode)) 259 263 ext4_handle_sync(handle); 264 + 265 + /* 266 + * Set inode->i_size to 0 before calling ext4_truncate(). We need 267 + * special handling of symlinks here because i_size is used to 268 + * determine whether ext4_inode_info->i_data contains symlink data or 269 + * block mappings. Setting i_size to 0 will remove its fast symlink 270 + * status. Erase i_data so that it becomes a valid empty block map. 271 + */ 272 + if (ext4_inode_is_fast_symlink(inode)) 273 + memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data)); 260 274 inode->i_size = 0; 261 275 err = ext4_mark_inode_dirty(handle, inode); 262 276 if (err) {