Home Home > GIT Browse > SLE15-AZURE
summaryrefslogtreecommitdiff
blob: f49cd1e28ec9d85785590e577f81e25fc772a540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
 * Squashfs3 - a compressed read only filesystem for Linux
 *
 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
 * Phillip Lougher <phillip@lougher.demon.co.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2,
 * or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * squashfs2_0.c
 */

#include <linux/module.h>
#include <linux/zlib.h>
#include <linux/fs.h>
#include <linux/slab.h>

#include "squashfs3_fs.h"
#include "squashfs3_fs_sb.h"
#include "squashfs3_fs_i.h"
#include "squashfs3.h"
static int squashfs3_readdir_2(struct file *file, struct dir_context *ctx);
static struct dentry *squashfs3_lookup_2(struct inode *, struct dentry *,
				unsigned int);

static struct file_operations squashfs3_dir_ops_2 = {
	.read = generic_read_dir,
	.iterate = squashfs3_readdir_2
};

static struct inode_operations squashfs3_dir_inode_ops_2 = {
	.lookup = squashfs3_lookup_2
};

static unsigned char squashfs3_filetype_table[] = {
	DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
};

static int read_fragment_index_table_2(struct super_block *s)
{
	struct squashfs3_sb_info *msblk = s->s_fs_info;
	struct squashfs3_super_block *sblk = &msblk->sblk;

	if (!(msblk->fragment_index_2 = kmalloc(SQUASHFS3_FRAGMENT_INDEX_BYTES_2
					(sblk->fragments), GFP_KERNEL))) {
		ERROR("Failed to allocate uid/gid table\n");
		return 0;
	}

	if (SQUASHFS3_FRAGMENT_INDEX_BYTES_2(sblk->fragments) &&
					!squashfs3_read_data(s, (char *)
					msblk->fragment_index_2,
					sblk->fragment_table_start,
					SQUASHFS3_FRAGMENT_INDEX_BYTES_2
					(sblk->fragments) |
					SQUASHFS3_COMPRESSED_BIT_BLOCK, NULL, SQUASHFS3_FRAGMENT_INDEX_BYTES_2(sblk->fragments))) {
		ERROR("unable to read fragment index table\n");
		return 0;
	}

	if (msblk->swap) {
		int i;
		unsigned int fragment;

		for (i = 0; i < SQUASHFS3_FRAGMENT_INDEXES_2(sblk->fragments);
									i++) {
			SQUASHFS3_SWAP_FRAGMENT_INDEXES_2((&fragment),
						&msblk->fragment_index_2[i], 1);
			msblk->fragment_index_2[i] = fragment;
		}
	}

	return 1;
}


static int get_fragment_location_2(struct super_block *s, unsigned int fragment,
				long long *fragment_start_block,
				unsigned int *fragment_size)
{
	struct squashfs3_sb_info *msblk = s->s_fs_info;
	long long start_block =
		msblk->fragment_index_2[SQUASHFS3_FRAGMENT_INDEX_2(fragment)];
	int offset = SQUASHFS3_FRAGMENT_INDEX_OFFSET_2(fragment);
	struct squashfs3_fragment_entry_2 fragment_entry;

	if (msblk->swap) {
		struct squashfs3_fragment_entry_2 sfragment_entry;

		if (!squashfs3_get_cached_block(s, (char *) &sfragment_entry,
					start_block, offset,
					sizeof(sfragment_entry), &start_block,
					&offset))
			goto out;
		SQUASHFS3_SWAP_FRAGMENT_ENTRY_2(&fragment_entry, &sfragment_entry);
	} else
		if (!squashfs3_get_cached_block(s, (char *) &fragment_entry,
					start_block, offset,
					sizeof(fragment_entry), &start_block,
					&offset))
			goto out;

	*fragment_start_block = fragment_entry.start_block;
	*fragment_size = fragment_entry.size;

	return 1;

out:
	return 0;
}


