misc.c 7.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * misc.c
 *
 * PURPOSE
 *	Miscellaneous routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *	This file is distributed under the terms of the GNU General Public
 *	License (GPL). Copies of the GPL can be obtained from:
 *		ftp://prep.ai.mit.edu/pub/gnu/GPL
 *	Each contributing author retains all rights to their own work.
 *
 *  (C) 1998 Dave Boynton
 *  (C) 1998-2004 Ben Fennema
 *  (C) 1999-2000 Stelias Computing Inc
 *
 * HISTORY
 *
 *  04/19/99 blf  partial support for reading/writing specific EA's
 */

#include "udfdecl.h"

#include <linux/fs.h>
#include <linux/string.h>
#include <linux/buffer_head.h>
27
#include <linux/crc-itu-t.h>
L
Linus Torvalds 已提交
28 29 30 31

#include "udf_i.h"
#include "udf_sb.h"

32
struct buffer_head *udf_tgetblk(struct super_block *sb, int block)
L
Linus Torvalds 已提交
33 34 35 36 37 38 39
{
	if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
		return sb_getblk(sb, udf_fixed_to_variable(block));
	else
		return sb_getblk(sb, block);
}

40
struct buffer_head *udf_tread(struct super_block *sb, int block)
L
Linus Torvalds 已提交
41 42 43 44 45 46 47
{
	if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV))
		return sb_bread(sb, udf_fixed_to_variable(block));
	else
		return sb_bread(sb, block);
}

48 49
struct genericFormat *udf_add_extendedattr(struct inode *inode, uint32_t size,
					   uint32_t type, uint8_t loc)
L
Linus Torvalds 已提交
50 51 52 53
{
	uint8_t *ea = NULL, *ad = NULL;
	int offset;
	uint16_t crclen;
54
	struct udf_inode_info *iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
55

56 57 58
	ea = iinfo->i_ext.i_data;
	if (iinfo->i_lenEAttr) {
		ad = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
59
	} else {
L
Linus Torvalds 已提交
60 61 62 63 64
		ad = ea;
		size += sizeof(struct extendedAttrHeaderDesc);
	}

	offset = inode->i_sb->s_blocksize - udf_file_entry_alloc_offset(inode) -
65
		iinfo->i_lenAlloc;
L
Linus Torvalds 已提交
66 67 68

	/* TODO - Check for FreeEASpace */

69
	if (loc & 0x01 && offset >= size) {
L
Linus Torvalds 已提交
70 71 72
		struct extendedAttrHeaderDesc *eahd;
		eahd = (struct extendedAttrHeaderDesc *)ea;

73 74
		if (iinfo->i_lenAlloc)
			memmove(&ad[size], ad, iinfo->i_lenAlloc);
L
Linus Torvalds 已提交
75

76
		if (iinfo->i_lenEAttr) {
L
Linus Torvalds 已提交
77
			/* check checksum/crc */
78 79
			if (eahd->descTag.tagIdent !=
					cpu_to_le16(TAG_IDENT_EAHD) ||
M
Marcin Slusarz 已提交
80
			    le32_to_cpu(eahd->descTag.tagLocation) !=
81
					iinfo->i_location.logicalBlockNum)
L
Linus Torvalds 已提交
82
				return NULL;
83
		} else {
M
Marcin Slusarz 已提交
84 85
			struct udf_sb_info *sbi = UDF_SB(inode->i_sb);

L
Linus Torvalds 已提交
86
			size -= sizeof(struct extendedAttrHeaderDesc);
87
			iinfo->i_lenEAttr +=
M
Marcin Slusarz 已提交
88
				sizeof(struct extendedAttrHeaderDesc);
L
Linus Torvalds 已提交
89
			eahd->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EAHD);
M
Marcin Slusarz 已提交
90
			if (sbi->s_udfrev >= 0x0200)
L
Linus Torvalds 已提交
91 92 93
				eahd->descTag.descVersion = cpu_to_le16(3);
			else
				eahd->descTag.descVersion = cpu_to_le16(2);
M
Marcin Slusarz 已提交
94 95 96
			eahd->descTag.tagSerialNum =
					cpu_to_le16(sbi->s_serial_number);
			eahd->descTag.tagLocation = cpu_to_le32(
97
					iinfo->i_location.logicalBlockNum);
L
Linus Torvalds 已提交
98 99 100 101
			eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
			eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
		}

102
		offset = iinfo->i_lenEAttr;
103
		if (type < 2048) {
M
Marcin Slusarz 已提交
104
			if (le32_to_cpu(eahd->appAttrLocation) <
105
					iinfo->i_lenEAttr) {
M
Marcin Slusarz 已提交
106 107
				uint32_t aal =
					le32_to_cpu(eahd->appAttrLocation);
108 109
				memmove(&ea[offset - aal + size],
					&ea[aal], offset - aal);
L
Linus Torvalds 已提交
110
				offset -= aal;
M
Marcin Slusarz 已提交
111 112
				eahd->appAttrLocation =
						cpu_to_le32(aal + size);
L
Linus Torvalds 已提交
113
			}
M
Marcin Slusarz 已提交
114
			if (le32_to_cpu(eahd->impAttrLocation) <
115
					iinfo->i_lenEAttr) {
M
Marcin Slusarz 已提交
116 117
				uint32_t ial =
					le32_to_cpu(eahd->impAttrLocation);
118 119
				memmove(&ea[offset - ial + size],
					&ea[ial], offset - ial);
L
Linus Torvalds 已提交
120
				offset -= ial;
M
Marcin Slusarz 已提交
121 122
				eahd->impAttrLocation =
						cpu_to_le32(ial + size);
L
Linus Torvalds 已提交
123
			}
124
		} else if (type < 65536) {
M
Marcin Slusarz 已提交
125
			if (le32_to_cpu(eahd->appAttrLocation) <
126
					iinfo->i_lenEAttr) {
M
Marcin Slusarz 已提交
127 128
				uint32_t aal =
					le32_to_cpu(eahd->appAttrLocation);
129 130
				memmove(&ea[offset - aal + size],
					&ea[aal], offset - aal);
L
Linus Torvalds 已提交
131
				offset -= aal;
M
Marcin Slusarz 已提交
132 133
				eahd->appAttrLocation =
						cpu_to_le32(aal + size);
L
Linus Torvalds 已提交
134 135 136
			}
		}
		/* rewrite CRC + checksum of eahd */
137
		crclen = sizeof(struct extendedAttrHeaderDesc) - sizeof(struct tag);
L
Linus Torvalds 已提交
138
		eahd->descTag.descCRCLength = cpu_to_le16(crclen);
139
		eahd->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)eahd +
