xref: /openbmc/linux/fs/isofs/inode.c (revision cb1aaebe)
1 /*
2  *  linux/fs/isofs/inode.c
3  *
4  *  (C) 1991  Linus Torvalds - minix filesystem
5  *      1992, 1993, 1994  Eric Youngdale Modified for ISO 9660 filesystem.
6  *      1994  Eberhard Mönkeberg - multi session handling.
7  *      1995  Mark Dobie - allow mounting of some weird VideoCDs and PhotoCDs.
8  *	1997  Gordon Chaffee - Joliet CDs
9  *	1998  Eric Lammerts - ISO 9660 Level 3
10  *	2004  Paul Serice - Inode Support pushed out from 4GB to 128GB
11  *	2004  Paul Serice - NFS Export Operations
12  */
13 
14 #include <linux/init.h>
15 #include <linux/module.h>
16 
17 #include <linux/slab.h>
18 #include <linux/cred.h>
19 #include <linux/nls.h>
20 #include <linux/ctype.h>
21 #include <linux/statfs.h>
22 #include <linux/cdrom.h>
23 #include <linux/parser.h>
24 #include <linux/mpage.h>
25 #include <linux/user_namespace.h>
26 #include <linux/seq_file.h>
27 #include <linux/blkdev.h>
28 
29 #include "isofs.h"
30 #include "zisofs.h"
31 
32 #define BEQUIET
33 
34 static int isofs_hashi(const struct dentry *parent, struct qstr *qstr);
35 static int isofs_dentry_cmpi(const struct dentry *dentry,
36 		unsigned int len, const char *str, const struct qstr *name);
37 
38 #ifdef CONFIG_JOLIET
39 static int isofs_hashi_ms(const struct dentry *parent, struct qstr *qstr);
40 static int isofs_hash_ms(const struct dentry *parent, struct qstr *qstr);
41 static int isofs_dentry_cmpi_ms(const struct dentry *dentry,
42 		unsigned int len, const char *str, const struct qstr *name);
43 static int isofs_dentry_cmp_ms(const struct dentry *dentry,
44 		unsigned int len, const char *str, const struct qstr *name);
45 #endif
46 
47 static void isofs_put_super(struct super_block *sb)
48 {
49 	struct isofs_sb_info *sbi = ISOFS_SB(sb);
50 
51 #ifdef CONFIG_JOLIET
52 	unload_nls(sbi->s_nls_iocharset);
53 #endif
54 
55 	kfree(sbi);
56 	sb->s_fs_info = NULL;
57 	return;
58 }
59 
60 static int isofs_read_inode(struct inode *, int relocated);
61 static int isofs_statfs (struct dentry *, struct kstatfs *);
62 static int isofs_show_options(struct seq_file *, struct dentry *);
63 
64 static struct kmem_cache *isofs_inode_cachep;
65 
66 static struct inode *isofs_alloc_inode(struct super_block *sb)
67 {
68 	struct iso_inode_info *ei;
69 	ei = kmem_cache_alloc(isofs_inode_cachep, GFP_KERNEL);
70 	if (!ei)
71 		return NULL;
72 	return &ei->vfs_inode;
73 }
74 
75 static void isofs_free_inode(struct inode *inode)
76 {
77 	kmem_cache_free(isofs_inode_cachep, ISOFS_I(inode));
78 }
79 
80 static void init_once(void *foo)
81 {
82 	struct iso_inode_info *ei = foo;
83 
84 	inode_init_once(&ei->vfs_inode);
85 }
86 
87 static int __init init_inodecache(void)
88 {
89 	isofs_inode_cachep = kmem_cache_create("isofs_inode_cache",
90 					sizeof(struct iso_inode_info),
91 					0, (SLAB_RECLAIM_ACCOUNT|
92 					SLAB_MEM_SPREAD|SLAB_ACCOUNT),
93 					init_once);
94 	if (!isofs_inode_cachep)
95 		return -ENOMEM;
96 	return 0;
97 }
98 
99 static void destroy_inodecache(void)
100 {
101 	/*
102 	 * Make sure all delayed rcu free inodes are flushed before we
103 	 * destroy cache.
104 	 */
105 	rcu_barrier();
106 	kmem_cache_destroy(isofs_inode_cachep);
107 }
108 
109 static int isofs_remount(struct super_block *sb, int *flags, char *data)
110 {
111 	sync_filesystem(sb);
112 	if (!(*flags & SB_RDONLY))
113 		return -EROFS;
114 	return 0;
115 }
116 
117 static const struct super_operations isofs_sops = {
118 	.alloc_inode	= isofs_alloc_inode,
119 	.free_inode	= isofs_free_inode,
120 	.put_super	= isofs_put_super,
121 	.statfs		= isofs_statfs,
122 	.remount_fs	= isofs_remount,
123 	.show_options	= isofs_show_options,
124 };
125 
126 
127 static const struct dentry_operations isofs_dentry_ops[] = {
128 	{
129 		.d_hash		= isofs_hashi,
130 		.d_compare	= isofs_dentry_cmpi,
131 	},
132 #ifdef CONFIG_JOLIET
133 	{
134 		.d_hash		= isofs_hash_ms,
135 		.d_compare	= isofs_dentry_cmp_ms,
136 	},
137 	{
138 		.d_hash		= isofs_hashi_ms,
139 		.d_compare	= isofs_dentry_cmpi_ms,
140 	},
141 #endif
142 };
143 
144 struct iso9660_options{
145 	unsigned int rock:1;
146 	unsigned int joliet:1;
147 	unsigned int cruft:1;
148 	unsigned int hide:1;
149 	unsigned int showassoc:1;
150 	unsigned int nocompress:1;
151 	unsigned int overriderockperm:1;
152 	unsigned int uid_set:1;
153 	unsigned int gid_set:1;
154 	unsigned int utf8:1;
155 	unsigned char map;
156 	unsigned char check;
157 	unsigned int blocksize;
158 	umode_t fmode;
159 	umode_t dmode;
160 	kgid_t gid;
161 	kuid_t uid;
162 	char *iocharset;
163 	/* LVE */
164 	s32 session;
165 	s32 sbsector;
166 };
167 
168 /*
169  * Compute the hash for the isofs name corresponding to the dentry.
170  */
171 static int
172 isofs_hashi_common(const struct dentry *dentry, struct qstr *qstr, int ms)
173 {
174 	const char *name;
175 	int len;
176 	char c;
177 	unsigned long hash;
178 
179 	len = qstr->len;
180 	name = qstr->name;
181 	if (ms) {
182 		while (len && name[len-1] == '.')
183 			len--;
184 	}
185 
186 	hash = init_name_hash(dentry);
187 	while (len--) {
188 		c = tolower(*name++);
189 		hash = partial_name_hash(c, hash);
190 	}
191 	qstr->hash = end_name_hash(hash);
192 
193 	return 0;
194 }
195 
196 /*
197  * Compare of two isofs names.
198  */
199 static int isofs_dentry_cmp_common(
200 		unsigned int len, const char *str,
201 		const struct qstr *name, int ms, int ci)
202 {
203 	int alen, blen;
204 
205 	/* A filename cannot end in '.' or we treat it like it has none */
206 	alen = name->len;
207 	blen = len;
208 	if (ms) {
209 		while (alen && name->name[alen-1] == '.')
210 			alen--;
211 		while (blen && str[blen-1] == '.')
212 			blen--;
213 	}
214 	if (alen == blen) {
215 		if (ci) {
216 			if (strncasecmp(name->name, str, alen) == 0)
217 				return 0;
218 		} else {
219 			if (strncmp(name->name, str, alen) == 0)
220 				return 0;
221 		}
222 	}
223 	return 1;
224 }
225 
226 static int
227 isofs_hashi(const struct dentry *dentry, struct qstr *qstr)
228 {
229 	return isofs_hashi_common(dentry, qstr, 0);
230 }
231 
232 static int
233 isofs_dentry_cmpi(const struct dentry *dentry,
234 		unsigned int len, const char *str, const struct qstr *name)
235 {
236 	return isofs_dentry_cmp_common(len, str, name, 0, 1);
237 }
238 
239 #ifdef CONFIG_JOLIET
240 /*
241  * Compute the hash for the isofs name corresponding to the dentry.
242  */
243 static int
244 isofs_hash_common(const struct dentry *dentry, struct qstr *qstr, int ms)
245 {
246 	const char *name;
247 	int len;
248 
249 	len = qstr->len;
250 	name = qstr->name;
251 	if (ms) {
252 		while (len && name[len-1] == '.')
253 			len--;
254 	}
255 
256 	qstr->hash = full_name_hash(dentry, name, len);
257 
258 	return 0;
259 }
260 
261 static int
262 isofs_hash_ms(const struct dentry *dentry, struct qstr *qstr)
263 {
264 	return isofs_hash_common(dentry, qstr, 1);
265 }
266 
267 static int
268 isofs_hashi_ms(const struct dentry *dentry, struct qstr *qstr)
269 {
270 	return isofs_hashi_common(dentry, qstr, 1);
271 }
272 
273 static int
274 isofs_dentry_cmp_ms(const struct dentry *dentry,
275 		unsigned int len, const char *str, const struct qstr *name)
276 {
277 	return isofs_dentry_cmp_common(len, str, name, 1, 0);
278 }
279 
280 static int
281 isofs_dentry_cmpi_ms(const struct dentry *dentry,
282 		unsigned int len, const char *str, const struct qstr *name)
283 {
284 	return isofs_dentry_cmp_common(len, str, name, 1, 1);
285 }
286 #endif
287 
288 enum {
289 	Opt_block, Opt_check_r, Opt_check_s, Opt_cruft, Opt_gid, Opt_ignore,
290 	Opt_iocharset, Opt_map_a, Opt_map_n, Opt_map_o, Opt_mode, Opt_nojoliet,
291 	Opt_norock, Opt_sb, Opt_session, Opt_uid, Opt_unhide, Opt_utf8, Opt_err,
292 	Opt_nocompress, Opt_hide, Opt_showassoc, Opt_dmode, Opt_overriderockperm,
293 };
294 
295 static const match_table_t tokens = {
296 	{Opt_norock, "norock"},
297 	{Opt_nojoliet, "nojoliet"},
298 	{Opt_unhide, "unhide"},
299 	{Opt_hide, "hide"},
300 	{Opt_showassoc, "showassoc"},
301 	{Opt_cruft, "cruft"},
302 	{Opt_utf8, "utf8"},
303 	{Opt_iocharset, "iocharset=%s"},
304 	{Opt_map_a, "map=acorn"},
305 	{Opt_map_a, "map=a"},
306 	{Opt_map_n, "map=normal"},
307 	{Opt_map_n, "map=n"},
308 	{Opt_map_o, "map=off"},
309 	{Opt_map_o, "map=o"},
310 	{Opt_session, "session=%u"},
311 	{Opt_sb, "sbsector=%u"},
312 	{Opt_check_r, "check=relaxed"},
313 	{Opt_check_r, "check=r"},
314 	{Opt_check_s, "check=strict"},
315 	{Opt_check_s, "check=s"},
316 	{Opt_uid, "uid=%u"},
317 	{Opt_gid, "gid=%u"},
318 	{Opt_mode, "mode=%u"},
319 	{Opt_dmode, "dmode=%u"},
320 	{Opt_overriderockperm, "overriderockperm"},
321 	{Opt_block, "block=%u"},
322 	{Opt_ignore, "conv=binary"},
323 	{Opt_ignore, "conv=b"},
324 	{Opt_ignore, "conv=text"},
325 	{Opt_ignore, "conv=t"},
326 	{Opt_ignore, "conv=mtext"},
327 	{Opt_ignore, "conv=m"},
328 	{Opt_ignore, "conv=auto"},
329 	{Opt_ignore, "conv=a"},
330 	{Opt_nocompress, "nocompress"},
331 	{Opt_err, NULL}
332 };
333 
334 static int parse_options(char *options, struct iso9660_options *popt)
335 {
336 	char *p;
337 	int option;
338 
339 	popt->map = 'n';
340 	popt->rock = 1;
341 	popt->joliet = 1;
342 	popt->cruft = 0;
343 	popt->hide = 0;
344 	popt->showassoc = 0;
345 	popt->check = 'u';		/* unset */
346 	popt->nocompress = 0;
347 	popt->blocksize = 1024;
348 	popt->fmode = popt->dmode = ISOFS_INVALID_MODE;
349 	popt->uid_set = 0;
350 	popt->gid_set = 0;
351 	popt->gid = GLOBAL_ROOT_GID;
352 	popt->uid = GLOBAL_ROOT_UID;
353 	popt->iocharset = NULL;
354 	popt->utf8 = 0;
355 	popt->overriderockperm = 0;
356 	popt->session=-1;
357 	popt->sbsector=-1;
358 	if (!options)
359 		return 1;
360 
361 	while ((p = strsep(&options, ",")) != NULL) {
362 		int token;
363 		substring_t args[MAX_OPT_ARGS];
364 		unsigned n;
365 
366 		if (!*p)
367 			continue;
368 
369 		token = match_token(p, tokens, args);
370 		switch (token) {
371 		case Opt_norock:
372 			popt->rock = 0;
373 			break;
374 		case Opt_nojoliet:
375 			popt->joliet = 0;
376 			break;
377 		case Opt_hide:
378 			popt->hide = 1;
379 			break;
380 		case Opt_unhide:
381 		case Opt_showassoc:
382 			popt->showassoc = 1;
383 			break;
384 		case Opt_cruft:
385 			popt->cruft = 1;
386 			break;
387 		case Opt_utf8:
388 			popt->utf8 = 1;
389 			break;
390 #ifdef CONFIG_JOLIET
391 		case Opt_iocharset:
392 			kfree(popt->iocharset);
393 			popt->iocharset = match_strdup(&args[0]);
394 			if (!popt->iocharset)
395 				return 0;
396 			break;
397 #endif
398 		case Opt_map_a:
399 			popt->map = 'a';
400 			break;
401 		case Opt_map_o:
402 			popt->map = 'o';
403 			break;
404 		case Opt_map_n:
405 			popt->map = 'n';
406 			break;
407 		case Opt_session:
408 			if (match_int(&args[0], &option))
409 				return 0;
410 			n = option;
411 			/*
412 			 * Track numbers are supposed to be in range 1-99, the
413 			 * mount option starts indexing at 0.
414 			 */
415 			if (n >= 99)
416 				return 0;
417 			popt->session = n + 1;
418 			break;
419 		case Opt_sb:
420 			if (match_int(&args[0], &option))
421 				return 0;
422 			popt->sbsector = option;
423 			break;
424 		case Opt_check_r:
425 			popt->check = 'r';
426 			break;
427 		case Opt_check_s:
428 			popt->check = 's';
429 			break;
430 		case Opt_ignore:
431 			break;
432 		case Opt_uid:
433 			if (match_int(&args[0], &option))
434 				return 0;
435 			popt->uid = make_kuid(current_user_ns(), option);
436 			if (!uid_valid(popt->uid))
437 				return 0;
438 			popt->uid_set = 1;
439 			break;
440 		case Opt_gid:
441 			if (match_int(&args[0], &option))
442 				return 0;
443 			popt->gid = make_kgid(current_user_ns(), option);
444 			if (!gid_valid(popt->gid))
445 				return 0;
446 			popt->gid_set = 1;
447 			break;
448 		case Opt_mode:
449 			if (match_int(&args[0], &option))
450 				return 0;
451 			popt->fmode = option;
452 			break;
453 		case Opt_dmode:
454 			if (match_int(&args[0], &option))
455 				return 0;
456 			popt->dmode = option;
457 			break;
458 		case Opt_overriderockperm:
459 			popt->overriderockperm = 1;
460 			break;
461 		case Opt_block:
462 			if (match_int(&args[0], &option))
463 				return 0;
464 			n = option;
465 			if (n != 512 && n != 1024 && n != 2048)
466 				return 0;
467 			popt->blocksize = n;
468 			break;
469 		case Opt_nocompress:
470 			popt->nocompress = 1;
471 			break;
472 		default:
473 			return 0;
474 		}
475 	}
476 	return 1;
477 }
478 
479 /*
480  * Display the mount options in /proc/mounts.
481  */
482 static int isofs_show_options(struct seq_file *m, struct dentry *root)
483 {
484 	struct isofs_sb_info *sbi = ISOFS_SB(root->d_sb);
485 
486 	if (!sbi->s_rock)		seq_puts(m, ",norock");
487 	else if (!sbi->s_joliet_level)	seq_puts(m, ",nojoliet");
488 	if (sbi->s_cruft)		seq_puts(m, ",cruft");
489 	if (sbi->s_hide)		seq_puts(m, ",hide");
490 	if (sbi->s_nocompress)		seq_puts(m, ",nocompress");
491 	if (sbi->s_overriderockperm)	seq_puts(m, ",overriderockperm");
492 	if (sbi->s_showassoc)		seq_puts(m, ",showassoc");
493 	if (sbi->s_utf8)		seq_puts(m, ",utf8");
494 
495 	if (sbi->s_check)		seq_printf(m, ",check=%c", sbi->s_check);
496 	if (sbi->s_mapping)		seq_printf(m, ",map=%c", sbi->s_mapping);
497 	if (sbi->s_session != 255)	seq_printf(m, ",session=%u", sbi->s_session - 1);
498 	if (sbi->s_sbsector != -1)	seq_printf(m, ",sbsector=%u", sbi->s_sbsector);
499 
500 	if (root->d_sb->s_blocksize != 1024)
501 		seq_printf(m, ",blocksize=%lu", root->d_sb->s_blocksize);
502 
503 	if (sbi->s_uid_set)
504 		seq_printf(m, ",uid=%u",
505 			   from_kuid_munged(&init_user_ns, sbi->s_uid));
506 	if (sbi->s_gid_set)
507 		seq_printf(m, ",gid=%u",
508 			   from_kgid_munged(&init_user_ns, sbi->s_gid));
509 
510 	if (sbi->s_dmode != ISOFS_INVALID_MODE)
511 		seq_printf(m, ",dmode=%o", sbi->s_dmode);
512 	if (sbi->s_fmode != ISOFS_INVALID_MODE)
513 		seq_printf(m, ",fmode=%o", sbi->s_fmode);
514 
515 #ifdef CONFIG_JOLIET
516 	if (sbi->s_nls_iocharset &&
517 	    strcmp(sbi->s_nls_iocharset->charset, CONFIG_NLS_DEFAULT) != 0)
518 		seq_printf(m, ",iocharset=%s", sbi->s_nls_iocharset->charset);
519 #endif
520 	return 0;
521 }
522 
523 /*
524  * look if the driver can tell the multi session redirection value
525  *
526  * don't change this if you don't know what you do, please!
527  * Multisession is legal only with XA disks.
528  * A non-XA disk with more than one volume descriptor may do it right, but
529  * usually is written in a nowhere standardized "multi-partition" manner.
530  * Multisession uses absolute addressing (solely the first frame of the whole
531  * track is #0), multi-partition uses relative addressing (each first frame of
532  * each track is #0), and a track is not a session.
533  *
534  * A broken CDwriter software or drive firmware does not set new standards,
535  * at least not if conflicting with the existing ones.
536  *
537  * emoenke@gwdg.de
538  */
539 #define WE_OBEY_THE_WRITTEN_STANDARDS 1
540 
541 static unsigned int isofs_get_last_session(struct super_block *sb, s32 session)
542 {
543 	struct cdrom_multisession ms_info;
544 	unsigned int vol_desc_start;
545 	struct block_device *bdev = sb->s_bdev;
546 	int i;
547 
548 	vol_desc_start=0;
549 	ms_info.addr_format=CDROM_LBA;
550 	if (session > 0) {
551 		struct cdrom_tocentry Te;
552 		Te.cdte_track=session;
553 		Te.cdte_format=CDROM_LBA;
554 		i = ioctl_by_bdev(bdev, CDROMREADTOCENTRY, (unsigned long) &Te);
555 		if (!i) {
556 			printk(KERN_DEBUG "ISOFS: Session %d start %d type %d\n",
557 				session, Te.cdte_addr.lba,
558 				Te.cdte_ctrl&CDROM_DATA_TRACK);
559 			if ((Te.cdte_ctrl&CDROM_DATA_TRACK) == 4)
560 				return Te.cdte_addr.lba;
561 		}
562 
563 		printk(KERN_ERR "ISOFS: Invalid session number or type of track\n");
564 	}
565 	i = ioctl_by_bdev(bdev, CDROMMULTISESSION, (unsigned long) &ms_info);
566 	if (session > 0)
567 		printk(KERN_ERR "ISOFS: Invalid session number\n");
568 #if 0
569 	printk(KERN_DEBUG "isofs.inode: CDROMMULTISESSION: rc=%d\n",i);
570 	if (i==0) {
571 		printk(KERN_DEBUG "isofs.inode: XA disk: %s\n",ms_info.xa_flag?"yes":"no");
572 		printk(KERN_DEBUG "isofs.inode: vol_desc_start = %d\n", ms_info.addr.lba);
573 	}
574 #endif
575 	if (i==0)
576 #if WE_OBEY_THE_WRITTEN_STANDARDS
577 		if (ms_info.xa_flag) /* necessary for a valid ms_info.addr */
578 #endif
579 			vol_desc_start=ms_info.addr.lba;
580 	return vol_desc_start;
581 }
582 
583 /*
584  * Check if root directory is empty (has less than 3 files).
585  *
586  * Used to detect broken CDs where ISO root directory is empty but Joliet root
587  * directory is OK. If such CD has Rock Ridge extensions, they will be disabled
588  * (and Joliet used instead) or else no files would be visible.
589  */
590 static bool rootdir_empty(struct super_block *sb, unsigned long block)
591 {
592 	int offset = 0, files = 0, de_len;
593 	struct iso_directory_record *de;
594 	struct buffer_head *bh;
595 
596 	bh = sb_bread(sb, block);
597 	if (!bh)
598 		return true;
599 	while (files < 3) {
600 		de = (struct iso_directory_record *) (bh->b_data + offset);
601 		de_len = *(unsigned char *) de;
602 		if (de_len == 0)
603 			break;
604 		files++;
605 		offset += de_len;
606 	}
607 	brelse(bh);
608 	return files < 3;
609 }
610 
611 /*
612  * Initialize the superblock and read the root inode.
613  *
614  * Note: a check_disk_change() has been done immediately prior
615  * to this call, so we don't need to check again.
616  */
617 static int isofs_fill_super(struct super_block *s, void *data, int silent)
618 {
619 	struct buffer_head *bh = NULL, *pri_bh = NULL;
620 	struct hs_primary_descriptor *h_pri = NULL;
621 	struct iso_primary_descriptor *pri = NULL;
622 	struct iso_supplementary_descriptor *sec = NULL;
623 	struct iso_directory_record *rootp;
624 	struct inode *inode;
625 	struct iso9660_options opt;
626 	struct isofs_sb_info *sbi;
627 	unsigned long first_data_zone;
628 	int joliet_level = 0;
629 	int iso_blknum, block;
630 	int orig_zonesize;
631 	int table, error = -EINVAL;
632 	unsigned int vol_desc_start;
633 
634 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
635 	if (!sbi)
636 		return -ENOMEM;
637 	s->s_fs_info = sbi;
638 
639 	if (!parse_options((char *)data, &opt))
640 		goto out_freesbi;
641 
642 	/*
643 	 * First of all, get the hardware blocksize for this device.
644 	 * If we don't know what it is, or the hardware blocksize is
645 	 * larger than the blocksize the user specified, then use
646 	 * that value.
647 	 */
648 	/*
649 	 * What if bugger tells us to go beyond page size?
650 	 */
651 	if (bdev_logical_block_size(s->s_bdev) > 2048) {
652 		printk(KERN_WARNING
653 		       "ISOFS: unsupported/invalid hardware sector size %d\n",
654 			bdev_logical_block_size(s->s_bdev));
655 		goto out_freesbi;
656 	}
657 	opt.blocksize = sb_min_blocksize(s, opt.blocksize);
658 
659 	sbi->s_high_sierra = 0; /* default is iso9660 */
660 	sbi->s_session = opt.session;
661 	sbi->s_sbsector = opt.sbsector;
662 
663 	vol_desc_start = (opt.sbsector != -1) ?
664 		opt.sbsector : isofs_get_last_session(s,opt.session);
665 
666 	for (iso_blknum = vol_desc_start+16;
667 		iso_blknum < vol_desc_start+100; iso_blknum++) {
668 		struct hs_volume_descriptor *hdp;
669 		struct iso_volume_descriptor  *vdp;
670 
671 		block = iso_blknum << (ISOFS_BLOCK_BITS - s->s_blocksize_bits);
672 		if (!(bh = sb_bread(s, block)))
673 			goto out_no_read;
674 
675 		vdp = (struct iso_volume_descriptor *)bh->b_data;
676 		hdp = (struct hs_volume_descriptor *)bh->b_data;
677 
678 		/*
679 		 * Due to the overlapping physical location of the descriptors,
680 		 * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
681 		 * proper identification in this case, we first check for ISO.
682 		 */
683 		if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
684 			if (isonum_711(vdp->type) == ISO_VD_END)
685 				break;
686 			if (isonum_711(vdp->type) == ISO_VD_PRIMARY) {
687 				if (!pri) {
688 					pri = (struct iso_primary_descriptor *)vdp;
689 					/* Save the buffer in case we need it ... */
690 					pri_bh = bh;
691 					bh = NULL;
692 				}
693 			}
694 #ifdef CONFIG_JOLIET
695 			else if (isonum_711(vdp->type) == ISO_VD_SUPPLEMENTARY) {
696 				sec = (struct iso_supplementary_descriptor *)vdp;
697 				if (sec->escape[0] == 0x25 && sec->escape[1] == 0x2f) {
698 					if (opt.joliet) {
699 						if (sec->escape[2] == 0x40)
700 							joliet_level = 1;
701 						else if (sec->escape[2] == 0x43)
702 							joliet_level = 2;
703 						else if (sec->escape[2] == 0x45)
704 							joliet_level = 3;
705 
706 						printk(KERN_DEBUG "ISO 9660 Extensions: "
707 							"Microsoft Joliet Level %d\n",
708 							joliet_level);
709 					}
710 					goto root_found;
711 				} else {
712 				/* Unknown supplementary volume descriptor */
713 				sec = NULL;
714 				}
715 			}
716 #endif
717 		} else {
718 			if (strncmp (hdp->id, HS_STANDARD_ID, sizeof hdp->id) == 0) {
719 				if (isonum_711(hdp->type) != ISO_VD_PRIMARY)
720 					goto out_freebh;
721 
722 				sbi->s_high_sierra = 1;
723 				opt.rock = 0;
724 				h_pri = (struct hs_primary_descriptor *)vdp;
725 				goto root_found;
726 			}
727 		}
728 
729 		/* Just skip any volume descriptors we don't recognize */
730 
731 		brelse(bh);
732 		bh = NULL;
733 	}
734 	/*
735 	 * If we fall through, either no volume descriptor was found,
736 	 * or else we passed a primary descriptor looking for others.
737 	 */
738 	if (!pri)
739 		goto out_unknown_format;
740 	brelse(bh);
741 	bh = pri_bh;
742 	pri_bh = NULL;
743 
744 root_found:
745 	/* We don't support read-write mounts */
746 	if (!sb_rdonly(s)) {
747 		error = -EACCES;
748 		goto out_freebh;
749 	}
750 
751 	if (joliet_level && (!pri || !opt.rock)) {
752 		/* This is the case of Joliet with the norock mount flag.
753 		 * A disc with both Joliet and Rock Ridge is handled later
754 		 */
755 		pri = (struct iso_primary_descriptor *) sec;
756 	}
757 
758 	if(sbi->s_high_sierra){
759 		rootp = (struct iso_directory_record *) h_pri->root_directory_record;
760 		sbi->s_nzones = isonum_733(h_pri->volume_space_size);
761 		sbi->s_log_zone_size = isonum_723(h_pri->logical_block_size);
762 		sbi->s_max_size = isonum_733(h_pri->volume_space_size);
763 	} else {
764 		if (!pri)
765 			goto out_freebh;
766 		rootp = (struct iso_directory_record *) pri->root_directory_record;
767 		sbi->s_nzones = isonum_733(pri->volume_space_size);
768 		sbi->s_log_zone_size = isonum_723(pri->logical_block_size);
769 		sbi->s_max_size = isonum_733(pri->volume_space_size);
770 	}
771 
772 	sbi->s_ninodes = 0; /* No way to figure this out easily */
773 
774 	orig_zonesize = sbi->s_log_zone_size;
775 	/*
776 	 * If the zone size is smaller than the hardware sector size,
777 	 * this is a fatal error.  This would occur if the disc drive
778 	 * had sectors that were 2048 bytes, but the filesystem had
779 	 * blocks that were 512 bytes (which should only very rarely
780 	 * happen.)
781 	 */
782 	if (orig_zonesize < opt.blocksize)
783 		goto out_bad_size;
784 
785 	/* RDE: convert log zone size to bit shift */
786 	switch (sbi->s_log_zone_size) {
787 	case  512: sbi->s_log_zone_size =  9; break;
788 	case 1024: sbi->s_log_zone_size = 10; break;
789 	case 2048: sbi->s_log_zone_size = 11; break;
790 
791 	default:
792 		goto out_bad_zone_size;
793 	}
794 
795 	s->s_magic = ISOFS_SUPER_MAGIC;
796 
797 	/*
798 	 * With multi-extent files, file size is only limited by the maximum
799 	 * size of a file system, which is 8 TB.
800 	 */
801 	s->s_maxbytes = 0x80000000000LL;
802 
803 	/* Set this for reference. Its not currently used except on write
804 	   which we don't have .. */
805 
806 	first_data_zone = isonum_733(rootp->extent) +
807 			  isonum_711(rootp->ext_attr_length);
808 	sbi->s_firstdatazone = first_data_zone;
809 #ifndef BEQUIET
810 	printk(KERN_DEBUG "ISOFS: Max size:%ld   Log zone size:%ld\n",
811 		sbi->s_max_size, 1UL << sbi->s_log_zone_size);
812 	printk(KERN_DEBUG "ISOFS: First datazone:%ld\n", sbi->s_firstdatazone);
813 	if(sbi->s_high_sierra)
814 		printk(KERN_DEBUG "ISOFS: Disc in High Sierra format.\n");
815 #endif
816 
817 	/*
818 	 * If the Joliet level is set, we _may_ decide to use the
819 	 * secondary descriptor, but can't be sure until after we
820 	 * read the root inode. But before reading the root inode
821 	 * we may need to change the device blocksize, and would
822 	 * rather release the old buffer first. So, we cache the
823 	 * first_data_zone value from the secondary descriptor.
824 	 */
825 	if (joliet_level) {
826 		pri = (struct iso_primary_descriptor *) sec;
827 		rootp = (struct iso_directory_record *)
828 			pri->root_directory_record;
829 		first_data_zone = isonum_733(rootp->extent) +
830 				isonum_711(rootp->ext_attr_length);
831 	}
832 
833 	/*
834 	 * We're all done using the volume descriptor, and may need
835 	 * to change the device blocksize, so release the buffer now.
836 	 */
837 	brelse(pri_bh);
838 	brelse(bh);
839 
840 	/*
841 	 * Force the blocksize to 512 for 512 byte sectors.  The file
842 	 * read primitives really get it wrong in a bad way if we don't
843 	 * do this.
844 	 *
845 	 * Note - we should never be setting the blocksize to something
846 	 * less than the hardware sector size for the device.  If we
847 	 * do, we would end up having to read larger buffers and split
848 	 * out portions to satisfy requests.
849 	 *
850 	 * Note2- the idea here is that we want to deal with the optimal
851 	 * zonesize in the filesystem.  If we have it set to something less,
852 	 * then we have horrible problems with trying to piece together
853 	 * bits of adjacent blocks in order to properly read directory
854 	 * entries.  By forcing the blocksize in this way, we ensure
855 	 * that we will never be required to do this.
856 	 */
857 	sb_set_blocksize(s, orig_zonesize);
858 
859 	sbi->s_nls_iocharset = NULL;
860 
861 #ifdef CONFIG_JOLIET
862 	if (joliet_level && opt.utf8 == 0) {
863 		char *p = opt.iocharset ? opt.iocharset : CONFIG_NLS_DEFAULT;
864 		sbi->s_nls_iocharset = load_nls(p);
865 		if (! sbi->s_nls_iocharset) {
866 			/* Fail only if explicit charset specified */
867 			if (opt.iocharset)
868 				goto out_freesbi;
869 			sbi->s_nls_iocharset = load_nls_default();
870 		}
871 	}
872 #endif
873 	s->s_op = &isofs_sops;
874 	s->s_export_op = &isofs_export_ops;
875 	sbi->s_mapping = opt.map;
876 	sbi->s_rock = (opt.rock ? 2 : 0);
877 	sbi->s_rock_offset = -1; /* initial offset, will guess until SP is found*/
878 	sbi->s_cruft = opt.cruft;
879 	sbi->s_hide = opt.hide;
880 	sbi->s_showassoc = opt.showassoc;
881 	sbi->s_uid = opt.uid;
882 	sbi->s_gid = opt.gid;
883 	sbi->s_uid_set = opt.uid_set;
884 	sbi->s_gid_set = opt.gid_set;
885 	sbi->s_utf8 = opt.utf8;
886 	sbi->s_nocompress = opt.nocompress;
887 	sbi->s_overriderockperm = opt.overriderockperm;
888 	/*
889 	 * It would be incredibly stupid to allow people to mark every file
890 	 * on the disk as suid, so we merely allow them to set the default
891 	 * permissions.
892 	 */
893 	if (opt.fmode != ISOFS_INVALID_MODE)
894 		sbi->s_fmode = opt.fmode & 0777;
895 	else
896 		sbi->s_fmode = ISOFS_INVALID_MODE;
897 	if (opt.dmode != ISOFS_INVALID_MODE)
898 		sbi->s_dmode = opt.dmode & 0777;
899 	else
900 		sbi->s_dmode = ISOFS_INVALID_MODE;
901 
902 	/*
903 	 * Read the root inode, which _may_ result in changing
904 	 * the s_rock flag. Once we have the final s_rock value,
905 	 * we then decide whether to use the Joliet descriptor.
906 	 */
907 	inode = isofs_iget(s, sbi->s_firstdatazone, 0);
908 	if (IS_ERR(inode))
909 		goto out_no_root;
910 
911 	/*
912 	 * Fix for broken CDs with Rock Ridge and empty ISO root directory but
913 	 * correct Joliet root directory.
914 	 */
915 	if (sbi->s_rock == 1 && joliet_level &&
916 				rootdir_empty(s, sbi->s_firstdatazone)) {
917 		printk(KERN_NOTICE
918 			"ISOFS: primary root directory is empty. "
919 			"Disabling Rock Ridge and switching to Joliet.");
920 		sbi->s_rock = 0;
921 	}
922 
923 	/*
924 	 * If this disk has both Rock Ridge and Joliet on it, then we
925 	 * want to use Rock Ridge by default.  This can be overridden
926 	 * by using the norock mount option.  There is still one other
927 	 * possibility that is not taken into account: a Rock Ridge
928 	 * CD with Unicode names.  Until someone sees such a beast, it
929 	 * will not be supported.
930 	 */
931 	if (sbi->s_rock == 1) {
932 		joliet_level = 0;
933 	} else if (joliet_level) {
934 		sbi->s_rock = 0;
935 		if (sbi->s_firstdatazone != first_data_zone) {
936 			sbi->s_firstdatazone = first_data_zone;
937 			printk(KERN_DEBUG
938 				"ISOFS: changing to secondary root\n");
939 			iput(inode);
940 			inode = isofs_iget(s, sbi->s_firstdatazone, 0);
941 			if (IS_ERR(inode))
942 				goto out_no_root;
943 		}
944 	}
945 
946 	if (opt.check == 'u') {
947 		/* Only Joliet is case insensitive by default */
948 		if (joliet_level)
949 			opt.check = 'r';
950 		else
951 			opt.check = 's';
952 	}
953 	sbi->s_joliet_level = joliet_level;
954 
955 	/* Make sure the root inode is a directory */
956 	if (!S_ISDIR(inode->i_mode)) {
957 		printk(KERN_WARNING
958 			"isofs_fill_super: root inode is not a directory. "
959 			"Corrupted media?\n");
960 		goto out_iput;
961 	}
962 
963 	table = 0;
964 	if (joliet_level)
965 		table += 2;
966 	if (opt.check == 'r')
967 		table++;
968 	sbi->s_check = opt.check;
969 
970 	if (table)
971 		s->s_d_op = &isofs_dentry_ops[table - 1];
972 
973 	/* get the root dentry */
974 	s->s_root = d_make_root(inode);
975 	if (!(s->s_root)) {
976 		error = -ENOMEM;
977 		goto out_no_inode;
978 	}
979 
980 	kfree(opt.iocharset);
981 
982 	return 0;
983 
984 	/*
985 	 * Display error messages and free resources.
986 	 */
987 out_iput:
988 	iput(inode);
989 	goto out_no_inode;
990 out_no_root:
991 	error = PTR_ERR(inode);
992 	if (error != -ENOMEM)
993 		printk(KERN_WARNING "%s: get root inode failed\n", __func__);
994 out_no_inode:
995 #ifdef CONFIG_JOLIET
996 	unload_nls(sbi->s_nls_iocharset);
997 #endif
998 	goto out_freesbi;
999 out_no_read:
1000 	printk(KERN_WARNING "%s: bread failed, dev=%s, iso_blknum=%d, block=%d\n",
1001 		__func__, s->s_id, iso_blknum, block);
1002 	goto out_freebh;
1003 out_bad_zone_size:
1004 	printk(KERN_WARNING "ISOFS: Bad logical zone size %ld\n",
1005 		sbi->s_log_zone_size);
1006 	goto out_freebh;
1007 out_bad_size:
1008 	printk(KERN_WARNING "ISOFS: Logical zone size(%d) < hardware blocksize(%u)\n",
1009 		orig_zonesize, opt.blocksize);
1010 	goto out_freebh;
1011 out_unknown_format:
1012 	if (!silent)
1013 		printk(KERN_WARNING "ISOFS: Unable to identify CD-ROM format.\n");
1014 
1015 out_freebh:
1016 	brelse(bh);
1017 	brelse(pri_bh);
1018 out_freesbi:
1019 	kfree(opt.iocharset);
1020 	kfree(sbi);
1021 	s->s_fs_info = NULL;
1022 	return error;
1023 }
1024 
1025 static int isofs_statfs (struct dentry *dentry, struct kstatfs *buf)
1026 {
1027 	struct super_block *sb = dentry->d_sb;
1028 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1029 
1030 	buf->f_type = ISOFS_SUPER_MAGIC;
1031 	buf->f_bsize = sb->s_blocksize;
1032 	buf->f_blocks = (ISOFS_SB(sb)->s_nzones
1033 		<< (ISOFS_SB(sb)->s_log_zone_size - sb->s_blocksize_bits));
1034 	buf->f_bfree = 0;
1035 	buf->f_bavail = 0;
1036 	buf->f_files = ISOFS_SB(sb)->s_ninodes;
1037 	buf->f_ffree = 0;
1038 	buf->f_fsid.val[0] = (u32)id;
1039 	buf->f_fsid.val[1] = (u32)(id >> 32);
1040 	buf->f_namelen = NAME_MAX;
1041 	return 0;
1042 }
1043 
1044 /*
1045  * Get a set of blocks; filling in buffer_heads if already allocated
1046  * or getblk() if they are not.  Returns the number of blocks inserted
1047  * (-ve == error.)
1048  */
1049 int isofs_get_blocks(struct inode *inode, sector_t iblock,
1050 		     struct buffer_head **bh, unsigned long nblocks)
1051 {
1052 	unsigned long b_off = iblock;
1053 	unsigned offset, sect_size;
1054 	unsigned int firstext;
1055 	unsigned long nextblk, nextoff;
1056 	int section, rv, error;
1057 	struct iso_inode_info *ei = ISOFS_I(inode);
1058 
1059 	error = -EIO;
1060 	rv = 0;
1061 	if (iblock != b_off) {
1062 		printk(KERN_DEBUG "%s: block number too large\n", __func__);
1063 		goto abort;
1064 	}
1065 
1066 
1067 	offset = 0;
1068 	firstext = ei->i_first_extent;
1069 	sect_size = ei->i_section_size >> ISOFS_BUFFER_BITS(inode);
1070 	nextblk = ei->i_next_section_block;
1071 	nextoff = ei->i_next_section_offset;
1072 	section = 0;
1073 
1074 	while (nblocks) {
1075 		/* If we are *way* beyond the end of the file, print a message.
1076 		 * Access beyond the end of the file up to the next page boundary
1077 		 * is normal, however because of the way the page cache works.
1078 		 * In this case, we just return 0 so that we can properly fill
1079 		 * the page with useless information without generating any
1080 		 * I/O errors.
1081 		 */
1082 		if (b_off > ((inode->i_size + PAGE_SIZE - 1) >> ISOFS_BUFFER_BITS(inode))) {
1083 			printk(KERN_DEBUG "%s: block >= EOF (%lu, %llu)\n",
1084 				__func__, b_off,
1085 				(unsigned long long)inode->i_size);
1086 			goto abort;
1087 		}
1088 
1089 		/* On the last section, nextblk == 0, section size is likely to
1090 		 * exceed sect_size by a partial block, and access beyond the
1091 		 * end of the file will reach beyond the section size, too.
1092 		 */
1093 		while (nextblk && (b_off >= (offset + sect_size))) {
1094 			struct inode *ninode;
1095 
1096 			offset += sect_size;
1097 			ninode = isofs_iget(inode->i_sb, nextblk, nextoff);
1098 			if (IS_ERR(ninode)) {
1099 				error = PTR_ERR(ninode);
1100 				goto abort;
1101 			}
1102 			firstext  = ISOFS_I(ninode)->i_first_extent;
1103 			sect_size = ISOFS_I(ninode)->i_section_size >> ISOFS_BUFFER_BITS(ninode);
1104 			nextblk   = ISOFS_I(ninode)->i_next_section_block;
1105 			nextoff   = ISOFS_I(ninode)->i_next_section_offset;
1106 			iput(ninode);
1107 
1108 			if (++section > 100) {
1109 				printk(KERN_DEBUG "%s: More than 100 file sections ?!?"
1110 					" aborting...\n", __func__);
1111 				printk(KERN_DEBUG "%s: block=%lu firstext=%u sect_size=%u "
1112 					"nextblk=%lu nextoff=%lu\n", __func__,
1113 					b_off, firstext, (unsigned) sect_size,
1114 					nextblk, nextoff);
1115 				goto abort;
1116 			}
1117 		}
1118 
1119 		if (*bh) {
1120 			map_bh(*bh, inode->i_sb, firstext + b_off - offset);
1121 		} else {
1122 			*bh = sb_getblk(inode->i_sb, firstext+b_off-offset);
1123 			if (!*bh)
1124 				goto abort;
1125 		}
1126 		bh++;	/* Next buffer head */
1127 		b_off++;	/* Next buffer offset */
1128 		nblocks--;
1129 		rv++;
1130 	}
1131 
1132 	error = 0;
1133 abort:
1134 	return rv != 0 ? rv : error;
1135 }
1136 
1137 /*
1138  * Used by the standard interfaces.
1139  */
1140 static int isofs_get_block(struct inode *inode, sector_t iblock,
1141 		    struct buffer_head *bh_result, int create)
1142 {
1143 	int ret;
1144 
1145 	if (create) {
1146 		printk(KERN_DEBUG "%s: Kernel tries to allocate a block\n", __func__);
1147 		return -EROFS;
1148 	}
1149 
1150 	ret = isofs_get_blocks(inode, iblock, &bh_result, 1);
1151 	return ret < 0 ? ret : 0;
1152 }
1153 
1154 static int isofs_bmap(struct inode *inode, sector_t block)
1155 {
1156 	struct buffer_head dummy;
1157 	int error;
1158 
1159 	dummy.b_state = 0;
1160 	dummy.b_blocknr = -1000;
1161 	error = isofs_get_block(inode, block, &dummy, 0);
1162 	if (!error)
1163 		return dummy.b_blocknr;
1164 	return 0;
1165 }
1166 
1167 struct buffer_head *isofs_bread(struct inode *inode, sector_t block)
1168 {
1169 	sector_t blknr = isofs_bmap(inode, block);
1170 	if (!blknr)
1171 		return NULL;
1172 	return sb_bread(inode->i_sb, blknr);
1173 }
1174 
1175 static int isofs_readpage(struct file *file, struct page *page)
1176 {
1177 	return mpage_readpage(page, isofs_get_block);
1178 }
1179 
1180 static int isofs_readpages(struct file *file, struct address_space *mapping,
1181 			struct list_head *pages, unsigned nr_pages)
1182 {
1183 	return mpage_readpages(mapping, pages, nr_pages, isofs_get_block);
1184 }
1185 
1186 static sector_t _isofs_bmap(struct address_space *mapping, sector_t block)
1187 {
1188 	return generic_block_bmap(mapping,block,isofs_get_block);
1189 }
1190 
1191 static const struct address_space_operations isofs_aops = {
1192 	.readpage = isofs_readpage,
1193 	.readpages = isofs_readpages,
1194 	.bmap = _isofs_bmap
1195 };
1196 
1197 static int isofs_read_level3_size(struct inode *inode)
1198 {
1199 	unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
1200 	int high_sierra = ISOFS_SB(inode->i_sb)->s_high_sierra;
1201 	struct buffer_head *bh = NULL;
1202 	unsigned long block, offset, block_saved, offset_saved;
1203 	int i = 0;
1204 	int more_entries = 0;
1205 	struct iso_directory_record *tmpde = NULL;
1206 	struct iso_inode_info *ei = ISOFS_I(inode);
1207 
1208 	inode->i_size = 0;
1209 
1210 	/* The first 16 blocks are reserved as the System Area.  Thus,
1211 	 * no inodes can appear in block 0.  We use this to flag that
1212 	 * this is the last section. */
1213 	ei->i_next_section_block = 0;
1214 	ei->i_next_section_offset = 0;
1215 
1216 	block = ei->i_iget5_block;
1217 	offset = ei->i_iget5_offset;
1218 
1219 	do {
1220 		struct iso_directory_record *de;
1221 		unsigned int de_len;
1222 
1223 		if (!bh) {
1224 			bh = sb_bread(inode->i_sb, block);
1225 			if (!bh)
1226 				goto out_noread;
1227 		}
1228 		de = (struct iso_directory_record *) (bh->b_data + offset);
1229 		de_len = *(unsigned char *) de;
1230 
1231 		if (de_len == 0) {
1232 			brelse(bh);
1233 			bh = NULL;
1234 			++block;
1235 			offset = 0;
1236 			continue;
1237 		}
1238 
1239 		block_saved = block;
1240 		offset_saved = offset;
1241 		offset += de_len;
1242 
1243 		/* Make sure we have a full directory entry */
1244 		if (offset >= bufsize) {
1245 			int slop = bufsize - offset + de_len;
1246 			if (!tmpde) {
1247 				tmpde = kmalloc(256, GFP_KERNEL);
1248 				if (!tmpde)
1249 					goto out_nomem;
1250 			}
1251 			memcpy(tmpde, de, slop);
1252 			offset &= bufsize - 1;
1253 			block++;
1254 			brelse(bh);
1255 			bh = NULL;
1256 			if (offset) {
1257 				bh = sb_bread(inode->i_sb, block);
1258 				if (!bh)
1259 					goto out_noread;
1260 				memcpy((void *)tmpde+slop, bh->b_data, offset);
1261 			}
1262 			de = tmpde;
1263 		}
1264 
1265 		inode->i_size += isonum_733(de->size);
1266 		if (i == 1) {
1267 			ei->i_next_section_block = block_saved;
1268 			ei->i_next_section_offset = offset_saved;
1269 		}
1270 
1271 		more_entries = de->flags[-high_sierra] & 0x80;
1272 
1273 		i++;
1274 		if (i > 100)
1275 			goto out_toomany;
1276 	} while (more_entries);
1277 out:
1278 	kfree(tmpde);
1279 	if (bh)
1280 		brelse(bh);
1281 	return 0;
1282 
1283 out_nomem:
1284 	if (bh)
1285 		brelse(bh);
1286 	return -ENOMEM;
1287 
1288 out_noread:
1289 	printk(KERN_INFO "ISOFS: unable to read i-node block %lu\n", block);
1290 	kfree(tmpde);
1291 	return -EIO;
1292 
1293 out_toomany:
1294 	printk(KERN_INFO "%s: More than 100 file sections ?!?, aborting...\n"
1295 		"isofs_read_level3_size: inode=%lu\n",
1296 		__func__, inode->i_ino);
1297 	goto out;
1298 }
1299 
1300 static int isofs_read_inode(struct inode *inode, int relocated)
1301 {
1302 	struct super_block *sb = inode->i_sb;
1303 	struct isofs_sb_info *sbi = ISOFS_SB(sb);
1304 	unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
1305 	unsigned long block;
1306 	int high_sierra = sbi->s_high_sierra;
1307 	struct buffer_head *bh;
1308 	struct iso_directory_record *de;
1309 	struct iso_directory_record *tmpde = NULL;
1310 	unsigned int de_len;
1311 	unsigned long offset;
1312 	struct iso_inode_info *ei = ISOFS_I(inode);
1313 	int ret = -EIO;
1314 
1315 	block = ei->i_iget5_block;
1316 	bh = sb_bread(inode->i_sb, block);
1317 	if (!bh)
1318 		goto out_badread;
1319 
1320 	offset = ei->i_iget5_offset;
1321 
1322 	de = (struct iso_directory_record *) (bh->b_data + offset);
1323 	de_len = *(unsigned char *) de;
1324 
1325 	if (offset + de_len > bufsize) {
1326 		int frag1 = bufsize - offset;
1327 
1328 		tmpde = kmalloc(de_len, GFP_KERNEL);
1329 		if (!tmpde) {
1330 			ret = -ENOMEM;
1331 			goto fail;
1332 		}
1333 		memcpy(tmpde, bh->b_data + offset, frag1);
1334 		brelse(bh);
1335 		bh = sb_bread(inode->i_sb, ++block);
1336 		if (!bh)
1337 			goto out_badread;
1338 		memcpy((char *)tmpde+frag1, bh->b_data, de_len - frag1);
1339 		de = tmpde;
1340 	}
1341 
1342 	inode->i_ino = isofs_get_ino(ei->i_iget5_block,
1343 					ei->i_iget5_offset,
1344 					ISOFS_BUFFER_BITS(inode));
1345 
1346 	/* Assume it is a normal-format file unless told otherwise */
1347 	ei->i_file_format = isofs_file_normal;
1348 
1349 	if (de->flags[-high_sierra] & 2) {
1350 		if (sbi->s_dmode != ISOFS_INVALID_MODE)
1351 			inode->i_mode = S_IFDIR | sbi->s_dmode;
1352 		else
1353 			inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1354 		set_nlink(inode, 1);	/*
1355 					 * Set to 1.  We know there are 2, but
1356 					 * the find utility tries to optimize
1357 					 * if it is 2, and it screws up.  It is
1358 					 * easier to give 1 which tells find to
1359 					 * do it the hard way.
1360 					 */
1361 	} else {
1362 		if (sbi->s_fmode != ISOFS_INVALID_MODE) {
1363 			inode->i_mode = S_IFREG | sbi->s_fmode;
1364 		} else {
1365 			/*
1366 			 * Set default permissions: r-x for all.  The disc
1367 			 * could be shared with DOS machines so virtually
1368 			 * anything could be a valid executable.
1369 			 */
1370 			inode->i_mode = S_IFREG | S_IRUGO | S_IXUGO;
1371 		}
1372 		set_nlink(inode, 1);
1373 	}
1374 	inode->i_uid = sbi->s_uid;
1375 	inode->i_gid = sbi->s_gid;
1376 	inode->i_blocks = 0;
1377 
1378 	ei->i_format_parm[0] = 0;
1379 	ei->i_format_parm[1] = 0;
1380 	ei->i_format_parm[2] = 0;
1381 
1382 	ei->i_section_size = isonum_733(de->size);
1383 	if (de->flags[-high_sierra] & 0x80) {
1384 		ret = isofs_read_level3_size(inode);
1385 		if (ret < 0)
1386 			goto fail;
1387 		ret = -EIO;
1388 	} else {
1389 		ei->i_next_section_block = 0;
1390 		ei->i_next_section_offset = 0;
1391 		inode->i_size = isonum_733(de->size);
1392 	}
1393 
1394 	/*
1395 	 * Some dipshit decided to store some other bit of information
1396 	 * in the high byte of the file length.  Truncate size in case
1397 	 * this CDROM was mounted with the cruft option.
1398 	 */
1399 
1400 	if (sbi->s_cruft)
1401 		inode->i_size &= 0x00ffffff;
1402 
1403 	if (de->interleave[0]) {
1404 		printk(KERN_DEBUG "ISOFS: Interleaved files not (yet) supported.\n");
1405 		inode->i_size = 0;
1406 	}
1407 
1408 	/* I have no idea what file_unit_size is used for, so
1409 	   we will flag it for now */
1410 	if (de->file_unit_size[0] != 0) {
1411 		printk(KERN_DEBUG "ISOFS: File unit size != 0 for ISO file (%ld).\n",
1412 			inode->i_ino);
1413 	}
1414 
1415 	/* I have no idea what other flag bits are used for, so
1416 	   we will flag it for now */
1417 #ifdef DEBUG
1418 	if((de->flags[-high_sierra] & ~2)!= 0){
1419 		printk(KERN_DEBUG "ISOFS: Unusual flag settings for ISO file "
1420 				"(%ld %x).\n",
1421 			inode->i_ino, de->flags[-high_sierra]);
1422 	}
1423 #endif
1424 
1425 	inode->i_mtime.tv_sec =
1426 	inode->i_atime.tv_sec =
1427 	inode->i_ctime.tv_sec = iso_date(de->date, high_sierra);
1428 	inode->i_mtime.tv_nsec =
1429 	inode->i_atime.tv_nsec =
1430 	inode->i_ctime.tv_nsec = 0;
1431 
1432 	ei->i_first_extent = (isonum_733(de->extent) +
1433 			isonum_711(de->ext_attr_length));
1434 
1435 	/* Set the number of blocks for stat() - should be done before RR */
1436 	inode->i_blocks = (inode->i_size + 511) >> 9;
1437 
1438 	/*
1439 	 * Now test for possible Rock Ridge extensions which will override
1440 	 * some of these numbers in the inode structure.
1441 	 */
1442 
1443 	if (!high_sierra) {
1444 		parse_rock_ridge_inode(de, inode, relocated);
1445 		/* if we want uid/gid set, override the rock ridge setting */
1446 		if (sbi->s_uid_set)
1447 			inode->i_uid = sbi->s_uid;
1448 		if (sbi->s_gid_set)
1449 			inode->i_gid = sbi->s_gid;
1450 	}
1451 	/* Now set final access rights if overriding rock ridge setting */
1452 	if (S_ISDIR(inode->i_mode) && sbi->s_overriderockperm &&
1453 	    sbi->s_dmode != ISOFS_INVALID_MODE)
1454 		inode->i_mode = S_IFDIR | sbi->s_dmode;
1455 	if (S_ISREG(inode->i_mode) && sbi->s_overriderockperm &&
1456 	    sbi->s_fmode != ISOFS_INVALID_MODE)
1457 		inode->i_mode = S_IFREG | sbi->s_fmode;
1458 
1459 	/* Install the inode operations vector */
1460 	if (S_ISREG(inode->i_mode)) {
1461 		inode->i_fop = &generic_ro_fops;
1462 		switch (ei->i_file_format) {
1463 #ifdef CONFIG_ZISOFS
1464 		case isofs_file_compressed:
1465 			inode->i_data.a_ops = &zisofs_aops;
1466 			break;
1467 #endif
1468 		default:
1469 			inode->i_data.a_ops = &isofs_aops;
1470 			break;
1471 		}
1472 	} else if (S_ISDIR(inode->i_mode)) {
1473 		inode->i_op = &isofs_dir_inode_operations;
1474 		inode->i_fop = &isofs_dir_operations;
1475 	} else if (S_ISLNK(inode->i_mode)) {
1476 		inode->i_op = &page_symlink_inode_operations;
1477 		inode_nohighmem(inode);
1478 		inode->i_data.a_ops = &isofs_symlink_aops;
1479 	} else
1480 		/* XXX - parse_rock_ridge_inode() had already set i_rdev. */
1481 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
1482 
1483 	ret = 0;
1484 out:
1485 	kfree(tmpde);
1486 	if (bh)
1487 		brelse(bh);
1488 	return ret;
1489 
1490 out_badread:
1491 	printk(KERN_WARNING "ISOFS: unable to read i-node block\n");
1492 fail:
1493 	goto out;
1494 }
1495 
1496 struct isofs_iget5_callback_data {
1497 	unsigned long block;
1498 	unsigned long offset;
1499 };
1500 
1501 static int isofs_iget5_test(struct inode *ino, void *data)
1502 {
1503 	struct iso_inode_info *i = ISOFS_I(ino);
1504 	struct isofs_iget5_callback_data *d =
1505 		(struct isofs_iget5_callback_data*)data;
1506 	return (i->i_iget5_block == d->block)
1507 		&& (i->i_iget5_offset == d->offset);
1508 }
1509 
1510 static int isofs_iget5_set(struct inode *ino, void *data)
1511 {
1512 	struct iso_inode_info *i = ISOFS_I(ino);
1513 	struct isofs_iget5_callback_data *d =
1514 		(struct isofs_iget5_callback_data*)data;
1515 	i->i_iget5_block = d->block;
1516 	i->i_iget5_offset = d->offset;
1517 	return 0;
1518 }
1519 
1520 /* Store, in the inode's containing structure, the block and block
1521  * offset that point to the underlying meta-data for the inode.  The
1522  * code below is otherwise similar to the iget() code in
1523  * include/linux/fs.h */
1524 struct inode *__isofs_iget(struct super_block *sb,
1525 			   unsigned long block,
1526 			   unsigned long offset,
1527 			   int relocated)
1528 {
1529 	unsigned long hashval;
1530 	struct inode *inode;
1531 	struct isofs_iget5_callback_data data;
1532 	long ret;
1533 
1534 	if (offset >= 1ul << sb->s_blocksize_bits)
1535 		return ERR_PTR(-EINVAL);
1536 
1537 	data.block = block;
1538 	data.offset = offset;
1539 
1540 	hashval = (block << sb->s_blocksize_bits) | offset;
1541 
1542 	inode = iget5_locked(sb, hashval, &isofs_iget5_test,
1543 				&isofs_iget5_set, &data);
1544 
1545 	if (!inode)
1546 		return ERR_PTR(-ENOMEM);
1547 
1548 	if (inode->i_state & I_NEW) {
1549 		ret = isofs_read_inode(inode, relocated);
1550 		if (ret < 0) {
1551 			iget_failed(inode);
1552 			inode = ERR_PTR(ret);
1553 		} else {
1554 			unlock_new_inode(inode);
1555 		}
1556 	}
1557 
1558 	return inode;
1559 }
1560 
1561 static struct dentry *isofs_mount(struct file_system_type *fs_type,
1562 	int flags, const char *dev_name, void *data)
1563 {
1564 	return mount_bdev(fs_type, flags, dev_name, data, isofs_fill_super);
1565 }
1566 
1567 static struct file_system_type iso9660_fs_type = {
1568 	.owner		= THIS_MODULE,
1569 	.name		= "iso9660",
1570 	.mount		= isofs_mount,
1571 	.kill_sb	= kill_block_super,
1572 	.fs_flags	= FS_REQUIRES_DEV,
1573 };
1574 MODULE_ALIAS_FS("iso9660");
1575 MODULE_ALIAS("iso9660");
1576 
1577 static int __init init_iso9660_fs(void)
1578 {
1579 	int err = init_inodecache();
1580 	if (err)
1581 		goto out;
1582 #ifdef CONFIG_ZISOFS
1583 	err = zisofs_init();
1584 	if (err)
1585 		goto out1;
1586 #endif
1587 	err = register_filesystem(&iso9660_fs_type);
1588 	if (err)
1589 		goto out2;
1590 	return 0;
1591 out2:
1592 #ifdef CONFIG_ZISOFS
1593 	zisofs_cleanup();
1594 out1:
1595 #endif
1596 	destroy_inodecache();
1597 out:
1598 	return err;
1599 }
1600 
1601 static void __exit exit_iso9660_fs(void)
1602 {
1603         unregister_filesystem(&iso9660_fs_type);
1604 #ifdef CONFIG_ZISOFS
1605 	zisofs_cleanup();
1606 #endif
1607 	destroy_inodecache();
1608 }
1609 
1610 module_init(init_iso9660_fs)
1611 module_exit(exit_iso9660_fs)
1612 MODULE_LICENSE("GPL");
1613