directory.c 7.5 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
/*
 * directory.c
 *
 * PURPOSE
 *	Directory related functions
 *
 * 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.
 */

#include "udfdecl.h"
#include "udf_i.h"

#include <linux/fs.h>
#include <linux/string.h>
#include <linux/buffer_head.h>

#if 0
M
Marcin Slusarz 已提交
22
static uint8_t *udf_filead_read(struct inode *dir, uint8_t *tmpad,
23 24 25
				uint8_t ad_size, kernel_lb_addr fe_loc,
				int *pos, int *offset, struct buffer_head **bh,
				int *error)
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33
{
	int loffset = *offset;
	int block;
	uint8_t *ad;
	int remainder;

	*error = 0;

34
	ad = (uint8_t *)(*bh)->b_data + *offset;
L
Linus Torvalds 已提交
35 36
	*offset += ad_size;

37
	if (!ad) {
J
Jan Kara 已提交
38
		brelse(*bh);
L
Linus Torvalds 已提交
39 40 41 42
		*error = 1;
		return NULL;
	}

43
	if (*offset == dir->i_sb->s_blocksize) {
J
Jan Kara 已提交
44
		brelse(*bh);
L
Linus Torvalds 已提交
45 46 47
		block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
		if (!block)
			return NULL;
M
Marcin Slusarz 已提交
48 49
		*bh = udf_tread(dir->i_sb, block);
		if (!*bh)
L
Linus Torvalds 已提交
50
			return NULL;
51
	} else if (*offset > dir->i_sb->s_blocksize) {
L
Linus Torvalds 已提交
52 53 54
		ad = tmpad;

		remainder = dir->i_sb->s_blocksize - loffset;
55
		memcpy((uint8_t *)ad, (*bh)->b_data + loffset, remainder);
L
Linus Torvalds 已提交
56

J
Jan Kara 已提交
57
		brelse(*bh);
L
Linus Torvalds 已提交
58 59 60
		block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
		if (!block)
			return NULL;
M
Marcin Slusarz 已提交
61 62
		(*bh) = udf_tread(dir->i_sb, block);
		if (!*bh)
L
Linus Torvalds 已提交
63 64
			return NULL;

M
Marcin Slusarz 已提交
65 66
		memcpy((uint8_t *)ad + remainder, (*bh)->b_data,
			ad_size - remainder);
L
Linus Torvalds 已提交
67 68
		*offset = ad_size - remainder;
	}
69

L
Linus Torvalds 已提交
70 71 72 73
	return ad;
}
#endif

M
Marcin Slusarz 已提交
74
struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
75 76 77
					 struct udf_fileident_bh *fibh,
					 struct fileIdentDesc *cfi,
					 struct extent_position *epos,
M
Marcin Slusarz 已提交
78 79
					 kernel_lb_addr *eloc, uint32_t *elen,
					 sector_t *offset)
L
Linus Torvalds 已提交
80 81 82
{
	struct fileIdentDesc *fi;
	int i, num, block;
83
	struct buffer_head *tmp, *bha[16];
84
	struct udf_inode_info *iinfo = UDF_I(dir);
L
Linus Torvalds 已提交
85 86 87

	fibh->soffset = fibh->eoffset;

88 89 90
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
		fi = udf_get_fileident(iinfo->i_ext.i_data -
				       (iinfo->i_efe ?
91 92
					sizeof(struct extendedFileEntry) :
					sizeof(struct fileEntry)),
M
Marcin Slusarz 已提交
93 94
				       dir->i_sb->s_blocksize,
				       &(fibh->eoffset));
L
Linus Torvalds 已提交
95 96 97 98 99
		if (!fi)
			return NULL;

		*nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);

100
		memcpy((uint8_t *)cfi, (uint8_t *)fi,
101
		       sizeof(struct fileIdentDesc));
L
Linus Torvalds 已提交
102 103 104 105

		return fi;
	}

106
	if (fibh->eoffset == dir->i_sb->s_blocksize) {
J
Jan Kara 已提交
107
		int lextoffset = epos->offset;
M
Marcin Slusarz 已提交
108
		unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
L
Linus Torvalds 已提交
109

J
Jan Kara 已提交
110
		if (udf_next_aext(dir, epos, eloc, elen, 1) !=
111
		    (EXT_RECORDED_ALLOCATED >> 30))
L
Linus Torvalds 已提交
112 113 114 115
			return NULL;

		block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);

116
		(*offset)++;
L
Linus Torvalds 已提交
117

