directory.c 5.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
/*
 * 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>
19
#include <linux/bio.h>
L
Linus Torvalds 已提交
20

M
Marcin Slusarz 已提交
21
struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
22 23 24
					 struct udf_fileident_bh *fibh,
					 struct fileIdentDesc *cfi,
					 struct extent_position *epos,
25
					 struct kernel_lb_addr *eloc, uint32_t *elen,
M
Marcin Slusarz 已提交
26
					 sector_t *offset)
L
Linus Torvalds 已提交
27 28
{
	struct fileIdentDesc *fi;
29 30
	int i, num;
	udf_pblk_t block;
31
	struct buffer_head *tmp, *bha[16];
32
	struct udf_inode_info *iinfo = UDF_I(dir);
L
Linus Torvalds 已提交
33 34 35

	fibh->soffset = fibh->eoffset;

36 37 38
	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
		fi = udf_get_fileident(iinfo->i_ext.i_data -
				       (iinfo->i_efe ?
39 40
					sizeof(struct extendedFileEntry) :
					sizeof(struct fileEntry)),
M
Marcin Slusarz 已提交
41 42
				       dir->i_sb->s_blocksize,
				       &(fibh->eoffset));
L
Linus Torvalds 已提交
43 44 45
		if (!fi)
			return NULL;

46
		*nf_pos += fibh->eoffset - fibh->soffset;
L
Linus Torvalds 已提交
47

48
		memcpy((uint8_t *)cfi, (uint8_t *)fi,
49
		       sizeof(struct fileIdentDesc));
L
Linus Torvalds 已提交
50 51 52 53

		return fi;
	}

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

J
Jan Kara 已提交
58
		if (udf_next_aext(dir, epos, eloc, elen, 1) !=
59
		    (EXT_RECORDED_ALLOCATED >> 30))
L
Linus Torvalds 已提交
60 61
			return NULL;

62
		block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
L
Linus Torvalds 已提交
63

64
		(*offset)++;
L
Linus Torvalds 已提交
65

M
Marcin Slusarz 已提交
66
		if ((*offset << blocksize_bits) >= *elen)
L
Linus Torvalds 已提交
67 68
			*offset = 0;
		else
J
Jan Kara 已提交
69
			epos->offset = lextoffset;
L
Linus Torvalds 已提交
70

J
Jan Kara 已提交
71
		brelse(fibh->sbh);
M
Marcin Slusarz 已提交
72 73
		fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
		if (!fibh->sbh)
L
Linus Torvalds 已提交
74 75 76
			return NULL;
		fibh->soffset = fibh->eoffset = 0;

M
Marcin Slusarz 已提交
77 78 79 80
		if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
			i = 16 >> (blocksize_bits - 9);
			if (i + *offset > (*elen >> blocksize_bits))
				i = (*elen >> blocksize_bits)-*offset;
81
			for (num = 0; i > 0; i--) {
82
				block = udf_get_lb_pblock(dir->i_sb, eloc,
M
Marcin Slusarz 已提交
83
							  *offset + i);
L
Linus Torvalds 已提交
84
				tmp = udf_tgetblk(dir->i_sb, block);
M
Marcin Slusarz 已提交
85 86
				if (tmp && !buffer_uptodate(tmp) &&
						!buffer_locked(tmp))
L
Linus Torvalds 已提交
87 88 89 90
					bha[num++] = tmp;
				else
					brelse(tmp);
			}
91
			if (num) {
92
				ll_rw_block(REQ_OP_READ, REQ_RAHEAD, num, bha);
93
				for (i = 0; i < num; i++)
L
Linus Torvalds 已提交
94 95 96
					brelse(bha[i]);
			}
		}
97
	} else if (fibh->sbh != fibh->ebh) {
J
Jan Kara 已提交
98
		brelse(fibh->sbh);
L
Linus Torvalds 已提交
99 100 101 102
		fibh->sbh = fibh->ebh;
	}

	fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
103
			       &(fibh->eoffset));
L
Linus Torvalds 已提交
104 105 106 107

	if (!fi)
		return NULL;

108
	*nf_pos += fibh->eoffset - fibh->soffset;
L
Linus Torvalds 已提交
109

110
	if (fibh->eoffset <= dir->i_sb->s_blocksize) {
111
		memcpy((uint8_t *)cfi, (uint8_t *)fi,
112 113
		       sizeof(struct fileIdentDesc));
	} else if (fibh->eoffset > dir->i_sb->s_blocksize) {
J
Jan Kara 已提交
114
		int lextoffset = epos->offset;
L
Linus Torvalds 已提交
115

J
Jan Kara 已提交
116
		if (udf_next_aext(dir, epos, eloc, elen, 1) !=
117
		    (EXT_RECORDED_ALLOCATED >> 30))
L
Linus Torvalds 已提交
118 119
			return NULL;

120
		block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
L
Linus Torvalds 已提交
121

122
		(*offset)++;
L
Linus Torvalds 已提交
123 124 125 126

		if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
			*offset = 0;
		else
J
Jan Kara 已提交
127
			epos->offset = lextoffset;
L
Linus Torvalds 已提交
128 129 130 131

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

M
Marcin Slusarz 已提交
132 133
		fibh->ebh = udf_tread(dir->i_sb, block);
		if (!fibh->ebh)
L
Linus Torvalds 已提交
134 135
			return NULL;

136
		if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
L
Linus Torvalds 已提交
137 138
			int fi_len;

139
			memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
M
Marcin Slusarz 已提交
140 141
			memcpy((uint8_t *)cfi - fibh->soffset,
			       fibh->ebh->b_data,
142
			       sizeof(struct fileIdentDesc) + fibh->soffset);
L
Linus Torvalds 已提交
143

M
Marcin Slusarz 已提交
144 145
			fi_len = (sizeof(struct fileIdentDesc) +
				  cfi->lengthFileIdent +
146
				  le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
L
Linus Torvalds 已提交
147

148
			*nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
L
Linus Torvalds 已提交
149
			fibh->eoffset = fibh->soffset + fi_len;
150
		} else {
151
			memcpy((uint8_t *)cfi, (uint8_t *)fi,
152
			       sizeof(struct fileIdentDesc));
L
Linus Torvalds 已提交
153 154 155 156 157
		}
	}
	return fi;
}

158
struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
L
Linus Torvalds 已提交
159 160 161
{
	struct fileIdentDesc *fi;
	int lengthThisIdent;
162
	uint8_t *ptr;
L
Linus Torvalds 已提交
163 164
	int padlen;

165
	if ((!buffer) || (!offset)) {
J
Joe Perches 已提交
166 167
		udf_debug("invalidparms, buffer=%p, offset=%p\n",
			  buffer, offset);
L
Linus Torvalds 已提交
168 169 170 171 172
		return NULL;
	}

	ptr = buffer;

M
Marcin Slusarz 已提交
173
	if ((*offset > 0) && (*offset < bufsize))
L
Linus Torvalds 已提交
174
		ptr += *offset;
175
	fi = (struct fileIdentDesc *)ptr;
176
	if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
L
Linus Torvalds 已提交
177
		udf_debug("0x%x != TAG_IDENT_FID\n",
178
			  le16_to_cpu(fi->descTag.tagIdent));
L
Linus Torvalds 已提交
179
		udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
180 181
			  *offset, (unsigned long)sizeof(struct fileIdentDesc),
			  bufsize);
L
Linus Torvalds 已提交
182 183
		return NULL;
	}
M
Marcin Slusarz 已提交
184
	if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
L
Linus Torvalds 已提交
185
		lengthThisIdent = sizeof(struct fileIdentDesc);
M
Marcin Slusarz 已提交
186
	else
L
Linus Torvalds 已提交
187
		lengthThisIdent = sizeof(struct fileIdentDesc) +
188
			fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196 197 198

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

	return fi;
}

199
struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
200
			      int inc)
L
Linus Torvalds 已提交
201
{
202
	struct short_ad *sa;
L
Linus Torvalds 已提交
203

204
	if ((!ptr) || (!offset)) {
J
Joe Perches 已提交
205
		pr_err("%s: invalidparms\n", __func__);
L
Linus Torvalds 已提交
206 207 208
		return NULL;
	}

209
	if ((*offset + sizeof(struct short_ad)) > maxoffset)
L
Linus Torvalds 已提交
210
		return NULL;
M
Marcin Slusarz 已提交
211
	else {
212
		sa = (struct short_ad *)ptr;
M
Marcin Slusarz 已提交
213 214 215
		if (sa->extLength == 0)
			return NULL;
	}
L
Linus Torvalds 已提交
216 217

	if (inc)
218
		*offset += sizeof(struct short_ad);
L
Linus Torvalds 已提交
219 220 221
	return sa;
}

222
struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
L
Linus Torvalds 已提交
223
{
224
	struct long_ad *la;
L
Linus Torvalds 已提交
225

226
	if ((!ptr) || (!offset)) {
J
Joe Perches 已提交
227
		pr_err("%s: invalidparms\n", __func__);
L
Linus Torvalds 已提交
228 229 230
		return NULL;
	}

231
	if ((*offset + sizeof(struct long_ad)) > maxoffset)
L
Linus Torvalds 已提交
232
		return NULL;
M
Marcin Slusarz 已提交
233
	else {
234
		la = (struct long_ad *)ptr;
M
Marcin Slusarz 已提交
235 236 237
		if (la->extLength == 0)
			return NULL;
	}
L
Linus Torvalds 已提交
238 239

	if (inc)
240
		*offset += sizeof(struct long_ad);
L
Linus Torvalds 已提交
241 242
	return la;
}