mdb.c 10.0 KB

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