Skip to content

Commit

Permalink
erofs: introduce meta buffer operations
Browse files Browse the repository at this point in the history
ANBZ: torvalds#228

commit fdf80a4 upstream.

In order to support subpage and folio for all uncompressed files,
introduce meta buffer descriptors, which can be effectively stored
on stack, in place of meta page operations.

This converts the uncompressed data path to meta buffers.

Link: https://lore.kernel.org/r/[email protected]
Reviewed-by: Liu Bo <[email protected]>
Reviewed-by: Chao Yu <[email protected]>
[ Gao Xiang: add anolis dax_read_one_pfn adaption. ]
Signed-off-by: Gao Xiang <[email protected]>
Acked-by: Joseph Qi <[email protected]>
  • Loading branch information
hsiangkao authored and josephhz committed Jan 20, 2022
1 parent f781feb commit 6a5a279
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
96 changes: 74 additions & 22 deletions fs/erofs/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void erofs_readendio(struct bio *bio)
bio_put(bio);
}

struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr)
static struct page *erofs_read_meta_page(struct super_block *sb, pgoff_t index)
{
struct address_space *mapping;
struct page *page;
Expand All @@ -43,21 +43,77 @@ struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr)

mapping = EROFS_SB(sb)->bootstrap->f_inode->i_mapping;
nofs_flag = memalloc_nofs_save();
page = read_cache_page(mapping, blkaddr,
page = read_cache_page(mapping, index,
(filler_t *)mapping->a_ops->readpage,
EROFS_SB(sb)->bootstrap);
memalloc_nofs_restore(nofs_flag);
} else {
mapping = sb->s_bdev->bd_inode->i_mapping;
page = read_cache_page_gfp(mapping, blkaddr,
page = read_cache_page_gfp(mapping, index,
mapping_gfp_constraint(mapping, ~__GFP_FS));
}
return page;
}

struct page *erofs_get_meta_page(struct super_block *sb, erofs_blk_t blkaddr)
{
struct page *page = erofs_read_meta_page(sb, blkaddr);

/* should already be PageUptodate */
if (!IS_ERR(page))
lock_page(page);
return page;
}

void erofs_unmap_metabuf(struct erofs_buf *buf)
{
if (buf->kmap_type == EROFS_KMAP)
kunmap(buf->page);
else if (buf->kmap_type == EROFS_KMAP_ATOMIC)
kunmap_atomic(buf->base);
buf->base = NULL;
buf->kmap_type = EROFS_NO_KMAP;
}

void erofs_put_metabuf(struct erofs_buf *buf)
{
if (!buf->page)
return;
erofs_unmap_metabuf(buf);
put_page(buf->page);
buf->page = NULL;
}

void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
erofs_blk_t blkaddr, enum erofs_kmap_type type)
{
erofs_off_t offset = blknr_to_addr(blkaddr);
pgoff_t index = offset >> PAGE_SHIFT;
struct page *page = buf->page;

if (!page || page->index != index) {
erofs_put_metabuf(buf);
page = erofs_read_meta_page(sb, index);
if (IS_ERR(page))
return page;
/* should already be PageUptodate, no need to lock page */
buf->page = page;
}
if (buf->kmap_type == EROFS_NO_KMAP) {
if (type == EROFS_KMAP)
buf->base = kmap(page);
else if (type == EROFS_KMAP_ATOMIC)
buf->base = kmap_atomic(page);
buf->kmap_type = type;
} else if (buf->kmap_type != type) {
DBG_BUGON(1);
return ERR_PTR(-EFAULT);
}
if (type == EROFS_NO_KMAP)
return NULL;
return buf->base + (offset & ~PAGE_MASK);
}

static int erofs_map_blocks_flatmode(struct inode *inode,
struct erofs_map_blocks *map,
int flags)
Expand All @@ -67,7 +123,7 @@ static int erofs_map_blocks_flatmode(struct inode *inode,
struct erofs_inode *vi = EROFS_I(inode);
bool tailendpacking = (vi->datalayout == EROFS_INODE_FLAT_INLINE);

nblocks = DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
nblocks = DIV_ROUND_UP(inode->i_size, EROFS_BLKSIZ);
lastblk = nblocks - tailendpacking;

/* there is no hole in flatmode */
Expand Down Expand Up @@ -108,10 +164,11 @@ int erofs_map_blocks(struct inode *inode,
struct super_block *sb = inode->i_sb;
struct erofs_inode *vi = EROFS_I(inode);
struct erofs_inode_chunk_index *idx;
struct page *page;
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
u64 chunknr;
unsigned int unit;
erofs_off_t pos;
void *kaddr;
int err = 0;

trace_erofs_map_blocks_enter(inode, map, flags);
Expand All @@ -137,9 +194,9 @@ int erofs_map_blocks(struct inode *inode,
pos = ALIGN(iloc(EROFS_SB(sb), vi->nid) + vi->inode_isize +
vi->xattr_isize, unit) + unit * chunknr;

page = erofs_get_meta_page(inode->i_sb, erofs_blknr(pos));
if (IS_ERR(page)) {
err = PTR_ERR(page);
kaddr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), EROFS_KMAP);
if (IS_ERR(kaddr)) {
err = PTR_ERR(kaddr);
goto out;
}
map->m_la = chunknr << vi->chunkbits;
Expand All @@ -148,7 +205,7 @@ int erofs_map_blocks(struct inode *inode,

/* handle block map */
if (!(vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES)) {
__le32 *blkaddr = page_address(page) + erofs_blkoff(pos);
__le32 *blkaddr = kaddr + erofs_blkoff(pos);

if (le32_to_cpu(*blkaddr) == EROFS_NULL_ADDR) {
map->m_flags = 0;
Expand All @@ -159,7 +216,7 @@ int erofs_map_blocks(struct inode *inode,
goto out_unlock;
}
/* parse chunk indexes */
idx = page_address(page) + erofs_blkoff(pos);
idx = kaddr + erofs_blkoff(pos);
switch (le32_to_cpu(idx->blkaddr)) {
case EROFS_NULL_ADDR:
map->m_flags = 0;
Expand All @@ -172,8 +229,7 @@ int erofs_map_blocks(struct inode *inode,
break;
}
out_unlock:
unlock_page(page);
put_page(page);
erofs_put_metabuf(&buf);
out:
if (!err)
map->m_llen = map->m_plen;
Expand Down Expand Up @@ -290,18 +346,16 @@ static inline struct bio *erofs_read_raw_page(struct bio *bio,
/* deal with inline page */
if (map.m_flags & EROFS_MAP_META) {
void *vsrc, *vto;
struct page *ipage;
struct erofs_buf buf = __EROFS_BUF_INITIALIZER;

DBG_BUGON(map.m_plen > PAGE_SIZE);

ipage = erofs_get_meta_page(sb, blknr);

if (IS_ERR(ipage)) {
err = PTR_ERR(ipage);
vsrc = erofs_read_metabuf(&buf, inode->i_sb,
blknr, EROFS_KMAP_ATOMIC);
if (IS_ERR(vsrc)) {
err = PTR_ERR(vsrc);
goto err_out;
}

vsrc = kmap_atomic(ipage);
vto = kmap_atomic(page);
memcpy(vto, vsrc + blkoff, map.m_plen);
memset(vto + map.m_plen, 0, PAGE_SIZE - map.m_plen);
Expand All @@ -310,10 +364,8 @@ static inline struct bio *erofs_read_raw_page(struct bio *bio,
flush_dcache_page(page);

SetPageUptodate(page);
/* TODO: could we unlock the page earlier? */
unlock_page(ipage);
put_page(ipage);

erofs_put_metabuf(&buf);
/* imply err = 0, see erofs_map_blocks */
goto has_updated;
}
Expand Down
13 changes: 13 additions & 0 deletions fs/erofs/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ static inline int erofs_wait_on_workgroup_freezed(struct erofs_workgroup *grp)
#error erofs cannot be used in this platform
#endif

enum erofs_kmap_type {
EROFS_NO_KMAP, /* don't map the buffer */
EROFS_KMAP, /* use kmap() to map the buffer */
EROFS_KMAP_ATOMIC, /* use kmap_atomic() to map the buffer */
};

struct erofs_buf {
struct page *page;
void *base;
enum erofs_kmap_type kmap_type;
};
#define __EROFS_BUF_INITIALIZER ((struct erofs_buf){ .page = NULL })

#define ROOT_NID(sb) ((sb)->root_nid)

#define erofs_blknr(addr) ((addr) / EROFS_BLKSIZ)
Expand Down

0 comments on commit 6a5a279

Please sign in to comment.