linux/fs/hfs/mdb.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/hfs/mdb.c
   3 *
   4 * Copyright (C) 1995-1997  Paul H. Hargrove
   5 * (C) 2003 Ardis Technologies <roman@ardistech.com>
   6 * This file may be distributed under the terms of the GNU General Public License.
   7 *
   8 * This file contains functions for reading/writing the MDB.
   9 */
  10
  11#include <linux/cdrom.h>
  12#include <linux/genhd.h>
  13#include <linux/nls.h>
  14
  15#include "hfs_fs.h"
  16#include "btree.h"
  17
  18/*================ File-local data types ================*/
  19
  20/*
  21 * The HFS Master Directory Block (MDB).
  22 *
  23 * Also known as the Volume Information Block (VIB), this structure is
  24 * the HFS equivalent of a superblock.
  25 *
  26 * Reference: _Inside Macintosh: Files_ pages 2-59 through 2-62
  27 *
  28 * modified for HFS Extended
  29 */
  30
  31static int hfs_get_last_session(struct super_block *sb,
  32                                sector_t *start, sector_t *size)
  33{
  34        struct cdrom_multisession ms_info;
  35        struct cdrom_tocentry te;
  36        int res;
  37
  38        /* default values */
  39        *start = 0;
  40        *size = sb->s_bdev->bd_inode->i_size >> 9;
  41
  42        if (HFS_SB(sb)->session >= 0) {
  43                te.cdte_track = HFS_SB(sb)->session;
  44                te.cdte_format = CDROM_LBA;
  45                res = ioctl_by_bdev(sb->s_bdev, CDROMREADTOCENTRY, (unsigned long)&te);
  46                if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
  47                        *start = (sector_t)te.cdte_addr.lba << 2;
  48                        return 0;
  49                }
  50                printk(KERN_ERR "hfs: invalid session number or type of track\n");
  51                return -EINVAL;
  52        }
  53        ms_info.addr_format = CDROM_LBA;
  54        res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, (unsigned long)&ms_info);
  55        if (!res && ms_info.xa_flag)
  56                *start = (sector_t)ms_info.addr.lba << 2;
  57        return 0;
  58}
  59
  60/*
  61 * hfs_mdb_get()
  62 *
  63 * Build the in-core MDB for a filesystem, including
  64 * the B-trees and the volume bitmap.
  65 */
  66int hfs_mdb_get(struct super_block *sb)
  67{
  68        struct buffer_head *bh;
  69        struct hfs_mdb *mdb, *mdb2;
  70        unsigned int block;
  71        char *ptr;
  72        int off2, len, size, sect;
  73        sector_t part_start, part_size;
  74        loff_t off;
  75        __be16 attrib;
  76
  77        /* set the device driver to 512-byte blocks */
  78        size = sb_min_blocksize(sb, HFS_SECTOR_SIZE);
  79        if (!size)
  80                return -EINVAL;
  81
  82        if (hfs_get_last_session(sb, &part_start, &part_size))
  83                return -EINVAL;
  84        while (1) {
  85                /* See if this is an HFS filesystem */
  86                bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
  87                if (!bh)
  88                        goto out;
  89
  90                if (mdb->drSigWord == cpu_to_be16(HFS_SUPER_MAGIC))
  91                        break;
  92                brelse(bh);
  93
  94                /* check for a partition block
  95                 * (should do this only for cdrom/loop though)
  96                 */
  97                if (hfs_part_find(sb, &part_start, &part_size))
  98                        goto out;
  99        }
 100
 101        HFS_SB(sb)->alloc_blksz = size = be32_to_cpu(mdb->drAlBlkSiz);
 102        if (!size || (size & (HFS_SECTOR_SIZE - 1))) {
 103                printk(KERN_ERR "hfs: bad allocation block size %d\n", size);
 104                goto out_bh;
 105        }
 106
 107        size = min(HFS_SB(sb)->alloc_blksz, (u32)PAGE_SIZE);
 108        /* size must be a multiple of 512 */
 109        while (size & (size - 1))
 110                size -= HFS_SECTOR_SIZE;
 111        sect = be16_to_cpu(mdb->drAlBlSt) + part_start;
 112        /* align block size to first sector */
 113        while (sect & ((size - 1) >> HFS_SECTOR_SIZE_BITS))
 114                size >>= 1;
 115        /* align block size to weird alloc size */
 116        while (HFS_SB(sb)->alloc_blksz & (size - 1))
 117                size >>= 1;
 118        brelse(bh);
 119        if (!sb_set_blocksize(sb, size)) {
 120                printk(KERN_ERR "hfs: unable to set blocksize to %u\n", size);
 121                goto out;
 122        }
 123
 124        bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
 125        if (!bh)
 126                goto out;
 127        if (mdb->drSigWord != cpu_to_be16(HFS_SUPER_MAGIC))
 128                goto out_bh;
 129
 130        HFS_SB(sb)->mdb_bh = bh;
 131        HFS_SB(sb)->mdb = mdb;
 132
 133        /* These parameters are read from the MDB, and never written */
 134        HFS_SB(sb)->part_start = part_start;
 135        HFS_SB(sb)->fs_ablocks = be16_to_cpu(mdb->drNmAlBlks);
 136        HFS_SB(sb)->fs_div = HFS_SB(sb)->alloc_blksz >> sb->s_blocksize_bits;
 137        HFS_SB(sb)->clumpablks = be32_to_cpu(mdb->drClpSiz) /
 138                                 HFS_SB(sb)->alloc_blksz;
 139        if (!HFS_SB(sb)->clumpablks)
 140                HFS_SB(sb)->clumpablks = 1;
 141        HFS_SB(sb)->fs_start = (be16_to_cpu(mdb->drAlBlSt) + part_start) >>
 142                               (sb->s_blocksize_bits - HFS_SECTOR_SIZE_BITS);
 143
 144        /* These parameters are read from and written to the MDB */
 145        HFS_SB(sb)->free_ablocks = be16_to_cpu(mdb->drFreeBks);
 146        HFS_SB(sb)->next_id = be32_to_cpu(mdb->drNxtCNID);
 147        HFS_SB(sb)->root_files = be16_to_cpu(mdb->drNmFls);
 148        HFS_SB(sb)->root_dirs = be16_to_cpu(mdb->drNmRtDirs);
 149        HFS_SB(sb)->file_count = be32_to_cpu(mdb->drFilCnt);
 150        HFS_SB(sb)->folder_count = be32_to_cpu(mdb->drDirCnt);
 151
 152        /* TRY to get the alternate (backup) MDB. */
 153        sect = part_start + part_size - 2;
 154        bh = sb_bread512(sb, sect, mdb2);
 155        if (bh) {
 156                if (mdb2->drSigWord == cpu_to_be16(HFS_SUPER_MAGIC)) {
 157                        HFS_SB(sb)->alt_mdb_bh = bh;
 158                        HFS_SB(sb)->alt_mdb = mdb2;
 159                } else
 160                        brelse(bh);
 161        }
 162
 163        if (!HFS_SB(sb)->alt_mdb) {
 164                printk(KERN_WARNING "hfs: unable to locate alternate MDB\n");
 165                printk(KERN_WARNING "hfs: continuing without an alternate MDB\n");
 166        }
 167
 168        HFS_SB(sb)->bitmap = (__be32 *)__get_free_pages(GFP_KERNEL, PAGE_SIZE < 8192 ? 1 : 0);
 169        if (!HFS_SB(sb)->bitmap)
 170                goto out;
 171
 172        /* read in the bitmap */
 173        block = be16_to_cpu(mdb->drVBMSt) + part_start;
 174        off = (loff_t)block << HFS_SECTOR_SIZE_BITS;
 175        size = (HFS_SB(sb)->fs_ablocks + 8) / 8;
 176        ptr = (u8 *)HFS_SB(sb)->bitmap;
 177        while (size) {
 178                bh = sb_bread(sb, off >> sb->s_blocksize_bits);
 179                if (!bh) {
 180                        printk(KERN_ERR "hfs: unable to read volume bitmap\n");
 181                        goto out;
 182                }
 183                off2 = off & (sb->s_blocksize - 1);
 184                len = min((int)sb->s_blocksize - off2, size);
 185                memcpy(ptr, bh->b_data + off2, len);
 186                brelse(bh);
 187                ptr += len;
 188                off += len;
 189                size -= len;
 190        }
 191
 192        HFS_SB(sb)->ext_tree = hfs_btree_open(sb, HFS_EXT_CNID, hfs_ext_keycmp);
 193        if (!HFS_SB(sb)->ext_tree) {
 194                printk(KERN_ERR "hfs: unable to open extent tree\n");
 195                goto out;
 196        }
 197        HFS_SB(sb)->cat_tree = hfs_btree_open(sb, HFS_CAT_CNID, hfs_cat_keycmp);
 198        if (!HFS_SB(sb)->cat_tree) {
 199                printk(KERN_ERR "hfs: unable to open catalog tree\n");
 200                goto out;
 201        }
 202
 203        attrib = mdb->drAtrb;
 204        if (!(attrib & cpu_to_be16(HFS_SB_ATTRIB_UNMNT))) {
 205                printk(KERN_WARNING "hfs: filesystem was not cleanly unmounted, "
 206                         "running fsck.hfs is recommended.  mounting read-only.\n");
 207                sb->s_flags |= MS_RDONLY;
 208        }
 209        if ((attrib & cpu_to_be16(HFS_SB_ATTRIB_SLOCK))) {
 210                printk(KERN_WARNING "hfs: filesystem is marked locked, mounting read-only.\n");
 211                sb->s_flags |= MS_RDONLY;
 212        }
 213        if (!(sb->s_flags & MS_RDONLY)) {
 214                /* Mark the volume uncleanly unmounted in case we crash */
 215                attrib &= cpu_to_be16(~HFS_SB_ATTRIB_UNMNT);
 216                attrib |= cpu_to_be16(HFS_SB_ATTRIB_INCNSTNT);
 217                mdb->drAtrb = attrib;
 218                be32_add_cpu(&mdb->drWrCnt, 1);
 219                mdb->drLsMod = hfs_mtime();
 220
 221                mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
 222                hfs_buffer_sync(HFS_SB(sb)->mdb_bh);
 223        }
 224
 225        return 0;
 226
 227out_bh:
 228        brelse(bh);
 229out:
 230        hfs_mdb_put(sb);
 231        return -EIO;
 232}
 233
 234/*
 235 * hfs_mdb_commit()
 236 *
 237 * Description:
 238 *   This updates the MDB on disk (look also at hfs_write_super()).
 239 *   It does not check, if the superblock has been modified, or
 240 *   if the filesystem has been mounted read-only. It is mainly
 241 *   called by hfs_write_super() and hfs_btree_extend().
 242 * Input Variable(s):
 243 *   struct hfs_mdb *mdb: Pointer to the hfs MDB
 244 *   int backup;
 245 * Output Variable(s):
 246 *   NONE
 247 * Returns:
 248 *   void
 249 * Preconditions:
 250 *   'mdb' points to a "valid" (struct hfs_mdb).
 251 * Postconditions:
 252 *   The HFS MDB and on disk will be updated, by copying the possibly
 253 *   modified fields from the in memory MDB (in native byte order) to
 254 *   the disk block buffer.
 255 *   If 'backup' is non-zero then the alternate MDB is also written
 256 *   and the function doesn't return until it is actually on disk.
 257 */
 258void hfs_mdb_commit(struct super_block *sb)
 259{
 260        struct hfs_mdb *mdb = HFS_SB(sb)->mdb;
 261
 262        if (test_and_clear_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags)) {
 263                /* These parameters may have been modified, so write them back */
 264                mdb->drLsMod = hfs_mtime();
 265                mdb->drFreeBks = cpu_to_be16(HFS_SB(sb)->free_ablocks);
 266                mdb->drNxtCNID = cpu_to_be32(HFS_SB(sb)->next_id);
 267                mdb->drNmFls = cpu_to_be16(HFS_SB(sb)->root_files);
 268                mdb->drNmRtDirs = cpu_to_be16(HFS_SB(sb)->root_dirs);
 269                mdb->drFilCnt = cpu_to_be32(HFS_SB(sb)->file_count);
 270                mdb->drDirCnt = cpu_to_be32(HFS_SB(sb)->folder_count);
 271
 272                /* write MDB to disk */
 273                mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
 274        }
 275
 276        /* write the backup MDB, not returning until it is written.
 277         * we only do this when either the catalog or extents overflow
 278         * files grow. */
 279        if (test_and_clear_bit(HFS_FLG_ALT_MDB_DIRTY, &HFS_SB(sb)->flags) &&
 280            HFS_SB(sb)->alt_mdb) {
 281                hfs_inode_write_fork(HFS_SB(sb)->ext_tree->inode, mdb->drXTExtRec,
 282                                     &mdb->drXTFlSize, NULL);
 283                hfs_inode_write_fork(HFS_SB(sb)->cat_tree->inode, mdb->drCTExtRec,
 284                                     &mdb->drCTFlSize, NULL);
 285                memcpy(HFS_SB(sb)->alt_mdb, HFS_SB(sb)->mdb, HFS_SECTOR_SIZE);
 286                HFS_SB(sb)->alt_mdb->drAtrb |= cpu_to_be16(HFS_SB_ATTRIB_UNMNT);
 287                HFS_SB(sb)->alt_mdb->drAtrb &= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT);
 288                mark_buffer_dirty(HFS_SB(sb)->alt_mdb_bh);
 289                hfs_buffer_sync(HFS_SB(sb)->alt_mdb_bh);
 290        }
 291
 292        if (test_and_clear_bit(HFS_FLG_BITMAP_DIRTY, &HFS_SB(sb)->flags)) {
 293                struct buffer_head *bh;
 294                sector_t block;
 295                char *ptr;
 296                int off, size, len;
 297
 298                block = be16_to_cpu(HFS_SB(sb)->mdb->drVBMSt) + HFS_SB(sb)->part_start;
 299                off = (block << HFS_SECTOR_SIZE_BITS) & (sb->s_blocksize - 1);
 300                block >>= sb->s_blocksize_bits - HFS_SECTOR_SIZE_BITS;
 301                size = (HFS_SB(sb)->fs_ablocks + 7) / 8;
 302                ptr = (u8 *)HFS_SB(sb)->bitmap;
 303                while (size) {
 304                        bh = sb_bread(sb, block);
 305                        if (!bh) {
 306                                printk(KERN_ERR "hfs: unable to read volume bitmap\n");
 307                                break;
 308                        }
 309                        len = min((int)sb->s_blocksize - off, size);
 310                        memcpy(bh->b_data + off, ptr, len);
 311                        mark_buffer_dirty(bh);
 312                        brelse(bh);
 313                        block++;
 314                        off = 0;
 315                        ptr += len;
 316                        size -= len;
 317                }
 318        }
 319}
 320
 321void hfs_mdb_close(struct super_block *sb)
 322{
 323        /* update volume attributes */
 324        if (sb->s_flags & MS_RDONLY)
 325                return;
 326        HFS_SB(sb)->mdb->drAtrb |= cpu_to_be16(HFS_SB_ATTRIB_UNMNT);
 327        HFS_SB(sb)->mdb->drAtrb &= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT);
 328        mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
 329}
 330
 331/*
 332 * hfs_mdb_put()
 333 *
 334 * Release the resources associated with the in-core MDB.  */
 335void hfs_mdb_put(struct super_block *sb)
 336{
 337        if (!HFS_SB(sb))
 338                return;
 339        /* free the B-trees */
 340        hfs_btree_close(HFS_SB(sb)->ext_tree);
 341        hfs_btree_close(HFS_SB(sb)->cat_tree);
 342
 343        /* free the buffers holding the primary and alternate MDBs */
 344        brelse(HFS_SB(sb)->mdb_bh);
 345        brelse(HFS_SB(sb)->alt_mdb_bh);
 346
 347        unload_nls(HFS_SB(sb)->nls_io);
 348        unload_nls(HFS_SB(sb)->nls_disk);
 349
 350        free_pages((unsigned long)HFS_SB(sb)->bitmap, PAGE_SIZE < 8192 ? 1 : 0);
 351        kfree(HFS_SB(sb));
 352        sb->s_fs_info = NULL;
 353}
 354