udf_subr.c revision 1.73.4.1 1 /* $NetBSD: udf_subr.c,v 1.73.4.1 2008/12/06 21:36:48 snj 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.73.4.1 2008/12/06 21:36:48 snj 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 /* print results */
1995 DPRINTF(VOLUMES, ("\tdata partition %d\n", ump->data_part));
1996 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->data_part]));
1997 DPRINTF(VOLUMES, ("\tnode partition %d\n", ump->node_part));
1998 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->node_part]));
1999 DPRINTF(VOLUMES, ("\tfids partition %d\n", ump->fids_part));
2000 DPRINTF(VOLUMES, ("\t\talloc scheme %d\n", ump->vtop_alloc[ump->fids_part]));
2001
2002 bitmask_snprintf(ump->lvopen, UDFLOGVOL_BITS, bits, sizeof(bits));
2003 DPRINTF(VOLUMES, ("\tactions on logvol open %s\n", bits));
2004 bitmask_snprintf(ump->lvclose, UDFLOGVOL_BITS, bits, sizeof(bits));
2005 DPRINTF(VOLUMES, ("\tactions on logvol close %s\n", bits));
2006 bitmask_snprintf(ump->lvreadwrite, UDFONERROR_BITS, bits, sizeof(bits));
2007 DPRINTF(VOLUMES, ("\tactions on logvol errors %s\n", bits));
2008
2009 DPRINTF(VOLUMES, ("\tselected sheduler `%s`\n",
2010 (ump->strategy == &udf_strat_direct) ? "Direct" :
2011 (ump->strategy == &udf_strat_sequential) ? "Sequential" :
2012 (ump->strategy == &udf_strat_rmw) ? "RMW" : "UNKNOWN!"));
2013
2014 /* signal its OK for now */
2015 return 0;
2016 }
2017
2018 /* --------------------------------------------------------------------- */
2019
2020 /*
2021 * Update logical volume name in all structures that keep a record of it. We
2022 * use memmove since each of them might be specified as a source.
2023 *
2024 * Note that it doesn't update the VAT structure!
2025 */
2026
2027 static void
2028 udf_update_logvolname(struct udf_mount *ump, char *logvol_id)
2029 {
2030 struct logvol_desc *lvd = NULL;
2031 struct fileset_desc *fsd = NULL;
2032 struct udf_lv_info *lvi = NULL;
2033
2034 DPRINTF(VOLUMES, ("Updating logical volume name\n"));
2035 lvd = ump->logical_vol;
2036 fsd = ump->fileset_desc;
2037 if (ump->implementation)
2038 lvi = &ump->implementation->_impl_use.lv_info;
2039
2040 /* logvol's id might be specified as origional so use memmove here */
2041 memmove(lvd->logvol_id, logvol_id, 128);
2042 if (fsd)
2043 memmove(fsd->logvol_id, logvol_id, 128);
2044 if (lvi)
2045 memmove(lvi->logvol_id, logvol_id, 128);
2046 }
2047
2048 /* --------------------------------------------------------------------- */
2049
2050 void
2051 udf_inittag(struct udf_mount *ump, struct desc_tag *tag, int tagid,
2052 uint32_t sector)
2053 {
2054 assert(ump->logical_vol);
2055
2056 tag->id = udf_rw16(tagid);
2057 tag->descriptor_ver = ump->logical_vol->tag.descriptor_ver;
2058 tag->cksum = 0;
2059 tag->reserved = 0;
2060 tag->serial_num = ump->logical_vol->tag.serial_num;
2061 tag->tag_loc = udf_rw32(sector);
2062 }
2063
2064
2065 uint64_t
2066 udf_advance_uniqueid(struct udf_mount *ump)
2067 {
2068 uint64_t unique_id;
2069
2070 mutex_enter(&ump->logvol_mutex);
2071 unique_id = udf_rw64(ump->logvol_integrity->lvint_next_unique_id);
2072 if (unique_id < 0x10)
2073 unique_id = 0x10;
2074 ump->logvol_integrity->lvint_next_unique_id = udf_rw64(unique_id + 1);
2075 mutex_exit(&ump->logvol_mutex);
2076
2077 return unique_id;
2078 }
2079
2080
2081 static void
2082 udf_adjust_filecount(struct udf_node *udf_node, int sign)
2083 {
2084 struct udf_mount *ump = udf_node->ump;
2085 uint32_t num_dirs, num_files;
2086 int udf_file_type;
2087
2088 /* get file type */
2089 if (udf_node->fe) {
2090 udf_file_type = udf_node->fe->icbtag.file_type;
2091 } else {
2092 udf_file_type = udf_node->efe->icbtag.file_type;
2093 }
2094
2095 /* adjust file count */
2096 mutex_enter(&ump->allocate_mutex);
2097 if (udf_file_type == UDF_ICB_FILETYPE_DIRECTORY) {
2098 num_dirs = udf_rw32(ump->logvol_info->num_directories);
2099 ump->logvol_info->num_directories =
2100 udf_rw32((num_dirs + sign));
2101 } else {
2102 num_files = udf_rw32(ump->logvol_info->num_files);
2103 ump->logvol_info->num_files =
2104 udf_rw32((num_files + sign));
2105 }
2106 mutex_exit(&ump->allocate_mutex);
2107 }
2108
2109
2110 void
2111 udf_osta_charset(struct charspec *charspec)
2112 {
2113 bzero(charspec, sizeof(struct charspec));
2114 charspec->type = 0;
2115 strcpy((char *) charspec->inf, "OSTA Compressed Unicode");
2116 }
2117
2118
2119 /* first call udf_set_regid and then the suffix */
2120 void
2121 udf_set_regid(struct regid *regid, char const *name)
2122 {
2123 bzero(regid, sizeof(struct regid));
2124 regid->flags = 0; /* not dirty and not protected */
2125 strcpy((char *) regid->id, name);
2126 }
2127
2128
2129 void
2130 udf_add_domain_regid(struct udf_mount *ump, struct regid *regid)
2131 {
2132 uint16_t *ver;
2133
2134 ver = (uint16_t *) regid->id_suffix;
2135 *ver = ump->logvol_info->min_udf_readver;
2136 }
2137
2138
2139 void
2140 udf_add_udf_regid(struct udf_mount *ump, struct regid *regid)
2141 {
2142 uint16_t *ver;
2143
2144 ver = (uint16_t *) regid->id_suffix;
2145 *ver = ump->logvol_info->min_udf_readver;
2146
2147 regid->id_suffix[2] = 4; /* unix */
2148 regid->id_suffix[3] = 8; /* NetBSD */
2149 }
2150
2151
2152 void
2153 udf_add_impl_regid(struct udf_mount *ump, struct regid *regid)
2154 {
2155 regid->id_suffix[0] = 4; /* unix */
2156 regid->id_suffix[1] = 8; /* NetBSD */
2157 }
2158
2159
2160 void
2161 udf_add_app_regid(struct udf_mount *ump, struct regid *regid)
2162 {
2163 regid->id_suffix[0] = APP_VERSION_MAIN;
2164 regid->id_suffix[1] = APP_VERSION_SUB;
2165 }
2166
2167 static int
2168 udf_create_parentfid(struct udf_mount *ump, struct fileid_desc *fid,
2169 struct long_ad *parent, uint64_t unique_id)
2170 {
2171 /* the size of an empty FID is 38 but needs to be a multiple of 4 */
2172 int fidsize = 40;
2173
2174 udf_inittag(ump, &fid->tag, TAGID_FID, udf_rw32(parent->loc.lb_num));
2175 fid->file_version_num = udf_rw16(1); /* UDF 2.3.4.1 */
2176 fid->file_char = UDF_FILE_CHAR_DIR | UDF_FILE_CHAR_PAR;
2177 fid->icb = *parent;
2178 fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
2179 fid->tag.desc_crc_len = fidsize - UDF_DESC_TAG_LENGTH;
2180 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
2181
2182 return fidsize;
2183 }
2184
2185 /* --------------------------------------------------------------------- */
2186
2187 /*
2188 * Extended attribute support. UDF knows of 3 places for extended attributes:
2189 *
2190 * (a) inside the file's (e)fe in the length of the extended attribute area
2191 * before the allocation descriptors/filedata
2192 *
2193 * (b) in a file referenced by (e)fe->ext_attr_icb and
2194 *
2195 * (c) in the e(fe)'s associated stream directory that can hold various
2196 * sub-files. In the stream directory a few fixed named subfiles are reserved
2197 * for NT/Unix ACL's and OS/2 attributes.
2198 *
2199 * NOTE: Extended attributes are read randomly but allways written
2200 * *atomicaly*. For ACL's this interface is propably different but not known
2201 * to me yet.
2202 *
2203 * Order of extended attributes in a space :
2204 * ECMA 167 EAs
2205 * Non block aligned Implementation Use EAs
2206 * Block aligned Implementation Use EAs
2207 * Application Use EAs
2208 */
2209
2210 static int
2211 udf_impl_extattr_check(struct impl_extattr_entry *implext)
2212 {
2213 uint16_t *spos;
2214
2215 if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) {
2216 /* checksum valid? */
2217 DPRINTF(EXTATTR, ("checking UDF impl. attr checksum\n"));
2218 spos = (uint16_t *) implext->data;
2219 if (udf_rw16(*spos) != udf_ea_cksum((uint8_t *) implext))
2220 return EINVAL;
2221 }
2222 return 0;
2223 }
2224
2225 static void
2226 udf_calc_impl_extattr_checksum(struct impl_extattr_entry *implext)
2227 {
2228 uint16_t *spos;
2229
2230 if (strncmp(implext->imp_id.id, "*UDF", 4) == 0) {
2231 /* set checksum */
2232 spos = (uint16_t *) implext->data;
2233 *spos = udf_rw16(udf_ea_cksum((uint8_t *) implext));
2234 }
2235 }
2236
2237
2238 int
2239 udf_extattr_search_intern(struct udf_node *node,
2240 uint32_t sattr, char const *sattrname,
2241 uint32_t *offsetp, uint32_t *lengthp)
2242 {
2243 struct extattrhdr_desc *eahdr;
2244 struct extattr_entry *attrhdr;
2245 struct impl_extattr_entry *implext;
2246 uint32_t offset, a_l, sector_size;
2247 int32_t l_ea;
2248 uint8_t *pos;
2249 int error;
2250
2251 /* get mountpoint */
2252 sector_size = node->ump->discinfo.sector_size;
2253
2254 /* get information from fe/efe */
2255 if (node->fe) {
2256 l_ea = udf_rw32(node->fe->l_ea);
2257 eahdr = (struct extattrhdr_desc *) node->fe->data;
2258 } else {
2259 assert(node->efe);
2260 l_ea = udf_rw32(node->efe->l_ea);
2261 eahdr = (struct extattrhdr_desc *) node->efe->data;
2262 }
2263
2264 /* something recorded here? */
2265 if (l_ea == 0)
2266 return ENOENT;
2267
2268 /* check extended attribute tag; what to do if it fails? */
2269 error = udf_check_tag(eahdr);
2270 if (error)
2271 return EINVAL;
2272 if (udf_rw16(eahdr->tag.id) != TAGID_EXTATTR_HDR)
2273 return EINVAL;
2274 error = udf_check_tag_payload(eahdr, sizeof(struct extattrhdr_desc));
2275 if (error)
2276 return EINVAL;
2277
2278 DPRINTF(EXTATTR, ("Found %d bytes of extended attributes\n", l_ea));
2279
2280 /* looking for Ecma-167 attributes? */
2281 offset = sizeof(struct extattrhdr_desc);
2282
2283 /* looking for either implemenation use or application use */
2284 if (sattr == 2048) { /* [4/48.10.8] */
2285 offset = udf_rw32(eahdr->impl_attr_loc);
2286 if (offset == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
2287 return ENOENT;
2288 }
2289 if (sattr == 65536) { /* [4/48.10.9] */
2290 offset = udf_rw32(eahdr->appl_attr_loc);
2291 if (offset == UDF_APPL_ATTR_LOC_NOT_PRESENT)
2292 return ENOENT;
2293 }
2294
2295 /* paranoia check offset and l_ea */
2296 if (l_ea + offset >= sector_size - sizeof(struct extattr_entry))
2297 return EINVAL;
2298
2299 DPRINTF(EXTATTR, ("Starting at offset %d\n", offset));
2300
2301 /* find our extended attribute */
2302 l_ea -= offset;
2303 pos = (uint8_t *) eahdr + offset;
2304
2305 while (l_ea >= sizeof(struct extattr_entry)) {
2306 DPRINTF(EXTATTR, ("%d extended attr bytes left\n", l_ea));
2307 attrhdr = (struct extattr_entry *) pos;
2308 implext = (struct impl_extattr_entry *) pos;
2309
2310 /* get complete attribute length and check for roque values */
2311 a_l = udf_rw32(attrhdr->a_l);
2312 DPRINTF(EXTATTR, ("attribute %d:%d, len %d/%d\n",
2313 udf_rw32(attrhdr->type),
2314 attrhdr->subtype, a_l, l_ea));
2315 if ((a_l == 0) || (a_l > l_ea))
2316 return EINVAL;
2317
2318 if (attrhdr->type != sattr)
2319 goto next_attribute;
2320
2321 /* we might have found it! */
2322 if (attrhdr->type < 2048) { /* Ecma-167 attribute */
2323 *offsetp = offset;
2324 *lengthp = a_l;
2325 return 0; /* success */
2326 }
2327
2328 /*
2329 * Implementation use and application use extended attributes
2330 * have a name to identify. They share the same structure only
2331 * UDF implementation use extended attributes have a checksum
2332 * we need to check
2333 */
2334
2335 DPRINTF(EXTATTR, ("named attribute %s\n", implext->imp_id.id));
2336 if (strcmp(implext->imp_id.id, sattrname) == 0) {
2337 /* we have found our appl/implementation attribute */
2338 *offsetp = offset;
2339 *lengthp = a_l;
2340 return 0; /* success */
2341 }
2342
2343 next_attribute:
2344 /* next attribute */
2345 pos += a_l;
2346 l_ea -= a_l;
2347 offset += a_l;
2348 }
2349 /* not found */
2350 return ENOENT;
2351 }
2352
2353
2354 static void
2355 udf_extattr_insert_internal(struct udf_mount *ump, union dscrptr *dscr,
2356 struct extattr_entry *extattr)
2357 {
2358 struct file_entry *fe;
2359 struct extfile_entry *efe;
2360 struct extattrhdr_desc *extattrhdr;
2361 struct impl_extattr_entry *implext;
2362 uint32_t impl_attr_loc, appl_attr_loc, l_ea, a_l, exthdr_len;
2363 uint32_t *l_eap, l_ad;
2364 uint16_t *spos;
2365 uint8_t *bpos, *data;
2366
2367 if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
2368 fe = &dscr->fe;
2369 data = fe->data;
2370 l_eap = &fe->l_ea;
2371 l_ad = udf_rw32(fe->l_ad);
2372 } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
2373 efe = &dscr->efe;
2374 data = efe->data;
2375 l_eap = &efe->l_ea;
2376 l_ad = udf_rw32(efe->l_ad);
2377 } else {
2378 panic("Bad tag passed to udf_extattr_insert_internal");
2379 }
2380
2381 /* can't append already written to file descriptors yet */
2382 assert(l_ad == 0);
2383
2384 /* should have a header! */
2385 extattrhdr = (struct extattrhdr_desc *) data;
2386 l_ea = udf_rw32(*l_eap);
2387 if (l_ea == 0) {
2388 /* create empty extended attribute header */
2389 exthdr_len = sizeof(struct extattrhdr_desc);
2390
2391 udf_inittag(ump, &extattrhdr->tag, TAGID_EXTATTR_HDR,
2392 /* loc */ 0);
2393 extattrhdr->impl_attr_loc = udf_rw32(exthdr_len);
2394 extattrhdr->appl_attr_loc = udf_rw32(exthdr_len);
2395 extattrhdr->tag.desc_crc_len = udf_rw16(8);
2396
2397 /* record extended attribute header length */
2398 l_ea = exthdr_len;
2399 *l_eap = udf_rw32(l_ea);
2400 }
2401
2402 /* extract locations */
2403 impl_attr_loc = udf_rw32(extattrhdr->impl_attr_loc);
2404 appl_attr_loc = udf_rw32(extattrhdr->appl_attr_loc);
2405 if (impl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
2406 impl_attr_loc = l_ea;
2407 if (appl_attr_loc == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
2408 appl_attr_loc = l_ea;
2409
2410 /* Ecma 167 EAs */
2411 if (udf_rw32(extattr->type) < 2048) {
2412 assert(impl_attr_loc == l_ea);
2413 assert(appl_attr_loc == l_ea);
2414 }
2415
2416 /* implementation use extended attributes */
2417 if (udf_rw32(extattr->type) == 2048) {
2418 assert(appl_attr_loc == l_ea);
2419
2420 /* calculate and write extended attribute header checksum */
2421 implext = (struct impl_extattr_entry *) extattr;
2422 assert(udf_rw32(implext->iu_l) == 4); /* [UDF 3.3.4.5] */
2423 spos = (uint16_t *) implext->data;
2424 *spos = udf_rw16(udf_ea_cksum((uint8_t *) implext));
2425 }
2426
2427 /* application use extended attributes */
2428 assert(udf_rw32(extattr->type) != 65536);
2429 assert(appl_attr_loc == l_ea);
2430
2431 /* append the attribute at the end of the current space */
2432 bpos = data + udf_rw32(*l_eap);
2433 a_l = udf_rw32(extattr->a_l);
2434
2435 /* update impl. attribute locations */
2436 if (udf_rw32(extattr->type) < 2048) {
2437 impl_attr_loc = l_ea + a_l;
2438 appl_attr_loc = l_ea + a_l;
2439 }
2440 if (udf_rw32(extattr->type) == 2048) {
2441 appl_attr_loc = l_ea + a_l;
2442 }
2443
2444 /* copy and advance */
2445 memcpy(bpos, extattr, a_l);
2446 l_ea += a_l;
2447 *l_eap = udf_rw32(l_ea);
2448
2449 /* do the `dance` again backwards */
2450 if (udf_rw16(ump->logical_vol->tag.descriptor_ver) != 2) {
2451 if (impl_attr_loc == l_ea)
2452 impl_attr_loc = UDF_IMPL_ATTR_LOC_NOT_PRESENT;
2453 if (appl_attr_loc == l_ea)
2454 appl_attr_loc = UDF_APPL_ATTR_LOC_NOT_PRESENT;
2455 }
2456
2457 /* store offsets */
2458 extattrhdr->impl_attr_loc = udf_rw32(impl_attr_loc);
2459 extattrhdr->appl_attr_loc = udf_rw32(appl_attr_loc);
2460 }
2461
2462
2463 /* --------------------------------------------------------------------- */
2464
2465 static int
2466 udf_update_lvid_from_vat_extattr(struct udf_node *vat_node)
2467 {
2468 struct udf_mount *ump;
2469 struct udf_logvol_info *lvinfo;
2470 struct impl_extattr_entry *implext;
2471 struct vatlvext_extattr_entry lvext;
2472 const char *extstr = "*UDF VAT LVExtension";
2473 uint64_t vat_uniqueid;
2474 uint32_t offset, a_l;
2475 uint8_t *ea_start, *lvextpos;
2476 int error;
2477
2478 /* get mountpoint and lvinfo */
2479 ump = vat_node->ump;
2480 lvinfo = ump->logvol_info;
2481
2482 /* get information from fe/efe */
2483 if (vat_node->fe) {
2484 vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
2485 ea_start = vat_node->fe->data;
2486 } else {
2487 vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
2488 ea_start = vat_node->efe->data;
2489 }
2490
2491 error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
2492 if (error)
2493 return error;
2494
2495 implext = (struct impl_extattr_entry *) (ea_start + offset);
2496 error = udf_impl_extattr_check(implext);
2497 if (error)
2498 return error;
2499
2500 /* paranoia */
2501 if (a_l != sizeof(*implext) -1 + udf_rw32(implext->iu_l) + sizeof(lvext)) {
2502 DPRINTF(VOLUMES, ("VAT LVExtension size doesn't compute\n"));
2503 return EINVAL;
2504 }
2505
2506 /*
2507 * we have found our "VAT LVExtension attribute. BUT due to a
2508 * bug in the specification it might not be word aligned so
2509 * copy first to avoid panics on some machines (!!)
2510 */
2511 DPRINTF(VOLUMES, ("Found VAT LVExtension attr\n"));
2512 lvextpos = implext->data + udf_rw32(implext->iu_l);
2513 memcpy(&lvext, lvextpos, sizeof(lvext));
2514
2515 /* check if it was updated the last time */
2516 if (udf_rw64(lvext.unique_id_chk) == vat_uniqueid) {
2517 lvinfo->num_files = lvext.num_files;
2518 lvinfo->num_directories = lvext.num_directories;
2519 udf_update_logvolname(ump, lvext.logvol_id);
2520 } else {
2521 DPRINTF(VOLUMES, ("VAT LVExtension out of date\n"));
2522 /* replace VAT LVExt by free space EA */
2523 memset(implext->imp_id.id, 0, UDF_REGID_ID_SIZE);
2524 strcpy(implext->imp_id.id, "*UDF FreeEASpace");
2525 udf_calc_impl_extattr_checksum(implext);
2526 }
2527
2528 return 0;
2529 }
2530
2531
2532 static int
2533 udf_update_vat_extattr_from_lvid(struct udf_node *vat_node)
2534 {
2535 struct udf_mount *ump;
2536 struct udf_logvol_info *lvinfo;
2537 struct impl_extattr_entry *implext;
2538 struct vatlvext_extattr_entry lvext;
2539 const char *extstr = "*UDF VAT LVExtension";
2540 uint64_t vat_uniqueid;
2541 uint32_t offset, a_l;
2542 uint8_t *ea_start, *lvextpos;
2543 int error;
2544
2545 /* get mountpoint and lvinfo */
2546 ump = vat_node->ump;
2547 lvinfo = ump->logvol_info;
2548
2549 /* get information from fe/efe */
2550 if (vat_node->fe) {
2551 vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
2552 ea_start = vat_node->fe->data;
2553 } else {
2554 vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
2555 ea_start = vat_node->efe->data;
2556 }
2557
2558 error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
2559 if (error)
2560 return error;
2561 /* found, it existed */
2562
2563 /* paranoia */
2564 implext = (struct impl_extattr_entry *) (ea_start + offset);
2565 error = udf_impl_extattr_check(implext);
2566 if (error) {
2567 DPRINTF(VOLUMES, ("VAT LVExtension bad on update\n"));
2568 return error;
2569 }
2570 /* it is correct */
2571
2572 /*
2573 * we have found our "VAT LVExtension attribute. BUT due to a
2574 * bug in the specification it might not be word aligned so
2575 * copy first to avoid panics on some machines (!!)
2576 */
2577 DPRINTF(VOLUMES, ("Updating VAT LVExtension attr\n"));
2578 lvextpos = implext->data + udf_rw32(implext->iu_l);
2579
2580 lvext.unique_id_chk = vat_uniqueid;
2581 lvext.num_files = lvinfo->num_files;
2582 lvext.num_directories = lvinfo->num_directories;
2583 memmove(lvext.logvol_id, ump->logical_vol->logvol_id, 128);
2584
2585 memcpy(lvextpos, &lvext, sizeof(lvext));
2586
2587 return 0;
2588 }
2589
2590 /* --------------------------------------------------------------------- */
2591
2592 int
2593 udf_vat_read(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset)
2594 {
2595 struct udf_mount *ump = vat_node->ump;
2596
2597 if (offset + size > ump->vat_offset + ump->vat_entries * 4)
2598 return EINVAL;
2599
2600 memcpy(blob, ump->vat_table + offset, size);
2601 return 0;
2602 }
2603
2604 int
2605 udf_vat_write(struct udf_node *vat_node, uint8_t *blob, int size, uint32_t offset)
2606 {
2607 struct udf_mount *ump = vat_node->ump;
2608 uint32_t offset_high;
2609 uint8_t *new_vat_table;
2610
2611 /* extent VAT allocation if needed */
2612 offset_high = offset + size;
2613 if (offset_high >= ump->vat_table_alloc_len) {
2614 /* realloc */
2615 new_vat_table = realloc(ump->vat_table,
2616 ump->vat_table_alloc_len + UDF_VAT_CHUNKSIZE,
2617 M_UDFVOLD, M_WAITOK | M_CANFAIL);
2618 if (!new_vat_table) {
2619 printf("udf_vat_write: can't extent VAT, out of mem\n");
2620 return ENOMEM;
2621 }
2622 ump->vat_table = new_vat_table;
2623 ump->vat_table_alloc_len += UDF_VAT_CHUNKSIZE;
2624 }
2625 ump->vat_table_len = MAX(ump->vat_table_len, offset_high);
2626
2627 memcpy(ump->vat_table + offset, blob, size);
2628 return 0;
2629 }
2630
2631 /* --------------------------------------------------------------------- */
2632
2633 /* TODO support previous VAT location writeout */
2634 static int
2635 udf_update_vat_descriptor(struct udf_mount *ump)
2636 {
2637 struct udf_node *vat_node = ump->vat_node;
2638 struct udf_logvol_info *lvinfo = ump->logvol_info;
2639 struct icb_tag *icbtag;
2640 struct udf_oldvat_tail *oldvat_tl;
2641 struct udf_vat *vat;
2642 uint64_t unique_id;
2643 uint32_t lb_size;
2644 uint8_t *raw_vat;
2645 int filetype, error;
2646
2647 KASSERT(vat_node);
2648 KASSERT(lvinfo);
2649 lb_size = udf_rw32(ump->logical_vol->lb_size);
2650
2651 /* get our new unique_id */
2652 unique_id = udf_advance_uniqueid(ump);
2653
2654 /* get information from fe/efe */
2655 if (vat_node->fe) {
2656 icbtag = &vat_node->fe->icbtag;
2657 vat_node->fe->unique_id = udf_rw64(unique_id);
2658 } else {
2659 icbtag = &vat_node->efe->icbtag;
2660 vat_node->efe->unique_id = udf_rw64(unique_id);
2661 }
2662
2663 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
2664 filetype = icbtag->file_type;
2665 KASSERT((filetype == 0) || (filetype == UDF_ICB_FILETYPE_VAT));
2666
2667 /* allocate piece to process head or tail of VAT file */
2668 raw_vat = malloc(lb_size, M_TEMP, M_WAITOK);
2669
2670 if (filetype == 0) {
2671 /*
2672 * Update "*UDF VAT LVExtension" extended attribute from the
2673 * lvint if present.
2674 */
2675 udf_update_vat_extattr_from_lvid(vat_node);
2676
2677 /* setup identifying regid */
2678 oldvat_tl = (struct udf_oldvat_tail *) raw_vat;
2679 memset(oldvat_tl, 0, sizeof(struct udf_oldvat_tail));
2680
2681 udf_set_regid(&oldvat_tl->id, "*UDF Virtual Alloc Tbl");
2682 udf_add_udf_regid(ump, &oldvat_tl->id);
2683 oldvat_tl->prev_vat = udf_rw32(0xffffffff);
2684
2685 /* write out new tail of virtual allocation table file */
2686 error = udf_vat_write(vat_node, raw_vat,
2687 sizeof(struct udf_oldvat_tail), ump->vat_entries * 4);
2688 } else {
2689 /* compose the VAT2 header */
2690 vat = (struct udf_vat *) raw_vat;
2691 memset(vat, 0, sizeof(struct udf_vat));
2692
2693 vat->header_len = udf_rw16(152); /* as per spec */
2694 vat->impl_use_len = udf_rw16(0);
2695 memmove(vat->logvol_id, ump->logical_vol->logvol_id, 128);
2696 vat->prev_vat = udf_rw32(0xffffffff);
2697 vat->num_files = lvinfo->num_files;
2698 vat->num_directories = lvinfo->num_directories;
2699 vat->min_udf_readver = lvinfo->min_udf_readver;
2700 vat->min_udf_writever = lvinfo->min_udf_writever;
2701 vat->max_udf_writever = lvinfo->max_udf_writever;
2702
2703 error = udf_vat_write(vat_node, raw_vat,
2704 sizeof(struct udf_vat), 0);
2705 }
2706 free(raw_vat, M_TEMP);
2707
2708 return error; /* success! */
2709 }
2710
2711
2712 int
2713 udf_writeout_vat(struct udf_mount *ump)
2714 {
2715 struct udf_node *vat_node = ump->vat_node;
2716 uint32_t vat_length;
2717 int error;
2718
2719 KASSERT(vat_node);
2720
2721 DPRINTF(CALL, ("udf_writeout_vat\n"));
2722
2723 mutex_enter(&ump->allocate_mutex);
2724 udf_update_vat_descriptor(ump);
2725
2726 /* write out the VAT contents ; TODO intelligent writing */
2727 vat_length = ump->vat_table_len;
2728 error = vn_rdwr(UIO_WRITE, vat_node->vnode,
2729 ump->vat_table, ump->vat_table_len, 0,
2730 UIO_SYSSPACE, IO_NODELOCKED, FSCRED, NULL, NULL);
2731 if (error) {
2732 printf("udf_writeout_vat: failed to write out VAT contents\n");
2733 goto out;
2734 }
2735
2736 mutex_exit(&ump->allocate_mutex);
2737
2738 vflushbuf(ump->vat_node->vnode, 1 /* sync */);
2739 error = VOP_FSYNC(ump->vat_node->vnode,
2740 FSCRED, FSYNC_WAIT, 0, 0);
2741 if (error)
2742 printf("udf_writeout_vat: error writing VAT node!\n");
2743 out:
2744
2745 return error;
2746 }
2747
2748 /* --------------------------------------------------------------------- */
2749
2750 /*
2751 * Read in relevant pieces of VAT file and check if its indeed a VAT file
2752 * descriptor. If OK, read in complete VAT file.
2753 */
2754
2755 static int
2756 udf_check_for_vat(struct udf_node *vat_node)
2757 {
2758 struct udf_mount *ump;
2759 struct icb_tag *icbtag;
2760 struct timestamp *mtime;
2761 struct udf_vat *vat;
2762 struct udf_oldvat_tail *oldvat_tl;
2763 struct udf_logvol_info *lvinfo;
2764 uint64_t unique_id;
2765 uint32_t vat_length;
2766 uint32_t vat_offset, vat_entries, vat_table_alloc_len;
2767 uint32_t sector_size;
2768 uint32_t *raw_vat;
2769 uint8_t *vat_table;
2770 char *regid_name;
2771 int filetype;
2772 int error;
2773
2774 /* vat_length is really 64 bits though impossible */
2775
2776 DPRINTF(VOLUMES, ("Checking for VAT\n"));
2777 if (!vat_node)
2778 return ENOENT;
2779
2780 /* get mount info */
2781 ump = vat_node->ump;
2782 sector_size = udf_rw32(ump->logical_vol->lb_size);
2783
2784 /* check assertions */
2785 assert(vat_node->fe || vat_node->efe);
2786 assert(ump->logvol_integrity);
2787
2788 /* set vnode type to regular file or we can't read from it! */
2789 vat_node->vnode->v_type = VREG;
2790
2791 /* get information from fe/efe */
2792 if (vat_node->fe) {
2793 vat_length = udf_rw64(vat_node->fe->inf_len);
2794 icbtag = &vat_node->fe->icbtag;
2795 mtime = &vat_node->fe->mtime;
2796 unique_id = udf_rw64(vat_node->fe->unique_id);
2797 } else {
2798 vat_length = udf_rw64(vat_node->efe->inf_len);
2799 icbtag = &vat_node->efe->icbtag;
2800 mtime = &vat_node->efe->mtime;
2801 unique_id = udf_rw64(vat_node->efe->unique_id);
2802 }
2803
2804 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
2805 filetype = icbtag->file_type;
2806 if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT))
2807 return ENOENT;
2808
2809 DPRINTF(VOLUMES, ("\tPossible VAT length %d\n", vat_length));
2810
2811 vat_table_alloc_len =
2812 ((vat_length + UDF_VAT_CHUNKSIZE-1) / UDF_VAT_CHUNKSIZE)
2813 * UDF_VAT_CHUNKSIZE;
2814
2815 vat_table = malloc(vat_table_alloc_len, M_UDFVOLD,
2816 M_CANFAIL | M_WAITOK);
2817 if (vat_table == NULL) {
2818 printf("allocation of %d bytes failed for VAT\n",
2819 vat_table_alloc_len);
2820 return ENOMEM;
2821 }
2822
2823 /* allocate piece to read in head or tail of VAT file */
2824 raw_vat = malloc(sector_size, M_TEMP, M_WAITOK);
2825
2826 /*
2827 * check contents of the file if its the old 1.50 VAT table format.
2828 * Its notoriously broken and allthough some implementations support an
2829 * extention as defined in the UDF 1.50 errata document, its doubtfull
2830 * to be useable since a lot of implementations don't maintain it.
2831 */
2832 lvinfo = ump->logvol_info;
2833
2834 if (filetype == 0) {
2835 /* definition */
2836 vat_offset = 0;
2837 vat_entries = (vat_length-36)/4;
2838
2839 /* read in tail of virtual allocation table file */
2840 error = vn_rdwr(UIO_READ, vat_node->vnode,
2841 (uint8_t *) raw_vat,
2842 sizeof(struct udf_oldvat_tail),
2843 vat_entries * 4,
2844 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
2845 NULL, NULL);
2846 if (error)
2847 goto out;
2848
2849 /* check 1.50 VAT */
2850 oldvat_tl = (struct udf_oldvat_tail *) raw_vat;
2851 regid_name = (char *) oldvat_tl->id.id;
2852 error = strncmp(regid_name, "*UDF Virtual Alloc Tbl", 22);
2853 if (error) {
2854 DPRINTF(VOLUMES, ("VAT format 1.50 rejected\n"));
2855 error = ENOENT;
2856 goto out;
2857 }
2858
2859 /*
2860 * update LVID from "*UDF VAT LVExtension" extended attribute
2861 * if present.
2862 */
2863 udf_update_lvid_from_vat_extattr(vat_node);
2864 } else {
2865 /* read in head of virtual allocation table file */
2866 error = vn_rdwr(UIO_READ, vat_node->vnode,
2867 (uint8_t *) raw_vat,
2868 sizeof(struct udf_vat), 0,
2869 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
2870 NULL, NULL);
2871 if (error)
2872 goto out;
2873
2874 /* definition */
2875 vat = (struct udf_vat *) raw_vat;
2876 vat_offset = vat->header_len;
2877 vat_entries = (vat_length - vat_offset)/4;
2878
2879 assert(lvinfo);
2880 lvinfo->num_files = vat->num_files;
2881 lvinfo->num_directories = vat->num_directories;
2882 lvinfo->min_udf_readver = vat->min_udf_readver;
2883 lvinfo->min_udf_writever = vat->min_udf_writever;
2884 lvinfo->max_udf_writever = vat->max_udf_writever;
2885
2886 udf_update_logvolname(ump, vat->logvol_id);
2887 }
2888
2889 /* read in complete VAT file */
2890 error = vn_rdwr(UIO_READ, vat_node->vnode,
2891 vat_table,
2892 vat_length, 0,
2893 UIO_SYSSPACE, IO_SYNC | IO_NODELOCKED, FSCRED,
2894 NULL, NULL);
2895 if (error)
2896 printf("read in of complete VAT file failed (error %d)\n",
2897 error);
2898 if (error)
2899 goto out;
2900
2901 DPRINTF(VOLUMES, ("VAT format accepted, marking it closed\n"));
2902 ump->logvol_integrity->lvint_next_unique_id = unique_id;
2903 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
2904 ump->logvol_integrity->time = *mtime;
2905
2906 ump->vat_table_len = vat_length;
2907 ump->vat_table_alloc_len = vat_table_alloc_len;
2908 ump->vat_table = vat_table;
2909 ump->vat_offset = vat_offset;
2910 ump->vat_entries = vat_entries;
2911 ump->vat_last_free_lb = 0; /* start at beginning */
2912
2913 out:
2914 if (error) {
2915 if (vat_table)
2916 free(vat_table, M_UDFVOLD);
2917 }
2918 free(raw_vat, M_TEMP);
2919
2920 return error;
2921 }
2922
2923 /* --------------------------------------------------------------------- */
2924
2925 static int
2926 udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping)
2927 {
2928 struct udf_node *vat_node;
2929 struct long_ad icb_loc;
2930 uint32_t early_vat_loc, late_vat_loc, vat_loc;
2931 int error;
2932
2933 /* mapping info not needed */
2934 mapping = mapping;
2935
2936 vat_loc = ump->last_possible_vat_location;
2937 early_vat_loc = vat_loc - 256; /* 8 blocks of 32 sectors */
2938
2939 DPRINTF(VOLUMES, ("1) last possible %d, early_vat_loc %d \n",
2940 vat_loc, early_vat_loc));
2941 early_vat_loc = MAX(early_vat_loc, ump->first_possible_vat_location);
2942 late_vat_loc = vat_loc + 1024;
2943
2944 DPRINTF(VOLUMES, ("2) last possible %d, early_vat_loc %d \n",
2945 vat_loc, early_vat_loc));
2946
2947 /* start looking from the end of the range */
2948 do {
2949 DPRINTF(VOLUMES, ("Checking for VAT at sector %d\n", vat_loc));
2950 icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART);
2951 icb_loc.loc.lb_num = udf_rw32(vat_loc);
2952
2953 error = udf_get_node(ump, &icb_loc, &vat_node);
2954 if (!error) {
2955 error = udf_check_for_vat(vat_node);
2956 DPRINTFIF(VOLUMES, !error,
2957 ("VAT accepted at %d\n", vat_loc));
2958 if (!error)
2959 break;
2960 }
2961 if (vat_node) {
2962 vput(vat_node->vnode);
2963 vat_node = NULL;
2964 }
2965 vat_loc--; /* walk backwards */
2966 } while (vat_loc >= early_vat_loc);
2967
2968 /* keep our VAT node around */
2969 if (vat_node) {
2970 UDF_SET_SYSTEMFILE(vat_node->vnode);
2971 ump->vat_node = vat_node;
2972 }
2973
2974 return error;
2975 }
2976
2977 /* --------------------------------------------------------------------- */
2978
2979 static int
2980 udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
2981 {
2982 union dscrptr *dscr;
2983 struct part_map_spare *pms = &mapping->pms;
2984 uint32_t lb_num;
2985 int spar, error;
2986
2987 /*
2988 * The partition mapping passed on to us specifies the information we
2989 * need to locate and initialise the sparable partition mapping
2990 * information we need.
2991 */
2992
2993 DPRINTF(VOLUMES, ("Read sparable table\n"));
2994 ump->sparable_packet_size = udf_rw16(pms->packet_len);
2995 KASSERT(ump->sparable_packet_size >= ump->packet_size); /* XXX */
2996
2997 for (spar = 0; spar < pms->n_st; spar++) {
2998 lb_num = pms->st_loc[spar];
2999 DPRINTF(VOLUMES, ("Checking for sparing table %d\n", lb_num));
3000 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
3001 if (!error && dscr) {
3002 if (udf_rw16(dscr->tag.id) == TAGID_SPARING_TABLE) {
3003 if (ump->sparing_table)
3004 free(ump->sparing_table, M_UDFVOLD);
3005 ump->sparing_table = &dscr->spt;
3006 dscr = NULL;
3007 DPRINTF(VOLUMES,
3008 ("Sparing table accepted (%d entries)\n",
3009 udf_rw16(ump->sparing_table->rt_l)));
3010 break; /* we're done */
3011 }
3012 }
3013 if (dscr)
3014 free(dscr, M_UDFVOLD);
3015 }
3016
3017 if (ump->sparing_table)
3018 return 0;
3019
3020 return ENOENT;
3021 }
3022
3023 /* --------------------------------------------------------------------- */
3024
3025 static int
3026 udf_read_metadata_nodes(struct udf_mount *ump, union udf_pmap *mapping)
3027 {
3028 struct part_map_meta *pmm = &mapping->pmm;
3029 struct long_ad icb_loc;
3030 struct vnode *vp;
3031 int error;
3032
3033 DPRINTF(VOLUMES, ("Reading in Metadata files\n"));
3034 icb_loc.loc.part_num = pmm->part_num;
3035 icb_loc.loc.lb_num = pmm->meta_file_lbn;
3036 DPRINTF(VOLUMES, ("Metadata file\n"));
3037 error = udf_get_node(ump, &icb_loc, &ump->metadata_node);
3038 if (ump->metadata_node) {
3039 vp = ump->metadata_node->vnode;
3040 UDF_SET_SYSTEMFILE(vp);
3041 }
3042
3043 icb_loc.loc.lb_num = pmm->meta_mirror_file_lbn;
3044 if (icb_loc.loc.lb_num != -1) {
3045 DPRINTF(VOLUMES, ("Metadata copy file\n"));
3046 error = udf_get_node(ump, &icb_loc, &ump->metadatamirror_node);
3047 if (ump->metadatamirror_node) {
3048 vp = ump->metadatamirror_node->vnode;
3049 UDF_SET_SYSTEMFILE(vp);
3050 }
3051 }
3052
3053 icb_loc.loc.lb_num = pmm->meta_bitmap_file_lbn;
3054 if (icb_loc.loc.lb_num != -1) {
3055 DPRINTF(VOLUMES, ("Metadata bitmap file\n"));
3056 error = udf_get_node(ump, &icb_loc, &ump->metadatabitmap_node);
3057 if (ump->metadatabitmap_node) {
3058 vp = ump->metadatabitmap_node->vnode;
3059 UDF_SET_SYSTEMFILE(vp);
3060 }
3061 }
3062
3063 /* if we're mounting read-only we relax the requirements */
3064 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY) {
3065 error = EFAULT;
3066 if (ump->metadata_node)
3067 error = 0;
3068 if ((ump->metadata_node == NULL) && (ump->metadatamirror_node)) {
3069 printf( "udf mount: Metadata file not readable, "
3070 "substituting Metadata copy file\n");
3071 ump->metadata_node = ump->metadatamirror_node;
3072 ump->metadatamirror_node = NULL;
3073 error = 0;
3074 }
3075 } else {
3076 /* mounting read/write */
3077 /* XXX DISABLED! metadata writing is not working yet XXX */
3078 if (error)
3079 error = EROFS;
3080 }
3081 DPRINTFIF(VOLUMES, error, ("udf mount: failed to read "
3082 "metadata files\n"));
3083 return error;
3084 }
3085
3086 /* --------------------------------------------------------------------- */
3087
3088 int
3089 udf_read_vds_tables(struct udf_mount *ump)
3090 {
3091 union udf_pmap *mapping;
3092 /* struct udf_args *args = &ump->mount_args; */
3093 uint32_t n_pm, mt_l;
3094 uint32_t log_part;
3095 uint8_t *pmap_pos;
3096 int pmap_size;
3097 int error;
3098
3099 /* Iterate (again) over the part mappings for locations */
3100 n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
3101 mt_l = udf_rw32(ump->logical_vol->mt_l); /* partmaps data length */
3102 pmap_pos = ump->logical_vol->maps;
3103
3104 for (log_part = 0; log_part < n_pm; log_part++) {
3105 mapping = (union udf_pmap *) pmap_pos;
3106 switch (ump->vtop_tp[log_part]) {
3107 case UDF_VTOP_TYPE_PHYS :
3108 /* nothing */
3109 break;
3110 case UDF_VTOP_TYPE_VIRT :
3111 /* search and load VAT */
3112 error = udf_search_vat(ump, mapping);
3113 if (error)
3114 return ENOENT;
3115 break;
3116 case UDF_VTOP_TYPE_SPARABLE :
3117 /* load one of the sparable tables */
3118 error = udf_read_sparables(ump, mapping);
3119 if (error)
3120 return ENOENT;
3121 break;
3122 case UDF_VTOP_TYPE_META :
3123 /* load the associated file descriptors */
3124 error = udf_read_metadata_nodes(ump, mapping);
3125 if (error)
3126 return ENOENT;
3127 break;
3128 default:
3129 break;
3130 }
3131 pmap_size = pmap_pos[1];
3132 pmap_pos += pmap_size;
3133 }
3134
3135 /* read in and check unallocated and free space info if writing */
3136 if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY) == 0) {
3137 error = udf_read_physical_partition_spacetables(ump);
3138 if (error)
3139 return error;
3140
3141 /* also read in metadata partion spacebitmap if defined */
3142 error = udf_read_metadata_partition_spacetable(ump);
3143 return error;
3144 }
3145
3146 return 0;
3147 }
3148
3149 /* --------------------------------------------------------------------- */
3150
3151 int
3152 udf_read_rootdirs(struct udf_mount *ump)
3153 {
3154 union dscrptr *dscr;
3155 /* struct udf_args *args = &ump->mount_args; */
3156 struct udf_node *rootdir_node, *streamdir_node;
3157 struct long_ad fsd_loc, *dir_loc;
3158 uint32_t lb_num, dummy;
3159 uint32_t fsd_len;
3160 int dscr_type;
3161 int error;
3162
3163 /* TODO implement FSD reading in separate function like integrity? */
3164 /* get fileset descriptor sequence */
3165 fsd_loc = ump->logical_vol->lv_fsd_loc;
3166 fsd_len = udf_rw32(fsd_loc.len);
3167
3168 dscr = NULL;
3169 error = 0;
3170 while (fsd_len || error) {
3171 DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
3172 /* translate fsd_loc to lb_num */
3173 error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
3174 if (error)
3175 break;
3176 DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
3177 error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
3178 /* end markers */
3179 if (error || (dscr == NULL))
3180 break;
3181
3182 /* analyse */
3183 dscr_type = udf_rw16(dscr->tag.id);
3184 if (dscr_type == TAGID_TERM)
3185 break;
3186 if (dscr_type != TAGID_FSD) {
3187 free(dscr, M_UDFVOLD);
3188 return ENOENT;
3189 }
3190
3191 /*
3192 * TODO check for multiple fileset descriptors; its only
3193 * picking the last now. Also check for FSD
3194 * correctness/interpretability
3195 */
3196
3197 /* update */
3198 if (ump->fileset_desc) {
3199 free(ump->fileset_desc, M_UDFVOLD);
3200 }
3201 ump->fileset_desc = &dscr->fsd;
3202 dscr = NULL;
3203
3204 /* continue to the next fsd */
3205 fsd_len -= ump->discinfo.sector_size;
3206 fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
3207
3208 /* follow up to fsd->next_ex (long_ad) if its not null */
3209 if (udf_rw32(ump->fileset_desc->next_ex.len)) {
3210 DPRINTF(VOLUMES, ("follow up FSD extent\n"));
3211 fsd_loc = ump->fileset_desc->next_ex;
3212 fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
3213 }
3214 }
3215 if (dscr)
3216 free(dscr, M_UDFVOLD);
3217
3218 /* there has to be one */
3219 if (ump->fileset_desc == NULL)
3220 return ENOENT;
3221
3222 DPRINTF(VOLUMES, ("FSD read in fine\n"));
3223 DPRINTF(VOLUMES, ("Updating fsd logical volume id\n"));
3224 udf_update_logvolname(ump, ump->logical_vol->logvol_id);
3225
3226 /*
3227 * Now the FSD is known, read in the rootdirectory and if one exists,
3228 * the system stream dir. Some files in the system streamdir are not
3229 * wanted in this implementation since they are not maintained. If
3230 * writing is enabled we'll delete these files if they exist.
3231 */
3232
3233 rootdir_node = streamdir_node = NULL;
3234 dir_loc = NULL;
3235
3236 /* try to read in the rootdir */
3237 dir_loc = &ump->fileset_desc->rootdir_icb;
3238 error = udf_get_node(ump, dir_loc, &rootdir_node);
3239 if (error)
3240 return ENOENT;
3241
3242 /* aparently it read in fine */
3243
3244 /*
3245 * Try the system stream directory; not very likely in the ones we
3246 * test, but for completeness.
3247 */
3248 dir_loc = &ump->fileset_desc->streamdir_icb;
3249 if (udf_rw32(dir_loc->len)) {
3250 printf("udf_read_rootdirs: streamdir defined ");
3251 error = udf_get_node(ump, dir_loc, &streamdir_node);
3252 if (error) {
3253 printf("but error in streamdir reading\n");
3254 } else {
3255 printf("but ignored\n");
3256 /*
3257 * TODO process streamdir `baddies' i.e. files we dont
3258 * want if R/W
3259 */
3260 }
3261 }
3262
3263 DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
3264
3265 /* release the vnodes again; they'll be auto-recycled later */
3266 if (streamdir_node) {
3267 vput(streamdir_node->vnode);
3268 }
3269 if (rootdir_node) {
3270 vput(rootdir_node->vnode);
3271 }
3272
3273 return 0;
3274 }
3275
3276 /* --------------------------------------------------------------------- */
3277
3278 /* To make absolutely sure we are NOT returning zero, add one :) */
3279
3280 long
3281 udf_calchash(struct long_ad *icbptr)
3282 {
3283 /* ought to be enough since each mountpoint has its own chain */
3284 return udf_rw32(icbptr->loc.lb_num) + 1;
3285 }
3286
3287
3288 static struct udf_node *
3289 udf_hash_lookup(struct udf_mount *ump, struct long_ad *icbptr)
3290 {
3291 struct udf_node *node;
3292 struct vnode *vp;
3293 uint32_t hashline;
3294
3295 loop:
3296 mutex_enter(&ump->ihash_lock);
3297
3298 hashline = udf_calchash(icbptr) & UDF_INODE_HASHMASK;
3299 LIST_FOREACH(node, &ump->udf_nodes[hashline], hashchain) {
3300 assert(node);
3301 if (node->loc.loc.lb_num == icbptr->loc.lb_num &&
3302 node->loc.loc.part_num == icbptr->loc.part_num) {
3303 vp = node->vnode;
3304 assert(vp);
3305 mutex_enter(&vp->v_interlock);
3306 mutex_exit(&ump->ihash_lock);
3307 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
3308 goto loop;
3309 return node;
3310 }
3311 }
3312 mutex_exit(&ump->ihash_lock);
3313
3314 return NULL;
3315 }
3316
3317
3318 static void
3319 udf_sorted_list_insert(struct udf_node *node)
3320 {
3321 struct udf_mount *ump;
3322 struct udf_node *s_node, *last_node;
3323 uint32_t loc, s_loc;
3324
3325 ump = node->ump;
3326 last_node = NULL; /* XXX gcc */
3327
3328 if (LIST_EMPTY(&ump->sorted_udf_nodes)) {
3329 LIST_INSERT_HEAD(&ump->sorted_udf_nodes, node, sortchain);
3330 return;
3331 }
3332
3333 /*
3334 * We sort on logical block number here and not on physical block
3335 * number here. Ideally we should go for the physical block nr to get
3336 * better sync performance though this sort will ensure that packets
3337 * won't get spit up unnessisarily.
3338 */
3339
3340 loc = udf_rw32(node->loc.loc.lb_num);
3341 LIST_FOREACH(s_node, &ump->sorted_udf_nodes, sortchain) {
3342 s_loc = udf_rw32(s_node->loc.loc.lb_num);
3343 if (s_loc > loc) {
3344 LIST_INSERT_BEFORE(s_node, node, sortchain);
3345 return;
3346 }
3347 last_node = s_node;
3348 }
3349 LIST_INSERT_AFTER(last_node, node, sortchain);
3350 }
3351
3352
3353 static void
3354 udf_register_node(struct udf_node *node)
3355 {
3356 struct udf_mount *ump;
3357 struct udf_node *chk;
3358 uint32_t hashline;
3359
3360 ump = node->ump;
3361 mutex_enter(&ump->ihash_lock);
3362
3363 /* add to our hash table */
3364 hashline = udf_calchash(&node->loc) & UDF_INODE_HASHMASK;
3365 #ifdef DEBUG
3366 LIST_FOREACH(chk, &ump->udf_nodes[hashline], hashchain) {
3367 assert(chk);
3368 if (chk->loc.loc.lb_num == node->loc.loc.lb_num &&
3369 chk->loc.loc.part_num == node->loc.loc.part_num)
3370 panic("Double node entered\n");
3371 }
3372 #else
3373 chk = NULL;
3374 #endif
3375 LIST_INSERT_HEAD(&ump->udf_nodes[hashline], node, hashchain);
3376
3377 /* add to our sorted list */
3378 udf_sorted_list_insert(node);
3379
3380 mutex_exit(&ump->ihash_lock);
3381 }
3382
3383
3384 static void
3385 udf_deregister_node(struct udf_node *node)
3386 {
3387 struct udf_mount *ump;
3388
3389 ump = node->ump;
3390 mutex_enter(&ump->ihash_lock);
3391
3392 /* from hash and sorted list */
3393 LIST_REMOVE(node, hashchain);
3394 LIST_REMOVE(node, sortchain);
3395
3396 mutex_exit(&ump->ihash_lock);
3397 }
3398
3399 /* --------------------------------------------------------------------- */
3400
3401 int
3402 udf_open_logvol(struct udf_mount *ump)
3403 {
3404 int logvol_integrity;
3405 int error;
3406
3407 /* already/still open? */
3408 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
3409 if (logvol_integrity == UDF_INTEGRITY_OPEN)
3410 return 0;
3411
3412 /* can we open it ? */
3413 if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
3414 return EROFS;
3415
3416 /* setup write parameters */
3417 DPRINTF(VOLUMES, ("Setting up write parameters\n"));
3418 if ((error = udf_setup_writeparams(ump)) != 0)
3419 return error;
3420
3421 /* determine data and metadata tracks (most likely same) */
3422 error = udf_search_writing_tracks(ump);
3423 if (error) {
3424 /* most likely lack of space */
3425 printf("udf_open_logvol: error searching writing tracks\n");
3426 return EROFS;
3427 }
3428
3429 /* writeout/update lvint on disc or only in memory */
3430 DPRINTF(VOLUMES, ("Opening logical volume\n"));
3431 if (ump->lvopen & UDF_OPEN_SESSION) {
3432 /* TODO implement writeout of VRS + VDS */
3433 printf( "udf_open_logvol:Opening a closed session not yet "
3434 "implemented\n");
3435 return EROFS;
3436
3437 /* determine data and metadata tracks again */
3438 error = udf_search_writing_tracks(ump);
3439 }
3440
3441 /* mark it open */
3442 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_OPEN);
3443
3444 /* do we need to write it out? */
3445 if (ump->lvopen & UDF_WRITE_LVINT) {
3446 error = udf_writeout_lvint(ump, ump->lvopen);
3447 /* if we couldn't write it mark it closed again */
3448 if (error) {
3449 ump->logvol_integrity->integrity_type =
3450 udf_rw32(UDF_INTEGRITY_CLOSED);
3451 return error;
3452 }
3453 }
3454
3455 return 0;
3456 }
3457
3458
3459 int
3460 udf_close_logvol(struct udf_mount *ump, int mntflags)
3461 {
3462 int logvol_integrity;
3463 int error = 0, error1 = 0, error2 = 0;
3464 int n;
3465
3466 /* already/still closed? */
3467 logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
3468 if (logvol_integrity == UDF_INTEGRITY_CLOSED)
3469 return 0;
3470
3471 /* writeout/update lvint or write out VAT */
3472 DPRINTF(VOLUMES, ("Closing logical volume\n"));
3473 if (ump->lvclose & UDF_WRITE_VAT) {
3474 DPRINTF(VOLUMES, ("lvclose & UDF_WRITE_VAT\n"));
3475
3476 /* write out the VAT node */
3477 DPRINTF(VOLUMES, ("writeout vat_node\n"));
3478 udf_writeout_vat(ump);
3479
3480 vflushbuf(ump->vat_node->vnode, 1 /* sync */);
3481 for (n = 0; n < 16; n++) {
3482 ump->vat_node->i_flags |= IN_MODIFIED;
3483 error = VOP_FSYNC(ump->vat_node->vnode,
3484 FSCRED, FSYNC_WAIT, 0, 0);
3485 }
3486 if (error) {
3487 printf("udf_close_logvol: writeout of VAT failed\n");
3488 return error;
3489 }
3490 }
3491
3492 if (ump->lvclose & UDF_WRITE_PART_BITMAPS) {
3493 /* sync writeout metadata spacetable if existing */
3494 error1 = udf_write_metadata_partition_spacetable(ump, true);
3495 if (error1)
3496 printf( "udf_close_logvol: writeout of metadata space "
3497 "bitmap failed\n");
3498
3499 /* sync writeout partition spacetables */
3500 error2 = udf_write_physical_partition_spacetables(ump, true);
3501 if (error2)
3502 printf( "udf_close_logvol: writeout of space tables "
3503 "failed\n");
3504
3505 if (error1 || error2)
3506 return (error1 | error2);
3507
3508 ump->lvclose &= ~UDF_WRITE_PART_BITMAPS;
3509 }
3510
3511 if (ump->lvclose & UDF_CLOSE_SESSION) {
3512 printf("TODO: Closing a session is not yet implemented\n");
3513 return EROFS;
3514 ump->lvopen |= UDF_OPEN_SESSION;
3515 }
3516
3517 /* mark it closed */
3518 ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
3519
3520 /* do we need to write out the logical volume integrity */
3521 if (ump->lvclose & UDF_WRITE_LVINT)
3522 error = udf_writeout_lvint(ump, ump->lvopen);
3523 if (error) {
3524 /* HELP now what? mark it open again for now */
3525 ump->logvol_integrity->integrity_type =
3526 udf_rw32(UDF_INTEGRITY_OPEN);
3527 return error;
3528 }
3529
3530 (void) udf_synchronise_caches(ump);
3531
3532 return 0;
3533 }
3534
3535 /* --------------------------------------------------------------------- */
3536
3537 /*
3538 * Genfs interfacing
3539 *
3540 * static const struct genfs_ops udf_genfsops = {
3541 * .gop_size = genfs_size,
3542 * size of transfers
3543 * .gop_alloc = udf_gop_alloc,
3544 * allocate len bytes at offset
3545 * .gop_write = genfs_gop_write,
3546 * putpages interface code
3547 * .gop_markupdate = udf_gop_markupdate,
3548 * set update/modify flags etc.
3549 * }
3550 */
3551
3552 /*
3553 * Genfs interface. These four functions are the only ones defined though not
3554 * documented... great....
3555 */
3556
3557 /*
3558 * Callback from genfs to allocate len bytes at offset off; only called when
3559 * filling up gaps in the allocation.
3560 */
3561 /* XXX should we check if there is space enough in udf_gop_alloc? */
3562 static int
3563 udf_gop_alloc(struct vnode *vp, off_t off,
3564 off_t len, int flags, kauth_cred_t cred)
3565 {
3566 #if 0
3567 struct udf_node *udf_node = VTOI(vp);
3568 struct udf_mount *ump = udf_node->ump;
3569 uint32_t lb_size, num_lb;
3570 #endif
3571
3572 DPRINTF(NOTIMPL, ("udf_gop_alloc not implemented\n"));
3573 DPRINTF(ALLOC, ("udf_gop_alloc called for %"PRIu64" bytes\n", len));
3574
3575 return 0;
3576 }
3577
3578
3579 /*
3580 * callback from genfs to update our flags
3581 */
3582 static void
3583 udf_gop_markupdate(struct vnode *vp, int flags)
3584 {
3585 struct udf_node *udf_node = VTOI(vp);
3586 u_long mask = 0;
3587
3588 if ((flags & GOP_UPDATE_ACCESSED) != 0) {
3589 mask = IN_ACCESS;
3590 }
3591 if ((flags & GOP_UPDATE_MODIFIED) != 0) {
3592 if (vp->v_type == VREG) {
3593 mask |= IN_CHANGE | IN_UPDATE;
3594 } else {
3595 mask |= IN_MODIFY;
3596 }
3597 }
3598 if (mask) {
3599 udf_node->i_flags |= mask;
3600 }
3601 }
3602
3603
3604 static const struct genfs_ops udf_genfsops = {
3605 .gop_size = genfs_size,
3606 .gop_alloc = udf_gop_alloc,
3607 .gop_write = genfs_gop_write_rwmap,
3608 .gop_markupdate = udf_gop_markupdate,
3609 };
3610
3611
3612 /* --------------------------------------------------------------------- */
3613
3614 int
3615 udf_write_terminator(struct udf_mount *ump, uint32_t sector)
3616 {
3617 union dscrptr *dscr;
3618 int error;
3619
3620 dscr = malloc(ump->discinfo.sector_size, M_TEMP, M_WAITOK);
3621 bzero(dscr, ump->discinfo.sector_size);
3622 udf_inittag(ump, &dscr->tag, TAGID_TERM, sector);
3623
3624 /* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
3625 dscr->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
3626 (void) udf_validate_tag_and_crc_sums(dscr);
3627
3628 error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_DSCR,
3629 dscr, sector, sector);
3630
3631 free(dscr, M_TEMP);
3632
3633 return error;
3634 }
3635
3636
3637 /* --------------------------------------------------------------------- */
3638
3639 /* UDF<->unix converters */
3640
3641 /* --------------------------------------------------------------------- */
3642
3643 static mode_t
3644 udf_perm_to_unix_mode(uint32_t perm)
3645 {
3646 mode_t mode;
3647
3648 mode = ((perm & UDF_FENTRY_PERM_USER_MASK) );
3649 mode |= ((perm & UDF_FENTRY_PERM_GRP_MASK ) >> 2);
3650 mode |= ((perm & UDF_FENTRY_PERM_OWNER_MASK) >> 4);
3651
3652 return mode;
3653 }
3654
3655 /* --------------------------------------------------------------------- */
3656
3657 static uint32_t
3658 unix_mode_to_udf_perm(mode_t mode)
3659 {
3660 uint32_t perm;
3661
3662 perm = ((mode & S_IRWXO) );
3663 perm |= ((mode & S_IRWXG) << 2);
3664 perm |= ((mode & S_IRWXU) << 4);
3665 perm |= ((mode & S_IWOTH) << 3);
3666 perm |= ((mode & S_IWGRP) << 5);
3667 perm |= ((mode & S_IWUSR) << 7);
3668
3669 return perm;
3670 }
3671
3672 /* --------------------------------------------------------------------- */
3673
3674 static uint32_t
3675 udf_icb_to_unix_filetype(uint32_t icbftype)
3676 {
3677 switch (icbftype) {
3678 case UDF_ICB_FILETYPE_DIRECTORY :
3679 case UDF_ICB_FILETYPE_STREAMDIR :
3680 return S_IFDIR;
3681 case UDF_ICB_FILETYPE_FIFO :
3682 return S_IFIFO;
3683 case UDF_ICB_FILETYPE_CHARDEVICE :
3684 return S_IFCHR;
3685 case UDF_ICB_FILETYPE_BLOCKDEVICE :
3686 return S_IFBLK;
3687 case UDF_ICB_FILETYPE_RANDOMACCESS :
3688 case UDF_ICB_FILETYPE_REALTIME :
3689 return S_IFREG;
3690 case UDF_ICB_FILETYPE_SYMLINK :
3691 return S_IFLNK;
3692 case UDF_ICB_FILETYPE_SOCKET :
3693 return S_IFSOCK;
3694 }
3695 /* no idea what this is */
3696 return 0;
3697 }
3698
3699 /* --------------------------------------------------------------------- */
3700
3701 void
3702 udf_to_unix_name(char *result, int result_len, char *id, int len,
3703 struct charspec *chsp)
3704 {
3705 uint16_t *raw_name, *unix_name;
3706 uint16_t *inchp, ch;
3707 uint8_t *outchp;
3708 const char *osta_id = "OSTA Compressed Unicode";
3709 int ucode_chars, nice_uchars, is_osta_typ0, nout;
3710
3711 raw_name = malloc(2048 * sizeof(uint16_t), M_UDFTEMP, M_WAITOK);
3712 unix_name = raw_name + 1024; /* split space in half */
3713 assert(sizeof(char) == sizeof(uint8_t));
3714 outchp = (uint8_t *) result;
3715
3716 is_osta_typ0 = (chsp->type == 0);
3717 is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
3718 if (is_osta_typ0) {
3719 /* TODO clean up */
3720 *raw_name = *unix_name = 0;
3721 ucode_chars = udf_UncompressUnicode(len, (uint8_t *) id, raw_name);
3722 ucode_chars = MIN(ucode_chars, UnicodeLength((unicode_t *) raw_name));
3723 nice_uchars = UDFTransName(unix_name, raw_name, ucode_chars);
3724 /* output UTF8 */
3725 for (inchp = unix_name; nice_uchars>0; inchp++, nice_uchars--) {
3726 ch = *inchp;
3727 nout = wput_utf8(outchp, result_len, ch);
3728 outchp += nout; result_len -= nout;
3729 if (!ch) break;
3730 }
3731 *outchp++ = 0;
3732 } else {
3733 /* assume 8bit char length byte latin-1 */
3734 assert(*id == 8);
3735 assert(strlen((char *) (id+1)) <= MAXNAMLEN);
3736 strncpy((char *) result, (char *) (id+1), strlen((char *) (id+1)));
3737 }
3738 free(raw_name, M_UDFTEMP);
3739 }
3740
3741 /* --------------------------------------------------------------------- */
3742
3743 void
3744 unix_to_udf_name(char *result, uint8_t *result_len, char const *name, int name_len,
3745 struct charspec *chsp)
3746 {
3747 uint16_t *raw_name;
3748 uint16_t *outchp;
3749 const char *inchp;
3750 const char *osta_id = "OSTA Compressed Unicode";
3751 int udf_chars, is_osta_typ0, bits;
3752 size_t cnt;
3753
3754 /* allocate temporary unicode-16 buffer */
3755 raw_name = malloc(1024, M_UDFTEMP, M_WAITOK);
3756
3757 /* convert utf8 to unicode-16 */
3758 *raw_name = 0;
3759 inchp = name;
3760 outchp = raw_name;
3761 bits = 8;
3762 for (cnt = name_len, udf_chars = 0; cnt;) {
3763 /*###3490 [cc] warning: passing argument 2 of 'wget_utf8' from incompatible pointer type%%%*/
3764 *outchp = wget_utf8(&inchp, &cnt);
3765 if (*outchp > 0xff)
3766 bits=16;
3767 outchp++;
3768 udf_chars++;
3769 }
3770 /* null terminate just in case */
3771 *outchp++ = 0;
3772
3773 is_osta_typ0 = (chsp->type == 0);
3774 is_osta_typ0 &= (strcmp((char *) chsp->inf, osta_id) == 0);
3775 if (is_osta_typ0) {
3776 udf_chars = udf_CompressUnicode(udf_chars, bits,
3777 (unicode_t *) raw_name,
3778 (byte *) result);
3779 } else {
3780 printf("unix to udf name: no CHSP0 ?\n");
3781 /* XXX assume 8bit char length byte latin-1 */
3782 *result++ = 8; udf_chars = 1;
3783 strncpy(result, name + 1, name_len);
3784 udf_chars += name_len;
3785 }
3786 *result_len = udf_chars;
3787 free(raw_name, M_UDFTEMP);
3788 }
3789
3790 /* --------------------------------------------------------------------- */
3791
3792 void
3793 udf_timestamp_to_timespec(struct udf_mount *ump,
3794 struct timestamp *timestamp,
3795 struct timespec *timespec)
3796 {
3797 struct clock_ymdhms ymdhms;
3798 uint32_t usecs, secs, nsecs;
3799 uint16_t tz;
3800
3801 /* fill in ymdhms structure from timestamp */
3802 memset(&ymdhms, 0, sizeof(ymdhms));
3803 ymdhms.dt_year = udf_rw16(timestamp->year);
3804 ymdhms.dt_mon = timestamp->month;
3805 ymdhms.dt_day = timestamp->day;
3806 ymdhms.dt_wday = 0; /* ? */
3807 ymdhms.dt_hour = timestamp->hour;
3808 ymdhms.dt_min = timestamp->minute;
3809 ymdhms.dt_sec = timestamp->second;
3810
3811 secs = clock_ymdhms_to_secs(&ymdhms);
3812 usecs = timestamp->usec +
3813 100*timestamp->hund_usec + 10000*timestamp->centisec;
3814 nsecs = usecs * 1000;
3815
3816 /*
3817 * Calculate the time zone. The timezone is 12 bit signed 2's
3818 * compliment, so we gotta do some extra magic to handle it right.
3819 */
3820 tz = udf_rw16(timestamp->type_tz);
3821 tz &= 0x0fff; /* only lower 12 bits are significant */
3822 if (tz & 0x0800) /* sign extention */
3823 tz |= 0xf000;
3824
3825 /* TODO check timezone conversion */
3826 /* check if we are specified a timezone to convert */
3827 if (udf_rw16(timestamp->type_tz) & 0x1000) {
3828 if ((int16_t) tz != -2047)
3829 secs -= (int16_t) tz * 60;
3830 } else {
3831 secs -= ump->mount_args.gmtoff;
3832 }
3833
3834 timespec->tv_sec = secs;
3835 timespec->tv_nsec = nsecs;
3836 }
3837
3838
3839 void
3840 udf_timespec_to_timestamp(struct timespec *timespec, struct timestamp *timestamp)
3841 {
3842 struct clock_ymdhms ymdhms;
3843 uint32_t husec, usec, csec;
3844
3845 (void) clock_secs_to_ymdhms(timespec->tv_sec, &ymdhms);
3846
3847 usec = timespec->tv_nsec / 1000;
3848 husec = usec / 100;
3849 usec -= husec * 100; /* only 0-99 in usec */
3850 csec = husec / 100; /* only 0-99 in csec */
3851 husec -= csec * 100; /* only 0-99 in husec */
3852
3853 /* set method 1 for CUT/GMT */
3854 timestamp->type_tz = udf_rw16((1<<12) + 0);
3855 timestamp->year = udf_rw16(ymdhms.dt_year);
3856 timestamp->month = ymdhms.dt_mon;
3857 timestamp->day = ymdhms.dt_day;
3858 timestamp->hour = ymdhms.dt_hour;
3859 timestamp->minute = ymdhms.dt_min;
3860 timestamp->second = ymdhms.dt_sec;
3861 timestamp->centisec = csec;
3862 timestamp->hund_usec = husec;
3863 timestamp->usec = usec;
3864 }
3865
3866 /* --------------------------------------------------------------------- */
3867
3868 /*
3869 * Attribute and filetypes converters with get/set pairs
3870 */
3871
3872 uint32_t
3873 udf_getaccessmode(struct udf_node *udf_node)
3874 {
3875 struct file_entry *fe = udf_node->fe;;
3876 struct extfile_entry *efe = udf_node->efe;
3877 uint32_t udf_perm, icbftype;
3878 uint32_t mode, ftype;
3879 uint16_t icbflags;
3880
3881 UDF_LOCK_NODE(udf_node, 0);
3882 if (fe) {
3883 udf_perm = udf_rw32(fe->perm);
3884 icbftype = fe->icbtag.file_type;
3885 icbflags = udf_rw16(fe->icbtag.flags);
3886 } else {
3887 assert(udf_node->efe);
3888 udf_perm = udf_rw32(efe->perm);
3889 icbftype = efe->icbtag.file_type;
3890 icbflags = udf_rw16(efe->icbtag.flags);
3891 }
3892
3893 mode = udf_perm_to_unix_mode(udf_perm);
3894 ftype = udf_icb_to_unix_filetype(icbftype);
3895
3896 /* set suid, sgid, sticky from flags in fe/efe */
3897 if (icbflags & UDF_ICB_TAG_FLAGS_SETUID)
3898 mode |= S_ISUID;
3899 if (icbflags & UDF_ICB_TAG_FLAGS_SETGID)
3900 mode |= S_ISGID;
3901 if (icbflags & UDF_ICB_TAG_FLAGS_STICKY)
3902 mode |= S_ISVTX;
3903
3904 UDF_UNLOCK_NODE(udf_node, 0);
3905
3906 return mode | ftype;
3907 }
3908
3909
3910 void
3911 udf_setaccessmode(struct udf_node *udf_node, mode_t mode)
3912 {
3913 struct file_entry *fe = udf_node->fe;
3914 struct extfile_entry *efe = udf_node->efe;
3915 uint32_t udf_perm;
3916 uint16_t icbflags;
3917
3918 UDF_LOCK_NODE(udf_node, 0);
3919 udf_perm = unix_mode_to_udf_perm(mode & ALLPERMS);
3920 if (fe) {
3921 icbflags = udf_rw16(fe->icbtag.flags);
3922 } else {
3923 icbflags = udf_rw16(efe->icbtag.flags);
3924 }
3925
3926 icbflags &= ~UDF_ICB_TAG_FLAGS_SETUID;
3927 icbflags &= ~UDF_ICB_TAG_FLAGS_SETGID;
3928 icbflags &= ~UDF_ICB_TAG_FLAGS_STICKY;
3929 if (mode & S_ISUID)
3930 icbflags |= UDF_ICB_TAG_FLAGS_SETUID;
3931 if (mode & S_ISGID)
3932 icbflags |= UDF_ICB_TAG_FLAGS_SETGID;
3933 if (mode & S_ISVTX)
3934 icbflags |= UDF_ICB_TAG_FLAGS_STICKY;
3935
3936 if (fe) {
3937 fe->perm = udf_rw32(udf_perm);
3938 fe->icbtag.flags = udf_rw16(icbflags);
3939 } else {
3940 efe->perm = udf_rw32(udf_perm);
3941 efe->icbtag.flags = udf_rw16(icbflags);
3942 }
3943
3944 UDF_UNLOCK_NODE(udf_node, 0);
3945 }
3946
3947
3948 void
3949 udf_getownership(struct udf_node *udf_node, uid_t *uidp, gid_t *gidp)
3950 {
3951 struct udf_mount *ump = udf_node->ump;
3952 struct file_entry *fe = udf_node->fe;
3953 struct extfile_entry *efe = udf_node->efe;
3954 uid_t uid;
3955 gid_t gid;
3956
3957 UDF_LOCK_NODE(udf_node, 0);
3958 if (fe) {
3959 uid = (uid_t)udf_rw32(fe->uid);
3960 gid = (gid_t)udf_rw32(fe->gid);
3961 } else {
3962 assert(udf_node->efe);
3963 uid = (uid_t)udf_rw32(efe->uid);
3964 gid = (gid_t)udf_rw32(efe->gid);
3965 }
3966
3967 /* do the uid/gid translation game */
3968 if ((uid == (uid_t) -1) && (gid == (gid_t) -1)) {
3969 uid = ump->mount_args.anon_uid;
3970 gid = ump->mount_args.anon_gid;
3971 }
3972 *uidp = uid;
3973 *gidp = gid;
3974
3975 UDF_UNLOCK_NODE(udf_node, 0);
3976 }
3977
3978
3979 void
3980 udf_setownership(struct udf_node *udf_node, uid_t uid, gid_t gid)
3981 {
3982 struct udf_mount *ump = udf_node->ump;
3983 struct file_entry *fe = udf_node->fe;
3984 struct extfile_entry *efe = udf_node->efe;
3985 uid_t nobody_uid;
3986 gid_t nobody_gid;
3987
3988 UDF_LOCK_NODE(udf_node, 0);
3989
3990 /* do the uid/gid translation game */
3991 nobody_uid = ump->mount_args.nobody_uid;
3992 nobody_gid = ump->mount_args.nobody_gid;
3993 if ((uid == nobody_uid) && (gid == nobody_gid)) {
3994 uid = (uid_t) -1;
3995 gid = (gid_t) -1;
3996 }
3997
3998 if (fe) {
3999 fe->uid = udf_rw32((uint32_t) uid);
4000 fe->gid = udf_rw32((uint32_t) gid);
4001 } else {
4002 efe->uid = udf_rw32((uint32_t) uid);
4003 efe->gid = udf_rw32((uint32_t) gid);
4004 }
4005
4006 UDF_UNLOCK_NODE(udf_node, 0);
4007 }
4008
4009
4010 /* --------------------------------------------------------------------- */
4011
4012
4013 static int
4014 dirhash_fill(struct udf_node *dir_node)
4015 {
4016 struct vnode *dvp = dir_node->vnode;
4017 struct dirhash *dirh;
4018 struct file_entry *fe = dir_node->fe;
4019 struct extfile_entry *efe = dir_node->efe;
4020 struct fileid_desc *fid;
4021 struct dirent *dirent;
4022 uint64_t file_size, pre_diroffset, diroffset;
4023 uint32_t lb_size;
4024 int error;
4025
4026 /* make sure we have a dirhash to work on */
4027 dirh = dir_node->dir_hash;
4028 KASSERT(dirh);
4029 KASSERT(dirh->refcnt > 0);
4030
4031 if (dirh->flags & DIRH_BROKEN)
4032 return EIO;
4033 if (dirh->flags & DIRH_COMPLETE)
4034 return 0;
4035
4036 /* make sure we have a clean dirhash to add to */
4037 dirhash_purge_entries(dirh);
4038
4039 /* get directory filesize */
4040 if (fe) {
4041 file_size = udf_rw64(fe->inf_len);
4042 } else {
4043 assert(efe);
4044 file_size = udf_rw64(efe->inf_len);
4045 }
4046
4047 /* allocate temporary space for fid */
4048 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4049 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4050
4051 /* allocate temporary space for dirent */
4052 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4053
4054 error = 0;
4055 diroffset = 0;
4056 while (diroffset < file_size) {
4057 /* transfer a new fid/dirent */
4058 pre_diroffset = diroffset;
4059 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
4060 if (error) {
4061 /* TODO what to do? continue but not add? */
4062 dirh->flags |= DIRH_BROKEN;
4063 dirhash_purge_entries(dirh);
4064 break;
4065 }
4066
4067 if ((fid->file_char & UDF_FILE_CHAR_DEL)) {
4068 /* register deleted extent for reuse */
4069 dirhash_enter_freed(dirh, pre_diroffset,
4070 udf_fidsize(fid));
4071 } else {
4072 /* append to the dirhash */
4073 dirhash_enter(dirh, dirent, pre_diroffset,
4074 udf_fidsize(fid), 0);
4075 }
4076 }
4077 dirh->flags |= DIRH_COMPLETE;
4078
4079 free(fid, M_UDFTEMP);
4080 free(dirent, M_UDFTEMP);
4081
4082 return error;
4083 }
4084
4085
4086 /* --------------------------------------------------------------------- */
4087
4088 /*
4089 * Directory read and manipulation functions.
4090 *
4091 */
4092
4093 int
4094 udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
4095 struct long_ad *icb_loc, int *found)
4096 {
4097 struct udf_node *dir_node = VTOI(vp);
4098 struct dirhash *dirh;
4099 struct dirhash_entry *dirh_ep;
4100 struct fileid_desc *fid;
4101 struct dirent *dirent;
4102 uint64_t diroffset;
4103 uint32_t lb_size;
4104 int hit, error;
4105
4106 /* set default return */
4107 *found = 0;
4108
4109 /* get our dirhash and make sure its read in */
4110 dirhash_get(&dir_node->dir_hash);
4111 error = dirhash_fill(dir_node);
4112 if (error) {
4113 dirhash_put(dir_node->dir_hash);
4114 return error;
4115 }
4116 dirh = dir_node->dir_hash;
4117
4118 /* allocate temporary space for fid */
4119 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4120 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4121 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4122
4123 DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n",
4124 namelen, namelen, name));
4125
4126 /* search our dirhash hits */
4127 memset(icb_loc, 0, sizeof(*icb_loc));
4128 dirh_ep = NULL;
4129 for (;;) {
4130 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
4131 /* if no hit, abort the search */
4132 if (!hit)
4133 break;
4134
4135 /* check this hit */
4136 diroffset = dirh_ep->offset;
4137
4138 /* transfer a new fid/dirent */
4139 error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
4140 if (error)
4141 break;
4142
4143 DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n",
4144 dirent->d_namlen, dirent->d_namlen, dirent->d_name));
4145
4146 /* see if its our entry */
4147 KASSERT(dirent->d_namlen == namelen);
4148 if (strncmp(dirent->d_name, name, namelen) == 0) {
4149 *found = 1;
4150 *icb_loc = fid->icb;
4151 break;
4152 }
4153 }
4154 free(fid, M_UDFTEMP);
4155 free(dirent, M_UDFTEMP);
4156
4157 dirhash_put(dir_node->dir_hash);
4158
4159 return error;
4160 }
4161
4162 /* --------------------------------------------------------------------- */
4163
4164 static int
4165 udf_create_new_fe(struct udf_mount *ump, struct file_entry *fe, int file_type,
4166 struct long_ad *node_icb, struct long_ad *parent_icb,
4167 uint64_t parent_unique_id)
4168 {
4169 struct timespec now;
4170 struct icb_tag *icb;
4171 struct filetimes_extattr_entry *ft_extattr;
4172 uint64_t unique_id;
4173 uint32_t fidsize, lb_num;
4174 uint8_t *bpos;
4175 int crclen, attrlen;
4176
4177 lb_num = udf_rw32(node_icb->loc.lb_num);
4178 udf_inittag(ump, &fe->tag, TAGID_FENTRY, lb_num);
4179 icb = &fe->icbtag;
4180
4181 /*
4182 * Always use strategy type 4 unless on WORM wich we don't support
4183 * (yet). Fill in defaults and set for internal allocation of data.
4184 */
4185 icb->strat_type = udf_rw16(4);
4186 icb->max_num_entries = udf_rw16(1);
4187 icb->file_type = file_type; /* 8 bit */
4188 icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC);
4189
4190 fe->perm = udf_rw32(0x7fff); /* all is allowed */
4191 fe->link_cnt = udf_rw16(0); /* explicit setting */
4192
4193 fe->ckpoint = udf_rw32(1); /* user supplied file version */
4194
4195 vfs_timestamp(&now);
4196 udf_timespec_to_timestamp(&now, &fe->atime);
4197 udf_timespec_to_timestamp(&now, &fe->attrtime);
4198 udf_timespec_to_timestamp(&now, &fe->mtime);
4199
4200 udf_set_regid(&fe->imp_id, IMPL_NAME);
4201 udf_add_impl_regid(ump, &fe->imp_id);
4202
4203 unique_id = udf_advance_uniqueid(ump);
4204 fe->unique_id = udf_rw64(unique_id);
4205 fe->l_ea = udf_rw32(0);
4206
4207 /* create extended attribute to record our creation time */
4208 attrlen = UDF_FILETIMES_ATTR_SIZE(1);
4209 ft_extattr = malloc(attrlen, M_UDFTEMP, M_WAITOK);
4210 memset(ft_extattr, 0, attrlen);
4211 ft_extattr->hdr.type = udf_rw32(UDF_FILETIMES_ATTR_NO);
4212 ft_extattr->hdr.subtype = 1; /* [4/48.10.5] */
4213 ft_extattr->hdr.a_l = udf_rw32(UDF_FILETIMES_ATTR_SIZE(1));
4214 ft_extattr->d_l = udf_rw32(UDF_TIMESTAMP_SIZE); /* one item */
4215 ft_extattr->existence = UDF_FILETIMES_FILE_CREATION;
4216 udf_timespec_to_timestamp(&now, &ft_extattr->times[0]);
4217
4218 udf_extattr_insert_internal(ump, (union dscrptr *) fe,
4219 (struct extattr_entry *) ft_extattr);
4220 free(ft_extattr, M_UDFTEMP);
4221
4222 /* if its a directory, create '..' */
4223 bpos = (uint8_t *) fe->data + udf_rw32(fe->l_ea);
4224 fidsize = 0;
4225 if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
4226 fidsize = udf_create_parentfid(ump,
4227 (struct fileid_desc *) bpos, parent_icb,
4228 parent_unique_id);
4229 }
4230
4231 /* record fidlength information */
4232 fe->inf_len = udf_rw64(fidsize);
4233 fe->l_ad = udf_rw32(fidsize);
4234 fe->logblks_rec = udf_rw64(0); /* intern */
4235
4236 crclen = sizeof(struct file_entry) - 1 - UDF_DESC_TAG_LENGTH;
4237 crclen += udf_rw32(fe->l_ea) + fidsize;
4238 fe->tag.desc_crc_len = udf_rw16(crclen);
4239
4240 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fe);
4241
4242 return fidsize;
4243 }
4244
4245 /* --------------------------------------------------------------------- */
4246
4247 static int
4248 udf_create_new_efe(struct udf_mount *ump, struct extfile_entry *efe,
4249 int file_type, struct long_ad *node_icb, struct long_ad *parent_icb,
4250 uint64_t parent_unique_id)
4251 {
4252 struct timespec now;
4253 struct icb_tag *icb;
4254 uint64_t unique_id;
4255 uint32_t fidsize, lb_num;
4256 uint8_t *bpos;
4257 int crclen;
4258
4259 lb_num = udf_rw32(node_icb->loc.lb_num);
4260 udf_inittag(ump, &efe->tag, TAGID_EXTFENTRY, lb_num);
4261 icb = &efe->icbtag;
4262
4263 /*
4264 * Always use strategy type 4 unless on WORM wich we don't support
4265 * (yet). Fill in defaults and set for internal allocation of data.
4266 */
4267 icb->strat_type = udf_rw16(4);
4268 icb->max_num_entries = udf_rw16(1);
4269 icb->file_type = file_type; /* 8 bit */
4270 icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC);
4271
4272 efe->perm = udf_rw32(0x7fff); /* all is allowed */
4273 efe->link_cnt = udf_rw16(0); /* explicit setting */
4274
4275 efe->ckpoint = udf_rw32(1); /* user supplied file version */
4276
4277 vfs_timestamp(&now);
4278 udf_timespec_to_timestamp(&now, &efe->ctime);
4279 udf_timespec_to_timestamp(&now, &efe->atime);
4280 udf_timespec_to_timestamp(&now, &efe->attrtime);
4281 udf_timespec_to_timestamp(&now, &efe->mtime);
4282
4283 udf_set_regid(&efe->imp_id, IMPL_NAME);
4284 udf_add_impl_regid(ump, &efe->imp_id);
4285
4286 unique_id = udf_advance_uniqueid(ump);
4287 efe->unique_id = udf_rw64(unique_id);
4288 efe->l_ea = udf_rw32(0);
4289
4290 /* if its a directory, create '..' */
4291 bpos = (uint8_t *) efe->data + udf_rw32(efe->l_ea);
4292 fidsize = 0;
4293 if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
4294 fidsize = udf_create_parentfid(ump,
4295 (struct fileid_desc *) bpos, parent_icb,
4296 parent_unique_id);
4297 }
4298
4299 /* record fidlength information */
4300 efe->obj_size = udf_rw64(fidsize);
4301 efe->inf_len = udf_rw64(fidsize);
4302 efe->l_ad = udf_rw32(fidsize);
4303 efe->logblks_rec = udf_rw64(0); /* intern */
4304
4305 crclen = sizeof(struct extfile_entry) - 1 - UDF_DESC_TAG_LENGTH;
4306 crclen += udf_rw32(efe->l_ea) + fidsize;
4307 efe->tag.desc_crc_len = udf_rw16(crclen);
4308
4309 (void) udf_validate_tag_and_crc_sums((union dscrptr *) efe);
4310
4311 return fidsize;
4312 }
4313
4314 /* --------------------------------------------------------------------- */
4315
4316 int
4317 udf_dir_detach(struct udf_mount *ump, struct udf_node *dir_node,
4318 struct udf_node *udf_node, struct componentname *cnp)
4319 {
4320 struct vnode *dvp = dir_node->vnode;
4321 struct dirhash *dirh;
4322 struct dirhash_entry *dirh_ep;
4323 struct file_entry *fe = dir_node->fe;
4324 struct extfile_entry *efe = dir_node->efe;
4325 struct fileid_desc *fid;
4326 struct dirent *dirent;
4327 uint64_t file_size, diroffset;
4328 uint32_t lb_size, fidsize;
4329 int found, error;
4330 char const *name = cnp->cn_nameptr;
4331 int namelen = cnp->cn_namelen;
4332 int hit, refcnt;
4333
4334 /* get our dirhash and make sure its read in */
4335 dirhash_get(&dir_node->dir_hash);
4336 error = dirhash_fill(dir_node);
4337 if (error) {
4338 dirhash_put(dir_node->dir_hash);
4339 return error;
4340 }
4341 dirh = dir_node->dir_hash;
4342
4343 /* get directory filesize */
4344 if (fe) {
4345 file_size = udf_rw64(fe->inf_len);
4346 } else {
4347 assert(efe);
4348 file_size = udf_rw64(efe->inf_len);
4349 }
4350
4351 /* allocate temporary space for fid */
4352 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4353 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4354 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4355
4356 /* search our dirhash hits */
4357 found = 0;
4358 dirh_ep = NULL;
4359 for (;;) {
4360 hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
4361 /* if no hit, abort the search */
4362 if (!hit)
4363 break;
4364
4365 /* check this hit */
4366 diroffset = dirh_ep->offset;
4367
4368 /* transfer a new fid/dirent */
4369 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
4370 if (error)
4371 break;
4372
4373 /* see if its our entry */
4374 KASSERT(dirent->d_namlen == namelen);
4375 if (strncmp(dirent->d_name, name, namelen) == 0) {
4376 found = 1;
4377 break;
4378 }
4379 }
4380
4381 if (!found)
4382 error = ENOENT;
4383 if (error)
4384 goto error_out;
4385
4386 /* mark deleted */
4387 fid->file_char |= UDF_FILE_CHAR_DEL;
4388 #ifdef UDF_COMPLETE_DELETE
4389 memset(&fid->icb, 0, sizeof(fid->icb));
4390 #endif
4391 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
4392
4393 /* get size of fid and compensate for the read_fid_stream advance */
4394 fidsize = udf_fidsize(fid);
4395 diroffset -= fidsize;
4396
4397 /* write out */
4398 error = vn_rdwr(UIO_WRITE, dir_node->vnode,
4399 fid, fidsize, diroffset,
4400 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
4401 FSCRED, NULL, NULL);
4402 if (error)
4403 goto error_out;
4404
4405 /* get reference count of attached node */
4406 if (udf_node->fe) {
4407 refcnt = udf_rw16(udf_node->fe->link_cnt);
4408 } else {
4409 KASSERT(udf_node->efe);
4410 refcnt = udf_rw16(udf_node->efe->link_cnt);
4411 }
4412 #ifdef UDF_COMPLETE_DELETE
4413 /* substract reference counter in attached node */
4414 refcnt -= 1;
4415 if (udf_node->fe) {
4416 udf_node->fe->link_cnt = udf_rw16(refcnt);
4417 } else {
4418 udf_node->efe->link_cnt = udf_rw16(refcnt);
4419 }
4420
4421 /* prevent writeout when refcnt == 0 */
4422 if (refcnt == 0)
4423 udf_node->i_flags |= IN_DELETED;
4424
4425 if (fid->file_char & UDF_FILE_CHAR_DIR) {
4426 int drefcnt;
4427
4428 /* substract reference counter in directory node */
4429 /* note subtract 2 (?) for its was also backreferenced */
4430 if (dir_node->fe) {
4431 drefcnt = udf_rw16(dir_node->fe->link_cnt);
4432 drefcnt -= 1;
4433 dir_node->fe->link_cnt = udf_rw16(drefcnt);
4434 } else {
4435 KASSERT(dir_node->efe);
4436 drefcnt = udf_rw16(dir_node->efe->link_cnt);
4437 drefcnt -= 1;
4438 dir_node->efe->link_cnt = udf_rw16(drefcnt);
4439 }
4440 }
4441
4442 udf_node->i_flags |= IN_MODIFIED;
4443 dir_node->i_flags |= IN_MODIFIED;
4444 #endif
4445 /* if it is/was a hardlink adjust the file count */
4446 if (refcnt > 0)
4447 udf_adjust_filecount(udf_node, -1);
4448
4449 /* remove from the dirhash */
4450 dirhash_remove(dirh, dirent, diroffset,
4451 udf_fidsize(fid));
4452
4453 error_out:
4454 free(fid, M_UDFTEMP);
4455 free(dirent, M_UDFTEMP);
4456
4457 dirhash_put(dir_node->dir_hash);
4458
4459 return error;
4460 }
4461
4462 /* --------------------------------------------------------------------- */
4463
4464 /*
4465 * We are not allowed to split the fid tag itself over an logical block so
4466 * check the space remaining in the logical block.
4467 *
4468 * We try to select the smallest candidate for recycling or when none is
4469 * found, append a new one at the end of the directory.
4470 */
4471
4472 int
4473 udf_dir_attach(struct udf_mount *ump, struct udf_node *dir_node,
4474 struct udf_node *udf_node, struct vattr *vap, struct componentname *cnp)
4475 {
4476 struct vnode *dvp = dir_node->vnode;
4477 struct dirhash *dirh;
4478 struct dirhash_entry *dirh_ep;
4479 struct fileid_desc *fid;
4480 struct icb_tag *icbtag;
4481 struct charspec osta_charspec;
4482 struct dirent dirent;
4483 uint64_t unique_id, dir_size;
4484 uint64_t fid_pos, end_fid_pos, chosen_fid_pos;
4485 uint32_t chosen_size, chosen_size_diff;
4486 int lb_size, lb_rest, fidsize, this_fidsize, size_diff;
4487 int file_char, refcnt, icbflags, addr_type, hit, error;
4488
4489 /* get our dirhash and make sure its read in */
4490 dirhash_get(&dir_node->dir_hash);
4491 error = dirhash_fill(dir_node);
4492 if (error) {
4493 dirhash_put(dir_node->dir_hash);
4494 return error;
4495 }
4496 dirh = dir_node->dir_hash;
4497
4498 /* get info */
4499 lb_size = udf_rw32(ump->logical_vol->lb_size);
4500 udf_osta_charset(&osta_charspec);
4501
4502 if (dir_node->fe) {
4503 dir_size = udf_rw64(dir_node->fe->inf_len);
4504 icbtag = &dir_node->fe->icbtag;
4505 } else {
4506 dir_size = udf_rw64(dir_node->efe->inf_len);
4507 icbtag = &dir_node->efe->icbtag;
4508 }
4509
4510 icbflags = udf_rw16(icbtag->flags);
4511 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
4512
4513 if (udf_node->fe) {
4514 unique_id = udf_rw64(udf_node->fe->unique_id);
4515 refcnt = udf_rw16(udf_node->fe->link_cnt);
4516 } else {
4517 unique_id = udf_rw64(udf_node->efe->unique_id);
4518 refcnt = udf_rw16(udf_node->efe->link_cnt);
4519 }
4520
4521 if (refcnt > 0) {
4522 unique_id = udf_advance_uniqueid(ump);
4523 udf_adjust_filecount(udf_node, 1);
4524 }
4525
4526 /* determine file characteristics */
4527 file_char = 0; /* visible non deleted file and not stream metadata */
4528 if (vap->va_type == VDIR)
4529 file_char = UDF_FILE_CHAR_DIR;
4530
4531 /* malloc scrap buffer */
4532 fid = malloc(lb_size, M_TEMP, M_WAITOK);
4533 bzero(fid, lb_size);
4534
4535 /* calculate _minimum_ fid size */
4536 unix_to_udf_name((char *) fid->data, &fid->l_fi,
4537 cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
4538 fidsize = UDF_FID_SIZE + fid->l_fi;
4539 fidsize = (fidsize + 3) & ~3; /* multiple of 4 */
4540
4541 /* find position that will fit the FID */
4542 chosen_fid_pos = dir_size;
4543 chosen_size = 0;
4544 chosen_size_diff = UINT_MAX;
4545
4546 /* shut up gcc */
4547 dirent.d_namlen = 0;
4548
4549 /* search our dirhash hits */
4550 error = 0;
4551 dirh_ep = NULL;
4552 for (;;) {
4553 hit = dirhash_lookup_freed(dirh, fidsize, &dirh_ep);
4554 /* if no hit, abort the search */
4555 if (!hit)
4556 break;
4557
4558 /* check this hit for size */
4559 this_fidsize = dirh_ep->entry_size;
4560
4561 /* check this hit */
4562 fid_pos = dirh_ep->offset;
4563 end_fid_pos = fid_pos + this_fidsize;
4564 size_diff = this_fidsize - fidsize;
4565 lb_rest = lb_size - (end_fid_pos % lb_size);
4566
4567 #ifndef UDF_COMPLETE_DELETE
4568 /* transfer a new fid/dirent */
4569 error = udf_read_fid_stream(vp, &fid_pos, fid, dirent);
4570 if (error)
4571 goto error_out;
4572
4573 /* only reuse entries that are wiped */
4574 /* check if the len + loc are marked zero */
4575 if (udf_rw32(fid->icb.len != 0))
4576 continue;
4577 if (udf_rw32(fid->icb.loc.lb_num) != 0)
4578 continue;
4579 if (udf_rw16(fid->icb.loc.part_num != 0))
4580 continue;
4581 #endif /* UDF_COMPLETE_DELETE */
4582
4583 /* select if not splitting the tag and its smaller */
4584 if ((size_diff >= 0) &&
4585 (size_diff < chosen_size_diff) &&
4586 (lb_rest >= sizeof(struct desc_tag)))
4587 {
4588 /* UDF 2.3.4.2+3 specifies rules for iu size */
4589 if ((size_diff == 0) || (size_diff >= 32)) {
4590 chosen_fid_pos = fid_pos;
4591 chosen_size = this_fidsize;
4592 chosen_size_diff = size_diff;
4593 }
4594 }
4595 }
4596
4597
4598 /* extend directory if no other candidate found */
4599 if (chosen_size == 0) {
4600 chosen_fid_pos = dir_size;
4601 chosen_size = fidsize;
4602 chosen_size_diff = 0;
4603
4604 /* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */
4605 if (addr_type == UDF_ICB_INTERN_ALLOC) {
4606 /* pre-grow directory to see if we're to switch */
4607 udf_grow_node(dir_node, dir_size + chosen_size);
4608
4609 icbflags = udf_rw16(icbtag->flags);
4610 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
4611 }
4612
4613 /* make sure the next fid desc_tag won't be splitted */
4614 if (addr_type != UDF_ICB_INTERN_ALLOC) {
4615 end_fid_pos = chosen_fid_pos + chosen_size;
4616 lb_rest = lb_size - (end_fid_pos % lb_size);
4617
4618 /* pad with implementation use regid if needed */
4619 if (lb_rest < sizeof(struct desc_tag))
4620 chosen_size += 32;
4621 }
4622 }
4623 chosen_size_diff = chosen_size - fidsize;
4624
4625 /* populate the FID */
4626 memset(fid, 0, lb_size);
4627 udf_inittag(ump, &fid->tag, TAGID_FID, 0);
4628 fid->file_version_num = udf_rw16(1); /* UDF 2.3.4.1 */
4629 fid->file_char = file_char;
4630 fid->icb = udf_node->loc;
4631 fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
4632 fid->l_iu = udf_rw16(0);
4633
4634 if (chosen_size > fidsize) {
4635 /* insert implementation-use regid to space it correctly */
4636 fid->l_iu = udf_rw16(chosen_size_diff);
4637
4638 /* set implementation use */
4639 udf_set_regid((struct regid *) fid->data, IMPL_NAME);
4640 udf_add_impl_regid(ump, (struct regid *) fid->data);
4641 }
4642
4643 /* fill in name */
4644 unix_to_udf_name((char *) fid->data + udf_rw16(fid->l_iu),
4645 &fid->l_fi, cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
4646
4647 fid->tag.desc_crc_len = chosen_size - UDF_DESC_TAG_LENGTH;
4648 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
4649
4650 /* writeout FID/update parent directory */
4651 error = vn_rdwr(UIO_WRITE, dvp,
4652 fid, chosen_size, chosen_fid_pos,
4653 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
4654 FSCRED, NULL, NULL);
4655
4656 if (error)
4657 goto error_out;
4658
4659 /* add reference counter in attached node */
4660 if (udf_node->fe) {
4661 refcnt = udf_rw16(udf_node->fe->link_cnt);
4662 udf_node->fe->link_cnt = udf_rw16(refcnt+1);
4663 } else {
4664 KASSERT(udf_node->efe);
4665 refcnt = udf_rw16(udf_node->efe->link_cnt);
4666 udf_node->efe->link_cnt = udf_rw16(refcnt+1);
4667 }
4668
4669 /* mark not deleted if it was... just in case, but do warn */
4670 if (udf_node->i_flags & IN_DELETED) {
4671 printf("udf: warning, marking a file undeleted\n");
4672 udf_node->i_flags &= ~IN_DELETED;
4673 }
4674
4675 if (file_char & UDF_FILE_CHAR_DIR) {
4676 /* add reference counter in directory node for '..' */
4677 if (dir_node->fe) {
4678 refcnt = udf_rw16(dir_node->fe->link_cnt);
4679 refcnt++;
4680 dir_node->fe->link_cnt = udf_rw16(refcnt);
4681 } else {
4682 KASSERT(dir_node->efe);
4683 refcnt = udf_rw16(dir_node->efe->link_cnt);
4684 refcnt++;
4685 dir_node->efe->link_cnt = udf_rw16(refcnt);
4686 }
4687 }
4688
4689 /* append to the dirhash */
4690 dirent.d_namlen = cnp->cn_namelen;
4691 memcpy(dirent.d_name, cnp->cn_nameptr, cnp->cn_namelen);
4692 dirhash_enter(dirh, &dirent, chosen_fid_pos,
4693 udf_fidsize(fid), 1);
4694
4695 /* note updates */
4696 udf_node->i_flags |= IN_CHANGE | IN_MODIFY; /* | IN_CREATE? */
4697 /* VN_KNOTE(udf_node, ...) */
4698 udf_update(udf_node->vnode, NULL, NULL, NULL, 0);
4699
4700 error_out:
4701 free(fid, M_TEMP);
4702
4703 dirhash_put(dir_node->dir_hash);
4704
4705 return error;
4706 }
4707
4708 /* --------------------------------------------------------------------- */
4709
4710 /*
4711 * Each node can have an attached streamdir node though not recursively. These
4712 * are otherwise known as named substreams/named extended attributes that have
4713 * no size limitations.
4714 *
4715 * `Normal' extended attributes are indicated with a number and are recorded
4716 * in either the fe/efe descriptor itself for small descriptors or recorded in
4717 * the attached extended attribute file. Since these spaces can get
4718 * fragmented, care ought to be taken.
4719 *
4720 * Since the size of the space reserved for allocation descriptors is limited,
4721 * there is a mechanim provided for extending this space; this is done by a
4722 * special extent to allow schrinking of the allocations without breaking the
4723 * linkage to the allocation extent descriptor.
4724 */
4725
4726 int
4727 udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
4728 struct udf_node **udf_noderes)
4729 {
4730 union dscrptr *dscr;
4731 struct udf_node *udf_node;
4732 struct vnode *nvp;
4733 struct long_ad icb_loc, last_fe_icb_loc;
4734 uint64_t file_size;
4735 uint32_t lb_size, sector, dummy;
4736 uint8_t *file_data;
4737 int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
4738 int slot, eof, error;
4739
4740 DPRINTF(NODE, ("udf_get_node called\n"));
4741 *udf_noderes = udf_node = NULL;
4742
4743 /* lock to disallow simultanious creation of same udf_node */
4744 mutex_enter(&ump->get_node_lock);
4745
4746 DPRINTF(NODE, ("\tlookup in hash table\n"));
4747 /* lookup in hash table */
4748 assert(ump);
4749 assert(node_icb_loc);
4750 udf_node = udf_hash_lookup(ump, node_icb_loc);
4751 if (udf_node) {
4752 DPRINTF(NODE, ("\tgot it from the hash!\n"));
4753 /* vnode is returned locked */
4754 *udf_noderes = udf_node;
4755 mutex_exit(&ump->get_node_lock);
4756 return 0;
4757 }
4758
4759 /* garbage check: translate udf_node_icb_loc to sectornr */
4760 error = udf_translate_vtop(ump, node_icb_loc, §or, &dummy);
4761 if (error) {
4762 /* no use, this will fail anyway */
4763 mutex_exit(&ump->get_node_lock);
4764 return EINVAL;
4765 }
4766
4767 /* build udf_node (do initialise!) */
4768 udf_node = pool_get(&udf_node_pool, PR_WAITOK);
4769 memset(udf_node, 0, sizeof(struct udf_node));
4770
4771 DPRINTF(NODE, ("\tget new vnode\n"));
4772 /* give it a vnode */
4773 error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp);
4774 if (error) {
4775 pool_put(&udf_node_pool, udf_node);
4776 mutex_exit(&ump->get_node_lock);
4777 return error;
4778 }
4779
4780 /* always return locked vnode */
4781 if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) {
4782 /* recycle vnode and unlock; simultanious will fail too */
4783 ungetnewvnode(nvp);
4784 mutex_exit(&ump->get_node_lock);
4785 return error;
4786 }
4787
4788 /* initialise crosslinks, note location of fe/efe for hashing */
4789 udf_node->ump = ump;
4790 udf_node->vnode = nvp;
4791 nvp->v_data = udf_node;
4792 udf_node->loc = *node_icb_loc;
4793 udf_node->lockf = 0;
4794 mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
4795 cv_init(&udf_node->node_lock, "udf_nlk");
4796 genfs_node_init(nvp, &udf_genfsops); /* inititise genfs */
4797 udf_node->outstanding_bufs = 0;
4798 udf_node->outstanding_nodedscr = 0;
4799
4800 /* insert into the hash lookup */
4801 udf_register_node(udf_node);
4802
4803 /* safe to unlock, the entry is in the hash table, vnode is locked */
4804 mutex_exit(&ump->get_node_lock);
4805
4806 icb_loc = *node_icb_loc;
4807 needs_indirect = 0;
4808 strat4096 = 0;
4809 udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
4810 file_size = 0;
4811 file_data = NULL;
4812 lb_size = udf_rw32(ump->logical_vol->lb_size);
4813
4814 DPRINTF(NODE, ("\tstart reading descriptors\n"));
4815 do {
4816 /* try to read in fe/efe */
4817 error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
4818
4819 /* blank sector marks end of sequence, check this */
4820 if ((dscr == NULL) && (!strat4096))
4821 error = ENOENT;
4822
4823 /* break if read error or blank sector */
4824 if (error || (dscr == NULL))
4825 break;
4826
4827 /* process descriptor based on the descriptor type */
4828 dscr_type = udf_rw16(dscr->tag.id);
4829 DPRINTF(NODE, ("\tread descriptor %d\n", dscr_type));
4830
4831 /* if dealing with an indirect entry, follow the link */
4832 if (dscr_type == TAGID_INDIRECTENTRY) {
4833 needs_indirect = 0;
4834 udf_free_logvol_dscr(ump, &icb_loc, dscr);
4835 icb_loc = dscr->inde.indirect_icb;
4836 continue;
4837 }
4838
4839 /* only file entries and extended file entries allowed here */
4840 if ((dscr_type != TAGID_FENTRY) &&
4841 (dscr_type != TAGID_EXTFENTRY)) {
4842 udf_free_logvol_dscr(ump, &icb_loc, dscr);
4843 error = ENOENT;
4844 break;
4845 }
4846
4847 KASSERT(udf_tagsize(dscr, lb_size) == lb_size);
4848
4849 /* choose this one */
4850 last_fe_icb_loc = icb_loc;
4851
4852 /* record and process/update (ext)fentry */
4853 file_data = NULL;
4854 if (dscr_type == TAGID_FENTRY) {
4855 if (udf_node->fe)
4856 udf_free_logvol_dscr(ump, &last_fe_icb_loc,
4857 udf_node->fe);
4858 udf_node->fe = &dscr->fe;
4859 strat = udf_rw16(udf_node->fe->icbtag.strat_type);
4860 udf_file_type = udf_node->fe->icbtag.file_type;
4861 file_size = udf_rw64(udf_node->fe->inf_len);
4862 file_data = udf_node->fe->data;
4863 } else {
4864 if (udf_node->efe)
4865 udf_free_logvol_dscr(ump, &last_fe_icb_loc,
4866 udf_node->efe);
4867 udf_node->efe = &dscr->efe;
4868 strat = udf_rw16(udf_node->efe->icbtag.strat_type);
4869 udf_file_type = udf_node->efe->icbtag.file_type;
4870 file_size = udf_rw64(udf_node->efe->inf_len);
4871 file_data = udf_node->efe->data;
4872 }
4873
4874 /* check recording strategy (structure) */
4875
4876 /*
4877 * Strategy 4096 is a daisy linked chain terminating with an
4878 * unrecorded sector or a TERM descriptor. The next
4879 * descriptor is to be found in the sector that follows the
4880 * current sector.
4881 */
4882 if (strat == 4096) {
4883 strat4096 = 1;
4884 needs_indirect = 1;
4885
4886 icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
4887 }
4888
4889 /*
4890 * Strategy 4 is the normal strategy and terminates, but if
4891 * we're in strategy 4096, we can't have strategy 4 mixed in
4892 */
4893
4894 if (strat == 4) {
4895 if (strat4096) {
4896 error = EINVAL;
4897 break;
4898 }
4899 break; /* done */
4900 }
4901 } while (!error);
4902
4903 /* first round of cleanup code */
4904 if (error) {
4905 DPRINTF(NODE, ("\tnode fe/efe failed!\n"));
4906 /* recycle udf_node */
4907 udf_dispose_node(udf_node);
4908
4909 vlockmgr(nvp->v_vnlock, LK_RELEASE);
4910 nvp->v_data = NULL;
4911 ungetnewvnode(nvp);
4912
4913 return EINVAL; /* error code ok? */
4914 }
4915 DPRINTF(NODE, ("\tnode fe/efe read in fine\n"));
4916
4917 /* assert no references to dscr anymore beyong this point */
4918 assert((udf_node->fe) || (udf_node->efe));
4919 dscr = NULL;
4920
4921 /*
4922 * Remember where to record an updated version of the descriptor. If
4923 * there is a sequence of indirect entries, icb_loc will have been
4924 * updated. Its the write disipline to allocate new space and to make
4925 * sure the chain is maintained.
4926 *
4927 * `needs_indirect' flags if the next location is to be filled with
4928 * with an indirect entry.
4929 */
4930 udf_node->write_loc = icb_loc;
4931 udf_node->needs_indirect = needs_indirect;
4932
4933 /*
4934 * Go trough all allocations extents of this descriptor and when
4935 * encountering a redirect read in the allocation extension. These are
4936 * daisy-chained.
4937 */
4938 UDF_LOCK_NODE(udf_node, 0);
4939 udf_node->num_extensions = 0;
4940
4941 error = 0;
4942 slot = 0;
4943 for (;;) {
4944 udf_get_adslot(udf_node, slot, &icb_loc, &eof);
4945 DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
4946 "lb_num = %d, part = %d\n", slot, eof,
4947 UDF_EXT_FLAGS(udf_rw32(icb_loc.len)),
4948 UDF_EXT_LEN(udf_rw32(icb_loc.len)),
4949 udf_rw32(icb_loc.loc.lb_num),
4950 udf_rw16(icb_loc.loc.part_num)));
4951 if (eof)
4952 break;
4953 slot++;
4954
4955 if (UDF_EXT_FLAGS(udf_rw32(icb_loc.len)) != UDF_EXT_REDIRECT)
4956 continue;
4957
4958 DPRINTF(NODE, ("\tgot redirect extent\n"));
4959 if (udf_node->num_extensions >= UDF_MAX_ALLOC_EXTENTS) {
4960 DPRINTF(ALLOC, ("udf_get_node: implementation limit, "
4961 "too many allocation extensions on "
4962 "udf_node\n"));
4963 error = EINVAL;
4964 break;
4965 }
4966
4967 /* length can only be *one* lb : UDF 2.50/2.3.7.1 */
4968 if (UDF_EXT_LEN(udf_rw32(icb_loc.len)) != lb_size) {
4969 DPRINTF(ALLOC, ("udf_get_node: bad allocation "
4970 "extension size in udf_node\n"));
4971 error = EINVAL;
4972 break;
4973 }
4974
4975 DPRINTF(NODE, ("read allocation extent at lb_num %d\n",
4976 UDF_EXT_LEN(udf_rw32(icb_loc.loc.lb_num))));
4977 /* load in allocation extent */
4978 error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
4979 if (error || (dscr == NULL))
4980 break;
4981
4982 /* process read-in descriptor */
4983 dscr_type = udf_rw16(dscr->tag.id);
4984
4985 if (dscr_type != TAGID_ALLOCEXTENT) {
4986 udf_free_logvol_dscr(ump, &icb_loc, dscr);
4987 error = ENOENT;
4988 break;
4989 }
4990
4991 DPRINTF(NODE, ("\trecording redirect extent\n"));
4992 udf_node->ext[udf_node->num_extensions] = &dscr->aee;
4993 udf_node->ext_loc[udf_node->num_extensions] = icb_loc;
4994
4995 udf_node->num_extensions++;
4996
4997 } /* while */
4998 UDF_UNLOCK_NODE(udf_node, 0);
4999
5000 /* second round of cleanup code */
5001 if (error) {
5002 /* recycle udf_node */
5003 udf_dispose_node(udf_node);
5004
5005 vlockmgr(nvp->v_vnlock, LK_RELEASE);
5006 nvp->v_data = NULL;
5007 ungetnewvnode(nvp);
5008
5009 return EINVAL; /* error code ok? */
5010 }
5011
5012 DPRINTF(NODE, ("\tnode read in fine\n"));
5013
5014 /*
5015 * Translate UDF filetypes into vnode types.
5016 *
5017 * Systemfiles like the meta main and mirror files are not treated as
5018 * normal files, so we type them as having no type. UDF dictates that
5019 * they are not allowed to be visible.
5020 */
5021
5022 switch (udf_file_type) {
5023 case UDF_ICB_FILETYPE_DIRECTORY :
5024 case UDF_ICB_FILETYPE_STREAMDIR :
5025 nvp->v_type = VDIR;
5026 break;
5027 case UDF_ICB_FILETYPE_BLOCKDEVICE :
5028 nvp->v_type = VBLK;
5029 break;
5030 case UDF_ICB_FILETYPE_CHARDEVICE :
5031 nvp->v_type = VCHR;
5032 break;
5033 case UDF_ICB_FILETYPE_SOCKET :
5034 nvp->v_type = VSOCK;
5035 break;
5036 case UDF_ICB_FILETYPE_FIFO :
5037 nvp->v_type = VFIFO;
5038 break;
5039 case UDF_ICB_FILETYPE_SYMLINK :
5040 nvp->v_type = VLNK;
5041 break;
5042 case UDF_ICB_FILETYPE_VAT :
5043 case UDF_ICB_FILETYPE_META_MAIN :
5044 case UDF_ICB_FILETYPE_META_MIRROR :
5045 nvp->v_type = VNON;
5046 break;
5047 case UDF_ICB_FILETYPE_RANDOMACCESS :
5048 case UDF_ICB_FILETYPE_REALTIME :
5049 nvp->v_type = VREG;
5050 break;
5051 default:
5052 /* YIKES, something else */
5053 nvp->v_type = VNON;
5054 }
5055
5056 /* TODO specfs, fifofs etc etc. vnops setting */
5057
5058 /* don't forget to set vnode's v_size */
5059 uvm_vnp_setsize(nvp, file_size);
5060
5061 /* TODO ext attr and streamdir udf_nodes */
5062
5063 *udf_noderes = udf_node;
5064
5065 return 0;
5066 }
5067
5068 /* --------------------------------------------------------------------- */
5069
5070 int
5071 udf_writeout_node(struct udf_node *udf_node, int waitfor)
5072 {
5073 union dscrptr *dscr;
5074 struct long_ad *loc;
5075 int extnr, flags, error;
5076
5077 DPRINTF(NODE, ("udf_writeout_node called\n"));
5078
5079 KASSERT(udf_node->outstanding_bufs == 0);
5080 KASSERT(udf_node->outstanding_nodedscr == 0);
5081
5082 KASSERT(LIST_EMPTY(&udf_node->vnode->v_dirtyblkhd));
5083
5084 if (udf_node->i_flags & IN_DELETED) {
5085 DPRINTF(NODE, ("\tnode deleted; not writing out\n"));
5086 return 0;
5087 }
5088
5089 /* lock node */
5090 flags = waitfor ? 0 : IN_CALLBACK_ULK;
5091 UDF_LOCK_NODE(udf_node, flags);
5092
5093 /* at least one descriptor writeout */
5094 udf_node->outstanding_nodedscr = 1;
5095
5096 /* we're going to write out the descriptor so clear the flags */
5097 udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED);
5098
5099 /* if we were rebuild, write out the allocation extents */
5100 if (udf_node->i_flags & IN_NODE_REBUILD) {
5101 /* mark outstanding node descriptors and issue them */
5102 udf_node->outstanding_nodedscr += udf_node->num_extensions;
5103 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
5104 loc = &udf_node->ext_loc[extnr];
5105 dscr = (union dscrptr *) udf_node->ext[extnr];
5106 error = udf_write_logvol_dscr(udf_node, dscr, loc, 0);
5107 if (error)
5108 return error;
5109 }
5110 /* mark allocation extents written out */
5111 udf_node->i_flags &= ~(IN_NODE_REBUILD);
5112 }
5113
5114 if (udf_node->fe) {
5115 KASSERT(udf_node->efe == NULL);
5116 dscr = (union dscrptr *) udf_node->fe;
5117 } else {
5118 KASSERT(udf_node->efe);
5119 KASSERT(udf_node->fe == NULL);
5120 dscr = (union dscrptr *) udf_node->efe;
5121 }
5122 KASSERT(dscr);
5123
5124 loc = &udf_node->write_loc;
5125 error = udf_write_logvol_dscr(udf_node, dscr, loc, waitfor);
5126 return error;
5127 }
5128
5129 /* --------------------------------------------------------------------- */
5130
5131 int
5132 udf_dispose_node(struct udf_node *udf_node)
5133 {
5134 struct vnode *vp;
5135 int extnr;
5136
5137 DPRINTF(NODE, ("udf_dispose_node called on node %p\n", udf_node));
5138 if (!udf_node) {
5139 DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
5140 return 0;
5141 }
5142
5143 vp = udf_node->vnode;
5144 #ifdef DIAGNOSTIC
5145 if (vp->v_numoutput)
5146 panic("disposing UDF node with pending I/O's, udf_node = %p, "
5147 "v_numoutput = %d", udf_node, vp->v_numoutput);
5148 #endif
5149
5150 /* wait until out of sync (just in case we happen to stumble over one */
5151 KASSERT(!mutex_owned(&mntvnode_lock));
5152 mutex_enter(&mntvnode_lock);
5153 while (udf_node->i_flags & IN_SYNCED) {
5154 cv_timedwait(&udf_node->ump->dirtynodes_cv, &mntvnode_lock,
5155 hz/16);
5156 }
5157 mutex_exit(&mntvnode_lock);
5158
5159 /* TODO extended attributes and streamdir */
5160
5161 /* remove dirhash if present */
5162 dirhash_purge(&udf_node->dir_hash);
5163
5164 /* remove from our hash lookup table */
5165 udf_deregister_node(udf_node);
5166
5167 /* destroy our lock */
5168 mutex_destroy(&udf_node->node_mutex);
5169 cv_destroy(&udf_node->node_lock);
5170
5171 /* dissociate our udf_node from the vnode */
5172 genfs_node_destroy(udf_node->vnode);
5173 vp->v_data = NULL;
5174
5175 /* free associated memory and the node itself */
5176 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
5177 udf_free_logvol_dscr(udf_node->ump, &udf_node->ext_loc[extnr],
5178 udf_node->ext[extnr]);
5179 udf_node->ext[extnr] = (void *) 0xdeadcccc;
5180 }
5181
5182 if (udf_node->fe)
5183 udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
5184 udf_node->fe);
5185 if (udf_node->efe)
5186 udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
5187 udf_node->efe);
5188
5189 udf_node->fe = (void *) 0xdeadaaaa;
5190 udf_node->efe = (void *) 0xdeadbbbb;
5191 udf_node->ump = (void *) 0xdeadbeef;
5192 pool_put(&udf_node_pool, udf_node);
5193
5194 return 0;
5195 }
5196
5197
5198
5199 /*
5200 * create a new node using the specified vnodeops, vap and cnp but with the
5201 * udf_file_type. This allows special files to be created. Use with care.
5202 */
5203
5204 static int
5205 udf_create_node_raw(struct vnode *dvp, struct vnode **vpp, int udf_file_type,
5206 int (**vnodeops)(void *), struct vattr *vap, struct componentname *cnp)
5207 {
5208 union dscrptr *dscr;
5209 struct udf_node *dir_node = VTOI(dvp);;
5210 struct udf_node *udf_node;
5211 struct udf_mount *ump = dir_node->ump;
5212 struct vnode *nvp;
5213 struct long_ad node_icb_loc;
5214 uint64_t parent_unique_id;
5215 uint64_t lmapping;
5216 uint32_t lb_size, lb_num;
5217 uint16_t vpart_num;
5218 uid_t uid;
5219 gid_t gid, parent_gid;
5220 int fid_size, error;
5221
5222 lb_size = udf_rw32(ump->logical_vol->lb_size);
5223 *vpp = NULL;
5224
5225 /* allocate vnode */
5226 error = getnewvnode(VT_UDF, ump->vfs_mountp, vnodeops, &nvp);
5227 if (error)
5228 return error;
5229
5230 /* lock node */
5231 error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
5232 if (error) {
5233 nvp->v_data = NULL;
5234 ungetnewvnode(nvp);
5235 return error;
5236 }
5237
5238 /* get disc allocation for one logical block */
5239 vpart_num = ump->node_part;
5240 error = udf_pre_allocate_space(ump, UDF_C_NODE, 1,
5241 vpart_num, &lmapping);
5242 lb_num = lmapping;
5243 if (error) {
5244 vlockmgr(nvp->v_vnlock, LK_RELEASE);
5245 ungetnewvnode(nvp);
5246 return error;
5247 }
5248
5249 /* initialise pointer to location */
5250 memset(&node_icb_loc, 0, sizeof(struct long_ad));
5251 node_icb_loc.len = lb_size;
5252 node_icb_loc.loc.lb_num = udf_rw32(lb_num);
5253 node_icb_loc.loc.part_num = udf_rw16(vpart_num);
5254
5255 /* build udf_node (do initialise!) */
5256 udf_node = pool_get(&udf_node_pool, PR_WAITOK);
5257 memset(udf_node, 0, sizeof(struct udf_node));
5258
5259 /* initialise crosslinks, note location of fe/efe for hashing */
5260 /* bugalert: synchronise with udf_get_node() */
5261 udf_node->ump = ump;
5262 udf_node->vnode = nvp;
5263 nvp->v_data = udf_node;
5264 udf_node->loc = node_icb_loc;
5265 udf_node->write_loc = node_icb_loc;
5266 udf_node->lockf = 0;
5267 mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
5268 cv_init(&udf_node->node_lock, "udf_nlk");
5269 udf_node->outstanding_bufs = 0;
5270 udf_node->outstanding_nodedscr = 0;
5271
5272 /* initialise genfs */
5273 genfs_node_init(nvp, &udf_genfsops);
5274
5275 /* insert into the hash lookup */
5276 udf_register_node(udf_node);
5277
5278 /* get parent's unique ID for refering '..' if its a directory */
5279 if (dir_node->fe) {
5280 parent_unique_id = udf_rw64(dir_node->fe->unique_id);
5281 parent_gid = (gid_t) udf_rw32(dir_node->fe->gid);
5282 } else {
5283 parent_unique_id = udf_rw64(dir_node->efe->unique_id);
5284 parent_gid = (gid_t) udf_rw32(dir_node->efe->gid);
5285 }
5286
5287 /* get descriptor */
5288 udf_create_logvol_dscr(ump, udf_node, &node_icb_loc, &dscr);
5289
5290 /* choose a fe or an efe for it */
5291 if (ump->logical_vol->tag.descriptor_ver == 2) {
5292 udf_node->fe = &dscr->fe;
5293 fid_size = udf_create_new_fe(ump, udf_node->fe,
5294 udf_file_type, &udf_node->loc,
5295 &dir_node->loc, parent_unique_id);
5296 /* TODO add extended attribute for creation time */
5297 } else {
5298 udf_node->efe = &dscr->efe;
5299 fid_size = udf_create_new_efe(ump, udf_node->efe,
5300 udf_file_type, &udf_node->loc,
5301 &dir_node->loc, parent_unique_id);
5302 }
5303 KASSERT(dscr->tag.tag_loc == udf_node->loc.loc.lb_num);
5304
5305 /* update vnode's size and type */
5306 nvp->v_type = vap->va_type;
5307 uvm_vnp_setsize(nvp, fid_size);
5308
5309 /* set access mode */
5310 udf_setaccessmode(udf_node, vap->va_mode);
5311
5312 /* set ownership */
5313 uid = kauth_cred_geteuid(cnp->cn_cred);
5314 gid = parent_gid;
5315 udf_setownership(udf_node, uid, gid);
5316
5317 error = udf_dir_attach(ump, dir_node, udf_node, vap, cnp);
5318 if (error) {
5319 /* free disc allocation for node */
5320 udf_free_allocated_space(ump, lb_num, vpart_num, 1);
5321
5322 /* recycle udf_node */
5323 udf_dispose_node(udf_node);
5324 vput(nvp);
5325
5326 *vpp = NULL;
5327 return error;
5328 }
5329
5330 /* adjust file count */
5331 udf_adjust_filecount(udf_node, 1);
5332
5333 /* return result */
5334 *vpp = nvp;
5335
5336 return 0;
5337 }
5338
5339
5340 int
5341 udf_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
5342 struct componentname *cnp)
5343 {
5344 int (**vnodeops)(void *);
5345 int udf_file_type;
5346
5347 DPRINTF(NODE, ("udf_create_node called\n"));
5348
5349 /* what type are we creating ? */
5350 vnodeops = udf_vnodeop_p;
5351 /* start with a default */
5352 udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
5353
5354 *vpp = NULL;
5355
5356 switch (vap->va_type) {
5357 case VREG :
5358 udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
5359 break;
5360 case VDIR :
5361 udf_file_type = UDF_ICB_FILETYPE_DIRECTORY;
5362 break;
5363 case VLNK :
5364 udf_file_type = UDF_ICB_FILETYPE_SYMLINK;
5365 break;
5366 case VBLK :
5367 udf_file_type = UDF_ICB_FILETYPE_BLOCKDEVICE;
5368 /* specfs */
5369 return ENOTSUP;
5370 break;
5371 case VCHR :
5372 udf_file_type = UDF_ICB_FILETYPE_CHARDEVICE;
5373 /* specfs */
5374 return ENOTSUP;
5375 break;
5376 case VFIFO :
5377 udf_file_type = UDF_ICB_FILETYPE_FIFO;
5378 /* specfs */
5379 return ENOTSUP;
5380 break;
5381 case VSOCK :
5382 udf_file_type = UDF_ICB_FILETYPE_SOCKET;
5383 /* specfs */
5384 return ENOTSUP;
5385 break;
5386 case VNON :
5387 case VBAD :
5388 default :
5389 /* nothing; can we even create these? */
5390 return EINVAL;
5391 }
5392
5393 return udf_create_node_raw(dvp, vpp, udf_file_type, vnodeops, vap, cnp);
5394 }
5395
5396 /* --------------------------------------------------------------------- */
5397
5398 static void
5399 udf_free_descriptor_space(struct udf_node *udf_node, struct long_ad *loc, void *mem)
5400 {
5401 struct udf_mount *ump = udf_node->ump;
5402 uint32_t lb_size, lb_num, len, num_lb;
5403 uint16_t vpart_num;
5404
5405 /* is there really one? */
5406 if (mem == NULL)
5407 return;
5408
5409 /* got a descriptor here */
5410 len = UDF_EXT_LEN(udf_rw32(loc->len));
5411 lb_num = udf_rw32(loc->loc.lb_num);
5412 vpart_num = udf_rw16(loc->loc.part_num);
5413
5414 lb_size = udf_rw32(ump->logical_vol->lb_size);
5415 num_lb = (len + lb_size -1) / lb_size;
5416
5417 udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
5418 }
5419
5420 void
5421 udf_delete_node(struct udf_node *udf_node)
5422 {
5423 void *dscr;
5424 struct udf_mount *ump;
5425 struct long_ad *loc;
5426 int extnr, lvint, dummy;
5427
5428 ump = udf_node->ump;
5429
5430 /* paranoia check on integrity; should be open!; we could panic */
5431 lvint = udf_rw32(udf_node->ump->logvol_integrity->integrity_type);
5432 if (lvint == UDF_INTEGRITY_CLOSED)
5433 printf("\tIntegrity was CLOSED!\n");
5434
5435 /* whatever the node type, change its size to zero */
5436 (void) udf_resize_node(udf_node, 0, &dummy);
5437
5438 /* force it to be `clean'; no use writing it out */
5439 udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED | IN_ACCESS |
5440 IN_CHANGE | IN_UPDATE | IN_MODIFY);
5441
5442 /* adjust file count */
5443 udf_adjust_filecount(udf_node, -1);
5444
5445 /*
5446 * Free its allocated descriptors; memory will be released when
5447 * vop_reclaim() is called.
5448 */
5449 loc = &udf_node->loc;
5450
5451 dscr = udf_node->fe;
5452 udf_free_descriptor_space(udf_node, loc, dscr);
5453 dscr = udf_node->efe;
5454 udf_free_descriptor_space(udf_node, loc, dscr);
5455
5456 for (extnr = 0; extnr < UDF_MAX_ALLOC_EXTENTS; extnr++) {
5457 dscr = udf_node->ext[extnr];
5458 loc = &udf_node->ext_loc[extnr];
5459 udf_free_descriptor_space(udf_node, loc, dscr);
5460 }
5461 }
5462
5463 /* --------------------------------------------------------------------- */
5464
5465 /* set new filesize; node but be LOCKED on entry and is locked on exit */
5466 int
5467 udf_resize_node(struct udf_node *udf_node, uint64_t new_size, int *extended)
5468 {
5469 struct file_entry *fe = udf_node->fe;
5470 struct extfile_entry *efe = udf_node->efe;
5471 uint64_t file_size;
5472 int error;
5473
5474 if (fe) {
5475 file_size = udf_rw64(fe->inf_len);
5476 } else {
5477 assert(udf_node->efe);
5478 file_size = udf_rw64(efe->inf_len);
5479 }
5480
5481 DPRINTF(ATTR, ("\tchanging file length from %"PRIu64" to %"PRIu64"\n",
5482 file_size, new_size));
5483
5484 /* if not changing, we're done */
5485 if (file_size == new_size)
5486 return 0;
5487
5488 *extended = (new_size > file_size);
5489 if (*extended) {
5490 error = udf_grow_node(udf_node, new_size);
5491 } else {
5492 error = udf_shrink_node(udf_node, new_size);
5493 }
5494
5495 return error;
5496 }
5497
5498
5499 /* --------------------------------------------------------------------- */
5500
5501 void
5502 udf_itimes(struct udf_node *udf_node, struct timespec *acc,
5503 struct timespec *mod, struct timespec *birth)
5504 {
5505 struct timespec now;
5506 struct file_entry *fe;
5507 struct extfile_entry *efe;
5508 struct filetimes_extattr_entry *ft_extattr;
5509 struct timestamp *atime, *mtime, *attrtime, *ctime;
5510 struct timestamp fe_ctime;
5511 struct timespec cur_birth;
5512 uint32_t offset, a_l;
5513 uint8_t *filedata;
5514 int error;
5515
5516 /* protect against rogue values */
5517 if (!udf_node)
5518 return;
5519
5520 fe = udf_node->fe;
5521 efe = udf_node->efe;
5522
5523 if (!(udf_node->i_flags & (IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY)))
5524 return;
5525
5526 /* get descriptor information */
5527 if (fe) {
5528 atime = &fe->atime;
5529 mtime = &fe->mtime;
5530 attrtime = &fe->attrtime;
5531 filedata = fe->data;
5532
5533 /* initial save dummy setting */
5534 ctime = &fe_ctime;
5535
5536 /* check our extended attribute if present */
5537 error = udf_extattr_search_intern(udf_node,
5538 UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
5539 if (!error) {
5540 ft_extattr = (struct filetimes_extattr_entry *)
5541 (filedata + offset);
5542 if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
5543 ctime = &ft_extattr->times[0];
5544 }
5545 /* TODO create the extended attribute if not found ? */
5546 } else {
5547 assert(udf_node->efe);
5548 atime = &efe->atime;
5549 mtime = &efe->mtime;
5550 attrtime = &efe->attrtime;
5551 ctime = &efe->ctime;
5552 }
5553
5554 vfs_timestamp(&now);
5555
5556 /* set access time */
5557 if (udf_node->i_flags & IN_ACCESS) {
5558 if (acc == NULL)
5559 acc = &now;
5560 udf_timespec_to_timestamp(acc, atime);
5561 }
5562
5563 /* set modification time */
5564 if (udf_node->i_flags & (IN_UPDATE | IN_MODIFY)) {
5565 if (mod == NULL)
5566 mod = &now;
5567 udf_timespec_to_timestamp(mod, mtime);
5568
5569 /* ensure birthtime is older than set modification! */
5570 udf_timestamp_to_timespec(udf_node->ump, ctime, &cur_birth);
5571 if ((cur_birth.tv_sec > mod->tv_sec) ||
5572 ((cur_birth.tv_sec == mod->tv_sec) &&
5573 (cur_birth.tv_nsec > mod->tv_nsec))) {
5574 udf_timespec_to_timestamp(mod, ctime);
5575 }
5576 }
5577
5578 /* update birthtime if specified */
5579 /* XXX we asume here that given birthtime is older than mod */
5580 if (birth && (birth->tv_sec != VNOVAL)) {
5581 udf_timespec_to_timestamp(birth, ctime);
5582 }
5583
5584 /* set change time */
5585 if (udf_node->i_flags & (IN_CHANGE | IN_MODIFY))
5586 udf_timespec_to_timestamp(&now, attrtime);
5587
5588 /* notify updates to the node itself */
5589 if (udf_node->i_flags & (IN_ACCESS | IN_MODIFY))
5590 udf_node->i_flags |= IN_ACCESSED;
5591 if (udf_node->i_flags & (IN_UPDATE | IN_CHANGE))
5592 udf_node->i_flags |= IN_MODIFIED;
5593
5594 /* clear modification flags */
5595 udf_node->i_flags &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
5596 }
5597
5598 /* --------------------------------------------------------------------- */
5599
5600 int
5601 udf_update(struct vnode *vp, struct timespec *acc,
5602 struct timespec *mod, struct timespec *birth, int updflags)
5603 {
5604 union dscrptr *dscrptr;
5605 struct udf_node *udf_node = VTOI(vp);
5606 struct udf_mount *ump = udf_node->ump;
5607 struct regid *impl_id;
5608 int mnt_async = (vp->v_mount->mnt_flag & MNT_ASYNC);
5609 int waitfor, flags;
5610
5611 #ifdef DEBUG
5612 char bits[128];
5613 DPRINTF(CALL, ("udf_update(node, %p, %p, %p, %d)\n", acc, mod, birth,
5614 updflags));
5615 bitmask_snprintf(udf_node->i_flags, IN_FLAGBITS, bits, sizeof(bits));
5616 DPRINTF(CALL, ("\tnode flags %s\n", bits));
5617 DPRINTF(CALL, ("\t\tmnt_async = %d\n", mnt_async));
5618 #endif
5619
5620 /* set our times */
5621 udf_itimes(udf_node, acc, mod, birth);
5622
5623 /* set our implementation id */
5624 if (udf_node->fe) {
5625 dscrptr = (union dscrptr *) udf_node->fe;
5626 impl_id = &udf_node->fe->imp_id;
5627 } else {
5628 dscrptr = (union dscrptr *) udf_node->efe;
5629 impl_id = &udf_node->efe->imp_id;
5630 }
5631
5632 /* set our ID */
5633 udf_set_regid(impl_id, IMPL_NAME);
5634 udf_add_impl_regid(ump, impl_id);
5635
5636 /* update our crc! on RMW we are not allowed to change a thing */
5637 udf_validate_tag_and_crc_sums(dscrptr);
5638
5639 /* if called when mounted readonly, never write back */
5640 if (vp->v_mount->mnt_flag & MNT_RDONLY)
5641 return 0;
5642
5643 /* check if the node is dirty 'enough'*/
5644 if (updflags & UPDATE_CLOSE) {
5645 flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
5646 } else {
5647 flags = udf_node->i_flags & IN_MODIFIED;
5648 }
5649 if (flags == 0)
5650 return 0;
5651
5652 /* determine if we need to write sync or async */
5653 waitfor = 0;
5654 if ((flags & IN_MODIFIED) && (mnt_async == 0)) {
5655 /* sync mounted */
5656 waitfor = updflags & UPDATE_WAIT;
5657 if (updflags & UPDATE_DIROP)
5658 waitfor |= UPDATE_WAIT;
5659 }
5660 if (waitfor)
5661 return VOP_FSYNC(vp, FSCRED, FSYNC_WAIT, 0,0);
5662
5663 return 0;
5664 }
5665
5666
5667 /* --------------------------------------------------------------------- */
5668
5669
5670 /*
5671 * Read one fid and process it into a dirent and advance to the next (*fid)
5672 * has to be allocated a logical block in size, (*dirent) struct dirent length
5673 */
5674
5675 int
5676 udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
5677 struct fileid_desc *fid, struct dirent *dirent)
5678 {
5679 struct udf_node *dir_node = VTOI(vp);
5680 struct udf_mount *ump = dir_node->ump;
5681 struct file_entry *fe = dir_node->fe;
5682 struct extfile_entry *efe = dir_node->efe;
5683 uint32_t fid_size, lb_size;
5684 uint64_t file_size;
5685 char *fid_name;
5686 int enough, error;
5687
5688 assert(fid);
5689 assert(dirent);
5690 assert(dir_node);
5691 assert(offset);
5692 assert(*offset != 1);
5693
5694 DPRINTF(FIDS, ("read_fid_stream called at offset %"PRIu64"\n", *offset));
5695 /* check if we're past the end of the directory */
5696 if (fe) {
5697 file_size = udf_rw64(fe->inf_len);
5698 } else {
5699 assert(dir_node->efe);
5700 file_size = udf_rw64(efe->inf_len);
5701 }
5702 if (*offset >= file_size)
5703 return EINVAL;
5704
5705 /* get maximum length of FID descriptor */
5706 lb_size = udf_rw32(ump->logical_vol->lb_size);
5707
5708 /* initialise return values */
5709 fid_size = 0;
5710 memset(dirent, 0, sizeof(struct dirent));
5711 memset(fid, 0, lb_size);
5712
5713 enough = (file_size - (*offset) >= UDF_FID_SIZE);
5714 if (!enough) {
5715 /* short dir ... */
5716 return EIO;
5717 }
5718
5719 error = vn_rdwr(UIO_READ, vp,
5720 fid, MIN(file_size - (*offset), lb_size), *offset,
5721 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, FSCRED,
5722 NULL, NULL);
5723 if (error)
5724 return error;
5725
5726 DPRINTF(FIDS, ("\tfid piece read in fine\n"));
5727 /*
5728 * Check if we got a whole descriptor.
5729 * TODO Try to `resync' directory stream when something is very wrong.
5730 */
5731
5732 /* check if our FID header is OK */
5733 error = udf_check_tag(fid);
5734 if (error) {
5735 goto brokendir;
5736 }
5737 DPRINTF(FIDS, ("\ttag check ok\n"));
5738
5739 if (udf_rw16(fid->tag.id) != TAGID_FID) {
5740 error = EIO;
5741 goto brokendir;
5742 }
5743 DPRINTF(FIDS, ("\ttag checked ok: got TAGID_FID\n"));
5744
5745 /* check for length */
5746 fid_size = udf_fidsize(fid);
5747 enough = (file_size - (*offset) >= fid_size);
5748 if (!enough) {
5749 error = EIO;
5750 goto brokendir;
5751 }
5752 DPRINTF(FIDS, ("\tthe complete fid is read in\n"));
5753
5754 /* check FID contents */
5755 error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
5756 brokendir:
5757 if (error) {
5758 /* note that is sometimes a bit quick to report */
5759 printf("BROKEN DIRECTORY ENTRY\n");
5760 /* RESYNC? */
5761 /* TODO: use udf_resync_fid_stream */
5762 return EIO;
5763 }
5764 DPRINTF(FIDS, ("\tpayload checked ok\n"));
5765
5766 /* we got a whole and valid descriptor! */
5767 DPRINTF(FIDS, ("\tinterpret FID\n"));
5768
5769 /* create resulting dirent structure */
5770 fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
5771 udf_to_unix_name(dirent->d_name, MAXNAMLEN,
5772 fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
5773
5774 /* '..' has no name, so provide one */
5775 if (fid->file_char & UDF_FILE_CHAR_PAR)
5776 strcpy(dirent->d_name, "..");
5777
5778 dirent->d_fileno = udf_calchash(&fid->icb); /* inode hash XXX */
5779 dirent->d_namlen = strlen(dirent->d_name);
5780 dirent->d_reclen = _DIRENT_SIZE(dirent);
5781
5782 /*
5783 * Note that its not worth trying to go for the filetypes now... its
5784 * too expensive too
5785 */
5786 dirent->d_type = DT_UNKNOWN;
5787
5788 /* initial guess for filetype we can make */
5789 if (fid->file_char & UDF_FILE_CHAR_DIR)
5790 dirent->d_type = DT_DIR;
5791
5792 /* advance */
5793 *offset += fid_size;
5794
5795 return error;
5796 }
5797
5798
5799 /* --------------------------------------------------------------------- */
5800
5801 static void
5802 udf_sync_pass(struct udf_mount *ump, kauth_cred_t cred, int waitfor,
5803 int pass, int *ndirty)
5804 {
5805 struct udf_node *udf_node, *n_udf_node;
5806 struct vnode *vp;
5807 int vdirty, error;
5808 int on_type, on_flags, on_vnode;
5809
5810 derailed:
5811 KASSERT(mutex_owned(&mntvnode_lock));
5812
5813 DPRINTF(SYNC, ("sync_pass %d\n", pass));
5814 udf_node = LIST_FIRST(&ump->sorted_udf_nodes);
5815 for (;udf_node; udf_node = n_udf_node) {
5816 DPRINTF(SYNC, ("."));
5817
5818 udf_node->i_flags &= ~IN_SYNCED;
5819 vp = udf_node->vnode;
5820
5821 mutex_enter(&vp->v_interlock);
5822 n_udf_node = LIST_NEXT(udf_node, sortchain);
5823 if (n_udf_node)
5824 n_udf_node->i_flags |= IN_SYNCED;
5825
5826 /* system nodes are not synced this way */
5827 if (vp->v_vflag & VV_SYSTEM) {
5828 mutex_exit(&vp->v_interlock);
5829 continue;
5830 }
5831
5832 /* check if its dirty enough to even try */
5833 on_type = (waitfor == MNT_LAZY || vp->v_type == VNON);
5834 on_flags = ((udf_node->i_flags &
5835 (IN_ACCESSED | IN_UPDATE | IN_MODIFIED)) == 0);
5836 on_vnode = LIST_EMPTY(&vp->v_dirtyblkhd)
5837 && UVM_OBJ_IS_CLEAN(&vp->v_uobj);
5838 if (on_type || (on_flags || on_vnode)) { /* XXX */
5839 /* not dirty (enough?) */
5840 mutex_exit(&vp->v_interlock);
5841 continue;
5842 }
5843
5844 mutex_exit(&mntvnode_lock);
5845 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
5846 if (error) {
5847 mutex_enter(&mntvnode_lock);
5848 if (error == ENOENT)
5849 goto derailed;
5850 *ndirty += 1;
5851 continue;
5852 }
5853
5854 switch (pass) {
5855 case 1:
5856 VOP_FSYNC(vp, cred, 0 | FSYNC_DATAONLY,0,0);
5857 break;
5858 case 2:
5859 vdirty = vp->v_numoutput;
5860 if (vp->v_tag == VT_UDF)
5861 vdirty += udf_node->outstanding_bufs +
5862 udf_node->outstanding_nodedscr;
5863 if (vdirty == 0)
5864 VOP_FSYNC(vp, cred, 0,0,0);
5865 *ndirty += vdirty;
5866 break;
5867 case 3:
5868 vdirty = vp->v_numoutput;
5869 if (vp->v_tag == VT_UDF)
5870 vdirty += udf_node->outstanding_bufs +
5871 udf_node->outstanding_nodedscr;
5872 *ndirty += vdirty;
5873 break;
5874 }
5875
5876 vput(vp);
5877 mutex_enter(&mntvnode_lock);
5878 }
5879 DPRINTF(SYNC, ("END sync_pass %d\n", pass));
5880 }
5881
5882
5883 void
5884 udf_do_sync(struct udf_mount *ump, kauth_cred_t cred, int waitfor)
5885 {
5886 int dummy, ndirty;
5887
5888 mutex_enter(&mntvnode_lock);
5889 recount:
5890 dummy = 0;
5891 DPRINTF(CALL, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
5892 DPRINTF(SYNC, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
5893 udf_sync_pass(ump, cred, waitfor, 1, &dummy);
5894
5895 DPRINTF(CALL, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
5896 DPRINTF(SYNC, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
5897 udf_sync_pass(ump, cred, waitfor, 2, &dummy);
5898
5899 if (waitfor == MNT_WAIT) {
5900 ndirty = ump->devvp->v_numoutput;
5901 DPRINTF(NODE, ("counting pending blocks: on devvp %d\n",
5902 ndirty));
5903 udf_sync_pass(ump, cred, waitfor, 3, &ndirty);
5904 DPRINTF(NODE, ("counted num dirty pending blocks %d\n",
5905 ndirty));
5906
5907 if (ndirty) {
5908 /* 1/4 second wait */
5909 cv_timedwait(&ump->dirtynodes_cv, &mntvnode_lock,
5910 hz/4);
5911 goto recount;
5912 }
5913 }
5914
5915 mutex_exit(&mntvnode_lock);
5916 }
5917
5918 /* --------------------------------------------------------------------- */
5919
5920 /*
5921 * Read and write file extent in/from the buffer.
5922 *
5923 * The splitup of the extent into seperate request-buffers is to minimise
5924 * copying around as much as possible.
5925 *
5926 * block based file reading and writing
5927 */
5928
5929 static int
5930 udf_read_internal(struct udf_node *node, uint8_t *blob)
5931 {
5932 struct udf_mount *ump;
5933 struct file_entry *fe = node->fe;
5934 struct extfile_entry *efe = node->efe;
5935 uint64_t inflen;
5936 uint32_t sector_size;
5937 uint8_t *pos;
5938 int icbflags, addr_type;
5939
5940 /* get extent and do some paranoia checks */
5941 ump = node->ump;
5942 sector_size = ump->discinfo.sector_size;
5943
5944 if (fe) {
5945 inflen = udf_rw64(fe->inf_len);
5946 pos = &fe->data[0] + udf_rw32(fe->l_ea);
5947 icbflags = udf_rw16(fe->icbtag.flags);
5948 } else {
5949 assert(node->efe);
5950 inflen = udf_rw64(efe->inf_len);
5951 pos = &efe->data[0] + udf_rw32(efe->l_ea);
5952 icbflags = udf_rw16(efe->icbtag.flags);
5953 }
5954 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
5955
5956 assert(addr_type == UDF_ICB_INTERN_ALLOC);
5957 assert(inflen < sector_size);
5958
5959 /* copy out info */
5960 memset(blob, 0, sector_size);
5961 memcpy(blob, pos, inflen);
5962
5963 return 0;
5964 }
5965
5966
5967 static int
5968 udf_write_internal(struct udf_node *node, uint8_t *blob)
5969 {
5970 struct udf_mount *ump;
5971 struct file_entry *fe = node->fe;
5972 struct extfile_entry *efe = node->efe;
5973 uint64_t inflen;
5974 uint32_t sector_size;
5975 uint8_t *pos;
5976 int icbflags, addr_type;
5977
5978 /* get extent and do some paranoia checks */
5979 ump = node->ump;
5980 sector_size = ump->discinfo.sector_size;
5981
5982 if (fe) {
5983 inflen = udf_rw64(fe->inf_len);
5984 pos = &fe->data[0] + udf_rw32(fe->l_ea);
5985 icbflags = udf_rw16(fe->icbtag.flags);
5986 } else {
5987 assert(node->efe);
5988 inflen = udf_rw64(efe->inf_len);
5989 pos = &efe->data[0] + udf_rw32(efe->l_ea);
5990 icbflags = udf_rw16(efe->icbtag.flags);
5991 }
5992 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
5993
5994 assert(addr_type == UDF_ICB_INTERN_ALLOC);
5995 assert(inflen < sector_size);
5996
5997 /* copy in blob */
5998 /* memset(pos, 0, inflen); */
5999 memcpy(pos, blob, inflen);
6000
6001 return 0;
6002 }
6003
6004
6005 void
6006 udf_read_filebuf(struct udf_node *udf_node, struct buf *buf)
6007 {
6008 struct buf *nestbuf;
6009 struct udf_mount *ump = udf_node->ump;
6010 uint64_t *mapping;
6011 uint64_t run_start;
6012 uint32_t sector_size;
6013 uint32_t buf_offset, sector, rbuflen, rblk;
6014 uint32_t from, lblkno;
6015 uint32_t sectors;
6016 uint8_t *buf_pos;
6017 int error, run_length, isdir, what;
6018
6019 sector_size = udf_node->ump->discinfo.sector_size;
6020
6021 from = buf->b_blkno;
6022 sectors = buf->b_bcount / sector_size;
6023
6024 isdir = (udf_node->vnode->v_type == VDIR);
6025 what = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
6026
6027 /* assure we have enough translation slots */
6028 KASSERT(buf->b_bcount / sector_size <= UDF_MAX_MAPPINGS);
6029 KASSERT(MAXPHYS / sector_size <= UDF_MAX_MAPPINGS);
6030
6031 if (sectors > UDF_MAX_MAPPINGS) {
6032 printf("udf_read_filebuf: implementation limit on bufsize\n");
6033 buf->b_error = EIO;
6034 biodone(buf);
6035 return;
6036 }
6037
6038 mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
6039
6040 error = 0;
6041 DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
6042 error = udf_translate_file_extent(udf_node, from, sectors, mapping);
6043 if (error) {
6044 buf->b_error = error;
6045 biodone(buf);
6046 goto out;
6047 }
6048 DPRINTF(READ, ("\ttranslate extent went OK\n"));
6049
6050 /* pre-check if its an internal */
6051 if (*mapping == UDF_TRANS_INTERN) {
6052 error = udf_read_internal(udf_node, (uint8_t *) buf->b_data);
6053 if (error)
6054 buf->b_error = error;
6055 biodone(buf);
6056 goto out;
6057 }
6058 DPRINTF(READ, ("\tnot intern\n"));
6059
6060 #ifdef DEBUG
6061 if (udf_verbose & UDF_DEBUG_TRANSLATE) {
6062 printf("Returned translation table:\n");
6063 for (sector = 0; sector < sectors; sector++) {
6064 printf("%d : %"PRIu64"\n", sector, mapping[sector]);
6065 }
6066 }
6067 #endif
6068
6069 /* request read-in of data from disc sheduler */
6070 buf->b_resid = buf->b_bcount;
6071 for (sector = 0; sector < sectors; sector++) {
6072 buf_offset = sector * sector_size;
6073 buf_pos = (uint8_t *) buf->b_data + buf_offset;
6074 DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
6075
6076 /* check if its zero or unmapped to stop reading */
6077 switch (mapping[sector]) {
6078 case UDF_TRANS_UNMAPPED:
6079 case UDF_TRANS_ZERO:
6080 /* copy zero sector TODO runlength like below */
6081 memset(buf_pos, 0, sector_size);
6082 DPRINTF(READ, ("\treturning zero sector\n"));
6083 nestiobuf_done(buf, sector_size, 0);
6084 break;
6085 default :
6086 DPRINTF(READ, ("\tread sector "
6087 "%"PRIu64"\n", mapping[sector]));
6088
6089 lblkno = from + sector;
6090 run_start = mapping[sector];
6091 run_length = 1;
6092 while (sector < sectors-1) {
6093 if (mapping[sector+1] != mapping[sector]+1)
6094 break;
6095 run_length++;
6096 sector++;
6097 }
6098
6099 /*
6100 * nest an iobuf and mark it for async reading. Since
6101 * we're using nested buffers, they can't be cached by
6102 * design.
6103 */
6104 rbuflen = run_length * sector_size;
6105 rblk = run_start * (sector_size/DEV_BSIZE);
6106
6107 nestbuf = getiobuf(NULL, true);
6108 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
6109 /* nestbuf is B_ASYNC */
6110
6111 /* identify this nestbuf */
6112 nestbuf->b_lblkno = lblkno;
6113 assert(nestbuf->b_vp == udf_node->vnode);
6114
6115 /* CD shedules on raw blkno */
6116 nestbuf->b_blkno = rblk;
6117 nestbuf->b_proc = NULL;
6118 nestbuf->b_rawblkno = rblk;
6119 nestbuf->b_udf_c_type = what;
6120
6121 udf_discstrat_queuebuf(ump, nestbuf);
6122 }
6123 }
6124 out:
6125 /* if we're synchronously reading, wait for the completion */
6126 if ((buf->b_flags & B_ASYNC) == 0)
6127 biowait(buf);
6128
6129 DPRINTF(READ, ("\tend of read_filebuf\n"));
6130 free(mapping, M_TEMP);
6131 return;
6132 }
6133
6134
6135 void
6136 udf_write_filebuf(struct udf_node *udf_node, struct buf *buf)
6137 {
6138 struct buf *nestbuf;
6139 struct udf_mount *ump = udf_node->ump;
6140 uint64_t *mapping;
6141 uint64_t run_start;
6142 uint32_t lb_size;
6143 uint32_t buf_offset, lb_num, rbuflen, rblk;
6144 uint32_t from, lblkno;
6145 uint32_t num_lb;
6146 uint8_t *buf_pos;
6147 int error, run_length, isdir, what, s;
6148
6149 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
6150
6151 from = buf->b_blkno;
6152 num_lb = buf->b_bcount / lb_size;
6153
6154 isdir = (udf_node->vnode->v_type == VDIR);
6155 what = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
6156
6157 if (udf_node == ump->metadatabitmap_node)
6158 what = UDF_C_METADATA_SBM;
6159
6160 /* assure we have enough translation slots */
6161 KASSERT(buf->b_bcount / lb_size <= UDF_MAX_MAPPINGS);
6162 KASSERT(MAXPHYS / lb_size <= UDF_MAX_MAPPINGS);
6163
6164 if (num_lb > UDF_MAX_MAPPINGS) {
6165 printf("udf_write_filebuf: implementation limit on bufsize\n");
6166 buf->b_error = EIO;
6167 biodone(buf);
6168 return;
6169 }
6170
6171 mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
6172
6173 error = 0;
6174 DPRINTF(WRITE, ("\ttranslate %d-%d\n", from, num_lb));
6175 error = udf_translate_file_extent(udf_node, from, num_lb, mapping);
6176 if (error) {
6177 buf->b_error = error;
6178 biodone(buf);
6179 goto out;
6180 }
6181 DPRINTF(WRITE, ("\ttranslate extent went OK\n"));
6182
6183 /* if its internally mapped, we can write it in the descriptor itself */
6184 if (*mapping == UDF_TRANS_INTERN) {
6185 /* TODO paranoia check if we ARE going to have enough space */
6186 error = udf_write_internal(udf_node, (uint8_t *) buf->b_data);
6187 if (error)
6188 buf->b_error = error;
6189 biodone(buf);
6190 goto out;
6191 }
6192 DPRINTF(WRITE, ("\tnot intern\n"));
6193
6194 /* request write out of data to disc sheduler */
6195 buf->b_resid = buf->b_bcount;
6196 for (lb_num = 0; lb_num < num_lb; lb_num++) {
6197 buf_offset = lb_num * lb_size;
6198 buf_pos = (uint8_t *) buf->b_data + buf_offset;
6199 DPRINTF(WRITE, ("\tprocessing rel lb_num %d\n", lb_num));
6200
6201 /*
6202 * Mappings are not that important here. Just before we write
6203 * the lb_num we late-allocate them when needed and update the
6204 * mapping in the udf_node.
6205 */
6206
6207 /* XXX why not ignore the mapping altogether ? */
6208 /* TODO estimate here how much will be late-allocated */
6209 DPRINTF(WRITE, ("\twrite lb_num "
6210 "%"PRIu64, mapping[lb_num]));
6211
6212 lblkno = from + lb_num;
6213 run_start = mapping[lb_num];
6214 run_length = 1;
6215 while (lb_num < num_lb-1) {
6216 if (mapping[lb_num+1] != mapping[lb_num]+1)
6217 if (mapping[lb_num+1] != mapping[lb_num])
6218 break;
6219 run_length++;
6220 lb_num++;
6221 }
6222 DPRINTF(WRITE, ("+ %d\n", run_length));
6223
6224 /* nest an iobuf on the master buffer for the extent */
6225 rbuflen = run_length * lb_size;
6226 rblk = run_start * (lb_size/DEV_BSIZE);
6227
6228 #if 0
6229 /* if its zero or unmapped, our blknr gets -1 for unmapped */
6230 switch (mapping[lb_num]) {
6231 case UDF_TRANS_UNMAPPED:
6232 case UDF_TRANS_ZERO:
6233 rblk = -1;
6234 break;
6235 default:
6236 rblk = run_start * (lb_size/DEV_BSIZE);
6237 break;
6238 }
6239 #endif
6240
6241 nestbuf = getiobuf(NULL, true);
6242 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
6243 /* nestbuf is B_ASYNC */
6244
6245 /* identify this nestbuf */
6246 nestbuf->b_lblkno = lblkno;
6247 KASSERT(nestbuf->b_vp == udf_node->vnode);
6248
6249 /* CD shedules on raw blkno */
6250 nestbuf->b_blkno = rblk;
6251 nestbuf->b_proc = NULL;
6252 nestbuf->b_rawblkno = rblk;
6253 nestbuf->b_udf_c_type = what;
6254
6255 /* increment our outstanding bufs counter */
6256 s = splbio();
6257 udf_node->outstanding_bufs++;
6258 splx(s);
6259
6260 udf_discstrat_queuebuf(ump, nestbuf);
6261 }
6262 out:
6263 /* if we're synchronously writing, wait for the completion */
6264 if ((buf->b_flags & B_ASYNC) == 0)
6265 biowait(buf);
6266
6267 DPRINTF(WRITE, ("\tend of write_filebuf\n"));
6268 free(mapping, M_TEMP);
6269 return;
6270 }
6271
6272 /* --------------------------------------------------------------------- */
6273
6274
6275