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