2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2010 LG Electronics
5 * Chan Jeong <chan.jeong@lge.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2,
10 * or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <linux/mutex.h>
25 #include <linux/buffer_head.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/lzo.h>
30 #include "squashfs_fs.h"
31 #include "squashfs_fs_sb.h"
33 #include "decompressor.h"
34 #include "page_actor.h"
41 static void *lzo_init(struct squashfs_sb_info
*msblk
, void *buff
)
43 int block_size
= max_t(int, msblk
->block_size
, SQUASHFS_METADATA_SIZE
);
45 struct squashfs_lzo
*stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
48 stream
->input
= vmalloc(block_size
);
49 if (stream
->input
== NULL
)
51 stream
->output
= vmalloc(block_size
);
52 if (stream
->output
== NULL
)
60 ERROR("Failed to allocate lzo workspace\n");
62 return ERR_PTR(-ENOMEM
);
66 static void lzo_free(void *strm
)
68 struct squashfs_lzo
*stream
= strm
;
72 vfree(stream
->output
);
78 static int lzo_uncompress(struct squashfs_sb_info
*msblk
, void *strm
,
79 struct buffer_head
**bh
, int b
, int offset
, int length
,
80 struct squashfs_page_actor
*output
)
82 struct squashfs_lzo
*stream
= strm
;
83 void *buff
= stream
->input
, *data
;
84 int avail
, i
, bytes
= length
, res
;
85 size_t out_len
= output
->length
;
87 for (i
= 0; i
< b
; i
++) {
88 avail
= min(bytes
, msblk
->devblksize
- offset
);
89 memcpy(buff
, bh
[i
]->b_data
+ offset
, avail
);
96 res
= lzo1x_decompress_safe(stream
->input
, (size_t)length
,
97 stream
->output
, &out_len
);
101 res
= bytes
= (int)out_len
;
102 data
= squashfs_first_page(output
);
103 buff
= stream
->output
;
105 if (bytes
<= PAGE_SIZE
) {
106 memcpy(data
, buff
, bytes
);
109 memcpy(data
, buff
, PAGE_SIZE
);
112 data
= squashfs_next_page(output
);
115 squashfs_finish_page(output
);
123 const struct squashfs_decompressor squashfs_lzo_comp_ops
= {
126 .decompress
= lzo_uncompress
,
127 .id
= LZO_COMPRESSION
,