udf_subr.c revision 1.62 1 /* $NetBSD: udf_subr.c,v 1.62 2008/07/18 14:18:51 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.62 2008/07/18 14:18:51 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 = *result;
4065
4066 /* retrieve information to avoid recalculation and advance */
4067 hashvalue = dirh_e->hashvalue;
4068 dirh_e = LIST_NEXT(*result, next);
4069 } else {
4070 /* calculate our hash and lookup all entries in hashline */
4071 hashvalue = udf_dirhash_hash(d_name, d_namlen);
4072 hashline = hashvalue & UDF_DIRHASH_HASHMASK;
4073 dirh_e = LIST_FIRST(&dirh->entries[hashline]);
4074 }
4075
4076 for (; dirh_e; dirh_e = LIST_NEXT(dirh_e, next)) {
4077 /* check for hash collision */
4078 if (dirh_e->hashvalue != hashvalue)
4079 continue;
4080 if (dirh_e->d_namlen != d_namlen)
4081 continue;
4082 /* might have an entry in the cache */
4083 *result = dirh_e;
4084 return 1;
4085 }
4086
4087 *result = NULL;
4088 return 0;
4089 }
4090
4091
4092 /* BUGALERT: don't use result longer than needed, never past the node lock */
4093 /* call with NULL *result initially and it will return nonzero if again */
4094 static int
4095 udf_dirhash_lookup_freed(struct udf_node *dir_node, uint32_t min_fidsize,
4096 struct udf_dirhash_entry **result)
4097 {
4098 struct udf_dirhash *dirh;
4099 struct udf_dirhash_entry *dirh_e;
4100
4101 KASSERT(VOP_ISLOCKED(dir_node->vnode));
4102
4103 /* make sure we have a dirhash to look into */
4104 udf_dirhash_init(dir_node);
4105 dirh = dir_node->dir_hash;
4106
4107 /* start where we were */
4108 if (*result) {
4109 KASSERT(dir_node->dir_hash);
4110 dirh_e = LIST_NEXT(*result, next);
4111 } else {
4112 /* lookup all entries that match */
4113 dirh_e = LIST_FIRST(&dirh->free_entries);
4114 }
4115
4116 for (; dirh_e; dirh_e = LIST_NEXT(dirh_e, next)) {
4117 /* check for minimum size */
4118 if (dirh_e->fid_size < min_fidsize)
4119 continue;
4120 /* might be a candidate */
4121 *result = dirh_e;
4122 return 1;
4123 }
4124
4125 *result = NULL;
4126 return 0;
4127 }
4128
4129
4130 static int
4131 udf_dirhash_fill(struct udf_node *dir_node)
4132 {
4133 struct vnode *dvp = dir_node->vnode;
4134 struct udf_dirhash *dirh;
4135 struct file_entry *fe = dir_node->fe;
4136 struct extfile_entry *efe = dir_node->efe;
4137 struct fileid_desc *fid;
4138 struct dirent *dirent;
4139 uint64_t file_size, pre_diroffset, diroffset;
4140 uint32_t lb_size;
4141 int error;
4142
4143 /* already filled/read in? */
4144 if (dir_node->i_flags & IN_DIRHASH_BROKEN)
4145 return EIO;
4146 if (dir_node->i_flags & IN_DIRHASH_COMPLETE)
4147 return 0;
4148
4149 /* make sure we have a dirhash to add to */
4150 udf_dirhash_init(dir_node);
4151 dirh = dir_node->dir_hash;
4152 KASSERT(dirh);
4153
4154 /* get directory filesize */
4155 if (fe) {
4156 file_size = udf_rw64(fe->inf_len);
4157 } else {
4158 assert(efe);
4159 file_size = udf_rw64(efe->inf_len);
4160 }
4161
4162 /* allocate temporary space for fid */
4163 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4164 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4165
4166 /* allocate temporary space for dirent */
4167 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4168
4169 error = 0;
4170 diroffset = 0;
4171 while (diroffset < file_size) {
4172 /* transfer a new fid/dirent */
4173 pre_diroffset = diroffset;
4174 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
4175 if (error) {
4176 /* TODO what to do? continue but not add? */
4177 dir_node->i_flags |= IN_DIRHASH_BROKEN;
4178 break;
4179 }
4180
4181 if ((fid->file_char & UDF_FILE_CHAR_DEL)) {
4182 /* register deleted extent for reuse */
4183 udf_dirhash_enter_freed(dir_node, pre_diroffset,
4184 udf_fidsize(fid));
4185 } else {
4186 /* append to the dirhash */
4187 udf_dirhash_enter(dir_node, fid, dirent, pre_diroffset,
4188 udf_fidsize(fid), 0);
4189 }
4190 }
4191 dir_node->i_flags |= IN_DIRHASH_COMPLETE;
4192
4193 free(fid, M_UDFTEMP);
4194 free(dirent, M_UDFTEMP);
4195
4196 return error;
4197 }
4198
4199
4200 /* --------------------------------------------------------------------- */
4201
4202 /*
4203 * Directory read and manipulation functions.
4204 *
4205 * Note that if the file is found, the cached diroffset position *before* the
4206 * advance is remembered. Thus if the same filename is lookup again just after
4207 * this lookup its immediately found.
4208 */
4209
4210 int
4211 udf_lookup_name_in_dir(struct vnode *vp, const char *name, int namelen,
4212 struct long_ad *icb_loc, int *found)
4213 {
4214 struct udf_node *dir_node = VTOI(vp);
4215 struct udf_dirhash_entry *dirh_ep;
4216 struct fileid_desc *fid;
4217 struct dirent *dirent;
4218 uint64_t diroffset;
4219 uint32_t lb_size;
4220 int hit, error;
4221
4222 /* set default return */
4223 *found = 0;
4224
4225 /* fillup with complete dir readin if needed */
4226 error = udf_dirhash_fill(dir_node);
4227 if (error)
4228 return error;
4229
4230 /* allocate temporary space for fid */
4231 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4232 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4233 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4234
4235 DPRINTF(DIRHASH, ("dirhash_lookup looking for `%*.*s`\n",
4236 namelen, namelen, name));
4237
4238 /* search our dirhash hits */
4239 memset(icb_loc, 0, sizeof(*icb_loc));
4240 dirh_ep = NULL;
4241 for (;;) {
4242 hit = udf_dirhash_lookup(dir_node, name, namelen, &dirh_ep);
4243 /* if no hit, abort the search */
4244 if (!hit)
4245 break;
4246
4247 /* check this hit */
4248 diroffset = dirh_ep->offset;
4249
4250 /* transfer a new fid/dirent */
4251 error = udf_read_fid_stream(vp, &diroffset, fid, dirent);
4252 if (error)
4253 break;
4254
4255 DPRINTF(DIRHASH, ("dirhash_lookup\tchecking `%*.*s`\n",
4256 dirent->d_namlen, dirent->d_namlen, dirent->d_name));
4257
4258 /* see if its our entry */
4259 KASSERT(dirent->d_namlen == namelen);
4260 if (strncmp(dirent->d_name, name, namelen) == 0) {
4261 *found = 1;
4262 *icb_loc = fid->icb;
4263 break;
4264 }
4265 }
4266 free(fid, M_UDFTEMP);
4267 free(dirent, M_UDFTEMP);
4268
4269 return error;
4270 }
4271
4272 /* --------------------------------------------------------------------- */
4273
4274 static int
4275 udf_create_new_fe(struct udf_mount *ump, struct file_entry *fe, int file_type,
4276 struct long_ad *node_icb, struct long_ad *parent_icb,
4277 uint64_t parent_unique_id)
4278 {
4279 struct timespec now;
4280 struct icb_tag *icb;
4281 struct filetimes_extattr_entry *ft_extattr;
4282 uint64_t unique_id;
4283 uint32_t fidsize, lb_num;
4284 uint8_t *bpos;
4285 int crclen, attrlen;
4286
4287 lb_num = udf_rw32(node_icb->loc.lb_num);
4288 udf_inittag(ump, &fe->tag, TAGID_FENTRY, lb_num);
4289 icb = &fe->icbtag;
4290
4291 /*
4292 * Always use strategy type 4 unless on WORM wich we don't support
4293 * (yet). Fill in defaults and set for internal allocation of data.
4294 */
4295 icb->strat_type = udf_rw16(4);
4296 icb->max_num_entries = udf_rw16(1);
4297 icb->file_type = file_type; /* 8 bit */
4298 icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC);
4299
4300 fe->perm = udf_rw32(0x7fff); /* all is allowed */
4301 fe->link_cnt = udf_rw16(0); /* explicit setting */
4302
4303 fe->ckpoint = udf_rw32(1); /* user supplied file version */
4304
4305 vfs_timestamp(&now);
4306 udf_timespec_to_timestamp(&now, &fe->atime);
4307 udf_timespec_to_timestamp(&now, &fe->attrtime);
4308 udf_timespec_to_timestamp(&now, &fe->mtime);
4309
4310 udf_set_regid(&fe->imp_id, IMPL_NAME);
4311 udf_add_impl_regid(ump, &fe->imp_id);
4312
4313 unique_id = udf_advance_uniqueid(ump);
4314 fe->unique_id = udf_rw64(unique_id);
4315 fe->l_ea = udf_rw32(0);
4316
4317 /* create extended attribute to record our creation time */
4318 attrlen = UDF_FILETIMES_ATTR_SIZE(1);
4319 ft_extattr = malloc(attrlen, M_UDFTEMP, M_WAITOK);
4320 memset(ft_extattr, 0, attrlen);
4321 ft_extattr->hdr.type = udf_rw32(UDF_FILETIMES_ATTR_NO);
4322 ft_extattr->hdr.subtype = 1; /* [4/48.10.5] */
4323 ft_extattr->hdr.a_l = udf_rw32(UDF_FILETIMES_ATTR_SIZE(1));
4324 ft_extattr->d_l = udf_rw32(UDF_TIMESTAMP_SIZE); /* one item */
4325 ft_extattr->existence = UDF_FILETIMES_FILE_CREATION;
4326 udf_timespec_to_timestamp(&now, &ft_extattr->times[0]);
4327
4328 udf_extattr_insert_internal(ump, (union dscrptr *) fe,
4329 (struct extattr_entry *) ft_extattr);
4330 free(ft_extattr, M_UDFTEMP);
4331
4332 /* if its a directory, create '..' */
4333 bpos = (uint8_t *) fe->data + udf_rw32(fe->l_ea);
4334 fidsize = 0;
4335 if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
4336 fidsize = udf_create_parentfid(ump,
4337 (struct fileid_desc *) bpos, parent_icb,
4338 parent_unique_id);
4339 }
4340
4341 /* record fidlength information */
4342 fe->inf_len = udf_rw64(fidsize);
4343 fe->l_ad = udf_rw32(fidsize);
4344 fe->logblks_rec = udf_rw64(0); /* intern */
4345
4346 crclen = sizeof(struct file_entry) - 1 - UDF_DESC_TAG_LENGTH;
4347 crclen += udf_rw32(fe->l_ea) + fidsize;
4348 fe->tag.desc_crc_len = udf_rw16(crclen);
4349
4350 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fe);
4351
4352 return fidsize;
4353 }
4354
4355 /* --------------------------------------------------------------------- */
4356
4357 static int
4358 udf_create_new_efe(struct udf_mount *ump, struct extfile_entry *efe,
4359 int file_type, struct long_ad *node_icb, struct long_ad *parent_icb,
4360 uint64_t parent_unique_id)
4361 {
4362 struct timespec now;
4363 struct icb_tag *icb;
4364 uint64_t unique_id;
4365 uint32_t fidsize, lb_num;
4366 uint8_t *bpos;
4367 int crclen;
4368
4369 lb_num = udf_rw32(node_icb->loc.lb_num);
4370 udf_inittag(ump, &efe->tag, TAGID_EXTFENTRY, lb_num);
4371 icb = &efe->icbtag;
4372
4373 /*
4374 * Always use strategy type 4 unless on WORM wich we don't support
4375 * (yet). Fill in defaults and set for internal allocation of data.
4376 */
4377 icb->strat_type = udf_rw16(4);
4378 icb->max_num_entries = udf_rw16(1);
4379 icb->file_type = file_type; /* 8 bit */
4380 icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC);
4381
4382 efe->perm = udf_rw32(0x7fff); /* all is allowed */
4383 efe->link_cnt = udf_rw16(0); /* explicit setting */
4384
4385 efe->ckpoint = udf_rw32(1); /* user supplied file version */
4386
4387 vfs_timestamp(&now);
4388 udf_timespec_to_timestamp(&now, &efe->ctime);
4389 udf_timespec_to_timestamp(&now, &efe->atime);
4390 udf_timespec_to_timestamp(&now, &efe->attrtime);
4391 udf_timespec_to_timestamp(&now, &efe->mtime);
4392
4393 udf_set_regid(&efe->imp_id, IMPL_NAME);
4394 udf_add_impl_regid(ump, &efe->imp_id);
4395
4396 unique_id = udf_advance_uniqueid(ump);
4397 efe->unique_id = udf_rw64(unique_id);
4398 efe->l_ea = udf_rw32(0);
4399
4400 /* if its a directory, create '..' */
4401 bpos = (uint8_t *) efe->data + udf_rw32(efe->l_ea);
4402 fidsize = 0;
4403 if (file_type == UDF_ICB_FILETYPE_DIRECTORY) {
4404 fidsize = udf_create_parentfid(ump,
4405 (struct fileid_desc *) bpos, parent_icb,
4406 parent_unique_id);
4407 }
4408
4409 /* record fidlength information */
4410 efe->obj_size = udf_rw64(fidsize);
4411 efe->inf_len = udf_rw64(fidsize);
4412 efe->l_ad = udf_rw32(fidsize);
4413 efe->logblks_rec = udf_rw64(0); /* intern */
4414
4415 crclen = sizeof(struct extfile_entry) - 1 - UDF_DESC_TAG_LENGTH;
4416 crclen += udf_rw32(efe->l_ea) + fidsize;
4417 efe->tag.desc_crc_len = udf_rw16(crclen);
4418
4419 (void) udf_validate_tag_and_crc_sums((union dscrptr *) efe);
4420
4421 return fidsize;
4422 }
4423
4424 /* --------------------------------------------------------------------- */
4425
4426 int
4427 udf_dir_detach(struct udf_mount *ump, struct udf_node *dir_node,
4428 struct udf_node *udf_node, struct componentname *cnp)
4429 {
4430 struct vnode *dvp = dir_node->vnode;
4431 struct udf_dirhash_entry *dirh_ep;
4432 struct file_entry *fe = dir_node->fe;
4433 struct extfile_entry *efe = dir_node->efe;
4434 struct fileid_desc *fid;
4435 struct dirent *dirent;
4436 uint64_t file_size, diroffset;
4437 uint32_t lb_size, fidsize;
4438 int found, error;
4439 char const *name = cnp->cn_nameptr;
4440 int namelen = cnp->cn_namelen;
4441 int hit, refcnt;
4442
4443 /* get directory filesize */
4444 if (fe) {
4445 file_size = udf_rw64(fe->inf_len);
4446 } else {
4447 assert(efe);
4448 file_size = udf_rw64(efe->inf_len);
4449 }
4450
4451 /* allocate temporary space for fid */
4452 lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
4453 fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
4454 dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
4455
4456 /* search our dirhash hits */
4457 found = 0;
4458 dirh_ep = NULL;
4459 for (;;) {
4460 hit = udf_dirhash_lookup(dir_node, name, namelen, &dirh_ep);
4461 /* if no hit, abort the search */
4462 if (!hit)
4463 break;
4464
4465 /* check this hit */
4466 diroffset = dirh_ep->offset;
4467
4468 /* transfer a new fid/dirent */
4469 error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
4470 if (error)
4471 break;
4472
4473 /* see if its our entry */
4474 KASSERT(dirent->d_namlen == namelen);
4475 if (strncmp(dirent->d_name, name, namelen) == 0) {
4476 found = 1;
4477 break;
4478 }
4479 }
4480
4481 if (!found)
4482 error = ENOENT;
4483 if (error)
4484 goto error_out;
4485
4486 /* mark deleted */
4487 fid->file_char |= UDF_FILE_CHAR_DEL;
4488 #ifdef UDF_COMPLETE_DELETE
4489 memset(&fid->icb, 0, sizeof(fid->icb));
4490 #endif
4491 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
4492
4493 /* get size of fid and compensate for the read_fid_stream advance */
4494 fidsize = udf_fidsize(fid);
4495 diroffset -= fidsize;
4496
4497 /* write out */
4498 error = vn_rdwr(UIO_WRITE, dir_node->vnode,
4499 fid, fidsize, diroffset,
4500 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
4501 FSCRED, NULL, NULL);
4502 if (error)
4503 goto error_out;
4504
4505 /* get reference count of attached node */
4506 if (udf_node->fe) {
4507 refcnt = udf_rw16(udf_node->fe->link_cnt);
4508 } else {
4509 KASSERT(udf_node->efe);
4510 refcnt = udf_rw16(udf_node->efe->link_cnt);
4511 }
4512 #ifdef UDF_COMPLETE_DELETE
4513 /* substract reference counter in attached node */
4514 refcnt -= 1;
4515 if (udf_node->fe) {
4516 udf_node->fe->link_cnt = udf_rw16(refcnt);
4517 } else {
4518 udf_node->efe->link_cnt = udf_rw16(refcnt);
4519 }
4520
4521 /* prevent writeout when refcnt == 0 */
4522 if (refcnt == 0)
4523 udf_node->i_flags |= IN_DELETED;
4524
4525 if (fid->file_char & UDF_FILE_CHAR_DIR) {
4526 int drefcnt;
4527
4528 /* substract reference counter in directory node */
4529 /* note subtract 2 (?) for its was also backreferenced */
4530 if (dir_node->fe) {
4531 drefcnt = udf_rw16(dir_node->fe->link_cnt);
4532 drefcnt -= 1;
4533 dir_node->fe->link_cnt = udf_rw16(drefcnt);
4534 } else {
4535 KASSERT(dir_node->efe);
4536 drefcnt = udf_rw16(dir_node->efe->link_cnt);
4537 drefcnt -= 1;
4538 dir_node->efe->link_cnt = udf_rw16(drefcnt);
4539 }
4540 }
4541
4542 udf_node->i_flags |= IN_MODIFIED;
4543 dir_node->i_flags |= IN_MODIFIED;
4544 #endif
4545 /* if it is/was a hardlink adjust the file count */
4546 if (refcnt > 0)
4547 udf_adjust_filecount(udf_node, -1);
4548
4549 /* remove from the dirhash */
4550 udf_dirhash_remove(dir_node, dirent, diroffset,
4551 udf_fidsize(fid));
4552
4553 error_out:
4554 free(fid, M_UDFTEMP);
4555 free(dirent, M_UDFTEMP);
4556
4557 return error;
4558 }
4559
4560 /* --------------------------------------------------------------------- */
4561
4562 /*
4563 * We are not allowed to split the fid tag itself over an logical block so
4564 * check the space remaining in the logical block.
4565 *
4566 * We try to select the smallest candidate for recycling or when none is
4567 * found, append a new one at the end of the directory.
4568 */
4569
4570 int
4571 udf_dir_attach(struct udf_mount *ump, struct udf_node *dir_node,
4572 struct udf_node *udf_node, struct vattr *vap, struct componentname *cnp)
4573 {
4574 struct vnode *dvp = dir_node->vnode;
4575 struct udf_dirhash_entry *dirh_ep;
4576 struct fileid_desc *fid;
4577 struct icb_tag *icbtag;
4578 struct charspec osta_charspec;
4579 struct dirent dirent;
4580 uint64_t unique_id, dir_size, diroffset;
4581 uint64_t fid_pos, end_fid_pos, chosen_fid_pos;
4582 uint32_t chosen_size, chosen_size_diff;
4583 int lb_size, lb_rest, fidsize, this_fidsize, size_diff;
4584 int file_char, refcnt, icbflags, addr_type, hit, error;
4585
4586 lb_size = udf_rw32(ump->logical_vol->lb_size);
4587 udf_osta_charset(&osta_charspec);
4588
4589 if (dir_node->fe) {
4590 dir_size = udf_rw64(dir_node->fe->inf_len);
4591 icbtag = &dir_node->fe->icbtag;
4592 } else {
4593 dir_size = udf_rw64(dir_node->efe->inf_len);
4594 icbtag = &dir_node->efe->icbtag;
4595 }
4596
4597 icbflags = udf_rw16(icbtag->flags);
4598 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
4599
4600 if (udf_node->fe) {
4601 unique_id = udf_rw64(udf_node->fe->unique_id);
4602 refcnt = udf_rw16(udf_node->fe->link_cnt);
4603 } else {
4604 unique_id = udf_rw64(udf_node->efe->unique_id);
4605 refcnt = udf_rw16(udf_node->efe->link_cnt);
4606 }
4607
4608 if (refcnt > 0) {
4609 unique_id = udf_advance_uniqueid(ump);
4610 udf_adjust_filecount(udf_node, 1);
4611 }
4612
4613 /* determine file characteristics */
4614 file_char = 0; /* visible non deleted file and not stream metadata */
4615 if (vap->va_type == VDIR)
4616 file_char = UDF_FILE_CHAR_DIR;
4617
4618 /* malloc scrap buffer */
4619 fid = malloc(lb_size, M_TEMP, M_WAITOK);
4620 bzero(fid, lb_size);
4621
4622 /* calculate _minimum_ fid size */
4623 unix_to_udf_name((char *) fid->data, &fid->l_fi,
4624 cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
4625 fidsize = UDF_FID_SIZE + fid->l_fi;
4626 fidsize = (fidsize + 3) & ~3; /* multiple of 4 */
4627
4628 /* find position that will fit the FID */
4629 chosen_fid_pos = dir_size;
4630 chosen_size = 0;
4631 chosen_size_diff = UINT_MAX;
4632
4633 /* shut up gcc */
4634 dirent.d_namlen = 0;
4635
4636 /* search our dirhash hits */
4637 error = 0;
4638 dirh_ep = NULL;
4639 for (;;) {
4640 hit = udf_dirhash_lookup_freed(dir_node, fidsize, &dirh_ep);
4641 /* if no hit, abort the search */
4642 if (!hit)
4643 break;
4644
4645 /* check this hit for size */
4646 this_fidsize = dirh_ep->fid_size;
4647
4648 /* check this hit */
4649 fid_pos = dirh_ep->offset;
4650 end_fid_pos = fid_pos + this_fidsize;
4651 size_diff = this_fidsize - fidsize;
4652 lb_rest = lb_size - (end_fid_pos % lb_size);
4653
4654 #ifndef UDF_COMPLETE_DELETE
4655 /* transfer a new fid/dirent */
4656 error = udf_read_fid_stream(vp, &fid_pos, fid, dirent);
4657 if (error)
4658 goto error_out;
4659
4660 /* only reuse entries that are wiped */
4661 /* check if the len + loc are marked zero */
4662 if (udf_rw32(fid->icb.len != 0))
4663 continue;
4664 if (udf_rw32(fid->icb.loc.lb_num) != 0)
4665 continue;
4666 if (udf_rw16(fid->icb.loc.part_num != 0))
4667 continue;
4668 #endif /* UDF_COMPLETE_DELETE */
4669
4670 /* select if not splitting the tag and its smaller */
4671 if ((size_diff >= 0) &&
4672 (size_diff < chosen_size_diff) &&
4673 (lb_rest >= sizeof(struct desc_tag)))
4674 {
4675 /* UDF 2.3.4.2+3 specifies rules for iu size */
4676 if ((size_diff == 0) || (size_diff >= 32)) {
4677 chosen_fid_pos = fid_pos;
4678 chosen_size = this_fidsize;
4679 chosen_size_diff = size_diff;
4680 }
4681 }
4682 }
4683
4684
4685 /* extend directory if no other candidate found */
4686 if (chosen_size == 0) {
4687 chosen_fid_pos = dir_size;
4688 chosen_size = fidsize;
4689 chosen_size_diff = 0;
4690
4691 /* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */
4692 if (addr_type == UDF_ICB_INTERN_ALLOC) {
4693 /* pre-grow directory to see if we're to switch */
4694 udf_grow_node(dir_node, dir_size + chosen_size);
4695
4696 icbflags = udf_rw16(icbtag->flags);
4697 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
4698 }
4699
4700 /* make sure the next fid desc_tag won't be splitted */
4701 if (addr_type != UDF_ICB_INTERN_ALLOC) {
4702 end_fid_pos = chosen_fid_pos + chosen_size;
4703 lb_rest = lb_size - (end_fid_pos % lb_size);
4704
4705 /* pad with implementation use regid if needed */
4706 if (lb_rest < sizeof(struct desc_tag))
4707 chosen_size += 32;
4708 }
4709 }
4710 chosen_size_diff = chosen_size - fidsize;
4711 diroffset = chosen_fid_pos + chosen_size;
4712
4713 /* populate the FID */
4714 memset(fid, 0, lb_size);
4715 udf_inittag(ump, &fid->tag, TAGID_FID, 0);
4716 fid->file_version_num = udf_rw16(1); /* UDF 2.3.4.1 */
4717 fid->file_char = file_char;
4718 fid->icb = udf_node->loc;
4719 fid->icb.longad_uniqueid = udf_rw32((uint32_t) unique_id);
4720 fid->l_iu = udf_rw16(0);
4721
4722 if (chosen_size > fidsize) {
4723 /* insert implementation-use regid to space it correctly */
4724 fid->l_iu = udf_rw16(chosen_size_diff);
4725
4726 /* set implementation use */
4727 udf_set_regid((struct regid *) fid->data, IMPL_NAME);
4728 udf_add_impl_regid(ump, (struct regid *) fid->data);
4729 }
4730
4731 /* fill in name */
4732 unix_to_udf_name((char *) fid->data + udf_rw16(fid->l_iu),
4733 &fid->l_fi, cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
4734
4735 fid->tag.desc_crc_len = chosen_size - UDF_DESC_TAG_LENGTH;
4736 (void) udf_validate_tag_and_crc_sums((union dscrptr *) fid);
4737
4738 /* writeout FID/update parent directory */
4739 error = vn_rdwr(UIO_WRITE, dvp,
4740 fid, chosen_size, chosen_fid_pos,
4741 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED,
4742 FSCRED, NULL, NULL);
4743
4744 if (error)
4745 goto error_out;
4746
4747 /* add reference counter in attached node */
4748 if (udf_node->fe) {
4749 refcnt = udf_rw16(udf_node->fe->link_cnt);
4750 udf_node->fe->link_cnt = udf_rw16(refcnt+1);
4751 } else {
4752 KASSERT(udf_node->efe);
4753 refcnt = udf_rw16(udf_node->efe->link_cnt);
4754 udf_node->efe->link_cnt = udf_rw16(refcnt+1);
4755 }
4756
4757 /* mark not deleted if it was... just in case, but do warn */
4758 if (udf_node->i_flags & IN_DELETED) {
4759 printf("udf: warning, marking a file undeleted\n");
4760 udf_node->i_flags &= ~IN_DELETED;
4761 }
4762
4763 if (file_char & UDF_FILE_CHAR_DIR) {
4764 /* add reference counter in directory node for '..' */
4765 if (dir_node->fe) {
4766 refcnt = udf_rw16(dir_node->fe->link_cnt);
4767 refcnt++;
4768 dir_node->fe->link_cnt = udf_rw16(refcnt);
4769 } else {
4770 KASSERT(dir_node->efe);
4771 refcnt = udf_rw16(dir_node->efe->link_cnt);
4772 refcnt++;
4773 dir_node->efe->link_cnt = udf_rw16(refcnt);
4774 }
4775 }
4776
4777 /* append to the dirhash */
4778 dirent.d_namlen = cnp->cn_namelen;
4779 memcpy(dirent.d_name, cnp->cn_nameptr, cnp->cn_namelen);
4780 udf_dirhash_enter(dir_node, fid, &dirent, chosen_fid_pos,
4781 udf_fidsize(fid), 1);
4782
4783 /* note updates */
4784 udf_node->i_flags |= IN_CHANGE | IN_MODIFY; /* | IN_CREATE? */
4785 /* VN_KNOTE(udf_node, ...) */
4786 udf_update(udf_node->vnode, NULL, NULL, NULL, 0);
4787
4788 error_out:
4789 free(fid, M_TEMP);
4790
4791 return error;
4792 }
4793
4794 /* --------------------------------------------------------------------- */
4795
4796 /*
4797 * Each node can have an attached streamdir node though not recursively. These
4798 * are otherwise known as named substreams/named extended attributes that have
4799 * no size limitations.
4800 *
4801 * `Normal' extended attributes are indicated with a number and are recorded
4802 * in either the fe/efe descriptor itself for small descriptors or recorded in
4803 * the attached extended attribute file. Since these spaces can get
4804 * fragmented, care ought to be taken.
4805 *
4806 * Since the size of the space reserved for allocation descriptors is limited,
4807 * there is a mechanim provided for extending this space; this is done by a
4808 * special extent to allow schrinking of the allocations without breaking the
4809 * linkage to the allocation extent descriptor.
4810 */
4811
4812 int
4813 udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
4814 struct udf_node **udf_noderes)
4815 {
4816 union dscrptr *dscr;
4817 struct udf_node *udf_node;
4818 struct vnode *nvp;
4819 struct long_ad icb_loc, last_fe_icb_loc;
4820 uint64_t file_size;
4821 uint32_t lb_size, sector, dummy;
4822 uint8_t *file_data;
4823 int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
4824 int slot, eof, error;
4825
4826 DPRINTF(NODE, ("udf_get_node called\n"));
4827 *udf_noderes = udf_node = NULL;
4828
4829 /* lock to disallow simultanious creation of same udf_node */
4830 mutex_enter(&ump->get_node_lock);
4831
4832 DPRINTF(NODE, ("\tlookup in hash table\n"));
4833 /* lookup in hash table */
4834 assert(ump);
4835 assert(node_icb_loc);
4836 udf_node = udf_hash_lookup(ump, node_icb_loc);
4837 if (udf_node) {
4838 DPRINTF(NODE, ("\tgot it from the hash!\n"));
4839 /* vnode is returned locked */
4840 *udf_noderes = udf_node;
4841 mutex_exit(&ump->get_node_lock);
4842 return 0;
4843 }
4844
4845 /* garbage check: translate udf_node_icb_loc to sectornr */
4846 error = udf_translate_vtop(ump, node_icb_loc, §or, &dummy);
4847 if (error) {
4848 /* no use, this will fail anyway */
4849 mutex_exit(&ump->get_node_lock);
4850 return EINVAL;
4851 }
4852
4853 /* build udf_node (do initialise!) */
4854 udf_node = pool_get(&udf_node_pool, PR_WAITOK);
4855 memset(udf_node, 0, sizeof(struct udf_node));
4856
4857 DPRINTF(NODE, ("\tget new vnode\n"));
4858 /* give it a vnode */
4859 error = getnewvnode(VT_UDF, ump->vfs_mountp, udf_vnodeop_p, &nvp);
4860 if (error) {
4861 pool_put(&udf_node_pool, udf_node);
4862 mutex_exit(&ump->get_node_lock);
4863 return error;
4864 }
4865
4866 /* always return locked vnode */
4867 if ((error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY))) {
4868 /* recycle vnode and unlock; simultanious will fail too */
4869 ungetnewvnode(nvp);
4870 mutex_exit(&ump->get_node_lock);
4871 return error;
4872 }
4873
4874 /* initialise crosslinks, note location of fe/efe for hashing */
4875 udf_node->ump = ump;
4876 udf_node->vnode = nvp;
4877 nvp->v_data = udf_node;
4878 udf_node->loc = *node_icb_loc;
4879 udf_node->lockf = 0;
4880 mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
4881 cv_init(&udf_node->node_lock, "udf_nlk");
4882 genfs_node_init(nvp, &udf_genfsops); /* inititise genfs */
4883 udf_node->outstanding_bufs = 0;
4884 udf_node->outstanding_nodedscr = 0;
4885
4886 /* insert into the hash lookup */
4887 udf_register_node(udf_node);
4888
4889 /* safe to unlock, the entry is in the hash table, vnode is locked */
4890 mutex_exit(&ump->get_node_lock);
4891
4892 icb_loc = *node_icb_loc;
4893 needs_indirect = 0;
4894 strat4096 = 0;
4895 udf_file_type = UDF_ICB_FILETYPE_UNKNOWN;
4896 file_size = 0;
4897 file_data = NULL;
4898 lb_size = udf_rw32(ump->logical_vol->lb_size);
4899
4900 DPRINTF(NODE, ("\tstart reading descriptors\n"));
4901 do {
4902 /* try to read in fe/efe */
4903 error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
4904
4905 /* blank sector marks end of sequence, check this */
4906 if ((dscr == NULL) && (!strat4096))
4907 error = ENOENT;
4908
4909 /* break if read error or blank sector */
4910 if (error || (dscr == NULL))
4911 break;
4912
4913 /* process descriptor based on the descriptor type */
4914 dscr_type = udf_rw16(dscr->tag.id);
4915 DPRINTF(NODE, ("\tread descriptor %d\n", dscr_type));
4916
4917 /* if dealing with an indirect entry, follow the link */
4918 if (dscr_type == TAGID_INDIRECTENTRY) {
4919 needs_indirect = 0;
4920 udf_free_logvol_dscr(ump, &icb_loc, dscr);
4921 icb_loc = dscr->inde.indirect_icb;
4922 continue;
4923 }
4924
4925 /* only file entries and extended file entries allowed here */
4926 if ((dscr_type != TAGID_FENTRY) &&
4927 (dscr_type != TAGID_EXTFENTRY)) {
4928 udf_free_logvol_dscr(ump, &icb_loc, dscr);
4929 error = ENOENT;
4930 break;
4931 }
4932
4933 KASSERT(udf_tagsize(dscr, lb_size) == lb_size);
4934
4935 /* choose this one */
4936 last_fe_icb_loc = icb_loc;
4937
4938 /* record and process/update (ext)fentry */
4939 file_data = NULL;
4940 if (dscr_type == TAGID_FENTRY) {
4941 if (udf_node->fe)
4942 udf_free_logvol_dscr(ump, &last_fe_icb_loc,
4943 udf_node->fe);
4944 udf_node->fe = &dscr->fe;
4945 strat = udf_rw16(udf_node->fe->icbtag.strat_type);
4946 udf_file_type = udf_node->fe->icbtag.file_type;
4947 file_size = udf_rw64(udf_node->fe->inf_len);
4948 file_data = udf_node->fe->data;
4949 } else {
4950 if (udf_node->efe)
4951 udf_free_logvol_dscr(ump, &last_fe_icb_loc,
4952 udf_node->efe);
4953 udf_node->efe = &dscr->efe;
4954 strat = udf_rw16(udf_node->efe->icbtag.strat_type);
4955 udf_file_type = udf_node->efe->icbtag.file_type;
4956 file_size = udf_rw64(udf_node->efe->inf_len);
4957 file_data = udf_node->efe->data;
4958 }
4959
4960 /* check recording strategy (structure) */
4961
4962 /*
4963 * Strategy 4096 is a daisy linked chain terminating with an
4964 * unrecorded sector or a TERM descriptor. The next
4965 * descriptor is to be found in the sector that follows the
4966 * current sector.
4967 */
4968 if (strat == 4096) {
4969 strat4096 = 1;
4970 needs_indirect = 1;
4971
4972 icb_loc.loc.lb_num = udf_rw32(icb_loc.loc.lb_num) + 1;
4973 }
4974
4975 /*
4976 * Strategy 4 is the normal strategy and terminates, but if
4977 * we're in strategy 4096, we can't have strategy 4 mixed in
4978 */
4979
4980 if (strat == 4) {
4981 if (strat4096) {
4982 error = EINVAL;
4983 break;
4984 }
4985 break; /* done */
4986 }
4987 } while (!error);
4988
4989 /* first round of cleanup code */
4990 if (error) {
4991 DPRINTF(NODE, ("\tnode fe/efe failed!\n"));
4992 /* recycle udf_node */
4993 udf_dispose_node(udf_node);
4994
4995 vlockmgr(nvp->v_vnlock, LK_RELEASE);
4996 nvp->v_data = NULL;
4997 ungetnewvnode(nvp);
4998
4999 return EINVAL; /* error code ok? */
5000 }
5001 DPRINTF(NODE, ("\tnode fe/efe read in fine\n"));
5002
5003 /* assert no references to dscr anymore beyong this point */
5004 assert((udf_node->fe) || (udf_node->efe));
5005 dscr = NULL;
5006
5007 /*
5008 * Remember where to record an updated version of the descriptor. If
5009 * there is a sequence of indirect entries, icb_loc will have been
5010 * updated. Its the write disipline to allocate new space and to make
5011 * sure the chain is maintained.
5012 *
5013 * `needs_indirect' flags if the next location is to be filled with
5014 * with an indirect entry.
5015 */
5016 udf_node->write_loc = icb_loc;
5017 udf_node->needs_indirect = needs_indirect;
5018
5019 /*
5020 * Go trough all allocations extents of this descriptor and when
5021 * encountering a redirect read in the allocation extension. These are
5022 * daisy-chained.
5023 */
5024 UDF_LOCK_NODE(udf_node, 0);
5025 udf_node->num_extensions = 0;
5026
5027 error = 0;
5028 slot = 0;
5029 for (;;) {
5030 udf_get_adslot(udf_node, slot, &icb_loc, &eof);
5031 DPRINTF(ADWLK, ("slot %d, eof = %d, flags = %d, len = %d, "
5032 "lb_num = %d, part = %d\n", slot, eof,
5033 UDF_EXT_FLAGS(udf_rw32(icb_loc.len)),
5034 UDF_EXT_LEN(udf_rw32(icb_loc.len)),
5035 udf_rw32(icb_loc.loc.lb_num),
5036 udf_rw16(icb_loc.loc.part_num)));
5037 if (eof)
5038 break;
5039 slot++;
5040
5041 if (UDF_EXT_FLAGS(udf_rw32(icb_loc.len)) != UDF_EXT_REDIRECT)
5042 continue;
5043
5044 DPRINTF(NODE, ("\tgot redirect extent\n"));
5045 if (udf_node->num_extensions >= UDF_MAX_ALLOC_EXTENTS) {
5046 DPRINTF(ALLOC, ("udf_get_node: implementation limit, "
5047 "too many allocation extensions on "
5048 "udf_node\n"));
5049 error = EINVAL;
5050 break;
5051 }
5052
5053 /* length can only be *one* lb : UDF 2.50/2.3.7.1 */
5054 if (UDF_EXT_LEN(udf_rw32(icb_loc.len)) != lb_size) {
5055 DPRINTF(ALLOC, ("udf_get_node: bad allocation "
5056 "extension size in udf_node\n"));
5057 error = EINVAL;
5058 break;
5059 }
5060
5061 DPRINTF(NODE, ("read allocation extent at lb_num %d\n",
5062 UDF_EXT_LEN(udf_rw32(icb_loc.loc.lb_num))));
5063 /* load in allocation extent */
5064 error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
5065 if (error || (dscr == NULL))
5066 break;
5067
5068 /* process read-in descriptor */
5069 dscr_type = udf_rw16(dscr->tag.id);
5070
5071 if (dscr_type != TAGID_ALLOCEXTENT) {
5072 udf_free_logvol_dscr(ump, &icb_loc, dscr);
5073 error = ENOENT;
5074 break;
5075 }
5076
5077 DPRINTF(NODE, ("\trecording redirect extent\n"));
5078 udf_node->ext[udf_node->num_extensions] = &dscr->aee;
5079 udf_node->ext_loc[udf_node->num_extensions] = icb_loc;
5080
5081 udf_node->num_extensions++;
5082
5083 } /* while */
5084 UDF_UNLOCK_NODE(udf_node, 0);
5085
5086 /* second round of cleanup code */
5087 if (error) {
5088 /* recycle udf_node */
5089 udf_dispose_node(udf_node);
5090
5091 vlockmgr(nvp->v_vnlock, LK_RELEASE);
5092 nvp->v_data = NULL;
5093 ungetnewvnode(nvp);
5094
5095 return EINVAL; /* error code ok? */
5096 }
5097
5098 DPRINTF(NODE, ("\tnode read in fine\n"));
5099
5100 /*
5101 * Translate UDF filetypes into vnode types.
5102 *
5103 * Systemfiles like the meta main and mirror files are not treated as
5104 * normal files, so we type them as having no type. UDF dictates that
5105 * they are not allowed to be visible.
5106 */
5107
5108 switch (udf_file_type) {
5109 case UDF_ICB_FILETYPE_DIRECTORY :
5110 case UDF_ICB_FILETYPE_STREAMDIR :
5111 nvp->v_type = VDIR;
5112 break;
5113 case UDF_ICB_FILETYPE_BLOCKDEVICE :
5114 nvp->v_type = VBLK;
5115 break;
5116 case UDF_ICB_FILETYPE_CHARDEVICE :
5117 nvp->v_type = VCHR;
5118 break;
5119 case UDF_ICB_FILETYPE_SOCKET :
5120 nvp->v_type = VSOCK;
5121 break;
5122 case UDF_ICB_FILETYPE_FIFO :
5123 nvp->v_type = VFIFO;
5124 break;
5125 case UDF_ICB_FILETYPE_SYMLINK :
5126 nvp->v_type = VLNK;
5127 break;
5128 case UDF_ICB_FILETYPE_VAT :
5129 case UDF_ICB_FILETYPE_META_MAIN :
5130 case UDF_ICB_FILETYPE_META_MIRROR :
5131 nvp->v_type = VNON;
5132 break;
5133 case UDF_ICB_FILETYPE_RANDOMACCESS :
5134 case UDF_ICB_FILETYPE_REALTIME :
5135 nvp->v_type = VREG;
5136 break;
5137 default:
5138 /* YIKES, something else */
5139 nvp->v_type = VNON;
5140 }
5141
5142 /* TODO specfs, fifofs etc etc. vnops setting */
5143
5144 /* don't forget to set vnode's v_size */
5145 uvm_vnp_setsize(nvp, file_size);
5146
5147 /* TODO ext attr and streamdir udf_nodes */
5148
5149 *udf_noderes = udf_node;
5150
5151 return 0;
5152 }
5153
5154 /* --------------------------------------------------------------------- */
5155
5156
5157 int
5158 udf_writeout_node(struct udf_node *udf_node, int waitfor)
5159 {
5160 union dscrptr *dscr;
5161 struct long_ad *loc;
5162 int extnr, flags, error;
5163
5164 DPRINTF(NODE, ("udf_writeout_node called\n"));
5165
5166 KASSERT(udf_node->outstanding_bufs == 0);
5167 KASSERT(udf_node->outstanding_nodedscr == 0);
5168
5169 KASSERT(LIST_EMPTY(&udf_node->vnode->v_dirtyblkhd));
5170
5171 if (udf_node->i_flags & IN_DELETED) {
5172 DPRINTF(NODE, ("\tnode deleted; not writing out\n"));
5173 return 0;
5174 }
5175
5176 /* lock node */
5177 flags = waitfor ? 0 : IN_CALLBACK_ULK;
5178 UDF_LOCK_NODE(udf_node, flags);
5179
5180 /* at least one descriptor writeout */
5181 udf_node->outstanding_nodedscr = 1;
5182
5183 /* we're going to write out the descriptor so clear the flags */
5184 udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED);
5185
5186 /* if we were rebuild, write out the allocation extents */
5187 if (udf_node->i_flags & IN_NODE_REBUILD) {
5188 /* mark outstanding node dscriptors and issue them */
5189 udf_node->outstanding_nodedscr += udf_node->num_extensions;
5190 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
5191 loc = &udf_node->ext_loc[extnr];
5192 dscr = (union dscrptr *) udf_node->ext[extnr];
5193 error = udf_write_logvol_dscr(udf_node, dscr, loc, 0);
5194 if (error)
5195 return error;
5196 }
5197 /* mark allocation extents written out */
5198 udf_node->i_flags &= ~(IN_NODE_REBUILD);
5199 }
5200
5201 if (udf_node->fe) {
5202 dscr = (union dscrptr *) udf_node->fe;
5203 } else {
5204 KASSERT(udf_node->efe);
5205 dscr = (union dscrptr *) udf_node->efe;
5206 }
5207 KASSERT(dscr);
5208
5209 loc = &udf_node->write_loc;
5210 error = udf_write_logvol_dscr(udf_node, dscr, loc, waitfor);
5211 return error;
5212 }
5213
5214 /* --------------------------------------------------------------------- */
5215
5216 int
5217 udf_dispose_node(struct udf_node *udf_node)
5218 {
5219 struct vnode *vp;
5220 int extnr;
5221
5222 DPRINTF(NODE, ("udf_dispose_node called on node %p\n", udf_node));
5223 if (!udf_node) {
5224 DPRINTF(NODE, ("UDF: Dispose node on node NULL, ignoring\n"));
5225 return 0;
5226 }
5227
5228 vp = udf_node->vnode;
5229 #ifdef DIAGNOSTIC
5230 if (vp->v_numoutput)
5231 panic("disposing UDF node with pending I/O's, udf_node = %p, "
5232 "v_numoutput = %d", udf_node, vp->v_numoutput);
5233 #endif
5234
5235 /* wait until out of sync (just in case we happen to stumble over one */
5236 KASSERT(!mutex_owned(&mntvnode_lock));
5237 mutex_enter(&mntvnode_lock);
5238 while (udf_node->i_flags & IN_SYNCED) {
5239 cv_timedwait(&udf_node->ump->dirtynodes_cv, &mntvnode_lock,
5240 hz/16);
5241 }
5242 mutex_exit(&mntvnode_lock);
5243
5244 /* TODO extended attributes and streamdir */
5245
5246 /* remove dirhash if present */
5247 udf_dirhash_destroy(udf_node);
5248
5249 /* remove from our hash lookup table */
5250 udf_deregister_node(udf_node);
5251
5252 /* destroy our lock */
5253 mutex_destroy(&udf_node->node_mutex);
5254 cv_destroy(&udf_node->node_lock);
5255
5256 /* dissociate our udf_node from the vnode */
5257 genfs_node_destroy(udf_node->vnode);
5258 vp->v_data = NULL;
5259
5260 /* free associated memory and the node itself */
5261 for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
5262 udf_free_logvol_dscr(udf_node->ump, &udf_node->ext_loc[extnr],
5263 udf_node->ext[extnr]);
5264 udf_node->ext[extnr] = (void *) 0xdeadcccc;
5265 }
5266
5267 if (udf_node->fe)
5268 udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
5269 udf_node->fe);
5270 if (udf_node->efe)
5271 udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
5272 udf_node->efe);
5273
5274 udf_node->fe = (void *) 0xdeadaaaa;
5275 udf_node->efe = (void *) 0xdeadbbbb;
5276 udf_node->ump = (void *) 0xdeadbeef;
5277 pool_put(&udf_node_pool, udf_node);
5278
5279 return 0;
5280 }
5281
5282
5283
5284 /*
5285 * create a new node using the specified vnodeops, vap and cnp but with the
5286 * udf_file_type. This allows special files to be created. Use with care.
5287 */
5288
5289 static int
5290 udf_create_node_raw(struct vnode *dvp, struct vnode **vpp, int udf_file_type,
5291 int (**vnodeops)(void *), struct vattr *vap, struct componentname *cnp)
5292 {
5293 union dscrptr *dscr;
5294 struct udf_node *dir_node = VTOI(dvp);;
5295 struct udf_node *udf_node;
5296 struct udf_mount *ump = dir_node->ump;
5297 struct vnode *nvp;
5298 struct long_ad node_icb_loc;
5299 uint64_t parent_unique_id;
5300 uint64_t lmapping, pmapping;
5301 uint32_t lb_size, lb_num;
5302 uint16_t vpart_num;
5303 uid_t uid;
5304 gid_t gid, parent_gid;
5305 int fid_size, error;
5306
5307 lb_size = udf_rw32(ump->logical_vol->lb_size);
5308 *vpp = NULL;
5309
5310 /* allocate vnode */
5311 error = getnewvnode(VT_UDF, ump->vfs_mountp, vnodeops, &nvp);
5312 if (error)
5313 return error;
5314
5315 /* lock node */
5316 error = vn_lock(nvp, LK_EXCLUSIVE | LK_RETRY);
5317 if (error) {
5318 nvp->v_data = NULL;
5319 ungetnewvnode(nvp);
5320 return error;
5321 }
5322
5323 /* get disc allocation for one logical block */
5324 error = udf_pre_allocate_space(ump, UDF_C_NODE, 1,
5325 &vpart_num, &lmapping, &pmapping);
5326 lb_num = lmapping;
5327 if (error) {
5328 vlockmgr(nvp->v_vnlock, LK_RELEASE);
5329 ungetnewvnode(nvp);
5330 return error;
5331 }
5332
5333 /* initialise pointer to location */
5334 memset(&node_icb_loc, 0, sizeof(struct long_ad));
5335 node_icb_loc.len = lb_size;
5336 node_icb_loc.loc.lb_num = udf_rw32(lb_num);
5337 node_icb_loc.loc.part_num = udf_rw16(vpart_num);
5338
5339 /* build udf_node (do initialise!) */
5340 udf_node = pool_get(&udf_node_pool, PR_WAITOK);
5341 memset(udf_node, 0, sizeof(struct udf_node));
5342
5343 /* initialise crosslinks, note location of fe/efe for hashing */
5344 /* bugalert: synchronise with udf_get_node() */
5345 udf_node->ump = ump;
5346 udf_node->vnode = nvp;
5347 nvp->v_data = udf_node;
5348 udf_node->loc = node_icb_loc;
5349 udf_node->write_loc = node_icb_loc;
5350 udf_node->lockf = 0;
5351 mutex_init(&udf_node->node_mutex, MUTEX_DEFAULT, IPL_NONE);
5352 cv_init(&udf_node->node_lock, "udf_nlk");
5353 udf_node->outstanding_bufs = 0;
5354 udf_node->outstanding_nodedscr = 0;
5355
5356 /* initialise genfs */
5357 genfs_node_init(nvp, &udf_genfsops);
5358
5359 /* insert into the hash lookup */
5360 udf_register_node(udf_node);
5361
5362 /* get parent's unique ID for refering '..' if its a directory */
5363 if (dir_node->fe) {
5364 parent_unique_id = udf_rw64(dir_node->fe->unique_id);
5365 parent_gid = (gid_t) udf_rw32(dir_node->fe->gid);
5366 } else {
5367 parent_unique_id = udf_rw64(dir_node->efe->unique_id);
5368 parent_gid = (gid_t) udf_rw32(dir_node->efe->gid);
5369 }
5370
5371 /* get descriptor */
5372 udf_create_logvol_dscr(ump, udf_node, &node_icb_loc, &dscr);
5373
5374 /* choose a fe or an efe for it */
5375 if (ump->logical_vol->tag.descriptor_ver == 2) {
5376 udf_node->fe = &dscr->fe;
5377 fid_size = udf_create_new_fe(ump, udf_node->fe,
5378 udf_file_type, &udf_node->loc,
5379 &dir_node->loc, parent_unique_id);
5380 /* TODO add extended attribute for creation time */
5381 } else {
5382 udf_node->efe = &dscr->efe;
5383 fid_size = udf_create_new_efe(ump, udf_node->efe,
5384 udf_file_type, &udf_node->loc,
5385 &dir_node->loc, parent_unique_id);
5386 }
5387 KASSERT(dscr->tag.tag_loc == udf_node->loc.loc.lb_num);
5388
5389 /* update vnode's size and type */
5390 nvp->v_type = vap->va_type;
5391 uvm_vnp_setsize(nvp, fid_size);
5392
5393 /* set access mode */
5394 udf_setaccessmode(udf_node, vap->va_mode);
5395
5396 /* set ownership */
5397 uid = kauth_cred_geteuid(cnp->cn_cred);
5398 gid = parent_gid;
5399 udf_setownership(udf_node, uid, gid);
5400
5401 error = udf_dir_attach(ump, dir_node, udf_node, vap, cnp);
5402 if (error) {
5403 /* free disc allocation for node */
5404 udf_free_allocated_space(ump, lb_num, vpart_num, 1);
5405
5406 /* recycle udf_node */
5407 udf_dispose_node(udf_node);
5408 vput(nvp);
5409
5410 *vpp = NULL;
5411 return error;
5412 }
5413
5414 /* adjust file count */
5415 udf_adjust_filecount(udf_node, 1);
5416
5417 /* return result */
5418 *vpp = nvp;
5419
5420 return 0;
5421 }
5422
5423
5424 int
5425 udf_create_node(struct vnode *dvp, struct vnode **vpp, struct vattr *vap,
5426 struct componentname *cnp)
5427 {
5428 int (**vnodeops)(void *);
5429 int udf_file_type;
5430
5431 DPRINTF(NODE, ("udf_create_node called\n"));
5432
5433 /* what type are we creating ? */
5434 vnodeops = udf_vnodeop_p;
5435 /* start with a default */
5436 udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
5437
5438 *vpp = NULL;
5439
5440 switch (vap->va_type) {
5441 case VREG :
5442 udf_file_type = UDF_ICB_FILETYPE_RANDOMACCESS;
5443 break;
5444 case VDIR :
5445 udf_file_type = UDF_ICB_FILETYPE_DIRECTORY;
5446 break;
5447 case VLNK :
5448 udf_file_type = UDF_ICB_FILETYPE_SYMLINK;
5449 break;
5450 case VBLK :
5451 udf_file_type = UDF_ICB_FILETYPE_BLOCKDEVICE;
5452 /* specfs */
5453 return ENOTSUP;
5454 break;
5455 case VCHR :
5456 udf_file_type = UDF_ICB_FILETYPE_CHARDEVICE;
5457 /* specfs */
5458 return ENOTSUP;
5459 break;
5460 case VFIFO :
5461 udf_file_type = UDF_ICB_FILETYPE_FIFO;
5462 /* specfs */
5463 return ENOTSUP;
5464 break;
5465 case VSOCK :
5466 udf_file_type = UDF_ICB_FILETYPE_SOCKET;
5467 /* specfs */
5468 return ENOTSUP;
5469 break;
5470 case VNON :
5471 case VBAD :
5472 default :
5473 /* nothing; can we even create these? */
5474 return EINVAL;
5475 }
5476
5477 return udf_create_node_raw(dvp, vpp, udf_file_type, vnodeops, vap, cnp);
5478 }
5479
5480 /* --------------------------------------------------------------------- */
5481
5482 static void
5483 udf_free_descriptor_space(struct udf_node *udf_node, struct long_ad *loc, void *mem)
5484 {
5485 struct udf_mount *ump = udf_node->ump;
5486 uint32_t lb_size, lb_num, len, num_lb;
5487 uint16_t vpart_num;
5488
5489 /* is there really one? */
5490 if (mem == NULL)
5491 return;
5492
5493 /* got a descriptor here */
5494 len = UDF_EXT_LEN(udf_rw32(loc->len));
5495 lb_num = udf_rw32(loc->loc.lb_num);
5496 vpart_num = udf_rw16(loc->loc.part_num);
5497
5498 lb_size = udf_rw32(ump->logical_vol->lb_size);
5499 num_lb = (len + lb_size -1) / lb_size;
5500
5501 udf_free_allocated_space(ump, lb_num, vpart_num, num_lb);
5502 }
5503
5504 void
5505 udf_delete_node(struct udf_node *udf_node)
5506 {
5507 void *dscr;
5508 struct udf_mount *ump;
5509 struct long_ad *loc;
5510 int extnr, lvint, dummy;
5511
5512 ump = udf_node->ump;
5513
5514 /* paranoia check on integrity; should be open!; we could panic */
5515 lvint = udf_rw32(udf_node->ump->logvol_integrity->integrity_type);
5516 if (lvint == UDF_INTEGRITY_CLOSED)
5517 printf("\tIntegrity was CLOSED!\n");
5518
5519 /* whatever the node type, change its size to zero */
5520 (void) udf_resize_node(udf_node, 0, &dummy);
5521
5522 /* force it to be `clean'; no use writing it out */
5523 udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED | IN_ACCESS |
5524 IN_CHANGE | IN_UPDATE | IN_MODIFY);
5525
5526 /* adjust file count */
5527 udf_adjust_filecount(udf_node, -1);
5528
5529 /*
5530 * Free its allocated descriptors; memory will be released when
5531 * vop_reclaim() is called.
5532 */
5533 loc = &udf_node->loc;
5534
5535 dscr = udf_node->fe;
5536 udf_free_descriptor_space(udf_node, loc, dscr);
5537 dscr = udf_node->efe;
5538 udf_free_descriptor_space(udf_node, loc, dscr);
5539
5540 for (extnr = 0; extnr < UDF_MAX_ALLOC_EXTENTS; extnr++) {
5541 dscr = udf_node->ext[extnr];
5542 loc = &udf_node->ext_loc[extnr];
5543 udf_free_descriptor_space(udf_node, loc, dscr);
5544 }
5545 }
5546
5547 /* --------------------------------------------------------------------- */
5548
5549 /* set new filesize; node but be LOCKED on entry and is locked on exit */
5550 int
5551 udf_resize_node(struct udf_node *udf_node, uint64_t new_size, int *extended)
5552 {
5553 struct file_entry *fe = udf_node->fe;
5554 struct extfile_entry *efe = udf_node->efe;
5555 uint64_t file_size;
5556 int error;
5557
5558 if (fe) {
5559 file_size = udf_rw64(fe->inf_len);
5560 } else {
5561 assert(udf_node->efe);
5562 file_size = udf_rw64(efe->inf_len);
5563 }
5564
5565 DPRINTF(ATTR, ("\tchanging file length from %"PRIu64" to %"PRIu64"\n",
5566 file_size, new_size));
5567
5568 /* if not changing, we're done */
5569 if (file_size == new_size)
5570 return 0;
5571
5572 *extended = (new_size > file_size);
5573 if (*extended) {
5574 error = udf_grow_node(udf_node, new_size);
5575 } else {
5576 error = udf_shrink_node(udf_node, new_size);
5577 }
5578
5579 return error;
5580 }
5581
5582
5583 /* --------------------------------------------------------------------- */
5584
5585 void
5586 udf_itimes(struct udf_node *udf_node, struct timespec *acc,
5587 struct timespec *mod, struct timespec *birth)
5588 {
5589 struct timespec now;
5590 struct file_entry *fe;
5591 struct extfile_entry *efe;
5592 struct filetimes_extattr_entry *ft_extattr;
5593 struct timestamp *atime, *mtime, *attrtime, *ctime;
5594 struct timestamp fe_ctime;
5595 struct timespec cur_birth;
5596 uint32_t offset, a_l;
5597 uint8_t *filedata;
5598 int error;
5599
5600 /* protect against rogue values */
5601 if (!udf_node)
5602 return;
5603
5604 fe = udf_node->fe;
5605 efe = udf_node->efe;
5606
5607 if (!(udf_node->i_flags & (IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY)))
5608 return;
5609
5610 /* get descriptor information */
5611 if (fe) {
5612 atime = &fe->atime;
5613 mtime = &fe->mtime;
5614 attrtime = &fe->attrtime;
5615 filedata = fe->data;
5616
5617 /* initial save dummy setting */
5618 ctime = &fe_ctime;
5619
5620 /* check our extended attribute if present */
5621 error = udf_extattr_search_intern(udf_node,
5622 UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
5623 if (!error) {
5624 ft_extattr = (struct filetimes_extattr_entry *)
5625 (filedata + offset);
5626 if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
5627 ctime = &ft_extattr->times[0];
5628 }
5629 /* TODO create the extended attribute if not found ? */
5630 } else {
5631 assert(udf_node->efe);
5632 atime = &efe->atime;
5633 mtime = &efe->mtime;
5634 attrtime = &efe->attrtime;
5635 ctime = &efe->ctime;
5636 }
5637
5638 vfs_timestamp(&now);
5639
5640 /* set access time */
5641 if (udf_node->i_flags & IN_ACCESS) {
5642 if (acc == NULL)
5643 acc = &now;
5644 udf_timespec_to_timestamp(acc, atime);
5645 }
5646
5647 /* set modification time */
5648 if (udf_node->i_flags & (IN_UPDATE | IN_MODIFY)) {
5649 if (mod == NULL)
5650 mod = &now;
5651 udf_timespec_to_timestamp(mod, mtime);
5652
5653 /* ensure birthtime is older than set modification! */
5654 udf_timestamp_to_timespec(udf_node->ump, ctime, &cur_birth);
5655 if ((cur_birth.tv_sec > mod->tv_sec) ||
5656 ((cur_birth.tv_sec == mod->tv_sec) &&
5657 (cur_birth.tv_nsec > mod->tv_nsec))) {
5658 udf_timespec_to_timestamp(mod, ctime);
5659 }
5660 }
5661
5662 /* update birthtime if specified */
5663 /* XXX we asume here that given birthtime is older than mod */
5664 if (birth && (birth->tv_sec != VNOVAL)) {
5665 udf_timespec_to_timestamp(birth, ctime);
5666 }
5667
5668 /* set change time */
5669 if (udf_node->i_flags & (IN_CHANGE | IN_MODIFY))
5670 udf_timespec_to_timestamp(&now, attrtime);
5671
5672 /* notify updates to the node itself */
5673 if (udf_node->i_flags & (IN_ACCESS | IN_MODIFY))
5674 udf_node->i_flags |= IN_ACCESSED;
5675 if (udf_node->i_flags & (IN_UPDATE | IN_CHANGE))
5676 udf_node->i_flags |= IN_MODIFIED;
5677
5678 /* clear modification flags */
5679 udf_node->i_flags &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE | IN_MODIFY);
5680 }
5681
5682 /* --------------------------------------------------------------------- */
5683
5684 int
5685 udf_update(struct vnode *vp, struct timespec *acc,
5686 struct timespec *mod, struct timespec *birth, int updflags)
5687 {
5688 struct udf_node *udf_node = VTOI(vp);
5689 struct udf_mount *ump = udf_node->ump;
5690 struct regid *impl_id;
5691 int mnt_async = (vp->v_mount->mnt_flag & MNT_ASYNC);
5692 int waitfor, flags;
5693
5694 #ifdef DEBUG
5695 char bits[128];
5696 DPRINTF(CALL, ("udf_update(node, %p, %p, %p, %d)\n", acc, mod, birth,
5697 updflags));
5698 bitmask_snprintf(udf_node->i_flags, IN_FLAGBITS, bits, sizeof(bits));
5699 DPRINTF(CALL, ("\tnode flags %s\n", bits));
5700 DPRINTF(CALL, ("\t\tmnt_async = %d\n", mnt_async));
5701 #endif
5702
5703 /* set our times */
5704 udf_itimes(udf_node, acc, mod, birth);
5705
5706 /* set our implementation id */
5707 if (udf_node->fe) {
5708 impl_id = &udf_node->fe->imp_id;
5709 } else {
5710 impl_id = &udf_node->efe->imp_id;
5711 }
5712 udf_set_regid(impl_id, IMPL_NAME);
5713 udf_add_impl_regid(ump, impl_id);
5714
5715 /* if called when mounted readonly, never write back */
5716 if (vp->v_mount->mnt_flag & MNT_RDONLY)
5717 return 0;
5718
5719 /* check if the node is dirty 'enough'*/
5720 if (updflags & UPDATE_CLOSE) {
5721 flags = udf_node->i_flags & (IN_MODIFIED | IN_ACCESSED);
5722 } else {
5723 flags = udf_node->i_flags & IN_MODIFIED;
5724 }
5725 if (flags == 0)
5726 return 0;
5727
5728 /* determine if we need to write sync or async */
5729 waitfor = 0;
5730 if ((flags & IN_MODIFIED) && (mnt_async == 0)) {
5731 /* sync mounted */
5732 waitfor = updflags & UPDATE_WAIT;
5733 if (updflags & UPDATE_DIROP)
5734 waitfor |= UPDATE_WAIT;
5735 }
5736 if (waitfor)
5737 return VOP_FSYNC(vp, FSCRED, FSYNC_WAIT, 0,0);
5738
5739 return 0;
5740 }
5741
5742
5743 /* --------------------------------------------------------------------- */
5744
5745
5746 /*
5747 * Read one fid and process it into a dirent and advance to the next (*fid)
5748 * has to be allocated a logical block in size, (*dirent) struct dirent length
5749 */
5750
5751 int
5752 udf_read_fid_stream(struct vnode *vp, uint64_t *offset,
5753 struct fileid_desc *fid, struct dirent *dirent)
5754 {
5755 struct udf_node *dir_node = VTOI(vp);
5756 struct udf_mount *ump = dir_node->ump;
5757 struct file_entry *fe = dir_node->fe;
5758 struct extfile_entry *efe = dir_node->efe;
5759 uint32_t fid_size, lb_size;
5760 uint64_t file_size;
5761 char *fid_name;
5762 int enough, error;
5763
5764 assert(fid);
5765 assert(dirent);
5766 assert(dir_node);
5767 assert(offset);
5768 assert(*offset != 1);
5769
5770 DPRINTF(FIDS, ("read_fid_stream called at offset %"PRIu64"\n", *offset));
5771 /* check if we're past the end of the directory */
5772 if (fe) {
5773 file_size = udf_rw64(fe->inf_len);
5774 } else {
5775 assert(dir_node->efe);
5776 file_size = udf_rw64(efe->inf_len);
5777 }
5778 if (*offset >= file_size)
5779 return EINVAL;
5780
5781 /* get maximum length of FID descriptor */
5782 lb_size = udf_rw32(ump->logical_vol->lb_size);
5783
5784 /* initialise return values */
5785 fid_size = 0;
5786 memset(dirent, 0, sizeof(struct dirent));
5787 memset(fid, 0, lb_size);
5788
5789 enough = (file_size - (*offset) >= UDF_FID_SIZE);
5790 if (!enough) {
5791 /* short dir ... */
5792 return EIO;
5793 }
5794
5795 error = vn_rdwr(UIO_READ, vp,
5796 fid, MIN(file_size - (*offset), lb_size), *offset,
5797 UIO_SYSSPACE, IO_ALTSEMANTICS | IO_NODELOCKED, FSCRED,
5798 NULL, NULL);
5799 if (error)
5800 return error;
5801
5802 DPRINTF(FIDS, ("\tfid piece read in fine\n"));
5803 /*
5804 * Check if we got a whole descriptor.
5805 * TODO Try to `resync' directory stream when something is very wrong.
5806 */
5807
5808 /* check if our FID header is OK */
5809 error = udf_check_tag(fid);
5810 if (error) {
5811 goto brokendir;
5812 }
5813 DPRINTF(FIDS, ("\ttag check ok\n"));
5814
5815 if (udf_rw16(fid->tag.id) != TAGID_FID) {
5816 error = EIO;
5817 goto brokendir;
5818 }
5819 DPRINTF(FIDS, ("\ttag checked ok: got TAGID_FID\n"));
5820
5821 /* check for length */
5822 fid_size = udf_fidsize(fid);
5823 enough = (file_size - (*offset) >= fid_size);
5824 if (!enough) {
5825 error = EIO;
5826 goto brokendir;
5827 }
5828 DPRINTF(FIDS, ("\tthe complete fid is read in\n"));
5829
5830 /* check FID contents */
5831 error = udf_check_tag_payload((union dscrptr *) fid, lb_size);
5832 brokendir:
5833 if (error) {
5834 /* note that is sometimes a bit quick to report */
5835 printf("BROKEN DIRECTORY ENTRY\n");
5836 /* RESYNC? */
5837 /* TODO: use udf_resync_fid_stream */
5838 return EIO;
5839 }
5840 DPRINTF(FIDS, ("\tpayload checked ok\n"));
5841
5842 /* we got a whole and valid descriptor! */
5843 DPRINTF(FIDS, ("\tinterpret FID\n"));
5844
5845 /* create resulting dirent structure */
5846 fid_name = (char *) fid->data + udf_rw16(fid->l_iu);
5847 udf_to_unix_name(dirent->d_name, MAXNAMLEN,
5848 fid_name, fid->l_fi, &ump->logical_vol->desc_charset);
5849
5850 /* '..' has no name, so provide one */
5851 if (fid->file_char & UDF_FILE_CHAR_PAR)
5852 strcpy(dirent->d_name, "..");
5853
5854 dirent->d_fileno = udf_calchash(&fid->icb); /* inode hash XXX */
5855 dirent->d_namlen = strlen(dirent->d_name);
5856 dirent->d_reclen = _DIRENT_SIZE(dirent);
5857
5858 /*
5859 * Note that its not worth trying to go for the filetypes now... its
5860 * too expensive too
5861 */
5862 dirent->d_type = DT_UNKNOWN;
5863
5864 /* initial guess for filetype we can make */
5865 if (fid->file_char & UDF_FILE_CHAR_DIR)
5866 dirent->d_type = DT_DIR;
5867
5868 /* advance */
5869 *offset += fid_size;
5870
5871 return error;
5872 }
5873
5874
5875 /* --------------------------------------------------------------------- */
5876
5877 static void
5878 udf_sync_pass(struct udf_mount *ump, kauth_cred_t cred, int waitfor,
5879 int pass, int *ndirty)
5880 {
5881 struct udf_node *udf_node, *n_udf_node;
5882 struct vnode *vp;
5883 int vdirty, error;
5884 int on_type, on_flags, on_vnode;
5885
5886 derailed:
5887 KASSERT(mutex_owned(&mntvnode_lock));
5888
5889 DPRINTF(SYNC, ("sync_pass %d\n", pass));
5890 udf_node = LIST_FIRST(&ump->sorted_udf_nodes);
5891 for (;udf_node; udf_node = n_udf_node) {
5892 DPRINTF(SYNC, ("."));
5893
5894 udf_node->i_flags &= ~IN_SYNCED;
5895 vp = udf_node->vnode;
5896
5897 mutex_enter(&vp->v_interlock);
5898 n_udf_node = LIST_NEXT(udf_node, sortchain);
5899 if (n_udf_node)
5900 n_udf_node->i_flags |= IN_SYNCED;
5901
5902 /* system nodes are not synced this way */
5903 if (vp->v_vflag & VV_SYSTEM) {
5904 mutex_exit(&vp->v_interlock);
5905 continue;
5906 }
5907
5908 /* check if its dirty enough to even try */
5909 on_type = (waitfor == MNT_LAZY || vp->v_type == VNON);
5910 on_flags = ((udf_node->i_flags &
5911 (IN_ACCESSED | IN_UPDATE | IN_MODIFIED)) == 0);
5912 on_vnode = LIST_EMPTY(&vp->v_dirtyblkhd)
5913 && UVM_OBJ_IS_CLEAN(&vp->v_uobj);
5914 if (on_type || (on_flags || on_vnode)) { /* XXX */
5915 /* not dirty (enough?) */
5916 mutex_exit(&vp->v_interlock);
5917 continue;
5918 }
5919
5920 mutex_exit(&mntvnode_lock);
5921 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
5922 if (error) {
5923 mutex_enter(&mntvnode_lock);
5924 if (error == ENOENT)
5925 goto derailed;
5926 *ndirty += 1;
5927 continue;
5928 }
5929
5930 switch (pass) {
5931 case 1:
5932 VOP_FSYNC(vp, cred, 0 | FSYNC_DATAONLY,0,0);
5933 break;
5934 case 2:
5935 vdirty = vp->v_numoutput;
5936 if (vp->v_tag == VT_UDF)
5937 vdirty += udf_node->outstanding_bufs +
5938 udf_node->outstanding_nodedscr;
5939 if (vdirty == 0)
5940 VOP_FSYNC(vp, cred, 0,0,0);
5941 *ndirty += vdirty;
5942 break;
5943 case 3:
5944 vdirty = vp->v_numoutput;
5945 if (vp->v_tag == VT_UDF)
5946 vdirty += udf_node->outstanding_bufs +
5947 udf_node->outstanding_nodedscr;
5948 *ndirty += vdirty;
5949 break;
5950 }
5951
5952 vput(vp);
5953 mutex_enter(&mntvnode_lock);
5954 }
5955 DPRINTF(SYNC, ("END sync_pass %d\n", pass));
5956 }
5957
5958
5959 void
5960 udf_do_sync(struct udf_mount *ump, kauth_cred_t cred, int waitfor)
5961 {
5962 int dummy, ndirty;
5963
5964 mutex_enter(&mntvnode_lock);
5965 recount:
5966 dummy = 0;
5967 DPRINTF(CALL, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
5968 DPRINTF(SYNC, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
5969 udf_sync_pass(ump, cred, waitfor, 1, &dummy);
5970
5971 DPRINTF(CALL, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
5972 DPRINTF(SYNC, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
5973 udf_sync_pass(ump, cred, waitfor, 2, &dummy);
5974
5975 if (waitfor == MNT_WAIT) {
5976 ndirty = ump->devvp->v_numoutput;
5977 DPRINTF(NODE, ("counting pending blocks: on devvp %d\n",
5978 ndirty));
5979 udf_sync_pass(ump, cred, waitfor, 3, &ndirty);
5980 DPRINTF(NODE, ("counted num dirty pending blocks %d\n",
5981 ndirty));
5982
5983 if (ndirty) {
5984 /* 1/4 second wait */
5985 cv_timedwait(&ump->dirtynodes_cv, &mntvnode_lock,
5986 hz/4);
5987 goto recount;
5988 }
5989 }
5990
5991 mutex_exit(&mntvnode_lock);
5992 }
5993
5994 /* --------------------------------------------------------------------- */
5995
5996 /*
5997 * Read and write file extent in/from the buffer.
5998 *
5999 * The splitup of the extent into seperate request-buffers is to minimise
6000 * copying around as much as possible.
6001 *
6002 * block based file reading and writing
6003 */
6004
6005 static int
6006 udf_read_internal(struct udf_node *node, uint8_t *blob)
6007 {
6008 struct udf_mount *ump;
6009 struct file_entry *fe = node->fe;
6010 struct extfile_entry *efe = node->efe;
6011 uint64_t inflen;
6012 uint32_t sector_size;
6013 uint8_t *pos;
6014 int icbflags, addr_type;
6015
6016 /* get extent and do some paranoia checks */
6017 ump = node->ump;
6018 sector_size = ump->discinfo.sector_size;
6019
6020 if (fe) {
6021 inflen = udf_rw64(fe->inf_len);
6022 pos = &fe->data[0] + udf_rw32(fe->l_ea);
6023 icbflags = udf_rw16(fe->icbtag.flags);
6024 } else {
6025 assert(node->efe);
6026 inflen = udf_rw64(efe->inf_len);
6027 pos = &efe->data[0] + udf_rw32(efe->l_ea);
6028 icbflags = udf_rw16(efe->icbtag.flags);
6029 }
6030 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
6031
6032 assert(addr_type == UDF_ICB_INTERN_ALLOC);
6033 assert(inflen < sector_size);
6034
6035 /* copy out info */
6036 memset(blob, 0, sector_size);
6037 memcpy(blob, pos, inflen);
6038
6039 return 0;
6040 }
6041
6042
6043 static int
6044 udf_write_internal(struct udf_node *node, uint8_t *blob)
6045 {
6046 struct udf_mount *ump;
6047 struct file_entry *fe = node->fe;
6048 struct extfile_entry *efe = node->efe;
6049 uint64_t inflen;
6050 uint32_t sector_size;
6051 uint8_t *pos;
6052 int icbflags, addr_type;
6053
6054 /* get extent and do some paranoia checks */
6055 ump = node->ump;
6056 sector_size = ump->discinfo.sector_size;
6057
6058 if (fe) {
6059 inflen = udf_rw64(fe->inf_len);
6060 pos = &fe->data[0] + udf_rw32(fe->l_ea);
6061 icbflags = udf_rw16(fe->icbtag.flags);
6062 } else {
6063 assert(node->efe);
6064 inflen = udf_rw64(efe->inf_len);
6065 pos = &efe->data[0] + udf_rw32(efe->l_ea);
6066 icbflags = udf_rw16(efe->icbtag.flags);
6067 }
6068 addr_type = icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK;
6069
6070 assert(addr_type == UDF_ICB_INTERN_ALLOC);
6071 assert(inflen < sector_size);
6072
6073 /* copy in blob */
6074 /* memset(pos, 0, inflen); */
6075 memcpy(pos, blob, inflen);
6076
6077 return 0;
6078 }
6079
6080
6081 void
6082 udf_read_filebuf(struct udf_node *udf_node, struct buf *buf)
6083 {
6084 struct buf *nestbuf;
6085 struct udf_mount *ump = udf_node->ump;
6086 uint64_t *mapping;
6087 uint64_t run_start;
6088 uint32_t sector_size;
6089 uint32_t buf_offset, sector, rbuflen, rblk;
6090 uint32_t from, lblkno;
6091 uint32_t sectors;
6092 uint8_t *buf_pos;
6093 int error, run_length, isdir, what;
6094
6095 sector_size = udf_node->ump->discinfo.sector_size;
6096
6097 from = buf->b_blkno;
6098 sectors = buf->b_bcount / sector_size;
6099
6100 isdir = (udf_node->vnode->v_type == VDIR);
6101 what = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
6102
6103 /* assure we have enough translation slots */
6104 KASSERT(buf->b_bcount / sector_size <= UDF_MAX_MAPPINGS);
6105 KASSERT(MAXPHYS / sector_size <= UDF_MAX_MAPPINGS);
6106
6107 if (sectors > UDF_MAX_MAPPINGS) {
6108 printf("udf_read_filebuf: implementation limit on bufsize\n");
6109 buf->b_error = EIO;
6110 biodone(buf);
6111 return;
6112 }
6113
6114 mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
6115
6116 error = 0;
6117 DPRINTF(READ, ("\ttranslate %d-%d\n", from, sectors));
6118 error = udf_translate_file_extent(udf_node, from, sectors, mapping);
6119 if (error) {
6120 buf->b_error = error;
6121 biodone(buf);
6122 goto out;
6123 }
6124 DPRINTF(READ, ("\ttranslate extent went OK\n"));
6125
6126 /* pre-check if its an internal */
6127 if (*mapping == UDF_TRANS_INTERN) {
6128 error = udf_read_internal(udf_node, (uint8_t *) buf->b_data);
6129 if (error)
6130 buf->b_error = error;
6131 biodone(buf);
6132 goto out;
6133 }
6134 DPRINTF(READ, ("\tnot intern\n"));
6135
6136 #ifdef DEBUG
6137 if (udf_verbose & UDF_DEBUG_TRANSLATE) {
6138 printf("Returned translation table:\n");
6139 for (sector = 0; sector < sectors; sector++) {
6140 printf("%d : %"PRIu64"\n", sector, mapping[sector]);
6141 }
6142 }
6143 #endif
6144
6145 /* request read-in of data from disc sheduler */
6146 buf->b_resid = buf->b_bcount;
6147 for (sector = 0; sector < sectors; sector++) {
6148 buf_offset = sector * sector_size;
6149 buf_pos = (uint8_t *) buf->b_data + buf_offset;
6150 DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
6151
6152 /* check if its zero or unmapped to stop reading */
6153 switch (mapping[sector]) {
6154 case UDF_TRANS_UNMAPPED:
6155 case UDF_TRANS_ZERO:
6156 /* copy zero sector TODO runlength like below */
6157 memset(buf_pos, 0, sector_size);
6158 DPRINTF(READ, ("\treturning zero sector\n"));
6159 nestiobuf_done(buf, sector_size, 0);
6160 break;
6161 default :
6162 DPRINTF(READ, ("\tread sector "
6163 "%"PRIu64"\n", mapping[sector]));
6164
6165 lblkno = from + sector;
6166 run_start = mapping[sector];
6167 run_length = 1;
6168 while (sector < sectors-1) {
6169 if (mapping[sector+1] != mapping[sector]+1)
6170 break;
6171 run_length++;
6172 sector++;
6173 }
6174
6175 /*
6176 * nest an iobuf and mark it for async reading. Since
6177 * we're using nested buffers, they can't be cached by
6178 * design.
6179 */
6180 rbuflen = run_length * sector_size;
6181 rblk = run_start * (sector_size/DEV_BSIZE);
6182
6183 nestbuf = getiobuf(NULL, true);
6184 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
6185 /* nestbuf is B_ASYNC */
6186
6187 /* identify this nestbuf */
6188 nestbuf->b_lblkno = lblkno;
6189 assert(nestbuf->b_vp == udf_node->vnode);
6190
6191 /* CD shedules on raw blkno */
6192 nestbuf->b_blkno = rblk;
6193 nestbuf->b_proc = NULL;
6194 nestbuf->b_rawblkno = rblk;
6195 nestbuf->b_udf_c_type = what;
6196
6197 udf_discstrat_queuebuf(ump, nestbuf);
6198 }
6199 }
6200 out:
6201 /* if we're synchronously reading, wait for the completion */
6202 if ((buf->b_flags & B_ASYNC) == 0)
6203 biowait(buf);
6204
6205 DPRINTF(READ, ("\tend of read_filebuf\n"));
6206 free(mapping, M_TEMP);
6207 return;
6208 }
6209
6210
6211 void
6212 udf_write_filebuf(struct udf_node *udf_node, struct buf *buf)
6213 {
6214 struct buf *nestbuf;
6215 struct udf_mount *ump = udf_node->ump;
6216 uint64_t *mapping;
6217 uint64_t run_start;
6218 uint32_t lb_size;
6219 uint32_t buf_offset, lb_num, rbuflen, rblk;
6220 uint32_t from, lblkno;
6221 uint32_t num_lb;
6222 uint8_t *buf_pos;
6223 int error, run_length, isdir, what, s;
6224
6225 lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
6226
6227 from = buf->b_blkno;
6228 num_lb = buf->b_bcount / lb_size;
6229
6230 isdir = (udf_node->vnode->v_type == VDIR);
6231 what = isdir ? UDF_C_FIDS : UDF_C_USERDATA;
6232
6233 /* assure we have enough translation slots */
6234 KASSERT(buf->b_bcount / lb_size <= UDF_MAX_MAPPINGS);
6235 KASSERT(MAXPHYS / lb_size <= UDF_MAX_MAPPINGS);
6236
6237 if (num_lb > UDF_MAX_MAPPINGS) {
6238 printf("udf_write_filebuf: implementation limit on bufsize\n");
6239 buf->b_error = EIO;
6240 biodone(buf);
6241 return;
6242 }
6243
6244 mapping = malloc(sizeof(*mapping) * UDF_MAX_MAPPINGS, M_TEMP, M_WAITOK);
6245
6246 error = 0;
6247 DPRINTF(WRITE, ("\ttranslate %d-%d\n", from, num_lb));
6248 error = udf_translate_file_extent(udf_node, from, num_lb, mapping);
6249 if (error) {
6250 buf->b_error = error;
6251 biodone(buf);
6252 goto out;
6253 }
6254 DPRINTF(WRITE, ("\ttranslate extent went OK\n"));
6255
6256 /* if its internally mapped, we can write it in the descriptor itself */
6257 if (*mapping == UDF_TRANS_INTERN) {
6258 /* TODO paranoia check if we ARE going to have enough space */
6259 error = udf_write_internal(udf_node, (uint8_t *) buf->b_data);
6260 if (error)
6261 buf->b_error = error;
6262 biodone(buf);
6263 goto out;
6264 }
6265 DPRINTF(WRITE, ("\tnot intern\n"));
6266
6267 /* request write out of data to disc sheduler */
6268 buf->b_resid = buf->b_bcount;
6269 for (lb_num = 0; lb_num < num_lb; lb_num++) {
6270 buf_offset = lb_num * lb_size;
6271 buf_pos = (uint8_t *) buf->b_data + buf_offset;
6272 DPRINTF(WRITE, ("\tprocessing rel lb_num %d\n", lb_num));
6273
6274 /*
6275 * Mappings are not that important here. Just before we write
6276 * the lb_num we late-allocate them when needed and update the
6277 * mapping in the udf_node.
6278 */
6279
6280 /* XXX why not ignore the mapping altogether ? */
6281 /* TODO estimate here how much will be late-allocated */
6282 DPRINTF(WRITE, ("\twrite lb_num "
6283 "%"PRIu64, mapping[lb_num]));
6284
6285 lblkno = from + lb_num;
6286 run_start = mapping[lb_num];
6287 run_length = 1;
6288 while (lb_num < num_lb-1) {
6289 if (mapping[lb_num+1] != mapping[lb_num]+1)
6290 if (mapping[lb_num+1] != mapping[lb_num])
6291 break;
6292 run_length++;
6293 lb_num++;
6294 }
6295 DPRINTF(WRITE, ("+ %d\n", run_length));
6296
6297 /* nest an iobuf on the master buffer for the extent */
6298 rbuflen = run_length * lb_size;
6299 rblk = run_start * (lb_size/DEV_BSIZE);
6300
6301 #if 0
6302 /* if its zero or unmapped, our blknr gets -1 for unmapped */
6303 switch (mapping[lb_num]) {
6304 case UDF_TRANS_UNMAPPED:
6305 case UDF_TRANS_ZERO:
6306 rblk = -1;
6307 break;
6308 default:
6309 rblk = run_start * (lb_size/DEV_BSIZE);
6310 break;
6311 }
6312 #endif
6313
6314 nestbuf = getiobuf(NULL, true);
6315 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
6316 /* nestbuf is B_ASYNC */
6317
6318 /* identify this nestbuf */
6319 nestbuf->b_lblkno = lblkno;
6320 KASSERT(nestbuf->b_vp == udf_node->vnode);
6321
6322 /* CD shedules on raw blkno */
6323 nestbuf->b_blkno = rblk;
6324 nestbuf->b_proc = NULL;
6325 nestbuf->b_rawblkno = rblk;
6326 nestbuf->b_udf_c_type = what;
6327
6328 /* increment our outstanding bufs counter */
6329 s = splbio();
6330 udf_node->outstanding_bufs++;
6331 splx(s);
6332
6333 udf_discstrat_queuebuf(ump, nestbuf);
6334 }
6335 out:
6336 /* if we're synchronously writing, wait for the completion */
6337 if ((buf->b_flags & B_ASYNC) == 0)
6338 biowait(buf);
6339
6340 DPRINTF(WRITE, ("\tend of write_filebuf\n"));
6341 free(mapping, M_TEMP);
6342 return;
6343 }
6344
6345 /* --------------------------------------------------------------------- */
6346
6347
6348