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