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