]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/patches/suse-2.6.27.31/patches.suse/squashfs-3.4.patch
Add a patch to fix Intel E100 wake-on-lan problems.
[people/pmueller/ipfire-2.x.git] / src / patches / suse-2.6.27.31 / patches.suse / squashfs-3.4.patch
CommitLineData
2cb7cef9
BS
1From: Phillip Lougher <phillip.lougher@gmail.com>
2Subject: squashfs v3.4
3Patch-mainline: Who knows?
4
5This patch contains the SquashFS file system.
6
7Acked-by: Greg Kroah-Hartman <gregkh@suse.com>
8
9---
10 fs/Kconfig | 50
11 fs/Makefile | 1
12 fs/squashfs/Makefile | 7
13 fs/squashfs/inode.c | 2174 +++++++++++++++++++++++++++++++++++++++++
14 fs/squashfs/squashfs.h | 86 +
15 fs/squashfs/squashfs2_0.c | 740 +++++++++++++
16 include/linux/squashfs_fs.h | 935 +++++++++++++++++
17 include/linux/squashfs_fs_i.h | 45
18 include/linux/squashfs_fs_sb.h | 79 +
19 init/do_mounts_rd.c | 16
20 10 files changed, 4133 insertions(+)
21
22--- a/fs/Kconfig
23+++ b/fs/Kconfig
24@@ -1391,6 +1391,56 @@ config CRAMFS
25
26 If unsure, say N.
27
28+config SQUASHFS
29+ tristate "SquashFS 3.4 - Squashed file system support"
30+ select ZLIB_INFLATE
31+ help
32+ Saying Y here includes support for SquashFS 3.4 (a Compressed
33+ Read-Only File System). Squashfs is a highly compressed read-only
34+ filesystem for Linux. It uses zlib compression to compress both
35+ files, inodes and directories. Inodes in the system are very small
36+ and all blocks are packed to minimise data overhead. Block sizes
37+ greater than 4K are supported up to a maximum of 1 Mbytes (default
38+ block size 128K). SquashFS 3.3 supports 64 bit filesystems and files
39+ (larger than 4GB), full uid/gid information, hard links and timestamps.
40+
41+ Squashfs is intended for general read-only filesystem use, for
42+ archival use (i.e. in cases where a .tar.gz file may be used), and in
43+ embedded systems where low overhead is needed. Further information
44+ and filesystem tools are available from http://squashfs.sourceforge.net.
45+
46+ If you want to compile this as a module ( = code which can be
47+ inserted in and removed from the running kernel whenever you want),
48+ say M here and read <file:Documentation/modules.txt>. The module
49+ will be called squashfs. Note that the root file system (the one
50+ containing the directory /) cannot be compiled as a module.
51+
52+ If unsure, say N.
53+
54+config SQUASHFS_EMBEDDED
55+
56+ bool "Additional option for memory-constrained systems"
57+ depends on SQUASHFS
58+ default n
59+ help
60+ Saying Y here allows you to specify cache size.
61+
62+ If unsure, say N.
63+
64+config SQUASHFS_FRAGMENT_CACHE_SIZE
65+ int "Number of fragments cached" if SQUASHFS_EMBEDDED
66+ depends on SQUASHFS
67+ default "3"
68+ help
69+ By default SquashFS caches the last 3 fragments read from
70+ the filesystem. Increasing this amount may mean SquashFS
71+ has to re-read fragments less often from disk, at the expense
72+ of extra system memory. Decreasing this amount will mean
73+ SquashFS uses less memory at the expense of extra reads from disk.
74+
75+ Note there must be at least one cached fragment. Anything
76+ much more than three will probably not make much difference.
77+
78 config VXFS_FS
79 tristate "FreeVxFS file system support (VERITAS VxFS(TM) compatible)"
80 depends on BLOCK
81--- a/fs/Makefile
82+++ b/fs/Makefile
83@@ -77,6 +77,7 @@ obj-$(CONFIG_JBD) += jbd/
84 obj-$(CONFIG_JBD2) += jbd2/
85 obj-$(CONFIG_EXT2_FS) += ext2/
86 obj-$(CONFIG_CRAMFS) += cramfs/
87+obj-$(CONFIG_SQUASHFS) += squashfs/
88 obj-y += ramfs/
89 obj-$(CONFIG_HUGETLBFS) += hugetlbfs/
90 obj-$(CONFIG_CODA_FS) += coda/
91--- /dev/null
92+++ b/fs/squashfs/Makefile
93@@ -0,0 +1,7 @@
94+#
95+# Makefile for the linux squashfs routines.
96+#
97+
98+obj-$(CONFIG_SQUASHFS) += squashfs.o
99+squashfs-y += inode.o
100+squashfs-y += squashfs2_0.o
101--- /dev/null
102+++ b/fs/squashfs/inode.c
103@@ -0,0 +1,2174 @@
104+/*
105+ * Squashfs - a compressed read only filesystem for Linux
106+ *
107+ * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
108+ * Phillip Lougher <phillip@lougher.demon.co.uk>
109+ *
110+ * This program is free software; you can redistribute it and/or
111+ * modify it under the terms of the GNU General Public License
112+ * as published by the Free Software Foundation; either version 2,
113+ * or (at your option) any later version.
114+ *
115+ * This program is distributed in the hope that it will be useful,
116+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
117+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
118+ * GNU General Public License for more details.
119+ *
120+ * You should have received a copy of the GNU General Public License
121+ * along with this program; if not, write to the Free Software
122+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
123+ *
124+ * inode.c
125+ */
126+
127+#include <linux/squashfs_fs.h>
128+#include <linux/module.h>
129+#include <linux/zlib.h>
130+#include <linux/fs.h>
131+#include <linux/squashfs_fs_sb.h>
132+#include <linux/squashfs_fs_i.h>
133+#include <linux/buffer_head.h>
134+#include <linux/vfs.h>
135+#include <linux/vmalloc.h>
136+#include <linux/spinlock.h>
137+#include <linux/smp_lock.h>
138+#include <linux/exportfs.h>
139+#include <linux/sched.h>
140+
141+#include "squashfs.h"
142+
143+static struct dentry *squashfs_fh_to_dentry(struct super_block *s,
144+ struct fid *fid, int fh_len, int fh_type);
145+static struct dentry *squashfs_fh_to_parent(struct super_block *s,
146+ struct fid *fid, int fh_len, int fh_type);
147+static struct dentry *squashfs_get_parent(struct dentry *child);
148+static int squashfs_read_inode(struct inode *i, squashfs_inode_t inode);
149+static int squashfs_statfs(struct dentry *, struct kstatfs *);
150+static int squashfs_symlink_readpage(struct file *file, struct page *page);
151+static long long read_blocklist(struct inode *inode, int index,
152+ int readahead_blks, char *block_list,
153+ unsigned short **block_p, unsigned int *bsize);
154+static int squashfs_readpage(struct file *file, struct page *page);
155+static int squashfs_readdir(struct file *, void *, filldir_t);
156+static struct dentry *squashfs_lookup(struct inode *, struct dentry *,
157+ struct nameidata *);
158+static int squashfs_remount(struct super_block *s, int *flags, char *data);
159+static void squashfs_put_super(struct super_block *);
160+static int squashfs_get_sb(struct file_system_type *,int, const char *, void *,
161+ struct vfsmount *);
162+static struct inode *squashfs_alloc_inode(struct super_block *sb);
163+static void squashfs_destroy_inode(struct inode *inode);
164+static int init_inodecache(void);
165+static void destroy_inodecache(void);
166+
167+static struct file_system_type squashfs_fs_type = {
168+ .owner = THIS_MODULE,
169+ .name = "squashfs",
170+ .get_sb = squashfs_get_sb,
171+ .kill_sb = kill_block_super,
172+ .fs_flags = FS_REQUIRES_DEV
173+};
174+
175+static const unsigned char squashfs_filetype_table[] = {
176+ DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
177+};
178+
179+static struct super_operations squashfs_super_ops = {
180+ .alloc_inode = squashfs_alloc_inode,
181+ .destroy_inode = squashfs_destroy_inode,
182+ .statfs = squashfs_statfs,
183+ .put_super = squashfs_put_super,
184+ .remount_fs = squashfs_remount
185+};
186+
187+static struct export_operations squashfs_export_ops = {
188+ .fh_to_dentry = squashfs_fh_to_dentry,
189+ .fh_to_parent = squashfs_fh_to_parent,
190+ .get_parent = squashfs_get_parent
191+};
192+
193+SQSH_EXTERN const struct address_space_operations squashfs_symlink_aops = {
194+ .readpage = squashfs_symlink_readpage
195+};
196+
197+SQSH_EXTERN const struct address_space_operations squashfs_aops = {
198+ .readpage = squashfs_readpage
199+};
200+
201+static const struct file_operations squashfs_dir_ops = {
202+ .read = generic_read_dir,
203+ .readdir = squashfs_readdir
204+};
205+
206+SQSH_EXTERN struct inode_operations squashfs_dir_inode_ops = {
207+ .lookup = squashfs_lookup
208+};
209+
210+
211+static struct buffer_head *get_block_length(struct super_block *s,
212+ int *cur_index, int *offset, int *c_byte)
213+{
214+ struct squashfs_sb_info *msblk = s->s_fs_info;
215+ unsigned short temp;
216+ struct buffer_head *bh;
217+
218+ if (!(bh = sb_bread(s, *cur_index)))
219+ goto out;
220+
221+ if (msblk->devblksize - *offset == 1) {
222+ if (msblk->swap)
223+ ((unsigned char *) &temp)[1] = *((unsigned char *)
224+ (bh->b_data + *offset));
225+ else
226+ ((unsigned char *) &temp)[0] = *((unsigned char *)
227+ (bh->b_data + *offset));
228+ brelse(bh);
229+ if (!(bh = sb_bread(s, ++(*cur_index))))
230+ goto out;
231+ if (msblk->swap)
232+ ((unsigned char *) &temp)[0] = *((unsigned char *)
233+ bh->b_data);
234+ else
235+ ((unsigned char *) &temp)[1] = *((unsigned char *)
236+ bh->b_data);
237+ *c_byte = temp;
238+ *offset = 1;
239+ } else {
240+ if (msblk->swap) {
241+ ((unsigned char *) &temp)[1] = *((unsigned char *)
242+ (bh->b_data + *offset));
243+ ((unsigned char *) &temp)[0] = *((unsigned char *)
244+ (bh->b_data + *offset + 1));
245+ } else {
246+ ((unsigned char *) &temp)[0] = *((unsigned char *)
247+ (bh->b_data + *offset));
248+ ((unsigned char *) &temp)[1] = *((unsigned char *)
249+ (bh->b_data + *offset + 1));
250+ }
251+ *c_byte = temp;
252+ *offset += 2;
253+ }
254+
255+ if (SQUASHFS_CHECK_DATA(msblk->sblk.flags)) {
256+ if (*offset == msblk->devblksize) {
257+ brelse(bh);
258+ if (!(bh = sb_bread(s, ++(*cur_index))))
259+ goto out;
260+ *offset = 0;
261+ }
262+ if (*((unsigned char *) (bh->b_data + *offset)) !=
263+ SQUASHFS_MARKER_BYTE) {
264+ ERROR("Metadata block marker corrupt @ %x\n",
265+ *cur_index);
266+ brelse(bh);
267+ goto out;
268+ }
269+ (*offset)++;
270+ }
271+ return bh;
272+
273+out:
274+ return NULL;
275+}
276+
277+
278+SQSH_EXTERN unsigned int squashfs_read_data(struct super_block *s, char *buffer,
279+ long long index, unsigned int length,
280+ long long *next_index, int srclength)
281+{
282+ struct squashfs_sb_info *msblk = s->s_fs_info;
283+ struct squashfs_super_block *sblk = &msblk->sblk;
284+ struct buffer_head **bh;
285+ unsigned int offset = index & ((1 << msblk->devblksize_log2) - 1);
286+ unsigned int cur_index = index >> msblk->devblksize_log2;
287+ int bytes, avail_bytes, b = 0, k = 0;
288+ unsigned int compressed;
289+ unsigned int c_byte = length;
290+
291+ bh = kmalloc(((sblk->block_size >> msblk->devblksize_log2) + 1) *
292+ sizeof(struct buffer_head *), GFP_KERNEL);
293+ if (bh == NULL)
294+ goto read_failure;
295+
296+ if (c_byte) {
297+ bytes = -offset;
298+ compressed = SQUASHFS_COMPRESSED_BLOCK(c_byte);
299+ c_byte = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
300+
301+ TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n", index,
302+ compressed ? "" : "un", (unsigned int) c_byte, srclength);
303+
304+ if (c_byte > srclength || index < 0 || (index + c_byte) > sblk->bytes_used)
305+ goto read_failure;
306+
307+ for (b = 0; bytes < (int) c_byte; b++, cur_index++) {
308+ bh[b] = sb_getblk(s, cur_index);
309+ if (bh[b] == NULL)
310+ goto block_release;
311+ bytes += msblk->devblksize;
312+ }
313+ ll_rw_block(READ, b, bh);
314+ } else {
315+ if (index < 0 || (index + 2) > sblk->bytes_used)
316+ goto read_failure;
317+
318+ bh[0] = get_block_length(s, &cur_index, &offset, &c_byte);
319+ if (bh[0] == NULL)
320+ goto read_failure;
321+ b = 1;
322+
323+ bytes = msblk->devblksize - offset;
324+ compressed = SQUASHFS_COMPRESSED(c_byte);
325+ c_byte = SQUASHFS_COMPRESSED_SIZE(c_byte);
326+
327+ TRACE("Block @ 0x%llx, %scompressed size %d\n", index, compressed
328+ ? "" : "un", (unsigned int) c_byte);
329+
330+ if (c_byte > srclength || (index + c_byte) > sblk->bytes_used)
331+ goto block_release;
332+
333+ for (; bytes < c_byte; b++) {
334+ bh[b] = sb_getblk(s, ++cur_index);
335+ if (bh[b] == NULL)
336+ goto block_release;
337+ bytes += msblk->devblksize;
338+ }
339+ ll_rw_block(READ, b - 1, bh + 1);
340+ }
341+
342+ if (compressed) {
343+ int zlib_err = 0;
344+
345+ /*
346+ * uncompress block
347+ */
348+
349+ mutex_lock(&msblk->read_data_mutex);
350+
351+ msblk->stream.next_out = buffer;
352+ msblk->stream.avail_out = srclength;
353+
354+ for (bytes = 0; k < b; k++) {
355+ avail_bytes = min(c_byte - bytes, msblk->devblksize - offset);
356+
357+ wait_on_buffer(bh[k]);
358+ if (!buffer_uptodate(bh[k]))
359+ goto release_mutex;
360+
361+ msblk->stream.next_in = bh[k]->b_data + offset;
362+ msblk->stream.avail_in = avail_bytes;
363+
364+ if (k == 0) {
365+ zlib_err = zlib_inflateInit(&msblk->stream);
366+ if (zlib_err != Z_OK) {
367+ ERROR("zlib_inflateInit returned unexpected result 0x%x,"
368+ " srclength %d\n", zlib_err, srclength);
369+ goto release_mutex;
370+ }
371+
372+ if (avail_bytes == 0) {
373+ offset = 0;
374+ brelse(bh[k]);
375+ continue;
376+ }
377+ }
378+
379+ zlib_err = zlib_inflate(&msblk->stream, Z_NO_FLUSH);
380+ if (zlib_err != Z_OK && zlib_err != Z_STREAM_END) {
381+ ERROR("zlib_inflate returned unexpected result 0x%x,"
382+ " srclength %d, avail_in %d, avail_out %d\n", zlib_err,
383+ srclength, msblk->stream.avail_in, msblk->stream.avail_out);
384+ goto release_mutex;
385+ }
386+
387+ bytes += avail_bytes;
388+ offset = 0;
389+ brelse(bh[k]);
390+ }
391+
392+ if (zlib_err != Z_STREAM_END)
393+ goto release_mutex;
394+
395+ zlib_err = zlib_inflateEnd(&msblk->stream);
396+ if (zlib_err != Z_OK) {
397+ ERROR("zlib_inflateEnd returned unexpected result 0x%x,"
398+ " srclength %d\n", zlib_err, srclength);
399+ goto release_mutex;
400+ }
401+ bytes = msblk->stream.total_out;
402+ mutex_unlock(&msblk->read_data_mutex);
403+ } else {
404+ int i;
405+
406+ for(i = 0; i < b; i++) {
407+ wait_on_buffer(bh[i]);
408+ if (!buffer_uptodate(bh[i]))
409+ goto block_release;
410+ }
411+
412+ for (bytes = 0; k < b; k++) {
413+ avail_bytes = min(c_byte - bytes, msblk->devblksize - offset);
414+
415+ memcpy(buffer + bytes, bh[k]->b_data + offset, avail_bytes);
416+ bytes += avail_bytes;
417+ offset = 0;
418+ brelse(bh[k]);
419+ }
420+ }
421+
422+ if (next_index)
423+ *next_index = index + c_byte + (length ? 0 :
424+ (SQUASHFS_CHECK_DATA(msblk->sblk.flags) ? 3 : 2));
425+
426+ kfree(bh);
427+ return bytes;
428+
429+release_mutex:
430+ mutex_unlock(&msblk->read_data_mutex);
431+
432+block_release:
433+ for (; k < b; k++)
434+ brelse(bh[k]);
435+
436+read_failure:
437+ ERROR("sb_bread failed reading block 0x%x\n", cur_index);
438+ kfree(bh);
439+ return 0;
440+}
441+
442+
443+static struct squashfs_cache_entry *squashfs_cache_get(struct super_block *s,
444+ struct squashfs_cache *cache, long long block, int length)
445+{
446+ int i, n;
447+ struct squashfs_cache_entry *entry;
448+
449+ spin_lock(&cache->lock);
450+
451+ while (1) {
452+ for (i = 0; i < cache->entries && cache->entry[i].block != block; i++);
453+
454+ if (i == cache->entries) {
455+ if (cache->unused_blks == 0) {
456+ cache->waiting ++;
457+ spin_unlock(&cache->lock);
458+ wait_event(cache->wait_queue, cache->unused_blks);
459+ spin_lock(&cache->lock);
460+ cache->waiting --;
461+ continue;
462+ }
463+
464+ i = cache->next_blk;
465+ for (n = 0; n < cache->entries; n++) {
466+ if (cache->entry[i].locked == 0)
467+ break;
468+ i = (i + 1) % cache->entries;
469+ }
470+
471+ cache->next_blk = (i + 1) % cache->entries;
472+ entry = &cache->entry[i];
473+
474+ cache->unused_blks --;
475+ entry->block = block;
476+ entry->locked = 1;
477+ entry->pending = 1;
478+ entry->waiting = 0;
479+ entry->error = 0;
480+ spin_unlock(&cache->lock);
481+
482+ entry->length = squashfs_read_data(s, entry->data,
483+ block, length, &entry->next_index, cache->block_size);
484+
485+ spin_lock(&cache->lock);
486+
487+ if (entry->length == 0)
488+ entry->error = 1;
489+
490+ entry->pending = 0;
491+ spin_unlock(&cache->lock);
492+ if (entry->waiting)
493+ wake_up_all(&entry->wait_queue);
494+ goto out;
495+ }
496+
497+ entry = &cache->entry[i];
498+ if (entry->locked == 0)
499+ cache->unused_blks --;
500+ entry->locked++;
501+
502+ if (entry->pending) {
503+ entry->waiting ++;
504+ spin_unlock(&cache->lock);
505+ wait_event(entry->wait_queue, !entry->pending);
506+ goto out;
507+ }
508+
509+ spin_unlock(&cache->lock);
510+ goto out;
511+ }
512+
513+out:
514+ TRACE("Got %s %d, start block %lld, locked %d, error %d\n", i,
515+ cache->name, entry->block, entry->locked, entry->error);
516+ if (entry->error)
517+ ERROR("Unable to read %s cache entry [%llx]\n", cache->name, block);
518+ return entry;
519+}
520+
521+
522+static void squashfs_cache_put(struct squashfs_cache *cache,
523+ struct squashfs_cache_entry *entry)
524+{
525+ spin_lock(&cache->lock);
526+ entry->locked --;
527+ if (entry->locked == 0) {
528+ cache->unused_blks ++;
529+ spin_unlock(&cache->lock);
530+ if (cache->waiting)
531+ wake_up(&cache->wait_queue);
532+ } else
533+ spin_unlock(&cache->lock);
534+}
535+
536+
537+static void squashfs_cache_delete(struct squashfs_cache *cache)
538+{
539+ int i;
540+
541+ if (cache == NULL)
542+ return;
543+
544+ for (i = 0; i < cache->entries; i++)
545+ if (cache->entry[i].data) {
546+ if (cache->use_vmalloc)
547+ vfree(cache->entry[i].data);
548+ else
549+ kfree(cache->entry[i].data);
550+ }
551+
552+ kfree(cache);
553+}
554+
555+
556+static struct squashfs_cache *squashfs_cache_init(char *name, int entries,
557+ int block_size, int use_vmalloc)
558+{
559+ int i;
560+ struct squashfs_cache *cache = kzalloc(sizeof(struct squashfs_cache) +
561+ entries * sizeof(struct squashfs_cache_entry), GFP_KERNEL);
562+ if (cache == NULL) {
563+ ERROR("Failed to allocate %s cache\n", name);
564+ goto failed;
565+ }
566+
567+ cache->next_blk = 0;
568+ cache->unused_blks = entries;
569+ cache->entries = entries;
570+ cache->block_size = block_size;
571+ cache->use_vmalloc = use_vmalloc;
572+ cache->name = name;
573+ cache->waiting = 0;
574+ spin_lock_init(&cache->lock);
575+ init_waitqueue_head(&cache->wait_queue);
576+
577+ for (i = 0; i < entries; i++) {
578+ init_waitqueue_head(&cache->entry[i].wait_queue);
579+ cache->entry[i].block = SQUASHFS_INVALID_BLK;
580+ cache->entry[i].data = use_vmalloc ? vmalloc(block_size) :
581+ kmalloc(block_size, GFP_KERNEL);
582+ if (cache->entry[i].data == NULL) {
583+ ERROR("Failed to allocate %s cache entry\n", name);
584+ goto cleanup;
585+ }
586+ }
587+
588+ return cache;
589+
590+cleanup:
591+ squashfs_cache_delete(cache);
592+failed:
593+ return NULL;
594+}
595+
596+
597+SQSH_EXTERN int squashfs_get_cached_block(struct super_block *s, void *buffer,
598+ long long block, unsigned int offset,
599+ int length, long long *next_block,
600+ unsigned int *next_offset)
601+{
602+ struct squashfs_sb_info *msblk = s->s_fs_info;
603+ int bytes, return_length = length;
604+ struct squashfs_cache_entry *entry;
605+
606+ TRACE("Entered squashfs_get_cached_block [%llx:%x]\n", block, offset);
607+
608+ while (1) {
609+ entry = squashfs_cache_get(s, msblk->block_cache, block, 0);
610+ bytes = entry->length - offset;
611+
612+ if (entry->error || bytes < 1) {
613+ return_length = 0;
614+ goto finish;
615+ } else if (bytes >= length) {
616+ if (buffer)
617+ memcpy(buffer, entry->data + offset, length);
618+ if (entry->length - offset == length) {
619+ *next_block = entry->next_index;
620+ *next_offset = 0;
621+ } else {
622+ *next_block = block;
623+ *next_offset = offset + length;
624+ }
625+ goto finish;
626+ } else {
627+ if (buffer) {
628+ memcpy(buffer, entry->data + offset, bytes);
629+ buffer = (char *) buffer + bytes;
630+ }
631+ block = entry->next_index;
632+ squashfs_cache_put(msblk->block_cache, entry);
633+ length -= bytes;
634+ offset = 0;
635+ }
636+ }
637+
638+finish:
639+ squashfs_cache_put(msblk->block_cache, entry);
640+ return return_length;
641+}
642+
643+
644+static int get_fragment_location(struct super_block *s, unsigned int fragment,
645+ long long *fragment_start_block,
646+ unsigned int *fragment_size)
647+{
648+ struct squashfs_sb_info *msblk = s->s_fs_info;
649+ long long start_block =
650+ msblk->fragment_index[SQUASHFS_FRAGMENT_INDEX(fragment)];
651+ int offset = SQUASHFS_FRAGMENT_INDEX_OFFSET(fragment);
652+ struct squashfs_fragment_entry fragment_entry;
653+
654+ if (msblk->swap) {
655+ struct squashfs_fragment_entry sfragment_entry;
656+
657+ if (!squashfs_get_cached_block(s, &sfragment_entry, start_block, offset,
658+ sizeof(sfragment_entry), &start_block, &offset))
659+ goto out;
660+ SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_entry, &sfragment_entry);
661+ } else
662+ if (!squashfs_get_cached_block(s, &fragment_entry, start_block, offset,
663+ sizeof(fragment_entry), &start_block, &offset))
664+ goto out;
665+
666+ *fragment_start_block = fragment_entry.start_block;
667+ *fragment_size = fragment_entry.size;
668+
669+ return 1;
670+
671+out:
672+ return 0;
673+}
674+
675+
676+SQSH_EXTERN void release_cached_fragment(struct squashfs_sb_info *msblk,
677+ struct squashfs_cache_entry *fragment)
678+{
679+ squashfs_cache_put(msblk->fragment_cache, fragment);
680+}
681+
682+
683+SQSH_EXTERN
684+struct squashfs_cache_entry *get_cached_fragment(struct super_block *s,
685+ long long start_block, int length)
686+{
687+ struct squashfs_sb_info *msblk = s->s_fs_info;
688+
689+ return squashfs_cache_get(s, msblk->fragment_cache, start_block, length);
690+}
691+
692+
693+static void squashfs_new_inode(struct squashfs_sb_info *msblk, struct inode *i,
694+ struct squashfs_base_inode_header *inodeb)
695+{
696+ i->i_ino = inodeb->inode_number;
697+ i->i_mtime.tv_sec = inodeb->mtime;
698+ i->i_atime.tv_sec = inodeb->mtime;
699+ i->i_ctime.tv_sec = inodeb->mtime;
700+ i->i_uid = msblk->uid[inodeb->uid];
701+ i->i_mode = inodeb->mode;
702+ i->i_size = 0;
703+
704+ if (inodeb->guid == SQUASHFS_GUIDS)
705+ i->i_gid = i->i_uid;
706+ else
707+ i->i_gid = msblk->guid[inodeb->guid];
708+}
709+
710+
711+static squashfs_inode_t squashfs_inode_lookup(struct super_block *s, int ino)
712+{
713+ struct squashfs_sb_info *msblk = s->s_fs_info;
714+ long long start = msblk->inode_lookup_table[SQUASHFS_LOOKUP_BLOCK(ino - 1)];
715+ int offset = SQUASHFS_LOOKUP_BLOCK_OFFSET(ino - 1);
716+ squashfs_inode_t inode;
717+
718+ TRACE("Entered squashfs_inode_lookup, inode_number = %d\n", ino);
719+
720+ if (msblk->swap) {
721+ squashfs_inode_t sinode;
722+
723+ if (!squashfs_get_cached_block(s, &sinode, start, offset,
724+ sizeof(sinode), &start, &offset))
725+ goto out;
726+ SQUASHFS_SWAP_INODE_T((&inode), &sinode);
727+ } else if (!squashfs_get_cached_block(s, &inode, start, offset,
728+ sizeof(inode), &start, &offset))
729+ goto out;
730+
731+ TRACE("squashfs_inode_lookup, inode = 0x%llx\n", inode);
732+
733+ return inode;
734+
735+out:
736+ return SQUASHFS_INVALID_BLK;
737+}
738+
739+
740+
741+static struct dentry *squashfs_export_iget(struct super_block *s,
742+ unsigned int inode_number)
743+{
744+ squashfs_inode_t inode;
745+ struct inode *i;
746+ struct dentry *dentry;
747+
748+ TRACE("Entered squashfs_export_iget\n");
749+
750+ inode = squashfs_inode_lookup(s, inode_number);
751+ if(inode == SQUASHFS_INVALID_BLK) {
752+ dentry = ERR_PTR(-ENOENT);
753+ goto failure;
754+ }
755+
756+ i = squashfs_iget(s, inode, inode_number);
757+ if(i == NULL) {
758+ dentry = ERR_PTR(-EACCES);
759+ goto failure;
760+ }
761+
762+ dentry = d_alloc_anon(i);
763+ if (dentry == NULL) {
764+ iput(i);
765+ dentry = ERR_PTR(-ENOMEM);
766+ }
767+
768+failure:
769+ return dentry;
770+}
771+
772+
773+static struct dentry *squashfs_fh_to_dentry(struct super_block *s,
774+ struct fid *fid, int fh_len, int fh_type)
775+{
776+ if((fh_type != FILEID_INO32_GEN && fh_type != FILEID_INO32_GEN_PARENT) ||
777+ fh_len < 2)
778+ return NULL;
779+
780+ return squashfs_export_iget(s, fid->i32.ino);
781+}
782+
783+
784+static struct dentry *squashfs_fh_to_parent(struct super_block *s,
785+ struct fid *fid, int fh_len, int fh_type)
786+{
787+ if(fh_type != FILEID_INO32_GEN_PARENT || fh_len < 4)
788+ return NULL;
789+
790+ return squashfs_export_iget(s, fid->i32.parent_ino);
791+}
792+
793+
794+static struct dentry *squashfs_get_parent(struct dentry *child)
795+{
796+ struct inode *i = child->d_inode;
797+
798+ TRACE("Entered squashfs_get_parent\n");
799+
800+ return squashfs_export_iget(i->i_sb, SQUASHFS_I(i)->u.s2.parent_inode);
801+}
802+
803+
804+SQSH_EXTERN struct inode *squashfs_iget(struct super_block *s,
805+ squashfs_inode_t inode, unsigned int inode_number)
806+{
807+ struct squashfs_sb_info *msblk = s->s_fs_info;
808+ struct inode *i = iget_locked(s, inode_number);
809+
810+ TRACE("Entered squashfs_iget\n");
811+
812+ if(i && (i->i_state & I_NEW)) {
813+ (msblk->read_inode)(i, inode);
814+ unlock_new_inode(i);
815+ }
816+
817+ return i;
818+}
819+
820+
821+static int squashfs_read_inode(struct inode *i, squashfs_inode_t inode)
822+{
823+ struct super_block *s = i->i_sb;
824+ struct squashfs_sb_info *msblk = s->s_fs_info;
825+ struct squashfs_super_block *sblk = &msblk->sblk;
826+ long long block = SQUASHFS_INODE_BLK(inode) + sblk->inode_table_start;
827+ unsigned int offset = SQUASHFS_INODE_OFFSET(inode);
828+ long long next_block;
829+ unsigned int next_offset;
830+ union squashfs_inode_header id, sid;
831+ struct squashfs_base_inode_header *inodeb = &id.base, *sinodeb = &sid.base;
832+
833+ TRACE("Entered squashfs_read_inode\n");
834+
835+ if (msblk->swap) {
836+ if (!squashfs_get_cached_block(s, sinodeb, block, offset,
837+ sizeof(*sinodeb), &next_block, &next_offset))
838+ goto failed_read;
839+ SQUASHFS_SWAP_BASE_INODE_HEADER(inodeb, sinodeb, sizeof(*sinodeb));
840+ } else
841+ if (!squashfs_get_cached_block(s, inodeb, block, offset,
842+ sizeof(*inodeb), &next_block, &next_offset))
843+ goto failed_read;
844+
845+ squashfs_new_inode(msblk, i, inodeb);
846+
847+ switch(inodeb->inode_type) {
848+ case SQUASHFS_FILE_TYPE: {
849+ unsigned int frag_size;
850+ long long frag_blk;
851+ struct squashfs_reg_inode_header *inodep = &id.reg;
852+ struct squashfs_reg_inode_header *sinodep = &sid.reg;
853+
854+ if (msblk->swap) {
855+ if (!squashfs_get_cached_block(s, sinodep, block, offset,
856+ sizeof(*sinodep), &next_block, &next_offset))
857+ goto failed_read;
858+ SQUASHFS_SWAP_REG_INODE_HEADER(inodep, sinodep);
859+ } else
860+ if (!squashfs_get_cached_block(s, inodep, block, offset,
861+ sizeof(*inodep), &next_block, &next_offset))
862+ goto failed_read;
863+
864+ frag_blk = SQUASHFS_INVALID_BLK;
865+
866+ if (inodep->fragment != SQUASHFS_INVALID_FRAG)
867+ if(!get_fragment_location(s, inodep->fragment, &frag_blk,
868+ &frag_size))
869+ goto failed_read;
870+
871+ i->i_nlink = 1;
872+ i->i_size = inodep->file_size;
873+ i->i_fop = &generic_ro_fops;
874+ i->i_mode |= S_IFREG;
875+ i->i_blocks = ((i->i_size - 1) >> 9) + 1;
876+ SQUASHFS_I(i)->u.s1.fragment_start_block = frag_blk;
877+ SQUASHFS_I(i)->u.s1.fragment_size = frag_size;
878+ SQUASHFS_I(i)->u.s1.fragment_offset = inodep->offset;
879+ SQUASHFS_I(i)->start_block = inodep->start_block;
880+ SQUASHFS_I(i)->u.s1.block_list_start = next_block;
881+ SQUASHFS_I(i)->offset = next_offset;
882+ i->i_data.a_ops = &squashfs_aops;
883+
884+ TRACE("File inode %x:%x, start_block %llx, "
885+ "block_list_start %llx, offset %x\n",
886+ SQUASHFS_INODE_BLK(inode), offset,
887+ inodep->start_block, next_block,
888+ next_offset);
889+ break;
890+ }
891+ case SQUASHFS_LREG_TYPE: {
892+ unsigned int frag_size;
893+ long long frag_blk;
894+ struct squashfs_lreg_inode_header *inodep = &id.lreg;
895+ struct squashfs_lreg_inode_header *sinodep = &sid.lreg;
896+
897+ if (msblk->swap) {
898+ if (!squashfs_get_cached_block(s, sinodep, block, offset,
899+ sizeof(*sinodep), &next_block, &next_offset))
900+ goto failed_read;
901+ SQUASHFS_SWAP_LREG_INODE_HEADER(inodep, sinodep);
902+ } else
903+ if (!squashfs_get_cached_block(s, inodep, block, offset,
904+ sizeof(*inodep), &next_block, &next_offset))
905+ goto failed_read;
906+
907+ frag_blk = SQUASHFS_INVALID_BLK;
908+
909+ if (inodep->fragment != SQUASHFS_INVALID_FRAG)
910+ if (!get_fragment_location(s, inodep->fragment, &frag_blk,
911+ &frag_size))
912+ goto failed_read;
913+
914+ i->i_nlink = inodep->nlink;
915+ i->i_size = inodep->file_size;
916+ i->i_fop = &generic_ro_fops;
917+ i->i_mode |= S_IFREG;
918+ i->i_blocks = ((i->i_size - 1) >> 9) + 1;
919+ SQUASHFS_I(i)->u.s1.fragment_start_block = frag_blk;
920+ SQUASHFS_I(i)->u.s1.fragment_size = frag_size;
921+ SQUASHFS_I(i)->u.s1.fragment_offset = inodep->offset;
922+ SQUASHFS_I(i)->start_block = inodep->start_block;
923+ SQUASHFS_I(i)->u.s1.block_list_start = next_block;
924+ SQUASHFS_I(i)->offset = next_offset;
925+ i->i_data.a_ops = &squashfs_aops;
926+
927+ TRACE("File inode %x:%x, start_block %llx, "
928+ "block_list_start %llx, offset %x\n",
929+ SQUASHFS_INODE_BLK(inode), offset,
930+ inodep->start_block, next_block,
931+ next_offset);
932+ break;
933+ }
934+ case SQUASHFS_DIR_TYPE: {
935+ struct squashfs_dir_inode_header *inodep = &id.dir;
936+ struct squashfs_dir_inode_header *sinodep = &sid.dir;
937+
938+ if (msblk->swap) {
939+ if (!squashfs_get_cached_block(s, sinodep, block, offset,
940+ sizeof(*sinodep), &next_block, &next_offset))
941+ goto failed_read;
942+ SQUASHFS_SWAP_DIR_INODE_HEADER(inodep, sinodep);
943+ } else
944+ if (!squashfs_get_cached_block(s, inodep, block, offset,
945+ sizeof(*inodep), &next_block, &next_offset))
946+ goto failed_read;
947+
948+ i->i_nlink = inodep->nlink;
949+ i->i_size = inodep->file_size;
950+ i->i_op = &squashfs_dir_inode_ops;
951+ i->i_fop = &squashfs_dir_ops;
952+ i->i_mode |= S_IFDIR;
953+ SQUASHFS_I(i)->start_block = inodep->start_block;
954+ SQUASHFS_I(i)->offset = inodep->offset;
955+ SQUASHFS_I(i)->u.s2.directory_index_count = 0;
956+ SQUASHFS_I(i)->u.s2.parent_inode = inodep->parent_inode;
957+
958+ TRACE("Directory inode %x:%x, start_block %x, offset "
959+ "%x\n", SQUASHFS_INODE_BLK(inode),
960+ offset, inodep->start_block,
961+ inodep->offset);
962+ break;
963+ }
964+ case SQUASHFS_LDIR_TYPE: {
965+ struct squashfs_ldir_inode_header *inodep = &id.ldir;
966+ struct squashfs_ldir_inode_header *sinodep = &sid.ldir;
967+
968+ if (msblk->swap) {
969+ if (!squashfs_get_cached_block(s, sinodep, block, offset,
970+ sizeof(*sinodep), &next_block, &next_offset))
971+ goto failed_read;
972+ SQUASHFS_SWAP_LDIR_INODE_HEADER(inodep, sinodep);
973+ } else
974+ if (!squashfs_get_cached_block(s, inodep, block, offset,
975+ sizeof(*inodep), &next_block, &next_offset))
976+ goto failed_read;
977+
978+ i->i_nlink = inodep->nlink;
979+ i->i_size = inodep->file_size;
980+ i->i_op = &squashfs_dir_inode_ops;
981+ i->i_fop = &squashfs_dir_ops;
982+ i->i_mode |= S_IFDIR;
983+ SQUASHFS_I(i)->start_block = inodep->start_block;
984+ SQUASHFS_I(i)->offset = inodep->offset;
985+ SQUASHFS_I(i)->u.s2.directory_index_start = next_block;
986+ SQUASHFS_I(i)->u.s2.directory_index_offset = next_offset;
987+ SQUASHFS_I(i)->u.s2.directory_index_count = inodep->i_count;
988+ SQUASHFS_I(i)->u.s2.parent_inode = inodep->parent_inode;
989+
990+ TRACE("Long directory inode %x:%x, start_block %x, offset %x\n",
991+ SQUASHFS_INODE_BLK(inode), offset,
992+ inodep->start_block, inodep->offset);
993+ break;
994+ }
995+ case SQUASHFS_SYMLINK_TYPE: {
996+ struct squashfs_symlink_inode_header *inodep = &id.symlink;
997+ struct squashfs_symlink_inode_header *sinodep = &sid.symlink;
998+
999+ if (msblk->swap) {
1000+ if (!squashfs_get_cached_block(s, sinodep, block, offset,
1001+ sizeof(*sinodep), &next_block, &next_offset))
1002+ goto failed_read;
1003+ SQUASHFS_SWAP_SYMLINK_INODE_HEADER(inodep, sinodep);
1004+ } else
1005+ if (!squashfs_get_cached_block(s, inodep, block, offset,
1006+ sizeof(*inodep), &next_block, &next_offset))
1007+ goto failed_read;
1008+
1009+ i->i_nlink = inodep->nlink;
1010+ i->i_size = inodep->symlink_size;
1011+ i->i_op = &page_symlink_inode_operations;
1012+ i->i_data.a_ops = &squashfs_symlink_aops;
1013+ i->i_mode |= S_IFLNK;
1014+ SQUASHFS_I(i)->start_block = next_block;
1015+ SQUASHFS_I(i)->offset = next_offset;
1016+
1017+ TRACE("Symbolic link inode %x:%x, start_block %llx, offset %x\n",
1018+ SQUASHFS_INODE_BLK(inode), offset,
1019+ next_block, next_offset);
1020+ break;
1021+ }
1022+ case SQUASHFS_BLKDEV_TYPE:
1023+ case SQUASHFS_CHRDEV_TYPE: {
1024+ struct squashfs_dev_inode_header *inodep = &id.dev;
1025+ struct squashfs_dev_inode_header *sinodep = &sid.dev;
1026+
1027+ if (msblk->swap) {
1028+ if (!squashfs_get_cached_block(s, sinodep, block, offset,
1029+ sizeof(*sinodep), &next_block, &next_offset))
1030+ goto failed_read;
1031+ SQUASHFS_SWAP_DEV_INODE_HEADER(inodep, sinodep);
1032+ } else
1033+ if (!squashfs_get_cached_block(s, inodep, block, offset,
1034+ sizeof(*inodep), &next_block, &next_offset))
1035+ goto failed_read;
1036+
1037+ i->i_nlink = inodep->nlink;
1038+ i->i_mode |= (inodeb->inode_type == SQUASHFS_CHRDEV_TYPE) ?
1039+ S_IFCHR : S_IFBLK;
1040+ init_special_inode(i, i->i_mode, old_decode_dev(inodep->rdev));
1041+
1042+ TRACE("Device inode %x:%x, rdev %x\n",
1043+ SQUASHFS_INODE_BLK(inode), offset, inodep->rdev);
1044+ break;
1045+ }
1046+ case SQUASHFS_FIFO_TYPE:
1047+ case SQUASHFS_SOCKET_TYPE: {
1048+ struct squashfs_ipc_inode_header *inodep = &id.ipc;
1049+ struct squashfs_ipc_inode_header *sinodep = &sid.ipc;
1050+
1051+ if (msblk->swap) {
1052+ if (!squashfs_get_cached_block(s, sinodep, block, offset,
1053+ sizeof(*sinodep), &next_block, &next_offset))
1054+ goto failed_read;
1055+ SQUASHFS_SWAP_IPC_INODE_HEADER(inodep, sinodep);
1056+ } else
1057+ if (!squashfs_get_cached_block(s, inodep, block, offset,
1058+ sizeof(*inodep), &next_block, &next_offset))
1059+ goto failed_read;
1060+
1061+ i->i_nlink = inodep->nlink;
1062+ i->i_mode |= (inodeb->inode_type == SQUASHFS_FIFO_TYPE)
1063+ ? S_IFIFO : S_IFSOCK;
1064+ init_special_inode(i, i->i_mode, 0);
1065+ break;
1066+ }
1067+ default:
1068+ ERROR("Unknown inode type %d in squashfs_iget!\n",
1069+ inodeb->inode_type);
1070+ goto failed_read1;
1071+ }
1072+
1073+ return 1;
1074+
1075+failed_read:
1076+ ERROR("Unable to read inode [%llx:%x]\n", block, offset);
1077+
1078+failed_read1:
1079+ make_bad_inode(i);
1080+ return 0;
1081+}
1082+
1083+
1084+static int read_inode_lookup_table(struct super_block *s)
1085+{
1086+ struct squashfs_sb_info *msblk = s->s_fs_info;
1087+ struct squashfs_super_block *sblk = &msblk->sblk;
1088+ unsigned int length = SQUASHFS_LOOKUP_BLOCK_BYTES(sblk->inodes);
1089+
1090+ TRACE("In read_inode_lookup_table, length %d\n", length);
1091+
1092+ /* Allocate inode lookup table */
1093+ msblk->inode_lookup_table = kmalloc(length, GFP_KERNEL);
1094+ if (msblk->inode_lookup_table == NULL) {
1095+ ERROR("Failed to allocate inode lookup table\n");
1096+ return 0;
1097+ }
1098+
1099+ if (!squashfs_read_data(s, (char *) msblk->inode_lookup_table,
1100+ sblk->lookup_table_start, length |
1101+ SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, length)) {
1102+ ERROR("unable to read inode lookup table\n");
1103+ return 0;
1104+ }
1105+
1106+ if (msblk->swap) {
1107+ int i;
1108+ long long block;
1109+
1110+ for (i = 0; i < SQUASHFS_LOOKUP_BLOCKS(sblk->inodes); i++) {
1111+ /* XXX */
1112+ SQUASHFS_SWAP_LOOKUP_BLOCKS((&block),
1113+ &msblk->inode_lookup_table[i], 1);
1114+ msblk->inode_lookup_table[i] = block;
1115+ }
1116+ }
1117+
1118+ return 1;
1119+}
1120+
1121+
1122+static int read_fragment_index_table(struct super_block *s)
1123+{
1124+ struct squashfs_sb_info *msblk = s->s_fs_info;
1125+ struct squashfs_super_block *sblk = &msblk->sblk;
1126+ unsigned int length = SQUASHFS_FRAGMENT_INDEX_BYTES(sblk->fragments);
1127+
1128+ if(length == 0)
1129+ return 1;
1130+
1131+ /* Allocate fragment index table */
1132+ msblk->fragment_index = kmalloc(length, GFP_KERNEL);
1133+ if (msblk->fragment_index == NULL) {
1134+ ERROR("Failed to allocate fragment index table\n");
1135+ return 0;
1136+ }
1137+
1138+ if (!squashfs_read_data(s, (char *) msblk->fragment_index,
1139+ sblk->fragment_table_start, length |
1140+ SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, length)) {
1141+ ERROR("unable to read fragment index table\n");
1142+ return 0;
1143+ }
1144+
1145+ if (msblk->swap) {
1146+ int i;
1147+ long long fragment;
1148+
1149+ for (i = 0; i < SQUASHFS_FRAGMENT_INDEXES(sblk->fragments); i++) {
1150+ /* XXX */
1151+ SQUASHFS_SWAP_FRAGMENT_INDEXES((&fragment),
1152+ &msblk->fragment_index[i], 1);
1153+ msblk->fragment_index[i] = fragment;
1154+ }
1155+ }
1156+
1157+ return 1;
1158+}
1159+
1160+
1161+static int supported_squashfs_filesystem(struct squashfs_sb_info *msblk, int silent)
1162+{
1163+ struct squashfs_super_block *sblk = &msblk->sblk;
1164+
1165+ msblk->read_inode = squashfs_read_inode;
1166+ msblk->read_blocklist = read_blocklist;
1167+ msblk->read_fragment_index_table = read_fragment_index_table;
1168+
1169+ if (sblk->s_major == 1) {
1170+ if (!squashfs_1_0_supported(msblk)) {
1171+ SERROR("Major/Minor mismatch, Squashfs 1.0 filesystems "
1172+ "are unsupported\n");
1173+ SERROR("Please recompile with Squashfs 1.0 support enabled\n");
1174+ return 0;
1175+ }
1176+ } else if (sblk->s_major == 2) {
1177+ if (!squashfs_2_0_supported(msblk)) {
1178+ SERROR("Major/Minor mismatch, Squashfs 2.0 filesystems "
1179+ "are unsupported\n");
1180+ SERROR("Please recompile with Squashfs 2.0 support enabled\n");
1181+ return 0;
1182+ }
1183+ } else if(sblk->s_major != SQUASHFS_MAJOR || sblk->s_minor >
1184+ SQUASHFS_MINOR) {
1185+ SERROR("Major/Minor mismatch, trying to mount newer %d.%d "
1186+ "filesystem\n", sblk->s_major, sblk->s_minor);
1187+ SERROR("Please update your kernel\n");
1188+ return 0;
1189+ }
1190+
1191+ return 1;
1192+}
1193+
1194+
1195+static int squashfs_fill_super(struct super_block *s, void *data, int silent)
1196+{
1197+ struct squashfs_sb_info *msblk;
1198+ struct squashfs_super_block *sblk;
1199+ char b[BDEVNAME_SIZE];
1200+ struct inode *root;
1201+
1202+ TRACE("Entered squashfs_fill_superblock\n");
1203+
1204+ s->s_fs_info = kzalloc(sizeof(struct squashfs_sb_info), GFP_KERNEL);
1205+ if (s->s_fs_info == NULL) {
1206+ ERROR("Failed to allocate superblock\n");
1207+ goto failure;
1208+ }
1209+ msblk = s->s_fs_info;
1210+
1211+ msblk->stream.workspace = vmalloc(zlib_inflate_workspacesize());
1212+ if (msblk->stream.workspace == NULL) {
1213+ ERROR("Failed to allocate zlib workspace\n");
1214+ goto failure;
1215+ }
1216+ sblk = &msblk->sblk;
1217+
1218+ msblk->devblksize = sb_min_blocksize(s, BLOCK_SIZE);
1219+ msblk->devblksize_log2 = ffz(~msblk->devblksize);
1220+
1221+ mutex_init(&msblk->read_data_mutex);
1222+ mutex_init(&msblk->read_page_mutex);
1223+ mutex_init(&msblk->meta_index_mutex);
1224+
1225+ /* sblk->bytes_used is checked in squashfs_read_data to ensure reads are not
1226+ * beyond filesystem end. As we're using squashfs_read_data to read sblk here,
1227+ * first set sblk->bytes_used to a useful value */
1228+ sblk->bytes_used = sizeof(struct squashfs_super_block);
1229+ if (!squashfs_read_data(s, (char *) sblk, SQUASHFS_START,
1230+ sizeof(struct squashfs_super_block) |
1231+ SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, sizeof(struct squashfs_super_block))) {
1232+ SERROR("unable to read superblock\n");
1233+ goto failed_mount;
1234+ }
1235+
1236+ /* Check it is a SQUASHFS superblock */
1237+ if ((s->s_magic = sblk->s_magic) != SQUASHFS_MAGIC) {
1238+ if (sblk->s_magic == SQUASHFS_MAGIC_SWAP) {
1239+ struct squashfs_super_block ssblk;
1240+
1241+ WARNING("Mounting a different endian SQUASHFS filesystem on %s\n",
1242+ bdevname(s->s_bdev, b));
1243+
1244+ SQUASHFS_SWAP_SUPER_BLOCK(&ssblk, sblk);
1245+ memcpy(sblk, &ssblk, sizeof(struct squashfs_super_block));
1246+ msblk->swap = 1;
1247+ } else {
1248+ SERROR("Can't find a SQUASHFS superblock on %s\n",
1249+ bdevname(s->s_bdev, b));
1250+ goto failed_mount;
1251+ }
1252+ }
1253+
1254+ /* Check the MAJOR & MINOR versions */
1255+ if(!supported_squashfs_filesystem(msblk, silent))
1256+ goto failed_mount;
1257+
1258+ /* Check the filesystem does not extend beyond the end of the
1259+ block device */
1260+ if(sblk->bytes_used < 0 || sblk->bytes_used > i_size_read(s->s_bdev->bd_inode))
1261+ goto failed_mount;
1262+
1263+ /* Check the root inode for sanity */
1264+ if (SQUASHFS_INODE_OFFSET(sblk->root_inode) > SQUASHFS_METADATA_SIZE)
1265+ goto failed_mount;
1266+
1267+ TRACE("Found valid superblock on %s\n", bdevname(s->s_bdev, b));
1268+ TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(sblk->flags)
1269+ ? "un" : "");
1270+ TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(sblk->flags)
1271+ ? "un" : "");
1272+ TRACE("Check data is %spresent in the filesystem\n",
1273+ SQUASHFS_CHECK_DATA(sblk->flags) ? "" : "not ");
1274+ TRACE("Filesystem size %lld bytes\n", sblk->bytes_used);
1275+ TRACE("Block size %d\n", sblk->block_size);
1276+ TRACE("Number of inodes %d\n", sblk->inodes);
1277+ if (sblk->s_major > 1)
1278+ TRACE("Number of fragments %d\n", sblk->fragments);
1279+ TRACE("Number of uids %d\n", sblk->no_uids);
1280+ TRACE("Number of gids %d\n", sblk->no_guids);
1281+ TRACE("sblk->inode_table_start %llx\n", sblk->inode_table_start);
1282+ TRACE("sblk->directory_table_start %llx\n", sblk->directory_table_start);
1283+ if (sblk->s_major > 1)
1284+ TRACE("sblk->fragment_table_start %llx\n", sblk->fragment_table_start);
1285+ TRACE("sblk->uid_start %llx\n", sblk->uid_start);
1286+
1287+ s->s_maxbytes = MAX_LFS_FILESIZE;
1288+ s->s_flags |= MS_RDONLY;
1289+ s->s_op = &squashfs_super_ops;
1290+
1291+ msblk->block_cache = squashfs_cache_init("metadata", SQUASHFS_CACHED_BLKS,
1292+ SQUASHFS_METADATA_SIZE, 0);
1293+ if (msblk->block_cache == NULL)
1294+ goto failed_mount;
1295+
1296+ /* Allocate read_page block */
1297+ msblk->read_page = vmalloc(sblk->block_size);
1298+ if (msblk->read_page == NULL) {
1299+ ERROR("Failed to allocate read_page block\n");
1300+ goto failed_mount;
1301+ }
1302+
1303+ /* Allocate uid and gid tables */
1304+ msblk->uid = kmalloc((sblk->no_uids + sblk->no_guids) *
1305+ sizeof(unsigned int), GFP_KERNEL);
1306+ if (msblk->uid == NULL) {
1307+ ERROR("Failed to allocate uid/gid table\n");
1308+ goto failed_mount;
1309+ }
1310+ msblk->guid = msblk->uid + sblk->no_uids;
1311+
1312+ if (msblk->swap) {
1313+ unsigned int suid[sblk->no_uids + sblk->no_guids];
1314+
1315+ if (!squashfs_read_data(s, (char *) &suid, sblk->uid_start,
1316+ ((sblk->no_uids + sblk->no_guids) *
1317+ sizeof(unsigned int)) |
1318+ SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, (sblk->no_uids + sblk->no_guids) * sizeof(unsigned int))) {
1319+ ERROR("unable to read uid/gid table\n");
1320+ goto failed_mount;
1321+ }
1322+
1323+ SQUASHFS_SWAP_DATA(msblk->uid, suid, (sblk->no_uids +
1324+ sblk->no_guids), (sizeof(unsigned int) * 8));
1325+ } else
1326+ if (!squashfs_read_data(s, (char *) msblk->uid, sblk->uid_start,
1327+ ((sblk->no_uids + sblk->no_guids) *
1328+ sizeof(unsigned int)) |
1329+ SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, (sblk->no_uids + sblk->no_guids) * sizeof(unsigned int))) {
1330+ ERROR("unable to read uid/gid table\n");
1331+ goto failed_mount;
1332+ }
1333+
1334+
1335+ if (sblk->s_major == 1 && squashfs_1_0_supported(msblk))
1336+ goto allocate_root;
1337+
1338+ msblk->fragment_cache = squashfs_cache_init("fragment",
1339+ SQUASHFS_CACHED_FRAGMENTS, sblk->block_size, 1);
1340+ if (msblk->fragment_cache == NULL)
1341+ goto failed_mount;
1342+
1343+ /* Allocate and read fragment index table */
1344+ if (msblk->read_fragment_index_table(s) == 0)
1345+ goto failed_mount;
1346+
1347+ if(sblk->s_major < 3 || sblk->lookup_table_start == SQUASHFS_INVALID_BLK)
1348+ goto allocate_root;
1349+
1350+ /* Allocate and read inode lookup table */
1351+ if (read_inode_lookup_table(s) == 0)
1352+ goto failed_mount;
1353+
1354+ s->s_export_op = &squashfs_export_ops;
1355+
1356+allocate_root:
1357+ root = new_inode(s);
1358+ if ((msblk->read_inode)(root, sblk->root_inode) == 0)
1359+ goto failed_mount;
1360+ insert_inode_hash(root);
1361+
1362+ s->s_root = d_alloc_root(root);
1363+ if (s->s_root == NULL) {
1364+ ERROR("Root inode create failed\n");
1365+ iput(root);
1366+ goto failed_mount;
1367+ }
1368+
1369+ TRACE("Leaving squashfs_fill_super\n");
1370+ return 0;
1371+
1372+failed_mount:
1373+ kfree(msblk->inode_lookup_table);
1374+ kfree(msblk->fragment_index);
1375+ squashfs_cache_delete(msblk->fragment_cache);
1376+ kfree(msblk->uid);
1377+ vfree(msblk->read_page);
1378+ squashfs_cache_delete(msblk->block_cache);
1379+ kfree(msblk->fragment_index_2);
1380+ vfree(msblk->stream.workspace);
1381+ kfree(s->s_fs_info);
1382+ s->s_fs_info = NULL;
1383+ return -EINVAL;
1384+
1385+failure:
1386+ return -ENOMEM;
1387+}
1388+
1389+
1390+static int squashfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1391+{
1392+ struct squashfs_sb_info *msblk = dentry->d_sb->s_fs_info;
1393+ struct squashfs_super_block *sblk = &msblk->sblk;
1394+
1395+ TRACE("Entered squashfs_statfs\n");
1396+
1397+ buf->f_type = SQUASHFS_MAGIC;
1398+ buf->f_bsize = sblk->block_size;
1399+ buf->f_blocks = ((sblk->bytes_used - 1) >> sblk->block_log) + 1;
1400+ buf->f_bfree = buf->f_bavail = 0;
1401+ buf->f_files = sblk->inodes;
1402+ buf->f_ffree = 0;
1403+ buf->f_namelen = SQUASHFS_NAME_LEN;
1404+
1405+ return 0;
1406+}
1407+
1408+
1409+static int squashfs_symlink_readpage(struct file *file, struct page *page)
1410+{
1411+ struct inode *inode = page->mapping->host;
1412+ int index = page->index << PAGE_CACHE_SHIFT, length, bytes, avail_bytes;
1413+ long long block = SQUASHFS_I(inode)->start_block;
1414+ int offset = SQUASHFS_I(inode)->offset;
1415+ void *pageaddr = kmap(page);
1416+
1417+ TRACE("Entered squashfs_symlink_readpage, page index %ld, start block "
1418+ "%llx, offset %x\n", page->index,
1419+ SQUASHFS_I(inode)->start_block,
1420+ SQUASHFS_I(inode)->offset);
1421+
1422+ for (length = 0; length < index; length += bytes) {
1423+ bytes = squashfs_get_cached_block(inode->i_sb, NULL, block,
1424+ offset, PAGE_CACHE_SIZE, &block, &offset);
1425+ if (bytes == 0) {
1426+ ERROR("Unable to read symbolic link [%llx:%x]\n", block, offset);
1427+ goto skip_read;
1428+ }
1429+ }
1430+
1431+ if (length != index) {
1432+ ERROR("(squashfs_symlink_readpage) length != index\n");
1433+ bytes = 0;
1434+ goto skip_read;
1435+ }
1436+
1437+ avail_bytes = min_t(int, i_size_read(inode) - length, PAGE_CACHE_SIZE);
1438+
1439+ bytes = squashfs_get_cached_block(inode->i_sb, pageaddr, block, offset,
1440+ avail_bytes, &block, &offset);
1441+ if (bytes == 0)
1442+ ERROR("Unable to read symbolic link [%llx:%x]\n", block, offset);
1443+
1444+skip_read:
1445+ memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1446+ kunmap(page);
1447+ flush_dcache_page(page);
1448+ SetPageUptodate(page);
1449+ unlock_page(page);
1450+
1451+ return 0;
1452+}
1453+
1454+
1455+static struct meta_index *locate_meta_index(struct inode *inode, int index, int offset)
1456+{
1457+ struct meta_index *meta = NULL;
1458+ struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
1459+ int i;
1460+
1461+ mutex_lock(&msblk->meta_index_mutex);
1462+
1463+ TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
1464+
1465+ if (msblk->meta_index == NULL)
1466+ goto not_allocated;
1467+
1468+ for (i = 0; i < SQUASHFS_META_NUMBER; i ++) {
1469+ if (msblk->meta_index[i].inode_number == inode->i_ino &&
1470+ msblk->meta_index[i].offset >= offset &&
1471+ msblk->meta_index[i].offset <= index &&
1472+ msblk->meta_index[i].locked == 0) {
1473+ TRACE("locate_meta_index: entry %d, offset %d\n", i,
1474+ msblk->meta_index[i].offset);
1475+ meta = &msblk->meta_index[i];
1476+ offset = meta->offset;
1477+ }
1478+ }
1479+
1480+ if (meta)
1481+ meta->locked = 1;
1482+
1483+not_allocated:
1484+ mutex_unlock(&msblk->meta_index_mutex);
1485+
1486+ return meta;
1487+}
1488+
1489+
1490+static struct meta_index *empty_meta_index(struct inode *inode, int offset, int skip)
1491+{
1492+ struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
1493+ struct meta_index *meta = NULL;
1494+ int i;
1495+
1496+ mutex_lock(&msblk->meta_index_mutex);
1497+
1498+ TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
1499+
1500+ if (msblk->meta_index == NULL) {
1501+ msblk->meta_index = kmalloc(sizeof(struct meta_index) *
1502+ SQUASHFS_META_NUMBER, GFP_KERNEL);
1503+ if (msblk->meta_index == NULL) {
1504+ ERROR("Failed to allocate meta_index\n");
1505+ goto failed;
1506+ }
1507+ for (i = 0; i < SQUASHFS_META_NUMBER; i++) {
1508+ msblk->meta_index[i].inode_number = 0;
1509+ msblk->meta_index[i].locked = 0;
1510+ }
1511+ msblk->next_meta_index = 0;
1512+ }
1513+
1514+ for (i = SQUASHFS_META_NUMBER; i &&
1515+ msblk->meta_index[msblk->next_meta_index].locked; i --)
1516+ msblk->next_meta_index = (msblk->next_meta_index + 1) %
1517+ SQUASHFS_META_NUMBER;
1518+
1519+ if (i == 0) {
1520+ TRACE("empty_meta_index: failed!\n");
1521+ goto failed;
1522+ }
1523+
1524+ TRACE("empty_meta_index: returned meta entry %d, %p\n",
1525+ msblk->next_meta_index,
1526+ &msblk->meta_index[msblk->next_meta_index]);
1527+
1528+ meta = &msblk->meta_index[msblk->next_meta_index];
1529+ msblk->next_meta_index = (msblk->next_meta_index + 1) %
1530+ SQUASHFS_META_NUMBER;
1531+
1532+ meta->inode_number = inode->i_ino;
1533+ meta->offset = offset;
1534+ meta->skip = skip;
1535+ meta->entries = 0;
1536+ meta->locked = 1;
1537+
1538+failed:
1539+ mutex_unlock(&msblk->meta_index_mutex);
1540+ return meta;
1541+}
1542+
1543+
1544+static void release_meta_index(struct inode *inode, struct meta_index *meta)
1545+{
1546+ meta->locked = 0;
1547+ smp_mb();
1548+}
1549+
1550+
1551+static int read_block_index(struct super_block *s, int blocks, char *block_list,
1552+ long long *start_block, int *offset)
1553+{
1554+ struct squashfs_sb_info *msblk = s->s_fs_info;
1555+ unsigned int *block_listp;
1556+ int block = 0;
1557+
1558+ if (msblk->swap) {
1559+ char sblock_list[blocks << 2];
1560+
1561+ if (!squashfs_get_cached_block(s, sblock_list, *start_block,
1562+ *offset, blocks << 2, start_block, offset)) {
1563+ ERROR("Fail reading block list [%llx:%x]\n", *start_block, *offset);
1564+ goto failure;
1565+ }
1566+ SQUASHFS_SWAP_INTS(((unsigned int *)block_list),
1567+ ((unsigned int *)sblock_list), blocks);
1568+ } else {
1569+ if (!squashfs_get_cached_block(s, block_list, *start_block,
1570+ *offset, blocks << 2, start_block, offset)) {
1571+ ERROR("Fail reading block list [%llx:%x]\n", *start_block, *offset);
1572+ goto failure;
1573+ }
1574+ }
1575+
1576+ for (block_listp = (unsigned int *) block_list; blocks;
1577+ block_listp++, blocks --)
1578+ block += SQUASHFS_COMPRESSED_SIZE_BLOCK(*block_listp);
1579+
1580+ return block;
1581+
1582+failure:
1583+ return -1;
1584+}
1585+
1586+
1587+#define SIZE 256
1588+
1589+static inline int calculate_skip(int blocks) {
1590+ int skip = (blocks - 1) / ((SQUASHFS_SLOTS * SQUASHFS_META_ENTRIES + 1) * SQUASHFS_META_INDEXES);
1591+ return skip >= 7 ? 7 : skip + 1;
1592+}
1593+
1594+
1595+static int get_meta_index(struct inode *inode, int index,
1596+ long long *index_block, int *index_offset,
1597+ long long *data_block, char *block_list)
1598+{
1599+ struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
1600+ struct squashfs_super_block *sblk = &msblk->sblk;
1601+ int skip = calculate_skip(i_size_read(inode) >> sblk->block_log);
1602+ int offset = 0;
1603+ struct meta_index *meta;
1604+ struct meta_entry *meta_entry;
1605+ long long cur_index_block = SQUASHFS_I(inode)->u.s1.block_list_start;
1606+ int cur_offset = SQUASHFS_I(inode)->offset;
1607+ long long cur_data_block = SQUASHFS_I(inode)->start_block;
1608+ int i;
1609+
1610+ index /= SQUASHFS_META_INDEXES * skip;
1611+
1612+ while (offset < index) {
1613+ meta = locate_meta_index(inode, index, offset + 1);
1614+
1615+ if (meta == NULL) {
1616+ meta = empty_meta_index(inode, offset + 1, skip);
1617+ if (meta == NULL)
1618+ goto all_done;
1619+ } else {
1620+ if(meta->entries == 0)
1621+ goto failed;
1622+ /* XXX */
1623+ offset = index < meta->offset + meta->entries ? index :
1624+ meta->offset + meta->entries - 1;
1625+ /* XXX */
1626+ meta_entry = &meta->meta_entry[offset - meta->offset];
1627+ cur_index_block = meta_entry->index_block + sblk->inode_table_start;
1628+ cur_offset = meta_entry->offset;
1629+ cur_data_block = meta_entry->data_block;
1630+ TRACE("get_meta_index: offset %d, meta->offset %d, "
1631+ "meta->entries %d\n", offset, meta->offset, meta->entries);
1632+ TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
1633+ " data_block 0x%llx\n", cur_index_block,
1634+ cur_offset, cur_data_block);
1635+ }
1636+
1637+ for (i = meta->offset + meta->entries; i <= index &&
1638+ i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
1639+ int blocks = skip * SQUASHFS_META_INDEXES;
1640+
1641+ while (blocks) {
1642+ int block = blocks > (SIZE >> 2) ? (SIZE >> 2) : blocks;
1643+ int res = read_block_index(inode->i_sb, block, block_list,
1644+ &cur_index_block, &cur_offset);
1645+
1646+ if (res == -1)
1647+ goto failed;
1648+
1649+ cur_data_block += res;
1650+ blocks -= block;
1651+ }
1652+
1653+ meta_entry = &meta->meta_entry[i - meta->offset];
1654+ meta_entry->index_block = cur_index_block - sblk->inode_table_start;
1655+ meta_entry->offset = cur_offset;
1656+ meta_entry->data_block = cur_data_block;
1657+ meta->entries ++;
1658+ offset ++;
1659+ }
1660+
1661+ TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
1662+ meta->offset, meta->entries);
1663+
1664+ release_meta_index(inode, meta);
1665+ }
1666+
1667+all_done:
1668+ *index_block = cur_index_block;
1669+ *index_offset = cur_offset;
1670+ *data_block = cur_data_block;
1671+
1672+ return offset * SQUASHFS_META_INDEXES * skip;
1673+
1674+failed:
1675+ release_meta_index(inode, meta);
1676+ return -1;
1677+}
1678+
1679+
1680+static long long read_blocklist(struct inode *inode, int index,
1681+ int readahead_blks, char *block_list,
1682+ unsigned short **block_p, unsigned int *bsize)
1683+{
1684+ long long block_ptr;
1685+ int offset;
1686+ long long block;
1687+ int res = get_meta_index(inode, index, &block_ptr, &offset, &block,
1688+ block_list);
1689+
1690+ TRACE("read_blocklist: res %d, index %d, block_ptr 0x%llx, offset"
1691+ " 0x%x, block 0x%llx\n", res, index, block_ptr, offset, block);
1692+
1693+ if(res == -1)
1694+ goto failure;
1695+
1696+ index -= res;
1697+
1698+ while (index) {
1699+ int blocks = index > (SIZE >> 2) ? (SIZE >> 2) : index;
1700+ int res = read_block_index(inode->i_sb, blocks, block_list,
1701+ &block_ptr, &offset);
1702+ if (res == -1)
1703+ goto failure;
1704+ block += res;
1705+ index -= blocks;
1706+ }
1707+
1708+ if (read_block_index(inode->i_sb, 1, block_list, &block_ptr, &offset) == -1)
1709+ goto failure;
1710+ *bsize = *((unsigned int *) block_list);
1711+
1712+ return block;
1713+
1714+failure:
1715+ return 0;
1716+}
1717+
1718+
1719+static int squashfs_readpage(struct file *file, struct page *page)
1720+{
1721+ struct inode *inode = page->mapping->host;
1722+ struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
1723+ struct squashfs_super_block *sblk = &msblk->sblk;
1724+ unsigned char *block_list = NULL;
1725+ long long block;
1726+ unsigned int bsize, i;
1727+ int bytes;
1728+ int index = page->index >> (sblk->block_log - PAGE_CACHE_SHIFT);
1729+ void *pageaddr;
1730+ struct squashfs_cache_entry *fragment = NULL;
1731+ char *data_ptr = msblk->read_page;
1732+
1733+ int mask = (1 << (sblk->block_log - PAGE_CACHE_SHIFT)) - 1;
1734+ int start_index = page->index & ~mask;
1735+ int end_index = start_index | mask;
1736+ int file_end = i_size_read(inode) >> sblk->block_log;
1737+ int sparse = 0;
1738+
1739+ TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
1740+ page->index, SQUASHFS_I(inode)->start_block);
1741+
1742+ if (page->index >= ((i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
1743+ PAGE_CACHE_SHIFT))
1744+ goto out;
1745+
1746+ if (SQUASHFS_I(inode)->u.s1.fragment_start_block == SQUASHFS_INVALID_BLK
1747+ || index < file_end) {
1748+ block_list = kmalloc(SIZE, GFP_KERNEL);
1749+ if (block_list == NULL) {
1750+ ERROR("Failed to allocate block_list\n");
1751+ goto error_out;
1752+ }
1753+
1754+ block = (msblk->read_blocklist)(inode, index, 1, block_list, NULL, &bsize);
1755+ if (block == 0)
1756+ goto error_out;
1757+
1758+ if (bsize == 0) { /* hole */
1759+ bytes = index == file_end ?
1760+ (i_size_read(inode) & (sblk->block_size - 1)) : sblk->block_size;
1761+ sparse = 1;
1762+ } else {
1763+ mutex_lock(&msblk->read_page_mutex);
1764+
1765+ bytes = squashfs_read_data(inode->i_sb, msblk->read_page, block,
1766+ bsize, NULL, sblk->block_size);
1767+
1768+ if (bytes == 0) {
1769+ ERROR("Unable to read page, block %llx, size %x\n", block, bsize);
1770+ mutex_unlock(&msblk->read_page_mutex);
1771+ goto error_out;
1772+ }
1773+ }
1774+ } else {
1775+ fragment = get_cached_fragment(inode->i_sb,
1776+ SQUASHFS_I(inode)-> u.s1.fragment_start_block,
1777+ SQUASHFS_I(inode)->u.s1.fragment_size);
1778+
1779+ if (fragment->error) {
1780+ ERROR("Unable to read page, block %llx, size %x\n",
1781+ SQUASHFS_I(inode)->u.s1.fragment_start_block,
1782+ (int) SQUASHFS_I(inode)->u.s1.fragment_size);
1783+ release_cached_fragment(msblk, fragment);
1784+ goto error_out;
1785+ }
1786+ bytes = i_size_read(inode) & (sblk->block_size - 1);
1787+ data_ptr = fragment->data + SQUASHFS_I(inode)->u.s1.fragment_offset;
1788+ }
1789+
1790+ for (i = start_index; i <= end_index && bytes > 0; i++,
1791+ bytes -= PAGE_CACHE_SIZE, data_ptr += PAGE_CACHE_SIZE) {
1792+ struct page *push_page;
1793+ int avail = sparse ? 0 : min_t(unsigned int, bytes, PAGE_CACHE_SIZE);
1794+
1795+ TRACE("bytes %d, i %d, available_bytes %d\n", bytes, i, avail);
1796+
1797+ push_page = (i == page->index) ? page :
1798+ grab_cache_page_nowait(page->mapping, i);
1799+
1800+ if (!push_page)
1801+ continue;
1802+
1803+ if (PageUptodate(push_page))
1804+ goto skip_page;
1805+
1806+ pageaddr = kmap_atomic(push_page, KM_USER0);
1807+ memcpy(pageaddr, data_ptr, avail);
1808+ memset(pageaddr + avail, 0, PAGE_CACHE_SIZE - avail);
1809+ kunmap_atomic(pageaddr, KM_USER0);
1810+ flush_dcache_page(push_page);
1811+ SetPageUptodate(push_page);
1812+skip_page:
1813+ unlock_page(push_page);
1814+ if(i != page->index)
1815+ page_cache_release(push_page);
1816+ }
1817+
1818+ if (SQUASHFS_I(inode)->u.s1.fragment_start_block == SQUASHFS_INVALID_BLK
1819+ || index < file_end) {
1820+ if (!sparse)
1821+ mutex_unlock(&msblk->read_page_mutex);
1822+ kfree(block_list);
1823+ } else
1824+ release_cached_fragment(msblk, fragment);
1825+
1826+ return 0;
1827+
1828+error_out:
1829+ SetPageError(page);
1830+out:
1831+ pageaddr = kmap_atomic(page, KM_USER0);
1832+ memset(pageaddr, 0, PAGE_CACHE_SIZE);
1833+ kunmap_atomic(pageaddr, KM_USER0);
1834+ flush_dcache_page(page);
1835+ if (!PageError(page))
1836+ SetPageUptodate(page);
1837+ unlock_page(page);
1838+
1839+ kfree(block_list);
1840+ return 0;
1841+}
1842+
1843+
1844+static int get_dir_index_using_offset(struct super_block *s,
1845+ long long *next_block, unsigned int *next_offset,
1846+ long long index_start, unsigned int index_offset, int i_count,
1847+ long long f_pos)
1848+{
1849+ struct squashfs_sb_info *msblk = s->s_fs_info;
1850+ struct squashfs_super_block *sblk = &msblk->sblk;
1851+ int i, length = 0;
1852+ struct squashfs_dir_index index;
1853+
1854+ TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %d\n",
1855+ i_count, (unsigned int) f_pos);
1856+
1857+ f_pos -= 3;
1858+ if (f_pos == 0)
1859+ goto finish;
1860+
1861+ for (i = 0; i < i_count; i++) {
1862+ if (msblk->swap) {
1863+ struct squashfs_dir_index sindex;
1864+ squashfs_get_cached_block(s, &sindex, index_start, index_offset,
1865+ sizeof(sindex), &index_start, &index_offset);
1866+ SQUASHFS_SWAP_DIR_INDEX(&index, &sindex);
1867+ } else
1868+ squashfs_get_cached_block(s, &index, index_start, index_offset,
1869+ sizeof(index), &index_start, &index_offset);
1870+
1871+ if (index.index > f_pos)
1872+ break;
1873+
1874+ squashfs_get_cached_block(s, NULL, index_start, index_offset,
1875+ index.size + 1, &index_start, &index_offset);
1876+
1877+ length = index.index;
1878+ *next_block = index.start_block + sblk->directory_table_start;
1879+ }
1880+
1881+ *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
1882+
1883+finish:
1884+ return length + 3;
1885+}
1886+
1887+
1888+static int get_dir_index_using_name(struct super_block *s,
1889+ long long *next_block, unsigned int *next_offset,
1890+ long long index_start, unsigned int index_offset, int i_count,
1891+ const char *name, int size)
1892+{
1893+ struct squashfs_sb_info *msblk = s->s_fs_info;
1894+ struct squashfs_super_block *sblk = &msblk->sblk;
1895+ int i, length = 0;
1896+ struct squashfs_dir_index *index;
1897+ char *str;
1898+
1899+ TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
1900+
1901+ str = kmalloc(sizeof(struct squashfs_dir_index) +
1902+ (SQUASHFS_NAME_LEN + 1) * 2, GFP_KERNEL);
1903+ if (str == NULL) {
1904+ ERROR("Failed to allocate squashfs_dir_index\n");
1905+ goto failure;
1906+ }
1907+
1908+ index = (struct squashfs_dir_index *) (str + SQUASHFS_NAME_LEN + 1);
1909+ strncpy(str, name, size);
1910+ str[size] = '\0';
1911+
1912+ for (i = 0; i < i_count; i++) {
1913+ if (msblk->swap) {
1914+ struct squashfs_dir_index sindex;
1915+ squashfs_get_cached_block(s, &sindex, index_start, index_offset,
1916+ sizeof(sindex), &index_start, &index_offset);
1917+ SQUASHFS_SWAP_DIR_INDEX(index, &sindex);
1918+ } else
1919+ squashfs_get_cached_block(s, index, index_start, index_offset,
1920+ sizeof(struct squashfs_dir_index), &index_start, &index_offset);
1921+
1922+ squashfs_get_cached_block(s, index->name, index_start, index_offset,
1923+ index->size + 1, &index_start, &index_offset);
1924+
1925+ index->name[index->size + 1] = '\0';
1926+
1927+ if (strcmp(index->name, str) > 0)
1928+ break;
1929+
1930+ length = index->index;
1931+ *next_block = index->start_block + sblk->directory_table_start;
1932+ }
1933+
1934+ *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
1935+ kfree(str);
1936+
1937+failure:
1938+ return length + 3;
1939+}
1940+
1941+
1942+static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
1943+{
1944+ struct inode *i = file->f_dentry->d_inode;
1945+ struct squashfs_sb_info *msblk = i->i_sb->s_fs_info;
1946+ struct squashfs_super_block *sblk = &msblk->sblk;
1947+ long long next_block = SQUASHFS_I(i)->start_block +
1948+ sblk->directory_table_start;
1949+ int next_offset = SQUASHFS_I(i)->offset, length = 0, dir_count;
1950+ struct squashfs_dir_header dirh;
1951+ struct squashfs_dir_entry *dire;
1952+
1953+ TRACE("Entered squashfs_readdir [%llx:%x]\n", next_block, next_offset);
1954+
1955+ dire = kmalloc(sizeof(struct squashfs_dir_entry) +
1956+ SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
1957+ if (dire == NULL) {
1958+ ERROR("Failed to allocate squashfs_dir_entry\n");
1959+ goto finish;
1960+ }
1961+
1962+ while(file->f_pos < 3) {
1963+ char *name;
1964+ int size, i_ino;
1965+
1966+ if(file->f_pos == 0) {
1967+ name = ".";
1968+ size = 1;
1969+ i_ino = i->i_ino;
1970+ } else {
1971+ name = "..";
1972+ size = 2;
1973+ i_ino = SQUASHFS_I(i)->u.s2.parent_inode;
1974+ }
1975+ TRACE("Calling filldir(%x, %s, %d, %d, %d, %d)\n",
1976+ (unsigned int) dirent, name, size, (int)
1977+ file->f_pos, i_ino, squashfs_filetype_table[1]);
1978+
1979+ if (filldir(dirent, name, size, file->f_pos, i_ino,
1980+ squashfs_filetype_table[1]) < 0) {
1981+ TRACE("Filldir returned less than 0\n");
1982+ goto finish;
1983+ }
1984+ file->f_pos += size;
1985+ }
1986+
1987+ length = get_dir_index_using_offset(i->i_sb, &next_block, &next_offset,
1988+ SQUASHFS_I(i)->u.s2.directory_index_start,
1989+ SQUASHFS_I(i)->u.s2.directory_index_offset,
1990+ SQUASHFS_I(i)->u.s2.directory_index_count, file->f_pos);
1991+
1992+ while (length < i_size_read(i)) {
1993+ /* read directory header */
1994+ if (msblk->swap) {
1995+ struct squashfs_dir_header sdirh;
1996+
1997+ if (!squashfs_get_cached_block(i->i_sb, &sdirh, next_block,
1998+ next_offset, sizeof(sdirh), &next_block, &next_offset))
1999+ goto failed_read;
2000+
2001+ length += sizeof(sdirh);
2002+ SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
2003+ } else {
2004+ if (!squashfs_get_cached_block(i->i_sb, &dirh, next_block,
2005+ next_offset, sizeof(dirh), &next_block, &next_offset))
2006+ goto failed_read;
2007+
2008+ length += sizeof(dirh);
2009+ }
2010+
2011+ dir_count = dirh.count + 1;
2012+ while (dir_count--) {
2013+ if (msblk->swap) {
2014+ struct squashfs_dir_entry sdire;
2015+ if (!squashfs_get_cached_block(i->i_sb, &sdire, next_block,
2016+ next_offset, sizeof(sdire), &next_block, &next_offset))
2017+ goto failed_read;
2018+
2019+ length += sizeof(sdire);
2020+ SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
2021+ } else {
2022+ if (!squashfs_get_cached_block(i->i_sb, dire, next_block,
2023+ next_offset, sizeof(*dire), &next_block, &next_offset))
2024+ goto failed_read;
2025+
2026+ length += sizeof(*dire);
2027+ }
2028+
2029+ if (!squashfs_get_cached_block(i->i_sb, dire->name, next_block,
2030+ next_offset, dire->size + 1, &next_block, &next_offset))
2031+ goto failed_read;
2032+
2033+ length += dire->size + 1;
2034+
2035+ if (file->f_pos >= length)
2036+ continue;
2037+
2038+ dire->name[dire->size + 1] = '\0';
2039+
2040+ TRACE("Calling filldir(%x, %s, %d, %d, %x:%x, %d, %d)\n",
2041+ (unsigned int) dirent, dire->name, dire->size + 1,
2042+ (int) file->f_pos, dirh.start_block, dire->offset,
2043+ dirh.inode_number + dire->inode_number,
2044+ squashfs_filetype_table[dire->type]);
2045+
2046+ if (filldir(dirent, dire->name, dire->size + 1, file->f_pos,
2047+ dirh.inode_number + dire->inode_number,
2048+ squashfs_filetype_table[dire->type]) < 0) {
2049+ TRACE("Filldir returned less than 0\n");
2050+ goto finish;
2051+ }
2052+ file->f_pos = length;
2053+ }
2054+ }
2055+
2056+finish:
2057+ kfree(dire);
2058+ return 0;
2059+
2060+failed_read:
2061+ ERROR("Unable to read directory block [%llx:%x]\n", next_block,
2062+ next_offset);
2063+ kfree(dire);
2064+ return 0;
2065+}
2066+
2067+
2068+static struct dentry *squashfs_lookup(struct inode *i, struct dentry *dentry,
2069+ struct nameidata *nd)
2070+{
2071+ const unsigned char *name = dentry->d_name.name;
2072+ int len = dentry->d_name.len;
2073+ struct inode *inode = NULL;
2074+ struct squashfs_sb_info *msblk = i->i_sb->s_fs_info;
2075+ struct squashfs_super_block *sblk = &msblk->sblk;
2076+ long long next_block = SQUASHFS_I(i)->start_block +
2077+ sblk->directory_table_start;
2078+ int next_offset = SQUASHFS_I(i)->offset, length = 0, dir_count;
2079+ struct squashfs_dir_header dirh;
2080+ struct squashfs_dir_entry *dire;
2081+
2082+ TRACE("Entered squashfs_lookup [%llx:%x]\n", next_block, next_offset);
2083+
2084+ dire = kmalloc(sizeof(struct squashfs_dir_entry) +
2085+ SQUASHFS_NAME_LEN + 1, GFP_KERNEL);
2086+ if (dire == NULL) {
2087+ ERROR("Failed to allocate squashfs_dir_entry\n");
2088+ goto exit_lookup;
2089+ }
2090+
2091+ if (len > SQUASHFS_NAME_LEN)
2092+ goto exit_lookup;
2093+
2094+ length = get_dir_index_using_name(i->i_sb, &next_block, &next_offset,
2095+ SQUASHFS_I(i)->u.s2.directory_index_start,
2096+ SQUASHFS_I(i)->u.s2.directory_index_offset,
2097+ SQUASHFS_I(i)->u.s2.directory_index_count, name, len);
2098+
2099+ while (length < i_size_read(i)) {
2100+ /* read directory header */
2101+ if (msblk->swap) {
2102+ struct squashfs_dir_header sdirh;
2103+ if (!squashfs_get_cached_block(i->i_sb, &sdirh, next_block,
2104+ next_offset, sizeof(sdirh), &next_block, &next_offset))
2105+ goto failed_read;
2106+
2107+ length += sizeof(sdirh);
2108+ SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
2109+ } else {
2110+ if (!squashfs_get_cached_block(i->i_sb, &dirh, next_block,
2111+ next_offset, sizeof(dirh), &next_block, &next_offset))
2112+ goto failed_read;
2113+
2114+ length += sizeof(dirh);
2115+ }
2116+
2117+ dir_count = dirh.count + 1;
2118+ while (dir_count--) {
2119+ if (msblk->swap) {
2120+ struct squashfs_dir_entry sdire;
2121+ if (!squashfs_get_cached_block(i->i_sb, &sdire, next_block,
2122+ next_offset, sizeof(sdire), &next_block, &next_offset))
2123+ goto failed_read;
2124+
2125+ length += sizeof(sdire);
2126+ SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
2127+ } else {
2128+ if (!squashfs_get_cached_block(i->i_sb, dire, next_block,
2129+ next_offset, sizeof(*dire), &next_block, &next_offset))
2130+ goto failed_read;
2131+
2132+ length += sizeof(*dire);
2133+ }
2134+
2135+ if (!squashfs_get_cached_block(i->i_sb, dire->name, next_block,
2136+ next_offset, dire->size + 1, &next_block, &next_offset))
2137+ goto failed_read;
2138+
2139+ length += dire->size + 1;
2140+
2141+ if (name[0] < dire->name[0])
2142+ goto exit_lookup;
2143+
2144+ if ((len == dire->size + 1) && !strncmp(name, dire->name, len)) {
2145+ squashfs_inode_t ino = SQUASHFS_MKINODE(dirh.start_block,
2146+ dire->offset);
2147+
2148+ TRACE("calling squashfs_iget for directory entry %s, inode"
2149+ " %x:%x, %d\n", name, dirh.start_block, dire->offset,
2150+ dirh.inode_number + dire->inode_number);
2151+
2152+ inode = squashfs_iget(i->i_sb, ino, dirh.inode_number + dire->inode_number);
2153+
2154+ goto exit_lookup;
2155+ }
2156+ }
2157+ }
2158+
2159+exit_lookup:
2160+ kfree(dire);
2161+ if (inode)
2162+ return d_splice_alias(inode, dentry);
2163+ d_add(dentry, inode);
2164+ return ERR_PTR(0);
2165+
2166+failed_read:
2167+ ERROR("Unable to read directory block [%llx:%x]\n", next_block,
2168+ next_offset);
2169+ goto exit_lookup;
2170+}
2171+
2172+
2173+static int squashfs_remount(struct super_block *s, int *flags, char *data)
2174+{
2175+ *flags |= MS_RDONLY;
2176+ return 0;
2177+}
2178+
2179+
2180+static void squashfs_put_super(struct super_block *s)
2181+{
2182+ if (s->s_fs_info) {
2183+ struct squashfs_sb_info *sbi = s->s_fs_info;
2184+ squashfs_cache_delete(sbi->block_cache);
2185+ squashfs_cache_delete(sbi->fragment_cache);
2186+ vfree(sbi->read_page);
2187+ kfree(sbi->uid);
2188+ kfree(sbi->fragment_index);
2189+ kfree(sbi->fragment_index_2);
2190+ kfree(sbi->meta_index);
2191+ vfree(sbi->stream.workspace);
2192+ kfree(s->s_fs_info);
2193+ s->s_fs_info = NULL;
2194+ }
2195+}
2196+
2197+
2198+static int squashfs_get_sb(struct file_system_type *fs_type, int flags,
2199+ const char *dev_name, void *data, struct vfsmount *mnt)
2200+{
2201+ return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super,
2202+ mnt);
2203+}
2204+
2205+
2206+static int __init init_squashfs_fs(void)
2207+{
2208+ int err = init_inodecache();
2209+ if (err)
2210+ goto out;
2211+
2212+ printk(KERN_INFO "squashfs: version 3.4 (2008/08/26) "
2213+ "Phillip Lougher\n");
2214+
2215+ err = register_filesystem(&squashfs_fs_type);
2216+ if (err)
2217+ destroy_inodecache();
2218+
2219+out:
2220+ return err;
2221+}
2222+
2223+
2224+static void __exit exit_squashfs_fs(void)
2225+{
2226+ unregister_filesystem(&squashfs_fs_type);
2227+ destroy_inodecache();
2228+}
2229+
2230+
2231+static struct kmem_cache * squashfs_inode_cachep;
2232+
2233+
2234+static struct inode *squashfs_alloc_inode(struct super_block *sb)
2235+{
2236+ struct squashfs_inode_info *ei;
2237+ ei = kmem_cache_alloc(squashfs_inode_cachep, GFP_KERNEL);
2238+ return ei ? &ei->vfs_inode : NULL;
2239+}
2240+
2241+
2242+static void squashfs_destroy_inode(struct inode *inode)
2243+{
2244+ kmem_cache_free(squashfs_inode_cachep, SQUASHFS_I(inode));
2245+}
2246+
2247+
2248+static void init_once(void *foo)
2249+{
2250+ struct squashfs_inode_info *ei = foo;
2251+
2252+ inode_init_once(&ei->vfs_inode);
2253+}
2254+
2255+
2256+static int __init init_inodecache(void)
2257+{
2258+ squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
2259+ sizeof(struct squashfs_inode_info), 0,
2260+ SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, init_once);
2261+ if (squashfs_inode_cachep == NULL)
2262+ return -ENOMEM;
2263+ return 0;
2264+}
2265+
2266+
2267+static void destroy_inodecache(void)
2268+{
2269+ kmem_cache_destroy(squashfs_inode_cachep);
2270+}
2271+
2272+
2273+module_init(init_squashfs_fs);
2274+module_exit(exit_squashfs_fs);
2275+MODULE_DESCRIPTION("squashfs 3.4, a compressed read-only filesystem");
2276+MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>");
2277+MODULE_LICENSE("GPL");
2278--- /dev/null
2279+++ b/fs/squashfs/squashfs.h
2280@@ -0,0 +1,86 @@
2281+/*
2282+ * Squashfs - a compressed read only filesystem for Linux
2283+ *
2284+ * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
2285+ * Phillip Lougher <phillip@lougher.demon.co.uk>
2286+ *
2287+ * This program is free software; you can redistribute it and/or
2288+ * modify it under the terms of the GNU General Public License
2289+ * as published by the Free Software Foundation; either version 2,
2290+ * or (at your option) any later version.
2291+ *
2292+ * This program is distributed in the hope that it will be useful,
2293+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2294+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2295+ * GNU General Public License for more details.
2296+ *
2297+ * You should have received a copy of the GNU General Public License
2298+ * along with this program; if not, write to the Free Software
2299+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2300+ *
2301+ * squashfs.h
2302+ */
2303+
2304+#ifdef CONFIG_SQUASHFS_1_0_COMPATIBILITY
2305+#undef CONFIG_SQUASHFS_1_0_COMPATIBILITY
2306+#endif
2307+
2308+#ifdef SQUASHFS_TRACE
2309+#define TRACE(s, args...) printk(KERN_NOTICE "SQUASHFS: "s, ## args)
2310+#else
2311+#define TRACE(s, args...) {}
2312+#endif
2313+
2314+#define ERROR(s, args...) printk(KERN_ERR "SQUASHFS error: "s, ## args)
2315+
2316+#define SERROR(s, args...) do { \
2317+ if (!silent) \
2318+ printk(KERN_ERR "SQUASHFS error: "s, ## args);\
2319+ } while(0)
2320+
2321+#define WARNING(s, args...) printk(KERN_WARNING "SQUASHFS: "s, ## args)
2322+
2323+static inline struct squashfs_inode_info *SQUASHFS_I(struct inode *inode)
2324+{
2325+ return list_entry(inode, struct squashfs_inode_info, vfs_inode);
2326+}
2327+
2328+#if defined(CONFIG_SQUASHFS_1_0_COMPATIBILITY ) || defined(CONFIG_SQUASHFS_2_0_COMPATIBILITY)
2329+#define SQSH_EXTERN
2330+extern unsigned int squashfs_read_data(struct super_block *s, char *buffer,
2331+ long long index, unsigned int length,
2332+ long long *next_index, int srclength);
2333+extern int squashfs_get_cached_block(struct super_block *s, void *buffer,
2334+ long long block, unsigned int offset,
2335+ int length, long long *next_block,
2336+ unsigned int *next_offset);
2337+extern void release_cached_fragment(struct squashfs_sb_info *msblk, struct
2338+ squashfs_cache_entry *fragment);
2339+extern struct squashfs_cache_entry *get_cached_fragment(struct super_block
2340+ *s, long long start_block,
2341+ int length);
2342+extern struct inode *squashfs_iget(struct super_block *s, squashfs_inode_t inode, unsigned int inode_number);
2343+extern const struct address_space_operations squashfs_symlink_aops;
2344+extern const struct address_space_operations squashfs_aops;
2345+extern struct inode_operations squashfs_dir_inode_ops;
2346+#else
2347+#define SQSH_EXTERN static
2348+#endif
2349+
2350+#ifdef CONFIG_SQUASHFS_1_0_COMPATIBILITY
2351+extern int squashfs_1_0_supported(struct squashfs_sb_info *msblk);
2352+#else
2353+static inline int squashfs_1_0_supported(struct squashfs_sb_info *msblk)
2354+{
2355+ return 0;
2356+}
2357+#endif
2358+
2359+#ifdef CONFIG_SQUASHFS_2_0_COMPATIBILITY
2360+extern int squashfs_2_0_supported(struct squashfs_sb_info *msblk);
2361+#else
2362+static inline int squashfs_2_0_supported(struct squashfs_sb_info *msblk)
2363+{
2364+ return 0;
2365+}
2366+#endif
2367--- /dev/null
2368+++ b/fs/squashfs/squashfs2_0.c
2369@@ -0,0 +1,740 @@
2370+/*
2371+ * Squashfs - a compressed read only filesystem for Linux
2372+ *
2373+ * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
2374+ * Phillip Lougher <phillip@lougher.demon.co.uk>
2375+ *
2376+ * This program is free software; you can redistribute it and/or
2377+ * modify it under the terms of the GNU General Public License
2378+ * as published by the Free Software Foundation; either version 2,
2379+ * or (at your option) any later version.
2380+ *
2381+ * This program is distributed in the hope that it will be useful,
2382+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2383+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2384+ * GNU General Public License for more details.
2385+ *
2386+ * You should have received a copy of the GNU General Public License
2387+ * along with this program; if not, write to the Free Software
2388+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2389+ *
2390+ * squashfs2_0.c
2391+ */
2392+
2393+#include <linux/squashfs_fs.h>
2394+#include <linux/module.h>
2395+#include <linux/zlib.h>
2396+#include <linux/fs.h>
2397+#include <linux/squashfs_fs_sb.h>
2398+#include <linux/squashfs_fs_i.h>
2399+
2400+#include "squashfs.h"
2401+static int squashfs_readdir_2(struct file *file, void *dirent, filldir_t filldir);
2402+static struct dentry *squashfs_lookup_2(struct inode *, struct dentry *,
2403+ struct nameidata *);
2404+
2405+static struct file_operations squashfs_dir_ops_2 = {
2406+ .read = generic_read_dir,
2407+ .readdir = squashfs_readdir_2
2408+};
2409+
2410+static struct inode_operations squashfs_dir_inode_ops_2 = {
2411+ .lookup = squashfs_lookup_2
2412+};
2413+
2414+static unsigned char squashfs_filetype_table[] = {
2415+ DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
2416+};
2417+
2418+static int read_fragment_index_table_2(struct super_block *s)
2419+{
2420+ struct squashfs_sb_info *msblk = s->s_fs_info;
2421+ struct squashfs_super_block *sblk = &msblk->sblk;
2422+
2423+ if (!(msblk->fragment_index_2 = kmalloc(SQUASHFS_FRAGMENT_INDEX_BYTES_2
2424+ (sblk->fragments), GFP_KERNEL))) {
2425+ ERROR("Failed to allocate uid/gid table\n");
2426+ return 0;
2427+ }
2428+
2429+ if (SQUASHFS_FRAGMENT_INDEX_BYTES_2(sblk->fragments) &&
2430+ !squashfs_read_data(s, (char *)
2431+ msblk->fragment_index_2,
2432+ sblk->fragment_table_start,
2433+ SQUASHFS_FRAGMENT_INDEX_BYTES_2
2434+ (sblk->fragments) |
2435+ SQUASHFS_COMPRESSED_BIT_BLOCK, NULL, SQUASHFS_FRAGMENT_INDEX_BYTES_2(sblk->fragments))) {
2436+ ERROR("unable to read fragment index table\n");
2437+ return 0;
2438+ }
2439+
2440+ if (msblk->swap) {
2441+ int i;
2442+ unsigned int fragment;
2443+
2444+ for (i = 0; i < SQUASHFS_FRAGMENT_INDEXES_2(sblk->fragments);
2445+ i++) {
2446+ SQUASHFS_SWAP_FRAGMENT_INDEXES_2((&fragment),
2447+ &msblk->fragment_index_2[i], 1);
2448+ msblk->fragment_index_2[i] = fragment;
2449+ }
2450+ }
2451+
2452+ return 1;
2453+}
2454+
2455+
2456+static int get_fragment_location_2(struct super_block *s, unsigned int fragment,
2457+ long long *fragment_start_block,
2458+ unsigned int *fragment_size)
2459+{
2460+ struct squashfs_sb_info *msblk = s->s_fs_info;
2461+ long long start_block =
2462+ msblk->fragment_index_2[SQUASHFS_FRAGMENT_INDEX_2(fragment)];
2463+ int offset = SQUASHFS_FRAGMENT_INDEX_OFFSET_2(fragment);
2464+ struct squashfs_fragment_entry_2 fragment_entry;
2465+
2466+ if (msblk->swap) {
2467+ struct squashfs_fragment_entry_2 sfragment_entry;
2468+
2469+ if (!squashfs_get_cached_block(s, (char *) &sfragment_entry,
2470+ start_block, offset,
2471+ sizeof(sfragment_entry), &start_block,
2472+ &offset))
2473+ goto out;
2474+ SQUASHFS_SWAP_FRAGMENT_ENTRY_2(&fragment_entry, &sfragment_entry);
2475+ } else
2476+ if (!squashfs_get_cached_block(s, (char *) &fragment_entry,
2477+ start_block, offset,
2478+ sizeof(fragment_entry), &start_block,
2479+ &offset))
2480+ goto out;
2481+
2482+ *fragment_start_block = fragment_entry.start_block;
2483+ *fragment_size = fragment_entry.size;
2484+
2485+ return 1;
2486+
2487+out:
2488+ return 0;
2489+}
2490+
2491+
2492+static void squashfs_new_inode(struct squashfs_sb_info *msblk, struct inode *i,
2493+ struct squashfs_base_inode_header_2 *inodeb, unsigned int ino)
2494+{
2495+ struct squashfs_super_block *sblk = &msblk->sblk;
2496+
2497+ i->i_ino = ino;
2498+ i->i_mtime.tv_sec = sblk->mkfs_time;
2499+ i->i_atime.tv_sec = sblk->mkfs_time;
2500+ i->i_ctime.tv_sec = sblk->mkfs_time;
2501+ i->i_uid = msblk->uid[inodeb->uid];
2502+ i->i_mode = inodeb->mode;
2503+ i->i_nlink = 1;
2504+ i->i_size = 0;
2505+ if (inodeb->guid == SQUASHFS_GUIDS)
2506+ i->i_gid = i->i_uid;
2507+ else
2508+ i->i_gid = msblk->guid[inodeb->guid];
2509+}
2510+
2511+
2512+static int squashfs_read_inode_2(struct inode *i, squashfs_inode_t inode)
2513+{
2514+ struct super_block *s = i->i_sb;
2515+ struct squashfs_sb_info *msblk = s->s_fs_info;
2516+ struct squashfs_super_block *sblk = &msblk->sblk;
2517+ unsigned int block = SQUASHFS_INODE_BLK(inode) +
2518+ sblk->inode_table_start;
2519+ unsigned int offset = SQUASHFS_INODE_OFFSET(inode);
2520+ unsigned int ino = SQUASHFS_MK_VFS_INODE(block -
2521+ sblk->inode_table_start, offset);
2522+ long long next_block;
2523+ unsigned int next_offset;
2524+ union squashfs_inode_header_2 id, sid;
2525+ struct squashfs_base_inode_header_2 *inodeb = &id.base,
2526+ *sinodeb = &sid.base;
2527+
2528+ TRACE("Entered squashfs_read_inode_2\n");
2529+
2530+ if (msblk->swap) {
2531+ if (!squashfs_get_cached_block(s, (char *) sinodeb, block,
2532+ offset, sizeof(*sinodeb), &next_block,
2533+ &next_offset))
2534+ goto failed_read;
2535+ SQUASHFS_SWAP_BASE_INODE_HEADER_2(inodeb, sinodeb,
2536+ sizeof(*sinodeb));
2537+ } else
2538+ if (!squashfs_get_cached_block(s, (char *) inodeb, block,
2539+ offset, sizeof(*inodeb), &next_block,
2540+ &next_offset))
2541+ goto failed_read;
2542+
2543+ squashfs_new_inode(msblk, i, inodeb, ino);
2544+
2545+ switch(inodeb->inode_type) {
2546+ case SQUASHFS_FILE_TYPE: {
2547+ struct squashfs_reg_inode_header_2 *inodep = &id.reg;
2548+ struct squashfs_reg_inode_header_2 *sinodep = &sid.reg;
2549+ long long frag_blk;
2550+ unsigned int frag_size = 0;
2551+
2552+ if (msblk->swap) {
2553+ if (!squashfs_get_cached_block(s, (char *)
2554+ sinodep, block, offset,
2555+ sizeof(*sinodep), &next_block,
2556+ &next_offset))
2557+ goto failed_read;
2558+ SQUASHFS_SWAP_REG_INODE_HEADER_2(inodep, sinodep);
2559+ } else
2560+ if (!squashfs_get_cached_block(s, (char *)
2561+ inodep, block, offset,
2562+ sizeof(*inodep), &next_block,
2563+ &next_offset))
2564+ goto failed_read;
2565+
2566+ frag_blk = SQUASHFS_INVALID_BLK;
2567+ if (inodep->fragment != SQUASHFS_INVALID_FRAG &&
2568+ !get_fragment_location_2(s,
2569+ inodep->fragment, &frag_blk, &frag_size))
2570+ goto failed_read;
2571+
2572+ i->i_size = inodep->file_size;
2573+ i->i_fop = &generic_ro_fops;
2574+ i->i_mode |= S_IFREG;
2575+ i->i_mtime.tv_sec = inodep->mtime;
2576+ i->i_atime.tv_sec = inodep->mtime;
2577+ i->i_ctime.tv_sec = inodep->mtime;
2578+ i->i_blocks = ((i->i_size - 1) >> 9) + 1;
2579+ SQUASHFS_I(i)->u.s1.fragment_start_block = frag_blk;
2580+ SQUASHFS_I(i)->u.s1.fragment_size = frag_size;
2581+ SQUASHFS_I(i)->u.s1.fragment_offset = inodep->offset;
2582+ SQUASHFS_I(i)->start_block = inodep->start_block;
2583+ SQUASHFS_I(i)->u.s1.block_list_start = next_block;
2584+ SQUASHFS_I(i)->offset = next_offset;
2585+ i->i_data.a_ops = &squashfs_aops;
2586+
2587+ TRACE("File inode %x:%x, start_block %x, "
2588+ "block_list_start %llx, offset %x\n",
2589+ SQUASHFS_INODE_BLK(inode), offset,
2590+ inodep->start_block, next_block,
2591+ next_offset);
2592+ break;
2593+ }
2594+ case SQUASHFS_DIR_TYPE: {
2595+ struct squashfs_dir_inode_header_2 *inodep = &id.dir;
2596+ struct squashfs_dir_inode_header_2 *sinodep = &sid.dir;
2597+
2598+ if (msblk->swap) {
2599+ if (!squashfs_get_cached_block(s, (char *)
2600+ sinodep, block, offset,
2601+ sizeof(*sinodep), &next_block,
2602+ &next_offset))
2603+ goto failed_read;
2604+ SQUASHFS_SWAP_DIR_INODE_HEADER_2(inodep, sinodep);
2605+ } else
2606+ if (!squashfs_get_cached_block(s, (char *)
2607+ inodep, block, offset,
2608+ sizeof(*inodep), &next_block,
2609+ &next_offset))
2610+ goto failed_read;
2611+
2612+ i->i_size = inodep->file_size;
2613+ i->i_op = &squashfs_dir_inode_ops_2;
2614+ i->i_fop = &squashfs_dir_ops_2;
2615+ i->i_mode |= S_IFDIR;
2616+ i->i_mtime.tv_sec = inodep->mtime;
2617+ i->i_atime.tv_sec = inodep->mtime;
2618+ i->i_ctime.tv_sec = inodep->mtime;
2619+ SQUASHFS_I(i)->start_block = inodep->start_block;
2620+ SQUASHFS_I(i)->offset = inodep->offset;
2621+ SQUASHFS_I(i)->u.s2.directory_index_count = 0;
2622+ SQUASHFS_I(i)->u.s2.parent_inode = 0;
2623+
2624+ TRACE("Directory inode %x:%x, start_block %x, offset "
2625+ "%x\n", SQUASHFS_INODE_BLK(inode),
2626+ offset, inodep->start_block,
2627+ inodep->offset);
2628+ break;
2629+ }
2630+ case SQUASHFS_LDIR_TYPE: {
2631+ struct squashfs_ldir_inode_header_2 *inodep = &id.ldir;
2632+ struct squashfs_ldir_inode_header_2 *sinodep = &sid.ldir;
2633+
2634+ if (msblk->swap) {
2635+ if (!squashfs_get_cached_block(s, (char *)
2636+ sinodep, block, offset,
2637+ sizeof(*sinodep), &next_block,
2638+ &next_offset))
2639+ goto failed_read;
2640+ SQUASHFS_SWAP_LDIR_INODE_HEADER_2(inodep,
2641+ sinodep);
2642+ } else
2643+ if (!squashfs_get_cached_block(s, (char *)
2644+ inodep, block, offset,
2645+ sizeof(*inodep), &next_block,
2646+ &next_offset))
2647+ goto failed_read;
2648+
2649+ i->i_size = inodep->file_size;
2650+ i->i_op = &squashfs_dir_inode_ops_2;
2651+ i->i_fop = &squashfs_dir_ops_2;
2652+ i->i_mode |= S_IFDIR;
2653+ i->i_mtime.tv_sec = inodep->mtime;
2654+ i->i_atime.tv_sec = inodep->mtime;
2655+ i->i_ctime.tv_sec = inodep->mtime;
2656+ SQUASHFS_I(i)->start_block = inodep->start_block;
2657+ SQUASHFS_I(i)->offset = inodep->offset;
2658+ SQUASHFS_I(i)->u.s2.directory_index_start = next_block;
2659+ SQUASHFS_I(i)->u.s2.directory_index_offset =
2660+ next_offset;
2661+ SQUASHFS_I(i)->u.s2.directory_index_count =
2662+ inodep->i_count;
2663+ SQUASHFS_I(i)->u.s2.parent_inode = 0;
2664+
2665+ TRACE("Long directory inode %x:%x, start_block %x, "
2666+ "offset %x\n",
2667+ SQUASHFS_INODE_BLK(inode), offset,
2668+ inodep->start_block, inodep->offset);
2669+ break;
2670+ }
2671+ case SQUASHFS_SYMLINK_TYPE: {
2672+ struct squashfs_symlink_inode_header_2 *inodep =
2673+ &id.symlink;
2674+ struct squashfs_symlink_inode_header_2 *sinodep =
2675+ &sid.symlink;
2676+
2677+ if (msblk->swap) {
2678+ if (!squashfs_get_cached_block(s, (char *)
2679+ sinodep, block, offset,
2680+ sizeof(*sinodep), &next_block,
2681+ &next_offset))
2682+ goto failed_read;
2683+ SQUASHFS_SWAP_SYMLINK_INODE_HEADER_2(inodep,
2684+ sinodep);
2685+ } else
2686+ if (!squashfs_get_cached_block(s, (char *)
2687+ inodep, block, offset,
2688+ sizeof(*inodep), &next_block,
2689+ &next_offset))
2690+ goto failed_read;
2691+
2692+ i->i_size = inodep->symlink_size;
2693+ i->i_op = &page_symlink_inode_operations;
2694+ i->i_data.a_ops = &squashfs_symlink_aops;
2695+ i->i_mode |= S_IFLNK;
2696+ SQUASHFS_I(i)->start_block = next_block;
2697+ SQUASHFS_I(i)->offset = next_offset;
2698+
2699+ TRACE("Symbolic link inode %x:%x, start_block %llx, "
2700+ "offset %x\n",
2701+ SQUASHFS_INODE_BLK(inode), offset,
2702+ next_block, next_offset);
2703+ break;
2704+ }
2705+ case SQUASHFS_BLKDEV_TYPE:
2706+ case SQUASHFS_CHRDEV_TYPE: {
2707+ struct squashfs_dev_inode_header_2 *inodep = &id.dev;
2708+ struct squashfs_dev_inode_header_2 *sinodep = &sid.dev;
2709+
2710+ if (msblk->swap) {
2711+ if (!squashfs_get_cached_block(s, (char *)
2712+ sinodep, block, offset,
2713+ sizeof(*sinodep), &next_block,
2714+ &next_offset))
2715+ goto failed_read;
2716+ SQUASHFS_SWAP_DEV_INODE_HEADER_2(inodep, sinodep);
2717+ } else
2718+ if (!squashfs_get_cached_block(s, (char *)
2719+ inodep, block, offset,
2720+ sizeof(*inodep), &next_block,
2721+ &next_offset))
2722+ goto failed_read;
2723+
2724+ i->i_mode |= (inodeb->inode_type ==
2725+ SQUASHFS_CHRDEV_TYPE) ? S_IFCHR :
2726+ S_IFBLK;
2727+ init_special_inode(i, i->i_mode,
2728+ old_decode_dev(inodep->rdev));
2729+
2730+ TRACE("Device inode %x:%x, rdev %x\n",
2731+ SQUASHFS_INODE_BLK(inode), offset,
2732+ inodep->rdev);
2733+ break;
2734+ }
2735+ case SQUASHFS_FIFO_TYPE:
2736+ case SQUASHFS_SOCKET_TYPE: {
2737+
2738+ i->i_mode |= (inodeb->inode_type == SQUASHFS_FIFO_TYPE)
2739+ ? S_IFIFO : S_IFSOCK;
2740+ init_special_inode(i, i->i_mode, 0);
2741+ break;
2742+ }
2743+ default:
2744+ ERROR("Unknown inode type %d in squashfs_iget!\n",
2745+ inodeb->inode_type);
2746+ goto failed_read1;
2747+ }
2748+
2749+ return 1;
2750+
2751+failed_read:
2752+ ERROR("Unable to read inode [%x:%x]\n", block, offset);
2753+
2754+failed_read1:
2755+ return 0;
2756+}
2757+
2758+
2759+static int get_dir_index_using_offset(struct super_block *s, long long
2760+ *next_block, unsigned int *next_offset,
2761+ long long index_start,
2762+ unsigned int index_offset, int i_count,
2763+ long long f_pos)
2764+{
2765+ struct squashfs_sb_info *msblk = s->s_fs_info;
2766+ struct squashfs_super_block *sblk = &msblk->sblk;
2767+ int i, length = 0;
2768+ struct squashfs_dir_index_2 index;
2769+
2770+ TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %d\n",
2771+ i_count, (unsigned int) f_pos);
2772+
2773+ if (f_pos == 0)
2774+ goto finish;
2775+
2776+ for (i = 0; i < i_count; i++) {
2777+ if (msblk->swap) {
2778+ struct squashfs_dir_index_2 sindex;
2779+ squashfs_get_cached_block(s, (char *) &sindex,
2780+ index_start, index_offset,
2781+ sizeof(sindex), &index_start,
2782+ &index_offset);
2783+ SQUASHFS_SWAP_DIR_INDEX_2(&index, &sindex);
2784+ } else
2785+ squashfs_get_cached_block(s, (char *) &index,
2786+ index_start, index_offset,
2787+ sizeof(index), &index_start,
2788+ &index_offset);
2789+
2790+ if (index.index > f_pos)
2791+ break;
2792+
2793+ squashfs_get_cached_block(s, NULL, index_start, index_offset,
2794+ index.size + 1, &index_start,
2795+ &index_offset);
2796+
2797+ length = index.index;
2798+ *next_block = index.start_block + sblk->directory_table_start;
2799+ }
2800+
2801+ *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
2802+
2803+finish:
2804+ return length;
2805+}
2806+
2807+
2808+static int get_dir_index_using_name(struct super_block *s, long long
2809+ *next_block, unsigned int *next_offset,
2810+ long long index_start,
2811+ unsigned int index_offset, int i_count,
2812+ const char *name, int size)
2813+{
2814+ struct squashfs_sb_info *msblk = s->s_fs_info;
2815+ struct squashfs_super_block *sblk = &msblk->sblk;
2816+ int i, length = 0;
2817+ struct squashfs_dir_index_2 *index;
2818+ char *str;
2819+
2820+ TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
2821+
2822+ if (!(str = kmalloc(sizeof(struct squashfs_dir_index) +
2823+ (SQUASHFS_NAME_LEN + 1) * 2, GFP_KERNEL))) {
2824+ ERROR("Failed to allocate squashfs_dir_index\n");
2825+ goto failure;
2826+ }
2827+
2828+ index = (struct squashfs_dir_index_2 *) (str + SQUASHFS_NAME_LEN + 1);
2829+ strncpy(str, name, size);
2830+ str[size] = '\0';
2831+
2832+ for (i = 0; i < i_count; i++) {
2833+ if (msblk->swap) {
2834+ struct squashfs_dir_index_2 sindex;
2835+ squashfs_get_cached_block(s, (char *) &sindex,
2836+ index_start, index_offset,
2837+ sizeof(sindex), &index_start,
2838+ &index_offset);
2839+ SQUASHFS_SWAP_DIR_INDEX_2(index, &sindex);
2840+ } else
2841+ squashfs_get_cached_block(s, (char *) index,
2842+ index_start, index_offset,
2843+ sizeof(struct squashfs_dir_index_2),
2844+ &index_start, &index_offset);
2845+
2846+ squashfs_get_cached_block(s, index->name, index_start,
2847+ index_offset, index->size + 1,
2848+ &index_start, &index_offset);
2849+
2850+ index->name[index->size + 1] = '\0';
2851+
2852+ if (strcmp(index->name, str) > 0)
2853+ break;
2854+
2855+ length = index->index;
2856+ *next_block = index->start_block + sblk->directory_table_start;
2857+ }
2858+
2859+ *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
2860+ kfree(str);
2861+failure:
2862+ return length;
2863+}
2864+
2865+
2866+static int squashfs_readdir_2(struct file *file, void *dirent, filldir_t filldir)
2867+{
2868+ struct inode *i = file->f_dentry->d_inode;
2869+ struct squashfs_sb_info *msblk = i->i_sb->s_fs_info;
2870+ struct squashfs_super_block *sblk = &msblk->sblk;
2871+ long long next_block = SQUASHFS_I(i)->start_block +
2872+ sblk->directory_table_start;
2873+ int next_offset = SQUASHFS_I(i)->offset, length = 0,
2874+ dir_count;
2875+ struct squashfs_dir_header_2 dirh;
2876+ struct squashfs_dir_entry_2 *dire;
2877+
2878+ TRACE("Entered squashfs_readdir_2 [%llx:%x]\n", next_block, next_offset);
2879+
2880+ if (!(dire = kmalloc(sizeof(struct squashfs_dir_entry) +
2881+ SQUASHFS_NAME_LEN + 1, GFP_KERNEL))) {
2882+ ERROR("Failed to allocate squashfs_dir_entry\n");
2883+ goto finish;
2884+ }
2885+
2886+ length = get_dir_index_using_offset(i->i_sb, &next_block, &next_offset,
2887+ SQUASHFS_I(i)->u.s2.directory_index_start,
2888+ SQUASHFS_I(i)->u.s2.directory_index_offset,
2889+ SQUASHFS_I(i)->u.s2.directory_index_count,
2890+ file->f_pos);
2891+
2892+ while (length < i_size_read(i)) {
2893+ /* read directory header */
2894+ if (msblk->swap) {
2895+ struct squashfs_dir_header_2 sdirh;
2896+
2897+ if (!squashfs_get_cached_block(i->i_sb, (char *) &sdirh,
2898+ next_block, next_offset, sizeof(sdirh),
2899+ &next_block, &next_offset))
2900+ goto failed_read;
2901+
2902+ length += sizeof(sdirh);
2903+ SQUASHFS_SWAP_DIR_HEADER_2(&dirh, &sdirh);
2904+ } else {
2905+ if (!squashfs_get_cached_block(i->i_sb, (char *) &dirh,
2906+ next_block, next_offset, sizeof(dirh),
2907+ &next_block, &next_offset))
2908+ goto failed_read;
2909+
2910+ length += sizeof(dirh);
2911+ }
2912+
2913+ dir_count = dirh.count + 1;
2914+ while (dir_count--) {
2915+ if (msblk->swap) {
2916+ struct squashfs_dir_entry_2 sdire;
2917+ if (!squashfs_get_cached_block(i->i_sb, (char *)
2918+ &sdire, next_block, next_offset,
2919+ sizeof(sdire), &next_block,
2920+ &next_offset))
2921+ goto failed_read;
2922+
2923+ length += sizeof(sdire);
2924+ SQUASHFS_SWAP_DIR_ENTRY_2(dire, &sdire);
2925+ } else {
2926+ if (!squashfs_get_cached_block(i->i_sb, (char *)
2927+ dire, next_block, next_offset,
2928+ sizeof(*dire), &next_block,
2929+ &next_offset))
2930+ goto failed_read;
2931+
2932+ length += sizeof(*dire);
2933+ }
2934+
2935+ if (!squashfs_get_cached_block(i->i_sb, dire->name,
2936+ next_block, next_offset,
2937+ dire->size + 1, &next_block,
2938+ &next_offset))
2939+ goto failed_read;
2940+
2941+ length += dire->size + 1;
2942+
2943+ if (file->f_pos >= length)
2944+ continue;
2945+
2946+ dire->name[dire->size + 1] = '\0';
2947+
2948+ TRACE("Calling filldir(%x, %s, %d, %d, %x:%x, %d)\n",
2949+ (unsigned int) dirent, dire->name,
2950+ dire->size + 1, (int) file->f_pos,
2951+ dirh.start_block, dire->offset,
2952+ squashfs_filetype_table[dire->type]);
2953+
2954+ if (filldir(dirent, dire->name, dire->size + 1,
2955+ file->f_pos, SQUASHFS_MK_VFS_INODE(
2956+ dirh.start_block, dire->offset),
2957+ squashfs_filetype_table[dire->type])
2958+ < 0) {
2959+ TRACE("Filldir returned less than 0\n");
2960+ goto finish;
2961+ }
2962+ file->f_pos = length;
2963+ }
2964+ }
2965+
2966+finish:
2967+ kfree(dire);
2968+ return 0;
2969+
2970+failed_read:
2971+ ERROR("Unable to read directory block [%llx:%x]\n", next_block,
2972+ next_offset);
2973+ kfree(dire);
2974+ return 0;
2975+}
2976+
2977+
2978+static struct dentry *squashfs_lookup_2(struct inode *i, struct dentry *dentry,
2979+ struct nameidata *nd)
2980+{
2981+ const unsigned char *name = dentry->d_name.name;
2982+ int len = dentry->d_name.len;
2983+ struct inode *inode = NULL;
2984+ struct squashfs_sb_info *msblk = i->i_sb->s_fs_info;
2985+ struct squashfs_super_block *sblk = &msblk->sblk;
2986+ long long next_block = SQUASHFS_I(i)->start_block +
2987+ sblk->directory_table_start;
2988+ int next_offset = SQUASHFS_I(i)->offset, length = 0,
2989+ dir_count;
2990+ struct squashfs_dir_header_2 dirh;
2991+ struct squashfs_dir_entry_2 *dire;
2992+ int sorted = sblk->s_major == 2 && sblk->s_minor >= 1;
2993+
2994+ TRACE("Entered squashfs_lookup_2 [%llx:%x]\n", next_block, next_offset);
2995+
2996+ if (!(dire = kmalloc(sizeof(struct squashfs_dir_entry) +
2997+ SQUASHFS_NAME_LEN + 1, GFP_KERNEL))) {
2998+ ERROR("Failed to allocate squashfs_dir_entry\n");
2999+ goto exit_loop;
3000+ }
3001+
3002+ if (len > SQUASHFS_NAME_LEN)
3003+ goto exit_loop;
3004+
3005+ length = get_dir_index_using_name(i->i_sb, &next_block, &next_offset,
3006+ SQUASHFS_I(i)->u.s2.directory_index_start,
3007+ SQUASHFS_I(i)->u.s2.directory_index_offset,
3008+ SQUASHFS_I(i)->u.s2.directory_index_count, name,
3009+ len);
3010+
3011+ while (length < i_size_read(i)) {
3012+ /* read directory header */
3013+ if (msblk->swap) {
3014+ struct squashfs_dir_header_2 sdirh;
3015+ if (!squashfs_get_cached_block(i->i_sb, (char *) &sdirh,
3016+ next_block, next_offset, sizeof(sdirh),
3017+ &next_block, &next_offset))
3018+ goto failed_read;
3019+
3020+ length += sizeof(sdirh);
3021+ SQUASHFS_SWAP_DIR_HEADER_2(&dirh, &sdirh);
3022+ } else {
3023+ if (!squashfs_get_cached_block(i->i_sb, (char *) &dirh,
3024+ next_block, next_offset, sizeof(dirh),
3025+ &next_block, &next_offset))
3026+ goto failed_read;
3027+
3028+ length += sizeof(dirh);
3029+ }
3030+
3031+ dir_count = dirh.count + 1;
3032+ while (dir_count--) {
3033+ if (msblk->swap) {
3034+ struct squashfs_dir_entry_2 sdire;
3035+ if (!squashfs_get_cached_block(i->i_sb, (char *)
3036+ &sdire, next_block,next_offset,
3037+ sizeof(sdire), &next_block,
3038+ &next_offset))
3039+ goto failed_read;
3040+
3041+ length += sizeof(sdire);
3042+ SQUASHFS_SWAP_DIR_ENTRY_2(dire, &sdire);
3043+ } else {
3044+ if (!squashfs_get_cached_block(i->i_sb, (char *)
3045+ dire, next_block,next_offset,
3046+ sizeof(*dire), &next_block,
3047+ &next_offset))
3048+ goto failed_read;
3049+
3050+ length += sizeof(*dire);
3051+ }
3052+
3053+ if (!squashfs_get_cached_block(i->i_sb, dire->name,
3054+ next_block, next_offset, dire->size + 1,
3055+ &next_block, &next_offset))
3056+ goto failed_read;
3057+
3058+ length += dire->size + 1;
3059+
3060+ if (sorted && name[0] < dire->name[0])
3061+ goto exit_loop;
3062+
3063+ if ((len == dire->size + 1) && !strncmp(name,
3064+ dire->name, len)) {
3065+ squashfs_inode_t ino =
3066+ SQUASHFS_MKINODE(dirh.start_block,
3067+ dire->offset);
3068+ unsigned int inode_number = SQUASHFS_MK_VFS_INODE(dirh.start_block,
3069+ dire->offset);
3070+
3071+ TRACE("calling squashfs_iget for directory "
3072+ "entry %s, inode %x:%x, %lld\n", name,
3073+ dirh.start_block, dire->offset, ino);
3074+
3075+ inode = squashfs_iget(i->i_sb, ino, inode_number);
3076+
3077+ goto exit_loop;
3078+ }
3079+ }
3080+ }
3081+
3082+exit_loop:
3083+ kfree(dire);
3084+ d_add(dentry, inode);
3085+ return ERR_PTR(0);
3086+
3087+failed_read:
3088+ ERROR("Unable to read directory block [%llx:%x]\n", next_block,
3089+ next_offset);
3090+ goto exit_loop;
3091+}
3092+
3093+
3094+int squashfs_2_0_supported(struct squashfs_sb_info *msblk)
3095+{
3096+ struct squashfs_super_block *sblk = &msblk->sblk;
3097+
3098+ msblk->read_inode = squashfs_read_inode_2;
3099+ msblk->read_fragment_index_table = read_fragment_index_table_2;
3100+
3101+ sblk->bytes_used = sblk->bytes_used_2;
3102+ sblk->uid_start = sblk->uid_start_2;
3103+ sblk->guid_start = sblk->guid_start_2;
3104+ sblk->inode_table_start = sblk->inode_table_start_2;
3105+ sblk->directory_table_start = sblk->directory_table_start_2;
3106+ sblk->fragment_table_start = sblk->fragment_table_start_2;
3107+
3108+ return 1;
3109+}
3110--- /dev/null
3111+++ b/include/linux/squashfs_fs.h
3112@@ -0,0 +1,935 @@
3113+#ifndef SQUASHFS_FS
3114+#define SQUASHFS_FS
3115+
3116+/*
3117+ * Squashfs
3118+ *
3119+ * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
3120+ * Phillip Lougher <phillip@lougher.demon.co.uk>
3121+ *
3122+ * This program is free software; you can redistribute it and/or
3123+ * modify it under the terms of the GNU General Public License
3124+ * as published by the Free Software Foundation; either version 2,
3125+ * or (at your option) any later version.
3126+ *
3127+ * This program is distributed in the hope that it will be useful,
3128+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
3129+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3130+ * GNU General Public License for more details.
3131+ *
3132+ * You should have received a copy of the GNU General Public License
3133+ * along with this program; if not, write to the Free Software
3134+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
3135+ *
3136+ * squashfs_fs.h
3137+ */
3138+
3139+#ifndef CONFIG_SQUASHFS_2_0_COMPATIBILITY
3140+#define CONFIG_SQUASHFS_2_0_COMPATIBILITY
3141+#endif
3142+
3143+#define SQUASHFS_CACHED_FRAGMENTS CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE
3144+#define SQUASHFS_MAJOR 3
3145+#define SQUASHFS_MINOR 1
3146+#define SQUASHFS_MAGIC 0x73717368
3147+#define SQUASHFS_MAGIC_SWAP 0x68737173
3148+#define SQUASHFS_START 0
3149+
3150+/* size of metadata (inode and directory) blocks */
3151+#define SQUASHFS_METADATA_SIZE 8192
3152+#define SQUASHFS_METADATA_LOG 13
3153+
3154+/* default size of data blocks */
3155+#define SQUASHFS_FILE_SIZE 131072
3156+#define SQUASHFS_FILE_LOG 17
3157+
3158+#define SQUASHFS_FILE_MAX_SIZE 1048576
3159+
3160+/* Max number of uids and gids */
3161+#define SQUASHFS_UIDS 256
3162+#define SQUASHFS_GUIDS 255
3163+
3164+/* Max length of filename (not 255) */
3165+#define SQUASHFS_NAME_LEN 256
3166+
3167+#define SQUASHFS_INVALID ((long long) 0xffffffffffff)
3168+#define SQUASHFS_INVALID_FRAG ((unsigned int) 0xffffffff)
3169+#define SQUASHFS_INVALID_BLK ((long long) -1)
3170+#define SQUASHFS_USED_BLK ((long long) -2)
3171+
3172+/* Filesystem flags */
3173+#define SQUASHFS_NOI 0
3174+#define SQUASHFS_NOD 1
3175+#define SQUASHFS_CHECK 2
3176+#define SQUASHFS_NOF 3
3177+#define SQUASHFS_NO_FRAG 4
3178+#define SQUASHFS_ALWAYS_FRAG 5
3179+#define SQUASHFS_DUPLICATE 6
3180+#define SQUASHFS_EXPORT 7
3181+
3182+#define SQUASHFS_BIT(flag, bit) ((flag >> bit) & 1)
3183+
3184+#define SQUASHFS_UNCOMPRESSED_INODES(flags) SQUASHFS_BIT(flags, \
3185+ SQUASHFS_NOI)
3186+
3187+#define SQUASHFS_UNCOMPRESSED_DATA(flags) SQUASHFS_BIT(flags, \
3188+ SQUASHFS_NOD)
3189+
3190+#define SQUASHFS_UNCOMPRESSED_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
3191+ SQUASHFS_NOF)
3192+
3193+#define SQUASHFS_NO_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
3194+ SQUASHFS_NO_FRAG)
3195+
3196+#define SQUASHFS_ALWAYS_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
3197+ SQUASHFS_ALWAYS_FRAG)
3198+
3199+#define SQUASHFS_DUPLICATES(flags) SQUASHFS_BIT(flags, \
3200+ SQUASHFS_DUPLICATE)
3201+
3202+#define SQUASHFS_EXPORTABLE(flags) SQUASHFS_BIT(flags, \
3203+ SQUASHFS_EXPORT)
3204+
3205+#define SQUASHFS_CHECK_DATA(flags) SQUASHFS_BIT(flags, \
3206+ SQUASHFS_CHECK)
3207+
3208+#define SQUASHFS_MKFLAGS(noi, nod, check_data, nof, no_frag, always_frag, \
3209+ duplicate_checking, exportable) (noi | (nod << 1) | (check_data << 2) \
3210+ | (nof << 3) | (no_frag << 4) | (always_frag << 5) | \
3211+ (duplicate_checking << 6) | (exportable << 7))
3212+
3213+/* Max number of types and file types */
3214+#define SQUASHFS_DIR_TYPE 1
3215+#define SQUASHFS_FILE_TYPE 2
3216+#define SQUASHFS_SYMLINK_TYPE 3
3217+#define SQUASHFS_BLKDEV_TYPE 4
3218+#define SQUASHFS_CHRDEV_TYPE 5
3219+#define SQUASHFS_FIFO_TYPE 6
3220+#define SQUASHFS_SOCKET_TYPE 7
3221+#define SQUASHFS_LDIR_TYPE 8
3222+#define SQUASHFS_LREG_TYPE 9
3223+
3224+/* 1.0 filesystem type definitions */
3225+#define SQUASHFS_TYPES 5
3226+#define SQUASHFS_IPC_TYPE 0
3227+
3228+/* Flag whether block is compressed or uncompressed, bit is set if block is
3229+ * uncompressed */
3230+#define SQUASHFS_COMPRESSED_BIT (1 << 15)
3231+
3232+#define SQUASHFS_COMPRESSED_SIZE(B) (((B) & ~SQUASHFS_COMPRESSED_BIT) ? \
3233+ (B) & ~SQUASHFS_COMPRESSED_BIT : SQUASHFS_COMPRESSED_BIT)
3234+
3235+#define SQUASHFS_COMPRESSED(B) (!((B) & SQUASHFS_COMPRESSED_BIT))
3236+
3237+#define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24)
3238+
3239+#define SQUASHFS_COMPRESSED_SIZE_BLOCK(B) ((B) & \
3240+ ~SQUASHFS_COMPRESSED_BIT_BLOCK)
3241+
3242+#define SQUASHFS_COMPRESSED_BLOCK(B) (!((B) & SQUASHFS_COMPRESSED_BIT_BLOCK))
3243+
3244+/*
3245+ * Inode number ops. Inodes consist of a compressed block number, and an
3246+ * uncompressed offset within that block
3247+ */
3248+#define SQUASHFS_INODE_BLK(a) ((unsigned int) ((a) >> 16))
3249+
3250+#define SQUASHFS_INODE_OFFSET(a) ((unsigned int) ((a) & 0xffff))
3251+
3252+#define SQUASHFS_MKINODE(A, B) ((squashfs_inode_t)(((squashfs_inode_t) (A)\
3253+ << 16) + (B)))
3254+
3255+/* Compute 32 bit VFS inode number from squashfs inode number */
3256+#define SQUASHFS_MK_VFS_INODE(a, b) ((unsigned int) (((a) << 8) + \
3257+ ((b) >> 2) + 1))
3258+/* XXX */
3259+
3260+/* Translate between VFS mode and squashfs mode */
3261+#define SQUASHFS_MODE(a) ((a) & 0xfff)
3262+
3263+/* fragment and fragment table defines */
3264+#define SQUASHFS_FRAGMENT_BYTES(A) ((A) * sizeof(struct squashfs_fragment_entry))
3265+
3266+#define SQUASHFS_FRAGMENT_INDEX(A) (SQUASHFS_FRAGMENT_BYTES(A) / \
3267+ SQUASHFS_METADATA_SIZE)
3268+
3269+#define SQUASHFS_FRAGMENT_INDEX_OFFSET(A) (SQUASHFS_FRAGMENT_BYTES(A) % \
3270+ SQUASHFS_METADATA_SIZE)
3271+
3272+#define SQUASHFS_FRAGMENT_INDEXES(A) ((SQUASHFS_FRAGMENT_BYTES(A) + \
3273+ SQUASHFS_METADATA_SIZE - 1) / \
3274+ SQUASHFS_METADATA_SIZE)
3275+
3276+#define SQUASHFS_FRAGMENT_INDEX_BYTES(A) (SQUASHFS_FRAGMENT_INDEXES(A) *\
3277+ sizeof(long long))
3278+
3279+/* inode lookup table defines */
3280+#define SQUASHFS_LOOKUP_BYTES(A) ((A) * sizeof(squashfs_inode_t))
3281+
3282+#define SQUASHFS_LOOKUP_BLOCK(A) (SQUASHFS_LOOKUP_BYTES(A) / \
3283+ SQUASHFS_METADATA_SIZE)
3284+
3285+#define SQUASHFS_LOOKUP_BLOCK_OFFSET(A) (SQUASHFS_LOOKUP_BYTES(A) % \
3286+ SQUASHFS_METADATA_SIZE)
3287+
3288+#define SQUASHFS_LOOKUP_BLOCKS(A) ((SQUASHFS_LOOKUP_BYTES(A) + \
3289+ SQUASHFS_METADATA_SIZE - 1) / \
3290+ SQUASHFS_METADATA_SIZE)
3291+
3292+#define SQUASHFS_LOOKUP_BLOCK_BYTES(A) (SQUASHFS_LOOKUP_BLOCKS(A) *\
3293+ sizeof(long long))
3294+
3295+/* cached data constants for filesystem */
3296+#define SQUASHFS_CACHED_BLKS 8
3297+
3298+#define SQUASHFS_MAX_FILE_SIZE_LOG 64
3299+
3300+#define SQUASHFS_MAX_FILE_SIZE ((long long) 1 << \
3301+ (SQUASHFS_MAX_FILE_SIZE_LOG - 2))
3302+
3303+#define SQUASHFS_MARKER_BYTE 0xff
3304+
3305+/* meta index cache */
3306+#define SQUASHFS_META_INDEXES (SQUASHFS_METADATA_SIZE / sizeof(unsigned int))
3307+#define SQUASHFS_META_ENTRIES 31
3308+#define SQUASHFS_META_NUMBER 8
3309+#define SQUASHFS_SLOTS 4
3310+
3311+struct meta_entry {
3312+ long long data_block;
3313+ unsigned int index_block;
3314+ unsigned short offset;
3315+ unsigned short pad;
3316+};
3317+
3318+struct meta_index {
3319+ unsigned int inode_number;
3320+ unsigned int offset;
3321+ unsigned short entries;
3322+ unsigned short skip;
3323+ unsigned short locked;
3324+ unsigned short pad;
3325+ struct meta_entry meta_entry[SQUASHFS_META_ENTRIES];
3326+};
3327+
3328+
3329+/*
3330+ * definitions for structures on disk
3331+ */
3332+
3333+typedef long long squashfs_block_t;
3334+typedef long long squashfs_inode_t;
3335+
3336+struct squashfs_super_block {
3337+ unsigned int s_magic;
3338+ unsigned int inodes;
3339+ unsigned int bytes_used_2;
3340+ unsigned int uid_start_2;
3341+ unsigned int guid_start_2;
3342+ unsigned int inode_table_start_2;
3343+ unsigned int directory_table_start_2;
3344+ unsigned int s_major:16;
3345+ unsigned int s_minor:16;
3346+ unsigned int block_size_1:16;
3347+ unsigned int block_log:16;
3348+ unsigned int flags:8;
3349+ unsigned int no_uids:8;
3350+ unsigned int no_guids:8;
3351+ unsigned int mkfs_time /* time of filesystem creation */;
3352+ squashfs_inode_t root_inode;
3353+ unsigned int block_size;
3354+ unsigned int fragments;
3355+ unsigned int fragment_table_start_2;
3356+ long long bytes_used;
3357+ long long uid_start;
3358+ long long guid_start;
3359+ long long inode_table_start;
3360+ long long directory_table_start;
3361+ long long fragment_table_start;
3362+ long long lookup_table_start;
3363+} __attribute__ ((packed));
3364+
3365+struct squashfs_dir_index {
3366+ unsigned int index;
3367+ unsigned int start_block;
3368+ unsigned char size;
3369+ unsigned char name[0];
3370+} __attribute__ ((packed));
3371+
3372+#define SQUASHFS_BASE_INODE_HEADER \
3373+ unsigned int inode_type:4; \
3374+ unsigned int mode:12; \
3375+ unsigned int uid:8; \
3376+ unsigned int guid:8; \
3377+ unsigned int mtime; \
3378+ unsigned int inode_number;
3379+
3380+struct squashfs_base_inode_header {
3381+ SQUASHFS_BASE_INODE_HEADER;
3382+} __attribute__ ((packed));
3383+
3384+struct squashfs_ipc_inode_header {
3385+ SQUASHFS_BASE_INODE_HEADER;
3386+ unsigned int nlink;
3387+} __attribute__ ((packed));
3388+
3389+struct squashfs_dev_inode_header {
3390+ SQUASHFS_BASE_INODE_HEADER;
3391+ unsigned int nlink;
3392+ unsigned short rdev;
3393+} __attribute__ ((packed));
3394+
3395+struct squashfs_symlink_inode_header {
3396+ SQUASHFS_BASE_INODE_HEADER;
3397+ unsigned int nlink;
3398+ unsigned short symlink_size;
3399+ char symlink[0];
3400+} __attribute__ ((packed));
3401+
3402+struct squashfs_reg_inode_header {
3403+ SQUASHFS_BASE_INODE_HEADER;
3404+ squashfs_block_t start_block;
3405+ unsigned int fragment;
3406+ unsigned int offset;
3407+ unsigned int file_size;
3408+ unsigned short block_list[0];
3409+} __attribute__ ((packed));
3410+
3411+struct squashfs_lreg_inode_header {
3412+ SQUASHFS_BASE_INODE_HEADER;
3413+ unsigned int nlink;
3414+ squashfs_block_t start_block;
3415+ unsigned int fragment;
3416+ unsigned int offset;
3417+ long long file_size;
3418+ unsigned short block_list[0];
3419+} __attribute__ ((packed));
3420+
3421+struct squashfs_dir_inode_header {
3422+ SQUASHFS_BASE_INODE_HEADER;
3423+ unsigned int nlink;
3424+ unsigned int file_size:19;
3425+ unsigned int offset:13;
3426+ unsigned int start_block;
3427+ unsigned int parent_inode;
3428+} __attribute__ ((packed));
3429+
3430+struct squashfs_ldir_inode_header {
3431+ SQUASHFS_BASE_INODE_HEADER;
3432+ unsigned int nlink;
3433+ unsigned int file_size:27;
3434+ unsigned int offset:13;
3435+ unsigned int start_block;
3436+ unsigned int i_count:16;
3437+ unsigned int parent_inode;
3438+ struct squashfs_dir_index index[0];
3439+} __attribute__ ((packed));
3440+
3441+union squashfs_inode_header {
3442+ struct squashfs_base_inode_header base;
3443+ struct squashfs_dev_inode_header dev;
3444+ struct squashfs_symlink_inode_header symlink;
3445+ struct squashfs_reg_inode_header reg;
3446+ struct squashfs_lreg_inode_header lreg;
3447+ struct squashfs_dir_inode_header dir;
3448+ struct squashfs_ldir_inode_header ldir;
3449+ struct squashfs_ipc_inode_header ipc;
3450+};
3451+
3452+struct squashfs_dir_entry {
3453+ unsigned int offset:13;
3454+ unsigned int type:3;
3455+ unsigned int size:8;
3456+ int inode_number:16;
3457+ char name[0];
3458+} __attribute__ ((packed));
3459+
3460+struct squashfs_dir_header {
3461+ unsigned int count:8;
3462+ unsigned int start_block;
3463+ unsigned int inode_number;
3464+} __attribute__ ((packed));
3465+
3466+struct squashfs_fragment_entry {
3467+ long long start_block;
3468+ unsigned int size;
3469+ unsigned int unused;
3470+} __attribute__ ((packed));
3471+
3472+extern int squashfs_uncompress_block(void *d, int dstlen, void *s, int srclen);
3473+extern int squashfs_uncompress_init(void);
3474+extern int squashfs_uncompress_exit(void);
3475+
3476+/*
3477+ * macros to convert each packed bitfield structure from little endian to big
3478+ * endian and vice versa. These are needed when creating or using a filesystem
3479+ * on a machine with different byte ordering to the target architecture.
3480+ *
3481+ */
3482+
3483+#define SQUASHFS_SWAP_START \
3484+ int bits;\
3485+ int b_pos;\
3486+ unsigned long long val;\
3487+ unsigned char *s;\
3488+ unsigned char *d;
3489+
3490+#define SQUASHFS_SWAP_SUPER_BLOCK(s, d) {\
3491+ SQUASHFS_SWAP_START\
3492+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_super_block));\
3493+ SQUASHFS_SWAP((s)->s_magic, d, 0, 32);\
3494+ SQUASHFS_SWAP((s)->inodes, d, 32, 32);\
3495+ SQUASHFS_SWAP((s)->bytes_used_2, d, 64, 32);\
3496+ SQUASHFS_SWAP((s)->uid_start_2, d, 96, 32);\
3497+ SQUASHFS_SWAP((s)->guid_start_2, d, 128, 32);\
3498+ SQUASHFS_SWAP((s)->inode_table_start_2, d, 160, 32);\
3499+ SQUASHFS_SWAP((s)->directory_table_start_2, d, 192, 32);\
3500+ SQUASHFS_SWAP((s)->s_major, d, 224, 16);\
3501+ SQUASHFS_SWAP((s)->s_minor, d, 240, 16);\
3502+ SQUASHFS_SWAP((s)->block_size_1, d, 256, 16);\
3503+ SQUASHFS_SWAP((s)->block_log, d, 272, 16);\
3504+ SQUASHFS_SWAP((s)->flags, d, 288, 8);\
3505+ SQUASHFS_SWAP((s)->no_uids, d, 296, 8);\
3506+ SQUASHFS_SWAP((s)->no_guids, d, 304, 8);\
3507+ SQUASHFS_SWAP((s)->mkfs_time, d, 312, 32);\
3508+ SQUASHFS_SWAP((s)->root_inode, d, 344, 64);\
3509+ SQUASHFS_SWAP((s)->block_size, d, 408, 32);\
3510+ SQUASHFS_SWAP((s)->fragments, d, 440, 32);\
3511+ SQUASHFS_SWAP((s)->fragment_table_start_2, d, 472, 32);\
3512+ SQUASHFS_SWAP((s)->bytes_used, d, 504, 64);\
3513+ SQUASHFS_SWAP((s)->uid_start, d, 568, 64);\
3514+ SQUASHFS_SWAP((s)->guid_start, d, 632, 64);\
3515+ SQUASHFS_SWAP((s)->inode_table_start, d, 696, 64);\
3516+ SQUASHFS_SWAP((s)->directory_table_start, d, 760, 64);\
3517+ SQUASHFS_SWAP((s)->fragment_table_start, d, 824, 64);\
3518+ SQUASHFS_SWAP((s)->lookup_table_start, d, 888, 64);\
3519+}
3520+
3521+#define SQUASHFS_SWAP_BASE_INODE_CORE(s, d, n)\
3522+ SQUASHFS_MEMSET(s, d, n);\
3523+ SQUASHFS_SWAP((s)->inode_type, d, 0, 4);\
3524+ SQUASHFS_SWAP((s)->mode, d, 4, 12);\
3525+ SQUASHFS_SWAP((s)->uid, d, 16, 8);\
3526+ SQUASHFS_SWAP((s)->guid, d, 24, 8);\
3527+ SQUASHFS_SWAP((s)->mtime, d, 32, 32);\
3528+ SQUASHFS_SWAP((s)->inode_number, d, 64, 32);
3529+
3530+#define SQUASHFS_SWAP_BASE_INODE_HEADER(s, d, n) {\
3531+ SQUASHFS_SWAP_START\
3532+ SQUASHFS_SWAP_BASE_INODE_CORE(s, d, n)\
3533+}
3534+
3535+#define SQUASHFS_SWAP_IPC_INODE_HEADER(s, d) {\
3536+ SQUASHFS_SWAP_START\
3537+ SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
3538+ sizeof(struct squashfs_ipc_inode_header))\
3539+ SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
3540+}
3541+
3542+#define SQUASHFS_SWAP_DEV_INODE_HEADER(s, d) {\
3543+ SQUASHFS_SWAP_START\
3544+ SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
3545+ sizeof(struct squashfs_dev_inode_header)); \
3546+ SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
3547+ SQUASHFS_SWAP((s)->rdev, d, 128, 16);\
3548+}
3549+
3550+#define SQUASHFS_SWAP_SYMLINK_INODE_HEADER(s, d) {\
3551+ SQUASHFS_SWAP_START\
3552+ SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
3553+ sizeof(struct squashfs_symlink_inode_header));\
3554+ SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
3555+ SQUASHFS_SWAP((s)->symlink_size, d, 128, 16);\
3556+}
3557+
3558+#define SQUASHFS_SWAP_REG_INODE_HEADER(s, d) {\
3559+ SQUASHFS_SWAP_START\
3560+ SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
3561+ sizeof(struct squashfs_reg_inode_header));\
3562+ SQUASHFS_SWAP((s)->start_block, d, 96, 64);\
3563+ SQUASHFS_SWAP((s)->fragment, d, 160, 32);\
3564+ SQUASHFS_SWAP((s)->offset, d, 192, 32);\
3565+ SQUASHFS_SWAP((s)->file_size, d, 224, 32);\
3566+}
3567+
3568+#define SQUASHFS_SWAP_LREG_INODE_HEADER(s, d) {\
3569+ SQUASHFS_SWAP_START\
3570+ SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
3571+ sizeof(struct squashfs_lreg_inode_header));\
3572+ SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
3573+ SQUASHFS_SWAP((s)->start_block, d, 128, 64);\
3574+ SQUASHFS_SWAP((s)->fragment, d, 192, 32);\
3575+ SQUASHFS_SWAP((s)->offset, d, 224, 32);\
3576+ SQUASHFS_SWAP((s)->file_size, d, 256, 64);\
3577+}
3578+
3579+#define SQUASHFS_SWAP_DIR_INODE_HEADER(s, d) {\
3580+ SQUASHFS_SWAP_START\
3581+ SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
3582+ sizeof(struct squashfs_dir_inode_header));\
3583+ SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
3584+ SQUASHFS_SWAP((s)->file_size, d, 128, 19);\
3585+ SQUASHFS_SWAP((s)->offset, d, 147, 13);\
3586+ SQUASHFS_SWAP((s)->start_block, d, 160, 32);\
3587+ SQUASHFS_SWAP((s)->parent_inode, d, 192, 32);\
3588+}
3589+
3590+#define SQUASHFS_SWAP_LDIR_INODE_HEADER(s, d) {\
3591+ SQUASHFS_SWAP_START\
3592+ SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
3593+ sizeof(struct squashfs_ldir_inode_header));\
3594+ SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
3595+ SQUASHFS_SWAP((s)->file_size, d, 128, 27);\
3596+ SQUASHFS_SWAP((s)->offset, d, 155, 13);\
3597+ SQUASHFS_SWAP((s)->start_block, d, 168, 32);\
3598+ SQUASHFS_SWAP((s)->i_count, d, 200, 16);\
3599+ SQUASHFS_SWAP((s)->parent_inode, d, 216, 32);\
3600+}
3601+
3602+#define SQUASHFS_SWAP_DIR_INDEX(s, d) {\
3603+ SQUASHFS_SWAP_START\
3604+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_index));\
3605+ SQUASHFS_SWAP((s)->index, d, 0, 32);\
3606+ SQUASHFS_SWAP((s)->start_block, d, 32, 32);\
3607+ SQUASHFS_SWAP((s)->size, d, 64, 8);\
3608+}
3609+
3610+#define SQUASHFS_SWAP_DIR_HEADER(s, d) {\
3611+ SQUASHFS_SWAP_START\
3612+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_header));\
3613+ SQUASHFS_SWAP((s)->count, d, 0, 8);\
3614+ SQUASHFS_SWAP((s)->start_block, d, 8, 32);\
3615+ SQUASHFS_SWAP((s)->inode_number, d, 40, 32);\
3616+}
3617+
3618+#define SQUASHFS_SWAP_DIR_ENTRY(s, d) {\
3619+ SQUASHFS_SWAP_START\
3620+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_entry));\
3621+ SQUASHFS_SWAP((s)->offset, d, 0, 13);\
3622+ SQUASHFS_SWAP((s)->type, d, 13, 3);\
3623+ SQUASHFS_SWAP((s)->size, d, 16, 8);\
3624+ SQUASHFS_SWAP((s)->inode_number, d, 24, 16);\
3625+}
3626+
3627+#define SQUASHFS_SWAP_FRAGMENT_ENTRY(s, d) {\
3628+ SQUASHFS_SWAP_START\
3629+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_fragment_entry));\
3630+ SQUASHFS_SWAP((s)->start_block, d, 0, 64);\
3631+ SQUASHFS_SWAP((s)->size, d, 64, 32);\
3632+}
3633+
3634+#define SQUASHFS_SWAP_INODE_T(s, d) SQUASHFS_SWAP_LONG_LONGS(s, d, 1)
3635+
3636+#define SQUASHFS_SWAP_SHORTS(s, d, n) {\
3637+ int entry;\
3638+ int bit_position;\
3639+ SQUASHFS_SWAP_START\
3640+ SQUASHFS_MEMSET(s, d, n * 2);\
3641+ for(entry = 0, bit_position = 0; entry < n; entry++, bit_position += \
3642+ 16)\
3643+ SQUASHFS_SWAP(s[entry], d, bit_position, 16);\
3644+}
3645+
3646+#define SQUASHFS_SWAP_INTS(s, d, n) {\
3647+ int entry;\
3648+ int bit_position;\
3649+ SQUASHFS_SWAP_START\
3650+ SQUASHFS_MEMSET(s, d, n * 4);\
3651+ for(entry = 0, bit_position = 0; entry < n; entry++, bit_position += \
3652+ 32)\
3653+ SQUASHFS_SWAP(s[entry], d, bit_position, 32);\
3654+}
3655+
3656+#define SQUASHFS_SWAP_LONG_LONGS(s, d, n) {\
3657+ int entry;\
3658+ int bit_position;\
3659+ SQUASHFS_SWAP_START\
3660+ SQUASHFS_MEMSET(s, d, n * 8);\
3661+ for(entry = 0, bit_position = 0; entry < n; entry++, bit_position += \
3662+ 64)\
3663+ SQUASHFS_SWAP(s[entry], d, bit_position, 64);\
3664+}
3665+
3666+#define SQUASHFS_SWAP_DATA(s, d, n, bits) {\
3667+ int entry;\
3668+ int bit_position;\
3669+ SQUASHFS_SWAP_START\
3670+ SQUASHFS_MEMSET(s, d, n * bits / 8);\
3671+ for(entry = 0, bit_position = 0; entry < n; entry++, bit_position += \
3672+ bits)\
3673+ SQUASHFS_SWAP(s[entry], d, bit_position, bits);\
3674+}
3675+
3676+#define SQUASHFS_SWAP_FRAGMENT_INDEXES(s, d, n) SQUASHFS_SWAP_LONG_LONGS(s, d, n)
3677+#define SQUASHFS_SWAP_LOOKUP_BLOCKS(s, d, n) SQUASHFS_SWAP_LONG_LONGS(s, d, n)
3678+
3679+#ifdef CONFIG_SQUASHFS_1_0_COMPATIBILITY
3680+
3681+struct squashfs_base_inode_header_1 {
3682+ unsigned int inode_type:4;
3683+ unsigned int mode:12; /* protection */
3684+ unsigned int uid:4; /* index into uid table */
3685+ unsigned int guid:4; /* index into guid table */
3686+} __attribute__ ((packed));
3687+
3688+struct squashfs_ipc_inode_header_1 {
3689+ unsigned int inode_type:4;
3690+ unsigned int mode:12; /* protection */
3691+ unsigned int uid:4; /* index into uid table */
3692+ unsigned int guid:4; /* index into guid table */
3693+ unsigned int type:4;
3694+ unsigned int offset:4;
3695+} __attribute__ ((packed));
3696+
3697+struct squashfs_dev_inode_header_1 {
3698+ unsigned int inode_type:4;
3699+ unsigned int mode:12; /* protection */
3700+ unsigned int uid:4; /* index into uid table */
3701+ unsigned int guid:4; /* index into guid table */
3702+ unsigned short rdev;
3703+} __attribute__ ((packed));
3704+
3705+struct squashfs_symlink_inode_header_1 {
3706+ unsigned int inode_type:4;
3707+ unsigned int mode:12; /* protection */
3708+ unsigned int uid:4; /* index into uid table */
3709+ unsigned int guid:4; /* index into guid table */
3710+ unsigned short symlink_size;
3711+ char symlink[0];
3712+} __attribute__ ((packed));
3713+
3714+struct squashfs_reg_inode_header_1 {
3715+ unsigned int inode_type:4;
3716+ unsigned int mode:12; /* protection */
3717+ unsigned int uid:4; /* index into uid table */
3718+ unsigned int guid:4; /* index into guid table */
3719+ unsigned int mtime;
3720+ unsigned int start_block;
3721+ unsigned int file_size:32;
3722+ unsigned short block_list[0];
3723+} __attribute__ ((packed));
3724+
3725+struct squashfs_dir_inode_header_1 {
3726+ unsigned int inode_type:4;
3727+ unsigned int mode:12; /* protection */
3728+ unsigned int uid:4; /* index into uid table */
3729+ unsigned int guid:4; /* index into guid table */
3730+ unsigned int file_size:19;
3731+ unsigned int offset:13;
3732+ unsigned int mtime;
3733+ unsigned int start_block:24;
3734+} __attribute__ ((packed));
3735+
3736+union squashfs_inode_header_1 {
3737+ struct squashfs_base_inode_header_1 base;
3738+ struct squashfs_dev_inode_header_1 dev;
3739+ struct squashfs_symlink_inode_header_1 symlink;
3740+ struct squashfs_reg_inode_header_1 reg;
3741+ struct squashfs_dir_inode_header_1 dir;
3742+ struct squashfs_ipc_inode_header_1 ipc;
3743+};
3744+
3745+#define SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, n) \
3746+ SQUASHFS_MEMSET(s, d, n);\
3747+ SQUASHFS_SWAP((s)->inode_type, d, 0, 4);\
3748+ SQUASHFS_SWAP((s)->mode, d, 4, 12);\
3749+ SQUASHFS_SWAP((s)->uid, d, 16, 4);\
3750+ SQUASHFS_SWAP((s)->guid, d, 20, 4);
3751+
3752+#define SQUASHFS_SWAP_BASE_INODE_HEADER_1(s, d, n) {\
3753+ SQUASHFS_SWAP_START\
3754+ SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, n)\
3755+}
3756+
3757+#define SQUASHFS_SWAP_IPC_INODE_HEADER_1(s, d) {\
3758+ SQUASHFS_SWAP_START\
3759+ SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
3760+ sizeof(struct squashfs_ipc_inode_header_1));\
3761+ SQUASHFS_SWAP((s)->type, d, 24, 4);\
3762+ SQUASHFS_SWAP((s)->offset, d, 28, 4);\
3763+}
3764+
3765+#define SQUASHFS_SWAP_DEV_INODE_HEADER_1(s, d) {\
3766+ SQUASHFS_SWAP_START\
3767+ SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
3768+ sizeof(struct squashfs_dev_inode_header_1));\
3769+ SQUASHFS_SWAP((s)->rdev, d, 24, 16);\
3770+}
3771+
3772+#define SQUASHFS_SWAP_SYMLINK_INODE_HEADER_1(s, d) {\
3773+ SQUASHFS_SWAP_START\
3774+ SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
3775+ sizeof(struct squashfs_symlink_inode_header_1));\
3776+ SQUASHFS_SWAP((s)->symlink_size, d, 24, 16);\
3777+}
3778+
3779+#define SQUASHFS_SWAP_REG_INODE_HEADER_1(s, d) {\
3780+ SQUASHFS_SWAP_START\
3781+ SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
3782+ sizeof(struct squashfs_reg_inode_header_1));\
3783+ SQUASHFS_SWAP((s)->mtime, d, 24, 32);\
3784+ SQUASHFS_SWAP((s)->start_block, d, 56, 32);\
3785+ SQUASHFS_SWAP((s)->file_size, d, 88, 32);\
3786+}
3787+
3788+#define SQUASHFS_SWAP_DIR_INODE_HEADER_1(s, d) {\
3789+ SQUASHFS_SWAP_START\
3790+ SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
3791+ sizeof(struct squashfs_dir_inode_header_1));\
3792+ SQUASHFS_SWAP((s)->file_size, d, 24, 19);\
3793+ SQUASHFS_SWAP((s)->offset, d, 43, 13);\
3794+ SQUASHFS_SWAP((s)->mtime, d, 56, 32);\
3795+ SQUASHFS_SWAP((s)->start_block, d, 88, 24);\
3796+}
3797+
3798+#endif
3799+
3800+#ifdef CONFIG_SQUASHFS_2_0_COMPATIBILITY
3801+
3802+struct squashfs_dir_index_2 {
3803+ unsigned int index:27;
3804+ unsigned int start_block:29;
3805+ unsigned char size;
3806+ unsigned char name[0];
3807+} __attribute__ ((packed));
3808+
3809+struct squashfs_base_inode_header_2 {
3810+ unsigned int inode_type:4;
3811+ unsigned int mode:12; /* protection */
3812+ unsigned int uid:8; /* index into uid table */
3813+ unsigned int guid:8; /* index into guid table */
3814+} __attribute__ ((packed));
3815+
3816+struct squashfs_ipc_inode_header_2 {
3817+ unsigned int inode_type:4;
3818+ unsigned int mode:12; /* protection */
3819+ unsigned int uid:8; /* index into uid table */
3820+ unsigned int guid:8; /* index into guid table */
3821+} __attribute__ ((packed));
3822+
3823+struct squashfs_dev_inode_header_2 {
3824+ unsigned int inode_type:4;
3825+ unsigned int mode:12; /* protection */
3826+ unsigned int uid:8; /* index into uid table */
3827+ unsigned int guid:8; /* index into guid table */
3828+ unsigned short rdev;
3829+} __attribute__ ((packed));
3830+
3831+struct squashfs_symlink_inode_header_2 {
3832+ unsigned int inode_type:4;
3833+ unsigned int mode:12; /* protection */
3834+ unsigned int uid:8; /* index into uid table */
3835+ unsigned int guid:8; /* index into guid table */
3836+ unsigned short symlink_size;
3837+ char symlink[0];
3838+} __attribute__ ((packed));
3839+
3840+struct squashfs_reg_inode_header_2 {
3841+ unsigned int inode_type:4;
3842+ unsigned int mode:12; /* protection */
3843+ unsigned int uid:8; /* index into uid table */
3844+ unsigned int guid:8; /* index into guid table */
3845+ unsigned int mtime;
3846+ unsigned int start_block;
3847+ unsigned int fragment;
3848+ unsigned int offset;
3849+ unsigned int file_size:32;
3850+ unsigned short block_list[0];
3851+} __attribute__ ((packed));
3852+
3853+struct squashfs_dir_inode_header_2 {
3854+ unsigned int inode_type:4;
3855+ unsigned int mode:12; /* protection */
3856+ unsigned int uid:8; /* index into uid table */
3857+ unsigned int guid:8; /* index into guid table */
3858+ unsigned int file_size:19;
3859+ unsigned int offset:13;
3860+ unsigned int mtime;
3861+ unsigned int start_block:24;
3862+} __attribute__ ((packed));
3863+
3864+struct squashfs_ldir_inode_header_2 {
3865+ unsigned int inode_type:4;
3866+ unsigned int mode:12; /* protection */
3867+ unsigned int uid:8; /* index into uid table */
3868+ unsigned int guid:8; /* index into guid table */
3869+ unsigned int file_size:27;
3870+ unsigned int offset:13;
3871+ unsigned int mtime;
3872+ unsigned int start_block:24;
3873+ unsigned int i_count:16;
3874+ struct squashfs_dir_index_2 index[0];
3875+} __attribute__ ((packed));
3876+
3877+union squashfs_inode_header_2 {
3878+ struct squashfs_base_inode_header_2 base;
3879+ struct squashfs_dev_inode_header_2 dev;
3880+ struct squashfs_symlink_inode_header_2 symlink;
3881+ struct squashfs_reg_inode_header_2 reg;
3882+ struct squashfs_dir_inode_header_2 dir;
3883+ struct squashfs_ldir_inode_header_2 ldir;
3884+ struct squashfs_ipc_inode_header_2 ipc;
3885+};
3886+
3887+struct squashfs_dir_header_2 {
3888+ unsigned int count:8;
3889+ unsigned int start_block:24;
3890+} __attribute__ ((packed));
3891+
3892+struct squashfs_dir_entry_2 {
3893+ unsigned int offset:13;
3894+ unsigned int type:3;
3895+ unsigned int size:8;
3896+ char name[0];
3897+} __attribute__ ((packed));
3898+
3899+struct squashfs_fragment_entry_2 {
3900+ unsigned int start_block;
3901+ unsigned int size;
3902+} __attribute__ ((packed));
3903+
3904+#define SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, n)\
3905+ SQUASHFS_MEMSET(s, d, n);\
3906+ SQUASHFS_SWAP((s)->inode_type, d, 0, 4);\
3907+ SQUASHFS_SWAP((s)->mode, d, 4, 12);\
3908+ SQUASHFS_SWAP((s)->uid, d, 16, 8);\
3909+ SQUASHFS_SWAP((s)->guid, d, 24, 8);\
3910+
3911+#define SQUASHFS_SWAP_BASE_INODE_HEADER_2(s, d, n) {\
3912+ SQUASHFS_SWAP_START\
3913+ SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, n)\
3914+}
3915+
3916+#define SQUASHFS_SWAP_IPC_INODE_HEADER_2(s, d) \
3917+ SQUASHFS_SWAP_BASE_INODE_HEADER_2(s, d, sizeof(struct squashfs_ipc_inode_header_2))
3918+
3919+#define SQUASHFS_SWAP_DEV_INODE_HEADER_2(s, d) {\
3920+ SQUASHFS_SWAP_START\
3921+ SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
3922+ sizeof(struct squashfs_dev_inode_header_2)); \
3923+ SQUASHFS_SWAP((s)->rdev, d, 32, 16);\
3924+}
3925+
3926+#define SQUASHFS_SWAP_SYMLINK_INODE_HEADER_2(s, d) {\
3927+ SQUASHFS_SWAP_START\
3928+ SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
3929+ sizeof(struct squashfs_symlink_inode_header_2));\
3930+ SQUASHFS_SWAP((s)->symlink_size, d, 32, 16);\
3931+}
3932+
3933+#define SQUASHFS_SWAP_REG_INODE_HEADER_2(s, d) {\
3934+ SQUASHFS_SWAP_START\
3935+ SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
3936+ sizeof(struct squashfs_reg_inode_header_2));\
3937+ SQUASHFS_SWAP((s)->mtime, d, 32, 32);\
3938+ SQUASHFS_SWAP((s)->start_block, d, 64, 32);\
3939+ SQUASHFS_SWAP((s)->fragment, d, 96, 32);\
3940+ SQUASHFS_SWAP((s)->offset, d, 128, 32);\
3941+ SQUASHFS_SWAP((s)->file_size, d, 160, 32);\
3942+}
3943+
3944+#define SQUASHFS_SWAP_DIR_INODE_HEADER_2(s, d) {\
3945+ SQUASHFS_SWAP_START\
3946+ SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
3947+ sizeof(struct squashfs_dir_inode_header_2));\
3948+ SQUASHFS_SWAP((s)->file_size, d, 32, 19);\
3949+ SQUASHFS_SWAP((s)->offset, d, 51, 13);\
3950+ SQUASHFS_SWAP((s)->mtime, d, 64, 32);\
3951+ SQUASHFS_SWAP((s)->start_block, d, 96, 24);\
3952+}
3953+
3954+#define SQUASHFS_SWAP_LDIR_INODE_HEADER_2(s, d) {\
3955+ SQUASHFS_SWAP_START\
3956+ SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
3957+ sizeof(struct squashfs_ldir_inode_header_2));\
3958+ SQUASHFS_SWAP((s)->file_size, d, 32, 27);\
3959+ SQUASHFS_SWAP((s)->offset, d, 59, 13);\
3960+ SQUASHFS_SWAP((s)->mtime, d, 72, 32);\
3961+ SQUASHFS_SWAP((s)->start_block, d, 104, 24);\
3962+ SQUASHFS_SWAP((s)->i_count, d, 128, 16);\
3963+}
3964+
3965+#define SQUASHFS_SWAP_DIR_INDEX_2(s, d) {\
3966+ SQUASHFS_SWAP_START\
3967+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_index_2));\
3968+ SQUASHFS_SWAP((s)->index, d, 0, 27);\
3969+ SQUASHFS_SWAP((s)->start_block, d, 27, 29);\
3970+ SQUASHFS_SWAP((s)->size, d, 56, 8);\
3971+}
3972+#define SQUASHFS_SWAP_DIR_HEADER_2(s, d) {\
3973+ SQUASHFS_SWAP_START\
3974+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_header_2));\
3975+ SQUASHFS_SWAP((s)->count, d, 0, 8);\
3976+ SQUASHFS_SWAP((s)->start_block, d, 8, 24);\
3977+}
3978+
3979+#define SQUASHFS_SWAP_DIR_ENTRY_2(s, d) {\
3980+ SQUASHFS_SWAP_START\
3981+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_entry_2));\
3982+ SQUASHFS_SWAP((s)->offset, d, 0, 13);\
3983+ SQUASHFS_SWAP((s)->type, d, 13, 3);\
3984+ SQUASHFS_SWAP((s)->size, d, 16, 8);\
3985+}
3986+
3987+#define SQUASHFS_SWAP_FRAGMENT_ENTRY_2(s, d) {\
3988+ SQUASHFS_SWAP_START\
3989+ SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_fragment_entry_2));\
3990+ SQUASHFS_SWAP((s)->start_block, d, 0, 32);\
3991+ SQUASHFS_SWAP((s)->size, d, 32, 32);\
3992+}
3993+
3994+#define SQUASHFS_SWAP_FRAGMENT_INDEXES_2(s, d, n) SQUASHFS_SWAP_INTS(s, d, n)
3995+
3996+/* fragment and fragment table defines */
3997+#define SQUASHFS_FRAGMENT_BYTES_2(A) (A * sizeof(struct squashfs_fragment_entry_2))
3998+
3999+#define SQUASHFS_FRAGMENT_INDEX_2(A) (SQUASHFS_FRAGMENT_BYTES_2(A) / \
4000+ SQUASHFS_METADATA_SIZE)
4001+
4002+#define SQUASHFS_FRAGMENT_INDEX_OFFSET_2(A) (SQUASHFS_FRAGMENT_BYTES_2(A) % \
4003+ SQUASHFS_METADATA_SIZE)
4004+
4005+#define SQUASHFS_FRAGMENT_INDEXES_2(A) ((SQUASHFS_FRAGMENT_BYTES_2(A) + \
4006+ SQUASHFS_METADATA_SIZE - 1) / \
4007+ SQUASHFS_METADATA_SIZE)
4008+
4009+#define SQUASHFS_FRAGMENT_INDEX_BYTES_2(A) (SQUASHFS_FRAGMENT_INDEXES_2(A) *\
4010+ sizeof(int))
4011+
4012+#endif
4013+
4014+#ifdef __KERNEL__
4015+
4016+/*
4017+ * macros used to swap each structure entry, taking into account
4018+ * bitfields and different bitfield placing conventions on differing
4019+ * architectures
4020+ */
4021+
4022+#include <asm/byteorder.h>
4023+
4024+#ifdef __BIG_ENDIAN
4025+ /* convert from little endian to big endian */
4026+#define SQUASHFS_SWAP(value, p, pos, tbits) _SQUASHFS_SWAP(value, p, pos, \
4027+ tbits, b_pos)
4028+#else
4029+ /* convert from big endian to little endian */
4030+#define SQUASHFS_SWAP(value, p, pos, tbits) _SQUASHFS_SWAP(value, p, pos, \
4031+ tbits, 64 - tbits - b_pos)
4032+#endif
4033+
4034+#define _SQUASHFS_SWAP(value, p, pos, tbits, SHIFT) {\
4035+ b_pos = pos % 8;\
4036+ val = 0;\
4037+ s = (unsigned char *)p + (pos / 8);\
4038+ d = ((unsigned char *) &val) + 7;\
4039+ for(bits = 0; bits < (tbits + b_pos); bits += 8) \
4040+ *d-- = *s++;\
4041+ value = (val >> (SHIFT))/* & ((1 << tbits) - 1)*/;\
4042+}
4043+
4044+#define SQUASHFS_MEMSET(s, d, n) memset(s, 0, n);
4045+
4046+#endif
4047+#endif
4048--- /dev/null
4049+++ b/include/linux/squashfs_fs_i.h
4050@@ -0,0 +1,45 @@
4051+#ifndef SQUASHFS_FS_I
4052+#define SQUASHFS_FS_I
4053+/*
4054+ * Squashfs
4055+ *
4056+ * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4057+ * Phillip Lougher <phillip@lougher.demon.co.uk>
4058+ *
4059+ * This program is free software; you can redistribute it and/or
4060+ * modify it under the terms of the GNU General Public License
4061+ * as published by the Free Software Foundation; either version 2,
4062+ * or (at your option) any later version.
4063+ *
4064+ * This program is distributed in the hope that it will be useful,
4065+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4066+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4067+ * GNU General Public License for more details.
4068+ *
4069+ * You should have received a copy of the GNU General Public License
4070+ * along with this program; if not, write to the Free Software
4071+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4072+ *
4073+ * squashfs_fs_i.h
4074+ */
4075+
4076+struct squashfs_inode_info {
4077+ long long start_block;
4078+ unsigned int offset;
4079+ union {
4080+ struct {
4081+ long long fragment_start_block;
4082+ unsigned int fragment_size;
4083+ unsigned int fragment_offset;
4084+ long long block_list_start;
4085+ } s1;
4086+ struct {
4087+ long long directory_index_start;
4088+ unsigned int directory_index_offset;
4089+ unsigned int directory_index_count;
4090+ unsigned int parent_inode;
4091+ } s2;
4092+ } u;
4093+ struct inode vfs_inode;
4094+};
4095+#endif
4096--- /dev/null
4097+++ b/include/linux/squashfs_fs_sb.h
4098@@ -0,0 +1,79 @@
4099+#ifndef SQUASHFS_FS_SB
4100+#define SQUASHFS_FS_SB
4101+/*
4102+ * Squashfs
4103+ *
4104+ * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
4105+ * Phillip Lougher <phillip@lougher.demon.co.uk>
4106+ *
4107+ * This program is free software; you can redistribute it and/or
4108+ * modify it under the terms of the GNU General Public License
4109+ * as published by the Free Software Foundation; either version 2,
4110+ * or (at your option) any later version.
4111+ *
4112+ * This program is distributed in the hope that it will be useful,
4113+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4114+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4115+ * GNU General Public License for more details.
4116+ *
4117+ * You should have received a copy of the GNU General Public License
4118+ * along with this program; if not, write to the Free Software
4119+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4120+ *
4121+ * squashfs_fs_sb.h
4122+ */
4123+
4124+#include <linux/squashfs_fs.h>
4125+
4126+struct squashfs_cache_entry {
4127+ long long block;
4128+ int length;
4129+ int locked;
4130+ long long next_index;
4131+ char pending;
4132+ char error;
4133+ int waiting;
4134+ wait_queue_head_t wait_queue;
4135+ char *data;
4136+};
4137+
4138+struct squashfs_cache {
4139+ char *name;
4140+ int entries;
4141+ int block_size;
4142+ int next_blk;
4143+ int waiting;
4144+ int unused_blks;
4145+ int use_vmalloc;
4146+ spinlock_t lock;
4147+ wait_queue_head_t wait_queue;
4148+ struct squashfs_cache_entry entry[0];
4149+};
4150+
4151+struct squashfs_sb_info {
4152+ struct squashfs_super_block sblk;
4153+ int devblksize;
4154+ int devblksize_log2;
4155+ int swap;
4156+ struct squashfs_cache *block_cache;
4157+ struct squashfs_cache *fragment_cache;
4158+ int next_meta_index;
4159+ unsigned int *uid;
4160+ unsigned int *guid;
4161+ long long *fragment_index;
4162+ unsigned int *fragment_index_2;
4163+ char *read_page;
4164+ struct mutex read_data_mutex;
4165+ struct mutex read_page_mutex;
4166+ struct mutex meta_index_mutex;
4167+ struct meta_index *meta_index;
4168+ z_stream stream;
4169+ long long *inode_lookup_table;
4170+ int (*read_inode)(struct inode *i, squashfs_inode_t \
4171+ inode);
4172+ long long (*read_blocklist)(struct inode *inode, int \
4173+ index, int readahead_blks, char *block_list, \
4174+ unsigned short **block_p, unsigned int *bsize);
4175+ int (*read_fragment_index_table)(struct super_block *s);
4176+};
4177+#endif
4178--- a/init/do_mounts_rd.c
4179+++ b/init/do_mounts_rd.c
4180@@ -5,6 +5,7 @@
4181 #include <linux/ext2_fs.h>
4182 #include <linux/romfs_fs.h>
4183 #include <linux/cramfs_fs.h>
4184+#include <linux/squashfs_fs.h>
4185 #include <linux/initrd.h>
4186 #include <linux/string.h>
4187
4188@@ -37,6 +38,7 @@ static int __init crd_load(int in_fd, in
4189 * numbers could not be found.
4190 *
4191 * We currently check for the following magic numbers:
4192+ * squashfs
4193 * minix
4194 * ext2
4195 * romfs
4196@@ -51,6 +53,7 @@ identify_ramdisk_image(int fd, int start
4197 struct ext2_super_block *ext2sb;
4198 struct romfs_super_block *romfsb;
4199 struct cramfs_super *cramfsb;
4200+ struct squashfs_super_block *squashfsb;
4201 int nblocks = -1;
4202 unsigned char *buf;
4203
4204@@ -62,6 +65,7 @@ identify_ramdisk_image(int fd, int start
4205 ext2sb = (struct ext2_super_block *) buf;
4206 romfsb = (struct romfs_super_block *) buf;
4207 cramfsb = (struct cramfs_super *) buf;
4208+ squashfsb = (struct squashfs_super_block *) buf;
4209 memset(buf, 0xe5, size);
4210
4211 /*
4212@@ -99,6 +103,18 @@ identify_ramdisk_image(int fd, int start
4213 goto done;
4214 }
4215
4216+ /* squashfs is at block zero too */
4217+ if (squashfsb->s_magic == SQUASHFS_MAGIC) {
4218+ printk(KERN_NOTICE
4219+ "RAMDISK: squashfs filesystem found at block %d\n",
4220+ start_block);
4221+ if (squashfsb->s_major < 3)
4222+ nblocks = (squashfsb->bytes_used_2+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
4223+ else
4224+ nblocks = (squashfsb->bytes_used+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
4225+ goto done;
4226+ }
4227+
4228 /*
4229 * Read block 1 to test for minix and ext2 superblock
4230 */