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