Skip to content

Commit

Permalink
initramfs: support initramfs that is bigger than 2GiB
Browse files Browse the repository at this point in the history
Now with 64bit bzImage and kexec tools, we support ramdisk that size is
bigger than 2g, as we could put it above 4G.

Found compressed initramfs image could not be decompressed properly.  It
turns out that image length is int during decompress detection, and it
will become < 0 when length is more than 2G.  Furthermore, during
decompressing len as int is used for inbuf count, that has problem too.

Change len to long, that should be ok as on 32 bit platform long is
32bits.

Tested with following compressed initramfs image as root with kexec.
	gzip, bzip2, xz, lzma, lzop, lz4.
run time for populate_rootfs():
   size        name       Nehalem-EX  Westmere-EX  Ivybridge-EX
 9034400256 root_img     :   26s           24s          30s
 3561095057 root_img.lz4 :   28s           27s          27s
 3459554629 root_img.lzo :   29s           29s          28s
 3219399480 root_img.gz  :   64s           62s          49s
 2251594592 root_img.xz  :  262s          260s         183s
 2226366598 root_img.lzma:  386s          376s         277s
 2901482513 root_img.bz2 :  635s          599s

Signed-off-by: Yinghai Lu <[email protected]>
Cc: "H. Peter Anvin" <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Rashika Kheria <[email protected]>
Cc: Josh Triplett <[email protected]>
Cc: Kyungsik Lee <[email protected]>
Cc: P J P <[email protected]>
Cc: Al Viro <[email protected]>
Cc: Tetsuo Handa <[email protected]>
Cc: "Daniel M. Weeks" <[email protected]>
Cc: Alexandre Courbot <[email protected]>
Cc: Jan Beulich <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Yinghai Lu authored and torvalds committed Aug 8, 2014
1 parent 3874743 commit d97b07c
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 110 deletions.
8 changes: 4 additions & 4 deletions crypto/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static int zlib_compress_update(struct crypto_pcomp *tfm,
}

