5 * Miscellaneous routines for the OSTA-UDF(tm) filesystem.
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.
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
19 * 04/19/99 blf partial support for reading/writing specific EA's
25 #include <linux/string.h>
26 #include <linux/crc-itu-t.h>
31 struct genericFormat *udf_add_extendedattr(struct inode *inode, uint32_t size,
32 uint32_t type, uint8_t loc)
34 uint8_t *ea = NULL, *ad = NULL;
37 struct udf_inode_info *iinfo = UDF_I(inode);
40 if (iinfo->i_lenEAttr) {
41 ad = iinfo->i_data + iinfo->i_lenEAttr;
44 size += sizeof(struct extendedAttrHeaderDesc);
47 offset = inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) -
50 /* TODO - Check for FreeEASpace */
52 if (loc & 0x01 && offset >= size) {
53 struct extendedAttrHeaderDesc *eahd;
54 eahd = (struct extendedAttrHeaderDesc *)ea;
56 if (iinfo->i_lenAlloc)
57 memmove(&ad[size], ad, iinfo->i_lenAlloc);
59 if (iinfo->i_lenEAttr) {
60 /* check checksum/crc */
61 if (eahd->descTag.tagIdent !=
62 cpu_to_le16(TAG_IDENT_EAHD) ||
63 le32_to_cpu(eahd->descTag.tagLocation) !=
64 iinfo->i_location.logicalBlockNum)
67 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
69 size -= sizeof(struct extendedAttrHeaderDesc);
71 sizeof(struct extendedAttrHeaderDesc);
72 eahd->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EAHD);
73 if (sbi->s_udfrev >= 0x0200)
74 eahd->descTag.descVersion = cpu_to_le16(3);
76 eahd->descTag.descVersion = cpu_to_le16(2);
77 eahd->descTag.tagSerialNum =
78 cpu_to_le16(sbi->s_serial_number);
79 eahd->descTag.tagLocation = cpu_to_le32(
80 iinfo->i_location.logicalBlockNum);
81 eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
82 eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
85 offset = iinfo->i_lenEAttr;
87 if (le32_to_cpu(eahd->appAttrLocation) <
90 le32_to_cpu(eahd->appAttrLocation);
91 memmove(&ea[offset - aal + size],
92 &ea[aal], offset - aal);
94 eahd->appAttrLocation =
95 cpu_to_le32(aal + size);
97 if (le32_to_cpu(eahd->impAttrLocation) <
100 le32_to_cpu(eahd->impAttrLocation);
101 memmove(&ea[offset - ial + size],
102 &ea[ial], offset - ial);
104 eahd->impAttrLocation =
105 cpu_to_le32(ial + size);
107 } else if (type < 65536) {
108 if (le32_to_cpu(eahd->appAttrLocation) <
111 le32_to_cpu(eahd->appAttrLocation);
112 memmove(&ea[offset - aal + size],
113 &ea[aal], offset - aal);
115 eahd->appAttrLocation =
116 cpu_to_le32(aal + size);
119 /* rewrite CRC + checksum of eahd */
120 crclen = sizeof(struct extendedAttrHeaderDesc) - sizeof(struct tag);
121 eahd->descTag.descCRCLength = cpu_to_le16(crclen);
122 eahd->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)eahd +
123 sizeof(struct tag), crclen));
124 eahd->descTag.tagChecksum = udf_tag_checksum(&eahd->descTag);
125 iinfo->i_lenEAttr += size;
126 return (struct genericFormat *)&ea[offset];
132 struct genericFormat *udf_get_extendedattr(struct inode *inode, uint32_t type,
135 struct genericFormat *gaf;
138 struct udf_inode_info *iinfo = UDF_I(inode);
142 if (iinfo->i_lenEAttr) {
143 struct extendedAttrHeaderDesc *eahd;
144 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)
154 offset = sizeof(struct extendedAttrHeaderDesc);
155 else if (type < 65536)
156 offset = le32_to_cpu(eahd->impAttrLocation);
158 offset = le32_to_cpu(eahd->appAttrLocation);
160 while (offset + sizeof(*gaf) < iinfo->i_lenEAttr) {
163 gaf = (struct genericFormat *)&ea[offset];
164 attrLength = le32_to_cpu(gaf->attrLength);
166 /* Detect undersized elements and buffer overflows */
167 if ((attrLength < sizeof(*gaf)) ||
168 (attrLength > (iinfo->i_lenEAttr - offset)))
171 if (le32_to_cpu(gaf->attrType) == type &&
172 gaf->attrSubtype == subtype)
175 offset += attrLength;
186 * Read the first block of a tagged descriptor.
189 * July 1, 1997 - Andrew E. Mileski
190 * Written, tested, and released.
192 struct buffer_head *udf_read_tagged(struct super_block *sb, uint32_t block,
193 uint32_t location, uint16_t *ident)
196 struct buffer_head *bh = NULL;
200 if (block == 0xFFFFFFFF)
203 bh = sb_bread(sb, block);
205 udf_err(sb, "read failed, block=%u, location=%u\n",
210 tag_p = (struct tag *)(bh->b_data);
212 *ident = le16_to_cpu(tag_p->tagIdent);
214 if (location != le32_to_cpu(tag_p->tagLocation)) {
215 udf_debug("location mismatch block %u, tag %u != %u\n",
216 block, le32_to_cpu(tag_p->tagLocation), location);
220 /* Verify the tag checksum */
221 checksum = udf_tag_checksum(tag_p);
222 if (checksum != tag_p->tagChecksum) {
223 udf_err(sb, "tag checksum failed, block %u: 0x%02x != 0x%02x\n",
224 block, checksum, tag_p->tagChecksum);
228 /* Verify the tag version */
229 if (tag_p->descVersion != cpu_to_le16(0x0002U) &&
230 tag_p->descVersion != cpu_to_le16(0x0003U)) {
231 udf_err(sb, "tag version 0x%04x != 0x0002 || 0x0003, block %u\n",
232 le16_to_cpu(tag_p->descVersion), block);
236 /* Verify the descriptor CRC */
237 if (le16_to_cpu(tag_p->descCRCLength) + sizeof(struct tag) > sb->s_blocksize ||
238 le16_to_cpu(tag_p->descCRC) == crc_itu_t(0,
239 bh->b_data + sizeof(struct tag),
240 le16_to_cpu(tag_p->descCRCLength)))
243 udf_debug("Crc failure block %u: crc = %u, crclen = %u\n", block,
244 le16_to_cpu(tag_p->descCRC),
245 le16_to_cpu(tag_p->descCRCLength));
251 struct buffer_head *udf_read_ptagged(struct super_block *sb,
252 struct kernel_lb_addr *loc,
253 uint32_t offset, uint16_t *ident)
255 return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
256 loc->logicalBlockNum + offset, ident);
259 void udf_update_tag(char *data, int length)
261 struct tag *tptr = (struct tag *)data;
262 length -= sizeof(struct tag);
264 tptr->descCRCLength = cpu_to_le16(length);
265 tptr->descCRC = cpu_to_le16(crc_itu_t(0, data + sizeof(struct tag), length));
266 tptr->tagChecksum = udf_tag_checksum(tptr);
269 void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
270 uint32_t loc, int length)
272 struct tag *tptr = (struct tag *)data;
273 tptr->tagIdent = cpu_to_le16(ident);
274 tptr->descVersion = cpu_to_le16(version);
275 tptr->tagSerialNum = cpu_to_le16(snum);
276 tptr->tagLocation = cpu_to_le32(loc);
277 udf_update_tag(data, length);
280 u8 udf_tag_checksum(const struct tag *t)
285 for (i = 0; i < sizeof(struct tag); ++i)
286 if (i != 4) /* position of checksum */