mdb.c 10 KB

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