inode.c 40.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
L
Linus Torvalds 已提交
2 3 4 5 6
/*
 *  linux/fs/isofs/inode.c
 *
 *  (C) 1991  Linus Torvalds - minix filesystem
 *      1992, 1993, 1994  Eric Youngdale Modified for ISO 9660 filesystem.
7
 *      1994  Eberhard Mönkeberg - multi session handling.
L
Linus Torvalds 已提交
8 9 10 11 12 13 14
 *      1995  Mark Dobie - allow mounting of some weird VideoCDs and PhotoCDs.
 *	1997  Gordon Chaffee - Joliet CDs
 *	1998  Eric Lammerts - ISO 9660 Level 3
 *	2004  Paul Serice - Inode Support pushed out from 4GB to 128GB
 *	2004  Paul Serice - NFS Export Operations
 */

A
Al Viro 已提交
15
#include <linux/init.h>
L
Linus Torvalds 已提交
16 17 18
#include <linux/module.h>

#include <linux/slab.h>
19
#include <linux/cred.h>
L
Linus Torvalds 已提交
20 21
#include <linux/nls.h>
#include <linux/ctype.h>
A
Al Viro 已提交
22 23
#include <linux/statfs.h>
#include <linux/cdrom.h>
L
Linus Torvalds 已提交
24
#include <linux/parser.h>
N
Namjae Jeon 已提交
25
#include <linux/mpage.h>
26
#include <linux/user_namespace.h>
D
David Howells 已提交
27
#include <linux/seq_file.h>
28
#include <linux/blkdev.h>
L
Linus Torvalds 已提交
29

A
Al Viro 已提交
30
#include "isofs.h"
L
Linus Torvalds 已提交
31 32
#include "zisofs.h"

33 34 35
/* max tz offset is 13 hours */
#define MAX_TZ_OFFSET (52*15*60)

L
Linus Torvalds 已提交
36 37
#define BEQUIET

38
static int isofs_hashi(const struct dentry *parent, struct qstr *qstr);
39
static int isofs_dentry_cmpi(const struct dentry *dentry,
N
Nick Piggin 已提交
40
		unsigned int len, const char *str, const struct qstr *name);
L
Linus Torvalds 已提交
41 42

#ifdef CONFIG_JOLIET
43 44
static int isofs_hashi_ms(const struct dentry *parent, struct qstr *qstr);
static int isofs_hash_ms(const struct dentry *parent, struct qstr *qstr);
45
static int isofs_dentry_cmpi_ms(const struct dentry *dentry,
N
Nick Piggin 已提交
46
		unsigned int len, const char *str, const struct qstr *name);
47
static int isofs_dentry_cmp_ms(const struct dentry *dentry,
N
Nick Piggin 已提交
48
		unsigned int len, const char *str, const struct qstr *name);
L
Linus Torvalds 已提交
49 50 51 52 53
#endif

static void isofs_put_super(struct super_block *sb)
{
	struct isofs_sb_info *sbi = ISOFS_SB(sb);
54

L
Linus Torvalds 已提交
55
#ifdef CONFIG_JOLIET
56
	unload_nls(sbi->s_nls_iocharset);
L
Linus Torvalds 已提交
57 58 59 60 61 62 63
#endif

	kfree(sbi);
	sb->s_fs_info = NULL;
	return;
}

64
static int isofs_read_inode(struct inode *, int relocated);
65
static int isofs_statfs (struct dentry *, struct kstatfs *);
D
David Howells 已提交
66
static int isofs_show_options(struct seq_file *, struct dentry *);
L
Linus Torvalds 已提交
67

68
static struct kmem_cache *isofs_inode_cachep;
L
Linus Torvalds 已提交
69 70 71 72

static struct inode *isofs_alloc_inode(struct super_block *sb)
{
	struct iso_inode_info *ei;
73
	ei = kmem_cache_alloc(isofs_inode_cachep, GFP_KERNEL);
L
Linus Torvalds 已提交
74 75 76 77 78
	if (!ei)
		return NULL;
	return &ei->vfs_inode;
}

A
Al Viro 已提交
79
static void isofs_free_inode(struct inode *inode)
L
Linus Torvalds 已提交
80 81 82 83
{
	kmem_cache_free(isofs_inode_cachep, ISOFS_I(inode));
}

84
static void init_once(void *foo)
L
Linus Torvalds 已提交
85
{
86
	struct iso_inode_info *ei = foo;
L
Linus Torvalds 已提交
87

C
Christoph Lameter 已提交
88
	inode_init_once(&ei->vfs_inode);
L
Linus Torvalds 已提交
89
}
D
Dave Jones 已提交
90

91
static int __init init_inodecache(void)
L
Linus Torvalds 已提交
92 93
{
	isofs_inode_cachep = kmem_cache_create("isofs_inode_cache",
D
Dave Jones 已提交
94 95
					sizeof(struct iso_inode_info),
					0, (SLAB_RECLAIM_ACCOUNT|
96
					SLAB_MEM_SPREAD|SLAB_ACCOUNT),
97
					init_once);
98
	if (!isofs_inode_cachep)
L
Linus Torvalds 已提交
99 100 101 102 103 104
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
105 106 107 108 109
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
110
	kmem_cache_destroy(isofs_inode_cachep);
L
Linus Torvalds 已提交
111 112 113 114
}

static int isofs_remount(struct super_block *sb, int *flags, char *data)
{
115
	sync_filesystem(sb);
116
	if (!(*flags & SB_RDONLY))
117
		return -EROFS;
L
Linus Torvalds 已提交
118 119 120
	return 0;
}

121
static const struct super_operations isofs_sops = {
L
Linus Torvalds 已提交
122
	.alloc_inode	= isofs_alloc_inode,
A
Al Viro 已提交
123
	.free_inode	= isofs_free_inode,
L
Linus Torvalds 已提交
124 125 126
	.put_super	= isofs_put_super,
	.statfs		= isofs_statfs,
	.remount_fs	= isofs_remount,
D
David Howells 已提交
127
	.show_options	= isofs_show_options,
L
Linus Torvalds 已提交
128 129 130
};


131
static const struct dentry_operations isofs_dentry_ops[] = {
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140 141 142 143
	{
		.d_hash		= isofs_hashi,
		.d_compare	= isofs_dentry_cmpi,
	},
#ifdef CONFIG_JOLIET
	{
		.d_hash		= isofs_hash_ms,
		.d_compare	= isofs_dentry_cmp_ms,
	},
	{
		.d_hash		= isofs_hashi_ms,
		.d_compare	= isofs_dentry_cmpi_ms,
144
	},
L
Linus Torvalds 已提交
145 146 147 148
#endif
};

struct iso9660_options{
149
	unsigned int rock:1;
150
	unsigned int joliet:1;
151 152 153 154 155 156 157 158
	unsigned int cruft:1;
	unsigned int hide:1;
	unsigned int showassoc:1;
	unsigned int nocompress:1;
	unsigned int overriderockperm:1;
	unsigned int uid_set:1;
	unsigned int gid_set:1;
	unsigned char map;
L
Linus Torvalds 已提交
159 160
	unsigned char check;
	unsigned int blocksize;
A
Al Viro 已提交
161 162
	umode_t fmode;
	umode_t dmode;
163 164
	kgid_t gid;
	kuid_t uid;
L
Linus Torvalds 已提交
165
	char *iocharset;
D
Dave Jones 已提交
166 167 168
	/* LVE */
	s32 session;
	s32 sbsector;
L
Linus Torvalds 已提交
169 170 171 172 173 174
};

/*
 * Compute the hash for the isofs name corresponding to the dentry.
 */
static int
175
isofs_hashi_common(const struct dentry *dentry, struct qstr *qstr, int ms)
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188
{
	const char *name;
	int len;
	char c;
	unsigned long hash;

	len = qstr->len;
	name = qstr->name;
	if (ms) {
		while (len && name[len-1] == '.')
			len--;
	}

189
	hash = init_name_hash(dentry);
L
Linus Torvalds 已提交
190 191
	while (len--) {
		c = tolower(*name++);
Y
young dave 已提交
192
		hash = partial_name_hash(c, hash);
L
Linus Torvalds 已提交
193 194 195 196 197 198 199
	}
	qstr->hash = end_name_hash(hash);

	return 0;
}

/*
N
Nick Piggin 已提交
200
 * Compare of two isofs names.
L
Linus Torvalds 已提交
201
 */
N
Nick Piggin 已提交
202 203 204
static int isofs_dentry_cmp_common(
		unsigned int len, const char *str,
		const struct qstr *name, int ms, int ci)