M
Marcin Slusarz 已提交
118
		if ((*offset << blocksize_bits) >= *elen)
L
Linus Torvalds 已提交
119 120
			*offset = 0;
		else
J
Jan Kara 已提交
121
			epos->offset = lextoffset;
L
Linus Torvalds 已提交
122

J
Jan Kara 已提交
123
		brelse(fibh->sbh);
M
Marcin Slusarz 已提交
124 125
		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
		if (!fibh->sbh)
L
Linus Torvalds 已提交
126 127 128
			return NULL;
		fibh->soffset = fibh->eoffset = 0;

M
Marcin Slusarz 已提交
129 130 131 132
		if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
			i = 16 >> (blocksize_bits - 9);
			if (i + *offset > (*elen >> blocksize_bits))
				i = (*elen >> blocksize_bits)-*offset;
133
			for (num = 0; i > 0; i--) {
M
Marcin Slusarz 已提交
134 135
				block = udf_get_lb_pblock(dir->i_sb, *eloc,
							  *offset + i);
L
Linus Torvalds 已提交
136
				tmp = udf_tgetblk(dir->i_sb, block);
M
Marcin Slusarz 已提交
137 138
				if (tmp && !buffer_uptodate(tmp) &&
						!buffer_locked(tmp))
L
Linus Torvalds 已提交
139 140 141 142
					bha[num++] = tmp;
				else
					brelse(tmp);
			}
143
			if (num) {
L
Linus Torvalds 已提交
144
				ll_rw_block(READA, num, bha);
145
				for (i = 0; i < num; i++)
L
Linus Torvalds 已提交
146 147 148
					brelse(bha[i]);
			}
		}
149
	} else if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
150
		brelse(fibh->sbh);
L
Linus Torvalds 已提交
151 152 153 154
		fibh->sbh = fibh->ebh;
	}

	fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
155
			       &(fibh->eoffset));
L
Linus Torvalds 已提交
156 157 158 159 160 161

	if (!fi)
		return NULL;

	*nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);

162
	if (fibh->eoffset <= dir->i_sb->s_blocksize) {
163
		memcpy((uint8_t *)cfi, (uint8_t *)fi,
164 165
		       sizeof(struct fileIdentDesc));
	} else if (fibh->eoffset > dir->i_sb->s_blocksize) {
J
Jan Kara 已提交
166
		int lextoffset = epos->offset;
L
Linus Torvalds 已提交
167

J
Jan Kara 已提交
168
		if (udf_next_aext(dir, epos, eloc, elen, 1) !=
169
		    (EXT_RECORDED_ALLOCATED >> 30))
L
Linus Torvalds 已提交
170 171 172 173
			return NULL;

		block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);

174
		(*offset)++;
L
Linus Torvalds 已提交
175 176 177 178

		if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
			*offset = 0;
		else
J
Jan Kara 已提交
179
			epos->offset = lextoffset;
L
Linus Torvalds 已提交
180 181 182 183

		fibh->soffset -= dir->i_sb->s_blocksize;
		fibh->eoffset -= dir->i_sb->s_blocksize;

M
Marcin Slusarz 已提交
184 185
		fibh->ebh = udf_tread(dir->i_sb, block);
		if (!fibh->ebh)
L
Linus Torvalds 已提交
186 187
			return NULL;

188
		if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
L
Linus Torvalds 已提交
189 190
			int fi_len;

191
			memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
M
Marcin Slusarz 已提交
192 193
			memcpy((uint8_t *)cfi - fibh->soffset,
			       fibh->ebh->b_data,
194
			       sizeof(struct fileIdentDesc) + fibh->soffset);
L
Linus Torvalds 已提交
195

M
Marcin Slusarz 已提交
196 197
			fi_len = (sizeof(struct fileIdentDesc) +
				  cfi->lengthFileIdent +
198
				  le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
L
Linus Torvalds 已提交
199

M
Marcin Slusarz 已提交
200 201
			*nf_pos += (fi_len - (fibh->eoffset - fibh->soffset))
					>> 2;
L
Linus Torvalds 已提交
202
			fibh->eoffset = fibh->soffset + fi_len;
203
		} else {
204
			memcpy((uint8_t *)cfi, (uint8_t *)fi,
205
			       sizeof(struct fileIdentDesc));
L
Linus Torvalds 已提交
206 207 208 209 210
		}
	}
	return fi;
}

