misc.c 7.8 KB

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