L
Linus Torvalds 已提交
205 206 207 208
{
	int alen, blen;

	/* A filename cannot end in '.' or we treat it like it has none */
N
Nick Piggin 已提交
209 210
	alen = name->len;
	blen = len;
L
Linus Torvalds 已提交
211
	if (ms) {
N
Nick Piggin 已提交
212
		while (alen && name->name[alen-1] == '.')
L
Linus Torvalds 已提交
213
			alen--;
N
Nick Piggin 已提交
214
		while (blen && str[blen-1] == '.')
L
Linus Torvalds 已提交
215 216 217
			blen--;
	}
	if (alen == blen) {
N
Nick Piggin 已提交
218
		if (ci) {
219
			if (strncasecmp(name->name, str, alen) == 0)
N
Nick Piggin 已提交
220 221 222 223 224
				return 0;
		} else {
			if (strncmp(name->name, str, alen) == 0)
				return 0;
		}
L
Linus Torvalds 已提交
225 226 227 228 229
	}
	return 1;
}

static int
230
isofs_hashi(const struct dentry *dentry, struct qstr *qstr)
L
Linus Torvalds 已提交
231
{
232
	return isofs_hashi_common(dentry, qstr, 0);
L
Linus Torvalds 已提交
233 234 235
}

static int
236
isofs_dentry_cmpi(const struct dentry *dentry,
N
Nick Piggin 已提交
237
		unsigned int len, const char *str, const struct qstr *name)
L
Linus Torvalds 已提交
238
{
N
Nick Piggin 已提交
239
	return isofs_dentry_cmp_common(len, str, name, 0, 1);
L
Linus Torvalds 已提交
240 241 242
}

#ifdef CONFIG_JOLIET
243 244 245 246
/*
 * Compute the hash for the isofs name corresponding to the dentry.
 */
static int
247
isofs_hash_common(const struct dentry *dentry, struct qstr *qstr, int ms)
248 249 250 251 252 253 254 255 256 257 258
{
	const char *name;
	int len;

	len = qstr->len;
	name = qstr->name;
	if (ms) {
		while (len && name[len-1] == '.')
			len--;
	}

259
	qstr->hash = full_name_hash(dentry, name, len);
260 261 262 263

	return 0;
}

L
Linus Torvalds 已提交
264
static int
265
isofs_hash_ms(const struct dentry *dentry, struct qstr *qstr)
L
Linus Torvalds 已提交
266
{
267
	return isofs_hash_common(dentry, qstr, 1);
L
Linus Torvalds 已提交
268 269 270
}

static int
271
isofs_hashi_ms(const struct dentry *dentry, struct qstr *qstr)
L
Linus Torvalds 已提交
272
{
273
	return isofs_hashi_common(dentry, qstr, 1);
L
Linus Torvalds 已提交
274 275 276
}

static int
277
isofs_dentry_cmp_ms(const struct dentry *dentry,
N
Nick Piggin 已提交
278
		unsigned int len, const char *str, const struct qstr *name)
L
Linus Torvalds 已提交
279
{
N
Nick Piggin 已提交
280
	return isofs_dentry_cmp_common(len, str, name, 1, 0);
L
Linus Torvalds 已提交
281 282 283
}

static int
284
isofs_dentry_cmpi_ms(const struct dentry *dentry,
N
Nick Piggin 已提交
285
		unsigned int len, const char *str, const struct qstr *name)
L
Linus Torvalds 已提交
286
{
N
Nick Piggin 已提交
287
	return isofs_dentry_cmp_common(len, str, name, 1, 1);
L
Linus Torvalds 已提交
288 289 290 291 292 293 294
}
#endif

enum {
	Opt_block, Opt_check_r, Opt_check_s, Opt_cruft, Opt_gid, Opt_ignore,
	Opt_iocharset, Opt_map_a, Opt_map_n, Opt_map_o, Opt_mode, Opt_nojoliet,
	Opt_norock, Opt_sb, Opt_session, Opt_uid, Opt_unhide, Opt_utf8, Opt_err,
295
	Opt_nocompress, Opt_hide, Opt_showassoc, Opt_dmode, Opt_overriderockperm,
L
Linus Torvalds 已提交
296 297
};

298
static const match_table_t tokens = {
L
Linus Torvalds 已提交
299 300 301
	{Opt_norock, "norock"},
	{Opt_nojoliet, "nojoliet"},
	{Opt_unhide, "unhide"},
302 303
	{Opt_hide, "hide"},
	{Opt_showassoc, "showassoc"},
L
Linus Torvalds 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	{Opt_cruft, "cruft"},
	{Opt_utf8, "utf8"},
	{Opt_iocharset, "iocharset=%s"},
	{Opt_map_a, "map=acorn"},
	{Opt_map_a, "map=a"},
	{Opt_map_n, "map=normal"},
	{Opt_map_n, "map=n"},
	{Opt_map_o, "map=off"},
	{Opt_map_o, "map=o"},
	{Opt_session, "session=%u"},
	{Opt_sb, "sbsector=%u"},
	{Opt_check_r, "check=relaxed"},
	{Opt_check_r, "check=r"},
	{Opt_check_s, "check=strict"},
	{Opt_check_s, "check=s"},
	{Opt_uid, "uid=%u"},
	{Opt_gid, "gid=%u"},
	{Opt_mode, "mode=%u"},
J
Jan Kara 已提交
322
	{Opt_dmode, "dmode=%u"},
323
	{Opt_overriderockperm, "overriderockperm"},
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336
	{Opt_block, "block=%u"},
	{Opt_ignore, "conv=binary"},
	{Opt_ignore, "conv=b"},
	{Opt_ignore, "conv=text"},
	{Opt_ignore, "conv=t"},
	{Opt_ignore, "conv=mtext"},
	{Opt_ignore, "conv=m"},
	{Opt_ignore, "conv=auto"},
	{Opt_ignore, "conv=a"},
	{Opt_nocompress, "nocompress"},
	{Opt_err, NULL}
};

337
static int parse_options(char *options, struct iso9660_options *popt)
L
Linus Torvalds 已提交
338 339 340
{
	char *p;
	int option;
341
	unsigned int uv;
L
Linus Torvalds 已提交
342 343

	popt->map = 'n';
344 345 346 347 348
	popt->rock = 1;
	popt->joliet = 1;
	popt->cruft = 0;
	popt->hide = 0;
	popt->showassoc = 0;
L
Linus Torvalds 已提交
349 350 351
	popt->check = 'u';		/* unset */
	popt->nocompress = 0;
	popt->blocksize = 1024;
352
	popt->fmode = popt->dmode = ISOFS_INVALID_MODE;
353 354
	popt->uid_set = 0;
	popt->gid_set = 0;
355 356
	popt->gid = GLOBAL_ROOT_GID;
	popt->uid = GLOBAL_ROOT_UID;
L
Linus Torvalds 已提交
357
	popt->iocharset = NULL;
358
	popt->overriderockperm = 0;
L
Linus Torvalds 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
	popt->session=-1;
	popt->sbsector=-1;
	if (!options)
		return 1;

	while ((p = strsep(&options, ",")) != NULL) {
		int token;
		substring_t args[MAX_OPT_ARGS];
		unsigned n;

		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
		case Opt_norock:
375
			popt->rock = 0;
L
Linus Torvalds 已提交
376 377
			break;
		case Opt_nojoliet:
378
			popt->joliet = 0;
L
Linus Torvalds 已提交
379
			break;
380
		case Opt_hide:
381
			popt->hide = 1;
382
			break;
L
Linus Torvalds 已提交
383
		case Opt_unhide:
384
		case Opt_showassoc:
385
			popt->showassoc = 1;
L
Linus Torvalds 已提交
386 387
			break;
		case Opt_cruft:
388
			popt->cruft = 1;
L
Linus Torvalds 已提交
389
			break;
390
#ifdef CONFIG_JOLIET
L
Linus Torvalds 已提交
391
		case Opt_utf8:
392 393 394 395
			kfree(popt->iocharset);
			popt->iocharset = kstrdup("utf8", GFP_KERNEL);
			if (!popt->iocharset)
				return 0;
L
Linus Torvalds 已提交
396 397
			break;
		case Opt_iocharset:
398
			kfree(popt->iocharset);
L
Linus Torvalds 已提交
399
			popt->iocharset = match_strdup(&args[0]);
400 401
			if (!popt->iocharset)
				return 0;
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
			break;
#endif
		case Opt_map_a:
			popt->map = 'a';
			break;
		case Opt_map_o:
			popt->map = 'o';
			break;
		case Opt_map_n:
			popt->map = 'n';
			break;
		case Opt_session:
			if (match_int(&args[0], &option))
				return 0;
			n = option;
417 418 419 420 421
			/*
			 * Track numbers are supposed to be in range 1-99, the
			 * mount option starts indexing at 0.
			 */
			if (n >= 99)
L
Linus Torvalds 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
				return 0;
			popt->session = n + 1;
			break;
		case Opt_sb:
			if (match_int(&args[0], &option))
				return 0;
			popt->sbsector = option;
			break;
		case Opt_check_r:
			popt->check = 'r';
			break;
		case Opt_check_s:
			popt->check = 's';
			break;
		case Opt_ignore:
			break;
		case Opt_uid:
439
			if (match_uint(&args[0], &uv))
L
Linus Torvalds 已提交
440
				return 0;
441
			popt->uid = make_kuid(current_user_ns(), uv);
442 443
			if (!uid_valid(popt->uid))
				return 0;
444
			popt->uid_set = 1;
L
Linus Torvalds 已提交
445 446
			break;
		case Opt_gid:
447
			if (match_uint(&args[0], &uv))
L
Linus Torvalds 已提交
448
				return 0;
449
			popt->gid = make_kgid(current_user_ns(), uv);
450 451
			if (!gid_valid(popt->gid))
				return 0;
452
			popt->gid_set = 1;
L
Linus Torvalds 已提交
453 454 455 456
			break;
		case Opt_mode:
			if (match_int(&args[0], &option))
				return 0;
J
Jan Kara 已提交
457 458 459 460 461 462
			popt->fmode = option;
			break;
		case Opt_dmode:
			if (match_int(&args[0], &option))
				return 0;
			popt->dmode = option;
L
Linus Torvalds 已提交
463
			break;
464 465 466
		case Opt_overriderockperm:
			popt->overriderockperm = 1;
			break;
L
Linus Torvalds 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
		case Opt_block:
			if (match_int(&args[0], &option))
				return 0;
			n = option;
			if (n != 512 && n != 1024 && n != 2048)
				return 0;
			popt->blocksize = n;
			break;
		case Opt_nocompress:
			popt->nocompress = 1;
			break;
		default:
			return 0;
		}
	}
	return 1;
}

