fs: add support for SquashFS
Subsystems (2)
RFC 1/1 fs: add support for SquashFS
From: Yegor Yefremov <[email protected]>
--- fs/Kconfig | 1 + fs/Makefile | 1 + fs/squashfs/Kconfig | 31 +++ fs/squashfs/Makefile | 14 ++ fs/squashfs/block.c | 216 +++++++++++++++++ fs/squashfs/cache.c | 455 ++++++++++++++++++++++++++++++++++++ fs/squashfs/decompressor.c | 144 ++++++++++++ fs/squashfs/decompressor.h | 61 +++++ fs/squashfs/decompressor_single.c | 86 +++++++ fs/squashfs/dir.c | 238 +++++++++++++++++++ fs/squashfs/file.c | 463 +++++++++++++++++++++++++++++++++++++ fs/squashfs/file_cache.c | 35 +++ fs/squashfs/fragment.c | 98 ++++++++ fs/squashfs/id.c | 103 +++++++++ fs/squashfs/inode.c | 475 ++++++++++++++++++++++++++++++++++++++ fs/squashfs/namei.c | 374 ++++++++++++++++++++++++++++++ fs/squashfs/page_actor.c | 99 ++++++++ fs/squashfs/page_actor.h | 53 +++++ fs/squashfs/squashfs.c | 323 ++++++++++++++++++++++++++ fs/squashfs/squashfs.h | 153 ++++++++++++ fs/squashfs/squashfs_fs.h | 458 ++++++++++++++++++++++++++++++++++++ fs/squashfs/squashfs_fs_i.h | 56 +++++ fs/squashfs/squashfs_fs_sb.h | 86 +++++++ fs/squashfs/super.c | 333 ++++++++++++++++++++++++++ fs/squashfs/xz_wrapper.c | 194 ++++++++++++++++ 25 files changed, 4550 insertions(+) create mode 100644 fs/squashfs/Kconfig create mode 100644 fs/squashfs/Makefile create mode 100644 fs/squashfs/block.c create mode 100644 fs/squashfs/cache.c create mode 100644 fs/squashfs/decompressor.c create mode 100644 fs/squashfs/decompressor.h create mode 100644 fs/squashfs/decompressor_single.c create mode 100644 fs/squashfs/dir.c create mode 100644 fs/squashfs/file.c create mode 100644 fs/squashfs/file_cache.c create mode 100644 fs/squashfs/fragment.c create mode 100644 fs/squashfs/id.c create mode 100644 fs/squashfs/inode.c create mode 100644 fs/squashfs/namei.c create mode 100644 fs/squashfs/page_actor.c create mode 100644 fs/squashfs/page_actor.h create mode 100644 fs/squashfs/squashfs.c create mode 100644 fs/squashfs/squashfs.h create mode 100644 fs/squashfs/squashfs_fs.h create mode 100644 fs/squashfs/squashfs_fs_i.h create mode 100644 fs/squashfs/squashfs_fs_sb.h create mode 100644 fs/squashfs/super.c create mode 100644 fs/squashfs/xz_wrapper.c
fs/Kconfig +1 -0
@@ -90,6 +90,7 @@ config FS_SMHFS Barebox source fs/pstore/Kconfig +source fs/squashfs/Kconfig config FS_RATP bool
fs/Makefile +1 -0
@@ -16,3 +16,4 @@ obj-$(CONFIG_FS_EFIVARFS) += efivarfs.o obj-$(CONFIG_FS_SMHFS) += smhfs.o obj-$(CONFIG_FS_PSTORE) += pstore/ obj-$(CONFIG_FS_RATP) += ratpfs.o +obj-$(CONFIG_FS_SQUASHFS) += squashfs/
fs/squashfs/Kconfig new file +31 -0
@@ -0,0 +1,31 @@ +menuconfig FS_SQUASHFS + bool + prompt "squashfs support" + help + Saying Y here includes support for SquashFS 4.0 (a Compressed + Read-Only File System). Squashfs is a highly compressed read-only + filesystem for Linux. It uses zlib, lzo or xz compression to + compress both files, inodes and directories. Inodes in the system + are very small and all blocks are packed to minimise data overhead. + Block sizes greater than 4K are supported up to a maximum of 1 Mbytes + (default block size 128K). SquashFS 4.0 supports 64 bit filesystems + and files (larger than 4GB), full uid/gid information, hard links and + timestamps. + + Squashfs is intended for general read-only filesystem use, for + archival use (i.e. in cases where a .tar.gz file may be used), and in + embedded systems where low overhead is needed. Further information + and tools are available from http://squashfs.sourceforge.net. + +config SQUASHFS_XZ + bool "Include support for XZ compressed file systems" + depends on FS_SQUASHFS + select XZ_DECOMPRESS + help + Saying Y here includes support for reading Squashfs file systems + compressed with XZ compression. XZ gives better compression than + the default zlib compression, at the expense of greater CPU and + memory overhead. + + XZ is not the standard compression used in Squashfs and so most + file systems will be readable without selecting this option.
fs/squashfs/Makefile new file +14 -0
@@ -0,0 +1,14 @@ +obj-y += squashfs.o +obj-y += block.o +obj-y += cache.o +obj-y += decompressor.o +obj-y += decompressor_single.o +obj-y += dir.o +obj-y += file.o +obj-y += file_cache.o +obj-y += fragment.o +obj-y += id.o +obj-y += inode.o +obj-y += namei.o +obj-y += super.o +obj-y += xz_wrapper.o
fs/squashfs/block.c new file +216 -0
216 lines not on this page · raw message
fs/squashfs/cache.c new file +455 -0
455 lines not on this page · raw message
fs/squashfs/decompressor.c new file +144 -0
144 lines not on this page · raw message
fs/squashfs/decompressor.h new file +61 -0
61 lines not on this page · raw message
fs/squashfs/decompressor_single.c new file +86 -0
86 lines not on this page · raw message
fs/squashfs/dir.c new file +238 -0
238 lines not on this page · raw message
fs/squashfs/file.c new file +463 -0
463 lines not on this page · raw message
fs/squashfs/file_cache.c new file +35 -0
@@ -0,0 +1,35 @@ +/* +* Copyright (c) 2013 +* Phillip Lougher <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#include <linux/fs.h> +#include <linux/kernel.h> +#include <linux/string.h> +#include <linux/pagemap.h> + +#include "squashfs_fs.h" +#include "squashfs_fs_sb.h" +#include "squashfs_fs_i.h" +#include "squashfs.h" + +/* Read separately compressed datablock and memcopy into page cache */ +int squashfs_readpage_block (struct page *page,u64 block,int bsize) +{ +struct inode *i = page->inode; +struct squashfs_cache_entry *buffer =squashfs_get_datablock (i->i_sb, + block, bsize); +int res = buffer->error; + +if (res) +ERROR ("Unable to read page, block %llx, size %x\n" , block, + bsize); +else +squashfs_copy_cache (page, buffer, buffer->length,0 ); + +squashfs_cache_put (buffer); +return res; +}
fs/squashfs/fragment.c new file +98 -0
98 lines not on this page · raw message
fs/squashfs/id.c new file +103 -0
103 lines not on this page · raw message
fs/squashfs/inode.c new file +475 -0
475 lines not on this page · raw message
fs/squashfs/namei.c new file +374 -0
374 lines not on this page · raw message
fs/squashfs/page_actor.c new file +99 -0
99 lines not on this page · raw message
fs/squashfs/page_actor.h new file +53 -0
53 lines not on this page · raw message
fs/squashfs/squashfs.c new file +323 -0
44 lines not on this page · raw message
fs/squashfs/squashfs.h new file +153 -0
153 lines not on this page · raw message
fs/squashfs/squashfs_fs.h new file +458 -0
458 lines not on this page · raw message
fs/squashfs/squashfs_fs_i.h new file +56 -0
56 lines not on this page · raw message
fs/squashfs/squashfs_fs_sb.h new file +86 -0
86 lines not on this page · raw message
fs/squashfs/super.c new file +333 -0
333 lines not on this page · raw message
fs/squashfs/xz_wrapper.c new file +194 -0
194 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 7 lines
2.1.4 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Part of the diff is not shown here. View the raw message.
Quoted 3 lines · On Thu, Feb 11, 2016 at 3:56 PM, <[email protected]> wrote:
From: Yegor Yefremov <[email protected]>
Signed-off-by Yegor Yefremov <[email protected]>
This is a first attempt to add SqaushFS to Barebox. The port is still not fully functional, but it is possible to read files.
TODO:
1. improve directory listing: for now only root directory can be listed
2. support symlinks
3. implement freeing allocated structures on routine exit
4. remove Linux related stuff like spinlocks, wait etc.
5. add other decompressors (Gzip etc.)
Yegor
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Hi Yegor,
This doesn't look too bad. Some things I noticed while looking at it and trying to compile it.
Quoted 3 lines · On Thu, Feb 11, 2016 at 03:56:15PM +0100, [email protected] wrote:
From: Yegor Yefremov <[email protected]>
Signed-off-by Yegor Yefremov <[email protected]>
The final commit should be a bit more descriptive. It should at least contain the Kernel version you started from so that later know from which kernel version on we have to look for updates.
Quoted 27 lines
fs/squashfs/Kconfig new file +21
-0@@ -0,0 +1,31 @@ +menuconfig FS_SQUASHFS + bool + prompt "squashfs support" + help + Saying Y here includes support for SquashFS 4.0 (a Compressed + Read-Only File System). Squashfs is a highly compressed read-only + filesystem for Linux. It uses zlib, lzo or xz compression to + compress both files, inodes and directories. Inodes in the system + are very small and all blocks are packed to minimise data overhead. + Block sizes greater than 4K are supported up to a maximum of 1 Mbytes + (default block size 128K). SquashFS 4.0 supports 64 bit filesystems + and files (larger than 4GB), full uid/gid information, hard links and + timestamps. + + Squashfs is intended for general read-only filesystem use, for + archival use (i.e. in cases where a .tar.gz file may be used), and in + embedded systems where low overhead is needed. Further information + and tools are available from http://squashfs.sourceforge.net. + +config SQUASHFS_XZ + bool "Include support for XZ compressed file systems"
fs/squashfs/Kconfig line 31
Quoted 10 lines
fs/squashfs/Kconfig new file +10
-0+ depends on FS_SQUASHFS + select XZ_DECOMPRESS + help + Saying Y here includes support for reading Squashfs file systems + compressed with XZ compression. XZ gives better compression than + the default zlib compression, at the expense of greater CPU and + memory overhead. + + XZ is not the standard compression used in Squashfs and so most + file systems will be readable without selecting this option.
I noticed we currently get linker errors when XZ decompression is disabled:
fs/built-in.o: In function `squashfs_xz_uncompress':
:(.text.squashfs_xz_uncompress+0x14): undefined reference to xz_dec_reset' :(.text.squashfs_xz_uncompress+0x98): undefined reference to xz_dec_run' fs/built-in.o: In function `squashfs_xz_free':
:(.text.squashfs_xz_free+0x8): undefined reference to xz_dec_end' fs/built-in.o: In function squashfs_xz_init':
:(.text.squashfs_xz_init+0x12): undefined reference to xz_dec_init' :(.text.squashfs_xz_init+0x22): undefined reference to xz_crc32_init'
Quoted 7 lines
fs/squashfs/Kconfig new file +7
-0+ +/* + * Read the metadata block length, this is stored in the first two + * bytes of the metadata block. + */ +static char *get_block_length(struct super_block *sb, + u64 *cur_index, int *offset, int *length)
fs/squashfs/Kconfig
Quoted 10 lines
fs/squashfs/Kconfig new file +10
-0+{ + struct squashfs_sb_info *msblk = sb->s_fs_info; + char *buf; + int rc; + + buf = squashfs_devread(msblk, + *cur_index * msblk->devblksize, + msblk->devblksize); + if (buf == NULL) + return NULL;
buf holds allocated memory here and is reinitialized with another buffer below without being freed beforehand. Please make sure that you eliminate those memory leaks. I usually do something like:
meminfo; mount -t squashfs /dev/friesel /frasel; umount /frasel; meminfo
Do this command (or a cp command) a few times, memory consumption should not increase. Note that the whole command should be in a single line, otherwise the commands you type will go into the command history each time you hit return and eat up a few bytes each time, and this would also increase memory usage.
Quoted 82 lines
fs/squashfs/Kconfig new file +81
-0+ + if (msblk->devblksize - *offset == 1) { + *length = (unsigned char) buf[*offset]; + buf = squashfs_devread(msblk, + ++(*cur_index) * msblk->devblksize, + msblk->devblksize); + if (buf == NULL) + return NULL; + *length |= (unsigned char) buf[0] << 8; + *offset = 1; + } else { + *length = (unsigned char) buf[*offset] | + (unsigned char) buf[*offset + 1] << 8; + *offset += 2; + + if (*offset == msblk->devblksize) { + buf = squashfs_devread(msblk, + ++(*cur_index) * msblk->devblksize, + msblk->devblksize); + if (buf == NULL) + return NULL; + *offset = 0; + } + } + + return buf; +} + + +/* + * Blocks in Squashfs are compressed. To avoid repeatedly decompressing + * recently accessed data Squashfs uses two small metadata and fragment caches. + * + * This file implements a generic cache implementation used for both caches, + * plus functions layered ontop of the generic cache implementation to + * access the metadata and fragment caches. + * + * To avoid out of memory and fragmentation issues with vmalloc the cache + * uses sequences of kmalloced PAGE_CACHE_SIZE buffers. + * + * It should be noted that the cache is not used for file datablocks, these + * are decompressed and cached in the page-cache in the normal way. The + * cache is only used to temporarily cache fragment and metadata blocks + * which have been read as as a result of a metadata (i.e. inode or + * directory) or fragment access. Because metadata and fragments are packed + * together into blocks (to gain greater compression) the read of a particular + * piece of metadata or fragment will retrieve other metadata/fragments which + * have been packed with it, these because of locality-of-reference may be read + * in the near future. Temporarily caching them ensures they are available for + * near future access without requiring an additional read and decompress. + */ + +#include <linux/fs.h> +#include <linux/pagemap.h> + +#include "squashfs_fs.h" +#include "squashfs_fs_sb.h" +#include "squashfs.h" +#include "page_actor.h" + +#if 1 +/* + * Look-up block in cache, and increment usage count. If not in cache, read + * and decompress it from disk. + */ +struct squashfs_cache_entry *squashfs_cache_get(struct super_block *sb, + struct squashfs_cache *cache, u64 block, int length) +{ + int i, n; + struct squashfs_cache_entry *entry; + + spin_lock(&cache->lock); + + while (1) { + for (i = cache->curr_blk, n = 0; n < cache->entries; n++) { + if (cache->entry[i].block == block) { + cache->curr_blk = i; + break; + } + i = (i + 1) % cache->entries; + }
fs/squashfs/Kconfig
Quoted 10 lines
fs/squashfs/Kconfig new file +10
-0+ + if (n == cache->entries) { + /* + * Block not in cache, if all cache entries are used + * go to sleep waiting for one to become available. + */ + if (cache->unused == 0) { + cache->num_waiters++; + spin_unlock(&cache->lock); + //wait_event(cache->wait_queue, cache->unused);
Did you ever hit this point? Do bad things happen then? I mean this condition the original code wants to wait upon will not become true.
Quoted 13 lines
fs/squashfs/Kconfig new file +12
-0+ spin_lock(&cache->lock); + cache->num_waiters--; + continue; + } + + /* + +char *squashfs_devread(struct squashfs_sb_info *fs, int byte_offset, + int byte_len) +{ + ssize_t size; + char *buf;
fs/squashfs/Kconfig
Quoted 10 lines
fs/squashfs/Kconfig new file +10
-0+ + buf = malloc(byte_len); + if (buf == NULL) + return NULL; + + size = cdev_read(fs->cdev, buf, byte_len, byte_offset, 0); + if (size < 0) { + dev_err(fs->dev, "read error: %s\n", + strerror(-size)); + return NULL;
free buf?
Quoted 28 lines
fs/squashfs/Kconfig new file +28
-0+ } + + return buf; +} + +struct squashfs_priv { + struct super_block *sb; +}; + +static struct dentry *squashfs_findfile(struct super_block *sb, const char *filename) +{ + int ret; + char *next; + char fpath[128]; + char *name = fpath; + unsigned long root_inum = 1; + unsigned long inum; + struct inode *inode = 0; + long long root_inode = 0; + struct dentry *dir1, *dir2 = NULL; + inode = sb->s_root->d_inode; + + strcpy(fpath, filename); + + /* Remove all leading slashes */ + while (*name == '/') + name++; +
fs/squashfs/Kconfig
Quoted 10 lines
fs/squashfs/Kconfig new file +10
-0+ /* + * Handle root-direcoty ('/') + */ + if (!name || *name == '\0') + return sb->s_root; + + dir1 = malloc(sizeof(struct dentry)); + if (dir1 == NULL) { + TRACE("Error allocating dir1\n"); + }
Why don't you allocate this on the stack? Then you wouldn't have to free it.
Please run the whole thing through scipts/checkpatch.pl. It contains several trailing whitespaces and other stylistic stuff.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Quoted 4 lines · Sascha Hauer
Hi Yegor,
This doesn't look too bad. Some things I noticed while looking at it and trying to compile it.
And BTW an entry in Documentation/filesystems would be nice.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox