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