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 27 28 29 30 31
/*
 * 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/udf_fs.h>
#include <linux/buffer_head.h>

#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 54 55 56
{
	uint8_t *ea = NULL, *ad = NULL;
	int offset;
	uint16_t crclen;
	int i;

	ea = UDF_I_DATA(inode);
57
	if (UDF_I_LENEATTR(inode)) {
L
Linus Torvalds 已提交
58
		ad = UDF_I_DATA(inode) + UDF_I_LENEATTR(inode);
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
		UDF_I_LENALLOC(inode);
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;

M
Marcin Slusarz 已提交
73
		if (UDF_I_LENALLOC(inode))
L
Linus Torvalds 已提交
74 75
			memmove(&ad[size], ad, UDF_I_LENALLOC(inode));

76
		if (UDF_I_LENEATTR(inode)) {
L
Linus Torvalds 已提交
77
			/* check checksum/crc */
M
Marcin Slusarz 已提交
78 79 80 81
			if (le16_to_cpu(eahd->descTag.tagIdent) !=
					TAG_IDENT_EAHD ||
			    le32_to_cpu(eahd->descTag.tagLocation) !=
					UDF_I_LOCATION(inode).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);
M
Marcin Slusarz 已提交
87 88
			UDF_I_LENEATTR(inode) +=
				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 97
			eahd->descTag.tagSerialNum =
					cpu_to_le16(sbi->s_serial_number);
			eahd->descTag.tagLocation = cpu_to_le32(
					UDF_I_LOCATION(inode).logicalBlockNum);
L
Linus Torvalds 已提交
98 99 100 101 102
			eahd->impAttrLocation = cpu_to_le32(0xFFFFFFFF);
			eahd->appAttrLocation = cpu_to_le32(0xFFFFFFFF);
		}

		offset = UDF_I_LENEATTR(inode);
103
		if (type < 2048) {
M
Marcin Slusarz 已提交
104 105 106 107
			if (le32_to_cpu(eahd->appAttrLocation) <
					UDF_I_LENEATTR(inode)) {
				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 115 116 117
			if (le32_to_cpu(eahd->impAttrLocation) <
					UDF_I_LENEATTR(inode)) {
				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 126 127 128
			if (le32_to_cpu(eahd->appAttrLocation) <
					UDF_I_LENEATTR(inode)) {
				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 137 138
			}
		}
		/* rewrite CRC + checksum of eahd */
		crclen = sizeof(struct extendedAttrHeaderDesc) - sizeof(tag);
		eahd->descTag.descCRCLength = cpu_to_le16(crclen);
139
		eahd->descTag.descCRC = cpu_to_le16(udf_crc((char *)eahd +
M
Marcin Slusarz 已提交
140
						sizeof(tag), crclen, 0));
L
Linus Torvalds 已提交
141
		eahd->descTag.tagChecksum = 0;
142
		for (i = 0; i < 16; i++)
L
Linus Torvalds 已提交
143
			if (i != 4)
M
Marcin Slusarz 已提交
144 145
				eahd->descTag.tagChecksum +=
					((uint8_t *)&(eahd->descTag))[i];
L
Linus Torvalds 已提交
146 147 148
		UDF_I_LENEATTR(inode) += size;
		return (struct genericFormat *)&ea[offset];
	}
M
Marcin Slusarz 已提交
149 150
	if (loc & 0x02)
		;
151

L
Linus Torvalds 已提交
152 153 154
	return NULL;
}

155 156
struct genericFormat *udf_get_extendedattr(struct inode *inode, uint32_t type,
					   uint8_t subtype)
