wrapper.c 4.7 KB

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