Import 2.3.1pre2
[davej-history.git] / fs / isofs / inode.c
blob9dc1f7dee7809a78911860fb051b610308ef33e3
1 /*
2 * linux/fs/isofs/inode.c
4 * (C) 1991 Linus Torvalds - minix filesystem
5 * 1992, 1993, 1994 Eric Youngdale Modified for ISO 9660 filesystem.
6 * 1994 Eberhard Moenkeberg - multi session handling.
7 * 1995 Mark Dobie - allow mounting of some weird VideoCDs and PhotoCDs.
8 * 1997 Gordon Chaffee - Joliet CDs
9 * 1998 Eric Lammerts - ISO 9660 Level 3
12 #include <linux/config.h>
13 #include <linux/module.h>
15 #include <linux/stat.h>
16 #include <linux/sched.h>
17 #include <linux/iso_fs.h>
18 #include <linux/kernel.h>
19 #include <linux/major.h>
20 #include <linux/mm.h>
21 #include <linux/string.h>
22 #include <linux/locks.h>
23 #include <linux/malloc.h>
24 #include <linux/errno.h>
25 #include <linux/cdrom.h>
26 #include <linux/init.h>
27 #include <linux/nls.h>
28 #include <linux/ctype.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
34 * We have no support for "multi volume" CDs, but more and more disks carry
35 * wrong information within the volume descriptors.
37 #define IGNORE_WRONG_MULTI_VOLUME_SPECS
38 #define BEQUIET
40 #ifdef LEAK_CHECK
41 static int check_malloc = 0;
42 static int check_bread = 0;
43 #endif
45 static int isofs_hashi(struct dentry *parent, struct qstr *qstr);
46 static int isofs_hash(struct dentry *parent, struct qstr *qstr);
47 static int isofs_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b);
48 static int isofs_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b);
50 #ifdef CONFIG_JOLIET
51 static int isofs_hashi_ms(struct dentry *parent, struct qstr *qstr);
52 static int isofs_hash_ms(struct dentry *parent, struct qstr *qstr);
53 static int isofs_cmpi_ms(struct dentry *dentry, struct qstr *a, struct qstr *b);
54 static int isofs_cmp_ms(struct dentry *dentry, struct qstr *a, struct qstr *b);
55 #endif
57 void isofs_put_super(struct super_block *sb)
59 #ifdef CONFIG_JOLIET
60 if (sb->u.isofs_sb.s_nls_iocharset) {
61 unload_nls(sb->u.isofs_sb.s_nls_iocharset);
62 sb->u.isofs_sb.s_nls_iocharset = NULL;
64 #endif
66 #ifdef LEAK_CHECK
67 printk("Outstanding mallocs:%d, outstanding buffers: %d\n",
68 check_malloc, check_bread);
69 #endif
71 MOD_DEC_USE_COUNT;
72 return;
75 static struct super_operations isofs_sops = {
76 isofs_read_inode,
77 NULL, /* write_inode */
78 NULL, /* put_inode */
79 NULL, /* delete_inode */
80 NULL, /* notify_change */
81 isofs_put_super,
82 NULL, /* write_super */
83 isofs_statfs,
84 NULL
87 static struct dentry_operations isofs_dentry_ops[] = {
89 NULL, /* d_revalidate */
90 isofs_hash,
91 isofs_cmp,
92 NULL /* d_delete */
95 NULL, /* d_revalidate */
96 isofs_hashi,
97 isofs_cmpi,
98 NULL /* d_delete */
100 #ifdef CONFIG_JOLIET
102 NULL, /* d_revalidate */
103 isofs_hash_ms,
104 isofs_cmp_ms,
105 NULL /* d_delete */
108 NULL, /* d_revalidate */
109 isofs_hashi_ms,
110 isofs_cmpi_ms,
111 NULL /* d_delete */
113 #endif
116 struct iso9660_options{
117 char map;
118 char rock;
119 char joliet;
120 char cruft;
121 char unhide;
122 unsigned char check;
123 unsigned int blocksize;
124 mode_t mode;
125 gid_t gid;
126 uid_t uid;
127 char *iocharset;
128 unsigned char utf8;
132 * Compute the hash for the isofs name corresponding to the dentry.
134 static int
135 isofs_hash_common(struct dentry *dentry, struct qstr *qstr, int ms)
137 const char *name;
138 int len;
140 len = qstr->len;
141 name = qstr->name;
142 if (ms) {
143 while (len && name[len-1] == '.')
144 len--;
147 qstr->hash = full_name_hash(name, len);
149 return 0;
153 * Compute the hash for the isofs name corresponding to the dentry.
155 static int
156 isofs_hashi_common(struct dentry *dentry, struct qstr *qstr, int ms)
158 const char *name;
159 int len;
160 char c;
161 unsigned long hash;
163 len = qstr->len;
164 name = qstr->name;
165 if (ms) {
166 while (len && name[len-1] == '.')
167 len--;
170 hash = init_name_hash();
171 while (len--) {
172 c = tolower(*name++);
173 hash = partial_name_hash(tolower(c), hash);
175 qstr->hash = end_name_hash(hash);
177 return 0;
181 * Case insensitive compare of two isofs names.
183 static int
184 isofs_cmpi_common(struct dentry *dentry,struct qstr *a,struct qstr *b,int ms)
186 int alen, blen;
188 /* A filename cannot end in '.' or we treat it like it has none */
189 alen = a->len;
190 blen = b->len;
191 if (ms) {
192 while (alen && a->name[alen-1] == '.')
193 alen--;
194 while (blen && b->name[blen-1] == '.')
195 blen--;
197 if (alen == blen) {
198 if (strnicmp(a->name, b->name, alen) == 0)
199 return 0;
201 return 1;
205 * Case sensitive compare of two isofs names.
207 static int
208 isofs_cmp_common(struct dentry *dentry,struct qstr *a,struct qstr *b,int ms)
210 int alen, blen;
212 /* A filename cannot end in '.' or we treat it like it has none */
213 alen = a->len;
214 blen = b->len;
215 if (ms) {
216 while (alen && a->name[alen-1] == '.')
217 alen--;
218 while (blen && b->name[blen-1] == '.')
219 blen--;
221 if (alen == blen) {
222 if (strncmp(a->name, b->name, alen) == 0)
223 return 0;
225 return 1;
228 static int
229 isofs_hash(struct dentry *dentry, struct qstr *qstr)
231 return isofs_hash_common(dentry, qstr, 0);
234 static int
235 isofs_hashi(struct dentry *dentry, struct qstr *qstr)
237 return isofs_hashi_common(dentry, qstr, 0);
240 static int
241 isofs_cmp(struct dentry *dentry,struct qstr *a,struct qstr *b)
243 return isofs_cmp_common(dentry, a, b, 0);
246 static int
247 isofs_cmpi(struct dentry *dentry,struct qstr *a,struct qstr *b)
249 return isofs_cmpi_common(dentry, a, b, 0);
252 #ifdef CONFIG_JOLIET
253 static int
254 isofs_hash_ms(struct dentry *dentry, struct qstr *qstr)
256 return isofs_hash_common(dentry, qstr, 1);
259 static int
260 isofs_hashi_ms(struct dentry *dentry, struct qstr *qstr)
262 return isofs_hashi_common(dentry, qstr, 1);
265 static int
266 isofs_cmp_ms(struct dentry *dentry,struct qstr *a,struct qstr *b)
268 return isofs_cmp_common(dentry, a, b, 1);
271 static int
272 isofs_cmpi_ms(struct dentry *dentry,struct qstr *a,struct qstr *b)
274 return isofs_cmpi_common(dentry, a, b, 1);
276 #endif
278 static int parse_options(char *options, struct iso9660_options * popt)
280 char *this_char,*value;
282 popt->map = 'n';
283 popt->rock = 'y';
284 popt->joliet = 'y';
285 popt->cruft = 'n';
286 popt->unhide = 'n';
287 popt->check = 'u'; /* unset */
288 popt->blocksize = 1024;
289 popt->mode = S_IRUGO | S_IXUGO; /* r-x for all. The disc could
290 be shared with DOS machines so
291 virtually anything could be
292 a valid executable. */
293 popt->gid = 0;
294 popt->uid = 0;
295 popt->iocharset = NULL;
296 popt->utf8 = 0;
297 if (!options) return 1;
298 for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
299 if (strncmp(this_char,"norock",6) == 0) {
300 popt->rock = 'n';
301 continue;
303 if (strncmp(this_char,"nojoliet",8) == 0) {
304 popt->joliet = 'n';
305 continue;
307 if (strncmp(this_char,"unhide",6) == 0) {
308 popt->unhide = 'y';
309 continue;
311 if (strncmp(this_char,"cruft",5) == 0) {
312 popt->cruft = 'y';
313 continue;
315 if (strncmp(this_char,"utf8",4) == 0) {
316 popt->utf8 = 1;
317 continue;
319 if ((value = strchr(this_char,'=')) != NULL)
320 *value++ = 0;
322 #ifdef CONFIG_JOLIET
323 if (!strcmp(this_char,"iocharset") && value) {
324 popt->iocharset = value;
325 while (*value && *value != ',')
326 value++;
327 if (value == popt->iocharset)
328 return 0;
329 *value = 0;
330 } else
331 #endif
332 if (!strcmp(this_char,"map") && value) {
333 if (value[0] && !value[1] && strchr("ano",*value))
334 popt->map = *value;
335 else if (!strcmp(value,"off")) popt->map = 'o';
336 else if (!strcmp(value,"normal")) popt->map = 'n';
337 else if (!strcmp(value,"acorn")) popt->map = 'a';
338 else return 0;
340 else if (!strcmp(this_char,"check") && value) {
341 if (value[0] && !value[1] && strchr("rs",*value))
342 popt->check = *value;
343 else if (!strcmp(value,"relaxed")) popt->check = 'r';
344 else if (!strcmp(value,"strict")) popt->check = 's';
345 else return 0;
347 else if (!strcmp(this_char,"conv") && value) {
348 /* no conversion is done anymore;
349 we still accept the same mount options,
350 but ignore them */
351 if (value[0] && !value[1] && strchr("btma",*value)) ;
352 else if (!strcmp(value,"binary")) ;
353 else if (!strcmp(value,"text")) ;
354 else if (!strcmp(value,"mtext")) ;
355 else if (!strcmp(value,"auto")) ;
356 else return 0;
358 else if (value &&
359 (!strcmp(this_char,"block") ||
360 !strcmp(this_char,"mode") ||
361 !strcmp(this_char,"uid") ||
362 !strcmp(this_char,"gid"))) {
363 char * vpnt = value;
364 unsigned int ivalue = simple_strtoul(vpnt, &vpnt, 0);
365 if (*vpnt) return 0;
366 switch(*this_char) {
367 case 'b':
368 if ( ivalue != 512
369 && ivalue != 1024
370 && ivalue != 2048) return 0;
371 popt->blocksize = ivalue;
372 break;
373 case 'u':
374 popt->uid = ivalue;
375 break;
376 case 'g':
377 popt->gid = ivalue;
378 break;
379 case 'm':
380 popt->mode = ivalue;
381 break;
384 else return 1;
386 return 1;
390 * look if the driver can tell the multi session redirection value
392 * don't change this if you don't know what you do, please!
393 * Multisession is legal only with XA disks.
394 * A non-XA disk with more than one volume descriptor may do it right, but
395 * usually is written in a nowhere standardized "multi-partition" manner.
396 * Multisession uses absolute addressing (solely the first frame of the whole
397 * track is #0), multi-partition uses relative addressing (each first frame of
398 * each track is #0), and a track is not a session.
400 * A broken CDwriter software or drive firmware does not set new standards,
401 * at least not if conflicting with the existing ones.
403 * emoenke@gwdg.de
405 #define WE_OBEY_THE_WRITTEN_STANDARDS 1
407 static unsigned int isofs_get_last_session(kdev_t dev)
409 struct cdrom_multisession ms_info;
410 unsigned int vol_desc_start;
411 struct inode inode_fake;
412 struct file_operations *fops;
413 extern struct file_operations * get_blkfops(unsigned int);
414 int i;
416 vol_desc_start=0;
417 fops = get_blkfops(MAJOR(dev));
418 if (fops && fops->ioctl)
420 /* Whoops. We must save the old FS, since otherwise
421 * we would destroy the kernels idea about FS on root
422 * mount in read_super... [chexum]
424 mm_segment_t old_fs=get_fs();
425 inode_fake.i_rdev=dev;
426 init_waitqueue_head(&inode_fake.i_wait);
427 ms_info.addr_format=CDROM_LBA;
428 set_fs(KERNEL_DS);
429 i=get_blkfops(MAJOR(dev))->ioctl(&inode_fake,
430 NULL,
431 CDROMMULTISESSION,
432 (unsigned long) &ms_info);
433 set_fs(old_fs);
434 #if 0
435 printk("isofs.inode: CDROMMULTISESSION: rc=%d\n",i);
436 if (i==0)
438 printk("isofs.inode: XA disk: %s\n", ms_info.xa_flag ? "yes":"no");
439 printk("isofs.inode: vol_desc_start = %d\n", ms_info.addr.lba);
441 #endif
442 if (i==0)
443 #if WE_OBEY_THE_WRITTEN_STANDARDS
444 if (ms_info.xa_flag) /* necessary for a valid ms_info.addr */
445 #endif
446 vol_desc_start=ms_info.addr.lba;
448 return vol_desc_start;
452 * Initialize the superblock and read the root inode.
454 * Note: a check_disk_change() has been done immediately prior
455 * to this call, so we don't need to check again.
457 struct super_block *isofs_read_super(struct super_block *s, void *data,
458 int silent)
460 kdev_t dev = s->s_dev;
461 struct buffer_head * bh = NULL, *pri_bh = NULL;
462 struct hs_primary_descriptor * h_pri = NULL;
463 struct iso_primary_descriptor * pri = NULL;
464 struct iso_supplementary_descriptor *sec = NULL;
465 struct iso_directory_record * rootp;
466 int joliet_level = 0;
467 int high_sierra;
468 int iso_blknum, block;
469 int orig_zonesize;
470 int table;
471 unsigned int blocksize, blocksize_bits;
472 unsigned int vol_desc_start;
473 unsigned long first_data_zone;
474 struct inode * inode;
475 struct iso9660_options opt;
477 MOD_INC_USE_COUNT;
478 /* lock before any blocking operations */
479 lock_super(s);
481 if (!parse_options((char *) data, &opt))
482 goto out_unlock;
484 #if 0
485 printk("map = %c\n", opt.map);
486 printk("rock = %c\n", opt.rock);
487 printk("joliet = %c\n", opt.joliet);
488 printk("check = %c\n", opt.check);
489 printk("cruft = %c\n", opt.cruft);
490 printk("unhide = %c\n", opt.unhide);
491 printk("blocksize = %d\n", opt.blocksize);
492 printk("gid = %d\n", opt.gid);
493 printk("uid = %d\n", opt.uid);
494 printk("iocharset = %s\n", opt.iocharset);
495 #endif
498 * First of all, get the hardware blocksize for this device.
499 * If we don't know what it is, or the hardware blocksize is
500 * larger than the blocksize the user specified, then use
501 * that value.
503 blocksize = get_hardblocksize(dev);
504 if( (blocksize != 0)
505 && (blocksize > opt.blocksize) )
508 * Force the blocksize we are going to use to be the
509 * hardware blocksize.
511 opt.blocksize = blocksize;
514 blocksize_bits = 0;
516 int i = opt.blocksize;
517 while (i != 1){
518 blocksize_bits++;
519 i >>=1;
523 set_blocksize(dev, opt.blocksize);
525 s->u.isofs_sb.s_high_sierra = high_sierra = 0; /* default is iso9660 */
527 vol_desc_start = isofs_get_last_session(dev);
529 for (iso_blknum = vol_desc_start+16;
530 iso_blknum < vol_desc_start+100; iso_blknum++)
532 struct hs_volume_descriptor * hdp;
533 struct iso_volume_descriptor * vdp;
535 block = iso_blknum << (ISOFS_BLOCK_BITS-blocksize_bits);
536 if (!(bh = bread(dev, block, opt.blocksize)))
537 goto out_no_read;
539 vdp = (struct iso_volume_descriptor *)bh->b_data;
540 hdp = (struct hs_volume_descriptor *)bh->b_data;
542 /* Due to the overlapping physical location of the descriptors,
543 * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
544 * proper identification in this case, we first check for ISO.
546 if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
547 if (isonum_711 (vdp->type) == ISO_VD_END)
548 break;
549 if (isonum_711 (vdp->type) == ISO_VD_PRIMARY) {
550 if (pri == NULL) {
551 pri = (struct iso_primary_descriptor *)vdp;
552 /* Save the buffer in case we need it ... */
553 pri_bh = bh;
554 bh = NULL;
557 #ifdef CONFIG_JOLIET
558 else if (isonum_711 (vdp->type) == ISO_VD_SUPPLEMENTARY) {
559 sec = (struct iso_supplementary_descriptor *)vdp;
560 if (sec->escape[0] == 0x25 && sec->escape[1] == 0x2f) {
561 if (opt.joliet == 'y') {
562 if (sec->escape[2] == 0x40) {
563 joliet_level = 1;
564 } else if (sec->escape[2] == 0x43) {
565 joliet_level = 2;
566 } else if (sec->escape[2] == 0x45) {
567 joliet_level = 3;
569 printk(KERN_DEBUG"ISO 9660 Extensions: Microsoft Joliet Level %d\n",
570 joliet_level);
572 goto root_found;
573 } else {
574 /* Unknown supplementary volume descriptor */
575 sec = NULL;
578 #endif
579 } else {
580 if (strncmp (hdp->id, HS_STANDARD_ID, sizeof hdp->id) == 0) {
581 if (isonum_711 (hdp->type) != ISO_VD_PRIMARY)
582 goto out_freebh;
584 s->u.isofs_sb.s_high_sierra = 1;
585 high_sierra = 1;
586 opt.rock = 'n';
587 h_pri = (struct hs_primary_descriptor *)vdp;
588 goto root_found;
592 /* Just skip any volume descriptors we don't recognize */
594 brelse(bh);
595 bh = NULL;
598 * If we fall through, either no volume descriptor was found,
599 * or else we passed a primary descriptor looking for others.
601 if (!pri)
602 goto out_unknown_format;
603 brelse(bh);
604 bh = pri_bh;
605 pri_bh = NULL;
607 root_found:
608 brelse(pri_bh);
610 if (joliet_level && opt.rock == 'n') {
611 /* This is the case of Joliet with the norock mount flag.
612 * A disc with both Joliet and Rock Ridge is handled later
614 pri = (struct iso_primary_descriptor *) sec;
617 if(high_sierra){
618 rootp = (struct iso_directory_record *) h_pri->root_directory_record;
619 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
620 if (isonum_723 (h_pri->volume_set_size) != 1)
621 goto out_no_support;
622 #endif IGNORE_WRONG_MULTI_VOLUME_SPECS
623 s->u.isofs_sb.s_nzones = isonum_733 (h_pri->volume_space_size);
624 s->u.isofs_sb.s_log_zone_size = isonum_723 (h_pri->logical_block_size);
625 s->u.isofs_sb.s_max_size = isonum_733(h_pri->volume_space_size);
626 } else {
627 rootp = (struct iso_directory_record *) pri->root_directory_record;
628 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
629 if (isonum_723 (pri->volume_set_size) != 1)
630 goto out_no_support;
631 #endif IGNORE_WRONG_MULTI_VOLUME_SPECS
632 s->u.isofs_sb.s_nzones = isonum_733 (pri->volume_space_size);
633 s->u.isofs_sb.s_log_zone_size = isonum_723 (pri->logical_block_size);
634 s->u.isofs_sb.s_max_size = isonum_733(pri->volume_space_size);
637 s->u.isofs_sb.s_ninodes = 0; /* No way to figure this out easily */
639 orig_zonesize = s -> u.isofs_sb.s_log_zone_size;
641 * If the zone size is smaller than the hardware sector size,
642 * this is a fatal error. This would occur if the disc drive
643 * had sectors that were 2048 bytes, but the filesystem had
644 * blocks that were 512 bytes (which should only very rarely
645 * happen.)
647 if(blocksize != 0 && orig_zonesize < blocksize)
648 goto out_bad_size;
650 /* RDE: convert log zone size to bit shift */
651 switch (s -> u.isofs_sb.s_log_zone_size)
652 { case 512: s -> u.isofs_sb.s_log_zone_size = 9; break;
653 case 1024: s -> u.isofs_sb.s_log_zone_size = 10; break;
654 case 2048: s -> u.isofs_sb.s_log_zone_size = 11; break;
656 default:
657 goto out_bad_zone_size;
660 s->s_magic = ISOFS_SUPER_MAGIC;
662 /* The CDROM is read-only, has no nodes (devices) on it, and since
663 all of the files appear to be owned by root, we really do not want
664 to allow suid. (suid or devices will not show up unless we have
665 Rock Ridge extensions) */
667 s->s_flags |= MS_RDONLY /* | MS_NODEV | MS_NOSUID */;
669 /* RDE: data zone now byte offset! */
671 first_data_zone = ((isonum_733 (rootp->extent) +
672 isonum_711 (rootp->ext_attr_length))
673 << s -> u.isofs_sb.s_log_zone_size);
674 s->u.isofs_sb.s_firstdatazone = first_data_zone;
675 #ifndef BEQUIET
676 printk(KERN_DEBUG "Max size:%ld Log zone size:%ld\n",
677 s->u.isofs_sb.s_max_size,
678 1UL << s->u.isofs_sb.s_log_zone_size);
679 printk(KERN_DEBUG "First datazone:%ld Root inode number:%ld\n",
680 s->u.isofs_sb.s_firstdatazone >> s -> u.isofs_sb.s_log_zone_size,
681 s->u.isofs_sb.s_firstdatazone);
682 if(high_sierra)
683 printk(KERN_DEBUG "Disc in High Sierra format.\n");
684 #endif
687 * If the Joliet level is set, we _may_ decide to use the
688 * secondary descriptor, but can't be sure until after we
689 * read the root inode. But before reading the root inode
690 * we may need to change the device blocksize, and would
691 * rather release the old buffer first. So, we cache the
692 * first_data_zone value from the secondary descriptor.
694 if (joliet_level) {
695 pri = (struct iso_primary_descriptor *) sec;
696 rootp = (struct iso_directory_record *)
697 pri->root_directory_record;
698 first_data_zone = ((isonum_733 (rootp->extent) +
699 isonum_711 (rootp->ext_attr_length))
700 << s -> u.isofs_sb.s_log_zone_size);
704 * We're all done using the volume descriptor, and may need
705 * to change the device blocksize, so release the buffer now.
707 brelse(bh);
710 * Force the blocksize to 512 for 512 byte sectors. The file
711 * read primitives really get it wrong in a bad way if we don't
712 * do this.
714 * Note - we should never be setting the blocksize to something
715 * less than the hardware sector size for the device. If we
716 * do, we would end up having to read larger buffers and split
717 * out portions to satisfy requests.
719 * Note2- the idea here is that we want to deal with the optimal
720 * zonesize in the filesystem. If we have it set to something less,
721 * then we have horrible problems with trying to piece together
722 * bits of adjacent blocks in order to properly read directory
723 * entries. By forcing the blocksize in this way, we ensure
724 * that we will never be required to do this.
726 if ( orig_zonesize != opt.blocksize ) {
727 set_blocksize(dev, orig_zonesize);
728 #ifndef BEQUIET
729 printk(KERN_DEBUG
730 "ISOFS: Forcing new log zone size:%d\n", orig_zonesize);
731 #endif
733 s->s_blocksize = orig_zonesize;
734 s->s_blocksize_bits = s -> u.isofs_sb.s_log_zone_size;
736 s->u.isofs_sb.s_nls_iocharset = NULL;
738 #ifdef CONFIG_JOLIET
739 if (joliet_level && opt.utf8 == 0) {
740 char * p = opt.iocharset ? opt.iocharset : "iso8859-1";
741 s->u.isofs_sb.s_nls_iocharset = load_nls(p);
742 if (! s->u.isofs_sb.s_nls_iocharset) {
743 /* Fail only if explicit charset specified */
744 if (opt.iocharset)
745 goto out_freebh;
746 s->u.isofs_sb.s_nls_iocharset = load_nls_default();
749 #endif
750 s->s_op = &isofs_sops;
751 s->u.isofs_sb.s_mapping = opt.map;
752 s->u.isofs_sb.s_rock = (opt.rock == 'y' ? 2 : 0);
753 s->u.isofs_sb.s_cruft = opt.cruft;
754 s->u.isofs_sb.s_unhide = opt.unhide;
755 s->u.isofs_sb.s_uid = opt.uid;
756 s->u.isofs_sb.s_gid = opt.gid;
757 s->u.isofs_sb.s_utf8 = opt.utf8;
759 * It would be incredibly stupid to allow people to mark every file on the disk
760 * as suid, so we merely allow them to set the default permissions.
762 s->u.isofs_sb.s_mode = opt.mode & 0777;
765 * Read the root inode, which _may_ result in changing
766 * the s_rock flag. Once we have the final s_rock value,
767 * we then decide whether to use the Joliet descriptor.
769 inode = iget(s, s->u.isofs_sb.s_firstdatazone);
772 * If this disk has both Rock Ridge and Joliet on it, then we
773 * want to use Rock Ridge by default. This can be overridden
774 * by using the norock mount option. There is still one other
775 * possibility that is not taken into account: a Rock Ridge
776 * CD with Unicode names. Until someone sees such a beast, it
777 * will not be supported.
779 if (s->u.isofs_sb.s_rock == 1) {
780 joliet_level = 0;
781 } else if (joliet_level) {
782 s->u.isofs_sb.s_rock = 0;
783 if (s->u.isofs_sb.s_firstdatazone != first_data_zone) {
784 s->u.isofs_sb.s_firstdatazone = first_data_zone;
785 printk(KERN_DEBUG
786 "ISOFS: changing to secondary root\n");
787 iput(inode);
788 inode = iget(s, s->u.isofs_sb.s_firstdatazone);
792 if (opt.check == 'u') {
793 /* Only Joliet is case insensitive by default */
794 if (joliet_level) opt.check = 'r';
795 else opt.check = 's';
797 s->u.isofs_sb.s_joliet_level = joliet_level;
799 /* check the root inode */
800 if (!inode)
801 goto out_no_root;
802 if (!inode->i_op)
803 goto out_bad_root;
804 /* get the root dentry */
805 s->s_root = d_alloc_root(inode, NULL);
806 if (!(s->s_root))
807 goto out_no_root;
809 table = 0;
810 if (joliet_level) table += 2;
811 if (opt.check == 'r') table++;
812 s->s_root->d_op = &isofs_dentry_ops[table];
814 unlock_super(s);
815 return s;
818 * Display error messages and free resources.
820 out_bad_root:
821 printk(KERN_WARNING "isofs_read_super: root inode not initialized\n");
822 goto out_iput;
823 out_no_root:
824 printk(KERN_WARNING "isofs_read_super: get root inode failed\n");
825 out_iput:
826 iput(inode);
827 #ifdef CONFIG_JOLIET
828 if (s->u.isofs_sb.s_nls_iocharset)
829 unload_nls(s->u.isofs_sb.s_nls_iocharset);
830 #endif
831 goto out_unlock;
832 out_no_read:
833 printk(KERN_WARNING "isofs_read_super: "
834 "bread failed, dev=%s, iso_blknum=%d, block=%d\n",
835 kdevname(dev), iso_blknum, block);
836 goto out_unlock;
837 out_bad_zone_size:
838 printk(KERN_WARNING "Bad logical zone size %ld\n",
839 s->u.isofs_sb.s_log_zone_size);
840 goto out_freebh;
841 out_bad_size:
842 printk(KERN_WARNING "Logical zone size(%d) < hardware blocksize(%u)\n",
843 orig_zonesize, blocksize);
844 goto out_freebh;
845 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
846 out_no_support:
847 printk(KERN_WARNING "Multi-volume disks not supported.\n");
848 goto out_freebh;
849 #endif
850 out_unknown_format:
851 if (!silent)
852 printk(KERN_WARNING "Unable to identify CD-ROM format.\n");
854 out_freebh:
855 brelse(bh);
856 out_unlock:
857 s->s_dev = 0;
858 unlock_super(s);
859 MOD_DEC_USE_COUNT;
860 return NULL;
863 int isofs_statfs (struct super_block *sb, struct statfs *buf, int bufsiz)
865 struct statfs tmp;
867 tmp.f_type = ISOFS_SUPER_MAGIC;
868 tmp.f_bsize = sb->s_blocksize;
869 tmp.f_blocks = (sb->u.isofs_sb.s_nzones
870 << (sb->u.isofs_sb.s_log_zone_size - sb->s_blocksize_bits));
871 tmp.f_bfree = 0;
872 tmp.f_bavail = 0;
873 tmp.f_files = sb->u.isofs_sb.s_ninodes;
874 tmp.f_ffree = 0;
875 tmp.f_namelen = NAME_MAX;
876 return copy_to_user(buf, &tmp, bufsiz) ? -EFAULT : 0;
879 int isofs_bmap(struct inode * inode,int block)
881 off_t b_off, offset, size;
882 struct inode *ino;
883 unsigned int firstext;
884 unsigned long nextino;
885 int i;
887 if (block<0) {
888 printk("_isofs_bmap: block<0");
889 return 0;
892 b_off = block << ISOFS_BUFFER_BITS(inode);
895 * If we are beyond the end of this file, don't give out any
896 * blocks.
898 if( b_off > inode->i_size )
900 off_t max_legal_read_offset;
903 * If we are *way* beyond the end of the file, print a message.
904 * Access beyond the end of the file up to the next page boundary
905 * is normal, however because of the way the page cache works.
906 * In this case, we just return 0 so that we can properly fill
907 * the page with useless information without generating any
908 * I/O errors.
910 max_legal_read_offset = (inode->i_size + PAGE_SIZE - 1)
911 & ~(PAGE_SIZE - 1);
912 if( b_off >= max_legal_read_offset )
915 printk("_isofs_bmap: block>= EOF(%d, %ld)\n", block,
916 inode->i_size);
918 return 0;
921 offset = 0;
922 firstext = inode->u.isofs_i.i_first_extent;
923 size = inode->u.isofs_i.i_section_size;
924 nextino = inode->u.isofs_i.i_next_section_ino;
925 #ifdef DEBUG
926 printk("first inode: inode=%x nextino=%x firstext=%u size=%lu\n",
927 inode->i_ino, nextino, firstext, size);
928 #endif
929 i = 0;
930 if (nextino) {
931 while(b_off >= offset + size) {
932 offset += size;
934 if(nextino == 0) return 0;
935 ino = iget(inode->i_sb, nextino);
936 if(!ino) return 0;
937 firstext = ino->u.isofs_i.i_first_extent;
938 size = ino->u.isofs_i.i_section_size;
939 #ifdef DEBUG
940 printk("read inode: inode=%lu ino=%lu nextino=%lu firstext=%u size=%lu\n",
941 inode->i_ino, nextino, ino->u.isofs_i.i_next_section_ino, firstext, size);
942 #endif
943 nextino = ino->u.isofs_i.i_next_section_ino;
944 iput(ino);
946 if(++i > 100) {
947 printk("isofs_bmap: More than 100 file sections ?!?, aborting...\n");
948 printk("isofs_bmap: ino=%lu block=%d firstext=%u size=%u nextino=%lu\n",
949 inode->i_ino, block, firstext, (unsigned)size, nextino);
950 return 0;
954 #ifdef DEBUG
955 printk("isofs_bmap: mapped inode:block %x:%d to block %lu\n",
956 inode->i_ino, block, (b_off - offset + firstext) >> ISOFS_BUFFER_BITS(inode));
957 #endif
958 return (b_off - offset + firstext) >> ISOFS_BUFFER_BITS(inode);
962 static void test_and_set_uid(uid_t *p, uid_t value)
964 if(value) {
965 *p = value;
966 #if 0
967 printk("Resetting to %d\n", value);
968 #endif
972 static int isofs_read_level3_size(struct inode * inode)
974 unsigned long ino = inode->i_ino;
975 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
976 int high_sierra = inode->i_sb->u.isofs_sb.s_high_sierra;
977 struct buffer_head * bh = NULL;
978 int block = 0;
979 int i = 0;
980 void *cpnt;
981 struct iso_directory_record * raw_inode;
983 inode->i_size = 0;
984 inode->u.isofs_i.i_next_section_ino = 0;
985 do {
986 unsigned char *pnt;
987 unsigned int reclen;
988 int offset = (ino & (bufsize - 1));
990 cpnt = NULL;
991 /* Check whether to update our buffer */
992 if (block != ino >> ISOFS_BUFFER_BITS(inode)) {
993 block = ino >> ISOFS_BUFFER_BITS(inode);
994 brelse(bh);
995 bh = bread(inode->i_dev, block, bufsize);
996 if (!bh)
997 goto out_noread;
999 pnt = ((unsigned char *) bh->b_data + offset);
1000 raw_inode = ((struct iso_directory_record *) pnt);
1002 * Note: this is invariant even if the record
1003 * spans buffers and must be copied ...
1005 reclen = *pnt;
1007 /* N.B. this test doesn't trigger the i++ code ... */
1008 if(reclen == 0) {
1009 ino = (ino & ~(ISOFS_BLOCK_SIZE - 1)) + ISOFS_BLOCK_SIZE;
1010 continue;
1013 /* Check whether the raw inode spans the buffer ... */
1014 if (offset + reclen > bufsize){
1015 int frag1 = bufsize - offset;
1017 cpnt = kmalloc(reclen, GFP_KERNEL);
1018 if (cpnt == NULL)
1019 goto out_nomem;
1020 memcpy(cpnt, pnt, frag1);
1021 brelse(bh);
1022 bh = bread(inode->i_dev, ++block, bufsize);
1023 if (!bh)
1024 goto out_noread;
1025 offset += reclen - bufsize;
1026 memcpy((char *)cpnt+frag1, bh->b_data, offset);
1027 raw_inode = ((struct iso_directory_record *) cpnt);
1030 inode->i_size += isonum_733 (raw_inode->size);
1031 if(i == 1) inode->u.isofs_i.i_next_section_ino = ino;
1033 ino += reclen;
1034 if (cpnt)
1035 kfree (cpnt);
1036 i++;
1037 if(i > 100)
1038 goto out_toomany;
1039 } while(raw_inode->flags[-high_sierra] & 0x80);
1040 out:
1041 brelse(bh);
1042 return 0;
1044 out_nomem:
1045 printk(KERN_INFO "ISOFS: NoMem ISO inode %lu\n", inode->i_ino);
1046 brelse(bh);
1047 return 1;
1048 out_noread:
1049 printk(KERN_INFO "ISOFS: unable to read i-node block %d\n", block);
1050 if (cpnt)
1051 kfree(cpnt);
1052 return 1;
1053 out_toomany:
1054 printk(KERN_INFO "isofs_read_level3_size: "
1055 "More than 100 file sections ?!?, aborting...\n"
1056 "isofs_read_level3_size: inode=%lu ino=%lu\n",
1057 inode->i_ino, ino);
1058 goto out;
1061 void isofs_read_inode(struct inode * inode)
1063 struct super_block *sb = inode->i_sb;
1064 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
1065 int block = inode->i_ino >> ISOFS_BUFFER_BITS(inode);
1066 int high_sierra = sb->u.isofs_sb.s_high_sierra;
1067 struct buffer_head * bh;
1068 struct iso_directory_record * raw_inode;
1069 unsigned char *pnt;
1070 int volume_seq_no, i;
1072 bh = bread(inode->i_dev, block, bufsize);
1073 if (!bh) {
1074 printk(KERN_WARNING "ISOFS: unable to read i-node block\n");
1075 goto fail;
1078 pnt = ((unsigned char *) bh->b_data
1079 + (inode->i_ino & (bufsize - 1)));
1080 raw_inode = ((struct iso_directory_record *) pnt);
1082 if (raw_inode->flags[-high_sierra] & 2) {
1083 inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
1084 inode->i_nlink = 1; /* Set to 1. We know there are 2, but
1085 the find utility tries to optimize
1086 if it is 2, and it screws up. It is
1087 easier to give 1 which tells find to
1088 do it the hard way. */
1089 } else {
1090 /* Everybody gets to read the file. */
1091 inode->i_mode = inode->i_sb->u.isofs_sb.s_mode;
1092 inode->i_nlink = 1;
1093 inode->i_mode |= S_IFREG;
1094 /* If there are no periods in the name,
1095 * then set the execute permission bit
1097 for(i=0; i< raw_inode->name_len[0]; i++)
1098 if(raw_inode->name[i]=='.' || raw_inode->name[i]==';')
1099 break;
1100 if(i == raw_inode->name_len[0] || raw_inode->name[i] == ';')
1101 inode->i_mode |= S_IXUGO; /* execute permission */
1103 inode->i_uid = inode->i_sb->u.isofs_sb.s_uid;
1104 inode->i_gid = inode->i_sb->u.isofs_sb.s_gid;
1105 inode->i_blocks = inode->i_blksize = 0;
1108 inode->u.isofs_i.i_section_size = isonum_733 (raw_inode->size);
1109 if(raw_inode->flags[-high_sierra] & 0x80) {
1110 if(isofs_read_level3_size(inode)) goto fail;
1111 } else {
1112 inode->i_size = isonum_733 (raw_inode->size);
1115 /* There are defective discs out there - we do this to protect
1116 ourselves. A cdrom will never contain more than 800Mb
1117 .. but a DVD may be up to 1Gig (Ulrich Habel) */
1118 if((inode->i_size < 0 || inode->i_size > 1073741824) &&
1119 inode->i_sb->u.isofs_sb.s_cruft == 'n') {
1120 printk("Warning: defective cdrom. Enabling \"cruft\" mount option.\n");
1121 inode->i_sb->u.isofs_sb.s_cruft = 'y';
1124 /* Some dipshit decided to store some other bit of information in the high
1125 byte of the file length. Catch this and holler. WARNING: this will make
1126 it impossible for a file to be > 16Mb on the CDROM!!!*/
1128 if(inode->i_sb->u.isofs_sb.s_cruft == 'y' &&
1129 inode->i_size & 0xff000000){
1130 /* printk("Illegal format on cdrom. Pester manufacturer.\n"); */
1131 inode->i_size &= 0x00ffffff;
1134 if (raw_inode->interleave[0]) {
1135 printk("Interleaved files not (yet) supported.\n");
1136 inode->i_size = 0;
1139 /* I have no idea what file_unit_size is used for, so
1140 we will flag it for now */
1141 if(raw_inode->file_unit_size[0] != 0){
1142 printk("File unit size != 0 for ISO file (%ld).\n",inode->i_ino);
1145 /* I have no idea what other flag bits are used for, so
1146 we will flag it for now */
1147 #ifdef DEBUG
1148 if((raw_inode->flags[-high_sierra] & ~2)!= 0){
1149 printk("Unusual flag settings for ISO file (%ld %x).\n",
1150 inode->i_ino, raw_inode->flags[-high_sierra]);
1152 #endif
1154 #ifdef DEBUG
1155 printk("Get inode %x: %d %d: %d\n",inode->i_ino, block,
1156 ((int)pnt) & 0x3ff, inode->i_size);
1157 #endif
1159 inode->i_mtime = inode->i_atime = inode->i_ctime =
1160 iso_date(raw_inode->date, high_sierra);
1162 inode->u.isofs_i.i_first_extent = (isonum_733 (raw_inode->extent) +
1163 isonum_711 (raw_inode->ext_attr_length))
1164 << inode -> i_sb -> u.isofs_sb.s_log_zone_size;
1166 /* Now test for possible Rock Ridge extensions which will override some of
1167 these numbers in the inode structure. */
1169 if (!high_sierra) {
1170 parse_rock_ridge_inode(raw_inode, inode);
1171 /* hmm..if we want uid or gid set, override the rock ridge setting */
1172 test_and_set_uid(&inode->i_uid, inode->i_sb->u.isofs_sb.s_uid);
1175 #ifdef DEBUG
1176 printk("Inode: %x extent: %x\n",inode->i_ino, inode->u.isofs_i.i_first_extent);
1177 #endif
1179 /* get the volume sequence number */
1180 volume_seq_no = isonum_723 (raw_inode->volume_sequence_number) ;
1183 * All done with buffer ... no more references to buffer memory!
1185 brelse(bh);
1188 * Disable checking if we see any volume number other than 0 or 1.
1189 * We could use the cruft option, but that has multiple purposes, one
1190 * of which is limiting the file size to 16Mb. Thus we silently allow
1191 * volume numbers of 0 to go through without complaining.
1193 if (inode->i_sb->u.isofs_sb.s_cruft == 'n' &&
1194 (volume_seq_no != 0) && (volume_seq_no != 1)) {
1195 printk("Warning: defective cdrom (volume sequence number). Enabling \"cruft\" mount option.\n");
1196 inode->i_sb->u.isofs_sb.s_cruft = 'y';
1199 /* Install the inode operations vector */
1200 inode->i_op = NULL;
1201 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
1202 if (inode->i_sb->u.isofs_sb.s_cruft != 'y' &&
1203 (volume_seq_no != 0) && (volume_seq_no != 1)) {
1204 printk("Multi volume CD somehow got mounted.\n");
1205 } else
1206 #endif IGNORE_WRONG_MULTI_VOLUME_SPECS
1208 if (S_ISREG(inode->i_mode))
1209 inode->i_op = &isofs_file_inode_operations;
1210 else if (S_ISDIR(inode->i_mode))
1211 inode->i_op = &isofs_dir_inode_operations;
1212 else if (S_ISLNK(inode->i_mode))
1213 inode->i_op = &isofs_symlink_inode_operations;
1214 else
1215 /* XXX - parse_rock_ridge_inode() had already set i_rdev. */
1216 init_special_inode(inode, inode->i_mode, kdev_t_to_nr(inode->i_rdev));
1218 return;
1220 fail:
1221 /* With a data error we return this information */
1222 inode->i_mtime = inode->i_atime = inode->i_ctime = 0;
1223 inode->u.isofs_i.i_first_extent = 0;
1224 inode->i_size = 0;
1225 inode->i_blocks = inode->i_blksize = 0;
1226 inode->i_nlink = 1;
1227 inode->i_uid = inode->i_gid = 0;
1228 inode->i_mode = S_IFREG; /*Regular file, no one gets to read*/
1229 inode->i_op = NULL;
1230 return;
1233 /* There are times when we need to know the inode number of a parent of
1234 a particular directory. When control passes through a routine that
1235 has access to the parent information, it fills it into the inode structure,
1236 but sometimes the inode gets flushed out of the queue, and someone
1237 remembers the number. When they try to open up again, we have lost
1238 the information. The '..' entry on the disc points to the data area
1239 for a particular inode, so we can follow these links back up, but since
1240 we do not know the inode number, we do not actually know how large the
1241 directory is. The disc is almost always correct, and there is
1242 enough error checking on the drive itself, but an open ended search
1243 makes me a little nervous.
1245 The BSD iso filesystem uses the extent number for an inode, and this
1246 would work really nicely for us except that the read_inode function
1247 would not have any clean way of finding the actual directory record
1248 that goes with the file. If we had such info, then it would pay
1249 to change the inode numbers and eliminate this function.
1252 int isofs_lookup_grandparent(struct inode * parent, int extent)
1254 unsigned long bufsize = ISOFS_BUFFER_SIZE(parent);
1255 unsigned char bufbits = ISOFS_BUFFER_BITS(parent);
1256 unsigned int block,offset;
1257 int parent_dir, inode_number;
1258 int result;
1259 int directory_size;
1260 struct buffer_head * bh;
1261 struct iso_directory_record * de;
1263 offset = 0;
1264 block = extent << (ISOFS_ZONE_BITS(parent) - bufbits);
1265 if (!(bh = bread(parent->i_dev, block, bufsize))) return -1;
1267 while (1 == 1) {
1268 de = (struct iso_directory_record *) (bh->b_data + offset);
1269 if (*((unsigned char *) de) == 0)
1271 brelse(bh);
1272 printk("Directory .. not found\n");
1273 return -1;
1276 offset += *((unsigned char *) de);
1278 if (offset >= bufsize)
1280 printk(".. Directory not in first block"
1281 " of directory.\n");
1282 brelse(bh);
1283 return -1;
1286 if (de->name_len[0] == 1 && de->name[0] == 1)
1288 parent_dir = find_rock_ridge_relocation(de, parent);
1289 directory_size = isonum_733 (de->size);
1290 brelse(bh);
1291 break;
1294 #ifdef DEBUG
1295 printk("Parent dir:%x\n",parent_dir);
1296 #endif
1297 /* Now we know the extent where the parent dir starts on. */
1299 result = -1;
1301 offset = 0;
1302 block = parent_dir << (ISOFS_ZONE_BITS(parent) - bufbits);
1303 if (!block || !(bh = bread(parent->i_dev,block, bufsize)))
1305 return -1;
1308 for(;;)
1310 de = (struct iso_directory_record *) (bh->b_data + offset);
1311 inode_number = (block << bufbits)+(offset & (bufsize - 1));
1313 /* If the length byte is zero, we should move on to the next
1314 CDROM sector. If we are at the end of the directory, we
1315 kick out of the while loop. */
1317 if ((*((unsigned char *) de) == 0) || (offset == bufsize) )
1319 brelse(bh);
1320 offset = 0;
1321 block++;
1322 directory_size -= bufsize;
1323 if(directory_size < 0) return -1;
1324 if((block & 1) && (ISOFS_ZONE_BITS(parent) - bufbits) == 1)
1326 return -1;
1328 if((block & 3) && (ISOFS_ZONE_BITS(parent) - bufbits) == 2)
1330 return -1;
1332 if (!block
1333 || !(bh = bread(parent->i_dev,block, bufsize)))
1335 return -1;
1337 continue;
1340 /* Make sure that the entire directory record is in the current
1341 bh block. If not, we malloc a buffer, and put the two
1342 halves together, so that we can cleanly read the block. */
1344 offset += *((unsigned char *) de);
1346 if (offset > bufsize)
1348 printk("Directory overrun\n");
1349 goto out;
1352 if (find_rock_ridge_relocation(de, parent) == extent){
1353 result = inode_number;
1354 goto out;
1359 /* We go here for any condition we cannot handle.
1360 We also drop through to here at the end of the directory. */
1362 out:
1363 brelse(bh);
1364 #ifdef DEBUG
1365 printk("Resultant Inode %d\n",result);
1366 #endif
1367 return result;
1370 #ifdef LEAK_CHECK
1371 #undef malloc
1372 #undef free_s
1373 #undef bread
1374 #undef brelse
1376 void * leak_check_malloc(unsigned int size){
1377 void * tmp;
1378 check_malloc++;
1379 tmp = kmalloc(size, GFP_KERNEL);
1380 return tmp;
1383 void leak_check_free_s(void * obj, int size){
1384 check_malloc--;
1385 return kfree_s(obj, size);
1388 struct buffer_head * leak_check_bread(int dev, int block, int size){
1389 check_bread++;
1390 return bread(dev, block, size);
1393 void leak_check_brelse(struct buffer_head * bh){
1394 check_bread--;
1395 return brelse(bh);
1398 #endif
1400 static struct file_system_type iso9660_fs_type = {
1401 "iso9660",
1402 FS_REQUIRES_DEV,
1403 isofs_read_super,
1404 NULL
1407 __initfunc(int init_iso9660_fs(void))
1409 return register_filesystem(&iso9660_fs_type);
1412 #ifdef MODULE
1413 EXPORT_NO_SYMBOLS;
1415 int init_module(void)
1417 return init_iso9660_fs();
1420 void cleanup_module(void)
1422 unregister_filesystem(&iso9660_fs_type);
1425 #endif