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