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