Skip to content

Commit 4e19d6b

Browse files
committed
ext4: allow directory holes
The largedir feature was intended to allow ext4 directories to have unmapped directory blocks (e.g., directory holes). And so the released e2fsprogs no longer enforces this for largedir file systems; however, the corresponding change to the kernel-side code was not made. This commit fixes this oversight. Signed-off-by: Theodore Ts'o <tytso@mit.edu> Cc: stable@kernel.org
1 parent 9382cde commit 4e19d6b

2 files changed

Lines changed: 46 additions & 18 deletions

File tree

‎fs/ext4/dir.c‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
112112
struct inode *inode = file_inode(file);
113113
struct super_block *sb = inode->i_sb;
114114
struct buffer_head *bh = NULL;
115-
int dir_has_error = 0;
116115
struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
117116

118117
if (IS_ENCRYPTED(inode)) {
@@ -148,8 +147,6 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
148147
return err;
149148
}
150149

151-
offset = ctx->pos & (sb->s_blocksize - 1);
152-
153150
while (ctx->pos < inode->i_size) {
154151
struct ext4_map_blocks map;
155152

@@ -158,9 +155,18 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
158155
goto errout;
159156
}
160157
cond_resched();
158+
offset = ctx->pos & (sb->s_blocksize - 1);
161159
map.m_lblk = ctx->pos >> EXT4_BLOCK_SIZE_BITS(sb);
162160
map.m_len = 1;
163161
err = ext4_map_blocks(NULL, inode, &map, 0);
162+
if (err == 0) {
163+
/* m_len should never be zero but let's avoid
164+
* an infinite loop if it somehow is */
165+
if (map.m_len == 0)
166+
map.m_len = 1;
167+
ctx->pos += map.m_len * sb->s_blocksize;
168+
continue;
169+
}
164170
if (err > 0) {
165171
pgoff_t index = map.m_pblk >>
166172
(PAGE_SHIFT - inode->i_blkbits);
@@ -179,13 +185,6 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
179185
}
180186

181187
if (!bh) {
182-
if (!dir_has_error) {
183-
EXT4_ERROR_FILE(file, 0,
184-
"directory contains a "
185-
"hole at offset %llu",
186-
(unsigned long long) ctx->pos);
187-
dir_has_error = 1;
188-
}
189188
/* corrupt size? Maybe no more blocks to read */
190189
if (ctx->pos > inode->i_blocks << 9)
191190
break;

‎fs/ext4/namei.c‎

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,18 @@ static struct buffer_head *ext4_append(handle_t *handle,
8282
static int ext4_dx_csum_verify(struct inode *inode,
8383
struct ext4_dir_entry *dirent);
8484

85+
/*
86+
* Hints to ext4_read_dirblock regarding whether we expect a directory
87+
* block being read to be an index block, or a block containing
88+
* directory entries (and if the latter, whether it was found via a
89+
* logical block in an htree index block). This is used to control
90+
* what sort of sanity checkinig ext4_read_dirblock() will do on the
91+
* directory block read from the storage device. EITHER will means
92+
* the caller doesn't know what kind of directory block will be read,
93+
* so no specific verification will be done.
94+
*/
8595
typedef enum {
86-
EITHER, INDEX, DIRENT
96+
EITHER, INDEX, DIRENT, DIRENT_HTREE
8797
} dirblock_type_t;
8898

8999
#define ext4_read_dirblock(inode, block, type) \
@@ -109,11 +119,14 @@ static struct buffer_head *__ext4_read_dirblock(struct inode *inode,
109119

110120
return bh;
111121
}
112-
if (!bh) {
122+
if (!bh && (type == INDEX || type == DIRENT_HTREE)) {
113123
ext4_error_inode(inode, func, line, block,
114-
"Directory hole found");
124+
"Directory hole found for htree %s block",
125+
(type == INDEX) ? "index" : "leaf");
115126
return ERR_PTR(-EFSCORRUPTED);
116127
}
128+
if (!bh)
129+
return NULL;
117130
dirent = (struct ext4_dir_entry *) bh->b_data;
118131
/* Determine whether or not we have an index block */
119132
if (is_dx(inode)) {
@@ -980,7 +993,7 @@ static int htree_dirblock_to_tree(struct file *dir_file,
980993

981994
dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
982995
(unsigned long)block));
983-
bh = ext4_read_dirblock(dir, block, DIRENT);
996+
bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
984997
if (IS_ERR(bh))
985998
return PTR_ERR(bh);
986999

@@ -1619,7 +1632,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
16191632
return (struct buffer_head *) frame;
16201633
do {
16211634
block = dx_get_block(frame->at);
1622-
bh = ext4_read_dirblock(dir, block, DIRENT);
1635+
bh = ext4_read_dirblock(dir, block, DIRENT_HTREE);
16231636
if (IS_ERR(bh))
16241637
goto errout;
16251638

@@ -2203,6 +2216,11 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
22032216
blocks = dir->i_size >> sb->s_blocksize_bits;
22042217
for (block = 0; block < blocks; block++) {
22052218
bh = ext4_read_dirblock(dir, block, DIRENT);
2219+
if (bh == NULL) {
2220+
bh = ext4_bread(handle, dir, block,
2221+
EXT4_GET_BLOCKS_CREATE);
2222+
goto add_to_new_block;
2223+
}
22062224
if (IS_ERR(bh)) {
22072225
retval = PTR_ERR(bh);
22082226
bh = NULL;
@@ -2223,6 +2241,7 @@ static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
22232241
brelse(bh);
22242242
}
22252243
bh = ext4_append(handle, dir, &block);
2244+
add_to_new_block:
22262245
if (IS_ERR(bh)) {
22272246
retval = PTR_ERR(bh);
22282247
bh = NULL;
@@ -2267,7 +2286,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
22672286
return PTR_ERR(frame);
22682287
entries = frame->entries;
22692288
at = frame->at;
2270-
bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT);
2289+
bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT_HTREE);
22712290
if (IS_ERR(bh)) {
22722291
err = PTR_ERR(bh);
22732292
bh = NULL;
@@ -2815,7 +2834,10 @@ bool ext4_empty_dir(struct inode *inode)
28152834
EXT4_ERROR_INODE(inode, "invalid size");
28162835
return true;
28172836
}
2818-
bh = ext4_read_dirblock(inode, 0, EITHER);
2837+
/* The first directory block must not be a hole,
2838+
* so treat it as DIRENT_HTREE
2839+
*/
2840+
bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
28192841
if (IS_ERR(bh))
28202842
return true;
28212843

@@ -2837,6 +2859,10 @@ bool ext4_empty_dir(struct inode *inode)
28372859
brelse(bh);
28382860
lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
28392861
bh = ext4_read_dirblock(inode, lblock, EITHER);
2862+
if (bh == NULL) {
2863+
offset += sb->s_blocksize;
2864+
continue;
2865+
}
28402866
if (IS_ERR(bh))
28412867
return true;
28422868
de = (struct ext4_dir_entry_2 *) bh->b_data;
@@ -3402,7 +3428,10 @@ static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
34023428
struct buffer_head *bh;
34033429

34043430
if (!ext4_has_inline_data(inode)) {
3405-
bh = ext4_read_dirblock(inode, 0, EITHER);
3431+
/* The first directory block must not be a hole, so
3432+
* treat it as DIRENT_HTREE
3433+
*/
3434+
bh = ext4_read_dirblock(inode, 0, DIRENT_HTREE);
34063435
if (IS_ERR(bh)) {
34073436
*retval = PTR_ERR(bh);
34083437
return NULL;

0 commit comments

Comments
 (0)