Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * directory.c | |
3 | * | |
4 | * PURPOSE | |
5 | * Directory related functions | |
6 | * | |
1da177e4 LT |
7 | * COPYRIGHT |
8 | * This file is distributed under the terms of the GNU General Public | |
9 | * License (GPL). Copies of the GPL can be obtained from: | |
10 | * ftp://prep.ai.mit.edu/pub/gnu/GPL | |
11 | * Each contributing author retains all rights to their own work. | |
12 | */ | |
13 | ||
14 | #include "udfdecl.h" | |
15 | #include "udf_i.h" | |
16 | ||
17 | #include <linux/fs.h> | |
18 | #include <linux/string.h> | |
19 | #include <linux/buffer_head.h> | |
20 | ||
21 | #if 0 | |
cb00ea35 CG |
22 | static uint8_t *udf_filead_read(struct inode *dir, uint8_t * tmpad, |
23 | uint8_t ad_size, kernel_lb_addr fe_loc, | |
24 | int *pos, int *offset, struct buffer_head **bh, | |
25 | int *error) | |
1da177e4 LT |
26 | { |
27 | int loffset = *offset; | |
28 | int block; | |
29 | uint8_t *ad; | |
30 | int remainder; | |
31 | ||
32 | *error = 0; | |
33 | ||
cb00ea35 | 34 | ad = (uint8_t *) (*bh)->b_data + *offset; |
1da177e4 LT |
35 | *offset += ad_size; |
36 | ||
cb00ea35 | 37 | if (!ad) { |
3bf25cb4 | 38 | brelse(*bh); |
1da177e4 LT |
39 | *error = 1; |
40 | return NULL; | |
41 | } | |
42 | ||
cb00ea35 | 43 | if (*offset == dir->i_sb->s_blocksize) { |
3bf25cb4 | 44 | brelse(*bh); |
1da177e4 LT |
45 | block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos); |
46 | if (!block) | |
47 | return NULL; | |
48 | if (!(*bh = udf_tread(dir->i_sb, block))) | |
49 | return NULL; | |
cb00ea35 | 50 | } else if (*offset > dir->i_sb->s_blocksize) { |
1da177e4 LT |
51 | ad = tmpad; |
52 | ||
53 | remainder = dir->i_sb->s_blocksize - loffset; | |
cb00ea35 | 54 | memcpy((uint8_t *) ad, (*bh)->b_data + loffset, remainder); |
1da177e4 | 55 | |
3bf25cb4 | 56 | brelse(*bh); |
1da177e4 LT |
57 | block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos); |
58 | if (!block) | |
59 | return NULL; | |
60 | if (!((*bh) = udf_tread(dir->i_sb, block))) | |
61 | return NULL; | |
62 | ||
cb00ea35 CG |
63 | memcpy((uint8_t *) ad + remainder, (*bh)->b_data, |
64 | ad_size - remainder); | |
1da177e4 LT |
65 | *offset = ad_size - remainder; |
66 | } | |
67 | return ad; | |
68 | } | |
69 | #endif | |
70 | ||
cb00ea35 CG |
71 | struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t * nf_pos, |
72 | struct udf_fileident_bh *fibh, | |
73 | struct fileIdentDesc *cfi, | |
74 | struct extent_position *epos, | |
75 | kernel_lb_addr * eloc, uint32_t * elen, | |
76 | sector_t * offset) | |
1da177e4 LT |
77 | { |
78 | struct fileIdentDesc *fi; | |
79 | int i, num, block; | |
cb00ea35 | 80 | struct buffer_head *tmp, *bha[16]; |
1da177e4 LT |
81 | |
82 | fibh->soffset = fibh->eoffset; | |
83 | ||
cb00ea35 | 84 | if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) { |
1da177e4 | 85 | fi = udf_get_fileident(UDF_I_DATA(dir) - |
cb00ea35 CG |
86 | (UDF_I_EFE(dir) ? |
87 | sizeof(struct extendedFileEntry) : | |
88 | sizeof(struct fileEntry)), | |
89 | dir->i_sb->s_blocksize, | |
90 | &(fibh->eoffset)); | |
1da177e4 LT |
91 | |
92 | if (!fi) | |
93 | return NULL; | |
94 | ||
95 | *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2); | |
96 | ||
cb00ea35 CG |
97 | memcpy((uint8_t *) cfi, (uint8_t *) fi, |
98 | sizeof(struct fileIdentDesc)); | |
1da177e4 LT |
99 | |
100 | return fi; | |
101 | } | |
102 | ||
cb00ea35 | 103 | if (fibh->eoffset == dir->i_sb->s_blocksize) { |
ff116fc8 | 104 | int lextoffset = epos->offset; |
1da177e4 | 105 | |
ff116fc8 | 106 | if (udf_next_aext(dir, epos, eloc, elen, 1) != |
cb00ea35 | 107 | (EXT_RECORDED_ALLOCATED >> 30)) |
1da177e4 | 108 | return NULL; |
1da177e4 LT |
109 | |
110 | block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset); | |
111 | ||
cb00ea35 | 112 | (*offset)++; |
1da177e4 LT |
113 | |
114 | if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen) | |
115 | *offset = 0; | |
116 | else | |
ff116fc8 | 117 | epos->offset = lextoffset; |
1da177e4 | 118 | |
3bf25cb4 | 119 | brelse(fibh->sbh); |
1da177e4 LT |
120 | if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block))) |
121 | return NULL; | |
122 | fibh->soffset = fibh->eoffset = 0; | |
123 | ||
cb00ea35 CG |
124 | if (! |
125 | (*offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9)) - 1))) | |
1da177e4 LT |
126 | { |
127 | i = 16 >> (dir->i_sb->s_blocksize_bits - 9); | |
cb00ea35 CG |
128 | if (i + *offset > |
129 | (*elen >> dir->i_sb->s_blocksize_bits)) | |
130 | i = (*elen >> dir->i_sb->s_blocksize_bits) - | |
131 | *offset; | |
132 | for (num = 0; i > 0; i--) { | |
133 | block = | |
134 | udf_get_lb_pblock(dir->i_sb, *eloc, | |
135 | *offset + i); | |
1da177e4 | 136 | tmp = udf_tgetblk(dir->i_sb, block); |
cb00ea35 CG |
137 | if (tmp && !buffer_uptodate(tmp) |
138 | && !buffer_locked(tmp)) | |
1da177e4 LT |
139 | bha[num++] = tmp; |
140 | else | |
141 | brelse(tmp); | |
142 | } | |
cb00ea35 | 143 | if (num) { |
1da177e4 | 144 | ll_rw_block(READA, num, bha); |
cb00ea35 | 145 | for (i = 0; i < num; i++) |
1da177e4 LT |
146 | brelse(bha[i]); |
147 | } | |
148 | } | |
cb00ea35 | 149 | } else if (fibh->sbh != fibh->ebh) { |
3bf25cb4 | 150 | brelse(fibh->sbh); |
1da177e4 LT |
151 | fibh->sbh = fibh->ebh; |
152 | } | |
153 | ||
154 | fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize, | |
cb00ea35 | 155 | &(fibh->eoffset)); |
1da177e4 LT |
156 | |
157 | if (!fi) | |
158 | return NULL; | |
159 | ||
160 | *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2); | |
161 | ||
cb00ea35 CG |
162 | if (fibh->eoffset <= dir->i_sb->s_blocksize) { |
163 | memcpy((uint8_t *) cfi, (uint8_t *) fi, | |
164 | sizeof(struct fileIdentDesc)); | |
165 | } else if (fibh->eoffset > dir->i_sb->s_blocksize) { | |
ff116fc8 | 166 | int lextoffset = epos->offset; |
1da177e4 | 167 | |
ff116fc8 | 168 | if (udf_next_aext(dir, epos, eloc, elen, 1) != |
cb00ea35 | 169 | (EXT_RECORDED_ALLOCATED >> 30)) |
1da177e4 | 170 | return NULL; |
1da177e4 LT |
171 | |
172 | block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset); | |
173 | ||
cb00ea35 | 174 | (*offset)++; |
1da177e4 LT |
175 | |
176 | if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen) | |
177 | *offset = 0; | |
178 | else | |
ff116fc8 | 179 | epos->offset = lextoffset; |
1da177e4 LT |
180 | |
181 | fibh->soffset -= dir->i_sb->s_blocksize; | |
182 | fibh->eoffset -= dir->i_sb->s_blocksize; | |
183 | ||
184 | if (!(fibh->ebh = udf_tread(dir->i_sb, block))) | |
185 | return NULL; | |
186 | ||
cb00ea35 | 187 | if (sizeof(struct fileIdentDesc) > -fibh->soffset) { |
1da177e4 LT |
188 | int fi_len; |
189 | ||
cb00ea35 CG |
190 | memcpy((uint8_t *) cfi, (uint8_t *) fi, -fibh->soffset); |
191 | memcpy((uint8_t *) cfi - fibh->soffset, | |
192 | fibh->ebh->b_data, | |
193 | sizeof(struct fileIdentDesc) + fibh->soffset); | |
1da177e4 | 194 | |
cb00ea35 CG |
195 | fi_len = |
196 | (sizeof(struct fileIdentDesc) + | |
197 | cfi->lengthFileIdent + | |
198 | le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3; | |
1da177e4 | 199 | |
cb00ea35 CG |
200 | *nf_pos += |
201 | ((fi_len - (fibh->eoffset - fibh->soffset)) >> 2); | |
1da177e4 | 202 | fibh->eoffset = fibh->soffset + fi_len; |
cb00ea35 CG |
203 | } else { |
204 | memcpy((uint8_t *) cfi, (uint8_t *) fi, | |
205 | sizeof(struct fileIdentDesc)); | |
1da177e4 LT |
206 | } |
207 | } | |
208 | return fi; | |
209 | } | |
210 | ||
cb00ea35 | 211 | struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset) |
1da177e4 LT |
212 | { |
213 | struct fileIdentDesc *fi; | |
214 | int lengthThisIdent; | |
cb00ea35 | 215 | uint8_t *ptr; |
1da177e4 LT |
216 | int padlen; |
217 | ||
cb00ea35 CG |
218 | if ((!buffer) || (!offset)) { |
219 | udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer, | |
220 | offset); | |
1da177e4 LT |
221 | return NULL; |
222 | } | |
223 | ||
224 | ptr = buffer; | |
225 | ||
cb00ea35 | 226 | if ((*offset > 0) && (*offset < bufsize)) { |
1da177e4 LT |
227 | ptr += *offset; |
228 | } | |
cb00ea35 CG |
229 | fi = (struct fileIdentDesc *)ptr; |
230 | if (le16_to_cpu(fi->descTag.tagIdent) != TAG_IDENT_FID) { | |
1da177e4 | 231 | udf_debug("0x%x != TAG_IDENT_FID\n", |
cb00ea35 | 232 | le16_to_cpu(fi->descTag.tagIdent)); |
1da177e4 | 233 | udf_debug("offset: %u sizeof: %lu bufsize: %u\n", |
cb00ea35 CG |
234 | *offset, (unsigned long)sizeof(struct fileIdentDesc), |
235 | bufsize); | |
1da177e4 LT |
236 | return NULL; |
237 | } | |
cb00ea35 | 238 | if ((*offset + sizeof(struct fileIdentDesc)) > bufsize) { |
1da177e4 | 239 | lengthThisIdent = sizeof(struct fileIdentDesc); |
cb00ea35 | 240 | } else |
1da177e4 | 241 | lengthThisIdent = sizeof(struct fileIdentDesc) + |
cb00ea35 | 242 | fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse); |
1da177e4 LT |
243 | |
244 | /* we need to figure padding, too! */ | |
245 | padlen = lengthThisIdent % UDF_NAME_PAD; | |
246 | if (padlen) | |
247 | lengthThisIdent += (UDF_NAME_PAD - padlen); | |
248 | *offset = *offset + lengthThisIdent; | |
249 | ||
250 | return fi; | |
251 | } | |
252 | ||
253 | #if 0 | |
cb00ea35 | 254 | static extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset) |
1da177e4 | 255 | { |
cb00ea35 | 256 | extent_ad *ext; |
1da177e4 | 257 | struct fileEntry *fe; |
cb00ea35 | 258 | uint8_t *ptr; |
1da177e4 | 259 | |
cb00ea35 | 260 | if ((!buffer) || (!offset)) { |
1da177e4 LT |
261 | printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n"); |
262 | return NULL; | |
263 | } | |
264 | ||
265 | fe = (struct fileEntry *)buffer; | |
266 | ||
cb00ea35 | 267 | if (le16_to_cpu(fe->descTag.tagIdent) != TAG_IDENT_FE) { |
1da177e4 | 268 | udf_debug("0x%x != TAG_IDENT_FE\n", |
cb00ea35 | 269 | le16_to_cpu(fe->descTag.tagIdent)); |
1da177e4 LT |
270 | return NULL; |
271 | } | |
272 | ||
cb00ea35 CG |
273 | ptr = |
274 | (uint8_t *) (fe->extendedAttr) + | |
275 | le32_to_cpu(fe->lengthExtendedAttr); | |
1da177e4 | 276 | |
cb00ea35 | 277 | if ((*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs))) { |
1da177e4 LT |
278 | ptr += *offset; |
279 | } | |
280 | ||
cb00ea35 | 281 | ext = (extent_ad *) ptr; |
1da177e4 LT |
282 | |
283 | *offset = *offset + sizeof(extent_ad); | |
284 | return ext; | |
285 | } | |
286 | #endif | |
287 | ||
cb00ea35 CG |
288 | short_ad *udf_get_fileshortad(uint8_t * ptr, int maxoffset, int *offset, |
289 | int inc) | |
1da177e4 LT |
290 | { |
291 | short_ad *sa; | |
292 | ||
cb00ea35 | 293 | if ((!ptr) || (!offset)) { |
1da177e4 LT |
294 | printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n"); |
295 | return NULL; | |
296 | } | |
297 | ||
cb00ea35 | 298 | if ((*offset < 0) || ((*offset + sizeof(short_ad)) > maxoffset)) |
1da177e4 | 299 | return NULL; |
cb00ea35 | 300 | else if ((sa = (short_ad *) ptr)->extLength == 0) |
1da177e4 LT |
301 | return NULL; |
302 | ||
303 | if (inc) | |
304 | *offset += sizeof(short_ad); | |
305 | return sa; | |
306 | } | |
307 | ||
cb00ea35 | 308 | long_ad *udf_get_filelongad(uint8_t * ptr, int maxoffset, int *offset, int inc) |
1da177e4 LT |
309 | { |
310 | long_ad *la; | |
311 | ||
cb00ea35 | 312 | if ((!ptr) || (!offset)) { |
1da177e4 LT |
313 | printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n"); |
314 | return NULL; | |
315 | } | |
316 | ||
cb00ea35 | 317 | if ((*offset < 0) || ((*offset + sizeof(long_ad)) > maxoffset)) |
1da177e4 | 318 | return NULL; |
cb00ea35 | 319 | else if ((la = (long_ad *) ptr)->extLength == 0) |
1da177e4 LT |
320 | return NULL; |
321 | ||
322 | if (inc) | |
323 | *offset += sizeof(long_ad); | |
324 | return la; | |
325 | } |