static void squashfs3_new_inode(struct squashfs3_sb_info *msblk, struct inode *i,
		struct squashfs3_base_inode_header_2 *inodeb, unsigned int ino)
{
	struct squashfs3_super_block *sblk = &msblk->sblk;

	i->i_ino = ino;
	i->i_mtime.tv_sec = sblk->mkfs_time;
	i->i_atime.tv_sec = sblk->mkfs_time;
	i->i_ctime.tv_sec = sblk->mkfs_time;
	i->i_uid = make_kuid(&init_user_ns, msblk->uid[inodeb->uid]);
	i->i_mode = inodeb->mode;
	set_nlink(i, 1);
	i->i_size = 0;
	if (inodeb->guid == SQUASHFS3_GUIDS)
		i->i_gid = make_kgid(&init_user_ns, msblk->uid[inodeb->uid]);
	else
		i->i_gid = make_kgid(&init_user_ns, msblk->guid[inodeb->guid]);
}


static int squashfs3_read_inode_2(struct inode *i, squashfs3_inode_t inode)
{
	struct super_block *s = i->i_sb;
	struct squashfs3_sb_info *msblk = s->s_fs_info;
	struct squashfs3_super_block *sblk = &msblk->sblk;
	unsigned int block = SQUASHFS3_INODE_BLK(inode) +
		sblk->inode_table_start;
	unsigned int offset = SQUASHFS3_INODE_OFFSET(inode);
	unsigned int ino = SQUASHFS3_MK_VFS_INODE(block -
		sblk->inode_table_start, offset);
	long long next_block;
	unsigned int next_offset;
	union squashfs3_inode_header_2 id, sid;
	struct squashfs3_base_inode_header_2 *inodeb = &id.base,
					  *sinodeb = &sid.base;

	TRACE("Entered squashfs3_read_inode_2\n");

	if (msblk->swap) {
		if (!squashfs3_get_cached_block(s, (char *) sinodeb, block,
					offset, sizeof(*sinodeb), &next_block,
					&next_offset))
			goto failed_read;
		SQUASHFS3_SWAP_BASE_INODE_HEADER_2(inodeb, sinodeb,
					sizeof(*sinodeb));
	} else
		if (!squashfs3_get_cached_block(s, (char *) inodeb, block,
					offset, sizeof(*inodeb), &next_block,
					&next_offset))
			goto failed_read;

	squashfs3_new_inode(msblk, i, inodeb, ino);

	switch(inodeb->inode_type) {
		case SQUASHFS3_FILE_TYPE: {
			struct squashfs3_reg_inode_header_2 *inodep = &id.reg;
			struct squashfs3_reg_inode_header_2 *sinodep = &sid.reg;
			long long frag_blk;
			unsigned int frag_size = 0;

			if (msblk->swap) {
				if (!squashfs3_get_cached_block(s, (char *)
						sinodep, block, offset,
						sizeof(*sinodep), &next_block,
						&next_offset))
					goto failed_read;
				SQUASHFS3_SWAP_REG_INODE_HEADER_2(inodep, sinodep);
			} else
				if (!squashfs3_get_cached_block(s, (char *)
						inodep, block, offset,
						sizeof(*inodep), &next_block,
						&next_offset))
					goto failed_read;

			frag_blk = SQUASHFS3_INVALID_BLK;
			if (inodep->fragment != SQUASHFS3_INVALID_FRAG &&
					!get_fragment_location_2(s,
					inodep->fragment, &frag_blk, &frag_size))
				goto failed_read;

			i->i_size = inodep->file_size;
			i->i_fop = &generic_ro_fops;
			i->i_mode |= S_IFREG;
			i->i_mtime.tv_sec = inodep->mtime;
			i->i_atime.tv_sec = inodep->mtime;
			i->i_ctime.tv_sec = inodep->mtime;
			i->i_blocks = ((i->i_size - 1) >> 9) + 1;
			SQUASHFS3_I(i)->u.s1.fragment_start_block = frag_blk;
			SQUASHFS3_I(i)->u.s1.fragment_size = frag_size;
			SQUASHFS3_I(i)->u.s1.fragment_offset = inodep->offset;
			SQUASHFS3_I(i)->start_block = inodep->start_block;
			SQUASHFS3_I(i)->u.s1.block_list_start = next_block;
			SQUASHFS3_I(i)->offset = next_offset;
			i->i_data.a_ops = &squashfs3_aops;

			TRACE("File inode %x:%x, start_block %x, "
					"block_list_start %llx, offset %x\n",
					SQUASHFS3_INODE_BLK(inode), offset,
					inodep->start_block, next_block,
					next_offset);
			break;
		}
		case SQUASHFS3_DIR_TYPE: {
			struct squashfs3_dir_inode_header_2 *inodep = &id.dir;
			struct squashfs3_dir_inode_header_2 *sinodep = &sid.dir;

			if (msblk->swap) {
				if (!squashfs3_get_cached_block(s, (char *)
						sinodep, block, offset,
						sizeof(*sinodep), &next_block,
						&next_offset))
					goto failed_read;
				SQUASHFS3_SWAP_DIR_INODE_HEADER_2(inodep, sinodep);
			} else
				if (!squashfs3_get_cached_block(s, (char *)
						inodep, block, offset,
						sizeof(*inodep), &next_block,
						&next_offset))
					goto failed_read;

			i->i_size = inodep->file_size;
			i->i_op = &squashfs3_dir_inode_ops_2;
			i->i_fop = &squashfs3_dir_ops_2;
			i->i_mode |= S_IFDIR;
			i->i_mtime.tv_sec = inodep->mtime;
			i->i_atime.tv_sec = inodep->mtime;
			i->i_ctime.tv_sec = inodep->mtime;
			SQUASHFS3_I(i)->start_block = inodep->start_block;
			SQUASHFS3_I(i)->offset = inodep->offset;
			SQUASHFS3_I(i)->u.s2.directory_index_count = 0;
			SQUASHFS3_I(i)->u.s2.parent_inode = 0;

			TRACE("Directory inode %x:%x, start_block %x, offset "
					"%x\n", SQUASHFS3_INODE_BLK(inode),
					offset, inodep->start_block,
					inodep->offset);
			break;
		}
		case SQUASHFS3_LDIR_TYPE: {
			struct squashfs3_ldir_inode_header_2 *inodep = &id.ldir;
			struct squashfs3_ldir_inode_header_2 *sinodep = &sid.ldir;

			if (msblk->swap) {
				if (!squashfs3_get_cached_block(s, (char *)
						sinodep, block, offset,
						sizeof(*sinodep), &next_block,
						&next_offset))
					goto failed_read;
				SQUASHFS3_SWAP_LDIR_INODE_HEADER_2(inodep,
						sinodep);
			} else
				if (!squashfs3_get_cached_block(s, (char *)
						inodep, block, offset,
						sizeof(*inodep), &next_block,
						&next_offset))
					goto failed_read;

			i->i_size = inodep->file_size;
			i->i_op = &squashfs3_dir_inode_ops_2;
			i->i_fop = &squashfs3_dir_ops_2;
			i->i_mode |= S_IFDIR;
			i->i_mtime.tv_sec = inodep->mtime;
			i->i_atime.tv_sec = inodep->mtime;
			i->i_ctime.tv_sec = inodep->mtime;
			SQUASHFS3_I(i)->start_block = inodep->start_block;
			SQUASHFS3_I(i)->offset = inodep->offset;
			SQUASHFS3_I(i)->u.s2.directory_index_start = next_block;
			SQUASHFS3_I(i)->u.s2.directory_index_offset =
								next_offset;
			SQUASHFS3_I(i)->u.s2.directory_index_count =
								inodep->i_count;
			SQUASHFS3_I(i)->u.s2.parent_inode = 0;

			TRACE("Long directory inode %x:%x, start_block %x, "
					"offset %x\n",
					SQUASHFS3_INODE_BLK(inode), offset,
					inodep->start_block, inodep->offset);
			break;
		}
		case SQUASHFS3_SYMLINK_TYPE: {
			struct squashfs3_symlink_inode_header_2 *inodep =
								&id.symlink;
			struct squashfs3_symlink_inode_header_2 *sinodep =
								&sid.symlink;

			if (msblk->swap) {
				if (!squashfs3_get_cached_block(s, (char *)
						sinodep, block, offset,
						sizeof(*sinodep), &next_block,
						&next_offset))
					goto failed_read;
				SQUASHFS3_SWAP_SYMLINK_INODE_HEADER_2(inodep,
								sinodep);
			} else
				if (!squashfs3_get_cached_block(s, (char *)
						inodep, block, offset,
						sizeof(*inodep), &next_block,
						&next_offset))
					goto failed_read;

			i->i_size = inodep->symlink_size;
			i->i_op = &page_symlink_inode_operations;
			i->i_data.a_ops = &squashfs3_symlink_aops;
			i->i_mode |= S_IFLNK;
			SQUASHFS3_I(i)->start_block = next_block;
			SQUASHFS3_I(i)->offset = next_offset;

			TRACE("Symbolic link inode %x:%x, start_block %llx, "
					"offset %x\n",
					SQUASHFS3_INODE_BLK(inode), offset,
					next_block, next_offset);
			break;
		 }
		 case SQUASHFS3_BLKDEV_TYPE:
		 case SQUASHFS3_CHRDEV_TYPE: {
			struct squashfs3_dev_inode_header_2 *inodep = &id.dev;
			struct squashfs3_dev_inode_header_2 *sinodep = &sid.dev;

			if (msblk->swap) {
				if (!squashfs3_get_cached_block(s, (char *)
						sinodep, block, offset,
						sizeof(*sinodep), &next_block,
						&next_offset))
					goto failed_read;
				SQUASHFS3_SWAP_DEV_INODE_HEADER_2(inodep, sinodep);
			} else
				if (!squashfs3_get_cached_block(s, (char *)
						inodep, block, offset,
						sizeof(*inodep), &next_block,
						&next_offset))
					goto failed_read;

			i->i_mode |= (inodeb->inode_type ==
					SQUASHFS3_CHRDEV_TYPE) ?  S_IFCHR :
					S_IFBLK;
			init_special_inode(i, i->i_mode,
					old_decode_dev(inodep->rdev));

			TRACE("Device inode %x:%x, rdev %x\n",
					SQUASHFS3_INODE_BLK(inode), offset,
					inodep->rdev);
			break;
		 }
		 case SQUASHFS3_FIFO_TYPE:
		 case SQUASHFS3_SOCKET_TYPE: {

			i->i_mode |= (inodeb->inode_type == SQUASHFS3_FIFO_TYPE)
							? S_IFIFO : S_IFSOCK;
			init_special_inode(i, i->i_mode, 0);
			break;
		 }
		 default:
			ERROR("Unknown inode type %d in squashfs3_iget!\n",
					inodeb->inode_type);
			goto failed_read1;
	}

	return 1;

failed_read:
	ERROR("Unable to read inode [%x:%x]\n", block, offset);

failed_read1:
	return 0;
}


