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>
21 #include <linux/string.h>
22 #include <linux/locks.h>
23 #include <linux/slab.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>
29 #include <linux/smp_lock.h>
30 #include <linux/blkdev.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
38 * We have no support for "multi volume" CDs, but more and more disks carry
39 * wrong information within the volume descriptors.
41 #define IGNORE_WRONG_MULTI_VOLUME_SPECS
45 static int check_malloc = 0;
46 static int check_bread = 0;
49 static int isofs_hashi(struct dentry *parent, struct qstr *qstr);
50 static int isofs_hash(struct dentry *parent, struct qstr *qstr);
51 static int isofs_dentry_cmpi(struct dentry *dentry, struct qstr *a, struct qstr *b);
52 static int isofs_dentry_cmp(struct dentry *dentry, struct qstr *a, struct qstr *b);
55 static int isofs_hashi_ms(struct dentry *parent, struct qstr *qstr);
56 static int isofs_hash_ms(struct dentry *parent, struct qstr *qstr);
57 static int isofs_dentry_cmpi_ms(struct dentry *dentry, struct qstr *a, struct qstr *b);
58 static int isofs_dentry_cmp_ms(struct dentry *dentry, struct qstr *a, struct qstr *b);
61 static void isofs_put_super(struct super_block *sb)
64 if (sb->u.isofs_sb.s_nls_iocharset) {
65 unload_nls(sb->u.isofs_sb.s_nls_iocharset);
66 sb->u.isofs_sb.s_nls_iocharset = NULL;
71 printk("Outstanding mallocs:%d, outstanding buffers: %d\n",
72 check_malloc, check_bread);
78 static void isofs_read_inode(struct inode *);
79 static int isofs_statfs (struct super_block *, struct statfs *);
81 static struct super_operations isofs_sops = {
82 read_inode: isofs_read_inode,
83 put_super: isofs_put_super,
87 static struct dentry_operations isofs_dentry_ops[] = {
90 d_compare: isofs_dentry_cmp,
94 d_compare: isofs_dentry_cmpi,
98 d_hash: isofs_hash_ms,
99 d_compare: isofs_dentry_cmp_ms,
102 d_hash: isofs_hashi_ms,
103 d_compare: isofs_dentry_cmpi_ms,
108 struct iso9660_options{
116 unsigned int blocksize;
128 * Compute the hash for the isofs name corresponding to the dentry.
131 isofs_hash_common(struct dentry *dentry, struct qstr *qstr, int ms)
139 while (len && name[len-1] == '.')
143 qstr->hash = full_name_hash(name, len);
149 * Compute the hash for the isofs name corresponding to the dentry.
152 isofs_hashi_common(struct dentry *dentry, struct qstr *qstr, int ms)
162 while (len && name[len-1] == '.')
166 hash = init_name_hash();
168 c = tolower(*name++);
169 hash = partial_name_hash(tolower(c), hash);
171 qstr->hash = end_name_hash(hash);
177 * Case insensitive compare of two isofs names.
180 isofs_dentry_cmpi_common(struct dentry *dentry,struct qstr *a,struct qstr *b,int ms)
184 /* A filename cannot end in '.' or we treat it like it has none */
188 while (alen && a->name[alen-1] == '.')
190 while (blen && b->name[blen-1] == '.')
194 if (strnicmp(a->name, b->name, alen) == 0)
201 * Case sensitive compare of two isofs names.
204 isofs_dentry_cmp_common(struct dentry *dentry,struct qstr *a,struct qstr *b,int ms)
208 /* A filename cannot end in '.' or we treat it like it has none */
212 while (alen && a->name[alen-1] == '.')
214 while (blen && b->name[blen-1] == '.')
218 if (strncmp(a->name, b->name, alen) == 0)
225 isofs_hash(struct dentry *dentry, struct qstr *qstr)
227 return isofs_hash_common(dentry, qstr, 0);
231 isofs_hashi(struct dentry *dentry, struct qstr *qstr)
233 return isofs_hashi_common(dentry, qstr, 0);
237 isofs_dentry_cmp(struct dentry *dentry,struct qstr *a,struct qstr *b)
239 return isofs_dentry_cmp_common(dentry, a, b, 0);
243 isofs_dentry_cmpi(struct dentry *dentry,struct qstr *a,struct qstr *b)
245 return isofs_dentry_cmpi_common(dentry, a, b, 0);
250 isofs_hash_ms(struct dentry *dentry, struct qstr *qstr)
252 return isofs_hash_common(dentry, qstr, 1);
256 isofs_hashi_ms(struct dentry *dentry, struct qstr *qstr)
258 return isofs_hashi_common(dentry, qstr, 1);
262 isofs_dentry_cmp_ms(struct dentry *dentry,struct qstr *a,struct qstr *b)
264 return isofs_dentry_cmp_common(dentry, a, b, 1);
268 isofs_dentry_cmpi_ms(struct dentry *dentry,struct qstr *a,struct qstr *b)
270 return isofs_dentry_cmpi_common(dentry, a, b, 1);
274 static int parse_options(char *options, struct iso9660_options * popt)
276 char *this_char,*value;
283 popt->check = 'u'; /* unset */
284 popt->nocompress = 0;
285 popt->blocksize = 1024;
286 popt->mode = S_IRUGO | S_IXUGO; /* r-x for all. The disc could
287 be shared with DOS machines so
288 virtually anything could be
289 a valid executable. */
292 popt->iocharset = NULL;
296 if (!options) return 1;
297 for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
298 if (strncmp(this_char,"norock",6) == 0) {
302 if (strncmp(this_char,"nojoliet",8) == 0) {
306 if (strncmp(this_char,"unhide",6) == 0) {
310 if (strncmp(this_char,"cruft",5) == 0) {
314 if (strncmp(this_char,"utf8",4) == 0) {
318 if (strncmp(this_char,"nocompress",10) == 0) {
319 popt->nocompress = 1;
322 if ((value = strchr(this_char,'=')) != NULL)
326 if (!strcmp(this_char,"iocharset") && value) {
327 popt->iocharset = value;
328 while (*value && *value != ',')
330 if (value == popt->iocharset)
335 if (!strcmp(this_char,"map") && value) {
336 if (value[0] && !value[1] && strchr("ano",*value))
338 else if (!strcmp(value,"off")) popt->map = 'o';
339 else if (!strcmp(value,"normal")) popt->map = 'n';
340 else if (!strcmp(value,"acorn")) popt->map = 'a';
343 if (!strcmp(this_char,"session") && value) {
345 unsigned int ivalue = simple_strtoul(vpnt, &vpnt, 0);
346 if(ivalue < 0 || ivalue >99) return 0;
347 popt->session=ivalue+1;
349 if (!strcmp(this_char,"sbsector") && value) {
351 unsigned int ivalue = simple_strtoul(vpnt, &vpnt, 0);
352 if(ivalue < 0 || ivalue >660*512) return 0;
353 popt->sbsector=ivalue;
355 else if (!strcmp(this_char,"check") && value) {
356 if (value[0] && !value[1] && strchr("rs",*value))
357 popt->check = *value;
358 else if (!strcmp(value,"relaxed")) popt->check = 'r';
359 else if (!strcmp(value,"strict")) popt->check = 's';
362 else if (!strcmp(this_char,"conv") && value) {
363 /* no conversion is done anymore;
364 we still accept the same mount options,
366 if (value[0] && !value[1] && strchr("btma",*value)) ;
367 else if (!strcmp(value,"binary")) ;
368 else if (!strcmp(value,"text")) ;
369 else if (!strcmp(value,"mtext")) ;
370 else if (!strcmp(value,"auto")) ;
374 (!strcmp(this_char,"block") ||
375 !strcmp(this_char,"mode") ||
376 !strcmp(this_char,"uid") ||
377 !strcmp(this_char,"gid"))) {
379 unsigned int ivalue = simple_strtoul(vpnt, &vpnt, 0);
385 && ivalue != 2048) return 0;
386 popt->blocksize = ivalue;
405 * look if the driver can tell the multi session redirection value
407 * don't change this if you don't know what you do, please!
408 * Multisession is legal only with XA disks.
409 * A non-XA disk with more than one volume descriptor may do it right, but
410 * usually is written in a nowhere standardized "multi-partition" manner.
411 * Multisession uses absolute addressing (solely the first frame of the whole
412 * track is #0), multi-partition uses relative addressing (each first frame of
413 * each track is #0), and a track is not a session.
415 * A broken CDwriter software or drive firmware does not set new standards,
416 * at least not if conflicting with the existing ones.
420 #define WE_OBEY_THE_WRITTEN_STANDARDS 1
422 static unsigned int isofs_get_last_session(struct super_block *sb,s32 session )
424 struct cdrom_multisession ms_info;
425 unsigned int vol_desc_start;
426 struct block_device *bdev = sb->s_bdev;
430 ms_info.addr_format=CDROM_LBA;
431 if(session >= 0 && session <= 99) {
432 struct cdrom_tocentry Te;
433 Te.cdte_track=session;
434 Te.cdte_format=CDROM_LBA;
435 i = ioctl_by_bdev(bdev, CDROMREADTOCENTRY, (unsigned long) &Te);
437 printk(KERN_DEBUG "Session %d start %d type %d\n",
438 session, Te.cdte_addr.lba,
439 Te.cdte_ctrl&CDROM_DATA_TRACK);
440 if ((Te.cdte_ctrl&CDROM_DATA_TRACK) == 4)
441 return Te.cdte_addr.lba;
444 printk(KERN_ERR "Invalid session number or type of track\n");
446 i = ioctl_by_bdev(bdev, CDROMMULTISESSION, (unsigned long) &ms_info);
447 if(session > 0) printk(KERN_ERR "Invalid session number\n");
449 printk("isofs.inode: CDROMMULTISESSION: rc=%d\n",i);
451 printk("isofs.inode: XA disk: %s\n",ms_info.xa_flag?"yes":"no");
452 printk("isofs.inode: vol_desc_start = %d\n", ms_info.addr.lba);
456 #if WE_OBEY_THE_WRITTEN_STANDARDS
457 if (ms_info.xa_flag) /* necessary for a valid ms_info.addr */
459 vol_desc_start=ms_info.addr.lba;
460 return vol_desc_start;
464 * Initialize the superblock and read the root inode.
466 * Note: a check_disk_change() has been done immediately prior
467 * to this call, so we don't need to check again.
469 static struct super_block *isofs_read_super(struct super_block *s, void *data,
472 kdev_t dev = s->s_dev;
473 struct buffer_head * bh = NULL, *pri_bh = NULL;
474 struct hs_primary_descriptor * h_pri = NULL;
475 struct iso_primary_descriptor * pri = NULL;
476 struct iso_supplementary_descriptor *sec = NULL;
477 struct iso_directory_record * rootp;
478 int joliet_level = 0;
480 int iso_blknum, block;
483 unsigned int blocksize, blocksize_bits;
484 unsigned int vol_desc_start;
485 unsigned long first_data_zone;
486 struct inode * inode;
487 struct iso9660_options opt;
489 if (!parse_options((char *) data, &opt))
493 printk("map = %c\n", opt.map);
494 printk("rock = %c\n", opt.rock);
495 printk("joliet = %c\n", opt.joliet);
496 printk("check = %c\n", opt.check);
497 printk("cruft = %c\n", opt.cruft);
498 printk("unhide = %c\n", opt.unhide);
499 printk("blocksize = %d\n", opt.blocksize);
500 printk("gid = %d\n", opt.gid);
501 printk("uid = %d\n", opt.uid);
502 printk("iocharset = %s\n", opt.iocharset);
506 * First of all, get the hardware blocksize for this device.
507 * If we don't know what it is, or the hardware blocksize is
508 * larger than the blocksize the user specified, then use
511 blocksize = get_hardsect_size(dev);
512 if(blocksize > opt.blocksize) {
514 * Force the blocksize we are going to use to be the
515 * hardware blocksize.
517 opt.blocksize = blocksize;
522 int i = opt.blocksize;
529 set_blocksize(dev, opt.blocksize);
530 s->s_blocksize = opt.blocksize;
532 s->u.isofs_sb.s_high_sierra = high_sierra = 0; /* default is iso9660 */
534 vol_desc_start = (opt.sbsector != -1) ?
535 opt.sbsector : isofs_get_last_session(s,opt.session);
537 for (iso_blknum = vol_desc_start+16;
538 iso_blknum < vol_desc_start+100; iso_blknum++)
540 struct hs_volume_descriptor * hdp;
541 struct iso_volume_descriptor * vdp;
543 block = iso_blknum << (ISOFS_BLOCK_BITS-blocksize_bits);
544 if (!(bh = sb_bread(s, block)))
547 vdp = (struct iso_volume_descriptor *)bh->b_data;
548 hdp = (struct hs_volume_descriptor *)bh->b_data;
550 /* Due to the overlapping physical location of the descriptors,
551 * ISO CDs can match hdp->id==HS_STANDARD_ID as well. To ensure
552 * proper identification in this case, we first check for ISO.
554 if (strncmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) == 0) {
555 if (isonum_711 (vdp->type) == ISO_VD_END)
557 if (isonum_711 (vdp->type) == ISO_VD_PRIMARY) {
559 pri = (struct iso_primary_descriptor *)vdp;
560 /* Save the buffer in case we need it ... */
566 else if (isonum_711 (vdp->type) == ISO_VD_SUPPLEMENTARY) {
567 sec = (struct iso_supplementary_descriptor *)vdp;
568 if (sec->escape[0] == 0x25 && sec->escape[1] == 0x2f) {
569 if (opt.joliet == 'y') {
570 if (sec->escape[2] == 0x40) {
572 } else if (sec->escape[2] == 0x43) {
574 } else if (sec->escape[2] == 0x45) {
577 printk(KERN_DEBUG"ISO 9660 Extensions: Microsoft Joliet Level %d\n",
582 /* Unknown supplementary volume descriptor */
588 if (strncmp (hdp->id, HS_STANDARD_ID, sizeof hdp->id) == 0) {
589 if (isonum_711 (hdp->type) != ISO_VD_PRIMARY)
592 s->u.isofs_sb.s_high_sierra = 1;
595 h_pri = (struct hs_primary_descriptor *)vdp;
600 /* Just skip any volume descriptors we don't recognize */
606 * If we fall through, either no volume descriptor was found,
607 * or else we passed a primary descriptor looking for others.
610 goto out_unknown_format;
617 if (joliet_level && (pri == NULL || opt.rock == 'n')) {
618 /* This is the case of Joliet with the norock mount flag.
619 * A disc with both Joliet and Rock Ridge is handled later
621 pri = (struct iso_primary_descriptor *) sec;
625 rootp = (struct iso_directory_record *) h_pri->root_directory_record;
626 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
627 if (isonum_723 (h_pri->volume_set_size) != 1)
629 #endif /* IGNORE_WRONG_MULTI_VOLUME_SPECS */
630 s->u.isofs_sb.s_nzones = isonum_733 (h_pri->volume_space_size);
631 s->u.isofs_sb.s_log_zone_size = isonum_723 (h_pri->logical_block_size);
632 s->u.isofs_sb.s_max_size = isonum_733(h_pri->volume_space_size);
634 rootp = (struct iso_directory_record *) pri->root_directory_record;
635 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
636 if (isonum_723 (pri->volume_set_size) != 1)
638 #endif /* IGNORE_WRONG_MULTI_VOLUME_SPECS */
639 s->u.isofs_sb.s_nzones = isonum_733 (pri->volume_space_size);
640 s->u.isofs_sb.s_log_zone_size = isonum_723 (pri->logical_block_size);
641 s->u.isofs_sb.s_max_size = isonum_733(pri->volume_space_size);
644 s->u.isofs_sb.s_ninodes = 0; /* No way to figure this out easily */
646 orig_zonesize = s -> u.isofs_sb.s_log_zone_size;
648 * If the zone size is smaller than the hardware sector size,
649 * this is a fatal error. This would occur if the disc drive
650 * had sectors that were 2048 bytes, but the filesystem had
651 * blocks that were 512 bytes (which should only very rarely
654 if(blocksize != 0 && orig_zonesize < blocksize)
657 /* RDE: convert log zone size to bit shift */
658 switch (s -> u.isofs_sb.s_log_zone_size)
659 { case 512: s -> u.isofs_sb.s_log_zone_size = 9; break;
660 case 1024: s -> u.isofs_sb.s_log_zone_size = 10; break;
661 case 2048: s -> u.isofs_sb.s_log_zone_size = 11; break;
664 goto out_bad_zone_size;
667 s->s_magic = ISOFS_SUPER_MAGIC;
669 /* The CDROM is read-only, has no nodes (devices) on it, and since
670 all of the files appear to be owned by root, we really do not want
671 to allow suid. (suid or devices will not show up unless we have
672 Rock Ridge extensions) */
674 s->s_flags |= MS_RDONLY /* | MS_NODEV | MS_NOSUID */;
676 /* Set this for reference. Its not currently used except on write
677 which we don't have .. */
679 /* RDE: data zone now byte offset! */
681 first_data_zone = ((isonum_733 (rootp->extent) +
682 isonum_711 (rootp->ext_attr_length))
683 << s -> u.isofs_sb.s_log_zone_size);
684 s->u.isofs_sb.s_firstdatazone = first_data_zone;
686 printk(KERN_DEBUG "Max size:%ld Log zone size:%ld\n",
687 s->u.isofs_sb.s_max_size,
688 1UL << s->u.isofs_sb.s_log_zone_size);
689 printk(KERN_DEBUG "First datazone:%ld Root inode number:%ld\n",
690 s->u.isofs_sb.s_firstdatazone >> s -> u.isofs_sb.s_log_zone_size,
691 s->u.isofs_sb.s_firstdatazone);
693 printk(KERN_DEBUG "Disc in High Sierra format.\n");
697 * If the Joliet level is set, we _may_ decide to use the
698 * secondary descriptor, but can't be sure until after we
699 * read the root inode. But before reading the root inode
700 * we may need to change the device blocksize, and would
701 * rather release the old buffer first. So, we cache the
702 * first_data_zone value from the secondary descriptor.
705 pri = (struct iso_primary_descriptor *) sec;
706 rootp = (struct iso_directory_record *)
707 pri->root_directory_record;
708 first_data_zone = ((isonum_733 (rootp->extent) +
709 isonum_711 (rootp->ext_attr_length))
710 << s -> u.isofs_sb.s_log_zone_size);
714 * We're all done using the volume descriptor, and may need
715 * to change the device blocksize, so release the buffer now.
721 * Force the blocksize to 512 for 512 byte sectors. The file
722 * read primitives really get it wrong in a bad way if we don't
725 * Note - we should never be setting the blocksize to something
726 * less than the hardware sector size for the device. If we
727 * do, we would end up having to read larger buffers and split
728 * out portions to satisfy requests.
730 * Note2- the idea here is that we want to deal with the optimal
731 * zonesize in the filesystem. If we have it set to something less,
732 * then we have horrible problems with trying to piece together
733 * bits of adjacent blocks in order to properly read directory
734 * entries. By forcing the blocksize in this way, we ensure
735 * that we will never be required to do this.
737 if ( orig_zonesize != opt.blocksize ) {
738 set_blocksize(dev, orig_zonesize);
741 "ISOFS: Forcing new log zone size:%d\n", orig_zonesize);
744 s->s_blocksize = orig_zonesize;
745 s->s_blocksize_bits = s -> u.isofs_sb.s_log_zone_size;
747 s->u.isofs_sb.s_nls_iocharset = NULL;
750 if (joliet_level && opt.utf8 == 0) {
751 char * p = opt.iocharset ? opt.iocharset : "iso8859-1";
752 s->u.isofs_sb.s_nls_iocharset = load_nls(p);
753 if (! s->u.isofs_sb.s_nls_iocharset) {
754 /* Fail only if explicit charset specified */
757 s->u.isofs_sb.s_nls_iocharset = load_nls_default();
761 s->s_op = &isofs_sops;
762 s->u.isofs_sb.s_mapping = opt.map;
763 s->u.isofs_sb.s_rock = (opt.rock == 'y' ? 2 : 0);
764 s->u.isofs_sb.s_rock_offset = -1; /* initial offset, will guess until SP is found*/
765 s->u.isofs_sb.s_cruft = opt.cruft;
766 s->u.isofs_sb.s_unhide = opt.unhide;
767 s->u.isofs_sb.s_uid = opt.uid;
768 s->u.isofs_sb.s_gid = opt.gid;
769 s->u.isofs_sb.s_utf8 = opt.utf8;
770 s->u.isofs_sb.s_nocompress = opt.nocompress;
772 * It would be incredibly stupid to allow people to mark every file
773 * on the disk as suid, so we merely allow them to set the default
776 s->u.isofs_sb.s_mode = opt.mode & 0777;
779 * Read the root inode, which _may_ result in changing
780 * the s_rock flag. Once we have the final s_rock value,
781 * we then decide whether to use the Joliet descriptor.
783 inode = iget(s, s->u.isofs_sb.s_firstdatazone);
786 * If this disk has both Rock Ridge and Joliet on it, then we
787 * want to use Rock Ridge by default. This can be overridden
788 * by using the norock mount option. There is still one other
789 * possibility that is not taken into account: a Rock Ridge
790 * CD with Unicode names. Until someone sees such a beast, it
791 * will not be supported.
793 if (s->u.isofs_sb.s_rock == 1) {
795 } else if (joliet_level) {
796 s->u.isofs_sb.s_rock = 0;
797 if (s->u.isofs_sb.s_firstdatazone != first_data_zone) {
798 s->u.isofs_sb.s_firstdatazone = first_data_zone;
800 "ISOFS: changing to secondary root\n");
802 inode = iget(s, s->u.isofs_sb.s_firstdatazone);
806 if (opt.check == 'u') {
807 /* Only Joliet is case insensitive by default */
808 if (joliet_level) opt.check = 'r';
809 else opt.check = 's';
811 s->u.isofs_sb.s_joliet_level = joliet_level;
813 /* check the root inode */
818 /* get the root dentry */
819 s->s_root = d_alloc_root(inode);
824 if (joliet_level) table += 2;
825 if (opt.check == 'r') table++;
826 s->s_root->d_op = &isofs_dentry_ops[table];
831 * Display error messages and free resources.
834 printk(KERN_WARNING "isofs_read_super: root inode not initialized\n");
837 printk(KERN_WARNING "isofs_read_super: get root inode failed\n");
841 if (s->u.isofs_sb.s_nls_iocharset)
842 unload_nls(s->u.isofs_sb.s_nls_iocharset);
846 printk(KERN_WARNING "isofs_read_super: "
847 "bread failed, dev=%s, iso_blknum=%d, block=%d\n",
848 kdevname(dev), iso_blknum, block);
851 printk(KERN_WARNING "Bad logical zone size %ld\n",
852 s->u.isofs_sb.s_log_zone_size);
855 printk(KERN_WARNING "Logical zone size(%d) < hardware blocksize(%u)\n",
856 orig_zonesize, blocksize);
858 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
860 printk(KERN_WARNING "Multi-volume disks not supported.\n");
865 printk(KERN_WARNING "Unable to identify CD-ROM format.\n");
873 static int isofs_statfs (struct super_block *sb, struct statfs *buf)
875 buf->f_type = ISOFS_SUPER_MAGIC;
876 buf->f_bsize = sb->s_blocksize;
877 buf->f_blocks = (sb->u.isofs_sb.s_nzones
878 << (sb->u.isofs_sb.s_log_zone_size - sb->s_blocksize_bits));
881 buf->f_files = sb->u.isofs_sb.s_ninodes;
883 buf->f_namelen = NAME_MAX;
888 * Get a set of blocks; filling in buffer_heads if already allocated
889 * or getblk() if they are not. Returns the number of blocks inserted
892 int isofs_get_blocks(struct inode *inode, long iblock,
893 struct buffer_head **bh_result, unsigned long nblocks)
896 unsigned offset, sect_size;
897 unsigned int firstext;
898 unsigned long nextino;
905 printk("isofs_get_blocks: block < 0\n");
912 firstext = inode->u.isofs_i.i_first_extent;
913 sect_size = inode->u.isofs_i.i_section_size >> ISOFS_BUFFER_BITS(inode);
914 nextino = inode->u.isofs_i.i_next_section_ino;
918 /* If we are *way* beyond the end of the file, print a message.
919 * Access beyond the end of the file up to the next page boundary
920 * is normal, however because of the way the page cache works.
921 * In this case, we just return 0 so that we can properly fill
922 * the page with useless information without generating any
925 if (b_off > ((inode->i_size + PAGE_CACHE_SIZE - 1) >> ISOFS_BUFFER_BITS(inode))) {
926 printk("isofs_get_blocks: block >= EOF (%ld, %ld)\n",
927 iblock, (unsigned long) inode->i_size);
932 while (b_off >= (offset + sect_size)) {
933 struct inode *ninode;
938 ninode = iget(inode->i_sb, nextino);
941 firstext = ninode->u.isofs_i.i_first_extent;
942 sect_size = ninode->u.isofs_i.i_section_size;
943 nextino = ninode->u.isofs_i.i_next_section_ino;
946 if (++section > 100) {
947 printk("isofs_get_blocks: More than 100 file sections ?!?, aborting...\n");
948 printk("isofs_get_blocks: ino=%lu block=%ld firstext=%u sect_size=%u nextino=%lu\n",
949 inode->i_ino, iblock, firstext, (unsigned) sect_size, nextino);
956 (*bh_result)->b_dev = inode->i_dev;
957 (*bh_result)->b_blocknr = firstext + b_off - offset;
958 (*bh_result)->b_state |= (1UL << BH_Mapped);
960 *bh_result = sb_getblk(inode->i_sb, firstext+b_off-offset);
964 bh_result++; /* Next buffer head */
965 b_off++; /* Next buffer offset */
977 * Used by the standard interfaces.
979 static int isofs_get_block(struct inode *inode, long iblock,
980 struct buffer_head *bh_result, int create)
983 printk("isofs_get_block: Kernel tries to allocate a block\n");
987 return isofs_get_blocks(inode, iblock, &bh_result, 1) ? 0 : -EIO;
990 static int isofs_bmap(struct inode *inode, int block)
992 struct buffer_head dummy;
996 dummy.b_blocknr = -1000;
997 error = isofs_get_block(inode, block, &dummy, 0);
999 return dummy.b_blocknr;
1003 struct buffer_head *isofs_bread(struct inode *inode, unsigned int block)
1005 unsigned int blknr = isofs_bmap(inode, block);
1008 return sb_bread(inode->i_sb, blknr);
1011 static int isofs_readpage(struct file *file, struct page *page)
1013 return block_read_full_page(page,isofs_get_block);
1016 static int _isofs_bmap(struct address_space *mapping, long block)
1018 return generic_block_bmap(mapping,block,isofs_get_block);
1021 static struct address_space_operations isofs_aops = {
1022 readpage: isofs_readpage,
1023 sync_page: block_sync_page,
1027 static inline void test_and_set_uid(uid_t *p, uid_t value)
1034 static inline void test_and_set_gid(gid_t *p, gid_t value)
1041 static int isofs_read_level3_size(struct inode * inode)
1043 unsigned long f_pos = inode->i_ino;
1044 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
1045 int high_sierra = inode->i_sb->u.isofs_sb.s_high_sierra;
1046 struct buffer_head * bh = NULL;
1047 unsigned long block, offset;
1049 int more_entries = 0;
1050 struct iso_directory_record * tmpde = NULL;
1053 inode->u.isofs_i.i_next_section_ino = 0;
1055 block = f_pos >> ISOFS_BUFFER_BITS(inode);
1056 offset = f_pos & (bufsize-1);
1059 struct iso_directory_record * de;
1060 unsigned int de_len;
1063 bh = sb_bread(inode->i_sb, block);
1067 de = (struct iso_directory_record *) (bh->b_data + offset);
1068 de_len = *(unsigned char *) de;
1073 f_pos = (f_pos + ISOFS_BLOCK_SIZE) & ~(ISOFS_BLOCK_SIZE - 1);
1074 block = f_pos >> ISOFS_BUFFER_BITS(inode);
1081 /* Make sure we have a full directory entry */
1082 if (offset >= bufsize) {
1083 int slop = bufsize - offset + de_len;
1085 tmpde = kmalloc(256, GFP_KERNEL);
1089 memcpy(tmpde, de, slop);
1090 offset &= bufsize - 1;
1095 bh = sb_bread(inode->i_sb, block);
1098 memcpy((void *) tmpde + slop, bh->b_data, offset);
1103 inode->i_size += isonum_733(de->size);
1105 inode->u.isofs_i.i_next_section_ino = f_pos;
1107 more_entries = de->flags[-high_sierra] & 0x80;
1113 } while(more_entries);
1127 printk(KERN_INFO "ISOFS: unable to read i-node block %lu\n", block);
1133 printk(KERN_INFO "isofs_read_level3_size: "
1134 "More than 100 file sections ?!?, aborting...\n"
1135 "isofs_read_level3_size: inode=%lu ino=%lu\n",
1136 inode->i_ino, f_pos);
1140 static void isofs_read_inode(struct inode * inode)
1142 struct super_block *sb = inode->i_sb;
1143 unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
1144 int block = inode->i_ino >> ISOFS_BUFFER_BITS(inode);
1145 int high_sierra = sb->u.isofs_sb.s_high_sierra;
1146 struct buffer_head * bh = NULL;
1147 struct iso_directory_record * de;
1148 struct iso_directory_record * tmpde = NULL;
1149 unsigned int de_len;
1150 unsigned long offset;
1151 int volume_seq_no, i;
1153 bh = sb_bread(inode->i_sb, block);
1157 offset = (inode->i_ino & (bufsize - 1));
1158 de = (struct iso_directory_record *) (bh->b_data + offset);
1159 de_len = *(unsigned char *) de;
1161 if (offset + de_len > bufsize) {
1162 int frag1 = bufsize - offset;
1164 tmpde = kmalloc(de_len, GFP_KERNEL);
1165 if (tmpde == NULL) {
1166 printk(KERN_INFO "isofs_read_inode: out of memory\n");
1169 memcpy(tmpde, bh->b_data + offset, frag1);
1171 bh = sb_bread(inode->i_sb, ++block);
1174 memcpy((char *)tmpde+frag1, bh->b_data, de_len - frag1);
1178 /* Assume it is a normal-format file unless told otherwise */
1179 inode->u.isofs_i.i_file_format = isofs_file_normal;
1181 if (de->flags[-high_sierra] & 2) {
1182 inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
1183 inode->i_nlink = 1; /* Set to 1. We know there are 2, but
1184 the find utility tries to optimize
1185 if it is 2, and it screws up. It is
1186 easier to give 1 which tells find to
1187 do it the hard way. */
1189 /* Everybody gets to read the file. */
1190 inode->i_mode = inode->i_sb->u.isofs_sb.s_mode;
1192 inode->i_mode |= S_IFREG;
1193 /* If there are no periods in the name,
1194 * then set the execute permission bit
1196 for(i=0; i< de->name_len[0]; i++)
1197 if(de->name[i]=='.' || de->name[i]==';')
1199 if(i == de->name_len[0] || de->name[i] == ';')
1200 inode->i_mode |= S_IXUGO; /* execute permission */
1202 inode->i_uid = inode->i_sb->u.isofs_sb.s_uid;
1203 inode->i_gid = inode->i_sb->u.isofs_sb.s_gid;
1204 inode->i_blocks = inode->i_blksize = 0;
1207 inode->u.isofs_i.i_section_size = isonum_733 (de->size);
1208 if(de->flags[-high_sierra] & 0x80) {
1209 if(isofs_read_level3_size(inode)) goto fail;
1211 inode->i_size = isonum_733 (de->size);
1215 * The ISO-9660 filesystem only stores 32 bits for file size.
1216 * mkisofs handles files up to 2GB-2 = 2147483646 = 0x7FFFFFFE bytes
1217 * in size. This is according to the large file summit paper from 1996.
1218 * WARNING: ISO-9660 filesystems > 1 GB and even > 2 GB are fully
1219 * legal. Do not prevent to use DVD's schilling@fokus.gmd.de
1221 if ((inode->i_size < 0 || inode->i_size > 0x7FFFFFFE) &&
1222 inode->i_sb->u.isofs_sb.s_cruft == 'n') {
1223 printk(KERN_WARNING "Warning: defective CD-ROM. "
1224 "Enabling \"cruft\" mount option.\n");
1225 inode->i_sb->u.isofs_sb.s_cruft = 'y';
1229 * Some dipshit decided to store some other bit of information
1230 * in the high byte of the file length. Catch this and holler.
1231 * WARNING: this will make it impossible for a file to be > 16MB
1235 if (inode->i_sb->u.isofs_sb.s_cruft == 'y' &&
1236 inode->i_size & 0xff000000) {
1237 inode->i_size &= 0x00ffffff;
1240 if (de->interleave[0]) {
1241 printk("Interleaved files not (yet) supported.\n");
1245 /* I have no idea what file_unit_size is used for, so
1246 we will flag it for now */
1247 if (de->file_unit_size[0] != 0) {
1248 printk("File unit size != 0 for ISO file (%ld).\n",
1252 /* I have no idea what other flag bits are used for, so
1253 we will flag it for now */
1255 if((de->flags[-high_sierra] & ~2)!= 0){
1256 printk("Unusual flag settings for ISO file (%ld %x).\n",
1257 inode->i_ino, de->flags[-high_sierra]);
1261 inode->i_mtime = inode->i_atime = inode->i_ctime =
1262 iso_date(de->date, high_sierra);
1264 inode->u.isofs_i.i_first_extent = (isonum_733 (de->extent) +
1265 isonum_711 (de->ext_attr_length));
1267 /* Set the number of blocks for stat() - should be done before RR */
1268 inode->i_blksize = PAGE_CACHE_SIZE; /* For stat() only */
1269 inode->i_blocks = (inode->i_size + 511) >> 9;
1272 * Now test for possible Rock Ridge extensions which will override
1273 * some of these numbers in the inode structure.
1277 parse_rock_ridge_inode(de, inode);
1278 /* if we want uid/gid set, override the rock ridge setting */
1279 test_and_set_uid(&inode->i_uid, inode->i_sb->u.isofs_sb.s_uid);
1280 test_and_set_gid(&inode->i_gid, inode->i_sb->u.isofs_sb.s_gid);
1283 /* get the volume sequence number */
1284 volume_seq_no = isonum_723 (de->volume_sequence_number) ;
1287 * Multi volume means tagging a group of CDs with info in their headers.
1288 * All CDs of a group must share the same vol set name and vol set size
1289 * and have different vol set seq num. Deciding that data is wrong based
1290 * in that three fields is wrong. The fields are informative, for
1291 * cataloging purposes in a big jukebox, ie. Read sections 4.17, 4.18, 6.6
1292 * of ftp://ftp.ecma.ch/ecma-st/Ecma-119.pdf (ECMA 119 2nd Ed = ISO 9660)
1294 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
1296 * Disable checking if we see any volume number other than 0 or 1.
1297 * We could use the cruft option, but that has multiple purposes, one
1298 * of which is limiting the file size to 16Mb. Thus we silently allow
1299 * volume numbers of 0 to go through without complaining.
1301 if (inode->i_sb->u.isofs_sb.s_cruft == 'n' &&
1302 (volume_seq_no != 0) && (volume_seq_no != 1)) {
1303 printk(KERN_WARNING "Warning: defective CD-ROM "
1304 "(volume sequence number %d). "
1305 "Enabling \"cruft\" mount option.\n", volume_seq_no);
1306 inode->i_sb->u.isofs_sb.s_cruft = 'y';
1308 #endif /*IGNORE_WRONG_MULTI_VOLUME_SPECS */
1310 /* Install the inode operations vector */
1311 #ifndef IGNORE_WRONG_MULTI_VOLUME_SPECS
1312 if (inode->i_sb->u.isofs_sb.s_cruft != 'y' &&
1313 (volume_seq_no != 0) && (volume_seq_no != 1)) {
1314 printk(KERN_WARNING "Multi-volume CD somehow got mounted.\n");
1316 #endif /*IGNORE_WRONG_MULTI_VOLUME_SPECS */
1318 if (S_ISREG(inode->i_mode)) {
1319 inode->i_fop = &generic_ro_fops;
1320 switch ( inode->u.isofs_i.i_file_format ) {
1321 #ifdef CONFIG_ZISOFS
1322 case isofs_file_compressed:
1323 inode->i_data.a_ops = &zisofs_aops;
1327 inode->i_data.a_ops = &isofs_aops;
1330 } else if (S_ISDIR(inode->i_mode)) {
1331 inode->i_op = &isofs_dir_inode_operations;
1332 inode->i_fop = &isofs_dir_operations;
1333 } else if (S_ISLNK(inode->i_mode)) {
1334 inode->i_op = &page_symlink_inode_operations;
1335 inode->i_data.a_ops = &isofs_symlink_aops;
1337 /* XXX - parse_rock_ridge_inode() had already set i_rdev. */
1338 init_special_inode(inode, inode->i_mode,
1339 kdev_t_to_nr(inode->i_rdev));
1349 printk(KERN_WARNING "ISOFS: unable to read i-node block\n");
1351 make_bad_inode(inode);
1361 void * leak_check_malloc(unsigned int size){
1364 tmp = kmalloc(size, GFP_KERNEL);
1368 void leak_check_free_s(void * obj, int size){
1373 struct buffer_head * leak_check_bread(struct super_block *sb, int block){
1375 return sb_bread(sb, block);
1378 void leak_check_brelse(struct buffer_head * bh){
1385 static DECLARE_FSTYPE_DEV(iso9660_fs_type, "iso9660", isofs_read_super);
1387 static int __init init_iso9660_fs(void)
1389 #ifdef CONFIG_ZISOFS
1392 err = zisofs_init();
1396 return register_filesystem(&iso9660_fs_type);
1399 static void __exit exit_iso9660_fs(void)
1401 unregister_filesystem(&iso9660_fs_type);
1402 #ifdef CONFIG_ZISOFS
1409 module_init(init_iso9660_fs)
1410 module_exit(exit_iso9660_fs)
1411 MODULE_LICENSE("GPL");