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