mdb.c 10 KB

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