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