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