/[linux-patches]/hardened/2.6/trunk/2.6.23/4300_squashfs-3.2-r2.patch
Gentoo

Contents of /hardened/2.6/trunk/2.6.23/4300_squashfs-3.2-r2.patch

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20