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