D
David Howells 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
/*
 * Display the mount options in /proc/mounts.
 */
static int isofs_show_options(struct seq_file *m, struct dentry *root)
{
	struct isofs_sb_info *sbi = ISOFS_SB(root->d_sb);

	if (!sbi->s_rock)		seq_puts(m, ",norock");
	else if (!sbi->s_joliet_level)	seq_puts(m, ",nojoliet");
	if (sbi->s_cruft)		seq_puts(m, ",cruft");
	if (sbi->s_hide)		seq_puts(m, ",hide");
	if (sbi->s_nocompress)		seq_puts(m, ",nocompress");
	if (sbi->s_overriderockperm)	seq_puts(m, ",overriderockperm");
	if (sbi->s_showassoc)		seq_puts(m, ",showassoc");

	if (sbi->s_check)		seq_printf(m, ",check=%c", sbi->s_check);
	if (sbi->s_mapping)		seq_printf(m, ",map=%c", sbi->s_mapping);
D
David Howells 已提交
502
	if (sbi->s_session != 255)	seq_printf(m, ",session=%u", sbi->s_session - 1);
D
David Howells 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	if (sbi->s_sbsector != -1)	seq_printf(m, ",sbsector=%u", sbi->s_sbsector);

	if (root->d_sb->s_blocksize != 1024)
		seq_printf(m, ",blocksize=%lu", root->d_sb->s_blocksize);

	if (sbi->s_uid_set)
		seq_printf(m, ",uid=%u",
			   from_kuid_munged(&init_user_ns, sbi->s_uid));
	if (sbi->s_gid_set)
		seq_printf(m, ",gid=%u",
			   from_kgid_munged(&init_user_ns, sbi->s_gid));

	if (sbi->s_dmode != ISOFS_INVALID_MODE)
		seq_printf(m, ",dmode=%o", sbi->s_dmode);
	if (sbi->s_fmode != ISOFS_INVALID_MODE)
		seq_printf(m, ",fmode=%o", sbi->s_fmode);

A
Arnd Bergmann 已提交
520
#ifdef CONFIG_JOLIET
521
	if (sbi->s_nls_iocharset)
D
David Howells 已提交
522
		seq_printf(m, ",iocharset=%s", sbi->s_nls_iocharset->charset);
523 524
	else
		seq_puts(m, ",iocharset=utf8");
A
Arnd Bergmann 已提交
525
#endif
D
David Howells 已提交
526 527 528
	return 0;
}

L
Linus Torvalds 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
/*
 * look if the driver can tell the multi session redirection value
 *
 * don't change this if you don't know what you do, please!
 * Multisession is legal only with XA disks.
 * A non-XA disk with more than one volume descriptor may do it right, but
 * usually is written in a nowhere standardized "multi-partition" manner.
 * Multisession uses absolute addressing (solely the first frame of the whole
 * track is #0), multi-partition uses relative addressing (each first frame of
 * each track is #0), and a track is not a session.
 *
 * A broken CDwriter software or drive firmware does not set new standards,
 * at least not if conflicting with the existing ones.
 *
 * emoenke@gwdg.de
 */
#define WE_OBEY_THE_WRITTEN_STANDARDS 1

547
static unsigned int isofs_get_last_session(struct super_block *sb, s32 session)
L
Linus Torvalds 已提交
548
{
549 550
	struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
	unsigned int vol_desc_start = 0;
L
Linus Torvalds 已提交
551

552
	if (session > 0) {
553 554 555 556 557 558 559 560
		struct cdrom_tocentry te;

		if (!cdi)
			return 0;

		te.cdte_track = session;
		te.cdte_format = CDROM_LBA;
		if (cdrom_read_tocentry(cdi, &te) == 0) {
D
Dave Jones 已提交
561
			printk(KERN_DEBUG "ISOFS: Session %d start %d type %d\n",
562 563 564 565
				session, te.cdte_addr.lba,
				te.cdte_ctrl & CDROM_DATA_TRACK);
			if ((te.cdte_ctrl & CDROM_DATA_TRACK) == 4)
				return te.cdte_addr.lba;
L
Linus Torvalds 已提交
566
		}
D
Dave Jones 已提交
567 568

		printk(KERN_ERR "ISOFS: Invalid session number or type of track\n");
L
Linus Torvalds 已提交
569
	}
570 571 572 573 574 575

	if (cdi) {
		struct cdrom_multisession ms_info;

		ms_info.addr_format = CDROM_LBA;
		if (cdrom_multisession(cdi, &ms_info) == 0) {
L
Linus Torvalds 已提交
576
#if WE_OBEY_THE_WRITTEN_STANDARDS
577 578
			/* necessary for a valid ms_info.addr */
			if (ms_info.xa_flag)
L
Linus Torvalds 已提交
579
#endif
580 581 582 583
				vol_desc_start = ms_info.addr.lba;
		}
	}

L
Linus Torvalds 已提交
584 585 586
	return vol_desc_start;
}

587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
/*
 * Check if root directory is empty (has less than 3 files).
 *
 * Used to detect broken CDs where ISO root directory is empty but Joliet root
 * directory is OK. If such CD has Rock Ridge extensions, they will be disabled
 * (and Joliet used instead) or else no files would be visible.
 */
static bool rootdir_empty(struct super_block *sb, unsigned long block)
{
	int offset = 0, files = 0, de_len;
	struct iso_directory_record *de;
	struct buffer_head *bh;

	bh = sb_bread(sb, block);
	if (!bh)
		return true;
	while (files < 3) {
		de = (struct iso_directory_record *) (bh->b_data + offset);
		de_len = *(unsigned char *) de;
		if (de_len == 0)
			break;
		files++;
		offset += de_len;
	}
	brelse(bh);
	return files < 3;
}

L
Linus Torvalds 已提交
615 616 617 618 619
/*
 * Initialize the superblock and read the root inode.
 */
