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