misc.c 7.9 KB

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