1 /* 2 * Squashfs - a compressed read only filesystem for Linux 3 * 4 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 5 * Phillip Lougher <phillip@squashfs.org.uk> 6 * 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. 11 * 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. 16 * 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. 20 * 21 * zlib_wrapper.c 22 */ 23 24 25 #include <linux/mutex.h> 26 #include <linux/buffer_head.h> 27 #include <linux/slab.h> 28 #include <linux/zlib.h> 29 #include <linux/vmalloc.h> 30 31 #include "squashfs_fs.h" 32 #include "squashfs_fs_sb.h" 33 #include "squashfs.h" 34 #include "decompressor.h" 35 36 static void *zlib_init(struct squashfs_sb_info *dummy, void *buff) 37 { 38 z_stream *stream = kmalloc(sizeof(z_stream), GFP_KERNEL); 39 if (stream == NULL) 40 goto failed; 41 stream->workspace = vmalloc(zlib_inflate_workspacesize()); 42 if (stream->workspace == NULL) 43 goto failed; 44 45 return stream; 46 47 failed: 48 ERROR("Failed to allocate zlib workspace\n"); 49 kfree(stream); 50 return ERR_PTR(-ENOMEM); 51 } 52 53 54 static void zlib_free(void *strm) 55 { 56 z_stream *stream = strm; 57 58 if (stream) 59 vfree(stream->workspace); 60 kfree(stream); 61 } 62 63 64 static int zlib_uncompress(struct squashfs_sb_info *msblk, void *strm, 65 void **buffer, struct buffer_head **bh, int b, int offset, int length, 66 int srclength, int pages) 67 { 68 int zlib_err, zlib_init = 0; 69 int k = 0, page = 0; 70 z_stream *stream = strm; 71 72 stream->avail_out = 0; 73 stream->avail_in = 0; 74 75 do { 76 if (stream->avail_in == 0 && k < b) { 77 int avail = min(length, msblk->devblksize - offset); 78 length -= avail; 79 stream->next_in = bh[k]->b_data + offset; 80 stream->avail_in = avail; 81 offset = 0; 82 } 83 84 if (stream->avail_out == 0 && page < pages) { 85 stream->next_out = buffer[page++]; 86 stream->avail_out = PAGE_CACHE_SIZE; 87 } 88 89 if (!zlib_init) { 90 zlib_err = zlib_inflateInit(stream); 91 if (zlib_err != Z_OK) 92 goto out; 93 zlib_init = 1; 94 } 95 96 zlib_err = zlib_inflate(stream, Z_SYNC_FLUSH); 97 98 if (stream->avail_in == 0 && k < b) 99 put_bh(bh[k++]); 100 } while (zlib_err == Z_OK); 101 102 if (zlib_err != Z_STREAM_END) 103 goto out; 104 105 zlib_err = zlib_inflateEnd(stream); 106 if (zlib_err != Z_OK) 107 goto out; 108 109 if (k < b) 110 goto out; 111 112 return stream->total_out; 113 114 out: 115 for (; k < b; k++) 116 put_bh(bh[k]); 117 118 return -EIO; 119 } 120 121 const struct squashfs_decompressor squashfs_zlib_comp_ops = { 122 .init = zlib_init, 123 .free = zlib_free, 124 .decompress = zlib_uncompress, 125 .id = ZLIB_COMPRESSION, 126 .name = "zlib", 127 .supported = 1 128 }; 129 130