This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / squashfs / inode.c
1 /*
2  * Squashfs - a compressed read only filesystem for Linux
3  *
4  * Copyright (c) 2002, 2003, 2004, 2005 Phillip Lougher <phillip@lougher.demon.co.uk>
5  *
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.
10  *
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.
15  *
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.
19  *
20  * inode.c
21  */
22
23 #define SQUASHFS_1_0_COMPATIBILITY
24
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>
30 #include <linux/fs.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>
45
46 #ifdef SQUASHFS_TRACE
47 #define TRACE(s, args...)                               printk(KERN_NOTICE "SQUASHFS: "s, ## args)
48 #else
49 #define TRACE(s, args...)                               {}
50 #endif
51
52 #define ERROR(s, args...)                               printk(KERN_ERR "SQUASHFS error: "s, ## args)
53
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)
56
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);
78
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);
84 #endif
85
86 DECLARE_MUTEX(read_data_mutex);
87
88 static z_stream stream;
89
90 static struct file_system_type squashfs_fs_type = {
91         .owner = THIS_MODULE,
92         .name = "squashfs",
93         .get_sb = squashfs_get_sb,
94         .kill_sb = kill_block_super,
95         .fs_flags = FS_REQUIRES_DEV
96         };
97
98 static unsigned char squashfs_filetype_table[] = {
99         DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
100 };
101
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,
107 };
108
109 static struct address_space_operations squashfs_symlink_aops = {
110         .readpage = squashfs_symlink_readpage
111 };
112
113 static struct address_space_operations squashfs_aops = {
114         .readpage = squashfs_readpage
115 };
116
117 static struct address_space_operations squashfs_aops_4K = {
118         .readpage = squashfs_readpage4K
119 };
120
121 #ifdef SQUASHFS_1_0_COMPATIBILITY
122 static struct address_space_operations squashfs_aops_lessthan4K = {
123         .readpage = squashfs_readpage_lessthan4K
124 };
125 #endif
126
127 static struct file_operations squashfs_dir_ops = {
128         .read = generic_read_dir,
129         .readdir = squashfs_readdir
130 };
131
132 static struct inode_operations squashfs_dir_inode_ops = {
133         .lookup = squashfs_lookup
134 };
135
136
137 static inline struct squashfs_inode_info *SQUASHFS_I(struct inode *inode)
138 {
139         return list_entry(inode, struct squashfs_inode_info, vfs_inode);
140 }
141
142
143 static struct buffer_head *get_block_length(struct super_block *s,
144                                 int *cur_index, int *offset, int *c_byte)
145 {
146         squashfs_sb_info *msblk = s->s_fs_info;
147         unsigned short temp;
148         struct buffer_head *bh;
149
150         if (!(bh = sb_bread(s, *cur_index)))
151                 goto out;
152
153         if (msblk->devblksize - *offset == 1) {
154                 if (msblk->swap)
155                         ((unsigned char *) &temp)[1] = *((unsigned char *)
156                                 (bh->b_data + *offset));
157                 else
158                         ((unsigned char *) &temp)[0] = *((unsigned char *)
159                                 (bh->b_data + *offset));
160                 brelse(bh);
161                 if (!(bh = sb_bread(s, ++(*cur_index))))
162                         goto out;
163                 if (msblk->swap)
164                         ((unsigned char *) &temp)[0] = *((unsigned char *)
165                                 bh->b_data); 
166                 else
167                         ((unsigned char *) &temp)[1] = *((unsigned char *)
168                                 bh->b_data); 
169                 *c_byte = temp;
170                 *offset = 1;
171         } else {
172                 if (msblk->swap) {
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)); 
177                 } else {
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)); 
182                 }
183                 *c_byte = temp;
184                 *offset += 2;
185         }
186
187         if (SQUASHFS_CHECK_DATA(msblk->sBlk.flags)) {
188                 if (*offset == msblk->devblksize) {
189                         brelse(bh);
190                         if (!(bh = sb_bread(s, ++(*cur_index))))
191                                 goto out;
192                         *offset = 0;
193                 }
194                 if (*((unsigned char *) (bh->b_data + *offset)) !=
195                                                 SQUASHFS_MARKER_BYTE) {
196                         ERROR("Metadata block marker corrupt @ %x\n",
197                                                 *cur_index);
198                         brelse(bh);
199                         goto out;
200                 }
201                 (*offset)++;
202         }
203         return bh;
204
205 out:
206         return NULL;
207 }
208
209
210 static unsigned int read_data(struct super_block *s, char *buffer,
211                 unsigned int index, unsigned int length, unsigned int *next_index)
212 {
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;
218         char *c_buffer;
219         unsigned int compressed;
220         unsigned int c_byte = length;
221
222         if(c_byte) {
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);
227
228                 TRACE("Block @ 0x%x, %scompressed size %d\n", index, compressed ? "" : "un", (unsigned int) c_byte);
229
230                 if(!(bh[0] = sb_getblk(s, cur_index)))
231                         goto block_release;
232                 for(b = 1; bytes < c_byte; b++) {
233                         if(!(bh[b] = sb_getblk(s, ++cur_index)))
234                                 goto block_release;
235                         bytes += msBlk->devblksize;
236                 }
237                 ll_rw_block(READ, b, bh);
238         } else {
239                 if(!(bh[0] = get_block_length(s, &cur_index, &offset, &c_byte)))
240                         goto read_failure;
241
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);
246
247                 TRACE("Block @ 0x%x, %scompressed size %d\n", index, compressed ? "" : "un", (unsigned int) c_byte);
248
249                 for(b = 1; bytes < c_byte; b++) {
250                         if(!(bh[b] = sb_getblk(s, ++cur_index)))
251                                 goto block_release;
252                         bytes += msBlk->devblksize;
253                 }
254                 ll_rw_block(READ, b - 1, bh + 1);
255         }
256
257         if(compressed)
258                 down(&read_data_mutex);
259
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]))
264                         goto block_release;
265                 memcpy(c_buffer + bytes, bh[k]->b_data + offset, avail_bytes);
266                 bytes += avail_bytes;
267                 offset = 0;
268                 brelse(bh[k]);
269         }
270
271         /*
272          * uncompress block
273          */
274         if(compressed) {
275                 int zlib_err;
276
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);
285                         bytes = 0;
286                 } else
287                         bytes = stream.total_out;
288                 up(&read_data_mutex);
289         }
290
291         if(next_index)
292                 *next_index = index + c_byte + (length ? 0 : (SQUASHFS_CHECK_DATA(msBlk->sBlk.flags) ? 3 : 2));
293
294         return bytes;
295
296 block_release:
297         while(--b >= 0) brelse(bh[b]);
298
299 read_failure:
300         ERROR("sb_bread failed reading block 0x%x\n", cur_index);
301         return 0;
302 }
303
304
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)
308 {
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;
312
313         TRACE("Entered squashfs_get_cached_block [%x:%x]\n", block, offset);
314
315         for(;;) {
316                 for(i = 0; i < SQUASHFS_CACHED_BLKS; i++) 
317                         if(msBlk->block_cache[i].block == block)
318                                 break; 
319                 
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)
325                                         break;
326                         if(n == 0) {
327                                 wait_queue_t wait;
328
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);
333                                 schedule();
334                                 set_current_state(TASK_RUNNING);
335                                 remove_wait_queue(&msBlk->waitq, &wait);
336                                 continue;
337                         }
338                         msBlk->next_cache = (i + 1) % SQUASHFS_CACHED_BLKS;
339
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);
345                                         return 0;
346                                 }
347                         }
348         
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,
352                                                         &next_index))) {
353                                 ERROR("Unable to read cache block [%x:%x]\n", block, offset);
354                                 return 0;
355                         }
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);
361                 }
362
363                 if(msBlk->block_cache[i].block != block) {
364                         up(&msBlk->block_cache_mutex);
365                         continue;
366                 }
367
368                 if((bytes = msBlk->block_cache[i].length - offset) >= length) {
369                         if(buffer)
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;
373                                 *next_offset = 0;
374                         } else {
375                                 *next_block = block;
376                                 *next_offset = offset + length;
377                         }
378         
379                         up(&msBlk->block_cache_mutex);
380                         return return_length;
381                 } else {
382                         if(buffer) {
383                                 memcpy(buffer, msBlk->block_cache[i].data + offset, bytes);
384                                 buffer += bytes;
385                         }
386                         block = msBlk->block_cache[i].next_index;
387                         up(&msBlk->block_cache_mutex);
388                         length -= bytes;
389                         offset = 0;
390                 }
391         }
392 }
393
394
395 static int get_fragment_location(struct super_block *s, unsigned int fragment, unsigned int *fragment_start_block, unsigned int *fragment_size)
396 {
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;
401
402         if(msBlk->swap) {
403                 squashfs_fragment_entry sfragment_entry;
404
405                 if(!squashfs_get_cached_block(s, (char *) &sfragment_entry, start_block, offset,
406                                         sizeof(sfragment_entry), &start_block, &offset))
407                         return 0;
408                 SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_entry, &sfragment_entry);
409         } else
410                 if(!squashfs_get_cached_block(s, (char *) &fragment_entry, start_block, offset,
411                                         sizeof(fragment_entry), &start_block, &offset))
412                         return 0;
413
414         *fragment_start_block = fragment_entry.start_block;
415         *fragment_size = fragment_entry.size;
416
417         return 1;
418 }
419
420
421 void release_cached_fragment(squashfs_sb_info *msBlk, struct squashfs_fragment_cache *fragment)
422 {
423         down(&msBlk->fragment_mutex);
424         fragment->locked --;
425         wake_up(&msBlk->fragment_wait_queue);
426         up(&msBlk->fragment_mutex);
427 }
428
429
430 struct squashfs_fragment_cache *get_cached_fragment(struct super_block *s, unsigned int start_block, int length)
431 {
432         int i, n;
433         squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
434
435         for(;;) {
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);
441
442                         if(n == 0) {
443                                 wait_queue_t wait;
444
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);
449                                 schedule();
450                                 set_current_state(TASK_RUNNING);
451                                 remove_wait_queue(&msBlk->fragment_wait_queue, &wait);
452                                 continue;
453                         }
454                         msBlk->next_fragment = (msBlk->next_fragment + 1) % SQUASHFS_CACHED_FRAGMENTS;
455                         
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);
461                                         return NULL;
462                                 }
463
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,
468                                                         NULL))) {
469                                 ERROR("Unable to read fragment cache block [%x]\n", start_block);
470                                 msBlk->fragment[i].locked = 0;
471                                 return NULL;
472                         }
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];
476                 }
477
478                 msBlk->fragment[i].locked ++;
479                 up(&msBlk->fragment_mutex);
480                 
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];
483         }
484 }
485
486
487 #ifdef SQUASHFS_1_0_COMPATIBILITY
488 static struct inode *squashfs_iget_1(struct super_block *s, squashfs_inode inode)
489 {
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;
497
498         TRACE("Entered squashfs_iget_1\n");
499
500         if(msBlk->swap) {
501                 squashfs_base_inode_header_1 sinodeb;
502
503                 if(!squashfs_get_cached_block(s, (char *) &sinodeb, block,  offset,
504                                         sizeof(sinodeb), &next_block, &next_offset))
505                         goto failed_read;
506                 SQUASHFS_SWAP_BASE_INODE_HEADER_1(&inodeb, &sinodeb, sizeof(sinodeb));
507         } else
508                 if(!squashfs_get_cached_block(s, (char *) &inodeb, block,  offset,
509                                         sizeof(inodeb), &next_block, &next_offset))
510                         goto failed_read;
511
512         i->i_nlink = 1;
513
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;
517
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);
521
522         i->i_mode = inodeb.mode;
523
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;
527
528                         if(msBlk->swap) {
529                                 squashfs_reg_inode_header_1 sinodep;
530
531                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
532                                                         &next_block, &next_offset))
533                                         goto failed_read;
534                                 SQUASHFS_SWAP_REG_INODE_HEADER_1(&inodep, &sinodep);
535                         } else
536                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
537                                                         &next_block, &next_offset))
538                                         goto failed_read;
539
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;
546                         else
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);
561                         break;
562                 }
563                 case SQUASHFS_DIR_TYPE: {
564                         squashfs_dir_inode_header_1 inodep;
565
566                         if(msBlk->swap) {
567                                 squashfs_dir_inode_header_1 sinodep;
568
569                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
570                                                         &next_block, &next_offset))
571                                         goto failed_read;
572                                 SQUASHFS_SWAP_DIR_INODE_HEADER_1(&inodep, &sinodep);
573                         } else
574                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
575                                                         &next_block, &next_offset))
576                                         goto failed_read;
577
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);
590                         break;
591                 }
592                 case SQUASHFS_SYMLINK_TYPE: {
593                         squashfs_symlink_inode_header_1 inodep;
594         
595                         if(msBlk->swap) {
596                                 squashfs_symlink_inode_header_1 sinodep;
597
598                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
599                                                         &next_block, &next_offset))
600                                         goto failed_read;
601                                 SQUASHFS_SWAP_SYMLINK_INODE_HEADER_1(&inodep, &sinodep);
602                         } else
603                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
604                                                         &next_block, &next_offset))
605                                         goto failed_read;
606
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);
615                         break;
616                  }
617                  case SQUASHFS_BLKDEV_TYPE:
618                  case SQUASHFS_CHRDEV_TYPE: {
619                         squashfs_dev_inode_header_1 inodep;
620
621                         if(msBlk->swap) {
622                                 squashfs_dev_inode_header_1 sinodep;
623
624                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
625                                                         &next_block, &next_offset))
626                                         goto failed_read;
627                                 SQUASHFS_SWAP_DEV_INODE_HEADER_1(&inodep, &sinodep);
628                         } else  
629                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
630                                                         &next_block, &next_offset))
631                                         goto failed_read;
632
633                         i->i_size = 0;
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);
637                         break;
638                  }
639                  case SQUASHFS_IPC_TYPE: {
640                         squashfs_ipc_inode_header_1 inodep;
641
642                         if(msBlk->swap) {
643                                 squashfs_ipc_inode_header_1 sinodep;
644
645                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
646                                                         &next_block, &next_offset))
647                                         goto failed_read;
648                                 SQUASHFS_SWAP_IPC_INODE_HEADER_1(&inodep, &sinodep);
649                         } else  
650                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
651                                                         &next_block, &next_offset))
652                                         goto failed_read;
653
654                         i->i_size = 0;
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);
658                         break;
659                  }
660                  default:
661                         ERROR("Unknown inode type %d in squashfs_iget!\n", inodeb.inode_type);
662                                 goto failed_read1;
663         }
664         
665         if(inodeb.guid == 15)
666                 i->i_gid = i->i_uid;
667         else
668                 i->i_gid = msBlk->guid[inodeb.guid];
669
670         insert_inode_hash(i);
671         return i;
672
673 failed_read:
674         ERROR("Unable to read inode [%x:%x]\n", block, offset);
675
676 failed_read1:
677         return NULL;
678 }
679 #endif
680
681
682 static struct inode *squashfs_iget(struct super_block *s, squashfs_inode inode)
683 {
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;
691
692         TRACE("Entered squashfs_iget\n");
693
694         if(msBlk->swap) {
695                 squashfs_base_inode_header sinodeb;
696
697                 if(!squashfs_get_cached_block(s, (char *) &sinodeb, block,  offset,
698                                         sizeof(sinodeb), &next_block, &next_offset))
699                         goto failed_read;
700                 SQUASHFS_SWAP_BASE_INODE_HEADER(&inodeb, &sinodeb, sizeof(sinodeb));
701         } else
702                 if(!squashfs_get_cached_block(s, (char *) &inodeb, block,  offset,
703                                         sizeof(inodeb), &next_block, &next_offset))
704                         goto failed_read;
705
706         i->i_nlink = 1;
707
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;
711
712         i->i_uid = msBlk->uid[inodeb.uid];
713         i->i_ino = SQUASHFS_MK_VFS_INODE(block - sBlk->inode_table_start, offset);
714
715         i->i_mode = inodeb.mode;
716
717         switch(inodeb.inode_type) {
718                 case SQUASHFS_FILE_TYPE: {
719                         squashfs_reg_inode_header inodep;
720
721                         if(msBlk->swap) {
722                                 squashfs_reg_inode_header sinodep;
723
724                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
725                                                         &next_block, &next_offset))
726                                         goto failed_read;
727                                 SQUASHFS_SWAP_REG_INODE_HEADER(&inodep, &sinodep);
728                         } else
729                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
730                                                         &next_block, &next_offset))
731                                         goto failed_read;
732
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))
736                                 goto failed_read;
737
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;
743                         else
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);
756                         break;
757                 }
758                 case SQUASHFS_DIR_TYPE: {
759                         squashfs_dir_inode_header inodep;
760
761                         if(msBlk->swap) {
762                                 squashfs_dir_inode_header sinodep;
763
764                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
765                                                         &next_block, &next_offset))
766                                         goto failed_read;
767                                 SQUASHFS_SWAP_DIR_INODE_HEADER(&inodep, &sinodep);
768                         } else
769                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
770                                                         &next_block, &next_offset))
771                                         goto failed_read;
772
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);
785                         break;
786                 }
787                 case SQUASHFS_LDIR_TYPE: {
788                         squashfs_ldir_inode_header inodep;
789
790                         if(msBlk->swap) {
791                                 squashfs_ldir_inode_header sinodep;
792
793                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
794                                                         &next_block, &next_offset))
795                                         goto failed_read;
796                                 SQUASHFS_SWAP_LDIR_INODE_HEADER(&inodep, &sinodep);
797                         } else
798                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
799                                                         &next_block, &next_offset))
800                                         goto failed_read;
801
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);
816                         break;
817                 }
818                 case SQUASHFS_SYMLINK_TYPE: {
819                         squashfs_symlink_inode_header inodep;
820         
821                         if(msBlk->swap) {
822                                 squashfs_symlink_inode_header sinodep;
823
824                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
825                                                         &next_block, &next_offset))
826                                         goto failed_read;
827                                 SQUASHFS_SWAP_SYMLINK_INODE_HEADER(&inodep, &sinodep);
828                         } else
829                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
830                                                         &next_block, &next_offset))
831                                         goto failed_read;
832
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);
841                         break;
842                  }
843                  case SQUASHFS_BLKDEV_TYPE:
844                  case SQUASHFS_CHRDEV_TYPE: {
845                         squashfs_dev_inode_header inodep;
846
847                         if(msBlk->swap) {
848                                 squashfs_dev_inode_header sinodep;
849
850                                 if(!squashfs_get_cached_block(s, (char *) &sinodep, block,  offset, sizeof(sinodep),
851                                                         &next_block, &next_offset))
852                                         goto failed_read;
853                                 SQUASHFS_SWAP_DEV_INODE_HEADER(&inodep, &sinodep);
854                         } else  
855                                 if(!squashfs_get_cached_block(s, (char *) &inodep, block,  offset, sizeof(inodep),
856                                                         &next_block, &next_offset))
857                                         goto failed_read;
858
859                         i->i_size = 0;
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);
863                         break;
864                  }
865                  case SQUASHFS_FIFO_TYPE:
866                  case SQUASHFS_SOCKET_TYPE: {
867                         i->i_size = 0;
868                         i->i_mode |= (inodeb.inode_type == SQUASHFS_FIFO_TYPE) ? S_IFIFO : S_IFSOCK;
869                         init_special_inode(i, i->i_mode, 0);
870                         break;
871                  }
872                  default:
873                         ERROR("Unknown inode type %d in squashfs_iget!\n", inodeb.inode_type);
874                                 goto failed_read1;
875         }
876         
877         if(inodeb.guid == SQUASHFS_GUIDS)
878                 i->i_gid = i->i_uid;
879         else
880                 i->i_gid = msBlk->guid[inodeb.guid];
881
882         insert_inode_hash(i);
883         return i;
884
885 failed_read:
886         ERROR("Unable to read inode [%x:%x]\n", block, offset);
887
888 failed_read1:
889         return NULL;
890 }
891
892
893 static int squashfs_fill_super(struct super_block *s,
894                 void *data, int silent)
895 {
896         squashfs_sb_info *msBlk;
897         squashfs_super_block *sBlk;
898         int i;
899         char b[BDEVNAME_SIZE];
900
901         TRACE("Entered squashfs_read_superblock\n");
902
903         if(!(s->s_fs_info = (void *) kmalloc(sizeof(squashfs_sb_info), GFP_KERNEL))) {
904                 ERROR("Failed to allocate superblock\n");
905                 return -ENOMEM;
906         }
907         msBlk = (squashfs_sb_info *) s->s_fs_info;
908         sBlk = &msBlk->sBlk;
909         
910         msBlk->devblksize = sb_min_blocksize(s, BLOCK_SIZE);
911         msBlk->devblksize_log2 = ffz(~msBlk->devblksize);
912
913         init_MUTEX(&msBlk->read_page_mutex);
914         init_MUTEX(&msBlk->block_cache_mutex);
915         init_MUTEX(&msBlk->fragment_mutex);
916         
917         init_waitqueue_head(&msBlk->waitq);
918         init_waitqueue_head(&msBlk->fragment_wait_queue);
919
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");
922                 goto failed_mount;
923         }
924
925         /* Check it is a SQUASHFS superblock */
926         msBlk->swap = 0;
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));
933                         msBlk->swap = 1;
934                 } else  {
935                         SERROR("Can't find a SQUASHFS superblock on %s\n", bdevname(s->s_bdev, b));
936                         goto failed_mount;
937                 }
938         }
939
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);
945                 goto failed_mount;
946         }
947         if(sBlk->s_major == 1)
948                 sBlk->block_size = sBlk->block_size_1;
949 #else
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);
953                 goto failed_mount;
954         }
955 #endif
956
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);
973
974         s->s_flags |= MS_RDONLY;
975         s->s_op = &squashfs_ops;
976
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");
980                 goto failed_mount;
981         }
982
983         for(i = 0; i < SQUASHFS_CACHED_BLKS; i++)
984                 msBlk->block_cache[i].block = SQUASHFS_INVALID_BLK;
985
986         msBlk->next_cache = 0;
987
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");
992                 goto failed_mount1;
993         }
994
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");
999                         goto failed_mount2;
1000                 }
1001         } else
1002                 msBlk->read_page = NULL;
1003
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");
1008                 goto failed_mount3;
1009         }
1010         msBlk->guid = msBlk->uid + sBlk->no_uids;
1011    
1012         if(msBlk->swap) {
1013                 squashfs_uid suid[sBlk->no_uids + sBlk->no_guids];
1014
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");
1018                         goto failed_mount4;
1019                 }
1020                 SQUASHFS_SWAP_DATA(msBlk->uid, suid, (sBlk->no_uids + sBlk->no_guids), (sizeof(squashfs_uid) * 8));
1021         } else
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");
1025                         goto failed_mount4;
1026                 }
1027
1028
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;
1035                 goto allocate_root;
1036         }
1037 #endif
1038         msBlk->iget = squashfs_iget;
1039         msBlk->read_blocklist = read_blocklist;
1040
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");
1043                 goto failed_mount4;
1044         }
1045
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;
1050         }
1051
1052         msBlk->next_fragment = 0;
1053
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");
1057                 goto failed_mount5;
1058         }
1059    
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");
1064                         goto failed_mount6;
1065         }
1066
1067         if(msBlk->swap) {
1068                 int i;
1069                 squashfs_fragment_index fragment;
1070
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;
1074                 }
1075         }
1076
1077 #ifdef SQUASHFS_1_0_COMPATIBILITY
1078 allocate_root:
1079 #endif
1080         if(!(s->s_root = d_alloc_root((msBlk->iget)(s, sBlk->root_inode)))) {
1081                 ERROR("Root inode create failed\n");
1082                 goto failed_mount5;
1083         }
1084
1085         TRACE("Leaving squashfs_read_super\n");
1086         return 0;
1087
1088 failed_mount6:
1089         kfree(msBlk->fragment_index);
1090 failed_mount5:
1091         kfree(msBlk->fragment);
1092 failed_mount4:
1093         kfree(msBlk->uid);
1094 failed_mount3:
1095         kfree(msBlk->read_page);
1096 failed_mount2:
1097         kfree(msBlk->read_data);
1098 failed_mount1:
1099         kfree(msBlk->block_cache);
1100 failed_mount:
1101         kfree(s->s_fs_info);
1102         s->s_fs_info = NULL;
1103         return -EINVAL;
1104 }
1105
1106
1107 static int squashfs_statfs(struct super_block *s, struct kstatfs *buf)
1108 {
1109         squashfs_super_block *sBlk = &((squashfs_sb_info *)s->s_fs_info)->sBlk;
1110
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;
1117         buf->f_ffree = 0;
1118         buf->f_namelen = SQUASHFS_NAME_LEN;
1119         return 0;
1120 }
1121
1122
1123 static int squashfs_symlink_readpage(struct file *file, struct page *page)
1124 {
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);
1130
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);
1133
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);
1138                         goto skip_read;
1139                 }
1140         }
1141
1142         if(length != index) {
1143                 ERROR("(squashfs_symlink_readpage) length != index\n");
1144                 bytes = 0;
1145                 goto skip_read;
1146         }
1147
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);
1151
1152 skip_read:
1153         memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1154         kunmap(page);
1155         flush_dcache_page(page);
1156         SetPageUptodate(page);
1157         unlock_page(page);
1158
1159         return 0;
1160 }
1161
1162
1163 #define SIZE 256
1164
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)
1168 {
1169         squashfs_sb_info *msBlk = (squashfs_sb_info *)inode->i_sb->s_fs_info;
1170         unsigned short *block_listp;
1171         int i = 0;
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;
1175
1176         for(;;) {
1177                 int blocks = (index + readahead_blks - i);
1178                 if(blocks > (SIZE >> 1)) {
1179                         if((index - i) <= (SIZE >> 1))
1180                                 blocks = index - i;
1181                         else
1182                                 blocks = SIZE >> 1;
1183                 }
1184
1185                 if(msBlk->swap) {
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);
1189                                 return 0;
1190                         }
1191                         SQUASHFS_SWAP_SHORTS(((unsigned short *)block_list), ((unsigned short *)sblock_list), blocks);
1192                 } else
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);
1195                                 return 0;
1196                         }
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)
1200                         break;
1201         }
1202
1203         if(bsize)
1204                 *bsize = SQUASHFS_COMPRESSED_SIZE(*block_listp) | (!SQUASHFS_COMPRESSED(*block_listp) ? SQUASHFS_COMPRESSED_BIT_BLOCK : 0);
1205         else
1206                 *block_p = block_listp;
1207         return block;
1208 }
1209 #endif
1210
1211
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)
1214 {
1215         squashfs_sb_info *msBlk = (squashfs_sb_info *)inode->i_sb->s_fs_info;
1216         unsigned int *block_listp;
1217         int i = 0;
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;
1221
1222         for(;;) {
1223                 int blocks = (index + readahead_blks - i);
1224                 if(blocks > (SIZE >> 2)) {
1225                         if((index - i) <= (SIZE >> 2))
1226                                 blocks = index - i;
1227                         else
1228                                 blocks = SIZE >> 2;
1229                 }
1230
1231                 if(msBlk->swap) {
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);
1235                                 return 0;
1236                         }
1237                         SQUASHFS_SWAP_INTS(((unsigned int *)block_list), ((unsigned int *)sblock_list), blocks);
1238                 } else
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);
1241                                 return 0;
1242                         }
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)
1246                         break;
1247         }
1248
1249         *bsize = *block_listp;
1250         return block;
1251 }
1252
1253
1254 static int squashfs_readpage(struct file *file, struct page *page)
1255 {
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;
1265         
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;
1269
1270         TRACE("Entered squashfs_readpage, page index %x, start block %x\n", (unsigned int) page->index,
1271                 SQUASHFS_I(inode)->start_block);
1272
1273         if(page->index >= ((inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT)) {
1274                 goto skip_read;
1275         }
1276
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)
1279                         goto skip_read;
1280
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);
1285                         goto skip_read;
1286                 }
1287         } else {
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);
1290                         goto skip_read;
1291                 }
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;
1295         }
1296
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;
1300
1301                 TRACE("bytes %d, i %d, byte_offset %d, available_bytes %d\n", bytes, i, byte_offset, available_bytes);
1302
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);
1306                         kunmap(page);
1307                         flush_dcache_page(page);
1308                         SetPageUptodate(page);
1309                         unlock_page(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);
1314                         kunmap(push_page);
1315                         flush_dcache_page(push_page);
1316                         SetPageUptodate(push_page);
1317                         unlock_page(push_page);
1318                         page_cache_release(push_page);
1319                 }
1320         }
1321
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);
1324         else
1325                 release_cached_fragment(msBlk, fragment);
1326
1327         return 0;
1328
1329 skip_read:
1330         memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1331         kunmap(page);
1332         flush_dcache_page(page);
1333         SetPageUptodate(page);
1334         unlock_page(page);
1335
1336         return 0;
1337 }
1338
1339
1340 static int squashfs_readpage4K(struct file *file, struct page *page)
1341 {
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);
1348         
1349         TRACE("Entered squashfs_readpage4K, page index %x, start block %x\n", (unsigned int) page->index,
1350                 SQUASHFS_I(inode)->start_block);
1351
1352         if(page->index >= ((inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT)) {
1353                 goto skip_read;
1354         }
1355
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);
1358
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);
1361         } else {
1362                 struct squashfs_fragment_cache *fragment;
1363
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);
1366                 else {
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);
1370                 }
1371         }
1372
1373 skip_read:
1374         memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
1375         kunmap(page);
1376         flush_dcache_page(page);
1377         SetPageUptodate(page);
1378         unlock_page(page);
1379
1380         return 0;
1381 }
1382
1383
1384 #ifdef SQUASHFS_1_0_COMPATIBILITY
1385 static int squashfs_readpage_lessthan4K(struct file *file, struct page *page)
1386 {
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);
1396         
1397         int i_end = index + (1 << (PAGE_CACHE_SHIFT - sBlk->block_log));
1398         int byte;
1399
1400         TRACE("Entered squashfs_readpage_lessthan4K, page index %x, start block %x\n", (unsigned int) page->index,
1401                 SQUASHFS_I(inode)->start_block);
1402
1403         block = read_blocklist_1(inode, index, readahead_blks, block_list, &block_listp, NULL);
1404
1405         if(i_end > file_blocks)
1406                 i_end = file_blocks;
1407
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);
1412                         goto skip_read;
1413                 }
1414                 block += SQUASHFS_COMPRESSED_SIZE(*block_listp);
1415                 pageaddr += byte;
1416                 bytes += byte;
1417                 index ++;
1418                 block_listp ++;
1419         }
1420
1421 skip_read:
1422         memset(pageaddr, 0, PAGE_CACHE_SIZE - bytes);
1423         kunmap(page);
1424         flush_dcache_page(page);
1425         SetPageUptodate(page);
1426         unlock_page(page);
1427
1428         return 0;
1429 }
1430 #endif
1431
1432
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)
1436 {
1437         squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
1438         squashfs_super_block *sBlk = &msBlk->sBlk;
1439         int i, length = 0;
1440         squashfs_dir_index index;
1441
1442         TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %d\n", i_count, (unsigned int) f_pos);
1443
1444         if(f_pos == 0)
1445                 return 0;
1446
1447         for(i = 0; i < i_count; i++) {
1448                 if(msBlk->swap) {
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);
1453                 } else
1454                         squashfs_get_cached_block(s, (char *) &index, index_start, index_offset,
1455                                 sizeof(index), &index_start, &index_offset);
1456
1457                 if(index.index > f_pos)
1458                         break;
1459
1460                 squashfs_get_cached_block(s, NULL, index_start, index_offset,
1461                                 index.size + 1, &index_start, &index_offset);
1462
1463                 length = index.index;
1464                 *next_block = index.start_block + sBlk->directory_table_start;
1465         }
1466
1467         *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
1468         return length;
1469 }
1470
1471
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)
1475 {
1476         squashfs_sb_info *msBlk = (squashfs_sb_info *)s->s_fs_info;
1477         squashfs_super_block *sBlk = &msBlk->sBlk;
1478         int i, length = 0;
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];
1482
1483         TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
1484
1485         strncpy(str, name, size);
1486         str[size] = '\0';
1487
1488         for(i = 0; i < i_count; i++) {
1489                 if(msBlk->swap) {
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);
1494                 } else
1495                         squashfs_get_cached_block(s, (char *) index, index_start, index_offset,
1496                                 sizeof(squashfs_dir_index), &index_start, &index_offset);
1497
1498                 squashfs_get_cached_block(s, index->name, index_start, index_offset,
1499                                 index->size + 1, &index_start, &index_offset);
1500
1501                 index->name[index->size + 1] = '\0';
1502
1503                 if(strcmp(index->name, str) > 0)
1504                         break;
1505
1506                 length = index->index;
1507                 *next_block = index->start_block + sBlk->directory_table_start;
1508         }
1509
1510         *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
1511         return length;
1512 }
1513
1514                 
1515 static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
1516 {
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;
1525
1526         TRACE("Entered squashfs_readdir [%x:%x]\n", next_block, next_offset);
1527
1528         lock_kernel();
1529
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);
1532
1533         while(length < i->i_size) {
1534                 /* read directory header */
1535                 if(msBlk->swap) {
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))
1539                                 goto failed_read;
1540                         length += sizeof(sdirh);
1541                         SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
1542                 } else {
1543                         if(!squashfs_get_cached_block(i->i_sb, (char *) &dirh, next_block,
1544                                                 next_offset, sizeof(dirh), &next_block, &next_offset))
1545                                 goto failed_read;
1546                         length += sizeof(dirh);
1547                 }
1548
1549                 dir_count = dirh.count + 1;
1550                 while(dir_count--) {
1551                         if(msBlk->swap) {
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))
1555                                         goto failed_read;
1556                                 length += sizeof(sdire);
1557                                 SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
1558                         } else {
1559                                 if(!squashfs_get_cached_block(i->i_sb, (char *) dire, next_block,
1560                                                         next_offset, sizeof(*dire), &next_block, &next_offset))
1561                                         goto failed_read;
1562                                 length += sizeof(*dire);
1563                         }
1564
1565                         if(!squashfs_get_cached_block(i->i_sb, dire->name, next_block,
1566                                                 next_offset, dire->size + 1, &next_block, &next_offset))
1567                                 goto failed_read;
1568                         length += dire->size + 1;
1569
1570                         if(file->f_pos >= length)
1571                                 continue;
1572
1573                         dire->name[dire->size + 1] = '\0';
1574
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]);
1578
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");
1582                                 unlock_kernel();
1583                                 return dirs_read;
1584                         }
1585
1586                         file->f_pos = length;
1587                         dirs_read ++;
1588                 }
1589         }
1590
1591         unlock_kernel();
1592         return dirs_read;
1593
1594 failed_read:
1595         unlock_kernel();
1596         ERROR("Unable to read directory block [%x:%x]\n", next_block, next_offset);
1597         return 0;
1598 }
1599
1600
1601 static struct dentry *squashfs_lookup(struct inode *i, struct dentry *dentry, struct nameidata *nd)
1602 {
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;
1614
1615         TRACE("Entered squashfs_lookup [%x:%x]\n", next_block, next_offset);
1616
1617         lock_kernel();
1618
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);
1621
1622         while(length < i->i_size) {
1623                 /* read directory header */
1624                 if(msBlk->swap) {
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))
1628                                 goto failed_read;
1629                         length += sizeof(sdirh);
1630                         SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
1631                 } else {
1632                         if(!squashfs_get_cached_block(i->i_sb, (char *) &dirh, next_block, next_offset,
1633                                                 sizeof(dirh), &next_block, &next_offset))
1634                                 goto failed_read;
1635                         length += sizeof(dirh);
1636                 }
1637
1638                 dir_count = dirh.count + 1;
1639                 while(dir_count--) {
1640                         if(msBlk->swap) {
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))
1644                                         goto failed_read;
1645                                 length += sizeof(sdire);
1646                                 SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
1647                         } else {
1648                                 if(!squashfs_get_cached_block(i->i_sb, (char *) dire,
1649                                                         next_block,next_offset, sizeof(*dire), &next_block, &next_offset))
1650                                         goto failed_read;
1651                                 length += sizeof(*dire);
1652                         }
1653
1654                         if(!squashfs_get_cached_block(i->i_sb, dire->name,
1655                                                 next_block, next_offset, dire->size + 1, &next_block, &next_offset))
1656                                 goto failed_read;
1657                         length += dire->size + 1;
1658
1659                         if(squashfs_2_1 && name[0] < dire->name[0])
1660                                 goto exit_loop;
1661
1662                         if((len == dire->size + 1) && !strncmp(name, dire->name, len)) {
1663                                 squashfs_inode ino = SQUASHFS_MKINODE(dirh.start_block, dire->offset);
1664
1665                                 TRACE("calling squashfs_iget for directory entry %s, inode %x:%x\n",
1666                                                 name, dirh.start_block, dire->offset);
1667
1668                                 inode = (msBlk->iget)(i->i_sb, ino);
1669
1670                                 goto exit_loop;
1671                         }
1672                 }
1673         }
1674
1675 exit_loop:
1676         d_add(dentry, inode);
1677         unlock_kernel();
1678         return ERR_PTR(0);
1679
1680 failed_read:
1681         ERROR("Unable to read directory block [%x:%x]\n", next_block, next_offset);
1682         goto exit_loop;
1683 }
1684
1685
1686 static void squashfs_put_super(struct super_block *s)
1687 {
1688         int i;
1689
1690         if(s->s_fs_info) {
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);
1697                 }
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);
1701                 if(sbi->fragment) {
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);
1706                 }
1707                 if(sbi->fragment_index) kfree(sbi->fragment_index);
1708                 kfree(s->s_fs_info);
1709                 s->s_fs_info = NULL;
1710         }
1711 }
1712
1713
1714 static struct super_block *squashfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
1715 {
1716         return get_sb_bdev(fs_type, flags, dev_name, data, squashfs_fill_super);
1717 }
1718
1719
1720 static int __init init_squashfs_fs(void)
1721 {
1722         int err = init_inodecache();
1723         if(err)
1724                 return err;
1725
1726         printk(KERN_INFO "Squashfs 2.2-r2 (released 2005/09/08) (C) 2002-2005 Phillip Lougher\n");
1727
1728         if(!(stream.workspace = (char *) vmalloc(zlib_inflate_workspacesize()))) {
1729                 ERROR("Failed to allocate zlib workspace\n");
1730                 destroy_inodecache();
1731                 return -ENOMEM;
1732         }
1733
1734         if((err = register_filesystem(&squashfs_fs_type))) {
1735                 vfree(stream.workspace);
1736                 destroy_inodecache();
1737         }
1738
1739         return err;
1740 }
1741
1742
1743 static void __exit exit_squashfs_fs(void)
1744 {
1745         vfree(stream.workspace);
1746         unregister_filesystem(&squashfs_fs_type);
1747         destroy_inodecache();
1748 }
1749
1750
1751 static kmem_cache_t * squashfs_inode_cachep;
1752
1753
1754 static struct inode *squashfs_alloc_inode(struct super_block *sb)
1755 {
1756         struct squashfs_inode_info *ei;
1757         ei = (struct squashfs_inode_info *)kmem_cache_alloc(squashfs_inode_cachep, SLAB_KERNEL);
1758         if (!ei)
1759                 return NULL;
1760         return &ei->vfs_inode;
1761 }
1762
1763
1764 static void squashfs_destroy_inode(struct inode *inode)
1765 {
1766         kmem_cache_free(squashfs_inode_cachep, SQUASHFS_I(inode));
1767 }
1768
1769
1770 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
1771 {
1772         struct squashfs_inode_info *ei = (struct squashfs_inode_info *) foo;
1773
1774         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1775             SLAB_CTOR_CONSTRUCTOR)
1776                 inode_init_once(&ei->vfs_inode);
1777 }
1778  
1779
1780 static int init_inodecache(void)
1781 {
1782         squashfs_inode_cachep = kmem_cache_create("squashfs_inode_cache",
1783                                              sizeof(struct squashfs_inode_info),
1784                                              0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
1785                                              init_once, NULL);
1786         if (squashfs_inode_cachep == NULL)
1787                 return -ENOMEM;
1788         return 0;
1789 }
1790
1791
1792 static void destroy_inodecache(void)
1793 {
1794         if (kmem_cache_destroy(squashfs_inode_cachep))
1795                 printk(KERN_INFO "squashfs_inode_cache: not all structures were freed\n");
1796 }
1797
1798
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");