140
						sizeof(struct tag), crclen));
141
		eahd->descTag.tagChecksum = udf_tag_checksum(&eahd->descTag);
142
		iinfo->i_lenEAttr += size;
L
Linus Torvalds 已提交
143 144
		return (struct genericFormat *)&ea[offset];
	}
M
Marcin Slusarz 已提交
145 146
	if (loc & 0x02)
		;
147

L
Linus Torvalds 已提交
148 149 150
	return NULL;
}

151 152
struct genericFormat *udf_get_extendedattr(struct inode *inode, uint32_t type,
					   uint8_t subtype)
L
Linus Torvalds 已提交
153 154 155 156
{
	struct genericFormat *gaf;
	uint8_t *ea = NULL;
	uint32_t offset;
157
	struct udf_inode_info *iinfo = UDF_I(inode);
L
Linus Torvalds 已提交
158

159
	ea = iinfo->i_ext.i_data;
L
Linus Torvalds 已提交
160

161
	if (iinfo->i_lenEAttr) {
L
Linus Torvalds 已提交
162 163 164 165
		struct extendedAttrHeaderDesc *eahd;
		eahd = (struct extendedAttrHeaderDesc *)ea;

		/* check checksum/crc */
166 167
		if (eahd->descTag.tagIdent !=
				cpu_to_le16(TAG_IDENT_EAHD) ||
M
Marcin Slusarz 已提交
168
		    le32_to_cpu(eahd->descTag.tagLocation) !=
169
				iinfo->i_location.logicalBlockNum)
L
Linus Torvalds 已提交
170
			return NULL;
171

L
Linus Torvalds 已提交
172 173 174 175 176 177 178
		if (type < 2048)
			offset = sizeof(struct extendedAttrHeaderDesc);
		else if (type < 65536)
			offset = le32_to_cpu(eahd->impAttrLocation);
		else
			offset = le32_to_cpu(eahd->appAttrLocation);

179
		while (offset < iinfo->i_lenEAttr) {
L
Linus Torvalds 已提交
180
			gaf = (struct genericFormat *)&ea[offset];
M
Marcin Slusarz 已提交
181 182
			if (le32_to_cpu(gaf->attrType) == type &&
					gaf->attrSubtype == subtype)
L
Linus Torvalds 已提交
183 184 185 186 187
				return gaf;
			else
				offset += le32_to_cpu(gaf->attrLength);
		}
	}
188

L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201
	return NULL;
}

/*
 * udf_read_tagged
 *
 * PURPOSE
 *	Read the first block of a tagged descriptor.
 *
 * HISTORY
 *	July 1, 1997 - Andrew E. Mileski
 *	Written, tested, and released.
 */
202
struct buffer_head *udf_read_tagged(struct super_block *sb, uint32_t block,
M
Marcin Slusarz 已提交
203
				    uint32_t location, uint16_t *ident)
