wrapper.c 4.5 KB

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