@@ -82,8 +82,18 @@ static struct buffer_head *ext4_append(handle_t *handle,
8282static 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+ */
8595typedef 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