Skip to content

Commit bfec07d

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: avoid overflow when large directory feathure is enabled
When large directory feathure is enable, We have one case which could cause overflow in dir_buckets() as following: special case: level + dir_level >= 32 and level < MAX_DIR_HASH_DEPTH / 2. Here we define MAX_DIR_BUCKETS to limit the return value when the condition could trigger potential overflow. Changes from V1 o modify description of calculation in f2fs.txt suggested by Changman Lee. Suggested-by: Changman Lee <cm224.lee@samsung.com> Signed-off-by: Chao Yu <chao2.yu@samsung.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent d631abd commit bfec07d

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

‎Documentation/filesystems/f2fs.txt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,11 +461,11 @@ The number of blocks and buckets are determined by,
461461
# of blocks in level #n = |
462462
`- 4, Otherwise
463463

464-
,- 2^ (n + dir_level),
465-
| if n < MAX_DIR_HASH_DEPTH / 2,
464+
,- 2^(n + dir_level),
465+
| if n + dir_level < MAX_DIR_HASH_DEPTH / 2,
466466
# of buckets in level #n = |
467-
`- 2^((MAX_DIR_HASH_DEPTH / 2 + dir_level) - 1),
468-
Otherwise
467+
`- 2^((MAX_DIR_HASH_DEPTH / 2) - 1),
468+
Otherwise
469469

470470
When F2FS finds a file name in a directory, at first a hash value of the file
471471
name is calculated. Then, F2FS scans the hash table in level #0 to find the

‎fs/f2fs/dir.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ static unsigned long dir_blocks(struct inode *inode)
2323

2424
static unsigned int dir_buckets(unsigned int level, int dir_level)
2525
{
26-
if (level < MAX_DIR_HASH_DEPTH / 2)
26+
if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
2727
return 1 << (level + dir_level);
2828
else
29-
return 1 << ((MAX_DIR_HASH_DEPTH / 2 + dir_level) - 1);
29+
return MAX_DIR_BUCKETS;
3030
}
3131

3232
static unsigned int bucket_blocks(unsigned int level)

‎include/linux/f2fs_fs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ typedef __le32 f2fs_hash_t;
394394
/* MAX level for dir lookup */
395395
#define MAX_DIR_HASH_DEPTH 63
396396

397+
/* MAX buckets in one level of dir */
398+
#define MAX_DIR_BUCKETS (1 << ((MAX_DIR_HASH_DEPTH / 2) - 1))
399+
397400
#define SIZE_OF_DIR_ENTRY 11 /* by byte */
398401
#define SIZE_OF_DENTRY_BITMAP ((NR_DENTRY_IN_BLOCK + BITS_PER_BYTE - 1) / \
399402
BITS_PER_BYTE)

0 commit comments

Comments
 (0)