ret = req->avail_out - stream->avail_out;
pr_debug("avail_in %u, avail_out %u (consumed %u, produced %u)\n",
pr_debug("avail_in %lu, avail_out %lu (consumed %lu, produced %u)\n",
stream->avail_in, stream->avail_out,
req->avail_in - stream->avail_in, ret);
req->next_in = stream->next_in;
Expand Down Expand Up @@ -198,7 +198,7 @@ static int zlib_compress_final(struct crypto_pcomp *tfm,
}

ret = req->avail_out - stream->avail_out;
pr_debug("avail_in %u, avail_out %u (consumed %u, produced %u)\n",
pr_debug("avail_in %lu, avail_out %lu (consumed %lu, produced %u)\n",
stream->avail_in, stream->avail_out,
req->avail_in - stream->avail_in, ret);
req->next_in = stream->next_in;
Expand Down Expand Up @@ -283,7 +283,7 @@ static int zlib_decompress_update(struct crypto_pcomp *tfm,
}

ret = req->avail_out - stream->avail_out;
pr_debug("avail_in %u, avail_out %u (consumed %u, produced %u)\n",
pr_debug("avail_in %lu, avail_out %lu (consumed %lu, produced %u)\n",
stream->avail_in, stream->avail_out,
req->avail_in - stream->avail_in, ret);
req->next_in = stream->next_in;
Expand Down Expand Up @@ -331,7 +331,7 @@ static int zlib_decompress_final(struct crypto_pcomp *tfm,
}

ret = req->avail_out - stream->avail_out;
pr_debug("avail_in %u, avail_out %u (consumed %u, produced %u)\n",
pr_debug("avail_in %lu, avail_out %lu (consumed %lu, produced %u)\n",
stream->avail_in, stream->avail_out,
req->avail_in - stream->avail_in, ret);
req->next_in = stream->next_in;
Expand Down
4 changes: 2 additions & 2 deletions fs/isofs/compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start,
"zisofs: zisofs_inflate returned"
" %d, inode = %lu,"
" page idx = %d, bh idx = %d,"
" avail_in = %d,"
" avail_out = %d\n",
" avail_in = %ld,"
" avail_out = %ld\n",
zerr, inode->i_ino, curpage,
curbh, stream.avail_in,
stream.avail_out);
Expand Down
7 changes: 4 additions & 3 deletions fs/jffs2/compr_zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ static int jffs2_zlib_compress(unsigned char *data_in,

while (def_strm.total_out < *dstlen - STREAM_END_SPACE && def_strm.total_in < *sourcelen) {
def_strm.avail_out = *dstlen - (def_strm.total_out + STREAM_END_SPACE);
def_strm.avail_in = min((unsigned)(*sourcelen-def_strm.total_in), def_strm.avail_out);
jffs2_dbg(1, "calling deflate with avail_in %d, avail_out %d\n",
def_strm.avail_in = min_t(unsigned long,
(*sourcelen-def_strm.total_in), def_strm.avail_out);
jffs2_dbg(1, "calling deflate with avail_in %ld, avail_out %ld\n",
def_strm.avail_in, def_strm.avail_out);
ret = zlib_deflate(&def_strm, Z_PARTIAL_FLUSH);
jffs2_dbg(1, "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n",
jffs2_dbg(1, "deflate returned with avail_in %ld, avail_out %ld, total_in %ld, total_out %ld\n",
def_strm.avail_in, def_strm.avail_out,
def_strm.total_in, def_strm.total_out);
if (ret != Z_OK) {
Expand Down
8 changes: 4 additions & 4 deletions include/linux/decompress/bunzip2.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef DECOMPRESS_BUNZIP2_H
#define DECOMPRESS_BUNZIP2_H

int bunzip2(unsigned char *inbuf, int len,
int(*fill)(void*, unsigned int),
int(*flush)(void*, unsigned int),
int bunzip2(unsigned char *inbuf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *output,
int *pos,
long *pos,
void(*error)(char *x));
#endif
10 changes: 5 additions & 5 deletions include/linux/decompress/generic.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef DECOMPRESS_GENERIC_H
#define DECOMPRESS_GENERIC_H

typedef int (*decompress_fn) (unsigned char *inbuf, int len,
int(*fill)(void*, unsigned int),
int(*flush)(void*, unsigned int),
typedef int (*decompress_fn) (unsigned char *inbuf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *outbuf,
int *posp,
long *posp,
void(*error)(char *x));

/* inbuf - input buffer
Expand Down Expand Up @@ -33,7 +33,7 @@ typedef int (*decompress_fn) (unsigned char *inbuf, int len,


/* Utility routine to detect the decompression method */
decompress_fn decompress_method(const unsigned char *inbuf, int len,
decompress_fn decompress_method(const unsigned char *inbuf, long len,
const char **name);

#endif
8 changes: 4 additions & 4 deletions include/linux/decompress/inflate.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef LINUX_DECOMPRESS_INFLATE_H
#define LINUX_DECOMPRESS_INFLATE_H

int gunzip(unsigned char *inbuf, int len,
int(*fill)(void*, unsigned int),
int(*flush)(void*, unsigned int),
int gunzip(unsigned char *inbuf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *output,
int *pos,
long *pos,
void(*error_fn)(char *x));
#endif
8 changes: 4 additions & 4 deletions include/linux/decompress/unlz4.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef DECOMPRESS_UNLZ4_H
#define DECOMPRESS_UNLZ4_H

int unlz4(unsigned char *inbuf, int len,
int(*fill)(void*, unsigned int),
int(*flush)(void*, unsigned int),
int unlz4(unsigned char *inbuf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *output,
int *pos,
long *pos,
void(*error)(char *x));
#endif
8 changes: 4 additions & 4 deletions include/linux/decompress/unlzma.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef DECOMPRESS_UNLZMA_H
#define DECOMPRESS_UNLZMA_H

int unlzma(unsigned char *, int,
int(*fill)(void*, unsigned int),
int(*flush)(void*, unsigned int),
int unlzma(unsigned char *, long,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *output,
int *posp,
long *posp,
void(*error)(char *x)
);

Expand Down
8 changes: 4 additions & 4 deletions include/linux/decompress/unlzo.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef DECOMPRESS_UNLZO_H
#define DECOMPRESS_UNLZO_H

int unlzo(unsigned char *inbuf, int len,
int(*fill)(void*, unsigned int),
int(*flush)(void*, unsigned int),
int unlzo(unsigned char *inbuf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *output,
int *pos,
long *pos,
void(*error)(char *x));
#endif
8 changes: 4 additions & 4 deletions include/linux/decompress/unxz.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#ifndef DECOMPRESS_UNXZ_H
#define DECOMPRESS_UNXZ_H

int unxz(unsigned char *in, int in_size,
int (*fill)(void *dest, unsigned int size),
int (*flush)(void *src, unsigned int size),
unsigned char *out, int *in_used,
int unxz(unsigned char *in, long in_size,
long (*fill)(void *dest, unsigned long size),
long (*flush)(void *src, unsigned long size),
unsigned char *out, long *in_used,
void (*error)(char *x));

#endif
4 changes: 2 additions & 2 deletions include/linux/zlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ struct internal_state;

typedef struct z_stream_s {
const Byte *next_in; /* next input byte */
uInt avail_in; /* number of bytes available at next_in */
uLong avail_in; /* number of bytes available at next_in */
uLong total_in; /* total nb of input bytes read so far */

Byte *next_out; /* next output byte should be put there */
uInt avail_out; /* remaining free space at next_out */
uLong avail_out; /* remaining free space at next_out */
uLong total_out; /* total nb of bytes output so far */

char *msg; /* last error message, NULL if no error */
Expand Down
10 changes: 5 additions & 5 deletions init/do_mounts_rd.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,23 @@ static int exit_code;
static int decompress_error;
static int crd_infd, crd_outfd;

static int __init compr_fill(void *buf, unsigned int len)
static long __init compr_fill(void *buf, unsigned long len)
{
int r = sys_read(crd_infd, buf, len);
long r = sys_read(crd_infd, buf, len);
if (r < 0)
printk(KERN_ERR "RAMDISK: error while reading compressed data");
else if (r == 0)
printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
return r;
}

static int __init compr_flush(void *window, unsigned int outcnt)
static long __init compr_flush(void *window, unsigned long outcnt)
{
int written = sys_write(crd_outfd, window, outcnt);
long written = sys_write(crd_outfd, window, outcnt);
if (written != outcnt) {
if (decompress_error == 0)
printk(KERN_ERR
"RAMDISK: incomplete write (%d != %d)\n",
"RAMDISK: incomplete write (%ld != %ld)\n",
written, outcnt);
decompress_error = 1;
return -1;
Expand Down
22 changes: 11 additions & 11 deletions init/initramfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static __initdata enum state {
} state, next_state;

static __initdata char *victim;
static __initdata unsigned count;
static unsigned long count __initdata;
static __initdata loff_t this_header, next_header;

static inline void __init eat(unsigned n)
Expand All @@ -209,7 +209,7 @@ static inline void __init eat(unsigned n)

static __initdata char *vcollected;
static __initdata char *collected;
static __initdata int remains;
static long remains __initdata;
static __initdata char *collect;

static void __init read_into(char *buf, unsigned size, enum state next)
Expand All @@ -236,7 +236,7 @@ static int __init do_start(void)

static int __init do_collect(void)
{
unsigned n = remains;
unsigned long n = remains;
if (count < n)
n = count;
memcpy(collect, victim, n);
Expand Down Expand Up @@ -407,7 +407,7 @@ static __initdata int (*actions[])(void) = {
[Reset] = do_reset,
};

static int __init write_buffer(char *buf, unsigned len)
static long __init write_buffer(char *buf, unsigned long len)
{
count = len;
victim = buf;
Expand All @@ -417,11 +417,11 @@ static int __init write_buffer(char *buf, unsigned len)
return len - count;
}

static int __init flush_buffer(void *bufv, unsigned len)
static long __init flush_buffer(void *bufv, unsigned long len)
{
char *buf = (char *) bufv;
int written;
int origLen = len;
long written;
long origLen = len;
if (message)
return -1;
while ((written = write_buffer(buf, len)) < len && !message) {
Expand All @@ -440,13 +440,13 @@ static int __init flush_buffer(void *bufv, unsigned len)
return origLen;
}

static unsigned my_inptr; /* index of next byte to be processed in inbuf */
static unsigned long my_inptr; /* index of next byte to be processed in inbuf */

#include <linux/decompress/generic.h>

static char * __init unpack_to_rootfs(char *buf, unsigned len)
static char * __init unpack_to_rootfs(char *buf, unsigned long len)
{
int written, res;
long written;
decompress_fn decompress;
const char *compress_name;
static __initdata char msg_buf[64];
Expand Down Expand Up @@ -480,7 +480,7 @@ static char * __init unpack_to_rootfs(char *buf, unsigned len)
decompress = decompress_method(buf, len, &compress_name);
pr_debug("Detected %s compressed data\n", compress_name);
if (decompress) {
res = decompress(buf, len, NULL, flush_buffer, NULL,
int res = decompress(buf, len, NULL, flush_buffer, NULL,
&my_inptr, error);
if (res)
error("decompressor failed");
Expand Down
2 changes: 1 addition & 1 deletion lib/decompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static const struct compress_format compressed_formats[] __initconst = {
{ {0, 0}, NULL, NULL }
};

decompress_fn __init decompress_method(const unsigned char *inbuf, int len,
decompress_fn __init decompress_method(const unsigned char *inbuf, long len,
const char **name)
{
const struct compress_format *cf;
Expand Down
26 changes: 13 additions & 13 deletions lib/decompress_bunzip2.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ struct bunzip_data {
/* State for interrupting output loop */
int writeCopies, writePos, writeRunCountdown, writeCount, writeCurrent;
/* I/O tracking data (file handles, buffers, positions, etc.) */
int (*fill)(void*, unsigned int);
int inbufCount, inbufPos /*, outbufPos*/;
long (*fill)(void*, unsigned long);
long inbufCount, inbufPos /*, outbufPos*/;
unsigned char *inbuf /*,*outbuf*/;
unsigned int inbufBitCount, inbufBits;
/* The CRC values stored in the block header and calculated from the
Expand Down Expand Up @@ -617,16 +617,16 @@ static int INIT read_bunzip(struct bunzip_data *bd, char *outbuf, int len)
goto decode_next_byte;
}

static int INIT nofill(void *buf, unsigned int len)
static long INIT nofill(void *buf, unsigned long len)
{
return -1;
}

/* Allocate the structure, read file header. If in_fd ==-1, inbuf must contain
a complete bunzip file (len bytes long). If in_fd!=-1, inbuf and len are
ignored, and data is read from file handle into temporary buffer. */
static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,
int (*fill)(void*, unsigned int))
static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, long len,
long (*fill)(void*, unsigned long))
{
struct bunzip_data *bd;
unsigned int i, j, c;
Expand Down Expand Up @@ -675,11 +675,11 @@ static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, int len,

/* Example usage: decompress src_fd to dst_fd. (Stops at end of bzip2 data,
not end of file.) */
STATIC int INIT bunzip2(unsigned char *buf, int len,
int(*fill)(void*, unsigned int),
int(*flush)(void*, unsigned int),
STATIC int INIT bunzip2(unsigned char *buf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *outbuf,
int *pos,
long *pos,
void(*error)(char *x))
{
struct bunzip_data *bd;
Expand Down Expand Up @@ -743,11 +743,11 @@ STATIC int INIT bunzip2(unsigned char *buf, int len,
}

#ifdef PREBOOT
STATIC int INIT decompress(unsigned char *buf, int len,
int(*fill)(void*, unsigned int),
int(*flush)(void*, unsigned int),
STATIC int INIT decompress(unsigned char *buf, long len,
long (*fill)(void*, unsigned long),
long (*flush)(void*, unsigned long),
unsigned char *outbuf,
int *pos,
long *pos,
void(*error)(char *x))
{
return bunzip2(buf, len - 4, fill, flush, outbuf, pos, error);
Expand Down
Loading

0 comments on commit d97b07c

Please sign in to comment.