Hi,
This patch enables the uncompressed files support in cramfs.
The word 'uncompressed file' is from linear cramfs (aka Application XIP).
In linear cramfs, it is used to suport XIP on NOR. However it is also helpful on OneNAND. It makes a filesystem faster by removing compression overhead.
In XIP mode it runs XIP, But non-XIP mode. It copies data to ram and runs.
In my simple test, copy busybox (compressed or uncompressed).
It reduces the about 50% time saving from 0.40s to 0.19s.
Yes, it incrases the file system size, but nowadays flash has big capacity.
It's trade-off between size and performance.
Also this patch uses the page cache directly.
In previous implementation, it used the local buffer. why?
It's already uncompressed and fits to page size. So It uses the page directly to remove useless memory copy.
It's compatible the existing linear cramfs image and original one.
Any comments are welcome.
Thank you,
Kyungmin Park
Signed-off-by: Kyungmin Park <[email protected]>
---
diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
index 3d194a2..edba28f 100644
--- a/fs/cramfs/inode.c
+++ b/fs/cramfs/inode.c
@@ -40,6 +40,7 @@ static DEFINE_MUTEX(read_mutex);
#define CRAMINO(x) (((x)->offset && (x)->size)?(x)->offset<<2:1)
#define OFFSET(x) ((x)->i_ino)
+#define CRAMFS_INODE_IS_XIP(x) ((x)->i_mode & S_ISVTX)
static int cramfs_iget5_test(struct inode *inode, void *opaque)
{
@@ -143,8 +144,9 @@ static int next_buffer;
/*
* Returns a pointer to a buffer containing at least LEN bytes of
* filesystem starting at byte offset OFFSET into the filesystem.
+ * If the @pg has the page, it returns the page buffer address
*/
-static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len)
+static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len, struct page **pg)
{
struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
struct page *pages[BLKS_PER_BUF];
@@ -174,6 +176,22 @@ static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned i
devsize = mapping->host->i_size >> PAGE_CACHE_SHIFT;
+ /*
+ * Use page directly either
+ * - uncompressed page or
+ * - comprssed page which has all required data
+ */
+ if (pg && offset + len <= PAGE_CACHE_SIZE) {
+ struct page *page = NULL;
+ page = read_mapping_page(mapping, blocknr, NULL);
+ if (!IS_ERR(page)) {
+ *pg = page;
+ data = kmap(page);
+ data += offset;
+ return data;
+ }
+ }
+
/* Ok, read in BLKS_PER_BUF pages completely first. */
unread = 0;
for (i = 0; i < BLKS_PER_BUF; i++) {
@@ -253,14 +271,14 @@ static int cramfs_fill_super(struct super_block *sb, void *data, int silent)
buffer_blocknr[i] = -1;
/* Read the first block and get the superblock from it */
- memcpy(&super, cramfs_read(sb, 0, sizeof(super)), sizeof(super));
+ memcpy(&super, cramfs_read(sb, 0, sizeof(super), NULL), sizeof(super));
mutex_unlock(&read_mutex);
/* Do sanity checks on the superblock */
if (super.magic != CRAMFS_MAGIC) {
/* check at 512 byte offset */
mutex_lock(&read_mutex);
- memcpy(&super, cramfs_read(sb, 512, sizeof(super)), sizeof(super));
+ memcpy(&super, cramfs_read(sb, 512, sizeof(super), NULL), sizeof(super));
mutex_unlock(&read_mutex);
if (super.magic != CRAMFS_MAGIC) {
if (!silent)
@@ -367,7 +385,7 @@ static int cramfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
int namelen, error;
mutex_lock(&read_mutex);
- de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+256);
+ de = cramfs_read(sb, OFFSET(inode) + offset, sizeof(*de)+256, NULL);
name = (char *)(de+1);
/*
@@ -417,7 +435,7 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
char *name;
int namelen, retval;
- de = cramfs_read(dir->i_sb, OFFSET(dir) + offset, sizeof(*de)+256);
+ de = cramfs_read(dir->i_sb, OFFSET(dir) + offset, sizeof(*de)+256, NULL);
name = (char *)(de+1);
/* Try to take advantage of sorted directories */
@@ -463,21 +481,44 @@ static struct dentry * cramfs_lookup(struct inode *dir, struct dentry *dentry, s
static int cramfs_readpage(struct file *file, struct page * page)
{
struct inode *inode = page->mapping->host;
+ struct super_block *sb = inode->i_sb;
u32 maxblock, bytes_filled;
+ struct page *pg = NULL;
void *pgdata;
maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
bytes_filled = 0;
- if (page->index < maxblock) {
- struct super_block *sb = inode->i_sb;
+
+ /* Handle uncompressed files */
+ if (CRAMFS_INODE_IS_XIP(inode) && page->index < maxblock) {
+ u32 blkptr_offset = PAGE_ALIGN(OFFSET(inode)) +
+ (page->index << PAGE_CACHE_SHIFT);
+
+ pgdata = kmap(page);
+ mutex_lock(&read_mutex);
+ memcpy(pgdata,
+ cramfs_read(sb, blkptr_offset, PAGE_CACHE_SIZE, &pg),
+ PAGE_CACHE_SIZE);
+ if (pg) {
+ kunmap(pg);
+ page_cache_release(pg);
+ }
+ mutex_unlock(&read_mutex);
+ bytes_filled = PAGE_CACHE_SIZE;
+ } else if (page->index < maxblock) {
u32 blkptr_offset = OFFSET(inode) + page->index*4;
u32 start_offset, compr_len;
start_offset = OFFSET(inode) + maxblock*4;
mutex_lock(&read_mutex);
if (page->index)
- start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4, 4);
- compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4) - start_offset);
+ start_offset = *(u32 *) cramfs_read(sb, blkptr_offset-4, 4, NULL);
+ compr_len = (*(u32 *) cramfs_read(sb, blkptr_offset, 4, &pg) - start_offset);
+ if (pg) {
+ kunmap(pg);
+ page_cache_release(pg);
+ pg = NULL;
+ }
mutex_unlock(&read_mutex);
pgdata = kmap(page);
if (compr_len == 0)
@@ -487,9 +528,13 @@ static int cramfs_readpage(struct file *file, struct page * page)
else {
mutex_lock(&read_mutex);
bytes_filled = cramfs_uncompress_block(pgdata,
- PAGE_CACHE_SIZE,
- cramfs_read(sb, start_offset, compr_len),
- compr_len);
+ PAGE_CACHE_SIZE,
+ cramfs_read(sb, start_offset, compr_len, &pg),
+ compr_len);
+ if (pg) {
+ kunmap(pg);
+ page_cache_release(pg);
+ }
mutex_unlock(&read_mutex);
}
} else
> On Tue, 22 Jan 2008 11:23:45 +0900 Kyungmin Park <[email protected]> wrote:
> Hi,
>
> This patch enables the uncompressed files support in cramfs.
>
> The word 'uncompressed file' is from linear cramfs (aka Application XIP).
> In linear cramfs, it is used to suport XIP on NOR. However it is also helpful on OneNAND. It makes a filesystem faster by removing compression overhead.
> In XIP mode it runs XIP, But non-XIP mode. It copies data to ram and runs.
>
> In my simple test, copy busybox (compressed or uncompressed).
> It reduces the about 50% time saving from 0.40s to 0.19s.
> Yes, it incrases the file system size, but nowadays flash has big capacity.
> It's trade-off between size and performance.
>
> Also this patch uses the page cache directly.
> In previous implementation, it used the local buffer. why?
> It's already uncompressed and fits to page size. So It uses the page directly to remove useless memory copy.
>
> It's compatible the existing linear cramfs image and original one.
>
> Any comments are welcome.
>
> diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
> index 3d194a2..edba28f 100644
> --- a/fs/cramfs/inode.c
> +++ b/fs/cramfs/inode.c
I don't know what kernel this is against but it generates a lot of rejects
against present mainline.
> @@ -40,6 +40,7 @@ static DEFINE_MUTEX(read_mutex);
> #define CRAMINO(x) (((x)->offset && (x)->size)?(x)->offset<<2:1)
> #define OFFSET(x) ((x)->i_ino)
>
> +#define CRAMFS_INODE_IS_XIP(x) ((x)->i_mode & S_ISVTX)
>
> static int cramfs_iget5_test(struct inode *inode, void *opaque)
> {
> @@ -143,8 +144,9 @@ static int next_buffer;
> /*
> * Returns a pointer to a buffer containing at least LEN bytes of
> * filesystem starting at byte offset OFFSET into the filesystem.
> + * If the @pg has the page, it returns the page buffer address
> */
> -static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len)
> +static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned int len, struct page **pg)
> {
> struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
> struct page *pages[BLKS_PER_BUF];
> @@ -174,6 +176,22 @@ static void *cramfs_read(struct super_block *sb, unsigned int offset, unsigned i
>
> devsize = mapping->host->i_size >> PAGE_CACHE_SHIFT;
>
> + /*
> + * Use page directly either
> + * - uncompressed page or
> + * - comprssed page which has all required data
> + */
> + if (pg && offset + len <= PAGE_CACHE_SIZE) {
> + struct page *page = NULL;
> + page = read_mapping_page(mapping, blocknr, NULL);
> + if (!IS_ERR(page)) {
> + *pg = page;
> + data = kmap(page);
> + data += offset;
> + return data;
> + }
> + }
Are you sure about the kmap/kunmap handling in this patch? It looks like
it might be unbalanced.
> > This patch enables the uncompressed files support in cramfs.
> >
> > The word 'uncompressed file' is from linear cramfs (aka Application XIP).
> > In linear cramfs, it is used to suport XIP on NOR. However it is also helpful on OneNAND. It makes a filesystem faster by removing compression overhead.
> > In XIP mode it runs XIP, But non-XIP mode. It copies data to ram and runs.
> >
> > In my simple test, copy busybox (compressed or uncompressed).
> > It reduces the about 50% time saving from 0.40s to 0.19s.
> > Yes, it incrases the file system size, but nowadays flash has big capacity.
> > It's trade-off between size and performance.
> >
> > Also this patch uses the page cache directly.
> > In previous implementation, it used the local buffer. why?
> > It's already uncompressed and fits to page size. So It uses the page directly to remove useless memory copy.
> >
> > It's compatible the existing linear cramfs image and original one.
> >
> > Any comments are welcome.
> >
> > diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
> > index 3d194a2..edba28f 100644
> > --- a/fs/cramfs/inode.c
> > +++ b/fs/cramfs/inode.c
>
> I don't know what kernel this is against but it generates a lot of rejects
> against present mainline.
>
Sorry, it used another tree instead of mainline. Following patch will
be against mainline.
>
> Are you sure about the kmap/kunmap handling in this patch? It looks like
> it might be unbalanced.
>
Yes, It's matching. Because cramfs_read returns virtual address, it
returns the kmap-ped address if it had page, then caller kunmap-ed and
release it. Otherwise pg doesn't have page pointer.
Anyway, here's more simple method using get_block sytle.
With this patch, it can also use multi page read, cramfs_readpages.
However, it has no big improvement in my environment.
The goal is same it uses the requested page directly instead of
useless memory copy.
Any comments are welcome.
Thank you,
Kyungmin Park
Signed-off-by: Kyungmin Park <[email protected]>
diff --git a/fs/cramfs/inode.c b/fs/cramfs/inode.c
index 350680f..2d6736d 100644
--- a/fs/cramfs/inode.c
+++ b/fs/cramfs/inode.c
@@ -23,6 +23,7 @@
#include <linux/buffer_head.h>
#include <linux/vfs.h>
#include <linux/mutex.h>
+#include <linux/mpage.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>
@@ -40,6 +41,7 @@ static DEFINE_MUTEX(read_mutex);
#define CRAMINO(x) (((x)->offset && (x)->size)?(x)->offset<<2:1)
#define OFFSET(x) ((x)->i_ino)
+#define CRAMFS_INODE_IS_XIP(x) ((x)->i_mode & S_ISVTX)
static int cramfs_iget5_test(struct inode *inode, void *opaque)
{
@@ -468,16 +470,32 @@ static struct dentry * cramfs_lookup(struct
inode *dir, struct dentry *dentry, s
return NULL;
}
+static int cramfs_get_block(struct inode *inode, sector_t iblock,
+ struct buffer_head *bh_result, int create)
+{
+ /* A write? */
+ BUG_ON(unlikely(create));
+
+ iblock += (PAGE_ALIGN(OFFSET(inode)) >> PAGE_CACHE_SHIFT);
+ map_bh(bh_result, inode->i_sb, iblock);
+
+ return 0;
+}
+
static int cramfs_readpage(struct file *file, struct page * page)
{
struct inode *inode = page->mapping->host;
+ struct super_block *sb = inode->i_sb;
u32 maxblock, bytes_filled;
void *pgdata;
maxblock = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
bytes_filled = 0;
- if (page->index < maxblock) {
- struct super_block *sb = inode->i_sb;
+
+ /* Handle uncompressed files */
+ if (CRAMFS_INODE_IS_XIP(inode) && page->index < maxblock) {
+ return mpage_readpage(page, cramfs_get_block);
+ } else if (page->index < maxblock) {
u32 blkptr_offset = OFFSET(inode) + page->index*4;
u32 start_offset, compr_len;