misc.c 7.7 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
/*
 * 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>

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

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

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

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

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

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

	/* TODO - Check for FreeEASpace */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

M
Marcin Slusarz 已提交
212
	bh = udf_tread(sb, block + sbi->s_session);
213 214
	if (!bh) {
		udf_debug("block=%d, location=%d: read failed\n",
M
Marcin Slusarz 已提交
215
			  block + sbi->s_session, location);
L
Linus Torvalds 已提交
216 217 218
		return NULL;
	}

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

	*ident = le16_to_cpu(tag_p->tagIdent);

223
	if (location != le32_to_cpu(tag_p->tagLocation)) {
L
Linus Torvalds 已提交
224
		udf_debug("location mismatch block %u, tag %u != %u\n",
M
Marcin Slusarz 已提交
225 226
			  block + sbi->s_session,
			  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
	if (udf_tag_checksum(tag_p) != tag_p->tagChecksum) {
L
Linus Torvalds 已提交
232 233 234 235 236
		printk(KERN_ERR "udf: tag checksum failed block %d\n", block);
		goto error_out;
	}

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

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

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

254
error_out:
L
Linus Torvalds 已提交
255 256 257 258
	brelse(bh);
	return NULL;
}

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

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

	tptr->descCRCLength = cpu_to_le16(length);
	tptr->descCRC = cpu_to_le16(udf_crc(data + sizeof(tag), length, 0));
273
	tptr->tagChecksum = udf_tag_checksum(tptr);
L
Linus Torvalds 已提交
274 275 276
}

void udf_new_tag(char *data, uint16_t ident, uint16_t version, uint16_t snum,
277
		 uint32_t loc, int length)
L
Linus Torvalds 已提交
278
{
279
	tag *tptr = (tag *)data;
L
Linus Torvalds 已提交
280 281 282 283 284 285
	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);
}
286 287 288 289 290 291 292 293 294 295 296

u8 udf_tag_checksum(const tag *t)
{
	u8 *data = (u8 *)t;
	u8 checksum = 0;
	int i;
	for (i = 0; i < sizeof(tag); ++i)
		if (i != 4) /* position of checksum */
			checksum += data[i];
	return checksum;
}