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/udf_fs.h>
27 #include <linux/buffer_head.h>
33 udf_tgetblk(struct super_block *sb, int block)
35 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
36 return sb_getblk(sb, udf_fixed_to_variable(block));
38 return sb_getblk(sb, block);
42 udf_tread(struct super_block *sb, int block)
44 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
45 return sb_bread(sb, udf_fixed_to_variable(block));
47 return sb_bread(sb, block);
50 struct genericFormat *
51 udf_add_extendedattr(struct inode * inode, uint32_t size, uint32_t type,
54 uint8_t *ea = NULL, *ad = NULL;
59 ea = UDF_I_DATA(inode);
60 if (UDF_I_LENEATTR(inode))
61 ad = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
65 size += sizeof(struct extendedAttrHeaderDesc);
68 offset = inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) -
69 UDF_I_LENALLOC(inode);
71 /* TODO - Check for FreeEASpace */
73 if (loc & 0x01 && offset >= size)
75 struct extendedAttrHeaderDesc *eahd;
76 eahd = (struct extendedAttrHeaderDesc *)ea;
78 if (UDF_I_LENALLOC(inode))
80 memmove(&ad[size], ad, UDF_I_LENALLOC(inode));
83 if (UDF_I_LENEATTR(inode))
85 /* check checksum/crc */
86 if (le16_to_cpu(eahd->descTag.tagIdent) != TAG_IDENT_EAHD ||
87 le32_to_cpu(eahd->descTag.tagLocation) != UDF_I_LOCATION(inode).logicalBlockNum)
94 size -= sizeof(struct extendedAttrHeaderDesc);
95 UDF_I_LENEATTR(inode) += sizeof(struct extendedAttrHeaderDesc);
96 eahd->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EAHD);
97 if (UDF_SB_UDFREV(inode->i_sb) >= 0x0200)
98 eahd->descTag.descVersion = cpu_to_le16(3);
100 eahd->descTag.descVersion = cpu_to_le16(2);
101 eahd->descTag.tagSerialNum = cpu_to_le16(UDF_SB_SERIALNUM(inode->i_sb));
102 eahd->descTag.tagLocation = cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
103 eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
104 eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
107 offset = UDF_I_LENEATTR(inode);
110 if (le32_to_cpu(eahd->appAttrLocation) < UDF_I_LENEATTR(inode))
112 uint32_t aal = le32_to_cpu(eahd->appAttrLocation);
113 memmove(&ea[offset - aal + size],
114 &ea[aal], offset - aal);
116 eahd->appAttrLocation = cpu_to_le32(aal + size);
118 if (le32_to_cpu(eahd->impAttrLocation) < UDF_I_LENEATTR(inode))
120 uint32_t ial = le32_to_cpu(eahd->impAttrLocation);
121 memmove(&ea[offset - ial + size],
122 &ea[ial], offset - ial);
124 eahd->impAttrLocation = cpu_to_le32(ial + size);
127 else if (type < 65536)
129 if (le32_to_cpu(eahd->appAttrLocation) < UDF_I_LENEATTR(inode))
131 uint32_t aal = le32_to_cpu(eahd->appAttrLocation);
132 memmove(&ea[offset - aal + size],
133 &ea[aal], offset - aal);
135 eahd->appAttrLocation = cpu_to_le32(aal + size);
138 /* rewrite CRC + checksum of eahd */
139 crclen = sizeof(struct extendedAttrHeaderDesc) - sizeof(tag);
140 eahd->descTag.descCRCLength = cpu_to_le16(crclen);
141 eahd->descTag.descCRC = cpu_to_le16(udf_crc((char *)eahd + sizeof(tag), crclen, 0));
142 eahd->descTag.tagChecksum = 0;
145 eahd->descTag.tagChecksum += ((uint8_t *)&(eahd->descTag))[i];
146 UDF_I_LENEATTR(inode) += size;
147 return (struct genericFormat *)&ea[offset];
155 struct genericFormat *
156 udf_get_extendedattr(struct inode *inode, uint32_t type, uint8_t subtype)
158 struct genericFormat *gaf;
162 ea = UDF_I_DATA(inode);
164 if (UDF_I_LENEATTR(inode))
166 struct extendedAttrHeaderDesc *eahd;
167 eahd = (struct extendedAttrHeaderDesc *)ea;
169 /* check checksum/crc */
170 if (le16_to_cpu(eahd->descTag.tagIdent) != TAG_IDENT_EAHD ||
171 le32_to_cpu(eahd->descTag.tagLocation) != UDF_I_LOCATION(inode).logicalBlockNum)
177 offset = sizeof(struct extendedAttrHeaderDesc);
178 else if (type < 65536)
179 offset = le32_to_cpu(eahd->impAttrLocation);
181 offset = le32_to_cpu(eahd->appAttrLocation);
183 while (offset < UDF_I_LENEATTR(inode))
185 gaf = (struct genericFormat *)&ea[offset];
186 if (le32_to_cpu(gaf->attrType) == type && gaf->attrSubtype == subtype)
189 offset += le32_to_cpu(gaf->attrLength);
199 * Read the first block of a tagged descriptor.
202 * July 1, 1997 - Andrew E. Mileski
203 * Written, tested, and released.
206 udf_read_tagged(struct super_block *sb, uint32_t block, uint32_t location, uint16_t *ident)
209 struct buffer_head *bh = NULL;
210 register uint8_t checksum;
214 if (block == 0xFFFFFFFF)
217 bh = udf_tread(sb, block + UDF_SB_SESSION(sb));
220 udf_debug("block=%d, location=%d: read failed\n", block + UDF_SB_SESSION(sb), location);
224 tag_p = (tag *)(bh->b_data);
226 *ident = le16_to_cpu(tag_p->tagIdent);
228 if ( location != le32_to_cpu(tag_p->tagLocation) )
230 udf_debug("location mismatch block %u, tag %u != %u\n",
231 block + UDF_SB_SESSION(sb), le32_to_cpu(tag_p->tagLocation), location);
235 /* Verify the tag checksum */
237 for (i = 0; i < 4; i++)
238 checksum += (uint8_t)(bh->b_data[i]);
239 for (i = 5; i < 16; i++)
240 checksum += (uint8_t)(bh->b_data[i]);
241 if (checksum != tag_p->tagChecksum) {
242 printk(KERN_ERR "udf: tag checksum failed block %d\n", block);
246 /* Verify the tag version */
247 if (le16_to_cpu(tag_p->descVersion) != 0x0002U &&
248 le16_to_cpu(tag_p->descVersion) != 0x0003U)
250 udf_debug("tag version 0x%04x != 0x0002 || 0x0003 block %d\n",
251 le16_to_cpu(tag_p->descVersion), block);
255 /* Verify the descriptor CRC */
256 if (le16_to_cpu(tag_p->descCRCLength) + sizeof(tag) > sb->s_blocksize ||
257 le16_to_cpu(tag_p->descCRC) == udf_crc(bh->b_data + sizeof(tag),
258 le16_to_cpu(tag_p->descCRCLength), 0))
262 udf_debug("Crc failure block %d: crc = %d, crclen = %d\n",
263 block + UDF_SB_SESSION(sb), le16_to_cpu(tag_p->descCRC), le16_to_cpu(tag_p->descCRCLength));
271 udf_read_ptagged(struct super_block *sb, kernel_lb_addr loc, uint32_t offset, uint16_t *ident)
273 return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
274 loc.logicalBlockNum + offset, ident);
277 void udf_update_tag(char *data, int length)
279 tag *tptr = (tag *)data;
282 length -= sizeof(tag);
284 tptr->tagChecksum = 0;
285 tptr->descCRCLength = cpu_to_le16(length);
286 tptr->descCRC = cpu_to_le16(udf_crc(data + sizeof(tag), length, 0));
290 tptr->tagChecksum += (uint8_t)(data[i]);
293 void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
294 uint32_t loc, int length)
296 tag *tptr = (tag *)data;
297 tptr->tagIdent = cpu_to_le16(ident);
298 tptr->descVersion = cpu_to_le16(version);
299 tptr->tagSerialNum = cpu_to_le16(snum);
300 tptr->tagLocation = cpu_to_le32(loc);
301 udf_update_tag(data, length);