static int isofs_fill_super(struct super_block *s, void *data, int silent)
{
D
Dave Jones 已提交
620 621 622
	struct buffer_head *bh = NULL, *pri_bh = NULL;
	struct hs_primary_descriptor *h_pri = NULL;
	struct iso_primary_descriptor *pri = NULL;
L
Linus Torvalds 已提交
623
	struct iso_supplementary_descriptor *sec = NULL;
D
Dave Jones 已提交
624 625 626 627 628 629 630 631
	struct iso_directory_record *rootp;
	struct inode *inode;
	struct iso9660_options opt;
	struct isofs_sb_info *sbi;
	unsigned long first_data_zone;
	int joliet_level = 0;
	int iso_blknum, block;
	int orig_zonesize;
632
	int table, error = -EINVAL;
D
Dave Jones 已提交
633
	unsigned int vol_desc_start;
L
Linus Torvalds 已提交
634

635
	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
A
Arnd Bergmann 已提交
636
	if (!sbi)
L
Linus Torvalds 已提交
637 638 639
		return -ENOMEM;
	s->s_fs_info = sbi;

640
	if (!parse_options((char *)data, &opt))
L
Linus Torvalds 已提交
641 642 643 644 645 646 647 648 649 650 651
		goto out_freesbi;

	/*
	 * First of all, get the hardware blocksize for this device.
	 * If we don't know what it is, or the hardware blocksize is
	 * larger than the blocksize the user specified, then use
	 * that value.
	 */
	/*
	 * What if bugger tells us to go beyond page size?
	 */
652 653 654 655 656 657
	if (bdev_logical_block_size(s->s_bdev) > 2048) {
		printk(KERN_WARNING
		       "ISOFS: unsupported/invalid hardware sector size %d\n",
			bdev_logical_block_size(s->s_bdev));
		goto out_freesbi;
	}
L
Linus Torvalds 已提交
658 659 660
	opt.blocksize = sb_min_blocksize(s, opt.blocksize);

	sbi->s_high_sierra = 0; /* default is iso9660 */
D
David Howells 已提交
661 662
	sbi->s_session = opt.session;
	sbi->s_sbsector = opt.sbsector;
L
Linus Torvalds 已提交
663 664 665 666

	vol_desc_start = (opt.sbsector != -1) ?
		opt.sbsector : isofs_get_last_session(s,opt.session);

D
Dave Jones 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	for (iso_blknum = vol_desc_start+16;
		iso_blknum < vol_desc_start+100; iso_blknum++) {
		struct hs_volume_descriptor *hdp;
		struct iso_volume_descriptor  *vdp;

		block = iso_blknum << (ISOFS_BLOCK_BITS - s->s_blocksize_bits);
		if (!(bh = sb_bread(s, block)))
			goto out_no_read;

		vdp = (struct iso_volume_descriptor *)bh->b_data;
		hdp = (struct hs_volume_descriptor *)bh->b_data;

		/*
		 * Due to the overlapping physical location of the descriptors,
		 * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
		 * proper identification in this case, we first check for ISO.
		 */
		if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
			if (isonum_711(vdp->type) == ISO_VD_END)
				break;
			if (isonum_711(vdp->type) == ISO_VD_PRIMARY) {
688
				if (!pri) {
D
Dave Jones 已提交
689 690 691 692 693 694
					pri = (struct iso_primary_descriptor *)vdp;
					/* Save the buffer in case we need it ... */
					pri_bh = bh;
					bh = NULL;
				}
			}
L
Linus Torvalds 已提交
695
#ifdef CONFIG_JOLIET
D
Dave Jones 已提交
696 697 698
			else if (isonum_711(vdp->type) == ISO_VD_SUPPLEMENTARY) {
				sec = (struct iso_supplementary_descriptor *)vdp;
				if (sec->escape[0] == 0x25 && sec->escape[1] == 0x2f) {
699
					if (opt.joliet) {
D
Dave Jones 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
						if (sec->escape[2] == 0x40)
							joliet_level = 1;
						else if (sec->escape[2] == 0x43)
							joliet_level = 2;
						else if (sec->escape[2] == 0x45)
							joliet_level = 3;

						printk(KERN_DEBUG "ISO 9660 Extensions: "
							"Microsoft Joliet Level %d\n",
							joliet_level);
					}
					goto root_found;
				} else {
				/* Unknown supplementary volume descriptor */
				sec = NULL;
				}
L
Linus Torvalds 已提交
716 717
			}
#endif
D
Dave Jones 已提交
718 719 720 721 722 723
		} else {
			if (strncmp (hdp->id, HS_STANDARD_ID, sizeof hdp->id) == 0) {
				if (isonum_711(hdp->type) != ISO_VD_PRIMARY)
					goto out_freebh;

				sbi->s_high_sierra = 1;
724
				opt.rock = 0;
D
Dave Jones 已提交
725 726 727
				h_pri = (struct hs_primary_descriptor *)vdp;
				goto root_found;
			}
L
Linus Torvalds 已提交
728 729
		}

D
Dave Jones 已提交
730
		/* Just skip any volume descriptors we don't recognize */
L
Linus Torvalds 已提交
731

D
Dave Jones 已提交
732 733
		brelse(bh);
		bh = NULL;
L
Linus Torvalds 已提交
734 735 736 737 738 739 740 741 742 743 744 745
	}
	/*
	 * If we fall through, either no volume descriptor was found,
	 * or else we passed a primary descriptor looking for others.
	 */
	if (!pri)
		goto out_unknown_format;
	brelse(bh);
	bh = pri_bh;
	pri_bh = NULL;

root_found:
746
	/* We don't support read-write mounts */
747
	if (!sb_rdonly(s)) {
748 749 750
		error = -EACCES;
		goto out_freebh;
	}
L
Linus Torvalds 已提交
751

752
	if (joliet_level && (!pri || !opt.rock)) {
D
Dave Jones 已提交
753 754 755 756
		/* This is the case of Joliet with the norock mount flag.
		 * A disc with both Joliet and Rock Ridge is handled later
		 */
		pri = (struct iso_primary_descriptor *) sec;
L
Linus Torvalds 已提交
757 758 759
	}

	if(sbi->s_high_sierra){
D
Dave Jones 已提交
760 761 762 763
		rootp = (struct iso_directory_record *) h_pri->root_directory_record;
		sbi->s_nzones = isonum_733(h_pri->volume_space_size);
		sbi->s_log_zone_size = isonum_723(h_pri->logical_block_size);
		sbi->s_max_size = isonum_733(h_pri->volume_space_size);
L
Linus Torvalds 已提交
764
	} else {
D
Dave Jones 已提交
765 766 767 768 769 770
		if (!pri)
			goto out_freebh;
		rootp = (struct iso_directory_record *) pri->root_directory_record;
		sbi->s_nzones = isonum_733(pri->volume_space_size);
		sbi->s_log_zone_size = isonum_723(pri->logical_block_size);
		sbi->s_max_size = isonum_733(pri->volume_space_size);
L
Linus Torvalds 已提交
771 772 773 774 775 776 777 778 779 780 781 782
	}

	sbi->s_ninodes = 0; /* No way to figure this out easily */

	orig_zonesize = sbi->s_log_zone_size;
	/*
	 * If the zone size is smaller than the hardware sector size,
	 * this is a fatal error.  This would occur if the disc drive
	 * had sectors that were 2048 bytes, but the filesystem had
	 * blocks that were 512 bytes (which should only very rarely
	 * happen.)
	 */
D
Dave Jones 已提交
783
	if (orig_zonesize < opt.blocksize)
L
Linus Torvalds 已提交
784 785 786
		goto out_bad_size;

	/* RDE: convert log zone size to bit shift */
D
Dave Jones 已提交
787 788 789 790
	switch (sbi->s_log_zone_size) {
	case  512: sbi->s_log_zone_size =  9; break;
	case 1024: sbi->s_log_zone_size = 10; break;
	case 2048: sbi->s_log_zone_size = 11; break;
L
Linus Torvalds 已提交
791

D
Dave Jones 已提交
792
	default:
L
Linus Torvalds 已提交
793
		goto out_bad_zone_size;
D
Dave Jones 已提交
794
	}
L
Linus Torvalds 已提交
795 796

	s->s_magic = ISOFS_SUPER_MAGIC;
797 798 799 800 801 802

	/*
	 * With multi-extent files, file size is only limited by the maximum
	 * size of a file system, which is 8 TB.
	 */
	s->s_maxbytes = 0x80000000000LL;
L
Linus Torvalds 已提交
803

804 805 806 807
	/* ECMA-119 timestamp from 1900/1/1 with tz offset */
	s->s_time_min = mktime64(1900, 1, 1, 0, 0, 0) - MAX_TZ_OFFSET;
	s->s_time_max = mktime64(U8_MAX+1900, 12, 31, 23, 59, 59) + MAX_TZ_OFFSET;

L
Linus Torvalds 已提交
808 809
	/* Set this for reference. Its not currently used except on write
	   which we don't have .. */
D
Dave Jones 已提交
810 811 812

	first_data_zone = isonum_733(rootp->extent) +
			  isonum_711(rootp->ext_attr_length);
L
Linus Torvalds 已提交
813 814
	sbi->s_firstdatazone = first_data_zone;
