1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* -*- linux-c -*- ------------------------------------------------------- * 3 * 4 * Copyright 2001 H. Peter Anvin - All Rights Reserved 5 * 6 * ----------------------------------------------------------------------- */ 7 8 /* 9 * linux/fs/isofs/compress.c 10 * 11 * Transparent decompression of files on an iso9660 filesystem 12 */ 13 14 #include <linux/module.h> 15 #include <linux/init.h> 16 #include <linux/bio.h> 17 18 #include <linux/slab.h> 19 #include <linux/vmalloc.h> 20 #include <linux/zlib.h> 21 22 #include "isofs.h" 23 #include "zisofs.h" 24 25 /* This should probably be global. */ 26 static char zisofs_sink_page[PAGE_SIZE]; 27 28 /* 29 * This contains the zlib memory allocation and the mutex for the 30 * allocation; this avoids failures at block-decompression time. 31 */ 32 static void *zisofs_zlib_workspace; 33 static DEFINE_MUTEX(zisofs_zlib_lock); 34 35 /* 36 * Read data of @inode from @block_start to @block_end and uncompress 37 * to one zisofs block. Store the data in the @pages array with @pcount 38 * entries. Start storing at offset @poffset of the first page. 39 */ 40 static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, 41 loff_t block_end, int pcount, 42 struct folio **folios, unsigned int poffset, 43 int *errp) 44 { 45 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; 46 unsigned int bufsize = ISOFS_BUFFER_SIZE(inode); 47 unsigned int bufshift = ISOFS_BUFFER_BITS(inode); 48 unsigned int bufmask = bufsize - 1; 49 int i, block_size = block_end - block_start; 50 z_stream stream = { .total_out = 0, 51 .avail_in = 0, 52 .avail_out = 0, }; 53 int zerr; 54 int needblocks = (block_size + (block_start & bufmask) + bufmask) 55 >> bufshift; 56 int haveblocks; 57 blkcnt_t blocknum; 58 struct buffer_head **bhs; 59 int curbh, curpage; 60 61 if (block_size > deflateBound(1UL << zisofs_block_shift)) { 62 *errp = -EIO; 63 return 0; 64 } 65 /* Empty block? */ 66 if (block_size == 0) { 67 for ( i = 0 ; i < pcount ; i++ ) { 68 unsigned int off = i ? 0 : poffset; 69 struct folio *folio = folios[i]; 70 71 if (!folio) 72 continue; 73 folio_zero_range(folio, off, folio_size(folio) - off); 74 folio_mark_uptodate(folio); 75 } 76 return (((loff_t)pcount) << PAGE_SHIFT) - poffset; 77 } 78 79 /* Because zlib is not thread-safe, do all the I/O at the top. */ 80 blocknum = block_start >> bufshift; 81 bhs = kzalloc_objs(*bhs, needblocks + 1); 82 if (!bhs) { 83 *errp = -ENOMEM; 84 return 0; 85 } 86 haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks); 87 bh_read_batch(haveblocks, bhs); 88 89 curbh = 0; 90 curpage = 0; 91 /* 92 * First block is special since it may be fractional. We also wait for 93 * it before grabbing the zlib mutex; odds are that the subsequent 94 * blocks are going to come in in short order so we don't hold the zlib 95 * mutex longer than necessary. 96 */ 97 98 if (!bhs[0]) 99 goto b_eio; 100 101 wait_on_buffer(bhs[0]); 102 if (!buffer_uptodate(bhs[0])) { 103 *errp = -EIO; 104 goto b_eio; 105 } 106 107 stream.workspace = zisofs_zlib_workspace; 108 mutex_lock(&zisofs_zlib_lock); 109 110 zerr = zlib_inflateInit(&stream); 111 if (zerr != Z_OK) { 112 if (zerr == Z_MEM_ERROR) 113 *errp = -ENOMEM; 114 else 115 *errp = -EIO; 116 printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n", 117 zerr); 118 goto z_eio; 119 } 120 121 while (curpage < pcount && curbh < haveblocks && 122 zerr != Z_STREAM_END) { 123 struct folio *folio = folios[curpage]; 124 125 if (!stream.avail_out) { 126 if (folio) { 127 stream.next_out = kmap_local_folio(folio, poffset); 128 stream.avail_out = folio_size(folio) - poffset; 129 poffset = 0; 130 } else { 131 stream.next_out = (void *)&zisofs_sink_page; 132 stream.avail_out = PAGE_SIZE; 133 } 134 } 135 if (!stream.avail_in) { 136 wait_on_buffer(bhs[curbh]); 137 if (!buffer_uptodate(bhs[curbh])) { 138 *errp = -EIO; 139 break; 140 } 141 stream.next_in = bhs[curbh]->b_data + 142 (block_start & bufmask); 143 stream.avail_in = min_t(unsigned, bufsize - 144 (block_start & bufmask), 145 block_size); 146 block_size -= stream.avail_in; 147 block_start = 0; 148 } 149 150 while (stream.avail_out && stream.avail_in) { 151 zerr = zlib_inflate(&stream, Z_SYNC_FLUSH); 152 if (zerr == Z_BUF_ERROR && stream.avail_in == 0) 153 break; 154 if (zerr == Z_STREAM_END) 155 break; 156 if (zerr != Z_OK) { 157 /* EOF, error, or trying to read beyond end of input */ 158 if (zerr == Z_MEM_ERROR) 159 *errp = -ENOMEM; 160 else { 161 printk(KERN_DEBUG 162 "zisofs: zisofs_inflate returned" 163 " %d, inode = %llu," 164 " page idx = %d, bh idx = %d," 165 " avail_in = %ld," 166 " avail_out = %ld\n", 167 zerr, inode->i_ino, curpage, 168 curbh, stream.avail_in, 169 stream.avail_out); 170 *errp = -EIO; 171 } 172 goto inflate_out; 173 } 174 } 175 176 if (!stream.avail_out) { 177 /* This page completed */ 178 if (folio) { 179 flush_dcache_folio(folio); 180 folio_mark_uptodate(folio); 181 } 182 if (stream.next_out != (unsigned char *)zisofs_sink_page) { 183 kunmap_local(stream.next_out); 184 stream.next_out = NULL; 185 } 186 curpage++; 187 } 188 if (!stream.avail_in) 189 curbh++; 190 } 191 inflate_out: 192 zlib_inflateEnd(&stream); 193 if (stream.next_out && stream.next_out != (unsigned char *)zisofs_sink_page) 194 kunmap_local(stream.next_out); 195 196 z_eio: 197 mutex_unlock(&zisofs_zlib_lock); 198 199 b_eio: 200 for (i = 0; i < haveblocks; i++) 201 brelse(bhs[i]); 202 kfree(bhs); 203 return stream.total_out; 204 } 205 206 /* 207 * Uncompress data so that pages[full_page] is fully uptodate and possibly 208 * fills in other pages if we have data for them. 209 */ 210 static int zisofs_fill_pages(struct inode *inode, int full_page, int pcount, 211 struct folio **folios) 212 { 213 loff_t start_off, end_off; 214 loff_t block_start, block_end; 215 unsigned int header_size = ISOFS_I(inode)->i_format_parm[0]; 216 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; 217 unsigned int blockptr; 218 loff_t poffset = 0; 219 blkcnt_t cstart_block, cend_block; 220 struct buffer_head *bh; 221 unsigned int blkbits = ISOFS_BUFFER_BITS(inode); 222 unsigned int blksize = 1 << blkbits; 223 int err; 224 loff_t ret; 225 226 BUG_ON(!folios[full_page]); 227 228 /* 229 * We want to read at least 'full_page' page. Because we have to 230 * uncompress the whole compression block anyway, fill the surrounding 231 * pages with the data we have anyway... 232 */ 233 start_off = folio_pos(folios[full_page]); 234 end_off = min_t(loff_t, start_off + PAGE_SIZE, inode->i_size); 235 236 cstart_block = start_off >> zisofs_block_shift; 237 cend_block = (end_off + (1 << zisofs_block_shift) - 1) 238 >> zisofs_block_shift; 239 240 WARN_ON(start_off - (full_page << PAGE_SHIFT) != 241 ((cstart_block << zisofs_block_shift) & PAGE_MASK)); 242 243 /* Find the pointer to this specific chunk */ 244 /* Note: we're not using isonum_731() here because the data is known aligned */ 245 /* Note: header_size is in 32-bit words (4 bytes) */ 246 blockptr = (header_size + cstart_block) << 2; 247 bh = isofs_bread(inode, blockptr >> blkbits); 248 if (!bh) 249 return -EIO; 250 block_start = le32_to_cpu(*(__le32 *) 251 (bh->b_data + (blockptr & (blksize - 1)))); 252 253 while (cstart_block < cend_block && pcount > 0) { 254 /* Load end of the compressed block in the file */ 255 blockptr += 4; 256 /* Traversed to next block? */ 257 if (!(blockptr & (blksize - 1))) { 258 brelse(bh); 259 260 bh = isofs_bread(inode, blockptr >> blkbits); 261 if (!bh) 262 return -EIO; 263 } 264 block_end = le32_to_cpu(*(__le32 *) 265 (bh->b_data + (blockptr & (blksize - 1)))); 266 if (block_start > block_end) { 267 brelse(bh); 268 return -EIO; 269 } 270 err = 0; 271 ret = zisofs_uncompress_block(inode, block_start, block_end, 272 pcount, folios, poffset, &err); 273 poffset += ret; 274 folios += poffset >> PAGE_SHIFT; 275 pcount -= poffset >> PAGE_SHIFT; 276 full_page -= poffset >> PAGE_SHIFT; 277 poffset &= ~PAGE_MASK; 278 279 if (err) { 280 brelse(bh); 281 /* 282 * Did we finish reading the page we really wanted 283 * to read? 284 */ 285 if (full_page < 0) 286 return 0; 287 return err; 288 } 289 290 block_start = block_end; 291 cstart_block++; 292 } 293 294 if (poffset && *folios) { 295 struct folio *folio = *folios; 296 297 folio_zero_range(folio, poffset, folio_size(folio) - poffset); 298 folio_mark_uptodate(folio); 299 } 300 brelse(bh); 301 return 0; 302 } 303 304 /* 305 * When decompressing, we typically obtain more than one page 306 * per reference. We inject the additional pages into the page 307 * cache as a form of readahead. 308 */ 309 static int zisofs_read_folio(struct file *file, struct folio *folio) 310 { 311 struct inode *inode = file_inode(file); 312 struct address_space *mapping = inode->i_mapping; 313 int err; 314 int i, pcount, full_page; 315 unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; 316 unsigned int zisofs_pages_per_cblock = 317 PAGE_SHIFT <= zisofs_block_shift ? 318 (1 << (zisofs_block_shift - PAGE_SHIFT)) : 0; 319 struct folio **folios; 320 pgoff_t index = folio->index, end_index; 321 322 end_index = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 323 /* 324 * If this folio is wholly outside i_size we just return zero; 325 * do_generic_file_read() will handle this for us 326 */ 327 if (index >= end_index) { 328 folio_end_read(folio, true); 329 return 0; 330 } 331 332 if (PAGE_SHIFT <= zisofs_block_shift) { 333 /* We have already been given one page, this is the one 334 we must do. */ 335 full_page = index & (zisofs_pages_per_cblock - 1); 336 pcount = min_t(int, zisofs_pages_per_cblock, 337 end_index - (index & ~(zisofs_pages_per_cblock - 1))); 338 index -= full_page; 339 } else { 340 full_page = 0; 341 pcount = 1; 342 } 343 folios = kzalloc_objs(*folios, 344 max_t(unsigned int, zisofs_pages_per_cblock, 1)); 345 if (!folios) { 346 folio_unlock(folio); 347 return -ENOMEM; 348 } 349 folios[full_page] = folio; 350 351 for (i = 0; i < pcount; i++, index++) { 352 if (i == full_page) 353 continue; 354 folios[i] = __filemap_get_folio(mapping, index, 355 FGP_LOCK | FGP_CREAT | FGP_NOWAIT, 356 mapping_gfp_mask(mapping)); 357 if (IS_ERR(folios[i])) 358 folios[i] = NULL; 359 } 360 361 err = zisofs_fill_pages(inode, full_page, pcount, folios); 362 363 /* Release any residual folios, do not mark them uptodate */ 364 for (i = 0; i < pcount; i++) { 365 if (folios[i]) { 366 flush_dcache_folio(folios[i]); 367 folio_unlock(folios[i]); 368 if (i != full_page) 369 folio_put(folios[i]); 370 } 371 } 372 373 /* At this point, err contains 0 or -EIO depending on the "critical" page */ 374 kfree(folios); 375 return err; 376 } 377 378 const struct address_space_operations zisofs_aops = { 379 .read_folio = zisofs_read_folio, 380 /* No bmap operation supported */ 381 }; 382 383 int __init zisofs_init(void) 384 { 385 zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize()); 386 if ( !zisofs_zlib_workspace ) 387 return -ENOMEM; 388 389 return 0; 390 } 391 392 void zisofs_cleanup(void) 393 { 394 vfree(zisofs_zlib_workspace); 395 } 396