static int get_dir_index_using_offset(struct super_block *s, long long
				*next_block, unsigned int *next_offset,
				long long index_start,
				unsigned int index_offset, int i_count,
				long long f_pos)
{
	struct squashfs3_sb_info *msblk = s->s_fs_info;
	struct squashfs3_super_block *sblk = &msblk->sblk;
	int i, length = 0;
	struct squashfs3_dir_index_2 index;

	TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %d\n",
					i_count, (unsigned int) f_pos);

	if (f_pos == 0)
		goto finish;

	for (i = 0; i < i_count; i++) {
		if (msblk->swap) {
			struct squashfs3_dir_index_2 sindex;
			squashfs3_get_cached_block(s, (char *) &sindex,
					index_start, index_offset,
					sizeof(sindex), &index_start,
					&index_offset);
			SQUASHFS3_SWAP_DIR_INDEX_2(&index, &sindex);
		} else
			squashfs3_get_cached_block(s, (char *) &index,
					index_start, index_offset,
					sizeof(index), &index_start,
					&index_offset);

		if (index.index > f_pos)
			break;

		squashfs3_get_cached_block(s, NULL, index_start, index_offset,
					index.size + 1, &index_start,
					&index_offset);

		length = index.index;
		*next_block = index.start_block + sblk->directory_table_start;
	}

	*next_offset = (length + *next_offset) % SQUASHFS3_METADATA_SIZE;

