Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Roman Zippel | 328b922 | 2005-09-06 15:18:49 -0700 | [diff] [blame] | 13 | #include <linux/nls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 31 | static 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 | } |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 50 | printk(KERN_ERR "hfs: invalid session number or type of track\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 66 | int 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))) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 103 | printk(KERN_ERR "hfs: bad allocation block size %d\n", size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 120 | printk(KERN_ERR "hfs: unable to set blocksize to %u\n", size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 164 | printk(KERN_WARNING "hfs: unable to locate alternate MDB\n"); |
| 165 | printk(KERN_WARNING "hfs: continuing without an alternate MDB\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 180 | printk(KERN_ERR "hfs: unable to read volume bitmap\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 194 | printk(KERN_ERR "hfs: unable to open extent tree\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 199 | printk(KERN_ERR "hfs: unable to open catalog tree\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | goto out; |
| 201 | } |
| 202 | |
| 203 | attrib = mdb->drAtrb; |
| 204 | if (!(attrib & cpu_to_be16(HFS_SB_ATTRIB_UNMNT))) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 205 | printk(KERN_WARNING "hfs: filesystem was not cleanly unmounted, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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))) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 210 | printk(KERN_WARNING "hfs: filesystem is marked locked, mounting read-only.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Marcin Slusarz | 20c79e7 | 2008-04-30 00:54:47 -0700 | [diff] [blame] | 218 | be32_add_cpu(&mdb->drWrCnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 227 | out_bh: |
| 228 | brelse(bh); |
| 229 | out: |
| 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 | */ |
| 258 | void 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) { |
Roman Zippel | 7cf3cc3 | 2006-01-18 17:43:07 -0800 | [diff] [blame] | 306 | printk(KERN_ERR "hfs: unable to read volume bitmap\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 321 | void 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. */ |
| 335 | void hfs_mdb_put(struct super_block *sb) |
| 336 | { |
Colin Leroy | 945b092 | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 337 | if (!HFS_SB(sb)) |
| 338 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Colin Leroy | 945b092 | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 346 | |
Roman Zippel | 328b922 | 2005-09-06 15:18:49 -0700 | [diff] [blame] | 347 | if (HFS_SB(sb)->nls_io) |
| 348 | unload_nls(HFS_SB(sb)->nls_io); |
| 349 | if (HFS_SB(sb)->nls_disk) |
| 350 | unload_nls(HFS_SB(sb)->nls_disk); |
| 351 | |
Dave Anderson | eb2e5f4 | 2009-04-13 14:39:46 -0700 | [diff] [blame] | 352 | free_pages((unsigned long)HFS_SB(sb)->bitmap, PAGE_SIZE < 8192 ? 1 : 0); |
Colin Leroy | 945b092 | 2005-05-01 08:59:16 -0700 | [diff] [blame] | 353 | kfree(HFS_SB(sb)); |
| 354 | sb->s_fs_info = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |