udf_subr.c revision 1.80 1 /* $NetBSD: udf_subr.c,v 1.80 2008/12/16 22:35:36 christos Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2008 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29
30 #include <sys/cdefs.h>
31 #ifndef lint
32 __KERNEL_RCSID(0, "$NetBSD: udf_subr.c,v 1.80 2008/12/16 22:35:36 christos Exp $");
33 #endif /* not lint */
34
35
36 #if defined(_KERNEL_OPT)
37 #include "opt_compat_netbsd.h"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <sys/namei.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <miscfs/genfs/genfs_node.h>
48 #include <sys/mount.h>
49 #include <sys/buf.h>
50 #include <sys/file.h>
51 #include <sys/device.h>
52 #include <sys/disklabel.h>
53 #include <sys/ioctl.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/stat.h>
57 #include <sys/conf.h>
58 #include <sys/kauth.h>
59 #include <fs/unicode.h>
60 #include <dev/clock_subr.h>
61
62 #include <fs/udf/ecma167-udf.h>
63 #include <fs/udf/udf_mount.h>
64 #include <sys/dirhash.h>
65
66 #include "udf.h"
67 #include "udf_subr.h"
68 #include "udf_bswap.h"
69
70
71 #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
72
73 #define UDF_SET_SYSTEMFILE(vp) \
74 /* XXXAD Is the vnode locked? */ \
75 (vp)->v_vflag |= VV_SYSTEM; \
76 vref(vp); \
77 vput(vp); \
78
79 extern int syncer_maxdelay; /* maximum delay time */
80 extern int (**udf_vnodeop_p)(void *);
81
82 /* --------------------------------------------------------------------- */
83
84 //#ifdef DEBUG
85 #if 1
86
87 #if 0
88 static void
89 udf_dumpblob(boid *blob, uint32_t dlen)
90 {
91 int i, j;
92
93 printf("blob = %p\n", blob);
94 printf("dump of %d bytes\n", dlen);
95
96 for (i = 0; i < dlen; i+ = 16) {
97 printf("%04x ", i);
98 for (j = 0; j < 16; j++) {
99 if (i+j < dlen) {
100 printf("%02x ", blob[i+j]);
101 } else {
102 printf(" ");
103 }
104 }
105 for (j = 0; j < 16; j++) {
106 if (i+j < dlen) {
107 if (blob[i+j]>32 && blob[i+j]! = 127) {
108 printf("%c", blob[i+j]);
109 } else {
110 printf(".");
111 }
112 }
113 }
114 printf("\n");
115 }
116 printf("\n");
117 Debugger();
118 }
119 #endif
120
121 static void
122 udf_dump_discinfo(struct udf_mount *ump)
123 {
124 char bits[128];
125 struct mmc_discinfo *di = &ump->discinfo;
126
127 if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0)
128 return;
129
130 printf("Device/media info :\n");
131 printf("\tMMC profile 0x%02x\n", di->mmc_profile);
132 printf("\tderived class %d\n", di->mmc_class);
133 printf("\tsector size %d\n", di->sector_size);
134 printf("\tdisc state %d\n", di->disc_state);
135 printf("\tlast ses state %d\n", di->last_session_state);
136 printf("\tbg format state %d\n", di->bg_format_state);
137 printf("\tfrst track %d\n", di->first_track);
138 printf("\tfst on last ses %d\n", di->first_track_last_session);
139 printf("\tlst on last ses %d\n", di->last_track_last_session);
140 printf("\tlink block penalty %d\n", di->link_block_penalty);
141 snprintb(bits, sizeof(bits), MMC_DFLAGS_FLAGBITS, di->disc_flags);
142 printf("\tdisc flags %s\n", bits);
143 printf("\tdisc id %x\n", di->disc_id);
144 printf("\tdisc barcode %"PRIx64"\n", di->disc_barcode);
145
146 printf("\tnum sessions %d\n", di->num_sessions);
147 printf("\tnum tracks %d\n", di->num_tracks);
148
149 snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cur);
150 printf("\tcapabilities cur %s\n", bits);
151 snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cap);
152 printf("\tcapabilities cap %s\n", bits);
153 }
154 #else
155 #define udf_dump_discinfo(a);
156 #endif
157
158
159 /* --------------------------------------------------------------------- */
160
161 /* not called often */
162 int
163 udf_update_discinfo(struct udf_mount *ump)
164 {
165 struct vnode *devvp = ump->devvp;
166 struct partinfo dpart;
167 struct mmc_discinfo *di;
168 int error;
169
170 DPRINTF(VOLUMES, ("read/update disc info\n"));
171 di = &ump->discinfo;
172 memset(di, 0, sizeof(struct mmc_discinfo));
173
174 /* check if we're on a MMC capable device, i.e. CD/DVD */
175 error = VOP_IOCTL(devvp, MMCGETDISCINFO, di, FKIOCTL, NOCRED);
176 if (error == 0) {
177 udf_dump_discinfo(ump);
178 return 0;
179 }
180
181 /* disc partition support */
182 error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED);
183 if (error)
184 return ENODEV;
185
186 /* set up a disc info profile for partitions */
187 di->mmc_profile = 0x01; /* disc type */
188 di->mmc_class = MMC_CLASS_DISC;
189 di->disc_state = MMC_STATE_CLOSED;
190 di->last_session_state = MMC_STATE_CLOSED;
191 di->bg_format_state = MMC_BGFSTATE_COMPLETED;
192 di->link_block_penalty = 0;
193
194 di->mmc_cur = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE |
195 MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE;
196 di->mmc_cap = di->mmc_cur;
197 di->disc_flags = MMC_DFLAGS_UNRESTRICTED;
198
199 /* TODO problem with last_possible_lba on resizable VND; request */
200 di->last_possible_lba = dpart.part->p_size;
201 di->sector_size = dpart.disklab->d_secsize;
202
203 di->num_sessions = 1;
204 di->num_tracks = 1;
205
206 di->first_track = 1;
207 di->first_track_last_session = di->last_track_last_session = 1;
208
209 udf_dump_discinfo(ump);
210 return 0;
211 }
212
213
214 int
215 udf_update_trackinfo(struct udf_mount *ump, struct mmc_trackinfo *ti)
216 {
217 struct vnode *devvp = ump->devvp;
218 struct mmc_discinfo *di = &ump->discinfo;
219 int error, class;
220
221 DPRINTF(VOLUMES, ("read track info\n"));
222
223 class = di->mmc_class;
224 if (class != MMC_CLASS_DISC) {
225 /* tracknr specified in struct ti */
226 error = VOP_IOCTL(devvp, MMCGETTRACKINFO, ti, FKIOCTL, NOCRED);
227 return error;
228 }
229
230 /* disc partition support */
231 if (ti->tracknr != 1)
232 return EIO;
233
234 /* create fake ti (TODO check for resized vnds) */
235 ti->sessionnr = 1;
236
237 ti->track_mode = 0; /* XXX */
238 ti->data_mode = 0; /* XXX */
239 ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID;
240
241 ti->track_start = 0;
242 ti->packet_size = 1;
243
244 /* TODO support for resizable vnd */
245 ti->track_size = di->last_possible_lba;
246 ti->next_writable = di->last_possible_lba;
247 ti->last_recorded = ti->next_writable;
248 ti->free_blocks = 0;
249
250 return 0;
251 }
252
253
254 int
255 udf_setup_writeparams(struct udf_mount *ump)
256 {
257 struct mmc_writeparams mmc_writeparams;
258 int error;
259
260 if (ump->discinfo.mmc_class == MMC_CLASS_DISC)
261 return 0;
262
263 /*
264 * only CD burning normally needs setting up, but other disc types
265 * might need other settings to be made. The MMC framework will set up
266 * the nessisary recording parameters according to the disc
267 * characteristics read in. Modifications can be made in the discinfo
268 * structure passed to change the nature of the disc.
269 */
270
271 memset(&mmc_writeparams, 0, sizeof(struct mmc_writeparams));
272 mmc_writeparams.mmc_class = ump->discinfo.mmc_class;
273 mmc_writeparams.mmc_cur = ump->discinfo.mmc_cur;
274
275 /*
276 * UDF dictates first track to determine track mode for the whole
277 * disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1]
278 * To prevent problems with a `reserved' track in front we start with
279 * the 2nd track and if that is not valid, go for the 1st.
280 */
281 mmc_writeparams.tracknr = 2;
282 mmc_writeparams.data_mode = MMC_DATAMODE_DEFAULT; /* XA disc */
283 mmc_writeparams.track_mode = MMC_TRACKMODE_DEFAULT; /* data */
284
285 error = VOP_IOCTL(ump->devvp, MMCSETUPWRITEPARAMS, &mmc_writeparams,
286 FKIOCTL, NOCRED);
287 if (error) {
288 mmc_writeparams.tracknr = 1;
289 error = VOP_IOCTL(ump->devvp, MMCSETUPWRITEPARAMS,
290 &mmc_writeparams, FKIOCTL, NOCRED);
291 }
292 return error;
293 }
294
295
296 int
297 udf_synchronise_caches(struct udf_mount *ump)
298 {
299 struct mmc_op mmc_op;
300
301 DPRINTF(CALL, ("udf_synchronise_caches()\n"));
302
303 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
304 return 0;
305
306 /* discs are done now */
307 if (ump->discinfo.mmc_class == MMC_CLASS_DISC)
308 return 0;
309
310 bzero(&mmc_op, sizeof(struct mmc_op));
311 mmc_op.operation = MMC_OP_SYNCHRONISECACHE;
312
313 /* ignore return code */
314 (void) VOP_IOCTL(ump->devvp, MMCOP, &mmc_op, FKIOCTL, NOCRED);
315
316 return 0;
317 }
318
319 /* --------------------------------------------------------------------- */
320
321 /* track/session searching for mounting */
322 int
323 udf_search_tracks(struct udf_mount *ump, struct udf_args *args,
324 int *first_tracknr, int *last_tracknr)
325 {
326 struct mmc_trackinfo trackinfo;
327 uint32_t tracknr, start_track, num_tracks;
328 int error;
329
330 /* if negative, sessionnr is relative to last session */
331 if (args->sessionnr < 0) {
332 args->sessionnr += ump->discinfo.num_sessions;
333 }
334
335 /* sanity */
336 if (args->sessionnr < 0)
337 args->sessionnr = 0;
338 if (args->sessionnr > ump->discinfo.num_sessions)
339 args->sessionnr = ump->discinfo.num_sessions;
340
341 /* search the tracks for this session, zero session nr indicates last */
342 if (args->sessionnr == 0)
343 args->sessionnr = ump->discinfo.num_sessions;
344 if (ump->discinfo.last_session_state == MMC_STATE_EMPTY)
345 args->sessionnr--;
346
347 /* sanity again */
348 if (args->sessionnr < 0)
349 args->sessionnr = 0;
350
351 /* search the first and last track of the specified session */
352 num_tracks = ump->discinfo.num_tracks;
353 start_track = ump->discinfo.first_track;
354
355 /* search for first track of this session */
356 for (tracknr = start_track; tracknr <= num_tracks; tracknr++) {
357 /* get track info */
358 trackinfo.tracknr = tracknr;
359 error = udf_update_trackinfo(ump, &trackinfo);
360 if (error)
361 return error;
362
363 if (trackinfo.sessionnr == args->sessionnr)
364 break;
365 }
366 *first_tracknr = tracknr;
367
368 /* search for last track of this session */
369 for (;tracknr <= num_tracks; tracknr++) {
370 /* get track info */
371 trackinfo.tracknr = tracknr;
372 error = udf_update_trackinfo(ump, &trackinfo);
373 if (error || (trackinfo.sessionnr != args->sessionnr)) {
374 tracknr--;
375 break;
376 }
377 }
378 if (tracknr > num_tracks)
379 tracknr--;
380
381 *last_tracknr = tracknr;
382
383 if (*last_tracknr < *first_tracknr) {
384 printf( "udf_search_tracks: sanity check on drive+disc failed, "
385 "drive returned garbage\n");
386 return EINVAL;
387 }
388
389 assert(*last_tracknr >= *first_tracknr);
390 return 0;
391 }
392
393
394 /*
395 * NOTE: this is the only routine in this file that directly peeks into the
396 * metadata file but since its at a larval state of the mount it can't hurt.
397 *
398 * XXX candidate for udf_allocation.c
399 * XXX clean me up!, change to new node reading code.
400 */
401
402 static void
403 udf_check_track_metadata_overlap(struct udf_mount *ump,
404 struct mmc_trackinfo *trackinfo)
405 {
406 struct part_desc *part;
407 struct file_entry *fe;
408 struct extfile_entry *efe;
409 struct short_ad *s_ad;
410 struct long_ad *l_ad;
411 uint32_t track_start, track_end;
412 uint32_t phys_part_start, phys_part_end, part_start, part_end;
413 uint32_t sector_size, len, alloclen, plb_num;
414 uint8_t *pos;
415 int addr_type, icblen, icbflags, flags;
416
417 /* get our track extents */
418 track_start = trackinfo->track_start;
419 track_end = track_start + trackinfo->track_size;
420
421 /* get our base partition extent */
422 KASSERT(ump->node_part == ump->fids_part);
423 part = ump->partitions[ump->node_part];
424 phys_part_start = udf_rw32(part->start_loc);
425 phys_part_end = phys_part_start + udf_rw32(part->part_len);
426
427 /* no use if its outside the physical partition */
428 if ((phys_part_start >= track_end) || (phys_part_end < track_start))
429 return;
430
431 /*
432 * now follow all extents in the fe/efe to see if they refer to this
433 * track
434 */
435
436 sector_size = ump->discinfo.sector_size;
437
438 /* XXX should we claim exclusive access to the metafile ? */
439 /* TODO: move to new node read code */
440 fe = ump->metadata_node->fe;
441 efe = ump->metadata_node->efe;
442 if (fe) {
443 alloclen = udf_rw32(fe->l_ad);
444 pos = &fe->data[0] + udf_rw32(fe->l_ea);
445 icbflags = udf_rw16(fe->icbtag.flags);
446 } else {
447 assert(efe);
448 alloclen = udf_rw32(efe->l_ad);
449 pos = &efe->data[0] + udf_rw32(efe->l_ea);
450 icbflags = udf_rw16(efe->icbtag.flags);
451 }
452 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
453
454 while (alloclen) {
455 if (addr_type == UDF_ICB_SHORT_ALLOC) {
456 icblen = sizeof(struct short_ad);
457 s_ad = (struct short_ad *) pos;
458 len = udf_rw32(s_ad->len);
459 plb_num = udf_rw32(s_ad->lb_num);
460 } else {
461 /* should not be present, but why not */
462 icblen = sizeof(struct long_ad);
463 l_ad = (struct long_ad *) pos;
464 len = udf_rw32(l_ad->len);
465 plb_num = udf_rw32(l_ad->loc.lb_num);
466 /* pvpart_num = udf_rw16(l_ad->loc.part_num); */
467 }
468 /* process extent */
469 flags = UDF_EXT_FLAGS(len);
470 len = UDF_EXT_LEN(len);
471
472 part_start = phys_part_start + plb_num;
473 part_end = part_start + (len / sector_size);
474
475 if ((part_start >= track_start) && (part_end <= track_end)) {
476 /* extent is enclosed within this track */
477 ump->metadata_track = *trackinfo;
478 return;
479 }
480
481 pos += icblen;
482 alloclen -= icblen;
483 }
484 }
485
486
487 int
488 udf_search_writing_tracks(struct udf_mount *ump)
489 {
490 struct mmc_trackinfo trackinfo;
491 struct part_desc *part;
492 uint32_t tracknr, start_track, num_tracks;
493 uint32_t track_start, track_end, part_start, part_end;
494 int node_alloc, error;
495
496 /*
497 * in the CD/(HD)DVD/BD recordable device model a few tracks within
498 * the last session might be open but in the UDF device model at most
499 * three tracks can be open: a reserved track for delayed ISO VRS
500 * writing, a data track and a metadata track. We search here for the
501 * data track and the metadata track. Note that the reserved track is
502 * troublesome but can be detected by its small size of < 512 sectors.
503 */
504
505 num_tracks = ump->discinfo.num_tracks;
506 start_track = ump->discinfo.first_track;
507
508 /* fetch info on first and possibly only track */
509 trackinfo.tracknr = start_track;
510 error = udf_update_trackinfo(ump, &trackinfo);
511 if (error)
512 return error;
513
514 /* copy results to our mount point */
515 ump->data_track = trackinfo;
516 ump->metadata_track = trackinfo;
517
518 /* if not sequential, we're done */
519 if (num_tracks == 1)
520 return 0;
521
522 for (tracknr = start_track;tracknr <= num_tracks; tracknr++) {
523 /* get track info */
524 trackinfo.tracknr = tracknr;
525 error = udf_update_trackinfo(ump, &trackinfo);
526 if (error)
527 return error;
528
529 if ((trackinfo.flags & MMC_TRACKINFO_NWA_VALID) == 0)
530 continue;
531
532 track_start = trackinfo.track_start;
533 track_end = track_start + trackinfo.track_size;
534
535 /* check for overlap on data partition */
536 part = ump->partitions[ump->data_part];
537 part_start = udf_rw32(part->start_loc);
538 part_end = part_start + udf_rw32(part->part_len);
539 if ((part_start < track_end) && (part_end > track_start)) {
540 ump->data_track = trackinfo;
541 /* TODO check if UDF partition data_part is writable */
542 }
543
544 /* check for overlap on metadata partition */
545 node_alloc = ump->vtop_alloc[ump->node_part];
546 if ((node_alloc == UDF_ALLOC_METASEQUENTIAL) ||
547 (node_alloc == UDF_ALLOC_METABITMAP)) {
548 udf_check_track_metadata_overlap(ump, &trackinfo);
549 } else {
550 ump->metadata_track = trackinfo;
551 }
552 }
553
554 if ((ump->data_track.flags & MMC_TRACKINFO_NWA_VALID) == 0)
555 return EROFS;
556
557 if ((ump->metadata_track.flags & MMC_TRACKINFO_NWA_VALID) == 0)
558 return EROFS;
559
560 return 0;
561 }
562
563 /* --------------------------------------------------------------------- */
564
565 /*
566 * Check if the blob starts with a good UDF tag. Tags are protected by a
567 * checksum over the reader except one byte at position 4 that is the checksum
568 * itself.
569 */
570
571 int
572 udf_check_tag(void *blob)
573 {
574 struct desc_tag *tag = blob;
575 uint8_t *pos, sum, cnt;
576
577 /* check TAG header checksum */
578 pos = (uint8_t *) tag;
579 sum = 0;
580
581 for(cnt = 0; cnt < 16; cnt++) {
582 if (cnt != 4)
583 sum += *pos;
584 pos++;
585 }
586 if (sum != tag->cksum) {
587 /* bad tag header checksum; this is not a valid tag */
588 return EINVAL;
589 }
590
591 return 0;
592 }
593
594
595 /*
596 * check tag payload will check descriptor CRC as specified.
597 * If the descriptor is too long, it will return EIO otherwise EINVAL.
598 */
599
600 int
601 udf_check_tag_payload(void *blob, uint32_t max_length)
602 {
603 struct desc_tag *tag = blob;
604 uint16_t crc, crc_len;
605
606 crc_len = udf_rw16(tag->desc_crc_len);
607
608 /* check payload CRC if applicable */
609 if (crc_len == 0)
610 return 0;
611
612 if (crc_len > max_length)
613 return EIO;
614
615 crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len);
616 if (crc != udf_rw16(tag->desc_crc)) {
617 /* bad payload CRC; this is a broken tag */
618 return EINVAL;
619 }
620
621 return 0;
622 }
623
624
625 void
626 udf_validate_tag_sum(void *blob)
627 {
628 struct desc_tag *tag = blob;
629 uint8_t *pos, sum, cnt;
630
631 /* calculate TAG header checksum */
632 pos = (uint8_t *) tag;
633 sum = 0;
634
635 for(cnt = 0; cnt < 16; cnt++) {
636 if (cnt != 4) sum += *pos;
637 pos++;
638 }
639 tag->cksum = sum; /* 8 bit */
640 }
641
642
643 /* assumes sector number of descriptor to be saved already present */
644 void
645 udf_validate_tag_and_crc_sums(void *blob)
646 {
647 struct desc_tag *tag = blob;
648 uint8_t *btag = (uint8_t *) tag;
649 uint16_t crc, crc_len;
650
651 crc_len = udf_rw16(tag->desc_crc_len);
652
653 /* check payload CRC if applicable */
654 if (crc_len > 0) {
655 crc = udf_cksum(btag + UDF_DESC_TAG_LENGTH, crc_len);
656 tag->desc_crc = udf_rw16(crc);
657 }
658
659 /* calculate TAG header checksum */
660 udf_validate_tag_sum(blob);
661 }
662
663 /* --------------------------------------------------------------------- */
664
665 /*
666 * XXX note the different semantics from udfclient: for FIDs it still rounds
667 * up to sectors. Use udf_fidsize() for a correct length.
668 */
669
670 int
671 udf_tagsize(union dscrptr *dscr, uint32_t lb_size)
672 {
673 uint32_t size, tag_id, num_lb, elmsz;
674
675 tag_id = udf_rw16(dscr->tag.id);
676
677 switch (tag_id) {
678 case TAGID_LOGVOL :
679 size = sizeof(struct logvol_desc) - 1;
680 size += udf_rw32(dscr->lvd.mt_l);
681 break;
682 case TAGID_UNALLOC_SPACE :
683 elmsz = sizeof(struct extent_ad);
684 size = sizeof(struct unalloc_sp_desc) - elmsz;
685 size += udf_rw32(dscr->usd.alloc_desc_num) * elmsz;
686 break;
687 case TAGID_FID :
688 size = UDF_FID_SIZE + dscr->fid.l_fi + udf_rw16(dscr->fid.l_iu);
689 size = (size + 3) & ~3;
690 break;
691 case TAGID_LOGVOL_INTEGRITY :
692 size = sizeof(struct logvol_int_desc) - sizeof(uint32_t);
693 size += udf_rw32(dscr->lvid.l_iu);
694 size += (2 * udf_rw32(dscr->lvid.num_part) * sizeof(uint32_t));
695 break;
696 case TAGID_SPACE_BITMAP :
697 size = sizeof(struct space_bitmap_desc) - 1;
698 size += udf_rw32(dscr->sbd.num_bytes);
699 break;
700 case TAGID_SPARING_TABLE :
701 elmsz = sizeof(struct spare_map_entry);
702 size = sizeof(struct udf_sparing_table) - elmsz;
703 size += udf_rw16(dscr->spt.rt_l) * elmsz;
704 break;
705 case TAGID_FENTRY :
706 size = sizeof(struct file_entry);
707 size += udf_rw32(dscr->fe.l_ea) + udf_rw32(dscr->fe.l_ad)-1;
708 break;
709 case TAGID_EXTFENTRY :
710 size = sizeof(struct extfile_entry);
711 size += udf_rw32(dscr->efe.l_ea) + udf_rw32(dscr->efe.l_ad)-1;
712 break;
713 case TAGID_FSD :
714 size = sizeof(struct fileset_desc);
715 break;
716 default :
717 size = sizeof(union dscrptr);
718 break;
719 }
720
721 if ((size == 0) || (lb_size == 0))
722 return 0;
723
724 if (lb_size == 1)
725 return size;
726
727 /* round up in sectors */
728 num_lb = (size + lb_size -1) / lb_size;
729 return num_lb * lb_size;
730 }
731
732
733 int
734 udf_fidsize(struct fileid_desc *fid)
735 {
736 uint32_t size;
737
738 if (udf_rw16(fid->tag.id) != TAGID_FID)
739 panic("got udf_fidsize on non FID\n");
740
741 size = UDF_FID_SIZE + fid->l_fi + udf_rw16(fid->l_iu);
742 size = (size + 3) & ~3;
743
744 return size;
745 }
746
747 /* --------------------------------------------------------------------- */
748
749 void
750 udf_lock_node(struct udf_node *udf_node, int flag, char const *fname, const int lineno)
751 {
752 int ret;
753
754 mutex_enter(&udf_node->node_mutex);
755 /* wait until free */
756 while (udf_node->i_flags & IN_LOCKED) {
757 ret = cv_timedwait(&udf_node->node_lock, &udf_node->node_mutex, hz/8);
758 /* TODO check if we should return error; abort */
759 if (ret == EWOULDBLOCK) {
760 DPRINTF(LOCKING, ( "udf_lock_node: udf_node %p would block "
761 "wanted at %s:%d, previously locked at %s:%d\n",
762 udf_node, fname, lineno,
763 udf_node->lock_fname, udf_node->lock_lineno));
764 }
765 }
766 /* grab */
767 udf_node->i_flags |= IN_LOCKED | flag;
768 /* debug */
769 udf_node->lock_fname = fname;
770 udf_node->lock_lineno = lineno;
771
772 mutex_exit(&udf_node->node_mutex);
773 }
774
775
776 void
777 udf_unlock_node(struct udf_node *udf_node, int flag)
778 {
779 mutex_enter(&udf_node->node_mutex);
780 udf_node->i_flags &= ~(IN_LOCKED | flag);
781 cv_broadcast(&udf_node->node_lock);
782 mutex_exit(&udf_node->node_mutex);
783 }
784
785
786 /* --------------------------------------------------------------------- */
787
788 static int
789 udf_read_anchor(struct udf_mount *ump, uint32_t sector, struct anchor_vdp **dst)
790 {
791 int error;
792
793 error = udf_read_phys_dscr(ump, sector, M_UDFVOLD,
794 (union dscrptr **) dst);
795 if (!error) {
796 /* blank terminator blocks are not allowed here */
797 if (*dst == NULL)
798 return ENOENT;
799 if (udf_rw16((*dst)->tag.id) != TAGID_ANCHOR) {
800 error = ENOENT;
801 free(*dst, M_UDFVOLD);
802 *dst = NULL;
803 DPRINTF(VOLUMES, ("Not an anchor\n"));
804 }
805 }
806
807 return error;
808 }
809
810
811 int
812 udf_read_anchors(struct udf_mount *ump)
813 {
814 struct udf_args *args = &ump->mount_args;
815 struct mmc_trackinfo first_track;
816 struct mmc_trackinfo second_track;
817 struct mmc_trackinfo last_track;
818 struct anchor_vdp **anchorsp;
819 uint32_t track_start;
820 uint32_t track_end;
821 uint32_t positions[4];
822 int first_tracknr, last_tracknr;
823 int error, anch, ok, first_anchor;
824
825 /* search the first and last track of the specified session */
826 error = udf_search_tracks(ump, args, &first_tracknr, &last_tracknr);
827 if (!error) {
828 first_track.tracknr = first_tracknr;
829 error = udf_update_trackinfo(ump, &first_track);
830 }
831 if (!error) {
832 last_track.tracknr = last_tracknr;
833 error = udf_update_trackinfo(ump, &last_track);
834 }
835 if ((!error) && (first_tracknr != last_tracknr)) {
836 second_track.tracknr = first_tracknr+1;
837 error = udf_update_trackinfo(ump, &second_track);
838 }
839 if (error) {
840 printf("UDF mount: reading disc geometry failed\n");
841 return 0;
842 }
843
844 track_start = first_track.track_start;
845
846 /* `end' is not as straitforward as start. */
847 track_end = last_track.track_start
848 + last_track.track_size - last_track.free_blocks - 1;
849
850 if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
851 /* end of track is not straitforward here */
852 if (last_track.flags & MMC_TRACKINFO_LRA_VALID)
853 track_end = last_track.last_recorded;
854 else if (last_track.flags & MMC_TRACKINFO_NWA_VALID)
855 track_end = last_track.next_writable
856 - ump->discinfo.link_block_penalty;
857 }
858
859 /* its no use reading a blank track */
860 first_anchor = 0;
861 if (first_track.flags & MMC_TRACKINFO_BLANK)
862 first_anchor = 1;
863
864 /* get our packet size */
865 ump->packet_size = first_track.packet_size;
866 if (first_track.flags & MMC_TRACKINFO_BLANK)
867 ump->packet_size = second_track.packet_size;
868
869 if (ump->packet_size <= 1) {
870 /* take max, but not bigger than 64 */
871 ump->packet_size = MAXPHYS / ump->discinfo.sector_size;
872 ump->packet_size = MIN(ump->packet_size, 64);
873 }
874 KASSERT(ump->packet_size >= 1);
875
876 /* read anchors start+256, start+512, end-256, end */
877 positions[0] = track_start+256;
878 positions[1] = track_end-256;
879 positions[2] = track_end;
880 positions[3] = track_start+512; /* [UDF 2.60/6.11.2] */
881 /* XXX shouldn't +512 be prefered above +256 for compat with Roxio CD */
882
883 ok = 0;
884 anchorsp = ump->anchors;
885 for (anch = first_anchor; anch < 4; anch++) {
886 DPRINTF(VOLUMES, ("Read anchor %d at sector %d\n", anch,
887 positions[anch]));
888 error = udf_read_anchor(ump, positions[anch], anchorsp);
889 if (!error) {
890 anchorsp++;
891 ok++;
892 }
893 }
894
895 /* VATs are only recorded on sequential media, but initialise */
896 ump->first_possible_vat_location = track_start + 2;
897 ump->last_possible_vat_location = track_end + last_track.packet_size;
898
899 return ok;
900 }
901
902 /* --------------------------------------------------------------------- */
903
904 /* we dont try to be smart; we just record the parts */
905 #define UDF_UPDATE_DSCR(name, dscr) \
906 if (name) \
907 free(name, M_UDFVOLD); \
908 name = dscr;
909
910 static int
911 udf_process_vds_descriptor(struct udf_mount *ump, union dscrptr *dscr)
912 {
913 struct part_desc *part;
914 uint16_t phys_part, raw_phys_part;
915
916 DPRINTF(VOLUMES, ("\tprocessing VDS descr %d\n",
917 udf_rw16(dscr->tag.id)));
918 switch (udf_rw16(dscr->tag.id)) {
919 case TAGID_PRI_VOL : /* primary partition */
920 UDF_UPDATE_DSCR(ump->primary_vol, &dscr->pvd);
921 break;
922 case TAGID_LOGVOL : /* logical volume */
923 UDF_UPDATE_DSCR(ump->logical_vol, &dscr->lvd);
924 break;
925 case TAGID_UNALLOC_SPACE : /* unallocated space */
926 UDF_UPDATE_DSCR(ump->unallocated, &dscr->usd);
927 break;
928 case TAGID_IMP_VOL : /* implementation */
929 /* XXX do we care about multiple impl. descr ? */
930 UDF_UPDATE_DSCR(ump->implementation, &dscr->ivd);
931 break;
932 case TAGID_PARTITION : /* physical partition */
933 /* not much use if its not allocated */
934 if ((udf_rw16(dscr->pd.flags) & UDF_PART_FLAG_ALLOCATED) == 0) {
935 free(dscr, M_UDFVOLD);
936 break;
937 }
938
939 /*
940 * BUGALERT: some rogue implementations use random physical
941 * partion numbers to break other implementations so lookup
942 * the number.
943 */
944 raw_phys_part = udf_rw16(dscr->pd.part_num);
945 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
946 part = ump->partitions[phys_part];
947 if (part == NULL)
948 break;
949 if (udf_rw16(part->part_num) == raw_phys_part)
950 break;
951 }
952 if (phys_part == UDF_PARTITIONS) {
953 free(dscr, M_UDFVOLD);
954 return EINVAL;
955 }
956
957 UDF_UPDATE_DSCR(ump->partitions[phys_part], &dscr->pd);
958 break;
959 case TAGID_VOL : /* volume space extender; rare */
960 DPRINTF(VOLUMES, ("VDS extender ignored\n"));
961 free(dscr, M_UDFVOLD);
962 break;
963 default :
964 DPRINTF(VOLUMES, ("Unhandled VDS type %d\n",
965 udf_rw16(dscr->tag.id)));
966 free(dscr, M_UDFVOLD);
967 }
968
969 return 0;
970 }
971 #undef UDF_UPDATE_DSCR
972
973 /* --------------------------------------------------------------------- */
974
975 static int
976 udf_read_vds_extent(struct udf_mount *ump, uint32_t loc, uint32_t len)
977 {
978 union dscrptr *dscr;
979 uint32_t sector_size, dscr_size;
980 int error;
981
982 sector_size = ump->discinfo.sector_size;
983
984 /* loc is sectornr, len is in bytes */
985 error = EIO;
986 while (len) {
987 error = udf_read_phys_dscr(ump, loc, M_UDFVOLD, &dscr);
988 if (error)
989 return error;
990
991 /* blank block is a terminator */
992 if (dscr == NULL)
993 return 0;
994
995 /* TERM descriptor is a terminator */
996 if (udf_rw16(dscr->tag.id) == TAGID_TERM) {
997 free(dscr, M_UDFVOLD);
998 return 0;
999 }
1000
1001 /* process all others */
1002 dscr_size = udf_tagsize(dscr, sector_size);
1003 error = udf_process_vds_descriptor(ump, dscr);
1004 if (error) {
1005 free(dscr, M_UDFVOLD);
1006 break;
1007 }
1008 assert((dscr_size % sector_size) == 0);
1009
1010 len -= dscr_size;
1011 loc += dscr_size / sector_size;
1012 }
1013
1014 return error;
1015 }
1016
1017
1018 int
1019 udf_read_vds_space(struct udf_mount *ump)
1020 {
1021 /* struct udf_args *args = &ump->mount_args; */
1022 struct anchor_vdp *anchor, *anchor2;
1023 size_t size;
1024 uint32_t main_loc, main_len;
1025 uint32_t reserve_loc, reserve_len;
1026 int error;
1027
1028 /*
1029 * read in VDS space provided by the anchors; if one descriptor read
1030 * fails, try the mirror sector.
1031 *
1032 * check if 2nd anchor is different from 1st; if so, go for 2nd. This
1033 * avoids the `compatibility features' of DirectCD that may confuse
1034 * stuff completely.
1035 */
1036
1037 anchor = ump->anchors[0];
1038 anchor2 = ump->anchors[1];
1039 assert(anchor);
1040
1041 if (anchor2) {
1042 size = sizeof(struct extent_ad);
1043 if (memcmp(&anchor->main_vds_ex, &anchor2->main_vds_ex, size))
1044 anchor = anchor2;
1045 /* reserve is specified to be a literal copy of main */
1046 }
1047
1048 main_loc = udf_rw32(anchor->main_vds_ex.loc);
1049 main_len = udf_rw32(anchor->main_vds_ex.len);
1050
1051 reserve_loc = udf_rw32(anchor->reserve_vds_ex.loc);
1052 reserve_len = udf_rw32(anchor->reserve_vds_ex.len);
1053
1054 error = udf_read_vds_extent(ump, main_loc, main_len);
1055 if (error) {
1056 printf("UDF mount: reading in reserve VDS extent\n");
1057 error = udf_read_vds_extent(ump, reserve_loc, reserve_len);
1058 }
1059
1060 return error;
1061 }
1062
1063 /* --------------------------------------------------------------------- */
1064
1065 /*
1066 * Read in the logical volume integrity sequence pointed to by our logical
1067 * volume descriptor. Its a sequence that can be extended using fields in the
1068 * integrity descriptor itself. On sequential media only one is found, on
1069 * rewritable media a sequence of descriptors can be found as a form of
1070 * history keeping and on non sequential write-once media the chain is vital
1071 * to allow more and more descriptors to be written. The last descriptor
1072 * written in an extent needs to claim space for a new extent.
1073 */
1074
1075 static int
1076 udf_retrieve_lvint(struct udf_mount *ump)
1077 {
1078 union dscrptr *dscr;
1079 struct logvol_int_desc *lvint;
1080 struct udf_lvintq *trace;
1081 uint32_t lb_size, lbnum, len;
1082 int dscr_type, error, trace_len;
1083
1084 lb_size = udf_rw32(ump->logical_vol->lb_size);
1085 len = udf_rw32(ump->logical_vol->integrity_seq_loc.len);
1086 lbnum = udf_rw32(ump->logical_vol->integrity_seq_loc.loc);
1087
1088 /* clean trace */
1089 memset(ump->lvint_trace, 0,
1090 UDF_LVDINT_SEGMENTS * sizeof(struct udf_lvintq));
1091
1092 trace_len = 0;
1093 trace = ump->lvint_trace;
1094 trace->start = lbnum;
1095 trace->end = lbnum + len/lb_size;
1096 trace->pos = 0;
1097 trace->wpos = 0;
1098
1099 lvint = NULL;
1100 dscr = NULL;
1101 error = 0;
1102 while (len) {
1103 trace->pos = lbnum - trace->start;
1104 trace->wpos = trace->pos + 1;
1105
1106 /* read in our integrity descriptor */
1107 error = udf_read_phys_dscr(ump, lbnum, M_UDFVOLD, &dscr);
1108 if (!error) {
1109 if (dscr == NULL) {
1110 trace->wpos = trace->pos;
1111 break; /* empty terminates */
1112 }
1113 dscr_type = udf_rw16(dscr->tag.id);
1114 if (dscr_type == TAGID_TERM) {
1115 trace->wpos = trace->pos;
1116 break; /* clean terminator */
1117 }
1118 if (dscr_type != TAGID_LOGVOL_INTEGRITY) {
1119 /* fatal... corrupt disc */
1120 error = ENOENT;
1121 break;
1122 }
1123 if (lvint)
1124 free(lvint, M_UDFVOLD);
1125 lvint = &dscr->lvid;
1126 dscr = NULL;
1127 } /* else hope for the best... maybe the next is ok */
1128
1129 DPRINTFIF(VOLUMES, lvint, ("logvol integrity read, state %s\n",
1130 udf_rw32(lvint->integrity_type) ? "CLOSED" : "OPEN"));
1131
1132 /* proceed sequential */
1133 lbnum += 1;
1134 len -= lb_size;
1135
1136 /* are we linking to a new piece? */
1137 if (dscr && lvint->next_extent.len) {
1138 len = udf_rw32(lvint->next_extent.len);
1139 lbnum = udf_rw32(lvint->next_extent.loc);
1140
1141 if (trace_len >= UDF_LVDINT_SEGMENTS-1) {
1142 /* IEK! segment link full... */
1143 DPRINTF(VOLUMES, ("lvdint segments full\n"));
1144 error = EINVAL;
1145 } else {
1146 trace++;
1147 trace_len++;
1148
1149 trace->start = lbnum;
1150 trace->end = lbnum + len/lb_size;
1151 trace->pos = 0;
1152 trace->wpos = 0;
1153 }
1154 }
1155 }
1156
1157 /* clean up the mess, esp. when there is an error */
1158 if (dscr)
1159 free(dscr, M_UDFVOLD);
1160
1161 if (error && lvint) {
1162 free(lvint, M_UDFVOLD);
1163 lvint = NULL;
1164 }
1165
1166 if (!lvint)
1167 error = ENOENT;
1168
1169 ump->logvol_integrity = lvint;
1170 return error;
1171 }
1172
1173
1174 static int
1175 udf_loose_lvint_history(struct udf_mount *ump)
1176 {
1177 union dscrptr **bufs, *dscr, *last_dscr;
1178 struct udf_lvintq *trace, *in_trace, *out_trace;
1179 struct logvol_int_desc *lvint;
1180 uint32_t in_ext, in_pos, in_len;
1181 uint32_t out_ext, out_wpos, out_len;
1182 uint32_t lb_size, packet_size, lb_num;
1183 uint32_t len, start;
1184 int ext, minext, extlen, cnt, cpy_len, dscr_type;
1185 int losing;
1186 int error;
1187
1188 DPRINTF(VOLUMES, ("need to lose some lvint history\n"));
1189
1190 lb_size = udf_rw32(ump->logical_vol->lb_size);
1191 packet_size = ump->data_track.packet_size; /* XXX data track */
1192
1193 /* search smallest extent */
1194 trace = &ump->lvint_trace[0];
1195 minext = trace->end - trace->start;
1196 for (ext = 1; ext < UDF_LVDINT_SEGMENTS; ext++) {
1197 trace = &ump->lvint_trace[ext];
1198 extlen = trace->end - trace->start;
1199 if (extlen == 0)
1200 break;
1201 minext = MIN(minext, extlen);
1202 }
1203 losing = MIN(minext, UDF_LVINT_LOSSAGE);
1204 /* no sense wiping all */
1205 if (losing == minext)
1206 losing--;
1207
1208 DPRINTF(VOLUMES, ("\tlosing %d entries\n", losing));
1209
1210 /* get buffer for pieces */
1211 bufs = malloc(UDF_LVDINT_SEGMENTS * sizeof(void *), M_TEMP, M_WAITOK);
1212
1213 in_ext = 0;
1214 in_pos = losing;
1215 in_trace = &ump->lvint_trace[in_ext];
1216 in_len = in_trace->end - in_trace->start;
1217 out_ext = 0;
1218 out_wpos = 0;
1219 out_trace = &ump->lvint_trace[out_ext];
1220 out_len = out_trace->end - out_trace->start;
1221
1222 last_dscr = NULL;
1223 for(;;) {
1224 out_trace->pos = out_wpos;
1225 out_trace->wpos = out_trace->pos;
1226 if (in_pos >= in_len) {
1227 in_ext++;
1228 in_pos = 0;
1229 in_trace = &ump->lvint_trace[in_ext];
1230 in_len = in_trace->end - in_trace->start;
1231 }
1232 if (out_wpos >= out_len) {
1233 out_ext++;
1234 out_wpos = 0;
1235 out_trace = &ump->lvint_trace[out_ext];
1236 out_len = out_trace->end - out_trace->start;
1237 }
1238 /* copy overlap contents */
1239 cpy_len = MIN(in_len - in_pos, out_len - out_wpos);
1240 cpy_len = MIN(cpy_len, in_len - in_trace->pos);
1241 if (cpy_len == 0)
1242 break;
1243
1244 /* copy */
1245 DPRINTF(VOLUMES, ("\treading %d lvid descriptors\n", cpy_len));
1246 for (cnt = 0; cnt < cpy_len; cnt++) {
1247 /* read in our integrity descriptor */
1248 lb_num = in_trace->start + in_pos + cnt;
1249 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD,
1250 &dscr);
1251 if (error) {
1252 /* copy last one */
1253 dscr = last_dscr;
1254 }
1255 bufs[cnt] = dscr;
1256 if (!error) {
1257 if (dscr == NULL) {
1258 out_trace->pos = out_wpos + cnt;
1259 out_trace->wpos = out_trace->pos;
1260 break; /* empty terminates */
1261 }
1262 dscr_type = udf_rw16(dscr->tag.id);
1263 if (dscr_type == TAGID_TERM) {
1264 out_trace->pos = out_wpos + cnt;
1265 out_trace->wpos = out_trace->pos;
1266 break; /* clean terminator */
1267 }
1268 if (dscr_type != TAGID_LOGVOL_INTEGRITY) {
1269 panic( "UDF integrity sequence "
1270 "corrupted while mounted!\n");
1271 }
1272 last_dscr = dscr;
1273 }
1274 }
1275
1276 /* patch up if first entry was on error */
1277 if (bufs[0] == NULL) {
1278 for (cnt = 0; cnt < cpy_len; cnt++)
1279 if (bufs[cnt] != NULL)
1280 break;
1281 last_dscr = bufs[cnt];
1282 for (; cnt > 0; cnt--) {
1283 bufs[cnt] = last_dscr;
1284 }
1285 }
1286
1287 /* glue + write out */
1288 DPRINTF(VOLUMES, ("\twriting %d lvid descriptors\n", cpy_len));
1289 for (cnt = 0; cnt < cpy_len; cnt++) {
1290 lb_num = out_trace->start + out_wpos + cnt;
1291 lvint = &bufs[cnt]->lvid;
1292
1293 /* set continuation */
1294 len = 0;
1295 start = 0;
1296 if (out_wpos + cnt == out_len) {
1297 /* get continuation */
1298 trace = &ump->lvint_trace[out_ext+1];
1299 len = trace->end - trace->start;
1300 start = trace->start;
1301 }
1302 lvint->next_extent.len = udf_rw32(len);
1303 lvint->next_extent.loc = udf_rw32(start);
1304
1305 lb_num = trace->start + trace->wpos;
1306 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
1307 bufs[cnt], lb_num, lb_num);
1308 DPRINTFIF(VOLUMES, error,
1309 ("error writing lvint lb_num\n"));
1310 }
1311
1312 /* free non repeating descriptors */
1313 last_dscr = NULL;
1314 for (cnt = 0; cnt < cpy_len; cnt++) {
1315 if (bufs[cnt] != last_dscr)
1316 free(bufs[cnt], M_UDFVOLD);
1317 last_dscr = bufs[cnt];
1318 }
1319
1320 /* advance */
1321 in_pos += cpy_len;
1322 out_wpos += cpy_len;
1323 }
1324
1325 free(bufs, M_TEMP);
1326
1327 return 0;
1328 }
1329
1330
1331 static int
1332 udf_writeout_lvint(struct udf_mount *ump, int lvflag)
1333 {
1334 struct udf_lvintq *trace;
1335 struct timeval now_v;
1336 struct timespec now_s;
1337 uint32_t sector;
1338 int logvol_integrity;
1339 int space, error;
1340
1341 DPRINTF(VOLUMES, ("writing out logvol integrity descriptor\n"));
1342
1343 again:
1344 /* get free space in last chunk */
1345 trace = ump->lvint_trace;
1346 while (trace->wpos > (trace->end - trace->start)) {
1347 DPRINTF(VOLUMES, ("skip : start = %d, end = %d, pos = %d, "
1348 "wpos = %d\n", trace->start, trace->end,
1349 trace->pos, trace->wpos));
1350 trace++;
1351 }
1352
1353 /* check if there is space to append */
1354 space = (trace->end - trace->start) - trace->wpos;
1355 DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, "
1356 "space = %d\n", trace->start, trace->end, trace->pos,
1357 trace->wpos, space));
1358
1359 /* get state */
1360 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
1361 if (logvol_integrity == UDF_INTEGRITY_CLOSED) {
1362 if ((space < 3) && (lvflag & UDF_APPENDONLY_LVINT)) {
1363 /* don't allow this logvol to be opened */
1364 /* TODO extent LVINT space if possible */
1365 return EROFS;
1366 }
1367 }
1368
1369 if (space < 1) {
1370 if (lvflag & UDF_APPENDONLY_LVINT)
1371 return EROFS;
1372 /* loose history by re-writing extents */
1373 error = udf_loose_lvint_history(ump);
1374 if (error)
1375 return error;
1376 goto again;
1377 }
1378
1379 /* update our integrity descriptor to identify us and timestamp it */
1380 DPRINTF(VOLUMES, ("updating integrity descriptor\n"));
1381 microtime(&now_v);
1382 TIMEVAL_TO_TIMESPEC(&now_v, &now_s);
1383 udf_timespec_to_timestamp(&now_s, &ump->logvol_integrity->time);
1384 udf_set_regid(&ump->logvol_info->impl_id, IMPL_NAME);
1385 udf_add_impl_regid(ump, &ump->logvol_info->impl_id);
1386
1387 /* writeout integrity descriptor */
1388 sector = trace->start + trace->wpos;
1389 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
1390 (union dscrptr *) ump->logvol_integrity,
1391 sector, sector);
1392 DPRINTF(VOLUMES, ("writeout lvint : error = %d\n", error));
1393 if (error)
1394 return error;
1395
1396 /* advance write position */
1397 trace->wpos++; space--;
1398 if (space >= 1) {
1399 /* append terminator */
1400 sector = trace->start + trace->wpos;
1401 error = udf_write_terminator(ump, sector);
1402
1403 DPRINTF(VOLUMES, ("write terminator : error = %d\n", error));
1404 }
1405
1406 space = (trace->end - trace->start) - trace->wpos;
1407 DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, "
1408 "space = %d\n", trace->start, trace->end, trace->pos,
1409 trace->wpos, space));
1410 DPRINTF(VOLUMES, ("finished writing out logvol integrity descriptor "
1411 "successfull\n"));
1412
1413 return error;
1414 }
1415
1416 /* --------------------------------------------------------------------- */
1417
1418 static int
1419 udf_read_physical_partition_spacetables(struct udf_mount *ump)
1420 {
1421 union dscrptr *dscr;
1422 /* struct udf_args *args = &ump->mount_args; */
1423 struct part_desc *partd;
1424 struct part_hdr_desc *parthdr;
1425 struct udf_bitmap *bitmap;
1426 uint32_t phys_part;
1427 uint32_t lb_num, len;
1428 int error, dscr_type;
1429
1430 /* unallocated space map */
1431 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
1432 partd = ump->partitions[phys_part];
1433 if (partd == NULL)
1434 continue;
1435 parthdr = &partd->_impl_use.part_hdr;
1436
1437 lb_num = udf_rw32(partd->start_loc);
1438 lb_num += udf_rw32(parthdr->unalloc_space_bitmap.lb_num);
1439 len = udf_rw32(parthdr->unalloc_space_bitmap.len);
1440 if (len == 0)
1441 continue;
1442
1443 DPRINTF(VOLUMES, ("Read unalloc. space bitmap %d\n", lb_num));
1444 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
1445 if (!error && dscr) {
1446 /* analyse */
1447 dscr_type = udf_rw16(dscr->tag.id);
1448 if (dscr_type == TAGID_SPACE_BITMAP) {
1449 DPRINTF(VOLUMES, ("Accepting space bitmap\n"));
1450 ump->part_unalloc_dscr[phys_part] = &dscr->sbd;
1451
1452 /* fill in ump->part_unalloc_bits */
1453 bitmap = &ump->part_unalloc_bits[phys_part];
1454 bitmap->blob = (uint8_t *) dscr;
1455 bitmap->bits = dscr->sbd.data;
1456 bitmap->max_offset = udf_rw32(dscr->sbd.num_bits);
1457 bitmap->pages = NULL; /* TODO */
1458 bitmap->data_pos = 0;
1459 bitmap->metadata_pos = 0;
1460 } else {
1461 free(dscr, M_UDFVOLD);
1462
1463 printf( "UDF mount: error reading unallocated "
1464 "space bitmap\n");
1465 return EROFS;
1466 }
1467 } else {
1468 /* blank not allowed */
1469 printf("UDF mount: blank unallocated space bitmap\n");
1470 return EROFS;
1471 }
1472 }
1473
1474 /* unallocated space table (not supported) */
1475 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
1476 partd = ump->partitions[phys_part];
1477 if (partd == NULL)
1478 continue;
1479 parthdr = &partd->_impl_use.part_hdr;
1480
1481 len = udf_rw32(parthdr->unalloc_space_table.len);
1482 if (len) {
1483 printf("UDF mount: space tables not supported\n");
1484 return EROFS;
1485 }
1486 }
1487
1488 /* freed space map */
1489 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
1490 partd = ump->partitions[phys_part];
1491 if (partd == NULL)
1492 continue;
1493 parthdr = &partd->_impl_use.part_hdr;
1494
1495 /* freed space map */
1496 lb_num = udf_rw32(partd->start_loc);
1497 lb_num += udf_rw32(parthdr->freed_space_bitmap.lb_num);
1498 len = udf_rw32(parthdr->freed_space_bitmap.len);
1499 if (len == 0)
1500 continue;
1501
1502 DPRINTF(VOLUMES, ("Read unalloc. space bitmap %d\n", lb_num));
1503 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
1504 if (!error && dscr) {
1505 /* analyse */
1506 dscr_type = udf_rw16(dscr->tag.id);
1507 if (dscr_type == TAGID_SPACE_BITMAP) {
1508 DPRINTF(VOLUMES, ("Accepting space bitmap\n"));
1509 ump->part_freed_dscr[phys_part] = &dscr->sbd;
1510
1511 /* fill in ump->part_freed_bits */
1512 bitmap = &ump->part_unalloc_bits[phys_part];
1513 bitmap->blob = (uint8_t *) dscr;
1514 bitmap->bits = dscr->sbd.data;
1515 bitmap->max_offset = udf_rw32(dscr->sbd.num_bits);
1516 bitmap->pages = NULL; /* TODO */
1517 bitmap->data_pos = 0;
1518 bitmap->metadata_pos = 0;
1519 } else {
1520 free(dscr, M_UDFVOLD);
1521
1522 printf( "UDF mount: error reading freed "
1523 "space bitmap\n");
1524 return EROFS;
1525 }
1526 } else {
1527 /* blank not allowed */
1528 printf("UDF mount: blank freed space bitmap\n");
1529 return EROFS;
1530 }
1531 }
1532
1533 /* freed space table (not supported) */
1534 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
1535 partd = ump->partitions[phys_part];
1536 if (partd == NULL)
1537 continue;
1538 parthdr = &partd->_impl_use.part_hdr;
1539
1540 len = udf_rw32(parthdr->freed_space_table.len);
1541 if (len) {
1542 printf("UDF mount: space tables not supported\n");
1543 return EROFS;
1544 }
1545 }
1546
1547 return 0;
1548 }
1549
1550
1551 /* TODO implement async writeout */
1552 int
1553 udf_write_physical_partition_spacetables(struct udf_mount *ump, int waitfor)
1554 {
1555 union dscrptr *dscr;
1556 /* struct udf_args *args = &ump->mount_args; */
1557 struct part_desc *partd;
1558 struct part_hdr_desc *parthdr;
1559 uint32_t phys_part;
1560 uint32_t lb_num, len, ptov;
1561 int error_all, error;
1562
1563 error_all = 0;
1564 /* unallocated space map */
1565 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
1566 partd = ump->partitions[phys_part];
1567 if (partd == NULL)
1568 continue;
1569 parthdr = &partd->_impl_use.part_hdr;
1570
1571 ptov = udf_rw32(partd->start_loc);
1572 lb_num = udf_rw32(parthdr->unalloc_space_bitmap.lb_num);
1573 len = udf_rw32(parthdr->unalloc_space_bitmap.len);
1574 if (len == 0)
1575 continue;
1576
1577 DPRINTF(VOLUMES, ("Write unalloc. space bitmap %d\n",
1578 lb_num + ptov));
1579 dscr = (union dscrptr *) ump->part_unalloc_dscr[phys_part];
1580 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
1581 (union dscrptr *) dscr,
1582 ptov + lb_num, lb_num);
1583 if (error) {
1584 DPRINTF(VOLUMES, ("\tfailed!! (error %d)\n", error));
1585 error_all = error;
1586 }
1587 }
1588
1589 /* freed space map */
1590 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
1591 partd = ump->partitions[phys_part];
1592 if (partd == NULL)
1593 continue;
1594 parthdr = &partd->_impl_use.part_hdr;
1595
1596 /* freed space map */
1597 ptov = udf_rw32(partd->start_loc);
1598 lb_num = udf_rw32(parthdr->freed_space_bitmap.lb_num);
1599 len = udf_rw32(parthdr->freed_space_bitmap.len);
1600 if (len == 0)
1601 continue;
1602
1603 DPRINTF(VOLUMES, ("Write freed space bitmap %d\n",
1604 lb_num + ptov));
1605 dscr = (union dscrptr *) ump->part_freed_dscr[phys_part];
1606 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
1607 (union dscrptr *) dscr,
1608 ptov + lb_num, lb_num);
1609 if (error) {
1610 DPRINTF(VOLUMES, ("\tfailed!! (error %d)\n", error));
1611 error_all = error;
1612 }
1613 }
1614
1615 return error_all;
1616 }
1617
1618
1619 static int
1620 udf_read_metadata_partition_spacetable(struct udf_mount *ump)
1621 {
1622 struct udf_node *bitmap_node;
1623 union dscrptr *dscr;
1624 struct udf_bitmap *bitmap;
1625 uint64_t inflen;
1626 int error, dscr_type;
1627
1628 bitmap_node = ump->metadatabitmap_node;
1629
1630 /* only read in when metadata bitmap node is read in */
1631 if (bitmap_node == NULL)
1632 return 0;
1633
1634 if (bitmap_node->fe) {
1635 inflen = udf_rw64(bitmap_node->fe->inf_len);
1636 } else {
1637 KASSERT(bitmap_node->efe);
1638 inflen = udf_rw64(bitmap_node->efe->inf_len);
1639 }
1640
1641 DPRINTF(VOLUMES, ("Reading metadata space bitmap for "
1642 "%"PRIu64" bytes\n", inflen));
1643
1644 /* allocate space for bitmap */
1645 dscr = malloc(inflen, M_UDFVOLD, M_CANFAIL | M_WAITOK);
1646 if (!dscr)
1647 return ENOMEM;
1648
1649 /* set vnode type to regular file or we can't read from it! */
1650 bitmap_node->vnode->v_type = VREG;
1651
1652 /* read in complete metadata bitmap file */
1653 error = vn_rdwr(UIO_READ, bitmap_node->vnode,
1654 dscr,
1655 inflen, 0,
1656 UIO_SYSSPACE,
1657 IO_SYNC | IO_NODELOCKED | IO_ALTSEMANTICS, FSCRED,
1658 NULL, NULL);
1659 if (error) {
1660 DPRINTF(VOLUMES, ("Error reading metadata space bitmap\n"));
1661 goto errorout;
1662 }
1663
1664 /* analyse */
1665 dscr_type = udf_rw16(dscr->tag.id);
1666 if (dscr_type == TAGID_SPACE_BITMAP) {
1667 DPRINTF(VOLUMES, ("Accepting metadata space bitmap\n"));
1668 ump->metadata_unalloc_dscr = &dscr->sbd;
1669
1670 /* fill in bitmap bits */
1671 bitmap = &ump->metadata_unalloc_bits;
1672 bitmap->blob = (uint8_t *) dscr;
1673 bitmap->bits = dscr->sbd.data;
1674 bitmap->max_offset = udf_rw32(dscr->sbd.num_bits);
1675 bitmap->pages = NULL; /* TODO */
1676 bitmap->data_pos = 0;
1677 bitmap->metadata_pos = 0;
1678 } else {
1679 DPRINTF(VOLUMES, ("No valid bitmap found!\n"));
1680 goto errorout;
1681 }
1682
1683 return 0;
1684
1685 errorout:
1686 free(dscr, M_UDFVOLD);
1687 printf( "UDF mount: error reading unallocated "
1688 "space bitmap for metadata partition\n");
1689 return EROFS;
1690 }
1691
1692
1693 int
1694 udf_write_metadata_partition_spacetable(struct udf_mount *ump, int waitfor)
1695 {
1696 struct udf_node *bitmap_node;
1697 union dscrptr *dscr;
1698 uint64_t inflen, new_inflen;
1699 int dummy, error;
1700
1701 bitmap_node = ump->metadatabitmap_node;
1702
1703 /* only write out when metadata bitmap node is known */
1704 if (bitmap_node == NULL)
1705 return 0;
1706
1707 if (bitmap_node->fe) {
1708 inflen = udf_rw64(bitmap_node->fe->inf_len);
1709 } else {
1710 KASSERT(bitmap_node->efe);
1711 inflen = udf_rw64(bitmap_node->efe->inf_len);
1712 }
1713
1714 /* reduce length to zero */
1715 dscr = (union dscrptr *) ump->metadata_unalloc_dscr;
1716 new_inflen = udf_tagsize(dscr, 1);
1717
1718 DPRINTF(VOLUMES, ("Resize and write out metadata space bitmap from "
1719 "%"PRIu64" to %"PRIu64" bytes\n", inflen, new_inflen));
1720
1721 error = udf_resize_node(bitmap_node, new_inflen, &dummy);
1722 if (error)
1723 printf("Error resizing metadata space bitmap\n");
1724
1725 error = vn_rdwr(UIO_WRITE, bitmap_node->vnode,
1726 dscr,
1727 new_inflen, 0,
1728 UIO_SYSSPACE,
1729 IO_NODELOCKED | IO_ALTSEMANTICS, FSCRED,
1730 NULL, NULL);
1731
1732 bitmap_node->i_flags |= IN_MODIFIED;
1733 vflushbuf(bitmap_node->vnode, 1 /* sync */);
1734
1735 error = VOP_FSYNC(bitmap_node->vnode,
1736 FSCRED, FSYNC_WAIT, 0, 0);
1737
1738 if (error)
1739 printf( "Error writing out metadata partition unalloced "
1740 "space bitmap!\n");
1741
1742 return error;
1743 }
1744
1745
1746 /* --------------------------------------------------------------------- */
1747
1748 /*
1749 * Checks if ump's vds information is correct and complete
1750 */
1751
1752 int
1753 udf_process_vds(struct udf_mount *ump) {
1754 union udf_pmap *mapping;
1755 /* struct udf_args *args = &ump->mount_args; */
1756 struct logvol_int_desc *lvint;
1757 struct udf_logvol_info *lvinfo;
1758 struct part_desc *part;
1759 uint32_t n_pm, mt_l;
1760 uint8_t *pmap_pos;
1761 char *domain_name, *map_name;
1762 const char *check_name;
1763 char bits[128];
1764 int pmap_stype, pmap_size;
1765 int pmap_type, log_part, phys_part, raw_phys_part, maps_on;
1766 int n_phys, n_virt, n_spar, n_meta;
1767 int len, error;
1768
1769 if (ump == NULL)
1770 return ENOENT;
1771
1772 /* we need at least an anchor (trivial, but for safety) */
1773 if (ump->anchors[0] == NULL)
1774 return EINVAL;
1775
1776 /* we need at least one primary and one logical volume descriptor */
1777 if ((ump->primary_vol == NULL) || (ump->logical_vol) == NULL)
1778 return EINVAL;
1779
1780 /* we need at least one partition descriptor */
1781 if (ump->partitions[0] == NULL)
1782 return EINVAL;
1783
1784 /* check logical volume sector size verses device sector size */
1785 if (udf_rw32(ump->logical_vol->lb_size) != ump->discinfo.sector_size) {
1786 printf("UDF mount: format violation, lb_size != sector size\n");
1787 return EINVAL;
1788 }
1789
1790 /* check domain name */
1791 domain_name = ump->logical_vol->domain_id.id;
1792 if (strncmp(domain_name, "*OSTA UDF Compliant", 20)) {
1793 printf("mount_udf: disc not OSTA UDF Compliant, aborting\n");
1794 return EINVAL;
1795 }
1796
1797 /* retrieve logical volume integrity sequence */
1798 error = udf_retrieve_lvint(ump);
1799
1800 /*
1801 * We need at least one logvol integrity descriptor recorded. Note
1802 * that its OK to have an open logical volume integrity here. The VAT
1803 * will close/update the integrity.
1804 */
1805 if (ump->logvol_integrity == NULL)
1806 return EINVAL;
1807
1808 /* process derived structures */
1809 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
1810 lvint = ump->logvol_integrity;
1811 lvinfo = (struct udf_logvol_info *) (&lvint->tables[2 * n_pm]);
1812 ump->logvol_info = lvinfo;
1813
1814 /* TODO check udf versions? */
1815
1816 /*
1817 * check logvol mappings: effective virt->log partmap translation
1818 * check and recording of the mapping results. Saves expensive
1819 * strncmp() in tight places.
1820 */
1821 DPRINTF(VOLUMES, ("checking logvol mappings\n"));
1822 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
1823 mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */
1824 pmap_pos = ump->logical_vol->maps;
1825
1826 if (n_pm > UDF_PMAPS) {
1827 printf("UDF mount: too many mappings\n");
1828 return EINVAL;
1829 }
1830
1831 /* count types and set partition numbers */
1832 ump->data_part = ump->node_part = ump->fids_part = 0;
1833 n_phys = n_virt = n_spar = n_meta = 0;
1834 for (log_part = 0; log_part < n_pm; log_part++) {
1835 mapping = (union udf_pmap *) pmap_pos;
1836 pmap_stype = pmap_pos[0];
1837 pmap_size = pmap_pos[1];
1838 switch (pmap_stype) {
1839 case 1: /* physical mapping */
1840 /* volseq = udf_rw16(mapping->pm1.vol_seq_num); */
1841 raw_phys_part = udf_rw16(mapping->pm1.part_num);
1842 pmap_type = UDF_VTOP_TYPE_PHYS;
1843 n_phys++;
1844 ump->data_part = log_part;
1845 ump->node_part = log_part;
1846 ump->fids_part = log_part;
1847 break;
1848 case 2: /* virtual/sparable/meta mapping */
1849 map_name = mapping->pm2.part_id.id;
1850 /* volseq = udf_rw16(mapping->pm2.vol_seq_num); */
1851 raw_phys_part = udf_rw16(mapping->pm2.part_num);
1852 pmap_type = UDF_VTOP_TYPE_UNKNOWN;
1853 len = UDF_REGID_ID_SIZE;
1854
1855 check_name = "*UDF Virtual Partition";
1856 if (strncmp(map_name, check_name, len) == 0) {
1857 pmap_type = UDF_VTOP_TYPE_VIRT;
1858 n_virt++;
1859 ump->node_part = log_part;
1860 break;
1861 }
1862 check_name = "*UDF Sparable Partition";
1863 if (strncmp(map_name, check_name, len) == 0) {
1864 pmap_type = UDF_VTOP_TYPE_SPARABLE;
1865 n_spar++;
1866 ump->data_part = log_part;
1867 ump->node_part = log_part;
1868 ump->fids_part = log_part;
1869 break;
1870 }
1871 check_name = "*UDF Metadata Partition";
1872 if (strncmp(map_name, check_name, len) == 0) {
1873 pmap_type = UDF_VTOP_TYPE_META;
1874 n_meta++;
1875 ump->node_part = log_part;
1876 ump->fids_part = log_part;
1877 break;
1878 }
1879 break;
1880 default:
1881 return EINVAL;
1882 }
1883
1884 /*
1885 * BUGALERT: some rogue implementations use random physical
1886 * partion numbers to break other implementations so lookup
1887 * the number.
1888 */
1889 for (phys_part = 0; phys_part < UDF_PARTITIONS; phys_part++) {
1890 part = ump->partitions[phys_part];
1891 if (part == NULL)
1892 continue;
1893 if (udf_rw16(part->part_num) == raw_phys_part)
1894 break;
1895 }
1896
1897 DPRINTF(VOLUMES, ("\t%d -> %d(%d) type %d\n", log_part,
1898 raw_phys_part, phys_part, pmap_type));
1899
1900 if (phys_part == UDF_PARTITIONS)
1901 return EINVAL;
1902 if (pmap_type == UDF_VTOP_TYPE_UNKNOWN)
1903 return EINVAL;
1904
1905 ump->vtop [log_part] = phys_part;
1906 ump->vtop_tp[log_part] = pmap_type;
1907
1908 pmap_pos += pmap_size;
1909 }
1910 /* not winning the beauty contest */
1911 ump->vtop_tp[UDF_VTOP_RAWPART] = UDF_VTOP_TYPE_RAW;
1912
1913 /* test some basic UDF assertions/requirements */
1914 if ((n_virt > 1) || (n_spar > 1) || (n_meta > 1))
1915 return EINVAL;
1916
1917 if (n_virt) {
1918 if ((n_phys == 0) || n_spar || n_meta)
1919 return EINVAL;
1920 }
1921 if (n_spar + n_phys == 0)
1922 return EINVAL;
1923
1924 /* select allocation type for each logical partition */
1925 for (log_part = 0; log_part < n_pm; log_part++) {
1926 maps_on = ump->vtop[log_part];
1927 switch (ump->vtop_tp[log_part]) {
1928 case UDF_VTOP_TYPE_PHYS :
1929 assert(maps_on == log_part);
1930 ump->vtop_alloc[log_part] = UDF_ALLOC_SPACEMAP;
1931 break;
1932 case UDF_VTOP_TYPE_VIRT :
1933 ump->vtop_alloc[log_part] = UDF_ALLOC_VAT;
1934 ump->vtop_alloc[maps_on] = UDF_ALLOC_SEQUENTIAL;
1935 break;
1936 case UDF_VTOP_TYPE_SPARABLE :
1937 assert(maps_on == log_part);
1938 ump->vtop_alloc[log_part] = UDF_ALLOC_SPACEMAP;
1939 break;
1940 case UDF_VTOP_TYPE_META :
1941 ump->vtop_alloc[log_part] = UDF_ALLOC_METABITMAP;
1942 if (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE) {
1943 /* special case for UDF 2.60 */
1944 ump->vtop_alloc[log_part] = UDF_ALLOC_METASEQUENTIAL;
1945 ump->vtop_alloc[maps_on] = UDF_ALLOC_SEQUENTIAL;
1946 }
1947 break;
1948 default:
1949 panic("bad alloction type in udf's ump->vtop\n");
1950 }
1951 }
1952
1953 /* determine logical volume open/closure actions */
1954 if (n_virt) {
1955 ump->lvopen = 0;
1956 if (ump->discinfo.last_session_state == MMC_STATE_CLOSED)
1957 ump->lvopen |= UDF_OPEN_SESSION ;
1958 ump->lvclose = UDF_WRITE_VAT;
1959 if (ump->mount_args.udfmflags & UDFMNT_CLOSESESSION)
1960 ump->lvclose |= UDF_CLOSE_SESSION;
1961 } else {
1962 /* `normal' rewritable or non sequential media */
1963 ump->lvopen = UDF_WRITE_LVINT;
1964 ump->lvclose = UDF_WRITE_LVINT;
1965 if ((ump->discinfo.mmc_cur & MMC_CAP_REWRITABLE) == 0)
1966 ump->lvopen |= UDF_APPENDONLY_LVINT;
1967 }
1968
1969 /*
1970 * Determine sheduler error behaviour. For virtual partions, update
1971 * the trackinfo; for sparable partitions replace a whole block on the
1972 * sparable table. Allways requeue.
1973 */
1974 ump->lvreadwrite = 0;
1975 if (n_virt)
1976 ump->lvreadwrite = UDF_UPDATE_TRACKINFO;
1977 if (n_spar)
1978 ump->lvreadwrite = UDF_REMAP_BLOCK;
1979
1980 /*
1981 * Select our sheduler
1982 */
1983 ump->strategy = &udf_strat_rmw;
1984 if (n_virt || (ump->discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE))
1985 ump->strategy = &udf_strat_sequential;
1986 if ((ump->discinfo.mmc_class == MMC_CLASS_DISC) ||
1987 (ump->discinfo.mmc_class == MMC_CLASS_UNKN))
1988 ump->strategy = &udf_strat_direct;
1989 if (n_spar)
1990 ump->strategy = &udf_strat_rmw;
1991
1992 #if 0
1993 /* read-only access won't benefit from the other shedulers */
1994 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
1995 ump->strategy = &udf_strat_direct;
1996 #endif
1997
1998 /* print results */
1999 DPRINTF(VOLUMES, ("\tdata partition %d\n", ump->data_part));
2000 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->data_part]));
2001 DPRINTF(VOLUMES, ("\tnode partition %d\n", ump->node_part));
2002 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->node_part]));
2003 DPRINTF(VOLUMES, ("\tfids partition %d\n", ump->fids_part));
2004 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->fids_part]));
2005
2006 snprintb(bits, sizeof(bits), UDFLOGVOL_BITS, ump->lvopen);
2007 DPRINTF(VOLUMES, ("\tactions on logvol open %s\n", bits));
2008 snprintb(bits, sizeof(bits), UDFLOGVOL_BITS, ump->lvclose);
2009 DPRINTF(VOLUMES, ("\tactions on logvol close %s\n", bits));
2010 snprintb(bits, sizeof(bits), UDFONERROR_BITS, ump->lvreadwrite);
2011 DPRINTF(VOLUMES, ("\tactions on logvol errors %s\n", bits));
2012
2013 DPRINTF(VOLUMES, ("\tselected sheduler `%s`\n",
2014 (ump->strategy == &udf_strat_direct) ? "Direct" :
2015 (ump->strategy == &udf_strat_sequential) ? "Sequential" :
2016 (ump->strategy == &udf_strat_rmw) ? "RMW" : "UNKNOWN!"));
2017
2018 /* signal its OK for now */
2019 return 0;
2020 }
2021
2022 /* --------------------------------------------------------------------- */
2023
2024 /*
2025 * Update logical volume name in all structures that keep a record of it. We
2026 * use memmove since each of them might be specified as a source.
2027 *
2028 * Note that it doesn't update the VAT structure!
2029 */
2030
2031 static void
2032 udf_update_logvolname(struct udf_mount *ump, char *logvol_id)
2033 {
2034 struct logvol_desc *lvd = NULL;
2035 struct fileset_desc *fsd = NULL;
2036 struct udf_lv_info *lvi = NULL;
2037
2038 DPRINTF(VOLUMES, ("Updating logical volume name\n"));
2039 lvd = ump->logical_vol;
2040 fsd = ump->fileset_desc;
2041 if (ump->implementation)
2042 lvi = &ump->implementation->_impl_use.lv_info;
2043
2044 /* logvol's id might be specified as origional so use memmove here */
2045 memmove(lvd->logvol_id, logvol_id, 128);
2046 if (fsd)
2047 memmove(fsd->logvol_id, logvol_id, 128);
2048 if (lvi)
2049 memmove(lvi->logvol_id, logvol_id, 128);
2050 }
2051
2052 /* --------------------------------------------------------------------- */
2053
2054 void
2055 udf_inittag(struct udf_mount *ump, struct desc_tag *tag, int tagid,
2056 uint32_t sector)
2057 {
2058 assert(ump->logical_vol);
2059
2060 tag->id = udf_rw16(tagid);
2061 tag->descriptor_ver = ump->logical_vol->tag.descriptor_ver;
2062 tag->cksum = 0;
2063 tag->reserved = 0;
2064 tag->serial_num = ump->logical_vol->tag.serial_num;
2065 tag->tag_loc = udf_rw32(sector);
2066 }
2067
2068
2069 uint64_t
2070 udf_advance_uniqueid(struct udf_mount *ump)
2071 {
2072 uint64_t unique_id;
2073
2074 mutex_enter(&ump->logvol_mutex);
2075 unique_id = udf_rw64(ump->logvol_integrity->lvint_next_unique_id);
2076 if (unique_id < 0x10)
2077 unique_id = 0x10;
2078 ump->logvol_integrity->lvint_next_unique_id = udf_rw64(unique_id + 1);
2079 mutex_exit(&ump->logvol_mutex);
2080
2081 return unique_id;
2082 }
2083
2084
2085 static void
2086 udf_adjust_filecount(struct udf_node *udf_node, int sign)
2087 {
2088 struct udf_mount *ump = udf_node->ump;
2089 uint32_t num_dirs, num_files;
2090 int udf_file_type;
2091
2092 /* get file type */
2093 if (udf_node->fe) {
2094 udf_file_type = udf_node->fe->icbtag.file_type;
2095 } else {
2096 udf_file_type = udf_node->efe->icbtag.file_type;
2097 }
2098
2099 /* adjust file count */
2100 mutex_enter(&ump->allocate_mutex);
2101 if (udf_file_type == UDF_ICB_FILETYPE_DIRECTORY) {
2102 num_dirs = udf_rw32(ump->logvol_info->num_directories);
2103 ump->logvol_info->num_directories =
2104 udf_rw32((num_dirs + sign));
2105 } else {
2106 num_files = udf_rw32(ump->logvol_info->num_files);
2107 ump->logvol_info->num_files =
2108 udf_rw32((num_files + sign));
2109 }
2110 mutex_exit(&ump->allocate_mutex);
2111 }
2112
2113
2114 void
2115 udf_osta_charset(struct charspec *charspec)
2116 {
2117 bzero(charspec, sizeof(struct charspec));
2118 charspec->type = 0;
2119 strcpy((char *) charspec->inf, "OSTA Compressed Unicode");
2120 }
2121
2122
2123 /* first call udf_set_regid and then the suffix */
2124 void
2125 udf_set_regid(struct regid *regid, char const *name)
2126 {
2127 bzero(regid, sizeof(struct regid));
2128 regid->flags = 0; /* not dirty and not protected */
2129 strcpy((char *) regid->id, name);
2130 }
2131
2132
2133 void
2134 udf_add_domain_regid(struct udf_mount *ump, struct regid *regid)
2135 {
2136 uint16_t *ver;
2137
2138 ver = (uint16_t *) regid->id_suffix;
2139 *ver = ump->logvol_info->min_udf_readver;
2140 }
2141
2142
2143 void
2144 udf_add_udf_regid(struct udf_mount *ump, struct regid *regid)
2145 {
2146 uint16_t *ver;
2147
2148 ver = (uint16_t *) regid->id_suffix;
2149 *ver = ump->logvol_info->min_udf_readver;
2150
2151 regid->id_suffix[2] = 4; /* unix */
2152 regid->id_suffix[3] = 8; /* NetBSD */
2153 }
2154
2155
2156 void
2157 udf_add_impl_regid(struct udf_mount *ump, struct regid *regid)
2158 {
2159 regid->id_suffix[0] = 4; /* unix */
2160 regid->id_suffix[1] = 8; /* NetBSD */
2161 }
2162
2163
2164 void
2165 udf_add_app_regid(struct udf_mount *ump, struct regid *regid)
2166 {
2167 regid->id_suffix[0] = APP_VERSION_MAIN;
2168 regid->id_suffix[1] = APP_VERSION_SUB;
2169 }
2170
2171 static int
2172 udf_create_parentfid(struct udf_mount *ump, struct fileid_desc *fid,
2173 struct long_ad *parent, uint64_t unique_id)
2174 {
2175 /* the size of an empty FID is 38 but needs to be a multiple of 4 */
2176 int fidsize = 40;
2177
2178 udf_inittag(ump, &fid->tag, TAGID_FID, udf_rw32(parent->loc.lb_num));
2179 fid->file_version_num = udf_rw16(1); /* UDF 2.3.4.1 */
2180 fid->file_char = UDF_FILE_CHAR_DIR | UDF_FILE_CHAR_PAR;
2181 fid->icb = *parent;
2182 fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
2183 fid->tag.desc_crc_len = fidsize - UDF_DESC_TAG_LENGTH;
2184 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
2185
2186 return fidsize;
2187 }
2188
2189 /* --------------------------------------------------------------------- */
2190
2191 /*
2192 * Extended attribute support. UDF knows of 3 places for extended attributes:
2193 *
2194 * (a) inside the file's (e)fe in the length of the extended attribute area
2195 * before the allocation descriptors/filedata
2196 *
2197 * (b) in a file referenced by (e)fe->ext_attr_icb and
2198 *
2199 * (c) in the e(fe)'s associated stream directory that can hold various
2200 * sub-files. In the stream directory a few fixed named subfiles are reserved
2201 * for NT/Unix ACL's and OS/2 attributes.
2202 *
2203 * NOTE: Extended attributes are read randomly but allways written
2204 * *atomicaly*. For ACL's this interface is propably different but not known
2205 * to me yet.
2206 *
2207 * Order of extended attributes in a space :
2208 * ECMA 167 EAs
2209 * Non block aligned Implementation Use EAs
2210 * Block aligned Implementation Use EAs
2211 * Application Use EAs
2212 */
2213
2214 static int
2215 udf_impl_extattr_check(struct impl_extattr_entry *implext)
2216 {
2217 uint16_t *spos;
2218
2219 if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) {
2220 /* checksum valid? */
2221 DPRINTF(EXTATTR, ("checking UDF impl. attr checksum\n"));
2222 spos = (uint16_t *) implext->data;
2223 if (udf_rw16(*spos) != udf_ea_cksum((uint8_t *) implext))
2224 return EINVAL;
2225 }
2226 return 0;
2227 }
2228
2229 static void
2230 udf_calc_impl_extattr_checksum(struct impl_extattr_entry *implext)
2231 {
2232 uint16_t *spos;
2233
2234 if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) {
2235 /* set checksum */
2236 spos = (uint16_t *) implext->data;
2237 *spos = udf_rw16(udf_ea_cksum((uint8_t *) implext));
2238 }
2239 }
2240
2241
2242 int
2243 udf_extattr_search_intern(struct udf_node *node,
2244 uint32_t sattr, char const *sattrname,
2245 uint32_t *offsetp, uint32_t *lengthp)
2246 {
2247 struct extattrhdr_desc *eahdr;
2248 struct extattr_entry *attrhdr;
2249 struct impl_extattr_entry *implext;
2250 uint32_t offset, a_l, sector_size;
2251 int32_t l_ea;
2252 uint8_t *pos;
2253 int error;
2254
2255 /* get mountpoint */
2256 sector_size = node->ump->discinfo.sector_size;
2257
2258 /* get information from fe/efe */
2259 if (node->fe) {
2260 l_ea = udf_rw32(node->fe->l_ea);
2261 eahdr = (struct extattrhdr_desc *) node->fe->data;
2262 } else {
2263 assert(node->efe);
2264 l_ea = udf_rw32(node->efe->l_ea);
2265 eahdr = (struct extattrhdr_desc *) node->efe->data;
2266 }
2267
2268 /* something recorded here? */
2269 if (l_ea == 0)
2270 return ENOENT;
2271
2272 /* check extended attribute tag; what to do if it fails? */
2273 error = udf_check_tag(eahdr);
2274 if (error)
2275 return EINVAL;
2276 if (udf_rw16(eahdr->tag.id) != TAGID_EXTATTR_HDR)
2277 return EINVAL;
2278 error = udf_check_tag_payload(eahdr, sizeof(struct extattrhdr_desc));
2279 if (error)
2280 return EINVAL;
2281
2282 DPRINTF(EXTATTR, ("Found %d bytes of extended attributes\n", l_ea));
2283
2284 /* looking for Ecma-167 attributes? */
2285 offset = sizeof(struct extattrhdr_desc);
2286
2287 /* looking for either implemenation use or application use */
2288 if (sattr == 2048) { /* [4/48.10.8] */
2289 offset = udf_rw32(eahdr->impl_attr_loc);
2290 if (offset == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
2291 return ENOENT;
2292 }
2293 if (sattr == 65536) { /* [4/48.10.9] */
2294 offset = udf_rw32(eahdr->appl_attr_loc);
2295 if (offset == UDF_APPL_ATTR_LOC_NOT_PRESENT)
2296 return ENOENT;
2297 }
2298
2299 /* paranoia check offset and l_ea */
2300 if (l_ea + offset >= sector_size - sizeof(struct extattr_entry))
2301 return EINVAL;
2302
2303 DPRINTF(EXTATTR, ("Starting at offset %d\n", offset));
2304
2305 /* find our extended attribute */
2306 l_ea -= offset;
2307 pos = (uint8_t *) eahdr + offset;
2308
2309 while (l_ea >= sizeof(struct extattr_entry)) {
2310 DPRINTF(EXTATTR, ("%d extended attr bytes left\n", l_ea));
2311 attrhdr = (struct extattr_entry *) pos;
2312 implext = (struct impl_extattr_entry *) pos;
2313
2314 /* get complete attribute length and check for roque values */
2315 a_l = udf_rw32(attrhdr->a_l);
2316 DPRINTF(EXTATTR, ("attribute %d:%d, len %d/%d\n",
2317 udf_rw32(attrhdr->type),
2318 attrhdr->subtype, a_l, l_ea));
2319 if ((a_l == 0) || (a_l > l_ea))
2320 return EINVAL;
2321
2322 if (attrhdr->type != sattr)
2323 goto next_attribute;
2324
2325 /* we might have found it! */
2326 if (attrhdr->type < 2048) { /* Ecma-167 attribute */
2327 *offsetp = offset;
2328 *lengthp = a_l;
2329 return 0; /* success */
2330 }
2331
2332 /*
2333 * Implementation use and application use extended attributes
2334 * have a name to identify. They share the same structure only
2335 * UDF implementation use extended attributes have a checksum
2336 * we need to check
2337 */
2338
2339 DPRINTF(EXTATTR, ("named attribute %s\n", implext->imp_id.id));
2340 if (strcmp(implext->imp_id.id, sattrname) == 0) {
2341 /* we have found our appl/implementation attribute */
2342 *offsetp = offset;
2343 *lengthp = a_l;
2344 return 0; /* success */
2345 }
2346
2347 next_attribute:
2348 /* next attribute */
2349 pos += a_l;
2350 l_ea -= a_l;
2351 offset += a_l;
2352 }
2353 /* not found */
2354 return ENOENT;
2355 }
2356
2357
2358 static void
2359 udf_extattr_insert_internal(struct udf_mount *ump, union dscrptr *dscr,
2360 struct extattr_entry *extattr)
2361 {
2362 struct file_entry *fe;
2363 struct extfile_entry *efe;
2364 struct extattrhdr_desc *extattrhdr;
2365 struct impl_extattr_entry *implext;
2366 uint32_t impl_attr_loc, appl_attr_loc, l_ea, a_l, exthdr_len;
2367 uint32_t *l_eap, l_ad;
2368 uint16_t *spos;
2369 uint8_t *bpos, *data;
2370
2371 if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
2372 fe = &dscr->fe;
2373 data = fe->data;
2374 l_eap = &fe->l_ea;
2375 l_ad = udf_rw32(fe->l_ad);
2376 } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
2377 efe = &dscr->efe;
2378 data = efe->data;
2379 l_eap = &efe->l_ea;
2380 l_ad = udf_rw32(efe->l_ad);
2381 } else {
2382 panic("Bad tag passed to udf_extattr_insert_internal");
2383 }
2384
2385 /* can't append already written to file descriptors yet */
2386 assert(l_ad == 0);
2387
2388 /* should have a header! */
2389 extattrhdr = (struct extattrhdr_desc *) data;
2390 l_ea = udf_rw32(*l_eap);
2391 if (l_ea == 0) {
2392 /* create empty extended attribute header */
2393 exthdr_len = sizeof(struct extattrhdr_desc);
2394
2395 udf_inittag(ump, &extattrhdr->tag, TAGID_EXTATTR_HDR,
2396 /* loc */ 0);
2397 extattrhdr->impl_attr_loc = udf_rw32(exthdr_len);
2398 extattrhdr->appl_attr_loc = udf_rw32(exthdr_len);
2399 extattrhdr->tag.desc_crc_len = udf_rw16(8);
2400
2401 /* record extended attribute header length */
2402 l_ea = exthdr_len;
2403 *l_eap = udf_rw32(l_ea);
2404 }
2405
2406 /* extract locations */
2407 impl_attr_loc = udf_rw32(extattrhdr->impl_attr_loc);
2408 appl_attr_loc = udf_rw32(extattrhdr->appl_attr_loc);
2409 if (impl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
2410 impl_attr_loc = l_ea;
2411 if (appl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
2412 appl_attr_loc = l_ea;
2413
2414 /* Ecma 167 EAs */
2415 if (udf_rw32(extattr->type) < 2048) {
2416 assert(impl_attr_loc == l_ea);
2417 assert(appl_attr_loc == l_ea);
2418 }
2419
2420 /* implementation use extended attributes */
2421 if (udf_rw32(extattr->type) == 2048) {
2422 assert(appl_attr_loc == l_ea);
2423
2424 /* calculate and write extended attribute header checksum */
2425 implext = (struct impl_extattr_entry *) extattr;
2426 assert(udf_rw32(implext->iu_l) == 4); /* [UDF 3.3.4.5] */
2427 spos = (uint16_t *) implext->data;
2428 *spos = udf_rw16(udf_ea_cksum((uint8_t *) implext));
2429 }
2430
2431 /* application use extended attributes */
2432 assert(udf_rw32(extattr->type) != 65536);
2433 assert(appl_attr_loc == l_ea);
2434
2435 /* append the attribute at the end of the current space */
2436 bpos = data + udf_rw32(*l_eap);
2437 a_l = udf_rw32(extattr->a_l);
2438
2439 /* update impl. attribute locations */
2440 if (udf_rw32(extattr->type) < 2048) {
2441 impl_attr_loc = l_ea + a_l;
2442 appl_attr_loc = l_ea + a_l;
2443 }
2444 if (udf_rw32(extattr->type) == 2048) {
2445 appl_attr_loc = l_ea + a_l;
2446 }
2447
2448 /* copy and advance */
2449 memcpy(bpos, extattr, a_l);
2450 l_ea += a_l;
2451 *l_eap = udf_rw32(l_ea);
2452
2453 /* do the `dance` again backwards */
2454 if (udf_rw16(ump->logical_vol->tag.descriptor_ver) != 2) {
2455 if (impl_attr_loc == l_ea)
2456 impl_attr_loc = UDF_IMPL_ATTR_LOC_NOT_PRESENT;
2457 if (appl_attr_loc == l_ea)
2458 appl_attr_loc = UDF_APPL_ATTR_LOC_NOT_PRESENT;
2459 }
2460
2461 /* store offsets */
2462 extattrhdr->impl_attr_loc = udf_rw32(impl_attr_loc);
2463 extattrhdr->appl_attr_loc = udf_rw32(appl_attr_loc);
2464 }
2465
2466
2467 /* --------------------------------------------------------------------- */
2468
2469 static int
2470 udf_update_lvid_from_vat_extattr(struct udf_node *vat_node)
2471 {
2472 struct udf_mount *ump;
2473 struct udf_logvol_info *lvinfo;
2474 struct impl_extattr_entry *implext;
2475 struct vatlvext_extattr_entry lvext;
2476 const char *extstr = "*UDF VAT LVExtension";
2477 uint64_t vat_uniqueid;
2478 uint32_t offset, a_l;
2479 uint8_t *ea_start, *lvextpos;
2480 int error;
2481
2482 /* get mountpoint and lvinfo */
2483 ump = vat_node->ump;
2484 lvinfo = ump->logvol_info;
2485
2486 /* get information from fe/efe */
2487 if (vat_node->fe) {
2488 vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
2489 ea_start = vat_node->fe->data;
2490 } else {
2491 vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
2492 ea_start = vat_node->efe->data;
2493 }
2494
2495 error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
2496 if (error)
2497 return error;
2498
2499 implext = (struct impl_extattr_entry *) (ea_start + offset);
2500 error = udf_impl_extattr_check(implext);
2501 if (error)
2502 return error;
2503
2504 /* paranoia */
2505 if (a_l != sizeof(*implext) -1 + udf_rw32(implext->iu_l) + sizeof(lvext)) {
2506 DPRINTF(VOLUMES, ("VAT LVExtension size doesn't compute\n"));
2507 return EINVAL;
2508 }
2509
2510 /*
2511 * we have found our "VAT LVExtension attribute. BUT due to a
2512 * bug in the specification it might not be word aligned so
2513 * copy first to avoid panics on some machines (!!)
2514 */
2515 DPRINTF(VOLUMES, ("Found VAT LVExtension attr\n"));
2516 lvextpos = implext->data + udf_rw32(implext->iu_l);
2517 memcpy(&lvext, lvextpos, sizeof(lvext));
2518
2519 /* check if it was updated the last time */
2520 if (udf_rw64(lvext.unique_id_chk) == vat_uniqueid) {
2521 lvinfo->num_files = lvext.num_files;
2522 lvinfo->num_directories = lvext.num_directories;
2523 udf_update_logvolname(ump, lvext.logvol_id);
2524 } else {
2525 DPRINTF(VOLUMES, ("VAT LVExtension out of date\n"));
2526 /* replace VAT LVExt by free space EA */
2527 memset(implext->imp_id.id, 0, UDF_REGID_ID_SIZE);
2528 strcpy(implext->imp_id.id, "*UDF FreeEASpace");
2529 udf_calc_impl_extattr_checksum(implext);
2530 }
2531
2532 return 0;
2533 }
2534
2535
2536 static int
2537 udf_update_vat_extattr_from_lvid(struct udf_node *vat_node)
2538 {
2539 struct udf_mount *ump;
2540 struct udf_logvol_info *lvinfo;
2541 struct impl_extattr_entry *implext;
2542 struct vatlvext_extattr_entry lvext;
2543 const char *extstr = "*UDF VAT LVExtension";
2544 uint64_t vat_uniqueid;
2545 uint32_t offset, a_l;
2546 uint8_t *ea_start, *lvextpos;
2547 int error;
2548
2549 /* get mountpoint and lvinfo */
2550 ump = vat_node->ump;
2551 lvinfo = ump->logvol_info;
2552
2553 /* get information from fe/efe */
2554 if (vat_node->fe) {
2555 vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
2556 ea_start = vat_node->fe->data;
2557 } else {
2558 vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
2559 ea_start = vat_node->efe->data;
2560 }
2561
2562 error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
2563 if (error)
2564 return error;
2565 /* found, it existed */
2566
2567 /* paranoia */
2568 implext = (struct impl_extattr_entry *) (ea_start + offset);
2569 error = udf_impl_extattr_check(implext);
2570 if (error) {
2571 DPRINTF(VOLUMES, ("VAT LVExtension bad on update\n"));
2572 return error;
2573 }
2574 /* it is correct */
2575
2576 /*
2577 * we have found our "VAT LVExtension attribute. BUT due to a
2578 * bug in the specification it might not be word aligned so
2579 * copy first to avoid panics on some machines (!!)
2580 */
2581 DPRINTF(VOLUMES, ("Updating VAT LVExtension attr\n"));
2582 lvextpos = implext->data + udf_rw32(implext->iu_l);
2583
2584 lvext.unique_id_chk = vat_uniqueid;
2585 lvext.num_files = lvinfo->num_files;
2586 lvext.num_directories = lvinfo->num_directories;
2587 memmove(lvext.logvol_id, ump->logical_vol->logvol_id, 128);
2588
2589 memcpy(lvextpos, &lvext, sizeof(lvext));
2590
2591 return 0;
2592 }
2593
2594 /* --------------------------------------------------------------------- */
2595
2596 int
2597 udf_vat_read(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset)
2598 {
2599 struct udf_mount *ump = vat_node->ump;
2600
2601 if (offset + size > ump->vat_offset + ump->vat_entries * 4)
2602 return EINVAL;
2603
2604 memcpy(blob, ump->vat_table + offset, size);
2605 return 0;
2606 }
2607
2608 int
2609 udf_vat_write(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset)
2610 {
2611 struct udf_mount *ump = vat_node->ump;
2612 uint32_t offset_high;
2613 uint8_t *new_vat_table;
2614
2615 /* extent VAT allocation if needed */
2616 offset_high = offset + size;
2617 if (offset_high >= ump->vat_table_alloc_len) {
2618 /* realloc */
2619 new_vat_table = realloc(ump->vat_table,
2620 ump->vat_table_alloc_len + UDF_VAT_CHUNKSIZE,
2621 M_UDFVOLD, M_WAITOK | M_CANFAIL);
2622 if (!new_vat_table) {
2623 printf("udf_vat_write: can't extent VAT, out of mem\n");
2624 return ENOMEM;
2625 }
2626 ump->vat_table = new_vat_table;
2627 ump->vat_table_alloc_len += UDF_VAT_CHUNKSIZE;
2628 }
2629 ump->vat_table_len = MAX(ump->vat_table_len, offset_high);
2630
2631 memcpy(ump->vat_table + offset, blob, size);
2632 return 0;
2633 }
2634
2635 /* --------------------------------------------------------------------- */
2636
2637 /* TODO support previous VAT location writeout */
2638 static int
2639 udf_update_vat_descriptor(struct udf_mount *ump)
2640 {
2641 struct udf_node *vat_node = ump->vat_node;
2642 struct udf_logvol_info *lvinfo = ump->logvol_info;
2643 struct icb_tag *icbtag;
2644 struct udf_oldvat_tail *oldvat_tl;
2645 struct udf_vat *vat;
2646 uint64_t unique_id;
2647 uint32_t lb_size;
2648 uint8_t *raw_vat;
2649 int filetype, error;
2650
2651 KASSERT(vat_node);
2652 KASSERT(lvinfo);
2653 lb_size = udf_rw32(ump->logical_vol->lb_size);
2654
2655 /* get our new unique_id */
2656 unique_id = udf_advance_uniqueid(ump);
2657
2658 /* get information from fe/efe */
2659 if (vat_node->fe) {
2660 icbtag = &vat_node->fe->icbtag;
2661 vat_node->fe->unique_id = udf_rw64(unique_id);
2662 } else {
2663 icbtag = &vat_node->efe->icbtag;
2664 vat_node->efe->unique_id = udf_rw64(unique_id);
2665 }
2666
2667 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
2668 filetype = icbtag->file_type;
2669 KASSERT((filetype == 0) || (filetype == UDF_ICB_FILETYPE_VAT));
2670
2671 /* allocate piece to process head or tail of VAT file */
2672 raw_vat = malloc(lb_size, M_TEMP, M_WAITOK);
2673
2674 if (filetype == 0) {
2675 /*
2676 * Update "*UDF VAT LVExtension" extended attribute from the
2677 * lvint if present.
2678 */
2679 udf_update_vat_extattr_from_lvid(vat_node);
2680
2681 /* setup identifying regid */
2682 oldvat_tl = (struct udf_oldvat_tail *) raw_vat;
2683 memset(oldvat_tl, 0, sizeof(struct udf_oldvat_tail));
2684
2685 udf_set_regid(&oldvat_tl->id, "*UDF Virtual Alloc Tbl");
2686 udf_add_udf_regid(ump, &oldvat_tl->id);
2687 oldvat_tl->prev_vat = udf_rw32(0xffffffff);
2688
2689 /* write out new tail of virtual allocation table file */
2690 error = udf_vat_write(vat_node, raw_vat,
2691 sizeof(struct udf_oldvat_tail), ump->vat_entries * 4);
2692 } else {
2693 /* compose the VAT2 header */
2694 vat = (struct udf_vat *) raw_vat;
2695 memset(vat, 0, sizeof(struct udf_vat));
2696
2697 vat->header_len = udf_rw16(152); /* as per spec */
2698 vat->impl_use_len = udf_rw16(0);
2699 memmove(vat->logvol_id, ump->logical_vol->logvol_id, 128);
2700 vat->prev_vat = udf_rw32(0xffffffff);
2701 vat->num_files = lvinfo->num_files;
2702 vat->num_directories = lvinfo->num_directories;
2703 vat->min_udf_readver = lvinfo->min_udf_readver;
2704 vat->min_udf_writever = lvinfo->min_udf_writever;
2705 vat->max_udf_writever = lvinfo->max_udf_writever;
2706
2707 error = udf_vat_write(vat_node, raw_vat,
2708 sizeof(struct udf_vat), 0);
2709 }
2710 free(raw_vat, M_TEMP);
2711
2712 return error; /* success! */
2713 }
2714
2715
2716 int
2717 udf_writeout_vat(struct udf_mount *ump)
2718 {
2719 struct udf_node *vat_node = ump->vat_node;
2720 uint32_t vat_length;
2721 int error;
2722
2723 KASSERT(vat_node);
2724
2725 DPRINTF(CALL, ("udf_writeout_vat\n"));
2726
2727 mutex_enter(&ump->allocate_mutex);
2728 udf_update_vat_descriptor(ump);
2729
2730 /* write out the VAT contents ; TODO intelligent writing */
2731 vat_length = ump->vat_table_len;
2732 error = vn_rdwr(UIO_WRITE, vat_node->vnode,
2733 ump->vat_table, ump->vat_table_len, 0,
2734 UIO_SYSSPACE, IO_NODELOCKED, FSCRED, NULL, NULL);
2735 if (error) {
2736 printf("udf_writeout_vat: failed to write out VAT contents\n");
2737 goto out;
2738 }
2739
2740 mutex_exit(&ump->allocate_mutex);
2741
2742 vflushbuf(ump->vat_node->vnode, 1 /* sync */);
2743 error = VOP_FSYNC(ump->vat_node->vnode,
2744 FSCRED, FSYNC_WAIT, 0, 0);
2745 if (error)
2746 printf("udf_writeout_vat: error writing VAT node!\n");
2747 out:
2748
2749 return error;
2750 }
2751
2752 /* --------------------------------------------------------------------- */
2753
2754 /*
2755 * Read in relevant pieces of VAT file and check if its indeed a VAT file
2756 * descriptor. If OK, read in complete VAT file.
2757 */
2758
2759 static int
2760 udf_check_for_vat(struct udf_node *vat_node)
2761 {
2762 struct udf_mount *ump;
2763 struct icb_tag *icbtag;
2764 struct timestamp *mtime;
2765 struct udf_vat *vat;
2766 struct udf_oldvat_tail *oldvat_tl;
2767 struct udf_logvol_info *lvinfo;
2768 uint64_t unique_id;
2769 uint32_t vat_length;
2770 uint32_t vat_offset, vat_entries, vat_table_alloc_len;
2771 uint32_t sector_size;
2772 uint32_t *raw_vat;
2773 uint8_t *vat_table;
2774 char *regid_name;
2775 int filetype;
2776 int error;
2777
2778 /* vat_length is really 64 bits though impossible */
2779
2780 DPRINTF(VOLUMES, ("Checking for VAT\n"));
2781 if (!vat_node)
2782 return ENOENT;
2783
2784 /* get mount info */
2785 ump = vat_node->ump;
2786 sector_size = udf_rw32(ump->logical_vol->lb_size);
2787
2788 /* check assertions */
2789 assert(vat_node->fe || vat_node->efe);
2790 assert(ump->logvol_integrity);
2791
2792 /* set vnode type to regular file or we can't read from it! */
2793 vat_node->vnode->v_type = VREG;
2794
2795 /* get information from fe/efe */
2796 if (vat_node->fe) {
2797 vat_length = udf_rw64(vat_node->fe->inf_len);
2798 icbtag = &vat_node->fe->icbtag;
2799 mtime = &vat_node->fe->mtime;
2800 unique_id = udf_rw64(vat_node->fe->unique_id);
2801 } else {
2802 vat_length = udf_rw64(vat_node->efe->inf_len);
2803 icbtag = &vat_node->efe->icbtag;
2804 mtime = &vat_node->efe->mtime;
2805 unique_id = udf_rw64(vat_node->efe->unique_id);
2806 }
2807
2808 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
2809 filetype = icbtag->file_type;
2810 if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT))
2811 return ENOENT;
2812
2813 DPRINTF(VOLUMES, ("\tPossible VAT length %d\n", vat_length));
2814
2815 vat_table_alloc_len =
2816 ((vat_length + UDF_VAT_CHUNKSIZE-1) / UDF_VAT_CHUNKSIZE)
2817 * UDF_VAT_CHUNKSIZE;
2818
2819 vat_table = malloc(vat_table_alloc_len, M_UDFVOLD,
2820 M_CANFAIL | M_WAITOK);
2821 if (vat_table == NULL) {
2822 printf("allocation of %d bytes failed for VAT\n",
2823 vat_table_alloc_len);
2824 return ENOMEM;
2825 }
2826
2827 /* allocate piece to read in head or tail of VAT file */
2828 raw_vat = malloc(sector_size, M_TEMP, M_WAITOK);
2829
2830 /*
2831 * check contents of the file if its the old 1.50 VAT table format.
2832 * Its notoriously broken and allthough some implementations support an
2833 * extention as defined in the UDF 1.50 errata document, its doubtfull
2834 * to be useable since a lot of implementations don't maintain it.
2835 */
2836 lvinfo = ump->logvol_info;
2837
2838 if (filetype == 0) {
2839 /* definition */
2840 vat_offset = 0;
2841 vat_entries = (vat_length-36)/4;
2842
2843 /* read in tail of virtual allocation table file */
2844 error = vn_rdwr(UIO_READ, vat_node->vnode,
2845 (uint8_t *) raw_vat,
2846 sizeof(struct udf_oldvat_tail),
2847 vat_entries * 4,
2848 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
2849 NULL, NULL);
2850 if (error)
2851 goto out;
2852
2853 /* check 1.50 VAT */
2854 oldvat_tl = (struct udf_oldvat_tail *) raw_vat;
2855 regid_name = (char *) oldvat_tl->id.id;
2856 error = strncmp(regid_name, "*UDF Virtual Alloc Tbl", 22);
2857 if (error) {
2858 DPRINTF(VOLUMES, ("VAT format 1.50 rejected\n"));
2859 error = ENOENT;
2860 goto out;
2861 }
2862
2863 /*
2864 * update LVID from "*UDF VAT LVExtension" extended attribute
2865 * if present.
2866 */
2867 udf_update_lvid_from_vat_extattr(vat_node);
2868 } else {
2869 /* read in head of virtual allocation table file */
2870 error = vn_rdwr(UIO_READ, vat_node->vnode,
2871 (uint8_t *) raw_vat,
2872 sizeof(struct udf_vat), 0,
2873 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
2874 NULL, NULL);
2875 if (error)
2876 goto out;
2877
2878 /* definition */
2879 vat = (struct udf_vat *) raw_vat;
2880 vat_offset = vat->header_len;
2881 vat_entries = (vat_length - vat_offset)/4;
2882
2883 assert(lvinfo);
2884 lvinfo->num_files = vat->num_files;
2885 lvinfo->num_directories = vat->num_directories;
2886 lvinfo->min_udf_readver = vat->min_udf_readver;
2887 lvinfo->min_udf_writever = vat->min_udf_writever;
2888 lvinfo->max_udf_writever = vat->max_udf_writever;
2889
2890 udf_update_logvolname(ump, vat->logvol_id);
2891 }
2892
2893 /* read in complete VAT file */
2894 error = vn_rdwr(UIO_READ, vat_node->vnode,
2895 vat_table,
2896 vat_length, 0,
2897 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
2898 NULL, NULL);
2899 if (error)
2900 printf("read in of complete VAT file failed (error %d)\n",
2901 error);
2902 if (error)
2903 goto out;
2904
2905 DPRINTF(VOLUMES, ("VAT format accepted, marking it closed\n"));
2906 ump->logvol_integrity->lvint_next_unique_id = unique_id;
2907 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
2908 ump->logvol_integrity->time = *mtime;
2909
2910 ump->vat_table_len = vat_length;
2911 ump->vat_table_alloc_len = vat_table_alloc_len;
2912 ump->vat_table = vat_table;
2913 ump->vat_offset = vat_offset;
2914 ump->vat_entries = vat_entries;
2915 ump->vat_last_free_lb = 0; /* start at beginning */
2916
2917 out:
2918 if (error) {
2919 if (vat_table)
2920 free(vat_table, M_UDFVOLD);
2921 }
2922 free(raw_vat, M_TEMP);
2923
2924 return error;
2925 }
2926
2927 /* --------------------------------------------------------------------- */
2928
2929 static int
2930 udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping)
2931 {
2932 struct udf_node *vat_node;
2933 struct long_ad icb_loc;
2934 uint32_t early_vat_loc, late_vat_loc, vat_loc;
2935 int error;
2936
2937 /* mapping info not needed */
2938 mapping = mapping;
2939
2940 vat_loc = ump->last_possible_vat_location;
2941 early_vat_loc = vat_loc - 256; /* 8 blocks of 32 sectors */
2942
2943 DPRINTF(VOLUMES, ("1) last possible %d, early_vat_loc %d \n",
2944 vat_loc, early_vat_loc));
2945 early_vat_loc = MAX(early_vat_loc, ump->first_possible_vat_location);
2946 late_vat_loc = vat_loc + 1024;
2947
2948 DPRINTF(VOLUMES, ("2) last possible %d, early_vat_loc %d \n",
2949 vat_loc, early_vat_loc));
2950
2951 /* start looking from the end of the range */
2952 do {
2953 DPRINTF(VOLUMES, ("Checking for VAT at sector %d\n", vat_loc));
2954 icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART);
2955 icb_loc.loc.lb_num = udf_rw32(vat_loc);
2956
2957 error = udf_get_node(ump, &icb_loc, &vat_node);
2958 if (!error) {
2959 error = udf_check_for_vat(vat_node);
2960 DPRINTFIF(VOLUMES, !error,
2961 ("VAT accepted at %d\n", vat_loc));
2962 if (!error)
2963 break;
2964 }
2965 if (vat_node) {
2966 vput(vat_node->vnode);
2967 vat_node = NULL;
2968 }
2969 vat_loc--; /* walk backwards */
2970 } while (vat_loc >= early_vat_loc);
2971
2972 /* keep our VAT node around */
2973 if (vat_node) {
2974 UDF_SET_SYSTEMFILE(vat_node->vnode);
2975 ump->vat_node = vat_node;
2976 }
2977
2978 return error;
2979 }
2980
2981 /* --------------------------------------------------------------------- */
2982
2983 static int
2984 udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
2985 {
2986 union dscrptr *dscr;
2987 struct part_map_spare *pms = &mapping->pms;
2988 uint32_t lb_num;
2989 int spar, error;
2990
2991 /*
2992 * The partition mapping passed on to us specifies the information we
2993 * need to locate and initialise the sparable partition mapping
2994 * information we need.
2995 */
2996
2997 DPRINTF(VOLUMES, ("Read sparable table\n"));
2998 ump->sparable_packet_size = udf_rw16(pms->packet_len);
2999 KASSERT(ump->sparable_packet_size >= ump->packet_size); /* XXX */
3000
3001 for (spar = 0; spar < pms->n_st; spar++) {
3002 lb_num = pms->st_loc[spar];
3003 DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num));
3004 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
3005 if (!error && dscr) {
3006 if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) {
3007 if (ump->sparing_table)
3008 free(ump->sparing_table, M_UDFVOLD);
3009 ump->sparing_table = &dscr->spt;
3010 dscr = NULL;
3011 DPRINTF(VOLUMES,
3012 ("Sparing table accepted (%d entries)\n",
3013 udf_rw16(ump->sparing_table->rt_l)));
3014 break; /* we're done */
3015 }
3016 }
3017 if (dscr)
3018 free(dscr, M_UDFVOLD);
3019 }
3020
3021 if (ump->sparing_table)
3022 return 0;
3023
3024 return ENOENT;
3025 }
3026
3027 /* --------------------------------------------------------------------- */
3028
3029 static int
3030 udf_read_metadata_nodes(struct udf_mount *ump, union udf_pmap *mapping)
3031 {
3032 struct part_map_meta *pmm = &mapping->pmm;
3033 struct long_ad icb_loc;
3034 struct vnode *vp;
3035 int error;
3036
3037 DPRINTF(VOLUMES, ("Reading in Metadata files\n"));
3038 icb_loc.loc.part_num = pmm->part_num;
3039 icb_loc.loc.lb_num = pmm->meta_file_lbn;
3040 DPRINTF(VOLUMES, ("Metadata file\n"));
3041 error = udf_get_node(ump, &icb_loc, &ump->metadata_node);
3042 if (ump->metadata_node) {
3043 vp = ump->metadata_node->vnode;
3044 UDF_SET_SYSTEMFILE(vp);
3045 }
3046
3047 icb_loc.loc.lb_num = pmm->meta_mirror_file_lbn;
3048 if (icb_loc.loc.lb_num != -1) {
3049 DPRINTF(VOLUMES, ("Metadata copy file\n"));
3050 error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_node);
3051 if (ump->metadatamirror_node) {
3052 vp = ump->metadatamirror_node->vnode;
3053 UDF_SET_SYSTEMFILE(vp);
3054 }
3055 }
3056
3057 icb_loc.loc.lb_num = pmm->meta_bitmap_file_lbn;
3058 if (icb_loc.loc.lb_num != -1) {
3059 DPRINTF(VOLUMES, ("Metadata bitmap file\n"));
3060 error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_node);
3061 if (ump->metadatabitmap_node) {
3062 vp = ump->metadatabitmap_node->vnode;
3063 UDF_SET_SYSTEMFILE(vp);
3064 }
3065 }
3066
3067 /* if we're mounting read-only we relax the requirements */
3068 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) {
3069 error = EFAULT;
3070 if (ump->metadata_node)
3071 error = 0;
3072 if ((ump->metadata_node == NULL) && (ump->metadatamirror_node)) {
3073 printf( "udf mount: Metadata file not readable, "
3074 "substituting Metadata copy file\n");
3075 ump->metadata_node = ump->metadatamirror_node;
3076 ump->metadatamirror_node = NULL;
3077 error = 0;
3078 }
3079 } else {
3080 /* mounting read/write */
3081 /* XXX DISABLED! metadata writing is not working yet XXX */
3082 if (error)
3083 error = EROFS;
3084 }
3085 DPRINTFIF(VOLUMES, error, ("udf mount: failed to read "
3086 "metadata files\n"));
3087 return error;
3088 }
3089
3090 /* --------------------------------------------------------------------- */
3091
3092 int
3093 udf_read_vds_tables(struct udf_mount *ump)
3094 {
3095 union udf_pmap *mapping;
3096 /* struct udf_args *args = &ump->mount_args; */
3097 uint32_t n_pm, mt_l;
3098 uint32_t log_part;
3099 uint8_t *pmap_pos;
3100 int pmap_size;
3101 int error;
3102
3103 /* Iterate (again) over the part mappings for locations */
3104 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
3105 mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */
3106 pmap_pos = ump->logical_vol->maps;
3107
3108 for (log_part = 0; log_part < n_pm; log_part++) {
3109 mapping = (union udf_pmap *) pmap_pos;
3110 switch (ump->vtop_tp[log_part]) {
3111 case UDF_VTOP_TYPE_PHYS :
3112 /* nothing */
3113 break;
3114 case UDF_VTOP_TYPE_VIRT :
3115 /* search and load VAT */
3116 error = udf_search_vat(ump, mapping);
3117 if (error)
3118 return ENOENT;
3119 break;
3120 case UDF_VTOP_TYPE_SPARABLE :
3121 /* load one of the sparable tables */
3122 error = udf_read_sparables(ump, mapping);
3123 if (error)
3124 return ENOENT;
3125 break;
3126 case UDF_VTOP_TYPE_META :
3127 /* load the associated file descriptors */
3128 error = udf_read_metadata_nodes(ump, mapping);
3129 if (error)
3130 return ENOENT;
3131 break;
3132 default:
3133 break;
3134 }
3135 pmap_size = pmap_pos[1];
3136 pmap_pos += pmap_size;
3137 }
3138
3139 /* read in and check unallocated and free space info if writing */
3140 if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY) == 0) {
3141 error = udf_read_physical_partition_spacetables(ump);
3142 if (error)
3143 return error;
3144
3145 /* also read in metadata partion spacebitmap if defined */
3146 error = udf_read_metadata_partition_spacetable(ump);
3147 return error;
3148 }
3149
3150 return 0;
3151 }
3152
3153 /* --------------------------------------------------------------------- */
3154
3155 int
3156 udf_read_rootdirs(struct udf_mount *ump)
3157 {
3158 union dscrptr *dscr;
3159 /* struct udf_args *args = &ump->mount_args; */
3160 struct udf_node *rootdir_node, *streamdir_node;
3161 struct long_ad fsd_loc, *dir_loc;
3162 uint32_t lb_num, dummy;
3163 uint32_t fsd_len;
3164 int dscr_type;
3165 int error;
3166
3167 /* TODO implement FSD reading in separate function like integrity? */
3168 /* get fileset descriptor sequence */
3169 fsd_loc = ump->logical_vol->lv_fsd_loc;
3170 fsd_len = udf_rw32(fsd_loc.len);
3171
3172 dscr = NULL;
3173 error = 0;
3174 while (fsd_len || error) {
3175 DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
3176 /* translate fsd_loc to lb_num */
3177 error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
3178 if (error)
3179 break;
3180 DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
3181 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
3182 /* end markers */
3183 if (error || (dscr == NULL))
3184 break;
3185
3186 /* analyse */
3187 dscr_type = udf_rw16(dscr->tag.id);
3188 if (dscr_type == TAGID_TERM)
3189 break;
3190 if (dscr_type != TAGID_FSD) {
3191 free(dscr, M_UDFVOLD);
3192 return ENOENT;
3193 }
3194
3195 /*
3196 * TODO check for multiple fileset descriptors; its only
3197 * picking the last now. Also check for FSD
3198 * correctness/interpretability
3199 */
3200
3201 /* update */
3202 if (ump->fileset_desc) {
3203 free(ump->fileset_desc, M_UDFVOLD);
3204 }
3205 ump->fileset_desc = &dscr->fsd;
3206 dscr = NULL;
3207
3208 /* continue to the next fsd */
3209 fsd_len -= ump->discinfo.sector_size;
3210 fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
3211
3212 /* follow up to fsd->next_ex (long_ad) if its not null */
3213 if (udf_rw32(ump->fileset_desc->next_ex.len)) {
3214 DPRINTF(VOLUMES, ("follow up FSD extent\n"));
3215 fsd_loc = ump->fileset_desc->next_ex;
3216 fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
3217 }
3218 }
3219 if (dscr)
3220 free(dscr, M_UDFVOLD);
3221
3222 /* there has to be one */
3223 if (ump->fileset_desc == NULL)
3224 return ENOENT;
3225
3226 DPRINTF(VOLUMES, ("FSD read in fine\n"));
3227 DPRINTF(VOLUMES, ("Updating fsd logical volume id\n"));
3228 udf_update_logvolname(ump, ump->logical_vol->logvol_id);
3229
3230 /*
3231 * Now the FSD is known, read in the rootdirectory and if one exists,
3232 * the system stream dir. Some files in the system streamdir are not
3233 * wanted in this implementation since they are not maintained. If
3234 * writing is enabled we'll delete these files if they exist.
3235 */
3236
3237 rootdir_node = streamdir_node = NULL;
3238 dir_loc = NULL;
3239
3240 /* try to read in the rootdir */
3241 dir_loc = &ump->fileset_desc->rootdir_icb;
3242 error = udf_get_node(ump, dir_loc, &rootdir_node);
3243 if (error)
3244 return ENOENT;
3245
3246 /* aparently it read in fine */
3247
3248 /*
3249 * Try the system stream directory; not very likely in the ones we
3250 * test, but for completeness.
3251 */
3252 dir_loc = &ump->fileset_desc->streamdir_icb;
3253 if (udf_rw32(dir_loc->len)) {
3254 printf("udf_read_rootdirs: streamdir defined ");
3255 error = udf_get_node(ump, dir_loc, &streamdir_node);
3256 if (error) {
3257 printf("but error in streamdir reading\n");
3258 } else {
3259 printf("but ignored\n");
3260 /*
3261 * TODO process streamdir `baddies' i.e. files we dont
3262 * want if R/W
3263 */
3264 }
3265 }
3266
3267 DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
3268
3269 /* release the vnodes again; they'll be auto-recycled later */
3270 if (streamdir_node) {
3271 vput(streamdir_node->vnode);
3272 }
3273 if (rootdir_node) {
3274 vput(rootdir_node->vnode);
3275 }
3276
3277 return 0;
3278 }
3279
3280 /* --------------------------------------------------------------------- */
3281
3282 /* To make absolutely sure we are NOT returning zero, add one :) */
3283
3284 long
3285 udf_calchash(struct long_ad *icbptr)
3286 {
3287 /* ought to be enough since each mountpoint has its own chain */
3288 return udf_rw32(icbptr->loc.lb_num) + 1;
3289 }
3290
3291
3292 static struct udf_node *
3293 udf_hash_lookup(struct udf_mount *ump, struct long_ad *icbptr)
3294 {
3295 struct udf_node *node;
3296 struct vnode *vp;
3297 uint32_t hashline;
3298
3299 loop:
3300 mutex_enter(&ump->ihash_lock);
3301
3302 hashline = udf_calchash(icbptr) & UDF_INODE_HASHMASK;
3303 LIST_FOREACH(node, &ump->udf_nodes[hashline], hashchain) {
3304 assert(node);
3305 if (node->loc.loc.lb_num == icbptr->loc.lb_num &&
3306 node->loc.loc.part_num == icbptr->loc.part_num) {
3307 vp = node->vnode;
3308 assert(vp);
3309 mutex_enter(&vp->v_interlock);
3310 mutex_exit(&ump->ihash_lock);
3311 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
3312 goto loop;
3313 return node;
3314 }
3315 }
3316 mutex_exit(&ump->ihash_lock);
3317
3318 return NULL;
3319 }
3320
3321
3322 static void
3323 udf_sorted_list_insert(struct udf_node *node)
3324 {
3325 struct udf_mount *ump;
3326 struct udf_node *s_node, *last_node;
3327 uint32_t loc, s_loc;
3328
3329 ump = node->ump;
3330 last_node = NULL; /* XXX gcc */
3331
3332 if (LIST_EMPTY(&ump->sorted_udf_nodes)) {
3333 LIST_INSERT_HEAD(&ump->sorted_udf_nodes, node, sortchain);
3334 return;
3335 }
3336
3337 /*
3338 * We sort on logical block number here and not on physical block
3339 * number here. Ideally we should go for the physical block nr to get
3340 * better sync performance though this sort will ensure that packets
3341 * won't get spit up unnessisarily.
3342 */
3343
3344 loc = udf_rw32(node->loc.loc.lb_num);
3345 LIST_FOREACH(s_node, &ump->sorted_udf_nodes, sortchain) {
3346 s_loc = udf_rw32(s_node->loc.loc.lb_num);
3347 if (s_loc > loc) {
3348 LIST_INSERT_BEFORE(s_node, node, sortchain);
3349 return;
3350 }
3351 last_node = s_node;
3352 }
3353 LIST_INSERT_AFTER(last_node, node, sortchain);
3354 }
3355
3356
3357 static void
3358 udf_register_node(struct udf_node *node)
3359 {
3360 struct udf_mount *ump;
3361 struct udf_node *chk;
3362 uint32_t hashline;
3363
3364 ump = node->ump;
3365 mutex_enter(&ump->ihash_lock);
3366
3367 /* add to our hash table */
3368 hashline = udf_calchash(&node->loc) & UDF_INODE_HASHMASK;
3369 #ifdef DEBUG
3370 LIST_FOREACH(chk, &ump->udf_nodes[hashline], hashchain) {
3371 assert(chk);
3372 if (chk->loc.loc.lb_num == node->loc.loc.lb_num &&
3373 chk->loc.loc.part_num == node->loc.loc.part_num)
3374 panic("Double node entered\n");
3375 }
3376 #else
3377 chk = NULL;
3378 #endif
3379 LIST_INSERT_HEAD(&ump->udf_nodes[hashline], node, hashchain);
3380
3381 /* add to our sorted list */
3382 udf_sorted_list_insert(node);
3383
3384 mutex_exit(&ump->ihash_lock);
3385 }
3386
3387
3388 static void
3389 udf_deregister_node(struct udf_node *node)
3390 {
3391 struct udf_mount *ump;
3392
3393 ump = node->ump;
3394 mutex_enter(&ump->ihash_lock);
3395
3396 /* from hash and sorted list */
3397 LIST_REMOVE(node, hashchain);
3398 LIST_REMOVE(node, sortchain);
3399
3400 mutex_exit(&ump->ihash_lock);
3401 }
3402
3403 /* --------------------------------------------------------------------- */
3404
3405 int
3406 udf_open_logvol(struct udf_mount *ump)
3407 {
3408 int logvol_integrity;
3409 int error;
3410
3411 /* already/still open? */
3412 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
3413 if (logvol_integrity == UDF_INTEGRITY_OPEN)
3414 return 0;
3415
3416 /* can we open it ? */
3417 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
3418 return EROFS;
3419
3420 /* setup write parameters */
3421 DPRINTF(VOLUMES, ("Setting up write parameters\n"));
3422 if ((error = udf_setup_writeparams(ump)) != 0)
3423 return error;
3424
3425 /* determine data and metadata tracks (most likely same) */
3426 error = udf_search_writing_tracks(ump);
3427 if (error) {
3428 /* most likely lack of space */
3429 printf("udf_open_logvol: error searching writing tracks\n");
3430 return EROFS;
3431 }
3432
3433 /* writeout/update lvint on disc or only in memory */
3434 DPRINTF(VOLUMES, ("Opening logical volume\n"));
3435 if (ump->lvopen & UDF_OPEN_SESSION) {
3436 /* TODO implement writeout of VRS + VDS */
3437 printf( "udf_open_logvol:Opening a closed session not yet "
3438 "implemented\n");
3439 return EROFS;
3440
3441 /* determine data and metadata tracks again */
3442 error = udf_search_writing_tracks(ump);
3443 }
3444
3445 /* mark it open */
3446 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_OPEN);
3447
3448 /* do we need to write it out? */
3449 if (ump->lvopen & UDF_WRITE_LVINT) {
3450 error = udf_writeout_lvint(ump, ump->lvopen);
3451 /* if we couldn't write it mark it closed again */
3452 if (error) {
3453 ump->logvol_integrity->integrity_type =
3454 udf_rw32(UDF_INTEGRITY_CLOSED);
3455 return error;
3456 }
3457 }
3458
3459 return 0;
3460 }
3461
3462
3463 int
3464 udf_close_logvol(struct udf_mount *ump, int mntflags)
3465 {
3466 int logvol_integrity;
3467 int error = 0, error1 = 0, error2 = 0;
3468 int n;
3469
3470 /* already/still closed? */
3471 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
3472 if (logvol_integrity == UDF_INTEGRITY_CLOSED)
3473 return 0;
3474
3475 /* writeout/update lvint or write out VAT */
3476 DPRINTF(VOLUMES, ("Closing logical volume\n"));
3477 if (ump->lvclose & UDF_WRITE_VAT) {
3478 DPRINTF(VOLUMES, ("lvclose & UDF_WRITE_VAT\n"));
3479
3480 /* write out the VAT node */
3481 DPRINTF(VOLUMES, ("writeout vat_node\n"));
3482 udf_writeout_vat(ump);
3483
3484 vflushbuf(ump->vat_node->vnode, 1 /* sync */);
3485 for (n = 0; n < 16; n++) {
3486 ump->vat_node->i_flags |= IN_MODIFIED;
3487 error = VOP_FSYNC(ump->vat_node->vnode,
3488 FSCRED, FSYNC_WAIT, 0, 0);
3489 }
3490 if (error) {
3491 printf("udf_close_logvol: writeout of VAT failed\n");
3492 return error;
3493 }
3494 }
3495
3496 if (ump->lvclose & UDF_WRITE_PART_BITMAPS) {
3497 /* sync writeout metadata spacetable if existing */
3498 error1 = udf_write_metadata_partition_spacetable(ump, true);
3499 if (error1)
3500 printf( "udf_close_logvol: writeout of metadata space "
3501 "bitmap failed\n");
3502
3503 /* sync writeout partition spacetables */
3504 error2 = udf_write_physical_partition_spacetables(ump, true);
3505 if (error2)
3506 printf( "udf_close_logvol: writeout of space tables "
3507 "failed\n");
3508
3509 if (error1 || error2)
3510 return (error1 | error2);
3511
3512 ump->lvclose &= ~UDF_WRITE_PART_BITMAPS;
3513 }
3514
3515 if (ump->lvclose & UDF_CLOSE_SESSION) {
3516 printf("TODO: Closing a session is not yet implemented\n");
3517 return EROFS;
3518 ump->lvopen |= UDF_OPEN_SESSION;
3519 }
3520
3521 /* mark it closed */
3522 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
3523
3524 /* do we need to write out the logical volume integrity */
3525 if (ump->lvclose & UDF_WRITE_LVINT)
3526 error = udf_writeout_lvint(ump, ump->lvopen);
3527 if (error) {
3528 /* HELP now what? mark it open again for now */
3529 ump->logvol_integrity->integrity_type =
3530 udf_rw32(UDF_INTEGRITY_OPEN);
3531 return error;
3532 }
3533
3534 (void) udf_synchronise_caches(ump);
3535
3536 return 0;
3537 }
3538
3539 /* --------------------------------------------------------------------- */
3540
3541 /*
3542 * Genfs interfacing
3543 *
3544 * static const struct genfs_ops udf_genfsops = {
3545 * .gop_size = genfs_size,
3546 * size of transfers
3547 * .gop_alloc = udf_gop_alloc,
3548 * allocate len bytes at offset
3549 * .gop_write = genfs_gop_write,
3550 * putpages interface code
3551 * .gop_markupdate = udf_gop_markupdate,
3552 * set update/modify flags etc.
3553 * }
3554 */
3555
3556 /*
3557 * Genfs interface. These four functions are the only ones defined though not
3558 * documented... great....
3559 */
3560
3561 /*
3562 * Callback from genfs to allocate len bytes at offset off; only called when
3563 * filling up gaps in the allocation.
3564 */
3565 /* XXX should we check if there is space enough in udf_gop_alloc? */
3566 static int
3567 udf_gop_alloc(struct vnode *vp, off_t off,
3568 off_t len, int flags, kauth_cred_t cred)
3569 {
3570 #if 0
3571 struct udf_node *udf_node = VTOI(vp);
3572 struct udf_mount *ump = udf_node->ump;
3573 uint32_t lb_size, num_lb;
3574 #endif
3575
3576 DPRINTF(NOTIMPL, ("udf_gop_alloc not implemented\n"));
3577 DPRINTF(ALLOC, ("udf_gop_alloc called for %"PRIu64" bytes\n", len));
3578
3579 return 0;
3580 }
3581
3582
3583 /*
3584 * callback from genfs to update our flags
3585 */
3586 static void
3587 udf_gop_markupdate(struct vnode *vp, int flags)
3588 {
3589 struct udf_node *udf_node = VTOI(vp);
3590 u_long mask = 0;
3591
3592 if ((flags & GOP_UPDATE_ACCESSED) != 0) {
3593 mask = IN_ACCESS;
3594 }
3595 if ((flags & GOP_UPDATE_MODIFIED) != 0) {
3596 if (vp->v_type == VREG) {
3597 mask |= IN_CHANGE | IN_UPDATE;
3598 } else {
3599 mask |= IN_MODIFY;
3600 }
3601 }
3602 if (mask) {
3603 udf_node->i_flags |= mask;
3604 }
3605 }
3606
3607
3608 static const struct genfs_ops udf_genfsops = {
3609 .gop_size = genfs_size,
3610 .gop_alloc = udf_gop_alloc,
3611 .gop_write = genfs_gop_write_rwmap,
3612 .gop_markupdate = udf_gop_markupdate,
3613 };
3614
3615
3616 /* --------------------------------------------------------------------- */
3617
3618 int
3619 udf_write_terminator(struct udf_mount *ump, uint32_t sector)
3620 {
3621 union dscrptr *dscr;
3622 int error;
3623
3624 dscr = malloc(ump->discinfo.sector_size, M_TEMP, M_WAITOK);
3625 bzero(dscr, ump->discinfo.sector_size);
3626 udf_inittag(ump, &dscr->tag, TAGID_TERM, sector);
3627
3628 /* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
3629 dscr->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
3630 (void) udf_validate_tag_and_crc_sums(dscr);
3631
3632 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
3633 dscr, sector, sector);
3634
3635 free(dscr, M_TEMP);
3636
3637 return error;
3638 }
3639
3640
3641 /* --------------------------------------------------------------------- */
3642
3643 /* UDF<->unix converters */
3644
3645 /* --------------------------------------------------------------------- */
3646
3647 static mode_t
3648 udf_perm_to_unix_mode(uint32_t perm)
3649 {
3650 mode_t mode;
3651
3652 mode = ((perm & UDF_FENTRY_PERM_USER_MASK) );
3653 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK ) >> 2);
3654 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
3655
3656 return mode;
3657 }
3658
3659 /* --------------------------------------------------------------------- */
3660
3661 static uint32_t
3662 unix_mode_to_udf_perm(mode_t mode)
3663 {
3664 uint32_t perm;
3665
3666 perm = ((mode & S_IRWXO) );
3667 perm |= ((mode & S_IRWXG) << 2);
3668 perm |= ((mode & S_IRWXU) << 4);
3669 perm |= ((mode & S_IWOTH) << 3);
3670 perm |= ((mode & S_IWGRP) << 5);
3671 perm |= ((mode & S_IWUSR) << 7);
3672
3673 return perm;
3674 }
3675
3676 /* --------------------------------------------------------------------- */
3677
3678 static uint32_t
3679 udf_icb_to_unix_filetype(uint32_t icbftype)
3680 {
3681 switch (icbftype) {
3682 case UDF_ICB_FILETYPE_DIRECTORY :
3683 case UDF_ICB_FILETYPE_STREAMDIR :
3684 return S_IFDIR;
3685 case UDF_ICB_FILETYPE_FIFO :
3686 return S_IFIFO;
3687 case UDF_ICB_FILETYPE_CHARDEVICE :
3688 return S_IFCHR;
3689 case UDF_ICB_FILETYPE_BLOCKDEVICE :
3690 return S_IFBLK;
3691 case UDF_ICB_FILETYPE_RANDOMACCESS :
3692 case UDF_ICB_FILETYPE_REALTIME :
3693 return S_IFREG;
3694 case UDF_ICB_FILETYPE_SYMLINK :
3695 return S_IFLNK;
3696 case UDF_ICB_FILETYPE_SOCKET :
3697 return S_IFSOCK;
3698 }
3699 /* no idea what this is */
3700 return 0;
3701 }
3702
3703 /* --------------------------------------------------------------------- */
3704
3705 void
3706 udf_to_unix_name(char *result, int result_len, char *id, int len,
3707 struct charspec *chsp)
3708 {
3709 uint16_t *raw_name, *unix_name;
3710 uint16_t *inchp, ch;
3711 uint8_t *outchp;
3712 const char *osta_id = "OSTA Compressed Unicode";
3713 int ucode_chars, nice_uchars, is_osta_typ0, nout;
3714
3715 raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK);
3716 unix_name = raw_name + 1024; /* split space in half */
3717 assert(sizeof(char) == sizeof(uint8_t));
3718 outchp = (uint8_t *) result;
3719
3720 is_osta_typ0 = (chsp->type == 0);
3721 is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
3722 if (is_osta_typ0) {
3723 /* TODO clean up */
3724 *raw_name = *unix_name = 0;
3725 ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
3726 ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
3727 nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
3728 /* output UTF8 */
3729 for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
3730 ch = *inchp;
3731 nout = wput_utf8(outchp, result_len, ch);
3732 outchp += nout; result_len -= nout;
3733 if (!ch) break;
3734 }
3735 *outchp++ = 0;
3736 } else {
3737 /* assume 8bit char length byte latin-1 */
3738 assert(*id == 8);
3739 assert(strlen((char *) (id+1)) <= MAXNAMLEN);
3740 strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
3741 }
3742 free(raw_name, M_UDFTEMP);
3743 }
3744
3745 /* --------------------------------------------------------------------- */
3746
3747 void
3748 unix_to_udf_name(char *result, uint8_t *result_len, char const *name, int name_len,
3749 struct charspec *chsp)
3750 {
3751 uint16_t *raw_name;
3752 uint16_t *outchp;
3753 const char *inchp;
3754 const char *osta_id = "OSTA Compressed Unicode";
3755 int udf_chars, is_osta_typ0, bits;
3756 size_t cnt;
3757
3758 /* allocate temporary unicode-16 buffer */
3759 raw_name = malloc(1024, M_UDFTEMP, M_WAITOK);
3760
3761 /* convert utf8 to unicode-16 */
3762 *raw_name = 0;
3763 inchp = name;
3764 outchp = raw_name;
3765 bits = 8;
3766 for (cnt = name_len, udf_chars = 0; cnt;) {
3767 /*###3490 [cc] warning: passing argument 2 of 'wget_utf8' from incompatible pointer type%%%*/
3768 *outchp = wget_utf8(&inchp, &cnt);
3769 if (*outchp > 0xff)
3770 bits=16;
3771 outchp++;
3772 udf_chars++;
3773 }
3774 /* null terminate just in case */
3775 *outchp++ = 0;
3776
3777 is_osta_typ0 = (chsp->type == 0);
3778 is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
3779 if (is_osta_typ0) {
3780 udf_chars = udf_CompressUnicode(udf_chars, bits,
3781 (unicode_t *) raw_name,
3782 (byte *) result);
3783 } else {
3784 printf("unix to udf name: no CHSP0 ?\n");
3785 /* XXX assume 8bit char length byte latin-1 */
3786 *result++ = 8; udf_chars = 1;
3787 strncpy(result, name + 1, name_len);
3788 udf_chars += name_len;
3789 }
3790 *result_len = udf_chars;
3791 free(raw_name, M_UDFTEMP);
3792 }
3793
3794 /* --------------------------------------------------------------------- */
3795
3796 void
3797 udf_timestamp_to_timespec(struct udf_mount *ump,
3798 struct timestamp *timestamp,
3799 struct timespec *timespec)
3800 {
3801 struct clock_ymdhms ymdhms;
3802 uint32_t usecs, secs, nsecs;
3803 uint16_t tz;
3804
3805 /* fill in ymdhms structure from timestamp */
3806 memset(&ymdhms, 0, sizeof(ymdhms));
3807 ymdhms.dt_year = udf_rw16(timestamp->year);
3808 ymdhms.dt_mon = timestamp->month;
3809 ymdhms.dt_day = timestamp->day;
3810 ymdhms.dt_wday = 0; /* ? */
3811 ymdhms.dt_hour = timestamp->hour;
3812 ymdhms.dt_min = timestamp->minute;
3813 ymdhms.dt_sec = timestamp->second;
3814
3815 secs = clock_ymdhms_to_secs(&ymdhms);
3816 usecs = timestamp->usec +
3817 100*timestamp->hund_usec + 10000*timestamp->centisec;
3818 nsecs = usecs * 1000;
3819
3820 /*
3821 * Calculate the time zone. The timezone is 12 bit signed 2's
3822 * compliment, so we gotta do some extra magic to handle it right.
3823 */
3824 tz = udf_rw16(timestamp->type_tz);
3825 tz &= 0x0fff; /* only lower 12 bits are significant */
3826 if (tz & 0x0800) /* sign extention */
3827 tz |= 0xf000;
3828
3829 /* TODO check timezone conversion */
3830 /* check if we are specified a timezone to convert */
3831 if (udf_rw16(timestamp->type_tz) & 0x1000) {
3832 if ((int16_t) tz != -2047)
3833 secs -= (int16_t) tz * 60;
3834 } else {
3835 secs -= ump->mount_args.gmtoff;
3836 }
3837
3838 timespec->tv_sec = secs;
3839 timespec->tv_nsec = nsecs;
3840 }
3841
3842
3843 void
3844 udf_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp)
3845 {
3846 struct clock_ymdhms ymdhms;
3847 uint32_t husec, usec, csec;
3848
3849 (void) clock_secs_to_ymdhms(timespec->tv_sec, &ymdhms);
3850
3851 usec = timespec->tv_nsec / 1000;
3852 husec = usec / 100;
3853 usec -= husec * 100; /* only 0-99 in usec */
3854 csec = husec / 100; /* only 0-99 in csec */
3855 husec -= csec * 100; /* only 0-99 in husec */
3856
3857 /* set method 1 for CUT/GMT */
3858 timestamp->type_tz = udf_rw16((1<<12) + 0);
3859 timestamp->year = udf_rw16(ymdhms.dt_year);
3860 timestamp->month = ymdhms.dt_mon;
3861 timestamp->day = ymdhms.dt_day;
3862 timestamp->hour = ymdhms.dt_hour;
3863 timestamp->minute = ymdhms.dt_min;
3864 timestamp->second = ymdhms.dt_sec;
3865 timestamp->centisec = csec;
3866 timestamp->hund_usec = husec;
3867 timestamp->usec = usec;
3868 }
3869
3870 /* --------------------------------------------------------------------- */
3871
3872 /*
3873 * Attribute and filetypes converters with get/set pairs
3874 */
3875
3876 uint32_t
3877 udf_getaccessmode(struct udf_node *udf_node)
3878 {
3879 struct file_entry *fe = udf_node->fe;;
3880 struct extfile_entry *efe = udf_node->efe;
3881 uint32_t udf_perm, icbftype;
3882 uint32_t mode, ftype;
3883 uint16_t icbflags;
3884
3885 UDF_LOCK_NODE(udf_node, 0);
3886 if (fe) {
3887 udf_perm = udf_rw32(fe->perm);
3888 icbftype = fe->icbtag.file_type;
3889 icbflags = udf_rw16(fe->icbtag.flags);
3890 } else {
3891 assert(udf_node->efe);
3892 udf_perm = udf_rw32(efe->perm);
3893 icbftype = efe->icbtag.file_type;
3894 icbflags = udf_rw16(efe->icbtag.flags);
3895 }
3896
3897 mode = udf_perm_to_unix_mode(udf_perm);
3898 ftype = udf_icb_to_unix_filetype(icbftype);
3899
3900 /* set suid, sgid, sticky from flags in fe/efe */
3901 if (icbflags & UDF_ICB_TAG_FLAGS_SETUID)
3902 mode |= S_ISUID;
3903 if (icbflags & UDF_ICB_TAG_FLAGS_SETGID)
3904 mode |= S_ISGID;
3905 if (icbflags & UDF_ICB_TAG_FLAGS_STICKY)
3906 mode |= S_ISVTX;
3907
3908 UDF_UNLOCK_NODE(udf_node, 0);
3909
3910 return mode | ftype;
3911 }
3912
3913
3914 void
3915 udf_setaccessmode(struct udf_node *udf_node, mode_t mode)
3916 {
3917 struct file_entry *fe = udf_node->fe;
3918 struct extfile_entry *efe = udf_node->efe;
3919 uint32_t udf_perm;
3920 uint16_t icbflags;
3921
3922 UDF_LOCK_NODE(udf_node, 0);
3923 udf_perm = unix_mode_to_udf_perm(mode & ALLPERMS);
3924 if (fe) {
3925 icbflags = udf_rw16(fe->icbtag.flags);
3926 } else {
3927 icbflags = udf_rw16(efe->icbtag.flags);
3928 }
3929
3930 icbflags &= ~UDF_ICB_TAG_FLAGS_SETUID;
3931 icbflags &= ~UDF_ICB_TAG_FLAGS_SETGID;
3932 icbflags &= ~UDF_ICB_TAG_FLAGS_STICKY;
3933 if (mode & S_ISUID)
3934 icbflags |= UDF_ICB_TAG_FLAGS_SETUID;
3935 if (mode & S_ISGID)
3936 icbflags |= UDF_ICB_TAG_FLAGS_SETGID;
3937 if (mode & S_ISVTX)
3938 icbflags |= UDF_ICB_TAG_FLAGS_STICKY;
3939
3940 if (fe) {
3941 fe->perm = udf_rw32(udf_perm);
3942 fe->icbtag.flags = udf_rw16(icbflags);
3943 } else {
3944 efe->perm = udf_rw32(udf_perm);
3945 efe->icbtag.flags = udf_rw16(icbflags);
3946 }
3947
3948 UDF_UNLOCK_NODE(udf_node, 0);
3949 }
3950
3951
3952 void
3953 udf_getownership(struct udf_node *udf_node, uid_t *uidp, gid_t *gidp)
3954 {
3955 struct udf_mount *ump = udf_node->ump;
3956 struct file_entry *fe = udf_node->fe;
3957 struct extfile_entry *efe = udf_node->efe;
3958 uid_t uid;
3959 gid_t gid;
3960
3961 UDF_LOCK_NODE(udf_node, 0);
3962 if (fe) {
3963 uid = (uid_t)udf_rw32(fe->uid);
3964 gid = (gid_t)udf_rw32(fe->gid);
3965 } else {
3966 assert(udf_node->efe);
3967 uid = (uid_t)udf_rw32(efe->uid);
3968 gid = (gid_t)udf_rw32(efe->gid);
3969 }
3970
3971 /* do the uid/gid translation game */
3972 if (uid == (uid_t) -1)
3973 uid = ump->mount_args.anon_uid;
3974 if (gid == (gid_t) -1)
3975 gid = ump->mount_args.anon_gid;
3976
3977 *uidp = uid;
3978 *gidp = gid;
3979
3980 UDF_UNLOCK_NODE(udf_node, 0);
3981 }
3982
3983
3984 void
3985 udf_setownership(struct udf_node *udf_node, uid_t uid, gid_t gid)
3986 {
3987 struct udf_mount *ump = udf_node->ump;
3988 struct file_entry *fe = udf_node->fe;
3989 struct extfile_entry *efe = udf_node->efe;
3990 uid_t nobody_uid;
3991 gid_t nobody_gid;
3992
3993 UDF_LOCK_NODE(udf_node, 0);
3994
3995 /* do the uid/gid translation game */
3996 nobody_uid = ump->mount_args.nobody_uid;
3997 nobody_gid = ump->mount_args.nobody_gid;
3998 if (uid == nobody_uid)
3999 uid = (uid_t) -1;
4000 if (gid == nobody_gid)
4001 gid = (gid_t) -1;
4002
4003 if (fe) {
4004 fe->uid = udf_rw32((uint32_t) uid);
4005 fe->gid = udf_rw32((uint32_t) gid);
4006 } else {
4007 efe->uid = udf_rw32((uint32_t) uid);
4008 efe->gid = udf_rw32((uint32_t) gid);
4009 }
4010
4011 UDF_UNLOCK_NODE(udf_node, 0);
4012 }
4013
4014
4015 /* --------------------------------------------------------------------- */
4016
4017
4018 static int
4019 dirhash_fill(struct udf_node *dir_node)
4020 {
4021 struct vnode *dvp = dir_node->vnode;
4022 struct dirhash *dirh;
4023 struct file_entry *fe = dir_node->fe;
4024 struct extfile_entry *efe = dir_node->efe;
4025 struct fileid_desc *fid;
4026 struct dirent *dirent;
4027 uint64_t file_size, pre_diroffset, diroffset;
4028 uint32_t lb_size;
4029 int error;
4030
4031 /* make sure we have a dirhash to work on */
4032 dirh = dir_node->dir_hash;
4033 KASSERT(dirh);
4034 KASSERT(dirh->refcnt > 0);
4035
4036 if (dirh->flags & DIRH_BROKEN)
4037 return EIO;
4038 if (dirh->flags & DIRH_COMPLETE)
4039 return 0;
4040
4041 /* make sure we have a clean dirhash to add to */
4042 dirhash_purge_entries(dirh);
4043
4044 /* get directory filesize */
4045 if (fe) {
4046 file_size = udf_rw64(fe->inf_len);
4047 } else {
4048 assert(efe);
4049 file_size = udf_rw64(efe->inf_len);
4050 }
4051
4052 /* allocate temporary space for fid */
4053 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4054 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4055
4056 /* allocate temporary space for dirent */
4057 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4058
4059 error = 0;
4060 diroffset = 0;
4061 while (diroffset < file_size) {
4062 /* transfer a new fid/dirent */
4063 pre_diroffset = diroffset;
4064 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
4065 if (error) {
4066 /* TODO what to do? continue but not add? */
4067 dirh->flags |= DIRH_BROKEN;
4068 dirhash_purge_entries(dirh);
4069 break;
4070 }
4071
4072 if ((fid->file_char & UDF_FILE_CHAR_DEL)) {
4073 /* register deleted extent for reuse */
4074 dirhash_enter_freed(dirh, pre_diroffset,
4075 udf_fidsize(fid));
4076 } else {
4077 /* append to the dirhash */
4078 dirhash_enter(dirh, dirent, pre_diroffset,
4079 udf_fidsize(fid), 0);
4080 }
4081 }
4082 dirh->flags |= DIRH_COMPLETE;
4083
4084 free(fid, M_UDFTEMP);
4085 free(dirent, M_UDFTEMP);
4086
4087 return error;
4088 }
4089
4090
4091 /* --------------------------------------------------------------------- */
4092
4093 /*
4094 * Directory read and manipulation functions.
4095 *
4096 */
4097
4098 int
4099 udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
4100 struct long_ad *icb_loc, int *found)
4101 {
4102 struct udf_node *dir_node = VTOI(vp);
4103 struct dirhash *dirh;
4104 struct dirhash_entry *dirh_ep;
4105 struct fileid_desc *fid;
4106 struct dirent *dirent;
4107 uint64_t diroffset;
4108 uint32_t lb_size;
4109 int hit, error;
4110
4111 /* set default return */
4112 *found = 0;
4113
4114 /* get our dirhash and make sure its read in */
4115 dirhash_get(&dir_node->dir_hash);
4116 error = dirhash_fill(dir_node);
4117 if (error) {
4118 dirhash_put(dir_node->dir_hash);
4119 return error;
4120 }
4121 dirh = dir_node->dir_hash;
4122
4123 /* allocate temporary space for fid */
4124 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4125 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4126 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4127
4128 DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n",
4129 namelen, namelen, name));
4130
4131 /* search our dirhash hits */
4132 memset(icb_loc, 0, sizeof(*icb_loc));
4133 dirh_ep = NULL;
4134 for (;;) {
4135 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
4136 /* if no hit, abort the search */
4137 if (!hit)
4138 break;
4139
4140 /* check this hit */
4141 diroffset = dirh_ep->offset;
4142
4143 /* transfer a new fid/dirent */
4144 error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
4145 if (error)
4146 break;
4147
4148 DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n",
4149 dirent->d_namlen, dirent->d_namlen, dirent->d_name));
4150
4151 /* see if its our entry */
4152 KASSERT(dirent->d_namlen == namelen);
4153 if (strncmp(dirent->d_name, name, namelen) == 0) {
4154 *found = 1;
4155 *icb_loc = fid->icb;
4156 break;
4157 }
4158 }
4159 free(fid, M_UDFTEMP);
4160 free(dirent, M_UDFTEMP);
4161
4162 dirhash_put(dir_node->dir_hash);
4163
4164 return error;
4165 }
4166
4167 /* --------------------------------------------------------------------- */
4168
4169 static int
4170 udf_create_new_fe(struct udf_mount *ump, struct file_entry *fe, int file_type,
4171 struct long_ad *node_icb, struct long_ad *parent_icb,
4172 uint64_t parent_unique_id)
4173 {
4174 struct timespec now;
4175 struct icb_tag *icb;
4176 struct filetimes_extattr_entry *ft_extattr;
4177 uint64_t unique_id;
4178 uint32_t fidsize, lb_num;
4179 uint8_t *bpos;
4180 int crclen, attrlen;
4181
4182 lb_num = udf_rw32(node_icb->loc.lb_num);
4183 udf_inittag(ump, &fe->tag, TAGID_FENTRY, lb_num);
4184 icb = &fe->icbtag;
4185
4186 /*
4187 * Always use strategy type 4 unless on WORM wich we don't support
4188 * (yet). Fill in defaults and set for internal allocation of data.
4189 */
4190 icb->strat_type = udf_rw16(4);
4191 icb->max_num_entries = udf_rw16(1);
4192 icb->file_type = file_type; /* 8 bit */
4193 icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC);
4194
4195 fe->perm = udf_rw32(0x7fff); /* all is allowed */
4196 fe->link_cnt = udf_rw16(0); /* explicit setting */
4197
4198 fe->ckpoint = udf_rw32(1); /* user supplied file version */
4199
4200 vfs_timestamp(&now);
4201 udf_timespec_to_timestamp(&now, &fe->atime);
4202 udf_timespec_to_timestamp(&now, &fe->attrtime);
4203 udf_timespec_to_timestamp(&now, &fe->mtime);
4204
4205 udf_set_regid(&fe->imp_id, IMPL_NAME);
4206 udf_add_impl_regid(ump, &fe->imp_id);
4207
4208 unique_id = udf_advance_uniqueid(ump);
4209 fe->unique_id = udf_rw64(unique_id);
4210 fe->l_ea = udf_rw32(0);
4211
4212 /* create extended attribute to record our creation time */
4213 attrlen = UDF_FILETIMES_ATTR_SIZE(1);
4214 ft_extattr = malloc(attrlen, M_UDFTEMP, M_WAITOK);
4215 memset(ft_extattr, 0, attrlen);
4216 ft_extattr->hdr.type = udf_rw32(UDF_FILETIMES_ATTR_NO);
4217 ft_extattr->hdr.subtype = 1; /* [4/48.10.5] */
4218 ft_extattr->hdr.a_l = udf_rw32(UDF_FILETIMES_ATTR_SIZE(1));
4219 ft_extattr->d_l = udf_rw32(UDF_TIMESTAMP_SIZE); /* one item */
4220 ft_extattr->existence = UDF_FILETIMES_FILE_CREATION;
4221 udf_timespec_to_timestamp(&now, &ft_extattr->times[0]);
4222
4223 udf_extattr_insert_internal(ump, (union dscrptr *) fe,
4224 (struct extattr_entry *) ft_extattr);
4225 free(ft_extattr, M_UDFTEMP);
4226
4227 /* if its a directory, create '..' */
4228 bpos = (uint8_t *) fe->data + udf_rw32(fe->l_ea);
4229 fidsize = 0;
4230 if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
4231 fidsize = udf_create_parentfid(ump,
4232 (struct fileid_desc *) bpos, parent_icb,
4233 parent_unique_id);
4234 }
4235
4236 /* record fidlength information */
4237 fe->inf_len = udf_rw64(fidsize);
4238 fe->l_ad = udf_rw32(fidsize);
4239 fe->logblks_rec = udf_rw64(0); /* intern */
4240
4241 crclen = sizeof(struct file_entry) - 1 - UDF_DESC_TAG_LENGTH;
4242 crclen += udf_rw32(fe->l_ea) + fidsize;
4243 fe->tag.desc_crc_len = udf_rw16(crclen);
4244
4245 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fe);
4246
4247 return fidsize;
4248 }
4249
4250 /* --------------------------------------------------------------------- */
4251
4252 static int
4253 udf_create_new_efe(struct udf_mount *ump, struct extfile_entry *efe,
4254 int file_type, struct long_ad *node_icb, struct long_ad *parent_icb,
4255 uint64_t parent_unique_id)
4256 {
4257 struct timespec now;
4258 struct icb_tag *icb;
4259 uint64_t unique_id;
4260 uint32_t fidsize, lb_num;
4261 uint8_t *bpos;
4262 int crclen;
4263
4264 lb_num = udf_rw32(node_icb->loc.lb_num);
4265 udf_inittag(ump, &efe->tag, TAGID_EXTFENTRY, lb_num);
4266 icb = &efe->icbtag;
4267
4268 /*
4269 * Always use strategy type 4 unless on WORM wich we don't support
4270 * (yet). Fill in defaults and set for internal allocation of data.
4271 */
4272 icb->strat_type = udf_rw16(4);
4273 icb->max_num_entries = udf_rw16(1);
4274 icb->file_type = file_type; /* 8 bit */
4275 icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC);
4276
4277 efe->perm = udf_rw32(0x7fff); /* all is allowed */
4278 efe->link_cnt = udf_rw16(0); /* explicit setting */
4279
4280 efe->ckpoint = udf_rw32(1); /* user supplied file version */
4281
4282 vfs_timestamp(&now);
4283 udf_timespec_to_timestamp(&now, &efe->ctime);
4284 udf_timespec_to_timestamp(&now, &efe->atime);
4285 udf_timespec_to_timestamp(&now, &efe->attrtime);
4286 udf_timespec_to_timestamp(&now, &efe->mtime);
4287
4288 udf_set_regid(&efe->imp_id, IMPL_NAME);
4289 udf_add_impl_regid(ump, &efe->imp_id);
4290
4291 unique_id = udf_advance_uniqueid(ump);
4292 efe->unique_id = udf_rw64(unique_id);
4293 efe->l_ea = udf_rw32(0);
4294
4295 /* if its a directory, create '..' */
4296 bpos = (uint8_t *) efe->data + udf_rw32(efe->l_ea);
4297 fidsize = 0;
4298 if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
4299 fidsize = udf_create_parentfid(ump,
4300 (struct fileid_desc *) bpos, parent_icb,
4301 parent_unique_id);
4302 }
4303
4304 /* record fidlength information */
4305 efe->obj_size = udf_rw64(fidsize);
4306 efe->inf_len = udf_rw64(fidsize);
4307 efe->l_ad = udf_rw32(fidsize);
4308 efe->logblks_rec = udf_rw64(0); /* intern */
4309
4310 crclen = sizeof(struct extfile_entry) - 1 - UDF_DESC_TAG_LENGTH;
4311 crclen += udf_rw32(efe->l_ea) + fidsize;
4312 efe->tag.desc_crc_len = udf_rw16(crclen);
4313
4314 (void) udf_validate_tag_and_crc_sums((union dscrptr *) efe);
4315
4316 return fidsize;
4317 }
4318
4319 /* --------------------------------------------------------------------- */
4320
4321 int
4322 udf_dir_detach(struct udf_mount *ump, struct udf_node *dir_node,
4323 struct udf_node *udf_node, struct componentname *cnp)
4324 {
4325 struct vnode *dvp = dir_node->vnode;
4326 struct dirhash *dirh;
4327 struct dirhash_entry *dirh_ep;
4328 struct file_entry *fe = dir_node->fe;
4329 struct extfile_entry *efe = dir_node->efe;
4330 struct fileid_desc *fid;
4331 struct dirent *dirent;
4332 uint64_t file_size, diroffset;
4333 uint32_t lb_size, fidsize;
4334 int found, error;
4335 char const *name = cnp->cn_nameptr;
4336 int namelen = cnp->cn_namelen;
4337 int hit, refcnt;
4338
4339 /* get our dirhash and make sure its read in */
4340 dirhash_get(&dir_node->dir_hash);
4341 error = dirhash_fill(dir_node);
4342 if (error) {
4343 dirhash_put(dir_node->dir_hash);
4344 return error;
4345 }
4346 dirh = dir_node->dir_hash;
4347
4348 /* get directory filesize */
4349 if (fe) {
4350 file_size = udf_rw64(fe->inf_len);
4351 } else {
4352 assert(efe);
4353 file_size = udf_rw64(efe->inf_len);
4354 }
4355
4356 /* allocate temporary space for fid */
4357 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4358 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4359 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4360
4361 /* search our dirhash hits */
4362 found = 0;
4363 dirh_ep = NULL;
4364 for (;;) {
4365 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
4366 /* if no hit, abort the search */
4367 if (!hit)
4368 break;
4369
4370 /* check this hit */
4371 diroffset = dirh_ep->offset;
4372
4373 /* transfer a new fid/dirent */
4374 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
4375 if (error)
4376 break;
4377
4378 /* see if its our entry */
4379 KASSERT(dirent->d_namlen == namelen);
4380 if (strncmp(dirent->d_name, name, namelen) == 0) {
4381 found = 1;
4382 break;
4383 }
4384 }
4385
4386 if (!found)
4387 error = ENOENT;
4388 if (error)
4389 goto error_out;
4390
4391 /* mark deleted */
4392 fid->file_char |= UDF_FILE_CHAR_DEL;
4393 #ifdef UDF_COMPLETE_DELETE
4394 memset(&fid->icb, 0, sizeof(fid->icb));
4395 #endif
4396 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
4397
4398 /* get size of fid and compensate for the read_fid_stream advance */
4399 fidsize = udf_fidsize(fid);
4400 diroffset -= fidsize;
4401
4402 /* write out */
4403 error = vn_rdwr(UIO_WRITE, dir_node->vnode,
4404 fid, fidsize, diroffset,
4405 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
4406 FSCRED, NULL, NULL);
4407 if (error)
4408 goto error_out;
4409
4410 /* get reference count of attached node */
4411 if (udf_node->fe) {
4412 refcnt = udf_rw16(udf_node->fe->link_cnt);
4413 } else {
4414 KASSERT(udf_node->efe);
4415 refcnt = udf_rw16(udf_node->efe->link_cnt);
4416 }
4417 #ifdef UDF_COMPLETE_DELETE
4418 /* substract reference counter in attached node */
4419 refcnt -= 1;
4420 if (udf_node->fe) {
4421 udf_node->fe->link_cnt = udf_rw16(refcnt);
4422 } else {
4423 udf_node->efe->link_cnt = udf_rw16(refcnt);
4424 }
4425
4426 /* prevent writeout when refcnt == 0 */
4427 if (refcnt == 0)
4428 udf_node->i_flags |= IN_DELETED;
4429
4430 if (fid->file_char & UDF_FILE_CHAR_DIR) {
4431 int drefcnt;
4432
4433 /* substract reference counter in directory node */
4434 /* note subtract 2 (?) for its was also backreferenced */
4435 if (dir_node->fe) {
4436 drefcnt = udf_rw16(dir_node->fe->link_cnt);
4437 drefcnt -= 1;
4438 dir_node->fe->link_cnt = udf_rw16(drefcnt);
4439 } else {
4440 KASSERT(dir_node->efe);
4441 drefcnt = udf_rw16(dir_node->efe->link_cnt);
4442 drefcnt -= 1;
4443 dir_node->efe->link_cnt = udf_rw16(drefcnt);
4444 }
4445 }
4446
4447 udf_node->i_flags |= IN_MODIFIED;
4448 dir_node->i_flags |= IN_MODIFIED;
4449 #endif
4450 /* if it is/was a hardlink adjust the file count */
4451 if (refcnt > 0)
4452 udf_adjust_filecount(udf_node, -1);
4453
4454 /* remove from the dirhash */
4455 dirhash_remove(dirh, dirent, diroffset,
4456 udf_fidsize(fid));
4457
4458 error_out:
4459 free(fid, M_UDFTEMP);
4460 free(dirent, M_UDFTEMP);
4461
4462 dirhash_put(dir_node->dir_hash);
4463
4464 return error;
4465 }
4466
4467 /* --------------------------------------------------------------------- */
4468
4469 /*
4470 * We are not allowed to split the fid tag itself over an logical block so
4471 * check the space remaining in the logical block.
4472 *
4473 * We try to select the smallest candidate for recycling or when none is
4474 * found, append a new one at the end of the directory.
4475 */
4476
4477 int
4478 udf_dir_attach(struct udf_mount *ump, struct udf_node *dir_node,
4479 struct udf_node *udf_node, struct vattr *vap, struct componentname *cnp)
4480 {
4481 struct vnode *dvp = dir_node->vnode;
4482 struct dirhash *dirh;
4483 struct dirhash_entry *dirh_ep;
4484 struct fileid_desc *fid;
4485 struct icb_tag *icbtag;
4486 struct charspec osta_charspec;
4487 struct dirent dirent;
4488 uint64_t unique_id, dir_size;
4489 uint64_t fid_pos, end_fid_pos, chosen_fid_pos;
4490 uint32_t chosen_size, chosen_size_diff;
4491 int lb_size, lb_rest, fidsize, this_fidsize, size_diff;
4492 int file_char, refcnt, icbflags, addr_type, hit, error;
4493
4494 /* get our dirhash and make sure its read in */
4495 dirhash_get(&dir_node->dir_hash);
4496 error = dirhash_fill(dir_node);
4497 if (error) {
4498 dirhash_put(dir_node->dir_hash);
4499 return error;
4500 }
4501 dirh = dir_node->dir_hash;
4502
4503 /* get info */
4504 lb_size = udf_rw32(ump->logical_vol->lb_size);
4505 udf_osta_charset(&osta_charspec);
4506
4507 if (dir_node->fe) {
4508 dir_size = udf_rw64(dir_node->fe->inf_len);
4509 icbtag = &dir_node->fe->icbtag;
4510 } else {
4511 dir_size = udf_rw64(dir_node->efe->inf_len);
4512 icbtag = &dir_node->efe->icbtag;
4513 }
4514
4515 icbflags = udf_rw16(icbtag->flags);
4516 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
4517
4518 if (udf_node->fe) {
4519 unique_id = udf_rw64(udf_node->fe->unique_id);
4520 refcnt = udf_rw16(udf_node->fe->link_cnt);
4521 } else {
4522 unique_id = udf_rw64(udf_node->efe->unique_id);
4523 refcnt = udf_rw16(udf_node->efe->link_cnt);
4524 }
4525
4526 if (refcnt > 0) {
4527 unique_id = udf_advance_uniqueid(ump);
4528 udf_adjust_filecount(udf_node, 1);
4529 }
4530
4531 /* determine file characteristics */
4532 file_char = 0; /* visible non deleted file and not stream metadata */
4533 if (vap->va_type == VDIR)
4534 file_char = UDF_FILE_CHAR_DIR;
4535
4536 /* malloc scrap buffer */
4537 fid = malloc(lb_size, M_TEMP, M_WAITOK);
4538 bzero(fid, lb_size);
4539
4540 /* calculate _minimum_ fid size */
4541 unix_to_udf_name((char *) fid->data, &fid->l_fi,
4542 cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
4543 fidsize = UDF_FID_SIZE + fid->l_fi;
4544 fidsize = (fidsize + 3) & ~3; /* multiple of 4 */
4545
4546 /* find position that will fit the FID */
4547 chosen_fid_pos = dir_size;
4548 chosen_size = 0;
4549 chosen_size_diff = UINT_MAX;
4550
4551 /* shut up gcc */
4552 dirent.d_namlen = 0;
4553
4554 /* search our dirhash hits */
4555 error = 0;
4556 dirh_ep = NULL;
4557 for (;;) {
4558 hit = dirhash_lookup_freed(dirh, fidsize, &dirh_ep);
4559 /* if no hit, abort the search */
4560 if (!hit)
4561 break;
4562
4563 /* check this hit for size */
4564 this_fidsize = dirh_ep->entry_size;
4565
4566 /* check this hit */
4567 fid_pos = dirh_ep->offset;
4568 end_fid_pos = fid_pos + this_fidsize;
4569 size_diff = this_fidsize - fidsize;
4570 lb_rest = lb_size - (end_fid_pos % lb_size);
4571
4572 #ifndef UDF_COMPLETE_DELETE
4573 /* transfer a new fid/dirent */
4574 error = udf_read_fid_stream(vp, &fid_pos, fid, dirent);
4575 if (error)
4576 goto error_out;
4577
4578 /* only reuse entries that are wiped */
4579 /* check if the len + loc are marked zero */
4580 if (udf_rw32(fid->icb.len != 0))
4581 continue;
4582 if (udf_rw32(fid->icb.loc.lb_num) != 0)
4583 continue;
4584 if (udf_rw16(fid->icb.loc.part_num != 0))
4585 continue;
4586 #endif /* UDF_COMPLETE_DELETE */
4587
4588 /* select if not splitting the tag and its smaller */
4589 if ((size_diff >= 0) &&
4590 (size_diff < chosen_size_diff) &&
4591 (lb_rest >= sizeof(struct desc_tag)))
4592 {
4593 /* UDF 2.3.4.2+3 specifies rules for iu size */
4594 if ((size_diff == 0) || (size_diff >= 32)) {
4595 chosen_fid_pos = fid_pos;
4596 chosen_size = this_fidsize;
4597 chosen_size_diff = size_diff;
4598 }
4599 }
4600 }
4601
4602
4603 /* extend directory if no other candidate found */
4604 if (chosen_size == 0) {
4605 chosen_fid_pos = dir_size;
4606 chosen_size = fidsize;
4607 chosen_size_diff = 0;
4608
4609 /* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */
4610 if (addr_type == UDF_ICB_INTERN_ALLOC) {
4611 /* pre-grow directory to see if we're to switch */
4612 udf_grow_node(dir_node, dir_size + chosen_size);
4613
4614 icbflags = udf_rw16(icbtag->flags);
4615 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
4616 }
4617
4618 /* make sure the next fid desc_tag won't be splitted */
4619 if (addr_type != UDF_ICB_INTERN_ALLOC) {
4620 end_fid_pos = chosen_fid_pos + chosen_size;
4621 lb_rest = lb_size - (end_fid_pos % lb_size);
4622
4623 /* pad with implementation use regid if needed */
4624 if (lb_rest < sizeof(struct desc_tag))
4625 chosen_size += 32;
4626 }
4627 }
4628 chosen_size_diff = chosen_size - fidsize;
4629
4630 /* populate the FID */
4631 memset(fid, 0, lb_size);
4632 udf_inittag(ump, &fid->tag, TAGID_FID, 0);
4633 fid->file_version_num = udf_rw16(1); /* UDF 2.3.4.1 */
4634 fid->file_char = file_char;
4635 fid->icb = udf_node->loc;
4636 fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
4637 fid->l_iu = udf_rw16(0);
4638
4639 if (chosen_size > fidsize) {
4640 /* insert implementation-use regid to space it correctly */
4641 fid->l_iu = udf_rw16(chosen_size_diff);
4642
4643 /* set implementation use */
4644 udf_set_regid((struct regid *) fid->data, IMPL_NAME);
4645 udf_add_impl_regid(ump, (struct regid *) fid->data);
4646 }
4647
4648 /* fill in name */
4649 unix_to_udf_name((char *) fid->data + udf_rw16(fid->l_iu),
4650 &fid->l_fi, cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
4651
4652 fid->tag.desc_crc_len = chosen_size - UDF_DESC_TAG_LENGTH;
4653 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
4654
4655 /* writeout FID/update parent directory */
4656 error = vn_rdwr(UIO_WRITE, dvp,
4657 fid, chosen_size, chosen_fid_pos,
4658 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
4659 FSCRED, NULL, NULL);
4660
4661 if (error)
4662 goto error_out;
4663
4664 /* add reference counter in attached node */
4665 if (udf_node->fe) {
4666 refcnt = udf_rw16(udf_node->fe->link_cnt);
4667 udf_node->fe->link_cnt = udf_rw16(refcnt+1);
4668 } else {
4669 KASSERT(udf_node->efe);
4670 refcnt = udf_rw16(udf_node->efe->link_cnt);
4671 udf_node->efe->link_cnt = udf_rw16(refcnt+1);
4672 }
4673
4674 /* mark not deleted if it was... just in case, but do warn */
4675 if (udf_node->i_flags & IN_DELETED) {
4676 printf("udf: warning, marking a file undeleted\n");
4677 udf_node->i_flags &= ~IN_DELETED;
4678 }
4679
4680 if (file_char & UDF_FILE_CHAR_DIR) {
4681 /* add reference counter in directory node for '..' */
4682 if (dir_node->fe) {
4683 refcnt = udf_rw16(dir_node->fe->link_cnt);
4684 refcnt++;
4685 dir_node->fe->link_cnt = udf_rw16(refcnt);
4686 } else {
4687 KASSERT(dir_node->efe);
4688 refcnt = udf_rw16(dir_node->efe->link_cnt);
4689 refcnt++;
4690 dir_node->efe->link_cnt = udf_rw16(refcnt);
4691 }
4692 }
4693
4694 /* append to the dirhash */
4695 dirent.d_namlen = cnp->cn_namelen;
4696 memcpy(dirent.d_name, cnp->cn_nameptr, cnp->cn_namelen);
4697 dirhash_enter(dirh, &dirent, chosen_fid_pos,
4698 udf_fidsize(fid), 1);
4699
4700 /* note updates */
4701 udf_node->i_flags |= IN_CHANGE | IN_MODIFY; /* | IN_CREATE? */
4702 /* VN_KNOTE(udf_node, ...) */
4703 udf_update(udf_node->vnode, NULL, NULL, NULL, 0);
4704
4705 error_out:
4706 free(fid, M_TEMP);
4707
4708 dirhash_put(dir_node->dir_hash);
4709
4710 return error;
4711 }
4712
4713 /* --------------------------------------------------------------------- */
4714
4715 /*
4716 * Each node can have an attached streamdir node though not recursively. These
4717 * are otherwise known as named substreams/named extended attributes that have
4718 * no size limitations.
4719 *
4720 * `Normal' extended attributes are indicated with a number and are recorded
4721 * in either the fe/efe descriptor itself for small descriptors or recorded in
4722 * the attached extended attribute file. Since these spaces can get
4723 * fragmented, care ought to be taken.
4724 *
4725 * Since the size of the space reserved for allocation descriptors is limited,
4726 * there is a mechanim provided for extending this space; this is done by a
4727 * special extent to allow schrinking of the allocations without breaking the
4728 * linkage to the allocation extent descriptor.
4729 */
4730
4731 int
4732 udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
4733 struct udf_node **udf_noderes)
4734 {
4735 union dscrptr *dscr;
4736 struct udf_node *udf_node;
4737 struct vnode *nvp;
4738 struct long_ad icb_loc, last_fe_icb_loc;
4739 uint64_t file_size;
4740 uint32_t lb_size, sector, dummy;
4741 uint8_t *file_data;
4742 int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
4743 int slot, eof, error;
4744
4745 DPRINTF(NODE, ("udf_get_node called\n"));
4746 *udf_noderes = udf_node = NULL;
4747
4748 /* lock to disallow simultanious creation of same udf_node */
4749 mutex_enter(&ump->get_node_lock);
4750
4751 DPRINTF(NODE, ("\tlookup in hash table\n"));
4752 /* lookup in hash table */
4753 assert(ump);
4754 assert(node_icb_loc);
4755 udf_node = udf_hash_lookup(ump, node_icb_loc);
4756 if (udf_node) {
4757 DPRINTF(NODE, ("\tgot it from the hash!\n"));
4758 /* vnode is returned locked */
4759 *udf_noderes = udf_node;
4760 mutex_exit(&ump->get_node_lock);
4761 return 0;
4762 }
4763
4764 /* garbage check: translate udf_node_icb_loc to sectornr */
4765 error = udf_translate_vtop(ump, node_icb_loc, §or, &dummy);
4766 if (error) {
4767 /* no use, this will fail anyway */
4768 mutex_exit(&ump->get_node_lock);
4769 return EINVAL;
4770 }
4771
4772 /* build udf_node (do initialise!) */
4773 udf_node = pool_get(&udf_node_pool, PR_WAITOK);
4774 memset(udf_node, 0, sizeof(struct udf_node));
4775
4776 DPRINTF(NODE, ("\tget new vnode\n"));
4777 /* give it a vnode */
4778 error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp);
4779 if (error) {
4780 pool_put(&udf_node_pool, udf_node);
4781 mutex_exit(&ump->get_node_lock);
4782 return error;
4783 }
4784
4785 /* always return locked vnode */
4786 if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) {
4787 /* recycle vnode and unlock; simultanious will fail too */
4788 ungetnewvnode(nvp);
4789 mutex_exit(&ump->get_node_lock);
4790 return error;
4791 }
4792
4793 /* initialise crosslinks, note location of fe/efe for hashing */
4794 udf_node->ump = ump;
4795 udf_node->vnode = nvp;
4796 nvp->v_data = udf_node;
4797 udf_node->loc = *node_icb_loc;
4798 udf_node->lockf = 0;
4799 mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
4800 cv_init(&udf_node->node_lock, "udf_nlk");
4801 genfs_node_init(nvp, &udf_genfsops); /* inititise genfs */
4802 udf_node->outstanding_bufs = 0;
4803 udf_node->outstanding_nodedscr = 0;
4804
4805 if (memcmp(&udf_node->loc, &ump->fileset_desc->rootdir_icb, sizeof(struct long_ad)) == 0)
4806 nvp->v_vflag |= VV_ROOT;
4807
4808 /* insert into the hash lookup */
4809 udf_register_node(udf_node);
4810
4811 /* safe to unlock, the entry is in the hash table, vnode is locked */
4812 mutex_exit(&ump->get_node_lock);
4813
4814 icb_loc = *node_icb_loc;
4815 needs_indirect = 0;
4816 strat4096 = 0;
4817 udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
4818 file_size = 0;
4819 file_data = NULL;
4820 lb_size = udf_rw32(ump->logical_vol->lb_size);
4821
4822 DPRINTF(NODE, ("\tstart reading descriptors\n"));
4823 do {
4824 /* try to read in fe/efe */
4825 error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
4826
4827 /* blank sector marks end of sequence, check this */
4828 if ((dscr == NULL) && (!strat4096))
4829 error = ENOENT;
4830
4831 /* break if read error or blank sector */
4832 if (error || (dscr == NULL))
4833 break;
4834
4835 /* process descriptor based on the descriptor type */
4836 dscr_type = udf_rw16(dscr->tag.id);
4837 DPRINTF(NODE, ("\tread descriptor %d\n", dscr_type));
4838
4839 /* if dealing with an indirect entry, follow the link */
4840 if (dscr_type == TAGID_INDIRECTENTRY) {
4841 needs_indirect = 0;
4842 udf_free_logvol_dscr(ump, &icb_loc, dscr);
4843 icb_loc = dscr->inde.indirect_icb;
4844 continue;
4845 }
4846
4847 /* only file entries and extended file entries allowed here */
4848 if ((dscr_type != TAGID_FENTRY) &&
4849 (dscr_type != TAGID_EXTFENTRY)) {
4850 udf_free_logvol_dscr(ump, &icb_loc, dscr);
4851 error = ENOENT;
4852 break;
4853 }
4854
4855 KASSERT(udf_tagsize(dscr, lb_size) == lb_size);
4856
4857 /* choose this one */
4858 last_fe_icb_loc = icb_loc;
4859
4860 /* record and process/update (ext)fentry */
4861 file_data = NULL;
4862 if (dscr_type == TAGID_FENTRY) {
4863 if (udf_node->fe)
4864 udf_free_logvol_dscr(ump, &last_fe_icb_loc,
4865 udf_node->fe);
4866 udf_node->fe = &dscr->fe;
4867 strat = udf_rw16(udf_node->fe->icbtag.strat_type);
4868 udf_file_type = udf_node->fe->icbtag.file_type;
4869 file_size = udf_rw64(udf_node->fe->inf_len);
4870 file_data = udf_node->fe->data;
4871 } else {
4872 if (udf_node->efe)
4873 udf_free_logvol_dscr(ump, &last_fe_icb_loc,
4874 udf_node->efe);
4875 udf_node->efe = &dscr->efe;
4876 strat = udf_rw16(udf_node->efe->icbtag.strat_type);
4877 udf_file_type = udf_node->efe->icbtag.file_type;
4878 file_size = udf_rw64(udf_node->efe->inf_len);
4879 file_data = udf_node->efe->data;
4880 }
4881
4882 /* check recording strategy (structure) */
4883
4884 /*
4885 * Strategy 4096 is a daisy linked chain terminating with an
4886 * unrecorded sector or a TERM descriptor. The next
4887 * descriptor is to be found in the sector that follows the
4888 * current sector.
4889 */
4890 if (strat == 4096) {
4891 strat4096 = 1;
4892 needs_indirect = 1;
4893
4894 icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
4895 }
4896
4897 /*
4898 * Strategy 4 is the normal strategy and terminates, but if
4899 * we're in strategy 4096, we can't have strategy 4 mixed in
4900 */
4901
4902 if (strat == 4) {
4903 if (strat4096) {
4904 error = EINVAL;
4905 break;
4906 }
4907 break; /* done */
4908 }
4909 } while (!error);
4910
4911 /* first round of cleanup code */
4912 if (error) {
4913 DPRINTF(NODE, ("\tnode fe/efe failed!\n"));
4914 /* recycle udf_node */
4915 udf_dispose_node(udf_node);
4916
4917 vlockmgr(nvp->v_vnlock, LK_RELEASE);
4918 nvp->v_data = NULL;
4919 ungetnewvnode(nvp);
4920
4921 return EINVAL; /* error code ok? */
4922 }
4923 DPRINTF(NODE, ("\tnode fe/efe read in fine\n"));
4924
4925 /* assert no references to dscr anymore beyong this point */
4926 assert((udf_node->fe) || (udf_node->efe));
4927 dscr = NULL;
4928
4929 /*
4930 * Remember where to record an updated version of the descriptor. If
4931 * there is a sequence of indirect entries, icb_loc will have been
4932 * updated. Its the write disipline to allocate new space and to make
4933 * sure the chain is maintained.
4934 *
4935 * `needs_indirect' flags if the next location is to be filled with
4936 * with an indirect entry.
4937 */
4938 udf_node->write_loc = icb_loc;
4939 udf_node->needs_indirect = needs_indirect;
4940
4941 /*
4942 * Go trough all allocations extents of this descriptor and when
4943 * encountering a redirect read in the allocation extension. These are
4944 * daisy-chained.
4945 */
4946 UDF_LOCK_NODE(udf_node, 0);
4947 udf_node->num_extensions = 0;
4948
4949 error = 0;
4950 slot = 0;
4951 for (;;) {
4952 udf_get_adslot(udf_node, slot, &icb_loc, &eof);
4953 DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
4954 "lb_num = %d, part = %d\n", slot, eof,
4955 UDF_EXT_FLAGS(udf_rw32(icb_loc.len)),
4956 UDF_EXT_LEN(udf_rw32(icb_loc.len)),
4957 udf_rw32(icb_loc.loc.lb_num),
4958 udf_rw16(icb_loc.loc.part_num)));
4959 if (eof)
4960 break;
4961 slot++;
4962
4963 if (UDF_EXT_FLAGS(udf_rw32(icb_loc.len)) != UDF_EXT_REDIRECT)
4964 continue;
4965
4966 DPRINTF(NODE, ("\tgot redirect extent\n"));
4967 if (udf_node->num_extensions >= UDF_MAX_ALLOC_EXTENTS) {
4968 DPRINTF(ALLOC, ("udf_get_node: implementation limit, "
4969 "too many allocation extensions on "
4970 "udf_node\n"));
4971 error = EINVAL;
4972 break;
4973 }
4974
4975 /* length can only be *one* lb : UDF 2.50/2.3.7.1 */
4976 if (UDF_EXT_LEN(udf_rw32(icb_loc.len)) != lb_size) {
4977 DPRINTF(ALLOC, ("udf_get_node: bad allocation "
4978 "extension size in udf_node\n"));
4979 error = EINVAL;
4980 break;
4981 }
4982
4983 DPRINTF(NODE, ("read allocation extent at lb_num %d\n",
4984 UDF_EXT_LEN(udf_rw32(icb_loc.loc.lb_num))));
4985 /* load in allocation extent */
4986 error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
4987 if (error || (dscr == NULL))
4988 break;
4989
4990 /* process read-in descriptor */
4991 dscr_type = udf_rw16(dscr->tag.id);
4992
4993 if (dscr_type != TAGID_ALLOCEXTENT) {
4994 udf_free_logvol_dscr(ump, &icb_loc, dscr);
4995 error = ENOENT;
4996 break;
4997 }
4998
4999 DPRINTF(NODE, ("\trecording redirect extent\n"));
5000 udf_node->ext[udf_node->num_extensions] = &dscr->aee;
5001 udf_node->ext_loc[udf_node->num_extensions] = icb_loc;
5002
5003 udf_node->num_extensions++;
5004
5005 } /* while */
5006 UDF_UNLOCK_NODE(udf_node, 0);
5007
5008 /* second round of cleanup code */
5009 if (error) {
5010 /* recycle udf_node */
5011 udf_dispose_node(udf_node);
5012
5013 vlockmgr(nvp->v_vnlock, LK_RELEASE);
5014 nvp->v_data = NULL;
5015 ungetnewvnode(nvp);
5016
5017 return EINVAL; /* error code ok? */
5018 }
5019
5020 DPRINTF(NODE, ("\tnode read in fine\n"));
5021
5022 /*
5023 * Translate UDF filetypes into vnode types.
5024 *
5025 * Systemfiles like the meta main and mirror files are not treated as
5026 * normal files, so we type them as having no type. UDF dictates that
5027 * they are not allowed to be visible.
5028 */
5029
5030 switch (udf_file_type) {
5031 case UDF_ICB_FILETYPE_DIRECTORY :
5032 case UDF_ICB_FILETYPE_STREAMDIR :
5033 nvp->v_type = VDIR;
5034 break;
5035 case UDF_ICB_FILETYPE_BLOCKDEVICE :
5036 nvp->v_type = VBLK;
5037 break;
5038 case UDF_ICB_FILETYPE_CHARDEVICE :
5039 nvp->v_type = VCHR;
5040 break;
5041 case UDF_ICB_FILETYPE_SOCKET :
5042 nvp->v_type = VSOCK;
5043 break;
5044 case UDF_ICB_FILETYPE_FIFO :
5045 nvp->v_type = VFIFO;
5046 break;
5047 case UDF_ICB_FILETYPE_SYMLINK :
5048 nvp->v_type = VLNK;
5049 break;
5050 case UDF_ICB_FILETYPE_VAT :
5051 case UDF_ICB_FILETYPE_META_MAIN :
5052 case UDF_ICB_FILETYPE_META_MIRROR :
5053 nvp->v_type = VNON;
5054 break;
5055 case UDF_ICB_FILETYPE_RANDOMACCESS :
5056 case UDF_ICB_FILETYPE_REALTIME :
5057 nvp->v_type = VREG;
5058 break;
5059 default:
5060 /* YIKES, something else */
5061 nvp->v_type = VNON;
5062 }
5063
5064 /* TODO specfs, fifofs etc etc. vnops setting */
5065
5066 /* don't forget to set vnode's v_size */
5067 uvm_vnp_setsize(nvp, file_size);
5068
5069 /* TODO ext attr and streamdir udf_nodes */
5070
5071 *udf_noderes = udf_node;
5072
5073 return 0;
5074 }
5075
5076 /* --------------------------------------------------------------------- */
5077
5078 int
5079 udf_writeout_node(struct udf_node *udf_node, int waitfor)
5080 {
5081 union dscrptr *dscr;
5082 struct long_ad *loc;
5083 int extnr, flags, error;
5084
5085 DPRINTF(NODE, ("udf_writeout_node called\n"));
5086
5087 KASSERT(udf_node->outstanding_bufs == 0);
5088 KASSERT(udf_node->outstanding_nodedscr == 0);
5089
5090 KASSERT(LIST_EMPTY(&udf_node->vnode->v_dirtyblkhd));
5091
5092 if (udf_node->i_flags & IN_DELETED) {
5093 DPRINTF(NODE, ("\tnode deleted; not writing out\n"));
5094 return 0;
5095 }
5096
5097 /* lock node */
5098 flags = waitfor ? 0 : IN_CALLBACK_ULK;
5099 UDF_LOCK_NODE(udf_node, flags);
5100
5101 /* at least one descriptor writeout */
5102 udf_node->outstanding_nodedscr = 1;
5103
5104 /* we're going to write out the descriptor so clear the flags */
5105 udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED);
5106
5107 /* if we were rebuild, write out the allocation extents */
5108 if (udf_node->i_flags & IN_NODE_REBUILD) {
5109 /* mark outstanding node descriptors and issue them */
5110 udf_node->outstanding_nodedscr += udf_node->num_extensions;
5111 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
5112 loc = &udf_node->ext_loc[extnr];
5113 dscr = (union dscrptr *) udf_node->ext[extnr];
5114 error = udf_write_logvol_dscr(udf_node, dscr, loc, 0);
5115 if (error)
5116 return error;
5117 }
5118 /* mark allocation extents written out */
5119 udf_node->i_flags &= ~(IN_NODE_REBUILD);
5120 }
5121
5122 if (udf_node->fe) {
5123 KASSERT(udf_node->efe == NULL);
5124 dscr = (union dscrptr *) udf_node->fe;
5125 } else {
5126 KASSERT(udf_node->efe);
5127 KASSERT(udf_node->fe == NULL);
5128 dscr = (union dscrptr *) udf_node->efe;
5129 }
5130 KASSERT(dscr);
5131
5132 loc = &udf_node->write_loc;
5133 error = udf_write_logvol_dscr(udf_node, dscr, loc, waitfor);
5134 return error;
5135 }
5136
5137 /* --------------------------------------------------------------------- */
5138
5139 int
5140 udf_dispose_node(struct udf_node *udf_node)
5141 {
5142 struct vnode *vp;
5143 int extnr;
5144
5145 DPRINTF(NODE, ("udf_dispose_node called on node %p\n", udf_node));
5146 if (!udf_node) {
5147 DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
5148 return 0;
5149 }
5150
5151 vp = udf_node->vnode;
5152 #ifdef DIAGNOSTIC
5153 if (vp->v_numoutput)
5154 panic("disposing UDF node with pending I/O's, udf_node = %p, "
5155 "v_numoutput = %d", udf_node, vp->v_numoutput);
5156 #endif
5157
5158 /* wait until out of sync (just in case we happen to stumble over one */
5159 KASSERT(!mutex_owned(&mntvnode_lock));
5160 mutex_enter(&mntvnode_lock);
5161 while (udf_node->i_flags & IN_SYNCED) {
5162 cv_timedwait(&udf_node->ump->dirtynodes_cv, &mntvnode_lock,
5163 hz/16);
5164 }
5165 mutex_exit(&mntvnode_lock);
5166
5167 /* TODO extended attributes and streamdir */
5168
5169 /* remove dirhash if present */
5170 dirhash_purge(&udf_node->dir_hash);
5171
5172 /* remove from our hash lookup table */
5173 udf_deregister_node(udf_node);
5174
5175 /* destroy our lock */
5176 mutex_destroy(&udf_node->node_mutex);
5177 cv_destroy(&udf_node->node_lock);
5178
5179 /* dissociate our udf_node from the vnode */
5180 genfs_node_destroy(udf_node->vnode);
5181 vp->v_data = NULL;
5182
5183 /* free associated memory and the node itself */
5184 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
5185 udf_free_logvol_dscr(udf_node->ump, &udf_node->ext_loc[extnr],
5186 udf_node->ext[extnr]);
5187 udf_node->ext[extnr] = (void *) 0xdeadcccc;
5188 }
5189
5190 if (udf_node->fe)
5191 udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
5192 udf_node->fe);
5193 if (udf_node->efe)
5194 udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
5195 udf_node->efe);
5196
5197 udf_node->fe = (void *) 0xdeadaaaa;
5198 udf_node->efe = (void *) 0xdeadbbbb;
5199 udf_node->ump = (void *) 0xdeadbeef;
5200 pool_put(&udf_node_pool, udf_node);
5201
5202 return 0;
5203 }
5204
5205
5206
5207 /*
5208 * create a new node using the specified vnodeops, vap and cnp but with the
5209 * udf_file_type. This allows special files to be created. Use with care.
5210 */
5211
5212 static int
5213 udf_create_node_raw(struct vnode *dvp, struct vnode **vpp, int udf_file_type,
5214 int (**vnodeops)(void *), struct vattr *vap, struct componentname *cnp)
5215 {
5216 union dscrptr *dscr;
5217 struct udf_node *dir_node = VTOI(dvp);;
5218 struct udf_node *udf_node;
5219 struct udf_mount *ump = dir_node->ump;
5220 struct vnode *nvp;
5221 struct long_ad node_icb_loc;
5222 uint64_t parent_unique_id;
5223 uint64_t lmapping;
5224 uint32_t lb_size, lb_num;
5225 uint16_t vpart_num;
5226 uid_t uid;
5227 gid_t gid, parent_gid;
5228 int fid_size, error;
5229
5230 lb_size = udf_rw32(ump->logical_vol->lb_size);
5231 *vpp = NULL;
5232
5233 /* allocate vnode */
5234 error = getnewvnode(VT_UDF, ump->vfs_mountp, vnodeops, &nvp);
5235 if (error)
5236 return error;
5237
5238 /* lock node */
5239 error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
5240 if (error) {
5241 nvp->v_data = NULL;
5242 ungetnewvnode(nvp);
5243 return error;
5244 }
5245
5246 /* get disc allocation for one logical block */
5247 vpart_num = ump->node_part;
5248 error = udf_pre_allocate_space(ump, UDF_C_NODE, 1,
5249 vpart_num, &lmapping);
5250 lb_num = lmapping;
5251 if (error) {
5252 vlockmgr(nvp->v_vnlock, LK_RELEASE);
5253 ungetnewvnode(nvp);
5254 return error;
5255 }
5256
5257 /* initialise pointer to location */
5258 memset(&node_icb_loc, 0, sizeof(struct long_ad));
5259 node_icb_loc.len = lb_size;
5260 node_icb_loc.loc.lb_num = udf_rw32(lb_num);
5261 node_icb_loc.loc.part_num = udf_rw16(vpart_num);
5262
5263 /* build udf_node (do initialise!) */
5264 udf_node = pool_get(&udf_node_pool, PR_WAITOK);
5265 memset(udf_node, 0, sizeof(struct udf_node));
5266
5267 /* initialise crosslinks, note location of fe/efe for hashing */
5268 /* bugalert: synchronise with udf_get_node() */
5269 udf_node->ump = ump;
5270 udf_node->vnode = nvp;
5271 nvp->v_data = udf_node;
5272 udf_node->loc = node_icb_loc;
5273 udf_node->write_loc = node_icb_loc;
5274 udf_node->lockf = 0;
5275 mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
5276 cv_init(&udf_node->node_lock, "udf_nlk");
5277 udf_node->outstanding_bufs = 0;
5278 udf_node->outstanding_nodedscr = 0;
5279
5280 /* initialise genfs */
5281 genfs_node_init(nvp, &udf_genfsops);
5282
5283 /* insert into the hash lookup */
5284 udf_register_node(udf_node);
5285
5286 /* get parent's unique ID for refering '..' if its a directory */
5287 if (dir_node->fe) {
5288 parent_unique_id = udf_rw64(dir_node->fe->unique_id);
5289 parent_gid = (gid_t) udf_rw32(dir_node->fe->gid);
5290 } else {
5291 parent_unique_id = udf_rw64(dir_node->efe->unique_id);
5292 parent_gid = (gid_t) udf_rw32(dir_node->efe->gid);
5293 }
5294
5295 /* get descriptor */
5296 udf_create_logvol_dscr(ump, udf_node, &node_icb_loc, &dscr);
5297
5298 /* choose a fe or an efe for it */
5299 if (ump->logical_vol->tag.descriptor_ver == 2) {
5300 udf_node->fe = &dscr->fe;
5301 fid_size = udf_create_new_fe(ump, udf_node->fe,
5302 udf_file_type, &udf_node->loc,
5303 &dir_node->loc, parent_unique_id);
5304 /* TODO add extended attribute for creation time */
5305 } else {
5306 udf_node->efe = &dscr->efe;
5307 fid_size = udf_create_new_efe(ump, udf_node->efe,
5308 udf_file_type, &udf_node->loc,
5309 &dir_node->loc, parent_unique_id);
5310 }
5311 KASSERT(dscr->tag.tag_loc == udf_node->loc.loc.lb_num);
5312
5313 /* update vnode's size and type */
5314 nvp->v_type = vap->va_type;
5315 uvm_vnp_setsize(nvp, fid_size);
5316
5317 /* set access mode */
5318 udf_setaccessmode(udf_node, vap->va_mode);
5319
5320 /* set ownership */
5321 uid = kauth_cred_geteuid(cnp->cn_cred);
5322 gid = parent_gid;
5323 udf_setownership(udf_node, uid, gid);
5324
5325 error = udf_dir_attach(ump, dir_node, udf_node, vap, cnp);
5326 if (error) {
5327 /* free disc allocation for node */
5328 udf_free_allocated_space(ump, lb_num, vpart_num, 1);
5329
5330 /* recycle udf_node */
5331 udf_dispose_node(udf_node);
5332 vput(nvp);
5333
5334 *vpp = NULL;
5335 return error;
5336 }
5337
5338 /* adjust file count */
5339 udf_adjust_filecount(udf_node, 1);
5340
5341 /* return result */
5342 *vpp = nvp;
5343
5344 return 0;
5345 }
5346
5347
5348 int
5349 udf_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
5350 struct componentname *cnp)
5351 {
5352 int (**vnodeops)(void *);
5353 int udf_file_type;
5354
5355 DPRINTF(NODE, ("udf_create_node called\n"));
5356
5357 /* what type are we creating ? */
5358 vnodeops = udf_vnodeop_p;
5359 /* start with a default */
5360 udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
5361
5362 *vpp = NULL;
5363
5364 switch (vap->va_type) {
5365 case VREG :
5366 udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
5367 break;
5368 case VDIR :
5369 udf_file_type = UDF_ICB_FILETYPE_DIRECTORY;
5370 break;
5371 case VLNK :
5372 udf_file_type = UDF_ICB_FILETYPE_SYMLINK;
5373 break;
5374 case VBLK :
5375 udf_file_type = UDF_ICB_FILETYPE_BLOCKDEVICE;
5376 /* specfs */
5377 return ENOTSUP;
5378 break;
5379 case VCHR :
5380 udf_file_type = UDF_ICB_FILETYPE_CHARDEVICE;
5381 /* specfs */
5382 return ENOTSUP;
5383 break;
5384 case VFIFO :
5385 udf_file_type = UDF_ICB_FILETYPE_FIFO;
5386 /* specfs */
5387 return ENOTSUP;
5388 break;
5389 case VSOCK :
5390 udf_file_type = UDF_ICB_FILETYPE_SOCKET;
5391 /* specfs */
5392 return ENOTSUP;
5393 break;
5394 case VNON :
5395 case VBAD :
5396 default :
5397 /* nothing; can we even create these? */
5398 return EINVAL;
5399 }
5400
5401 return udf_create_node_raw(dvp, vpp, udf_file_type, vnodeops, vap, cnp);
5402 }
5403
5404 /* --------------------------------------------------------------------- */
5405
5406 static void
5407 udf_free_descriptor_space(struct udf_node *udf_node, struct long_ad *loc, void *mem)
5408 {
5409 struct udf_mount *ump = udf_node->ump;
5410 uint32_t lb_size, lb_num, len, num_lb;
5411 uint16_t vpart_num;
5412
5413 /* is there really one? */
5414 if (mem == NULL)
5415 return;
5416
5417 /* got a descriptor here */
5418 len = UDF_EXT_LEN(udf_rw32(loc->len));
5419 lb_num = udf_rw32(loc->loc.lb_num);
5420 vpart_num = udf_rw16(loc->loc.part_num);
5421
5422 lb_size = udf_rw32(ump->logical_vol->lb_size);
5423 num_lb = (len + lb_size -1) / lb_size;
5424
5425 udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
5426 }
5427
5428 void
5429 udf_delete_node(struct udf_node *udf_node)
5430 {
5431 void *dscr;
5432 struct udf_mount *ump;
5433 struct long_ad *loc;
5434 int extnr, lvint, dummy;
5435
5436 ump = udf_node->ump;
5437
5438 /* paranoia check on integrity; should be open!; we could panic */
5439 lvint = udf_rw32(udf_node->ump->logvol_integrity->integrity_type);
5440 if (lvint == UDF_INTEGRITY_CLOSED)
5441 printf("\tIntegrity was CLOSED!\n");
5442
5443 /* whatever the node type, change its size to zero */
5444 (void) udf_resize_node(udf_node, 0, &dummy);
5445
5446 /* force it to be `clean'; no use writing it out */
5447 udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED | IN_ACCESS |
5448 IN_CHANGE | IN_UPDATE | IN_MODIFY);
5449
5450 /* adjust file count */
5451 udf_adjust_filecount(udf_node, -1);
5452
5453 /*
5454 * Free its allocated descriptors; memory will be released when
5455 * vop_reclaim() is called.
5456 */
5457 loc = &udf_node->loc;
5458
5459 dscr = udf_node->fe;
5460 udf_free_descriptor_space(udf_node, loc, dscr);
5461 dscr = udf_node->efe;
5462 udf_free_descriptor_space(udf_node, loc, dscr);
5463
5464 for (extnr = 0; extnr < UDF_MAX_ALLOC_EXTENTS; extnr++) {
5465 dscr = udf_node->ext[extnr];
5466 loc = &udf_node->ext_loc[extnr];
5467 udf_free_descriptor_space(udf_node, loc, dscr);
5468 }
5469 }
5470
5471 /* --------------------------------------------------------------------- */
5472
5473 /* set new filesize; node but be LOCKED on entry and is locked on exit */
5474 int
5475 udf_resize_node(struct udf_node *udf_node, uint64_t new_size, int *extended)
5476 {
5477 struct file_entry *fe = udf_node->fe;
5478 struct extfile_entry *efe = udf_node->efe;
5479 uint64_t file_size;
5480 int error;
5481
5482 if (fe) {
5483 file_size = udf_rw64(fe->inf_len);
5484 } else {
5485 assert(udf_node->efe);
5486 file_size = udf_rw64(efe->inf_len);
5487 }
5488
5489 DPRINTF(ATTR, ("\tchanging file length from %"PRIu64" to %"PRIu64"\n",
5490 file_size, new_size));
5491
5492 /* if not changing, we're done */
5493 if (file_size == new_size)
5494 return 0;
5495
5496 *extended = (new_size > file_size);
5497 if (*extended) {
5498 error = udf_grow_node(udf_node, new_size);
5499 } else {
5500 error = udf_shrink_node(udf_node, new_size);
5501 }
5502
5503 return error;
5504 }
5505
5506
5507 /* --------------------------------------------------------------------- */
5508
5509 void
5510 udf_itimes(struct udf_node *udf_node, struct timespec *acc,
5511 struct timespec *mod, struct timespec *birth)
5512 {
5513 struct timespec now;
5514 struct file_entry *fe;
5515 struct extfile_entry *efe;
5516 struct filetimes_extattr_entry *ft_extattr;
5517 struct timestamp *atime, *mtime, *attrtime, *ctime;
5518 struct timestamp fe_ctime;
5519 struct timespec cur_birth;
5520 uint32_t offset, a_l;
5521 uint8_t *filedata;
5522 int error;
5523
5524 /* protect against rogue values */
5525 if (!udf_node)
5526 return;
5527
5528 fe = udf_node->fe;
5529 efe = udf_node->efe;
5530
5531 if (!(udf_node->i_flags & (IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY)))
5532 return;
5533
5534 /* get descriptor information */
5535 if (fe) {
5536 atime = &fe->atime;
5537 mtime = &fe->mtime;
5538 attrtime = &fe->attrtime;
5539 filedata = fe->data;
5540
5541 /* initial save dummy setting */
5542 ctime = &fe_ctime;
5543
5544 /* check our extended attribute if present */
5545 error = udf_extattr_search_intern(udf_node,
5546 UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
5547 if (!error) {
5548 ft_extattr = (struct filetimes_extattr_entry *)
5549 (filedata + offset);
5550 if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
5551 ctime = &ft_extattr->times[0];
5552 }
5553 /* TODO create the extended attribute if not found ? */
5554 } else {
5555 assert(udf_node->efe);
5556 atime = &efe->atime;
5557 mtime = &efe->mtime;
5558 attrtime = &efe->attrtime;
5559 ctime = &efe->ctime;
5560 }
5561
5562 vfs_timestamp(&now);
5563
5564 /* set access time */
5565 if (udf_node->i_flags & IN_ACCESS) {
5566 if (acc == NULL)
5567 acc = &now;
5568 udf_timespec_to_timestamp(acc, atime);
5569 }
5570
5571 /* set modification time */
5572 if (udf_node->i_flags & (IN_UPDATE | IN_MODIFY)) {
5573 if (mod == NULL)
5574 mod = &now;
5575 udf_timespec_to_timestamp(mod, mtime);
5576
5577 /* ensure birthtime is older than set modification! */
5578 udf_timestamp_to_timespec(udf_node->ump, ctime, &cur_birth);
5579 if ((cur_birth.tv_sec > mod->tv_sec) ||
5580 ((cur_birth.tv_sec == mod->tv_sec) &&
5581 (cur_birth.tv_nsec > mod->tv_nsec))) {
5582 udf_timespec_to_timestamp(mod, ctime);
5583 }
5584 }
5585
5586 /* update birthtime if specified */
5587 /* XXX we asume here that given birthtime is older than mod */
5588 if (birth && (birth->tv_sec != VNOVAL)) {
5589 udf_timespec_to_timestamp(birth, ctime);
5590 }
5591
5592 /* set change time */
5593 if (udf_node->i_flags & (IN_CHANGE | IN_MODIFY))
5594 udf_timespec_to_timestamp(&now, attrtime);
5595
5596 /* notify updates to the node itself */
5597 if (udf_node->i_flags & (IN_ACCESS | IN_MODIFY))
5598 udf_node->i_flags |= IN_ACCESSED;
5599 if (udf_node->i_flags & (IN_UPDATE | IN_CHANGE))
5600 udf_node->i_flags |= IN_MODIFIED;
5601
5602 /* clear modification flags */
5603 udf_node->i_flags &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
5604 }
5605
5606 /* --------------------------------------------------------------------- */
5607
5608 int
5609 udf_update(struct vnode *vp, struct timespec *acc,
5610 struct timespec *mod, struct timespec *birth, int updflags)
5611 {
5612 union dscrptr *dscrptr;
5613 struct udf_node *udf_node = VTOI(vp);
5614 struct udf_mount *ump = udf_node->ump;
5615 struct regid *impl_id;
5616 int mnt_async = (vp->v_mount->mnt_flag & MNT_ASYNC);
5617 int waitfor, flags;
5618
5619 #ifdef DEBUG
5620 char bits[128];
5621 DPRINTF(CALL, ("udf_update(node, %p, %p, %p, %d)\n", acc, mod, birth,
5622 updflags));
5623 snprintb(bits, sizeof(bits), IN_FLAGBITS, udf_node->i_flags);
5624 DPRINTF(CALL, ("\tnode flags %s\n", bits));
5625 DPRINTF(CALL, ("\t\tmnt_async = %d\n", mnt_async));
5626 #endif
5627
5628 /* set our times */
5629 udf_itimes(udf_node, acc, mod, birth);
5630
5631 /* set our implementation id */
5632 if (udf_node->fe) {
5633 dscrptr = (union dscrptr *) udf_node->fe;
5634 impl_id = &udf_node->fe->imp_id;
5635 } else {
5636 dscrptr = (union dscrptr *) udf_node->efe;
5637 impl_id = &udf_node->efe->imp_id;
5638 }
5639
5640 /* set our ID */
5641 udf_set_regid(impl_id, IMPL_NAME);
5642 udf_add_impl_regid(ump, impl_id);
5643
5644 /* update our crc! on RMW we are not allowed to change a thing */
5645 udf_validate_tag_and_crc_sums(dscrptr);
5646
5647 /* if called when mounted readonly, never write back */
5648 if (vp->v_mount->mnt_flag & MNT_RDONLY)
5649 return 0;
5650
5651 /* check if the node is dirty 'enough'*/
5652 if (updflags & UPDATE_CLOSE) {
5653 flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
5654 } else {
5655 flags = udf_node->i_flags & IN_MODIFIED;
5656 }
5657 if (flags == 0)
5658 return 0;
5659
5660 /* determine if we need to write sync or async */
5661 waitfor = 0;
5662 if ((flags & IN_MODIFIED) && (mnt_async == 0)) {
5663 /* sync mounted */
5664 waitfor = updflags & UPDATE_WAIT;
5665 if (updflags & UPDATE_DIROP)
5666 waitfor |= UPDATE_WAIT;
5667 }
5668 if (waitfor)
5669 return VOP_FSYNC(vp, FSCRED, FSYNC_WAIT, 0,0);
5670
5671 return 0;
5672 }
5673
5674
5675 /* --------------------------------------------------------------------- */
5676
5677
5678 /*
5679 * Read one fid and process it into a dirent and advance to the next (*fid)
5680 * has to be allocated a logical block in size, (*dirent) struct dirent length
5681 */
5682
5683 int
5684 udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
5685 struct fileid_desc *fid, struct dirent *dirent)
5686 {
5687 struct udf_node *dir_node = VTOI(vp);
5688 struct udf_mount *ump = dir_node->ump;
5689 struct file_entry *fe = dir_node->fe;
5690 struct extfile_entry *efe = dir_node->efe;
5691 uint32_t fid_size, lb_size;
5692 uint64_t file_size;
5693 char *fid_name;
5694 int enough, error;
5695
5696 assert(fid);
5697 assert(dirent);
5698 assert(dir_node);
5699 assert(offset);
5700 assert(*offset != 1);
5701
5702 DPRINTF(FIDS, ("read_fid_stream called at offset %"PRIu64"\n", *offset));
5703 /* check if we're past the end of the directory */
5704 if (fe) {
5705 file_size = udf_rw64(fe->inf_len);
5706 } else {
5707 assert(dir_node->efe);
5708 file_size = udf_rw64(efe->inf_len);
5709 }
5710 if (*offset >= file_size)
5711 return EINVAL;
5712
5713 /* get maximum length of FID descriptor */
5714 lb_size = udf_rw32(ump->logical_vol->lb_size);
5715
5716 /* initialise return values */
5717 fid_size = 0;
5718 memset(dirent, 0, sizeof(struct dirent));
5719 memset(fid, 0, lb_size);
5720
5721 enough = (file_size - (*offset) >= UDF_FID_SIZE);
5722 if (!enough) {
5723 /* short dir ... */
5724 return EIO;
5725 }
5726
5727 error = vn_rdwr(UIO_READ, vp,
5728 fid, MIN(file_size - (*offset), lb_size), *offset,
5729 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, FSCRED,
5730 NULL, NULL);
5731 if (error)
5732 return error;
5733
5734 DPRINTF(FIDS, ("\tfid piece read in fine\n"));
5735 /*
5736 * Check if we got a whole descriptor.
5737 * TODO Try to `resync' directory stream when something is very wrong.
5738 */
5739
5740 /* check if our FID header is OK */
5741 error = udf_check_tag(fid);
5742 if (error) {
5743 goto brokendir;
5744 }
5745 DPRINTF(FIDS, ("\ttag check ok\n"));
5746
5747 if (udf_rw16(fid->tag.id) != TAGID_FID) {
5748 error = EIO;
5749 goto brokendir;
5750 }
5751 DPRINTF(FIDS, ("\ttag checked ok: got TAGID_FID\n"));
5752
5753 /* check for length */
5754 fid_size = udf_fidsize(fid);
5755 enough = (file_size - (*offset) >= fid_size);
5756 if (!enough) {
5757 error = EIO;
5758 goto brokendir;
5759 }
5760 DPRINTF(FIDS, ("\tthe complete fid is read in\n"));
5761
5762 /* check FID contents */
5763 error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
5764 brokendir:
5765 if (error) {
5766 /* note that is sometimes a bit quick to report */
5767 printf("BROKEN DIRECTORY ENTRY\n");
5768 /* RESYNC? */
5769 /* TODO: use udf_resync_fid_stream */
5770 return EIO;
5771 }
5772 DPRINTF(FIDS, ("\tpayload checked ok\n"));
5773
5774 /* we got a whole and valid descriptor! */
5775 DPRINTF(FIDS, ("\tinterpret FID\n"));
5776
5777 /* create resulting dirent structure */
5778 fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
5779 udf_to_unix_name(dirent->d_name, MAXNAMLEN,
5780 fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
5781
5782 /* '..' has no name, so provide one */
5783 if (fid->file_char & UDF_FILE_CHAR_PAR)
5784 strcpy(dirent->d_name, "..");
5785
5786 dirent->d_fileno = udf_calchash(&fid->icb); /* inode hash XXX */
5787 dirent->d_namlen = strlen(dirent->d_name);
5788 dirent->d_reclen = _DIRENT_SIZE(dirent);
5789
5790 /*
5791 * Note that its not worth trying to go for the filetypes now... its
5792 * too expensive too
5793 */
5794 dirent->d_type = DT_UNKNOWN;
5795
5796 /* initial guess for filetype we can make */
5797 if (fid->file_char & UDF_FILE_CHAR_DIR)
5798 dirent->d_type = DT_DIR;
5799
5800 /* advance */
5801 *offset += fid_size;
5802
5803 return error;
5804 }
5805
5806
5807 /* --------------------------------------------------------------------- */
5808
5809 static void
5810 udf_sync_pass(struct udf_mount *ump, kauth_cred_t cred, int waitfor,
5811 int pass, int *ndirty)
5812 {
5813 struct udf_node *udf_node, *n_udf_node;
5814 struct vnode *vp;
5815 int vdirty, error;
5816 int on_type, on_flags, on_vnode;
5817
5818 derailed:
5819 KASSERT(mutex_owned(&mntvnode_lock));
5820
5821 DPRINTF(SYNC, ("sync_pass %d\n", pass));
5822 udf_node = LIST_FIRST(&ump->sorted_udf_nodes);
5823 for (;udf_node; udf_node = n_udf_node) {
5824 DPRINTF(SYNC, ("."));
5825
5826 udf_node->i_flags &= ~IN_SYNCED;
5827 vp = udf_node->vnode;
5828
5829 mutex_enter(&vp->v_interlock);
5830 n_udf_node = LIST_NEXT(udf_node, sortchain);
5831 if (n_udf_node)
5832 n_udf_node->i_flags |= IN_SYNCED;
5833
5834 /* system nodes are not synced this way */
5835 if (vp->v_vflag & VV_SYSTEM) {
5836 mutex_exit(&vp->v_interlock);
5837 continue;
5838 }
5839
5840 /* check if its dirty enough to even try */
5841 on_type = (waitfor == MNT_LAZY || vp->v_type == VNON);
5842 on_flags = ((udf_node->i_flags &
5843 (IN_ACCESSED | IN_UPDATE | IN_MODIFIED)) == 0);
5844 on_vnode = LIST_EMPTY(&vp->v_dirtyblkhd)
5845 && UVM_OBJ_IS_CLEAN(&vp->v_uobj);
5846 if (on_type || (on_flags || on_vnode)) { /* XXX */
5847 /* not dirty (enough?) */
5848 mutex_exit(&vp->v_interlock);
5849 continue;
5850 }
5851
5852 mutex_exit(&mntvnode_lock);
5853 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
5854 if (error) {
5855 mutex_enter(&mntvnode_lock);
5856 if (error == ENOENT)
5857 goto derailed;
5858 *ndirty += 1;
5859 continue;
5860 }
5861
5862 switch (pass) {
5863 case 1:
5864 VOP_FSYNC(vp, cred, 0 | FSYNC_DATAONLY,0,0);
5865 break;
5866 case 2:
5867 vdirty = vp->v_numoutput;
5868 if (vp->v_tag == VT_UDF)
5869 vdirty += udf_node->outstanding_bufs +
5870 udf_node->outstanding_nodedscr;
5871 if (vdirty == 0)
5872 VOP_FSYNC(vp, cred, 0,0,0);
5873 *ndirty += vdirty;
5874 break;
5875 case 3:
5876 vdirty = vp->v_numoutput;
5877 if (vp->v_tag == VT_UDF)
5878 vdirty += udf_node->outstanding_bufs +
5879 udf_node->outstanding_nodedscr;
5880 *ndirty += vdirty;
5881 break;
5882 }
5883
5884 vput(vp);
5885 mutex_enter(&mntvnode_lock);
5886 }
5887 DPRINTF(SYNC, ("END sync_pass %d\n", pass));
5888 }
5889
5890
5891 void
5892 udf_do_sync(struct udf_mount *ump, kauth_cred_t cred, int waitfor)
5893 {
5894 int dummy, ndirty;
5895
5896 mutex_enter(&mntvnode_lock);
5897 recount:
5898 dummy = 0;
5899 DPRINTF(CALL, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
5900 DPRINTF(SYNC, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
5901 udf_sync_pass(ump, cred, waitfor, 1, &dummy);
5902
5903 DPRINTF(CALL, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
5904 DPRINTF(SYNC, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
5905 udf_sync_pass(ump, cred, waitfor, 2, &dummy);
5906
5907 if (waitfor == MNT_WAIT) {
5908 ndirty = ump->devvp->v_numoutput;
5909 DPRINTF(SYNC, ("counting pending blocks: on devvp %d\n",
5910 ndirty));
5911 udf_sync_pass(ump, cred, waitfor, 3, &ndirty);
5912 DPRINTF(SYNC, ("counted num dirty pending blocks %d\n",
5913 ndirty));
5914
5915 if (ndirty) {
5916 /* 1/4 second wait */
5917 cv_timedwait(&ump->dirtynodes_cv, &mntvnode_lock,
5918 hz/4);
5919 goto recount;
5920 }
5921 }
5922
5923 mutex_exit(&mntvnode_lock);
5924 }
5925
5926 /* --------------------------------------------------------------------- */
5927
5928 /*
5929 * Read and write file extent in/from the buffer.
5930 *
5931 * The splitup of the extent into seperate request-buffers is to minimise
5932 * copying around as much as possible.
5933 *
5934 * block based file reading and writing
5935 */
5936
5937 static int
5938 udf_read_internal(struct udf_node *node, uint8_t *blob)
5939 {
5940 struct udf_mount *ump;
5941 struct file_entry *fe = node->fe;
5942 struct extfile_entry *efe = node->efe;
5943 uint64_t inflen;
5944 uint32_t sector_size;
5945 uint8_t *pos;
5946 int icbflags, addr_type;
5947
5948 /* get extent and do some paranoia checks */
5949 ump = node->ump;
5950 sector_size = ump->discinfo.sector_size;
5951
5952 if (fe) {
5953 inflen = udf_rw64(fe->inf_len);
5954 pos = &fe->data[0] + udf_rw32(fe->l_ea);
5955 icbflags = udf_rw16(fe->icbtag.flags);
5956 } else {
5957 assert(node->efe);
5958 inflen = udf_rw64(efe->inf_len);
5959 pos = &efe->data[0] + udf_rw32(efe->l_ea);
5960 icbflags = udf_rw16(efe->icbtag.flags);
5961 }
5962 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
5963
5964 assert(addr_type == UDF_ICB_INTERN_ALLOC);
5965 assert(inflen < sector_size);
5966
5967 /* copy out info */
5968 memset(blob, 0, sector_size);
5969 memcpy(blob, pos, inflen);
5970
5971 return 0;
5972 }
5973
5974
5975 static int
5976 udf_write_internal(struct udf_node *node, uint8_t *blob)
5977 {
5978 struct udf_mount *ump;
5979 struct file_entry *fe = node->fe;
5980 struct extfile_entry *efe = node->efe;
5981 uint64_t inflen;
5982 uint32_t sector_size;
5983 uint8_t *pos;
5984 int icbflags, addr_type;
5985
5986 /* get extent and do some paranoia checks */
5987 ump = node->ump;
5988 sector_size = ump->discinfo.sector_size;
5989
5990 if (fe) {
5991 inflen = udf_rw64(fe->inf_len);
5992 pos = &fe->data[0] + udf_rw32(fe->l_ea);
5993 icbflags = udf_rw16(fe->icbtag.flags);
5994 } else {
5995 assert(node->efe);
5996 inflen = udf_rw64(efe->inf_len);
5997 pos = &efe->data[0] + udf_rw32(efe->l_ea);
5998 icbflags = udf_rw16(efe->icbtag.flags);
5999 }
6000 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
6001
6002 assert(addr_type == UDF_ICB_INTERN_ALLOC);
6003 assert(inflen < sector_size);
6004
6005 /* copy in blob */
6006 /* memset(pos, 0, inflen); */
6007 memcpy(pos, blob, inflen);
6008
6009 return 0;
6010 }
6011
6012
6013 void
6014 udf_read_filebuf(struct udf_node *udf_node, struct buf *buf)
6015 {
6016 struct buf *nestbuf;
6017 struct udf_mount *ump = udf_node->ump;
6018 uint64_t *mapping;
6019 uint64_t run_start;
6020 uint32_t sector_size;
6021 uint32_t buf_offset, sector, rbuflen, rblk;
6022 uint32_t from, lblkno;
6023 uint32_t sectors;
6024 uint8_t *buf_pos;
6025 int error, run_length, isdir, what;
6026
6027 sector_size = udf_node->ump->discinfo.sector_size;
6028
6029 from = buf->b_blkno;
6030 sectors = buf->b_bcount / sector_size;
6031
6032 isdir = (udf_node->vnode->v_type == VDIR);
6033 what = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
6034
6035 /* assure we have enough translation slots */
6036 KASSERT(buf->b_bcount / sector_size <= UDF_MAX_MAPPINGS);
6037 KASSERT(MAXPHYS / sector_size <= UDF_MAX_MAPPINGS);
6038
6039 if (sectors > UDF_MAX_MAPPINGS) {
6040 printf("udf_read_filebuf: implementation limit on bufsize\n");
6041 buf->b_error = EIO;
6042 biodone(buf);
6043 return;
6044 }
6045
6046 mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
6047
6048 error = 0;
6049 DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
6050 error = udf_translate_file_extent(udf_node, from, sectors, mapping);
6051 if (error) {
6052 buf->b_error = error;
6053 biodone(buf);
6054 goto out;
6055 }
6056 DPRINTF(READ, ("\ttranslate extent went OK\n"));
6057
6058 /* pre-check if its an internal */
6059 if (*mapping == UDF_TRANS_INTERN) {
6060 error = udf_read_internal(udf_node, (uint8_t *) buf->b_data);
6061 if (error)
6062 buf->b_error = error;
6063 biodone(buf);
6064 goto out;
6065 }
6066 DPRINTF(READ, ("\tnot intern\n"));
6067
6068 #ifdef DEBUG
6069 if (udf_verbose & UDF_DEBUG_TRANSLATE) {
6070 printf("Returned translation table:\n");
6071 for (sector = 0; sector < sectors; sector++) {
6072 printf("%d : %"PRIu64"\n", sector, mapping[sector]);
6073 }
6074 }
6075 #endif
6076
6077 /* request read-in of data from disc sheduler */
6078 buf->b_resid = buf->b_bcount;
6079 for (sector = 0; sector < sectors; sector++) {
6080 buf_offset = sector * sector_size;
6081 buf_pos = (uint8_t *) buf->b_data + buf_offset;
6082 DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
6083
6084 /* check if its zero or unmapped to stop reading */
6085 switch (mapping[sector]) {
6086 case UDF_TRANS_UNMAPPED:
6087 case UDF_TRANS_ZERO:
6088 /* copy zero sector TODO runlength like below */
6089 memset(buf_pos, 0, sector_size);
6090 DPRINTF(READ, ("\treturning zero sector\n"));
6091 nestiobuf_done(buf, sector_size, 0);
6092 break;
6093 default :
6094 DPRINTF(READ, ("\tread sector "
6095 "%"PRIu64"\n", mapping[sector]));
6096
6097 lblkno = from + sector;
6098 run_start = mapping[sector];
6099 run_length = 1;
6100 while (sector < sectors-1) {
6101 if (mapping[sector+1] != mapping[sector]+1)
6102 break;
6103 run_length++;
6104 sector++;
6105 }
6106
6107 /*
6108 * nest an iobuf and mark it for async reading. Since
6109 * we're using nested buffers, they can't be cached by
6110 * design.
6111 */
6112 rbuflen = run_length * sector_size;
6113 rblk = run_start * (sector_size/DEV_BSIZE);
6114
6115 nestbuf = getiobuf(NULL, true);
6116 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
6117 /* nestbuf is B_ASYNC */
6118
6119 /* identify this nestbuf */
6120 nestbuf->b_lblkno = lblkno;
6121 assert(nestbuf->b_vp == udf_node->vnode);
6122
6123 /* CD shedules on raw blkno */
6124 nestbuf->b_blkno = rblk;
6125 nestbuf->b_proc = NULL;
6126 nestbuf->b_rawblkno = rblk;
6127 nestbuf->b_udf_c_type = what;
6128
6129 udf_discstrat_queuebuf(ump, nestbuf);
6130 }
6131 }
6132 out:
6133 /* if we're synchronously reading, wait for the completion */
6134 if ((buf->b_flags & B_ASYNC) == 0)
6135 biowait(buf);
6136
6137 DPRINTF(READ, ("\tend of read_filebuf\n"));
6138 free(mapping, M_TEMP);
6139 return;
6140 }
6141
6142
6143 void
6144 udf_write_filebuf(struct udf_node *udf_node, struct buf *buf)
6145 {
6146 struct buf *nestbuf;
6147 struct udf_mount *ump = udf_node->ump;
6148 uint64_t *mapping;
6149 uint64_t run_start;
6150 uint32_t lb_size;
6151 uint32_t buf_offset, lb_num, rbuflen, rblk;
6152 uint32_t from, lblkno;
6153 uint32_t num_lb;
6154 uint8_t *buf_pos;
6155 int error, run_length, isdir, what, s;
6156
6157 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
6158
6159 from = buf->b_blkno;
6160 num_lb = buf->b_bcount / lb_size;
6161
6162 isdir = (udf_node->vnode->v_type == VDIR);
6163 what = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
6164
6165 if (udf_node == ump->metadatabitmap_node)
6166 what = UDF_C_METADATA_SBM;
6167
6168 /* assure we have enough translation slots */
6169 KASSERT(buf->b_bcount / lb_size <= UDF_MAX_MAPPINGS);
6170 KASSERT(MAXPHYS / lb_size <= UDF_MAX_MAPPINGS);
6171
6172 if (num_lb > UDF_MAX_MAPPINGS) {
6173 printf("udf_write_filebuf: implementation limit on bufsize\n");
6174 buf->b_error = EIO;
6175 biodone(buf);
6176 return;
6177 }
6178
6179 mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
6180
6181 error = 0;
6182 DPRINTF(WRITE, ("\ttranslate %d-%d\n", from, num_lb));
6183 error = udf_translate_file_extent(udf_node, from, num_lb, mapping);
6184 if (error) {
6185 buf->b_error = error;
6186 biodone(buf);
6187 goto out;
6188 }
6189 DPRINTF(WRITE, ("\ttranslate extent went OK\n"));
6190
6191 /* if its internally mapped, we can write it in the descriptor itself */
6192 if (*mapping == UDF_TRANS_INTERN) {
6193 /* TODO paranoia check if we ARE going to have enough space */
6194 error = udf_write_internal(udf_node, (uint8_t *) buf->b_data);
6195 if (error)
6196 buf->b_error = error;
6197 biodone(buf);
6198 goto out;
6199 }
6200 DPRINTF(WRITE, ("\tnot intern\n"));
6201
6202 /* request write out of data to disc sheduler */
6203 buf->b_resid = buf->b_bcount;
6204 for (lb_num = 0; lb_num < num_lb; lb_num++) {
6205 buf_offset = lb_num * lb_size;
6206 buf_pos = (uint8_t *) buf->b_data + buf_offset;
6207 DPRINTF(WRITE, ("\tprocessing rel lb_num %d\n", lb_num));
6208
6209 /*
6210 * Mappings are not that important here. Just before we write
6211 * the lb_num we late-allocate them when needed and update the
6212 * mapping in the udf_node.
6213 */
6214
6215 /* XXX why not ignore the mapping altogether ? */
6216 /* TODO estimate here how much will be late-allocated */
6217 DPRINTF(WRITE, ("\twrite lb_num "
6218 "%"PRIu64, mapping[lb_num]));
6219
6220 lblkno = from + lb_num;
6221 run_start = mapping[lb_num];
6222 run_length = 1;
6223 while (lb_num < num_lb-1) {
6224 if (mapping[lb_num+1] != mapping[lb_num]+1)
6225 if (mapping[lb_num+1] != mapping[lb_num])
6226 break;
6227 run_length++;
6228 lb_num++;
6229 }
6230 DPRINTF(WRITE, ("+ %d\n", run_length));
6231
6232 /* nest an iobuf on the master buffer for the extent */
6233 rbuflen = run_length * lb_size;
6234 rblk = run_start * (lb_size/DEV_BSIZE);
6235
6236 #if 0
6237 /* if its zero or unmapped, our blknr gets -1 for unmapped */
6238 switch (mapping[lb_num]) {
6239 case UDF_TRANS_UNMAPPED:
6240 case UDF_TRANS_ZERO:
6241 rblk = -1;
6242 break;
6243 default:
6244 rblk = run_start * (lb_size/DEV_BSIZE);
6245 break;
6246 }
6247 #endif
6248
6249 nestbuf = getiobuf(NULL, true);
6250 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
6251 /* nestbuf is B_ASYNC */
6252
6253 /* identify this nestbuf */
6254 nestbuf->b_lblkno = lblkno;
6255 KASSERT(nestbuf->b_vp == udf_node->vnode);
6256
6257 /* CD shedules on raw blkno */
6258 nestbuf->b_blkno = rblk;
6259 nestbuf->b_proc = NULL;
6260 nestbuf->b_rawblkno = rblk;
6261 nestbuf->b_udf_c_type = what;
6262
6263 /* increment our outstanding bufs counter */
6264 s = splbio();
6265 udf_node->outstanding_bufs++;
6266 splx(s);
6267
6268 udf_discstrat_queuebuf(ump, nestbuf);
6269 }
6270 out:
6271 /* if we're synchronously writing, wait for the completion */
6272 if ((buf->b_flags & B_ASYNC) == 0)
6273 biowait(buf);
6274
6275 DPRINTF(WRITE, ("\tend of write_filebuf\n"));
6276 free(mapping, M_TEMP);
6277 return;
6278 }
6279
6280 /* --------------------------------------------------------------------- */
6281
6282
6283