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