mdb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <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_multisession ms_info;
  31. struct cdrom_tocentry te;
  32. int res;
  33. /* default values */
  34. *start = 0;
  35. *size = sb->s_bdev->bd_inode->i_size >> 9;
  36. if (HFS_SB(sb)->session >= 0) {
  37. te.cdte_track = HFS_SB(sb)->session;
  38. te.cdte_format = CDROM_LBA;
  39. res = ioctl_by_bdev(sb->s_bdev, CDROMREADTOCENTRY, (unsigned long)&te);
  40. if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
  41. *start = (sector_t)te.cdte_addr.lba << 2;
  42. return 0;
  43. }
  44. pr_err("invalid session number or type of track\n");
  45. return -EINVAL;
  46. }
  47. ms_info.addr_format = CDROM_LBA;
  48. res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, (unsigned long)&ms_info);
  49. if (!res && ms_info.xa_flag)
  50. *start = (sector_t)ms_info.addr.lba << 2;
  51. return 0;
  52. }
  53. /*
  54. * hfs_mdb_get()
  55. *
  56. * Build the in-core MDB for a filesystem, including
  57. * the B-trees and the volume bitmap.
  58. */
  59. int hfs_mdb_get(struct super_block *sb)
  60. {
  61. struct buffer_head *bh;
  62. struct hfs_mdb *mdb, *mdb2;
  63. unsigned int block;
  64. char *ptr;
  65. int off2, len, size, sect;
  66. sector_t part_start, part_size;
  67. loff_t off;
  68. __be16 attrib;
  69. /* set the device driver to 512-byte blocks */
  70. size = sb_min_blocksize(sb, HFS_SECTOR_SIZE);
  71. if (!size)
  72. return -EINVAL;
  73. if (hfs_get_last_session(sb, &part_start, &part_size))
  74. return -EINVAL;
  75. while (1) {
  76. /* See if this is an HFS filesystem */
  77. bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
  78. if (!bh)
  79. goto out;
  80. if (mdb->drSigWord == cpu_to_be16(HFS_SUPER_MAGIC))
  81. break;
  82. brelse(bh);
  83. /* check for a partition block
  84. * (should do this only for cdrom/loop though)
  85. */
  86. if (hfs_part_find(sb, &part_start, &part_size))
  87. goto out;
  88. }
  89. HFS_SB(sb)->alloc_blksz = size = be32_to_cpu(mdb->drAlBlkSiz);
  90. if (!size || (size & (HFS_SECTOR_SIZE - 1))) {
  91. pr_err("bad allocation block size %d\n", size);
  92. goto out_bh;
  93. }
  94. size = min(HFS_SB(sb)->alloc_blksz, (u32)PAGE_SIZE);
  95. /* size must be a multiple of 512 */
  96. while (size & (size - 1))
  97. size -= HFS_SECTOR_SIZE;
  98. sect = be16_to_cpu(mdb->drAlBlSt) + part_start;
  99. /* align block size to first sector */
  100. while (sect & ((size - 1) >> HFS_SECTOR_SIZE_BITS))
  101. size >>= 1;
  102. /* align block size to weird alloc size */
  103. while (HFS_SB(sb)->alloc_blksz & (size - 1))
  104. size >>= 1;
  105. brelse(bh);
  106. if (!sb_set_blocksize(sb, size)) {
  107. pr_err("unable to set blocksize to %u\n", size);
  108. goto out;
  109. }
  110. bh = sb_bread512(sb, part_start + HFS_MDB_BLK, mdb);
  111. if (!bh)
  112. goto out;
  113. if (mdb->drSigWord != cpu_to_be16(HFS_SUPER_MAGIC))
  114. goto out_bh;
  115. HFS_SB(sb)->mdb_bh = bh;
  116. HFS_SB(sb)->mdb = mdb;
  117. /* These parameters are read from the MDB, and never written */
  118. HFS_SB(sb)->part_start = part_start;
  119. HFS_SB(sb)->fs_ablocks = be16_to_cpu(mdb->drNmAlBlks);
  120. HFS_SB(sb)->fs_div = HFS_SB(sb)->alloc_blksz >> sb->s_blocksize_bits;
  121. HFS_SB(sb)->clumpablks = be32_to_cpu(mdb->drClpSiz) /
  122. HFS_SB(sb)->alloc_blksz;
  123. if (!HFS_SB(sb)->clumpablks)
  124. HFS_SB(sb)->clumpablks = 1;
  125. HFS_SB(sb)->fs_start = (be16_to_cpu(mdb->drAlBlSt) + part_start) >>
  126. (sb->s_blocksize_bits - HFS_SECTOR_SIZE_BITS);
  127. /* These parameters are read from and written to the MDB */
  128. HFS_SB(sb)->free_ablocks = be16_to_cpu(mdb->drFreeBks);
  129. HFS_SB(sb)->next_id = be32_to_cpu(mdb->drNxtCNID);
  130. HFS_SB(sb)->root_files = be16_to_cpu(mdb->drNmFls);
  131. HFS_SB(sb)->root_dirs = be16_to_cpu(mdb->drNmRtDirs);
  132. HFS_SB(sb)->file_count = be32_to_cpu(mdb->drFilCnt);
  133. HFS_SB(sb)->folder_count = be32_to_cpu(mdb->drDirCnt);
  134. /* TRY to get the alternate (backup) MDB. */
  135. sect = part_start + part_size - 2;
  136. bh = sb_bread512(sb, sect, mdb2);
  137. if (bh) {
  138. if (mdb2->drSigWord == cpu_to_be16(HFS_SUPER_MAGIC)) {
  139. HFS_SB(sb)->alt_mdb_bh = bh;
  140. HFS_SB(sb)->alt_mdb = mdb2;
  141. } else
  142. brelse(bh);
  143. }
  144. if (!HFS_SB(sb)->alt_mdb) {
  145. pr_warn("unable to locate alternate MDB\n");
  146. pr_warn("continuing without an alternate MDB\n");
  147. }
  148. HFS_SB(sb)->bitmap = (__be32 *)__get_free_pages(GFP_KERNEL, PAGE_SIZE < 8192 ? 1 : 0);
  149. if (!HFS_SB(sb)->bitmap)
  150. goto out;
  151. /* read in the bitmap */
  152. block = be16_to_cpu(mdb->drVBMSt) + part_start;
  153. off = (loff_t)block << HFS_SECTOR_SIZE_BITS;
  154. size = (HFS_SB(sb)->fs_ablocks + 8) / 8;
  155. ptr = (u8 *)HFS_SB(sb)->bitmap;
  156. while (size) {
  157. bh = sb_bread(sb, off >> sb->s_blocksize_bits);
  158. if (!bh) {
  159. pr_err("unable to read volume bitmap\n");
  160. goto out;
  161. }
  162. off2 = off & (sb->s_blocksize - 1);
  163. len = min((int)sb->s_blocksize - off2, size);
  164. memcpy(ptr, bh->b_data + off2, len);
  165. brelse(bh);
  166. ptr += len;
  167. off += len;
  168. size -= len;
  169. }
  170. HFS_SB(sb)->ext_tree = hfs_btree_open(sb, HFS_EXT_CNID, hfs_ext_keycmp);
  171. if (!HFS_SB(sb)->ext_tree) {
  172. pr_err("unable to open extent tree\n");
  173. goto out;
  174. }
  175. HFS_SB(sb)->cat_tree = hfs_btree_open(sb, HFS_CAT_CNID, hfs_cat_keycmp);
  176. if (!HFS_SB(sb)->cat_tree) {
  177. pr_err("unable to open catalog tree\n");
  178. goto out;
  179. }
  180. attrib = mdb->drAtrb;
  181. if (!(attrib & cpu_to_be16(HFS_SB_ATTRIB_UNMNT))) {
  182. pr_warn("filesystem was not cleanly unmounted, 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. pr_warn("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. be32_add_cpu(&mdb->drWrCnt, 1);
  195. mdb->drLsMod = hfs_mtime();
  196. mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
  197. sync_dirty_buffer(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.
  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_sync_fs() and flush_mdb().
  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 (sb->s_flags & MS_RDONLY)
  234. return;
  235. lock_buffer(HFS_SB(sb)->mdb_bh);
  236. if (test_and_clear_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags)) {
  237. /* These parameters may have been modified, so write them back */
  238. mdb->drLsMod = hfs_mtime();
  239. mdb->drFreeBks = cpu_to_be16(HFS_SB(sb)->free_ablocks);
  240. mdb->drNxtCNID = cpu_to_be32(HFS_SB(sb)->next_id);
  241. mdb->drNmFls = cpu_to_be16(HFS_SB(sb)->root_files);
  242. mdb->drNmRtDirs = cpu_to_be16(HFS_SB(sb)->root_dirs);
  243. mdb->drFilCnt = cpu_to_be32(HFS_SB(sb)->file_count);
  244. mdb->drDirCnt = cpu_to_be32(HFS_SB(sb)->folder_count);
  245. /* write MDB to disk */
  246. mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
  247. }
  248. /* write the backup MDB, not returning until it is written.
  249. * we only do this when either the catalog or extents overflow
  250. * files grow. */
  251. if (test_and_clear_bit(HFS_FLG_ALT_MDB_DIRTY, &HFS_SB(sb)->flags) &&
  252. HFS_SB(sb)->alt_mdb) {
  253. hfs_inode_write_fork(HFS_SB(sb)->ext_tree->inode, mdb->drXTExtRec,
  254. &mdb->drXTFlSize, NULL);
  255. hfs_inode_write_fork(HFS_SB(sb)->cat_tree->inode, mdb->drCTExtRec,
  256. &mdb->drCTFlSize, NULL);
  257. lock_buffer(HFS_SB(sb)->alt_mdb_bh);
  258. memcpy(HFS_SB(sb)->alt_mdb, HFS_SB(sb)->mdb, HFS_SECTOR_SIZE);
  259. HFS_SB(sb)->alt_mdb->drAtrb |= cpu_to_be16(HFS_SB_ATTRIB_UNMNT);
  260. HFS_SB(sb)->alt_mdb->drAtrb &= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT);
  261. unlock_buffer(HFS_SB(sb)->alt_mdb_bh);
  262. mark_buffer_dirty(HFS_SB(sb)->alt_mdb_bh);
  263. sync_dirty_buffer(HFS_SB(sb)->alt_mdb_bh);
  264. }
  265. if (test_and_clear_bit(HFS_FLG_BITMAP_DIRTY, &HFS_SB(sb)->flags)) {
  266. struct buffer_head *bh;
  267. sector_t block;
  268. char *ptr;
  269. int off, size, len;
  270. block = be16_to_cpu(HFS_SB(sb)->mdb->drVBMSt) + HFS_SB(sb)->part_start;
  271. off = (block << HFS_SECTOR_SIZE_BITS) & (sb->s_blocksize - 1);
  272. block >>= sb->s_blocksize_bits - HFS_SECTOR_SIZE_BITS;
  273. size = (HFS_SB(sb)->fs_ablocks + 7) / 8;
  274. ptr = (u8 *)HFS_SB(sb)->bitmap;
  275. while (size) {
  276. bh = sb_bread(sb, block);
  277. if (!bh) {
  278. pr_err("unable to read volume bitmap\n");
  279. break;
  280. }
  281. len = min((int)sb->s_blocksize - off, size);
  282. lock_buffer(bh);
  283. memcpy(bh->b_data + off, ptr, len);
  284. unlock_buffer(bh);
  285. mark_buffer_dirty(bh);
  286. brelse(bh);
  287. block++;
  288. off = 0;
  289. ptr += len;
  290. size -= len;
  291. }
  292. }
  293. unlock_buffer(HFS_SB(sb)->mdb_bh);
  294. }
  295. void hfs_mdb_close(struct super_block *sb)
  296. {
  297. /* update volume attributes */
  298. if (sb->s_flags & MS_RDONLY)
  299. return;
  300. HFS_SB(sb)->mdb->drAtrb |= cpu_to_be16(HFS_SB_ATTRIB_UNMNT);
  301. HFS_SB(sb)->mdb->drAtrb &= cpu_to_be16(~HFS_SB_ATTRIB_INCNSTNT);
  302. mark_buffer_dirty(HFS_SB(sb)->mdb_bh);
  303. }
  304. /*
  305. * hfs_mdb_put()
  306. *
  307. * Release the resources associated with the in-core MDB. */
  308. void hfs_mdb_put(struct super_block *sb)
  309. {
  310. if (!HFS_SB(sb))
  311. return;
  312. /* free the B-trees */
  313. hfs_btree_close(HFS_SB(sb)->ext_tree);
  314. hfs_btree_close(HFS_SB(sb)->cat_tree);
  315. /* free the buffers holding the primary and alternate MDBs */
  316. brelse(HFS_SB(sb)->mdb_bh);
  317. brelse(HFS_SB(sb)->alt_mdb_bh);
  318. unload_nls(HFS_SB(sb)->nls_io);
  319. unload_nls(HFS_SB(sb)->nls_disk);
  320. free_pages((unsigned long)HFS_SB(sb)->bitmap, PAGE_SIZE < 8192 ? 1 : 0);
  321. kfree(HFS_SB(sb));
  322. sb->s_fs_info = NULL;
  323. }