Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  linux/fs/hfs/mdb.c
0003  *
0004  * Copyright (C) 1995-1997  Paul H. Hargrove
0005  * (C) 2003 Ardis Technologies <roman@ardistech.com>
0006  * This file may be distributed under the terms of the GNU General Public License.
0007  *
0008  * This file contains functions for reading/writing the MDB.
0009  */
0010 
0011 #include <linux/cdrom.h>
0012 #include <linux/blkdev.h>
0013 #include <linux/nls.h>
0014 #include <linux/slab.h>
0015 
0016 #include "hfs_fs.h"
0017 #include "btree.h"
0018 
0019 /*================ File-local data types ================*/
0020 
0021 /*
0022  * The HFS Master Directory Block (MDB).
0023  *
0024  * Also known as the Volume Information Block (VIB), this structure is
0025  * the HFS equivalent of a superblock.
0026  *
0027  * Reference: _Inside Macintosh: Files_ pages 2-59 through 2-62
0028  *
0029  * modified for HFS Extended
0030  */
0031 
0032 static int hfs_get_last_session(struct super_block *sb,
0033                 sector_t *start, sector_t *size)
0034 {
0035     struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
0036 
0037     /* default values */
0038     *start = 0;
0039     *size = bdev_nr_sectors(sb->s_bdev);
0040 
0041     if (HFS_SB(sb)->session >= 0) {
0042         struct cdrom_tocentry te;
0043     
0044         if (!cdi)
0045             return -EINVAL;
0046 
0047         te.cdte_track = HFS_SB(sb)->session;
0048         te.cdte_format = CDROM_LBA;
0049         if (cdrom_read_tocentry(cdi, &te) ||
0050             (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
0051             pr_err("invalid session number or type of track\n");
0052             return -EINVAL;
0053         }
0054 
0055         *start = (sector_t)te.cdte_addr.lba << 2;
0056     } else if (cdi) {
0057         struct cdrom_multisession ms_info;
0058 
0059         ms_info.addr_format = CDROM_LBA;
0060         if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
0061             *start = (sector_t)ms_info.addr.lba << 2;
0062     }
0063 
0064     return 0;
0065 }
0066 
0067 /*
0068  * hfs_mdb_get()
0069  *
0070  * Build the in-core MDB for a filesystem, including
0071  * the B-trees and the volume bitmap.
0072  */
0073 int hfs_mdb_get(struct super_block *sb)
0074 {
0075     struct buffer_head *bh;
0076     struct hfs_mdb *mdb, *mdb2;
0077     unsigned int block;
0078     char *ptr;
0079     int off2, len, size, sect;
0080     sector_t part_start, part_size;
0081     loff_t off;
0082     __be16 attrib;
0083 
0084     /* set the device driver to 512-byte blocks */
0085     size = sb_min_blocksize(sb, HFS_SECTOR_SIZE);
0086     if (!size)
0087         return -EINVAL;
0088 
0089     if (hfs_get_last_session(sb, &part_start, &part_size))
0090         return -EINVAL;
0091     while (1) {
0092         /* See if this is an HFS filesystem */
0093         bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
0094         if (!bh)
0095             goto out;
0096 
0097         if (mdb->drSigWord == cpu_to_be16(HFS_SUPER_MAGIC))
0098             break;
0099         brelse(bh);
0100 
0101         /* check for a partition block
0102          * (should do this only for cdrom/loop though)
0103          */
0104         if (hfs_part_find(sb, &part_start, &part_size))
0105             goto out;
0106     }
0107 
0108     HFS_SB(sb)->alloc_blksz = size = be32_to_cpu(mdb->drAlBlkSiz);
0109     if (!size || (size & (HFS_SECTOR_SIZE - 1))) {
0110         pr_err("bad allocation block size %d\n", size);
0111         goto out_bh;
0112     }
0113 
0114     size = min(HFS_SB(sb)->alloc_blksz, (u32)PAGE_SIZE);
0115     /* size must be a multiple of 512 */
0116     while (size & (size - 1))
0117         size -= HFS_SECTOR_SIZE;
0118     sect = be16_to_cpu(mdb->drAlBlSt) + part_start;
0119     /* align block size to first sector */
0120     while (sect & ((size - 1) >> HFS_SECTOR_SIZE_BITS))
0121         size >>= 1;
0122     /* align block size to weird alloc size */
0123     while (HFS_SB(sb)->alloc_blksz & (size - 1))
0124         size >>= 1;
0125     brelse(bh);
0126     if (!sb_set_blocksize(sb, size)) {
0127         pr_err("unable to set blocksize to %u\n", size);
0128         goto out;
0129     }
0130 
0131     bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
0132     if (!bh)
0133         goto out;
0134     if (mdb->drSigWord != cpu_to_be16(HFS_SUPER_MAGIC))
0135         goto out_bh;
0136 
0137     HFS_SB(sb)->mdb_bh = bh;
0138     HFS_SB(sb)->mdb = mdb;
0139 
0140     /* These parameters are read from the MDB, and never written */
0141     HFS_SB(sb)->part_start = part_start;
0142     HFS_SB(sb)->fs_ablocks = be16_to_cpu(mdb->drNmAlBlks);
0143     HFS_SB(sb)->fs_div = HFS_SB(sb)->alloc_blksz >> sb->s_blocksize_bits;
0144     HFS_SB(sb)->clumpablks = be32_to_cpu(mdb->drClpSiz) /
0145                  HFS_SB(sb)->alloc_blksz;
0146     if (!HFS_SB(sb)->clumpablks)
0147         HFS_SB(sb)->clumpablks = 1;
0148     HFS_SB(sb)->fs_start = (be16_to_cpu(mdb->drAlBlSt) + part_start) >>
0149                    (sb->s_blocksize_bits - HFS_SECTOR_SIZE_BITS);
0150 
0151     /* These parameters are read from and written to the MDB */
0152     HFS_SB(sb)->free_ablocks = be16_to_cpu(mdb->drFreeBks);
0153     HFS_SB(sb)->next_id = be32_to_cpu(mdb->drNxtCNID);
0154     HFS_SB(sb)->root_files = be16_to_cpu(mdb->drNmFls);
0155     HFS_SB(sb)->root_dirs = be16_to_cpu(mdb->drNmRtDirs);
0156     HFS_SB(sb)->file_count = be32_to_cpu(mdb->drFilCnt);
0157     HFS_SB(sb)->folder_count = be32_to_cpu(mdb->drDirCnt);
0158 
0159     /* TRY to get the alternate (backup) MDB. */
0160     sect = part_start + part_size - 2;
0161     bh = sb_bread512(sb, sect, mdb2);
0162     if (bh) {
0163         if (mdb2->drSigWord == cpu_to_be16(HFS_SUPER_MAGIC)) {
0164             HFS_SB(sb)->alt_mdb_bh = bh;
0165             HFS_SB(sb)->alt_mdb = mdb2;
0166         } else
0167             brelse(bh);
0168     }
0169 
0170     if (!HFS_SB(sb)->alt_mdb) {
0171         pr_warn("unable to locate alternate MDB\n");
0172         pr_warn("continuing without an alternate MDB\n");
0173     }
0174 
0175     HFS_SB(sb)->bitmap = kmalloc(8192, GFP_KERNEL);
0176     if (!HFS_SB(sb)->bitmap)
0177         goto out;
0178 
0179     /* read in the bitmap */
0180     block = be16_to_cpu(mdb->drVBMSt) + part_start;
0181     off = (loff_t)block << HFS_SECTOR_SIZE_BITS;
0182     size = (HFS_SB(sb)->fs_ablocks + 8) / 8;
0183     ptr = (u8 *)HFS_SB(sb)->bitmap;
0184     while (size) {
0185         bh = sb_bread(sb, off >> sb->s_blocksize_bits);
0186         if (!bh) {
0187             pr_err("unable to read volume bitmap\n");
0188             goto out;
0189         }
0190         off2 = off & (sb->s_blocksize - 1);
0191         len = min((int)sb->s_blocksize - off2, size);
0192         memcpy(ptr, bh->b_data + off2, len);
0193         brelse(bh);
0194         ptr += len;
0195         off += len;
0196         size -= len;
0197     }
0198 
0199     HFS_SB(sb)->ext_tree = hfs_btree_open(sb, HFS_EXT_CNID, hfs_ext_keycmp);
0200     if (!HFS_SB(sb)->ext_tree) {
0201         pr_err("unable to open extent tree\n");
0202         goto out;
0203     }
0204     HFS_SB(sb)->cat_tree = hfs_btree_open(sb, HFS_CAT_CNID, hfs_cat_keycmp);
0205     if (!HFS_SB(sb)->cat_tree) {
0206         pr_err("unable to open catalog tree\n");
0207         goto out;
0208     }
0209 
0210     attrib = mdb->drAtrb;
0211     if (!(attrib & cpu_to_be16(HFS_SB_ATTRIB_UNMNT))) {
0212         pr_warn("filesystem was not cleanly unmounted, running fsck.hfs is recommended.  mounting read-only.\n");
0213         sb->s_flags |= SB_RDONLY;
0214     }
0215     if ((attrib & cpu_to_be16(HFS_SB_ATTRIB_SLOCK))) {
0216         pr_warn("filesystem is marked locked, mounting read-only.\n");
0217         sb->s_flags |= SB_RDONLY;
0218     }
0219     if (!sb_rdonly(sb)) {
0220         /* Mark the volume uncleanly unmounted in case we crash */
0221         attrib &= cpu_to_be16(~HFS_SB_ATTRIB_UNMNT);
0222         attrib |= cpu_to_be16(HFS_SB_ATTRIB_INCNSTNT);
0223         mdb->drAtrb = attrib;
0224         be32_add_cpu(&mdb->drWrCnt, 1);
0225         mdb->drLsMod = hfs_mtime();
0226 
0227         mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
0228         sync_dirty_buffer(HFS_SB(sb)->mdb_bh);
0229     }
0230 
0231     return 0;
0232 
0233 out_bh:
0234     brelse(bh);
0235 out:
0236     hfs_mdb_put(sb);
0237     return -EIO;
0238 }
0239 
0240 /*
0241  * hfs_mdb_commit()
0242  *
0243  * Description:
0244  *   This updates the MDB on disk.
0245  *   It does not check, if the superblock has been modified, or
0246  *   if the filesystem has been mounted read-only. It is mainly
0247  *   called by hfs_sync_fs() and flush_mdb().
0248  * Input Variable(s):
0249  *   struct hfs_mdb *mdb: Pointer to the hfs MDB
0250  *   int backup;
0251  * Output Variable(s):
0252  *   NONE
0253  * Returns:
0254  *   void
0255  * Preconditions:
0256  *   'mdb' points to a "valid" (struct hfs_mdb).
0257  * Postconditions:
0258  *   The HFS MDB and on disk will be updated, by copying the possibly
0259  *   modified fields from the in memory MDB (in native byte order) to
0260  *   the disk block buffer.
0261  *   If 'backup' is non-zero then the alternate MDB is also written
0262  *   and the function doesn't return until it is actually on disk.
0263  */
0264 void hfs_mdb_commit(struct super_block *sb)
0265 {
0266     struct hfs_mdb *mdb = HFS_SB(sb)->mdb;
0267 
0268     if (sb_rdonly(sb))
0269         return;
0270 
0271     lock_buffer(HFS_SB(sb)->mdb_bh);
0272     if (test_and_clear_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags)) {
0273         /* These parameters may have been modified, so write them back */
0274         mdb->drLsMod = hfs_mtime();
0275         mdb->drFreeBks = cpu_to_be16(HFS_SB(sb)->free_ablocks);
0276         mdb->drNxtCNID = cpu_to_be32(HFS_SB(sb)->next_id);
0277         mdb->drNmFls = cpu_to_be16(HFS_SB(sb)->root_files);
0278         mdb->drNmRtDirs = cpu_to_be16(HFS_SB(sb)->root_dirs);
0279         mdb->drFilCnt = cpu_to_be32(HFS_SB(sb)->file_count);
0280         mdb->drDirCnt = cpu_to_be32(HFS_SB(sb)->folder_count);
0281 
0282         /* write MDB to disk */
0283         mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
0284     }
0285 
0286     /* write the backup MDB, not returning until it is written.
0287      * we only do this when either the catalog or extents overflow
0288      * files grow. */
0289     if (test_and_clear_bit(HFS_FLG_ALT_MDB_DIRTY, &HFS_SB(sb)->flags) &&
0290         HFS_SB(sb)->alt_mdb) {
0291         hfs_inode_write_fork(HFS_SB(sb)->ext_tree->inode, mdb->drXTExtRec,
0292                      &mdb->drXTFlSize, NULL);
0293         hfs_inode_write_fork(HFS_SB(sb)->cat_tree->inode, mdb->drCTExtRec,
0294                      &mdb->drCTFlSize, NULL);
0295 
0296         lock_buffer(HFS_SB(sb)->alt_mdb_bh);
0297         memcpy(HFS_SB(sb)->alt_mdb, HFS_SB(sb)->mdb, HFS_SECTOR_SIZE);
0298         HFS_SB(sb)->alt_mdb->drAtrb |= cpu_to_be16(HFS_SB_ATTRIB_UNMNT);
0299         HFS_SB(sb)->alt_mdb->drAtrb &= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT);
0300         unlock_buffer(HFS_SB(sb)->alt_mdb_bh);
0301 
0302         mark_buffer_dirty(HFS_SB(sb)->alt_mdb_bh);
0303         sync_dirty_buffer(HFS_SB(sb)->alt_mdb_bh);
0304     }
0305 
0306     if (test_and_clear_bit(HFS_FLG_BITMAP_DIRTY, &HFS_SB(sb)->flags)) {
0307         struct buffer_head *bh;
0308         sector_t block;
0309         char *ptr;
0310         int off, size, len;
0311 
0312         block = be16_to_cpu(HFS_SB(sb)->mdb->drVBMSt) + HFS_SB(sb)->part_start;
0313         off = (block << HFS_SECTOR_SIZE_BITS) & (sb->s_blocksize - 1);
0314         block >>= sb->s_blocksize_bits - HFS_SECTOR_SIZE_BITS;
0315         size = (HFS_SB(sb)->fs_ablocks + 7) / 8;
0316         ptr = (u8 *)HFS_SB(sb)->bitmap;
0317         while (size) {
0318             bh = sb_bread(sb, block);
0319             if (!bh) {
0320                 pr_err("unable to read volume bitmap\n");
0321                 break;
0322             }
0323             len = min((int)sb->s_blocksize - off, size);
0324 
0325             lock_buffer(bh);
0326             memcpy(bh->b_data + off, ptr, len);
0327             unlock_buffer(bh);
0328 
0329             mark_buffer_dirty(bh);
0330             brelse(bh);
0331             block++;
0332             off = 0;
0333             ptr += len;
0334             size -= len;
0335         }
0336     }
0337     unlock_buffer(HFS_SB(sb)->mdb_bh);
0338 }
0339 
0340 void hfs_mdb_close(struct super_block *sb)
0341 {
0342     /* update volume attributes */
0343     if (sb_rdonly(sb))
0344         return;
0345     HFS_SB(sb)->mdb->drAtrb |= cpu_to_be16(HFS_SB_ATTRIB_UNMNT);
0346     HFS_SB(sb)->mdb->drAtrb &= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT);
0347     mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
0348 }
0349 
0350 /*
0351  * hfs_mdb_put()
0352  *
0353  * Release the resources associated with the in-core MDB.  */
0354 void hfs_mdb_put(struct super_block *sb)
0355 {
0356     if (!HFS_SB(sb))
0357         return;
0358     /* free the B-trees */
0359     hfs_btree_close(HFS_SB(sb)->ext_tree);
0360     hfs_btree_close(HFS_SB(sb)->cat_tree);
0361 
0362     /* free the buffers holding the primary and alternate MDBs */
0363     brelse(HFS_SB(sb)->mdb_bh);
0364     brelse(HFS_SB(sb)->alt_mdb_bh);
0365 
0366     unload_nls(HFS_SB(sb)->nls_io);
0367     unload_nls(HFS_SB(sb)->nls_disk);
0368 
0369     kfree(HFS_SB(sb)->bitmap);
0370     kfree(HFS_SB(sb));
0371     sb->s_fs_info = NULL;
0372 }