#ifndef BEQUIET
D
Dave Jones 已提交
815 816 817
	printk(KERN_DEBUG "ISOFS: Max size:%ld   Log zone size:%ld\n",
		sbi->s_max_size, 1UL << sbi->s_log_zone_size);
	printk(KERN_DEBUG "ISOFS: First datazone:%ld\n", sbi->s_firstdatazone);
L
Linus Torvalds 已提交
818
	if(sbi->s_high_sierra)
D
Dave Jones 已提交
819
		printk(KERN_DEBUG "ISOFS: Disc in High Sierra format.\n");
L
Linus Torvalds 已提交
820 821 822 823 824 825 826 827 828 829 830 831 832 833
#endif

	/*
	 * If the Joliet level is set, we _may_ decide to use the
	 * secondary descriptor, but can't be sure until after we
	 * read the root inode. But before reading the root inode
	 * we may need to change the device blocksize, and would
	 * rather release the old buffer first. So, we cache the
	 * first_data_zone value from the secondary descriptor.
	 */
	if (joliet_level) {
		pri = (struct iso_primary_descriptor *) sec;
		rootp = (struct iso_directory_record *)
			pri->root_directory_record;
D
Dave Jones 已提交
834 835
		first_data_zone = isonum_733(rootp->extent) +
				isonum_711(rootp->ext_attr_length);
L
Linus Torvalds 已提交
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
	}

	/*
	 * We're all done using the volume descriptor, and may need
	 * to change the device blocksize, so release the buffer now.
	 */
	brelse(pri_bh);
	brelse(bh);

	/*
	 * Force the blocksize to 512 for 512 byte sectors.  The file
	 * read primitives really get it wrong in a bad way if we don't
	 * do this.
	 *
	 * Note - we should never be setting the blocksize to something
	 * less than the hardware sector size for the device.  If we
	 * do, we would end up having to read larger buffers and split
	 * out portions to satisfy requests.
	 *
	 * Note2- the idea here is that we want to deal with the optimal
	 * zonesize in the filesystem.  If we have it set to something less,
	 * then we have horrible problems with trying to piece together
	 * bits of adjacent blocks in order to properly read directory
	 * entries.  By forcing the blocksize in this way, we ensure
	 * that we will never be required to do this.
	 */
	sb_set_blocksize(s, orig_zonesize);

	sbi->s_nls_iocharset = NULL;

#ifdef CONFIG_JOLIET
867
	if (joliet_level) {
D
Dave Jones 已提交
868
		char *p = opt.iocharset ? opt.iocharset : CONFIG_NLS_DEFAULT;
869 870 871 872
		if (strcmp(p, "utf8") != 0) {
			sbi->s_nls_iocharset = opt.iocharset ?
				load_nls(opt.iocharset) : load_nls_default();
			if (!sbi->s_nls_iocharset)
L
Linus Torvalds 已提交
873 874 875 876 877 878 879
				goto out_freesbi;
		}
	}
#endif
	s->s_op = &isofs_sops;
	s->s_export_op = &isofs_export_ops;
	sbi->s_mapping = opt.map;
880
	sbi->s_rock = (opt.rock ? 2 : 0);
L
Linus Torvalds 已提交
881 882
	sbi->s_rock_offset = -1; /* initial offset, will guess until SP is found*/
	sbi->s_cruft = opt.cruft;
883 884
	sbi->s_hide = opt.hide;
	sbi->s_showassoc = opt.showassoc;
L
Linus Torvalds 已提交
885 886
	sbi->s_uid = opt.uid;
	sbi->s_gid = opt.gid;
887 888
	sbi->s_uid_set = opt.uid_set;
	sbi->s_gid_set = opt.gid_set;
L
Linus Torvalds 已提交
889
	sbi->s_nocompress = opt.nocompress;
890
	sbi->s_overriderockperm = opt.overriderockperm;
L
Linus Torvalds 已提交
891 892 893 894 895
	/*
	 * It would be incredibly stupid to allow people to mark every file
	 * on the disk as suid, so we merely allow them to set the default
	 * permissions.
	 */
896 897 898 899 900 901 902 903
	if (opt.fmode != ISOFS_INVALID_MODE)
		sbi->s_fmode = opt.fmode & 0777;
	else
		sbi->s_fmode = ISOFS_INVALID_MODE;
	if (opt.dmode != ISOFS_INVALID_MODE)
		sbi->s_dmode = opt.dmode & 0777;
	else
		sbi->s_dmode = ISOFS_INVALID_MODE;
L
Linus Torvalds 已提交
904 905 906 907 908 909 910

	/*
	 * Read the root inode, which _may_ result in changing
	 * the s_rock flag. Once we have the final s_rock value,
	 * we then decide whether to use the Joliet descriptor.
	 */
	inode = isofs_iget(s, sbi->s_firstdatazone, 0);
911 912
	if (IS_ERR(inode))
		goto out_no_root;
L
Linus Torvalds 已提交
913

914 915 916 917 918 919 920 921 922 923 924 925
	/*
	 * Fix for broken CDs with Rock Ridge and empty ISO root directory but
	 * correct Joliet root directory.
	 */
	if (sbi->s_rock == 1 && joliet_level &&
				rootdir_empty(s, sbi->s_firstdatazone)) {
		printk(KERN_NOTICE
			"ISOFS: primary root directory is empty. "
			"Disabling Rock Ridge and switching to Joliet.");
		sbi->s_rock = 0;
	}

L
Linus Torvalds 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939
	/*
	 * If this disk has both Rock Ridge and Joliet on it, then we
	 * want to use Rock Ridge by default.  This can be overridden
	 * by using the norock mount option.  There is still one other
	 * possibility that is not taken into account: a Rock Ridge
	 * CD with Unicode names.  Until someone sees such a beast, it
	 * will not be supported.
	 */
	if (sbi->s_rock == 1) {
		joliet_level = 0;
	} else if (joliet_level) {
		sbi->s_rock = 0;
		if (sbi->s_firstdatazone != first_data_zone) {
			sbi->s_firstdatazone = first_data_zone;
D
Dave Jones 已提交
940
			printk(KERN_DEBUG
L
Linus Torvalds 已提交
941 942 943
				"ISOFS: changing to secondary root\n");
			iput(inode);
			inode = isofs_iget(s, sbi->s_firstdatazone, 0);
944 945
			if (IS_ERR(inode))
				goto out_no_root;
L
Linus Torvalds 已提交
946 947 948 949 950
		}
	}

	if (opt.check == 'u') {
		/* Only Joliet is case insensitive by default */
D
Dave Jones 已提交
951 952 953 954
		if (joliet_level)
			opt.check = 'r';
		else
			opt.check = 's';
L
Linus Torvalds 已提交
955 956 957
	}
	sbi->s_joliet_level = joliet_level;

958 959 960 961 962 963 964 965
	/* Make sure the root inode is a directory */
	if (!S_ISDIR(inode->i_mode)) {
		printk(KERN_WARNING
			"isofs_fill_super: root inode is not a directory. "
			"Corrupted media?\n");
		goto out_iput;
	}

L
Linus Torvalds 已提交
966
	table = 0;
D
Dave Jones 已提交
967 968 969 970
	if (joliet_level)
		table += 2;
	if (opt.check == 'r')
		table++;
D
David Howells 已提交
971
	sbi->s_check = opt.check;
A
Al Viro 已提交
972

973 974
	if (table)
		s->s_d_op = &isofs_dentry_ops[table - 1];
A
Al Viro 已提交
975 976

	/* get the root dentry */
977
	s->s_root = d_make_root(inode);
A
Al Viro 已提交
978 979 980 981
	if (!(s->s_root)) {
		error = -ENOMEM;
		goto out_no_inode;
	}
L
Linus Torvalds 已提交
982

J
Jesper Juhl 已提交
983
	kfree(opt.iocharset);
L
Linus Torvalds 已提交
984 985 986 987 988 989 990 991

	return 0;

	/*
	 * Display error messages and free resources.
	 */
out_iput:
	iput(inode);
992 993 994 995 996 997
	goto out_no_inode;
out_no_root:
	error = PTR_ERR(inode);
	if (error != -ENOMEM)
		printk(KERN_WARNING "%s: get root inode failed\n", __func__);
out_no_inode:
L
Linus Torvalds 已提交
998
#ifdef CONFIG_JOLIET
999
	unload_nls(sbi->s_nls_iocharset);
L
Linus Torvalds 已提交
1000 1001 1002
#endif
	goto out_freesbi;
