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