udf_subr.c revision 1.4.2.3 1 1.4.2.3 yamt /* $NetBSD: udf_subr.c,v 1.4.2.3 2006/03/01 09:34:56 yamt Exp $ */
2 1.4.2.2 yamt
3 1.4.2.2 yamt /*
4 1.4.2.2 yamt * Copyright (c) 2006 Reinoud Zandijk
5 1.4.2.2 yamt * All rights reserved.
6 1.4.2.2 yamt *
7 1.4.2.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.4.2.2 yamt * modification, are permitted provided that the following conditions
9 1.4.2.2 yamt * are met:
10 1.4.2.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.4.2.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.4.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.2.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.4.2.2 yamt * documentation and/or other materials provided with the distribution.
15 1.4.2.2 yamt * 3. All advertising materials mentioning features or use of this software
16 1.4.2.2 yamt * must display the following acknowledgement:
17 1.4.2.2 yamt * This product includes software developed for the
18 1.4.2.2 yamt * NetBSD Project. See http://www.NetBSD.org/ for
19 1.4.2.2 yamt * information about NetBSD.
20 1.4.2.2 yamt * 4. The name of the author may not be used to endorse or promote products
21 1.4.2.2 yamt * derived from this software without specific prior written permission.
22 1.4.2.2 yamt *
23 1.4.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.4.2.2 yamt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.4.2.2 yamt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.4.2.2 yamt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.4.2.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.4.2.2 yamt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.4.2.2 yamt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.4.2.2 yamt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.4.2.2 yamt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.4.2.2 yamt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.4.2.2 yamt *
34 1.4.2.2 yamt */
35 1.4.2.2 yamt
36 1.4.2.2 yamt
37 1.4.2.2 yamt #include <sys/cdefs.h>
38 1.4.2.2 yamt #ifndef lint
39 1.4.2.3 yamt __RCSID("$NetBSD: udf_subr.c,v 1.4.2.3 2006/03/01 09:34:56 yamt Exp $");
40 1.4.2.2 yamt #endif /* not lint */
41 1.4.2.2 yamt
42 1.4.2.2 yamt
43 1.4.2.2 yamt #if defined(_KERNEL_OPT)
44 1.4.2.2 yamt #include "opt_quota.h"
45 1.4.2.2 yamt #include "opt_compat_netbsd.h"
46 1.4.2.2 yamt #endif
47 1.4.2.2 yamt
48 1.4.2.2 yamt #include <sys/param.h>
49 1.4.2.2 yamt #include <sys/systm.h>
50 1.4.2.2 yamt #include <sys/sysctl.h>
51 1.4.2.2 yamt #include <sys/namei.h>
52 1.4.2.2 yamt #include <sys/proc.h>
53 1.4.2.2 yamt #include <sys/kernel.h>
54 1.4.2.2 yamt #include <sys/vnode.h>
55 1.4.2.2 yamt #include <miscfs/genfs/genfs_node.h>
56 1.4.2.2 yamt #include <sys/mount.h>
57 1.4.2.2 yamt #include <sys/buf.h>
58 1.4.2.2 yamt #include <sys/file.h>
59 1.4.2.2 yamt #include <sys/device.h>
60 1.4.2.2 yamt #include <sys/disklabel.h>
61 1.4.2.2 yamt #include <sys/ioctl.h>
62 1.4.2.2 yamt #include <sys/malloc.h>
63 1.4.2.2 yamt #include <sys/dirent.h>
64 1.4.2.2 yamt #include <sys/stat.h>
65 1.4.2.2 yamt #include <sys/conf.h>
66 1.4.2.2 yamt
67 1.4.2.2 yamt #include <fs/udf/ecma167-udf.h>
68 1.4.2.2 yamt #include <fs/udf/udf_mount.h>
69 1.4.2.2 yamt
70 1.4.2.2 yamt #include "udf.h"
71 1.4.2.2 yamt #include "udf_subr.h"
72 1.4.2.2 yamt #include "udf_bswap.h"
73 1.4.2.2 yamt
74 1.4.2.2 yamt
75 1.4.2.2 yamt #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
76 1.4.2.2 yamt
77 1.4.2.2 yamt
78 1.4.2.2 yamt /* predefines */
79 1.4.2.2 yamt
80 1.4.2.2 yamt
81 1.4.2.2 yamt #if 0
82 1.4.2.2 yamt {
83 1.4.2.2 yamt int i, j, dlen;
84 1.4.2.2 yamt uint8_t *blob;
85 1.4.2.2 yamt
86 1.4.2.2 yamt blob = (uint8_t *) fid;
87 1.4.2.2 yamt dlen = file_size - (*offset);
88 1.4.2.2 yamt
89 1.4.2.2 yamt printf("blob = %p\n", blob);
90 1.4.2.2 yamt printf("dump of %d bytes\n", dlen);
91 1.4.2.2 yamt
92 1.4.2.2 yamt for (i = 0; i < dlen; i+ = 16) {
93 1.4.2.2 yamt printf("%04x ", i);
94 1.4.2.2 yamt for (j = 0; j < 16; j++) {
95 1.4.2.2 yamt if (i+j < dlen) {
96 1.4.2.2 yamt printf("%02x ", blob[i+j]);
97 1.4.2.2 yamt } else {
98 1.4.2.2 yamt printf(" ");
99 1.4.2.2 yamt };
100 1.4.2.2 yamt };
101 1.4.2.2 yamt for (j = 0; j < 16; j++) {
102 1.4.2.2 yamt if (i+j < dlen) {
103 1.4.2.2 yamt if (blob[i+j]>32 && blob[i+j]! = 127) {
104 1.4.2.2 yamt printf("%c", blob[i+j]);
105 1.4.2.2 yamt } else {
106 1.4.2.2 yamt printf(".");
107 1.4.2.2 yamt };
108 1.4.2.2 yamt };
109 1.4.2.2 yamt };
110 1.4.2.2 yamt printf("\n");
111 1.4.2.2 yamt };
112 1.4.2.2 yamt printf("\n");
113 1.4.2.2 yamt };
114 1.4.2.2 yamt Debugger();
115 1.4.2.2 yamt #endif
116 1.4.2.2 yamt
117 1.4.2.2 yamt
118 1.4.2.2 yamt /* --------------------------------------------------------------------- */
119 1.4.2.2 yamt
120 1.4.2.2 yamt /* STUB */
121 1.4.2.2 yamt
122 1.4.2.2 yamt static int
123 1.4.2.2 yamt udf_bread(struct udf_mount *ump, uint32_t sector, struct buf **bpp)
124 1.4.2.2 yamt {
125 1.4.2.2 yamt int sector_size = ump->discinfo.sector_size;
126 1.4.2.2 yamt int blks = sector_size / DEV_BSIZE;
127 1.4.2.2 yamt
128 1.4.2.2 yamt /* NOTE bread() checks if block is in cache or not */
129 1.4.2.2 yamt return bread(ump->devvp, sector*blks, sector_size, NOCRED, bpp);
130 1.4.2.2 yamt }
131 1.4.2.2 yamt
132 1.4.2.2 yamt
133 1.4.2.2 yamt /* --------------------------------------------------------------------- */
134 1.4.2.2 yamt
135 1.4.2.2 yamt /*
136 1.4.2.2 yamt * Check if the blob starts with a good UDF tag. Tags are protected by a
137 1.4.2.2 yamt * checksum over the reader except one byte at position 4 that is the checksum
138 1.4.2.2 yamt * itself.
139 1.4.2.2 yamt */
140 1.4.2.2 yamt
141 1.4.2.2 yamt int
142 1.4.2.2 yamt udf_check_tag(void *blob)
143 1.4.2.2 yamt {
144 1.4.2.2 yamt struct desc_tag *tag = blob;
145 1.4.2.2 yamt uint8_t *pos, sum, cnt;
146 1.4.2.2 yamt
147 1.4.2.2 yamt /* check TAG header checksum */
148 1.4.2.2 yamt pos = (uint8_t *) tag;
149 1.4.2.2 yamt sum = 0;
150 1.4.2.2 yamt
151 1.4.2.2 yamt for(cnt = 0; cnt < 16; cnt++) {
152 1.4.2.2 yamt if (cnt != 4)
153 1.4.2.2 yamt sum += *pos;
154 1.4.2.2 yamt pos++;
155 1.4.2.2 yamt }
156 1.4.2.2 yamt if (sum != tag->cksum) {
157 1.4.2.2 yamt /* bad tag header checksum; this is not a valid tag */
158 1.4.2.2 yamt return EINVAL;
159 1.4.2.2 yamt }
160 1.4.2.2 yamt
161 1.4.2.2 yamt return 0;
162 1.4.2.2 yamt }
163 1.4.2.2 yamt
164 1.4.2.2 yamt /* --------------------------------------------------------------------- */
165 1.4.2.2 yamt
166 1.4.2.2 yamt /*
167 1.4.2.2 yamt * check tag payload will check descriptor CRC as specified.
168 1.4.2.2 yamt * If the descriptor is too short, it will return EIO otherwise EINVAL.
169 1.4.2.2 yamt */
170 1.4.2.2 yamt
171 1.4.2.2 yamt int
172 1.4.2.2 yamt udf_check_tag_payload(void *blob, uint32_t max_length)
173 1.4.2.2 yamt {
174 1.4.2.2 yamt struct desc_tag *tag = blob;
175 1.4.2.2 yamt uint16_t crc, crc_len;
176 1.4.2.2 yamt
177 1.4.2.2 yamt crc_len = udf_rw16(tag->desc_crc_len);
178 1.4.2.2 yamt
179 1.4.2.2 yamt /* check payload CRC if applicable */
180 1.4.2.2 yamt if (crc_len == 0)
181 1.4.2.2 yamt return 0;
182 1.4.2.2 yamt
183 1.4.2.2 yamt if (crc_len > max_length)
184 1.4.2.2 yamt return EIO;
185 1.4.2.2 yamt
186 1.4.2.2 yamt crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len);
187 1.4.2.2 yamt if (crc != udf_rw16(tag->desc_crc)) {
188 1.4.2.2 yamt /* bad payload CRC; this is a broken tag */
189 1.4.2.2 yamt return EINVAL;
190 1.4.2.2 yamt };
191 1.4.2.2 yamt
192 1.4.2.2 yamt return 0;
193 1.4.2.2 yamt }
194 1.4.2.2 yamt
195 1.4.2.2 yamt /* --------------------------------------------------------------------- */
196 1.4.2.2 yamt
197 1.4.2.2 yamt int
198 1.4.2.2 yamt udf_validate_tag_sum(void *blob)
199 1.4.2.2 yamt {
200 1.4.2.2 yamt struct desc_tag *tag = blob;
201 1.4.2.2 yamt uint8_t *pos, sum, cnt;
202 1.4.2.2 yamt
203 1.4.2.2 yamt /* calculate TAG header checksum */
204 1.4.2.2 yamt pos = (uint8_t *) tag;
205 1.4.2.2 yamt sum = 0;
206 1.4.2.2 yamt
207 1.4.2.2 yamt for(cnt = 0; cnt < 16; cnt++) {
208 1.4.2.2 yamt if (cnt != 4) sum += *pos;
209 1.4.2.2 yamt pos++;
210 1.4.2.2 yamt };
211 1.4.2.2 yamt tag->cksum = sum; /* 8 bit */
212 1.4.2.2 yamt
213 1.4.2.2 yamt return 0;
214 1.4.2.2 yamt }
215 1.4.2.2 yamt
216 1.4.2.2 yamt /* --------------------------------------------------------------------- */
217 1.4.2.2 yamt
218 1.4.2.2 yamt /* assumes sector number of descriptor to be saved allready present */
219 1.4.2.2 yamt
220 1.4.2.2 yamt int
221 1.4.2.2 yamt udf_validate_tag_and_crc_sums(void *blob)
222 1.4.2.2 yamt {
223 1.4.2.2 yamt struct desc_tag *tag = blob;
224 1.4.2.2 yamt uint8_t *btag = (uint8_t *) tag;
225 1.4.2.2 yamt uint16_t crc, crc_len;
226 1.4.2.2 yamt
227 1.4.2.2 yamt crc_len = udf_rw16(tag->desc_crc_len);
228 1.4.2.2 yamt
229 1.4.2.2 yamt /* check payload CRC if applicable */
230 1.4.2.2 yamt if (crc_len > 0) {
231 1.4.2.2 yamt crc = udf_cksum(btag + UDF_DESC_TAG_LENGTH, crc_len);
232 1.4.2.2 yamt tag->desc_crc = udf_rw16(crc);
233 1.4.2.2 yamt };
234 1.4.2.2 yamt
235 1.4.2.2 yamt /* calculate TAG header checksum */
236 1.4.2.2 yamt return udf_validate_tag_sum(blob);
237 1.4.2.2 yamt }
238 1.4.2.2 yamt
239 1.4.2.2 yamt /* --------------------------------------------------------------------- */
240 1.4.2.2 yamt
241 1.4.2.2 yamt /*
242 1.4.2.2 yamt * XXX note the different semantics from udfclient: for FIDs it still rounds
243 1.4.2.2 yamt * up to sectors. Use udf_fidsize() for a correct length.
244 1.4.2.2 yamt */
245 1.4.2.2 yamt
246 1.4.2.2 yamt int
247 1.4.2.2 yamt udf_tagsize(union dscrptr *dscr, uint32_t udf_sector_size)
248 1.4.2.2 yamt {
249 1.4.2.2 yamt uint32_t size, tag_id, num_secs, elmsz;
250 1.4.2.2 yamt
251 1.4.2.2 yamt tag_id = udf_rw16(dscr->tag.id);
252 1.4.2.2 yamt
253 1.4.2.2 yamt switch (tag_id) {
254 1.4.2.2 yamt case TAGID_LOGVOL :
255 1.4.2.2 yamt size = sizeof(struct logvol_desc) - 1;
256 1.4.2.2 yamt size += udf_rw32(dscr->lvd.mt_l);
257 1.4.2.2 yamt break;
258 1.4.2.2 yamt case TAGID_UNALLOC_SPACE :
259 1.4.2.2 yamt elmsz = sizeof(struct extent_ad);
260 1.4.2.2 yamt size = sizeof(struct unalloc_sp_desc) - elmsz;
261 1.4.2.2 yamt size += udf_rw32(dscr->usd.alloc_desc_num) * elmsz;
262 1.4.2.2 yamt break;
263 1.4.2.2 yamt case TAGID_FID :
264 1.4.2.2 yamt size = UDF_FID_SIZE + dscr->fid.l_fi + udf_rw16(dscr->fid.l_iu);
265 1.4.2.2 yamt size = (size + 3) & ~3;
266 1.4.2.2 yamt break;
267 1.4.2.2 yamt case TAGID_LOGVOL_INTEGRITY :
268 1.4.2.2 yamt size = sizeof(struct logvol_int_desc) - sizeof(uint32_t);
269 1.4.2.2 yamt size += udf_rw32(dscr->lvid.l_iu);
270 1.4.2.2 yamt size += (2 * udf_rw32(dscr->lvid.num_part) * sizeof(uint32_t));
271 1.4.2.2 yamt break;
272 1.4.2.2 yamt case TAGID_SPACE_BITMAP :
273 1.4.2.2 yamt size = sizeof(struct space_bitmap_desc) - 1;
274 1.4.2.2 yamt size += udf_rw32(dscr->sbd.num_bytes);
275 1.4.2.2 yamt break;
276 1.4.2.2 yamt case TAGID_SPARING_TABLE :
277 1.4.2.2 yamt elmsz = sizeof(struct spare_map_entry);
278 1.4.2.2 yamt size = sizeof(struct udf_sparing_table) - elmsz;
279 1.4.2.2 yamt size += udf_rw16(dscr->spt.rt_l) * elmsz;
280 1.4.2.2 yamt break;
281 1.4.2.2 yamt case TAGID_FENTRY :
282 1.4.2.2 yamt size = sizeof(struct file_entry);
283 1.4.2.2 yamt size += udf_rw32(dscr->fe.l_ea) + udf_rw32(dscr->fe.l_ad)-1;
284 1.4.2.2 yamt break;
285 1.4.2.2 yamt case TAGID_EXTFENTRY :
286 1.4.2.2 yamt size = sizeof(struct extfile_entry);
287 1.4.2.2 yamt size += udf_rw32(dscr->efe.l_ea) + udf_rw32(dscr->efe.l_ad)-1;
288 1.4.2.2 yamt break;
289 1.4.2.2 yamt case TAGID_FSD :
290 1.4.2.2 yamt size = sizeof(struct fileset_desc);
291 1.4.2.2 yamt break;
292 1.4.2.2 yamt default :
293 1.4.2.2 yamt size = sizeof(union dscrptr);
294 1.4.2.2 yamt break;
295 1.4.2.2 yamt };
296 1.4.2.2 yamt
297 1.4.2.2 yamt if ((size == 0) || (udf_sector_size == 0)) return 0;
298 1.4.2.2 yamt
299 1.4.2.2 yamt /* round up in sectors */
300 1.4.2.2 yamt num_secs = (size + udf_sector_size -1) / udf_sector_size;
301 1.4.2.2 yamt return num_secs * udf_sector_size;
302 1.4.2.2 yamt }
303 1.4.2.2 yamt
304 1.4.2.2 yamt
305 1.4.2.2 yamt static int
306 1.4.2.2 yamt udf_fidsize(struct fileid_desc *fid, uint32_t udf_sector_size)
307 1.4.2.2 yamt {
308 1.4.2.2 yamt uint32_t size;
309 1.4.2.2 yamt
310 1.4.2.2 yamt if (udf_rw16(fid->tag.id) != TAGID_FID)
311 1.4.2.2 yamt panic("got udf_fidsize on non FID\n");
312 1.4.2.2 yamt
313 1.4.2.2 yamt size = UDF_FID_SIZE + fid->l_fi + udf_rw16(fid->l_iu);
314 1.4.2.2 yamt size = (size + 3) & ~3;
315 1.4.2.2 yamt
316 1.4.2.2 yamt return size;
317 1.4.2.2 yamt }
318 1.4.2.2 yamt
319 1.4.2.2 yamt /* --------------------------------------------------------------------- */
320 1.4.2.2 yamt
321 1.4.2.2 yamt /*
322 1.4.2.2 yamt * Problem with read_descriptor are long descriptors spanning more than one
323 1.4.2.2 yamt * sector. Luckily long descriptors can't be in `logical space'.
324 1.4.2.2 yamt *
325 1.4.2.2 yamt * Size of allocated piece is returned in multiple of sector size due to
326 1.4.2.2 yamt * udf_calc_udf_malloc_size().
327 1.4.2.2 yamt */
328 1.4.2.2 yamt
329 1.4.2.2 yamt int
330 1.4.2.2 yamt udf_read_descriptor(struct udf_mount *ump, uint32_t sector,
331 1.4.2.2 yamt struct malloc_type *mtype, union dscrptr **dstp)
332 1.4.2.2 yamt {
333 1.4.2.2 yamt union dscrptr *src, *dst;
334 1.4.2.2 yamt struct buf *bp;
335 1.4.2.2 yamt uint8_t *pos;
336 1.4.2.2 yamt int blks, blk, dscrlen;
337 1.4.2.2 yamt int i, error, sector_size;
338 1.4.2.2 yamt
339 1.4.2.2 yamt sector_size = ump->discinfo.sector_size;
340 1.4.2.2 yamt
341 1.4.2.2 yamt *dstp = dst = NULL;
342 1.4.2.2 yamt dscrlen = sector_size;
343 1.4.2.2 yamt
344 1.4.2.2 yamt /* read initial piece */
345 1.4.2.2 yamt error = udf_bread(ump, sector, &bp);
346 1.4.2.2 yamt DPRINTFIF(DESCRIPTOR, error, ("read error (%d)\n", error));
347 1.4.2.2 yamt
348 1.4.2.2 yamt if (!error) {
349 1.4.2.2 yamt /* check if its a valid tag */
350 1.4.2.2 yamt error = udf_check_tag(bp->b_data);
351 1.4.2.2 yamt if (error) {
352 1.4.2.2 yamt /* check if its an empty block */
353 1.4.2.2 yamt pos = bp->b_data;
354 1.4.2.2 yamt for (i = 0; i < sector_size; i++, pos++) {
355 1.4.2.2 yamt if (*pos) break;
356 1.4.2.2 yamt };
357 1.4.2.2 yamt if (i == sector_size) {
358 1.4.2.2 yamt /* return no error but with no dscrptr */
359 1.4.2.2 yamt /* dispose first block */
360 1.4.2.2 yamt brelse(bp);
361 1.4.2.2 yamt return 0;
362 1.4.2.2 yamt };
363 1.4.2.2 yamt };
364 1.4.2.2 yamt };
365 1.4.2.2 yamt DPRINTFIF(DESCRIPTOR, error, ("bad tag checksum\n"));
366 1.4.2.2 yamt if (!error) {
367 1.4.2.2 yamt src = (union dscrptr *) bp->b_data;
368 1.4.2.2 yamt dscrlen = udf_tagsize(src, sector_size);
369 1.4.2.2 yamt dst = malloc(dscrlen, mtype, M_WAITOK);
370 1.4.2.2 yamt memcpy(dst, src, dscrlen);
371 1.4.2.2 yamt };
372 1.4.2.2 yamt /* dispose first block */
373 1.4.2.2 yamt bp->b_flags |= B_AGE;
374 1.4.2.2 yamt brelse(bp);
375 1.4.2.2 yamt
376 1.4.2.2 yamt if (!error && (dscrlen > sector_size)) {
377 1.4.2.2 yamt DPRINTF(DESCRIPTOR, ("multi block descriptor read\n"));
378 1.4.2.2 yamt /*
379 1.4.2.2 yamt * Read the rest of descriptor. Since it is only used at mount
380 1.4.2.2 yamt * time its overdone to define and use a specific udf_breadn
381 1.4.2.2 yamt * for this alone.
382 1.4.2.2 yamt */
383 1.4.2.2 yamt blks = (dscrlen + sector_size -1) / sector_size;
384 1.4.2.2 yamt for (blk = 1; blk < blks; blk++) {
385 1.4.2.2 yamt error = udf_bread(ump, sector + blk, &bp);
386 1.4.2.2 yamt if (error) {
387 1.4.2.2 yamt brelse(bp);
388 1.4.2.2 yamt break;
389 1.4.2.2 yamt };
390 1.4.2.2 yamt pos = (uint8_t *) dst + blk*sector_size;
391 1.4.2.2 yamt memcpy(pos, bp->b_data, sector_size);
392 1.4.2.2 yamt
393 1.4.2.2 yamt /* dispose block */
394 1.4.2.2 yamt bp->b_flags |= B_AGE;
395 1.4.2.2 yamt brelse(bp);
396 1.4.2.2 yamt };
397 1.4.2.2 yamt DPRINTFIF(DESCRIPTOR, error, ("read error on multi (%d)\n",
398 1.4.2.2 yamt error));
399 1.4.2.2 yamt };
400 1.4.2.2 yamt if (!error) {
401 1.4.2.2 yamt error = udf_check_tag_payload(dst, dscrlen);
402 1.4.2.2 yamt DPRINTFIF(DESCRIPTOR, error, ("bad payload check sum\n"));
403 1.4.2.2 yamt };
404 1.4.2.2 yamt if (error && dst) {
405 1.4.2.2 yamt free(dst, mtype);
406 1.4.2.2 yamt dst = NULL;
407 1.4.2.2 yamt };
408 1.4.2.2 yamt *dstp = dst;
409 1.4.2.2 yamt
410 1.4.2.2 yamt return error;
411 1.4.2.2 yamt }
412 1.4.2.2 yamt
413 1.4.2.2 yamt /* --------------------------------------------------------------------- */
414 1.4.2.2 yamt #ifdef DEBUG
415 1.4.2.2 yamt static void
416 1.4.2.2 yamt udf_dump_discinfo(struct udf_mount *ump)
417 1.4.2.2 yamt {
418 1.4.2.2 yamt char bits[128];
419 1.4.2.2 yamt struct mmc_discinfo *di = &ump->discinfo;
420 1.4.2.2 yamt
421 1.4.2.2 yamt if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0)
422 1.4.2.2 yamt return;
423 1.4.2.2 yamt
424 1.4.2.2 yamt printf("Device/media info :\n");
425 1.4.2.2 yamt printf("\tMMC profile 0x%02x\n", di->mmc_profile);
426 1.4.2.2 yamt printf("\tderived class %d\n", di->mmc_class);
427 1.4.2.2 yamt printf("\tsector size %d\n", di->sector_size);
428 1.4.2.2 yamt printf("\tdisc state %d\n", di->disc_state);
429 1.4.2.2 yamt printf("\tlast ses state %d\n", di->last_session_state);
430 1.4.2.2 yamt printf("\tbg format state %d\n", di->bg_format_state);
431 1.4.2.2 yamt printf("\tfrst track %d\n", di->first_track);
432 1.4.2.2 yamt printf("\tfst on last ses %d\n", di->first_track_last_session);
433 1.4.2.2 yamt printf("\tlst on last ses %d\n", di->last_track_last_session);
434 1.4.2.2 yamt printf("\tlink block penalty %d\n", di->link_block_penalty);
435 1.4.2.2 yamt bitmask_snprintf(di->disc_flags, MMC_DFLAGS_FLAGBITS, bits,
436 1.4.2.2 yamt sizeof(bits));
437 1.4.2.2 yamt printf("\tdisc flags %s\n", bits);
438 1.4.2.2 yamt printf("\tdisc id %x\n", di->disc_id);
439 1.4.2.2 yamt printf("\tdisc barcode %"PRIx64"\n", di->disc_barcode);
440 1.4.2.2 yamt
441 1.4.2.2 yamt printf("\tnum sessions %d\n", di->num_sessions);
442 1.4.2.2 yamt printf("\tnum tracks %d\n", di->num_tracks);
443 1.4.2.2 yamt
444 1.4.2.2 yamt bitmask_snprintf(di->mmc_cur, MMC_CAP_FLAGBITS, bits, sizeof(bits));
445 1.4.2.2 yamt printf("\tcapabilities cur %s\n", bits);
446 1.4.2.2 yamt bitmask_snprintf(di->mmc_cap, MMC_CAP_FLAGBITS, bits, sizeof(bits));
447 1.4.2.2 yamt printf("\tcapabilities cap %s\n", bits);
448 1.4.2.2 yamt }
449 1.4.2.2 yamt #else
450 1.4.2.2 yamt #define udf_dump_discinfo(a);
451 1.4.2.2 yamt #endif
452 1.4.2.2 yamt
453 1.4.2.2 yamt /* not called often */
454 1.4.2.2 yamt int
455 1.4.2.2 yamt udf_update_discinfo(struct udf_mount *ump)
456 1.4.2.2 yamt {
457 1.4.2.2 yamt struct vnode *devvp = ump->devvp;
458 1.4.2.2 yamt struct partinfo dpart;
459 1.4.2.2 yamt struct mmc_discinfo *di;
460 1.4.2.2 yamt int error;
461 1.4.2.2 yamt
462 1.4.2.2 yamt DPRINTF(VOLUMES, ("read/update disc info\n"));
463 1.4.2.2 yamt di = &ump->discinfo;
464 1.4.2.2 yamt memset(di, 0, sizeof(struct mmc_discinfo));
465 1.4.2.2 yamt
466 1.4.2.2 yamt /* check if we're on a MMC capable device, i.e. CD/DVD */
467 1.4.2.2 yamt error = VOP_IOCTL(devvp, MMCGETDISCINFO, di, FKIOCTL, NOCRED, NULL);
468 1.4.2.2 yamt if (error == 0) {
469 1.4.2.2 yamt udf_dump_discinfo(ump);
470 1.4.2.2 yamt return 0;
471 1.4.2.2 yamt };
472 1.4.2.2 yamt
473 1.4.2.2 yamt /* disc partition support */
474 1.4.2.2 yamt error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, NULL);
475 1.4.2.2 yamt if (error)
476 1.4.2.2 yamt return ENODEV;
477 1.4.2.2 yamt
478 1.4.2.2 yamt /* set up a disc info profile for partitions */
479 1.4.2.2 yamt di->mmc_profile = 0x01; /* disc type */
480 1.4.2.2 yamt di->mmc_class = MMC_CLASS_DISC;
481 1.4.2.2 yamt di->disc_state = MMC_STATE_CLOSED;
482 1.4.2.2 yamt di->last_session_state = MMC_STATE_CLOSED;
483 1.4.2.2 yamt di->bg_format_state = MMC_BGFSTATE_COMPLETED;
484 1.4.2.2 yamt di->link_block_penalty = 0;
485 1.4.2.2 yamt
486 1.4.2.2 yamt di->mmc_cur = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE |
487 1.4.2.2 yamt MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE;
488 1.4.2.2 yamt di->mmc_cap = di->mmc_cur;
489 1.4.2.2 yamt di->disc_flags = MMC_DFLAGS_UNRESTRICTED;
490 1.4.2.2 yamt
491 1.4.2.2 yamt /* TODO problem with last_possible_lba on resizable VND; request */
492 1.4.2.2 yamt di->last_possible_lba = dpart.part->p_size;
493 1.4.2.2 yamt di->sector_size = dpart.disklab->d_secsize;
494 1.4.2.2 yamt di->blockingnr = 1;
495 1.4.2.2 yamt
496 1.4.2.2 yamt di->num_sessions = 1;
497 1.4.2.2 yamt di->num_tracks = 1;
498 1.4.2.2 yamt
499 1.4.2.2 yamt di->first_track = 1;
500 1.4.2.2 yamt di->first_track_last_session = di->last_track_last_session = 1;
501 1.4.2.2 yamt
502 1.4.2.2 yamt udf_dump_discinfo(ump);
503 1.4.2.2 yamt return 0;
504 1.4.2.2 yamt }
505 1.4.2.2 yamt
506 1.4.2.2 yamt /* --------------------------------------------------------------------- */
507 1.4.2.2 yamt
508 1.4.2.2 yamt int
509 1.4.2.2 yamt udf_update_trackinfo(struct udf_mount *ump, struct mmc_trackinfo *ti)
510 1.4.2.2 yamt {
511 1.4.2.2 yamt struct vnode *devvp = ump->devvp;
512 1.4.2.2 yamt struct mmc_discinfo *di = &ump->discinfo;
513 1.4.2.2 yamt int error, class;
514 1.4.2.2 yamt
515 1.4.2.2 yamt DPRINTF(VOLUMES, ("read track info\n"));
516 1.4.2.2 yamt
517 1.4.2.2 yamt class = di->mmc_class;
518 1.4.2.2 yamt if (class != MMC_CLASS_DISC) {
519 1.4.2.2 yamt /* tracknr specified in struct ti */
520 1.4.2.2 yamt error = VOP_IOCTL(devvp, MMCGETTRACKINFO, ti, FKIOCTL,
521 1.4.2.2 yamt NOCRED, NULL);
522 1.4.2.2 yamt return error;
523 1.4.2.2 yamt };
524 1.4.2.2 yamt
525 1.4.2.2 yamt /* disc partition support */
526 1.4.2.2 yamt if (ti->tracknr != 1)
527 1.4.2.2 yamt return EIO;
528 1.4.2.2 yamt
529 1.4.2.2 yamt /* create fake ti (TODO check for resized vnds) */
530 1.4.2.2 yamt ti->sessionnr = 1;
531 1.4.2.2 yamt
532 1.4.2.2 yamt ti->track_mode = 0; /* XXX */
533 1.4.2.2 yamt ti->data_mode = 0; /* XXX */
534 1.4.2.2 yamt ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID;
535 1.4.2.2 yamt
536 1.4.2.2 yamt ti->track_start = 0;
537 1.4.2.2 yamt ti->packet_size = 1;
538 1.4.2.2 yamt
539 1.4.2.2 yamt /* TODO support for resizable vnd */
540 1.4.2.2 yamt ti->track_size = di->last_possible_lba;
541 1.4.2.2 yamt ti->next_writable = di->last_possible_lba;
542 1.4.2.2 yamt ti->last_recorded = ti->next_writable;
543 1.4.2.2 yamt ti->free_blocks = 0;
544 1.4.2.2 yamt
545 1.4.2.2 yamt return 0;
546 1.4.2.2 yamt }
547 1.4.2.2 yamt
548 1.4.2.2 yamt /* --------------------------------------------------------------------- */
549 1.4.2.2 yamt
550 1.4.2.2 yamt /* track/session searching for mounting */
551 1.4.2.2 yamt
552 1.4.2.2 yamt static int
553 1.4.2.2 yamt udf_search_tracks(struct udf_mount *ump, struct udf_args *args,
554 1.4.2.2 yamt int *first_tracknr, int *last_tracknr)
555 1.4.2.2 yamt {
556 1.4.2.2 yamt struct mmc_trackinfo trackinfo;
557 1.4.2.2 yamt uint32_t tracknr, start_track, num_tracks;
558 1.4.2.2 yamt int error;
559 1.4.2.2 yamt
560 1.4.2.2 yamt /* if negative, sessionnr is relative to last session */
561 1.4.2.2 yamt if (args->sessionnr < 0) {
562 1.4.2.2 yamt args->sessionnr += ump->discinfo.num_sessions;
563 1.4.2.2 yamt /* sanity */
564 1.4.2.2 yamt if (args->sessionnr < 0)
565 1.4.2.2 yamt args->sessionnr = 0;
566 1.4.2.2 yamt };
567 1.4.2.2 yamt
568 1.4.2.2 yamt /* sanity */
569 1.4.2.2 yamt if (args->sessionnr > ump->discinfo.num_sessions)
570 1.4.2.2 yamt args->sessionnr = ump->discinfo.num_sessions;
571 1.4.2.2 yamt
572 1.4.2.2 yamt /* search the tracks for this session, zero session nr indicates last */
573 1.4.2.2 yamt if (args->sessionnr == 0) {
574 1.4.2.2 yamt args->sessionnr = ump->discinfo.num_sessions;
575 1.4.2.2 yamt if (ump->discinfo.last_session_state == MMC_STATE_EMPTY) {
576 1.4.2.2 yamt args->sessionnr--;
577 1.4.2.2 yamt }
578 1.4.2.2 yamt };
579 1.4.2.2 yamt
580 1.4.2.2 yamt /* search the first and last track of the specified session */
581 1.4.2.2 yamt num_tracks = ump->discinfo.num_tracks;
582 1.4.2.2 yamt start_track = ump->discinfo.first_track;
583 1.4.2.2 yamt
584 1.4.2.2 yamt /* search for first track of this session */
585 1.4.2.2 yamt for (tracknr = start_track; tracknr <= num_tracks; tracknr++) {
586 1.4.2.2 yamt /* get track info */
587 1.4.2.2 yamt trackinfo.tracknr = tracknr;
588 1.4.2.2 yamt error = udf_update_trackinfo(ump, &trackinfo);
589 1.4.2.2 yamt if (error)
590 1.4.2.2 yamt return error;
591 1.4.2.2 yamt
592 1.4.2.2 yamt if (trackinfo.sessionnr == args->sessionnr)
593 1.4.2.2 yamt break;
594 1.4.2.2 yamt }
595 1.4.2.2 yamt *first_tracknr = tracknr;
596 1.4.2.2 yamt
597 1.4.2.2 yamt /* search for last track of this session */
598 1.4.2.2 yamt for (;tracknr <= num_tracks; tracknr++) {
599 1.4.2.2 yamt /* get track info */
600 1.4.2.2 yamt trackinfo.tracknr = tracknr;
601 1.4.2.2 yamt error = udf_update_trackinfo(ump, &trackinfo);
602 1.4.2.2 yamt if (error || (trackinfo.sessionnr != args->sessionnr)) {
603 1.4.2.2 yamt tracknr--;
604 1.4.2.2 yamt break;
605 1.4.2.2 yamt };
606 1.4.2.2 yamt };
607 1.4.2.2 yamt if (tracknr > num_tracks)
608 1.4.2.2 yamt tracknr--;
609 1.4.2.2 yamt
610 1.4.2.2 yamt *last_tracknr = tracknr;
611 1.4.2.2 yamt
612 1.4.2.2 yamt assert(*last_tracknr >= *first_tracknr);
613 1.4.2.2 yamt return 0;
614 1.4.2.2 yamt }
615 1.4.2.2 yamt
616 1.4.2.2 yamt /* --------------------------------------------------------------------- */
617 1.4.2.2 yamt
618 1.4.2.2 yamt static int
619 1.4.2.2 yamt udf_read_anchor(struct udf_mount *ump, uint32_t sector, struct anchor_vdp **dst)
620 1.4.2.2 yamt {
621 1.4.2.2 yamt int error;
622 1.4.2.2 yamt
623 1.4.2.2 yamt error = udf_read_descriptor(ump, sector, M_UDFVOLD,
624 1.4.2.2 yamt (union dscrptr **) dst);
625 1.4.2.2 yamt if (!error) {
626 1.4.2.2 yamt /* blank terminator blocks are not allowed here */
627 1.4.2.2 yamt if (*dst == NULL)
628 1.4.2.2 yamt return ENOENT;
629 1.4.2.2 yamt if (udf_rw16((*dst)->tag.id) != TAGID_ANCHOR) {
630 1.4.2.2 yamt error = ENOENT;
631 1.4.2.2 yamt free(*dst, M_UDFVOLD);
632 1.4.2.2 yamt *dst = NULL;
633 1.4.2.2 yamt DPRINTF(VOLUMES, ("Not an anchor\n"));
634 1.4.2.2 yamt };
635 1.4.2.2 yamt };
636 1.4.2.2 yamt
637 1.4.2.2 yamt return error;
638 1.4.2.2 yamt }
639 1.4.2.2 yamt
640 1.4.2.2 yamt
641 1.4.2.2 yamt int
642 1.4.2.2 yamt udf_read_anchors(struct udf_mount *ump, struct udf_args *args)
643 1.4.2.2 yamt {
644 1.4.2.2 yamt struct mmc_trackinfo first_track;
645 1.4.2.2 yamt struct mmc_trackinfo last_track;
646 1.4.2.2 yamt struct anchor_vdp **anchorsp;
647 1.4.2.2 yamt uint32_t track_start;
648 1.4.2.2 yamt uint32_t track_end;
649 1.4.2.2 yamt uint32_t positions[4];
650 1.4.2.2 yamt int first_tracknr, last_tracknr;
651 1.4.2.2 yamt int error, anch, ok, first_anchor;
652 1.4.2.2 yamt
653 1.4.2.2 yamt /* search the first and last track of the specified session */
654 1.4.2.2 yamt error = udf_search_tracks(ump, args, &first_tracknr, &last_tracknr);
655 1.4.2.2 yamt if (!error) {
656 1.4.2.2 yamt first_track.tracknr = first_tracknr;
657 1.4.2.2 yamt error = udf_update_trackinfo(ump, &first_track);
658 1.4.2.2 yamt };
659 1.4.2.2 yamt if (!error) {
660 1.4.2.2 yamt last_track.tracknr = last_tracknr;
661 1.4.2.2 yamt error = udf_update_trackinfo(ump, &last_track);
662 1.4.2.2 yamt };
663 1.4.2.2 yamt if (error) {
664 1.4.2.2 yamt printf("UDF mount: reading disc geometry failed\n");
665 1.4.2.2 yamt return 0;
666 1.4.2.2 yamt };
667 1.4.2.2 yamt
668 1.4.2.2 yamt track_start = first_track.track_start;
669 1.4.2.2 yamt
670 1.4.2.2 yamt /* `end' is not as straitforward as start. */
671 1.4.2.2 yamt track_end = last_track.track_start
672 1.4.2.2 yamt + last_track.track_size - last_track.free_blocks - 1;
673 1.4.2.2 yamt
674 1.4.2.2 yamt if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
675 1.4.2.2 yamt /* end of track is not straitforward here */
676 1.4.2.2 yamt if (last_track.flags & MMC_TRACKINFO_LRA_VALID)
677 1.4.2.2 yamt track_end = last_track.last_recorded;
678 1.4.2.2 yamt else if (last_track.flags & MMC_TRACKINFO_NWA_VALID)
679 1.4.2.2 yamt track_end = last_track.next_writable
680 1.4.2.2 yamt - ump->discinfo.link_block_penalty;
681 1.4.2.2 yamt };
682 1.4.2.2 yamt /* VATs are only recorded on sequential media, but initialise */
683 1.4.2.2 yamt ump->possible_vat_location = track_end;
684 1.4.2.2 yamt
685 1.4.2.2 yamt /* its no use reading a blank track */
686 1.4.2.2 yamt first_anchor = 0;
687 1.4.2.2 yamt if (first_track.flags & MMC_TRACKINFO_BLANK)
688 1.4.2.2 yamt first_anchor = 1;
689 1.4.2.2 yamt
690 1.4.2.2 yamt /* read anchors start+256, start+512, end-256, end */
691 1.4.2.2 yamt positions[0] = track_start+256;
692 1.4.2.2 yamt positions[1] = track_end-256;
693 1.4.2.2 yamt positions[2] = track_end;
694 1.4.2.2 yamt positions[3] = track_start+512; /* [UDF 2.60/6.11.2] */
695 1.4.2.2 yamt /* XXX shouldn't +512 be prefered above +256 for compat with Roxio CD */
696 1.4.2.2 yamt
697 1.4.2.2 yamt ok = 0;
698 1.4.2.2 yamt anchorsp = ump->anchors;
699 1.4.2.2 yamt for (anch = first_anchor; anch < 4; anch++) {
700 1.4.2.2 yamt DPRINTF(VOLUMES, ("Read anchor %d at sector %d\n", anch,
701 1.4.2.2 yamt positions[anch]));
702 1.4.2.2 yamt error = udf_read_anchor(ump, positions[anch], anchorsp);
703 1.4.2.2 yamt if (!error) {
704 1.4.2.2 yamt anchorsp++;
705 1.4.2.2 yamt ok++;
706 1.4.2.2 yamt };
707 1.4.2.2 yamt };
708 1.4.2.2 yamt
709 1.4.2.2 yamt return ok;
710 1.4.2.2 yamt }
711 1.4.2.2 yamt
712 1.4.2.2 yamt /* --------------------------------------------------------------------- */
713 1.4.2.2 yamt
714 1.4.2.2 yamt /* we dont try to be smart; we just record the parts */
715 1.4.2.2 yamt #define UDF_UPDATE_DSCR(name, dscr) \
716 1.4.2.2 yamt if (name) \
717 1.4.2.2 yamt free(name, M_UDFVOLD); \
718 1.4.2.2 yamt name = dscr;
719 1.4.2.2 yamt
720 1.4.2.2 yamt static int
721 1.4.2.2 yamt udf_process_vds_descriptor(struct udf_mount *ump, union dscrptr *dscr)
722 1.4.2.2 yamt {
723 1.4.2.2 yamt uint16_t partnr;
724 1.4.2.2 yamt
725 1.4.2.2 yamt DPRINTF(VOLUMES, ("\tprocessing VDS descr %d\n",
726 1.4.2.2 yamt udf_rw16(dscr->tag.id)));
727 1.4.2.2 yamt switch (udf_rw16(dscr->tag.id)) {
728 1.4.2.2 yamt case TAGID_PRI_VOL : /* primary partition */
729 1.4.2.2 yamt UDF_UPDATE_DSCR(ump->primary_vol, &dscr->pvd);
730 1.4.2.2 yamt break;
731 1.4.2.2 yamt case TAGID_LOGVOL : /* logical volume */
732 1.4.2.2 yamt UDF_UPDATE_DSCR(ump->logical_vol, &dscr->lvd);
733 1.4.2.2 yamt break;
734 1.4.2.2 yamt case TAGID_UNALLOC_SPACE : /* unallocated space */
735 1.4.2.2 yamt UDF_UPDATE_DSCR(ump->unallocated, &dscr->usd);
736 1.4.2.2 yamt break;
737 1.4.2.2 yamt case TAGID_IMP_VOL : /* implementation */
738 1.4.2.2 yamt /* XXX do we care about multiple impl. descr ? */
739 1.4.2.2 yamt UDF_UPDATE_DSCR(ump->implementation, &dscr->ivd);
740 1.4.2.2 yamt break;
741 1.4.2.2 yamt case TAGID_PARTITION : /* physical partition */
742 1.4.2.2 yamt /* not much use if its not allocated */
743 1.4.2.2 yamt if ((udf_rw16(dscr->pd.flags) & UDF_PART_FLAG_ALLOCATED) == 0) {
744 1.4.2.2 yamt free(dscr, M_UDFVOLD);
745 1.4.2.2 yamt break;
746 1.4.2.2 yamt };
747 1.4.2.2 yamt
748 1.4.2.2 yamt /* check partnr boundaries */
749 1.4.2.2 yamt partnr = udf_rw16(dscr->pd.part_num);
750 1.4.2.2 yamt if (partnr >= UDF_PARTITIONS)
751 1.4.2.2 yamt return EINVAL;
752 1.4.2.2 yamt
753 1.4.2.2 yamt UDF_UPDATE_DSCR(ump->partitions[partnr], &dscr->pd);
754 1.4.2.2 yamt break;
755 1.4.2.2 yamt case TAGID_VOL : /* volume space extender; rare */
756 1.4.2.2 yamt DPRINTF(VOLUMES, ("VDS extender ignored\n"));
757 1.4.2.2 yamt free(dscr, M_UDFVOLD);
758 1.4.2.2 yamt break;
759 1.4.2.2 yamt default :
760 1.4.2.2 yamt DPRINTF(VOLUMES, ("Unhandled VDS type %d\n",
761 1.4.2.2 yamt udf_rw16(dscr->tag.id)));
762 1.4.2.2 yamt free(dscr, M_UDFVOLD);
763 1.4.2.2 yamt };
764 1.4.2.2 yamt
765 1.4.2.2 yamt return 0;
766 1.4.2.2 yamt }
767 1.4.2.2 yamt #undef UDF_UPDATE_DSCR
768 1.4.2.2 yamt
769 1.4.2.2 yamt /* --------------------------------------------------------------------- */
770 1.4.2.2 yamt
771 1.4.2.2 yamt static int
772 1.4.2.2 yamt udf_read_vds_extent(struct udf_mount *ump, uint32_t loc, uint32_t len)
773 1.4.2.2 yamt {
774 1.4.2.2 yamt union dscrptr *dscr;
775 1.4.2.2 yamt uint32_t sector_size, dscr_size;
776 1.4.2.2 yamt int error;
777 1.4.2.2 yamt
778 1.4.2.2 yamt sector_size = ump->discinfo.sector_size;
779 1.4.2.2 yamt
780 1.4.2.2 yamt /* loc is sectornr, len is in bytes */
781 1.4.2.2 yamt error = EIO;
782 1.4.2.2 yamt while (len) {
783 1.4.2.2 yamt error = udf_read_descriptor(ump, loc, M_UDFVOLD, &dscr);
784 1.4.2.2 yamt if (error)
785 1.4.2.2 yamt return error;
786 1.4.2.2 yamt
787 1.4.2.2 yamt /* blank block is a terminator */
788 1.4.2.2 yamt if (dscr == NULL)
789 1.4.2.2 yamt return 0;
790 1.4.2.2 yamt
791 1.4.2.2 yamt /* TERM descriptor is a terminator */
792 1.4.2.2 yamt if (udf_rw16(dscr->tag.id) == TAGID_TERM)
793 1.4.2.2 yamt return 0;
794 1.4.2.2 yamt
795 1.4.2.2 yamt /* process all others */
796 1.4.2.2 yamt dscr_size = udf_tagsize(dscr, sector_size);
797 1.4.2.2 yamt error = udf_process_vds_descriptor(ump, dscr);
798 1.4.2.2 yamt if (error) {
799 1.4.2.2 yamt free(dscr, M_UDFVOLD);
800 1.4.2.2 yamt break;
801 1.4.2.2 yamt };
802 1.4.2.2 yamt assert((dscr_size % sector_size) == 0);
803 1.4.2.2 yamt
804 1.4.2.2 yamt len -= dscr_size;
805 1.4.2.2 yamt loc += dscr_size / sector_size;
806 1.4.2.2 yamt };
807 1.4.2.2 yamt
808 1.4.2.2 yamt return error;
809 1.4.2.2 yamt }
810 1.4.2.2 yamt
811 1.4.2.2 yamt
812 1.4.2.2 yamt int
813 1.4.2.2 yamt udf_read_vds_space(struct udf_mount *ump)
814 1.4.2.2 yamt {
815 1.4.2.2 yamt struct anchor_vdp *anchor, *anchor2;
816 1.4.2.2 yamt size_t size;
817 1.4.2.2 yamt uint32_t main_loc, main_len;
818 1.4.2.2 yamt uint32_t reserve_loc, reserve_len;
819 1.4.2.2 yamt int error;
820 1.4.2.2 yamt
821 1.4.2.2 yamt /*
822 1.4.2.2 yamt * read in VDS space provided by the anchors; if one descriptor read
823 1.4.2.2 yamt * fails, try the mirror sector.
824 1.4.2.2 yamt *
825 1.4.2.2 yamt * check if 2nd anchor is different from 1st; if so, go for 2nd. This
826 1.4.2.2 yamt * avoids the `compatibility features' of DirectCD that may confuse
827 1.4.2.2 yamt * stuff completely.
828 1.4.2.2 yamt */
829 1.4.2.2 yamt
830 1.4.2.2 yamt anchor = ump->anchors[0];
831 1.4.2.2 yamt anchor2 = ump->anchors[1];
832 1.4.2.2 yamt assert(anchor);
833 1.4.2.2 yamt
834 1.4.2.2 yamt if (anchor2) {
835 1.4.2.2 yamt size = sizeof(struct extent_ad);
836 1.4.2.2 yamt if (memcmp(&anchor->main_vds_ex, &anchor2->main_vds_ex, size))
837 1.4.2.2 yamt anchor = anchor2;
838 1.4.2.2 yamt /* reserve is specified to be a literal copy of main */
839 1.4.2.2 yamt };
840 1.4.2.2 yamt
841 1.4.2.2 yamt main_loc = udf_rw32(anchor->main_vds_ex.loc);
842 1.4.2.2 yamt main_len = udf_rw32(anchor->main_vds_ex.len);
843 1.4.2.2 yamt
844 1.4.2.2 yamt reserve_loc = udf_rw32(anchor->reserve_vds_ex.loc);
845 1.4.2.2 yamt reserve_len = udf_rw32(anchor->reserve_vds_ex.len);
846 1.4.2.2 yamt
847 1.4.2.2 yamt error = udf_read_vds_extent(ump, main_loc, main_len);
848 1.4.2.2 yamt if (error) {
849 1.4.2.2 yamt printf("UDF mount: reading in reserve VDS extent\n");
850 1.4.2.2 yamt error = udf_read_vds_extent(ump, reserve_loc, reserve_len);
851 1.4.2.2 yamt };
852 1.4.2.2 yamt
853 1.4.2.2 yamt return error;
854 1.4.2.2 yamt }
855 1.4.2.2 yamt
856 1.4.2.2 yamt /* --------------------------------------------------------------------- */
857 1.4.2.2 yamt
858 1.4.2.2 yamt /*
859 1.4.2.2 yamt * Read in the logical volume integrity sequence pointed to by our logical
860 1.4.2.2 yamt * volume descriptor. Its a sequence that can be extended using fields in the
861 1.4.2.2 yamt * integrity descriptor itself. On sequential media only one is found, on
862 1.4.2.2 yamt * rewritable media a sequence of descriptors can be found as a form of
863 1.4.2.2 yamt * history keeping and on non sequential write-once media the chain is vital
864 1.4.2.2 yamt * to allow more and more descriptors to be written. The last descriptor
865 1.4.2.2 yamt * written in an extent needs to claim space for a new extent.
866 1.4.2.2 yamt */
867 1.4.2.2 yamt
868 1.4.2.2 yamt static int
869 1.4.2.2 yamt udf_retrieve_lvint(struct udf_mount *ump, struct logvol_int_desc **lvintp)
870 1.4.2.2 yamt {
871 1.4.2.2 yamt union dscrptr *dscr;
872 1.4.2.2 yamt struct logvol_int_desc *lvint;
873 1.4.2.2 yamt uint32_t sector_size, sector, len;
874 1.4.2.2 yamt int dscr_type, error;
875 1.4.2.2 yamt
876 1.4.2.2 yamt sector_size = ump->discinfo.sector_size;
877 1.4.2.2 yamt len = udf_rw32(ump->logical_vol->integrity_seq_loc.len);
878 1.4.2.2 yamt sector = udf_rw32(ump->logical_vol->integrity_seq_loc.loc);
879 1.4.2.2 yamt
880 1.4.2.2 yamt lvint = NULL;
881 1.4.2.2 yamt dscr = NULL;
882 1.4.2.2 yamt error = 0;
883 1.4.2.2 yamt while (len) {
884 1.4.2.2 yamt /* read in our integrity descriptor */
885 1.4.2.2 yamt error = udf_read_descriptor(ump, sector, M_UDFVOLD, &dscr);
886 1.4.2.2 yamt if (!error) {
887 1.4.2.2 yamt if (dscr == NULL)
888 1.4.2.2 yamt break; /* empty terminates */
889 1.4.2.2 yamt dscr_type = udf_rw16(dscr->tag.id);
890 1.4.2.2 yamt if (dscr_type == TAGID_TERM) {
891 1.4.2.2 yamt break; /* clean terminator */
892 1.4.2.2 yamt };
893 1.4.2.2 yamt if (dscr_type != TAGID_LOGVOL_INTEGRITY) {
894 1.4.2.2 yamt /* fatal... corrupt disc */
895 1.4.2.2 yamt error = ENOENT;
896 1.4.2.2 yamt break;
897 1.4.2.2 yamt };
898 1.4.2.2 yamt if (lvint)
899 1.4.2.2 yamt free(lvint, M_UDFVOLD);
900 1.4.2.2 yamt lvint = &dscr->lvid;
901 1.4.2.2 yamt dscr = NULL;
902 1.4.2.2 yamt }; /* else hope for the best... maybe the next is ok */
903 1.4.2.2 yamt
904 1.4.2.2 yamt DPRINTFIF(VOLUMES, lvint, ("logvol integrity read, state %s\n",
905 1.4.2.2 yamt udf_rw32(lvint->integrity_type) ? "CLOSED" : "OPEN"));
906 1.4.2.2 yamt
907 1.4.2.2 yamt /* proceed sequential */
908 1.4.2.2 yamt sector += 1;
909 1.4.2.2 yamt len -= sector_size;
910 1.4.2.2 yamt
911 1.4.2.2 yamt /* are we linking to a new piece? */
912 1.4.2.2 yamt if (lvint->next_extent.len) {
913 1.4.2.2 yamt len = udf_rw32(lvint->next_extent.len);
914 1.4.2.2 yamt sector = udf_rw32(lvint->next_extent.loc);
915 1.4.2.2 yamt };
916 1.4.2.2 yamt };
917 1.4.2.2 yamt
918 1.4.2.2 yamt /* clean up the mess, esp. when there is an error */
919 1.4.2.2 yamt if (dscr)
920 1.4.2.2 yamt free(dscr, M_UDFVOLD);
921 1.4.2.2 yamt
922 1.4.2.2 yamt if (error && lvint) {
923 1.4.2.2 yamt free(lvint, M_UDFVOLD);
924 1.4.2.2 yamt lvint = NULL;
925 1.4.2.2 yamt };
926 1.4.2.2 yamt
927 1.4.2.2 yamt if (!lvint)
928 1.4.2.2 yamt error = ENOENT;
929 1.4.2.2 yamt
930 1.4.2.2 yamt *lvintp = lvint;
931 1.4.2.2 yamt return error;
932 1.4.2.2 yamt }
933 1.4.2.2 yamt
934 1.4.2.2 yamt /* --------------------------------------------------------------------- */
935 1.4.2.2 yamt
936 1.4.2.2 yamt /*
937 1.4.2.2 yamt * Checks if ump's vds information is correct and complete
938 1.4.2.2 yamt */
939 1.4.2.2 yamt
940 1.4.2.2 yamt int
941 1.4.2.2 yamt udf_process_vds(struct udf_mount *ump, struct udf_args *args) {
942 1.4.2.2 yamt union udf_pmap *mapping;
943 1.4.2.2 yamt struct logvol_int_desc *lvint;
944 1.4.2.2 yamt struct udf_logvol_info *lvinfo;
945 1.4.2.2 yamt uint32_t n_pm, mt_l;
946 1.4.2.2 yamt uint8_t *pmap_pos;
947 1.4.2.2 yamt char *domain_name, *map_name;
948 1.4.2.2 yamt const char *check_name;
949 1.4.2.2 yamt int pmap_stype, pmap_size;
950 1.4.2.2 yamt int pmap_type, log_part, phys_part;
951 1.4.2.2 yamt int n_phys, n_virt, n_spar, n_meta;
952 1.4.2.2 yamt int len, error;
953 1.4.2.2 yamt
954 1.4.2.2 yamt if (ump == NULL)
955 1.4.2.2 yamt return ENOENT;
956 1.4.2.2 yamt
957 1.4.2.2 yamt /* we need at least an anchor (trivial, but for safety) */
958 1.4.2.2 yamt if (ump->anchors[0] == NULL)
959 1.4.2.2 yamt return EINVAL;
960 1.4.2.2 yamt
961 1.4.2.2 yamt /* we need at least one primary and one logical volume descriptor */
962 1.4.2.2 yamt if ((ump->primary_vol == NULL) || (ump->logical_vol) == NULL)
963 1.4.2.2 yamt return EINVAL;
964 1.4.2.2 yamt
965 1.4.2.2 yamt /* we need at least one partition descriptor */
966 1.4.2.2 yamt if (ump->partitions[0] == NULL)
967 1.4.2.2 yamt return EINVAL;
968 1.4.2.2 yamt
969 1.4.2.2 yamt /* check logical volume sector size verses device sector size */
970 1.4.2.2 yamt if (udf_rw32(ump->logical_vol->lb_size) != ump->discinfo.sector_size) {
971 1.4.2.2 yamt printf("UDF mount: format violation, lb_size != sector size\n");
972 1.4.2.2 yamt return EINVAL;
973 1.4.2.2 yamt };
974 1.4.2.2 yamt
975 1.4.2.2 yamt domain_name = ump->logical_vol->domain_id.id;
976 1.4.2.2 yamt if (strncmp(domain_name, "*OSTA UDF Compliant", 20)) {
977 1.4.2.2 yamt printf("mount_udf: disc not OSTA UDF Compliant, aborting\n");
978 1.4.2.2 yamt return EINVAL;
979 1.4.2.2 yamt };
980 1.4.2.2 yamt
981 1.4.2.2 yamt /* retrieve logical volume integrity sequence */
982 1.4.2.2 yamt error = udf_retrieve_lvint(ump, &ump->logvol_integrity);
983 1.4.2.2 yamt
984 1.4.2.2 yamt /*
985 1.4.2.2 yamt * We need at least one logvol integrity descriptor recorded. Note
986 1.4.2.2 yamt * that its OK to have an open logical volume integrity here. The VAT
987 1.4.2.2 yamt * will close/update the integrity.
988 1.4.2.2 yamt */
989 1.4.2.2 yamt if (ump->logvol_integrity == NULL)
990 1.4.2.2 yamt return EINVAL;
991 1.4.2.2 yamt
992 1.4.2.2 yamt /* process derived structures */
993 1.4.2.2 yamt n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
994 1.4.2.2 yamt lvint = ump->logvol_integrity;
995 1.4.2.2 yamt lvinfo = (struct udf_logvol_info *) (&lvint->tables[2 * n_pm]);
996 1.4.2.2 yamt ump->logvol_info = lvinfo;
997 1.4.2.2 yamt
998 1.4.2.2 yamt /* TODO check udf versions? */
999 1.4.2.2 yamt
1000 1.4.2.2 yamt /*
1001 1.4.2.2 yamt * check logvol mappings: effective virt->log partmap translation
1002 1.4.2.2 yamt * check and recording of the mapping results. Saves expensive
1003 1.4.2.2 yamt * strncmp() in tight places.
1004 1.4.2.2 yamt */
1005 1.4.2.2 yamt DPRINTF(VOLUMES, ("checking logvol mappings\n"));
1006 1.4.2.2 yamt n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
1007 1.4.2.2 yamt mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */
1008 1.4.2.2 yamt pmap_pos = ump->logical_vol->maps;
1009 1.4.2.2 yamt
1010 1.4.2.2 yamt if (n_pm > UDF_PMAPS) {
1011 1.4.2.2 yamt printf("UDF mount: too many mappings\n");
1012 1.4.2.2 yamt return EINVAL;
1013 1.4.2.2 yamt };
1014 1.4.2.2 yamt
1015 1.4.2.2 yamt n_phys = n_virt = n_spar = n_meta = 0;
1016 1.4.2.2 yamt for (log_part = 0; log_part < n_pm; log_part++) {
1017 1.4.2.2 yamt mapping = (union udf_pmap *) pmap_pos;
1018 1.4.2.2 yamt pmap_stype = pmap_pos[0];
1019 1.4.2.2 yamt pmap_size = pmap_pos[1];
1020 1.4.2.2 yamt switch (pmap_stype) {
1021 1.4.2.2 yamt case 1: /* physical mapping */
1022 1.4.2.2 yamt /* volseq = udf_rw16(mapping->pm1.vol_seq_num); */
1023 1.4.2.2 yamt phys_part = udf_rw16(mapping->pm1.part_num);
1024 1.4.2.2 yamt pmap_type = UDF_VTOP_TYPE_PHYS;
1025 1.4.2.2 yamt n_phys++;
1026 1.4.2.2 yamt break;
1027 1.4.2.2 yamt case 2: /* virtual/sparable/meta mapping */
1028 1.4.2.2 yamt map_name = mapping->pm2.part_id.id;
1029 1.4.2.2 yamt /* volseq = udf_rw16(mapping->pm2.vol_seq_num); */
1030 1.4.2.2 yamt phys_part = udf_rw16(mapping->pm2.part_num);
1031 1.4.2.2 yamt pmap_type = UDF_VTOP_TYPE_UNKNOWN;
1032 1.4.2.2 yamt len = UDF_REGID_ID_SIZE;
1033 1.4.2.2 yamt
1034 1.4.2.2 yamt check_name = "*UDF Virtual Partition";
1035 1.4.2.2 yamt if (strncmp(map_name, check_name, len) == 0) {
1036 1.4.2.2 yamt pmap_type = UDF_VTOP_TYPE_VIRT;
1037 1.4.2.2 yamt n_virt++;
1038 1.4.2.2 yamt break;
1039 1.4.2.2 yamt };
1040 1.4.2.2 yamt check_name = "*UDF Sparable Partition";
1041 1.4.2.2 yamt if (strncmp(map_name, check_name, len) == 0) {
1042 1.4.2.2 yamt pmap_type = UDF_VTOP_TYPE_SPARABLE;
1043 1.4.2.2 yamt n_spar++;
1044 1.4.2.2 yamt break;
1045 1.4.2.2 yamt };
1046 1.4.2.2 yamt check_name = "*UDF Metadata Partition";
1047 1.4.2.2 yamt if (strncmp(map_name, check_name, len) == 0) {
1048 1.4.2.2 yamt pmap_type = UDF_VTOP_TYPE_META;
1049 1.4.2.2 yamt n_meta++;
1050 1.4.2.2 yamt break;
1051 1.4.2.2 yamt };
1052 1.4.2.2 yamt break;
1053 1.4.2.2 yamt default:
1054 1.4.2.2 yamt return EINVAL;
1055 1.4.2.2 yamt };
1056 1.4.2.2 yamt
1057 1.4.2.2 yamt DPRINTF(VOLUMES, ("\t%d -> %d type %d\n", log_part, phys_part,
1058 1.4.2.2 yamt pmap_type));
1059 1.4.2.2 yamt if (pmap_type == UDF_VTOP_TYPE_UNKNOWN)
1060 1.4.2.2 yamt return EINVAL;
1061 1.4.2.2 yamt
1062 1.4.2.2 yamt ump->vtop [log_part] = phys_part;
1063 1.4.2.2 yamt ump->vtop_tp[log_part] = pmap_type;
1064 1.4.2.2 yamt
1065 1.4.2.2 yamt pmap_pos += pmap_size;
1066 1.4.2.2 yamt };
1067 1.4.2.2 yamt /* not winning the beauty contest */
1068 1.4.2.2 yamt ump->vtop_tp[UDF_VTOP_RAWPART] = UDF_VTOP_TYPE_RAW;
1069 1.4.2.2 yamt
1070 1.4.2.2 yamt /* test some basic UDF assertions/requirements */
1071 1.4.2.2 yamt if ((n_virt > 1) || (n_spar > 1) || (n_meta > 1))
1072 1.4.2.2 yamt return EINVAL;
1073 1.4.2.2 yamt
1074 1.4.2.2 yamt if (n_virt) {
1075 1.4.2.2 yamt if ((n_phys == 0) || n_spar || n_meta)
1076 1.4.2.2 yamt return EINVAL;
1077 1.4.2.2 yamt };
1078 1.4.2.2 yamt if (n_spar + n_phys == 0)
1079 1.4.2.2 yamt return EINVAL;
1080 1.4.2.2 yamt
1081 1.4.2.2 yamt /* vat's can only be on a sequential media */
1082 1.4.2.2 yamt ump->data_alloc = UDF_ALLOC_SPACEMAP;
1083 1.4.2.2 yamt if (n_virt)
1084 1.4.2.2 yamt ump->data_alloc = UDF_ALLOC_SEQUENTIAL;
1085 1.4.2.2 yamt
1086 1.4.2.2 yamt ump->meta_alloc = UDF_ALLOC_SPACEMAP;
1087 1.4.2.2 yamt if (n_virt)
1088 1.4.2.2 yamt ump->meta_alloc = UDF_ALLOC_VAT;
1089 1.4.2.2 yamt if (n_meta)
1090 1.4.2.2 yamt ump->meta_alloc = UDF_ALLOC_METABITMAP;
1091 1.4.2.2 yamt
1092 1.4.2.2 yamt /* special cases for pseudo-overwrite */
1093 1.4.2.2 yamt if (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE) {
1094 1.4.2.2 yamt ump->data_alloc = UDF_ALLOC_SEQUENTIAL;
1095 1.4.2.2 yamt if (n_meta) {
1096 1.4.2.2 yamt ump->meta_alloc = UDF_ALLOC_METASEQUENTIAL;
1097 1.4.2.2 yamt } else {
1098 1.4.2.2 yamt ump->meta_alloc = UDF_ALLOC_RELAXEDSEQUENTIAL;
1099 1.4.2.2 yamt };
1100 1.4.2.2 yamt };
1101 1.4.2.2 yamt
1102 1.4.2.2 yamt DPRINTF(VOLUMES, ("\tdata alloc scheme %d, meta alloc scheme %d\n",
1103 1.4.2.2 yamt ump->data_alloc, ump->meta_alloc));
1104 1.4.2.2 yamt /* TODO determine partitions to write data and metadata ? */
1105 1.4.2.2 yamt
1106 1.4.2.2 yamt /* signal its OK for now */
1107 1.4.2.2 yamt return 0;
1108 1.4.2.2 yamt }
1109 1.4.2.2 yamt
1110 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1111 1.4.2.2 yamt
1112 1.4.2.2 yamt /*
1113 1.4.2.2 yamt * Read in complete VAT file and check if its indeed a VAT file descriptor
1114 1.4.2.2 yamt */
1115 1.4.2.2 yamt
1116 1.4.2.2 yamt static int
1117 1.4.2.2 yamt udf_check_for_vat(struct udf_node *vat_node)
1118 1.4.2.2 yamt {
1119 1.4.2.2 yamt struct udf_mount *ump;
1120 1.4.2.2 yamt struct icb_tag *icbtag;
1121 1.4.2.2 yamt struct timestamp *mtime;
1122 1.4.2.2 yamt struct regid *regid;
1123 1.4.2.2 yamt struct udf_vat *vat;
1124 1.4.2.2 yamt struct udf_logvol_info *lvinfo;
1125 1.4.2.2 yamt uint32_t vat_length, alloc_length;
1126 1.4.2.2 yamt uint32_t vat_offset, vat_entries;
1127 1.4.2.2 yamt uint32_t sector_size;
1128 1.4.2.2 yamt uint32_t sectors;
1129 1.4.2.2 yamt uint32_t *raw_vat;
1130 1.4.2.2 yamt char *regid_name;
1131 1.4.2.2 yamt int filetype;
1132 1.4.2.2 yamt int error;
1133 1.4.2.2 yamt
1134 1.4.2.2 yamt /* vat_length is really 64 bits though impossible */
1135 1.4.2.2 yamt
1136 1.4.2.2 yamt DPRINTF(VOLUMES, ("Checking for VAT\n"));
1137 1.4.2.2 yamt if (!vat_node)
1138 1.4.2.2 yamt return ENOENT;
1139 1.4.2.2 yamt
1140 1.4.2.2 yamt /* get mount info */
1141 1.4.2.2 yamt ump = vat_node->ump;
1142 1.4.2.2 yamt
1143 1.4.2.2 yamt /* check assertions */
1144 1.4.2.2 yamt assert(vat_node->fe || vat_node->efe);
1145 1.4.2.2 yamt assert(ump->logvol_integrity);
1146 1.4.2.2 yamt
1147 1.4.2.2 yamt /* get information from fe/efe */
1148 1.4.2.2 yamt if (vat_node->fe) {
1149 1.4.2.2 yamt vat_length = udf_rw64(vat_node->fe->inf_len);
1150 1.4.2.2 yamt icbtag = &vat_node->fe->icbtag;
1151 1.4.2.2 yamt mtime = &vat_node->fe->mtime;
1152 1.4.2.2 yamt } else {
1153 1.4.2.2 yamt vat_length = udf_rw64(vat_node->efe->inf_len);
1154 1.4.2.2 yamt icbtag = &vat_node->efe->icbtag;
1155 1.4.2.2 yamt mtime = &vat_node->efe->mtime;
1156 1.4.2.2 yamt };
1157 1.4.2.2 yamt
1158 1.4.2.2 yamt /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
1159 1.4.2.2 yamt filetype = icbtag->file_type;
1160 1.4.2.2 yamt if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT))
1161 1.4.2.2 yamt return ENOENT;
1162 1.4.2.2 yamt
1163 1.4.2.2 yamt DPRINTF(VOLUMES, ("\tPossible VAT length %d\n", vat_length));
1164 1.4.2.2 yamt /* place a sanity check on the length; currently 1Mb in size */
1165 1.4.2.2 yamt if (vat_length > 1*1024*1024)
1166 1.4.2.2 yamt return ENOENT;
1167 1.4.2.2 yamt
1168 1.4.2.2 yamt /* get sector size */
1169 1.4.2.2 yamt sector_size = vat_node->ump->discinfo.sector_size;
1170 1.4.2.2 yamt
1171 1.4.2.2 yamt /* calculate how many sectors to read in and how much to allocate */
1172 1.4.2.2 yamt sectors = (vat_length + sector_size -1) / sector_size;
1173 1.4.2.2 yamt alloc_length = (sectors + 2) * sector_size;
1174 1.4.2.2 yamt
1175 1.4.2.2 yamt /* try to allocate the space */
1176 1.4.2.2 yamt ump->vat_table_alloc_length = alloc_length;
1177 1.4.2.2 yamt ump->vat_table = malloc(alloc_length, M_UDFMNT, M_CANFAIL | M_WAITOK);
1178 1.4.2.2 yamt if (!ump->vat_table)
1179 1.4.2.2 yamt return ENOMEM; /* impossible to allocate */
1180 1.4.2.2 yamt DPRINTF(VOLUMES, ("\talloced fine\n"));
1181 1.4.2.2 yamt
1182 1.4.2.2 yamt /* read it in! */
1183 1.4.2.2 yamt raw_vat = (uint32_t *) ump->vat_table;
1184 1.4.2.2 yamt error = udf_read_file_extent(vat_node, 0, sectors, (uint8_t *) raw_vat);
1185 1.4.2.2 yamt if (error) {
1186 1.4.2.2 yamt DPRINTF(VOLUMES, ("\tread failed : %d\n", error));
1187 1.4.2.2 yamt /* not completely readable... :( bomb out */
1188 1.4.2.2 yamt free(ump->vat_table, M_UDFMNT);
1189 1.4.2.2 yamt ump->vat_table = NULL;
1190 1.4.2.2 yamt return error;
1191 1.4.2.2 yamt };
1192 1.4.2.2 yamt DPRINTF(VOLUMES, ("VAT read in fine!\n"));
1193 1.4.2.2 yamt
1194 1.4.2.2 yamt /*
1195 1.4.2.2 yamt * check contents of the file if its the old 1.50 VAT table format.
1196 1.4.2.2 yamt * Its notoriously broken and allthough some implementations support an
1197 1.4.2.2 yamt * extention as defined in the UDF 1.50 errata document, its doubtfull
1198 1.4.2.2 yamt * to be useable since a lot of implementations don't maintain it.
1199 1.4.2.2 yamt */
1200 1.4.2.2 yamt lvinfo = ump->logvol_info;
1201 1.4.2.2 yamt
1202 1.4.2.2 yamt if (filetype == 0) {
1203 1.4.2.2 yamt /* definition */
1204 1.4.2.2 yamt vat_offset = 0;
1205 1.4.2.2 yamt vat_entries = (vat_length-36)/4;
1206 1.4.2.2 yamt
1207 1.4.2.2 yamt /* check 1.50 VAT */
1208 1.4.2.2 yamt regid = (struct regid *) (raw_vat + vat_entries);
1209 1.4.2.2 yamt regid_name = (char *) regid->id;
1210 1.4.2.2 yamt error = strncmp(regid_name, "*UDF Virtual Alloc Tbl", 22);
1211 1.4.2.2 yamt if (error) {
1212 1.4.2.2 yamt DPRINTF(VOLUMES, ("VAT format 1.50 rejected\n"));
1213 1.4.2.2 yamt free(ump->vat_table, M_UDFMNT);
1214 1.4.2.2 yamt ump->vat_table = NULL;
1215 1.4.2.2 yamt return ENOENT;
1216 1.4.2.2 yamt };
1217 1.4.2.2 yamt /* TODO update LVID from "*UDF VAT LVExtension" ext. attr. */
1218 1.4.2.2 yamt } else {
1219 1.4.2.2 yamt vat = (struct udf_vat *) raw_vat;
1220 1.4.2.2 yamt
1221 1.4.2.2 yamt /* definition */
1222 1.4.2.2 yamt vat_offset = vat->header_len;
1223 1.4.2.2 yamt vat_entries = (vat_length - vat_offset)/4;
1224 1.4.2.2 yamt
1225 1.4.2.2 yamt assert(lvinfo);
1226 1.4.2.2 yamt lvinfo->num_files = vat->num_files;
1227 1.4.2.2 yamt lvinfo->num_directories = vat->num_directories;
1228 1.4.2.2 yamt lvinfo->min_udf_readver = vat->min_udf_readver;
1229 1.4.2.2 yamt lvinfo->min_udf_writever = vat->min_udf_writever;
1230 1.4.2.2 yamt lvinfo->max_udf_writever = vat->max_udf_writever;
1231 1.4.2.2 yamt };
1232 1.4.2.2 yamt
1233 1.4.2.2 yamt ump->vat_offset = vat_offset;
1234 1.4.2.2 yamt ump->vat_entries = vat_entries;
1235 1.4.2.2 yamt
1236 1.4.2.2 yamt DPRINTF(VOLUMES, ("VAT format accepted, marking it closed\n"));
1237 1.4.2.2 yamt ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
1238 1.4.2.2 yamt ump->logvol_integrity->time = *mtime;
1239 1.4.2.2 yamt
1240 1.4.2.2 yamt return 0; /* success! */
1241 1.4.2.2 yamt }
1242 1.4.2.2 yamt
1243 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1244 1.4.2.2 yamt
1245 1.4.2.2 yamt static int
1246 1.4.2.2 yamt udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping)
1247 1.4.2.2 yamt {
1248 1.4.2.2 yamt struct udf_node *vat_node;
1249 1.4.2.2 yamt struct long_ad icb_loc;
1250 1.4.2.2 yamt uint32_t early_vat_loc, late_vat_loc, vat_loc;
1251 1.4.2.2 yamt int error;
1252 1.4.2.2 yamt
1253 1.4.2.2 yamt /* mapping info not needed */
1254 1.4.2.2 yamt mapping = mapping;
1255 1.4.2.2 yamt
1256 1.4.2.2 yamt vat_loc = ump->possible_vat_location;
1257 1.4.2.2 yamt early_vat_loc = vat_loc - 20;
1258 1.4.2.2 yamt late_vat_loc = vat_loc + 1024;
1259 1.4.2.2 yamt
1260 1.4.2.2 yamt /* TODO first search last sector? */
1261 1.4.2.2 yamt do {
1262 1.4.2.2 yamt DPRINTF(VOLUMES, ("Checking for VAT at sector %d\n", vat_loc));
1263 1.4.2.2 yamt icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART);
1264 1.4.2.2 yamt icb_loc.loc.lb_num = udf_rw32(vat_loc);
1265 1.4.2.2 yamt
1266 1.4.2.2 yamt error = udf_get_node(ump, &icb_loc, &vat_node);
1267 1.4.2.2 yamt if (!error) error = udf_check_for_vat(vat_node);
1268 1.4.2.2 yamt if (!error) break;
1269 1.4.2.2 yamt if (vat_node) {
1270 1.4.2.2 yamt vput(vat_node->vnode);
1271 1.4.2.2 yamt udf_dispose_node(vat_node);
1272 1.4.2.2 yamt };
1273 1.4.2.2 yamt vat_loc--; /* walk backwards */
1274 1.4.2.2 yamt } while (vat_loc >= early_vat_loc);
1275 1.4.2.2 yamt
1276 1.4.2.2 yamt /* we don't need our VAT node anymore */
1277 1.4.2.2 yamt if (vat_node) {
1278 1.4.2.2 yamt vput(vat_node->vnode);
1279 1.4.2.2 yamt udf_dispose_node(vat_node);
1280 1.4.2.2 yamt };
1281 1.4.2.2 yamt
1282 1.4.2.2 yamt return error;
1283 1.4.2.2 yamt }
1284 1.4.2.2 yamt
1285 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1286 1.4.2.2 yamt
1287 1.4.2.2 yamt static int
1288 1.4.2.2 yamt udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
1289 1.4.2.2 yamt {
1290 1.4.2.2 yamt union dscrptr *dscr;
1291 1.4.2.2 yamt struct part_map_spare *pms = (struct part_map_spare *) mapping;
1292 1.4.2.2 yamt uint32_t lb_num;
1293 1.4.2.2 yamt int spar, error;
1294 1.4.2.2 yamt
1295 1.4.2.2 yamt /*
1296 1.4.2.2 yamt * The partition mapping passed on to us specifies the information we
1297 1.4.2.2 yamt * need to locate and initialise the sparable partition mapping
1298 1.4.2.2 yamt * information we need.
1299 1.4.2.2 yamt */
1300 1.4.2.2 yamt
1301 1.4.2.2 yamt DPRINTF(VOLUMES, ("Read sparable table\n"));
1302 1.4.2.2 yamt ump->sparable_packet_len = udf_rw16(pms->packet_len);
1303 1.4.2.2 yamt for (spar = 0; spar < pms->n_st; spar++) {
1304 1.4.2.2 yamt lb_num = pms->st_loc[spar];
1305 1.4.2.2 yamt DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num));
1306 1.4.2.2 yamt error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr);
1307 1.4.2.2 yamt if (!error && dscr) {
1308 1.4.2.2 yamt if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) {
1309 1.4.2.2 yamt if (ump->sparing_table)
1310 1.4.2.2 yamt free(ump->sparing_table, M_UDFVOLD);
1311 1.4.2.2 yamt ump->sparing_table = &dscr->spt;
1312 1.4.2.2 yamt dscr = NULL;
1313 1.4.2.2 yamt DPRINTF(VOLUMES,
1314 1.4.2.2 yamt ("Sparing table accepted (%d entries)\n",
1315 1.4.2.2 yamt udf_rw16(ump->sparing_table->rt_l)));
1316 1.4.2.2 yamt break; /* we're done */
1317 1.4.2.2 yamt };
1318 1.4.2.2 yamt };
1319 1.4.2.2 yamt if (dscr)
1320 1.4.2.2 yamt free(dscr, M_UDFVOLD);
1321 1.4.2.2 yamt };
1322 1.4.2.2 yamt
1323 1.4.2.2 yamt if (ump->sparing_table)
1324 1.4.2.2 yamt return 0;
1325 1.4.2.2 yamt
1326 1.4.2.2 yamt return ENOENT;
1327 1.4.2.2 yamt }
1328 1.4.2.2 yamt
1329 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1330 1.4.2.2 yamt
1331 1.4.2.2 yamt int
1332 1.4.2.2 yamt udf_read_vds_tables(struct udf_mount *ump, struct udf_args *args)
1333 1.4.2.2 yamt {
1334 1.4.2.2 yamt union udf_pmap *mapping;
1335 1.4.2.2 yamt uint32_t n_pm, mt_l;
1336 1.4.2.2 yamt uint32_t log_part;
1337 1.4.2.2 yamt uint8_t *pmap_pos;
1338 1.4.2.2 yamt int pmap_size;
1339 1.4.2.2 yamt int error;
1340 1.4.2.2 yamt
1341 1.4.2.2 yamt /* We have to iterate again over the part mappings for locations */
1342 1.4.2.2 yamt n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
1343 1.4.2.2 yamt mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */
1344 1.4.2.2 yamt pmap_pos = ump->logical_vol->maps;
1345 1.4.2.2 yamt
1346 1.4.2.2 yamt for (log_part = 0; log_part < n_pm; log_part++) {
1347 1.4.2.2 yamt mapping = (union udf_pmap *) pmap_pos;
1348 1.4.2.2 yamt switch (ump->vtop_tp[log_part]) {
1349 1.4.2.2 yamt case UDF_VTOP_TYPE_PHYS :
1350 1.4.2.2 yamt /* nothing */
1351 1.4.2.2 yamt break;
1352 1.4.2.2 yamt case UDF_VTOP_TYPE_VIRT :
1353 1.4.2.2 yamt /* search and load VAT */
1354 1.4.2.2 yamt error = udf_search_vat(ump, mapping);
1355 1.4.2.2 yamt if (error)
1356 1.4.2.2 yamt return ENOENT;
1357 1.4.2.2 yamt break;
1358 1.4.2.2 yamt case UDF_VTOP_TYPE_SPARABLE :
1359 1.4.2.2 yamt /* load one of the sparable tables */
1360 1.4.2.2 yamt error = udf_read_sparables(ump, mapping);
1361 1.4.2.2 yamt break;
1362 1.4.2.2 yamt case UDF_VTOP_TYPE_META :
1363 1.4.2.2 yamt /* TODO load metafile and metabitmapfile FE/EFEs */
1364 1.4.2.2 yamt break;
1365 1.4.2.2 yamt default:
1366 1.4.2.2 yamt break;
1367 1.4.2.2 yamt };
1368 1.4.2.2 yamt pmap_size = pmap_pos[1];
1369 1.4.2.2 yamt pmap_pos += pmap_size;
1370 1.4.2.2 yamt };
1371 1.4.2.2 yamt
1372 1.4.2.2 yamt return 0;
1373 1.4.2.2 yamt }
1374 1.4.2.2 yamt
1375 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1376 1.4.2.2 yamt
1377 1.4.2.2 yamt int
1378 1.4.2.2 yamt udf_read_rootdirs(struct udf_mount *ump, struct udf_args *args)
1379 1.4.2.2 yamt {
1380 1.4.2.2 yamt struct udf_node *rootdir_node, *streamdir_node;
1381 1.4.2.2 yamt union dscrptr *dscr;
1382 1.4.2.2 yamt struct long_ad fsd_loc, *dir_loc;
1383 1.4.2.2 yamt uint32_t lb_num, dummy;
1384 1.4.2.2 yamt uint32_t fsd_len;
1385 1.4.2.2 yamt int dscr_type;
1386 1.4.2.2 yamt int error;
1387 1.4.2.2 yamt
1388 1.4.2.2 yamt /* TODO implement FSD reading in seperate function like integrity? */
1389 1.4.2.2 yamt /* get fileset descriptor sequence */
1390 1.4.2.2 yamt fsd_loc = ump->logical_vol->lv_fsd_loc;
1391 1.4.2.2 yamt fsd_len = udf_rw32(fsd_loc.len);
1392 1.4.2.2 yamt
1393 1.4.2.2 yamt dscr = NULL;
1394 1.4.2.2 yamt error = 0;
1395 1.4.2.2 yamt while (fsd_len || error) {
1396 1.4.2.2 yamt DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
1397 1.4.2.2 yamt /* translate fsd_loc to lb_num */
1398 1.4.2.2 yamt error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
1399 1.4.2.2 yamt if (error)
1400 1.4.2.2 yamt break;
1401 1.4.2.2 yamt DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
1402 1.4.2.2 yamt error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr);
1403 1.4.2.2 yamt /* end markers */
1404 1.4.2.2 yamt if (error || (dscr == NULL))
1405 1.4.2.2 yamt break;
1406 1.4.2.2 yamt
1407 1.4.2.2 yamt /* analyse */
1408 1.4.2.2 yamt dscr_type = udf_rw16(dscr->tag.id);
1409 1.4.2.2 yamt if (dscr_type == TAGID_TERM)
1410 1.4.2.2 yamt break;
1411 1.4.2.2 yamt if (dscr_type != TAGID_FSD) {
1412 1.4.2.2 yamt free(dscr, M_UDFVOLD);
1413 1.4.2.2 yamt return ENOENT;
1414 1.4.2.2 yamt };
1415 1.4.2.2 yamt
1416 1.4.2.2 yamt /*
1417 1.4.2.2 yamt * TODO check for multiple fileset descriptors; its only
1418 1.4.2.2 yamt * picking the last now. Also check for FSD
1419 1.4.2.2 yamt * correctness/interpretability
1420 1.4.2.2 yamt */
1421 1.4.2.2 yamt
1422 1.4.2.2 yamt /* update */
1423 1.4.2.2 yamt if (ump->fileset_desc) {
1424 1.4.2.2 yamt free(ump->fileset_desc, M_UDFVOLD);
1425 1.4.2.2 yamt };
1426 1.4.2.2 yamt ump->fileset_desc = &dscr->fsd;
1427 1.4.2.2 yamt dscr = NULL;
1428 1.4.2.2 yamt
1429 1.4.2.2 yamt /* continue to the next fsd */
1430 1.4.2.2 yamt fsd_len -= ump->discinfo.sector_size;
1431 1.4.2.2 yamt fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
1432 1.4.2.2 yamt
1433 1.4.2.2 yamt /* follow up to fsd->next_ex (long_ad) if its not null */
1434 1.4.2.2 yamt if (udf_rw32(ump->fileset_desc->next_ex.len)) {
1435 1.4.2.2 yamt DPRINTF(VOLUMES, ("follow up FSD extent\n"));
1436 1.4.2.2 yamt fsd_loc = ump->fileset_desc->next_ex;
1437 1.4.2.2 yamt fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
1438 1.4.2.2 yamt };
1439 1.4.2.2 yamt };
1440 1.4.2.2 yamt if (dscr)
1441 1.4.2.2 yamt free(dscr, M_UDFVOLD);
1442 1.4.2.2 yamt
1443 1.4.2.2 yamt /* there has to be one */
1444 1.4.2.2 yamt if (ump->fileset_desc == NULL)
1445 1.4.2.2 yamt return ENOENT;
1446 1.4.2.2 yamt
1447 1.4.2.2 yamt DPRINTF(VOLUMES, ("FSD read in fine\n"));
1448 1.4.2.2 yamt
1449 1.4.2.2 yamt /*
1450 1.4.2.2 yamt * Now the FSD is known, read in the rootdirectory and if one exists,
1451 1.4.2.2 yamt * the system stream dir. Some files in the system streamdir are not
1452 1.4.2.2 yamt * wanted in this implementation since they are not maintained. If
1453 1.4.2.2 yamt * writing is enabled we'll delete these files if they exist.
1454 1.4.2.2 yamt */
1455 1.4.2.2 yamt
1456 1.4.2.2 yamt rootdir_node = streamdir_node = NULL;
1457 1.4.2.2 yamt dir_loc = NULL;
1458 1.4.2.2 yamt
1459 1.4.2.2 yamt /* try to read in the rootdir */
1460 1.4.2.2 yamt dir_loc = &ump->fileset_desc->rootdir_icb;
1461 1.4.2.2 yamt error = udf_get_node(ump, dir_loc, &rootdir_node);
1462 1.4.2.2 yamt if (error)
1463 1.4.2.2 yamt return ENOENT;
1464 1.4.2.2 yamt
1465 1.4.2.2 yamt /* aparently it read in fine */
1466 1.4.2.2 yamt
1467 1.4.2.2 yamt /*
1468 1.4.2.2 yamt * Try the system stream directory; not very likely in the ones we
1469 1.4.2.2 yamt * test, but for completeness.
1470 1.4.2.2 yamt */
1471 1.4.2.2 yamt dir_loc = &ump->fileset_desc->streamdir_icb;
1472 1.4.2.2 yamt if (udf_rw32(dir_loc->len)) {
1473 1.4.2.2 yamt error = udf_get_node(ump, dir_loc, &streamdir_node);
1474 1.4.2.2 yamt if (error)
1475 1.4.2.2 yamt printf("udf mount: streamdir defined but ignored\n");
1476 1.4.2.2 yamt if (!error) {
1477 1.4.2.2 yamt /*
1478 1.4.2.2 yamt * TODO process streamdir `baddies' i.e. files we dont
1479 1.4.2.2 yamt * want if R/W
1480 1.4.2.2 yamt */
1481 1.4.2.2 yamt };
1482 1.4.2.2 yamt };
1483 1.4.2.2 yamt
1484 1.4.2.2 yamt DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
1485 1.4.2.2 yamt
1486 1.4.2.2 yamt /* release the vnodes again; they'll be auto-recycled later */
1487 1.4.2.2 yamt if (streamdir_node) {
1488 1.4.2.2 yamt vput(streamdir_node->vnode);
1489 1.4.2.2 yamt };
1490 1.4.2.2 yamt if (rootdir_node) {
1491 1.4.2.2 yamt vput(rootdir_node->vnode);
1492 1.4.2.2 yamt };
1493 1.4.2.2 yamt
1494 1.4.2.2 yamt return 0;
1495 1.4.2.2 yamt }
1496 1.4.2.2 yamt
1497 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1498 1.4.2.2 yamt
1499 1.4.2.2 yamt int
1500 1.4.2.2 yamt udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc,
1501 1.4.2.2 yamt uint32_t *lb_numres, uint32_t *extres)
1502 1.4.2.2 yamt {
1503 1.4.2.2 yamt struct part_desc *pdesc;
1504 1.4.2.2 yamt struct spare_map_entry *sme;
1505 1.4.2.2 yamt uint32_t *trans;
1506 1.4.2.2 yamt uint32_t lb_num, lb_rel, lb_packet;
1507 1.4.2.2 yamt int rel, vpart, part;
1508 1.4.2.2 yamt
1509 1.4.2.2 yamt assert(ump && icb_loc && lb_numres);
1510 1.4.2.2 yamt
1511 1.4.2.2 yamt vpart = udf_rw16(icb_loc->loc.part_num);
1512 1.4.2.2 yamt lb_num = udf_rw32(icb_loc->loc.lb_num);
1513 1.4.2.2 yamt if (vpart < 0 || vpart > UDF_VTOP_RAWPART)
1514 1.4.2.2 yamt return EINVAL;
1515 1.4.2.2 yamt
1516 1.4.2.2 yamt switch (ump->vtop_tp[vpart]) {
1517 1.4.2.2 yamt case UDF_VTOP_TYPE_RAW :
1518 1.4.2.2 yamt /* 1:1 to the end of the device */
1519 1.4.2.2 yamt *lb_numres = lb_num;
1520 1.4.2.2 yamt *extres = INT_MAX;
1521 1.4.2.2 yamt return 0;
1522 1.4.2.2 yamt case UDF_VTOP_TYPE_PHYS :
1523 1.4.2.2 yamt /* transform into its disc logical block */
1524 1.4.2.2 yamt part = ump->vtop[vpart];
1525 1.4.2.2 yamt pdesc = ump->partitions[part];
1526 1.4.2.2 yamt if (lb_num > udf_rw32(pdesc->part_len))
1527 1.4.2.2 yamt return EINVAL;
1528 1.4.2.2 yamt *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1529 1.4.2.2 yamt
1530 1.4.2.2 yamt /* extent from here to the end of the partition */
1531 1.4.2.2 yamt *extres = udf_rw32(pdesc->part_len) - lb_num;
1532 1.4.2.2 yamt return 0;
1533 1.4.2.2 yamt case UDF_VTOP_TYPE_VIRT :
1534 1.4.2.2 yamt /* only maps one sector, lookup in VAT */
1535 1.4.2.2 yamt if (lb_num >= ump->vat_entries) /* XXX > or >= ? */
1536 1.4.2.2 yamt return EINVAL;
1537 1.4.2.2 yamt
1538 1.4.2.2 yamt /* lookup in virtual allocation table */
1539 1.4.2.2 yamt trans = (uint32_t *) (ump->vat_table + ump->vat_offset);
1540 1.4.2.2 yamt lb_num = udf_rw32(trans[lb_num]);
1541 1.4.2.2 yamt
1542 1.4.2.2 yamt /* transform into its disc logical block */
1543 1.4.2.2 yamt part = ump->vtop[vpart];
1544 1.4.2.2 yamt pdesc = ump->partitions[part];
1545 1.4.2.2 yamt if (lb_num > udf_rw32(pdesc->part_len))
1546 1.4.2.2 yamt return EINVAL;
1547 1.4.2.2 yamt *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1548 1.4.2.2 yamt
1549 1.4.2.2 yamt /* just one logical block */
1550 1.4.2.2 yamt *extres = 1;
1551 1.4.2.2 yamt return 0;
1552 1.4.2.2 yamt case UDF_VTOP_TYPE_SPARABLE :
1553 1.4.2.2 yamt /* check if the packet containing the lb_num is remapped */
1554 1.4.2.2 yamt lb_packet = lb_num / ump->sparable_packet_len;
1555 1.4.2.2 yamt lb_rel = lb_num % ump->sparable_packet_len;
1556 1.4.2.2 yamt
1557 1.4.2.2 yamt for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) {
1558 1.4.2.2 yamt sme = &ump->sparing_table->entries[rel];
1559 1.4.2.2 yamt if (lb_packet == udf_rw32(sme->org)) {
1560 1.4.2.2 yamt /* NOTE maps to absolute disc logical block! */
1561 1.4.2.2 yamt *lb_numres = udf_rw32(sme->map) + lb_rel;
1562 1.4.2.2 yamt *extres = ump->sparable_packet_len - lb_rel;
1563 1.4.2.2 yamt return 0;
1564 1.4.2.2 yamt };
1565 1.4.2.2 yamt };
1566 1.4.2.2 yamt
1567 1.4.2.2 yamt /* transform into its disc logical block */
1568 1.4.2.2 yamt part = ump->vtop[vpart];
1569 1.4.2.2 yamt pdesc = ump->partitions[part];
1570 1.4.2.2 yamt if (lb_num > udf_rw32(pdesc->part_len))
1571 1.4.2.2 yamt return EINVAL;
1572 1.4.2.2 yamt *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1573 1.4.2.2 yamt
1574 1.4.2.2 yamt /* rest of block */
1575 1.4.2.2 yamt *extres = ump->sparable_packet_len - lb_rel;
1576 1.4.2.2 yamt return 0;
1577 1.4.2.2 yamt case UDF_VTOP_TYPE_META :
1578 1.4.2.2 yamt default:
1579 1.4.2.2 yamt printf("UDF vtop translation scheme %d unimplemented yet\n",
1580 1.4.2.2 yamt ump->vtop_tp[vpart]);
1581 1.4.2.2 yamt };
1582 1.4.2.2 yamt
1583 1.4.2.2 yamt return EINVAL;
1584 1.4.2.2 yamt }
1585 1.4.2.2 yamt
1586 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1587 1.4.2.2 yamt
1588 1.4.2.2 yamt /* To make absolutely sure we are NOT returning zero, add one :) */
1589 1.4.2.2 yamt
1590 1.4.2.2 yamt long
1591 1.4.2.2 yamt udf_calchash(struct long_ad *icbptr)
1592 1.4.2.2 yamt {
1593 1.4.2.2 yamt /* ought to be enough since each mountpoint has its own chain */
1594 1.4.2.2 yamt return udf_rw32(icbptr->loc.lb_num) + 1;
1595 1.4.2.2 yamt }
1596 1.4.2.2 yamt
1597 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1598 1.4.2.2 yamt
1599 1.4.2.2 yamt static struct udf_node *
1600 1.4.2.2 yamt udf_hashget(struct udf_mount *ump, struct long_ad *icbptr)
1601 1.4.2.2 yamt {
1602 1.4.2.2 yamt struct udf_node *unp;
1603 1.4.2.2 yamt struct vnode *vp;
1604 1.4.2.2 yamt uint32_t hashline;
1605 1.4.2.2 yamt
1606 1.4.2.2 yamt loop:
1607 1.4.2.2 yamt simple_lock(&ump->ihash_slock);
1608 1.4.2.2 yamt
1609 1.4.2.2 yamt hashline = udf_calchash(icbptr) & UDF_INODE_HASHMASK;
1610 1.4.2.2 yamt LIST_FOREACH(unp, &ump->udf_nodes[hashline], hashchain) {
1611 1.4.2.2 yamt assert(unp);
1612 1.4.2.2 yamt if (unp->loc.loc.lb_num == icbptr->loc.lb_num &&
1613 1.4.2.2 yamt unp->loc.loc.part_num == icbptr->loc.part_num) {
1614 1.4.2.2 yamt vp = unp->vnode;
1615 1.4.2.2 yamt assert(vp);
1616 1.4.2.2 yamt simple_lock(&vp->v_interlock);
1617 1.4.2.2 yamt simple_unlock(&ump->ihash_slock);
1618 1.4.2.2 yamt if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
1619 1.4.2.2 yamt goto loop;
1620 1.4.2.2 yamt return unp;
1621 1.4.2.2 yamt };
1622 1.4.2.2 yamt };
1623 1.4.2.2 yamt simple_unlock(&ump->ihash_slock);
1624 1.4.2.2 yamt
1625 1.4.2.2 yamt return NULL;
1626 1.4.2.2 yamt };
1627 1.4.2.2 yamt
1628 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1629 1.4.2.2 yamt
1630 1.4.2.2 yamt static void
1631 1.4.2.2 yamt udf_hashins(struct udf_node *unp)
1632 1.4.2.2 yamt {
1633 1.4.2.2 yamt struct udf_mount *ump;
1634 1.4.2.2 yamt uint32_t hashline;
1635 1.4.2.2 yamt
1636 1.4.2.2 yamt ump = unp->ump;
1637 1.4.2.2 yamt simple_lock(&ump->ihash_slock);
1638 1.4.2.2 yamt
1639 1.4.2.2 yamt hashline = udf_calchash(&unp->loc) & UDF_INODE_HASHMASK;
1640 1.4.2.2 yamt LIST_INSERT_HEAD(&ump->udf_nodes[hashline], unp, hashchain);
1641 1.4.2.2 yamt
1642 1.4.2.2 yamt simple_unlock(&ump->ihash_slock);
1643 1.4.2.2 yamt }
1644 1.4.2.2 yamt
1645 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1646 1.4.2.2 yamt
1647 1.4.2.2 yamt static void
1648 1.4.2.2 yamt udf_hashrem(struct udf_node *unp)
1649 1.4.2.2 yamt {
1650 1.4.2.2 yamt struct udf_mount *ump;
1651 1.4.2.2 yamt
1652 1.4.2.2 yamt ump = unp->ump;
1653 1.4.2.2 yamt simple_lock(&ump->ihash_slock);
1654 1.4.2.2 yamt
1655 1.4.2.2 yamt LIST_REMOVE(unp, hashchain);
1656 1.4.2.2 yamt
1657 1.4.2.2 yamt simple_unlock(&ump->ihash_slock);
1658 1.4.2.2 yamt }
1659 1.4.2.2 yamt
1660 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1661 1.4.2.2 yamt
1662 1.4.2.2 yamt int
1663 1.4.2.2 yamt udf_dispose_locked_node(struct udf_node *node)
1664 1.4.2.2 yamt {
1665 1.4.2.2 yamt if (!node)
1666 1.4.2.2 yamt return 0;
1667 1.4.2.2 yamt if (node->vnode)
1668 1.4.2.2 yamt VOP_UNLOCK(node->vnode, 0);
1669 1.4.2.2 yamt return udf_dispose_node(node);
1670 1.4.2.2 yamt }
1671 1.4.2.2 yamt
1672 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1673 1.4.2.2 yamt
1674 1.4.2.2 yamt int
1675 1.4.2.2 yamt udf_dispose_node(struct udf_node *node)
1676 1.4.2.2 yamt {
1677 1.4.2.2 yamt struct vnode *vp;
1678 1.4.2.2 yamt
1679 1.4.2.2 yamt DPRINTF(NODE, ("udf_dispose_node called on node %p\n", node));
1680 1.4.2.2 yamt if (!node) {
1681 1.4.2.2 yamt DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
1682 1.4.2.2 yamt return 0;
1683 1.4.2.2 yamt };
1684 1.4.2.2 yamt
1685 1.4.2.2 yamt vp = node->vnode;
1686 1.4.2.2 yamt
1687 1.4.2.2 yamt /* TODO extended attributes and streamdir */
1688 1.4.2.2 yamt
1689 1.4.2.2 yamt /* remove from our hash lookup table */
1690 1.4.2.2 yamt udf_hashrem(node);
1691 1.4.2.2 yamt
1692 1.4.2.2 yamt /* dissociate our udf_node from the vnode */
1693 1.4.2.2 yamt vp->v_data = NULL;
1694 1.4.2.2 yamt
1695 1.4.2.2 yamt /* free associated memory and the node itself */
1696 1.4.2.2 yamt if (node->fe)
1697 1.4.2.2 yamt pool_put(&node->ump->desc_pool, node->fe);
1698 1.4.2.2 yamt if (node->efe)
1699 1.4.2.2 yamt pool_put(&node->ump->desc_pool, node->efe);
1700 1.4.2.2 yamt pool_put(&udf_node_pool, node);
1701 1.4.2.2 yamt
1702 1.4.2.2 yamt return 0;
1703 1.4.2.2 yamt }
1704 1.4.2.2 yamt
1705 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1706 1.4.2.2 yamt
1707 1.4.2.2 yamt /*
1708 1.4.2.2 yamt * Genfs interfacing
1709 1.4.2.2 yamt *
1710 1.4.2.2 yamt * static const struct genfs_ops udffs_genfsops = {
1711 1.4.2.2 yamt * .gop_size = genfs_size,
1712 1.4.2.2 yamt * size of transfers
1713 1.4.2.2 yamt * .gop_alloc = udf_gop_alloc,
1714 1.4.2.2 yamt * unknown
1715 1.4.2.2 yamt * .gop_write = genfs_gop_write,
1716 1.4.2.2 yamt * putpages interface code
1717 1.4.2.2 yamt * .gop_markupdate = udf_gop_markupdate,
1718 1.4.2.2 yamt * set update/modify flags etc.
1719 1.4.2.2 yamt * };
1720 1.4.2.2 yamt */
1721 1.4.2.2 yamt
1722 1.4.2.2 yamt /*
1723 1.4.2.2 yamt * Genfs interface. These four functions are the only ones defined though not
1724 1.4.2.2 yamt * documented... great.... why is chosen for the `.' initialisers i dont know
1725 1.4.2.2 yamt * but other filingsystems seem to use it this way.
1726 1.4.2.2 yamt */
1727 1.4.2.2 yamt
1728 1.4.2.2 yamt static int
1729 1.4.2.2 yamt udf_gop_alloc(struct vnode *vp, off_t off, off_t len, int flags,
1730 1.4.2.2 yamt struct ucred *cred)
1731 1.4.2.2 yamt {
1732 1.4.2.2 yamt return 0;
1733 1.4.2.2 yamt }
1734 1.4.2.2 yamt
1735 1.4.2.2 yamt
1736 1.4.2.2 yamt static void
1737 1.4.2.2 yamt udf_gop_markupdate(struct vnode *vp, int flags)
1738 1.4.2.2 yamt {
1739 1.4.2.2 yamt struct udf_node *udf_node = VTOI(vp);
1740 1.4.2.2 yamt u_long mask;
1741 1.4.2.2 yamt
1742 1.4.2.2 yamt udf_node = udf_node; /* shut up gcc */
1743 1.4.2.2 yamt
1744 1.4.2.2 yamt mask = 0;
1745 1.4.2.2 yamt #ifdef notyet
1746 1.4.2.2 yamt if ((flags & GOP_UPDATE_ACCESSED) != 0) {
1747 1.4.2.2 yamt mask = UDF_SET_ACCESS;
1748 1.4.2.2 yamt }
1749 1.4.2.2 yamt if ((flags & GOP_UPDATE_MODIFIED) != 0) {
1750 1.4.2.2 yamt mask |= UDF_SET_UPDATE;
1751 1.4.2.2 yamt }
1752 1.4.2.2 yamt if (mask) {
1753 1.4.2.2 yamt udf_node->update_flag |= mask;
1754 1.4.2.2 yamt }
1755 1.4.2.2 yamt #endif
1756 1.4.2.2 yamt /* msdosfs doesn't do it, but shouldn't we update the times here? */
1757 1.4.2.2 yamt }
1758 1.4.2.2 yamt
1759 1.4.2.2 yamt
1760 1.4.2.2 yamt static const struct genfs_ops udf_genfsops = {
1761 1.4.2.2 yamt .gop_size = genfs_size,
1762 1.4.2.2 yamt .gop_alloc = udf_gop_alloc,
1763 1.4.2.2 yamt .gop_write = genfs_gop_write,
1764 1.4.2.2 yamt .gop_markupdate = udf_gop_markupdate,
1765 1.4.2.2 yamt };
1766 1.4.2.2 yamt
1767 1.4.2.2 yamt /* --------------------------------------------------------------------- */
1768 1.4.2.2 yamt
1769 1.4.2.2 yamt /*
1770 1.4.2.2 yamt * Each node can have an attached streamdir node though not
1771 1.4.2.2 yamt * recursively. These are otherwise known as named substreams/named
1772 1.4.2.2 yamt * extended attributes that have no size limitations.
1773 1.4.2.2 yamt *
1774 1.4.2.2 yamt * `Normal' extended attributes are indicated with a number and are recorded
1775 1.4.2.2 yamt * in either the fe/efe descriptor itself for small descriptors or recorded in
1776 1.4.2.2 yamt * the attached extended attribute file. Since this file can get fragmented,
1777 1.4.2.2 yamt * care ought to be taken.
1778 1.4.2.2 yamt */
1779 1.4.2.2 yamt
1780 1.4.2.2 yamt int
1781 1.4.2.2 yamt udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
1782 1.4.2.2 yamt struct udf_node **noderes)
1783 1.4.2.2 yamt {
1784 1.4.2.2 yamt union dscrptr *dscr, *tmpdscr;
1785 1.4.2.2 yamt struct udf_node *node;
1786 1.4.2.2 yamt struct vnode *nvp;
1787 1.4.2.2 yamt struct long_ad icb_loc;
1788 1.4.2.2 yamt extern int (**udf_vnodeop_p)(void *);
1789 1.4.2.2 yamt uint64_t file_size;
1790 1.4.2.2 yamt uint32_t lb_size, sector, dummy;
1791 1.4.2.2 yamt int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
1792 1.4.2.2 yamt int error;
1793 1.4.2.2 yamt
1794 1.4.2.2 yamt DPRINTF(NODE, ("udf_get_node called\n"));
1795 1.4.2.2 yamt *noderes = node = NULL;
1796 1.4.2.2 yamt
1797 1.4.2.2 yamt /* lock to disallow simultanious creation of same node */
1798 1.4.2.2 yamt lockmgr(&ump->get_node_lock, LK_EXCLUSIVE, NULL);
1799 1.4.2.2 yamt
1800 1.4.2.2 yamt DPRINTF(NODE, ("\tlookup in hash table\n"));
1801 1.4.2.2 yamt /* lookup in hash table */
1802 1.4.2.2 yamt assert(ump);
1803 1.4.2.2 yamt assert(node_icb_loc);
1804 1.4.2.2 yamt node = udf_hashget(ump, node_icb_loc);
1805 1.4.2.2 yamt if (node) {
1806 1.4.2.2 yamt DPRINTF(NODE, ("\tgot it from the hash!\n"));
1807 1.4.2.2 yamt /* vnode is returned locked */
1808 1.4.2.2 yamt *noderes = node;
1809 1.4.2.2 yamt lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
1810 1.4.2.2 yamt return 0;
1811 1.4.2.2 yamt };
1812 1.4.2.2 yamt
1813 1.4.2.2 yamt /* garbage check: translate node_icb_loc to sectornr */
1814 1.4.2.2 yamt error = udf_translate_vtop(ump, node_icb_loc, §or, &dummy);
1815 1.4.2.2 yamt if (error) {
1816 1.4.2.2 yamt /* no use, this will fail anyway */
1817 1.4.2.2 yamt lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
1818 1.4.2.2 yamt return EINVAL;
1819 1.4.2.2 yamt };
1820 1.4.2.2 yamt
1821 1.4.2.2 yamt /* build node (do initialise!) */
1822 1.4.2.2 yamt node = pool_get(&udf_node_pool, PR_WAITOK);
1823 1.4.2.2 yamt memset(node, 0, sizeof(struct udf_node));
1824 1.4.2.2 yamt
1825 1.4.2.2 yamt DPRINTF(NODE, ("\tget new vnode\n"));
1826 1.4.2.2 yamt /* give it a vnode */
1827 1.4.2.2 yamt error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp);
1828 1.4.2.2 yamt if (error) {
1829 1.4.2.2 yamt pool_put(&udf_node_pool, node);
1830 1.4.2.2 yamt lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
1831 1.4.2.2 yamt return error;
1832 1.4.2.2 yamt };
1833 1.4.2.2 yamt
1834 1.4.2.2 yamt /* allways return locked vnode */
1835 1.4.2.2 yamt if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) {
1836 1.4.2.2 yamt /* recycle vnode and unlock; simultanious will fail too */
1837 1.4.2.2 yamt ungetnewvnode(nvp);
1838 1.4.2.2 yamt lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
1839 1.4.2.2 yamt return error;
1840 1.4.2.2 yamt };
1841 1.4.2.2 yamt
1842 1.4.2.2 yamt /* initialise crosslinks, note location of fe/efe for hashing */
1843 1.4.2.2 yamt node->ump = ump;
1844 1.4.2.2 yamt node->vnode = nvp;
1845 1.4.2.2 yamt nvp->v_data = node;
1846 1.4.2.2 yamt node->loc = *node_icb_loc;
1847 1.4.2.2 yamt node->lockf = 0;
1848 1.4.2.2 yamt
1849 1.4.2.2 yamt /* insert into the hash lookup */
1850 1.4.2.2 yamt udf_hashins(node);
1851 1.4.2.2 yamt
1852 1.4.2.2 yamt /* safe to unlock, the entry is in the hash table, vnode is locked */
1853 1.4.2.2 yamt lockmgr(&ump->get_node_lock, LK_RELEASE, NULL);
1854 1.4.2.2 yamt
1855 1.4.2.2 yamt icb_loc = *node_icb_loc;
1856 1.4.2.2 yamt needs_indirect = 0;
1857 1.4.2.2 yamt strat4096 = 0;
1858 1.4.2.2 yamt udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
1859 1.4.2.2 yamt file_size = 0;
1860 1.4.2.2 yamt lb_size = udf_rw32(ump->logical_vol->lb_size);
1861 1.4.2.2 yamt
1862 1.4.2.2 yamt do {
1863 1.4.2.2 yamt error = udf_translate_vtop(ump, &icb_loc, §or, &dummy);
1864 1.4.2.2 yamt if (error)
1865 1.4.2.2 yamt break;
1866 1.4.2.2 yamt
1867 1.4.2.2 yamt /* try to read in fe/efe */
1868 1.4.2.2 yamt error = udf_read_descriptor(ump, sector, M_UDFTEMP, &tmpdscr);
1869 1.4.2.2 yamt
1870 1.4.2.2 yamt /* blank sector marks end of sequence, check this */
1871 1.4.2.2 yamt if ((tmpdscr == NULL) && (!strat4096))
1872 1.4.2.2 yamt error = ENOENT;
1873 1.4.2.2 yamt
1874 1.4.2.2 yamt /* break if read error or blank sector */
1875 1.4.2.2 yamt if (error || (tmpdscr == NULL))
1876 1.4.2.2 yamt break;
1877 1.4.2.2 yamt
1878 1.4.2.2 yamt /* process descriptor based on the descriptor type */
1879 1.4.2.2 yamt dscr_type = udf_rw16(tmpdscr->tag.id);
1880 1.4.2.2 yamt
1881 1.4.2.2 yamt /* if dealing with an indirect entry, follow the link */
1882 1.4.2.2 yamt if (dscr_type == TAGID_INDIRECT_ENTRY) {
1883 1.4.2.2 yamt needs_indirect = 0;
1884 1.4.2.2 yamt icb_loc = tmpdscr->inde.indirect_icb;
1885 1.4.2.2 yamt free(tmpdscr, M_UDFTEMP);
1886 1.4.2.2 yamt continue;
1887 1.4.2.2 yamt };
1888 1.4.2.2 yamt
1889 1.4.2.2 yamt /* only file entries and extended file entries allowed here */
1890 1.4.2.2 yamt if ((dscr_type != TAGID_FENTRY) &&
1891 1.4.2.2 yamt (dscr_type != TAGID_EXTFENTRY)) {
1892 1.4.2.2 yamt free(tmpdscr, M_UDFTEMP);
1893 1.4.2.2 yamt error = ENOENT;
1894 1.4.2.2 yamt break;
1895 1.4.2.2 yamt };
1896 1.4.2.2 yamt
1897 1.4.2.2 yamt /* get descriptor space from our pool */
1898 1.4.2.2 yamt KASSERT(udf_tagsize(tmpdscr, lb_size) == lb_size);
1899 1.4.2.2 yamt
1900 1.4.2.2 yamt dscr = pool_get(&ump->desc_pool, PR_WAITOK);
1901 1.4.2.2 yamt memcpy(dscr, tmpdscr, lb_size);
1902 1.4.2.2 yamt free(tmpdscr, M_UDFTEMP);
1903 1.4.2.2 yamt
1904 1.4.2.2 yamt /* record and process/update (ext)fentry */
1905 1.4.2.2 yamt if (dscr_type == TAGID_FENTRY) {
1906 1.4.2.2 yamt if (node->fe)
1907 1.4.2.2 yamt pool_put(&ump->desc_pool, node->fe);
1908 1.4.2.2 yamt node->fe = &dscr->fe;
1909 1.4.2.2 yamt strat = udf_rw16(node->fe->icbtag.strat_type);
1910 1.4.2.2 yamt udf_file_type = node->fe->icbtag.file_type;
1911 1.4.2.2 yamt file_size = udf_rw64(node->fe->inf_len);
1912 1.4.2.2 yamt } else {
1913 1.4.2.2 yamt if (node->efe)
1914 1.4.2.2 yamt pool_put(&ump->desc_pool, node->efe);
1915 1.4.2.2 yamt node->efe = &dscr->efe;
1916 1.4.2.2 yamt strat = udf_rw16(node->efe->icbtag.strat_type);
1917 1.4.2.2 yamt udf_file_type = node->efe->icbtag.file_type;
1918 1.4.2.2 yamt file_size = udf_rw64(node->efe->inf_len);
1919 1.4.2.2 yamt };
1920 1.4.2.2 yamt
1921 1.4.2.2 yamt /* check recording strategy (structure) */
1922 1.4.2.2 yamt
1923 1.4.2.2 yamt /*
1924 1.4.2.2 yamt * Strategy 4096 is a daisy linked chain terminating with an
1925 1.4.2.2 yamt * unrecorded sector or a TERM descriptor. The next
1926 1.4.2.2 yamt * descriptor is to be found in the sector that follows the
1927 1.4.2.2 yamt * current sector.
1928 1.4.2.2 yamt */
1929 1.4.2.2 yamt if (strat == 4096) {
1930 1.4.2.2 yamt strat4096 = 1;
1931 1.4.2.2 yamt needs_indirect = 1;
1932 1.4.2.2 yamt
1933 1.4.2.2 yamt icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
1934 1.4.2.2 yamt };
1935 1.4.2.2 yamt
1936 1.4.2.2 yamt /*
1937 1.4.2.2 yamt * Strategy 4 is the normal strategy and terminates, but if
1938 1.4.2.2 yamt * we're in strategy 4096, we can't have strategy 4 mixed in
1939 1.4.2.2 yamt */
1940 1.4.2.2 yamt
1941 1.4.2.2 yamt if (strat == 4) {
1942 1.4.2.2 yamt if (strat4096) {
1943 1.4.2.2 yamt error = EINVAL;
1944 1.4.2.2 yamt break;
1945 1.4.2.2 yamt };
1946 1.4.2.2 yamt break; /* done */
1947 1.4.2.2 yamt };
1948 1.4.2.2 yamt } while (!error);
1949 1.4.2.2 yamt
1950 1.4.2.2 yamt if (error) {
1951 1.4.2.2 yamt /* recycle udf_node */
1952 1.4.2.2 yamt udf_dispose_node(node);
1953 1.4.2.2 yamt
1954 1.4.2.2 yamt /* recycle vnode */
1955 1.4.2.2 yamt nvp->v_data = NULL;
1956 1.4.2.2 yamt ungetnewvnode(nvp);
1957 1.4.2.2 yamt
1958 1.4.2.2 yamt return EINVAL; /* error code ok? */
1959 1.4.2.2 yamt };
1960 1.4.2.2 yamt
1961 1.4.2.2 yamt /* post process and initialise node */
1962 1.4.2.2 yamt
1963 1.4.2.2 yamt /* assert no references to dscr anymore beyong this point */
1964 1.4.2.2 yamt assert((node->fe) || (node->efe));
1965 1.4.2.2 yamt dscr = NULL;
1966 1.4.2.2 yamt
1967 1.4.2.2 yamt /*
1968 1.4.2.2 yamt * Record where to record an updated version of the descriptor. If
1969 1.4.2.2 yamt * there is a sequence of indirect entries, icb_loc will have been
1970 1.4.2.2 yamt * updated. Its the write disipline to allocate new space and to make
1971 1.4.2.2 yamt * sure the chain is maintained.
1972 1.4.2.2 yamt *
1973 1.4.2.2 yamt * `needs_indirect' flags if the next location is to be filled with
1974 1.4.2.2 yamt * with an indirect entry.
1975 1.4.2.2 yamt */
1976 1.4.2.2 yamt node->next_loc = icb_loc;
1977 1.4.2.2 yamt node->needs_indirect = needs_indirect;
1978 1.4.2.2 yamt
1979 1.4.2.2 yamt /*
1980 1.4.2.2 yamt * Translate UDF filetypes into vnode types.
1981 1.4.2.2 yamt *
1982 1.4.2.2 yamt * Systemfiles like the meta main and mirror files are not treated as
1983 1.4.2.2 yamt * normal files, so we type them as having no type. UDF dictates that
1984 1.4.2.2 yamt * they are not allowed to be visible.
1985 1.4.2.2 yamt */
1986 1.4.2.2 yamt
1987 1.4.2.2 yamt /* TODO specfs, fifofs etc etc. vnops setting */
1988 1.4.2.2 yamt switch (udf_file_type) {
1989 1.4.2.2 yamt case UDF_ICB_FILETYPE_DIRECTORY :
1990 1.4.2.2 yamt case UDF_ICB_FILETYPE_STREAMDIR :
1991 1.4.2.2 yamt nvp->v_type = VDIR;
1992 1.4.2.2 yamt break;
1993 1.4.2.2 yamt case UDF_ICB_FILETYPE_BLOCKDEVICE :
1994 1.4.2.2 yamt nvp->v_type = VBLK;
1995 1.4.2.2 yamt break;
1996 1.4.2.2 yamt case UDF_ICB_FILETYPE_CHARDEVICE :
1997 1.4.2.2 yamt nvp->v_type = VCHR;
1998 1.4.2.2 yamt break;
1999 1.4.2.2 yamt case UDF_ICB_FILETYPE_SYMLINK :
2000 1.4.2.2 yamt nvp->v_type = VLNK;
2001 1.4.2.2 yamt break;
2002 1.4.2.2 yamt case UDF_ICB_FILETYPE_META_MAIN :
2003 1.4.2.2 yamt case UDF_ICB_FILETYPE_META_MIRROR :
2004 1.4.2.2 yamt nvp->v_type = VNON;
2005 1.4.2.2 yamt break;
2006 1.4.2.2 yamt case UDF_ICB_FILETYPE_RANDOMACCESS :
2007 1.4.2.2 yamt nvp->v_type = VREG;
2008 1.4.2.2 yamt break;
2009 1.4.2.2 yamt default:
2010 1.4.2.2 yamt /* YIKES, either a block/char device, fifo or something else */
2011 1.4.2.2 yamt nvp->v_type = VNON;
2012 1.4.2.2 yamt };
2013 1.4.2.2 yamt
2014 1.4.2.2 yamt /* initialise genfs */
2015 1.4.2.2 yamt genfs_node_init(nvp, &udf_genfsops);
2016 1.4.2.2 yamt
2017 1.4.2.2 yamt /* don't forget to set vnode's v_size */
2018 1.4.2.2 yamt nvp->v_size = file_size;
2019 1.4.2.2 yamt
2020 1.4.2.2 yamt /* TODO ext attr and streamdir nodes */
2021 1.4.2.2 yamt
2022 1.4.2.2 yamt *noderes = node;
2023 1.4.2.2 yamt
2024 1.4.2.2 yamt return 0;
2025 1.4.2.2 yamt }
2026 1.4.2.2 yamt
2027 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2028 1.4.2.2 yamt
2029 1.4.2.2 yamt /* UDF<->unix converters */
2030 1.4.2.2 yamt
2031 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2032 1.4.2.2 yamt
2033 1.4.2.2 yamt static mode_t
2034 1.4.2.2 yamt udf_perm_to_unix_mode(uint32_t perm)
2035 1.4.2.2 yamt {
2036 1.4.2.2 yamt mode_t mode;
2037 1.4.2.2 yamt
2038 1.4.2.2 yamt mode = ((perm & UDF_FENTRY_PERM_USER_MASK) );
2039 1.4.2.2 yamt mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK ) >> 2);
2040 1.4.2.2 yamt mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
2041 1.4.2.2 yamt
2042 1.4.2.2 yamt return mode;
2043 1.4.2.2 yamt }
2044 1.4.2.2 yamt
2045 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2046 1.4.2.2 yamt
2047 1.4.2.2 yamt #ifdef notyet
2048 1.4.2.2 yamt static uint32_t
2049 1.4.2.2 yamt unix_mode_to_udf_perm(mode_t mode)
2050 1.4.2.2 yamt {
2051 1.4.2.2 yamt uint32_t perm;
2052 1.4.2.2 yamt
2053 1.4.2.2 yamt perm = ((mode & S_IRWXO) );
2054 1.4.2.2 yamt perm |= ((mode & S_IRWXG) << 2);
2055 1.4.2.2 yamt perm |= ((mode & S_IRWXU) << 4);
2056 1.4.2.2 yamt perm |= ((mode & S_IWOTH) << 3);
2057 1.4.2.2 yamt perm |= ((mode & S_IWGRP) << 5);
2058 1.4.2.2 yamt perm |= ((mode & S_IWUSR) << 7);
2059 1.4.2.2 yamt
2060 1.4.2.2 yamt return perm;
2061 1.4.2.2 yamt }
2062 1.4.2.2 yamt #endif
2063 1.4.2.2 yamt
2064 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2065 1.4.2.2 yamt
2066 1.4.2.2 yamt static uint32_t
2067 1.4.2.2 yamt udf_icb_to_unix_filetype(uint32_t icbftype)
2068 1.4.2.2 yamt {
2069 1.4.2.2 yamt switch (icbftype) {
2070 1.4.2.2 yamt case UDF_ICB_FILETYPE_DIRECTORY :
2071 1.4.2.2 yamt case UDF_ICB_FILETYPE_STREAMDIR :
2072 1.4.2.2 yamt return S_IFDIR;
2073 1.4.2.2 yamt case UDF_ICB_FILETYPE_FIFO :
2074 1.4.2.2 yamt return S_IFIFO;
2075 1.4.2.2 yamt case UDF_ICB_FILETYPE_CHARDEVICE :
2076 1.4.2.2 yamt return S_IFCHR;
2077 1.4.2.2 yamt case UDF_ICB_FILETYPE_BLOCKDEVICE :
2078 1.4.2.2 yamt return S_IFBLK;
2079 1.4.2.2 yamt case UDF_ICB_FILETYPE_RANDOMACCESS :
2080 1.4.2.2 yamt return S_IFREG;
2081 1.4.2.2 yamt case UDF_ICB_FILETYPE_SYMLINK :
2082 1.4.2.2 yamt return S_IFLNK;
2083 1.4.2.2 yamt case UDF_ICB_FILETYPE_SOCKET :
2084 1.4.2.2 yamt return S_IFSOCK;
2085 1.4.2.2 yamt };
2086 1.4.2.2 yamt /* no idea what this is */
2087 1.4.2.2 yamt return 0;
2088 1.4.2.2 yamt }
2089 1.4.2.2 yamt
2090 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2091 1.4.2.2 yamt
2092 1.4.2.2 yamt /* TODO KNF-ify */
2093 1.4.2.2 yamt
2094 1.4.2.2 yamt void
2095 1.4.2.2 yamt udf_to_unix_name(char *result, char *id, int len, struct charspec *chsp)
2096 1.4.2.2 yamt {
2097 1.4.2.2 yamt uint16_t raw_name[1024], unix_name[1024];
2098 1.4.2.2 yamt uint16_t *inchp, ch;
2099 1.4.2.2 yamt uint8_t *outchp;
2100 1.4.2.2 yamt int ucode_chars, nice_uchars;
2101 1.4.2.2 yamt
2102 1.4.2.2 yamt assert(sizeof(char) == sizeof(uint8_t));
2103 1.4.2.2 yamt outchp = (uint8_t *) result;
2104 1.4.2.2 yamt if ((chsp->type == 0) && (strcmp((char*) chsp->inf, "OSTA Compressed Unicode") == 0)) {
2105 1.4.2.2 yamt *raw_name = *unix_name = 0;
2106 1.4.2.2 yamt ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
2107 1.4.2.2 yamt ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
2108 1.4.2.2 yamt nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
2109 1.4.2.2 yamt for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
2110 1.4.2.2 yamt ch = *inchp;
2111 1.4.2.2 yamt /* XXX sloppy unicode -> latin */
2112 1.4.2.2 yamt *outchp++ = ch & 255;
2113 1.4.2.2 yamt if (!ch) break;
2114 1.4.2.2 yamt };
2115 1.4.2.2 yamt *outchp++ = 0;
2116 1.4.2.2 yamt } else {
2117 1.4.2.2 yamt /* assume 8bit char length byte latin-1 */
2118 1.4.2.2 yamt assert(*id == 8);
2119 1.4.2.2 yamt strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
2120 1.4.2.2 yamt };
2121 1.4.2.2 yamt }
2122 1.4.2.2 yamt
2123 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2124 1.4.2.2 yamt
2125 1.4.2.2 yamt /* TODO KNF-ify */
2126 1.4.2.2 yamt
2127 1.4.2.2 yamt void
2128 1.4.2.2 yamt unix_to_udf_name(char *result, char *name,
2129 1.4.2.2 yamt uint8_t *result_len, struct charspec *chsp)
2130 1.4.2.2 yamt {
2131 1.4.2.2 yamt uint16_t raw_name[1024];
2132 1.4.2.2 yamt int udf_chars, name_len;
2133 1.4.2.2 yamt char *inchp;
2134 1.4.2.2 yamt uint16_t *outchp;
2135 1.4.2.2 yamt
2136 1.4.2.2 yamt /* convert latin-1 or whatever to unicode-16 */
2137 1.4.2.2 yamt *raw_name = 0;
2138 1.4.2.2 yamt name_len = 0;
2139 1.4.2.2 yamt inchp = name;
2140 1.4.2.2 yamt outchp = raw_name;
2141 1.4.2.2 yamt while (*inchp) {
2142 1.4.2.2 yamt *outchp++ = (uint16_t) (*inchp++);
2143 1.4.2.2 yamt name_len++;
2144 1.4.2.2 yamt };
2145 1.4.2.2 yamt
2146 1.4.2.2 yamt if ((chsp->type == 0) && (strcmp((char *) chsp->inf, "OSTA Compressed Unicode") == 0)) {
2147 1.4.2.2 yamt udf_chars = udf_CompressUnicode(name_len, 8, (unicode_t *) raw_name, (byte *) result);
2148 1.4.2.2 yamt } else {
2149 1.4.2.2 yamt /* XXX assume 8bit char length byte latin-1 */
2150 1.4.2.2 yamt *result++ = 8; udf_chars = 1;
2151 1.4.2.2 yamt strncpy(result, name + 1, strlen(name+1));
2152 1.4.2.2 yamt udf_chars += strlen(name);
2153 1.4.2.2 yamt };
2154 1.4.2.2 yamt *result_len = udf_chars;
2155 1.4.2.2 yamt }
2156 1.4.2.2 yamt
2157 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2158 1.4.2.2 yamt
2159 1.4.2.2 yamt /*
2160 1.4.2.2 yamt * Timestamp to timespec conversion code is taken with small modifications
2161 1.4.2.2 yamt * from FreeBSDs /sys/fs/udf by Scott Long <scottl (at) freebsd.org>. Added with
2162 1.4.2.2 yamt * permission from Scott.
2163 1.4.2.2 yamt */
2164 1.4.2.2 yamt
2165 1.4.2.2 yamt static int mon_lens[2][12] = {
2166 1.4.2.2 yamt {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
2167 1.4.2.2 yamt {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
2168 1.4.2.2 yamt };
2169 1.4.2.2 yamt
2170 1.4.2.2 yamt
2171 1.4.2.2 yamt static int
2172 1.4.2.2 yamt udf_isaleapyear(int year)
2173 1.4.2.2 yamt {
2174 1.4.2.2 yamt int i;
2175 1.4.2.2 yamt
2176 1.4.2.2 yamt i = (year % 4) ? 0 : 1;
2177 1.4.2.2 yamt i &= (year % 100) ? 1 : 0;
2178 1.4.2.2 yamt i |= (year % 400) ? 0 : 1;
2179 1.4.2.2 yamt
2180 1.4.2.2 yamt return i;
2181 1.4.2.2 yamt }
2182 1.4.2.2 yamt
2183 1.4.2.2 yamt
2184 1.4.2.2 yamt void
2185 1.4.2.2 yamt udf_timestamp_to_timespec(struct udf_mount *ump,
2186 1.4.2.2 yamt struct timestamp *timestamp,
2187 1.4.2.2 yamt struct timespec *timespec)
2188 1.4.2.2 yamt {
2189 1.4.2.2 yamt uint32_t usecs, secs, nsecs;
2190 1.4.2.2 yamt uint16_t tz;
2191 1.4.2.2 yamt int i, lpyear, daysinyear, year;
2192 1.4.2.2 yamt
2193 1.4.2.2 yamt timespec->tv_sec = secs = 0;
2194 1.4.2.2 yamt timespec->tv_nsec = nsecs = 0;
2195 1.4.2.2 yamt
2196 1.4.2.2 yamt /*
2197 1.4.2.2 yamt * DirectCD seems to like using bogus year values.
2198 1.4.2.2 yamt *
2199 1.4.2.2 yamt * Distrust time->month especially, since it will be used for an array
2200 1.4.2.2 yamt * index.
2201 1.4.2.2 yamt */
2202 1.4.2.2 yamt year = udf_rw16(timestamp->year);
2203 1.4.2.2 yamt if ((year < 1970) || (timestamp->month > 12)) {
2204 1.4.2.2 yamt return;
2205 1.4.2.2 yamt }
2206 1.4.2.2 yamt
2207 1.4.2.2 yamt /* Calculate the time and day
2208 1.4.2.2 yamt * Day is 1-31, Month is 1-12
2209 1.4.2.2 yamt */
2210 1.4.2.2 yamt
2211 1.4.2.2 yamt usecs = timestamp->usec +
2212 1.4.2.2 yamt 100*timestamp->hund_usec + 10000*timestamp->centisec;
2213 1.4.2.2 yamt nsecs = usecs * 1000;
2214 1.4.2.2 yamt secs = timestamp->second;
2215 1.4.2.2 yamt secs += timestamp->minute * 60;
2216 1.4.2.2 yamt secs += timestamp->hour * 3600;
2217 1.4.2.2 yamt secs += (timestamp->day-1) * 3600 * 24;
2218 1.4.2.2 yamt
2219 1.4.2.2 yamt /* Calclulate the month */
2220 1.4.2.2 yamt lpyear = udf_isaleapyear(year);
2221 1.4.2.2 yamt for (i = 1; i < timestamp->month; i++)
2222 1.4.2.2 yamt secs += mon_lens[lpyear][i-1] * 3600 * 24;
2223 1.4.2.2 yamt
2224 1.4.2.2 yamt for (i = 1970; i < year; i++) {
2225 1.4.2.2 yamt daysinyear = udf_isaleapyear(i) + 365 ;
2226 1.4.2.2 yamt secs += daysinyear * 3600 * 24;
2227 1.4.2.2 yamt }
2228 1.4.2.2 yamt
2229 1.4.2.2 yamt /*
2230 1.4.2.2 yamt * Calculate the time zone. The timezone is 12 bit signed 2's
2231 1.4.2.2 yamt * compliment, so we gotta do some extra magic to handle it right.
2232 1.4.2.2 yamt */
2233 1.4.2.2 yamt tz = udf_rw16(timestamp->type_tz);
2234 1.4.2.2 yamt tz &= 0x0fff; /* only lower 12 bits are significant */
2235 1.4.2.2 yamt if (tz & 0x0800) /* sign extention */
2236 1.4.2.2 yamt tz |= 0xf000;
2237 1.4.2.2 yamt
2238 1.4.2.2 yamt /* TODO check timezone conversion */
2239 1.4.2.2 yamt /* check if we are specified a timezone to convert */
2240 1.4.2.2 yamt if (udf_rw16(timestamp->type_tz) & 0x1000) {
2241 1.4.2.2 yamt if ((int16_t) tz != -2047)
2242 1.4.2.2 yamt secs -= (int16_t) tz * 60;
2243 1.4.2.2 yamt } else {
2244 1.4.2.2 yamt secs -= ump->mount_args.gmtoff;
2245 1.4.2.2 yamt };
2246 1.4.2.2 yamt
2247 1.4.2.2 yamt timespec->tv_sec = secs;
2248 1.4.2.2 yamt timespec->tv_nsec = nsecs;
2249 1.4.2.2 yamt }
2250 1.4.2.2 yamt
2251 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2252 1.4.2.2 yamt
2253 1.4.2.2 yamt /*
2254 1.4.2.2 yamt * Attribute and filetypes converters with get/set pairs
2255 1.4.2.2 yamt */
2256 1.4.2.2 yamt
2257 1.4.2.2 yamt uint32_t
2258 1.4.2.2 yamt udf_getaccessmode(struct udf_node *udf_node)
2259 1.4.2.2 yamt {
2260 1.4.2.2 yamt struct file_entry *fe;
2261 1.4.2.2 yamt struct extfile_entry *efe;
2262 1.4.2.2 yamt uint32_t udf_perm, icbftype;
2263 1.4.2.2 yamt uint32_t mode, ftype;
2264 1.4.2.2 yamt uint16_t icbflags;
2265 1.4.2.2 yamt
2266 1.4.2.2 yamt if (udf_node->fe) {
2267 1.4.2.2 yamt fe = udf_node->fe;
2268 1.4.2.2 yamt udf_perm = udf_rw32(fe->perm);
2269 1.4.2.2 yamt icbftype = fe->icbtag.file_type;
2270 1.4.2.2 yamt icbflags = udf_rw16(fe->icbtag.flags);
2271 1.4.2.2 yamt } else {
2272 1.4.2.2 yamt assert(udf_node->efe);
2273 1.4.2.2 yamt efe = udf_node->efe;
2274 1.4.2.2 yamt udf_perm = udf_rw32(efe->perm);
2275 1.4.2.2 yamt icbftype = efe->icbtag.file_type;
2276 1.4.2.2 yamt icbflags = udf_rw16(efe->icbtag.flags);
2277 1.4.2.2 yamt };
2278 1.4.2.2 yamt
2279 1.4.2.2 yamt mode = udf_perm_to_unix_mode(udf_perm);
2280 1.4.2.2 yamt ftype = udf_icb_to_unix_filetype(icbftype);
2281 1.4.2.2 yamt
2282 1.4.2.2 yamt /* set suid, sgid, sticky from flags in fe/efe */
2283 1.4.2.2 yamt if (icbflags & UDF_ICB_TAG_FLAGS_SETUID)
2284 1.4.2.2 yamt mode |= S_ISUID;
2285 1.4.2.2 yamt if (icbflags & UDF_ICB_TAG_FLAGS_SETGID)
2286 1.4.2.2 yamt mode |= S_ISGID;
2287 1.4.2.2 yamt if (icbflags & UDF_ICB_TAG_FLAGS_STICKY)
2288 1.4.2.2 yamt mode |= S_ISVTX;
2289 1.4.2.2 yamt
2290 1.4.2.2 yamt return mode | ftype;
2291 1.4.2.2 yamt }
2292 1.4.2.2 yamt
2293 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2294 1.4.2.2 yamt
2295 1.4.2.2 yamt /*
2296 1.4.2.2 yamt * Directory read and manipulation functions
2297 1.4.2.2 yamt */
2298 1.4.2.2 yamt
2299 1.4.2.2 yamt int
2300 1.4.2.2 yamt udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
2301 1.4.2.2 yamt struct long_ad *icb_loc)
2302 1.4.2.2 yamt {
2303 1.4.2.2 yamt struct udf_node *dir_node = VTOI(vp);
2304 1.4.2.2 yamt struct file_entry *fe;
2305 1.4.2.2 yamt struct extfile_entry *efe;
2306 1.4.2.2 yamt struct fileid_desc *fid;
2307 1.4.2.2 yamt struct dirent dirent;
2308 1.4.2.2 yamt uint64_t file_size, diroffset;
2309 1.4.2.2 yamt uint32_t lb_size;
2310 1.4.2.2 yamt int found, error;
2311 1.4.2.2 yamt
2312 1.4.2.2 yamt /* get directory filesize */
2313 1.4.2.2 yamt if (dir_node->fe) {
2314 1.4.2.2 yamt fe = dir_node->fe;
2315 1.4.2.2 yamt file_size = udf_rw64(fe->inf_len);
2316 1.4.2.2 yamt } else {
2317 1.4.2.2 yamt assert(dir_node->efe);
2318 1.4.2.2 yamt efe = dir_node->efe;
2319 1.4.2.2 yamt file_size = udf_rw64(efe->inf_len);
2320 1.4.2.2 yamt };
2321 1.4.2.2 yamt
2322 1.4.2.2 yamt /* allocate temporary space for fid */
2323 1.4.2.2 yamt lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
2324 1.4.2.2 yamt fid = malloc(lb_size, M_TEMP, M_WAITOK);
2325 1.4.2.2 yamt
2326 1.4.2.2 yamt found = 0;
2327 1.4.2.2 yamt diroffset = 0;
2328 1.4.2.2 yamt while (!found && (diroffset < file_size)) {
2329 1.4.2.2 yamt /* transfer a new fid/dirent */
2330 1.4.2.2 yamt error = udf_read_fid_stream(vp, &diroffset, fid, &dirent);
2331 1.4.2.2 yamt if (error)
2332 1.4.2.2 yamt break;
2333 1.4.2.2 yamt
2334 1.4.2.2 yamt /* skip deleted entries */
2335 1.4.2.2 yamt if (fid->file_char & UDF_FILE_CHAR_DEL)
2336 1.4.2.2 yamt continue;
2337 1.4.2.2 yamt
2338 1.4.2.2 yamt if ((strlen(dirent.d_name) == namelen) &&
2339 1.4.2.2 yamt (strncmp(dirent.d_name, name, namelen) == 0)) {
2340 1.4.2.2 yamt found = 1;
2341 1.4.2.2 yamt *icb_loc = fid->icb;
2342 1.4.2.2 yamt };
2343 1.4.2.2 yamt };
2344 1.4.2.2 yamt free(fid, M_TEMP);
2345 1.4.2.2 yamt
2346 1.4.2.2 yamt return found;
2347 1.4.2.2 yamt }
2348 1.4.2.2 yamt
2349 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2350 1.4.2.2 yamt
2351 1.4.2.2 yamt /*
2352 1.4.2.2 yamt * Read one fid and process it into a dirent and advance to the next (*fid)
2353 1.4.2.2 yamt * has to be allocated a logical block in size, (*dirent) struct dirent length
2354 1.4.2.2 yamt */
2355 1.4.2.2 yamt
2356 1.4.2.2 yamt int
2357 1.4.2.2 yamt udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
2358 1.4.2.2 yamt struct fileid_desc *fid, struct dirent *dirent)
2359 1.4.2.2 yamt {
2360 1.4.2.2 yamt struct udf_node *dir_node = VTOI(vp);
2361 1.4.2.2 yamt struct udf_mount *ump = dir_node->ump;
2362 1.4.2.2 yamt struct file_entry *fe;
2363 1.4.2.2 yamt struct extfile_entry *efe;
2364 1.4.2.2 yamt struct uio dir_uio;
2365 1.4.2.2 yamt struct iovec dir_iovec;
2366 1.4.2.2 yamt uint32_t entry_length, lb_size;
2367 1.4.2.2 yamt uint64_t file_size;
2368 1.4.2.2 yamt char *fid_name;
2369 1.4.2.2 yamt int enough, error;
2370 1.4.2.2 yamt
2371 1.4.2.2 yamt assert(fid);
2372 1.4.2.2 yamt assert(dirent);
2373 1.4.2.2 yamt assert(dir_node);
2374 1.4.2.2 yamt assert(offset);
2375 1.4.2.2 yamt assert(*offset != 1);
2376 1.4.2.2 yamt
2377 1.4.2.2 yamt DPRINTF(FIDS, ("read_fid_stream called\n"));
2378 1.4.2.2 yamt /* check if we're past the end of the directory */
2379 1.4.2.2 yamt if (dir_node->fe) {
2380 1.4.2.2 yamt fe = dir_node->fe;
2381 1.4.2.2 yamt file_size = udf_rw64(fe->inf_len);
2382 1.4.2.2 yamt } else {
2383 1.4.2.2 yamt assert(dir_node->efe);
2384 1.4.2.2 yamt efe = dir_node->efe;
2385 1.4.2.2 yamt file_size = udf_rw64(efe->inf_len);
2386 1.4.2.2 yamt };
2387 1.4.2.2 yamt if (*offset >= file_size)
2388 1.4.2.2 yamt return EINVAL;
2389 1.4.2.2 yamt
2390 1.4.2.2 yamt /* get maximum length of FID descriptor */
2391 1.4.2.2 yamt lb_size = udf_rw32(ump->logical_vol->lb_size);
2392 1.4.2.2 yamt
2393 1.4.2.2 yamt /* initialise return values */
2394 1.4.2.2 yamt entry_length = 0;
2395 1.4.2.2 yamt memset(dirent, 0, sizeof(struct dirent));
2396 1.4.2.2 yamt memset(fid, 0, lb_size);
2397 1.4.2.2 yamt
2398 1.4.2.2 yamt /* TODO use vn_rdwr instead of creating our own uio */
2399 1.4.2.2 yamt /* read part of the directory */
2400 1.4.2.2 yamt memset(&dir_uio, 0, sizeof(struct uio));
2401 1.4.2.2 yamt dir_uio.uio_rw = UIO_READ; /* read into this space */
2402 1.4.2.2 yamt dir_uio.uio_iovcnt = 1;
2403 1.4.2.2 yamt dir_uio.uio_iov = &dir_iovec;
2404 1.4.2.3 yamt UIO_SETUP_SYSSPACE(&dir_uio);
2405 1.4.2.2 yamt dir_iovec.iov_base = fid;
2406 1.4.2.2 yamt dir_iovec.iov_len = lb_size;
2407 1.4.2.2 yamt dir_uio.uio_offset = *offset;
2408 1.4.2.2 yamt
2409 1.4.2.2 yamt /* limit length of read in piece */
2410 1.4.2.2 yamt dir_uio.uio_resid = MIN(file_size - (*offset), lb_size);
2411 1.4.2.2 yamt
2412 1.4.2.2 yamt /* read the part into the fid space */
2413 1.4.2.2 yamt error = VOP_READ(vp, &dir_uio, IO_ALTSEMANTICS, NOCRED);
2414 1.4.2.2 yamt if (error)
2415 1.4.2.2 yamt return error;
2416 1.4.2.2 yamt
2417 1.4.2.2 yamt /*
2418 1.4.2.2 yamt * Check if we got a whole descriptor.
2419 1.4.2.2 yamt * XXX Try to `resync' directory stream when something is very wrong.
2420 1.4.2.2 yamt *
2421 1.4.2.2 yamt */
2422 1.4.2.2 yamt enough = (dir_uio.uio_offset - (*offset) >= UDF_FID_SIZE);
2423 1.4.2.2 yamt if (!enough) {
2424 1.4.2.2 yamt /* short dir ... */
2425 1.4.2.2 yamt return EIO;
2426 1.4.2.2 yamt };
2427 1.4.2.2 yamt
2428 1.4.2.2 yamt /* check if our FID header is OK */
2429 1.4.2.2 yamt error = udf_check_tag(fid);
2430 1.4.2.2 yamt DPRINTFIF(FIDS, error, ("read fids: tag check failed\n"));
2431 1.4.2.2 yamt if (!error) {
2432 1.4.2.2 yamt if (udf_rw16(fid->tag.id) != TAGID_FID)
2433 1.4.2.2 yamt error = ENOENT;
2434 1.4.2.2 yamt };
2435 1.4.2.2 yamt DPRINTFIF(FIDS, !error, ("\ttag checked ok: got TAGID_FID\n"));
2436 1.4.2.2 yamt
2437 1.4.2.2 yamt /* check for length */
2438 1.4.2.2 yamt if (!error) {
2439 1.4.2.2 yamt entry_length = udf_fidsize(fid, lb_size);
2440 1.4.2.2 yamt enough = (dir_uio.uio_offset - (*offset) >= entry_length);
2441 1.4.2.2 yamt };
2442 1.4.2.2 yamt DPRINTFIF(FIDS, !error, ("\tentry_length = %d, enough = %s\n",
2443 1.4.2.2 yamt entry_length, enough?"yes":"no"));
2444 1.4.2.2 yamt
2445 1.4.2.2 yamt if (!enough) {
2446 1.4.2.2 yamt /* short dir ... bomb out */
2447 1.4.2.2 yamt return EIO;
2448 1.4.2.2 yamt };
2449 1.4.2.2 yamt
2450 1.4.2.2 yamt /* check FID contents */
2451 1.4.2.2 yamt if (!error) {
2452 1.4.2.2 yamt error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
2453 1.4.2.2 yamt DPRINTF(FIDS, ("\tpayload checked ok\n"));
2454 1.4.2.2 yamt };
2455 1.4.2.2 yamt if (error) {
2456 1.4.2.2 yamt /* note that is sometimes a bit quick to report */
2457 1.4.2.2 yamt printf("BROKEN DIRECTORY ENTRY\n");
2458 1.4.2.2 yamt /* RESYNC? */
2459 1.4.2.2 yamt /* TODO: use udf_resync_fid_stream */
2460 1.4.2.2 yamt return EIO;
2461 1.4.2.2 yamt };
2462 1.4.2.2 yamt DPRINTF(FIDS, ("\tinterpret FID\n"));
2463 1.4.2.2 yamt
2464 1.4.2.2 yamt /* we got a whole and valid descriptor! */
2465 1.4.2.2 yamt
2466 1.4.2.2 yamt /* create resulting dirent structure */
2467 1.4.2.2 yamt fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
2468 1.4.2.2 yamt udf_to_unix_name(dirent->d_name,
2469 1.4.2.2 yamt fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
2470 1.4.2.2 yamt
2471 1.4.2.2 yamt /* '..' has no name, so provide one */
2472 1.4.2.2 yamt if (fid->file_char & UDF_FILE_CHAR_PAR)
2473 1.4.2.2 yamt strcpy(dirent->d_name, "..");
2474 1.4.2.2 yamt
2475 1.4.2.2 yamt dirent->d_fileno = udf_calchash(&fid->icb); /* inode hash XXX */
2476 1.4.2.2 yamt dirent->d_namlen = strlen(dirent->d_name);
2477 1.4.2.2 yamt dirent->d_reclen = _DIRENT_SIZE(dirent);
2478 1.4.2.2 yamt
2479 1.4.2.2 yamt /*
2480 1.4.2.2 yamt * Note that its not worth trying to go for the filetypes now... its
2481 1.4.2.2 yamt * too expensive too
2482 1.4.2.2 yamt */
2483 1.4.2.2 yamt dirent->d_type = DT_UNKNOWN;
2484 1.4.2.2 yamt
2485 1.4.2.2 yamt /* initial guess for filetype we can make */
2486 1.4.2.2 yamt if (fid->file_char & UDF_FILE_CHAR_DIR)
2487 1.4.2.2 yamt dirent->d_type = DT_DIR;
2488 1.4.2.2 yamt
2489 1.4.2.2 yamt /* advance */
2490 1.4.2.2 yamt *offset += entry_length;
2491 1.4.2.2 yamt
2492 1.4.2.2 yamt return error;
2493 1.4.2.2 yamt }
2494 1.4.2.2 yamt
2495 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2496 1.4.2.2 yamt
2497 1.4.2.2 yamt /*
2498 1.4.2.2 yamt * block based file reading and writing
2499 1.4.2.2 yamt */
2500 1.4.2.2 yamt
2501 1.4.2.2 yamt static int
2502 1.4.2.2 yamt udf_read_internal(struct udf_node *node, uint8_t *blob)
2503 1.4.2.2 yamt {
2504 1.4.2.2 yamt struct udf_mount *ump;
2505 1.4.2.2 yamt struct file_entry *fe;
2506 1.4.2.2 yamt struct extfile_entry *efe;
2507 1.4.2.2 yamt uint64_t inflen;
2508 1.4.2.2 yamt uint32_t sector_size;
2509 1.4.2.2 yamt uint8_t *pos;
2510 1.4.2.2 yamt int icbflags, addr_type;
2511 1.4.2.2 yamt
2512 1.4.2.2 yamt /* shut up gcc */
2513 1.4.2.2 yamt inflen = addr_type = icbflags = 0;
2514 1.4.2.2 yamt pos = NULL;
2515 1.4.2.2 yamt
2516 1.4.2.2 yamt /* get extent and do some paranoia checks */
2517 1.4.2.2 yamt ump = node->ump;
2518 1.4.2.2 yamt sector_size = ump->discinfo.sector_size;
2519 1.4.2.2 yamt
2520 1.4.2.2 yamt fe = node->fe;
2521 1.4.2.2 yamt efe = node->efe;
2522 1.4.2.2 yamt if (fe) {
2523 1.4.2.2 yamt inflen = udf_rw64(fe->inf_len);
2524 1.4.2.2 yamt pos = &fe->data[0] + udf_rw32(fe->l_ea);
2525 1.4.2.2 yamt icbflags = udf_rw16(fe->icbtag.flags);
2526 1.4.2.2 yamt };
2527 1.4.2.2 yamt if (efe) {
2528 1.4.2.2 yamt inflen = udf_rw64(efe->inf_len);
2529 1.4.2.2 yamt pos = &efe->data[0] + udf_rw32(efe->l_ea);
2530 1.4.2.2 yamt icbflags = udf_rw16(efe->icbtag.flags);
2531 1.4.2.2 yamt };
2532 1.4.2.2 yamt addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2533 1.4.2.2 yamt
2534 1.4.2.2 yamt assert(addr_type == UDF_ICB_INTERN_ALLOC);
2535 1.4.2.2 yamt assert(inflen < sector_size);
2536 1.4.2.2 yamt
2537 1.4.2.2 yamt /* copy out info */
2538 1.4.2.2 yamt memset(blob, 0, sector_size);
2539 1.4.2.2 yamt memcpy(blob, pos, inflen);
2540 1.4.2.2 yamt
2541 1.4.2.2 yamt return 0;
2542 1.4.2.2 yamt }
2543 1.4.2.2 yamt
2544 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2545 1.4.2.2 yamt
2546 1.4.2.2 yamt /*
2547 1.4.2.2 yamt * Read file extent reads an extent specified in sectors from the file. It is
2548 1.4.2.2 yamt * sector based; i.e. no `fancy' offsets.
2549 1.4.2.2 yamt */
2550 1.4.2.2 yamt
2551 1.4.2.2 yamt int
2552 1.4.2.2 yamt udf_read_file_extent(struct udf_node *node,
2553 1.4.2.2 yamt uint32_t from, uint32_t sectors,
2554 1.4.2.2 yamt uint8_t *blob)
2555 1.4.2.2 yamt {
2556 1.4.2.2 yamt struct buf buf;
2557 1.4.2.2 yamt uint32_t sector_size;
2558 1.4.2.2 yamt
2559 1.4.2.2 yamt BUF_INIT(&buf);
2560 1.4.2.2 yamt
2561 1.4.2.2 yamt sector_size = node->ump->discinfo.sector_size;
2562 1.4.2.2 yamt
2563 1.4.2.2 yamt buf.b_bufsize = sectors * sector_size;
2564 1.4.2.2 yamt buf.b_data = blob;
2565 1.4.2.2 yamt buf.b_bcount = buf.b_bufsize;
2566 1.4.2.2 yamt buf.b_resid = buf.b_bcount;
2567 1.4.2.2 yamt buf.b_flags = B_BUSY | B_READ;
2568 1.4.2.2 yamt buf.b_vp = node->vnode;
2569 1.4.2.2 yamt buf.b_proc = NULL;
2570 1.4.2.2 yamt
2571 1.4.2.2 yamt buf.b_blkno = from;
2572 1.4.2.2 yamt buf.b_lblkno = 0;
2573 1.4.2.2 yamt BIO_SETPRIO(&buf, BPRIO_TIMELIMITED);
2574 1.4.2.2 yamt
2575 1.4.2.2 yamt udf_read_filebuf(node, &buf);
2576 1.4.2.2 yamt return biowait(&buf);
2577 1.4.2.2 yamt }
2578 1.4.2.2 yamt
2579 1.4.2.2 yamt
2580 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2581 1.4.2.2 yamt
2582 1.4.2.2 yamt /*
2583 1.4.2.2 yamt * Read file extent in the buffer.
2584 1.4.2.2 yamt *
2585 1.4.2.2 yamt * The splitup of the extent into seperate request-buffers is to minimise
2586 1.4.2.2 yamt * copying around as much as possible.
2587 1.4.2.2 yamt */
2588 1.4.2.2 yamt
2589 1.4.2.2 yamt
2590 1.4.2.2 yamt /* mininum of 128 translations (!) (64 kb in 512 byte sectors) */
2591 1.4.2.2 yamt #define FILEBUFSECT 128
2592 1.4.2.2 yamt
2593 1.4.2.2 yamt void
2594 1.4.2.2 yamt udf_read_filebuf(struct udf_node *node, struct buf *buf)
2595 1.4.2.2 yamt {
2596 1.4.2.2 yamt struct buf *nestbuf;
2597 1.4.2.2 yamt uint64_t mapping[FILEBUFSECT];
2598 1.4.2.2 yamt uint64_t run_start;
2599 1.4.2.2 yamt uint32_t sector_size;
2600 1.4.2.2 yamt uint32_t buf_offset, sector, rbuflen, rblk;
2601 1.4.2.2 yamt uint8_t *buf_pos;
2602 1.4.2.2 yamt int error, run_length;
2603 1.4.2.2 yamt
2604 1.4.2.2 yamt uint32_t from;
2605 1.4.2.2 yamt uint32_t sectors;
2606 1.4.2.2 yamt
2607 1.4.2.2 yamt sector_size = node->ump->discinfo.sector_size;
2608 1.4.2.2 yamt
2609 1.4.2.2 yamt from = buf->b_blkno;
2610 1.4.2.2 yamt sectors = buf->b_bcount / sector_size;
2611 1.4.2.2 yamt
2612 1.4.2.2 yamt /* assure we have enough translation slots */
2613 1.4.2.2 yamt KASSERT(buf->b_bcount / sector_size <= FILEBUFSECT);
2614 1.4.2.2 yamt KASSERT(MAXPHYS / sector_size <= FILEBUFSECT);
2615 1.4.2.2 yamt
2616 1.4.2.2 yamt if (sectors > FILEBUFSECT) {
2617 1.4.2.2 yamt printf("udf_read_filebuf: implementation limit on bufsize\n");
2618 1.4.2.2 yamt buf->b_error = EIO;
2619 1.4.2.2 yamt buf->b_flags |= B_ERROR;
2620 1.4.2.2 yamt biodone(buf);
2621 1.4.2.2 yamt return;
2622 1.4.2.2 yamt };
2623 1.4.2.2 yamt
2624 1.4.2.2 yamt error = 0;
2625 1.4.2.2 yamt DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
2626 1.4.2.2 yamt error = udf_translate_file_extent(node, from, sectors, mapping);
2627 1.4.2.2 yamt if (error) {
2628 1.4.2.2 yamt buf->b_error = error;
2629 1.4.2.2 yamt buf->b_flags |= B_ERROR;
2630 1.4.2.2 yamt biodone(buf);
2631 1.4.2.2 yamt return;
2632 1.4.2.2 yamt };
2633 1.4.2.2 yamt DPRINTF(READ, ("\ttranslate extent went OK\n"));
2634 1.4.2.2 yamt
2635 1.4.2.2 yamt /* pre-check if internal or parts are zero */
2636 1.4.2.2 yamt if (*mapping == UDF_TRANS_INTERN) {
2637 1.4.2.2 yamt error = udf_read_internal(node, (uint8_t *) buf->b_data);
2638 1.4.2.2 yamt if (error) {
2639 1.4.2.2 yamt buf->b_error = error;
2640 1.4.2.2 yamt buf->b_flags |= B_ERROR;
2641 1.4.2.2 yamt };
2642 1.4.2.2 yamt biodone(buf);
2643 1.4.2.2 yamt return;
2644 1.4.2.2 yamt };
2645 1.4.2.2 yamt DPRINTF(READ, ("\tnot intern\n"));
2646 1.4.2.2 yamt
2647 1.4.2.2 yamt /* request read-in of data from disc sheduler */
2648 1.4.2.2 yamt buf->b_resid = buf->b_bcount;
2649 1.4.2.2 yamt for (sector = 0; sector < sectors; sector++) {
2650 1.4.2.2 yamt buf_offset = sector * sector_size;
2651 1.4.2.2 yamt buf_pos = (uint8_t *) buf->b_data + buf_offset;
2652 1.4.2.2 yamt DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
2653 1.4.2.2 yamt
2654 1.4.2.2 yamt switch (mapping[sector]) {
2655 1.4.2.2 yamt case UDF_TRANS_UNMAPPED:
2656 1.4.2.2 yamt case UDF_TRANS_ZERO:
2657 1.4.2.2 yamt /* copy zero sector */
2658 1.4.2.2 yamt memset(buf_pos, 0, sector_size);
2659 1.4.2.2 yamt DPRINTF(READ, ("\treturning zero sector\n"));
2660 1.4.2.2 yamt nestiobuf_done(buf, sector_size, 0);
2661 1.4.2.2 yamt break;
2662 1.4.2.2 yamt default :
2663 1.4.2.2 yamt DPRINTF(READ, ("\tread sector "
2664 1.4.2.2 yamt "%"PRIu64"\n", mapping[sector]));
2665 1.4.2.2 yamt
2666 1.4.2.2 yamt run_start = mapping[sector];
2667 1.4.2.2 yamt run_length = 1;
2668 1.4.2.2 yamt while (sector < sectors-1) {
2669 1.4.2.2 yamt if (mapping[sector+1] != mapping[sector]+1)
2670 1.4.2.2 yamt break;
2671 1.4.2.2 yamt run_length++;
2672 1.4.2.2 yamt sector++;
2673 1.4.2.2 yamt };
2674 1.4.2.2 yamt
2675 1.4.2.2 yamt /*
2676 1.4.2.2 yamt * nest an iobuf and mark it for async reading. Since
2677 1.4.2.2 yamt * we're using nested buffers, they can't be cached by
2678 1.4.2.2 yamt * design.
2679 1.4.2.2 yamt */
2680 1.4.2.2 yamt rbuflen = run_length * sector_size;
2681 1.4.2.2 yamt rblk = run_start * (sector_size/DEV_BSIZE);
2682 1.4.2.2 yamt
2683 1.4.2.2 yamt nestbuf = getiobuf();
2684 1.4.2.2 yamt nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
2685 1.4.2.2 yamt /* nestbuf is B_ASYNC */
2686 1.4.2.2 yamt
2687 1.4.2.2 yamt /* CD shedules on raw blkno */
2688 1.4.2.2 yamt nestbuf->b_blkno = rblk;
2689 1.4.2.2 yamt nestbuf->b_proc = NULL;
2690 1.4.2.2 yamt nestbuf->b_cylinder = 0;
2691 1.4.2.2 yamt nestbuf->b_rawblkno = rblk;
2692 1.4.2.2 yamt VOP_STRATEGY(node->ump->devvp, nestbuf);
2693 1.4.2.2 yamt };
2694 1.4.2.2 yamt };
2695 1.4.2.2 yamt DPRINTF(READ, ("\tend of read_filebuf\n"));
2696 1.4.2.2 yamt }
2697 1.4.2.2 yamt #undef FILEBUFSECT
2698 1.4.2.2 yamt
2699 1.4.2.2 yamt
2700 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2701 1.4.2.2 yamt
2702 1.4.2.2 yamt /*
2703 1.4.2.2 yamt * Translate an extent (in sectors) into sector numbers; used for read and
2704 1.4.2.2 yamt * write operations. DOESNT't check extents.
2705 1.4.2.2 yamt */
2706 1.4.2.2 yamt
2707 1.4.2.2 yamt int
2708 1.4.2.2 yamt udf_translate_file_extent(struct udf_node *node,
2709 1.4.2.2 yamt uint32_t from, uint32_t pages,
2710 1.4.2.2 yamt uint64_t *map)
2711 1.4.2.2 yamt {
2712 1.4.2.2 yamt struct udf_mount *ump;
2713 1.4.2.2 yamt struct file_entry *fe;
2714 1.4.2.2 yamt struct extfile_entry *efe;
2715 1.4.2.2 yamt struct short_ad *s_ad;
2716 1.4.2.2 yamt struct long_ad *l_ad, t_ad;
2717 1.4.2.2 yamt uint64_t transsec;
2718 1.4.2.2 yamt uint32_t sector_size, transsec32;
2719 1.4.2.2 yamt uint32_t overlap, translen;
2720 1.4.2.2 yamt uint32_t vpart_num, lb_num, len, alloclen;
2721 1.4.2.2 yamt uint8_t *pos;
2722 1.4.2.2 yamt int error, flags, addr_type, icblen, icbflags;
2723 1.4.2.2 yamt
2724 1.4.2.2 yamt if (!node)
2725 1.4.2.2 yamt return ENOENT;
2726 1.4.2.2 yamt
2727 1.4.2.2 yamt /* shut up gcc */
2728 1.4.2.2 yamt alloclen = addr_type = icbflags = 0;
2729 1.4.2.2 yamt pos = NULL;
2730 1.4.2.2 yamt
2731 1.4.2.2 yamt /* do the work */
2732 1.4.2.2 yamt ump = node->ump;
2733 1.4.2.2 yamt sector_size = ump->discinfo.sector_size;
2734 1.4.2.2 yamt fe = node->fe;
2735 1.4.2.2 yamt efe = node->efe;
2736 1.4.2.2 yamt if (fe) {
2737 1.4.2.2 yamt alloclen = udf_rw32(fe->l_ad);
2738 1.4.2.2 yamt pos = &fe->data[0] + udf_rw32(fe->l_ea);
2739 1.4.2.2 yamt icbflags = udf_rw16(fe->icbtag.flags);
2740 1.4.2.2 yamt };
2741 1.4.2.2 yamt if (efe) {
2742 1.4.2.2 yamt alloclen = udf_rw32(efe->l_ad);
2743 1.4.2.2 yamt pos = &efe->data[0] + udf_rw32(efe->l_ea);
2744 1.4.2.2 yamt icbflags = udf_rw16(efe->icbtag.flags);
2745 1.4.2.2 yamt };
2746 1.4.2.2 yamt addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2747 1.4.2.2 yamt
2748 1.4.2.2 yamt DPRINTF(TRANSLATE, ("udf trans: alloc_len = %d, addr_type %d, "
2749 1.4.2.2 yamt "fe %p, efe %p\n", alloclen, addr_type, fe, efe));
2750 1.4.2.2 yamt
2751 1.4.2.2 yamt vpart_num = udf_rw16(node->loc.loc.part_num);
2752 1.4.2.2 yamt lb_num = len = icblen = 0; /* shut up gcc */
2753 1.4.2.2 yamt while (pages && alloclen) {
2754 1.4.2.2 yamt DPRINTF(TRANSLATE, ("\taddr_type %d\n", addr_type));
2755 1.4.2.2 yamt switch (addr_type) {
2756 1.4.2.2 yamt case UDF_ICB_INTERN_ALLOC :
2757 1.4.2.2 yamt /* TODO check extents? */
2758 1.4.2.2 yamt *map = UDF_TRANS_INTERN;
2759 1.4.2.2 yamt return 0;
2760 1.4.2.2 yamt case UDF_ICB_SHORT_ALLOC :
2761 1.4.2.2 yamt icblen = sizeof(struct short_ad);
2762 1.4.2.2 yamt s_ad = (struct short_ad *) pos;
2763 1.4.2.2 yamt len = udf_rw32(s_ad->len);
2764 1.4.2.2 yamt lb_num = udf_rw32(s_ad->lb_num);
2765 1.4.2.2 yamt break;
2766 1.4.2.2 yamt case UDF_ICB_LONG_ALLOC :
2767 1.4.2.2 yamt icblen = sizeof(struct long_ad);
2768 1.4.2.2 yamt l_ad = (struct long_ad *) pos;
2769 1.4.2.2 yamt len = udf_rw32(l_ad->len);
2770 1.4.2.2 yamt lb_num = udf_rw32(l_ad->loc.lb_num);
2771 1.4.2.2 yamt vpart_num = udf_rw16(l_ad->loc.part_num);
2772 1.4.2.2 yamt DPRINTFIF(TRANSLATE,
2773 1.4.2.2 yamt (l_ad->impl.im_used.flags &
2774 1.4.2.2 yamt UDF_ADIMP_FLAGS_EXTENT_ERASED),
2775 1.4.2.2 yamt ("UDF: got an `extent erased' flag in long_ad\n"));
2776 1.4.2.2 yamt break;
2777 1.4.2.2 yamt default:
2778 1.4.2.2 yamt /* can't be here */
2779 1.4.2.2 yamt return EINVAL; /* for sure */
2780 1.4.2.2 yamt };
2781 1.4.2.2 yamt
2782 1.4.2.2 yamt /* process extent */
2783 1.4.2.2 yamt flags = UDF_EXT_FLAGS(len);
2784 1.4.2.2 yamt len = UDF_EXT_LEN(len);
2785 1.4.2.2 yamt
2786 1.4.2.2 yamt overlap = (len + sector_size -1) / sector_size;
2787 1.4.2.2 yamt if (from) {
2788 1.4.2.2 yamt if (from > overlap) {
2789 1.4.2.2 yamt from -= overlap;
2790 1.4.2.2 yamt overlap = 0;
2791 1.4.2.2 yamt } else {
2792 1.4.2.2 yamt lb_num += from; /* advance in extent */
2793 1.4.2.2 yamt overlap -= from;
2794 1.4.2.2 yamt from = 0;
2795 1.4.2.2 yamt };
2796 1.4.2.2 yamt };
2797 1.4.2.2 yamt
2798 1.4.2.2 yamt overlap = MIN(overlap, pages);
2799 1.4.2.2 yamt while (overlap) {
2800 1.4.2.2 yamt switch (flags) {
2801 1.4.2.2 yamt case UDF_EXT_REDIRECT :
2802 1.4.2.2 yamt /* no support for allocation extentions yet */
2803 1.4.2.2 yamt /* TODO support for allocation extention */
2804 1.4.2.2 yamt return ENOENT;
2805 1.4.2.2 yamt case UDF_EXT_FREED :
2806 1.4.2.2 yamt case UDF_EXT_FREE :
2807 1.4.2.2 yamt transsec = UDF_TRANS_ZERO;
2808 1.4.2.2 yamt translen = overlap;
2809 1.4.2.2 yamt while (overlap && pages && translen) {
2810 1.4.2.2 yamt *map++ = transsec;
2811 1.4.2.2 yamt overlap--; pages--; translen--;
2812 1.4.2.2 yamt };
2813 1.4.2.2 yamt break;
2814 1.4.2.2 yamt case UDF_EXT_ALLOCATED :
2815 1.4.2.2 yamt t_ad.loc.lb_num = udf_rw32(lb_num);
2816 1.4.2.2 yamt t_ad.loc.part_num = udf_rw16(vpart_num);
2817 1.4.2.2 yamt error = udf_translate_vtop(ump,
2818 1.4.2.2 yamt &t_ad, &transsec32, &translen);
2819 1.4.2.2 yamt transsec = transsec32;
2820 1.4.2.2 yamt if (error)
2821 1.4.2.2 yamt return error;
2822 1.4.2.2 yamt while (overlap && pages && translen) {
2823 1.4.2.2 yamt *map++ = transsec;
2824 1.4.2.2 yamt transsec++;
2825 1.4.2.2 yamt overlap--; pages--; translen--;
2826 1.4.2.2 yamt };
2827 1.4.2.2 yamt break;
2828 1.4.2.2 yamt };
2829 1.4.2.2 yamt };
2830 1.4.2.2 yamt pos += icblen;
2831 1.4.2.2 yamt alloclen -= icblen;
2832 1.4.2.2 yamt };
2833 1.4.2.2 yamt return 0;
2834 1.4.2.2 yamt }
2835 1.4.2.2 yamt
2836 1.4.2.2 yamt /* --------------------------------------------------------------------- */
2837 1.4.2.2 yamt
2838