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