PageRenderTime 114ms CodeModel.GetById 13ms app.highlight 89ms RepoModel.GetById 1ms app.codeStats 1ms

/filesystems/unixfs/ufs/ufs.c

http://macfuse.googlecode.com/
C | 1698 lines | 1319 code | 335 blank | 44 comment | 197 complexity | 7723892cb9dd127fdab29bb22c63f2d1 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * UFS for MacFUSE
   3 * Amit Singh
   4 * http://osxbook.com
   5 *
   6 * Most of the code in this file comes from the Linux kernel implementation
   7 * of UFS. See fs/ufs/ in the Linux kernel source tree.
   8 *
   9 * The code is Copyright (c) its various authors. It is covered by the
  10 * GNU GENERAL PUBLIC LICENSE Version 2.
  11 */
  12 
  13#include "ufs.h"
  14
  15#include <errno.h>
  16#include <fcntl.h>
  17#include <stdio.h>
  18#include <stdlib.h>
  19#include <string.h>
  20#include <unistd.h>
  21#include <sys/ioctl.h>
  22#include <sys/stat.h>
  23
  24#include <ufs/util.c>
  25
  26static ino_t ufs_find_entry_s(struct inode *dir, const char* name);
  27static int ufs_get_dirpage(struct inode *inode, sector_t index, char *pagebuf);
  28static int ufs_read_cylinder_structures(struct super_block *sb);
  29static void ufs_print_cylinder_stuff(struct super_block* sb,
  30                                     struct ufs_cylinder_group* cg);
  31static void ufs_setup_cstotal(struct super_block* sb);
  32static int ufs_read_cylinder_structures(struct super_block* sb);
  33static u64 ufs_frag_map(struct inode *inode, sector_t frag, int* error);
  34int ufs_getfrag_block(struct inode* inode, sector_t fragment,
  35                      struct buffer_head* bh_result, int create);
  36static struct buffer_head* ufs_getfrag(struct inode* inode,
  37                                       unsigned int fragment,
  38                                       int create, int* err);
  39struct buffer_head* ufs_bread(struct inode* inode, unsigned fragment,
  40                              int create, int* err);
  41static int ufs1_read_inode(struct inode* inode, struct ufs_inode* ufs_inode);
  42static int ufs2_read_inode(struct inode* inode, struct ufs2_inode* ufs2_inode);
  43static unsigned ufs_last_byte(struct inode* inode, unsigned long page_nr);
  44static int ufs_get_dirpage(struct inode* inode, sector_t index, char* pagebuf);
  45static ino_t ufs_find_entry_s(struct inode* dir, const char* name);
  46
  47#define UFS_USED __attribute__((used))
  48static struct ufs_dir_entry*
  49    ufs_dotdot_s(struct inode* dir, char* pagebuf) UFS_USED;
  50static int
  51    ufs_check_page(struct inode* dir, sector_t index, char* page) UFS_USED;
  52static void
  53    ufs_print_super_stuff(struct super_block* sb,
  54        struct ufs_super_block_first*  usb1,
  55        struct ufs_super_block_second* usb2,
  56        struct ufs_super_block_third*  usb3) UFS_USED;
  57
  58enum {
  59   Opt_type_old        = UFS_MOUNT_UFSTYPE_OLD,
  60   Opt_type_sunx86     = UFS_MOUNT_UFSTYPE_SUNx86,
  61   Opt_type_sun        = UFS_MOUNT_UFSTYPE_SUN,
  62   Opt_type_sunos      = UFS_MOUNT_UFSTYPE_SUNOS,
  63   Opt_type_44bsd      = UFS_MOUNT_UFSTYPE_44BSD,
  64   Opt_type_ufs2       = UFS_MOUNT_UFSTYPE_UFS2,
  65   Opt_type_hp         = UFS_MOUNT_UFSTYPE_HP,
  66   Opt_type_nextstepcd = UFS_MOUNT_UFSTYPE_NEXTSTEP_CD,
  67   Opt_type_nextstep   = UFS_MOUNT_UFSTYPE_NEXTSTEP,
  68   Opt_type_openstep   = UFS_MOUNT_UFSTYPE_OPENSTEP,
  69   Opt_onerror_panic   = UFS_MOUNT_ONERROR_PANIC,
  70   Opt_onerror_lock    = UFS_MOUNT_ONERROR_LOCK,
  71   Opt_onerror_umount  = UFS_MOUNT_ONERROR_UMOUNT,
  72   Opt_onerror_repair  = UFS_MOUNT_ONERROR_REPAIR,
  73   Opt_err
  74};
  75
  76static match_table_t tokens __attribute__((used)) = {
  77    { Opt_type_old,        "old"         },
  78    { Opt_type_sunx86,     "sunx86"      },
  79    { Opt_type_sun,        "sun"         },
  80    { Opt_type_sunos,      "sunos"       },
  81    { Opt_type_44bsd,      "44bsd"       },
  82    { Opt_type_ufs2,       "ufs2"        },
  83    { Opt_type_ufs2,       "5xbsd"       },
  84    { Opt_type_hp,         "hp"          },
  85    { Opt_type_nextstepcd, "nextstep-cd" },
  86    { Opt_type_nextstep,   "nextstep"    },
  87    { Opt_type_openstep,   "openstep"    },
  88
  89    /* end of possible ufs types */
  90    { Opt_onerror_panic,   "onerror=panic"  },
  91    { Opt_onerror_lock,    "onerror=lock"   },
  92    { Opt_onerror_umount,  "onerror=umount" },
  93    { Opt_onerror_repair,  "onerror=repair" },
  94    { Opt_err, NULL}
  95};
  96
  97static void
  98ufs_print_super_stuff(struct super_block* sb,
  99                      struct ufs_super_block_first*  usb1,
 100                      struct ufs_super_block_second* usb2,
 101                      struct ufs_super_block_third*  usb3)
 102{
 103    u32 magic = fs32_to_cpu(sb, usb3->fs_magic);
 104
 105    printk("ufs_print_super_stuff\n");
 106    printk("  magic:     0x%x\n", magic);
 107    if (fs32_to_cpu(sb, usb3->fs_magic) == UFS2_MAGIC) {
 108        printk("  fs_size:   %llu\n", (unsigned long long)
 109               fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size));
 110        printk("  fs_dsize:  %llu\n", (unsigned long long)
 111               fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize));
 112        printk("  bsize:         %u\n",
 113               fs32_to_cpu(sb, usb1->fs_bsize));
 114        printk("  fsize:         %u\n",
 115               fs32_to_cpu(sb, usb1->fs_fsize));
 116        printk("  fs_volname:  %s\n", usb2->fs_un.fs_u2.fs_volname);
 117        printk("  fs_sblockloc: %llu\n", (unsigned long long)
 118               fs64_to_cpu(sb, usb2->fs_un.fs_u2.fs_sblockloc));
 119        printk("  cs_ndir(No of dirs):  %llu\n", (unsigned long long)
 120               fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir));
 121        printk("  cs_nbfree(No of free blocks):  %llu\n",
 122               (unsigned long long)
 123               fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree));
 124        printk(KERN_INFO"  cs_nifree(Num of free inodes): %llu\n",
 125               (unsigned long long)
 126               fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree));
 127        printk(KERN_INFO"  cs_nffree(Num of free frags): %llu\n",
 128               (unsigned long long)
 129               fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree));
 130        printk(KERN_INFO"  fs_maxsymlinklen: %u\n",
 131               fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen));
 132    } else {
 133        printk(" sblkno:      %u\n", fs32_to_cpu(sb, usb1->fs_sblkno));
 134        printk(" cblkno:      %u\n", fs32_to_cpu(sb, usb1->fs_cblkno));
 135        printk(" iblkno:      %u\n", fs32_to_cpu(sb, usb1->fs_iblkno));
 136        printk(" dblkno:      %u\n", fs32_to_cpu(sb, usb1->fs_dblkno));
 137        printk(" cgoffset:    %u\n",
 138               fs32_to_cpu(sb, usb1->fs_cgoffset));
 139        printk(" ~cgmask:     0x%x\n",
 140               ~fs32_to_cpu(sb, usb1->fs_cgmask));
 141        printk(" size:        %u\n", fs32_to_cpu(sb, usb1->fs_size));
 142        printk(" dsize:       %u\n", fs32_to_cpu(sb, usb1->fs_dsize));
 143        printk(" ncg:         %u\n", fs32_to_cpu(sb, usb1->fs_ncg));
 144        printk(" bsize:       %u\n", fs32_to_cpu(sb, usb1->fs_bsize));
 145        printk(" fsize:       %u\n", fs32_to_cpu(sb, usb1->fs_fsize));
 146        printk(" frag:        %u\n", fs32_to_cpu(sb, usb1->fs_frag));
 147        printk(" fragshift:   %u\n",
 148               fs32_to_cpu(sb, usb1->fs_fragshift));
 149        printk(" ~fmask:      %u\n", ~fs32_to_cpu(sb, usb1->fs_fmask));
 150        printk(" fshift:      %u\n", fs32_to_cpu(sb, usb1->fs_fshift));
 151        printk(" sbsize:      %u\n", fs32_to_cpu(sb, usb1->fs_sbsize));
 152        printk(" spc:         %u\n", fs32_to_cpu(sb, usb1->fs_spc));
 153        printk(" cpg:         %u\n", fs32_to_cpu(sb, usb1->fs_cpg));
 154        printk(" ipg:         %u\n", fs32_to_cpu(sb, usb1->fs_ipg));
 155        printk(" fpg:         %u\n", fs32_to_cpu(sb, usb1->fs_fpg));
 156        printk(" csaddr:      %u\n", fs32_to_cpu(sb, usb1->fs_csaddr));
 157        printk(" cssize:      %u\n", fs32_to_cpu(sb, usb1->fs_cssize));
 158        printk(" cgsize:      %u\n", fs32_to_cpu(sb, usb1->fs_cgsize));
 159        printk(" fstodb:      %u\n",
 160               fs32_to_cpu(sb, usb1->fs_fsbtodb));
 161        printk(" nrpos:       %u\n", fs32_to_cpu(sb, usb3->fs_nrpos));
 162        printk(" ndir         %u\n",
 163               fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir));
 164        printk(" nifree       %u\n",
 165               fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree));
 166        printk(" nbfree       %u\n",
 167               fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree));
 168        printk(" nffree       %u\n",
 169               fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree));
 170    }
 171    printk("\n");
 172}
 173
 174static void
 175ufs_print_cylinder_stuff(struct super_block* sb, struct ufs_cylinder_group* cg)
 176{
 177    printk("\nufs_print_cylinder_stuff\n");
 178    printk("size of ucg: %zu\n", sizeof(struct ufs_cylinder_group));
 179    printk("  magic:        %x\n", fs32_to_cpu(sb, cg->cg_magic));
 180    printk("  time:         %u\n", fs32_to_cpu(sb, cg->cg_time));
 181    printk("  cgx:          %u\n", fs32_to_cpu(sb, cg->cg_cgx));
 182    printk("  ncyl:         %u\n", fs16_to_cpu(sb, cg->cg_ncyl));
 183    printk("  niblk:        %u\n", fs16_to_cpu(sb, cg->cg_niblk));
 184    printk("  ndblk:        %u\n", fs32_to_cpu(sb, cg->cg_ndblk));
 185    printk("  cs_ndir:      %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_ndir));
 186    printk("  cs_nbfree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nbfree));
 187    printk("  cs_nifree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nifree));
 188    printk("  cs_nffree:    %u\n", fs32_to_cpu(sb, cg->cg_cs.cs_nffree));
 189    printk("  rotor:        %u\n", fs32_to_cpu(sb, cg->cg_rotor));
 190    printk("  frotor:       %u\n", fs32_to_cpu(sb, cg->cg_frotor));
 191    printk("  irotor:       %u\n", fs32_to_cpu(sb, cg->cg_irotor));
 192    printk("  frsum:        %u, %u, %u, %u, %u, %u, %u, %u\n",
 193           fs32_to_cpu(sb, cg->cg_frsum[0]), fs32_to_cpu(sb, cg->cg_frsum[1]),
 194           fs32_to_cpu(sb, cg->cg_frsum[2]), fs32_to_cpu(sb, cg->cg_frsum[3]),
 195           fs32_to_cpu(sb, cg->cg_frsum[4]), fs32_to_cpu(sb, cg->cg_frsum[5]),
 196           fs32_to_cpu(sb, cg->cg_frsum[6]), fs32_to_cpu(sb, cg->cg_frsum[7]));
 197    printk("  btotoff:      %u\n", fs32_to_cpu(sb, cg->cg_btotoff));
 198    printk("  boff:         %u\n", fs32_to_cpu(sb, cg->cg_boff));
 199    printk("  iuseoff:      %u\n", fs32_to_cpu(sb, cg->cg_iusedoff));
 200    printk("  freeoff:      %u\n", fs32_to_cpu(sb, cg->cg_freeoff));
 201    printk("  nextfreeoff:  %u\n", fs32_to_cpu(sb, cg->cg_nextfreeoff));
 202    printk("  clustersumoff %u\n",
 203           fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clustersumoff));
 204    printk("  clusteroff    %u\n",
 205           fs32_to_cpu(sb, cg->cg_u.cg_44.cg_clusteroff));
 206    printk("  nclusterblks  %u\n",
 207           fs32_to_cpu(sb, cg->cg_u.cg_44.cg_nclusterblks));
 208    printk("\n");
 209}
 210
 211static int
 212ufs_parse_options(char* options, unsigned* mount_options)
 213{
 214    char* p;
 215    
 216    UFSD("ENTER\n");
 217    
 218    if (!options)
 219        return 1;
 220
 221    while ((p = strsep(&options, ",")) != NULL) {
 222
 223        substring_t args[MAX_OPT_ARGS];
 224        int token;
 225        if (!*p)
 226            continue;
 227
 228        token = match_token(p, tokens, args);
 229
 230        switch (token) {
 231        case Opt_type_old:
 232            ufs_clear_opt(*mount_options, UFSTYPE);
 233            ufs_set_opt(*mount_options, UFSTYPE_OLD);
 234            break;
 235
 236        case Opt_type_sunx86:
 237            ufs_clear_opt(*mount_options, UFSTYPE);
 238            ufs_set_opt(*mount_options, UFSTYPE_SUNx86);
 239            break;
 240
 241        case Opt_type_sun:
 242            ufs_clear_opt(*mount_options, UFSTYPE);
 243            ufs_set_opt(*mount_options, UFSTYPE_SUN);
 244            break;
 245
 246        case Opt_type_sunos:
 247            ufs_clear_opt(*mount_options, UFSTYPE);
 248            ufs_set_opt(*mount_options, UFSTYPE_SUNOS);
 249            break;
 250
 251        case Opt_type_44bsd:
 252            ufs_clear_opt(*mount_options, UFSTYPE);
 253            ufs_set_opt(*mount_options, UFSTYPE_44BSD);
 254            break;
 255
 256        case Opt_type_ufs2:
 257            ufs_clear_opt(*mount_options, UFSTYPE);
 258            ufs_set_opt(*mount_options, UFSTYPE_UFS2);
 259            break;
 260
 261        case Opt_type_hp:
 262            ufs_clear_opt(*mount_options, UFSTYPE);
 263            ufs_set_opt(*mount_options, UFSTYPE_HP);
 264            break;
 265
 266        case Opt_type_nextstepcd:
 267            ufs_clear_opt(*mount_options, UFSTYPE);
 268            ufs_set_opt(*mount_options, UFSTYPE_NEXTSTEP_CD);
 269            break;
 270
 271        case Opt_type_nextstep:
 272            ufs_clear_opt(*mount_options, UFSTYPE);
 273            ufs_set_opt(*mount_options, UFSTYPE_NEXTSTEP);
 274            break;
 275
 276        case Opt_type_openstep:
 277            ufs_clear_opt(*mount_options, UFSTYPE);
 278            ufs_set_opt(*mount_options, UFSTYPE_OPENSTEP);
 279            break;
 280
 281        case Opt_onerror_panic:
 282            ufs_clear_opt(*mount_options, ONERROR);
 283            ufs_set_opt(*mount_options, ONERROR_PANIC);
 284            break;
 285
 286        case Opt_onerror_lock:
 287            ufs_clear_opt(*mount_options, ONERROR);
 288            ufs_set_opt(*mount_options, ONERROR_LOCK);
 289            break;
 290
 291        case Opt_onerror_umount:
 292            ufs_clear_opt(*mount_options, ONERROR);
 293            ufs_set_opt(*mount_options, ONERROR_UMOUNT);
 294            break;
 295
 296        case Opt_onerror_repair:
 297            printk("UFS-fs: Unable to do repair on error, "
 298                   "will lock lock instead\n");
 299            ufs_clear_opt(*mount_options, ONERROR);
 300            ufs_set_opt(*mount_options, ONERROR_REPAIR);
 301            break;
 302
 303        default:
 304            printk("UFS-fs: Invalid option: \"%s\" or missing value\n", p);
 305            return 0;
 306        }
 307    }
 308
 309    return 1;
 310}
 311
 312static void
 313ufs_setup_cstotal(struct super_block* sb)
 314{
 315    struct ufs_sb_info* sbi = UFS_SB(sb);
 316    struct ufs_sb_private_info* uspi = sbi->s_uspi;
 317
 318    struct ufs_super_block_first*  usb1;
 319    struct ufs_super_block_second* usb2;
 320    struct ufs_super_block_third*  usb3;
 321
 322    unsigned mtype = sbi->s_mount_opt & UFS_MOUNT_UFSTYPE;
 323
 324    UFSD("ENTER, mtype=%u\n", mtype);
 325
 326    usb1 = ubh_get_usb_first(uspi);
 327    usb2 = ubh_get_usb_second(uspi);
 328    usb3 = ubh_get_usb_third(uspi);
 329
 330    if ((mtype == UFS_MOUNT_UFSTYPE_44BSD &&
 331        (usb1->fs_flags & UFS_FLAGS_UPDATED)) ||
 332        (mtype == UFS_MOUNT_UFSTYPE_UFS2)) {
 333
 334        uspi->cs_total.cs_ndir = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_ndir);
 335        uspi->cs_total.cs_nbfree = fs64_to_cpu(sb, usb2->fs_un.fs_u2.cs_nbfree);
 336        uspi->cs_total.cs_nifree =
 337            fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nifree);
 338        uspi->cs_total.cs_nffree =
 339            fs64_to_cpu(sb, usb3->fs_un1.fs_u2.cs_nffree);
 340    } else {
 341        uspi->cs_total.cs_ndir = fs32_to_cpu(sb, usb1->fs_cstotal.cs_ndir);
 342        uspi->cs_total.cs_nbfree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nbfree);
 343        uspi->cs_total.cs_nifree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nifree);
 344        uspi->cs_total.cs_nffree = fs32_to_cpu(sb, usb1->fs_cstotal.cs_nffree);
 345    }
 346    UFSD("EXIT\n");
 347}
 348
 349static int
 350ufs_read_cylinder_structures(struct super_block* sb)
 351{
 352    struct ufs_sb_info* sbi = UFS_SB(sb);
 353    struct ufs_sb_private_info* uspi = sbi->s_uspi;
 354
 355    struct ufs_buffer_head* ubh;
 356    unsigned char* base;
 357    unsigned char* space;
 358    unsigned size, blks, i;
 359    struct ufs_super_block_third* usb3;
 360
 361    UFSD("ENTER\n");
 362
 363    usb3 = ubh_get_usb_third(uspi);
 364
 365    /* Read cs structures from (usually) first data block on the device. */
 366
 367    size = uspi->s_cssize;
 368    blks = (size + uspi->s_fsize - 1) >> uspi->s_fshift;
 369    base = space = kmalloc(size, GFP_KERNEL);
 370    if (!base)
 371        goto failed; 
 372
 373    sbi->s_csp = (struct ufs_csum*)space;
 374
 375    for (i = 0; i < blks; i += uspi->s_fpb) {
 376        size = uspi->s_bsize;
 377        if (i + uspi->s_fpb > blks)
 378            size = (blks - i) * uspi->s_fsize;
 379
 380        ubh = ubh_bread(sb, uspi->s_csaddr + i, size);
 381        
 382        if (!ubh)
 383            goto failed;
 384
 385        ubh_ubhcpymem (space, ubh, size);
 386
 387        space += size;
 388        ubh_brelse (ubh);
 389        ubh = NULL;
 390    }
 391
 392    /*
 393     * Read cylinder group (we read only first fragment from block
 394     * at this time) and prepare internal data structures for cg caching.
 395     */
 396
 397    if (!(sbi->s_ucg = kmalloc(sizeof(struct buffer_head*) * uspi->s_ncg,
 398        GFP_KERNEL)))
 399        goto failed;
 400
 401    for (i = 0; i < uspi->s_ncg; i++) 
 402        sbi->s_ucg[i] = NULL;
 403
 404    for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
 405        sbi->s_ucpi[i] = NULL;
 406        sbi->s_cgno[i] = UFS_CGNO_EMPTY;
 407    }
 408
 409    for (i = 0; i < uspi->s_ncg; i++) {
 410        UFSD("read cg %u\n", i);
 411        if (!(sbi->s_ucg[i] = sb_bread(sb, ufs_cgcmin(i))))
 412            goto failed;
 413        if (!ufs_cg_chkmagic(sb,
 414            (struct ufs_cylinder_group*)sbi->s_ucg[i]->b_data))
 415            goto failed;
 416
 417        ufs_print_cylinder_stuff(sb,
 418            (struct ufs_cylinder_group*)sbi->s_ucg[i]->b_data);
 419    }
 420
 421    for (i = 0; i < UFS_MAX_GROUP_LOADED; i++) {
 422        if (!(sbi->s_ucpi[i] = kmalloc(sizeof(struct ufs_cg_private_info),
 423                                       GFP_KERNEL)))
 424            goto failed;
 425        sbi->s_cgno[i] = UFS_CGNO_EMPTY;
 426    }
 427
 428    sbi->s_cg_loaded = 0;
 429
 430    UFSD("EXIT\n");
 431
 432    return 1;
 433
 434failed:
 435    kfree(base);
 436
 437    if (sbi->s_ucg) {
 438        for (i = 0; i < uspi->s_ncg; i++)
 439            if (sbi->s_ucg[i])
 440                brelse (sbi->s_ucg[i]);
 441        kfree (sbi->s_ucg);
 442        for (i = 0; i < UFS_MAX_GROUP_LOADED; i++)
 443            kfree (sbi->s_ucpi[i]);
 444    }
 445
 446    UFSD("EXIT (FAILED)\n");
 447
 448    return 0;
 449}
 450
 451static int
 452ufs_block_to_path(struct inode* inode, sector_t i_block, sector_t offsets[4])
 453{
 454    struct ufs_sb_private_info* uspi = UFS_SB(inode->I_sb)->s_uspi;
 455
 456    int ptrs = uspi->s_apb;
 457    int ptrs_bits = uspi->s_apbshift;
 458    const long direct_blocks = UFS_NDADDR,
 459    indirect_blocks = ptrs,
 460    double_blocks = (1 << (ptrs_bits * 2));
 461    int n = 0;
 462
 463    UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
 464
 465    if (i_block < 0) {
 466        fprintf(stderr, "ufs_block_to_path: block < 0\n");
 467    } else if (i_block < direct_blocks) {
 468        offsets[n++] = i_block;
 469    } else if ((i_block -= direct_blocks) < indirect_blocks) {
 470        offsets[n++] = UFS_IND_BLOCK;
 471        offsets[n++] = i_block;
 472    } else if ((i_block -= indirect_blocks) < double_blocks) {
 473        offsets[n++] = UFS_DIND_BLOCK;
 474        offsets[n++] = i_block >> ptrs_bits;
 475        offsets[n++] = i_block & (ptrs - 1);
 476    } else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
 477        offsets[n++] = UFS_TIND_BLOCK;
 478        offsets[n++] = i_block >> (ptrs_bits * 2);
 479        offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
 480        offsets[n++] = i_block & (ptrs - 1);
 481    } else {
 482         fprintf(stderr, "ufs_block_to_path: block > big\n");
 483    }
 484
 485    return n;
 486}
 487
 488/* Returns the location of the fragment from the begining of the filesystem. */
 489
 490static u64
 491ufs_frag_map(struct inode* inode, sector_t frag, int* error)
 492{
 493    struct ufs_inode_info* ufsi = inode->I_private;
 494    struct super_block* sb = inode->I_sb;
 495    struct ufs_sb_private_info* uspi = UFS_SB(sb)->s_uspi;
 496
 497    u64 mask = (u64)uspi->s_apbmask>>uspi->s_fpbshift;
 498    int shift = uspi->s_apbshift-uspi->s_fpbshift;
 499    unsigned flags = UFS_SB(sb)->s_flags;
 500
 501    sector_t offsets[4];
 502    sector_t* p;
 503
 504    int depth = ufs_block_to_path(inode, frag >> uspi->s_fpbshift, offsets);
 505
 506    u64 ret = 0L;
 507    u64 temp = 0L;
 508
 509    __fs32 block;
 510    __fs64 u2_block = 0L;
 511
 512    UFSD(": frag = %llu  depth = %d\n", (unsigned long long)frag, depth);
 513    UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
 514         uspi->s_fpbshift, uspi->s_apbmask, (unsigned long long)mask);
 515
 516    if (depth == 0)
 517        return 0;
 518
 519    p = offsets;
 520
 521    lock_kernel();
 522
 523    if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
 524        goto ufs2;
 525
 526    block = ufsi->i_u1.i_data[*p++];
 527    if (!block)
 528        goto out;
 529
 530    while (--depth) {
 531
 532        struct buffer_head _bh;
 533        struct buffer_head* bh = &_bh;
 534        sector_t n = *p++;
 535
 536        bh->b_flags.dynamic = 0;
 537
 538        if (sb_bread_intobh(sb, uspi->s_sbbase +
 539                            fs32_to_cpu(sb, block) + (n >> shift), bh) != 0)
 540            goto out;
 541
 542        block = ((__fs32 *) bh->b_data)[n & mask];
 543
 544        brelse (bh);
 545
 546        if (!block)
 547            goto out;
 548    }
 549
 550    ret = (u64)(uspi->s_sbbase + fs32_to_cpu(sb, block) +
 551                (frag & uspi->s_fpbmask));
 552
 553    goto out;
 554
 555ufs2:
 556
 557    u2_block = ufsi->i_u1.u2_i_data[*p++];
 558    if (!u2_block)
 559        goto out;
 560
 561    while (--depth) {
 562
 563        struct buffer_head _bh;
 564        struct buffer_head* bh = &_bh;
 565        sector_t n = *p++;
 566
 567        temp = (u64)(uspi->s_sbbase) + fs64_to_cpu(sb, u2_block);
 568
 569        bh->b_flags.dynamic = 0;
 570        if (sb_bread_intobh(sb, temp +(u64) (n>>shift), bh) != 0)
 571            goto out;
 572
 573        u2_block = ((__fs64 *)bh->b_data)[n & mask];
 574
 575        brelse(bh);
 576
 577        if (!u2_block)
 578            goto out;
 579    }
 580
 581    temp = (u64)uspi->s_sbbase + fs64_to_cpu(sb, u2_block);
 582    ret = temp + (u64)(frag & uspi->s_fpbmask);
 583
 584out:
 585    unlock_kernel();
 586
 587    return ret;
 588}
 589
 590int
 591ufs_getfrag_block(struct inode* inode, sector_t fragment,
 592                  struct buffer_head* bh_result, int create)
 593{
 594    int err;
 595    u64 phys64 = 0;
 596   
 597    struct super_block* sb = inode->I_sb;
 598
 599    if (!create) {
 600        phys64 = ufs_frag_map(inode, fragment, &err);
 601        UFSD("phys64 = %llu\n", (unsigned long long)phys64);
 602        if (phys64) {
 603            bh_result->b_blocknr = phys64; /* map_bh */
 604            bh_result->b_size = sb->s_blocksize; /* map_bh */
 605        }
 606        return 0;
 607    }
 608
 609    return -1;
 610}
 611
 612static struct buffer_head*
 613ufs_getfrag(struct inode* inode, unsigned int fragment, int create, int* err)
 614{
 615    struct buffer_head dummy;
 616    int error;
 617
 618    dummy.b_blocknr = -1000;
 619    error = ufs_getfrag_block(inode, fragment, &dummy, create);
 620    *err = error;
 621    if (!error) {
 622        struct buffer_head *bh;
 623        bh = sb_getblk(inode->I_sb, dummy.b_blocknr);
 624        return bh;
 625    }
 626    return NULL;
 627}
 628
 629/* We don't use this. */
 630struct buffer_head*
 631ufs_bread(struct inode* inode, unsigned fragment, int create, int* err)
 632{
 633    struct buffer_head* bh;
 634
 635    UFSD("ENTER, ino %llu, fragment %u\n", inode->I_ino, fragment);
 636
 637    bh = ufs_getfrag(inode, fragment, create, err);
 638    if (!bh)
 639        return bh;
 640
 641    struct buffer_head* bhnew = sb_bread(inode->I_sb, bh->b_blocknr);
 642
 643    brelse(bh);
 644
 645    return bhnew;
 646}
 647
 648static int
 649ufs1_read_inode(struct inode* inode, struct ufs_inode* ufs_inode)
 650{
 651    struct ufs_inode_info* ufsi = inode->I_private;
 652    struct super_block* sb = inode->I_sb;
 653
 654    mode_t mode;
 655    unsigned i;
 656
 657    inode->I_mode = mode = fs16_to_cpu(sb, ufs_inode->ui_mode);
 658    inode->I_nlink = fs16_to_cpu(sb, ufs_inode->ui_nlink);
 659
 660    if (inode->I_nlink == 0) {
 661        fprintf(stderr,
 662                "ufs_read_inode: inode %llu has zero nlink\n", inode->I_ino);
 663        return -1;
 664    }
 665    
 666    inode->I_uid  = ufs_get_inode_uid(sb, ufs_inode);
 667    inode->I_gid  = ufs_get_inode_gid(sb, ufs_inode);
 668    inode->I_size = fs64_to_cpu(sb, ufs_inode->ui_size);
 669    inode->I_atime.tv_sec  = fs32_to_cpu(sb, ufs_inode->ui_atime.tv_sec);
 670    inode->I_ctime.tv_sec  = fs32_to_cpu(sb, ufs_inode->ui_ctime.tv_sec);
 671    inode->I_mtime.tv_sec  = fs32_to_cpu(sb, ufs_inode->ui_mtime.tv_sec);
 672    inode->I_mtime.tv_nsec = 0;
 673    inode->I_atime.tv_nsec = 0;
 674    inode->I_ctime.tv_nsec = 0;
 675    inode->I_blocks = fs32_to_cpu(sb, ufs_inode->ui_blocks);
 676    inode->I_generation = fs32_to_cpu(sb, ufs_inode->ui_gen);
 677    inode->I_flags = fs32_to_cpu(sb, ufs_inode->ui_flags); /* XXX */
 678    ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
 679    ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
 680    
 681    if (S_ISCHR(mode) || S_ISBLK(mode) || inode->I_blocks) {
 682        for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
 683            ufsi->i_u1.i_data[i] = ufs_inode->ui_u2.ui_addr.ui_db[i];
 684    } else {
 685        for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
 686            ufsi->i_u1.i_symlink[i] = ufs_inode->ui_u2.ui_symlink[i];
 687    }
 688
 689    return 0;
 690}
 691
 692static int
 693ufs2_read_inode(struct inode* inode, struct ufs2_inode* ufs2_inode)
 694{
 695    struct ufs_inode_info* ufsi = inode->I_private;
 696    struct super_block* sb = inode->I_sb;
 697
 698    mode_t mode;
 699    unsigned i;
 700
 701    UFSD("Reading ufs2 inode, ino %llu\n", inode->I_ino);
 702
 703    inode->I_mode = mode = fs16_to_cpu(sb, ufs2_inode->ui_mode);
 704    inode->I_nlink = fs16_to_cpu(sb, ufs2_inode->ui_nlink);
 705    if (inode->I_nlink == 0) {
 706        fprintf(stderr,
 707                "ufs_read_inode: inode %llu has zero nlink\n", inode->I_ino);
 708        return -1;
 709    }
 710
 711    inode->I_uid  = fs32_to_cpu(sb, ufs2_inode->ui_uid);
 712    inode->I_gid  = fs32_to_cpu(sb, ufs2_inode->ui_gid);
 713    inode->I_size = fs64_to_cpu(sb, ufs2_inode->ui_size);
 714    inode->I_atime.tv_sec  = fs64_to_cpu(sb, ufs2_inode->ui_atime);
 715    inode->I_ctime.tv_sec  = fs64_to_cpu(sb, ufs2_inode->ui_ctime);
 716    inode->I_mtime.tv_sec  = fs64_to_cpu(sb, ufs2_inode->ui_mtime);
 717    inode->I_atime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_atimensec);
 718    inode->I_ctime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_ctimensec);
 719    inode->I_mtime.tv_nsec = fs32_to_cpu(sb, ufs2_inode->ui_mtimensec);
 720    inode->I_blocks = fs64_to_cpu(sb, ufs2_inode->ui_blocks);
 721    inode->I_generation = fs32_to_cpu(sb, ufs2_inode->ui_gen);
 722    ufsi->i_flags = fs32_to_cpu(sb, ufs2_inode->ui_flags);
 723    /*
 724     * ufsi->i_shadow = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_shadow);
 725     * ufsi->i_oeftflag = fs32_to_cpu(sb, ufs_inode->ui_u3.ui_sun.ui_oeftflag);
 726     */
 727
 728    if (S_ISCHR(mode) || S_ISBLK(mode) || inode->I_blocks) {
 729        for (i = 0; i < (UFS_NDADDR + UFS_NINDIR); i++)
 730            ufsi->i_u1.u2_i_data[i] =
 731                ufs2_inode->ui_u2.ui_addr.ui_db[i];
 732    } else {
 733        for (i = 0; i < (UFS_NDADDR + UFS_NINDIR) * 4; i++)
 734            ufsi->i_u1.i_symlink[i] = ufs2_inode->ui_u2.ui_symlink[i];
 735    }
 736
 737    return 0;
 738}
 739
 740static inline int
 741ufs_match(struct super_block* sb, int len, const char* const name,
 742          struct ufs_dir_entry* de)
 743{
 744    if (len != ufs_get_de_namlen(sb, de))
 745        return 0;
 746
 747    if (!de->d_ino)
 748        return 0;
 749
 750    return !memcmp(name, de->d_name, len);
 751}
 752
 753static inline struct ufs_dir_entry*
 754ufs_next_entry(struct super_block* sb, struct ufs_dir_entry* p)
 755{
 756    return (struct ufs_dir_entry*)((char*)p + fs16_to_cpu(sb, p->d_reclen));
 757}
 758
 759static inline unsigned
 760ufs_validate_entry(struct super_block* sb, char* base, unsigned offset,
 761                   unsigned mask)
 762{
 763    struct ufs_dir_entry* de = (struct ufs_dir_entry*)(base + offset);
 764    struct ufs_dir_entry* p = (struct ufs_dir_entry*)(base + (offset & mask));
 765
 766    while ((char*)p < (char*)de) {
 767        if (p->d_reclen == 0)
 768            break;
 769        p = ufs_next_entry(sb, p);
 770    }
 771
 772    return (char*)p - base;
 773}
 774
 775static inline unsigned long
 776ufs_dir_pages(struct inode *inode)
 777{
 778    return (inode->I_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
 779}
 780
 781static int
 782ufs_check_page(struct inode* dir, sector_t index, char* page)
 783{
 784    struct super_block* sb = dir->I_sb;
 785    char* kaddr = (char*)page;
 786    unsigned offs, rec_len;
 787    unsigned limit = PAGE_CACHE_SIZE;
 788    const unsigned chunk_mask = UFS_SB(sb)->s_uspi->s_dirblksize - 1;
 789    struct ufs_dir_entry*p;
 790    char* error;
 791
 792    int fres = -1;
 793
 794    if ((dir->I_size >> PAGE_CACHE_SHIFT) == index) {
 795
 796        limit = dir->I_size & ~PAGE_CACHE_MASK;
 797
 798        if (limit & chunk_mask)
 799            goto Ebadsize;
 800
 801        if (!limit)
 802            goto out;
 803    }
 804
 805    for (offs = 0; offs <= limit - UFS_DIR_REC_LEN(1); offs += rec_len) {
 806
 807        p = (struct ufs_dir_entry*)(kaddr + offs);
 808        rec_len = fs16_to_cpu(sb, p->d_reclen);
 809
 810        if (rec_len < UFS_DIR_REC_LEN(1))
 811            goto Eshort;
 812
 813        if (rec_len & 3)
 814            goto Ealign;
 815
 816        if (rec_len < UFS_DIR_REC_LEN(ufs_get_de_namlen(sb, p)))
 817            goto Enamelen;
 818
 819        if (((offs + rec_len - 1) ^ offs) & ~chunk_mask)
 820            goto Espan;
 821
 822        if (fs32_to_cpu(sb, p->d_ino) > (UFS_SB(sb)->s_uspi->s_ipg *
 823                        UFS_SB(sb)->s_uspi->s_ncg))
 824            goto Einumber;
 825    }
 826
 827    if (offs != limit)
 828        goto Eend;
 829
 830out:
 831    return 0; /* good */
 832
 833Ebadsize:
 834    fprintf(stderr, "ufs_check_page: size of directory #%llu is "
 835            "not a multiple of chunk size", dir->I_ino);
 836    goto fail;
 837
 838Eshort:
 839    error = "rec_len is smaller than minimal";
 840    goto bad_entry;
 841
 842Ealign:
 843    error = "unaligned directory entry";
 844    goto bad_entry;
 845
 846Enamelen:
 847    error = "rec_len is too small for name_len";
 848    goto bad_entry;
 849
 850Espan:
 851    error = "directory entry across blocks";
 852    goto bad_entry;
 853
 854Einumber:
 855    error = "inode out of bounds";
 856
 857bad_entry:
 858    fprintf(stderr, "ufs_check_page: bad entry in directory #%llu: %s - "
 859            "offset=%llu, rec_len=%d, name_len=%d",
 860            dir->I_ino, error, (index << PAGE_CACHE_SHIFT) + offs,
 861            rec_len, ufs_get_de_namlen(sb, p));
 862    goto fail;
 863
 864Eend:
 865    p = (struct ufs_dir_entry *)(kaddr + offs);
 866
 867    fprintf(stderr, "entry in directory #%llu spans the page boundary"
 868           "offset=%llu", dir->I_ino, (index << PAGE_CACHE_SHIFT) + offs);
 869fail:
 870    return fres;
 871}
 872
 873static unsigned
 874ufs_last_byte(struct inode* inode, unsigned long page_nr)
 875{
 876    unsigned last_byte = inode->I_size;
 877
 878    last_byte -= page_nr << PAGE_CACHE_SHIFT;
 879    if (last_byte > PAGE_CACHE_SIZE)
 880        last_byte = PAGE_CACHE_SIZE;
 881
 882    return last_byte;
 883}
 884
 885static struct ufs_dir_entry*
 886ufs_dotdot_s(struct inode* dir, char* pagebuf)
 887{
 888    int ret = ufs_get_dirpage(dir, 0, pagebuf);
 889    struct ufs_dir_entry* de = NULL;
 890    if (ret == 0)
 891        de = ufs_next_entry(dir->I_sb, (struct ufs_dir_entry*)pagebuf);
 892    return de;
 893}
 894
 895static int
 896ufs_get_dirpage(struct inode* inode, sector_t index, char* pagebuf)
 897{
 898    int ret = U_ufs_get_page(inode, index, pagebuf);
 899    /* if (ufs_check_page(inode, index, pagebuf) != 0) return -1; */
 900    return ret;
 901}
 902
 903static ino_t
 904ufs_find_entry_s(struct inode* dir, const char* name)
 905{
 906    struct super_block* sb = dir->I_sb;
 907    struct ufs_inode_info* ui = dir->I_private;
 908
 909    int namelen = strlen(name);
 910    unsigned reclen = UFS_DIR_REC_LEN(namelen);
 911    unsigned long npages = ufs_dir_pages(dir);
 912    unsigned long start, n;
 913    struct ufs_dir_entry* de;
 914
 915    ino_t result = 0;
 916
 917    UFSD("ENTER, dir_ino %llu, name %s, namlen %u\n",
 918         dir->I_ino, name, namelen);
 919
 920    if (npages == 0 || namelen > UFS_MAXNAMLEN)
 921        goto out;
 922
 923    start = ui->i_dir_start_lookup;
 924
 925    if (start >= npages)
 926        start = 0;
 927
 928    n = start;
 929
 930    do {
 931        char page[PAGE_SIZE];
 932        char* kaddr;
 933
 934        if (ufs_get_dirpage(dir, n, page) == 0) {
 935            kaddr = page;
 936            de = (struct ufs_dir_entry*)kaddr;
 937            kaddr += ufs_last_byte(dir, n) - reclen;
 938            while ((char*)de <= kaddr) {
 939                if (de->d_reclen == 0) {
 940                    fprintf(stderr, "zero-length directory entry\n");
 941                    goto out;
 942                }
 943                if (ufs_match(sb, namelen, (char*)name, de)) {
 944                    result = fs32_to_cpu(sb, de->d_ino);
 945                    goto found;
 946                }
 947                de = ufs_next_entry(sb, de);
 948            }
 949        }
 950
 951        if (++n >= npages)
 952            n = 0;
 953    } while (n != start);
 954
 955out:
 956    return result;
 957
 958found:
 959    ui->i_dir_start_lookup = n;
 960
 961    return result;
 962}
 963
 964struct super_block*
 965U_ufs_fill_super(int fd, void* data, int silent)
 966{
 967    struct super_block* sb = NULL;
 968    struct ufs_sb_info* sbi = NULL;
 969    struct ufs_sb_private_info* uspi;
 970
 971    struct ufs_super_block_first*  usb1;
 972    struct ufs_super_block_second* usb2;
 973    struct ufs_super_block_third*  usb3;
 974
 975    struct ufs_buffer_head* ubh;    
 976
 977    unsigned block_size, super_block_size;
 978    unsigned flags, super_block_offset;
 979
 980    uspi = NULL;
 981    ubh = NULL;
 982    flags = 0;
 983
 984    UFSD("ENTER\n");
 985
 986    sb = calloc(1, sizeof(struct super_block));
 987    if (!sb)
 988        return NULL;
 989
 990    sb->s_bdev = fd;        
 991    sb->s_flags |= MS_RDONLY;
 992
 993    sbi = kzalloc(sizeof(struct ufs_sb_info), GFP_KERNEL);
 994    if (!sbi)
 995        goto failed_nomem;
 996
 997    sbi->s_mount_opt = 0;
 998    sb->s_fs_info = sbi;
 999
1000    ufs_set_opt(sbi->s_mount_opt, ONERROR_LOCK);
1001    if (!ufs_parse_options((char*) data, &sbi->s_mount_opt)) {
1002        printk("wrong mount options\n");
1003        goto failed;
1004    }
1005
1006    if (!(sbi->s_mount_opt & UFS_MOUNT_UFSTYPE))
1007        ufs_set_opt(sbi->s_mount_opt, UFSTYPE_OLD);
1008
1009    uspi = kzalloc(sizeof(struct ufs_sb_private_info), GFP_KERNEL);
1010    sbi->s_uspi = uspi;
1011    if (!uspi)
1012        goto failed;
1013
1014    uspi->s_dirblksize = UFS_SECTOR_SIZE;
1015    super_block_offset=UFS_SBLOCK;
1016
1017    switch (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) {
1018
1019    case UFS_MOUNT_UFSTYPE_44BSD:
1020        UFSD("ufstype=44bsd\n");
1021        uspi->s_fsize  = block_size = 512;
1022        uspi->s_fmask  = ~(512 - 1);
1023        uspi->s_fshift = 9;
1024        uspi->s_sbsize = super_block_size = 1536;
1025        uspi->s_sbbase = 0;
1026        flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
1027        break;
1028
1029    case UFS_MOUNT_UFSTYPE_UFS2:
1030        UFSD("ufstype=ufs2\n");
1031        super_block_offset = SBLOCK_UFS2;
1032        uspi->s_fsize  = block_size = 512;
1033        uspi->s_fmask  = ~(512 - 1);
1034        uspi->s_fshift = 9;
1035        uspi->s_sbsize = super_block_size = 1536;
1036        uspi->s_sbbase =  0;
1037        flags |= UFS_TYPE_UFS2 | UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
1038        break;
1039        
1040    case UFS_MOUNT_UFSTYPE_SUN:
1041        UFSD("ufstype=sun\n");
1042        uspi->s_fsize  = block_size = 1024;
1043        uspi->s_fmask  = ~(1024 - 1);
1044        uspi->s_fshift = 10;
1045        uspi->s_sbsize = super_block_size = 2048;
1046        uspi->s_sbbase = 0;
1047        uspi->s_maxsymlinklen = 0; /* Not supported on disk */
1048        flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUN | UFS_CG_SUN;
1049        break;
1050
1051    case UFS_MOUNT_UFSTYPE_SUNOS:
1052        UFSD(("ufstype=sunos\n"))
1053        uspi->s_fsize    = block_size = 1024;
1054        uspi->s_fmask    = ~(1024 - 1);
1055        uspi->s_fshift   = 10;
1056        uspi->s_sbsize   = 2048;
1057        super_block_size = 2048;
1058        uspi->s_sbbase   = 0;
1059        uspi->s_maxsymlinklen = 0; /* Not supported on disk */
1060        flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_SUNOS | UFS_CG_SUN;
1061        break;
1062
1063    case UFS_MOUNT_UFSTYPE_SUNx86:
1064        UFSD("ufstype=sunx86\n");
1065        uspi->s_fsize  = block_size = 1024;
1066        uspi->s_fmask  = ~(1024 - 1);
1067        uspi->s_fshift = 10;
1068        uspi->s_sbsize = super_block_size = 2048;
1069        uspi->s_sbbase = 0;
1070        uspi->s_maxsymlinklen = 0; /* Not supported on disk */
1071        flags |= UFS_DE_OLD | UFS_UID_EFT | UFS_ST_SUNx86 | UFS_CG_SUN;
1072        break;
1073
1074    case UFS_MOUNT_UFSTYPE_OLD:
1075        UFSD("ufstype=old\n");
1076        uspi->s_fsize  = block_size = 1024;
1077        uspi->s_fmask  = ~(1024 - 1);
1078        uspi->s_fshift = 10;
1079        uspi->s_sbsize = super_block_size = 2048;
1080        uspi->s_sbbase = 0;
1081        flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
1082        if (!(sb->s_flags & MS_RDONLY)) {
1083            if (!silent)
1084                printk(KERN_INFO "ufstype=old is supported read-only\n");
1085            sb->s_flags |= MS_RDONLY;
1086        }
1087        break;
1088    
1089    case UFS_MOUNT_UFSTYPE_NEXTSTEP:
1090        UFSD("ufstype=nextstep\n");
1091        uspi->s_fsize  = block_size = 1024;
1092        uspi->s_fmask  = ~(1024 - 1);
1093        uspi->s_fshift = 10;
1094        uspi->s_sbsize = super_block_size = 2048;
1095        uspi->s_sbbase = 0;
1096        uspi->s_dirblksize = 1024;
1097        flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
1098        if (!(sb->s_flags & MS_RDONLY)) {
1099            if (!silent)
1100                printk(KERN_INFO "ufstype=nextstep is supported read-only\n");
1101            sb->s_flags |= MS_RDONLY;
1102        }
1103        break;
1104    
1105    case UFS_MOUNT_UFSTYPE_NEXTSTEP_CD:
1106        UFSD("ufstype=nextstep-cd\n");
1107        uspi->s_fsize  = block_size = 2048;
1108        uspi->s_fmask  = ~(2048 - 1);
1109        uspi->s_fshift = 11;
1110        uspi->s_sbsize = super_block_size = 2048;
1111        uspi->s_sbbase = 0;
1112        uspi->s_dirblksize = 1024;
1113        flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
1114        if (!(sb->s_flags & MS_RDONLY)) {
1115            if (!silent)
1116                printk(KERN_INFO "ufstype=nextstep-cd is supported read-only\n");
1117            sb->s_flags |= MS_RDONLY;
1118        }
1119        break;
1120    
1121    case UFS_MOUNT_UFSTYPE_OPENSTEP:
1122        UFSD("ufstype=openstep\n");
1123        uspi->s_fsize  = block_size = 1024;
1124        uspi->s_fmask  = ~(1024 - 1);
1125        uspi->s_fshift = 10;
1126        uspi->s_sbsize = super_block_size = 2048;
1127        uspi->s_sbbase = 0;
1128        uspi->s_dirblksize = 1024;
1129        flags |= UFS_DE_44BSD | UFS_UID_44BSD | UFS_ST_44BSD | UFS_CG_44BSD;
1130        if (!(sb->s_flags & MS_RDONLY)) {
1131            if (!silent)
1132                printk(KERN_INFO "ufstype=openstep is supported read-only\n");
1133            sb->s_flags |= MS_RDONLY;
1134        }
1135        break;
1136    
1137    case UFS_MOUNT_UFSTYPE_HP:
1138        UFSD("ufstype=hp\n");
1139        uspi->s_fsize  = block_size = 1024;
1140        uspi->s_fmask  = ~(1024 - 1);
1141        uspi->s_fshift = 10;
1142        uspi->s_sbsize = super_block_size = 2048;
1143        uspi->s_sbbase = 0;
1144        flags |= UFS_DE_OLD | UFS_UID_OLD | UFS_ST_OLD | UFS_CG_OLD;
1145        if (!(sb->s_flags & MS_RDONLY)) {
1146            if (!silent)
1147                printk(KERN_INFO "ufstype=hp is supported read-only\n");
1148            sb->s_flags |= MS_RDONLY;
1149         }
1150         break;
1151
1152    default:
1153        if (!silent)
1154            printk("unknown ufstype\n");
1155        goto failed;
1156    }
1157    
1158again:    
1159
1160    /* set block size */
1161
1162    if (block_size > PAGE_SIZE || block_size < 512 ||
1163        !is_power_of_2(block_size))
1164        goto failed;
1165
1166    if (block_size < 512)
1167        goto failed;
1168
1169    sb->s_blocksize = block_size;
1170    sb->s_blocksize_bits = blksize_bits(block_size);
1171
1172    /* Read UFS super block from device */
1173
1174    ubh = ubh_bread_uspi(uspi, sb,
1175              uspi->s_sbbase + super_block_offset/block_size, super_block_size);
1176    
1177    if (!ubh) 
1178        goto failed;
1179
1180    usb1 = ubh_get_usb_first(uspi);
1181    usb2 = ubh_get_usb_second(uspi);
1182    usb3 = ubh_get_usb_third(uspi);
1183
1184    /* Sort out mod used on SunOS 4.1.3 for fs_state */
1185
1186    uspi->s_postblformat = fs32_to_cpu(sb, usb3->fs_postblformat);
1187    if (((flags & UFS_ST_MASK) == UFS_ST_SUNOS) &&
1188        (uspi->s_postblformat != UFS_42POSTBLFMT)) {
1189        flags &= ~UFS_ST_MASK;
1190        flags |=  UFS_ST_SUN;
1191    }
1192
1193    /* Check UFS magic number */
1194
1195    sbi->s_bytesex = BYTESEX_LE;
1196
1197    switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
1198    case UFS_MAGIC:
1199    case UFS2_MAGIC:
1200    case UFS_MAGIC_LFN:
1201    case UFS_MAGIC_FEA:
1202    case UFS_MAGIC_4GB:
1203        goto magic_found;
1204    }
1205
1206    sbi->s_bytesex = BYTESEX_BE;
1207
1208    switch ((uspi->fs_magic = fs32_to_cpu(sb, usb3->fs_magic))) {
1209    case UFS_MAGIC:
1210    case UFS2_MAGIC:
1211    case UFS_MAGIC_LFN:
1212    case UFS_MAGIC_FEA:
1213    case UFS_MAGIC_4GB:
1214        goto magic_found;
1215    }
1216
1217    if ((((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) ==
1218           UFS_MOUNT_UFSTYPE_NEXTSTEP)    ||
1219         ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) ==
1220           UFS_MOUNT_UFSTYPE_NEXTSTEP_CD) ||
1221         ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) ==
1222           UFS_MOUNT_UFSTYPE_OPENSTEP)) && uspi->s_sbbase < 256) {
1223        ubh_brelse_uspi(uspi);
1224        ubh = NULL;
1225        uspi->s_sbbase += 8;
1226        goto again;
1227    }
1228
1229    if (!silent)
1230        printk("ufs_read_super: bad magic number\n");
1231
1232    goto failed;
1233
1234magic_found:
1235
1236    /* Check block and fragment sizes */
1237
1238    uspi->s_bsize  = fs32_to_cpu(sb, usb1->fs_bsize);
1239    uspi->s_fsize  = fs32_to_cpu(sb, usb1->fs_fsize);
1240    uspi->s_sbsize = fs32_to_cpu(sb, usb1->fs_sbsize);
1241    uspi->s_fmask  = fs32_to_cpu(sb, usb1->fs_fmask);
1242    uspi->s_fshift = fs32_to_cpu(sb, usb1->fs_fshift);
1243
1244    if (!is_power_of_2(uspi->s_fsize)) {
1245        printk(KERN_ERR
1246               "ufs_read_super: fragment size %u is not a power of 2\n",
1247        uspi->s_fsize);
1248        goto failed;
1249    }
1250
1251    if (uspi->s_fsize < 512) {
1252        printk(KERN_ERR "ufs_read_super: fragment size %u is too small\n",
1253               uspi->s_fsize);
1254        goto failed;
1255    }
1256
1257    if (uspi->s_fsize > 4096) {
1258        printk(KERN_ERR "ufs_read_super: fragment size %u is too large\n",
1259               uspi->s_fsize);
1260        goto failed;
1261    }
1262
1263    if (!is_power_of_2(uspi->s_bsize)) {
1264        printk(KERN_ERR "ufs_read_super: block size %u is not a power of 2\n",
1265               uspi->s_bsize);
1266        goto failed;
1267    }
1268
1269    if (uspi->s_bsize < 4096) {
1270        printk(KERN_ERR "ufs_read_super: block size %u is too small\n",
1271               uspi->s_bsize);
1272        goto failed;
1273    }
1274
1275    if (uspi->s_bsize / uspi->s_fsize > 8) {
1276        printk(KERN_ERR "ufs_read_super: too many fragments per block (%u)\n",
1277               uspi->s_bsize / uspi->s_fsize);
1278        goto failed;
1279    }
1280
1281    if (uspi->s_fsize != block_size || uspi->s_sbsize != super_block_size) {
1282        ubh_brelse_uspi(uspi);
1283        ubh = NULL;
1284        block_size = uspi->s_fsize;
1285        super_block_size = uspi->s_sbsize;
1286        UFSD("another value of block_size or super_block_size %u, %u\n",
1287             block_size, super_block_size);
1288        goto again;
1289    }
1290
1291    sbi->s_flags = flags; /* After this line some functions use s_flags*/
1292
1293    /* ufs_print_super_stuff(sb, usb1, usb2, usb3); */
1294
1295    /* Check if file system was unmounted cleanly. Make read-only otherwise. */
1296
1297    if (((flags & UFS_ST_MASK) == UFS_ST_44BSD) ||
1298        ((flags & UFS_ST_MASK) == UFS_ST_OLD)   ||
1299        (((flags & UFS_ST_MASK) == UFS_ST_SUN   ||
1300        (flags & UFS_ST_MASK) == UFS_ST_SUNOS   ||
1301        (flags & UFS_ST_MASK) == UFS_ST_SUNx86) &&
1302        (ufs_get_fs_state(sb, usb1, usb3) ==
1303         (UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time))))) {
1304
1305        switch(usb1->fs_clean) {
1306
1307        case UFS_FSCLEAN:
1308            UFSD("fs is clean\n");
1309            break;
1310
1311        case UFS_FSSTABLE:
1312            UFSD("fs is stable\n");
1313            break;
1314
1315        case UFS_FSOSF1:
1316            UFSD("fs is DEC OSF/1\n");
1317            break;
1318
1319        case UFS_FSACTIVE:
1320            printk("ufs_read_super: fs is active\n");
1321            sb->s_flags |= MS_RDONLY;
1322            break;
1323
1324        case UFS_FSBAD:
1325            printk("ufs_read_super: fs is bad\n");
1326            sb->s_flags |= MS_RDONLY;
1327            break;
1328
1329        default:
1330            printk("ufs_read_super: can't grok fs_clean 0x%x\n",
1331                   usb1->fs_clean);
1332            sb->s_flags |= MS_RDONLY;
1333            break;
1334        }
1335    } else {
1336        printk("ufs_read_super: fs needs fsck\n");
1337        sb->s_flags |= MS_RDONLY;
1338    }
1339
1340    /* Read ufs_super_block into internal data structures */
1341
1342    sb->s_magic = fs32_to_cpu(sb, usb3->fs_magic);
1343
1344    uspi->s_sblkno   = fs32_to_cpu(sb, usb1->fs_sblkno);
1345    uspi->s_cblkno   = fs32_to_cpu(sb, usb1->fs_cblkno);
1346    uspi->s_iblkno   = fs32_to_cpu(sb, usb1->fs_iblkno);
1347    uspi->s_dblkno   = fs32_to_cpu(sb, usb1->fs_dblkno);
1348    uspi->s_cgoffset = fs32_to_cpu(sb, usb1->fs_cgoffset);
1349    uspi->s_cgmask   = fs32_to_cpu(sb, usb1->fs_cgmask);
1350
1351    if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1352        uspi->s_u2_size  = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_size);
1353        uspi->s_u2_dsize = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1354    } else {
1355        uspi->s_size  = fs32_to_cpu(sb, usb1->fs_size);
1356        uspi->s_dsize = fs32_to_cpu(sb, usb1->fs_dsize);
1357    }
1358
1359    uspi->s_ncg = fs32_to_cpu(sb, usb1->fs_ncg);
1360
1361    /* s_bsize already set */
1362    /* s_fsize already set */
1363
1364    uspi->s_fpb     = fs32_to_cpu(sb, usb1->fs_frag);
1365    uspi->s_minfree = fs32_to_cpu(sb, usb1->fs_minfree);
1366    uspi->s_bmask   = fs32_to_cpu(sb, usb1->fs_bmask);
1367    uspi->s_fmask   = fs32_to_cpu(sb, usb1->fs_fmask);
1368    uspi->s_bshift  = fs32_to_cpu(sb, usb1->fs_bshift);
1369    uspi->s_fshift  = fs32_to_cpu(sb, usb1->fs_fshift);
1370
1371    UFSD("uspi->s_bshift = %d,uspi->s_fshift = %d", uspi->s_bshift,
1372         uspi->s_fshift);
1373
1374    uspi->s_fpbshift = fs32_to_cpu(sb, usb1->fs_fragshift);
1375    uspi->s_fsbtodb  = fs32_to_cpu(sb, usb1->fs_fsbtodb);
1376
1377    /* s_sbsize already set */
1378
1379    uspi->s_csmask     = fs32_to_cpu(sb, usb1->fs_csmask);
1380    uspi->s_csshift    = fs32_to_cpu(sb, usb1->fs_csshift);
1381    uspi->s_nindir     = fs32_to_cpu(sb, usb1->fs_nindir);
1382    uspi->s_inopb      = fs32_to_cpu(sb, usb1->fs_inopb);
1383    uspi->s_nspf       = fs32_to_cpu(sb, usb1->fs_nspf);
1384    uspi->s_npsect     = ufs_get_fs_npsect(sb, usb1, usb3);
1385    uspi->s_interleave = fs32_to_cpu(sb, usb1->fs_interleave);
1386    uspi->s_trackskew  = fs32_to_cpu(sb, usb1->fs_trackskew);
1387
1388    if (uspi->fs_magic == UFS2_MAGIC)
1389        uspi->s_csaddr = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_csaddr);
1390    else
1391        uspi->s_csaddr = fs32_to_cpu(sb, usb1->fs_csaddr);
1392
1393    uspi->s_cssize = fs32_to_cpu(sb, usb1->fs_cssize);
1394    uspi->s_cgsize = fs32_to_cpu(sb, usb1->fs_cgsize);
1395    uspi->s_ntrak  = fs32_to_cpu(sb, usb1->fs_ntrak);
1396    uspi->s_nsect  = fs32_to_cpu(sb, usb1->fs_nsect);
1397    uspi->s_spc    = fs32_to_cpu(sb, usb1->fs_spc);
1398    uspi->s_ipg    = fs32_to_cpu(sb, usb1->fs_ipg);
1399    uspi->s_fpg    = fs32_to_cpu(sb, usb1->fs_fpg);
1400    uspi->s_cpc    = fs32_to_cpu(sb, usb2->fs_un.fs_u1.fs_cpc);
1401    uspi->s_contigsumsize = fs32_to_cpu(sb,
1402                                        usb3->fs_un2.fs_44.fs_contigsumsize);
1403    uspi->s_qbmask = ufs_get_fs_qbmask(sb, usb3);
1404    uspi->s_qfmask = ufs_get_fs_qfmask(sb, usb3);
1405    uspi->s_nrpos  = fs32_to_cpu(sb, usb3->fs_nrpos);
1406    uspi->s_postbloff = fs32_to_cpu(sb, usb3->fs_postbloff);
1407    uspi->s_rotbloff  = fs32_to_cpu(sb, usb3->fs_rotbloff);
1408
1409    /* Compute another frequently used values */
1410
1411    uspi->s_fpbmask = uspi->s_fpb - 1;
1412    if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
1413        uspi->s_apbshift = uspi->s_bshift - 3;
1414    else
1415        uspi->s_apbshift = uspi->s_bshift - 2;
1416
1417    uspi->s_2apbshift = uspi->s_apbshift * 2;
1418    uspi->s_3apbshift = uspi->s_apbshift * 3;
1419    uspi->s_apb  = 1 << uspi->s_apbshift;
1420    uspi->s_2apb = 1 << uspi->s_2apbshift;
1421    uspi->s_3apb = 1 << uspi->s_3apbshift;
1422    uspi->s_apbmask = uspi->s_apb - 1;
1423    uspi->s_nspfshift = uspi->s_fshift - UFS_SECTOR_BITS;
1424    uspi->s_nspb = uspi->s_nspf << uspi->s_fpbshift;
1425    uspi->s_inopf = uspi->s_inopb >> uspi->s_fpbshift;
1426    uspi->s_bpf = uspi->s_fsize << 3;
1427    uspi->s_bpfshift = uspi->s_fshift + 3;
1428    uspi->s_bpfmask = uspi->s_bpf - 1;
1429
1430    if ((sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_44BSD ||
1431        (sbi->s_mount_opt & UFS_MOUNT_UFSTYPE) == UFS_MOUNT_UFSTYPE_UFS2)
1432        uspi->s_maxsymlinklen =
1433            fs32_to_cpu(sb, usb3->fs_un2.fs_44.fs_maxsymlinklen);
1434
1435    ufs_setup_cstotal(sb);
1436
1437    /* Read cylinder group structures */
1438
1439    if (!(sb->s_flags & MS_RDONLY))
1440        if (!ufs_read_cylinder_structures(sb))
1441            goto failed;
1442
1443    /*
1444     * ufs_read_cylinder_structures(sb);
1445     */
1446
1447    UFSD("EXIT\n");
1448
1449    return sb;
1450
1451failed:
1452
1453    if (ubh)
1454        ubh_brelse_uspi(uspi);
1455
1456    kfree(uspi);
1457    kfree(sbi);
1458    sb->s_fs_info = NULL;
1459
1460    UFSD("EXIT (FAILED)\n");
1461
1462    return NULL;
1463
1464failed_nomem:
1465
1466    UFSD("EXIT (NOMEM)\n");
1467
1468    return NULL;
1469}
1470
1471int
1472U_ufs_statvfs(struct super_block* sb, struct statvfs* buf)
1473{
1474    struct ufs_sb_private_info *uspi= UFS_SB(sb)->s_uspi;
1475    unsigned flags = UFS_SB(sb)->s_flags;
1476
1477    struct ufs_super_block_first*  usb1;
1478    struct ufs_super_block_second* usb2;
1479    struct ufs_super_block_third*  usb3;
1480
1481    lock_kernel();
1482
1483    usb1 = ubh_get_usb_first(uspi);
1484    usb2 = ubh_get_usb_second(uspi);
1485    usb3 = ubh_get_usb_third(uspi);
1486    
1487    if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1488        /* buf->f_type = UFS2_MAGIC; */
1489        buf->f_blocks = fs64_to_cpu(sb, usb3->fs_un1.fs_u2.fs_dsize);
1490    } else {
1491        /* buf->f_type = UFS_MAGIC; */
1492        buf->f_blocks = uspi->s_dsize;
1493    }
1494
1495    buf->f_bfree = ufs_blkstofrags(uspi->cs_total.cs_nbfree) +
1496                                   uspi->cs_total.cs_nffree;
1497    buf->f_ffree = uspi->cs_total.cs_nifree;
1498
1499    buf->f_bavail =
1500        (buf->f_bfree > (((long)buf->f_blocks / 100) * uspi->s_minfree))
1501        ? (buf->f_bfree - (((long)buf->f_blocks / 100) * uspi->s_minfree)) : 0;
1502
1503    buf->f_files = uspi->s_ncg * uspi->s_ipg;
1504    buf->f_namemax = UFS_MAXNAMLEN;
1505
1506    buf->f_frsize = sb->s_blocksize;
1507    buf->f_bsize = fs32_to_cpu(sb, usb1->fs_bsize);
1508
1509    unlock_kernel();
1510
1511    return 0;
1512}
1513
1514int
1515U_ufs_iget(struct super_block* sb, struct inode* inode)
1516{
1517    struct ufs_inode_info* ufsi;
1518    struct ufs_sb_private_info* uspi = UFS_SB(sb)->s_uspi;
1519    int err;
1520
1521    unsigned long ino = inode->I_ino;
1522
1523    UFSD("ENTER, ino %lu\n", ino);
1524
1525    if (ino < UFS_ROOTINO || ino > (uspi->s_ncg * uspi->s_ipg)) {
1526        fprintf(stderr, "ufs_read_inode: bad inode number (%lu)\n", ino);
1527        return EIO;
1528    }
1529
1530    inode->I_sb = sb;
1531    inode->I_blkbits = sb->s_blocksize_bits;
1532    inode->I_nlink = 1;
1533
1534    ufsi = inode->I_private;
1535    ufsi->i_unused1 = 0;
1536    ufsi->i_dir_start_lookup = 0;
1537
1538    struct buffer_head _bh;
1539    struct buffer_head* bh = &_bh;
1540
1541    bh->b_flags.dynamic = 0;
1542    if (sb_bread_intobh(sb, uspi->s_sbbase +
1543                        ufs_inotofsba(inode->I_ino), bh) != 0) {
1544        fprintf(stderr,
1545                "ufs_read_inode: unable to read inode %llu\n", inode->I_ino);
1546        goto bad_inode;
1547    }
1548
1549    if ((UFS_SB(sb)->s_flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2) {
1550        struct ufs2_inode* ufs2_inode = (struct ufs2_inode*)bh->b_data;
1551        err = ufs2_read_inode(inode, ufs2_inode + ufs_inotofsbo(inode->I_ino));
1552    } else {
1553        struct ufs_inode* ufs_inode = (struct ufs_inode*)bh->b_data;
1554        err = ufs1_read_inode(inode, ufs_inode + ufs_inotofsbo(inode->I_ino));
1555    }
1556
1557    if (err)
1558        goto bad_inode;
1559
1560    inode->I_version++;
1561
1562    ufsi->i_lastfrag = (inode->I_size + uspi->s_fsize - 1) >> uspi->s_fshift;
1563    ufsi->i_osync = 0;
1564
1565    brelse(bh);
1566
1567    UFSD("EXIT\n");
1568
1569    return 0;
1570
1571bad_inode:
1572
1573    return -1;
1574}
1575
1576ino_t
1577U_ufs_inode_by_name(struct inode* dir, const char* name)
1578{
1579    return ufs_find_entry_s(dir, name);
1580}
1581
1582int
1583U_ufs_next_direntry(struct inode* dir, struct unixfs_dirbuf* dirpagebuf,
1584                    off_t* offset, struct unixfs_direntry* dent)
1585{
1586    struct super_block* sb = dir->I_sb;
1587    struct ufs_inode_info* ui = dir->I_private;
1588
1589    unsigned long npages = ufs_dir_pages(dir);
1590    unsigned long start, n;
1591    struct ufs_dir_entry* de;
1592
1593    UFSD("ENTER, dir_ino %llu\n", dir->I_ino);
1594
1595    if (*offset > (dir->I_size - UFS_DIR_REC_LEN(1)))
1596        return -1;
1597
1598    if (npages == 0)
1599        retu

Large files files are truncated, but you can click here to view the full file