L
Linus Torvalds 已提交
157 158 159 160 161 162 163
{
	struct genericFormat *gaf;
	uint8_t *ea = NULL;
	uint32_t offset;

	ea = UDF_I_DATA(inode);

164
	if (UDF_I_LENEATTR(inode)) {
L
Linus Torvalds 已提交
165 166 167 168
		struct extendedAttrHeaderDesc *eahd;
		eahd = (struct extendedAttrHeaderDesc *)ea;

		/* check checksum/crc */
M
Marcin Slusarz 已提交
169 170 171 172
		if (le16_to_cpu(eahd->descTag.tagIdent) !=
				TAG_IDENT_EAHD ||
		    le32_to_cpu(eahd->descTag.tagLocation) !=
				UDF_I_LOCATION(inode).logicalBlockNum)
L
Linus Torvalds 已提交
173
			return NULL;
174

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

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

L
Linus Torvalds 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204
	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.
 */
205
struct buffer_head *udf_read_tagged(struct super_block *sb, uint32_t block,
M
Marcin Slusarz 已提交
206
				    uint32_t location, uint16_t *ident)
L
Linus Torvalds 已提交
207 208 209 210 211
{
	tag *tag_p;
	struct buffer_head *bh = NULL;
	register uint8_t checksum;
	register int i;
M
Marcin Slusarz 已提交
212
	struct udf_sb_info *sbi = UDF_SB(sb);
L
Linus Torvalds 已提交
213 214 215 216 217

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

M
Marcin Slusarz 已提交
218
	bh = udf_tread(sb, block + sbi->s_session);
219 220
	if (!bh) {
		udf_debug("block=%d, location=%d: read failed\n",
M
Marcin Slusarz 已提交
221
			  block + sbi->s_session, location);
L
Linus Torvalds 已提交
222 223 224
		return NULL;
	}

225
	tag_p = (tag *)(bh->b_data);
L
Linus Torvalds 已提交
226 227 228

	*ident = le16_to_cpu(tag_p->tagIdent);

229
	if (location != le32_to_cpu(tag_p->tagLocation)) {
L
Linus Torvalds 已提交
230
		udf_debug("location mismatch block %u, tag %u != %u\n",
M
Marcin Slusarz 已提交
231 232
			  block + sbi->s_session,
			  le32_to_cpu(tag_p->tagLocation), location);
L
Linus Torvalds 已提交
233 234
		goto error_out;
	}
235

L
Linus Torvalds 已提交
236 237 238
	/* Verify the tag checksum */
	checksum = 0U;
	for (i = 0; i < 4; i++)
239
		checksum += (uint8_t)(bh->b_data[i]);
L
Linus Torvalds 已提交
240
	for (i = 5; i < 16; i++)
241
		checksum += (uint8_t)(bh->b_data[i]);
L
Linus Torvalds 已提交
242 243 244 245 246 247 248
	if (checksum != tag_p->tagChecksum) {
		printk(KERN_ERR "udf: tag checksum failed block %d\n", block);
		goto error_out;
	}

	/* Verify the tag version */
	if (le16_to_cpu(tag_p->descVersion) != 0x0002U &&
249
	    le16_to_cpu(tag_p->descVersion) != 0x0003U) {
L
Linus Torvalds 已提交
250
		udf_debug("tag version 0x%04x != 0x0002 || 0x0003 block %d\n",
251
			  le16_to_cpu(tag_p->descVersion), block);
L
Linus Torvalds 已提交
252 253 254 255 256
		goto error_out;
	}

	/* Verify the descriptor CRC */
	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),
M
Marcin Slusarz 已提交
258
					le16_to_cpu(tag_p->descCRCLength), 0))
L
Linus Torvalds 已提交
259
		return bh;
M
Marcin Slusarz 已提交
260

L
Linus Torvalds 已提交
261
	udf_debug("Crc failure block %d: crc = %d, crclen = %d\n",
M
Marcin Slusarz 已提交
262
		  block + sbi->s_session, le16_to_cpu(tag_p->descCRC),
263
		  le16_to_cpu(tag_p->descCRCLength));
L
Linus Torvalds 已提交
264

265
error_out:
L
Linus Torvalds 已提交
266 267 268 269
	brelse(bh);
	return NULL;
}

270
struct buffer_head *udf_read_ptagged(struct super_block *sb, kernel_lb_addr loc,
M
Marcin Slusarz 已提交
271
				     uint32_t offset, uint16_t *ident)
L
Linus Torvalds 已提交
272 273
{
	return udf_read_tagged(sb, udf_get_lb_pblock(sb, loc, offset),
274
			       loc.logicalBlockNum + offset, ident);
L
Linus Torvalds 已提交
275 276 277 278
}

void udf_update_tag(char *data, int length)
{
279
	tag *tptr = (tag *)data;
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287
	int i;

	length -= sizeof(tag);

	tptr->tagChecksum = 0;
	tptr->descCRCLength = cpu_to_le16(length);
	tptr->descCRC = cpu_to_le16(udf_crc(data + sizeof(tag), length, 0));

288
	for (i = 0; i < 16; i++)
L
Linus Torvalds 已提交
289
		if (i != 4)
290
			tptr->tagChecksum += (uint8_t)(data[i]);
L
Linus Torvalds 已提交
291 292 293
}

void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
294
		 uint32_t loc, int length)
L
Linus Torvalds 已提交
295
{
296
	tag *tptr = (tag *)data;
L
Linus Torvalds 已提交
297 298 299 300 301 302
	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);
}