out_no_read:
D
Dave Jones 已提交
1003 1004
	printk(KERN_WARNING "%s: bread failed, dev=%s, iso_blknum=%d, block=%d\n",
		__func__, s->s_id, iso_blknum, block);
1005
	goto out_freebh;
L
Linus Torvalds 已提交
1006
out_bad_zone_size:
D
Dave Jones 已提交
1007
	printk(KERN_WARNING "ISOFS: Bad logical zone size %ld\n",
L
Linus Torvalds 已提交
1008 1009 1010
		sbi->s_log_zone_size);
	goto out_freebh;
out_bad_size:
D
Dave Jones 已提交
1011
	printk(KERN_WARNING "ISOFS: Logical zone size(%d) < hardware blocksize(%u)\n",
L
Linus Torvalds 已提交
1012 1013 1014 1015
		orig_zonesize, opt.blocksize);
	goto out_freebh;
out_unknown_format:
	if (!silent)
D
Dave Jones 已提交
1016
		printk(KERN_WARNING "ISOFS: Unable to identify CD-ROM format.\n");
L
Linus Torvalds 已提交
1017 1018 1019

out_freebh:
	brelse(bh);
1020
	brelse(pri_bh);
L
Linus Torvalds 已提交
1021
out_freesbi:
J
Jesper Juhl 已提交
1022
	kfree(opt.iocharset);
L
Linus Torvalds 已提交
1023 1024
	kfree(sbi);
	s->s_fs_info = NULL;
1025
	return error;
L
Linus Torvalds 已提交
1026 1027
}

1028
static int isofs_statfs (struct dentry *dentry, struct kstatfs *buf)
L
Linus Torvalds 已提交
1029
{
1030
	struct super_block *sb = dentry->d_sb;
C
Coly Li 已提交
1031
	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1032

L
Linus Torvalds 已提交
1033 1034 1035
	buf->f_type = ISOFS_SUPER_MAGIC;
	buf->f_bsize = sb->s_blocksize;
	buf->f_blocks = (ISOFS_SB(sb)->s_nzones
D
Dave Jones 已提交
1036
		<< (ISOFS_SB(sb)->s_log_zone_size - sb->s_blocksize_bits));
L
Linus Torvalds 已提交
1037 1038 1039 1040
	buf->f_bfree = 0;
	buf->f_bavail = 0;
	buf->f_files = ISOFS_SB(sb)->s_ninodes;
	buf->f_ffree = 0;
1041
	buf->f_fsid = u64_to_fsid(id);
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047 1048
	buf->f_namelen = NAME_MAX;
	return 0;
}

/*
 * Get a set of blocks; filling in buffer_heads if already allocated
 * or getblk() if they are not.  Returns the number of blocks inserted
1049
 * (-ve == error.)
L
Linus Torvalds 已提交
1050
 */
1051
int isofs_get_blocks(struct inode *inode, sector_t iblock,
L
Linus Torvalds 已提交
1052 1053
		     struct buffer_head **bh, unsigned long nblocks)
{
1054
	unsigned long b_off = iblock;
L
Linus Torvalds 已提交
1055 1056 1057
	unsigned offset, sect_size;
	unsigned int firstext;
	unsigned long nextblk, nextoff;
1058
	int section, rv, error;
L
Linus Torvalds 已提交
1059 1060
	struct iso_inode_info *ei = ISOFS_I(inode);

1061
	error = -EIO;
L
Linus Torvalds 已提交
1062
	rv = 0;
1063
	if (iblock != b_off) {
D
Dave Jones 已提交
1064
		printk(KERN_DEBUG "%s: block number too large\n", __func__);
L
Linus Torvalds 已提交
1065 1066 1067
		goto abort;
	}

D
Dave Jones 已提交
1068 1069 1070

	offset = 0;
	firstext = ei->i_first_extent;
L
Linus Torvalds 已提交
1071
	sect_size = ei->i_section_size >> ISOFS_BUFFER_BITS(inode);
D
Dave Jones 已提交
1072 1073 1074
	nextblk = ei->i_next_section_block;
	nextoff = ei->i_next_section_offset;
	section = 0;
L
Linus Torvalds 已提交
1075

D
Dave Jones 已提交
1076
	while (nblocks) {
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081 1082 1083
		/* If we are *way* beyond the end of the file, print a message.
		 * Access beyond the end of the file up to the next page boundary
		 * is normal, however because of the way the page cache works.
		 * In this case, we just return 0 so that we can properly fill
		 * the page with useless information without generating any
		 * I/O errors.
		 */
1084
		if (b_off > ((inode->i_size + PAGE_SIZE - 1) >> ISOFS_BUFFER_BITS(inode))) {
1085 1086 1087
			printk(KERN_DEBUG "%s: block >= EOF (%lu, %llu)\n",
				__func__, b_off,
				(unsigned long long)inode->i_size);
L
Linus Torvalds 已提交
1088 1089
			goto abort;
		}
D
Dave Jones 已提交
1090

1091 1092 1093 1094 1095 1096 1097 1098 1099
		/* On the last section, nextblk == 0, section size is likely to
		 * exceed sect_size by a partial block, and access beyond the
		 * end of the file will reach beyond the section size, too.
		 */
		while (nextblk && (b_off >= (offset + sect_size))) {
			struct inode *ninode;

			offset += sect_size;
			ninode = isofs_iget(inode->i_sb, nextblk, nextoff);
1100 1101
			if (IS_ERR(ninode)) {
				error = PTR_ERR(ninode);
1102
				goto abort;
1103
			}
1104 1105 1106 1107 1108 1109 1110
			firstext  = ISOFS_I(ninode)->i_first_extent;
			sect_size = ISOFS_I(ninode)->i_section_size >> ISOFS_BUFFER_BITS(ninode);
			nextblk   = ISOFS_I(ninode)->i_next_section_block;
			nextoff   = ISOFS_I(ninode)->i_next_section_offset;
			iput(ninode);

			if (++section > 100) {
D
Dave Jones 已提交
1111 1112
				printk(KERN_DEBUG "%s: More than 100 file sections ?!?"
					" aborting...\n", __func__);
1113
				printk(KERN_DEBUG "%s: block=%lu firstext=%u sect_size=%u "
D
Dave Jones 已提交
1114
					"nextblk=%lu nextoff=%lu\n", __func__,
1115
					b_off, firstext, (unsigned) sect_size,
D
Dave Jones 已提交
1116
					nextblk, nextoff);
1117
				goto abort;
L
Linus Torvalds 已提交
1118 1119
			}
		}
D
Dave Jones 已提交
1120 1121

		if (*bh) {
L
Linus Torvalds 已提交
1122 1123 1124
			map_bh(*bh, inode->i_sb, firstext + b_off - offset);
		} else {
			*bh = sb_getblk(inode->i_sb, firstext+b_off-offset);
D
Dave Jones 已提交
1125
			if (!*bh)
L
Linus Torvalds 已提交
1126 1127 1128 1129 1130 1131 1132 1133
				goto abort;
		}
		bh++;	/* Next buffer head */
		b_off++;	/* Next buffer offset */
		nblocks--;
		rv++;
	}

1134
	error = 0;
L
Linus Torvalds 已提交
1135
abort:
1136
	return rv != 0 ? rv : error;
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141 1142 1143 1144
}

/*
 * Used by the standard interfaces.
 */
static int isofs_get_block(struct inode *inode, sector_t iblock,
		    struct buffer_head *bh_result, int create)
{
1145 1146
	int ret;

1147
	if (create) {
D
Dave Jones 已提交
1148
		printk(KERN_DEBUG "%s: Kernel tries to allocate a block\n", __func__);
L
Linus Torvalds 已提交
1149 1150 1151
		return -EROFS;
	}

1152 1153
	ret = isofs_get_blocks(inode, iblock, &bh_result, 1);
	return ret < 0 ? ret : 0;
L
Linus Torvalds 已提交
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
}

static int isofs_bmap(struct inode *inode, sector_t block)
{
	struct buffer_head dummy;
	int error;

	dummy.b_state = 0;
	dummy.b_blocknr = -1000;
	error = isofs_get_block(inode, block, &dummy, 0);
	if (!error)
		return dummy.b_blocknr;
	return 0;
}

struct buffer_head *isofs_bread(struct inode *inode, sector_t block)
{
	sector_t blknr = isofs_bmap(inode, block);
	if (!blknr)
		return NULL;
	return sb_bread(inode->i_sb, blknr);
}

static int isofs_readpage(struct file *file, struct page *page)
{
N
Namjae Jeon 已提交
1179 1180 1181
	return mpage_readpage(page, isofs_get_block);
}

