Commit cadf1ccf authored by Gao Xiang's avatar Gao Xiang Committed by Greg Kroah-Hartman

staging: erofs: add error handling for xattr submodule

This patch enhances the missing error handling code for
xattr submodule, which improves the stability for the rare cases.
Signed-off-by: default avatarGao Xiang <gaoxiang25@huawei.com>
Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 6e78901a
...@@ -485,10 +485,10 @@ struct erofs_map_blocks_iter { ...@@ -485,10 +485,10 @@ struct erofs_map_blocks_iter {
static inline struct page * static inline struct page *
erofs_get_inline_page_nofail(struct inode *inode, erofs_get_inline_page(struct inode *inode,
erofs_blk_t blkaddr) erofs_blk_t blkaddr)
{ {
return erofs_get_meta_page_nofail(inode->i_sb, return erofs_get_meta_page(inode->i_sb,
blkaddr, S_ISDIR(inode->i_mode)); blkaddr, S_ISDIR(inode->i_mode));
} }
......
...@@ -24,16 +24,25 @@ struct xattr_iter { ...@@ -24,16 +24,25 @@ struct xattr_iter {
static inline void xattr_iter_end(struct xattr_iter *it, bool atomic) static inline void xattr_iter_end(struct xattr_iter *it, bool atomic)
{ {
/* only init_inode_xattrs use non-atomic once */ /* the only user of kunmap() is 'init_inode_xattrs' */
if (unlikely(!atomic)) if (unlikely(!atomic))
kunmap(it->page); kunmap(it->page);
else else
kunmap_atomic(it->kaddr); kunmap_atomic(it->kaddr);
unlock_page(it->page); unlock_page(it->page);
put_page(it->page); put_page(it->page);
} }
static void init_inode_xattrs(struct inode *inode) static inline void xattr_iter_end_final(struct xattr_iter *it)
{
if (it->page == NULL)
return;
xattr_iter_end(it, true);
}
static int init_inode_xattrs(struct inode *inode)
{ {
struct xattr_iter it; struct xattr_iter it;
unsigned i; unsigned i;
...@@ -44,7 +53,7 @@ static void init_inode_xattrs(struct inode *inode) ...@@ -44,7 +53,7 @@ static void init_inode_xattrs(struct inode *inode)
bool atomic_map; bool atomic_map;
if (likely(inode_has_inited_xattr(inode))) if (likely(inode_has_inited_xattr(inode)))
return; return 0;
vi = EROFS_V(inode); vi = EROFS_V(inode);
BUG_ON(!vi->xattr_isize); BUG_ON(!vi->xattr_isize);
...@@ -54,8 +63,9 @@ static void init_inode_xattrs(struct inode *inode) ...@@ -54,8 +63,9 @@ static void init_inode_xattrs(struct inode *inode)
it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize); it.blkaddr = erofs_blknr(iloc(sbi, vi->nid) + vi->inode_isize);
it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize); it.ofs = erofs_blkoff(iloc(sbi, vi->nid) + vi->inode_isize);
it.page = erofs_get_inline_page_nofail(inode, it.blkaddr); it.page = erofs_get_inline_page(inode, it.blkaddr);
BUG_ON(IS_ERR(it.page)); if (IS_ERR(it.page))
return PTR_ERR(it.page);
/* read in shared xattr array (non-atomic, see kmalloc below) */ /* read in shared xattr array (non-atomic, see kmalloc below) */
it.kaddr = kmap(it.page); it.kaddr = kmap(it.page);
...@@ -64,9 +74,12 @@ static void init_inode_xattrs(struct inode *inode) ...@@ -64,9 +74,12 @@ static void init_inode_xattrs(struct inode *inode)
ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs); ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
vi->xattr_shared_count = ih->h_shared_count; vi->xattr_shared_count = ih->h_shared_count;
vi->xattr_shared_xattrs = (unsigned *)kmalloc_array( vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
vi->xattr_shared_count, sizeof(unsigned), sizeof(uint), GFP_KERNEL);
GFP_KERNEL | __GFP_NOFAIL); if (vi->xattr_shared_xattrs == NULL) {
xattr_iter_end(&it, atomic_map);
return -ENOMEM;
}
/* let's skip ibody header */ /* let's skip ibody header */
it.ofs += sizeof(struct erofs_xattr_ibody_header); it.ofs += sizeof(struct erofs_xattr_ibody_header);
...@@ -77,9 +90,10 @@ static void init_inode_xattrs(struct inode *inode) ...@@ -77,9 +90,10 @@ static void init_inode_xattrs(struct inode *inode)
BUG_ON(it.ofs != EROFS_BLKSIZ); BUG_ON(it.ofs != EROFS_BLKSIZ);
xattr_iter_end(&it, atomic_map); xattr_iter_end(&it, atomic_map);
it.page = erofs_get_meta_page_nofail(sb, it.page = erofs_get_meta_page(sb,
++it.blkaddr, S_ISDIR(inode->i_mode)); ++it.blkaddr, S_ISDIR(inode->i_mode));
BUG_ON(IS_ERR(it.page)); if (IS_ERR(it.page))
return PTR_ERR(it.page);
it.kaddr = kmap_atomic(it.page); it.kaddr = kmap_atomic(it.page);
atomic_map = true; atomic_map = true;
...@@ -92,6 +106,7 @@ static void init_inode_xattrs(struct inode *inode) ...@@ -92,6 +106,7 @@ static void init_inode_xattrs(struct inode *inode)
xattr_iter_end(&it, atomic_map); xattr_iter_end(&it, atomic_map);
inode_set_inited_xattr(inode); inode_set_inited_xattr(inode);
return 0;
} }
struct xattr_iter_handlers { struct xattr_iter_handlers {
...@@ -101,19 +116,26 @@ struct xattr_iter_handlers { ...@@ -101,19 +116,26 @@ struct xattr_iter_handlers {
void (*value)(struct xattr_iter *, unsigned, char *, unsigned); void (*value)(struct xattr_iter *, unsigned, char *, unsigned);
}; };
static void xattr_iter_fixup(struct xattr_iter *it) static inline int xattr_iter_fixup(struct xattr_iter *it)
{ {
if (unlikely(it->ofs >= EROFS_BLKSIZ)) { if (it->ofs < EROFS_BLKSIZ)
return 0;
xattr_iter_end(it, true); xattr_iter_end(it, true);
it->blkaddr += erofs_blknr(it->ofs); it->blkaddr += erofs_blknr(it->ofs);
it->page = erofs_get_meta_page_nofail(it->sb,
it->blkaddr, false); it->page = erofs_get_meta_page(it->sb, it->blkaddr, false);
BUG_ON(IS_ERR(it->page)); if (IS_ERR(it->page)) {
int err = PTR_ERR(it->page);
it->page = NULL;
return err;
}
it->kaddr = kmap_atomic(it->page); it->kaddr = kmap_atomic(it->page);
it->ofs = erofs_blkoff(it->ofs); it->ofs = erofs_blkoff(it->ofs);
} return 0;
} }
static int inline_xattr_iter_begin(struct xattr_iter *it, static int inline_xattr_iter_begin(struct xattr_iter *it,
...@@ -134,22 +156,25 @@ static int inline_xattr_iter_begin(struct xattr_iter *it, ...@@ -134,22 +156,25 @@ static int inline_xattr_iter_begin(struct xattr_iter *it,
it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs); it->blkaddr = erofs_blknr(iloc(sbi, vi->nid) + inline_xattr_ofs);
it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs); it->ofs = erofs_blkoff(iloc(sbi, vi->nid) + inline_xattr_ofs);
it->page = erofs_get_inline_page_nofail(inode, it->blkaddr); it->page = erofs_get_inline_page(inode, it->blkaddr);
BUG_ON(IS_ERR(it->page)); if (IS_ERR(it->page))
it->kaddr = kmap_atomic(it->page); return PTR_ERR(it->page);
it->kaddr = kmap_atomic(it->page);
return vi->xattr_isize - xattr_header_sz; return vi->xattr_isize - xattr_header_sz;
} }
static int xattr_foreach(struct xattr_iter *it, static int xattr_foreach(struct xattr_iter *it,
struct xattr_iter_handlers *op, unsigned *tlimit) const struct xattr_iter_handlers *op, unsigned int *tlimit)
{ {
struct erofs_xattr_entry entry; struct erofs_xattr_entry entry;
unsigned value_sz, processed, slice; unsigned value_sz, processed, slice;
int err; int err;
/* 0. fixup blkaddr, ofs, ipage */ /* 0. fixup blkaddr, ofs, ipage */
xattr_iter_fixup(it); err = xattr_iter_fixup(it);
if (err)
return err;
/* /*
* 1. read xattr entry to the memory, * 1. read xattr entry to the memory,
...@@ -181,7 +206,9 @@ static int xattr_foreach(struct xattr_iter *it, ...@@ -181,7 +206,9 @@ static int xattr_foreach(struct xattr_iter *it,
if (it->ofs >= EROFS_BLKSIZ) { if (it->ofs >= EROFS_BLKSIZ) {
BUG_ON(it->ofs > EROFS_BLKSIZ); BUG_ON(it->ofs > EROFS_BLKSIZ);
xattr_iter_fixup(it); err = xattr_iter_fixup(it);
if (err)
goto out;
it->ofs = 0; it->ofs = 0;
} }
...@@ -213,7 +240,10 @@ static int xattr_foreach(struct xattr_iter *it, ...@@ -213,7 +240,10 @@ static int xattr_foreach(struct xattr_iter *it,
while (processed < value_sz) { while (processed < value_sz) {
if (it->ofs >= EROFS_BLKSIZ) { if (it->ofs >= EROFS_BLKSIZ) {
BUG_ON(it->ofs > EROFS_BLKSIZ); BUG_ON(it->ofs > EROFS_BLKSIZ);
xattr_iter_fixup(it);
err = xattr_iter_fixup(it);
if (err)
goto out;
it->ofs = 0; it->ofs = 0;
} }
...@@ -273,7 +303,7 @@ static void xattr_copyvalue(struct xattr_iter *_it, ...@@ -273,7 +303,7 @@ static void xattr_copyvalue(struct xattr_iter *_it,
memcpy(it->buffer + processed, buf, len); memcpy(it->buffer + processed, buf, len);
} }
static struct xattr_iter_handlers find_xattr_handlers = { static const struct xattr_iter_handlers find_xattr_handlers = {
.entry = xattr_entrymatch, .entry = xattr_entrymatch,
.name = xattr_namematch, .name = xattr_namematch,
.alloc_buffer = xattr_checkbuffer, .alloc_buffer = xattr_checkbuffer,
...@@ -294,8 +324,11 @@ static int inline_getxattr(struct inode *inode, struct getxattr_iter *it) ...@@ -294,8 +324,11 @@ static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining); ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
if (ret >= 0) if (ret >= 0)
break; break;
if (ret != -ENOATTR) /* -ENOMEM, -EIO, etc. */
break;
} }
xattr_iter_end(&it->it, true); xattr_iter_end_final(&it->it);
return ret < 0 ? ret : it->buffer_size; return ret < 0 ? ret : it->buffer_size;
} }
...@@ -318,9 +351,10 @@ static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) ...@@ -318,9 +351,10 @@ static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
if (i) if (i)
xattr_iter_end(&it->it, true); xattr_iter_end(&it->it, true);
it->it.page = erofs_get_meta_page_nofail(sb, it->it.page = erofs_get_meta_page(sb, blkaddr, false);
blkaddr, false); if (IS_ERR(it->it.page))
BUG_ON(IS_ERR(it->it.page)); return PTR_ERR(it->it.page);
it->it.kaddr = kmap_atomic(it->it.page); it->it.kaddr = kmap_atomic(it->it.page);
it->it.blkaddr = blkaddr; it->it.blkaddr = blkaddr;
} }
...@@ -328,9 +362,12 @@ static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) ...@@ -328,9 +362,12 @@ static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL); ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
if (ret >= 0) if (ret >= 0)
break; break;
if (ret != -ENOATTR) /* -ENOMEM, -EIO, etc. */
break;
} }
if (vi->xattr_shared_count) if (vi->xattr_shared_count)
xattr_iter_end(&it->it, true); xattr_iter_end_final(&it->it);
return ret < 0 ? ret : it->buffer_size; return ret < 0 ? ret : it->buffer_size;
} }
...@@ -355,7 +392,9 @@ int erofs_getxattr(struct inode *inode, int index, ...@@ -355,7 +392,9 @@ int erofs_getxattr(struct inode *inode, int index,
if (unlikely(name == NULL)) if (unlikely(name == NULL))
return -EINVAL; return -EINVAL;
init_inode_xattrs(inode); ret = init_inode_xattrs(inode);
if (ret)
return ret;
it.index = index; it.index = index;
...@@ -498,7 +537,7 @@ static int xattr_skipvalue(struct xattr_iter *_it, ...@@ -498,7 +537,7 @@ static int xattr_skipvalue(struct xattr_iter *_it,
return 1; return 1;
} }
static struct xattr_iter_handlers list_xattr_handlers = { static const struct xattr_iter_handlers list_xattr_handlers = {
.entry = xattr_entrylist, .entry = xattr_entrylist,
.name = xattr_namelist, .name = xattr_namelist,
.alloc_buffer = xattr_skipvalue, .alloc_buffer = xattr_skipvalue,
...@@ -520,7 +559,7 @@ static int inline_listxattr(struct listxattr_iter *it) ...@@ -520,7 +559,7 @@ static int inline_listxattr(struct listxattr_iter *it)
if (ret < 0) if (ret < 0)
break; break;
} }
xattr_iter_end(&it->it, true); xattr_iter_end_final(&it->it);
return ret < 0 ? ret : it->buffer_ofs; return ret < 0 ? ret : it->buffer_ofs;
} }
...@@ -542,9 +581,10 @@ static int shared_listxattr(struct listxattr_iter *it) ...@@ -542,9 +581,10 @@ static int shared_listxattr(struct listxattr_iter *it)
if (i) if (i)
xattr_iter_end(&it->it, true); xattr_iter_end(&it->it, true);
it->it.page = erofs_get_meta_page_nofail(sb, it->it.page = erofs_get_meta_page(sb, blkaddr, false);
blkaddr, false); if (IS_ERR(it->it.page))
BUG_ON(IS_ERR(it->it.page)); return PTR_ERR(it->it.page);
it->it.kaddr = kmap_atomic(it->it.page); it->it.kaddr = kmap_atomic(it->it.page);
it->it.blkaddr = blkaddr; it->it.blkaddr = blkaddr;
} }
...@@ -554,7 +594,7 @@ static int shared_listxattr(struct listxattr_iter *it) ...@@ -554,7 +594,7 @@ static int shared_listxattr(struct listxattr_iter *it)
break; break;
} }
if (vi->xattr_shared_count) if (vi->xattr_shared_count)
xattr_iter_end(&it->it, true); xattr_iter_end_final(&it->it);
return ret < 0 ? ret : it->buffer_ofs; return ret < 0 ? ret : it->buffer_ofs;
} }
...@@ -565,7 +605,9 @@ ssize_t erofs_listxattr(struct dentry *dentry, ...@@ -565,7 +605,9 @@ ssize_t erofs_listxattr(struct dentry *dentry,
int ret; int ret;
struct listxattr_iter it; struct listxattr_iter it;
init_inode_xattrs(d_inode(dentry)); ret = init_inode_xattrs(d_inode(dentry));
if (ret)
return ret;
it.dentry = dentry; it.dentry = dentry;
it.buffer = buffer; it.buffer = buffer;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment