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