directory.c 5.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
/*
 * 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>

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

	fibh->soffset = fibh->eoffset;

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

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

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

		return fi;
	}

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

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

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

62
		(*offset)++;
L
Linus Torvalds 已提交
63

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

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

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

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

	if (!fi)
		return NULL;

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

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

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

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

120
		(*offset)++;
L
Linus Torvalds 已提交
121 122 123 124

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

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

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

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

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

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

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

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

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

	ptr = buffer;

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

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

	return fi;
}

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

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

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

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

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

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

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

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