wrapper.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * linux/fs/hfsplus/wrapper.c
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. *
  8. * Handling of HFS wrappers around HFS+ volumes
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/cdrom.h>
  13. #include <linux/genhd.h>
  14. #include <linux/version.h>
  15. #include <asm/unaligned.h>
  16. #include "hfsplus_fs.h"
  17. #include "hfsplus_raw.h"
  18. struct hfsplus_wd {
  19. u32 ablk_size;
  20. u16 ablk_start;
  21. u16 embed_start;
  22. u16 embed_count;
  23. };
  24. static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
  25. {
  26. u32 extent;
  27. u16 attrib;
  28. if (be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG)) != HFSPLUS_VOLHEAD_SIG)
  29. return 0;
  30. attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
  31. if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
  32. !(attrib & HFSP_WRAP_ATTRIB_SPARED))
  33. return 0;
  34. wd->ablk_size = be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
  35. if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
  36. return 0;
  37. if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
  38. return 0;
  39. wd->ablk_start = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
  40. extent = be32_to_cpu(get_unaligned((__be32 *)(bufptr + HFSP_WRAPOFF_EMBEDEXT)));
  41. wd->embed_start = (extent >> 16) & 0xFFFF;
  42. wd->embed_count = extent & 0xFFFF;
  43. return 1;
  44. }
  45. static int hfsplus_get_last_session(struct super_block *sb,
  46. sector_t *start, sector_t *size)
  47. {
  48. struct cdrom_multisession ms_info;
  49. struct cdrom_tocentry te;
  50. int res;
  51. /* default values */
  52. *start = 0;
  53. *size = sb->s_bdev->bd_inode->i_size >> 9;
  54. if (HFSPLUS_SB(sb).session >= 0) {
  55. te.cdte_track = HFSPLUS_SB(sb).session;
  56. te.cdte_format = CDROM_LBA;
  57. res = ioctl_by_bdev(sb->s_bdev, CDROMREADTOCENTRY, (unsigned long)&te);
  58. if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
  59. *start = (sector_t)te.cdte_addr.lba << 2;
  60. return 0;
  61. }
  62. printk(KERN_ERR "HFS: Invalid session number or type of track\n");
  63. return -EINVAL;
  64. }
  65. ms_info.addr_format = CDROM_LBA;
  66. res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, (unsigned long)&ms_info);
  67. if (!res && ms_info.xa_flag)
  68. *start = (sector_t)ms_info.addr.lba << 2;
  69. return 0;
  70. }
  71. /* Find the volume header and fill in some minimum bits in superblock */
  72. /* Takes in super block, returns true if good data read */
  73. int hfsplus_read_wrapper(struct super_block *sb)
  74. {
  75. struct buffer_head *bh;
  76. struct hfsplus_vh *vhdr;
  77. struct hfsplus_wd wd;
  78. sector_t part_start, part_size;
  79. u32 blocksize;
  80. blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
  81. if (!blocksize)
  82. return -EINVAL;
  83. if (hfsplus_get_last_session(sb, &part_start, &part_size))
  84. return -EINVAL;
  85. while (1) {
  86. bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
  87. if (!bh)
  88. return -EIO;
  89. if (vhdr->signature == cpu_to_be16(HFSP_WRAP_MAGIC)) {
  90. if (!hfsplus_read_mdb(vhdr, &wd))
  91. goto error;
  92. wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
  93. part_start += wd.ablk_start + wd.embed_start * wd.ablk_size;
  94. part_size = wd.embed_count * wd.ablk_size;
  95. brelse(bh);
  96. bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
  97. if (!bh)
  98. return -EIO;
  99. }
  100. if (vhdr->signature == cpu_to_be16(HFSPLUS_VOLHEAD_SIG))
  101. break;
  102. brelse(bh);
  103. /* check for a partition block
  104. * (should do this only for cdrom/loop though)
  105. */
  106. if (hfs_part_find(sb, &part_start, &part_size))
  107. return -EINVAL;
  108. }
  109. blocksize = be32_to_cpu(vhdr->blocksize);
  110. brelse(bh);
  111. /* block size must be at least as large as a sector
  112. * and a multiple of 2
  113. */
  114. if (blocksize < HFSPLUS_SECTOR_SIZE ||
  115. ((blocksize - 1) & blocksize))
  116. return -EINVAL;
  117. HFSPLUS_SB(sb).alloc_blksz = blocksize;
  118. HFSPLUS_SB(sb).alloc_blksz_shift = 0;
  119. while ((blocksize >>= 1) != 0)
  120. HFSPLUS_SB(sb).alloc_blksz_shift++;
  121. blocksize = min(HFSPLUS_SB(sb).alloc_blksz, (u32)PAGE_SIZE);
  122. /* align block size to block offset */
  123. while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
  124. blocksize >>= 1;
  125. if (sb_set_blocksize(sb, blocksize) != blocksize) {
  126. printk("HFS+: unable to blocksize to %u!\n", blocksize);
  127. return -EINVAL;
  128. }
  129. HFSPLUS_SB(sb).blockoffset = part_start >>
  130. (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
  131. HFSPLUS_SB(sb).sect_count = part_size;
  132. HFSPLUS_SB(sb).fs_shift = HFSPLUS_SB(sb).alloc_blksz_shift -
  133. sb->s_blocksize_bits;
  134. bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
  135. if (!bh)
  136. return -EIO;
  137. /* should still be the same... */
  138. if (be16_to_cpu(vhdr->signature) != HFSPLUS_VOLHEAD_SIG)
  139. goto error;
  140. HFSPLUS_SB(sb).s_vhbh = bh;
  141. HFSPLUS_SB(sb).s_vhdr = vhdr;
  142. return 0;
  143. error:
  144. brelse(bh);
  145. return -EINVAL;
  146. }