finish:
	return length;
}


static int get_dir_index_using_name(struct super_block *s, long long
				*next_block, unsigned int *next_offset,
				long long index_start,
				unsigned int index_offset, int i_count,
				const char *name, int size)
{
	struct squashfs3_sb_info *msblk = s->s_fs_info;
	struct squashfs3_super_block *sblk = &msblk->sblk;
	int i, length = 0;
	struct squashfs3_dir_index_2 *index;
	char *str;

	TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);

	if (!(str = kmalloc(sizeof(struct squashfs3_dir_index) +
		(SQUASHFS3_NAME_LEN + 1) * 2, GFP_KERNEL))) {
		ERROR("Failed to allocate squashfs3_dir_index\n");
		goto failure;
	}

	index = (struct squashfs3_dir_index_2 *) (str + SQUASHFS3_NAME_LEN + 1);
	strncpy(str, name, size);
	str[size] = '\0';

	for (i = 0; i < i_count; i++) {
		if (msblk->swap) {
			struct squashfs3_dir_index_2 sindex;
			squashfs3_get_cached_block(s, (char *) &sindex,
					index_start, index_offset,
					sizeof(sindex), &index_start,
					&index_offset);
			SQUASHFS3_SWAP_DIR_INDEX_2(index, &sindex);
		} else
			squashfs3_get_cached_block(s, (char *) index,
					index_start, index_offset,
					sizeof(struct squashfs3_dir_index_2),
					&index_start, &index_offset);

		squashfs3_get_cached_block(s, index->name, index_start,
					index_offset, index->size + 1,
					&index_start, &index_offset);

		index->name[index->size + 1] = '\0';

		if (strcmp(index->name, str) > 0)
			break;

		length = index->index;
		*next_block = index->start_block + sblk->directory_table_start;
	}

	*next_offset = (length + *next_offset) % SQUASHFS3_METADATA_SIZE;
	kfree(str);
