Skip to contentSkip to conversation

ext2: propagate errors up to ext2_find_entry()'s callers

PATCH Superseded zhangyi (F) 2 participants 4 messages Jun 3, 2020 – Jun 8, 2020 ext2 file system

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 ext2: propagate errors up to ext2_find_entry()'s callers

The same to commit <36de928641ee4> (ext4: propagate errors up to ext4_find_entry()'s callers') in ext4, also return error instead of NULL pointer in case of some error happens in ext2_find_entry() (e.g. -ENOMEM or -EIO). This could avoid a negative dentry cache entry installed even it failed to read directory block due to IO error.

---
Changes since v1:
- Remove ret parameter in ext2_find_entry().

 fs/ext2/dir.c   | 53 ++++++++++++++++++++++++-------------------------
 fs/ext2/ext2.h  |  3 ++-
 fs/ext2/namei.c | 32 +++++++++++++++++++++++------
 3 files changed, 54 insertions(+), 34 deletions(-)
fs/ext2/dir.c +26 -27
index 13318e255ebf..95e4f0bd55a3 100644
@@ -348,7 +348,6 @@ struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
 	struct page *page = NULL;
 	struct ext2_inode_info *ei = EXT2_I(dir);
 	ext2_dirent * de;
-	int dir_has_error = 0;
 
 	if (npages == 0)
 		goto out;
