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