misc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * misc.c
  3. *
  4. * PURPOSE
  5. * Miscellaneous routines for the OSTA-UDF(tm) filesystem.
  6. *
  7. * COPYRIGHT
  8. * This file is distributed under the terms of the GNU General Public
  9. * License (GPL). Copies of the GPL can be obtained from:
  10. * ftp://prep.ai.mit.edu/pub/gnu/GPL
  11. * Each contributing author retains all rights to their own work.
  12. *
  13. * (C) 1998 Dave Boynton
  14. * (C) 1998-2004 Ben Fennema
  15. * (C) 1999-2000 Stelias Computing Inc
  16. *
  17. * HISTORY
  18. *
  19. * 04/19/99 blf partial support for reading/writing specific EA's
  20. */
  21. #include "udfdecl.h"
  22. #include <linux/fs.h>
  23. #include <linux/string.h>
  24. #include <linux/buffer_head.h>
  25. #include <linux/crc-itu-t.h>
  26. #include "udf_i.h"
  27. #include "udf_sb.h"
  28. struct buffer_head *udf_tgetblk(struct super_block *sb, int block)
  29. {
  30. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
  31. return sb_getblk(sb, udf_fixed_to_variable(block));
  32. else
  33. return sb_getblk(sb, block);
  34. }
  35. struct buffer_head *udf_tread(struct super_block *sb, int block)
  36. {
  37. if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
  38. return sb_bread(sb, udf_fixed_to_variable(block));
  39. else
  40. return sb_bread(sb, block);
  41. }
  42. struct genericFormat *udf_add_extendedattr(struct inode *inode, uint32_t size,
  43. uint32_t type, uint8_t loc)
  44. {
  45. uint8_t *ea = NULL, *ad = NULL;
  46. int offset;
  47. uint16_t crclen;
  48. struct udf_inode_info *iinfo = UDF_I(inode);
  49. ea = iinfo->i_ext.i_data;
  50. if (iinfo->i_lenEAttr) {
  51. ad = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
  52. } else {
  53. ad = ea;
  54. size += sizeof(struct extendedAttrHeaderDesc);
  55. }
  56. offset = inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) -
  57. iinfo->i_lenAlloc;
  58. /* TODO - Check for FreeEASpace */
  59. if (loc & 0x01 && offset >= size) {
  60. struct extendedAttrHeaderDesc *eahd;
  61. eahd = (struct extendedAttrHeaderDesc *)ea;
  62. if (iinfo->i_lenAlloc)
  63. memmove(&ad[size], ad, iinfo->i_lenAlloc);
  64. if (iinfo->i_lenEAttr) {
  65. /* check checksum/crc */
  66. if (eahd->descTag.tagIdent !=
  67. cpu_to_le16(TAG_IDENT_EAHD) ||
  68. le32_to_cpu(eahd->descTag.tagLocation) !=
  69. iinfo->i_location.logicalBlockNum)
  70. return NULL;
  71. } else {
  72. struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
  73. size -= sizeof(struct extendedAttrHeaderDesc);
  74. iinfo->i_lenEAttr +=
  75. sizeof(struct extendedAttrHeaderDesc);
  76. eahd->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EAHD);
  77. if (sbi->s_udfrev >= 0x0200)
  78. eahd->descTag.descVersion = cpu_to_le16(3);
  79. else
  80. eahd->descTag.descVersion = cpu_to_le16(2);
  81. eahd->descTag.tagSerialNum =
  82. cpu_to_le16(sbi->s_serial_number);
  83. eahd->descTag.tagLocation = cpu_to_le32(
  84. iinfo->i_location.logicalBlockNum);
  85. eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
  86. eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
  87. }
  88. offset = iinfo->i_lenEAttr;
  89. if (type < 2048) {
  90. if (le32_to_cpu(eahd->appAttrLocation) <
  91. iinfo->i_lenEAttr) {
  92. uint32_t aal =
  93. le32_to_cpu(eahd->appAttrLocation);
  94. memmove(&ea[offset - aal + size],
  95. &ea[aal], offset - aal);
  96. offset -= aal;
  97. eahd->appAttrLocation =
  98. cpu_to_le32(aal + size);
  99. }
  100. if (le32_to_cpu(eahd->impAttrLocation) <
  101. iinfo->i_lenEAttr) {
  102. uint32_t ial =
  103. le32_to_cpu(eahd->impAttrLocation);
  104. memmove(&ea[offset - ial + size],
  105. &ea[ial], offset - ial);
  106. offset -= ial;
  107. eahd->impAttrLocation =
  108. cpu_to_le32(ial + size);
  109. }
  110. } else if (type < 65536) {
  111. if (le32_to_cpu(eahd->appAttrLocation) <
  112. iinfo->i_lenEAttr) {
  113. uint32_t aal =
  114. le32_to_cpu(eahd->appAttrLocation);
  115. memmove(&ea[offset - aal + size],
  116. &ea[aal], offset - aal);
  117. offset -= aal;
  118. eahd->appAttrLocation =
  119. cpu_to_le32(aal + size);
  120. }
  121. }
  122. /* rewrite CRC + checksum of eahd */
  123. crclen = sizeof(struct extendedAttrHeaderDesc) - sizeof(struct tag);
  124. eahd->descTag.descCRCLength = cpu_to_le16(crclen);
  125. eahd->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)eahd +
  126. sizeof(struct tag), crclen));
  127. eahd->descTag.tagChecksum = udf_tag_checksum(&eahd->descTag);
  128. iinfo->i_lenEAttr += size;
  129. return (struct genericFormat *)&ea[offset];
  130. }
  131. if (loc & 0x02)
  132. ;
  133. return NULL;
  134. }
  135. struct genericFormat *udf_get_extendedattr(struct inode *inode, uint32_t type,
  136. uint8_t subtype)
  137. {
  138. struct genericFormat *gaf;
  139. uint8_t *ea = NULL;
  140. uint32_t offset;
  141. struct udf_inode_info *iinfo = UDF_I(inode);
  142. ea = iinfo->i_ext.i_data;
  143. if (iinfo->i_lenEAttr) {
  144. struct extendedAttrHeaderDesc *eahd;
  145. eahd = (struct extendedAttrHeaderDesc *)ea;
  146. /* check checksum/crc */
  147. if (eahd->descTag.tagIdent !=
  148. cpu_to_le16(TAG_IDENT_EAHD) ||
  149. le32_to_cpu(eahd->descTag.tagLocation) !=
  150. iinfo->i_location.logicalBlockNum)
  151. return NULL;
  152. if (type < 2048)
  153. offset = sizeof(struct extendedAttrHeaderDesc);
  154. else if (type < 65536)
  155. offset = le32_to_cpu(eahd->impAttrLocation);
  156. else
  157. offset = le32_to_cpu(eahd->appAttrLocation);
  158. while (offset < iinfo->i_lenEAttr) {
  159. gaf = (struct genericFormat *)&ea[offset];
  160. if (le32_to_cpu(gaf->attrType) == type &&
  161. gaf->attrSubtype == subtype)
  162. return gaf;
  163. else
  164. offset += le32_to_cpu(gaf->attrLength);
  165. }
  166. }
  167. return NULL;
  168. }
  169. /*
  170. * udf_read_tagged
  171. *
  172. * PURPOSE
  173. * Read the first block of a tagged descriptor.
  174. *
  175. * HISTORY
  176. * July 1, 1997 - Andrew E. Mileski
  177. * Written, tested, and released.
  178. */
  179. struct buffer_head *udf_read_tagged(struct super_block *sb, uint32_t block,
  180. uint32_t location, uint16_t *ident)
  181. {
  182. struct tag *tag_p;
  183. struct buffer_head *bh = NULL;
  184. u8 checksum;
  185. /* Read the block */
  186. if (block == 0xFFFFFFFF)
  187. return NULL;
  188. bh = udf_tread(sb, block);
  189. if (!bh) {
  190. udf_err(sb, "read failed, block=%u, location=%d\n",
  191. block, location);
  192. return NULL;
  193. }
  194. tag_p = (struct tag *)(bh->b_data);
  195. *ident = le16_to_cpu(tag_p->tagIdent);
  196. if (location != le32_to_cpu(tag_p->tagLocation)) {
  197. udf_debug("location mismatch block %u, tag %u != %u\n",
  198. block, le32_to_cpu(tag_p->tagLocation), location);
  199. goto error_out;
  200. }
  201. /* Verify the tag checksum */
  202. checksum = udf_tag_checksum(tag_p);
  203. if (checksum != tag_p->tagChecksum) {
  204. udf_err(sb, "tag checksum failed, block %u: 0x%02x != 0x%02x\n",
  205. block, checksum, tag_p->tagChecksum);
  206. goto error_out;
  207. }
  208. /* Verify the tag version */
  209. if (tag_p->descVersion != cpu_to_le16(0x0002U) &&
  210. tag_p->descVersion != cpu_to_le16(0x0003U)) {
  211. udf_err(sb, "tag version 0x%04x != 0x0002 || 0x0003, block %u\n",
  212. le16_to_cpu(tag_p->descVersion), block);
  213. goto error_out;
  214. }
  215. /* Verify the descriptor CRC */
  216. if (le16_to_cpu(tag_p->descCRCLength) + sizeof(struct tag) > sb->s_blocksize ||
  217. le16_to_cpu(tag_p->descCRC) == crc_itu_t(0,
  218. bh->b_data + sizeof(struct tag),
  219. le16_to_cpu(tag_p->descCRCLength)))
  220. return bh;
  221. udf_debug("Crc failure block %d: crc = %d, crclen = %d\n", block,
  222. le16_to_cpu(tag_p->descCRC),
  223. le16_to_cpu(tag_p->descCRCLength));
  224. error_out:
  225. brelse(bh);
  226. return NULL;
  227. }
  228. struct buffer_head *udf_read_ptagged(struct super_block *sb,
  229. struct kernel_lb_addr *loc,
  230. uint32_t offset, uint16_t *ident)
  231. {
  232. return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
  233. loc->logicalBlockNum + offset, ident);
  234. }
  235. void udf_update_tag(char *data, int length)
  236. {
  237. struct tag *tptr = (struct tag *)data;
  238. length -= sizeof(struct tag);
  239. tptr->descCRCLength = cpu_to_le16(length);
  240. tptr->descCRC = cpu_to_le16(crc_itu_t(0, data + sizeof(struct tag), length));
  241. tptr->tagChecksum = udf_tag_checksum(tptr);
  242. }
  243. void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
  244. uint32_t loc, int length)
  245. {
  246. struct tag *tptr = (struct tag *)data;
  247. tptr->tagIdent = cpu_to_le16(ident);
  248. tptr->descVersion = cpu_to_le16(version);
  249. tptr->tagSerialNum = cpu_to_le16(snum);
  250. tptr->tagLocation = cpu_to_le32(loc);
  251. udf_update_tag(data, length);
  252. }
  253. u8 udf_tag_checksum(const struct tag *t)
  254. {
  255. u8 *data = (u8 *)t;
  256. u8 checksum = 0;
  257. int i;
  258. for (i = 0; i < sizeof(struct tag); ++i)
  259. if (i != 4) /* position of checksum */
  260. checksum += data[i];
  261. return checksum;
  262. }