failure:
	return length;
}


static int squashfs3_readdir_2(struct file *file, struct dir_context *ctx)
{
	struct inode *i = file_inode(file);
	struct squashfs3_sb_info *msblk = i->i_sb->s_fs_info;
	struct squashfs3_super_block *sblk = &msblk->sblk;
	long long next_block = SQUASHFS3_I(i)->start_block +
		sblk->directory_table_start;
	int next_offset = SQUASHFS3_I(i)->offset, length = 0,
		dir_count;
	struct squashfs3_dir_header_2 dirh;
	struct squashfs3_dir_entry_2 *dire;

	TRACE("Entered squashfs3_readdir_2 [%llx:%x]\n", next_block, next_offset);

	if (!(dire = kmalloc(sizeof(struct squashfs3_dir_entry) +
		SQUASHFS3_NAME_LEN + 1, GFP_KERNEL))) {
		ERROR("Failed to allocate squashfs3_dir_entry\n");
		goto finish;
	}

	length = get_dir_index_using_offset(i->i_sb, &next_block, &next_offset,
				SQUASHFS3_I(i)->u.s2.directory_index_start,
				SQUASHFS3_I(i)->u.s2.directory_index_offset,
				SQUASHFS3_I(i)->u.s2.directory_index_count,
				ctx->pos);

	while (length < i_size_read(i)) {
		/* read directory header */
		if (msblk->swap) {
			struct squashfs3_dir_header_2 sdirh;

			if (!squashfs3_get_cached_block(i->i_sb, (char *) &sdirh,
					next_block, next_offset, sizeof(sdirh),
					&next_block, &next_offset))
				goto failed_read;

			length += sizeof(sdirh);
			SQUASHFS3_SWAP_DIR_HEADER_2(&dirh, &sdirh);
		} else {
			if (!squashfs3_get_cached_block(i->i_sb, (char *) &dirh,
					next_block, next_offset, sizeof(dirh),
					&next_block, &next_offset))
				goto failed_read;

			length += sizeof(dirh);
		}

		dir_count = dirh.count + 1;
		while (dir_count--) {
			if (msblk->swap) {
				struct squashfs3_dir_entry_2 sdire;
				if (!squashfs3_get_cached_block(i->i_sb, (char *)
						&sdire, next_block, next_offset,
						sizeof(sdire), &next_block,
						&next_offset))
					goto failed_read;

				length += sizeof(sdire);
				SQUASHFS3_SWAP_DIR_ENTRY_2(dire, &sdire);
			} else {
				if (!squashfs3_get_cached_block(i->i_sb, (char *)
						dire, next_block, next_offset,
						sizeof(*dire), &next_block,
						&next_offset))
					goto failed_read;

				length += sizeof(*dire);
			}

			if (!squashfs3_get_cached_block(i->i_sb, dire->name,
						next_block, next_offset,
						dire->size + 1, &next_block,
						&next_offset))
				goto failed_read;

			length += dire->size + 1;

			if (ctx->pos >= length)
				continue;

			dire->name[dire->size + 1] = '\0';

			TRACE("Calling filldir(%x, %s, %d, %d, %x:%x, %d)\n",
					(unsigned int) ctx, dire->name,
					dire->size + 1, (int) ctx->pos,
					dirh.start_block, dire->offset,
					squashfs3_filetype_table[dire->type]);

			if (!dir_emit(ctx, dire->name, dire->size + 1,
					SQUASHFS3_MK_VFS_INODE(
					dirh.start_block, dire->offset),
					squashfs3_filetype_table[dire->type])) {
				TRACE("Filldir failed\n");
				goto finish;
			}
			ctx->pos = length;
		}
	}

finish:
	kfree(dire);
	return 0;

failed_read:
	ERROR("Unable to read directory block [%llx:%x]\n", next_block,
		next_offset);
	kfree(dire);
	return 0;
}