@@ -362,25 +361,25 @@ struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
 	n = start;
 	do {
 		char *kaddr;
-		page = ext2_get_page(dir, n, dir_has_error);
-		if (!IS_ERR(page)) {
-			kaddr = page_address(page);
-			de = (ext2_dirent *) kaddr;
-			kaddr += ext2_last_byte(dir, n) - reclen;
-			while ((char *) de <= kaddr) {
-				if (de->rec_len == 0) {
-					ext2_error(dir->i_sb, __func__,
-						"zero-length directory entry");
-					ext2_put_page(page);
-					goto out;
-				}
-				if (ext2_match (namelen, name, de))
-					goto found;
-				de = ext2_next_entry(de);
+		page = ext2_get_page(dir, n, 0);
+		if (IS_ERR(page))
+			return ERR_CAST(page);
+
+		kaddr = page_address(page);
+		de = (ext2_dirent *) kaddr;
+		kaddr += ext2_last_byte(dir, n) - reclen;
+		while ((char *) de <= kaddr) {
+			if (de->rec_len == 0) {
+				ext2_error(dir->i_sb, __func__,
+					"zero-length directory entry");
+				ext2_put_page(page);
+				goto out;
 			}
-			ext2_put_page(page);
-		} else
-			dir_has_error = 1;
+			if (ext2_match(namelen, name, de))
+				goto found;
+			de = ext2_next_entry(de);
+		}
+		ext2_put_page(page);
 
 		if (++n >= npages)
 			n = 0;
@@ -414,18 +413,18 @@ struct ext2_dir_entry_2 * ext2_dotdot (struct inode *dir, struct page **p)
 	return de;
 }
 
-ino_t ext2_inode_by_name(struct inode *dir, const struct qstr *child)
+int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
 {
-	ino_t res = 0;
 	struct ext2_dir_entry_2 *de;
 	struct page *page;
 	
-	de = ext2_find_entry (dir, child, &page);
-	if (de) {
-		res = le32_to_cpu(de->inode);
-		ext2_put_page(page);
-	}
-	return res;
+	de = ext2_find_entry(dir, child, &page);
+	if (IS_ERR_OR_NULL(de))
+		return PTR_ERR(de);
+
+	*ino = le32_to_cpu(de->inode);
+	ext2_put_page(page);
+	return 0;
 }
 
 static int ext2_prepare_chunk(struct page *page, loff_t pos, unsigned len)
fs/ext2/ext2.h +2 -1
index 8178bd38a9d6..a321ff9bf1b4 100644
@@ -738,7 +738,8 @@ extern void ext2_rsv_window_add(struct super_block *sb, struct ext2_reserve_wind
 
 /* dir.c */
 extern int ext2_add_link (struct dentry *, struct inode *);
-extern ino_t ext2_inode_by_name(struct inode *, const struct qstr *);
+extern int ext2_inode_by_name(struct inode *dir,
+			      const struct qstr *child, ino_t *ino);
 extern int ext2_make_empty(struct inode *, struct inode *);
 extern struct ext2_dir_entry_2 * ext2_find_entry (struct inode *,const struct qstr *, struct page **);
 extern int ext2_delete_entry (struct ext2_dir_entry_2 *, struct page *);
fs/ext2/namei.c +26 -6
index ccfbbf59e2fc..4b38e558d477 100644
@@ -56,12 +56,15 @@ static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
 {
 	struct inode * inode;
-	ino_t ino;
+	ino_t ino = 0;
+	int res;
 	
 	if (dentry->d_name.len > EXT2_NAME_LEN)
 		return ERR_PTR(-ENAMETOOLONG);
 
-	ino = ext2_inode_by_name(dir, &dentry->d_name);
+	res = ext2_inode_by_name(dir, &dentry->d_name, &ino);
+	if (res)
+		return ERR_PTR(res);
 	inode = NULL;
 	if (ino) {
 		inode = ext2_iget(dir->i_sb, ino);
@@ -78,7 +81,12 @@ static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, uns
 struct dentry *ext2_get_parent(struct dentry *child)
 {
 	struct qstr dotdot = QSTR_INIT("..", 2);
-	unsigned long ino = ext2_inode_by_name(d_inode(child), &dotdot);
+	ino_t ino = 0;
+	int res;
+
+	res = ext2_inode_by_name(d_inode(child), &dotdot, &ino);
+	if (res)
+		return ERR_PTR(res);
 	if (!ino)
 		return ERR_PTR(-ENOENT);
 	return d_obtain_alias(ext2_iget(child->d_sb, ino));
@@ -276,7 +284,11 @@ static int ext2_unlink(struct inode * dir, struct dentry *dentry)
 	if (err)
 		goto out;
 
-	de = ext2_find_entry (dir, &dentry->d_name, &page);
+	de = ext2_find_entry(dir, &dentry->d_name, &page);
+	if (IS_ERR(de)) {
+		err = PTR_ERR(de);
+		goto out;
+	}
 	if (!de) {
 		err = -ENOENT;
 		goto out;
@@ -332,7 +344,11 @@ static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
 	if (err)
 		goto out;
 
-	old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
+	old_de = ext2_find_entry(old_dir, &old_dentry->d_name, &old_page);
+	if (IS_ERR(old_de)) {
+		err = PTR_ERR(old_de);
+		goto out;
+	}
 	if (!old_de) {
 		err = -ENOENT;
 		goto out;
@@ -354,7 +370,11 @@ static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
 			goto out_dir;
 
 		err = -ENOENT;
-		new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
+		new_de = ext2_find_entry(new_dir, &new_dentry->d_name, &new_page);
+		if (IS_ERR(new_de)) {
+			err = PTR_ERR(new_de);
+			goto out_dir;
+		}
 		if (!new_de)
 			goto out_dir;
 		ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
Signature · 1 line
2.25.4
#1 lore raw

PATCH v2 2/2 ext2: ext2_find_entry() return -ENOENT if no entry found

Almost all callers of ext2_find_entry() transform NULL return value to -ENOENT, so just let ext2_find_entry() retuen -ENOENT instead of NULL if no valid entry found, and also switch to check the return value of ext2_inode_by_name() in ext2_lookup() and ext2_get_parent().

---
 fs/ext2/dir.c   | 11 ++++++++---
 fs/ext2/namei.c | 27 ++++++++-------------------
 2 files changed, 16 insertions(+), 22 deletions(-)
fs/ext2/dir.c +8 -3
index 95e4f0bd55a3..96c40fd7550e 100644
@@ -393,7 +393,7 @@ struct ext2_dir_entry_2 *ext2_find_entry (struct inode *dir,
 		}
 	} while (n != start);
 out:
-	return NULL;
+	return ERR_PTR(-ENOENT);
 
 found:
 	*res_page = page;
@@ -419,11 +419,16 @@ int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
 	struct page *page;
 	
 	de = ext2_find_entry(dir, child, &page);
-	if (IS_ERR_OR_NULL(de))
+	if (IS_ERR(de))
 		return PTR_ERR(de);
 
-	*ino = le32_to_cpu(de->inode);
 	ext2_put_page(page);
+	if (!de->inode) {
+		ext2_error(dir->i_sb, __func__, "bad inode number: %u",
+			   le32_to_cpu(de->inode));
+		return -EFSCORRUPTED;
+	}
+	*ino = le32_to_cpu(de->inode);
 	return 0;
 }
 
fs/ext2/namei.c +8 -19
index 4b38e558d477..9601d469c5b1 100644
@@ -56,17 +56,18 @@ static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
 static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
 {
 	struct inode * inode;
-	ino_t ino = 0;
+	ino_t ino;
 	int res;
 	
 	if (dentry->d_name.len > EXT2_NAME_LEN)
 		return ERR_PTR(-ENAMETOOLONG);
 
 	res = ext2_inode_by_name(dir, &dentry->d_name, &ino);
-	if (res)
-		return ERR_PTR(res);
-	inode = NULL;
-	if (ino) {
+	if (res) {
+		if (res != -ENOENT)
+			return ERR_PTR(res);
+		inode = NULL;
+	} else {
 		inode = ext2_iget(dir->i_sb, ino);
 		if (inode == ERR_PTR(-ESTALE)) {
 			ext2_error(dir->i_sb, __func__,
@@ -81,14 +82,13 @@ static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, uns
 struct dentry *ext2_get_parent(struct dentry *child)
 {
 	struct qstr dotdot = QSTR_INIT("..", 2);
-	ino_t ino = 0;
+	ino_t ino;
 	int res;
 
 	res = ext2_inode_by_name(d_inode(child), &dotdot, &ino);
 	if (res)
 		return ERR_PTR(res);
-	if (!ino)
-		return ERR_PTR(-ENOENT);
+
 	return d_obtain_alias(ext2_iget(child->d_sb, ino));
 } 
 
@@ -289,10 +289,6 @@ static int ext2_unlink(struct inode * dir, struct dentry *dentry)
 		err = PTR_ERR(de);
 		goto out;
 	}
-	if (!de) {
-		err = -ENOENT;
-		goto out;
-	}
 
 	err = ext2_delete_entry (de, page);
 	if (err)
@@ -349,10 +345,6 @@ static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
 		err = PTR_ERR(old_de);
 		goto out;
 	}
-	if (!old_de) {
-		err = -ENOENT;
-		goto out;
-	}
 
 	if (S_ISDIR(old_inode->i_mode)) {
 		err = -EIO;
@@ -369,14 +361,11 @@ static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
 		if (dir_de && !ext2_empty_dir (new_inode))
 			goto out_dir;
 
-		err = -ENOENT;
 		new_de = ext2_find_entry(new_dir, &new_dentry->d_name, &new_page);
 		if (IS_ERR(new_de)) {
 			err = PTR_ERR(new_de);
 			goto out_dir;
 		}
-		if (!new_de)
-			goto out_dir;
 		ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
 		new_inode->i_ctime = current_time(new_inode);
 		if (dir_de)
Signature · 1 line
2.25.4
#2 lore raw

Re: [PATCH v2 2/2] ext2: ext2_find_entry() return -ENOENT if no entry found

Quoted 7 lines · zhangyi (F)

Almost all callers of ext2_find_entry() transform NULL return value to -ENOENT, so just let ext2_find_entry() retuen -ENOENT instead of NULL if no valid entry found, and also switch to check the return value of ext2_inode_by_name() in ext2_lookup() and ext2_get_parent().

Thanks for the patch. Just one small nit below.

fs/ext2/dir.c int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino) · line 426

Quoted 11 lines
fs/ext2/dir.c +2 -2
@@ -419,11 +419,16 @@ int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
 	struct page *page;
 
 	de = ext2_find_entry(dir, child, &page);
-	if (IS_ERR_OR_NULL(de))
+	if (IS_ERR(de))
 		return PTR_ERR(de);
 
-	*ino = le32_to_cpu(de->inode);
 	ext2_put_page(page);
+	if (!de->inode) {

ext2_find_entry() will not ever return de with de->inode == 0 because ext2_match() never returns true for such entries. So I'd just remove this condition...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

Re: [PATCH v2 2/2] ext2: ext2_find_entry() return -ENOENT if no entry found

Quoted 19 lines · Jan Kara
Quoted 7 lines · zhangyi (F)

Almost all callers of ext2_find_entry() transform NULL return value to -ENOENT, so just let ext2_find_entry() retuen -ENOENT instead of NULL if no valid entry found, and also switch to check the return value of ext2_inode_by_name() in ext2_lookup() and ext2_get_parent().

Thanks for the patch. Just one small nit below.

Quoted 8 lines
fs/ext2/dir.c +1 -1
@@ -419,11 +419,16 @@ int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino)
 	struct page *page;
 
 	de = ext2_find_entry(dir, child, &page);
-	if (IS_ERR_OR_NULL(de))
+	if (IS_ERR(de))
 		return PTR_ERR(de);
 
Quoted 8 lines

fs/ext2/dir.c int ext2_inode_by_name(struct inode *dir, const struct qstr *child, ino_t *ino) · line 426

Quoted 3 lines
fs/ext2/dir.c +1 -1
-	*ino = le32_to_cpu(de->inode);
 	ext2_put_page(page);
+	if (!de->inode) {

ext2_find_entry() will not ever return de with de->inode == 0 because ext2_match() never returns true for such entries. So I'd just remove this condition...

Indeed, I missed this point, will do.

Thanks,
Yi.