1182
static void isofs_readahead(struct readahead_control *rac)
N
Namjae Jeon 已提交
1183
{
1184
	mpage_readahead(rac, isofs_get_block);
L
Linus Torvalds 已提交
1185 1186 1187 1188 1189 1190 1191
}

static sector_t _isofs_bmap(struct address_space *mapping, sector_t block)
{
	return generic_block_bmap(mapping,block,isofs_get_block);
}

1192
static const struct address_space_operations isofs_aops = {
L
Linus Torvalds 已提交
1193
	.readpage = isofs_readpage,
1194
	.readahead = isofs_readahead,
L
Linus Torvalds 已提交
1195 1196 1197
	.bmap = _isofs_bmap
};

1198
static int isofs_read_level3_size(struct inode *inode)
L
Linus Torvalds 已提交
1199 1200 1201
{
	unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
	int high_sierra = ISOFS_SB(inode->i_sb)->s_high_sierra;
D
Dave Jones 已提交
1202
	struct buffer_head *bh = NULL;
L
Linus Torvalds 已提交
1203 1204 1205
	unsigned long block, offset, block_saved, offset_saved;
	int i = 0;
	int more_entries = 0;
D
Dave Jones 已提交
1206
	struct iso_directory_record *tmpde = NULL;
L
Linus Torvalds 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
	struct iso_inode_info *ei = ISOFS_I(inode);

	inode->i_size = 0;

	/* The first 16 blocks are reserved as the System Area.  Thus,
	 * no inodes can appear in block 0.  We use this to flag that
	 * this is the last section. */
	ei->i_next_section_block = 0;
	ei->i_next_section_offset = 0;

	block = ei->i_iget5_block;
	offset = ei->i_iget5_offset;

	do {
D
Dave Jones 已提交
1221
		struct iso_directory_record *de;
L
Linus Torvalds 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
		unsigned int de_len;

		if (!bh) {
			bh = sb_bread(inode->i_sb, block);
			if (!bh)
				goto out_noread;
		}
		de = (struct iso_directory_record *) (bh->b_data + offset);
		de_len = *(unsigned char *) de;

		if (de_len == 0) {
			brelse(bh);
			bh = NULL;
			++block;
			offset = 0;
			continue;
		}

		block_saved = block;
		offset_saved = offset;
		offset += de_len;

		/* Make sure we have a full directory entry */
		if (offset >= bufsize) {
			int slop = bufsize - offset + de_len;
			if (!tmpde) {
				tmpde = kmalloc(256, GFP_KERNEL);
				if (!tmpde)
					goto out_nomem;
			}
			memcpy(tmpde, de, slop);
			offset &= bufsize - 1;
			block++;
			brelse(bh);
			bh = NULL;
			if (offset) {
				bh = sb_bread(inode->i_sb, block);
				if (!bh)
					goto out_noread;
1261
				memcpy((void *)tmpde+slop, bh->b_data, offset);
L
Linus Torvalds 已提交
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
			}
			de = tmpde;
		}

		inode->i_size += isonum_733(de->size);
		if (i == 1) {
			ei->i_next_section_block = block_saved;
			ei->i_next_section_offset = offset_saved;
		}

		more_entries = de->flags[-high_sierra] & 0x80;

		i++;
1275
		if (i > 100)
L
Linus Torvalds 已提交
1276
			goto out_toomany;
1277
	} while (more_entries);
L
Linus Torvalds 已提交
1278
out:
1279
	kfree(tmpde);
L
Linus Torvalds 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	if (bh)
		brelse(bh);
	return 0;

out_nomem:
	if (bh)
		brelse(bh);
	return -ENOMEM;

out_noread:
	printk(KERN_INFO "ISOFS: unable to read i-node block %lu\n", block);
J
Jesper Juhl 已提交
1291
	kfree(tmpde);
L
Linus Torvalds 已提交
1292 1293 1294
	return -EIO;

out_toomany:
D
Dave Jones 已提交
1295 1296 1297
	printk(KERN_INFO "%s: More than 100 file sections ?!?, aborting...\n"
		"isofs_read_level3_size: inode=%lu\n",
		__func__, inode->i_ino);
L
Linus Torvalds 已提交
1298 1299 1300
	goto out;
}