211
struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
L
Linus Torvalds 已提交
212 213 214
{
	struct fileIdentDesc *fi;
	int lengthThisIdent;
215
	uint8_t *ptr;
L
Linus Torvalds 已提交
216 217
	int padlen;

218 219 220
	if ((!buffer) || (!offset)) {
		udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer,
			  offset);
L
Linus Torvalds 已提交
221 222 223 224 225
		return NULL;
	}

	ptr = buffer;

M
Marcin Slusarz 已提交
226
	if ((*offset > 0) && (*offset < bufsize))
L
Linus Torvalds 已提交
227
		ptr += *offset;
228
	fi = (struct fileIdentDesc *)ptr;
229
	if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
L
Linus Torvalds 已提交
230
		udf_debug("0x%x != TAG_IDENT_FID\n",
231
			  le16_to_cpu(fi->descTag.tagIdent));
L
Linus Torvalds 已提交
232
		udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
233 234
			  *offset, (unsigned long)sizeof(struct fileIdentDesc),
			  bufsize);
L
Linus Torvalds 已提交
235 236
		return NULL;
	}
M
Marcin Slusarz 已提交
237
	if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
L
Linus Torvalds 已提交
238
		lengthThisIdent = sizeof(struct fileIdentDesc);
M
Marcin Slusarz 已提交
239
	else
L
Linus Torvalds 已提交
240
		lengthThisIdent = sizeof(struct fileIdentDesc) +
241
			fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
L
Linus Torvalds 已提交
242 243 244 245 246 247 248 249 250 251 252

	/* we need to figure padding, too! */
	padlen = lengthThisIdent % UDF_NAME_PAD;
	if (padlen)
		lengthThisIdent += (UDF_NAME_PAD - padlen);
	*offset = *offset + lengthThisIdent;

	return fi;
}

#if 0
253
static extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset)
L
Linus Torvalds 已提交
254
{
255
	extent_ad *ext;
L
Linus Torvalds 已提交
256
	struct fileEntry *fe;
257
	uint8_t *ptr;
L
Linus Torvalds 已提交
258

259
	if ((!buffer) || (!offset)) {
L
Linus Torvalds 已提交
260 261 262 263 264 265
		printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n");
		return NULL;
	}

	fe = (struct fileEntry *)buffer;

266
	if (fe->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FE)) {
L
Linus Torvalds 已提交
267
		udf_debug("0x%x != TAG_IDENT_FE\n",
268
			  le16_to_cpu(fe->descTag.tagIdent));
L
Linus Torvalds 已提交
269 270 271
		return NULL;
	}

M
Marcin Slusarz 已提交
272 273
	ptr = (uint8_t *)(fe->extendedAttr) +
		le32_to_cpu(fe->lengthExtendedAttr);
L
Linus Torvalds 已提交
274

M
Marcin Slusarz 已提交
275
	if ((*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs)))
L
Linus Torvalds 已提交
276 277
		ptr += *offset;

278
	ext = (extent_ad *)ptr;
L
Linus Torvalds 已提交
279 280 281 282 283 284

	*offset = *offset + sizeof(extent_ad);
	return ext;
}
#endif

285
short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
286
			      int inc)
L
Linus Torvalds 已提交
287 288 289
{
	short_ad *sa;

290
	if ((!ptr) || (!offset)) {
L
Linus Torvalds 已提交
291 292 293 294
		printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n");
		return NULL;
	}

295
	if ((*offset + sizeof(short_ad)) > maxoffset)
L
Linus Torvalds 已提交
296
		return NULL;
M
Marcin Slusarz 已提交
297 298 299 300 301
	else {
		sa = (short_ad *)ptr;
		if (sa->extLength == 0)
			return NULL;
	}
L
Linus Torvalds 已提交
302 303 304 305 306 307

	if (inc)
		*offset += sizeof(short_ad);
	return sa;
}

308
long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
L
Linus Torvalds 已提交
309 310 311
{
	long_ad *la;

312
	if ((!ptr) || (!offset)) {
L
Linus Torvalds 已提交
313 314 315 316
		printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n");
		return NULL;
	}

317
	if ((*offset + sizeof(long_ad)) > maxoffset)
L
Linus Torvalds 已提交
318
		return NULL;
M
Marcin Slusarz 已提交
319 320 321 322 323
	else {
		la = (long_ad *)ptr;
		if (la->extLength == 0)
			return NULL;
	}
L
Linus Torvalds 已提交
324 325 326 327 328

	if (inc)
		*offset += sizeof(long_ad);
	return la;
}