L
Linus Torvalds 已提交
204
{
205
	struct tag *tag_p;
L
Linus Torvalds 已提交
206
	struct buffer_head *bh = NULL;
207
	u8 checksum;
L
Linus Torvalds 已提交
208 209 210 211 212

	/* Read the block */
	if (block == 0xFFFFFFFF)
		return NULL;

213
	bh = udf_tread(sb, block);
214
	if (!bh) {
J
Joe Perches 已提交
215 216
		udf_err(sb, "read failed, block=%u, location=%d\n",
			block, location);
L
Linus Torvalds 已提交
217 218 219
		return NULL;
	}

220
	tag_p = (struct tag *)(bh->b_data);
L
Linus Torvalds 已提交
221 222 223

	*ident = le16_to_cpu(tag_p->tagIdent);

224
	if (location != le32_to_cpu(tag_p->tagLocation)) {
L
Linus Torvalds 已提交
225
		udf_debug("location mismatch block %u, tag %u != %u\n",
226
			  block, le32_to_cpu(tag_p->tagLocation), location);
L
Linus Torvalds 已提交
227 228
		goto error_out;
	}
229

L
Linus Torvalds 已提交
230
	/* Verify the tag checksum */
231 232
	checksum = udf_tag_checksum(tag_p);
	if (checksum != tag_p->tagChecksum) {
J
Joe Perches 已提交
233 234
		udf_err(sb, "tag checksum failed, block %u: 0x%02x != 0x%02x\n",
			block, checksum, tag_p->tagChecksum);
L
Linus Torvalds 已提交
235 236 237 238
		goto error_out;
	}

	/* Verify the tag version */
239 240
	if (tag_p->descVersion != cpu_to_le16(0x0002U) &&
	    tag_p->descVersion != cpu_to_le16(0x0003U)) {
J
Joe Perches 已提交
241 242
		udf_err(sb, "tag version 0x%04x != 0x0002 || 0x0003, block %u\n",
			le16_to_cpu(tag_p->descVersion), block);
L
Linus Torvalds 已提交
243 244 245 246
		goto error_out;
	}

	/* Verify the descriptor CRC */
247
	if (le16_to_cpu(tag_p->descCRCLength) + sizeof(struct tag) > sb->s_blocksize ||
248
	    le16_to_cpu(tag_p->descCRC) == crc_itu_t(0,
249
					bh->b_data + sizeof(struct tag),
250
					le16_to_cpu(tag_p->descCRCLength)))
L
Linus Torvalds 已提交
251
		return bh;
M
Marcin Slusarz 已提交
252

253
	udf_debug("Crc failure block %d: crc = %d, crclen = %d\n", block,
J
Joe Perches 已提交
254 255
		  le16_to_cpu(tag_p->descCRC),
		  le16_to_cpu(tag_p->descCRCLength));
256
error_out:
L
Linus Torvalds 已提交
257 258 259 260
	brelse(bh);
	return NULL;
}

261 262
struct buffer_head *udf_read_ptagged(struct super_block *sb,
				     struct kernel_lb_addr *loc,
M
Marcin Slusarz 已提交
263
				     uint32_t offset, uint16_t *ident)
L
Linus Torvalds 已提交
264 265
{
	return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
266
			       loc->logicalBlockNum + offset, ident);
L
Linus Torvalds 已提交
267 268 269 270
}

void udf_update_tag(char *data, int length)
{
271 272
	struct tag *tptr = (struct tag *)data;
	length -= sizeof(struct tag);
L
Linus Torvalds 已提交
273 274

	tptr->descCRCLength = cpu_to_le16(length);
275
	tptr->descCRC = cpu_to_le16(crc_itu_t(0, data + sizeof(struct tag), length));
276
	tptr->tagChecksum = udf_tag_checksum(tptr);
L
Linus Torvalds 已提交
277 278 279
}

void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
280
		 uint32_t loc, int length)
L
Linus Torvalds 已提交
281
{
282
	struct tag *tptr = (struct tag *)data;
L
Linus Torvalds 已提交
283 284 285 286 287 288
	tptr->tagIdent = cpu_to_le16(ident);
	tptr->descVersion = cpu_to_le16(version);
	tptr->tagSerialNum = cpu_to_le16(snum);
	tptr->tagLocation = cpu_to_le32(loc);
	udf_update_tag(data, length);
}
289

290
u8 udf_tag_checksum(const struct tag *t)
291 292 293 294
{
	u8 *data = (u8 *)t;
	u8 checksum = 0;
	int i;
295
	for (i = 0; i < sizeof(struct tag); ++i)
296 297 298 299
		if (i != 4) /* position of checksum */
			checksum += data[i];
	return checksum;
}