Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security
Insights
Files
master
Documentation
arch
block
certs
crypto
drivers
firmware
fs
9p
adfs
affs
afs
autofs4
befs
bfs
btrfs
cachefiles
ceph
cifs
coda
configfs
cramfs
crypto
debugfs
devpts
dlm
ecryptfs
efivarfs
efs
exofs
exportfs
ext2
ext4
f2fs
fat
freevxfs
fscache
fuse
gfs2
hfs
hfsplus
hostfs
hpfs
hugetlbfs
isofs
Kconfig
Makefile
compress.c
dir.c
export.c
inode.c
isofs.h
joliet.c
namei.c
rock.c
rock.h
util.c
zisofs.h
jbd2
jffs2
jfs
kernfs
lockd
logfs
minix
ncpfs
nfs
nfs_common
nfsd
nilfs2
nls
notify
ntfs
ocfs2
omfs
openpromfs
orangefs
overlayfs
proc
pstore
qnx4
qnx6
quota
ramfs
reiserfs
romfs
squashfs
sysfs
sysv
tracefs
ubifs
udf
ufs
xfs
Kconfig
Kconfig.binfmt
Makefile
aio.c
anon_inodes.c
attr.c
bad_inode.c
binfmt_aout.c
binfmt_elf.c
binfmt_elf_fdpic.c
binfmt_em86.c
binfmt_flat.c
binfmt_misc.c
binfmt_script.c
block_dev.c
buffer.c
char_dev.c
compat.c
compat_binfmt_elf.c
compat_ioctl.c
coredump.c
dax.c
dcache.c
dcookies.c
direct-io.c
drop_caches.c
eventfd.c
eventpoll.c
exec.c
fcntl.c
fhandle.c
file.c
file_table.c
filesystems.c
fs-writeback.c
fs_pin.c
fs_struct.c
inode.c
internal.h
ioctl.c
libfs.c
locks.c
mbcache.c
mount.h
mpage.c
namei.c
namespace.c
no-block.c
nsfs.c
open.c
pipe.c
pnode.c
pnode.h
posix_acl.c
proc_namespace.c
read_write.c
readdir.c
select.c
seq_file.c
signalfd.c
splice.c
stack.c
stat.c
statfs.c
super.c
sync.c
timerfd.c
userfaultfd.c
utimes.c
xattr.c
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
usr
virt
.get_maintainer.ignore
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
REPORTING-BUGS
Breadcrumbs
linux
/
fs
/
isofs
/
compress.c
Blame
Blame
Latest commit
History
History
380 lines (339 loc) · 10 KB
Breadcrumbs
linux
/
fs
/
isofs
/
compress.c
Top
File metadata and controls
Code
Blame
380 lines (339 loc) · 10 KB
Raw
/* -*- linux-c -*- ------------------------------------------------------- * * * Copyright 2001 H. Peter Anvin - All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139, * USA; either version 2 of the License, or (at your option) any later * version; incorporated herein by reference. * * ----------------------------------------------------------------------- */ /* * linux/fs/isofs/compress.c * * Transparent decompression of files on an iso9660 filesystem */ #include <linux/module.h> #include <linux/init.h> #include <linux/vmalloc.h> #include <linux/zlib.h> #include "isofs.h" #include "zisofs.h" /* This should probably be global. */ static char zisofs_sink_page[PAGE_SIZE]; /* * This contains the zlib memory allocation and the mutex for the * allocation; this avoids failures at block-decompression time. */ static void *zisofs_zlib_workspace; static DEFINE_MUTEX(zisofs_zlib_lock); /* * Read data of @inode from @block_start to @block_end and uncompress * to one zisofs block. Store the data in the @pages array with @pcount * entries. Start storing at offset @poffset of the first page. */ static loff_t zisofs_uncompress_block(struct inode *inode, loff_t block_start, loff_t block_end, int pcount, struct page **pages, unsigned poffset, int *errp) { unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; unsigned int bufsize = ISOFS_BUFFER_SIZE(inode); unsigned int bufshift = ISOFS_BUFFER_BITS(inode); unsigned int bufmask = bufsize - 1; int i, block_size = block_end - block_start; z_stream stream = { .total_out = 0, .avail_in = 0, .avail_out = 0, }; int zerr; int needblocks = (block_size + (block_start & bufmask) + bufmask) >> bufshift; int haveblocks; blkcnt_t blocknum; struct buffer_head *bhs[needblocks + 1]; int curbh, curpage; if (block_size > deflateBound(1UL << zisofs_block_shift)) { *errp = -EIO; return 0; } /* Empty block? */ if (block_size == 0) { for ( i = 0 ; i < pcount ; i++ ) { if (!pages[i]) continue; memset(page_address(pages[i]), 0, PAGE_SIZE); flush_dcache_page(pages[i]); SetPageUptodate(pages[i]); } return ((loff_t)pcount) << PAGE_SHIFT; } /* Because zlib is not thread-safe, do all the I/O at the top. */ blocknum = block_start >> bufshift; memset(bhs, 0, (needblocks + 1) * sizeof(struct buffer_head *)); haveblocks = isofs_get_blocks(inode, blocknum, bhs, needblocks); ll_rw_block(READ, haveblocks, bhs); curbh = 0; curpage = 0; /* * First block is special since it may be fractional. We also wait for * it before grabbing the zlib mutex; odds are that the subsequent * blocks are going to come in in short order so we don't hold the zlib * mutex longer than necessary. */ if (!bhs[0]) goto b_eio; wait_on_buffer(bhs[0]); if (!buffer_uptodate(bhs[0])) { *errp = -EIO; goto b_eio; } stream.workspace = zisofs_zlib_workspace; mutex_lock(&zisofs_zlib_lock); zerr = zlib_inflateInit(&stream); if (zerr != Z_OK) { if (zerr == Z_MEM_ERROR) *errp = -ENOMEM; else *errp = -EIO; printk(KERN_DEBUG "zisofs: zisofs_inflateInit returned %d\n", zerr); goto z_eio; } while (curpage < pcount && curbh < haveblocks && zerr != Z_STREAM_END) { if (!stream.avail_out) { if (pages[curpage]) { stream.next_out = page_address(pages[curpage]) + poffset; stream.avail_out = PAGE_SIZE - poffset; poffset = 0; } else { stream.next_out = (void *)&zisofs_sink_page; stream.avail_out = PAGE_SIZE; } } if (!stream.avail_in) { wait_on_buffer(bhs[curbh]); if (!buffer_uptodate(bhs[curbh])) { *errp = -EIO; break; } stream.next_in = bhs[curbh]->b_data + (block_start & bufmask); stream.avail_in = min_t(unsigned, bufsize - (block_start & bufmask), block_size); block_size -= stream.avail_in; block_start = 0; } while (stream.avail_out && stream.avail_in) { zerr = zlib_inflate(&stream, Z_SYNC_FLUSH); if (zerr == Z_BUF_ERROR && stream.avail_in == 0) break; if (zerr == Z_STREAM_END) break; if (zerr != Z_OK) { /* EOF, error, or trying to read beyond end of input */ if (zerr == Z_MEM_ERROR) *errp = -ENOMEM; else { printk(KERN_DEBUG "zisofs: zisofs_inflate returned" " %d, inode = %lu," " page idx = %d, bh idx = %d," " avail_in = %ld," " avail_out = %ld\n", zerr, inode->i_ino, curpage, curbh, stream.avail_in, stream.avail_out); *errp = -EIO; } goto inflate_out; } } if (!stream.avail_out) { /* This page completed */ if (pages[curpage]) { flush_dcache_page(pages[curpage]); SetPageUptodate(pages[curpage]); } curpage++; } if (!stream.avail_in) curbh++; } inflate_out: zlib_inflateEnd(&stream); z_eio: mutex_unlock(&zisofs_zlib_lock); b_eio: for (i = 0; i < haveblocks; i++) brelse(bhs[i]); return stream.total_out; } /* * Uncompress data so that pages[full_page] is fully uptodate and possibly * fills in other pages if we have data for them. */ static int zisofs_fill_pages(struct inode *inode, int full_page, int pcount, struct page **pages) { loff_t start_off, end_off; loff_t block_start, block_end; unsigned int header_size = ISOFS_I(inode)->i_format_parm[0]; unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; unsigned int blockptr; loff_t poffset = 0; blkcnt_t cstart_block, cend_block; struct buffer_head *bh; unsigned int blkbits = ISOFS_BUFFER_BITS(inode); unsigned int blksize = 1 << blkbits; int err; loff_t ret; BUG_ON(!pages[full_page]); /* * We want to read at least 'full_page' page. Because we have to * uncompress the whole compression block anyway, fill the surrounding * pages with the data we have anyway... */ start_off = page_offset(pages[full_page]); end_off = min_t(loff_t, start_off + PAGE_SIZE, inode->i_size); cstart_block = start_off >> zisofs_block_shift; cend_block = (end_off + (1 << zisofs_block_shift) - 1) >> zisofs_block_shift; WARN_ON(start_off - (full_page << PAGE_SHIFT) != ((cstart_block << zisofs_block_shift) & PAGE_MASK)); /* Find the pointer to this specific chunk */ /* Note: we're not using isonum_731() here because the data is known aligned */ /* Note: header_size is in 32-bit words (4 bytes) */ blockptr = (header_size + cstart_block) << 2; bh = isofs_bread(inode, blockptr >> blkbits); if (!bh) return -EIO; block_start = le32_to_cpu(*(__le32 *) (bh->b_data + (blockptr & (blksize - 1)))); while (cstart_block < cend_block && pcount > 0) { /* Load end of the compressed block in the file */ blockptr += 4; /* Traversed to next block? */ if (!(blockptr & (blksize - 1))) { brelse(bh); bh = isofs_bread(inode, blockptr >> blkbits); if (!bh) return -EIO; } block_end = le32_to_cpu(*(__le32 *) (bh->b_data + (blockptr & (blksize - 1)))); if (block_start > block_end) { brelse(bh); return -EIO; } err = 0; ret = zisofs_uncompress_block(inode, block_start, block_end, pcount, pages, poffset, &err); poffset += ret; pages += poffset >> PAGE_SHIFT; pcount -= poffset >> PAGE_SHIFT; full_page -= poffset >> PAGE_SHIFT; poffset &= ~PAGE_MASK; if (err) { brelse(bh); /* * Did we finish reading the page we really wanted * to read? */ if (full_page < 0) return 0; return err; } block_start = block_end; cstart_block++; } if (poffset && *pages) { memset(page_address(*pages) + poffset, 0, PAGE_SIZE - poffset); flush_dcache_page(*pages); SetPageUptodate(*pages); } return 0; } /* * When decompressing, we typically obtain more than one page * per reference. We inject the additional pages into the page * cache as a form of readahead. */ static int zisofs_readpage(struct file *file, struct page *page) { struct inode *inode = file_inode(file); struct address_space *mapping = inode->i_mapping; int err; int i, pcount, full_page; unsigned int zisofs_block_shift = ISOFS_I(inode)->i_format_parm[1]; unsigned int zisofs_pages_per_cblock = PAGE_SHIFT <= zisofs_block_shift ? (1 << (zisofs_block_shift - PAGE_SHIFT)) : 0; struct page *pages[max_t(unsigned, zisofs_pages_per_cblock, 1)]; pgoff_t index = page->index, end_index; end_index = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; /* * If this page is wholly outside i_size we just return zero; * do_generic_file_read() will handle this for us */ if (index >= end_index) { SetPageUptodate(page); unlock_page(page); return 0; } if (PAGE_SHIFT <= zisofs_block_shift) { /* We have already been given one page, this is the one we must do. */ full_page = index & (zisofs_pages_per_cblock - 1); pcount = min_t(int, zisofs_pages_per_cblock, end_index - (index & ~(zisofs_pages_per_cblock - 1))); index -= full_page; } else { full_page = 0; pcount = 1; } pages[full_page] = page; for (i = 0; i < pcount; i++, index++) { if (i != full_page) pages[i] = grab_cache_page_nowait(mapping, index); if (pages[i]) { ClearPageError(pages[i]); kmap(pages[i]); } } err = zisofs_fill_pages(inode, full_page, pcount, pages); /* Release any residual pages, do not SetPageUptodate */ for (i = 0; i < pcount; i++) { if (pages[i]) { flush_dcache_page(pages[i]); if (i == full_page && err) SetPageError(pages[i]); kunmap(pages[i]); unlock_page(pages[i]); if (i != full_page) put_page(pages[i]); } } /* At this point, err contains 0 or -EIO depending on the "critical" page */ return err; } const struct address_space_operations zisofs_aops = { .readpage = zisofs_readpage, /* No sync_page operation supported? */ /* No bmap operation supported */ }; int __init zisofs_init(void) { zisofs_zlib_workspace = vmalloc(zlib_inflate_workspacesize()); if ( !zisofs_zlib_workspace ) return -ENOMEM; return 0; } void zisofs_cleanup(void) { vfree(zisofs_zlib_workspace); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
You can’t perform that action at this time.