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