2 * Squashfs - a compressed read only filesystem for Linux
4 * Copyright (c) 2002, 2003, 2004, 2005 Phillip Lougher <phillip@lougher.demon.co.uk>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #define SQUASHFS_1_0_COMPATIBILITY
25 #include <linux/types.h>
26 #include <linux/squashfs_fs.h>
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
31 #include <linux/smp_lock.h>
32 #include <linux/slab.h>
33 #include <linux/squashfs_fs_sb.h>
34 #include <linux/squashfs_fs_i.h>
35 #include <linux/buffer_head.h>
36 #include <linux/vfs.h>
37 #include <linux/init.h>
38 #include <linux/dcache.h>
39 #include <asm/uaccess.h>
40 #include <linux/wait.h>
41 #include <asm/semaphore.h>
42 #include <linux/zlib.h>
43 #include <linux/blkdev.h>
44 #include <linux/vmalloc.h>
47 #define TRACE(s, args...) printk(KERN_NOTICE "SQUASHFS: "s, ## args)
49 #define TRACE(s, args...) {}
52 #define ERROR(s, args...) printk(KERN_ERR "SQUASHFS error: "s, ## args)
54 #define SERROR(s, args...) if(!silent) printk(KERN_ERR "SQUASHFS error: "s, ## args)
55 #define WARNING(s, args...) printk(KERN_WARNING "SQUASHFS: "s, ## args)
57 static void squashfs_put_super(struct super_block *);
58 static int squashfs_statfs(struct super_block *, struct kstatfs *);
59 static int squashfs_symlink_readpage(struct file *file, struct page *page);
60 static int squashfs_readpage(struct file *file, struct page *page);
61 static int squashfs_readpage4K(struct file *file, struct page *page);
62 static int squashfs_readdir(struct file *, void *, filldir_t);
63 static struct dentry *squashfs_lookup(struct inode *, struct dentry *, struct nameidata *);
64 static unsigned int read_data(struct super_block *s, char *buffer,
65 unsigned int index, unsigned int length, unsigned int *next_index);
66 static int squashfs_get_cached_block(struct super_block *s, char *buffer,
67 unsigned int block, unsigned int offset, int length,
68 unsigned int *next_block, unsigned int *next_offset);
69 static struct inode *squashfs_iget(struct super_block *s, squashfs_inode inode);
70 static unsigned int read_blocklist(struct inode *inode, int index, int readahead_blks,
71 char *block_list, unsigned short **block_p, unsigned int *bsize);
72 static void squashfs_put_super(struct super_block *s);
73 static struct super_block *squashfs_get_sb(struct file_system_type *, int, const char *, void *);
74 static struct inode *squashfs_alloc_inode(struct super_block *sb);
75 static void squashfs_destroy_inode(struct inode *inode);
76 static int init_inodecache(void);
77 static void destroy_inodecache(void);
79 #ifdef SQUASHFS_1_0_COMPATIBILITY
80 static int squashfs_readpage_lessthan4K(struct file *file, struct page *page);
81 static struct inode *squashfs_iget_1(struct super_block *s, squashfs_inode inode);
82 static unsigned int read_blocklist_1(struct inode *inode, int index, int readahead_blks,
83 char *block_list, unsigned short **block_p, unsigned int *bsize);
86 DECLARE_MUTEX(read_data_mutex);
88 static z_stream stream;
90 static struct file_system_type squashfs_fs_type = {
93 .get_sb = squashfs_get_sb,
94 .kill_sb = kill_block_super,
95 .fs_flags = FS_REQUIRES_DEV
98 static unsigned char squashfs_filetype_table[] = {
99 DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
102 static struct super_operations squashfs_ops = {
103 .alloc_inode = squashfs_alloc_inode,
104 .destroy_inode = squashfs_destroy_inode,
105 .statfs = squashfs_statfs,
106 .put_super = squashfs_put_super,
109 static struct address_space_operations squashfs_symlink_aops = {
110 .readpage = squashfs_symlink_readpage
113 static struct address_space_operations squashfs_aops = {
114 .readpage = squashfs_readpage
117 static struct address_space_operations squashfs_aops_4K = {
118 .readpage = squashfs_readpage4K
121 #ifdef SQUASHFS_1_0_COMPATIBILITY
122 static struct address_space_operations squashfs_aops_lessthan4K = {
123 .readpage = squashfs_readpage_lessthan4K
127 static struct file_operations squashfs_dir_ops = {
128 .read = generic_read_dir,
129 .readdir = squashfs_readdir
132 static struct inode_operations squashfs_dir_inode_ops = {
133 .lookup = squashfs_lookup
137 static inline struct squashfs_inode_info *SQUASHFS_I(struct inode *inode)
139 return list_entry(inode, struct squashfs_inode_info, vfs_inode);
143 static struct buffer_head *get_block_length(struct super_block *s,
144 int *cur_index, int *offset, int *c_byte)
146 squashfs_sb_info *msblk = s->s_fs_info;
148 struct buffer_head *bh;
150 if (!(bh = sb_bread(s, *cur_index)))
153 if (msblk->devblksize - *offset == 1) {
155 ((unsigned char *) &temp)[1] = *((unsigned char *)
156 (bh->b_data + *offset));
158 ((unsigned char *) &temp)[0] = *((unsigned char *)
159 (bh->b_data + *offset));
161 if (!(bh = sb_bread(s, ++(*cur_index))))
164 ((unsigned char *) &temp)[0] = *((unsigned char *)
167 ((unsigned char *) &temp)[1] = *((unsigned char *)
173 ((unsigned char *) &temp)[1] = *((unsigned char *)
174 (bh->b_data + *offset));
175 ((unsigned char *) &temp)[0] = *((unsigned char *)
176 (bh->b_data + *offset + 1));
178 ((unsigned char *) &temp)[0] = *((unsigned char *)
179 (bh->b_data + *offset));
180 ((unsigned char *) &temp)[1] = *((unsigned char *)
181 (bh->b_data + *offset + 1));
187 if (SQUASHFS_CHECK_DATA(msblk->sBlk.flags)) {
188 if (*offset == msblk->devblksize) {
190 if (!(bh = sb_bread(s, ++(*cur_index))))
194 if (*((unsigned char *) (bh->b_data + *offset)) !=
195 SQUASHFS_MARKER_BYTE) {
196 ERROR("Metadata block marker corrupt @ %x\n",
210 static unsigned int read_data(struct super_block *s, char *buffer,
211 unsigned int index, unsigned int length, unsigned int *next_index)
213 squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
214 struct buffer_head *bh[((SQUASHFS_FILE_MAX_SIZE - 1) >> msBlk->devblksize_log2) + 2];
215 unsigned int offset = index & ((1 << msBlk->devblksize_log2) - 1);
216 unsigned int cur_index = index >> msBlk->devblksize_log2;
217 int bytes, avail_bytes, b = 0, k;
219 unsigned int compressed;
220 unsigned int c_byte = length;
223 bytes = msBlk->devblksize - offset;
224 compressed = SQUASHFS_COMPRESSED_BLOCK(c_byte);
225 c_buffer = compressed ? msBlk->read_data : buffer;
226 c_byte = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
228 TRACE("Block @ 0x%x, %scompressed size %d\n", index, compressed ? "" : "un", (unsigned int) c_byte);
230 if(!(bh[0] = sb_getblk(s, cur_index)))
232 for(b = 1; bytes < c_byte; b++) {
233 if(!(bh[b] = sb_getblk(s, ++cur_index)))
235 bytes += msBlk->devblksize;
237 ll_rw_block(READ, b, bh);
239 if(!(bh[0] = get_block_length(s, &cur_index, &offset, &c_byte)))
242 bytes = msBlk->devblksize - offset;
243 compressed = SQUASHFS_COMPRESSED(c_byte);
244 c_buffer = compressed ? msBlk->read_data : buffer;
245 c_byte = SQUASHFS_COMPRESSED_SIZE(c_byte);
247 TRACE("Block @ 0x%x, %scompressed size %d\n", index, compressed ? "" : "un", (unsigned int) c_byte);
249 for(b = 1; bytes < c_byte; b++) {
250 if(!(bh[b] = sb_getblk(s, ++cur_index)))
252 bytes += msBlk->devblksize;
254 ll_rw_block(READ, b - 1, bh + 1);
258 down(&read_data_mutex);
260 for(bytes = 0, k = 0; k < b; k++) {
261 avail_bytes = (c_byte - bytes) > (msBlk->devblksize - offset) ? msBlk->devblksize - offset : c_byte - bytes;
262 wait_on_buffer(bh[k]);
263 if (!buffer_uptodate(bh[k]))
265 memcpy(c_buffer + bytes, bh[k]->b_data + offset, avail_bytes);
266 bytes += avail_bytes;
277 stream.next_in = c_buffer;
278 stream.avail_in = c_byte;
279 stream.next_out = buffer;
280 stream.avail_out = msBlk->read_size;
281 if(((zlib_err = zlib_inflateInit(&stream)) != Z_OK) ||
282 ((zlib_err = zlib_inflate(&stream, Z_FINISH)) != Z_STREAM_END) ||
283 ((zlib_err = zlib_inflateEnd(&stream)) != Z_OK)) {
284 ERROR("zlib_fs returned unexpected result 0x%x\n", zlib_err);
287 bytes = stream.total_out;
288 up(&read_data_mutex);
292 *next_index = index + c_byte + (length ? 0 : (SQUASHFS_CHECK_DATA(msBlk->sBlk.flags) ? 3 : 2));
297 while(--b >= 0) brelse(bh[b]);
300 ERROR("sb_bread failed reading block 0x%x\n", cur_index);
305 static int squashfs_get_cached_block(struct super_block *s, char *buffer,
306 unsigned int block, unsigned int offset, int length,
307 unsigned int *next_block, unsigned int *next_offset)
309 squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
310 int n, i, bytes, return_length = length;
311 unsigned int next_index;
313 TRACE("Entered squashfs_get_cached_block [%x:%x]\n", block, offset);
316 for(i = 0; i < SQUASHFS_CACHED_BLKS; i++)
317 if(msBlk->block_cache[i].block == block)
320 down(&msBlk->block_cache_mutex);
321 if(i == SQUASHFS_CACHED_BLKS) {
322 /* read inode header block */
323 for(i = msBlk->next_cache, n = SQUASHFS_CACHED_BLKS; n ; n --, i = (i + 1) % SQUASHFS_CACHED_BLKS)
324 if(msBlk->block_cache[i].block != SQUASHFS_USED_BLK)
329 init_waitqueue_entry(&wait, current);
330 add_wait_queue(&msBlk->waitq, &wait);
331 up(&msBlk->block_cache_mutex);
332 set_current_state(TASK_UNINTERRUPTIBLE);
334 set_current_state(TASK_RUNNING);
335 remove_wait_queue(&msBlk->waitq, &wait);
338 msBlk->next_cache = (i + 1) % SQUASHFS_CACHED_BLKS;
340 if(msBlk->block_cache[i].block == SQUASHFS_INVALID_BLK) {
341 if(!(msBlk->block_cache[i].data = (unsigned char *)
342 kmalloc(SQUASHFS_METADATA_SIZE, GFP_KERNEL))) {
343 ERROR("Failed to allocate cache block\n");
344 up(&msBlk->block_cache_mutex);
349 msBlk->block_cache[i].block = SQUASHFS_USED_BLK;
350 up(&msBlk->block_cache_mutex);
351 if(!(msBlk->block_cache[i].length = read_data(s, msBlk->block_cache[i].data, block, 0,
353 ERROR("Unable to read cache block [%x:%x]\n", block, offset);
356 down(&msBlk->block_cache_mutex);
357 wake_up(&msBlk->waitq);
358 msBlk->block_cache[i].block = block;
359 msBlk->block_cache[i].next_index = next_index;
360 TRACE("Read cache block [%x:%x]\n", block, offset);
363 if(msBlk->block_cache[i].block != block) {
364 up(&msBlk->block_cache_mutex);
368 if((bytes = msBlk->block_cache[i].length - offset) >= length) {
370 memcpy(buffer, msBlk->block_cache[i].data + offset, length);
371 if(msBlk->block_cache[i].length - offset == length) {
372 *next_block = msBlk->block_cache[i].next_index;
376 *next_offset = offset + length;
379 up(&msBlk->block_cache_mutex);
380 return return_length;
383 memcpy(buffer, msBlk->block_cache[i].data + offset, bytes);
386 block = msBlk->block_cache[i].next_index;
387 up(&msBlk->block_cache_mutex);
395 static int get_fragment_location(struct super_block *s, unsigned int fragment, unsigned int *fragment_start_block, unsigned int *fragment_size)
397 squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
398 unsigned int start_block = msBlk->fragment_index[SQUASHFS_FRAGMENT_INDEX(fragment)];
399 int offset = SQUASHFS_FRAGMENT_INDEX_OFFSET(fragment);
400 squashfs_fragment_entry fragment_entry;
403 squashfs_fragment_entry sfragment_entry;
405 if(!squashfs_get_cached_block(s, (char *) &sfragment_entry, start_block, offset,
406 sizeof(sfragment_entry), &start_block, &offset))
408 SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_entry, &sfragment_entry);
410 if(!squashfs_get_cached_block(s, (char *) &fragment_entry, start_block, offset,
411 sizeof(fragment_entry), &start_block, &offset))
414 *fragment_start_block = fragment_entry.start_block;
415 *fragment_size = fragment_entry.size;
421 void release_cached_fragment(squashfs_sb_info *msBlk, struct squashfs_fragment_cache *fragment)
423 down(&msBlk->fragment_mutex);
425 wake_up(&msBlk->fragment_wait_queue);
426 up(&msBlk->fragment_mutex);
430 struct squashfs_fragment_cache *get_cached_fragment(struct super_block *s, unsigned int start_block, int length)
433 squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
436 down(&msBlk->fragment_mutex);
437 for(i = 0; i < SQUASHFS_CACHED_FRAGMENTS && msBlk->fragment[i].block != start_block; i++);
438 if(i == SQUASHFS_CACHED_FRAGMENTS) {
439 for(i = msBlk->next_fragment, n = SQUASHFS_CACHED_FRAGMENTS;
440 n && msBlk->fragment[i].locked; n--, i = (i + 1) % SQUASHFS_CACHED_FRAGMENTS);
445 init_waitqueue_entry(&wait, current);
446 add_wait_queue(&msBlk->fragment_wait_queue, &wait);
447 up(&msBlk->fragment_mutex);
448 set_current_state(TASK_UNINTERRUPTIBLE);
450 set_current_state(TASK_RUNNING);
451 remove_wait_queue(&msBlk->fragment_wait_queue, &wait);
454 msBlk->next_fragment = (msBlk->next_fragment + 1) % SQUASHFS_CACHED_FRAGMENTS;
456 if(msBlk->fragment[i].data == NULL)
457 if(!(msBlk->fragment[i].data = (unsigned char *)
458 SQUASHFS_ALLOC(SQUASHFS_FILE_MAX_SIZE))) {
459 ERROR("Failed to allocate fragment cache block\n");
460 up(&msBlk->fragment_mutex);
464 msBlk->fragment[i].block = SQUASHFS_INVALID_BLK;
465 msBlk->fragment[i].locked = 1;
466 up(&msBlk->fragment_mutex);
467 if(!(msBlk->fragment[i].length = read_data(s, msBlk->fragment[i].data, start_block, length,
469 ERROR("Unable to read fragment cache block [%x]\n", start_block);
470 msBlk->fragment[i].locked = 0;
473 msBlk->fragment[i].block = start_block;
474 TRACE("New fragment %d, start block %d, locked %d\n", i, msBlk->fragment[i].block, msBlk->fragment[i].locked);
475 return &msBlk->fragment[i];
478 msBlk->fragment[i].locked ++;
479 up(&msBlk->fragment_mutex);
481 TRACE("Got fragment %d, start block %d, locked %d\n", i, msBlk->fragment[i].block, msBlk->fragment[i].locked);
482 return &msBlk->fragment[i];
487 #ifdef SQUASHFS_1_0_COMPATIBILITY
488 static struct inode *squashfs_iget_1(struct super_block *s, squashfs_inode inode)
490 struct inode *i = new_inode(s);
491 squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
492 squashfs_super_block *sBlk = &msBlk->sBlk;
493 unsigned int block = SQUASHFS_INODE_BLK(inode) + sBlk->inode_table_start;
494 unsigned int offset = SQUASHFS_INODE_OFFSET(inode);
495 unsigned int next_block, next_offset;
496 squashfs_base_inode_header_1 inodeb;
498 TRACE("Entered squashfs_iget_1\n");
501 squashfs_base_inode_header_1 sinodeb;
503 if(!squashfs_get_cached_block(s, (char *) &sinodeb, block, offset,
504 sizeof(sinodeb), &next_block, &next_offset))
506 SQUASHFS_SWAP_BASE_INODE_HEADER_1(&inodeb, &sinodeb, sizeof(sinodeb));
508 if(!squashfs_get_cached_block(s, (char *) &inodeb, block, offset,
509 sizeof(inodeb), &next_block, &next_offset))
514 i->i_mtime.tv_sec = sBlk->mkfs_time;
515 i->i_atime.tv_sec = sBlk->mkfs_time;
516 i->i_ctime.tv_sec = sBlk->mkfs_time;
518 if(inodeb.inode_type != SQUASHFS_IPC_TYPE)
519 i->i_uid = msBlk->uid[((inodeb.inode_type - 1) / SQUASHFS_TYPES) * 16 + inodeb.uid];
520 i->i_ino = SQUASHFS_MK_VFS_INODE(block - sBlk->inode_table_start, offset);
522 i->i_mode = inodeb.mode;
524 switch(inodeb.inode_type == SQUASHFS_IPC_TYPE ? SQUASHFS_IPC_TYPE : (inodeb.inode_type - 1) % SQUASHFS_TYPES + 1) {
525 case SQUASHFS_FILE_TYPE: {
526 squashfs_reg_inode_header_1 inodep;
529 squashfs_reg_inode_header_1 sinodep;
531 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
532 &next_block, &next_offset))
534 SQUASHFS_SWAP_REG_INODE_HEADER_1(&inodep, &sinodep);
536 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
537 &next_block, &next_offset))
540 i->i_size = inodep.file_size;
541 i->i_fop = &generic_ro_fops;
542 if(sBlk->block_size > 4096)
543 i->i_data.a_ops = &squashfs_aops;
544 else if(sBlk->block_size == 4096)
545 i->i_data.a_ops = &squashfs_aops_4K;
547 i->i_data.a_ops = &squashfs_aops_lessthan4K;
548 i->i_mode |= S_IFREG;
549 i->i_mtime.tv_sec = inodep.mtime;
550 i->i_atime.tv_sec = inodep.mtime;
551 i->i_ctime.tv_sec = inodep.mtime;
552 i->i_blocks = ((i->i_size - 1) >> 9) + 1;
553 i->i_blksize = PAGE_CACHE_SIZE;
554 SQUASHFS_I(i)->u.s1.fragment_start_block = SQUASHFS_INVALID_BLK;
555 SQUASHFS_I(i)->u.s1.fragment_offset = 0;
556 SQUASHFS_I(i)->start_block = inodep.start_block;
557 SQUASHFS_I(i)->block_list_start = next_block;
558 SQUASHFS_I(i)->offset = next_offset;
559 TRACE("File inode %x:%x, start_block %x, block_list_start %x, offset %x\n",
560 SQUASHFS_INODE_BLK(inode), offset, inodep.start_block, next_block, next_offset);
563 case SQUASHFS_DIR_TYPE: {
564 squashfs_dir_inode_header_1 inodep;
567 squashfs_dir_inode_header_1 sinodep;
569 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
570 &next_block, &next_offset))
572 SQUASHFS_SWAP_DIR_INODE_HEADER_1(&inodep, &sinodep);
574 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
575 &next_block, &next_offset))
578 i->i_size = inodep.file_size;
579 i->i_op = &squashfs_dir_inode_ops;
580 i->i_fop = &squashfs_dir_ops;
581 i->i_mode |= S_IFDIR;
582 i->i_mtime.tv_sec = inodep.mtime;
583 i->i_atime.tv_sec = inodep.mtime;
584 i->i_ctime.tv_sec = inodep.mtime;
585 SQUASHFS_I(i)->start_block = inodep.start_block;
586 SQUASHFS_I(i)->offset = inodep.offset;
587 SQUASHFS_I(i)->u.s2.directory_index_count = 0;
588 TRACE("Directory inode %x:%x, start_block %x, offset %x\n", SQUASHFS_INODE_BLK(inode), offset,
589 inodep.start_block, inodep.offset);
592 case SQUASHFS_SYMLINK_TYPE: {
593 squashfs_symlink_inode_header_1 inodep;
596 squashfs_symlink_inode_header_1 sinodep;
598 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
599 &next_block, &next_offset))
601 SQUASHFS_SWAP_SYMLINK_INODE_HEADER_1(&inodep, &sinodep);
603 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
604 &next_block, &next_offset))
607 i->i_size = inodep.symlink_size;
608 i->i_op = &page_symlink_inode_operations;
609 i->i_data.a_ops = &squashfs_symlink_aops;
610 i->i_mode |= S_IFLNK;
611 SQUASHFS_I(i)->start_block = next_block;
612 SQUASHFS_I(i)->offset = next_offset;
613 TRACE("Symbolic link inode %x:%x, start_block %x, offset %x\n",
614 SQUASHFS_INODE_BLK(inode), offset, next_block, next_offset);
617 case SQUASHFS_BLKDEV_TYPE:
618 case SQUASHFS_CHRDEV_TYPE: {
619 squashfs_dev_inode_header_1 inodep;
622 squashfs_dev_inode_header_1 sinodep;
624 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
625 &next_block, &next_offset))
627 SQUASHFS_SWAP_DEV_INODE_HEADER_1(&inodep, &sinodep);
629 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
630 &next_block, &next_offset))
634 i->i_mode |= (inodeb.inode_type == SQUASHFS_CHRDEV_TYPE) ? S_IFCHR : S_IFBLK;
635 init_special_inode(i, i->i_mode, old_decode_dev(inodep.rdev));
636 TRACE("Device inode %x:%x, rdev %x\n", SQUASHFS_INODE_BLK(inode), offset, inodep.rdev);
639 case SQUASHFS_IPC_TYPE: {
640 squashfs_ipc_inode_header_1 inodep;
643 squashfs_ipc_inode_header_1 sinodep;
645 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
646 &next_block, &next_offset))
648 SQUASHFS_SWAP_IPC_INODE_HEADER_1(&inodep, &sinodep);
650 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
651 &next_block, &next_offset))
655 i->i_mode |= (inodep.type == SQUASHFS_FIFO_TYPE) ? S_IFIFO : S_IFSOCK;
656 i->i_uid = msBlk->uid[inodep.offset * 16 + inodeb.uid];
657 init_special_inode(i, i->i_mode, 0);
661 ERROR("Unknown inode type %d in squashfs_iget!\n", inodeb.inode_type);
665 if(inodeb.guid == 15)
668 i->i_gid = msBlk->guid[inodeb.guid];
670 insert_inode_hash(i);
674 ERROR("Unable to read inode [%x:%x]\n", block, offset);
682 static struct inode *squashfs_iget(struct super_block *s, squashfs_inode inode)
684 struct inode *i = new_inode(s);
685 squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
686 squashfs_super_block *sBlk = &msBlk->sBlk;
687 unsigned int block = SQUASHFS_INODE_BLK(inode) + sBlk->inode_table_start;
688 unsigned int offset = SQUASHFS_INODE_OFFSET(inode);
689 unsigned int next_block, next_offset;
690 squashfs_base_inode_header inodeb;
692 TRACE("Entered squashfs_iget\n");
695 squashfs_base_inode_header sinodeb;
697 if(!squashfs_get_cached_block(s, (char *) &sinodeb, block, offset,
698 sizeof(sinodeb), &next_block, &next_offset))
700 SQUASHFS_SWAP_BASE_INODE_HEADER(&inodeb, &sinodeb, sizeof(sinodeb));
702 if(!squashfs_get_cached_block(s, (char *) &inodeb, block, offset,
703 sizeof(inodeb), &next_block, &next_offset))
708 i->i_mtime.tv_sec = sBlk->mkfs_time;
709 i->i_atime.tv_sec = sBlk->mkfs_time;
710 i->i_ctime.tv_sec = sBlk->mkfs_time;
712 i->i_uid = msBlk->uid[inodeb.uid];
713 i->i_ino = SQUASHFS_MK_VFS_INODE(block - sBlk->inode_table_start, offset);
715 i->i_mode = inodeb.mode;
717 switch(inodeb.inode_type) {
718 case SQUASHFS_FILE_TYPE: {
719 squashfs_reg_inode_header inodep;
722 squashfs_reg_inode_header sinodep;
724 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
725 &next_block, &next_offset))
727 SQUASHFS_SWAP_REG_INODE_HEADER(&inodep, &sinodep);
729 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
730 &next_block, &next_offset))
733 SQUASHFS_I(i)->u.s1.fragment_start_block = SQUASHFS_INVALID_BLK;
734 if(inodep.fragment != SQUASHFS_INVALID_BLK && !get_fragment_location(s, inodep.fragment,
735 &SQUASHFS_I(i)->u.s1.fragment_start_block, &SQUASHFS_I(i)->u.s1.fragment_size))
738 SQUASHFS_I(i)->u.s1.fragment_offset = inodep.offset;
739 i->i_size = inodep.file_size;
740 i->i_fop = &generic_ro_fops;
741 if(sBlk->block_size > 4096)
742 i->i_data.a_ops = &squashfs_aops;
744 i->i_data.a_ops = &squashfs_aops_4K;
745 i->i_mode |= S_IFREG;
746 i->i_mtime.tv_sec = inodep.mtime;
747 i->i_atime.tv_sec = inodep.mtime;
748 i->i_ctime.tv_sec = inodep.mtime;
749 i->i_blocks = ((i->i_size - 1) >> 9) + 1;
750 i->i_blksize = PAGE_CACHE_SIZE;
751 SQUASHFS_I(i)->start_block = inodep.start_block;
752 SQUASHFS_I(i)->block_list_start = next_block;
753 SQUASHFS_I(i)->offset = next_offset;
754 TRACE("File inode %x:%x, start_block %x, block_list_start %x, offset %x\n",
755 SQUASHFS_INODE_BLK(inode), offset, inodep.start_block, next_block, next_offset);
758 case SQUASHFS_DIR_TYPE: {
759 squashfs_dir_inode_header inodep;
762 squashfs_dir_inode_header sinodep;
764 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
765 &next_block, &next_offset))
767 SQUASHFS_SWAP_DIR_INODE_HEADER(&inodep, &sinodep);
769 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
770 &next_block, &next_offset))
773 i->i_size = inodep.file_size;
774 i->i_op = &squashfs_dir_inode_ops;
775 i->i_fop = &squashfs_dir_ops;
776 i->i_mode |= S_IFDIR;
777 i->i_mtime.tv_sec = inodep.mtime;
778 i->i_atime.tv_sec = inodep.mtime;
779 i->i_ctime.tv_sec = inodep.mtime;
780 SQUASHFS_I(i)->start_block = inodep.start_block;
781 SQUASHFS_I(i)->offset = inodep.offset;
782 SQUASHFS_I(i)->u.s2.directory_index_count = 0;
783 TRACE("Directory inode %x:%x, start_block %x, offset %x\n", SQUASHFS_INODE_BLK(inode), offset,
784 inodep.start_block, inodep.offset);
787 case SQUASHFS_LDIR_TYPE: {
788 squashfs_ldir_inode_header inodep;
791 squashfs_ldir_inode_header sinodep;
793 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
794 &next_block, &next_offset))
796 SQUASHFS_SWAP_LDIR_INODE_HEADER(&inodep, &sinodep);
798 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
799 &next_block, &next_offset))
802 i->i_size = inodep.file_size;
803 i->i_op = &squashfs_dir_inode_ops;
804 i->i_fop = &squashfs_dir_ops;
805 i->i_mode |= S_IFDIR;
806 i->i_mtime.tv_sec = inodep.mtime;
807 i->i_atime.tv_sec = inodep.mtime;
808 i->i_ctime.tv_sec = inodep.mtime;
809 SQUASHFS_I(i)->start_block = inodep.start_block;
810 SQUASHFS_I(i)->offset = inodep.offset;
811 SQUASHFS_I(i)->u.s2.directory_index_start = next_block;
812 SQUASHFS_I(i)->u.s2.directory_index_offset = next_offset;
813 SQUASHFS_I(i)->u.s2.directory_index_count = inodep.i_count;
814 TRACE("Long directory inode %x:%x, start_block %x, offset %x\n", SQUASHFS_INODE_BLK(inode), offset,
815 inodep.start_block, inodep.offset);
818 case SQUASHFS_SYMLINK_TYPE: {
819 squashfs_symlink_inode_header inodep;
822 squashfs_symlink_inode_header sinodep;
824 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
825 &next_block, &next_offset))
827 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(&inodep, &sinodep);
829 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
830 &next_block, &next_offset))
833 i->i_size = inodep.symlink_size;
834 i->i_op = &page_symlink_inode_operations;
835 i->i_data.a_ops = &squashfs_symlink_aops;
836 i->i_mode |= S_IFLNK;
837 SQUASHFS_I(i)->start_block = next_block;
838 SQUASHFS_I(i)->offset = next_offset;
839 TRACE("Symbolic link inode %x:%x, start_block %x, offset %x\n",
840 SQUASHFS_INODE_BLK(inode), offset, next_block, next_offset);
843 case SQUASHFS_BLKDEV_TYPE:
844 case SQUASHFS_CHRDEV_TYPE: {
845 squashfs_dev_inode_header inodep;
848 squashfs_dev_inode_header sinodep;
850 if(!squashfs_get_cached_block(s, (char *) &sinodep, block, offset, sizeof(sinodep),
851 &next_block, &next_offset))
853 SQUASHFS_SWAP_DEV_INODE_HEADER(&inodep, &sinodep);
855 if(!squashfs_get_cached_block(s, (char *) &inodep, block, offset, sizeof(inodep),
856 &next_block, &next_offset))
860 i->i_mode |= (inodeb.inode_type == SQUASHFS_CHRDEV_TYPE) ? S_IFCHR : S_IFBLK;
861 init_special_inode(i, i->i_mode, old_decode_dev(inodep.rdev));
862 TRACE("Device inode %x:%x, rdev %x\n", SQUASHFS_INODE_BLK(inode), offset, inodep.rdev);
865 case SQUASHFS_FIFO_TYPE:
866 case SQUASHFS_SOCKET_TYPE: {
868 i->i_mode |= (inodeb.inode_type == SQUASHFS_FIFO_TYPE) ? S_IFIFO : S_IFSOCK;
869 init_special_inode(i, i->i_mode, 0);
873 ERROR("Unknown inode type %d in squashfs_iget!\n", inodeb.inode_type);
877 if(inodeb.guid == SQUASHFS_GUIDS)
880 i->i_gid = msBlk->guid[inodeb.guid];
882 insert_inode_hash(i);
886 ERROR("Unable to read inode [%x:%x]\n", block, offset);
893 static int squashfs_fill_super(struct super_block *s,
894 void *data, int silent)
896 squashfs_sb_info *msBlk;
897 squashfs_super_block *sBlk;
899 char b[BDEVNAME_SIZE];
901 TRACE("Entered squashfs_read_superblock\n");
903 if(!(s->s_fs_info = (void *) kmalloc(sizeof(squashfs_sb_info), GFP_KERNEL))) {
904 ERROR("Failed to allocate superblock\n");
907 msBlk = (squashfs_sb_info *) s->s_fs_info;
910 msBlk->devblksize = sb_min_blocksize(s, BLOCK_SIZE);
911 msBlk->devblksize_log2 = ffz(~msBlk->devblksize);
913 init_MUTEX(&msBlk->read_page_mutex);
914 init_MUTEX(&msBlk->block_cache_mutex);
915 init_MUTEX(&msBlk->fragment_mutex);
917 init_waitqueue_head(&msBlk->waitq);
918 init_waitqueue_head(&msBlk->fragment_wait_queue);
920 if(!read_data(s, (char *) sBlk, SQUASHFS_START, sizeof(squashfs_super_block) | SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
921 SERROR("unable to read superblock\n");
925 /* Check it is a SQUASHFS superblock */
927 if((s->s_magic = sBlk->s_magic) != SQUASHFS_MAGIC) {
928 if(sBlk->s_magic == SQUASHFS_MAGIC_SWAP) {
929 squashfs_super_block sblk;
930 WARNING("Mounting a different endian SQUASHFS filesystem on %s\n", bdevname(s->s_bdev, b));
931 SQUASHFS_SWAP_SUPER_BLOCK(&sblk, sBlk);
932 memcpy(sBlk, &sblk, sizeof(squashfs_super_block));
935 SERROR("Can't find a SQUASHFS superblock on %s\n", bdevname(s->s_bdev, b));
940 /* Check the MAJOR & MINOR versions */
941 #ifdef SQUASHFS_1_0_COMPATIBILITY
942 if((sBlk->s_major != 1) && (sBlk->s_major != 2 || sBlk->s_minor > SQUASHFS_MINOR)) {
943 SERROR("Major/Minor mismatch, filesystem is (%d:%d), I support (1 : x) or (2 : <= %d)\n",
944 sBlk->s_major, sBlk->s_minor, SQUASHFS_MINOR);
947 if(sBlk->s_major == 1)
948 sBlk->block_size = sBlk->block_size_1;
950 if(sBlk->s_major != SQUASHFS_MAJOR || sBlk->s_minor > SQUASHFS_MINOR) {
951 SERROR("Major/Minor mismatch, filesystem is (%d:%d), I support (%d: <= %d)\n",
952 sBlk->s_major, sBlk->s_minor, SQUASHFS_MAJOR, SQUASHFS_MINOR);
957 TRACE("Found valid superblock on %s\n", bdevname(s->s_bdev, b));
958 TRACE("Inodes are %scompressed\n", SQUASHFS_UNCOMPRESSED_INODES(sBlk->flags) ? "un" : "");
959 TRACE("Data is %scompressed\n", SQUASHFS_UNCOMPRESSED_DATA(sBlk->flags) ? "un" : "");
960 TRACE("Check data is %s present in the filesystem\n", SQUASHFS_CHECK_DATA(sBlk->flags) ? "" : "not");
961 TRACE("Filesystem size %d bytes\n", sBlk->bytes_used);
962 TRACE("Block size %d\n", sBlk->block_size);
963 TRACE("Number of inodes %d\n", sBlk->inodes);
964 if(sBlk->s_major > 1)
965 TRACE("Number of fragments %d\n", sBlk->fragments);
966 TRACE("Number of uids %d\n", sBlk->no_uids);
967 TRACE("Number of gids %d\n", sBlk->no_guids);
968 TRACE("sBlk->inode_table_start %x\n", sBlk->inode_table_start);
969 TRACE("sBlk->directory_table_start %x\n", sBlk->directory_table_start);
970 if(sBlk->s_major > 1)
971 TRACE("sBlk->fragment_table_start %x\n", sBlk->fragment_table_start);
972 TRACE("sBlk->uid_start %x\n", sBlk->uid_start);
974 s->s_flags |= MS_RDONLY;
975 s->s_op = &squashfs_ops;
977 /* Init inode_table block pointer array */
978 if(!(msBlk->block_cache = (squashfs_cache *) kmalloc(sizeof(squashfs_cache) * SQUASHFS_CACHED_BLKS, GFP_KERNEL))) {
979 ERROR("Failed to allocate block cache\n");
983 for(i = 0; i < SQUASHFS_CACHED_BLKS; i++)
984 msBlk->block_cache[i].block = SQUASHFS_INVALID_BLK;
986 msBlk->next_cache = 0;
988 /* Allocate read_data block */
989 msBlk->read_size = (sBlk->block_size < SQUASHFS_METADATA_SIZE) ? SQUASHFS_METADATA_SIZE : sBlk->block_size;
990 if(!(msBlk->read_data = (char *) kmalloc(msBlk->read_size, GFP_KERNEL))) {
991 ERROR("Failed to allocate read_data block\n");
995 /* Allocate read_page block */
996 if(sBlk->block_size > PAGE_CACHE_SIZE) {
997 if(!(msBlk->read_page = (char *) kmalloc(sBlk->block_size, GFP_KERNEL))) {
998 ERROR("Failed to allocate read_page block\n");
1002 msBlk->read_page = NULL;
1004 /* Allocate uid and gid tables */
1005 if(!(msBlk->uid = (squashfs_uid *) kmalloc((sBlk->no_uids +
1006 sBlk->no_guids) * sizeof(squashfs_uid), GFP_KERNEL))) {
1007 ERROR("Failed to allocate uid/gid table\n");
1010 msBlk->guid = msBlk->uid + sBlk->no_uids;
1013 squashfs_uid suid[sBlk->no_uids + sBlk->no_guids];
1015 if(!read_data(s, (char *) &suid, sBlk->uid_start, ((sBlk->no_uids + sBlk->no_guids) *
1016 sizeof(squashfs_uid)) | SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
1017 SERROR("unable to read uid/gid table\n");
1020 SQUASHFS_SWAP_DATA(msBlk->uid, suid, (sBlk->no_uids + sBlk->no_guids), (sizeof(squashfs_uid) * 8));
1022 if(!read_data(s, (char *) msBlk->uid, sBlk->uid_start, ((sBlk->no_uids + sBlk->no_guids) *
1023 sizeof(squashfs_uid)) | SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
1024 SERROR("unable to read uid/gid table\n");
1029 #ifdef SQUASHFS_1_0_COMPATIBILITY
1030 if(sBlk->s_major == 1) {
1031 msBlk->iget = squashfs_iget_1;
1032 msBlk->read_blocklist = read_blocklist_1;
1033 msBlk->fragment = NULL;
1034 msBlk->fragment_index = NULL;
1038 msBlk->iget = squashfs_iget;
1039 msBlk->read_blocklist = read_blocklist;
1041 if(!(msBlk->fragment = (struct squashfs_fragment_cache *) kmalloc(sizeof(struct squashfs_fragment_cache) * SQUASHFS_CACHED_FRAGMENTS, GFP_KERNEL))) {
1042 ERROR("Failed to allocate fragment block cache\n");
1046 for(i = 0; i < SQUASHFS_CACHED_FRAGMENTS; i++) {
1047 msBlk->fragment[i].locked = 0;
1048 msBlk->fragment[i].block = SQUASHFS_INVALID_BLK;
1049 msBlk->fragment[i].data = NULL;
1052 msBlk->next_fragment = 0;
1054 /* Allocate fragment index table */
1055 if(!(msBlk->fragment_index = (squashfs_fragment_index *) kmalloc(SQUASHFS_FRAGMENT_INDEX_BYTES(sBlk->fragments), GFP_KERNEL))) {
1056 ERROR("Failed to allocate uid/gid table\n");
1060 if(SQUASHFS_FRAGMENT_INDEX_BYTES(sBlk->fragments) &&
1061 !read_data(s, (char *) msBlk->fragment_index, sBlk->fragment_table_start,
1062 SQUASHFS_FRAGMENT_INDEX_BYTES(sBlk->fragments) | SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
1063 SERROR("unable to read fragment index table\n");
1069 squashfs_fragment_index fragment;
1071 for(i = 0; i < SQUASHFS_FRAGMENT_INDEXES(sBlk->fragments); i++) {
1072 SQUASHFS_SWAP_FRAGMENT_INDEXES((&fragment), &msBlk->fragment_index[i], 1);
1073 msBlk->fragment_index[i] = fragment;
1077 #ifdef SQUASHFS_1_0_COMPATIBILITY
1080 if(!(s->s_root = d_alloc_root((msBlk->iget)(s, sBlk->root_inode)))) {
1081 ERROR("Root inode create failed\n");
1085 TRACE("Leaving squashfs_read_super\n");
1089 kfree(msBlk->fragment_index);
1091 kfree(msBlk->fragment);
1095 kfree(msBlk->read_page);
1097 kfree(msBlk->read_data);
1099 kfree(msBlk->block_cache);
1101 kfree(s->s_fs_info);
1102 s->s_fs_info = NULL;
1107 static int squashfs_statfs(struct super_block *s, struct kstatfs *buf)
1109 squashfs_super_block *sBlk = &((squashfs_sb_info *)s->s_fs_info)->sBlk;
1111 TRACE("Entered squashfs_statfs\n");
1112 buf->f_type = SQUASHFS_MAGIC;
1113 buf->f_bsize = sBlk->block_size;
1114 buf->f_blocks = ((sBlk->bytes_used - 1) >> sBlk->block_log) + 1;
1115 buf->f_bfree = buf->f_bavail = 0;
1116 buf->f_files = sBlk->inodes;
1118 buf->f_namelen = SQUASHFS_NAME_LEN;
1123 static int squashfs_symlink_readpage(struct file *file, struct page *page)
1125 struct inode *inode = page->mapping->host;
1126 int index = page->index << PAGE_CACHE_SHIFT, length, bytes;
1127 unsigned int block = SQUASHFS_I(inode)->start_block;
1128 int offset = SQUASHFS_I(inode)->offset;
1129 void *pageaddr = kmap(page);
1131 TRACE("Entered squashfs_symlink_readpage, page index %d, start block %x, offset %x\n",
1132 page->index, SQUASHFS_I(inode)->start_block, SQUASHFS_I(inode)->offset);
1134 for(length = 0; length < index; length += bytes) {
1135 if(!(bytes = squashfs_get_cached_block(inode->i_sb, NULL, block, offset,
1136 PAGE_CACHE_SIZE, &block, &offset))) {
1137 ERROR("Unable to read symbolic link [%x:%x]\n", block, offset);
1142 if(length != index) {
1143 ERROR("(squashfs_symlink_readpage) length != index\n");
1148 bytes = (inode->i_size - length) > PAGE_CACHE_SIZE ? PAGE_CACHE_SIZE : inode->i_size - length;
1149 if(!(bytes = squashfs_get_cached_block(inode->i_sb, pageaddr, block, offset, bytes, &block, &offset)))
1150 ERROR("Unable to read symbolic link [%x:%x]\n", block, offset);
1153 memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1155 flush_dcache_page(page);
1156 SetPageUptodate(page);
1165 #ifdef SQUASHFS_1_0_COMPATIBILITY
1166 static unsigned int read_blocklist_1(struct inode *inode, int index, int readahead_blks,
1167 char *block_list, unsigned short **block_p, unsigned int *bsize)
1169 squashfs_sb_info *msBlk = (squashfs_sb_info *)inode->i_sb->s_fs_info;
1170 unsigned short *block_listp;
1172 int block_ptr = SQUASHFS_I(inode)->block_list_start;
1173 int offset = SQUASHFS_I(inode)->offset;
1174 unsigned int block = SQUASHFS_I(inode)->start_block;
1177 int blocks = (index + readahead_blks - i);
1178 if(blocks > (SIZE >> 1)) {
1179 if((index - i) <= (SIZE >> 1))
1186 unsigned char sblock_list[SIZE];
1187 if(!squashfs_get_cached_block(inode->i_sb, (char *) sblock_list, block_ptr, offset, blocks << 1, &block_ptr, &offset)) {
1188 ERROR("Unable to read block list [%d:%x]\n", block_ptr, offset);
1191 SQUASHFS_SWAP_SHORTS(((unsigned short *)block_list), ((unsigned short *)sblock_list), blocks);
1193 if(!squashfs_get_cached_block(inode->i_sb, (char *) block_list, block_ptr, offset, blocks << 1, &block_ptr, &offset)) {
1194 ERROR("Unable to read block list [%d:%x]\n", block_ptr, offset);
1197 for(block_listp = (unsigned short *) block_list; i < index && blocks; i ++, block_listp ++, blocks --)
1198 block += SQUASHFS_COMPRESSED_SIZE(*block_listp);
1199 if(blocks >= readahead_blks)
1204 *bsize = SQUASHFS_COMPRESSED_SIZE(*block_listp) | (!SQUASHFS_COMPRESSED(*block_listp) ? SQUASHFS_COMPRESSED_BIT_BLOCK : 0);
1206 *block_p = block_listp;
1212 static unsigned int read_blocklist(struct inode *inode, int index, int readahead_blks,
1213 char *block_list, unsigned short **block_p, unsigned int *bsize)
1215 squashfs_sb_info *msBlk = (squashfs_sb_info *)inode->i_sb->s_fs_info;
1216 unsigned int *block_listp;
1218 int block_ptr = SQUASHFS_I(inode)->block_list_start;
1219 int offset = SQUASHFS_I(inode)->offset;
1220 unsigned int block = SQUASHFS_I(inode)->start_block;
1223 int blocks = (index + readahead_blks - i);
1224 if(blocks > (SIZE >> 2)) {
1225 if((index - i) <= (SIZE >> 2))
1232 unsigned char sblock_list[SIZE];
1233 if(!squashfs_get_cached_block(inode->i_sb, (char *) sblock_list, block_ptr, offset, blocks << 2, &block_ptr, &offset)) {
1234 ERROR("Unable to read block list [%d:%x]\n", block_ptr, offset);
1237 SQUASHFS_SWAP_INTS(((unsigned int *)block_list), ((unsigned int *)sblock_list), blocks);
1239 if(!squashfs_get_cached_block(inode->i_sb, (char *) block_list, block_ptr, offset, blocks << 2, &block_ptr, &offset)) {
1240 ERROR("Unable to read block list [%d:%x]\n", block_ptr, offset);
1243 for(block_listp = (unsigned int *) block_list; i < index && blocks; i ++, block_listp ++, blocks --)
1244 block += SQUASHFS_COMPRESSED_SIZE_BLOCK(*block_listp);
1245 if(blocks >= readahead_blks)
1249 *bsize = *block_listp;
1254 static int squashfs_readpage(struct file *file, struct page *page)
1256 struct inode *inode = page->mapping->host;
1257 squashfs_sb_info *msBlk = (squashfs_sb_info *)inode->i_sb->s_fs_info;
1258 squashfs_super_block *sBlk = &msBlk->sBlk;
1259 unsigned char block_list[SIZE];
1260 unsigned int bsize, block, i = 0, bytes = 0, byte_offset = 0;
1261 int index = page->index >> (sBlk->block_log - PAGE_CACHE_SHIFT);
1262 void *pageaddr = kmap(page);
1263 struct squashfs_fragment_cache *fragment = NULL;
1264 char *data_ptr = msBlk->read_page;
1266 int mask = (1 << (sBlk->block_log - PAGE_CACHE_SHIFT)) - 1;
1267 int start_index = page->index & ~mask;
1268 int end_index = start_index | mask;
1270 TRACE("Entered squashfs_readpage, page index %x, start block %x\n", (unsigned int) page->index,
1271 SQUASHFS_I(inode)->start_block);
1273 if(page->index >= ((inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT)) {
1277 if(SQUASHFS_I(inode)->u.s1.fragment_start_block == SQUASHFS_INVALID_BLK || index < (inode->i_size >> sBlk->block_log)) {
1278 if((block = (msBlk->read_blocklist)(inode, index, 1, block_list, NULL, &bsize)) == 0)
1281 down(&msBlk->read_page_mutex);
1282 if(!(bytes = read_data(inode->i_sb, msBlk->read_page, block, bsize, NULL))) {
1283 ERROR("Unable to read page, block %x, size %x\n", block, bsize);
1284 up(&msBlk->read_page_mutex);
1288 if((fragment = get_cached_fragment(inode->i_sb, SQUASHFS_I(inode)->u.s1.fragment_start_block, SQUASHFS_I(inode)->u.s1.fragment_size)) == NULL) {
1289 ERROR("Unable to read page, block %x, size %x\n", SQUASHFS_I(inode)->u.s1.fragment_start_block, (int) SQUASHFS_I(inode)->u.s1.fragment_size);
1292 bytes = SQUASHFS_I(inode)->u.s1.fragment_offset + (inode->i_size & (sBlk->block_size - 1));
1293 byte_offset = SQUASHFS_I(inode)->u.s1.fragment_offset;
1294 data_ptr = fragment->data;
1297 for(i = start_index; i <= end_index && byte_offset < bytes; i++, byte_offset += PAGE_CACHE_SIZE) {
1298 struct page *push_page;
1299 int available_bytes = (bytes - byte_offset) > PAGE_CACHE_SIZE ? PAGE_CACHE_SIZE : bytes - byte_offset;
1301 TRACE("bytes %d, i %d, byte_offset %d, available_bytes %d\n", bytes, i, byte_offset, available_bytes);
1303 if(i == page->index) {
1304 memcpy(pageaddr, data_ptr + byte_offset, available_bytes);
1305 memset(pageaddr + available_bytes, 0, PAGE_CACHE_SIZE - available_bytes);
1307 flush_dcache_page(page);
1308 SetPageUptodate(page);
1310 } else if((push_page = grab_cache_page_nowait(page->mapping, i))) {
1311 void *pageaddr = kmap(push_page);
1312 memcpy(pageaddr, data_ptr + byte_offset, available_bytes);
1313 memset(pageaddr + available_bytes, 0, PAGE_CACHE_SIZE - available_bytes);
1315 flush_dcache_page(push_page);
1316 SetPageUptodate(push_page);
1317 unlock_page(push_page);
1318 page_cache_release(push_page);
1322 if(SQUASHFS_I(inode)->u.s1.fragment_start_block == SQUASHFS_INVALID_BLK || index < (inode->i_size >> sBlk->block_log))
1323 up(&msBlk->read_page_mutex);
1325 release_cached_fragment(msBlk, fragment);
1330 memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1332 flush_dcache_page(page);
1333 SetPageUptodate(page);
1340 static int squashfs_readpage4K(struct file *file, struct page *page)
1342 struct inode *inode = page->mapping->host;
1343 squashfs_sb_info *msBlk = (squashfs_sb_info *)inode->i_sb->s_fs_info;
1344 squashfs_super_block *sBlk = &msBlk->sBlk;
1345 unsigned char block_list[SIZE];
1346 unsigned int bsize, block, bytes = 0;
1347 void *pageaddr = kmap(page);
1349 TRACE("Entered squashfs_readpage4K, page index %x, start block %x\n", (unsigned int) page->index,
1350 SQUASHFS_I(inode)->start_block);
1352 if(page->index >= ((inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT)) {
1356 if(SQUASHFS_I(inode)->u.s1.fragment_start_block == SQUASHFS_INVALID_BLK || page->index < (inode->i_size >> sBlk->block_log)) {
1357 block = (msBlk->read_blocklist)(inode, page->index, 1, block_list, NULL, &bsize);
1359 if(!(bytes = read_data(inode->i_sb, pageaddr, block, bsize, NULL)))
1360 ERROR("Unable to read page, block %x, size %x\n", block, bsize);
1362 struct squashfs_fragment_cache *fragment;
1364 if((fragment = get_cached_fragment(inode->i_sb, SQUASHFS_I(inode)->u.s1.fragment_start_block, SQUASHFS_I(inode)->u.s1.fragment_size)) == NULL)
1365 ERROR("Unable to read page, block %x, size %x\n", SQUASHFS_I(inode)->u.s1.fragment_start_block, (int) SQUASHFS_I(inode)->u.s1.fragment_size);
1367 bytes = inode->i_size & (sBlk->block_size - 1);
1368 memcpy(pageaddr, fragment->data + SQUASHFS_I(inode)->u.s1.fragment_offset, bytes);
1369 release_cached_fragment(msBlk, fragment);
1374 memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1376 flush_dcache_page(page);
1377 SetPageUptodate(page);
1384 #ifdef SQUASHFS_1_0_COMPATIBILITY
1385 static int squashfs_readpage_lessthan4K(struct file *file, struct page *page)
1387 struct inode *inode = page->mapping->host;
1388 squashfs_sb_info *msBlk = (squashfs_sb_info *)inode->i_sb->s_fs_info;
1389 squashfs_super_block *sBlk = &msBlk->sBlk;
1390 unsigned char block_list[SIZE];
1391 unsigned short *block_listp, block, bytes = 0;
1392 int index = page->index << (PAGE_CACHE_SHIFT - sBlk->block_log);
1393 int file_blocks = ((inode->i_size - 1) >> sBlk->block_log) + 1;
1394 int readahead_blks = 1 << (PAGE_CACHE_SHIFT - sBlk->block_log);
1395 void *pageaddr = kmap(page);
1397 int i_end = index + (1 << (PAGE_CACHE_SHIFT - sBlk->block_log));
1400 TRACE("Entered squashfs_readpage_lessthan4K, page index %x, start block %x\n", (unsigned int) page->index,
1401 SQUASHFS_I(inode)->start_block);
1403 block = read_blocklist_1(inode, index, readahead_blks, block_list, &block_listp, NULL);
1405 if(i_end > file_blocks)
1406 i_end = file_blocks;
1408 while(index < i_end) {
1409 int c_byte = !SQUASHFS_COMPRESSED(*block_listp) ? SQUASHFS_COMPRESSED_SIZE(*block_listp) | SQUASHFS_COMPRESSED_BIT_BLOCK : *block_listp;
1410 if(!(byte = read_data(inode->i_sb, pageaddr, block, c_byte, NULL))) {
1411 ERROR("Unable to read page, block %x, size %x\n", block, *block_listp);
1414 block += SQUASHFS_COMPRESSED_SIZE(*block_listp);
1422 memset(pageaddr, 0, PAGE_CACHE_SIZE - bytes);
1424 flush_dcache_page(page);
1425 SetPageUptodate(page);
1433 static int get_dir_index_using_offset(struct super_block *s, unsigned int *next_block,
1434 unsigned int *next_offset, unsigned int index_start, unsigned int index_offset,
1435 int i_count, long long f_pos)
1437 squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
1438 squashfs_super_block *sBlk = &msBlk->sBlk;
1440 squashfs_dir_index index;
1442 TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %d\n", i_count, (unsigned int) f_pos);
1447 for(i = 0; i < i_count; i++) {
1449 squashfs_dir_index sindex;
1450 squashfs_get_cached_block(s, (char *) &sindex, index_start, index_offset,
1451 sizeof(sindex), &index_start, &index_offset);
1452 SQUASHFS_SWAP_DIR_INDEX(&index, &sindex);
1454 squashfs_get_cached_block(s, (char *) &index, index_start, index_offset,
1455 sizeof(index), &index_start, &index_offset);
1457 if(index.index > f_pos)
1460 squashfs_get_cached_block(s, NULL, index_start, index_offset,
1461 index.size + 1, &index_start, &index_offset);
1463 length = index.index;
1464 *next_block = index.start_block + sBlk->directory_table_start;
1467 *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
1472 static int get_dir_index_using_name(struct super_block *s, unsigned int *next_block,
1473 unsigned int *next_offset, unsigned int index_start, unsigned int index_offset,
1474 int i_count, const char *name, int size)
1476 squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
1477 squashfs_super_block *sBlk = &msBlk->sBlk;
1479 char buffer[sizeof(squashfs_dir_index) + SQUASHFS_NAME_LEN + 1];
1480 squashfs_dir_index *index = (squashfs_dir_index *) buffer;
1481 char str[SQUASHFS_NAME_LEN + 1];
1483 TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
1485 strncpy(str, name, size);
1488 for(i = 0; i < i_count; i++) {
1490 squashfs_dir_index sindex;
1491 squashfs_get_cached_block(s, (char *) &sindex, index_start, index_offset,
1492 sizeof(sindex), &index_start, &index_offset);
1493 SQUASHFS_SWAP_DIR_INDEX(index, &sindex);
1495 squashfs_get_cached_block(s, (char *) index, index_start, index_offset,
1496 sizeof(squashfs_dir_index), &index_start, &index_offset);
1498 squashfs_get_cached_block(s, index->name, index_start, index_offset,
1499 index->size + 1, &index_start, &index_offset);
1501 index->name[index->size + 1] = '\0';
1503 if(strcmp(index->name, str) > 0)
1506 length = index->index;
1507 *next_block = index->start_block + sBlk->directory_table_start;
1510 *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
1515 static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
1517 struct inode *i = file->f_dentry->d_inode;
1518 squashfs_sb_info *msBlk = (squashfs_sb_info *)i->i_sb->s_fs_info;
1519 squashfs_super_block *sBlk = &msBlk->sBlk;
1520 int next_block = SQUASHFS_I(i)->start_block + sBlk->directory_table_start, next_offset =
1521 SQUASHFS_I(i)->offset, length = 0, dirs_read = 0, dir_count;
1522 squashfs_dir_header dirh;
1523 char buffer[sizeof(squashfs_dir_entry) + SQUASHFS_NAME_LEN + 1];
1524 squashfs_dir_entry *dire = (squashfs_dir_entry *) buffer;
1526 TRACE("Entered squashfs_readdir [%x:%x]\n", next_block, next_offset);
1530 length = get_dir_index_using_offset(i->i_sb, &next_block, &next_offset, SQUASHFS_I(i)->u.s2.directory_index_start,
1531 SQUASHFS_I(i)->u.s2.directory_index_offset, SQUASHFS_I(i)->u.s2.directory_index_count, file->f_pos);
1533 while(length < i->i_size) {
1534 /* read directory header */
1536 squashfs_dir_header sdirh;
1537 if(!squashfs_get_cached_block(i->i_sb, (char *) &sdirh, next_block,
1538 next_offset, sizeof(sdirh), &next_block, &next_offset))
1540 length += sizeof(sdirh);
1541 SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
1543 if(!squashfs_get_cached_block(i->i_sb, (char *) &dirh, next_block,
1544 next_offset, sizeof(dirh), &next_block, &next_offset))
1546 length += sizeof(dirh);
1549 dir_count = dirh.count + 1;
1550 while(dir_count--) {
1552 squashfs_dir_entry sdire;
1553 if(!squashfs_get_cached_block(i->i_sb, (char *) &sdire, next_block,
1554 next_offset, sizeof(sdire), &next_block, &next_offset))
1556 length += sizeof(sdire);
1557 SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
1559 if(!squashfs_get_cached_block(i->i_sb, (char *) dire, next_block,
1560 next_offset, sizeof(*dire), &next_block, &next_offset))
1562 length += sizeof(*dire);
1565 if(!squashfs_get_cached_block(i->i_sb, dire->name, next_block,
1566 next_offset, dire->size + 1, &next_block, &next_offset))
1568 length += dire->size + 1;
1570 if(file->f_pos >= length)
1573 dire->name[dire->size + 1] = '\0';
1575 TRACE("Calling filldir(%x, %s, %d, %d, %x:%x, %d)\n", (unsigned int) dirent,
1576 dire->name, dire->size + 1, (int) file->f_pos,
1577 dirh.start_block, dire->offset, squashfs_filetype_table[dire->type]);
1579 if(filldir(dirent, dire->name, dire->size + 1, file->f_pos, SQUASHFS_MK_VFS_INODE(dirh.start_block,
1580 dire->offset), squashfs_filetype_table[dire->type]) < 0) {
1581 TRACE("Filldir returned less than 0\n");
1586 file->f_pos = length;
1596 ERROR("Unable to read directory block [%x:%x]\n", next_block, next_offset);
1601 static struct dentry *squashfs_lookup(struct inode *i, struct dentry *dentry, struct nameidata *nd)
1603 const unsigned char *name =dentry->d_name.name;
1604 int len = dentry->d_name.len;
1605 struct inode *inode = NULL;
1606 squashfs_sb_info *msBlk = (squashfs_sb_info *)i->i_sb->s_fs_info;
1607 squashfs_super_block *sBlk = &msBlk->sBlk;
1608 int next_block = SQUASHFS_I(i)->start_block + sBlk->directory_table_start, next_offset =
1609 SQUASHFS_I(i)->offset, length = 0, dir_count;
1610 squashfs_dir_header dirh;
1611 char buffer[sizeof(squashfs_dir_entry) + SQUASHFS_NAME_LEN];
1612 squashfs_dir_entry *dire = (squashfs_dir_entry *) buffer;
1613 int squashfs_2_1 = sBlk->s_major == 2 && sBlk->s_minor == 1;
1615 TRACE("Entered squashfs_lookup [%x:%x]\n", next_block, next_offset);
1619 length = get_dir_index_using_name(i->i_sb, &next_block, &next_offset, SQUASHFS_I(i)->u.s2.directory_index_start,
1620 SQUASHFS_I(i)->u.s2.directory_index_offset, SQUASHFS_I(i)->u.s2.directory_index_count, name, len);
1622 while(length < i->i_size) {
1623 /* read directory header */
1625 squashfs_dir_header sdirh;
1626 if(!squashfs_get_cached_block(i->i_sb, (char *) &sdirh, next_block, next_offset,
1627 sizeof(sdirh), &next_block, &next_offset))
1629 length += sizeof(sdirh);
1630 SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
1632 if(!squashfs_get_cached_block(i->i_sb, (char *) &dirh, next_block, next_offset,
1633 sizeof(dirh), &next_block, &next_offset))
1635 length += sizeof(dirh);
1638 dir_count = dirh.count + 1;
1639 while(dir_count--) {
1641 squashfs_dir_entry sdire;
1642 if(!squashfs_get_cached_block(i->i_sb, (char *) &sdire,
1643 next_block,next_offset, sizeof(sdire), &next_block, &next_offset))
1645 length += sizeof(sdire);
1646 SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
1648 if(!squashfs_get_cached_block(i->i_sb, (char *) dire,
1649 next_block,next_offset, sizeof(*dire), &next_block, &next_offset))
1651 length += sizeof(*dire);
1654 if(!squashfs_get_cached_block(i->i_sb, dire->name,
1655 next_block, next_offset, dire->size + 1, &next_block, &next_offset))
1657 length += dire->size + 1;
1659 if(squashfs_2_1 && name[0] < dire->name[0])
1662 if((len == dire->size + 1) && !strncmp(name, dire->name, len)) {
1663 squashfs_inode ino = SQUASHFS_MKINODE(dirh.start_block, dire->offset);
1665 TRACE("calling squashfs_iget for directory entry %s, inode %x:%x\n",
1666 name, dirh.start_block, dire->offset);
1668 inode = (msBlk->iget)(i->i_sb, ino);
1676 d_add(dentry, inode);
1681 ERROR("Unable to read directory block [%x:%x]\n", next_block, next_offset);
1686 static void squashfs_put_super(struct super_block *s)
1691 squashfs_sb_info *sbi = (squashfs_sb_info *) s->s_fs_info;
1692 if(sbi->block_cache) {
1693 for(i = 0; i < SQUASHFS_CACHED_BLKS; i++)
1694 if(sbi->block_cache[i].block != SQUASHFS_INVALID_BLK)
1695 kfree(sbi->block_cache[i].data);
1696 kfree(sbi->block_cache);
1698 if(sbi->read_data) kfree(sbi->read_data);
1699 if(sbi->read_page) kfree(sbi->read_page);
1700 if(sbi->uid) kfree(sbi->uid);
1702 for(i = 0; i < SQUASHFS_CACHED_FRAGMENTS; i++)
1703 if(sbi->fragment[i].data != NULL)
1704 SQUASHFS_FREE(sbi->fragment[i].data);
1705 kfree(sbi->fragment);
1707 if(sbi->fragment_index) kfree(sbi->fragment_index);
1708 kfree(s->s_fs_info);
1709 s->s_fs_info = NULL;
1714 static struct super_block *squashfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
1716 return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super);
1720 static int __init init_squashfs_fs(void)
1722 int err = init_inodecache();
1726 printk(KERN_INFO "Squashfs 2.2-r2 (released 2005/09/08) (C) 2002-2005 Phillip Lougher\n");
1728 if(!(stream.workspace = (char *) vmalloc(zlib_inflate_workspacesize()))) {
1729 ERROR("Failed to allocate zlib workspace\n");
1730 destroy_inodecache();
1734 if((err = register_filesystem(&squashfs_fs_type))) {
1735 vfree(stream.workspace);
1736 destroy_inodecache();
1743 static void __exit exit_squashfs_fs(void)
1745 vfree(stream.workspace);
1746 unregister_filesystem(&squashfs_fs_type);
1747 destroy_inodecache();
1751 static kmem_cache_t * squashfs_inode_cachep;
1754 static struct inode *squashfs_alloc_inode(struct super_block *sb)
1756 struct squashfs_inode_info *ei;
1757 ei = (struct squashfs_inode_info *)kmem_cache_alloc(squashfs_inode_cachep, SLAB_KERNEL);
1760 return &ei->vfs_inode;
1764 static void squashfs_destroy_inode(struct inode *inode)
1766 kmem_cache_free(squashfs_inode_cachep, SQUASHFS_I(inode));
1770 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1772 struct squashfs_inode_info *ei = (struct squashfs_inode_info *) foo;
1774 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1775 SLAB_CTOR_CONSTRUCTOR)
1776 inode_init_once(&ei->vfs_inode);
1780 static int init_inodecache(void)
1782 squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
1783 sizeof(struct squashfs_inode_info),
1784 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1786 if (squashfs_inode_cachep == NULL)
1792 static void destroy_inodecache(void)
1794 if (kmem_cache_destroy(squashfs_inode_cachep))
1795 printk(KERN_INFO "squashfs_inode_cache: not all structures were freed\n");
1799 module_init(init_squashfs_fs);
1800 module_exit(exit_squashfs_fs);
1801 MODULE_DESCRIPTION("squashfs, a compressed read-only filesystem");
1802 MODULE_AUTHOR("Phillip Lougher <phillip@lougher.demon.co.uk>");
1803 MODULE_LICENSE("GPL");