1301
static int isofs_read_inode(struct inode *inode, int relocated)
L
Linus Torvalds 已提交
1302 1303 1304 1305 1306 1307
{
	struct super_block *sb = inode->i_sb;
	struct isofs_sb_info *sbi = ISOFS_SB(sb);
	unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
	unsigned long block;
	int high_sierra = sbi->s_high_sierra;
1308
	struct buffer_head *bh;
D
Dave Jones 已提交
1309 1310
	struct iso_directory_record *de;
	struct iso_directory_record *tmpde = NULL;
L
Linus Torvalds 已提交
1311 1312 1313
	unsigned int de_len;
	unsigned long offset;
	struct iso_inode_info *ei = ISOFS_I(inode);
1314
	int ret = -EIO;
L
Linus Torvalds 已提交
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329

	block = ei->i_iget5_block;
	bh = sb_bread(inode->i_sb, block);
	if (!bh)
		goto out_badread;

	offset = ei->i_iget5_offset;

	de = (struct iso_directory_record *) (bh->b_data + offset);
	de_len = *(unsigned char *) de;

	if (offset + de_len > bufsize) {
		int frag1 = bufsize - offset;

		tmpde = kmalloc(de_len, GFP_KERNEL);
1330
		if (!tmpde) {
1331
			ret = -ENOMEM;
L
Linus Torvalds 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
			goto fail;
		}
		memcpy(tmpde, bh->b_data + offset, frag1);
		brelse(bh);
		bh = sb_bread(inode->i_sb, ++block);
		if (!bh)
			goto out_badread;
		memcpy((char *)tmpde+frag1, bh->b_data, de_len - frag1);
		de = tmpde;
	}

	inode->i_ino = isofs_get_ino(ei->i_iget5_block,
D
Dave Jones 已提交
1344 1345
					ei->i_iget5_offset,
					ISOFS_BUFFER_BITS(inode));
L
Linus Torvalds 已提交
1346 1347 1348 1349 1350

	/* Assume it is a normal-format file unless told otherwise */
	ei->i_file_format = isofs_file_normal;

	if (de->flags[-high_sierra] & 2) {
1351 1352 1353 1354
		if (sbi->s_dmode != ISOFS_INVALID_MODE)
			inode->i_mode = S_IFDIR | sbi->s_dmode;
		else
			inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
M
Miklos Szeredi 已提交
1355
		set_nlink(inode, 1);	/*
D
Dave Jones 已提交
1356 1357 1358 1359 1360 1361
					 * Set to 1.  We know there are 2, but
					 * the find utility tries to optimize
					 * if it is 2, and it screws up.  It is
					 * easier to give 1 which tells find to
					 * do it the hard way.
					 */
L
Linus Torvalds 已提交
1362
	} else {
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
		if (sbi->s_fmode != ISOFS_INVALID_MODE) {
			inode->i_mode = S_IFREG | sbi->s_fmode;
		} else {
			/*
			 * Set default permissions: r-x for all.  The disc
			 * could be shared with DOS machines so virtually
			 * anything could be a valid executable.
			 */
			inode->i_mode = S_IFREG | S_IRUGO | S_IXUGO;
		}
M
Miklos Szeredi 已提交
1373
		set_nlink(inode, 1);
L
Linus Torvalds 已提交
1374 1375 1376
	}
	inode->i_uid = sbi->s_uid;
	inode->i_gid = sbi->s_gid;
1377
	inode->i_blocks = 0;
L
Linus Torvalds 已提交
1378 1379 1380 1381 1382

	ei->i_format_parm[0] = 0;
	ei->i_format_parm[1] = 0;
	ei->i_format_parm[2] = 0;

D
Dave Jones 已提交
1383
	ei->i_section_size = isonum_733(de->size);
1384
	if (de->flags[-high_sierra] & 0x80) {
1385 1386
		ret = isofs_read_level3_size(inode);
		if (ret < 0)
D
Dave Jones 已提交
1387
			goto fail;
1388
		ret = -EIO;
L
Linus Torvalds 已提交
1389 1390 1391
	} else {
		ei->i_next_section_block = 0;
		ei->i_next_section_offset = 0;
D
Dave Jones 已提交
1392
		inode->i_size = isonum_733(de->size);
L
Linus Torvalds 已提交
1393 1394 1395 1396 1397 1398 1399 1400
	}

	/*
	 * Some dipshit decided to store some other bit of information
	 * in the high byte of the file length.  Truncate size in case
	 * this CDROM was mounted with the cruft option.
	 */

1401
	if (sbi->s_cruft)
L
Linus Torvalds 已提交
1402 1403 1404
		inode->i_size &= 0x00ffffff;

	if (de->interleave[0]) {
D
Dave Jones 已提交
1405
		printk(KERN_DEBUG "ISOFS: Interleaved files not (yet) supported.\n");
L
Linus Torvalds 已提交
1406 1407 1408 1409 1410 1411
		inode->i_size = 0;
	}

	/* I have no idea what file_unit_size is used for, so
	   we will flag it for now */
	if (de->file_unit_size[0] != 0) {
D
Dave Jones 已提交
1412 1413
		printk(KERN_DEBUG "ISOFS: File unit size != 0 for ISO file (%ld).\n",
			inode->i_ino);
L
Linus Torvalds 已提交
1414 1415 1416 1417 1418 1419
	}

	/* I have no idea what other flag bits are used for, so
	   we will flag it for now */
#ifdef DEBUG
	if((de->flags[-high_sierra] & ~2)!= 0){
D
Dave Jones 已提交
1420 1421 1422
		printk(KERN_DEBUG "ISOFS: Unusual flag settings for ISO file "
				"(%ld %x).\n",
			inode->i_ino, de->flags[-high_sierra]);
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	}
#endif

	inode->i_mtime.tv_sec =
	inode->i_atime.tv_sec =
	inode->i_ctime.tv_sec = iso_date(de->date, high_sierra);
	inode->i_mtime.tv_nsec =
	inode->i_atime.tv_nsec =
	inode->i_ctime.tv_nsec = 0;

D
Dave Jones 已提交
1433 1434
	ei->i_first_extent = (isonum_733(de->extent) +
			isonum_711(de->ext_attr_length));
L
Linus Torvalds 已提交
1435 1436

	/* Set the number of blocks for stat() - should be done before RR */
D
Dave Jones 已提交
1437
	inode->i_blocks = (inode->i_size + 511) >> 9;
L
Linus Torvalds 已提交
1438 1439 1440 1441 1442 1443 1444

	/*
	 * Now test for possible Rock Ridge extensions which will override
	 * some of these numbers in the inode structure.
	 */

	if (!high_sierra) {
1445
		parse_rock_ridge_inode(de, inode, relocated);
L
Linus Torvalds 已提交
1446
		/* if we want uid/gid set, override the rock ridge setting */
1447 1448 1449 1450
		if (sbi->s_uid_set)
			inode->i_uid = sbi->s_uid;
		if (sbi->s_gid_set)
			inode->i_gid = sbi->s_gid;
L
Linus Torvalds 已提交
1451
	}
1452 1453 1454 1455 1456 1457 1458
	/* Now set final access rights if overriding rock ridge setting */
	if (S_ISDIR(inode->i_mode) && sbi->s_overriderockperm &&
	    sbi->s_dmode != ISOFS_INVALID_MODE)
		inode->i_mode = S_IFDIR | sbi->s_dmode;
	if (S_ISREG(inode->i_mode) && sbi->s_overriderockperm &&
	    sbi->s_fmode != ISOFS_INVALID_MODE)
		inode->i_mode = S_IFREG | sbi->s_fmode;
L
Linus Torvalds 已提交
1459 1460 1461 1462

	/* Install the inode operations vector */
	if (S_ISREG(inode->i_mode)) {
		inode->i_fop = &generic_ro_fops;
D
Dave Jones 已提交
1463
		switch (ei->i_file_format) {
L
Linus Torvalds 已提交
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
#ifdef CONFIG_ZISOFS
		case isofs_file_compressed:
			inode->i_data.a_ops = &zisofs_aops;
			break;
#endif
		default:
			inode->i_data.a_ops = &isofs_aops;
			break;
		}
	} else if (S_ISDIR(inode->i_mode)) {
		inode->i_op = &isofs_dir_inode_operations;
		inode->i_fop = &isofs_dir_operations;
	} else if (S_ISLNK(inode->i_mode)) {
		inode->i_op = &page_symlink_inode_operations;
1478
		inode_nohighmem(inode);
L
Linus Torvalds 已提交
1479 1480 1481 1482 1483
		inode->i_data.a_ops = &isofs_symlink_aops;
	} else
		/* XXX - parse_rock_ridge_inode() had already set i_rdev. */
		init_special_inode(inode, inode->i_mode, inode->i_rdev);

1484
	ret = 0;
1485
out:
J
Jesper Juhl 已提交
1486
	kfree(tmpde);
L
Linus Torvalds 已提交
1487 1488
	if (bh)
		brelse(bh);
1489
	return ret;
L
Linus Torvalds 已提交
1490

1491
out_badread:
L
Linus Torvalds 已提交
1492
	printk(KERN_WARNING "ISOFS: unable to read i-node block\n");
1493
fail:
L
Linus Torvalds 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
	goto out;
}

struct isofs_iget5_callback_data {
	unsigned long block;
	unsigned long offset;
};

static int isofs_iget5_test(struct inode *ino, void *data)
{
	struct iso_inode_info *i = ISOFS_I(ino);
	struct isofs_iget5_callback_data *d =
		(struct isofs_iget5_callback_data*)data;
	return (i->i_iget5_block == d->block)
D
Dave Jones 已提交
1508
		&& (i->i_iget5_offset == d->offset);
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
}

static int isofs_iget5_set(struct inode *ino, void *data)
{
	struct iso_inode_info *i = ISOFS_I(ino);
	struct isofs_iget5_callback_data *d =
		(struct isofs_iget5_callback_data*)data;
	i->i_iget5_block = d->block;
	i->i_iget5_offset = d->offset;
	return 0;
}

/* Store, in the inode's containing structure, the block and block
 * offset that point to the underlying meta-data for the inode.  The
 * code below is otherwise similar to the iget() code in
 * include/linux/fs.h */
1525 1526 1527 1528
struct inode *__isofs_iget(struct super_block *sb,
			   unsigned long block,
			   unsigned long offset,
			   int relocated)
L
Linus Torvalds 已提交
1529 1530 1531 1532
{
	unsigned long hashval;
	struct inode *inode;
	struct isofs_iget5_callback_data data;
1533
	long ret;
L
Linus Torvalds 已提交
1534 1535

	if (offset >= 1ul << sb->s_blocksize_bits)
1536
		return ERR_PTR(-EINVAL);
L
Linus Torvalds 已提交
1537 1538 1539 1540 1541 1542

	data.block = block;
	data.offset = offset;

	hashval = (block << sb->s_blocksize_bits) | offset;

1543
	inode = iget5_locked(sb, hashval, &isofs_iget5_test,
D
Dave Jones 已提交
1544
				&isofs_iget5_set, &data);
L
Linus Torvalds 已提交
1545

1546 1547 1548 1549
	if (!inode)
		return ERR_PTR(-ENOMEM);

	if (inode->i_state & I_NEW) {
1550
		ret = isofs_read_inode(inode, relocated);
1551 1552 1553 1554 1555 1556
		if (ret < 0) {
			iget_failed(inode);
			inode = ERR_PTR(ret);
		} else {
			unlock_new_inode(inode);
		}
L
Linus Torvalds 已提交
1557 1558 1559 1560 1561
	}

	return inode;
}

A
Al Viro 已提交
1562 1563
static struct dentry *isofs_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
1564
{
A
Al Viro 已提交
1565
	return mount_bdev(fs_type, flags, dev_name, data, isofs_fill_super);
L
Linus Torvalds 已提交
1566 1567 1568 1569 1570
}

static struct file_system_type iso9660_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "iso9660",
A
Al Viro 已提交
1571
	.mount		= isofs_mount,
L
Linus Torvalds 已提交
1572 1573 1574
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};
1575
MODULE_ALIAS_FS("iso9660");
1576
MODULE_ALIAS("iso9660");
L
Linus Torvalds 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613

static int __init init_iso9660_fs(void)
{
	int err = init_inodecache();
	if (err)
		goto out;
#ifdef CONFIG_ZISOFS
	err = zisofs_init();
	if (err)
		goto out1;
#endif
	err = register_filesystem(&iso9660_fs_type);
	if (err)
		goto out2;
	return 0;
out2:
#ifdef CONFIG_ZISOFS
	zisofs_cleanup();
out1:
#endif
	destroy_inodecache();
out:
	return err;
}

static void __exit exit_iso9660_fs(void)
{
        unregister_filesystem(&iso9660_fs_type);
#ifdef CONFIG_ZISOFS
	zisofs_cleanup();
#endif
	destroy_inodecache();
}

module_init(init_iso9660_fs)
module_exit(exit_iso9660_fs)
MODULE_LICENSE("GPL");