5 * Miscellaneous routines for the OSTA-UDF(tm) filesystem.
8 * E-mail regarding any portion of the Linux UDF file system should be
9 * directed to the development team mailing list (run by majordomo):
10 * linux_udf@hpesjro.fc.hp.com
13 * This file is distributed under the terms of the GNU General Public
14 * License (GPL). Copies of the GPL can be obtained from:
15 * ftp://prep.ai.mit.edu/pub/gnu/GPL
16 * Each contributing author retains all rights to their own work.
18 * (C) 1998 Dave Boynton
19 * (C) 1998-2001 Ben Fennema
20 * (C) 1999-2000 Stelias Computing Inc
24 * 04/19/99 blf partial support for reading/writing specific EA's
30 #include <linux/string.h>
31 #include <linux/udf_fs.h>
37 udf64_low32(uint64_t indat)
39 return indat & 0x00000000FFFFFFFFULL;
43 udf64_high32(uint64_t indat)
48 extern struct buffer_head *
49 udf_tgetblk(struct super_block *sb, int block)
51 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
52 return sb_getblk(sb, udf_fixed_to_variable(block));
54 return sb_getblk(sb, block);
57 extern struct buffer_head *
58 udf_tread(struct super_block *sb, int block)
60 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
61 return sb_bread(sb, udf_fixed_to_variable(block));
63 return sb_bread(sb, block);
66 extern struct genericFormat *
67 udf_add_extendedattr(struct inode * inode, uint32_t size, uint32_t type,
68 uint8_t loc, struct buffer_head **bh)
70 uint8_t *ea = NULL, *ad = NULL;
74 *bh = udf_tread(inode->i_sb, inode->i_ino);
76 if (UDF_I_EXTENDED_FE(inode) == 0)
80 fe = (struct fileEntry *)(*bh)->b_data;
81 eaicb = lela_to_cpu(fe->extendedAttrICB);
82 offset = sizeof(struct fileEntry);
86 struct extendedFileEntry *efe;
88 efe = (struct extendedFileEntry *)(*bh)->b_data;
89 eaicb = lela_to_cpu(efe->extendedAttrICB);
90 offset = sizeof(struct extendedFileEntry);
93 ea = &(*bh)->b_data[offset];
94 if (UDF_I_LENEATTR(inode))
95 offset += UDF_I_LENEATTR(inode);
97 size += sizeof(struct extendedAttrHeaderDesc);
99 ad = &(*bh)->b_data[offset];
100 if (UDF_I_LENALLOC(inode))
101 offset += UDF_I_LENALLOC(inode);
103 offset = inode->i_sb->s_blocksize - offset;
105 /* TODO - Check for FreeEASpace */
107 if (loc & 0x01 && offset >= size)
109 struct extendedAttrHeaderDesc *eahd;
110 eahd = (struct extendedAttrHeaderDesc *)ea;
112 if (UDF_I_LENALLOC(inode))
114 memmove(&ad[size], ad, UDF_I_LENALLOC(inode));
117 if (UDF_I_LENEATTR(inode))
119 /* check checksum/crc */
120 if (le16_to_cpu(eahd->descTag.tagIdent) != TAG_IDENT_EAHD ||
121 le32_to_cpu(eahd->descTag.tagLocation) != UDF_I_LOCATION(inode).logicalBlockNum)
123 udf_release_data(*bh);
129 size -= sizeof(struct extendedAttrHeaderDesc);
130 UDF_I_LENEATTR(inode) += sizeof(struct extendedAttrHeaderDesc);
131 eahd->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EAHD);
132 eahd->descTag.descVersion = cpu_to_le16(2);
133 eahd->descTag.tagSerialNum = cpu_to_le16(1);
134 eahd->descTag.tagLocation = cpu_to_le32(UDF_I_LOCATION(inode).logicalBlockNum);
135 eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
136 eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
139 offset = UDF_I_LENEATTR(inode);
142 if (le32_to_cpu(eahd->appAttrLocation) < UDF_I_LENEATTR(inode))
144 uint32_t aal = le32_to_cpu(eahd->appAttrLocation);
145 memmove(&ea[offset - aal + size],
146 &ea[aal], offset - aal);
148 eahd->appAttrLocation = cpu_to_le32(aal + size);
150 if (le32_to_cpu(eahd->impAttrLocation) < UDF_I_LENEATTR(inode))
152 uint32_t ial = le32_to_cpu(eahd->impAttrLocation);
153 memmove(&ea[offset - ial + size],
154 &ea[ial], offset - ial);
156 eahd->impAttrLocation = cpu_to_le32(ial + size);
159 else if (type < 65536)
161 if (le32_to_cpu(eahd->appAttrLocation) < UDF_I_LENEATTR(inode))
163 uint32_t aal = le32_to_cpu(eahd->appAttrLocation);
164 memmove(&ea[offset - aal + size],
165 &ea[aal], offset - aal);
167 eahd->appAttrLocation = cpu_to_le32(aal + size);
170 /* rewrite CRC + checksum of eahd */
171 UDF_I_LENEATTR(inode) += size;
172 return (struct genericFormat *)&ea[offset];
177 udf_release_data(*bh);
181 extern struct genericFormat *
182 udf_get_extendedattr(struct inode * inode, uint32_t type, uint8_t subtype,
183 struct buffer_head **bh)
185 struct genericFormat *gaf;
190 *bh = udf_tread(inode->i_sb, inode->i_ino);
192 if (UDF_I_EXTENDED_FE(inode) == 0)
194 struct fileEntry *fe;
196 fe = (struct fileEntry *)(*bh)->b_data;
197 eaicb = lela_to_cpu(fe->extendedAttrICB);
198 if (UDF_I_LENEATTR(inode))
199 ea = fe->extendedAttr;
203 struct extendedFileEntry *efe;
205 efe = (struct extendedFileEntry *)(*bh)->b_data;
206 eaicb = lela_to_cpu(efe->extendedAttrICB);
207 if (UDF_I_LENEATTR(inode))
208 ea = efe->extendedAttr;
211 if (UDF_I_LENEATTR(inode))
213 struct extendedAttrHeaderDesc *eahd;
214 eahd = (struct extendedAttrHeaderDesc *)ea;
216 /* check checksum/crc */
217 if (le16_to_cpu(eahd->descTag.tagIdent) != TAG_IDENT_EAHD ||
218 le32_to_cpu(eahd->descTag.tagLocation) != UDF_I_LOCATION(inode).logicalBlockNum)
220 udf_release_data(*bh);
225 offset = sizeof(struct extendedAttrHeaderDesc);
226 else if (type < 65536)
227 offset = le32_to_cpu(eahd->impAttrLocation);
229 offset = le32_to_cpu(eahd->appAttrLocation);
231 while (offset < UDF_I_LENEATTR(inode))
233 gaf = (struct genericFormat *)&ea[offset];
234 if (le32_to_cpu(gaf->attrType) == type && gaf->attrSubtype == subtype)
237 offset += le32_to_cpu(gaf->attrLength);
241 udf_release_data(*bh);
253 * Read the first block of a tagged descriptor.
256 * July 1, 1997 - Andrew E. Mileski
257 * Written, tested, and released.
259 extern struct buffer_head *
260 udf_read_tagged(struct super_block *sb, uint32_t block, uint32_t location, uint16_t *ident)
263 struct buffer_head *bh = NULL;
264 register uint8_t checksum;
268 if (block == 0xFFFFFFFF)
271 bh = udf_tread(sb, block);
274 udf_debug("block=%d, location=%d: read failed\n", block, location);
278 tag_p = (tag *)(bh->b_data);
280 *ident = le16_to_cpu(tag_p->tagIdent);
282 if ( location != le32_to_cpu(tag_p->tagLocation) )
284 udf_debug("location mismatch block %u, tag %u != %u\n",
285 block, le32_to_cpu(tag_p->tagLocation), location);
289 /* Verify the tag checksum */
291 for (i = 0; i < 4; i++)
292 checksum += (uint8_t)(bh->b_data[i]);
293 for (i = 5; i < 16; i++)
294 checksum += (uint8_t)(bh->b_data[i]);
295 if (checksum != tag_p->tagChecksum) {
296 printk(KERN_ERR "udf: tag checksum failed block %d\n", block);
300 /* Verify the tag version */
301 if (le16_to_cpu(tag_p->descVersion) != 0x0002U &&
302 le16_to_cpu(tag_p->descVersion) != 0x0003U)
304 udf_debug("tag version 0x%04x != 0x0002 || 0x0003 block %d\n",
305 le16_to_cpu(tag_p->descVersion), block);
309 /* Verify the descriptor CRC */
310 if (le16_to_cpu(tag_p->descCRCLength) + sizeof(tag) > sb->s_blocksize ||
311 le16_to_cpu(tag_p->descCRC) == udf_crc(bh->b_data + sizeof(tag),
312 le16_to_cpu(tag_p->descCRCLength), 0))
316 udf_debug("Crc failure block %d: crc = %d, crclen = %d\n",
317 block, le16_to_cpu(tag_p->descCRC), le16_to_cpu(tag_p->descCRCLength));
324 extern struct buffer_head *
325 udf_read_ptagged(struct super_block *sb, lb_addr loc, uint32_t offset, uint16_t *ident)
327 return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
328 loc.logicalBlockNum + offset, ident);
331 void udf_release_data(struct buffer_head *bh)
337 void udf_update_tag(char *data, int length)
339 tag *tptr = (tag *)data;
342 length -= sizeof(tag);
344 tptr->tagChecksum = 0;
345 tptr->descCRCLength = le16_to_cpu(length);
346 tptr->descCRC = le16_to_cpu(udf_crc(data + sizeof(tag), length, 0));
350 tptr->tagChecksum += (uint8_t)(data[i]);
353 void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
354 uint32_t loc, int length)
356 tag *tptr = (tag *)data;
357 tptr->tagIdent = le16_to_cpu(ident);
358 tptr->descVersion = le16_to_cpu(version);
359 tptr->tagSerialNum = le16_to_cpu(snum);
360 tptr->tagLocation = le32_to_cpu(loc);
361 udf_update_tag(data, length);