static struct dentry *squashfs3_lookup_2(struct inode *i, struct dentry *dentry,
				unsigned int flags)
{
	const unsigned char *name = dentry->d_name.name;
	int len = dentry->d_name.len;
	struct inode *inode = NULL;
	struct squashfs3_sb_info *msblk = i->i_sb->s_fs_info;
	struct squashfs3_super_block *sblk = &msblk->sblk;
	long long next_block = SQUASHFS3_I(i)->start_block +
				sblk->directory_table_start;
	int next_offset = SQUASHFS3_I(i)->offset, length = 0,
				dir_count;
	struct squashfs3_dir_header_2 dirh;
	struct squashfs3_dir_entry_2 *dire;
	int sorted = sblk->s_major == 2 && sblk->s_minor >= 1;

	TRACE("Entered squashfs3_lookup_2 [%llx:%x]\n", next_block, next_offset);

	if (!(dire = kmalloc(sizeof(struct squashfs3_dir_entry) +
		SQUASHFS3_NAME_LEN + 1, GFP_KERNEL))) {
		ERROR("Failed to allocate squashfs3_dir_entry\n");
		goto exit_loop;
	}

	if (len > SQUASHFS3_NAME_LEN)
		goto exit_loop;

	length = get_dir_index_using_name(i->i_sb, &next_block, &next_offset,
				SQUASHFS3_I(i)->u.s2.directory_index_start,
				SQUASHFS3_I(i)->u.s2.directory_index_offset,
				SQUASHFS3_I(i)->u.s2.directory_index_count, name,
				len);

	while (length < i_size_read(i)) {
		/* read directory header */
		if (msblk->swap) {
			struct squashfs3_dir_header_2 sdirh;
			if (!squashfs3_get_cached_block(i->i_sb, (char *) &sdirh,
					next_block, next_offset, sizeof(sdirh),
					&next_block, &next_offset))
				goto failed_read;

			length += sizeof(sdirh);
			SQUASHFS3_SWAP_DIR_HEADER_2(&dirh, &sdirh);
		} else {
			if (!squashfs3_get_cached_block(i->i_sb, (char *) &dirh,
					next_block, next_offset, sizeof(dirh),
					&next_block, &next_offset))
				goto failed_read;

			length += sizeof(dirh);
		}

		dir_count = dirh.count + 1;
		while (dir_count--) {
			if (msblk->swap) {
				struct squashfs3_dir_entry_2 sdire;
				if (!squashfs3_get_cached_block(i->i_sb, (char *)
						&sdire, next_block,next_offset,
						sizeof(sdire), &next_block,
						&next_offset))
					goto failed_read;

				length += sizeof(sdire);
				SQUASHFS3_SWAP_DIR_ENTRY_2(dire, &sdire);
			} else {
				if (!squashfs3_get_cached_block(i->i_sb, (char *)
						dire, next_block,next_offset,
						sizeof(*dire), &next_block,
						&next_offset))
					goto failed_read;

				length += sizeof(*dire);
			}

			if (!squashfs3_get_cached_block(i->i_sb, dire->name,
					next_block, next_offset, dire->size + 1,
					&next_block, &next_offset))
				goto failed_read;

			length += dire->size + 1;

			if (sorted && name[0] < dire->name[0])
				goto exit_loop;

			if ((len == dire->size + 1) && !strncmp(name,
						dire->name, len)) {
				squashfs3_inode_t ino =
					SQUASHFS3_MKINODE(dirh.start_block,
					dire->offset);
				unsigned int inode_number = SQUASHFS3_MK_VFS_INODE(dirh.start_block,
					dire->offset);

				TRACE("calling squashfs3_iget for directory "
					"entry %s, inode %x:%x, %lld\n", name,
					dirh.start_block, dire->offset, ino);

				inode = squashfs3_iget(i->i_sb, ino, inode_number);

				goto exit_loop;
			}
		}
	}

exit_loop:
	kfree(dire);
	d_add(dentry, inode);
	return ERR_PTR(0);

failed_read:
	ERROR("Unable to read directory block [%llx:%x]\n", next_block,
		next_offset);
	goto exit_loop;
}


int squashfs3_2_0_supported(struct squashfs3_sb_info *msblk)
{
	struct squashfs3_super_block *sblk = &msblk->sblk;

	msblk->read_inode = squashfs3_read_inode_2;
	msblk->read_fragment_index_table = read_fragment_index_table_2;

	sblk->bytes_used = sblk->bytes_used_2;
	sblk->uid_start = sblk->uid_start_2;
	sblk->guid_start = sblk->guid_start_2;
	sblk->inode_table_start = sblk->inode_table_start_2;
	sblk->directory_table_start = sblk->directory_table_start_2;
	sblk->fragment_table_start = sblk->fragment_table_start_2;

	return 1;
}