udf_subr.c revision 1.39 1 /* $NetBSD: udf_subr.c,v 1.39 2007/10/10 20:42:25 ad 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.39 2007/10/10 20:42:25 ad 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 }
1311 vat_loc--; /* walk backwards */
1312 } while (vat_loc >= early_vat_loc);
1313
1314 /* we don't need our VAT node anymore */
1315 if (vat_node) {
1316 vput(vat_node->vnode);
1317 udf_dispose_node(vat_node);
1318 }
1319
1320 return error;
1321 }
1322
1323 /* --------------------------------------------------------------------- */
1324
1325 static int
1326 udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
1327 {
1328 union dscrptr *dscr;
1329 struct part_map_spare *pms = &mapping->pms;
1330 uint32_t lb_num;
1331 int spar, error;
1332
1333 /*
1334 * The partition mapping passed on to us specifies the information we
1335 * need to locate and initialise the sparable partition mapping
1336 * information we need.
1337 */
1338
1339 DPRINTF(VOLUMES, ("Read sparable table\n"));
1340 ump->sparable_packet_len = udf_rw16(pms->packet_len);
1341 for (spar = 0; spar < pms->n_st; spar++) {
1342 lb_num = pms->st_loc[spar];
1343 DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num));
1344 error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr);
1345 if (!error && dscr) {
1346 if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) {
1347 if (ump->sparing_table)
1348 free(ump->sparing_table, M_UDFVOLD);
1349 ump->sparing_table = &dscr->spt;
1350 dscr = NULL;
1351 DPRINTF(VOLUMES,
1352 ("Sparing table accepted (%d entries)\n",
1353 udf_rw16(ump->sparing_table->rt_l)));
1354 break; /* we're done */
1355 }
1356 }
1357 if (dscr)
1358 free(dscr, M_UDFVOLD);
1359 }
1360
1361 if (ump->sparing_table)
1362 return 0;
1363
1364 return ENOENT;
1365 }
1366
1367 /* --------------------------------------------------------------------- */
1368
1369 #define UDF_SET_SYSTEMFILE(vp) \
1370 /* XXXAD Is the vnode locked? */ \
1371 (vp)->v_vflag |= VV_SYSTEM; \
1372 vref(vp); \
1373 vput(vp); \
1374
1375 static int
1376 udf_read_metadata_files(struct udf_mount *ump, union udf_pmap *mapping)
1377 {
1378 struct part_map_meta *pmm = &mapping->pmm;
1379 struct long_ad icb_loc;
1380 struct vnode *vp;
1381 int error;
1382
1383 DPRINTF(VOLUMES, ("Reading in Metadata files\n"));
1384 icb_loc.loc.part_num = pmm->part_num;
1385 icb_loc.loc.lb_num = pmm->meta_file_lbn;
1386 DPRINTF(VOLUMES, ("Metadata file\n"));
1387 error = udf_get_node(ump, &icb_loc, &ump->metadata_file);
1388 if (ump->metadata_file) {
1389 vp = ump->metadata_file->vnode;
1390 UDF_SET_SYSTEMFILE(vp);
1391 }
1392
1393 icb_loc.loc.lb_num = pmm->meta_mirror_file_lbn;
1394 if (icb_loc.loc.lb_num != -1) {
1395 DPRINTF(VOLUMES, ("Metadata copy file\n"));
1396 error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_file);
1397 if (ump->metadatamirror_file) {
1398 vp = ump->metadatamirror_file->vnode;
1399 UDF_SET_SYSTEMFILE(vp);
1400 }
1401 }
1402
1403 icb_loc.loc.lb_num = pmm->meta_bitmap_file_lbn;
1404 if (icb_loc.loc.lb_num != -1) {
1405 DPRINTF(VOLUMES, ("Metadata bitmap file\n"));
1406 error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_file);
1407 if (ump->metadatabitmap_file) {
1408 vp = ump->metadatabitmap_file->vnode;
1409 UDF_SET_SYSTEMFILE(vp);
1410 }
1411 }
1412
1413 /* if we're mounting read-only we relax the requirements */
1414 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) {
1415 error = EFAULT;
1416 if (ump->metadata_file)
1417 error = 0;
1418 if ((ump->metadata_file == NULL) && (ump->metadatamirror_file)) {
1419 printf( "udf mount: Metadata file not readable, "
1420 "substituting Metadata copy file\n");
1421 ump->metadata_file = ump->metadatamirror_file;
1422 ump->metadatamirror_file = NULL;
1423 error = 0;
1424 }
1425 } else {
1426 /* mounting read/write */
1427 DPRINTF(VOLUMES, ("udf mount: read only file system\n"));
1428 error = EROFS;
1429 }
1430 DPRINTFIF(VOLUMES, error, ("udf mount: failed to read "
1431 "metadata files\n"));
1432 return error;
1433 }
1434 #undef UDF_SET_SYSTEMFILE
1435
1436 /* --------------------------------------------------------------------- */
1437
1438 int
1439 udf_read_vds_tables(struct udf_mount *ump, struct udf_args *args)
1440 {
1441 union udf_pmap *mapping;
1442 uint32_t n_pm, mt_l;
1443 uint32_t log_part;
1444 uint8_t *pmap_pos;
1445 int pmap_size;
1446 int error;
1447
1448 /* We have to iterate again over the part mappings for locations */
1449 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
1450 mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */
1451 pmap_pos = ump->logical_vol->maps;
1452
1453 for (log_part = 0; log_part < n_pm; log_part++) {
1454 mapping = (union udf_pmap *) pmap_pos;
1455 switch (ump->vtop_tp[log_part]) {
1456 case UDF_VTOP_TYPE_PHYS :
1457 /* nothing */
1458 break;
1459 case UDF_VTOP_TYPE_VIRT :
1460 /* search and load VAT */
1461 error = udf_search_vat(ump, mapping);
1462 if (error)
1463 return ENOENT;
1464 break;
1465 case UDF_VTOP_TYPE_SPARABLE :
1466 /* load one of the sparable tables */
1467 error = udf_read_sparables(ump, mapping);
1468 if (error)
1469 return ENOENT;
1470 break;
1471 case UDF_VTOP_TYPE_META :
1472 /* load the associated file descriptors */
1473 error = udf_read_metadata_files(ump, mapping);
1474 if (error)
1475 return ENOENT;
1476 break;
1477 default:
1478 break;
1479 }
1480 pmap_size = pmap_pos[1];
1481 pmap_pos += pmap_size;
1482 }
1483
1484 return 0;
1485 }
1486
1487 /* --------------------------------------------------------------------- */
1488
1489 int
1490 udf_read_rootdirs(struct udf_mount *ump, struct udf_args *args)
1491 {
1492 struct udf_node *rootdir_node, *streamdir_node;
1493 union dscrptr *dscr;
1494 struct long_ad fsd_loc, *dir_loc;
1495 uint32_t lb_num, dummy;
1496 uint32_t fsd_len;
1497 int dscr_type;
1498 int error;
1499
1500 /* TODO implement FSD reading in separate function like integrity? */
1501 /* get fileset descriptor sequence */
1502 fsd_loc = ump->logical_vol->lv_fsd_loc;
1503 fsd_len = udf_rw32(fsd_loc.len);
1504
1505 dscr = NULL;
1506 error = 0;
1507 while (fsd_len || error) {
1508 DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
1509 /* translate fsd_loc to lb_num */
1510 error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
1511 if (error)
1512 break;
1513 DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
1514 error = udf_read_descriptor(ump, lb_num, M_UDFVOLD, &dscr);
1515 /* end markers */
1516 if (error || (dscr == NULL))
1517 break;
1518
1519 /* analyse */
1520 dscr_type = udf_rw16(dscr->tag.id);
1521 if (dscr_type == TAGID_TERM)
1522 break;
1523 if (dscr_type != TAGID_FSD) {
1524 free(dscr, M_UDFVOLD);
1525 return ENOENT;
1526 }
1527
1528 /*
1529 * TODO check for multiple fileset descriptors; its only
1530 * picking the last now. Also check for FSD
1531 * correctness/interpretability
1532 */
1533
1534 /* update */
1535 if (ump->fileset_desc) {
1536 free(ump->fileset_desc, M_UDFVOLD);
1537 }
1538 ump->fileset_desc = &dscr->fsd;
1539 dscr = NULL;
1540
1541 /* continue to the next fsd */
1542 fsd_len -= ump->discinfo.sector_size;
1543 fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
1544
1545 /* follow up to fsd->next_ex (long_ad) if its not null */
1546 if (udf_rw32(ump->fileset_desc->next_ex.len)) {
1547 DPRINTF(VOLUMES, ("follow up FSD extent\n"));
1548 fsd_loc = ump->fileset_desc->next_ex;
1549 fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
1550 }
1551 }
1552 if (dscr)
1553 free(dscr, M_UDFVOLD);
1554
1555 /* there has to be one */
1556 if (ump->fileset_desc == NULL)
1557 return ENOENT;
1558
1559 DPRINTF(VOLUMES, ("FSD read in fine\n"));
1560
1561 /*
1562 * Now the FSD is known, read in the rootdirectory and if one exists,
1563 * the system stream dir. Some files in the system streamdir are not
1564 * wanted in this implementation since they are not maintained. If
1565 * writing is enabled we'll delete these files if they exist.
1566 */
1567
1568 rootdir_node = streamdir_node = NULL;
1569 dir_loc = NULL;
1570
1571 /* try to read in the rootdir */
1572 dir_loc = &ump->fileset_desc->rootdir_icb;
1573 error = udf_get_node(ump, dir_loc, &rootdir_node);
1574 if (error)
1575 return ENOENT;
1576
1577 /* aparently it read in fine */
1578
1579 /*
1580 * Try the system stream directory; not very likely in the ones we
1581 * test, but for completeness.
1582 */
1583 dir_loc = &ump->fileset_desc->streamdir_icb;
1584 if (udf_rw32(dir_loc->len)) {
1585 error = udf_get_node(ump, dir_loc, &streamdir_node);
1586 if (error)
1587 printf("udf mount: streamdir defined but ignored\n");
1588 if (!error) {
1589 /*
1590 * TODO process streamdir `baddies' i.e. files we dont
1591 * want if R/W
1592 */
1593 }
1594 }
1595
1596 DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
1597
1598 /* release the vnodes again; they'll be auto-recycled later */
1599 if (streamdir_node) {
1600 vput(streamdir_node->vnode);
1601 }
1602 if (rootdir_node) {
1603 vput(rootdir_node->vnode);
1604 }
1605
1606 return 0;
1607 }
1608
1609 /* --------------------------------------------------------------------- */
1610
1611 int
1612 udf_translate_vtop(struct udf_mount *ump, struct long_ad *icb_loc,
1613 uint32_t *lb_numres, uint32_t *extres)
1614 {
1615 struct part_desc *pdesc;
1616 struct spare_map_entry *sme;
1617 struct file_entry *fe;
1618 struct extfile_entry *efe;
1619 struct short_ad *s_ad;
1620 struct long_ad *l_ad;
1621 uint64_t cur_offset;
1622 uint32_t *trans;
1623 uint32_t lb_num, plb_num, lb_rel, lb_packet;
1624 uint32_t sector_size, len, alloclen;
1625 uint8_t *pos;
1626 int rel, vpart, part, addr_type, icblen, icbflags, flags;
1627
1628 assert(ump && icb_loc && lb_numres);
1629
1630 vpart = udf_rw16(icb_loc->loc.part_num);
1631 lb_num = udf_rw32(icb_loc->loc.lb_num);
1632 if (vpart < 0 || vpart > UDF_VTOP_RAWPART)
1633 return EINVAL;
1634
1635 part = ump->vtop[vpart];
1636 pdesc = ump->partitions[part];
1637
1638 switch (ump->vtop_tp[vpart]) {
1639 case UDF_VTOP_TYPE_RAW :
1640 /* 1:1 to the end of the device */
1641 *lb_numres = lb_num;
1642 *extres = INT_MAX;
1643 return 0;
1644 case UDF_VTOP_TYPE_PHYS :
1645 /* transform into its disc logical block */
1646 if (lb_num > udf_rw32(pdesc->part_len))
1647 return EINVAL;
1648 *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1649
1650 /* extent from here to the end of the partition */
1651 *extres = udf_rw32(pdesc->part_len) - lb_num;
1652 return 0;
1653 case UDF_VTOP_TYPE_VIRT :
1654 /* only maps one sector, lookup in VAT */
1655 if (lb_num >= ump->vat_entries) /* XXX > or >= ? */
1656 return EINVAL;
1657
1658 /* lookup in virtual allocation table */
1659 trans = (uint32_t *) (ump->vat_table + ump->vat_offset);
1660 lb_num = udf_rw32(trans[lb_num]);
1661
1662 /* transform into its disc logical block */
1663 if (lb_num > udf_rw32(pdesc->part_len))
1664 return EINVAL;
1665 *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1666
1667 /* just one logical block */
1668 *extres = 1;
1669 return 0;
1670 case UDF_VTOP_TYPE_SPARABLE :
1671 /* check if the packet containing the lb_num is remapped */
1672 lb_packet = lb_num / ump->sparable_packet_len;
1673 lb_rel = lb_num % ump->sparable_packet_len;
1674
1675 for (rel = 0; rel < udf_rw16(ump->sparing_table->rt_l); rel++) {
1676 sme = &ump->sparing_table->entries[rel];
1677 if (lb_packet == udf_rw32(sme->org)) {
1678 /* NOTE maps to absolute disc logical block! */
1679 *lb_numres = udf_rw32(sme->map) + lb_rel;
1680 *extres = ump->sparable_packet_len - lb_rel;
1681 return 0;
1682 }
1683 }
1684
1685 /* transform into its disc logical block */
1686 if (lb_num > udf_rw32(pdesc->part_len))
1687 return EINVAL;
1688 *lb_numres = lb_num + udf_rw32(pdesc->start_loc);
1689
1690 /* rest of block */
1691 *extres = ump->sparable_packet_len - lb_rel;
1692 return 0;
1693 case UDF_VTOP_TYPE_META :
1694 /* we have to look into the file's allocation descriptors */
1695 /* free after udf_translate_file_extent() */
1696 /* XXX sector size or lb_size? */
1697 sector_size = ump->discinfo.sector_size;
1698 /* XXX should we claim exclusive access to the metafile ? */
1699 fe = ump->metadata_file->fe;
1700 efe = ump->metadata_file->efe;
1701 if (fe) {
1702 alloclen = udf_rw32(fe->l_ad);
1703 pos = &fe->data[0] + udf_rw32(fe->l_ea);
1704 icbflags = udf_rw16(fe->icbtag.flags);
1705 } else {
1706 assert(efe);
1707 alloclen = udf_rw32(efe->l_ad);
1708 pos = &efe->data[0] + udf_rw32(efe->l_ea);
1709 icbflags = udf_rw16(efe->icbtag.flags);
1710 }
1711 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
1712
1713 cur_offset = 0;
1714 while (alloclen) {
1715 if (addr_type == UDF_ICB_SHORT_ALLOC) {
1716 icblen = sizeof(struct short_ad);
1717 s_ad = (struct short_ad *) pos;
1718 len = udf_rw32(s_ad->len);
1719 plb_num = udf_rw32(s_ad->lb_num);
1720 } else {
1721 /* should not be present, but why not */
1722 icblen = sizeof(struct long_ad);
1723 l_ad = (struct long_ad *) pos;
1724 len = udf_rw32(l_ad->len);
1725 plb_num = udf_rw32(l_ad->loc.lb_num);
1726 /* pvpart_num = udf_rw16(l_ad->loc.part_num); */
1727 }
1728 /* process extent */
1729 flags = UDF_EXT_FLAGS(len);
1730 len = UDF_EXT_LEN(len);
1731
1732 if (cur_offset + len > lb_num * sector_size) {
1733 if (flags != UDF_EXT_ALLOCATED)
1734 return EINVAL;
1735 lb_rel = lb_num - cur_offset / sector_size;
1736 /* remainder of this extent */
1737 *lb_numres = plb_num + lb_rel +
1738 udf_rw32(pdesc->start_loc);
1739 *extres = (len / sector_size) - lb_rel;
1740 return 0;
1741 }
1742 cur_offset += len;
1743 pos += icblen;
1744 alloclen -= icblen;
1745 }
1746 /* not found */
1747 DPRINTF(TRANSLATE, ("Metadata partition translation failed\n"));
1748 return EINVAL;
1749 default:
1750 printf("UDF vtop translation scheme %d unimplemented yet\n",
1751 ump->vtop_tp[vpart]);
1752 }
1753
1754 return EINVAL;
1755 }
1756
1757 /* --------------------------------------------------------------------- */
1758
1759 /* To make absolutely sure we are NOT returning zero, add one :) */
1760
1761 long
1762 udf_calchash(struct long_ad *icbptr)
1763 {
1764 /* ought to be enough since each mountpoint has its own chain */
1765 return udf_rw32(icbptr->loc.lb_num) + 1;
1766 }
1767
1768 /* --------------------------------------------------------------------- */
1769
1770 static struct udf_node *
1771 udf_hashget(struct udf_mount *ump, struct long_ad *icbptr)
1772 {
1773 struct udf_node *unp;
1774 struct vnode *vp;
1775 uint32_t hashline;
1776
1777 loop:
1778 mutex_enter(&ump->ihash_lock);
1779
1780 hashline = udf_calchash(icbptr) & UDF_INODE_HASHMASK;
1781 LIST_FOREACH(unp, &ump->udf_nodes[hashline], hashchain) {
1782 assert(unp);
1783 if (unp->loc.loc.lb_num == icbptr->loc.lb_num &&
1784 unp->loc.loc.part_num == icbptr->loc.part_num) {
1785 vp = unp->vnode;
1786 assert(vp);
1787 simple_lock(&vp->v_interlock);
1788 mutex_exit(&ump->ihash_lock);
1789 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
1790 goto loop;
1791 return unp;
1792 }
1793 }
1794 mutex_exit(&ump->ihash_lock);
1795
1796 return NULL;
1797 }
1798
1799 /* --------------------------------------------------------------------- */
1800
1801 static void
1802 udf_hashins(struct udf_node *unp)
1803 {
1804 struct udf_mount *ump;
1805 uint32_t hashline;
1806
1807 ump = unp->ump;
1808 mutex_enter(&ump->ihash_lock);
1809
1810 hashline = udf_calchash(&unp->loc) & UDF_INODE_HASHMASK;
1811 LIST_INSERT_HEAD(&ump->udf_nodes[hashline], unp, hashchain);
1812
1813 mutex_exit(&ump->ihash_lock);
1814 }
1815
1816 /* --------------------------------------------------------------------- */
1817
1818 static void
1819 udf_hashrem(struct udf_node *unp)
1820 {
1821 struct udf_mount *ump;
1822
1823 ump = unp->ump;
1824 mutex_enter(&ump->ihash_lock);
1825
1826 LIST_REMOVE(unp, hashchain);
1827
1828 mutex_exit(&ump->ihash_lock);
1829 }
1830
1831 /* --------------------------------------------------------------------- */
1832
1833 int
1834 udf_dispose_locked_node(struct udf_node *node)
1835 {
1836 if (!node)
1837 return 0;
1838 if (node->vnode)
1839 VOP_UNLOCK(node->vnode, 0);
1840 return udf_dispose_node(node);
1841 }
1842
1843 /* --------------------------------------------------------------------- */
1844
1845 int
1846 udf_dispose_node(struct udf_node *node)
1847 {
1848 struct vnode *vp;
1849
1850 DPRINTF(NODE, ("udf_dispose_node called on node %p\n", node));
1851 if (!node) {
1852 DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
1853 return 0;
1854 }
1855
1856 vp = node->vnode;
1857
1858 /* TODO extended attributes and streamdir */
1859
1860 /* remove from our hash lookup table */
1861 udf_hashrem(node);
1862
1863 /* destroy genfs structures */
1864 genfs_node_destroy(vp);
1865
1866 /* dissociate our udf_node from the vnode */
1867 vp->v_data = NULL;
1868
1869 /* free associated memory and the node itself */
1870 if (node->fe)
1871 pool_put(node->ump->desc_pool, node->fe);
1872 if (node->efe)
1873 pool_put(node->ump->desc_pool, node->efe);
1874 pool_put(&udf_node_pool, node);
1875
1876 return 0;
1877 }
1878
1879 /* --------------------------------------------------------------------- */
1880
1881 /*
1882 * Genfs interfacing
1883 *
1884 * static const struct genfs_ops udffs_genfsops = {
1885 * .gop_size = genfs_size,
1886 * size of transfers
1887 * .gop_alloc = udf_gop_alloc,
1888 * unknown
1889 * .gop_write = genfs_gop_write,
1890 * putpages interface code
1891 * .gop_markupdate = udf_gop_markupdate,
1892 * set update/modify flags etc.
1893 * }
1894 */
1895
1896 /*
1897 * Genfs interface. These four functions are the only ones defined though not
1898 * documented... great.... why is chosen for the `.' initialisers i dont know
1899 * but other filingsystems seem to use it this way.
1900 */
1901
1902 static int
1903 udf_gop_alloc(struct vnode *vp, off_t off,
1904 off_t len, int flags, kauth_cred_t cred)
1905 {
1906 return 0;
1907 }
1908
1909
1910 static void
1911 udf_gop_markupdate(struct vnode *vp, int flags)
1912 {
1913 struct udf_node *udf_node = VTOI(vp);
1914 u_long mask;
1915
1916 udf_node = udf_node; /* shut up gcc */
1917
1918 mask = 0;
1919 #ifdef notyet
1920 if ((flags & GOP_UPDATE_ACCESSED) != 0) {
1921 mask = UDF_SET_ACCESS;
1922 }
1923 if ((flags & GOP_UPDATE_MODIFIED) != 0) {
1924 mask |= UDF_SET_UPDATE;
1925 }
1926 if (mask) {
1927 udf_node->update_flag |= mask;
1928 }
1929 #endif
1930 /* msdosfs doesn't do it, but shouldn't we update the times here? */
1931 }
1932
1933
1934 static const struct genfs_ops udf_genfsops = {
1935 .gop_size = genfs_size,
1936 .gop_alloc = udf_gop_alloc,
1937 .gop_write = genfs_gop_write,
1938 .gop_markupdate = udf_gop_markupdate,
1939 };
1940
1941 /* --------------------------------------------------------------------- */
1942
1943 /*
1944 * Each node can have an attached streamdir node though not
1945 * recursively. These are otherwise known as named substreams/named
1946 * extended attributes that have no size limitations.
1947 *
1948 * `Normal' extended attributes are indicated with a number and are recorded
1949 * in either the fe/efe descriptor itself for small descriptors or recorded in
1950 * the attached extended attribute file. Since this file can get fragmented,
1951 * care ought to be taken.
1952 */
1953
1954 int
1955 udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
1956 struct udf_node **noderes)
1957 {
1958 union dscrptr *dscr, *tmpdscr;
1959 struct udf_node *node;
1960 struct vnode *nvp;
1961 struct long_ad icb_loc;
1962 extern int (**udf_vnodeop_p)(void *);
1963 uint64_t file_size;
1964 uint32_t lb_size, sector, dummy;
1965 int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
1966 int error;
1967
1968 DPRINTF(NODE, ("udf_get_node called\n"));
1969 *noderes = node = NULL;
1970
1971 /* lock to disallow simultanious creation of same node */
1972 mutex_enter(&ump->get_node_lock);
1973
1974 DPRINTF(NODE, ("\tlookup in hash table\n"));
1975 /* lookup in hash table */
1976 assert(ump);
1977 assert(node_icb_loc);
1978 node = udf_hashget(ump, node_icb_loc);
1979 if (node) {
1980 DPRINTF(NODE, ("\tgot it from the hash!\n"));
1981 /* vnode is returned locked */
1982 *noderes = node;
1983 mutex_exit(&ump->get_node_lock);
1984 return 0;
1985 }
1986
1987 /* garbage check: translate node_icb_loc to sectornr */
1988 error = udf_translate_vtop(ump, node_icb_loc, §or, &dummy);
1989 if (error) {
1990 /* no use, this will fail anyway */
1991 mutex_exit(&ump->get_node_lock);
1992 return EINVAL;
1993 }
1994
1995 /* build node (do initialise!) */
1996 node = pool_get(&udf_node_pool, PR_WAITOK);
1997 memset(node, 0, sizeof(struct udf_node));
1998
1999 DPRINTF(NODE, ("\tget new vnode\n"));
2000 /* give it a vnode */
2001 error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp);
2002 if (error) {
2003 pool_put(&udf_node_pool, node);
2004 mutex_exit(&ump->get_node_lock);
2005 return error;
2006 }
2007
2008 /* always return locked vnode */
2009 if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) {
2010 /* recycle vnode and unlock; simultanious will fail too */
2011 ungetnewvnode(nvp);
2012 mutex_exit(&ump->get_node_lock);
2013 return error;
2014 }
2015
2016 /* initialise crosslinks, note location of fe/efe for hashing */
2017 node->ump = ump;
2018 node->vnode = nvp;
2019 nvp->v_data = node;
2020 node->loc = *node_icb_loc;
2021 node->lockf = 0;
2022
2023 /* insert into the hash lookup */
2024 udf_hashins(node);
2025
2026 /* safe to unlock, the entry is in the hash table, vnode is locked */
2027 mutex_exit(&ump->get_node_lock);
2028
2029 icb_loc = *node_icb_loc;
2030 needs_indirect = 0;
2031 strat4096 = 0;
2032 udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
2033 file_size = 0;
2034 lb_size = udf_rw32(ump->logical_vol->lb_size);
2035
2036 do {
2037 error = udf_translate_vtop(ump, &icb_loc, §or, &dummy);
2038 if (error)
2039 break;
2040
2041 /* try to read in fe/efe */
2042 error = udf_read_descriptor(ump, sector, M_UDFTEMP, &tmpdscr);
2043
2044 /* blank sector marks end of sequence, check this */
2045 if ((tmpdscr == NULL) && (!strat4096))
2046 error = ENOENT;
2047
2048 /* break if read error or blank sector */
2049 if (error || (tmpdscr == NULL))
2050 break;
2051
2052 /* process descriptor based on the descriptor type */
2053 dscr_type = udf_rw16(tmpdscr->tag.id);
2054
2055 /* if dealing with an indirect entry, follow the link */
2056 if (dscr_type == TAGID_INDIRECT_ENTRY) {
2057 needs_indirect = 0;
2058 icb_loc = tmpdscr->inde.indirect_icb;
2059 free(tmpdscr, M_UDFTEMP);
2060 continue;
2061 }
2062
2063 /* only file entries and extended file entries allowed here */
2064 if ((dscr_type != TAGID_FENTRY) &&
2065 (dscr_type != TAGID_EXTFENTRY)) {
2066 free(tmpdscr, M_UDFTEMP);
2067 error = ENOENT;
2068 break;
2069 }
2070
2071 /* get descriptor space from our pool */
2072 KASSERT(udf_tagsize(tmpdscr, lb_size) == lb_size);
2073
2074 dscr = pool_get(ump->desc_pool, PR_WAITOK);
2075 memcpy(dscr, tmpdscr, lb_size);
2076 free(tmpdscr, M_UDFTEMP);
2077
2078 /* record and process/update (ext)fentry */
2079 if (dscr_type == TAGID_FENTRY) {
2080 if (node->fe)
2081 pool_put(ump->desc_pool, node->fe);
2082 node->fe = &dscr->fe;
2083 strat = udf_rw16(node->fe->icbtag.strat_type);
2084 udf_file_type = node->fe->icbtag.file_type;
2085 file_size = udf_rw64(node->fe->inf_len);
2086 } else {
2087 if (node->efe)
2088 pool_put(ump->desc_pool, node->efe);
2089 node->efe = &dscr->efe;
2090 strat = udf_rw16(node->efe->icbtag.strat_type);
2091 udf_file_type = node->efe->icbtag.file_type;
2092 file_size = udf_rw64(node->efe->inf_len);
2093 }
2094
2095 /* check recording strategy (structure) */
2096
2097 /*
2098 * Strategy 4096 is a daisy linked chain terminating with an
2099 * unrecorded sector or a TERM descriptor. The next
2100 * descriptor is to be found in the sector that follows the
2101 * current sector.
2102 */
2103 if (strat == 4096) {
2104 strat4096 = 1;
2105 needs_indirect = 1;
2106
2107 icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
2108 }
2109
2110 /*
2111 * Strategy 4 is the normal strategy and terminates, but if
2112 * we're in strategy 4096, we can't have strategy 4 mixed in
2113 */
2114
2115 if (strat == 4) {
2116 if (strat4096) {
2117 error = EINVAL;
2118 break;
2119 }
2120 break; /* done */
2121 }
2122 } while (!error);
2123
2124 if (error) {
2125 /* recycle udf_node */
2126 udf_dispose_node(node);
2127
2128 /* recycle vnode */
2129 nvp->v_data = NULL;
2130 ungetnewvnode(nvp);
2131
2132 return EINVAL; /* error code ok? */
2133 }
2134
2135 /* post process and initialise node */
2136
2137 /* assert no references to dscr anymore beyong this point */
2138 assert((node->fe) || (node->efe));
2139 dscr = NULL;
2140
2141 /*
2142 * Record where to record an updated version of the descriptor. If
2143 * there is a sequence of indirect entries, icb_loc will have been
2144 * updated. Its the write disipline to allocate new space and to make
2145 * sure the chain is maintained.
2146 *
2147 * `needs_indirect' flags if the next location is to be filled with
2148 * with an indirect entry.
2149 */
2150 node->next_loc = icb_loc;
2151 node->needs_indirect = needs_indirect;
2152
2153 /*
2154 * Translate UDF filetypes into vnode types.
2155 *
2156 * Systemfiles like the meta main and mirror files are not treated as
2157 * normal files, so we type them as having no type. UDF dictates that
2158 * they are not allowed to be visible.
2159 */
2160
2161 /* TODO specfs, fifofs etc etc. vnops setting */
2162 switch (udf_file_type) {
2163 case UDF_ICB_FILETYPE_DIRECTORY :
2164 case UDF_ICB_FILETYPE_STREAMDIR :
2165 nvp->v_type = VDIR;
2166 break;
2167 case UDF_ICB_FILETYPE_BLOCKDEVICE :
2168 nvp->v_type = VBLK;
2169 break;
2170 case UDF_ICB_FILETYPE_CHARDEVICE :
2171 nvp->v_type = VCHR;
2172 break;
2173 case UDF_ICB_FILETYPE_SYMLINK :
2174 nvp->v_type = VLNK;
2175 break;
2176 case UDF_ICB_FILETYPE_VAT :
2177 case UDF_ICB_FILETYPE_META_MAIN :
2178 case UDF_ICB_FILETYPE_META_MIRROR :
2179 nvp->v_type = VNON;
2180 break;
2181 case UDF_ICB_FILETYPE_RANDOMACCESS :
2182 case UDF_ICB_FILETYPE_REALTIME :
2183 nvp->v_type = VREG;
2184 break;
2185 default:
2186 /* YIKES, either a block/char device, fifo or something else */
2187 nvp->v_type = VNON;
2188 }
2189
2190 /* initialise genfs */
2191 genfs_node_init(nvp, &udf_genfsops);
2192
2193 /* don't forget to set vnode's v_size */
2194 uvm_vnp_setsize(nvp, file_size);
2195
2196 /* TODO ext attr and streamdir nodes */
2197
2198 *noderes = node;
2199
2200 return 0;
2201 }
2202
2203 /* --------------------------------------------------------------------- */
2204
2205 /* UDF<->unix converters */
2206
2207 /* --------------------------------------------------------------------- */
2208
2209 static mode_t
2210 udf_perm_to_unix_mode(uint32_t perm)
2211 {
2212 mode_t mode;
2213
2214 mode = ((perm & UDF_FENTRY_PERM_USER_MASK) );
2215 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK ) >> 2);
2216 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
2217
2218 return mode;
2219 }
2220
2221 /* --------------------------------------------------------------------- */
2222
2223 #ifdef notyet
2224 static uint32_t
2225 unix_mode_to_udf_perm(mode_t mode)
2226 {
2227 uint32_t perm;
2228
2229 perm = ((mode & S_IRWXO) );
2230 perm |= ((mode & S_IRWXG) << 2);
2231 perm |= ((mode & S_IRWXU) << 4);
2232 perm |= ((mode & S_IWOTH) << 3);
2233 perm |= ((mode & S_IWGRP) << 5);
2234 perm |= ((mode & S_IWUSR) << 7);
2235
2236 return perm;
2237 }
2238 #endif
2239
2240 /* --------------------------------------------------------------------- */
2241
2242 static uint32_t
2243 udf_icb_to_unix_filetype(uint32_t icbftype)
2244 {
2245 switch (icbftype) {
2246 case UDF_ICB_FILETYPE_DIRECTORY :
2247 case UDF_ICB_FILETYPE_STREAMDIR :
2248 return S_IFDIR;
2249 case UDF_ICB_FILETYPE_FIFO :
2250 return S_IFIFO;
2251 case UDF_ICB_FILETYPE_CHARDEVICE :
2252 return S_IFCHR;
2253 case UDF_ICB_FILETYPE_BLOCKDEVICE :
2254 return S_IFBLK;
2255 case UDF_ICB_FILETYPE_RANDOMACCESS :
2256 case UDF_ICB_FILETYPE_REALTIME :
2257 return S_IFREG;
2258 case UDF_ICB_FILETYPE_SYMLINK :
2259 return S_IFLNK;
2260 case UDF_ICB_FILETYPE_SOCKET :
2261 return S_IFSOCK;
2262 }
2263 /* no idea what this is */
2264 return 0;
2265 }
2266
2267 /* --------------------------------------------------------------------- */
2268
2269 /* TODO KNF-ify */
2270
2271 void
2272 udf_to_unix_name(char *result, char *id, int len, struct charspec *chsp)
2273 {
2274 uint16_t *raw_name, *unix_name;
2275 uint16_t *inchp, ch;
2276 uint8_t *outchp;
2277 int ucode_chars, nice_uchars;
2278
2279 raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK);
2280 unix_name = raw_name + 1024; /* split space in half */
2281 assert(sizeof(char) == sizeof(uint8_t));
2282 outchp = (uint8_t *) result;
2283 if ((chsp->type == 0) && (strcmp((char*) chsp->inf, "OSTA Compressed Unicode") == 0)) {
2284 *raw_name = *unix_name = 0;
2285 ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
2286 ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
2287 nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
2288 for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
2289 ch = *inchp;
2290 /* XXX sloppy unicode -> latin */
2291 *outchp++ = ch & 255;
2292 if (!ch) break;
2293 }
2294 *outchp++ = 0;
2295 } else {
2296 /* assume 8bit char length byte latin-1 */
2297 assert(*id == 8);
2298 strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
2299 }
2300 free(raw_name, M_UDFTEMP);
2301 }
2302
2303 /* --------------------------------------------------------------------- */
2304
2305 /* TODO KNF-ify */
2306
2307 void
2308 unix_to_udf_name(char *result, char *name,
2309 uint8_t *result_len, struct charspec *chsp)
2310 {
2311 uint16_t *raw_name;
2312 int udf_chars, name_len;
2313 char *inchp;
2314 uint16_t *outchp;
2315
2316 raw_name = malloc(1024, M_UDFTEMP, M_WAITOK);
2317 /* convert latin-1 or whatever to unicode-16 */
2318 *raw_name = 0;
2319 name_len = 0;
2320 inchp = name;
2321 outchp = raw_name;
2322 while (*inchp) {
2323 *outchp++ = (uint16_t) (*inchp++);
2324 name_len++;
2325 }
2326
2327 if ((chsp->type == 0) && (strcmp((char *) chsp->inf, "OSTA Compressed Unicode") == 0)) {
2328 udf_chars = udf_CompressUnicode(name_len, 8, (unicode_t *) raw_name, (byte *) result);
2329 } else {
2330 /* XXX assume 8bit char length byte latin-1 */
2331 *result++ = 8; udf_chars = 1;
2332 strncpy(result, name + 1, strlen(name+1));
2333 udf_chars += strlen(name);
2334 }
2335 *result_len = udf_chars;
2336 free(raw_name, M_UDFTEMP);
2337 }
2338
2339 /* --------------------------------------------------------------------- */
2340
2341 void
2342 udf_timestamp_to_timespec(struct udf_mount *ump,
2343 struct timestamp *timestamp,
2344 struct timespec *timespec)
2345 {
2346 struct clock_ymdhms ymdhms;
2347 uint32_t usecs, secs, nsecs;
2348 uint16_t tz;
2349
2350 /* fill in ymdhms structure from timestamp */
2351 memset(&ymdhms, 0, sizeof(ymdhms));
2352 ymdhms.dt_year = udf_rw16(timestamp->year);
2353 ymdhms.dt_mon = timestamp->month;
2354 ymdhms.dt_day = timestamp->day;
2355 ymdhms.dt_wday = 0; /* ? */
2356 ymdhms.dt_hour = timestamp->hour;
2357 ymdhms.dt_min = timestamp->minute;
2358 ymdhms.dt_sec = timestamp->second;
2359
2360 secs = clock_ymdhms_to_secs(&ymdhms);
2361 usecs = timestamp->usec +
2362 100*timestamp->hund_usec + 10000*timestamp->centisec;
2363 nsecs = usecs * 1000;
2364
2365 /*
2366 * Calculate the time zone. The timezone is 12 bit signed 2's
2367 * compliment, so we gotta do some extra magic to handle it right.
2368 */
2369 tz = udf_rw16(timestamp->type_tz);
2370 tz &= 0x0fff; /* only lower 12 bits are significant */
2371 if (tz & 0x0800) /* sign extention */
2372 tz |= 0xf000;
2373
2374 /* TODO check timezone conversion */
2375 /* check if we are specified a timezone to convert */
2376 if (udf_rw16(timestamp->type_tz) & 0x1000) {
2377 if ((int16_t) tz != -2047)
2378 secs -= (int16_t) tz * 60;
2379 } else {
2380 secs -= ump->mount_args.gmtoff;
2381 }
2382
2383 timespec->tv_sec = secs;
2384 timespec->tv_nsec = nsecs;
2385 }
2386
2387 /* --------------------------------------------------------------------- */
2388
2389 /*
2390 * Attribute and filetypes converters with get/set pairs
2391 */
2392
2393 uint32_t
2394 udf_getaccessmode(struct udf_node *udf_node)
2395 {
2396 struct file_entry *fe;
2397 struct extfile_entry *efe;
2398 uint32_t udf_perm, icbftype;
2399 uint32_t mode, ftype;
2400 uint16_t icbflags;
2401
2402 if (udf_node->fe) {
2403 fe = udf_node->fe;
2404 udf_perm = udf_rw32(fe->perm);
2405 icbftype = fe->icbtag.file_type;
2406 icbflags = udf_rw16(fe->icbtag.flags);
2407 } else {
2408 assert(udf_node->efe);
2409 efe = udf_node->efe;
2410 udf_perm = udf_rw32(efe->perm);
2411 icbftype = efe->icbtag.file_type;
2412 icbflags = udf_rw16(efe->icbtag.flags);
2413 }
2414
2415 mode = udf_perm_to_unix_mode(udf_perm);
2416 ftype = udf_icb_to_unix_filetype(icbftype);
2417
2418 /* set suid, sgid, sticky from flags in fe/efe */
2419 if (icbflags & UDF_ICB_TAG_FLAGS_SETUID)
2420 mode |= S_ISUID;
2421 if (icbflags & UDF_ICB_TAG_FLAGS_SETGID)
2422 mode |= S_ISGID;
2423 if (icbflags & UDF_ICB_TAG_FLAGS_STICKY)
2424 mode |= S_ISVTX;
2425
2426 return mode | ftype;
2427 }
2428
2429 /* --------------------------------------------------------------------- */
2430
2431 /*
2432 * Directory read and manipulation functions
2433 */
2434
2435 int
2436 udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
2437 struct long_ad *icb_loc)
2438 {
2439 struct udf_node *dir_node = VTOI(vp);
2440 struct file_entry *fe;
2441 struct extfile_entry *efe;
2442 struct fileid_desc *fid;
2443 struct dirent *dirent;
2444 uint64_t file_size, diroffset;
2445 uint32_t lb_size;
2446 int found, error;
2447
2448 /* get directory filesize */
2449 if (dir_node->fe) {
2450 fe = dir_node->fe;
2451 file_size = udf_rw64(fe->inf_len);
2452 } else {
2453 assert(dir_node->efe);
2454 efe = dir_node->efe;
2455 file_size = udf_rw64(efe->inf_len);
2456 }
2457
2458 /* allocate temporary space for fid */
2459 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
2460 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
2461
2462 found = 0;
2463 diroffset = dir_node->last_diroffset;
2464
2465 /*
2466 * if the directory is trunced or if we have never visited it yet,
2467 * start at the end.
2468 */
2469 if ((diroffset >= file_size) || (diroffset == 0)) {
2470 diroffset = dir_node->last_diroffset = file_size;
2471 }
2472
2473 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
2474
2475 while (!found) {
2476 /* if at the end, go trough zero */
2477 if (diroffset >= file_size)
2478 diroffset = 0;
2479
2480 /* transfer a new fid/dirent */
2481 error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
2482 if (error)
2483 break;
2484
2485 /* skip deleted entries */
2486 if ((fid->file_char & UDF_FILE_CHAR_DEL) == 0) {
2487 if ((strlen(dirent->d_name) == namelen) &&
2488 (strncmp(dirent->d_name, name, namelen) == 0)) {
2489 found = 1;
2490 *icb_loc = fid->icb;
2491 }
2492 }
2493
2494 if (diroffset == dir_node->last_diroffset) {
2495 /* we have cycled */
2496 break;
2497 }
2498 }
2499 free(fid, M_UDFTEMP);
2500 free(dirent, M_UDFTEMP);
2501 dir_node->last_diroffset = diroffset;
2502
2503 return found;
2504 }
2505
2506 /* --------------------------------------------------------------------- */
2507
2508 /*
2509 * Read one fid and process it into a dirent and advance to the next (*fid)
2510 * has to be allocated a logical block in size, (*dirent) struct dirent length
2511 */
2512
2513 int
2514 udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
2515 struct fileid_desc *fid, struct dirent *dirent)
2516 {
2517 struct udf_node *dir_node = VTOI(vp);
2518 struct udf_mount *ump = dir_node->ump;
2519 struct file_entry *fe;
2520 struct extfile_entry *efe;
2521 struct uio dir_uio;
2522 struct iovec dir_iovec;
2523 uint32_t entry_length, lb_size;
2524 uint64_t file_size;
2525 char *fid_name;
2526 int enough, error;
2527
2528 assert(fid);
2529 assert(dirent);
2530 assert(dir_node);
2531 assert(offset);
2532 assert(*offset != 1);
2533
2534 DPRINTF(FIDS, ("read_fid_stream called\n"));
2535 /* check if we're past the end of the directory */
2536 if (dir_node->fe) {
2537 fe = dir_node->fe;
2538 file_size = udf_rw64(fe->inf_len);
2539 } else {
2540 assert(dir_node->efe);
2541 efe = dir_node->efe;
2542 file_size = udf_rw64(efe->inf_len);
2543 }
2544 if (*offset >= file_size)
2545 return EINVAL;
2546
2547 /* get maximum length of FID descriptor */
2548 lb_size = udf_rw32(ump->logical_vol->lb_size);
2549
2550 /* initialise return values */
2551 entry_length = 0;
2552 memset(dirent, 0, sizeof(struct dirent));
2553 memset(fid, 0, lb_size);
2554
2555 /* TODO use vn_rdwr instead of creating our own uio */
2556 /* read part of the directory */
2557 memset(&dir_uio, 0, sizeof(struct uio));
2558 dir_uio.uio_rw = UIO_READ; /* read into this space */
2559 dir_uio.uio_iovcnt = 1;
2560 dir_uio.uio_iov = &dir_iovec;
2561 UIO_SETUP_SYSSPACE(&dir_uio);
2562 dir_iovec.iov_base = fid;
2563 dir_iovec.iov_len = lb_size;
2564 dir_uio.uio_offset = *offset;
2565
2566 /* limit length of read in piece */
2567 dir_uio.uio_resid = MIN(file_size - (*offset), lb_size);
2568
2569 /* read the part into the fid space */
2570 error = VOP_READ(vp, &dir_uio, IO_ALTSEMANTICS, NOCRED);
2571 if (error)
2572 return error;
2573
2574 /*
2575 * Check if we got a whole descriptor.
2576 * XXX Try to `resync' directory stream when something is very wrong.
2577 *
2578 */
2579 enough = (dir_uio.uio_offset - (*offset) >= UDF_FID_SIZE);
2580 if (!enough) {
2581 /* short dir ... */
2582 return EIO;
2583 }
2584
2585 /* check if our FID header is OK */
2586 error = udf_check_tag(fid);
2587 DPRINTFIF(FIDS, error, ("read fids: tag check failed\n"));
2588 if (!error) {
2589 if (udf_rw16(fid->tag.id) != TAGID_FID)
2590 error = ENOENT;
2591 }
2592 DPRINTFIF(FIDS, !error, ("\ttag checked ok: got TAGID_FID\n"));
2593
2594 /* check for length */
2595 if (!error) {
2596 entry_length = udf_fidsize(fid, lb_size);
2597 enough = (dir_uio.uio_offset - (*offset) >= entry_length);
2598 }
2599 DPRINTFIF(FIDS, !error, ("\tentry_length = %d, enough = %s\n",
2600 entry_length, enough?"yes":"no"));
2601
2602 if (!enough) {
2603 /* short dir ... bomb out */
2604 return EIO;
2605 }
2606
2607 /* check FID contents */
2608 if (!error) {
2609 error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
2610 DPRINTF(FIDS, ("\tpayload checked ok\n"));
2611 }
2612 if (error) {
2613 /* note that is sometimes a bit quick to report */
2614 printf("BROKEN DIRECTORY ENTRY\n");
2615 /* RESYNC? */
2616 /* TODO: use udf_resync_fid_stream */
2617 return EIO;
2618 }
2619 DPRINTF(FIDS, ("\tinterpret FID\n"));
2620
2621 /* we got a whole and valid descriptor! */
2622
2623 /* create resulting dirent structure */
2624 fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
2625 udf_to_unix_name(dirent->d_name,
2626 fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
2627
2628 /* '..' has no name, so provide one */
2629 if (fid->file_char & UDF_FILE_CHAR_PAR)
2630 strcpy(dirent->d_name, "..");
2631
2632 dirent->d_fileno = udf_calchash(&fid->icb); /* inode hash XXX */
2633 dirent->d_namlen = strlen(dirent->d_name);
2634 dirent->d_reclen = _DIRENT_SIZE(dirent);
2635
2636 /*
2637 * Note that its not worth trying to go for the filetypes now... its
2638 * too expensive too
2639 */
2640 dirent->d_type = DT_UNKNOWN;
2641
2642 /* initial guess for filetype we can make */
2643 if (fid->file_char & UDF_FILE_CHAR_DIR)
2644 dirent->d_type = DT_DIR;
2645
2646 /* advance */
2647 *offset += entry_length;
2648
2649 return error;
2650 }
2651
2652 /* --------------------------------------------------------------------- */
2653
2654 /*
2655 * block based file reading and writing
2656 */
2657
2658 static int
2659 udf_read_internal(struct udf_node *node, uint8_t *blob)
2660 {
2661 struct udf_mount *ump;
2662 struct file_entry *fe;
2663 struct extfile_entry *efe;
2664 uint64_t inflen;
2665 uint32_t sector_size;
2666 uint8_t *pos;
2667 int icbflags, addr_type;
2668
2669 /* shut up gcc */
2670 inflen = addr_type = icbflags = 0;
2671 pos = NULL;
2672
2673 /* get extent and do some paranoia checks */
2674 ump = node->ump;
2675 sector_size = ump->discinfo.sector_size;
2676
2677 fe = node->fe;
2678 efe = node->efe;
2679 if (fe) {
2680 inflen = udf_rw64(fe->inf_len);
2681 pos = &fe->data[0] + udf_rw32(fe->l_ea);
2682 icbflags = udf_rw16(fe->icbtag.flags);
2683 }
2684 if (efe) {
2685 inflen = udf_rw64(efe->inf_len);
2686 pos = &efe->data[0] + udf_rw32(efe->l_ea);
2687 icbflags = udf_rw16(efe->icbtag.flags);
2688 }
2689 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2690
2691 assert(addr_type == UDF_ICB_INTERN_ALLOC);
2692 assert(inflen < sector_size);
2693
2694 /* copy out info */
2695 memset(blob, 0, sector_size);
2696 memcpy(blob, pos, inflen);
2697
2698 return 0;
2699 }
2700
2701 /* --------------------------------------------------------------------- */
2702
2703 /*
2704 * Read file extent reads an extent specified in sectors from the file. It is
2705 * sector based; i.e. no `fancy' offsets.
2706 */
2707
2708 int
2709 udf_read_file_extent(struct udf_node *node,
2710 uint32_t from, uint32_t sectors,
2711 uint8_t *blob)
2712 {
2713 struct buf buf;
2714 uint32_t sector_size;
2715
2716 BUF_INIT(&buf);
2717
2718 sector_size = node->ump->discinfo.sector_size;
2719
2720 buf.b_bufsize = sectors * sector_size;
2721 buf.b_data = blob;
2722 buf.b_bcount = buf.b_bufsize;
2723 buf.b_resid = buf.b_bcount;
2724 buf.b_flags = B_BUSY | B_READ;
2725 buf.b_vp = node->vnode;
2726 buf.b_proc = NULL;
2727
2728 buf.b_blkno = from;
2729 buf.b_lblkno = 0;
2730 BIO_SETPRIO(&buf, BPRIO_TIMELIMITED);
2731
2732 udf_read_filebuf(node, &buf);
2733 return biowait(&buf);
2734 }
2735
2736
2737 /* --------------------------------------------------------------------- */
2738
2739 /*
2740 * Read file extent in the buffer.
2741 *
2742 * The splitup of the extent into separate request-buffers is to minimise
2743 * copying around as much as possible.
2744 */
2745
2746
2747 /* maximum of 128 translations (!) (64 kb in 512 byte sectors) */
2748 #define FILEBUFSECT 128
2749
2750 void
2751 udf_read_filebuf(struct udf_node *node, struct buf *buf)
2752 {
2753 struct buf *nestbuf;
2754 uint64_t *mapping;
2755 uint64_t run_start;
2756 uint32_t sector_size;
2757 uint32_t buf_offset, sector, rbuflen, rblk;
2758 uint8_t *buf_pos;
2759 int error, run_length;
2760
2761 uint32_t from;
2762 uint32_t sectors;
2763
2764 sector_size = node->ump->discinfo.sector_size;
2765
2766 from = buf->b_blkno;
2767 sectors = buf->b_bcount / sector_size;
2768
2769 /* assure we have enough translation slots */
2770 KASSERT(buf->b_bcount / sector_size <= FILEBUFSECT);
2771 KASSERT(MAXPHYS / sector_size <= FILEBUFSECT);
2772
2773 if (sectors > FILEBUFSECT) {
2774 printf("udf_read_filebuf: implementation limit on bufsize\n");
2775 buf->b_error = EIO;
2776 biodone(buf);
2777 return;
2778 }
2779
2780 mapping = malloc(sizeof(*mapping) * FILEBUFSECT, M_UDFTEMP, M_WAITOK);
2781
2782 error = 0;
2783 DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
2784 error = udf_translate_file_extent(node, from, sectors, mapping);
2785 if (error) {
2786 buf->b_error = error;
2787 biodone(buf);
2788 goto out;
2789 }
2790 DPRINTF(READ, ("\ttranslate extent went OK\n"));
2791
2792 /* pre-check if internal or parts are zero */
2793 if (*mapping == UDF_TRANS_INTERN) {
2794 error = udf_read_internal(node, (uint8_t *) buf->b_data);
2795 if (error) {
2796 buf->b_error = error;
2797 }
2798 biodone(buf);
2799 goto out;
2800 }
2801 DPRINTF(READ, ("\tnot intern\n"));
2802
2803 /* request read-in of data from disc scheduler */
2804 buf->b_resid = buf->b_bcount;
2805 for (sector = 0; sector < sectors; sector++) {
2806 buf_offset = sector * sector_size;
2807 buf_pos = (uint8_t *) buf->b_data + buf_offset;
2808 DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
2809
2810 switch (mapping[sector]) {
2811 case UDF_TRANS_UNMAPPED:
2812 case UDF_TRANS_ZERO:
2813 /* copy zero sector */
2814 memset(buf_pos, 0, sector_size);
2815 DPRINTF(READ, ("\treturning zero sector\n"));
2816 nestiobuf_done(buf, sector_size, 0);
2817 break;
2818 default :
2819 DPRINTF(READ, ("\tread sector "
2820 "%"PRIu64"\n", mapping[sector]));
2821
2822 run_start = mapping[sector];
2823 run_length = 1;
2824 while (sector < sectors-1) {
2825 if (mapping[sector+1] != mapping[sector]+1)
2826 break;
2827 run_length++;
2828 sector++;
2829 }
2830
2831 /*
2832 * nest an iobuf and mark it for async reading. Since
2833 * we're using nested buffers, they can't be cached by
2834 * design.
2835 */
2836 rbuflen = run_length * sector_size;
2837 rblk = run_start * (sector_size/DEV_BSIZE);
2838
2839 nestbuf = getiobuf();
2840 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
2841 /* nestbuf is B_ASYNC */
2842
2843 /* CD schedules on raw blkno */
2844 nestbuf->b_blkno = rblk;
2845 nestbuf->b_proc = NULL;
2846 nestbuf->b_cylinder = 0;
2847 nestbuf->b_rawblkno = rblk;
2848 VOP_STRATEGY(node->ump->devvp, nestbuf);
2849 }
2850 }
2851 out:
2852 DPRINTF(READ, ("\tend of read_filebuf\n"));
2853 free(mapping, M_UDFTEMP);
2854 return;
2855 }
2856 #undef FILEBUFSECT
2857
2858
2859 /* --------------------------------------------------------------------- */
2860
2861 /*
2862 * Translate an extent (in sectors) into sector numbers; used for read and
2863 * write operations. DOESNT't check extents.
2864 */
2865
2866 int
2867 udf_translate_file_extent(struct udf_node *node,
2868 uint32_t from, uint32_t pages,
2869 uint64_t *map)
2870 {
2871 struct udf_mount *ump;
2872 struct file_entry *fe;
2873 struct extfile_entry *efe;
2874 struct short_ad *s_ad;
2875 struct long_ad *l_ad, t_ad;
2876 uint64_t transsec;
2877 uint32_t sector_size, transsec32;
2878 uint32_t overlap, translen;
2879 uint32_t vpart_num, lb_num, len, alloclen;
2880 uint8_t *pos;
2881 int error, flags, addr_type, icblen, icbflags;
2882
2883 if (!node)
2884 return ENOENT;
2885
2886 /* shut up gcc */
2887 alloclen = addr_type = icbflags = 0;
2888 pos = NULL;
2889
2890 /* do the work */
2891 ump = node->ump;
2892 sector_size = ump->discinfo.sector_size;
2893 fe = node->fe;
2894 efe = node->efe;
2895 if (fe) {
2896 alloclen = udf_rw32(fe->l_ad);
2897 pos = &fe->data[0] + udf_rw32(fe->l_ea);
2898 icbflags = udf_rw16(fe->icbtag.flags);
2899 }
2900 if (efe) {
2901 alloclen = udf_rw32(efe->l_ad);
2902 pos = &efe->data[0] + udf_rw32(efe->l_ea);
2903 icbflags = udf_rw16(efe->icbtag.flags);
2904 }
2905 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
2906
2907 DPRINTF(TRANSLATE, ("udf trans: alloc_len = %d, addr_type %d, "
2908 "fe %p, efe %p\n", alloclen, addr_type, fe, efe));
2909
2910 vpart_num = udf_rw16(node->loc.loc.part_num);
2911 lb_num = len = icblen = 0; /* shut up gcc */
2912 while (pages && alloclen) {
2913 DPRINTF(TRANSLATE, ("\taddr_type %d\n", addr_type));
2914 switch (addr_type) {
2915 case UDF_ICB_INTERN_ALLOC :
2916 /* TODO check extents? */
2917 *map = UDF_TRANS_INTERN;
2918 return 0;
2919 case UDF_ICB_SHORT_ALLOC :
2920 icblen = sizeof(struct short_ad);
2921 s_ad = (struct short_ad *) pos;
2922 len = udf_rw32(s_ad->len);
2923 lb_num = udf_rw32(s_ad->lb_num);
2924 break;
2925 case UDF_ICB_LONG_ALLOC :
2926 icblen = sizeof(struct long_ad);
2927 l_ad = (struct long_ad *) pos;
2928 len = udf_rw32(l_ad->len);
2929 lb_num = udf_rw32(l_ad->loc.lb_num);
2930 vpart_num = udf_rw16(l_ad->loc.part_num);
2931 DPRINTFIF(TRANSLATE,
2932 (l_ad->impl.im_used.flags &
2933 UDF_ADIMP_FLAGS_EXTENT_ERASED),
2934 ("UDF: got an `extent erased' flag in long_ad\n"));
2935 break;
2936 default:
2937 /* can't be here */
2938 return EINVAL; /* for sure */
2939 }
2940
2941 /* process extent */
2942 flags = UDF_EXT_FLAGS(len);
2943 len = UDF_EXT_LEN(len);
2944
2945 overlap = (len + sector_size -1) / sector_size;
2946 if (from) {
2947 if (from > overlap) {
2948 from -= overlap;
2949 overlap = 0;
2950 } else {
2951 lb_num += from; /* advance in extent */
2952 overlap -= from;
2953 from = 0;
2954 }
2955 }
2956
2957 overlap = MIN(overlap, pages);
2958 while (overlap) {
2959 switch (flags) {
2960 case UDF_EXT_REDIRECT :
2961 /* no support for allocation extentions yet */
2962 /* TODO support for allocation extention */
2963 return ENOENT;
2964 case UDF_EXT_FREED :
2965 case UDF_EXT_FREE :
2966 transsec = UDF_TRANS_ZERO;
2967 translen = overlap;
2968 while (overlap && pages && translen) {
2969 *map++ = transsec;
2970 lb_num++;
2971 overlap--; pages--; translen--;
2972 }
2973 break;
2974 case UDF_EXT_ALLOCATED :
2975 t_ad.loc.lb_num = udf_rw32(lb_num);
2976 t_ad.loc.part_num = udf_rw16(vpart_num);
2977 error = udf_translate_vtop(ump,
2978 &t_ad, &transsec32, &translen);
2979 transsec = transsec32;
2980 if (error)
2981 return error;
2982 while (overlap && pages && translen) {
2983 *map++ = transsec;
2984 lb_num++; transsec++;
2985 overlap--; pages--; translen--;
2986 }
2987 break;
2988 }
2989 }
2990 pos += icblen;
2991 alloclen -= icblen;
2992 }
2993 return 0;
2994 }
2995
2996 /* --------------------------------------------------------------------- */
2997
2998