1 /* $NetBSD: udf_subr.c,v 1.100 2009/12/23 09:17:41 mbalmer Exp $ */
4 * Copyright (c) 2006, 2008 Reinoud Zandijk
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: udf_subr.c,v 1.100 2009/12/23 09:17:41 mbalmer Exp $");
36 #if defined(_KERNEL_OPT)
37 #include "opt_compat_netbsd.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <sys/namei.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <miscfs/genfs/genfs_node.h>
48 #include <sys/mount.h>
51 #include <sys/device.h>
52 #include <sys/disklabel.h>
53 #include <sys/ioctl.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
58 #include <sys/kauth.h>
59 #include <fs/unicode.h>
60 #include <dev/clock_subr.h>
62 #include <fs/udf/ecma167-udf.h>
63 #include <fs/udf/udf_mount.h>
64 #include <sys/dirhash.h>
68 #include "udf_bswap.h"
71 #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
73 #define UDF_SET_SYSTEMFILE(vp) \
74 /* XXXAD Is the vnode locked? */ \
75 (vp)->v_vflag |= VV_SYSTEM; \
79 extern int syncer_maxdelay; /* maximum delay time */
80 extern int (**udf_vnodeop_p
)(void *);
82 /* --------------------------------------------------------------------- */
89 udf_dumpblob(boid
*blob
, uint32_t dlen
)
93 printf("blob = %p\n", blob
);
94 printf("dump of %d bytes\n", dlen
);
96 for (i
= 0; i
< dlen
; i
+ = 16) {
98 for (j
= 0; j
< 16; j
++) {
100 printf("%02x ", blob
[i
+j
]);
105 for (j
= 0; j
< 16; j
++) {
107 if (blob
[i
+j
]>32 && blob
[i
+j
]! = 127) {
108 printf("%c", blob
[i
+j
]);
122 udf_dump_discinfo(struct udf_mount
*ump
)
125 struct mmc_discinfo
*di
= &ump
->discinfo
;
127 if ((udf_verbose
& UDF_DEBUG_VOLUMES
) == 0)
130 printf("Device/media info :\n");
131 printf("\tMMC profile 0x%02x\n", di
->mmc_profile
);
132 printf("\tderived class %d\n", di
->mmc_class
);
133 printf("\tsector size %d\n", di
->sector_size
);
134 printf("\tdisc state %d\n", di
->disc_state
);
135 printf("\tlast ses state %d\n", di
->last_session_state
);
136 printf("\tbg format state %d\n", di
->bg_format_state
);
137 printf("\tfrst track %d\n", di
->first_track
);
138 printf("\tfst on last ses %d\n", di
->first_track_last_session
);
139 printf("\tlst on last ses %d\n", di
->last_track_last_session
);
140 printf("\tlink block penalty %d\n", di
->link_block_penalty
);
141 snprintb(bits
, sizeof(bits
), MMC_DFLAGS_FLAGBITS
, di
->disc_flags
);
142 printf("\tdisc flags %s\n", bits
);
143 printf("\tdisc id %x\n", di
->disc_id
);
144 printf("\tdisc barcode %"PRIx64
"\n", di
->disc_barcode
);
146 printf("\tnum sessions %d\n", di
->num_sessions
);
147 printf("\tnum tracks %d\n", di
->num_tracks
);
149 snprintb(bits
, sizeof(bits
), MMC_CAP_FLAGBITS
, di
->mmc_cur
);
150 printf("\tcapabilities cur %s\n", bits
);
151 snprintb(bits
, sizeof(bits
), MMC_CAP_FLAGBITS
, di
->mmc_cap
);
152 printf("\tcapabilities cap %s\n", bits
);
156 udf_dump_trackinfo(struct mmc_trackinfo
*trackinfo
)
160 if ((udf_verbose
& UDF_DEBUG_VOLUMES
) == 0)
163 printf("Trackinfo for track %d:\n", trackinfo
->tracknr
);
164 printf("\tsessionnr %d\n", trackinfo
->sessionnr
);
165 printf("\ttrack mode %d\n", trackinfo
->track_mode
);
166 printf("\tdata mode %d\n", trackinfo
->data_mode
);
167 snprintb(bits
, sizeof(bits
), MMC_TRACKINFO_FLAGBITS
, trackinfo
->flags
);
168 printf("\tflags %s\n", bits
);
170 printf("\ttrack start %d\n", trackinfo
->track_start
);
171 printf("\tnext_writable %d\n", trackinfo
->next_writable
);
172 printf("\tfree_blocks %d\n", trackinfo
->free_blocks
);
173 printf("\tpacket_size %d\n", trackinfo
->packet_size
);
174 printf("\ttrack size %d\n", trackinfo
->track_size
);
175 printf("\tlast recorded block %d\n", trackinfo
->last_recorded
);
179 #define udf_dump_discinfo(a);
180 #define udf_dump_trackinfo(a);
184 /* --------------------------------------------------------------------- */
186 /* not called often */
188 udf_update_discinfo(struct udf_mount
*ump
)
190 struct vnode
*devvp
= ump
->devvp
;
191 struct partinfo dpart
;
192 struct mmc_discinfo
*di
;
195 DPRINTF(VOLUMES
, ("read/update disc info\n"));
197 memset(di
, 0, sizeof(struct mmc_discinfo
));
199 /* check if we're on a MMC capable device, i.e. CD/DVD */
200 error
= VOP_IOCTL(devvp
, MMCGETDISCINFO
, di
, FKIOCTL
, NOCRED
);
202 udf_dump_discinfo(ump
);
206 /* disc partition support */
207 error
= VOP_IOCTL(devvp
, DIOCGPART
, &dpart
, FREAD
, NOCRED
);
211 /* set up a disc info profile for partitions */
212 di
->mmc_profile
= 0x01; /* disc type */
213 di
->mmc_class
= MMC_CLASS_DISC
;
214 di
->disc_state
= MMC_STATE_CLOSED
;
215 di
->last_session_state
= MMC_STATE_CLOSED
;
216 di
->bg_format_state
= MMC_BGFSTATE_COMPLETED
;
217 di
->link_block_penalty
= 0;
219 di
->mmc_cur
= MMC_CAP_RECORDABLE
| MMC_CAP_REWRITABLE
|
220 MMC_CAP_ZEROLINKBLK
| MMC_CAP_HW_DEFECTFREE
;
221 di
->mmc_cap
= di
->mmc_cur
;
222 di
->disc_flags
= MMC_DFLAGS_UNRESTRICTED
;
224 /* TODO problem with last_possible_lba on resizable VND; request */
225 di
->last_possible_lba
= dpart
.part
->p_size
;
226 di
->sector_size
= dpart
.disklab
->d_secsize
;
228 di
->num_sessions
= 1;
232 di
->first_track_last_session
= di
->last_track_last_session
= 1;
234 udf_dump_discinfo(ump
);
240 udf_update_trackinfo(struct udf_mount
*ump
, struct mmc_trackinfo
*ti
)
242 struct vnode
*devvp
= ump
->devvp
;
243 struct mmc_discinfo
*di
= &ump
->discinfo
;
246 DPRINTF(VOLUMES
, ("read track info\n"));
248 class = di
->mmc_class
;
249 if (class != MMC_CLASS_DISC
) {
250 /* tracknr specified in struct ti */
251 error
= VOP_IOCTL(devvp
, MMCGETTRACKINFO
, ti
, FKIOCTL
, NOCRED
);
255 /* disc partition support */
256 if (ti
->tracknr
!= 1)
259 /* create fake ti (TODO check for resized vnds) */
262 ti
->track_mode
= 0; /* XXX */
263 ti
->data_mode
= 0; /* XXX */
264 ti
->flags
= MMC_TRACKINFO_LRA_VALID
| MMC_TRACKINFO_NWA_VALID
;
269 /* TODO support for resizable vnd */
270 ti
->track_size
= di
->last_possible_lba
;
271 ti
->next_writable
= di
->last_possible_lba
;
272 ti
->last_recorded
= ti
->next_writable
;
280 udf_setup_writeparams(struct udf_mount
*ump
)
282 struct mmc_writeparams mmc_writeparams
;
285 if (ump
->discinfo
.mmc_class
== MMC_CLASS_DISC
)
289 * only CD burning normally needs setting up, but other disc types
290 * might need other settings to be made. The MMC framework will set up
291 * the nessisary recording parameters according to the disc
292 * characteristics read in. Modifications can be made in the discinfo
293 * structure passed to change the nature of the disc.
296 memset(&mmc_writeparams
, 0, sizeof(struct mmc_writeparams
));
297 mmc_writeparams
.mmc_class
= ump
->discinfo
.mmc_class
;
298 mmc_writeparams
.mmc_cur
= ump
->discinfo
.mmc_cur
;
301 * UDF dictates first track to determine track mode for the whole
302 * disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1]
303 * To prevent problems with a `reserved' track in front we start with
304 * the 2nd track and if that is not valid, go for the 1st.
306 mmc_writeparams
.tracknr
= 2;
307 mmc_writeparams
.data_mode
= MMC_DATAMODE_DEFAULT
; /* XA disc */
308 mmc_writeparams
.track_mode
= MMC_TRACKMODE_DEFAULT
; /* data */
310 error
= VOP_IOCTL(ump
->devvp
, MMCSETUPWRITEPARAMS
, &mmc_writeparams
,
313 mmc_writeparams
.tracknr
= 1;
314 error
= VOP_IOCTL(ump
->devvp
, MMCSETUPWRITEPARAMS
,
315 &mmc_writeparams
, FKIOCTL
, NOCRED
);
322 udf_synchronise_caches(struct udf_mount
*ump
)
324 struct mmc_op mmc_op
;
326 DPRINTF(CALL
, ("udf_synchronise_caches()\n"));
328 if (ump
->vfs_mountp
->mnt_flag
& MNT_RDONLY
)
331 /* discs are done now */
332 if (ump
->discinfo
.mmc_class
== MMC_CLASS_DISC
)
335 memset(&mmc_op
, 0, sizeof(struct mmc_op
));
336 mmc_op
.operation
= MMC_OP_SYNCHRONISECACHE
;
338 /* ignore return code */
339 (void) VOP_IOCTL(ump
->devvp
, MMCOP
, &mmc_op
, FKIOCTL
, NOCRED
);
344 /* --------------------------------------------------------------------- */
346 /* track/session searching for mounting */
348 udf_search_tracks(struct udf_mount
*ump
, struct udf_args
*args
,
349 int *first_tracknr
, int *last_tracknr
)
351 struct mmc_trackinfo trackinfo
;
352 uint32_t tracknr
, start_track
, num_tracks
;
355 /* if negative, sessionnr is relative to last session */
356 if (args
->sessionnr
< 0) {
357 args
->sessionnr
+= ump
->discinfo
.num_sessions
;
361 if (args
->sessionnr
< 0)
363 if (args
->sessionnr
> ump
->discinfo
.num_sessions
)
364 args
->sessionnr
= ump
->discinfo
.num_sessions
;
366 /* search the tracks for this session, zero session nr indicates last */
367 if (args
->sessionnr
== 0)
368 args
->sessionnr
= ump
->discinfo
.num_sessions
;
369 if (ump
->discinfo
.last_session_state
== MMC_STATE_EMPTY
)
373 if (args
->sessionnr
< 0)
376 /* search the first and last track of the specified session */
377 num_tracks
= ump
->discinfo
.num_tracks
;
378 start_track
= ump
->discinfo
.first_track
;
380 /* search for first track of this session */
381 for (tracknr
= start_track
; tracknr
<= num_tracks
; tracknr
++) {
383 trackinfo
.tracknr
= tracknr
;
384 error
= udf_update_trackinfo(ump
, &trackinfo
);
388 if (trackinfo
.sessionnr
== args
->sessionnr
)
391 *first_tracknr
= tracknr
;
393 /* search for last track of this session */
394 for (;tracknr
<= num_tracks
; tracknr
++) {
396 trackinfo
.tracknr
= tracknr
;
397 error
= udf_update_trackinfo(ump
, &trackinfo
);
398 if (error
|| (trackinfo
.sessionnr
!= args
->sessionnr
)) {
403 if (tracknr
> num_tracks
)
406 *last_tracknr
= tracknr
;
408 if (*last_tracknr
< *first_tracknr
) {
409 printf( "udf_search_tracks: sanity check on drive+disc failed, "
410 "drive returned garbage\n");
414 assert(*last_tracknr
>= *first_tracknr
);
420 * NOTE: this is the only routine in this file that directly peeks into the
421 * metadata file but since its at a larval state of the mount it can't hurt.
423 * XXX candidate for udf_allocation.c
424 * XXX clean me up!, change to new node reading code.
428 udf_check_track_metadata_overlap(struct udf_mount
*ump
,
429 struct mmc_trackinfo
*trackinfo
)
431 struct part_desc
*part
;
432 struct file_entry
*fe
;
433 struct extfile_entry
*efe
;
434 struct short_ad
*s_ad
;
435 struct long_ad
*l_ad
;
436 uint32_t track_start
, track_end
;
437 uint32_t phys_part_start
, phys_part_end
, part_start
, part_end
;
438 uint32_t sector_size
, len
, alloclen
, plb_num
;
440 int addr_type
, icblen
, icbflags
, flags
;
442 /* get our track extents */
443 track_start
= trackinfo
->track_start
;
444 track_end
= track_start
+ trackinfo
->track_size
;
446 /* get our base partition extent */
447 KASSERT(ump
->node_part
== ump
->fids_part
);
448 part
= ump
->partitions
[ump
->node_part
];
449 phys_part_start
= udf_rw32(part
->start_loc
);
450 phys_part_end
= phys_part_start
+ udf_rw32(part
->part_len
);
452 /* no use if its outside the physical partition */
453 if ((phys_part_start
>= track_end
) || (phys_part_end
< track_start
))
457 * now follow all extents in the fe/efe to see if they refer to this
461 sector_size
= ump
->discinfo
.sector_size
;
463 /* XXX should we claim exclusive access to the metafile ? */
464 /* TODO: move to new node read code */
465 fe
= ump
->metadata_node
->fe
;
466 efe
= ump
->metadata_node
->efe
;
468 alloclen
= udf_rw32(fe
->l_ad
);
469 pos
= &fe
->data
[0] + udf_rw32(fe
->l_ea
);
470 icbflags
= udf_rw16(fe
->icbtag
.flags
);
473 alloclen
= udf_rw32(efe
->l_ad
);
474 pos
= &efe
->data
[0] + udf_rw32(efe
->l_ea
);
475 icbflags
= udf_rw16(efe
->icbtag
.flags
);
477 addr_type
= icbflags
& UDF_ICB_TAG_FLAGS_ALLOC_MASK
;
480 if (addr_type
== UDF_ICB_SHORT_ALLOC
) {
481 icblen
= sizeof(struct short_ad
);
482 s_ad
= (struct short_ad
*) pos
;
483 len
= udf_rw32(s_ad
->len
);
484 plb_num
= udf_rw32(s_ad
->lb_num
);
486 /* should not be present, but why not */
487 icblen
= sizeof(struct long_ad
);
488 l_ad
= (struct long_ad
*) pos
;
489 len
= udf_rw32(l_ad
->len
);
490 plb_num
= udf_rw32(l_ad
->loc
.lb_num
);
491 /* pvpart_num = udf_rw16(l_ad->loc.part_num); */
494 flags
= UDF_EXT_FLAGS(len
);
495 len
= UDF_EXT_LEN(len
);
497 part_start
= phys_part_start
+ plb_num
;
498 part_end
= part_start
+ (len
/ sector_size
);
500 if ((part_start
>= track_start
) && (part_end
<= track_end
)) {
501 /* extent is enclosed within this track */
502 ump
->metadata_track
= *trackinfo
;
513 udf_search_writing_tracks(struct udf_mount
*ump
)
515 struct vnode
*devvp
= ump
->devvp
;
516 struct mmc_trackinfo trackinfo
;
517 struct mmc_op mmc_op
;
518 struct part_desc
*part
;
519 uint32_t tracknr
, start_track
, num_tracks
;
520 uint32_t track_start
, track_end
, part_start
, part_end
;
521 int node_alloc
, error
;
524 * in the CD/(HD)DVD/BD recordable device model a few tracks within
525 * the last session might be open but in the UDF device model at most
526 * three tracks can be open: a reserved track for delayed ISO VRS
527 * writing, a data track and a metadata track. We search here for the
528 * data track and the metadata track. Note that the reserved track is
529 * troublesome but can be detected by its small size of < 512 sectors.
532 /* update discinfo since it might have changed */
533 error
= udf_update_discinfo(ump
);
537 num_tracks
= ump
->discinfo
.num_tracks
;
538 start_track
= ump
->discinfo
.first_track
;
540 /* fetch info on first and possibly only track */
541 trackinfo
.tracknr
= start_track
;
542 error
= udf_update_trackinfo(ump
, &trackinfo
);
546 /* copy results to our mount point */
547 ump
->data_track
= trackinfo
;
548 ump
->metadata_track
= trackinfo
;
550 /* if not sequential, we're done */
554 for (tracknr
= start_track
;tracknr
<= num_tracks
; tracknr
++) {
556 trackinfo
.tracknr
= tracknr
;
557 error
= udf_update_trackinfo(ump
, &trackinfo
);
562 * If this track is marked damaged, ask for repair. This is an
563 * optional command, so ignore its error but report warning.
565 if (trackinfo
.flags
& MMC_TRACKINFO_DAMAGED
) {
566 memset(&mmc_op
, 0, sizeof(mmc_op
));
567 mmc_op
.operation
= MMC_OP_REPAIRTRACK
;
568 mmc_op
.mmc_profile
= ump
->discinfo
.mmc_profile
;
569 mmc_op
.tracknr
= tracknr
;
570 error
= VOP_IOCTL(devvp
, MMCOP
, &mmc_op
, FKIOCTL
, NOCRED
);
572 (void)printf("Drive can't explicitly repair "
573 "damaged track %d, but it might "
574 "autorepair\n", tracknr
);
576 /* reget track info */
577 error
= udf_update_trackinfo(ump
, &trackinfo
);
581 if ((trackinfo
.flags
& MMC_TRACKINFO_NWA_VALID
) == 0)
584 track_start
= trackinfo
.track_start
;
585 track_end
= track_start
+ trackinfo
.track_size
;
587 /* check for overlap on data partition */
588 part
= ump
->partitions
[ump
->data_part
];
589 part_start
= udf_rw32(part
->start_loc
);
590 part_end
= part_start
+ udf_rw32(part
->part_len
);
591 if ((part_start
< track_end
) && (part_end
> track_start
)) {
592 ump
->data_track
= trackinfo
;
593 /* TODO check if UDF partition data_part is writable */
596 /* check for overlap on metadata partition */
597 node_alloc
= ump
->vtop_alloc
[ump
->node_part
];
598 if ((node_alloc
== UDF_ALLOC_METASEQUENTIAL
) ||
599 (node_alloc
== UDF_ALLOC_METABITMAP
)) {
600 udf_check_track_metadata_overlap(ump
, &trackinfo
);
602 ump
->metadata_track
= trackinfo
;
606 if ((ump
->data_track
.flags
& MMC_TRACKINFO_NWA_VALID
) == 0)
609 if ((ump
->metadata_track
.flags
& MMC_TRACKINFO_NWA_VALID
) == 0)
615 /* --------------------------------------------------------------------- */
618 * Check if the blob starts with a good UDF tag. Tags are protected by a
619 * checksum over the reader except one byte at position 4 that is the checksum
624 udf_check_tag(void *blob
)
626 struct desc_tag
*tag
= blob
;
627 uint8_t *pos
, sum
, cnt
;
629 /* check TAG header checksum */
630 pos
= (uint8_t *) tag
;
633 for(cnt
= 0; cnt
< 16; cnt
++) {
638 if (sum
!= tag
->cksum
) {
639 /* bad tag header checksum; this is not a valid tag */
648 * check tag payload will check descriptor CRC as specified.
649 * If the descriptor is too long, it will return EIO otherwise EINVAL.
653 udf_check_tag_payload(void *blob
, uint32_t max_length
)
655 struct desc_tag
*tag
= blob
;
656 uint16_t crc
, crc_len
;
658 crc_len
= udf_rw16(tag
->desc_crc_len
);
660 /* check payload CRC if applicable */
664 if (crc_len
> max_length
)
667 crc
= udf_cksum(((uint8_t *) tag
) + UDF_DESC_TAG_LENGTH
, crc_len
);
668 if (crc
!= udf_rw16(tag
->desc_crc
)) {
669 /* bad payload CRC; this is a broken tag */
678 udf_validate_tag_sum(void *blob
)
680 struct desc_tag
*tag
= blob
;
681 uint8_t *pos
, sum
, cnt
;
683 /* calculate TAG header checksum */
684 pos
= (uint8_t *) tag
;
687 for(cnt
= 0; cnt
< 16; cnt
++) {
688 if (cnt
!= 4) sum
+= *pos
;
691 tag
->cksum
= sum
; /* 8 bit */
695 /* assumes sector number of descriptor to be saved already present */
697 udf_validate_tag_and_crc_sums(void *blob
)
699 struct desc_tag
*tag
= blob
;
700 uint8_t *btag
= (uint8_t *) tag
;
701 uint16_t crc
, crc_len
;
703 crc_len
= udf_rw16(tag
->desc_crc_len
);
705 /* check payload CRC if applicable */
707 crc
= udf_cksum(btag
+ UDF_DESC_TAG_LENGTH
, crc_len
);
708 tag
->desc_crc
= udf_rw16(crc
);
711 /* calculate TAG header checksum */
712 udf_validate_tag_sum(blob
);
715 /* --------------------------------------------------------------------- */
718 * XXX note the different semantics from udfclient: for FIDs it still rounds
719 * up to sectors. Use udf_fidsize() for a correct length.
723 udf_tagsize(union dscrptr
*dscr
, uint32_t lb_size
)
725 uint32_t size
, tag_id
, num_lb
, elmsz
;
727 tag_id
= udf_rw16(dscr
->tag
.id
);
731 size
= sizeof(struct logvol_desc
) - 1;
732 size
+= udf_rw32(dscr
->lvd
.mt_l
);
734 case TAGID_UNALLOC_SPACE
:
735 elmsz
= sizeof(struct extent_ad
);
736 size
= sizeof(struct unalloc_sp_desc
) - elmsz
;
737 size
+= udf_rw32(dscr
->usd
.alloc_desc_num
) * elmsz
;
740 size
= UDF_FID_SIZE
+ dscr
->fid
.l_fi
+ udf_rw16(dscr
->fid
.l_iu
);
741 size
= (size
+ 3) & ~3;
743 case TAGID_LOGVOL_INTEGRITY
:
744 size
= sizeof(struct logvol_int_desc
) - sizeof(uint32_t);
745 size
+= udf_rw32(dscr
->lvid
.l_iu
);
746 size
+= (2 * udf_rw32(dscr
->lvid
.num_part
) * sizeof(uint32_t));
748 case TAGID_SPACE_BITMAP
:
749 size
= sizeof(struct space_bitmap_desc
) - 1;
750 size
+= udf_rw32(dscr
->sbd
.num_bytes
);
752 case TAGID_SPARING_TABLE
:
753 elmsz
= sizeof(struct spare_map_entry
);
754 size
= sizeof(struct udf_sparing_table
) - elmsz
;
755 size
+= udf_rw16(dscr
->spt
.rt_l
) * elmsz
;
758 size
= sizeof(struct file_entry
);
759 size
+= udf_rw32(dscr
->fe
.l_ea
) + udf_rw32(dscr
->fe
.l_ad
)-1;
761 case TAGID_EXTFENTRY
:
762 size
= sizeof(struct extfile_entry
);
763 size
+= udf_rw32(dscr
->efe
.l_ea
) + udf_rw32(dscr
->efe
.l_ad
)-1;
766 size
= sizeof(struct fileset_desc
);
769 size
= sizeof(union dscrptr
);
773 if ((size
== 0) || (lb_size
== 0))
779 /* round up in sectors */
780 num_lb
= (size
+ lb_size
-1) / lb_size
;
781 return num_lb
* lb_size
;
786 udf_fidsize(struct fileid_desc
*fid
)
790 if (udf_rw16(fid
->tag
.id
) != TAGID_FID
)
791 panic("got udf_fidsize on non FID\n");
793 size
= UDF_FID_SIZE
+ fid
->l_fi
+ udf_rw16(fid
->l_iu
);
794 size
= (size
+ 3) & ~3;
799 /* --------------------------------------------------------------------- */
802 udf_lock_node(struct udf_node
*udf_node
, int flag
, char const *fname
, const int lineno
)
806 mutex_enter(&udf_node
->node_mutex
);
807 /* wait until free */
808 while (udf_node
->i_flags
& IN_LOCKED
) {
809 ret
= cv_timedwait(&udf_node
->node_lock
, &udf_node
->node_mutex
, hz
/8);
810 /* TODO check if we should return error; abort */
811 if (ret
== EWOULDBLOCK
) {
812 DPRINTF(LOCKING
, ( "udf_lock_node: udf_node %p would block "
813 "wanted at %s:%d, previously locked at %s:%d\n",
814 udf_node
, fname
, lineno
,
815 udf_node
->lock_fname
, udf_node
->lock_lineno
));
819 udf_node
->i_flags
|= IN_LOCKED
| flag
;
821 udf_node
->lock_fname
= fname
;
822 udf_node
->lock_lineno
= lineno
;
824 mutex_exit(&udf_node
->node_mutex
);
829 udf_unlock_node(struct udf_node
*udf_node
, int flag
)
831 mutex_enter(&udf_node
->node_mutex
);
832 udf_node
->i_flags
&= ~(IN_LOCKED
| flag
);
833 cv_broadcast(&udf_node
->node_lock
);
834 mutex_exit(&udf_node
->node_mutex
);
838 /* --------------------------------------------------------------------- */
841 udf_read_anchor(struct udf_mount
*ump
, uint32_t sector
, struct anchor_vdp
**dst
)
845 error
= udf_read_phys_dscr(ump
, sector
, M_UDFVOLD
,
846 (union dscrptr
**) dst
);
848 /* blank terminator blocks are not allowed here */
851 if (udf_rw16((*dst
)->tag
.id
) != TAGID_ANCHOR
) {
853 free(*dst
, M_UDFVOLD
);
855 DPRINTF(VOLUMES
, ("Not an anchor\n"));
864 udf_read_anchors(struct udf_mount
*ump
)
866 struct udf_args
*args
= &ump
->mount_args
;
867 struct mmc_trackinfo first_track
;
868 struct mmc_trackinfo second_track
;
869 struct mmc_trackinfo last_track
;
870 struct anchor_vdp
**anchorsp
;
871 uint32_t track_start
;
873 uint32_t positions
[4];
874 int first_tracknr
, last_tracknr
;
875 int error
, anch
, ok
, first_anchor
;
877 /* search the first and last track of the specified session */
878 error
= udf_search_tracks(ump
, args
, &first_tracknr
, &last_tracknr
);
880 first_track
.tracknr
= first_tracknr
;
881 error
= udf_update_trackinfo(ump
, &first_track
);
884 last_track
.tracknr
= last_tracknr
;
885 error
= udf_update_trackinfo(ump
, &last_track
);
887 if ((!error
) && (first_tracknr
!= last_tracknr
)) {
888 second_track
.tracknr
= first_tracknr
+1;
889 error
= udf_update_trackinfo(ump
, &second_track
);
892 printf("UDF mount: reading disc geometry failed\n");
896 track_start
= first_track
.track_start
;
898 /* `end' is not as straitforward as start. */
899 track_end
= last_track
.track_start
900 + last_track
.track_size
- last_track
.free_blocks
- 1;
902 if (ump
->discinfo
.mmc_cur
& MMC_CAP_SEQUENTIAL
) {
903 /* end of track is not straitforward here */
904 if (last_track
.flags
& MMC_TRACKINFO_LRA_VALID
)
905 track_end
= last_track
.last_recorded
;
906 else if (last_track
.flags
& MMC_TRACKINFO_NWA_VALID
)
907 track_end
= last_track
.next_writable
908 - ump
->discinfo
.link_block_penalty
;
911 /* its no use reading a blank track */
913 if (first_track
.flags
& MMC_TRACKINFO_BLANK
)
916 /* get our packet size */
917 ump
->packet_size
= first_track
.packet_size
;
918 if (first_track
.flags
& MMC_TRACKINFO_BLANK
)
919 ump
->packet_size
= second_track
.packet_size
;
921 if (ump
->packet_size
<= 1) {
922 /* take max, but not bigger than 64 */
923 ump
->packet_size
= MAXPHYS
/ ump
->discinfo
.sector_size
;
924 ump
->packet_size
= MIN(ump
->packet_size
, 64);
926 KASSERT(ump
->packet_size
>= 1);
928 /* read anchors start+256, start+512, end-256, end */
929 positions
[0] = track_start
+256;
930 positions
[1] = track_end
-256;
931 positions
[2] = track_end
;
932 positions
[3] = track_start
+512; /* [UDF 2.60/6.11.2] */
933 /* XXX shouldn't +512 be prefered above +256 for compat with Roxio CD */
936 anchorsp
= ump
->anchors
;
937 for (anch
= first_anchor
; anch
< 4; anch
++) {
938 DPRINTF(VOLUMES
, ("Read anchor %d at sector %d\n", anch
,
940 error
= udf_read_anchor(ump
, positions
[anch
], anchorsp
);
947 /* VATs are only recorded on sequential media, but initialise */
948 ump
->first_possible_vat_location
= track_start
+ 2;
949 ump
->last_possible_vat_location
= track_end
+ last_track
.packet_size
;
954 /* --------------------------------------------------------------------- */
957 udf_get_c_type(struct udf_node
*udf_node
)
961 isdir
= (udf_node
->vnode
->v_type
== VDIR
);
962 what
= isdir
? UDF_C_FIDS
: UDF_C_USERDATA
;
965 if (udf_node
== udf_node
->ump
->metadatabitmap_node
)
966 what
= UDF_C_METADATA_SBM
;
973 udf_get_record_vpart(struct udf_mount
*ump
, int udf_c_type
)
977 vpart_num
= ump
->data_part
;
978 if (udf_c_type
== UDF_C_NODE
)
979 vpart_num
= ump
->node_part
;
980 if (udf_c_type
== UDF_C_FIDS
)
981 vpart_num
= ump
->fids_part
;
986 /* --------------------------------------------------------------------- */
988 /* we dont try to be smart; we just record the parts */
989 #define UDF_UPDATE_DSCR(name, dscr) \
991 free(name, M_UDFVOLD); \
995 udf_process_vds_descriptor(struct udf_mount
*ump
, union dscrptr
*dscr
)
997 struct part_desc
*part
;
998 uint16_t phys_part
, raw_phys_part
;
1000 DPRINTF(VOLUMES
, ("\tprocessing VDS descr %d\n",
1001 udf_rw16(dscr
->tag
.id
)));
1002 switch (udf_rw16(dscr
->tag
.id
)) {
1003 case TAGID_PRI_VOL
: /* primary partition */
1004 UDF_UPDATE_DSCR(ump
->primary_vol
, &dscr
->pvd
);
1006 case TAGID_LOGVOL
: /* logical volume */
1007 UDF_UPDATE_DSCR(ump
->logical_vol
, &dscr
->lvd
);
1009 case TAGID_UNALLOC_SPACE
: /* unallocated space */
1010 UDF_UPDATE_DSCR(ump
->unallocated
, &dscr
->usd
);
1012 case TAGID_IMP_VOL
: /* implementation */
1013 /* XXX do we care about multiple impl. descr ? */
1014 UDF_UPDATE_DSCR(ump
->implementation
, &dscr
->ivd
);
1016 case TAGID_PARTITION
: /* physical partition */
1017 /* not much use if its not allocated */
1018 if ((udf_rw16(dscr
->pd
.flags
) & UDF_PART_FLAG_ALLOCATED
) == 0) {
1019 free(dscr
, M_UDFVOLD
);
1024 * BUGALERT: some rogue implementations use random physical
1025 * partition numbers to break other implementations so lookup
1028 raw_phys_part
= udf_rw16(dscr
->pd
.part_num
);
1029 for (phys_part
= 0; phys_part
< UDF_PARTITIONS
; phys_part
++) {
1030 part
= ump
->partitions
[phys_part
];
1033 if (udf_rw16(part
->part_num
) == raw_phys_part
)
1036 if (phys_part
== UDF_PARTITIONS
) {
1037 free(dscr
, M_UDFVOLD
);
1041 UDF_UPDATE_DSCR(ump
->partitions
[phys_part
], &dscr
->pd
);
1043 case TAGID_VOL
: /* volume space extender; rare */
1044 DPRINTF(VOLUMES
, ("VDS extender ignored\n"));
1045 free(dscr
, M_UDFVOLD
);
1048 DPRINTF(VOLUMES
, ("Unhandled VDS type %d\n",
1049 udf_rw16(dscr
->tag
.id
)));
1050 free(dscr
, M_UDFVOLD
);
1055 #undef UDF_UPDATE_DSCR
1057 /* --------------------------------------------------------------------- */
1060 udf_read_vds_extent(struct udf_mount
*ump
, uint32_t loc
, uint32_t len
)
1062 union dscrptr
*dscr
;
1063 uint32_t sector_size
, dscr_size
;
1066 sector_size
= ump
->discinfo
.sector_size
;
1068 /* loc is sectornr, len is in bytes */
1071 error
= udf_read_phys_dscr(ump
, loc
, M_UDFVOLD
, &dscr
);
1075 /* blank block is a terminator */
1079 /* TERM descriptor is a terminator */
1080 if (udf_rw16(dscr
->tag
.id
) == TAGID_TERM
) {
1081 free(dscr
, M_UDFVOLD
);
1085 /* process all others */
1086 dscr_size
= udf_tagsize(dscr
, sector_size
);
1087 error
= udf_process_vds_descriptor(ump
, dscr
);
1089 free(dscr
, M_UDFVOLD
);
1092 assert((dscr_size
% sector_size
) == 0);
1095 loc
+= dscr_size
/ sector_size
;
1103 udf_read_vds_space(struct udf_mount
*ump
)
1105 /* struct udf_args *args = &ump->mount_args; */
1106 struct anchor_vdp
*anchor
, *anchor2
;
1108 uint32_t main_loc
, main_len
;
1109 uint32_t reserve_loc
, reserve_len
;
1113 * read in VDS space provided by the anchors; if one descriptor read
1114 * fails, try the mirror sector.
1116 * check if 2nd anchor is different from 1st; if so, go for 2nd. This
1117 * avoids the `compatibility features' of DirectCD that may confuse
1121 anchor
= ump
->anchors
[0];
1122 anchor2
= ump
->anchors
[1];
1126 size
= sizeof(struct extent_ad
);
1127 if (memcmp(&anchor
->main_vds_ex
, &anchor2
->main_vds_ex
, size
))
1129 /* reserve is specified to be a literal copy of main */
1132 main_loc
= udf_rw32(anchor
->main_vds_ex
.loc
);
1133 main_len
= udf_rw32(anchor
->main_vds_ex
.len
);
1135 reserve_loc
= udf_rw32(anchor
->reserve_vds_ex
.loc
);
1136 reserve_len
= udf_rw32(anchor
->reserve_vds_ex
.len
);
1138 error
= udf_read_vds_extent(ump
, main_loc
, main_len
);
1140 printf("UDF mount: reading in reserve VDS extent\n");
1141 error
= udf_read_vds_extent(ump
, reserve_loc
, reserve_len
);
1147 /* --------------------------------------------------------------------- */
1150 * Read in the logical volume integrity sequence pointed to by our logical
1151 * volume descriptor. Its a sequence that can be extended using fields in the
1152 * integrity descriptor itself. On sequential media only one is found, on
1153 * rewritable media a sequence of descriptors can be found as a form of
1154 * history keeping and on non sequential write-once media the chain is vital
1155 * to allow more and more descriptors to be written. The last descriptor
1156 * written in an extent needs to claim space for a new extent.
1160 udf_retrieve_lvint(struct udf_mount
*ump
)
1162 union dscrptr
*dscr
;
1163 struct logvol_int_desc
*lvint
;
1164 struct udf_lvintq
*trace
;
1165 uint32_t lb_size
, lbnum
, len
;
1166 int dscr_type
, error
, trace_len
;
1168 lb_size
= udf_rw32(ump
->logical_vol
->lb_size
);
1169 len
= udf_rw32(ump
->logical_vol
->integrity_seq_loc
.len
);
1170 lbnum
= udf_rw32(ump
->logical_vol
->integrity_seq_loc
.loc
);
1173 memset(ump
->lvint_trace
, 0,
1174 UDF_LVDINT_SEGMENTS
* sizeof(struct udf_lvintq
));
1177 trace
= ump
->lvint_trace
;
1178 trace
->start
= lbnum
;
1179 trace
->end
= lbnum
+ len
/lb_size
;
1187 trace
->pos
= lbnum
- trace
->start
;
1188 trace
->wpos
= trace
->pos
+ 1;
1190 /* read in our integrity descriptor */
1191 error
= udf_read_phys_dscr(ump
, lbnum
, M_UDFVOLD
, &dscr
);
1194 trace
->wpos
= trace
->pos
;
1195 break; /* empty terminates */
1197 dscr_type
= udf_rw16(dscr
->tag
.id
);
1198 if (dscr_type
== TAGID_TERM
) {
1199 trace
->wpos
= trace
->pos
;
1200 break; /* clean terminator */
1202 if (dscr_type
!= TAGID_LOGVOL_INTEGRITY
) {
1203 /* fatal... corrupt disc */
1208 free(lvint
, M_UDFVOLD
);
1209 lvint
= &dscr
->lvid
;
1211 } /* else hope for the best... maybe the next is ok */
1213 DPRINTFIF(VOLUMES
, lvint
, ("logvol integrity read, state %s\n",
1214 udf_rw32(lvint
->integrity_type
) ? "CLOSED" : "OPEN"));
1216 /* proceed sequential */
1220 /* are we linking to a new piece? */
1221 if (dscr
&& lvint
->next_extent
.len
) {
1222 len
= udf_rw32(lvint
->next_extent
.len
);
1223 lbnum
= udf_rw32(lvint
->next_extent
.loc
);
1225 if (trace_len
>= UDF_LVDINT_SEGMENTS
-1) {
1226 /* IEK! segment link full... */
1227 DPRINTF(VOLUMES
, ("lvdint segments full\n"));
1233 trace
->start
= lbnum
;
1234 trace
->end
= lbnum
+ len
/lb_size
;
1241 /* clean up the mess, esp. when there is an error */
1243 free(dscr
, M_UDFVOLD
);
1245 if (error
&& lvint
) {
1246 free(lvint
, M_UDFVOLD
);
1253 ump
->logvol_integrity
= lvint
;
1259 udf_loose_lvint_history(struct udf_mount
*ump
)
1261 union dscrptr
**bufs
, *dscr
, *last_dscr
;
1262 struct udf_lvintq
*trace
, *in_trace
, *out_trace
;
1263 struct logvol_int_desc
*lvint
;
1264 uint32_t in_ext
, in_pos
, in_len
;
1265 uint32_t out_ext
, out_wpos
, out_len
;
1266 uint32_t lb_size
, packet_size
, lb_num
;
1267 uint32_t len
, start
;
1268 int ext
, minext
, extlen
, cnt
, cpy_len
, dscr_type
;
1272 DPRINTF(VOLUMES
, ("need to lose some lvint history\n"));
1274 lb_size
= udf_rw32(ump
->logical_vol
->lb_size
);
1275 packet_size
= ump
->data_track
.packet_size
; /* XXX data track */
1277 /* search smallest extent */
1278 trace
= &ump
->lvint_trace
[0];
1279 minext
= trace
->end
- trace
->start
;
1280 for (ext
= 1; ext
< UDF_LVDINT_SEGMENTS
; ext
++) {
1281 trace
= &ump
->lvint_trace
[ext
];
1282 extlen
= trace
->end
- trace
->start
;
1285 minext
= MIN(minext
, extlen
);
1287 losing
= MIN(minext
, UDF_LVINT_LOSSAGE
);
1288 /* no sense wiping all */
1289 if (losing
== minext
)
1292 DPRINTF(VOLUMES
, ("\tlosing %d entries\n", losing
));
1294 /* get buffer for pieces */
1295 bufs
= malloc(UDF_LVDINT_SEGMENTS
* sizeof(void *), M_TEMP
, M_WAITOK
);
1299 in_trace
= &ump
->lvint_trace
[in_ext
];
1300 in_len
= in_trace
->end
- in_trace
->start
;
1303 out_trace
= &ump
->lvint_trace
[out_ext
];
1304 out_len
= out_trace
->end
- out_trace
->start
;
1308 out_trace
->pos
= out_wpos
;
1309 out_trace
->wpos
= out_trace
->pos
;
1310 if (in_pos
>= in_len
) {
1313 in_trace
= &ump
->lvint_trace
[in_ext
];
1314 in_len
= in_trace
->end
- in_trace
->start
;
1316 if (out_wpos
>= out_len
) {
1319 out_trace
= &ump
->lvint_trace
[out_ext
];
1320 out_len
= out_trace
->end
- out_trace
->start
;
1322 /* copy overlap contents */
1323 cpy_len
= MIN(in_len
- in_pos
, out_len
- out_wpos
);
1324 cpy_len
= MIN(cpy_len
, in_len
- in_trace
->pos
);
1329 DPRINTF(VOLUMES
, ("\treading %d lvid descriptors\n", cpy_len
));
1330 for (cnt
= 0; cnt
< cpy_len
; cnt
++) {
1331 /* read in our integrity descriptor */
1332 lb_num
= in_trace
->start
+ in_pos
+ cnt
;
1333 error
= udf_read_phys_dscr(ump
, lb_num
, M_UDFVOLD
,
1342 out_trace
->pos
= out_wpos
+ cnt
;
1343 out_trace
->wpos
= out_trace
->pos
;
1344 break; /* empty terminates */
1346 dscr_type
= udf_rw16(dscr
->tag
.id
);
1347 if (dscr_type
== TAGID_TERM
) {
1348 out_trace
->pos
= out_wpos
+ cnt
;
1349 out_trace
->wpos
= out_trace
->pos
;
1350 break; /* clean terminator */
1352 if (dscr_type
!= TAGID_LOGVOL_INTEGRITY
) {
1353 panic( "UDF integrity sequence "
1354 "corrupted while mounted!\n");
1360 /* patch up if first entry was on error */
1361 if (bufs
[0] == NULL
) {
1362 for (cnt
= 0; cnt
< cpy_len
; cnt
++)
1363 if (bufs
[cnt
] != NULL
)
1365 last_dscr
= bufs
[cnt
];
1366 for (; cnt
> 0; cnt
--) {
1367 bufs
[cnt
] = last_dscr
;
1371 /* glue + write out */
1372 DPRINTF(VOLUMES
, ("\twriting %d lvid descriptors\n", cpy_len
));
1373 for (cnt
= 0; cnt
< cpy_len
; cnt
++) {
1374 lb_num
= out_trace
->start
+ out_wpos
+ cnt
;
1375 lvint
= &bufs
[cnt
]->lvid
;
1377 /* set continuation */
1380 if (out_wpos
+ cnt
== out_len
) {
1381 /* get continuation */
1382 trace
= &ump
->lvint_trace
[out_ext
+1];
1383 len
= trace
->end
- trace
->start
;
1384 start
= trace
->start
;
1386 lvint
->next_extent
.len
= udf_rw32(len
);
1387 lvint
->next_extent
.loc
= udf_rw32(start
);
1389 lb_num
= trace
->start
+ trace
->wpos
;
1390 error
= udf_write_phys_dscr_sync(ump
, NULL
, UDF_C_DSCR
,
1391 bufs
[cnt
], lb_num
, lb_num
);
1392 DPRINTFIF(VOLUMES
, error
,
1393 ("error writing lvint lb_num\n"));
1396 /* free non repeating descriptors */
1398 for (cnt
= 0; cnt
< cpy_len
; cnt
++) {
1399 if (bufs
[cnt
] != last_dscr
)
1400 free(bufs
[cnt
], M_UDFVOLD
);
1401 last_dscr
= bufs
[cnt
];
1406 out_wpos
+= cpy_len
;
1416 udf_writeout_lvint(struct udf_mount
*ump
, int lvflag
)
1418 struct udf_lvintq
*trace
;
1419 struct timeval now_v
;
1420 struct timespec now_s
;
1422 int logvol_integrity
;
1425 DPRINTF(VOLUMES
, ("writing out logvol integrity descriptor\n"));
1428 /* get free space in last chunk */
1429 trace
= ump
->lvint_trace
;
1430 while (trace
->wpos
> (trace
->end
- trace
->start
)) {
1431 DPRINTF(VOLUMES
, ("skip : start = %d, end = %d, pos = %d, "
1432 "wpos = %d\n", trace
->start
, trace
->end
,
1433 trace
->pos
, trace
->wpos
));
1437 /* check if there is space to append */
1438 space
= (trace
->end
- trace
->start
) - trace
->wpos
;
1439 DPRINTF(VOLUMES
, ("write start = %d, end = %d, pos = %d, wpos = %d, "
1440 "space = %d\n", trace
->start
, trace
->end
, trace
->pos
,
1441 trace
->wpos
, space
));
1444 logvol_integrity
= udf_rw32(ump
->logvol_integrity
->integrity_type
);
1445 if (logvol_integrity
== UDF_INTEGRITY_CLOSED
) {
1446 if ((space
< 3) && (lvflag
& UDF_APPENDONLY_LVINT
)) {
1447 /* don't allow this logvol to be opened */
1448 /* TODO extent LVINT space if possible */
1454 if (lvflag
& UDF_APPENDONLY_LVINT
)
1456 /* loose history by re-writing extents */
1457 error
= udf_loose_lvint_history(ump
);
1463 /* update our integrity descriptor to identify us and timestamp it */
1464 DPRINTF(VOLUMES
, ("updating integrity descriptor\n"));
1466 TIMEVAL_TO_TIMESPEC(&now_v
, &now_s
);
1467 udf_timespec_to_timestamp(&now_s
, &ump
->logvol_integrity
->time
);
1468 udf_set_regid(&ump
->logvol_info
->impl_id
, IMPL_NAME
);
1469 udf_add_impl_regid(ump
, &ump
->logvol_info
->impl_id
);
1471 /* writeout integrity descriptor */
1472 sector
= trace
->start
+ trace
->wpos
;
1473 error
= udf_write_phys_dscr_sync(ump
, NULL
, UDF_C_DSCR
,
1474 (union dscrptr
*) ump
->logvol_integrity
,
1476 DPRINTF(VOLUMES
, ("writeout lvint : error = %d\n", error
));
1480 /* advance write position */
1481 trace
->wpos
++; space
--;
1483 /* append terminator */
1484 sector
= trace
->start
+ trace
->wpos
;
1485 error
= udf_write_terminator(ump
, sector
);
1487 DPRINTF(VOLUMES
, ("write terminator : error = %d\n", error
));
1490 space
= (trace
->end
- trace
->start
) - trace
->wpos
;
1491 DPRINTF(VOLUMES
, ("write start = %d, end = %d, pos = %d, wpos = %d, "
1492 "space = %d\n", trace
->start
, trace
->end
, trace
->pos
,
1493 trace
->wpos
, space
));
1494 DPRINTF(VOLUMES
, ("finished writing out logvol integrity descriptor "
1500 /* --------------------------------------------------------------------- */
1503 udf_read_physical_partition_spacetables(struct udf_mount
*ump
)
1505 union dscrptr
*dscr
;
1506 /* struct udf_args *args = &ump->mount_args; */
1507 struct part_desc
*partd
;
1508 struct part_hdr_desc
*parthdr
;
1509 struct udf_bitmap
*bitmap
;
1511 uint32_t lb_num
, len
;
1512 int error
, dscr_type
;
1514 /* unallocated space map */
1515 for (phys_part
= 0; phys_part
< UDF_PARTITIONS
; phys_part
++) {
1516 partd
= ump
->partitions
[phys_part
];
1519 parthdr
= &partd
->_impl_use
.part_hdr
;
1521 lb_num
= udf_rw32(partd
->start_loc
);
1522 lb_num
+= udf_rw32(parthdr
->unalloc_space_bitmap
.lb_num
);
1523 len
= udf_rw32(parthdr
->unalloc_space_bitmap
.len
);
1527 DPRINTF(VOLUMES
, ("Read unalloc. space bitmap %d\n", lb_num
));
1528 error
= udf_read_phys_dscr(ump
, lb_num
, M_UDFVOLD
, &dscr
);
1529 if (!error
&& dscr
) {
1531 dscr_type
= udf_rw16(dscr
->tag
.id
);
1532 if (dscr_type
== TAGID_SPACE_BITMAP
) {
1533 DPRINTF(VOLUMES
, ("Accepting space bitmap\n"));
1534 ump
->part_unalloc_dscr
[phys_part
] = &dscr
->sbd
;
1536 /* fill in ump->part_unalloc_bits */
1537 bitmap
= &ump
->part_unalloc_bits
[phys_part
];
1538 bitmap
->blob
= (uint8_t *) dscr
;
1539 bitmap
->bits
= dscr
->sbd
.data
;
1540 bitmap
->max_offset
= udf_rw32(dscr
->sbd
.num_bits
);
1541 bitmap
->pages
= NULL
; /* TODO */
1542 bitmap
->data_pos
= 0;
1543 bitmap
->metadata_pos
= 0;
1545 free(dscr
, M_UDFVOLD
);
1547 printf( "UDF mount: error reading unallocated "
1552 /* blank not allowed */
1553 printf("UDF mount: blank unallocated space bitmap\n");
1558 /* unallocated space table (not supported) */
1559 for (phys_part
= 0; phys_part
< UDF_PARTITIONS
; phys_part
++) {
1560 partd
= ump
->partitions
[phys_part
];
1563 parthdr
= &partd
->_impl_use
.part_hdr
;
1565 len
= udf_rw32(parthdr
->unalloc_space_table
.len
);
1567 printf("UDF mount: space tables not supported\n");
1572 /* freed space map */
1573 for (phys_part
= 0; phys_part
< UDF_PARTITIONS
; phys_part
++) {
1574 partd
= ump
->partitions
[phys_part
];
1577 parthdr
= &partd
->_impl_use
.part_hdr
;
1579 /* freed space map */
1580 lb_num
= udf_rw32(partd
->start_loc
);
1581 lb_num
+= udf_rw32(parthdr
->freed_space_bitmap
.lb_num
);
1582 len
= udf_rw32(parthdr
->freed_space_bitmap
.len
);
1586 DPRINTF(VOLUMES
, ("Read unalloc. space bitmap %d\n", lb_num
));
1587 error
= udf_read_phys_dscr(ump
, lb_num
, M_UDFVOLD
, &dscr
);
1588 if (!error
&& dscr
) {
1590 dscr_type
= udf_rw16(dscr
->tag
.id
);
1591 if (dscr_type
== TAGID_SPACE_BITMAP
) {
1592 DPRINTF(VOLUMES
, ("Accepting space bitmap\n"));
1593 ump
->part_freed_dscr
[phys_part
] = &dscr
->sbd
;
1595 /* fill in ump->part_freed_bits */
1596 bitmap
= &ump
->part_unalloc_bits
[phys_part
];
1597 bitmap
->blob
= (uint8_t *) dscr
;
1598 bitmap
->bits
= dscr
->sbd
.data
;
1599 bitmap
->max_offset
= udf_rw32(dscr
->sbd
.num_bits
);
1600 bitmap
->pages
= NULL
; /* TODO */
1601 bitmap
->data_pos
= 0;
1602 bitmap
->metadata_pos
= 0;
1604 free(dscr
, M_UDFVOLD
);
1606 printf( "UDF mount: error reading freed "
1611 /* blank not allowed */
1612 printf("UDF mount: blank freed space bitmap\n");
1617 /* freed space table (not supported) */
1618 for (phys_part
= 0; phys_part
< UDF_PARTITIONS
; phys_part
++) {
1619 partd
= ump
->partitions
[phys_part
];
1622 parthdr
= &partd
->_impl_use
.part_hdr
;
1624 len
= udf_rw32(parthdr
->freed_space_table
.len
);
1626 printf("UDF mount: space tables not supported\n");
1635 /* TODO implement async writeout */
1637 udf_write_physical_partition_spacetables(struct udf_mount
*ump
, int waitfor
)
1639 union dscrptr
*dscr
;
1640 /* struct udf_args *args = &ump->mount_args; */
1641 struct part_desc
*partd
;
1642 struct part_hdr_desc
*parthdr
;
1644 uint32_t lb_num
, len
, ptov
;
1645 int error_all
, error
;
1648 /* unallocated space map */
1649 for (phys_part
= 0; phys_part
< UDF_PARTITIONS
; phys_part
++) {
1650 partd
= ump
->partitions
[phys_part
];
1653 parthdr
= &partd
->_impl_use
.part_hdr
;
1655 ptov
= udf_rw32(partd
->start_loc
);
1656 lb_num
= udf_rw32(parthdr
->unalloc_space_bitmap
.lb_num
);
1657 len
= udf_rw32(parthdr
->unalloc_space_bitmap
.len
);
1661 DPRINTF(VOLUMES
, ("Write unalloc. space bitmap %d\n",
1663 dscr
= (union dscrptr
*) ump
->part_unalloc_dscr
[phys_part
];
1664 error
= udf_write_phys_dscr_sync(ump
, NULL
, UDF_C_DSCR
,
1665 (union dscrptr
*) dscr
,
1666 ptov
+ lb_num
, lb_num
);
1668 DPRINTF(VOLUMES
, ("\tfailed!! (error %d)\n", error
));
1673 /* freed space map */
1674 for (phys_part
= 0; phys_part
< UDF_PARTITIONS
; phys_part
++) {
1675 partd
= ump
->partitions
[phys_part
];
1678 parthdr
= &partd
->_impl_use
.part_hdr
;
1680 /* freed space map */
1681 ptov
= udf_rw32(partd
->start_loc
);
1682 lb_num
= udf_rw32(parthdr
->freed_space_bitmap
.lb_num
);
1683 len
= udf_rw32(parthdr
->freed_space_bitmap
.len
);
1687 DPRINTF(VOLUMES
, ("Write freed space bitmap %d\n",
1689 dscr
= (union dscrptr
*) ump
->part_freed_dscr
[phys_part
];
1690 error
= udf_write_phys_dscr_sync(ump
, NULL
, UDF_C_DSCR
,
1691 (union dscrptr
*) dscr
,
1692 ptov
+ lb_num
, lb_num
);
1694 DPRINTF(VOLUMES
, ("\tfailed!! (error %d)\n", error
));
1704 udf_read_metadata_partition_spacetable(struct udf_mount
*ump
)
1706 struct udf_node
*bitmap_node
;
1707 union dscrptr
*dscr
;
1708 struct udf_bitmap
*bitmap
;
1710 int error
, dscr_type
;
1712 bitmap_node
= ump
->metadatabitmap_node
;
1714 /* only read in when metadata bitmap node is read in */
1715 if (bitmap_node
== NULL
)
1718 if (bitmap_node
->fe
) {
1719 inflen
= udf_rw64(bitmap_node
->fe
->inf_len
);
1721 KASSERT(bitmap_node
->efe
);
1722 inflen
= udf_rw64(bitmap_node
->efe
->inf_len
);
1725 DPRINTF(VOLUMES
, ("Reading metadata space bitmap for "
1726 "%"PRIu64
" bytes\n", inflen
));
1728 /* allocate space for bitmap */
1729 dscr
= malloc(inflen
, M_UDFVOLD
, M_CANFAIL
| M_WAITOK
);
1733 /* set vnode type to regular file or we can't read from it! */
1734 bitmap_node
->vnode
->v_type
= VREG
;
1736 /* read in complete metadata bitmap file */
1737 error
= vn_rdwr(UIO_READ
, bitmap_node
->vnode
,
1741 IO_SYNC
| IO_NODELOCKED
| IO_ALTSEMANTICS
, FSCRED
,
1744 DPRINTF(VOLUMES
, ("Error reading metadata space bitmap\n"));
1749 dscr_type
= udf_rw16(dscr
->tag
.id
);
1750 if (dscr_type
== TAGID_SPACE_BITMAP
) {
1751 DPRINTF(VOLUMES
, ("Accepting metadata space bitmap\n"));
1752 ump
->metadata_unalloc_dscr
= &dscr
->sbd
;
1754 /* fill in bitmap bits */
1755 bitmap
= &ump
->metadata_unalloc_bits
;
1756 bitmap
->blob
= (uint8_t *) dscr
;
1757 bitmap
->bits
= dscr
->sbd
.data
;
1758 bitmap
->max_offset
= udf_rw32(dscr
->sbd
.num_bits
);
1759 bitmap
->pages
= NULL
; /* TODO */
1760 bitmap
->data_pos
= 0;
1761 bitmap
->metadata_pos
= 0;
1763 DPRINTF(VOLUMES
, ("No valid bitmap found!\n"));
1770 free(dscr
, M_UDFVOLD
);
1771 printf( "UDF mount: error reading unallocated "
1772 "space bitmap for metadata partition\n");
1778 udf_write_metadata_partition_spacetable(struct udf_mount
*ump
, int waitfor
)
1780 struct udf_node
*bitmap_node
;
1781 union dscrptr
*dscr
;
1782 uint64_t inflen
, new_inflen
;
1785 bitmap_node
= ump
->metadatabitmap_node
;
1787 /* only write out when metadata bitmap node is known */
1788 if (bitmap_node
== NULL
)
1791 if (bitmap_node
->fe
) {
1792 inflen
= udf_rw64(bitmap_node
->fe
->inf_len
);
1794 KASSERT(bitmap_node
->efe
);
1795 inflen
= udf_rw64(bitmap_node
->efe
->inf_len
);
1798 /* reduce length to zero */
1799 dscr
= (union dscrptr
*) ump
->metadata_unalloc_dscr
;
1800 new_inflen
= udf_tagsize(dscr
, 1);
1802 DPRINTF(VOLUMES
, ("Resize and write out metadata space bitmap from "
1803 "%"PRIu64
" to %"PRIu64
" bytes\n", inflen
, new_inflen
));
1805 error
= udf_resize_node(bitmap_node
, new_inflen
, &dummy
);
1807 printf("Error resizing metadata space bitmap\n");
1809 error
= vn_rdwr(UIO_WRITE
, bitmap_node
->vnode
,
1813 IO_NODELOCKED
| IO_ALTSEMANTICS
, FSCRED
,
1816 bitmap_node
->i_flags
|= IN_MODIFIED
;
1817 vflushbuf(bitmap_node
->vnode
, 1 /* sync */);
1819 error
= VOP_FSYNC(bitmap_node
->vnode
,
1820 FSCRED
, FSYNC_WAIT
, 0, 0);
1823 printf( "Error writing out metadata partition unalloced "
1830 /* --------------------------------------------------------------------- */
1833 * Checks if ump's vds information is correct and complete
1837 udf_process_vds(struct udf_mount
*ump
) {
1838 union udf_pmap
*mapping
;
1839 /* struct udf_args *args = &ump->mount_args; */
1840 struct logvol_int_desc
*lvint
;
1841 struct udf_logvol_info
*lvinfo
;
1842 struct part_desc
*part
;
1843 uint32_t n_pm
, mt_l
;
1845 char *domain_name
, *map_name
;
1846 const char *check_name
;
1848 int pmap_stype
, pmap_size
;
1849 int pmap_type
, log_part
, phys_part
, raw_phys_part
, maps_on
;
1850 int n_phys
, n_virt
, n_spar
, n_meta
;
1856 /* we need at least an anchor (trivial, but for safety) */
1857 if (ump
->anchors
[0] == NULL
)
1860 /* we need at least one primary and one logical volume descriptor */
1861 if ((ump
->primary_vol
== NULL
) || (ump
->logical_vol
) == NULL
)
1864 /* we need at least one partition descriptor */
1865 if (ump
->partitions
[0] == NULL
)
1868 /* check logical volume sector size verses device sector size */
1869 if (udf_rw32(ump
->logical_vol
->lb_size
) != ump
->discinfo
.sector_size
) {
1870 printf("UDF mount: format violation, lb_size != sector size\n");
1874 /* check domain name */
1875 domain_name
= ump
->logical_vol
->domain_id
.id
;
1876 if (strncmp(domain_name
, "*OSTA UDF Compliant", 20)) {
1877 printf("mount_udf: disc not OSTA UDF Compliant, aborting\n");
1881 /* retrieve logical volume integrity sequence */
1882 error
= udf_retrieve_lvint(ump
);
1885 * We need at least one logvol integrity descriptor recorded. Note
1886 * that its OK to have an open logical volume integrity here. The VAT
1887 * will close/update the integrity.
1889 if (ump
->logvol_integrity
== NULL
)
1892 /* process derived structures */
1893 n_pm
= udf_rw32(ump
->logical_vol
->n_pm
); /* num partmaps */
1894 lvint
= ump
->logvol_integrity
;
1895 lvinfo
= (struct udf_logvol_info
*) (&lvint
->tables
[2 * n_pm
]);
1896 ump
->logvol_info
= lvinfo
;
1898 /* TODO check udf versions? */
1901 * check logvol mappings: effective virt->log partmap translation
1902 * check and recording of the mapping results. Saves expensive
1903 * strncmp() in tight places.
1905 DPRINTF(VOLUMES
, ("checking logvol mappings\n"));
1906 n_pm
= udf_rw32(ump
->logical_vol
->n_pm
); /* num partmaps */
1907 mt_l
= udf_rw32(ump
->logical_vol
->mt_l
); /* partmaps data length */
1908 pmap_pos
= ump
->logical_vol
->maps
;
1910 if (n_pm
> UDF_PMAPS
) {
1911 printf("UDF mount: too many mappings\n");
1915 /* count types and set partition numbers */
1916 ump
->data_part
= ump
->node_part
= ump
->fids_part
= 0;
1917 n_phys
= n_virt
= n_spar
= n_meta
= 0;
1918 for (log_part
= 0; log_part
< n_pm
; log_part
++) {
1919 mapping
= (union udf_pmap
*) pmap_pos
;
1920 pmap_stype
= pmap_pos
[0];
1921 pmap_size
= pmap_pos
[1];
1922 switch (pmap_stype
) {
1923 case 1: /* physical mapping */
1924 /* volseq = udf_rw16(mapping->pm1.vol_seq_num); */
1925 raw_phys_part
= udf_rw16(mapping
->pm1
.part_num
);
1926 pmap_type
= UDF_VTOP_TYPE_PHYS
;
1928 ump
->data_part
= log_part
;
1929 ump
->node_part
= log_part
;
1930 ump
->fids_part
= log_part
;
1932 case 2: /* virtual/sparable/meta mapping */
1933 map_name
= mapping
->pm2
.part_id
.id
;
1934 /* volseq = udf_rw16(mapping->pm2.vol_seq_num); */
1935 raw_phys_part
= udf_rw16(mapping
->pm2
.part_num
);
1936 pmap_type
= UDF_VTOP_TYPE_UNKNOWN
;
1937 len
= UDF_REGID_ID_SIZE
;
1939 check_name
= "*UDF Virtual Partition";
1940 if (strncmp(map_name
, check_name
, len
) == 0) {
1941 pmap_type
= UDF_VTOP_TYPE_VIRT
;
1943 ump
->node_part
= log_part
;
1946 check_name
= "*UDF Sparable Partition";
1947 if (strncmp(map_name
, check_name
, len
) == 0) {
1948 pmap_type
= UDF_VTOP_TYPE_SPARABLE
;
1950 ump
->data_part
= log_part
;
1951 ump
->node_part
= log_part
;
1952 ump
->fids_part
= log_part
;
1955 check_name
= "*UDF Metadata Partition";
1956 if (strncmp(map_name
, check_name
, len
) == 0) {
1957 pmap_type
= UDF_VTOP_TYPE_META
;
1959 ump
->node_part
= log_part
;
1960 ump
->fids_part
= log_part
;
1969 * BUGALERT: some rogue implementations use random physical
1970 * partition numbers to break other implementations so lookup
1973 for (phys_part
= 0; phys_part
< UDF_PARTITIONS
; phys_part
++) {
1974 part
= ump
->partitions
[phys_part
];
1977 if (udf_rw16(part
->part_num
) == raw_phys_part
)
1981 DPRINTF(VOLUMES
, ("\t%d -> %d(%d) type %d\n", log_part
,
1982 raw_phys_part
, phys_part
, pmap_type
));
1984 if (phys_part
== UDF_PARTITIONS
)
1986 if (pmap_type
== UDF_VTOP_TYPE_UNKNOWN
)
1989 ump
->vtop
[log_part
] = phys_part
;
1990 ump
->vtop_tp
[log_part
] = pmap_type
;
1992 pmap_pos
+= pmap_size
;
1994 /* not winning the beauty contest */
1995 ump
->vtop_tp
[UDF_VTOP_RAWPART
] = UDF_VTOP_TYPE_RAW
;
1997 /* test some basic UDF assertions/requirements */
1998 if ((n_virt
> 1) || (n_spar
> 1) || (n_meta
> 1))
2002 if ((n_phys
== 0) || n_spar
|| n_meta
)
2005 if (n_spar
+ n_phys
== 0)
2008 /* select allocation type for each logical partition */
2009 for (log_part
= 0; log_part
< n_pm
; log_part
++) {
2010 maps_on
= ump
->vtop
[log_part
];
2011 switch (ump
->vtop_tp
[log_part
]) {
2012 case UDF_VTOP_TYPE_PHYS
:
2013 assert(maps_on
== log_part
);
2014 ump
->vtop_alloc
[log_part
] = UDF_ALLOC_SPACEMAP
;
2016 case UDF_VTOP_TYPE_VIRT
:
2017 ump
->vtop_alloc
[log_part
] = UDF_ALLOC_VAT
;
2018 ump
->vtop_alloc
[maps_on
] = UDF_ALLOC_SEQUENTIAL
;
2020 case UDF_VTOP_TYPE_SPARABLE
:
2021 assert(maps_on
== log_part
);
2022 ump
->vtop_alloc
[log_part
] = UDF_ALLOC_SPACEMAP
;
2024 case UDF_VTOP_TYPE_META
:
2025 ump
->vtop_alloc
[log_part
] = UDF_ALLOC_METABITMAP
;
2026 if (ump
->discinfo
.mmc_cur
& MMC_CAP_PSEUDOOVERWRITE
) {
2027 /* special case for UDF 2.60 */
2028 ump
->vtop_alloc
[log_part
] = UDF_ALLOC_METASEQUENTIAL
;
2029 ump
->vtop_alloc
[maps_on
] = UDF_ALLOC_SEQUENTIAL
;
2033 panic("bad alloction type in udf's ump->vtop\n");
2037 /* determine logical volume open/closure actions */
2040 if (ump
->discinfo
.last_session_state
== MMC_STATE_EMPTY
)
2041 ump
->lvopen
|= UDF_OPEN_SESSION
;
2042 ump
->lvclose
= UDF_WRITE_VAT
;
2043 if (ump
->mount_args
.udfmflags
& UDFMNT_CLOSESESSION
)
2044 ump
->lvclose
|= UDF_CLOSE_SESSION
;
2046 /* `normal' rewritable or non sequential media */
2047 ump
->lvopen
= UDF_WRITE_LVINT
;
2048 ump
->lvclose
= UDF_WRITE_LVINT
;
2049 if ((ump
->discinfo
.mmc_cur
& MMC_CAP_REWRITABLE
) == 0)
2050 ump
->lvopen
|= UDF_APPENDONLY_LVINT
;
2054 * Determine sheduler error behaviour. For virtual partitions, update
2055 * the trackinfo; for sparable partitions replace a whole block on the
2056 * sparable table. Allways requeue.
2058 ump
->lvreadwrite
= 0;
2060 ump
->lvreadwrite
= UDF_UPDATE_TRACKINFO
;
2062 ump
->lvreadwrite
= UDF_REMAP_BLOCK
;
2065 * Select our sheduler
2067 ump
->strategy
= &udf_strat_rmw
;
2068 if (n_virt
|| (ump
->discinfo
.mmc_cur
& MMC_CAP_PSEUDOOVERWRITE
))
2069 ump
->strategy
= &udf_strat_sequential
;
2070 if ((ump
->discinfo
.mmc_class
== MMC_CLASS_DISC
) ||
2071 (ump
->discinfo
.mmc_class
== MMC_CLASS_UNKN
))
2072 ump
->strategy
= &udf_strat_direct
;
2074 ump
->strategy
= &udf_strat_rmw
;
2077 /* read-only access won't benefit from the other shedulers */
2078 if (ump
->vfs_mountp
->mnt_flag
& MNT_RDONLY
)
2079 ump
->strategy
= &udf_strat_direct
;
2083 DPRINTF(VOLUMES
, ("\tdata partition %d\n", ump
->data_part
));
2084 DPRINTF(VOLUMES
, ("\t\talloc scheme %d\n", ump
->vtop_alloc
[ump
->data_part
]));
2085 DPRINTF(VOLUMES
, ("\tnode partition %d\n", ump
->node_part
));
2086 DPRINTF(VOLUMES
, ("\t\talloc scheme %d\n", ump
->vtop_alloc
[ump
->node_part
]));
2087 DPRINTF(VOLUMES
, ("\tfids partition %d\n", ump
->fids_part
));
2088 DPRINTF(VOLUMES
, ("\t\talloc scheme %d\n", ump
->vtop_alloc
[ump
->fids_part
]));
2090 snprintb(bits
, sizeof(bits
), UDFLOGVOL_BITS
, ump
->lvopen
);
2091 DPRINTF(VOLUMES
, ("\tactions on logvol open %s\n", bits
));
2092 snprintb(bits
, sizeof(bits
), UDFLOGVOL_BITS
, ump
->lvclose
);
2093 DPRINTF(VOLUMES
, ("\tactions on logvol close %s\n", bits
));
2094 snprintb(bits
, sizeof(bits
), UDFONERROR_BITS
, ump
->lvreadwrite
);
2095 DPRINTF(VOLUMES
, ("\tactions on logvol errors %s\n", bits
));
2097 DPRINTF(VOLUMES
, ("\tselected sheduler `%s`\n",
2098 (ump
->strategy
== &udf_strat_direct
) ? "Direct" :
2099 (ump
->strategy
== &udf_strat_sequential
) ? "Sequential" :
2100 (ump
->strategy
== &udf_strat_rmw
) ? "RMW" : "UNKNOWN!"));
2102 /* signal its OK for now */
2106 /* --------------------------------------------------------------------- */
2109 * Update logical volume name in all structures that keep a record of it. We
2110 * use memmove since each of them might be specified as a source.
2112 * Note that it doesn't update the VAT structure!
2116 udf_update_logvolname(struct udf_mount
*ump
, char *logvol_id
)
2118 struct logvol_desc
*lvd
= NULL
;
2119 struct fileset_desc
*fsd
= NULL
;
2120 struct udf_lv_info
*lvi
= NULL
;
2122 DPRINTF(VOLUMES
, ("Updating logical volume name\n"));
2123 lvd
= ump
->logical_vol
;
2124 fsd
= ump
->fileset_desc
;
2125 if (ump
->implementation
)
2126 lvi
= &ump
->implementation
->_impl_use
.lv_info
;
2128 /* logvol's id might be specified as origional so use memmove here */
2129 memmove(lvd
->logvol_id
, logvol_id
, 128);
2131 memmove(fsd
->logvol_id
, logvol_id
, 128);
2133 memmove(lvi
->logvol_id
, logvol_id
, 128);
2136 /* --------------------------------------------------------------------- */
2139 udf_inittag(struct udf_mount
*ump
, struct desc_tag
*tag
, int tagid
,
2142 assert(ump
->logical_vol
);
2144 tag
->id
= udf_rw16(tagid
);
2145 tag
->descriptor_ver
= ump
->logical_vol
->tag
.descriptor_ver
;
2148 tag
->serial_num
= ump
->logical_vol
->tag
.serial_num
;
2149 tag
->tag_loc
= udf_rw32(sector
);
2154 udf_advance_uniqueid(struct udf_mount
*ump
)
2158 mutex_enter(&ump
->logvol_mutex
);
2159 unique_id
= udf_rw64(ump
->logvol_integrity
->lvint_next_unique_id
);
2160 if (unique_id
< 0x10)
2162 ump
->logvol_integrity
->lvint_next_unique_id
= udf_rw64(unique_id
+ 1);
2163 mutex_exit(&ump
->logvol_mutex
);
2170 udf_adjust_filecount(struct udf_node
*udf_node
, int sign
)
2172 struct udf_mount
*ump
= udf_node
->ump
;
2173 uint32_t num_dirs
, num_files
;
2178 udf_file_type
= udf_node
->fe
->icbtag
.file_type
;
2180 udf_file_type
= udf_node
->efe
->icbtag
.file_type
;
2183 /* adjust file count */
2184 mutex_enter(&ump
->allocate_mutex
);
2185 if (udf_file_type
== UDF_ICB_FILETYPE_DIRECTORY
) {
2186 num_dirs
= udf_rw32(ump
->logvol_info
->num_directories
);
2187 ump
->logvol_info
->num_directories
=
2188 udf_rw32((num_dirs
+ sign
));
2190 num_files
= udf_rw32(ump
->logvol_info
->num_files
);
2191 ump
->logvol_info
->num_files
=
2192 udf_rw32((num_files
+ sign
));
2194 mutex_exit(&ump
->allocate_mutex
);
2199 udf_osta_charset(struct charspec
*charspec
)
2201 memset(charspec
, 0, sizeof(struct charspec
));
2203 strcpy((char *) charspec
->inf
, "OSTA Compressed Unicode");
2207 /* first call udf_set_regid and then the suffix */
2209 udf_set_regid(struct regid
*regid
, char const *name
)
2211 memset(regid
, 0, sizeof(struct regid
));
2212 regid
->flags
= 0; /* not dirty and not protected */
2213 strcpy((char *) regid
->id
, name
);
2218 udf_add_domain_regid(struct udf_mount
*ump
, struct regid
*regid
)
2222 ver
= (uint16_t *) regid
->id_suffix
;
2223 *ver
= ump
->logvol_info
->min_udf_readver
;
2228 udf_add_udf_regid(struct udf_mount
*ump
, struct regid
*regid
)
2232 ver
= (uint16_t *) regid
->id_suffix
;
2233 *ver
= ump
->logvol_info
->min_udf_readver
;
2235 regid
->id_suffix
[2] = 4; /* unix */
2236 regid
->id_suffix
[3] = 8; /* NetBSD */
2241 udf_add_impl_regid(struct udf_mount
*ump
, struct regid
*regid
)
2243 regid
->id_suffix
[0] = 4; /* unix */
2244 regid
->id_suffix
[1] = 8; /* NetBSD */
2249 udf_add_app_regid(struct udf_mount
*ump
, struct regid
*regid
)
2251 regid
->id_suffix
[0] = APP_VERSION_MAIN
;
2252 regid
->id_suffix
[1] = APP_VERSION_SUB
;
2256 udf_create_parentfid(struct udf_mount
*ump
, struct fileid_desc
*fid
,
2257 struct long_ad
*parent
, uint64_t unique_id
)
2259 /* the size of an empty FID is 38 but needs to be a multiple of 4 */
2262 udf_inittag(ump
, &fid
->tag
, TAGID_FID
, udf_rw32(parent
->loc
.lb_num
));
2263 fid
->file_version_num
= udf_rw16(1); /* UDF 2.3.4.1 */
2264 fid
->file_char
= UDF_FILE_CHAR_DIR
| UDF_FILE_CHAR_PAR
;
2266 fid
->icb
.longad_uniqueid
= udf_rw32((uint32_t) unique_id
);
2267 fid
->tag
.desc_crc_len
= udf_rw16(fidsize
- UDF_DESC_TAG_LENGTH
);
2268 (void) udf_validate_tag_and_crc_sums((union dscrptr
*) fid
);
2273 /* --------------------------------------------------------------------- */
2276 * Extended attribute support. UDF knows of 3 places for extended attributes:
2278 * (a) inside the file's (e)fe in the length of the extended attribute area
2279 * before the allocation descriptors/filedata
2281 * (b) in a file referenced by (e)fe->ext_attr_icb and
2283 * (c) in the e(fe)'s associated stream directory that can hold various
2284 * sub-files. In the stream directory a few fixed named subfiles are reserved
2285 * for NT/Unix ACL's and OS/2 attributes.
2287 * NOTE: Extended attributes are read randomly but allways written
2288 * *atomicaly*. For ACL's this interface is propably different but not known
2291 * Order of extended attributes in a space :
2293 * Non block aligned Implementation Use EAs
2294 * Block aligned Implementation Use EAs
2295 * Application Use EAs
2299 udf_impl_extattr_check(struct impl_extattr_entry
*implext
)
2303 if (strncmp(implext
->imp_id
.id
, "*UDF", 4) == 0) {
2304 /* checksum valid? */
2305 DPRINTF(EXTATTR
, ("checking UDF impl. attr checksum\n"));
2306 spos
= (uint16_t *) implext
->data
;
2307 if (udf_rw16(*spos
) != udf_ea_cksum((uint8_t *) implext
))
2314 udf_calc_impl_extattr_checksum(struct impl_extattr_entry
*implext
)
2318 if (strncmp(implext
->imp_id
.id
, "*UDF", 4) == 0) {
2320 spos
= (uint16_t *) implext
->data
;
2321 *spos
= udf_rw16(udf_ea_cksum((uint8_t *) implext
));
2327 udf_extattr_search_intern(struct udf_node
*node
,
2328 uint32_t sattr
, char const *sattrname
,
2329 uint32_t *offsetp
, uint32_t *lengthp
)
2331 struct extattrhdr_desc
*eahdr
;
2332 struct extattr_entry
*attrhdr
;
2333 struct impl_extattr_entry
*implext
;
2334 uint32_t offset
, a_l
, sector_size
;
2339 /* get mountpoint */
2340 sector_size
= node
->ump
->discinfo
.sector_size
;
2342 /* get information from fe/efe */
2344 l_ea
= udf_rw32(node
->fe
->l_ea
);
2345 eahdr
= (struct extattrhdr_desc
*) node
->fe
->data
;
2348 l_ea
= udf_rw32(node
->efe
->l_ea
);
2349 eahdr
= (struct extattrhdr_desc
*) node
->efe
->data
;
2352 /* something recorded here? */
2356 /* check extended attribute tag; what to do if it fails? */
2357 error
= udf_check_tag(eahdr
);
2360 if (udf_rw16(eahdr
->tag
.id
) != TAGID_EXTATTR_HDR
)
2362 error
= udf_check_tag_payload(eahdr
, sizeof(struct extattrhdr_desc
));
2366 DPRINTF(EXTATTR
, ("Found %d bytes of extended attributes\n", l_ea
));
2368 /* looking for Ecma-167 attributes? */
2369 offset
= sizeof(struct extattrhdr_desc
);
2371 /* looking for either implemenation use or application use */
2372 if (sattr
== 2048) { /* [4/48.10.8] */
2373 offset
= udf_rw32(eahdr
->impl_attr_loc
);
2374 if (offset
== UDF_IMPL_ATTR_LOC_NOT_PRESENT
)
2377 if (sattr
== 65536) { /* [4/48.10.9] */
2378 offset
= udf_rw32(eahdr
->appl_attr_loc
);
2379 if (offset
== UDF_APPL_ATTR_LOC_NOT_PRESENT
)
2383 /* paranoia check offset and l_ea */
2384 if (l_ea
+ offset
>= sector_size
- sizeof(struct extattr_entry
))
2387 DPRINTF(EXTATTR
, ("Starting at offset %d\n", offset
));
2389 /* find our extended attribute */
2391 pos
= (uint8_t *) eahdr
+ offset
;
2393 while (l_ea
>= sizeof(struct extattr_entry
)) {
2394 DPRINTF(EXTATTR
, ("%d extended attr bytes left\n", l_ea
));
2395 attrhdr
= (struct extattr_entry
*) pos
;
2396 implext
= (struct impl_extattr_entry
*) pos
;
2398 /* get complete attribute length and check for roque values */
2399 a_l
= udf_rw32(attrhdr
->a_l
);
2400 DPRINTF(EXTATTR
, ("attribute %d:%d, len %d/%d\n",
2401 udf_rw32(attrhdr
->type
),
2402 attrhdr
->subtype
, a_l
, l_ea
));
2403 if ((a_l
== 0) || (a_l
> l_ea
))
2406 if (attrhdr
->type
!= sattr
)
2407 goto next_attribute
;
2409 /* we might have found it! */
2410 if (attrhdr
->type
< 2048) { /* Ecma-167 attribute */
2413 return 0; /* success */
2417 * Implementation use and application use extended attributes
2418 * have a name to identify. They share the same structure only
2419 * UDF implementation use extended attributes have a checksum
2423 DPRINTF(EXTATTR
, ("named attribute %s\n", implext
->imp_id
.id
));
2424 if (strcmp(implext
->imp_id
.id
, sattrname
) == 0) {
2425 /* we have found our appl/implementation attribute */
2428 return 0; /* success */
2432 /* next attribute */
2443 udf_extattr_insert_internal(struct udf_mount
*ump
, union dscrptr
*dscr
,
2444 struct extattr_entry
*extattr
)
2446 struct file_entry
*fe
;
2447 struct extfile_entry
*efe
;
2448 struct extattrhdr_desc
*extattrhdr
;
2449 struct impl_extattr_entry
*implext
;
2450 uint32_t impl_attr_loc
, appl_attr_loc
, l_ea
, a_l
, exthdr_len
;
2451 uint32_t *l_eap
, l_ad
;
2453 uint8_t *bpos
, *data
;
2455 if (udf_rw16(dscr
->tag
.id
) == TAGID_FENTRY
) {
2459 l_ad
= udf_rw32(fe
->l_ad
);
2460 } else if (udf_rw16(dscr
->tag
.id
) == TAGID_EXTFENTRY
) {
2464 l_ad
= udf_rw32(efe
->l_ad
);
2466 panic("Bad tag passed to udf_extattr_insert_internal");
2469 /* can't append already written to file descriptors yet */
2472 /* should have a header! */
2473 extattrhdr
= (struct extattrhdr_desc
*) data
;
2474 l_ea
= udf_rw32(*l_eap
);
2476 /* create empty extended attribute header */
2477 exthdr_len
= sizeof(struct extattrhdr_desc
);
2479 udf_inittag(ump
, &extattrhdr
->tag
, TAGID_EXTATTR_HDR
,
2481 extattrhdr
->impl_attr_loc
= udf_rw32(exthdr_len
);
2482 extattrhdr
->appl_attr_loc
= udf_rw32(exthdr_len
);
2483 extattrhdr
->tag
.desc_crc_len
= udf_rw16(8);
2485 /* record extended attribute header length */
2487 *l_eap
= udf_rw32(l_ea
);
2490 /* extract locations */
2491 impl_attr_loc
= udf_rw32(extattrhdr
->impl_attr_loc
);
2492 appl_attr_loc
= udf_rw32(extattrhdr
->appl_attr_loc
);
2493 if (impl_attr_loc
== UDF_IMPL_ATTR_LOC_NOT_PRESENT
)
2494 impl_attr_loc
= l_ea
;
2495 if (appl_attr_loc
== UDF_IMPL_ATTR_LOC_NOT_PRESENT
)
2496 appl_attr_loc
= l_ea
;
2499 if (udf_rw32(extattr
->type
) < 2048) {
2500 assert(impl_attr_loc
== l_ea
);
2501 assert(appl_attr_loc
== l_ea
);
2504 /* implementation use extended attributes */
2505 if (udf_rw32(extattr
->type
) == 2048) {
2506 assert(appl_attr_loc
== l_ea
);
2508 /* calculate and write extended attribute header checksum */
2509 implext
= (struct impl_extattr_entry
*) extattr
;
2510 assert(udf_rw32(implext
->iu_l
) == 4); /* [UDF 3.3.4.5] */
2511 spos
= (uint16_t *) implext
->data
;
2512 *spos
= udf_rw16(udf_ea_cksum((uint8_t *) implext
));
2515 /* application use extended attributes */
2516 assert(udf_rw32(extattr
->type
) != 65536);
2517 assert(appl_attr_loc
== l_ea
);
2519 /* append the attribute at the end of the current space */
2520 bpos
= data
+ udf_rw32(*l_eap
);
2521 a_l
= udf_rw32(extattr
->a_l
);
2523 /* update impl. attribute locations */
2524 if (udf_rw32(extattr
->type
) < 2048) {
2525 impl_attr_loc
= l_ea
+ a_l
;
2526 appl_attr_loc
= l_ea
+ a_l
;
2528 if (udf_rw32(extattr
->type
) == 2048) {
2529 appl_attr_loc
= l_ea
+ a_l
;
2532 /* copy and advance */
2533 memcpy(bpos
, extattr
, a_l
);
2535 *l_eap
= udf_rw32(l_ea
);
2537 /* do the `dance` again backwards */
2538 if (udf_rw16(ump
->logical_vol
->tag
.descriptor_ver
) != 2) {
2539 if (impl_attr_loc
== l_ea
)
2540 impl_attr_loc
= UDF_IMPL_ATTR_LOC_NOT_PRESENT
;
2541 if (appl_attr_loc
== l_ea
)
2542 appl_attr_loc
= UDF_APPL_ATTR_LOC_NOT_PRESENT
;
2546 extattrhdr
->impl_attr_loc
= udf_rw32(impl_attr_loc
);
2547 extattrhdr
->appl_attr_loc
= udf_rw32(appl_attr_loc
);
2551 /* --------------------------------------------------------------------- */
2554 udf_update_lvid_from_vat_extattr(struct udf_node
*vat_node
)
2556 struct udf_mount
*ump
;
2557 struct udf_logvol_info
*lvinfo
;
2558 struct impl_extattr_entry
*implext
;
2559 struct vatlvext_extattr_entry lvext
;
2560 const char *extstr
= "*UDF VAT LVExtension";
2561 uint64_t vat_uniqueid
;
2562 uint32_t offset
, a_l
;
2563 uint8_t *ea_start
, *lvextpos
;
2566 /* get mountpoint and lvinfo */
2567 ump
= vat_node
->ump
;
2568 lvinfo
= ump
->logvol_info
;
2570 /* get information from fe/efe */
2572 vat_uniqueid
= udf_rw64(vat_node
->fe
->unique_id
);
2573 ea_start
= vat_node
->fe
->data
;
2575 vat_uniqueid
= udf_rw64(vat_node
->efe
->unique_id
);
2576 ea_start
= vat_node
->efe
->data
;
2579 error
= udf_extattr_search_intern(vat_node
, 2048, extstr
, &offset
, &a_l
);
2583 implext
= (struct impl_extattr_entry
*) (ea_start
+ offset
);
2584 error
= udf_impl_extattr_check(implext
);
2589 if (a_l
!= sizeof(*implext
) -1 + udf_rw32(implext
->iu_l
) + sizeof(lvext
)) {
2590 DPRINTF(VOLUMES
, ("VAT LVExtension size doesn't compute\n"));
2595 * we have found our "VAT LVExtension attribute. BUT due to a
2596 * bug in the specification it might not be word aligned so
2597 * copy first to avoid panics on some machines (!!)
2599 DPRINTF(VOLUMES
, ("Found VAT LVExtension attr\n"));
2600 lvextpos
= implext
->data
+ udf_rw32(implext
->iu_l
);
2601 memcpy(&lvext
, lvextpos
, sizeof(lvext
));
2603 /* check if it was updated the last time */
2604 if (udf_rw64(lvext
.unique_id_chk
) == vat_uniqueid
) {
2605 lvinfo
->num_files
= lvext
.num_files
;
2606 lvinfo
->num_directories
= lvext
.num_directories
;
2607 udf_update_logvolname(ump
, lvext
.logvol_id
);
2609 DPRINTF(VOLUMES
, ("VAT LVExtension out of date\n"));
2610 /* replace VAT LVExt by free space EA */
2611 memset(implext
->imp_id
.id
, 0, UDF_REGID_ID_SIZE
);
2612 strcpy(implext
->imp_id
.id
, "*UDF FreeEASpace");
2613 udf_calc_impl_extattr_checksum(implext
);
2621 udf_update_vat_extattr_from_lvid(struct udf_node
*vat_node
)
2623 struct udf_mount
*ump
;
2624 struct udf_logvol_info
*lvinfo
;
2625 struct impl_extattr_entry
*implext
;
2626 struct vatlvext_extattr_entry lvext
;
2627 const char *extstr
= "*UDF VAT LVExtension";
2628 uint64_t vat_uniqueid
;
2629 uint32_t offset
, a_l
;
2630 uint8_t *ea_start
, *lvextpos
;
2633 /* get mountpoint and lvinfo */
2634 ump
= vat_node
->ump
;
2635 lvinfo
= ump
->logvol_info
;
2637 /* get information from fe/efe */
2639 vat_uniqueid
= udf_rw64(vat_node
->fe
->unique_id
);
2640 ea_start
= vat_node
->fe
->data
;
2642 vat_uniqueid
= udf_rw64(vat_node
->efe
->unique_id
);
2643 ea_start
= vat_node
->efe
->data
;
2646 error
= udf_extattr_search_intern(vat_node
, 2048, extstr
, &offset
, &a_l
);
2649 /* found, it existed */
2652 implext
= (struct impl_extattr_entry
*) (ea_start
+ offset
);
2653 error
= udf_impl_extattr_check(implext
);
2655 DPRINTF(VOLUMES
, ("VAT LVExtension bad on update\n"));
2661 * we have found our "VAT LVExtension attribute. BUT due to a
2662 * bug in the specification it might not be word aligned so
2663 * copy first to avoid panics on some machines (!!)
2665 DPRINTF(VOLUMES
, ("Updating VAT LVExtension attr\n"));
2666 lvextpos
= implext
->data
+ udf_rw32(implext
->iu_l
);
2668 lvext
.unique_id_chk
= vat_uniqueid
;
2669 lvext
.num_files
= lvinfo
->num_files
;
2670 lvext
.num_directories
= lvinfo
->num_directories
;
2671 memmove(lvext
.logvol_id
, ump
->logical_vol
->logvol_id
, 128);
2673 memcpy(lvextpos
, &lvext
, sizeof(lvext
));
2678 /* --------------------------------------------------------------------- */
2681 udf_vat_read(struct udf_node
*vat_node
, uint8_t *blob
, int size
, uint32_t offset
)
2683 struct udf_mount
*ump
= vat_node
->ump
;
2685 if (offset
+ size
> ump
->vat_offset
+ ump
->vat_entries
* 4)
2688 memcpy(blob
, ump
->vat_table
+ offset
, size
);
2693 udf_vat_write(struct udf_node
*vat_node
, uint8_t *blob
, int size
, uint32_t offset
)
2695 struct udf_mount
*ump
= vat_node
->ump
;
2696 uint32_t offset_high
;
2697 uint8_t *new_vat_table
;
2699 /* extent VAT allocation if needed */
2700 offset_high
= offset
+ size
;
2701 if (offset_high
>= ump
->vat_table_alloc_len
) {
2703 new_vat_table
= realloc(ump
->vat_table
,
2704 ump
->vat_table_alloc_len
+ UDF_VAT_CHUNKSIZE
,
2705 M_UDFVOLD
, M_WAITOK
| M_CANFAIL
);
2706 if (!new_vat_table
) {
2707 printf("udf_vat_write: can't extent VAT, out of mem\n");
2710 ump
->vat_table
= new_vat_table
;
2711 ump
->vat_table_alloc_len
+= UDF_VAT_CHUNKSIZE
;
2713 ump
->vat_table_len
= MAX(ump
->vat_table_len
, offset_high
);
2715 memcpy(ump
->vat_table
+ offset
, blob
, size
);
2719 /* --------------------------------------------------------------------- */
2721 /* TODO support previous VAT location writeout */
2723 udf_update_vat_descriptor(struct udf_mount
*ump
)
2725 struct udf_node
*vat_node
= ump
->vat_node
;
2726 struct udf_logvol_info
*lvinfo
= ump
->logvol_info
;
2727 struct icb_tag
*icbtag
;
2728 struct udf_oldvat_tail
*oldvat_tl
;
2729 struct udf_vat
*vat
;
2733 int filetype
, error
;
2737 lb_size
= udf_rw32(ump
->logical_vol
->lb_size
);
2739 /* get our new unique_id */
2740 unique_id
= udf_advance_uniqueid(ump
);
2742 /* get information from fe/efe */
2744 icbtag
= &vat_node
->fe
->icbtag
;
2745 vat_node
->fe
->unique_id
= udf_rw64(unique_id
);
2747 icbtag
= &vat_node
->efe
->icbtag
;
2748 vat_node
->efe
->unique_id
= udf_rw64(unique_id
);
2751 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
2752 filetype
= icbtag
->file_type
;
2753 KASSERT((filetype
== 0) || (filetype
== UDF_ICB_FILETYPE_VAT
));
2755 /* allocate piece to process head or tail of VAT file */
2756 raw_vat
= malloc(lb_size
, M_TEMP
, M_WAITOK
);
2758 if (filetype
== 0) {
2760 * Update "*UDF VAT LVExtension" extended attribute from the
2763 udf_update_vat_extattr_from_lvid(vat_node
);
2765 /* setup identifying regid */
2766 oldvat_tl
= (struct udf_oldvat_tail
*) raw_vat
;
2767 memset(oldvat_tl
, 0, sizeof(struct udf_oldvat_tail
));
2769 udf_set_regid(&oldvat_tl
->id
, "*UDF Virtual Alloc Tbl");
2770 udf_add_udf_regid(ump
, &oldvat_tl
->id
);
2771 oldvat_tl
->prev_vat
= udf_rw32(0xffffffff);
2773 /* write out new tail of virtual allocation table file */
2774 error
= udf_vat_write(vat_node
, raw_vat
,
2775 sizeof(struct udf_oldvat_tail
), ump
->vat_entries
* 4);
2777 /* compose the VAT2 header */
2778 vat
= (struct udf_vat
*) raw_vat
;
2779 memset(vat
, 0, sizeof(struct udf_vat
));
2781 vat
->header_len
= udf_rw16(152); /* as per spec */
2782 vat
->impl_use_len
= udf_rw16(0);
2783 memmove(vat
->logvol_id
, ump
->logical_vol
->logvol_id
, 128);
2784 vat
->prev_vat
= udf_rw32(0xffffffff);
2785 vat
->num_files
= lvinfo
->num_files
;
2786 vat
->num_directories
= lvinfo
->num_directories
;
2787 vat
->min_udf_readver
= lvinfo
->min_udf_readver
;
2788 vat
->min_udf_writever
= lvinfo
->min_udf_writever
;
2789 vat
->max_udf_writever
= lvinfo
->max_udf_writever
;
2791 error
= udf_vat_write(vat_node
, raw_vat
,
2792 sizeof(struct udf_vat
), 0);
2794 free(raw_vat
, M_TEMP
);
2796 return error
; /* success! */
2801 udf_writeout_vat(struct udf_mount
*ump
)
2803 struct udf_node
*vat_node
= ump
->vat_node
;
2804 uint32_t vat_length
;
2809 DPRINTF(CALL
, ("udf_writeout_vat\n"));
2811 // mutex_enter(&ump->allocate_mutex);
2812 udf_update_vat_descriptor(ump
);
2814 /* write out the VAT contents ; TODO intelligent writing */
2815 vat_length
= ump
->vat_table_len
;
2816 error
= vn_rdwr(UIO_WRITE
, vat_node
->vnode
,
2817 ump
->vat_table
, ump
->vat_table_len
, 0,
2818 UIO_SYSSPACE
, IO_NODELOCKED
, FSCRED
, NULL
, NULL
);
2820 printf("udf_writeout_vat: failed to write out VAT contents\n");
2824 // mutex_exit(&ump->allocate_mutex);
2826 vflushbuf(ump
->vat_node
->vnode
, 1 /* sync */);
2827 error
= VOP_FSYNC(ump
->vat_node
->vnode
,
2828 FSCRED
, FSYNC_WAIT
, 0, 0);
2830 printf("udf_writeout_vat: error writing VAT node!\n");
2836 /* --------------------------------------------------------------------- */
2839 * Read in relevant pieces of VAT file and check if its indeed a VAT file
2840 * descriptor. If OK, read in complete VAT file.
2844 udf_check_for_vat(struct udf_node
*vat_node
)
2846 struct udf_mount
*ump
;
2847 struct icb_tag
*icbtag
;
2848 struct timestamp
*mtime
;
2849 struct udf_vat
*vat
;
2850 struct udf_oldvat_tail
*oldvat_tl
;
2851 struct udf_logvol_info
*lvinfo
;
2853 uint32_t vat_length
;
2854 uint32_t vat_offset
, vat_entries
, vat_table_alloc_len
;
2855 uint32_t sector_size
;
2862 /* vat_length is really 64 bits though impossible */
2864 DPRINTF(VOLUMES
, ("Checking for VAT\n"));
2868 /* get mount info */
2869 ump
= vat_node
->ump
;
2870 sector_size
= udf_rw32(ump
->logical_vol
->lb_size
);
2872 /* check assertions */
2873 assert(vat_node
->fe
|| vat_node
->efe
);
2874 assert(ump
->logvol_integrity
);
2876 /* set vnode type to regular file or we can't read from it! */
2877 vat_node
->vnode
->v_type
= VREG
;
2879 /* get information from fe/efe */
2881 vat_length
= udf_rw64(vat_node
->fe
->inf_len
);
2882 icbtag
= &vat_node
->fe
->icbtag
;
2883 mtime
= &vat_node
->fe
->mtime
;
2884 unique_id
= udf_rw64(vat_node
->fe
->unique_id
);
2886 vat_length
= udf_rw64(vat_node
->efe
->inf_len
);
2887 icbtag
= &vat_node
->efe
->icbtag
;
2888 mtime
= &vat_node
->efe
->mtime
;
2889 unique_id
= udf_rw64(vat_node
->efe
->unique_id
);
2892 /* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
2893 filetype
= icbtag
->file_type
;
2894 if ((filetype
!= 0) && (filetype
!= UDF_ICB_FILETYPE_VAT
))
2897 DPRINTF(VOLUMES
, ("\tPossible VAT length %d\n", vat_length
));
2899 vat_table_alloc_len
=
2900 ((vat_length
+ UDF_VAT_CHUNKSIZE
-1) / UDF_VAT_CHUNKSIZE
)
2901 * UDF_VAT_CHUNKSIZE
;
2903 vat_table
= malloc(vat_table_alloc_len
, M_UDFVOLD
,
2904 M_CANFAIL
| M_WAITOK
);
2905 if (vat_table
== NULL
) {
2906 printf("allocation of %d bytes failed for VAT\n",
2907 vat_table_alloc_len
);
2911 /* allocate piece to read in head or tail of VAT file */
2912 raw_vat
= malloc(sector_size
, M_TEMP
, M_WAITOK
);
2915 * check contents of the file if its the old 1.50 VAT table format.
2916 * Its notoriously broken and allthough some implementations support an
2917 * extention as defined in the UDF 1.50 errata document, its doubtfull
2918 * to be useable since a lot of implementations don't maintain it.
2920 lvinfo
= ump
->logvol_info
;
2922 if (filetype
== 0) {
2925 vat_entries
= (vat_length
-36)/4;
2927 /* read in tail of virtual allocation table file */
2928 error
= vn_rdwr(UIO_READ
, vat_node
->vnode
,
2929 (uint8_t *) raw_vat
,
2930 sizeof(struct udf_oldvat_tail
),
2932 UIO_SYSSPACE
, IO_SYNC
| IO_NODELOCKED
, FSCRED
,
2937 /* check 1.50 VAT */
2938 oldvat_tl
= (struct udf_oldvat_tail
*) raw_vat
;
2939 regid_name
= (char *) oldvat_tl
->id
.id
;
2940 error
= strncmp(regid_name
, "*UDF Virtual Alloc Tbl", 22);
2942 DPRINTF(VOLUMES
, ("VAT format 1.50 rejected\n"));
2948 * update LVID from "*UDF VAT LVExtension" extended attribute
2951 udf_update_lvid_from_vat_extattr(vat_node
);
2953 /* read in head of virtual allocation table file */
2954 error
= vn_rdwr(UIO_READ
, vat_node
->vnode
,
2955 (uint8_t *) raw_vat
,
2956 sizeof(struct udf_vat
), 0,
2957 UIO_SYSSPACE
, IO_SYNC
| IO_NODELOCKED
, FSCRED
,
2963 vat
= (struct udf_vat
*) raw_vat
;
2964 vat_offset
= vat
->header_len
;
2965 vat_entries
= (vat_length
- vat_offset
)/4;
2968 lvinfo
->num_files
= vat
->num_files
;
2969 lvinfo
->num_directories
= vat
->num_directories
;
2970 lvinfo
->min_udf_readver
= vat
->min_udf_readver
;
2971 lvinfo
->min_udf_writever
= vat
->min_udf_writever
;
2972 lvinfo
->max_udf_writever
= vat
->max_udf_writever
;
2974 udf_update_logvolname(ump
, vat
->logvol_id
);
2977 /* read in complete VAT file */
2978 error
= vn_rdwr(UIO_READ
, vat_node
->vnode
,
2981 UIO_SYSSPACE
, IO_SYNC
| IO_NODELOCKED
, FSCRED
,
2984 printf("read in of complete VAT file failed (error %d)\n",
2989 DPRINTF(VOLUMES
, ("VAT format accepted, marking it closed\n"));
2990 ump
->logvol_integrity
->lvint_next_unique_id
= udf_rw64(unique_id
);
2991 ump
->logvol_integrity
->integrity_type
= udf_rw32(UDF_INTEGRITY_CLOSED
);
2992 ump
->logvol_integrity
->time
= *mtime
;
2994 ump
->vat_table_len
= vat_length
;
2995 ump
->vat_table_alloc_len
= vat_table_alloc_len
;
2996 ump
->vat_table
= vat_table
;
2997 ump
->vat_offset
= vat_offset
;
2998 ump
->vat_entries
= vat_entries
;
2999 ump
->vat_last_free_lb
= 0; /* start at beginning */
3004 free(vat_table
, M_UDFVOLD
);
3006 free(raw_vat
, M_TEMP
);
3011 /* --------------------------------------------------------------------- */
3014 udf_search_vat(struct udf_mount
*ump
, union udf_pmap
*mapping
)
3016 struct udf_node
*vat_node
;
3017 struct long_ad icb_loc
;
3018 uint32_t early_vat_loc
, late_vat_loc
, vat_loc
;
3021 /* mapping info not needed */
3024 vat_loc
= ump
->last_possible_vat_location
;
3025 early_vat_loc
= vat_loc
- 256; /* 8 blocks of 32 sectors */
3027 DPRINTF(VOLUMES
, ("1) last possible %d, early_vat_loc %d \n",
3028 vat_loc
, early_vat_loc
));
3029 early_vat_loc
= MAX(early_vat_loc
, ump
->first_possible_vat_location
);
3030 late_vat_loc
= vat_loc
+ 1024;
3032 DPRINTF(VOLUMES
, ("2) last possible %d, early_vat_loc %d \n",
3033 vat_loc
, early_vat_loc
));
3035 /* start looking from the end of the range */
3037 DPRINTF(VOLUMES
, ("Checking for VAT at sector %d\n", vat_loc
));
3038 icb_loc
.loc
.part_num
= udf_rw16(UDF_VTOP_RAWPART
);
3039 icb_loc
.loc
.lb_num
= udf_rw32(vat_loc
);
3041 error
= udf_get_node(ump
, &icb_loc
, &vat_node
);
3043 error
= udf_check_for_vat(vat_node
);
3044 DPRINTFIF(VOLUMES
, !error
,
3045 ("VAT accepted at %d\n", vat_loc
));
3050 vput(vat_node
->vnode
);
3053 vat_loc
--; /* walk backwards */
3054 } while (vat_loc
>= early_vat_loc
);
3056 /* keep our VAT node around */
3058 UDF_SET_SYSTEMFILE(vat_node
->vnode
);
3059 ump
->vat_node
= vat_node
;
3065 /* --------------------------------------------------------------------- */
3068 udf_read_sparables(struct udf_mount
*ump
, union udf_pmap
*mapping
)
3070 union dscrptr
*dscr
;
3071 struct part_map_spare
*pms
= &mapping
->pms
;
3076 * The partition mapping passed on to us specifies the information we
3077 * need to locate and initialise the sparable partition mapping
3078 * information we need.
3081 DPRINTF(VOLUMES
, ("Read sparable table\n"));
3082 ump
->sparable_packet_size
= udf_rw16(pms
->packet_len
);
3083 KASSERT(ump
->sparable_packet_size
>= ump
->packet_size
); /* XXX */
3085 for (spar
= 0; spar
< pms
->n_st
; spar
++) {
3086 lb_num
= pms
->st_loc
[spar
];
3087 DPRINTF(VOLUMES
, ("Checking for sparing table %d\n", lb_num
));
3088 error
= udf_read_phys_dscr(ump
, lb_num
, M_UDFVOLD
, &dscr
);
3089 if (!error
&& dscr
) {
3090 if (udf_rw16(dscr
->tag
.id
) == TAGID_SPARING_TABLE
) {
3091 if (ump
->sparing_table
)
3092 free(ump
->sparing_table
, M_UDFVOLD
);
3093 ump
->sparing_table
= &dscr
->spt
;
3096 ("Sparing table accepted (%d entries)\n",
3097 udf_rw16(ump
->sparing_table
->rt_l
)));
3098 break; /* we're done */
3102 free(dscr
, M_UDFVOLD
);
3105 if (ump
->sparing_table
)
3111 /* --------------------------------------------------------------------- */
3114 udf_read_metadata_nodes(struct udf_mount
*ump
, union udf_pmap
*mapping
)
3116 struct part_map_meta
*pmm
= &mapping
->pmm
;
3117 struct long_ad icb_loc
;
3121 DPRINTF(VOLUMES
, ("Reading in Metadata files\n"));
3122 icb_loc
.loc
.part_num
= pmm
->part_num
;
3123 icb_loc
.loc
.lb_num
= pmm
->meta_file_lbn
;
3124 DPRINTF(VOLUMES
, ("Metadata file\n"));
3125 error
= udf_get_node(ump
, &icb_loc
, &ump
->metadata_node
);
3126 if (ump
->metadata_node
) {
3127 vp
= ump
->metadata_node
->vnode
;
3128 UDF_SET_SYSTEMFILE(vp
);
3131 icb_loc
.loc
.lb_num
= pmm
->meta_mirror_file_lbn
;
3132 if (icb_loc
.loc
.lb_num
!= -1) {
3133 DPRINTF(VOLUMES
, ("Metadata copy file\n"));
3134 error
= udf_get_node(ump
, &icb_loc
, &ump
->metadatamirror_node
);
3135 if (ump
->metadatamirror_node
) {
3136 vp
= ump
->metadatamirror_node
->vnode
;
3137 UDF_SET_SYSTEMFILE(vp
);
3141 icb_loc
.loc
.lb_num
= pmm
->meta_bitmap_file_lbn
;
3142 if (icb_loc
.loc
.lb_num
!= -1) {
3143 DPRINTF(VOLUMES
, ("Metadata bitmap file\n"));
3144 error
= udf_get_node(ump
, &icb_loc
, &ump
->metadatabitmap_node
);
3145 if (ump
->metadatabitmap_node
) {
3146 vp
= ump
->metadatabitmap_node
->vnode
;
3147 UDF_SET_SYSTEMFILE(vp
);
3151 /* if we're mounting read-only we relax the requirements */
3152 if (ump
->vfs_mountp
->mnt_flag
& MNT_RDONLY
) {
3154 if (ump
->metadata_node
)
3156 if ((ump
->metadata_node
== NULL
) && (ump
->metadatamirror_node
)) {
3157 printf( "udf mount: Metadata file not readable, "
3158 "substituting Metadata copy file\n");
3159 ump
->metadata_node
= ump
->metadatamirror_node
;
3160 ump
->metadatamirror_node
= NULL
;
3164 /* mounting read/write */
3165 /* XXX DISABLED! metadata writing is not working yet XXX */
3169 DPRINTFIF(VOLUMES
, error
, ("udf mount: failed to read "
3170 "metadata files\n"));
3174 /* --------------------------------------------------------------------- */
3177 udf_read_vds_tables(struct udf_mount
*ump
)
3179 union udf_pmap
*mapping
;
3180 /* struct udf_args *args = &ump->mount_args; */
3181 uint32_t n_pm
, mt_l
;
3187 /* Iterate (again) over the part mappings for locations */
3188 n_pm
= udf_rw32(ump
->logical_vol
->n_pm
); /* num partmaps */
3189 mt_l
= udf_rw32(ump
->logical_vol
->mt_l
); /* partmaps data length */
3190 pmap_pos
= ump
->logical_vol
->maps
;
3192 for (log_part
= 0; log_part
< n_pm
; log_part
++) {
3193 mapping
= (union udf_pmap
*) pmap_pos
;
3194 switch (ump
->vtop_tp
[log_part
]) {
3195 case UDF_VTOP_TYPE_PHYS
:
3198 case UDF_VTOP_TYPE_VIRT
:
3199 /* search and load VAT */
3200 error
= udf_search_vat(ump
, mapping
);
3204 case UDF_VTOP_TYPE_SPARABLE
:
3205 /* load one of the sparable tables */
3206 error
= udf_read_sparables(ump
, mapping
);
3210 case UDF_VTOP_TYPE_META
:
3211 /* load the associated file descriptors */
3212 error
= udf_read_metadata_nodes(ump
, mapping
);
3219 pmap_size
= pmap_pos
[1];
3220 pmap_pos
+= pmap_size
;
3223 /* read in and check unallocated and free space info if writing */
3224 if ((ump
->vfs_mountp
->mnt_flag
& MNT_RDONLY
) == 0) {
3225 error
= udf_read_physical_partition_spacetables(ump
);
3229 /* also read in metadata partition spacebitmap if defined */
3230 error
= udf_read_metadata_partition_spacetable(ump
);
3237 /* --------------------------------------------------------------------- */
3240 udf_read_rootdirs(struct udf_mount
*ump
)
3242 union dscrptr
*dscr
;
3243 /* struct udf_args *args = &ump->mount_args; */
3244 struct udf_node
*rootdir_node
, *streamdir_node
;
3245 struct long_ad fsd_loc
, *dir_loc
;
3246 uint32_t lb_num
, dummy
;
3251 /* TODO implement FSD reading in separate function like integrity? */
3252 /* get fileset descriptor sequence */
3253 fsd_loc
= ump
->logical_vol
->lv_fsd_loc
;
3254 fsd_len
= udf_rw32(fsd_loc
.len
);
3258 while (fsd_len
|| error
) {
3259 DPRINTF(VOLUMES
, ("fsd_len = %d\n", fsd_len
));
3260 /* translate fsd_loc to lb_num */
3261 error
= udf_translate_vtop(ump
, &fsd_loc
, &lb_num
, &dummy
);
3264 DPRINTF(VOLUMES
, ("Reading FSD at lb %d\n", lb_num
));
3265 error
= udf_read_phys_dscr(ump
, lb_num
, M_UDFVOLD
, &dscr
);
3267 if (error
|| (dscr
== NULL
))
3271 dscr_type
= udf_rw16(dscr
->tag
.id
);
3272 if (dscr_type
== TAGID_TERM
)
3274 if (dscr_type
!= TAGID_FSD
) {
3275 free(dscr
, M_UDFVOLD
);
3280 * TODO check for multiple fileset descriptors; its only
3281 * picking the last now. Also check for FSD
3282 * correctness/interpretability
3286 if (ump
->fileset_desc
) {
3287 free(ump
->fileset_desc
, M_UDFVOLD
);
3289 ump
->fileset_desc
= &dscr
->fsd
;
3292 /* continue to the next fsd */
3293 fsd_len
-= ump
->discinfo
.sector_size
;
3294 fsd_loc
.loc
.lb_num
= udf_rw32(udf_rw32(fsd_loc
.loc
.lb_num
)+1);
3296 /* follow up to fsd->next_ex (long_ad) if its not null */
3297 if (udf_rw32(ump
->fileset_desc
->next_ex
.len
)) {
3298 DPRINTF(VOLUMES
, ("follow up FSD extent\n"));
3299 fsd_loc
= ump
->fileset_desc
->next_ex
;
3300 fsd_len
= udf_rw32(ump
->fileset_desc
->next_ex
.len
);
3304 free(dscr
, M_UDFVOLD
);
3306 /* there has to be one */
3307 if (ump
->fileset_desc
== NULL
)
3310 DPRINTF(VOLUMES
, ("FSD read in fine\n"));
3311 DPRINTF(VOLUMES
, ("Updating fsd logical volume id\n"));
3312 udf_update_logvolname(ump
, ump
->logical_vol
->logvol_id
);
3315 * Now the FSD is known, read in the rootdirectory and if one exists,
3316 * the system stream dir. Some files in the system streamdir are not
3317 * wanted in this implementation since they are not maintained. If
3318 * writing is enabled we'll delete these files if they exist.
3321 rootdir_node
= streamdir_node
= NULL
;
3324 /* try to read in the rootdir */
3325 dir_loc
= &ump
->fileset_desc
->rootdir_icb
;
3326 error
= udf_get_node(ump
, dir_loc
, &rootdir_node
);
3330 /* aparently it read in fine */
3333 * Try the system stream directory; not very likely in the ones we
3334 * test, but for completeness.
3336 dir_loc
= &ump
->fileset_desc
->streamdir_icb
;
3337 if (udf_rw32(dir_loc
->len
)) {
3338 printf("udf_read_rootdirs: streamdir defined ");
3339 error
= udf_get_node(ump
, dir_loc
, &streamdir_node
);
3341 printf("but error in streamdir reading\n");
3343 printf("but ignored\n");
3345 * TODO process streamdir `baddies' i.e. files we dont
3351 DPRINTF(VOLUMES
, ("Rootdir(s) read in fine\n"));
3353 /* release the vnodes again; they'll be auto-recycled later */
3354 if (streamdir_node
) {
3355 vput(streamdir_node
->vnode
);
3358 vput(rootdir_node
->vnode
);
3364 /* --------------------------------------------------------------------- */
3366 /* To make absolutely sure we are NOT returning zero, add one :) */
3369 udf_get_node_id(const struct long_ad
*icbptr
)
3371 /* ought to be enough since each mountpoint has its own chain */
3372 return udf_rw32(icbptr
->loc
.lb_num
) + 1;
3377 udf_compare_icb(const struct long_ad
*a
, const struct long_ad
*b
)
3379 if (udf_rw16(a
->loc
.part_num
) < udf_rw16(b
->loc
.part_num
))
3381 if (udf_rw16(a
->loc
.part_num
) > udf_rw16(b
->loc
.part_num
))
3384 if (udf_rw32(a
->loc
.lb_num
) < udf_rw32(b
->loc
.lb_num
))
3386 if (udf_rw32(a
->loc
.lb_num
) > udf_rw32(b
->loc
.lb_num
))
3394 udf_compare_rbnodes(const struct rb_node
*a
, const struct rb_node
*b
)
3396 struct udf_node
*a_node
= RBTOUDFNODE(a
);
3397 struct udf_node
*b_node
= RBTOUDFNODE(b
);
3399 return udf_compare_icb(&a_node
->loc
, &b_node
->loc
);
3404 udf_compare_rbnode_icb(const struct rb_node
*a
, const void *key
)
3406 struct udf_node
*a_node
= RBTOUDFNODE(a
);
3407 const struct long_ad
* const icb
= key
;
3409 return udf_compare_icb(&a_node
->loc
, icb
);
3413 static const struct rb_tree_ops udf_node_rbtree_ops
= {
3414 .rbto_compare_nodes
= udf_compare_rbnodes
,
3415 .rbto_compare_key
= udf_compare_rbnode_icb
,
3420 udf_init_nodes_tree(struct udf_mount
*ump
)
3422 rb_tree_init(&ump
->udf_node_tree
, &udf_node_rbtree_ops
);
3426 static struct udf_node
*
3427 udf_node_lookup(struct udf_mount
*ump
, struct long_ad
*icbptr
)
3429 struct rb_node
*rb_node
;
3430 struct udf_node
*udf_node
;
3434 mutex_enter(&ump
->ihash_lock
);
3436 rb_node
= rb_tree_find_node(&ump
->udf_node_tree
, icbptr
);
3438 udf_node
= RBTOUDFNODE(rb_node
);
3439 vp
= udf_node
->vnode
;
3441 mutex_enter(&vp
->v_interlock
);
3442 mutex_exit(&ump
->ihash_lock
);
3443 if (vget(vp
, LK_EXCLUSIVE
| LK_INTERLOCK
))
3447 mutex_exit(&ump
->ihash_lock
);
3454 udf_register_node(struct udf_node
*udf_node
)
3456 struct udf_mount
*ump
= udf_node
->ump
;
3458 /* add node to the rb tree */
3459 mutex_enter(&ump
->ihash_lock
);
3460 rb_tree_insert_node(&ump
->udf_node_tree
, &udf_node
->rbnode
);
3461 mutex_exit(&ump
->ihash_lock
);
3466 udf_deregister_node(struct udf_node
*udf_node
)
3468 struct udf_mount
*ump
= udf_node
->ump
;
3470 /* remove node from the rb tree */
3471 mutex_enter(&ump
->ihash_lock
);
3472 rb_tree_remove_node(&ump
->udf_node_tree
, &udf_node
->rbnode
);
3473 mutex_exit(&ump
->ihash_lock
);
3476 /* --------------------------------------------------------------------- */
3479 udf_validate_session_start(struct udf_mount
*ump
)
3481 struct mmc_trackinfo trackinfo
;
3482 struct vrs_desc
*vrs
;
3483 uint32_t tracknr
, sessionnr
, sector
, sector_size
;
3484 uint32_t iso9660_vrs
, write_track_start
;
3485 uint8_t *buffer
, *blank
, *pos
;
3486 int blks
, max_sectors
, vrs_len
;
3489 /* disc appendable? */
3490 if (ump
->discinfo
.disc_state
== MMC_STATE_FULL
)
3493 /* already written here? if so, there should be an ISO VDS */
3494 if (ump
->discinfo
.last_session_state
== MMC_STATE_INCOMPLETE
)
3498 * Check if the first track of the session is blank and if so, copy or
3499 * create a dummy ISO descriptor so the disc is valid again.
3502 tracknr
= ump
->discinfo
.first_track_last_session
;
3503 memset(&trackinfo
, 0, sizeof(struct mmc_trackinfo
));
3504 trackinfo
.tracknr
= tracknr
;
3505 error
= udf_update_trackinfo(ump
, &trackinfo
);
3509 udf_dump_trackinfo(&trackinfo
);
3510 KASSERT(trackinfo
.flags
& (MMC_TRACKINFO_BLANK
| MMC_TRACKINFO_RESERVED
));
3511 KASSERT(trackinfo
.sessionnr
> 1);
3513 KASSERT(trackinfo
.flags
& MMC_TRACKINFO_NWA_VALID
);
3514 write_track_start
= trackinfo
.next_writable
;
3516 /* we have to copy the ISO VRS from a former session */
3517 DPRINTF(VOLUMES
, ("validate_session_start: "
3518 "blank or reserved track, copying VRS\n"));
3520 /* sessionnr should be the session we're mounting */
3521 sessionnr
= ump
->mount_args
.sessionnr
;
3523 /* start at the first track */
3524 tracknr
= ump
->discinfo
.first_track
;
3525 while (tracknr
<= ump
->discinfo
.num_tracks
) {
3526 trackinfo
.tracknr
= tracknr
;
3527 error
= udf_update_trackinfo(ump
, &trackinfo
);
3529 DPRINTF(VOLUMES
, ("failed to get trackinfo; aborting\n"));
3532 if (trackinfo
.sessionnr
== sessionnr
)
3536 if (trackinfo
.sessionnr
!= sessionnr
) {
3537 DPRINTF(VOLUMES
, ("failed to get trackinfo; aborting\n"));
3541 DPRINTF(VOLUMES
, ("found possible former ISO VRS at\n"));
3542 udf_dump_trackinfo(&trackinfo
);
3545 * location of iso9660 vrs is defined as first sector AFTER 32kb,
3546 * minimum ISO `sector size' 2048
3548 sector_size
= ump
->discinfo
.sector_size
;
3549 iso9660_vrs
= ((32*1024 + sector_size
- 1) / sector_size
)
3550 + trackinfo
.track_start
;
3552 buffer
= malloc(UDF_ISO_VRS_SIZE
, M_TEMP
, M_WAITOK
);
3553 max_sectors
= UDF_ISO_VRS_SIZE
/ sector_size
;
3554 blks
= MAX(1, 2048 / sector_size
);
3557 for (sector
= 0; sector
< max_sectors
; sector
+= blks
) {
3558 pos
= buffer
+ sector
* sector_size
;
3559 error
= udf_read_phys_sectors(ump
, UDF_C_DSCR
, pos
,
3560 iso9660_vrs
+ sector
, blks
);
3563 /* check this ISO descriptor */
3564 vrs
= (struct vrs_desc
*) pos
;
3565 DPRINTF(VOLUMES
, ("got VRS id `%4s`\n", vrs
->identifier
));
3566 if (strncmp(vrs
->identifier
, VRS_CD001
, 5) == 0)
3568 if (strncmp(vrs
->identifier
, VRS_CDW02
, 5) == 0)
3570 if (strncmp(vrs
->identifier
, VRS_BEA01
, 5) == 0)
3572 if (strncmp(vrs
->identifier
, VRS_NSR02
, 5) == 0)
3574 if (strncmp(vrs
->identifier
, VRS_NSR03
, 5) == 0)
3576 if (strncmp(vrs
->identifier
, VRS_TEA01
, 5) == 0)
3578 /* now what? for now, end of sequence */
3581 vrs_len
= sector
+ blks
;
3583 DPRINTF(VOLUMES
, ("error reading old ISO VRS\n"));
3584 DPRINTF(VOLUMES
, ("creating minimal ISO VRS\n"));
3586 memset(buffer
, 0, UDF_ISO_VRS_SIZE
);
3588 vrs
= (struct vrs_desc
*) (buffer
);
3589 vrs
->struct_type
= 0;
3591 memcpy(vrs
->identifier
,VRS_BEA01
, 5);
3593 vrs
= (struct vrs_desc
*) (buffer
+ 2048);
3594 vrs
->struct_type
= 0;
3596 if (udf_rw16(ump
->logical_vol
->tag
.descriptor_ver
) == 2) {
3597 memcpy(vrs
->identifier
,VRS_NSR02
, 5);
3599 memcpy(vrs
->identifier
,VRS_NSR03
, 5);
3602 vrs
= (struct vrs_desc
*) (buffer
+ 4096);
3603 vrs
->struct_type
= 0;
3605 memcpy(vrs
->identifier
, VRS_TEA01
, 5);
3610 DPRINTF(VOLUMES
, ("Got VRS of %d sectors long\n", vrs_len
));
3613 * location of iso9660 vrs is defined as first sector AFTER 32kb,
3614 * minimum ISO `sector size' 2048
3616 sector_size
= ump
->discinfo
.sector_size
;
3617 iso9660_vrs
= ((32*1024 + sector_size
- 1) / sector_size
)
3618 + write_track_start
;
3620 /* write out 32 kb */
3621 blank
= malloc(sector_size
, M_TEMP
, M_WAITOK
);
3622 memset(blank
, 0, sector_size
);
3624 for (sector
= write_track_start
; sector
< iso9660_vrs
; sector
++) {
3625 error
= udf_write_phys_sectors(ump
, UDF_C_ABSOLUTE
,
3631 /* write out our ISO VRS */
3632 KASSERT(sector
== iso9660_vrs
);
3633 error
= udf_write_phys_sectors(ump
, UDF_C_ABSOLUTE
, buffer
,
3638 /* fill upto the first anchor at S+256 */
3639 for (; sector
< write_track_start
+256; sector
++) {
3640 error
= udf_write_phys_sectors(ump
, UDF_C_ABSOLUTE
,
3647 /* write out anchor; write at ABSOLUTE place! */
3648 error
= udf_write_phys_dscr_sync(ump
, NULL
, UDF_C_ABSOLUTE
,
3649 (union dscrptr
*) ump
->anchors
[0], sector
, sector
);
3651 printf("writeout of anchor failed!\n");
3654 free(blank
, M_TEMP
);
3655 free(buffer
, M_TEMP
);
3658 printf("udf_open_session: error writing iso vrs! : "
3659 "leaving disc in compromised state!\n");
3661 /* synchronise device caches */
3662 (void) udf_synchronise_caches(ump
);
3669 udf_open_logvol(struct udf_mount
*ump
)
3671 int logvol_integrity
;
3674 /* already/still open? */
3675 logvol_integrity
= udf_rw32(ump
->logvol_integrity
->integrity_type
);
3676 if (logvol_integrity
== UDF_INTEGRITY_OPEN
)
3679 /* can we open it ? */
3680 if (ump
->vfs_mountp
->mnt_flag
& MNT_RDONLY
)
3683 /* setup write parameters */
3684 DPRINTF(VOLUMES
, ("Setting up write parameters\n"));
3685 if ((error
= udf_setup_writeparams(ump
)) != 0)
3688 /* determine data and metadata tracks (most likely same) */
3689 error
= udf_search_writing_tracks(ump
);
3691 /* most likely lack of space */
3692 printf("udf_open_logvol: error searching writing tracks\n");
3696 /* writeout/update lvint on disc or only in memory */
3697 DPRINTF(VOLUMES
, ("Opening logical volume\n"));
3698 if (ump
->lvopen
& UDF_OPEN_SESSION
) {
3699 /* TODO optional track reservation opening */
3700 error
= udf_validate_session_start(ump
);
3704 /* determine data and metadata tracks again */
3705 error
= udf_search_writing_tracks(ump
);
3709 ump
->logvol_integrity
->integrity_type
= udf_rw32(UDF_INTEGRITY_OPEN
);
3711 /* do we need to write it out? */
3712 if (ump
->lvopen
& UDF_WRITE_LVINT
) {
3713 error
= udf_writeout_lvint(ump
, ump
->lvopen
);
3714 /* if we couldn't write it mark it closed again */
3716 ump
->logvol_integrity
->integrity_type
=
3717 udf_rw32(UDF_INTEGRITY_CLOSED
);
3727 udf_close_logvol(struct udf_mount
*ump
, int mntflags
)
3729 struct vnode
*devvp
= ump
->devvp
;
3730 struct mmc_op mmc_op
;
3731 int logvol_integrity
;
3732 int error
= 0, error1
= 0, error2
= 0;
3736 /* already/still closed? */
3737 logvol_integrity
= udf_rw32(ump
->logvol_integrity
->integrity_type
);
3738 if (logvol_integrity
== UDF_INTEGRITY_CLOSED
)
3741 /* writeout/update lvint or write out VAT */
3742 DPRINTF(VOLUMES
, ("udf_close_logvol: closing logical volume\n"));
3744 if (ump
->lvclose
& UDF_CLOSE_SESSION
)
3745 KASSERT(ump
->lvclose
& UDF_WRITE_VAT
);
3748 if (ump
->lvclose
& UDF_WRITE_VAT
) {
3749 DPRINTF(VOLUMES
, ("lvclose & UDF_WRITE_VAT\n"));
3751 /* write out the VAT data and all its descriptors */
3752 DPRINTF(VOLUMES
, ("writeout vat_node\n"));
3753 udf_writeout_vat(ump
);
3754 vflushbuf(ump
->vat_node
->vnode
, 1 /* sync */);
3756 (void) VOP_FSYNC(ump
->vat_node
->vnode
,
3757 FSCRED
, FSYNC_WAIT
, 0, 0);
3759 if (ump
->lvclose
& UDF_CLOSE_SESSION
) {
3760 DPRINTF(VOLUMES
, ("udf_close_logvol: closing session "
3764 /* at least two DVD packets and 3 CD-R packets */
3769 * TODO calculate the available space and if the disc is
3770 * allmost full, write out till end-256-1 with banks, write
3771 * AVDP and fill up with VATs, then close session and close
3774 if (ump
->lvclose
& UDF_FINALISE_DISC
) {
3775 error
= udf_write_phys_dscr_sync(ump
, NULL
,
3777 (union dscrptr
*) ump
->anchors
[0],
3780 printf("writeout of anchor failed!\n");
3782 /* pad space with VAT ICBs */
3787 /* write out a number of VAT nodes */
3789 for (n
= 0; n
< nvats
; n
++) {
3790 /* will now only write last FE/EFE */
3791 ump
->vat_node
->i_flags
|= IN_MODIFIED
;
3792 error
= VOP_FSYNC(ump
->vat_node
->vnode
,
3793 FSCRED
, FSYNC_WAIT
, 0, 0);
3798 /* arbitrary; but at least one or two CD frames */
3799 printf("writeout of at least 14 VATs failed\n");
3804 /* NOTE the disc is in a (minimal) valid state now; no erroring out */
3806 /* finish closing of session */
3807 if (ump
->lvclose
& UDF_CLOSE_SESSION
) {
3808 error
= udf_validate_session_start(ump
);
3812 (void) udf_synchronise_caches(ump
);
3814 /* close all associated tracks */
3815 tracknr
= ump
->discinfo
.first_track_last_session
;
3817 while (tracknr
<= ump
->discinfo
.last_track_last_session
) {
3818 DPRINTF(VOLUMES
, ("\tclosing possible open "
3819 "track %d\n", tracknr
));
3820 memset(&mmc_op
, 0, sizeof(mmc_op
));
3821 mmc_op
.operation
= MMC_OP_CLOSETRACK
;
3822 mmc_op
.mmc_profile
= ump
->discinfo
.mmc_profile
;
3823 mmc_op
.tracknr
= tracknr
;
3824 error
= VOP_IOCTL(devvp
, MMCOP
, &mmc_op
,
3827 printf("udf_close_logvol: closing of "
3828 "track %d failed\n", tracknr
);
3832 DPRINTF(VOLUMES
, ("closing session\n"));
3833 memset(&mmc_op
, 0, sizeof(mmc_op
));
3834 mmc_op
.operation
= MMC_OP_CLOSESESSION
;
3835 mmc_op
.mmc_profile
= ump
->discinfo
.mmc_profile
;
3836 mmc_op
.sessionnr
= ump
->discinfo
.num_sessions
;
3837 error
= VOP_IOCTL(devvp
, MMCOP
, &mmc_op
,
3840 printf("udf_close_logvol: closing of session"
3844 ump
->lvopen
|= UDF_OPEN_SESSION
;
3846 printf("udf_close_logvol: leaving disc as it is\n");
3847 ump
->lvclose
&= ~UDF_FINALISE_DISC
;
3851 if (ump
->lvclose
& UDF_FINALISE_DISC
) {
3852 memset(&mmc_op
, 0, sizeof(mmc_op
));
3853 mmc_op
.operation
= MMC_OP_FINALISEDISC
;
3854 mmc_op
.mmc_profile
= ump
->discinfo
.mmc_profile
;
3855 mmc_op
.sessionnr
= ump
->discinfo
.num_sessions
;
3856 error
= VOP_IOCTL(devvp
, MMCOP
, &mmc_op
,
3859 printf("udf_close_logvol: finalising disc"
3863 /* write out partition bitmaps if requested */
3864 if (ump
->lvclose
& UDF_WRITE_PART_BITMAPS
) {
3865 /* sync writeout metadata spacetable if existing */
3866 error1
= udf_write_metadata_partition_spacetable(ump
, true);
3868 printf( "udf_close_logvol: writeout of metadata space "
3871 /* sync writeout partition spacetables */
3872 error2
= udf_write_physical_partition_spacetables(ump
, true);
3874 printf( "udf_close_logvol: writeout of space tables "
3877 if (error1
|| error2
)
3878 return (error1
| error2
);
3880 ump
->lvclose
&= ~UDF_WRITE_PART_BITMAPS
;
3883 /* mark it closed */
3884 ump
->logvol_integrity
->integrity_type
= udf_rw32(UDF_INTEGRITY_CLOSED
);
3886 /* do we need to write out the logical volume integrity? */
3887 if (ump
->lvclose
& UDF_WRITE_LVINT
)
3888 error
= udf_writeout_lvint(ump
, ump
->lvopen
);
3890 /* HELP now what? mark it open again for now */
3891 ump
->logvol_integrity
->integrity_type
=
3892 udf_rw32(UDF_INTEGRITY_OPEN
);
3896 (void) udf_synchronise_caches(ump
);
3901 /* --------------------------------------------------------------------- */
3906 * static const struct genfs_ops udf_genfsops = {
3907 * .gop_size = genfs_size,
3909 * .gop_alloc = udf_gop_alloc,
3910 * allocate len bytes at offset
3911 * .gop_write = genfs_gop_write,
3912 * putpages interface code
3913 * .gop_markupdate = udf_gop_markupdate,
3914 * set update/modify flags etc.
3919 * Genfs interface. These four functions are the only ones defined though not
3920 * documented... great....
3924 * Called for allocating an extent of the file either by VOP_WRITE() or by
3925 * genfs filling up gaps.
3928 udf_gop_alloc(struct vnode
*vp
, off_t off
,
3929 off_t len
, int flags
, kauth_cred_t cred
)
3931 struct udf_node
*udf_node
= VTOI(vp
);
3932 struct udf_mount
*ump
= udf_node
->ump
;
3933 uint64_t lb_start
, lb_end
;
3934 uint32_t lb_size
, num_lb
;
3935 int udf_c_type
, vpart_num
, can_fail
;
3938 DPRINTF(ALLOC
, ("udf_gop_alloc called for offset %"PRIu64
" for %"PRIu64
" bytes, %s\n",
3939 off
, len
, flags
? "SYNC":"NONE"));
3942 * request the pages of our vnode and see how many pages will need to
3943 * be allocated and reserve that space
3945 lb_size
= udf_rw32(udf_node
->ump
->logical_vol
->lb_size
);
3946 lb_start
= off
/ lb_size
;
3947 lb_end
= (off
+ len
+ lb_size
-1) / lb_size
;
3948 num_lb
= lb_end
- lb_start
;
3950 udf_c_type
= udf_get_c_type(udf_node
);
3951 vpart_num
= udf_get_record_vpart(ump
, udf_c_type
);
3953 /* all requests can fail */
3956 /* fid's (directories) can't fail */
3957 if (udf_c_type
== UDF_C_FIDS
)
3960 /* system files can't fail */
3961 if (vp
->v_vflag
& VV_SYSTEM
)
3964 error
= udf_reserve_space(ump
, udf_node
, udf_c_type
,
3965 vpart_num
, num_lb
, can_fail
);
3967 DPRINTF(ALLOC
, ("\tlb_start %"PRIu64
", lb_end %"PRIu64
", num_lb %d\n",
3968 lb_start
, lb_end
, num_lb
));
3975 * callback from genfs to update our flags
3978 udf_gop_markupdate(struct vnode
*vp
, int flags
)
3980 struct udf_node
*udf_node
= VTOI(vp
);
3983 if ((flags
& GOP_UPDATE_ACCESSED
) != 0) {
3986 if ((flags
& GOP_UPDATE_MODIFIED
) != 0) {
3987 if (vp
->v_type
== VREG
) {
3988 mask
|= IN_CHANGE
| IN_UPDATE
;
3994 udf_node
->i_flags
|= mask
;
3999 static const struct genfs_ops udf_genfsops
= {
4000 .gop_size
= genfs_size
,
4001 .gop_alloc
= udf_gop_alloc
,
4002 .gop_write
= genfs_gop_write_rwmap
,
4003 .gop_markupdate
= udf_gop_markupdate
,
4007 /* --------------------------------------------------------------------- */
4010 udf_write_terminator(struct udf_mount
*ump
, uint32_t sector
)
4012 union dscrptr
*dscr
;
4015 dscr
= malloc(ump
->discinfo
.sector_size
, M_TEMP
, M_WAITOK
|M_ZERO
);
4016 udf_inittag(ump
, &dscr
->tag
, TAGID_TERM
, sector
);
4018 /* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
4019 dscr
->tag
.desc_crc_len
= udf_rw16(512-UDF_DESC_TAG_LENGTH
);
4020 (void) udf_validate_tag_and_crc_sums(dscr
);
4022 error
= udf_write_phys_dscr_sync(ump
, NULL
, UDF_C_DSCR
,
4023 dscr
, sector
, sector
);
4031 /* --------------------------------------------------------------------- */
4033 /* UDF<->unix converters */
4035 /* --------------------------------------------------------------------- */
4038 udf_perm_to_unix_mode(uint32_t perm
)
4042 mode
= ((perm
& UDF_FENTRY_PERM_USER_MASK
) );
4043 mode
|= ((perm
& UDF_FENTRY_PERM_GRP_MASK
) >> 2);
4044 mode
|= ((perm
& UDF_FENTRY_PERM_OWNER_MASK
) >> 4);
4049 /* --------------------------------------------------------------------- */
4052 unix_mode_to_udf_perm(mode_t mode
)
4056 perm
= ((mode
& S_IRWXO
) );
4057 perm
|= ((mode
& S_IRWXG
) << 2);
4058 perm
|= ((mode
& S_IRWXU
) << 4);
4059 perm
|= ((mode
& S_IWOTH
) << 3);
4060 perm
|= ((mode
& S_IWGRP
) << 5);
4061 perm
|= ((mode
& S_IWUSR
) << 7);
4066 /* --------------------------------------------------------------------- */
4069 udf_icb_to_unix_filetype(uint32_t icbftype
)
4072 case UDF_ICB_FILETYPE_DIRECTORY
:
4073 case UDF_ICB_FILETYPE_STREAMDIR
:
4075 case UDF_ICB_FILETYPE_FIFO
:
4077 case UDF_ICB_FILETYPE_CHARDEVICE
:
4079 case UDF_ICB_FILETYPE_BLOCKDEVICE
:
4081 case UDF_ICB_FILETYPE_RANDOMACCESS
:
4082 case UDF_ICB_FILETYPE_REALTIME
:
4084 case UDF_ICB_FILETYPE_SYMLINK
:
4086 case UDF_ICB_FILETYPE_SOCKET
:
4089 /* no idea what this is */
4093 /* --------------------------------------------------------------------- */
4096 udf_to_unix_name(char *result
, int result_len
, char *id
, int len
,
4097 struct charspec
*chsp
)
4099 uint16_t *raw_name
, *unix_name
;
4100 uint16_t *inchp
, ch
;
4102 const char *osta_id
= "OSTA Compressed Unicode";
4103 int ucode_chars
, nice_uchars
, is_osta_typ0
, nout
;
4105 raw_name
= malloc(2048 * sizeof(uint16_t), M_UDFTEMP
, M_WAITOK
);
4106 unix_name
= raw_name
+ 1024; /* split space in half */
4107 assert(sizeof(char) == sizeof(uint8_t));
4108 outchp
= (uint8_t *) result
;
4110 is_osta_typ0
= (chsp
->type
== 0);
4111 is_osta_typ0
&= (strcmp((char *) chsp
->inf
, osta_id
) == 0);
4114 *raw_name
= *unix_name
= 0;
4115 ucode_chars
= udf_UncompressUnicode(len
, (uint8_t *) id
, raw_name
);
4116 ucode_chars
= MIN(ucode_chars
, UnicodeLength((unicode_t
*) raw_name
));
4117 nice_uchars
= UDFTransName(unix_name
, raw_name
, ucode_chars
);
4119 for (inchp
= unix_name
; nice_uchars
>0; inchp
++, nice_uchars
--) {
4121 nout
= wput_utf8(outchp
, result_len
, ch
);
4122 outchp
+= nout
; result_len
-= nout
;
4127 /* assume 8bit char length byte latin-1 */
4129 assert(strlen((char *) (id
+1)) <= MAXNAMLEN
);
4130 strncpy((char *) result
, (char *) (id
+1), strlen((char *) (id
+1)));
4132 free(raw_name
, M_UDFTEMP
);
4135 /* --------------------------------------------------------------------- */
4138 unix_to_udf_name(char *result
, uint8_t *result_len
, char const *name
, int name_len
,
4139 struct charspec
*chsp
)
4144 const char *osta_id
= "OSTA Compressed Unicode";
4145 int udf_chars
, is_osta_typ0
, bits
;
4148 /* allocate temporary unicode-16 buffer */
4149 raw_name
= malloc(1024, M_UDFTEMP
, M_WAITOK
);
4151 /* convert utf8 to unicode-16 */
4156 for (cnt
= name_len
, udf_chars
= 0; cnt
;) {
4157 /*###3490 [cc] warning: passing argument 2 of 'wget_utf8' from incompatible pointer type%%%*/
4158 *outchp
= wget_utf8(&inchp
, &cnt
);
4164 /* null terminate just in case */
4167 is_osta_typ0
= (chsp
->type
== 0);
4168 is_osta_typ0
&= (strcmp((char *) chsp
->inf
, osta_id
) == 0);
4170 udf_chars
= udf_CompressUnicode(udf_chars
, bits
,
4171 (unicode_t
*) raw_name
,
4174 printf("unix to udf name: no CHSP0 ?\n");
4175 /* XXX assume 8bit char length byte latin-1 */
4176 *result
++ = 8; udf_chars
= 1;
4177 strncpy(result
, name
+ 1, name_len
);
4178 udf_chars
+= name_len
;
4180 *result_len
= udf_chars
;
4181 free(raw_name
, M_UDFTEMP
);
4184 /* --------------------------------------------------------------------- */
4187 udf_timestamp_to_timespec(struct udf_mount
*ump
,
4188 struct timestamp
*timestamp
,
4189 struct timespec
*timespec
)
4191 struct clock_ymdhms ymdhms
;
4192 uint32_t usecs
, secs
, nsecs
;
4195 /* fill in ymdhms structure from timestamp */
4196 memset(&ymdhms
, 0, sizeof(ymdhms
));
4197 ymdhms
.dt_year
= udf_rw16(timestamp
->year
);
4198 ymdhms
.dt_mon
= timestamp
->month
;
4199 ymdhms
.dt_day
= timestamp
->day
;
4200 ymdhms
.dt_wday
= 0; /* ? */
4201 ymdhms
.dt_hour
= timestamp
->hour
;
4202 ymdhms
.dt_min
= timestamp
->minute
;
4203 ymdhms
.dt_sec
= timestamp
->second
;
4205 secs
= clock_ymdhms_to_secs(&ymdhms
);
4206 usecs
= timestamp
->usec
+
4207 100*timestamp
->hund_usec
+ 10000*timestamp
->centisec
;
4208 nsecs
= usecs
* 1000;
4211 * Calculate the time zone. The timezone is 12 bit signed 2's
4212 * compliment, so we gotta do some extra magic to handle it right.
4214 tz
= udf_rw16(timestamp
->type_tz
);
4215 tz
&= 0x0fff; /* only lower 12 bits are significant */
4216 if (tz
& 0x0800) /* sign extention */
4219 /* TODO check timezone conversion */
4220 /* check if we are specified a timezone to convert */
4221 if (udf_rw16(timestamp
->type_tz
) & 0x1000) {
4222 if ((int16_t) tz
!= -2047)
4223 secs
-= (int16_t) tz
* 60;
4225 secs
-= ump
->mount_args
.gmtoff
;
4228 timespec
->tv_sec
= secs
;
4229 timespec
->tv_nsec
= nsecs
;
4234 udf_timespec_to_timestamp(struct timespec
*timespec
, struct timestamp
*timestamp
)
4236 struct clock_ymdhms ymdhms
;
4237 uint32_t husec
, usec
, csec
;
4239 (void) clock_secs_to_ymdhms(timespec
->tv_sec
, &ymdhms
);
4241 usec
= timespec
->tv_nsec
/ 1000;
4243 usec
-= husec
* 100; /* only 0-99 in usec */
4244 csec
= husec
/ 100; /* only 0-99 in csec */
4245 husec
-= csec
* 100; /* only 0-99 in husec */
4247 /* set method 1 for CUT/GMT */
4248 timestamp
->type_tz
= udf_rw16((1<<12) + 0);
4249 timestamp
->year
= udf_rw16(ymdhms
.dt_year
);
4250 timestamp
->month
= ymdhms
.dt_mon
;
4251 timestamp
->day
= ymdhms
.dt_day
;
4252 timestamp
->hour
= ymdhms
.dt_hour
;
4253 timestamp
->minute
= ymdhms
.dt_min
;
4254 timestamp
->second
= ymdhms
.dt_sec
;
4255 timestamp
->centisec
= csec
;
4256 timestamp
->hund_usec
= husec
;
4257 timestamp
->usec
= usec
;
4260 /* --------------------------------------------------------------------- */
4263 * Attribute and filetypes converters with get/set pairs
4267 udf_getaccessmode(struct udf_node
*udf_node
)
4269 struct file_entry
*fe
= udf_node
->fe
;
4270 struct extfile_entry
*efe
= udf_node
->efe
;
4271 uint32_t udf_perm
, icbftype
;
4272 uint32_t mode
, ftype
;
4275 UDF_LOCK_NODE(udf_node
, 0);
4277 udf_perm
= udf_rw32(fe
->perm
);
4278 icbftype
= fe
->icbtag
.file_type
;
4279 icbflags
= udf_rw16(fe
->icbtag
.flags
);
4281 assert(udf_node
->efe
);
4282 udf_perm
= udf_rw32(efe
->perm
);
4283 icbftype
= efe
->icbtag
.file_type
;
4284 icbflags
= udf_rw16(efe
->icbtag
.flags
);
4287 mode
= udf_perm_to_unix_mode(udf_perm
);
4288 ftype
= udf_icb_to_unix_filetype(icbftype
);
4290 /* set suid, sgid, sticky from flags in fe/efe */
4291 if (icbflags
& UDF_ICB_TAG_FLAGS_SETUID
)
4293 if (icbflags
& UDF_ICB_TAG_FLAGS_SETGID
)
4295 if (icbflags
& UDF_ICB_TAG_FLAGS_STICKY
)
4298 UDF_UNLOCK_NODE(udf_node
, 0);
4300 return mode
| ftype
;
4305 udf_setaccessmode(struct udf_node
*udf_node
, mode_t mode
)
4307 struct file_entry
*fe
= udf_node
->fe
;
4308 struct extfile_entry
*efe
= udf_node
->efe
;
4312 UDF_LOCK_NODE(udf_node
, 0);
4313 udf_perm
= unix_mode_to_udf_perm(mode
& ALLPERMS
);
4315 icbflags
= udf_rw16(fe
->icbtag
.flags
);
4317 icbflags
= udf_rw16(efe
->icbtag
.flags
);
4320 icbflags
&= ~UDF_ICB_TAG_FLAGS_SETUID
;
4321 icbflags
&= ~UDF_ICB_TAG_FLAGS_SETGID
;
4322 icbflags
&= ~UDF_ICB_TAG_FLAGS_STICKY
;
4324 icbflags
|= UDF_ICB_TAG_FLAGS_SETUID
;
4326 icbflags
|= UDF_ICB_TAG_FLAGS_SETGID
;
4328 icbflags
|= UDF_ICB_TAG_FLAGS_STICKY
;
4331 fe
->perm
= udf_rw32(udf_perm
);
4332 fe
->icbtag
.flags
= udf_rw16(icbflags
);
4334 efe
->perm
= udf_rw32(udf_perm
);
4335 efe
->icbtag
.flags
= udf_rw16(icbflags
);
4338 UDF_UNLOCK_NODE(udf_node
, 0);
4343 udf_getownership(struct udf_node
*udf_node
, uid_t
*uidp
, gid_t
*gidp
)
4345 struct udf_mount
*ump
= udf_node
->ump
;
4346 struct file_entry
*fe
= udf_node
->fe
;
4347 struct extfile_entry
*efe
= udf_node
->efe
;
4351 UDF_LOCK_NODE(udf_node
, 0);
4353 uid
= (uid_t
)udf_rw32(fe
->uid
);
4354 gid
= (gid_t
)udf_rw32(fe
->gid
);
4356 assert(udf_node
->efe
);
4357 uid
= (uid_t
)udf_rw32(efe
->uid
);
4358 gid
= (gid_t
)udf_rw32(efe
->gid
);
4361 /* do the uid/gid translation game */
4362 if (uid
== (uid_t
) -1)
4363 uid
= ump
->mount_args
.anon_uid
;
4364 if (gid
== (gid_t
) -1)
4365 gid
= ump
->mount_args
.anon_gid
;
4370 UDF_UNLOCK_NODE(udf_node
, 0);
4375 udf_setownership(struct udf_node
*udf_node
, uid_t uid
, gid_t gid
)
4377 struct udf_mount
*ump
= udf_node
->ump
;
4378 struct file_entry
*fe
= udf_node
->fe
;
4379 struct extfile_entry
*efe
= udf_node
->efe
;
4383 UDF_LOCK_NODE(udf_node
, 0);
4385 /* do the uid/gid translation game */
4386 nobody_uid
= ump
->mount_args
.nobody_uid
;
4387 nobody_gid
= ump
->mount_args
.nobody_gid
;
4388 if (uid
== nobody_uid
)
4390 if (gid
== nobody_gid
)
4394 fe
->uid
= udf_rw32((uint32_t) uid
);
4395 fe
->gid
= udf_rw32((uint32_t) gid
);
4397 efe
->uid
= udf_rw32((uint32_t) uid
);
4398 efe
->gid
= udf_rw32((uint32_t) gid
);
4401 UDF_UNLOCK_NODE(udf_node
, 0);
4405 /* --------------------------------------------------------------------- */
4409 dirhash_fill(struct udf_node
*dir_node
)
4411 struct vnode
*dvp
= dir_node
->vnode
;
4412 struct dirhash
*dirh
;
4413 struct file_entry
*fe
= dir_node
->fe
;
4414 struct extfile_entry
*efe
= dir_node
->efe
;
4415 struct fileid_desc
*fid
;
4416 struct dirent
*dirent
;
4417 uint64_t file_size
, pre_diroffset
, diroffset
;
4421 /* make sure we have a dirhash to work on */
4422 dirh
= dir_node
->dir_hash
;
4424 KASSERT(dirh
->refcnt
> 0);
4426 if (dirh
->flags
& DIRH_BROKEN
)
4428 if (dirh
->flags
& DIRH_COMPLETE
)
4431 /* make sure we have a clean dirhash to add to */
4432 dirhash_purge_entries(dirh
);
4434 /* get directory filesize */
4436 file_size
= udf_rw64(fe
->inf_len
);
4439 file_size
= udf_rw64(efe
->inf_len
);
4442 /* allocate temporary space for fid */
4443 lb_size
= udf_rw32(dir_node
->ump
->logical_vol
->lb_size
);
4444 fid
= malloc(lb_size
, M_UDFTEMP
, M_WAITOK
);
4446 /* allocate temporary space for dirent */
4447 dirent
= malloc(sizeof(struct dirent
), M_UDFTEMP
, M_WAITOK
);
4451 while (diroffset
< file_size
) {
4452 /* transfer a new fid/dirent */
4453 pre_diroffset
= diroffset
;
4454 error
= udf_read_fid_stream(dvp
, &diroffset
, fid
, dirent
);
4456 /* TODO what to do? continue but not add? */
4457 dirh
->flags
|= DIRH_BROKEN
;
4458 dirhash_purge_entries(dirh
);
4462 if ((fid
->file_char
& UDF_FILE_CHAR_DEL
)) {
4463 /* register deleted extent for reuse */
4464 dirhash_enter_freed(dirh
, pre_diroffset
,
4467 /* append to the dirhash */
4468 dirhash_enter(dirh
, dirent
, pre_diroffset
,
4469 udf_fidsize(fid
), 0);
4472 dirh
->flags
|= DIRH_COMPLETE
;
4474 free(fid
, M_UDFTEMP
);
4475 free(dirent
, M_UDFTEMP
);
4481 /* --------------------------------------------------------------------- */
4484 * Directory read and manipulation functions.
4489 udf_lookup_name_in_dir(struct vnode
*vp
, const char *name
, int namelen
,
4490 struct long_ad
*icb_loc
, int *found
)
4492 struct udf_node
*dir_node
= VTOI(vp
);
4493 struct dirhash
*dirh
;
4494 struct dirhash_entry
*dirh_ep
;
4495 struct fileid_desc
*fid
;
4496 struct dirent
*dirent
;
4501 /* set default return */
4504 /* get our dirhash and make sure its read in */
4505 dirhash_get(&dir_node
->dir_hash
);
4506 error
= dirhash_fill(dir_node
);
4508 dirhash_put(dir_node
->dir_hash
);
4511 dirh
= dir_node
->dir_hash
;
4513 /* allocate temporary space for fid */
4514 lb_size
= udf_rw32(dir_node
->ump
->logical_vol
->lb_size
);
4515 fid
= malloc(lb_size
, M_UDFTEMP
, M_WAITOK
);
4516 dirent
= malloc(sizeof(struct dirent
), M_UDFTEMP
, M_WAITOK
);
4518 DPRINTF(DIRHASH
, ("dirhash_lookup looking for `%*.*s`\n",
4519 namelen
, namelen
, name
));
4521 /* search our dirhash hits */
4522 memset(icb_loc
, 0, sizeof(*icb_loc
));
4525 hit
= dirhash_lookup(dirh
, name
, namelen
, &dirh_ep
);
4526 /* if no hit, abort the search */
4530 /* check this hit */
4531 diroffset
= dirh_ep
->offset
;
4533 /* transfer a new fid/dirent */
4534 error
= udf_read_fid_stream(vp
, &diroffset
, fid
, dirent
);
4538 DPRINTF(DIRHASH
, ("dirhash_lookup\tchecking `%*.*s`\n",
4539 dirent
->d_namlen
, dirent
->d_namlen
, dirent
->d_name
));
4541 /* see if its our entry */
4542 KASSERT(dirent
->d_namlen
== namelen
);
4543 if (strncmp(dirent
->d_name
, name
, namelen
) == 0) {
4545 *icb_loc
= fid
->icb
;
4549 free(fid
, M_UDFTEMP
);
4550 free(dirent
, M_UDFTEMP
);
4552 dirhash_put(dir_node
->dir_hash
);
4557 /* --------------------------------------------------------------------- */
4560 udf_create_new_fe(struct udf_mount
*ump
, struct file_entry
*fe
, int file_type
,
4561 struct long_ad
*node_icb
, struct long_ad
*parent_icb
,
4562 uint64_t parent_unique_id
)
4564 struct timespec now
;
4565 struct icb_tag
*icb
;
4566 struct filetimes_extattr_entry
*ft_extattr
;
4568 uint32_t fidsize
, lb_num
;
4570 int crclen
, attrlen
;
4572 lb_num
= udf_rw32(node_icb
->loc
.lb_num
);
4573 udf_inittag(ump
, &fe
->tag
, TAGID_FENTRY
, lb_num
);
4577 * Always use strategy type 4 unless on WORM wich we don't support
4578 * (yet). Fill in defaults and set for internal allocation of data.
4580 icb
->strat_type
= udf_rw16(4);
4581 icb
->max_num_entries
= udf_rw16(1);
4582 icb
->file_type
= file_type
; /* 8 bit */
4583 icb
->flags
= udf_rw16(UDF_ICB_INTERN_ALLOC
);
4585 fe
->perm
= udf_rw32(0x7fff); /* all is allowed */
4586 fe
->link_cnt
= udf_rw16(0); /* explicit setting */
4588 fe
->ckpoint
= udf_rw32(1); /* user supplied file version */
4590 vfs_timestamp(&now
);
4591 udf_timespec_to_timestamp(&now
, &fe
->atime
);
4592 udf_timespec_to_timestamp(&now
, &fe
->attrtime
);
4593 udf_timespec_to_timestamp(&now
, &fe
->mtime
);
4595 udf_set_regid(&fe
->imp_id
, IMPL_NAME
);
4596 udf_add_impl_regid(ump
, &fe
->imp_id
);
4598 unique_id
= udf_advance_uniqueid(ump
);
4599 fe
->unique_id
= udf_rw64(unique_id
);
4600 fe
->l_ea
= udf_rw32(0);
4602 /* create extended attribute to record our creation time */
4603 attrlen
= UDF_FILETIMES_ATTR_SIZE(1);
4604 ft_extattr
= malloc(attrlen
, M_UDFTEMP
, M_WAITOK
);
4605 memset(ft_extattr
, 0, attrlen
);
4606 ft_extattr
->hdr
.type
= udf_rw32(UDF_FILETIMES_ATTR_NO
);
4607 ft_extattr
->hdr
.subtype
= 1; /* [4/48.10.5] */
4608 ft_extattr
->hdr
.a_l
= udf_rw32(UDF_FILETIMES_ATTR_SIZE(1));
4609 ft_extattr
->d_l
= udf_rw32(UDF_TIMESTAMP_SIZE
); /* one item */
4610 ft_extattr
->existence
= UDF_FILETIMES_FILE_CREATION
;
4611 udf_timespec_to_timestamp(&now
, &ft_extattr
->times
[0]);
4613 udf_extattr_insert_internal(ump
, (union dscrptr
*) fe
,
4614 (struct extattr_entry
*) ft_extattr
);
4615 free(ft_extattr
, M_UDFTEMP
);
4617 /* if its a directory, create '..' */
4618 bpos
= (uint8_t *) fe
->data
+ udf_rw32(fe
->l_ea
);
4620 if (file_type
== UDF_ICB_FILETYPE_DIRECTORY
) {
4621 fidsize
= udf_create_parentfid(ump
,
4622 (struct fileid_desc
*) bpos
, parent_icb
,
4626 /* record fidlength information */
4627 fe
->inf_len
= udf_rw64(fidsize
);
4628 fe
->l_ad
= udf_rw32(fidsize
);
4629 fe
->logblks_rec
= udf_rw64(0); /* intern */
4631 crclen
= sizeof(struct file_entry
) - 1 - UDF_DESC_TAG_LENGTH
;
4632 crclen
+= udf_rw32(fe
->l_ea
) + fidsize
;
4633 fe
->tag
.desc_crc_len
= udf_rw16(crclen
);
4635 (void) udf_validate_tag_and_crc_sums((union dscrptr
*) fe
);
4640 /* --------------------------------------------------------------------- */
4643 udf_create_new_efe(struct udf_mount
*ump
, struct extfile_entry
*efe
,
4644 int file_type
, struct long_ad
*node_icb
, struct long_ad
*parent_icb
,
4645 uint64_t parent_unique_id
)
4647 struct timespec now
;
4648 struct icb_tag
*icb
;
4650 uint32_t fidsize
, lb_num
;
4654 lb_num
= udf_rw32(node_icb
->loc
.lb_num
);
4655 udf_inittag(ump
, &efe
->tag
, TAGID_EXTFENTRY
, lb_num
);
4659 * Always use strategy type 4 unless on WORM wich we don't support
4660 * (yet). Fill in defaults and set for internal allocation of data.
4662 icb
->strat_type
= udf_rw16(4);
4663 icb
->max_num_entries
= udf_rw16(1);
4664 icb
->file_type
= file_type
; /* 8 bit */
4665 icb
->flags
= udf_rw16(UDF_ICB_INTERN_ALLOC
);
4667 efe
->perm
= udf_rw32(0x7fff); /* all is allowed */
4668 efe
->link_cnt
= udf_rw16(0); /* explicit setting */
4670 efe
->ckpoint
= udf_rw32(1); /* user supplied file version */
4672 vfs_timestamp(&now
);
4673 udf_timespec_to_timestamp(&now
, &efe
->ctime
);
4674 udf_timespec_to_timestamp(&now
, &efe
->atime
);
4675 udf_timespec_to_timestamp(&now
, &efe
->attrtime
);
4676 udf_timespec_to_timestamp(&now
, &efe
->mtime
);
4678 udf_set_regid(&efe
->imp_id
, IMPL_NAME
);
4679 udf_add_impl_regid(ump
, &efe
->imp_id
);
4681 unique_id
= udf_advance_uniqueid(ump
);
4682 efe
->unique_id
= udf_rw64(unique_id
);
4683 efe
->l_ea
= udf_rw32(0);
4685 /* if its a directory, create '..' */
4686 bpos
= (uint8_t *) efe
->data
+ udf_rw32(efe
->l_ea
);
4688 if (file_type
== UDF_ICB_FILETYPE_DIRECTORY
) {
4689 fidsize
= udf_create_parentfid(ump
,
4690 (struct fileid_desc
*) bpos
, parent_icb
,
4694 /* record fidlength information */
4695 efe
->obj_size
= udf_rw64(fidsize
);
4696 efe
->inf_len
= udf_rw64(fidsize
);
4697 efe
->l_ad
= udf_rw32(fidsize
);
4698 efe
->logblks_rec
= udf_rw64(0); /* intern */
4700 crclen
= sizeof(struct extfile_entry
) - 1 - UDF_DESC_TAG_LENGTH
;
4701 crclen
+= udf_rw32(efe
->l_ea
) + fidsize
;
4702 efe
->tag
.desc_crc_len
= udf_rw16(crclen
);
4704 (void) udf_validate_tag_and_crc_sums((union dscrptr
*) efe
);
4709 /* --------------------------------------------------------------------- */
4712 udf_dir_detach(struct udf_mount
*ump
, struct udf_node
*dir_node
,
4713 struct udf_node
*udf_node
, struct componentname
*cnp
)
4715 struct vnode
*dvp
= dir_node
->vnode
;
4716 struct dirhash
*dirh
;
4717 struct dirhash_entry
*dirh_ep
;
4718 struct file_entry
*fe
= dir_node
->fe
;
4719 struct extfile_entry
*efe
= dir_node
->efe
;
4720 struct fileid_desc
*fid
;
4721 struct dirent
*dirent
;
4722 uint64_t file_size
, diroffset
;
4723 uint32_t lb_size
, fidsize
;
4725 char const *name
= cnp
->cn_nameptr
;
4726 int namelen
= cnp
->cn_namelen
;
4729 /* get our dirhash and make sure its read in */
4730 dirhash_get(&dir_node
->dir_hash
);
4731 error
= dirhash_fill(dir_node
);
4733 dirhash_put(dir_node
->dir_hash
);
4736 dirh
= dir_node
->dir_hash
;
4738 /* get directory filesize */
4740 file_size
= udf_rw64(fe
->inf_len
);
4743 file_size
= udf_rw64(efe
->inf_len
);
4746 /* allocate temporary space for fid */
4747 lb_size
= udf_rw32(dir_node
->ump
->logical_vol
->lb_size
);
4748 fid
= malloc(lb_size
, M_UDFTEMP
, M_WAITOK
);
4749 dirent
= malloc(sizeof(struct dirent
), M_UDFTEMP
, M_WAITOK
);
4751 /* search our dirhash hits */
4755 hit
= dirhash_lookup(dirh
, name
, namelen
, &dirh_ep
);
4756 /* if no hit, abort the search */
4760 /* check this hit */
4761 diroffset
= dirh_ep
->offset
;
4763 /* transfer a new fid/dirent */
4764 error
= udf_read_fid_stream(dvp
, &diroffset
, fid
, dirent
);
4768 /* see if its our entry */
4769 KASSERT(dirent
->d_namlen
== namelen
);
4770 if (strncmp(dirent
->d_name
, name
, namelen
) == 0) {
4782 fid
->file_char
|= UDF_FILE_CHAR_DEL
;
4783 #ifdef UDF_COMPLETE_DELETE
4784 memset(&fid
->icb
, 0, sizeof(fid
->icb
));
4786 (void) udf_validate_tag_and_crc_sums((union dscrptr
*) fid
);
4788 /* get size of fid and compensate for the read_fid_stream advance */
4789 fidsize
= udf_fidsize(fid
);
4790 diroffset
-= fidsize
;
4793 error
= vn_rdwr(UIO_WRITE
, dir_node
->vnode
,
4794 fid
, fidsize
, diroffset
,
4795 UIO_SYSSPACE
, IO_ALTSEMANTICS
| IO_NODELOCKED
,
4796 FSCRED
, NULL
, NULL
);
4800 /* get reference count of attached node */
4802 refcnt
= udf_rw16(udf_node
->fe
->link_cnt
);
4804 KASSERT(udf_node
->efe
);
4805 refcnt
= udf_rw16(udf_node
->efe
->link_cnt
);
4807 #ifdef UDF_COMPLETE_DELETE
4808 /* substract reference counter in attached node */
4811 udf_node
->fe
->link_cnt
= udf_rw16(refcnt
);
4813 udf_node
->efe
->link_cnt
= udf_rw16(refcnt
);
4816 /* prevent writeout when refcnt == 0 */
4818 udf_node
->i_flags
|= IN_DELETED
;
4820 if (fid
->file_char
& UDF_FILE_CHAR_DIR
) {
4823 /* substract reference counter in directory node */
4824 /* note subtract 2 (?) for its was also backreferenced */
4826 drefcnt
= udf_rw16(dir_node
->fe
->link_cnt
);
4828 dir_node
->fe
->link_cnt
= udf_rw16(drefcnt
);
4830 KASSERT(dir_node
->efe
);
4831 drefcnt
= udf_rw16(dir_node
->efe
->link_cnt
);
4833 dir_node
->efe
->link_cnt
= udf_rw16(drefcnt
);
4837 udf_node
->i_flags
|= IN_MODIFIED
;
4838 dir_node
->i_flags
|= IN_MODIFIED
;
4840 /* if it is/was a hardlink adjust the file count */
4842 udf_adjust_filecount(udf_node
, -1);
4844 /* remove from the dirhash */
4845 dirhash_remove(dirh
, dirent
, diroffset
,
4849 free(fid
, M_UDFTEMP
);
4850 free(dirent
, M_UDFTEMP
);
4852 dirhash_put(dir_node
->dir_hash
);
4857 /* --------------------------------------------------------------------- */
4860 udf_dir_update_rootentry(struct udf_mount
*ump
, struct udf_node
*dir_node
,
4861 struct udf_node
*new_parent_node
)
4863 struct vnode
*dvp
= dir_node
->vnode
;
4864 struct dirhash
*dirh
;
4865 struct dirhash_entry
*dirh_ep
;
4866 struct file_entry
*fe
;
4867 struct extfile_entry
*efe
;
4868 struct fileid_desc
*fid
;
4869 struct dirent
*dirent
;
4870 uint64_t file_size
, diroffset
;
4871 uint64_t new_parent_unique_id
;
4872 uint32_t lb_size
, fidsize
;
4874 char const *name
= "..";
4878 /* get our dirhash and make sure its read in */
4879 dirhash_get(&dir_node
->dir_hash
);
4880 error
= dirhash_fill(dir_node
);
4882 dirhash_put(dir_node
->dir_hash
);
4885 dirh
= dir_node
->dir_hash
;
4887 /* get new parent's unique ID */
4888 fe
= new_parent_node
->fe
;
4889 efe
= new_parent_node
->efe
;
4891 new_parent_unique_id
= udf_rw64(fe
->unique_id
);
4894 new_parent_unique_id
= udf_rw64(efe
->unique_id
);
4897 /* get directory filesize */
4899 efe
= dir_node
->efe
;
4901 file_size
= udf_rw64(fe
->inf_len
);
4904 file_size
= udf_rw64(efe
->inf_len
);
4907 /* allocate temporary space for fid */
4908 lb_size
= udf_rw32(dir_node
->ump
->logical_vol
->lb_size
);
4909 fid
= malloc(lb_size
, M_UDFTEMP
, M_WAITOK
);
4910 dirent
= malloc(sizeof(struct dirent
), M_UDFTEMP
, M_WAITOK
);
4913 * NOTE the standard does not dictate the FID entry '..' should be
4914 * first, though in practice it will most likely be.
4917 /* search our dirhash hits */
4921 hit
= dirhash_lookup(dirh
, name
, namelen
, &dirh_ep
);
4922 /* if no hit, abort the search */
4926 /* check this hit */
4927 diroffset
= dirh_ep
->offset
;
4929 /* transfer a new fid/dirent */
4930 error
= udf_read_fid_stream(dvp
, &diroffset
, fid
, dirent
);
4934 /* see if its our entry */
4935 KASSERT(dirent
->d_namlen
== namelen
);
4936 if (strncmp(dirent
->d_name
, name
, namelen
) == 0) {
4947 /* update our ICB to the new parent, hit of lower 32 bits of uniqueid */
4948 fid
->icb
= new_parent_node
->write_loc
;
4949 fid
->icb
.longad_uniqueid
= udf_rw32(new_parent_unique_id
);
4951 (void) udf_validate_tag_and_crc_sums((union dscrptr
*) fid
);
4953 /* get size of fid and compensate for the read_fid_stream advance */
4954 fidsize
= udf_fidsize(fid
);
4955 diroffset
-= fidsize
;
4958 error
= vn_rdwr(UIO_WRITE
, dir_node
->vnode
,
4959 fid
, fidsize
, diroffset
,
4960 UIO_SYSSPACE
, IO_ALTSEMANTICS
| IO_NODELOCKED
,
4961 FSCRED
, NULL
, NULL
);
4963 /* nothing to be done in the dirhash */
4966 free(fid
, M_UDFTEMP
);
4967 free(dirent
, M_UDFTEMP
);
4969 dirhash_put(dir_node
->dir_hash
);
4974 /* --------------------------------------------------------------------- */
4977 * We are not allowed to split the fid tag itself over an logical block so
4978 * check the space remaining in the logical block.
4980 * We try to select the smallest candidate for recycling or when none is
4981 * found, append a new one at the end of the directory.
4985 udf_dir_attach(struct udf_mount
*ump
, struct udf_node
*dir_node
,
4986 struct udf_node
*udf_node
, struct vattr
*vap
, struct componentname
*cnp
)
4988 struct vnode
*dvp
= dir_node
->vnode
;
4989 struct dirhash
*dirh
;
4990 struct dirhash_entry
*dirh_ep
;
4991 struct fileid_desc
*fid
;
4992 struct icb_tag
*icbtag
;
4993 struct charspec osta_charspec
;
4994 struct dirent dirent
;
4995 uint64_t unique_id
, dir_size
;
4996 uint64_t fid_pos
, end_fid_pos
, chosen_fid_pos
;
4997 uint32_t chosen_size
, chosen_size_diff
;
4998 int lb_size
, lb_rest
, fidsize
, this_fidsize
, size_diff
;
4999 int file_char
, refcnt
, icbflags
, addr_type
, hit
, error
;
5001 /* get our dirhash and make sure its read in */
5002 dirhash_get(&dir_node
->dir_hash
);
5003 error
= dirhash_fill(dir_node
);
5005 dirhash_put(dir_node
->dir_hash
);
5008 dirh
= dir_node
->dir_hash
;
5011 lb_size
= udf_rw32(ump
->logical_vol
->lb_size
);
5012 udf_osta_charset(&osta_charspec
);
5015 dir_size
= udf_rw64(dir_node
->fe
->inf_len
);
5016 icbtag
= &dir_node
->fe
->icbtag
;
5018 dir_size
= udf_rw64(dir_node
->efe
->inf_len
);
5019 icbtag
= &dir_node
->efe
->icbtag
;
5022 icbflags
= udf_rw16(icbtag
->flags
);
5023 addr_type
= icbflags
& UDF_ICB_TAG_FLAGS_ALLOC_MASK
;
5026 unique_id
= udf_rw64(udf_node
->fe
->unique_id
);
5027 refcnt
= udf_rw16(udf_node
->fe
->link_cnt
);
5029 unique_id
= udf_rw64(udf_node
->efe
->unique_id
);
5030 refcnt
= udf_rw16(udf_node
->efe
->link_cnt
);
5034 unique_id
= udf_advance_uniqueid(ump
);
5035 udf_adjust_filecount(udf_node
, 1);
5038 /* determine file characteristics */
5039 file_char
= 0; /* visible non deleted file and not stream metadata */
5040 if (vap
->va_type
== VDIR
)
5041 file_char
= UDF_FILE_CHAR_DIR
;
5043 /* malloc scrap buffer */
5044 fid
= malloc(lb_size
, M_TEMP
, M_WAITOK
|M_ZERO
);
5046 /* calculate _minimum_ fid size */
5047 unix_to_udf_name((char *) fid
->data
, &fid
->l_fi
,
5048 cnp
->cn_nameptr
, cnp
->cn_namelen
, &osta_charspec
);
5049 fidsize
= UDF_FID_SIZE
+ fid
->l_fi
;
5050 fidsize
= (fidsize
+ 3) & ~3; /* multiple of 4 */
5052 /* find position that will fit the FID */
5053 chosen_fid_pos
= dir_size
;
5055 chosen_size_diff
= UINT_MAX
;
5058 dirent
.d_namlen
= 0;
5060 /* search our dirhash hits */
5064 hit
= dirhash_lookup_freed(dirh
, fidsize
, &dirh_ep
);
5065 /* if no hit, abort the search */
5069 /* check this hit for size */
5070 this_fidsize
= dirh_ep
->entry_size
;
5072 /* check this hit */
5073 fid_pos
= dirh_ep
->offset
;
5074 end_fid_pos
= fid_pos
+ this_fidsize
;
5075 size_diff
= this_fidsize
- fidsize
;
5076 lb_rest
= lb_size
- (end_fid_pos
% lb_size
);
5078 #ifndef UDF_COMPLETE_DELETE
5079 /* transfer a new fid/dirent */
5080 error
= udf_read_fid_stream(vp
, &fid_pos
, fid
, dirent
);
5084 /* only reuse entries that are wiped */
5085 /* check if the len + loc are marked zero */
5086 if (udf_rw32(fid
->icb
.len
) != 0)
5088 if (udf_rw32(fid
->icb
.loc
.lb_num
) != 0)
5090 if (udf_rw16(fid
->icb
.loc
.part_num
) != 0)
5092 #endif /* UDF_COMPLETE_DELETE */
5094 /* select if not splitting the tag and its smaller */
5095 if ((size_diff
>= 0) &&
5096 (size_diff
< chosen_size_diff
) &&
5097 (lb_rest
>= sizeof(struct desc_tag
)))
5099 /* UDF 2.3.4.2+3 specifies rules for iu size */
5100 if ((size_diff
== 0) || (size_diff
>= 32)) {
5101 chosen_fid_pos
= fid_pos
;
5102 chosen_size
= this_fidsize
;
5103 chosen_size_diff
= size_diff
;
5109 /* extend directory if no other candidate found */
5110 if (chosen_size
== 0) {
5111 chosen_fid_pos
= dir_size
;
5112 chosen_size
= fidsize
;
5113 chosen_size_diff
= 0;
5115 /* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */
5116 if (addr_type
== UDF_ICB_INTERN_ALLOC
) {
5117 /* pre-grow directory to see if we're to switch */
5118 udf_grow_node(dir_node
, dir_size
+ chosen_size
);
5120 icbflags
= udf_rw16(icbtag
->flags
);
5121 addr_type
= icbflags
& UDF_ICB_TAG_FLAGS_ALLOC_MASK
;
5124 /* make sure the next fid desc_tag won't be splitted */
5125 if (addr_type
!= UDF_ICB_INTERN_ALLOC
) {
5126 end_fid_pos
= chosen_fid_pos
+ chosen_size
;
5127 lb_rest
= lb_size
- (end_fid_pos
% lb_size
);
5129 /* pad with implementation use regid if needed */
5130 if (lb_rest
< sizeof(struct desc_tag
))
5134 chosen_size_diff
= chosen_size
- fidsize
;
5136 /* populate the FID */
5137 memset(fid
, 0, lb_size
);
5138 udf_inittag(ump
, &fid
->tag
, TAGID_FID
, 0);
5139 fid
->file_version_num
= udf_rw16(1); /* UDF 2.3.4.1 */
5140 fid
->file_char
= file_char
;
5141 fid
->icb
= udf_node
->loc
;
5142 fid
->icb
.longad_uniqueid
= udf_rw32((uint32_t) unique_id
);
5143 fid
->l_iu
= udf_rw16(0);
5145 if (chosen_size
> fidsize
) {
5146 /* insert implementation-use regid to space it correctly */
5147 fid
->l_iu
= udf_rw16(chosen_size_diff
);
5149 /* set implementation use */
5150 udf_set_regid((struct regid
*) fid
->data
, IMPL_NAME
);
5151 udf_add_impl_regid(ump
, (struct regid
*) fid
->data
);
5155 unix_to_udf_name((char *) fid
->data
+ udf_rw16(fid
->l_iu
),
5156 &fid
->l_fi
, cnp
->cn_nameptr
, cnp
->cn_namelen
, &osta_charspec
);
5158 fid
->tag
.desc_crc_len
= udf_rw16(chosen_size
- UDF_DESC_TAG_LENGTH
);
5159 (void) udf_validate_tag_and_crc_sums((union dscrptr
*) fid
);
5161 /* writeout FID/update parent directory */
5162 error
= vn_rdwr(UIO_WRITE
, dvp
,
5163 fid
, chosen_size
, chosen_fid_pos
,
5164 UIO_SYSSPACE
, IO_ALTSEMANTICS
| IO_NODELOCKED
,
5165 FSCRED
, NULL
, NULL
);
5170 /* add reference counter in attached node */
5172 refcnt
= udf_rw16(udf_node
->fe
->link_cnt
);
5173 udf_node
->fe
->link_cnt
= udf_rw16(refcnt
+1);
5175 KASSERT(udf_node
->efe
);
5176 refcnt
= udf_rw16(udf_node
->efe
->link_cnt
);
5177 udf_node
->efe
->link_cnt
= udf_rw16(refcnt
+1);
5180 /* mark not deleted if it was... just in case, but do warn */
5181 if (udf_node
->i_flags
& IN_DELETED
) {
5182 printf("udf: warning, marking a file undeleted\n");
5183 udf_node
->i_flags
&= ~IN_DELETED
;
5186 if (file_char
& UDF_FILE_CHAR_DIR
) {
5187 /* add reference counter in directory node for '..' */
5189 refcnt
= udf_rw16(dir_node
->fe
->link_cnt
);
5191 dir_node
->fe
->link_cnt
= udf_rw16(refcnt
);
5193 KASSERT(dir_node
->efe
);
5194 refcnt
= udf_rw16(dir_node
->efe
->link_cnt
);
5196 dir_node
->efe
->link_cnt
= udf_rw16(refcnt
);
5200 /* append to the dirhash */
5201 dirent
.d_namlen
= cnp
->cn_namelen
;
5202 memcpy(dirent
.d_name
, cnp
->cn_nameptr
, cnp
->cn_namelen
);
5203 dirhash_enter(dirh
, &dirent
, chosen_fid_pos
,
5204 udf_fidsize(fid
), 1);
5207 udf_node
->i_flags
|= IN_CHANGE
| IN_MODIFY
; /* | IN_CREATE? */
5208 /* VN_KNOTE(udf_node, ...) */
5209 udf_update(udf_node
->vnode
, NULL
, NULL
, NULL
, 0);
5214 dirhash_put(dir_node
->dir_hash
);
5219 /* --------------------------------------------------------------------- */
5222 * Each node can have an attached streamdir node though not recursively. These
5223 * are otherwise known as named substreams/named extended attributes that have
5224 * no size limitations.
5226 * `Normal' extended attributes are indicated with a number and are recorded
5227 * in either the fe/efe descriptor itself for small descriptors or recorded in
5228 * the attached extended attribute file. Since these spaces can get
5229 * fragmented, care ought to be taken.
5231 * Since the size of the space reserved for allocation descriptors is limited,
5232 * there is a mechanim provided for extending this space; this is done by a
5233 * special extent to allow schrinking of the allocations without breaking the
5234 * linkage to the allocation extent descriptor.
5238 udf_get_node(struct udf_mount
*ump
, struct long_ad
*node_icb_loc
,
5239 struct udf_node
**udf_noderes
)
5241 union dscrptr
*dscr
;
5242 struct udf_node
*udf_node
;
5244 struct long_ad icb_loc
, last_fe_icb_loc
;
5246 uint32_t lb_size
, sector
, dummy
;
5248 int udf_file_type
, dscr_type
, strat
, strat4096
, needs_indirect
;
5249 int slot
, eof
, error
;
5251 DPRINTF(NODE
, ("udf_get_node called\n"));
5252 *udf_noderes
= udf_node
= NULL
;
5254 /* lock to disallow simultanious creation of same udf_node */
5255 mutex_enter(&ump
->get_node_lock
);
5257 DPRINTF(NODE
, ("\tlookup in hash table\n"));
5258 /* lookup in hash table */
5260 assert(node_icb_loc
);
5261 udf_node
= udf_node_lookup(ump
, node_icb_loc
);
5263 DPRINTF(NODE
, ("\tgot it from the hash!\n"));
5264 /* vnode is returned locked */
5265 *udf_noderes
= udf_node
;
5266 mutex_exit(&ump
->get_node_lock
);
5270 /* garbage check: translate udf_node_icb_loc to sectornr */
5271 error
= udf_translate_vtop(ump
, node_icb_loc
, §or
, &dummy
);
5273 /* no use, this will fail anyway */
5274 mutex_exit(&ump
->get_node_lock
);
5278 /* build udf_node (do initialise!) */
5279 udf_node
= pool_get(&udf_node_pool
, PR_WAITOK
);
5280 memset(udf_node
, 0, sizeof(struct udf_node
));
5282 DPRINTF(NODE
, ("\tget new vnode\n"));
5283 /* give it a vnode */
5284 error
= getnewvnode(VT_UDF
, ump
->vfs_mountp
, udf_vnodeop_p
, &nvp
);
5286 pool_put(&udf_node_pool
, udf_node
);
5287 mutex_exit(&ump
->get_node_lock
);
5291 /* always return locked vnode */
5292 if ((error
= vn_lock(nvp
, LK_EXCLUSIVE
| LK_RETRY
))) {
5293 /* recycle vnode and unlock; simultanious will fail too */
5295 mutex_exit(&ump
->get_node_lock
);
5299 /* initialise crosslinks, note location of fe/efe for hashing */
5300 udf_node
->ump
= ump
;
5301 udf_node
->vnode
= nvp
;
5302 nvp
->v_data
= udf_node
;
5303 udf_node
->loc
= *node_icb_loc
;
5304 udf_node
->lockf
= 0;
5305 mutex_init(&udf_node
->node_mutex
, MUTEX_DEFAULT
, IPL_NONE
);
5306 cv_init(&udf_node
->node_lock
, "udf_nlk");
5307 genfs_node_init(nvp
, &udf_genfsops
); /* inititise genfs */
5308 udf_node
->outstanding_bufs
= 0;
5309 udf_node
->outstanding_nodedscr
= 0;
5310 udf_node
->uncommitted_lbs
= 0;
5312 /* check if we're fetching the root */
5313 if (ump
->fileset_desc
)
5314 if (memcmp(&udf_node
->loc
, &ump
->fileset_desc
->rootdir_icb
,
5315 sizeof(struct long_ad
)) == 0)
5316 nvp
->v_vflag
|= VV_ROOT
;
5318 /* insert into the hash lookup */
5319 udf_register_node(udf_node
);
5321 /* safe to unlock, the entry is in the hash table, vnode is locked */
5322 mutex_exit(&ump
->get_node_lock
);
5324 icb_loc
= *node_icb_loc
;
5327 udf_file_type
= UDF_ICB_FILETYPE_UNKNOWN
;
5330 lb_size
= udf_rw32(ump
->logical_vol
->lb_size
);
5332 DPRINTF(NODE
, ("\tstart reading descriptors\n"));
5334 /* try to read in fe/efe */
5335 error
= udf_read_logvol_dscr(ump
, &icb_loc
, &dscr
);
5337 /* blank sector marks end of sequence, check this */
5338 if ((dscr
== NULL
) && (!strat4096
))
5341 /* break if read error or blank sector */
5342 if (error
|| (dscr
== NULL
))
5345 /* process descriptor based on the descriptor type */
5346 dscr_type
= udf_rw16(dscr
->tag
.id
);
5347 DPRINTF(NODE
, ("\tread descriptor %d\n", dscr_type
));
5349 /* if dealing with an indirect entry, follow the link */
5350 if (dscr_type
== TAGID_INDIRECTENTRY
) {
5352 udf_free_logvol_dscr(ump
, &icb_loc
, dscr
);
5353 icb_loc
= dscr
->inde
.indirect_icb
;
5357 /* only file entries and extended file entries allowed here */
5358 if ((dscr_type
!= TAGID_FENTRY
) &&
5359 (dscr_type
!= TAGID_EXTFENTRY
)) {
5360 udf_free_logvol_dscr(ump
, &icb_loc
, dscr
);
5365 KASSERT(udf_tagsize(dscr
, lb_size
) == lb_size
);
5367 /* choose this one */
5368 last_fe_icb_loc
= icb_loc
;
5370 /* record and process/update (ext)fentry */
5372 if (dscr_type
== TAGID_FENTRY
) {
5374 udf_free_logvol_dscr(ump
, &last_fe_icb_loc
,
5376 udf_node
->fe
= &dscr
->fe
;
5377 strat
= udf_rw16(udf_node
->fe
->icbtag
.strat_type
);
5378 udf_file_type
= udf_node
->fe
->icbtag
.file_type
;
5379 file_size
= udf_rw64(udf_node
->fe
->inf_len
);
5380 file_data
= udf_node
->fe
->data
;
5383 udf_free_logvol_dscr(ump
, &last_fe_icb_loc
,
5385 udf_node
->efe
= &dscr
->efe
;
5386 strat
= udf_rw16(udf_node
->efe
->icbtag
.strat_type
);
5387 udf_file_type
= udf_node
->efe
->icbtag
.file_type
;
5388 file_size
= udf_rw64(udf_node
->efe
->inf_len
);
5389 file_data
= udf_node
->efe
->data
;
5392 /* check recording strategy (structure) */
5395 * Strategy 4096 is a daisy linked chain terminating with an
5396 * unrecorded sector or a TERM descriptor. The next
5397 * descriptor is to be found in the sector that follows the
5400 if (strat
== 4096) {
5404 icb_loc
.loc
.lb_num
= udf_rw32(icb_loc
.loc
.lb_num
) + 1;
5408 * Strategy 4 is the normal strategy and terminates, but if
5409 * we're in strategy 4096, we can't have strategy 4 mixed in
5421 /* first round of cleanup code */
5423 DPRINTF(NODE
, ("\tnode fe/efe failed!\n"));
5424 /* recycle udf_node */
5425 udf_dispose_node(udf_node
);
5427 vlockmgr(nvp
->v_vnlock
, LK_RELEASE
);
5431 return EINVAL
; /* error code ok? */
5433 DPRINTF(NODE
, ("\tnode fe/efe read in fine\n"));
5435 /* assert no references to dscr anymore beyong this point */
5436 assert((udf_node
->fe
) || (udf_node
->efe
));
5440 * Remember where to record an updated version of the descriptor. If
5441 * there is a sequence of indirect entries, icb_loc will have been
5442 * updated. Its the write disipline to allocate new space and to make
5443 * sure the chain is maintained.
5445 * `needs_indirect' flags if the next location is to be filled with
5446 * with an indirect entry.
5448 udf_node
->write_loc
= icb_loc
;
5449 udf_node
->needs_indirect
= needs_indirect
;
5452 * Go trough all allocations extents of this descriptor and when
5453 * encountering a redirect read in the allocation extension. These are
5456 UDF_LOCK_NODE(udf_node
, 0);
5457 udf_node
->num_extensions
= 0;
5462 udf_get_adslot(udf_node
, slot
, &icb_loc
, &eof
);
5463 DPRINTF(ADWLK
, ("slot %d, eof = %d, flags = %d, len = %d, "
5464 "lb_num = %d, part = %d\n", slot
, eof
,
5465 UDF_EXT_FLAGS(udf_rw32(icb_loc
.len
)),
5466 UDF_EXT_LEN(udf_rw32(icb_loc
.len
)),
5467 udf_rw32(icb_loc
.loc
.lb_num
),
5468 udf_rw16(icb_loc
.loc
.part_num
)));
5473 if (UDF_EXT_FLAGS(udf_rw32(icb_loc
.len
)) != UDF_EXT_REDIRECT
)
5476 DPRINTF(NODE
, ("\tgot redirect extent\n"));
5477 if (udf_node
->num_extensions
>= UDF_MAX_ALLOC_EXTENTS
) {
5478 DPRINTF(ALLOC
, ("udf_get_node: implementation limit, "
5479 "too many allocation extensions on "
5485 /* length can only be *one* lb : UDF 2.50/2.3.7.1 */
5486 if (UDF_EXT_LEN(udf_rw32(icb_loc
.len
)) != lb_size
) {
5487 DPRINTF(ALLOC
, ("udf_get_node: bad allocation "
5488 "extension size in udf_node\n"));
5493 DPRINTF(NODE
, ("read allocation extent at lb_num %d\n",
5494 UDF_EXT_LEN(udf_rw32(icb_loc
.loc
.lb_num
))));
5495 /* load in allocation extent */
5496 error
= udf_read_logvol_dscr(ump
, &icb_loc
, &dscr
);
5497 if (error
|| (dscr
== NULL
))
5500 /* process read-in descriptor */
5501 dscr_type
= udf_rw16(dscr
->tag
.id
);
5503 if (dscr_type
!= TAGID_ALLOCEXTENT
) {
5504 udf_free_logvol_dscr(ump
, &icb_loc
, dscr
);
5509 DPRINTF(NODE
, ("\trecording redirect extent\n"));
5510 udf_node
->ext
[udf_node
->num_extensions
] = &dscr
->aee
;
5511 udf_node
->ext_loc
[udf_node
->num_extensions
] = icb_loc
;
5513 udf_node
->num_extensions
++;
5516 UDF_UNLOCK_NODE(udf_node
, 0);
5518 /* second round of cleanup code */
5520 /* recycle udf_node */
5521 udf_dispose_node(udf_node
);
5523 vlockmgr(nvp
->v_vnlock
, LK_RELEASE
);
5527 return EINVAL
; /* error code ok? */
5530 DPRINTF(NODE
, ("\tnode read in fine\n"));
5533 * Translate UDF filetypes into vnode types.
5535 * Systemfiles like the meta main and mirror files are not treated as
5536 * normal files, so we type them as having no type. UDF dictates that
5537 * they are not allowed to be visible.
5540 switch (udf_file_type
) {
5541 case UDF_ICB_FILETYPE_DIRECTORY
:
5542 case UDF_ICB_FILETYPE_STREAMDIR
:
5545 case UDF_ICB_FILETYPE_BLOCKDEVICE
:
5548 case UDF_ICB_FILETYPE_CHARDEVICE
:
5551 case UDF_ICB_FILETYPE_SOCKET
:
5552 nvp
->v_type
= VSOCK
;
5554 case UDF_ICB_FILETYPE_FIFO
:
5555 nvp
->v_type
= VFIFO
;
5557 case UDF_ICB_FILETYPE_SYMLINK
:
5560 case UDF_ICB_FILETYPE_VAT
:
5561 case UDF_ICB_FILETYPE_META_MAIN
:
5562 case UDF_ICB_FILETYPE_META_MIRROR
:
5565 case UDF_ICB_FILETYPE_RANDOMACCESS
:
5566 case UDF_ICB_FILETYPE_REALTIME
:
5570 /* YIKES, something else */
5574 /* TODO specfs, fifofs etc etc. vnops setting */
5576 /* don't forget to set vnode's v_size */
5577 uvm_vnp_setsize(nvp
, file_size
);
5579 /* TODO ext attr and streamdir udf_nodes */
5581 *udf_noderes
= udf_node
;
5586 /* --------------------------------------------------------------------- */
5589 udf_writeout_node(struct udf_node
*udf_node
, int waitfor
)
5591 union dscrptr
*dscr
;
5592 struct long_ad
*loc
;
5595 DPRINTF(NODE
, ("udf_writeout_node called\n"));
5597 KASSERT(udf_node
->outstanding_bufs
== 0);
5598 KASSERT(udf_node
->outstanding_nodedscr
== 0);
5600 KASSERT(LIST_EMPTY(&udf_node
->vnode
->v_dirtyblkhd
));
5602 if (udf_node
->i_flags
& IN_DELETED
) {
5603 DPRINTF(NODE
, ("\tnode deleted; not writing out\n"));
5604 udf_cleanup_reservation(udf_node
);
5608 /* lock node; unlocked in callback */
5609 UDF_LOCK_NODE(udf_node
, 0);
5611 /* remove pending reservations, we're written out */
5612 udf_cleanup_reservation(udf_node
);
5614 /* at least one descriptor writeout */
5615 udf_node
->outstanding_nodedscr
= 1;
5617 /* we're going to write out the descriptor so clear the flags */
5618 udf_node
->i_flags
&= ~(IN_MODIFIED
| IN_ACCESSED
);
5620 /* if we were rebuild, write out the allocation extents */
5621 if (udf_node
->i_flags
& IN_NODE_REBUILD
) {
5622 /* mark outstanding node descriptors and issue them */
5623 udf_node
->outstanding_nodedscr
+= udf_node
->num_extensions
;
5624 for (extnr
= 0; extnr
< udf_node
->num_extensions
; extnr
++) {
5625 loc
= &udf_node
->ext_loc
[extnr
];
5626 dscr
= (union dscrptr
*) udf_node
->ext
[extnr
];
5627 error
= udf_write_logvol_dscr(udf_node
, dscr
, loc
, 0);
5631 /* mark allocation extents written out */
5632 udf_node
->i_flags
&= ~(IN_NODE_REBUILD
);
5636 KASSERT(udf_node
->efe
== NULL
);
5637 dscr
= (union dscrptr
*) udf_node
->fe
;
5639 KASSERT(udf_node
->efe
);
5640 KASSERT(udf_node
->fe
== NULL
);
5641 dscr
= (union dscrptr
*) udf_node
->efe
;
5645 loc
= &udf_node
->write_loc
;
5646 error
= udf_write_logvol_dscr(udf_node
, dscr
, loc
, waitfor
);
5651 /* --------------------------------------------------------------------- */
5654 udf_dispose_node(struct udf_node
*udf_node
)
5659 DPRINTF(NODE
, ("udf_dispose_node called on node %p\n", udf_node
));
5661 DPRINTF(NODE
, ("UDF: Dispose node on node NULL, ignoring\n"));
5665 vp
= udf_node
->vnode
;
5667 if (vp
->v_numoutput
)
5668 panic("disposing UDF node with pending I/O's, udf_node = %p, "
5669 "v_numoutput = %d", udf_node
, vp
->v_numoutput
);
5672 udf_cleanup_reservation(udf_node
);
5674 /* TODO extended attributes and streamdir */
5676 /* remove dirhash if present */
5677 dirhash_purge(&udf_node
->dir_hash
);
5679 /* remove from our hash lookup table */
5680 udf_deregister_node(udf_node
);
5682 /* destroy our lock */
5683 mutex_destroy(&udf_node
->node_mutex
);
5684 cv_destroy(&udf_node
->node_lock
);
5686 /* dissociate our udf_node from the vnode */
5687 genfs_node_destroy(udf_node
->vnode
);
5690 /* free associated memory and the node itself */
5691 for (extnr
= 0; extnr
< udf_node
->num_extensions
; extnr
++) {
5692 udf_free_logvol_dscr(udf_node
->ump
, &udf_node
->ext_loc
[extnr
],
5693 udf_node
->ext
[extnr
]);
5694 udf_node
->ext
[extnr
] = (void *) 0xdeadcccc;
5698 udf_free_logvol_dscr(udf_node
->ump
, &udf_node
->loc
,
5701 udf_free_logvol_dscr(udf_node
->ump
, &udf_node
->loc
,
5704 udf_node
->fe
= (void *) 0xdeadaaaa;
5705 udf_node
->efe
= (void *) 0xdeadbbbb;
5706 udf_node
->ump
= (void *) 0xdeadbeef;
5707 pool_put(&udf_node_pool
, udf_node
);
5715 * create a new node using the specified vnodeops, vap and cnp but with the
5716 * udf_file_type. This allows special files to be created. Use with care.
5720 udf_create_node_raw(struct vnode
*dvp
, struct vnode
**vpp
, int udf_file_type
,
5721 int (**vnodeops
)(void *), struct vattr
*vap
, struct componentname
*cnp
)
5723 union dscrptr
*dscr
;
5724 struct udf_node
*dir_node
= VTOI(dvp
);
5725 struct udf_node
*udf_node
;
5726 struct udf_mount
*ump
= dir_node
->ump
;
5728 struct long_ad node_icb_loc
;
5729 uint64_t parent_unique_id
;
5731 uint32_t lb_size
, lb_num
;
5734 gid_t gid
, parent_gid
;
5735 int fid_size
, error
;
5737 lb_size
= udf_rw32(ump
->logical_vol
->lb_size
);
5740 /* allocate vnode */
5741 error
= getnewvnode(VT_UDF
, ump
->vfs_mountp
, vnodeops
, &nvp
);
5746 error
= vn_lock(nvp
, LK_EXCLUSIVE
| LK_RETRY
);
5748 goto error_out_unget
;
5750 /* reserve space for one logical block */
5751 vpart_num
= ump
->node_part
;
5752 error
= udf_reserve_space(ump
, NULL
, UDF_C_NODE
,
5753 vpart_num
, 1, /* can_fail */ true);
5755 goto error_out_unlock
;
5758 error
= udf_allocate_space(ump
, NULL
, UDF_C_NODE
,
5759 vpart_num
, 1, &lmapping
);
5761 goto error_out_unreserve
;
5764 /* initialise pointer to location */
5765 memset(&node_icb_loc
, 0, sizeof(struct long_ad
));
5766 node_icb_loc
.len
= udf_rw32(lb_size
);
5767 node_icb_loc
.loc
.lb_num
= udf_rw32(lb_num
);
5768 node_icb_loc
.loc
.part_num
= udf_rw16(vpart_num
);
5770 /* build udf_node (do initialise!) */
5771 udf_node
= pool_get(&udf_node_pool
, PR_WAITOK
);
5772 memset(udf_node
, 0, sizeof(struct udf_node
));
5774 /* initialise crosslinks, note location of fe/efe for hashing */
5775 /* bugalert: synchronise with udf_get_node() */
5776 udf_node
->ump
= ump
;
5777 udf_node
->vnode
= nvp
;
5778 nvp
->v_data
= udf_node
;
5779 udf_node
->loc
= node_icb_loc
;
5780 udf_node
->write_loc
= node_icb_loc
;
5781 udf_node
->lockf
= 0;
5782 mutex_init(&udf_node
->node_mutex
, MUTEX_DEFAULT
, IPL_NONE
);
5783 cv_init(&udf_node
->node_lock
, "udf_nlk");
5784 udf_node
->outstanding_bufs
= 0;
5785 udf_node
->outstanding_nodedscr
= 0;
5786 udf_node
->uncommitted_lbs
= 0;
5788 /* initialise genfs */
5789 genfs_node_init(nvp
, &udf_genfsops
);
5791 /* insert into the hash lookup */
5792 udf_register_node(udf_node
);
5794 /* get parent's unique ID for refering '..' if its a directory */
5796 parent_unique_id
= udf_rw64(dir_node
->fe
->unique_id
);
5797 parent_gid
= (gid_t
) udf_rw32(dir_node
->fe
->gid
);
5799 parent_unique_id
= udf_rw64(dir_node
->efe
->unique_id
);
5800 parent_gid
= (gid_t
) udf_rw32(dir_node
->efe
->gid
);
5803 /* get descriptor */
5804 udf_create_logvol_dscr(ump
, udf_node
, &node_icb_loc
, &dscr
);
5806 /* choose a fe or an efe for it */
5807 if (udf_rw16(ump
->logical_vol
->tag
.descriptor_ver
) == 2) {
5808 udf_node
->fe
= &dscr
->fe
;
5809 fid_size
= udf_create_new_fe(ump
, udf_node
->fe
,
5810 udf_file_type
, &udf_node
->loc
,
5811 &dir_node
->loc
, parent_unique_id
);
5812 /* TODO add extended attribute for creation time */
5814 udf_node
->efe
= &dscr
->efe
;
5815 fid_size
= udf_create_new_efe(ump
, udf_node
->efe
,
5816 udf_file_type
, &udf_node
->loc
,
5817 &dir_node
->loc
, parent_unique_id
);
5819 KASSERT(dscr
->tag
.tag_loc
== udf_node
->loc
.loc
.lb_num
);
5821 /* update vnode's size and type */
5822 nvp
->v_type
= vap
->va_type
;
5823 uvm_vnp_setsize(nvp
, fid_size
);
5825 /* set access mode */
5826 udf_setaccessmode(udf_node
, vap
->va_mode
);
5829 uid
= kauth_cred_geteuid(cnp
->cn_cred
);
5831 udf_setownership(udf_node
, uid
, gid
);
5833 error
= udf_dir_attach(ump
, dir_node
, udf_node
, vap
, cnp
);
5835 /* free disc allocation for node */
5836 udf_free_allocated_space(ump
, lb_num
, vpart_num
, 1);
5838 /* recycle udf_node */
5839 udf_dispose_node(udf_node
);
5846 /* adjust file count */
5847 udf_adjust_filecount(udf_node
, 1);
5854 error_out_unreserve
:
5855 udf_do_unreserve_space(ump
, NULL
, vpart_num
, 1);
5858 vlockmgr(nvp
->v_vnlock
, LK_RELEASE
);
5869 udf_create_node(struct vnode
*dvp
, struct vnode
**vpp
, struct vattr
*vap
,
5870 struct componentname
*cnp
)
5872 int (**vnodeops
)(void *);
5875 DPRINTF(NODE
, ("udf_create_node called\n"));
5877 /* what type are we creating ? */
5878 vnodeops
= udf_vnodeop_p
;
5879 /* start with a default */
5880 udf_file_type
= UDF_ICB_FILETYPE_RANDOMACCESS
;
5884 switch (vap
->va_type
) {
5886 udf_file_type
= UDF_ICB_FILETYPE_RANDOMACCESS
;
5889 udf_file_type
= UDF_ICB_FILETYPE_DIRECTORY
;
5892 udf_file_type
= UDF_ICB_FILETYPE_SYMLINK
;
5895 udf_file_type
= UDF_ICB_FILETYPE_BLOCKDEVICE
;
5900 udf_file_type
= UDF_ICB_FILETYPE_CHARDEVICE
;
5905 udf_file_type
= UDF_ICB_FILETYPE_FIFO
;
5910 udf_file_type
= UDF_ICB_FILETYPE_SOCKET
;
5917 /* nothing; can we even create these? */
5921 return udf_create_node_raw(dvp
, vpp
, udf_file_type
, vnodeops
, vap
, cnp
);
5924 /* --------------------------------------------------------------------- */
5927 udf_free_descriptor_space(struct udf_node
*udf_node
, struct long_ad
*loc
, void *mem
)
5929 struct udf_mount
*ump
= udf_node
->ump
;
5930 uint32_t lb_size
, lb_num
, len
, num_lb
;
5933 /* is there really one? */
5937 /* got a descriptor here */
5938 len
= UDF_EXT_LEN(udf_rw32(loc
->len
));
5939 lb_num
= udf_rw32(loc
->loc
.lb_num
);
5940 vpart_num
= udf_rw16(loc
->loc
.part_num
);
5942 lb_size
= udf_rw32(ump
->logical_vol
->lb_size
);
5943 num_lb
= (len
+ lb_size
-1) / lb_size
;
5945 udf_free_allocated_space(ump
, lb_num
, vpart_num
, num_lb
);
5949 udf_delete_node(struct udf_node
*udf_node
)
5952 struct udf_mount
*ump
;
5953 struct long_ad
*loc
;
5954 int extnr
, lvint
, dummy
;
5956 ump
= udf_node
->ump
;
5958 /* paranoia check on integrity; should be open!; we could panic */
5959 lvint
= udf_rw32(udf_node
->ump
->logvol_integrity
->integrity_type
);
5960 if (lvint
== UDF_INTEGRITY_CLOSED
)
5961 printf("\tIntegrity was CLOSED!\n");
5963 /* whatever the node type, change its size to zero */
5964 (void) udf_resize_node(udf_node
, 0, &dummy
);
5966 /* force it to be `clean'; no use writing it out */
5967 udf_node
->i_flags
&= ~(IN_MODIFIED
| IN_ACCESSED
| IN_ACCESS
|
5968 IN_CHANGE
| IN_UPDATE
| IN_MODIFY
);
5970 /* adjust file count */
5971 udf_adjust_filecount(udf_node
, -1);
5974 * Free its allocated descriptors; memory will be released when
5975 * vop_reclaim() is called.
5977 loc
= &udf_node
->loc
;
5979 dscr
= udf_node
->fe
;
5980 udf_free_descriptor_space(udf_node
, loc
, dscr
);
5981 dscr
= udf_node
->efe
;
5982 udf_free_descriptor_space(udf_node
, loc
, dscr
);
5984 for (extnr
= 0; extnr
< UDF_MAX_ALLOC_EXTENTS
; extnr
++) {
5985 dscr
= udf_node
->ext
[extnr
];
5986 loc
= &udf_node
->ext_loc
[extnr
];
5987 udf_free_descriptor_space(udf_node
, loc
, dscr
);
5991 /* --------------------------------------------------------------------- */
5993 /* set new filesize; node but be LOCKED on entry and is locked on exit */
5995 udf_resize_node(struct udf_node
*udf_node
, uint64_t new_size
, int *extended
)
5997 struct file_entry
*fe
= udf_node
->fe
;
5998 struct extfile_entry
*efe
= udf_node
->efe
;
6003 file_size
= udf_rw64(fe
->inf_len
);
6005 assert(udf_node
->efe
);
6006 file_size
= udf_rw64(efe
->inf_len
);
6009 DPRINTF(ATTR
, ("\tchanging file length from %"PRIu64
" to %"PRIu64
"\n",
6010 file_size
, new_size
));
6012 /* if not changing, we're done */
6013 if (file_size
== new_size
)
6016 *extended
= (new_size
> file_size
);
6018 error
= udf_grow_node(udf_node
, new_size
);
6020 error
= udf_shrink_node(udf_node
, new_size
);
6027 /* --------------------------------------------------------------------- */
6030 udf_itimes(struct udf_node
*udf_node
, struct timespec
*acc
,
6031 struct timespec
*mod
, struct timespec
*birth
)
6033 struct timespec now
;
6034 struct file_entry
*fe
;
6035 struct extfile_entry
*efe
;
6036 struct filetimes_extattr_entry
*ft_extattr
;
6037 struct timestamp
*atime
, *mtime
, *attrtime
, *ctime
;
6038 struct timestamp fe_ctime
;
6039 struct timespec cur_birth
;
6040 uint32_t offset
, a_l
;
6044 /* protect against rogue values */
6049 efe
= udf_node
->efe
;
6051 if (!(udf_node
->i_flags
& (IN_ACCESS
|IN_CHANGE
|IN_UPDATE
|IN_MODIFY
)))
6054 /* get descriptor information */
6058 attrtime
= &fe
->attrtime
;
6059 filedata
= fe
->data
;
6061 /* initial save dummy setting */
6064 /* check our extended attribute if present */
6065 error
= udf_extattr_search_intern(udf_node
,
6066 UDF_FILETIMES_ATTR_NO
, "", &offset
, &a_l
);
6068 ft_extattr
= (struct filetimes_extattr_entry
*)
6069 (filedata
+ offset
);
6070 if (ft_extattr
->existence
& UDF_FILETIMES_FILE_CREATION
)
6071 ctime
= &ft_extattr
->times
[0];
6073 /* TODO create the extended attribute if not found ? */
6075 assert(udf_node
->efe
);
6076 atime
= &efe
->atime
;
6077 mtime
= &efe
->mtime
;
6078 attrtime
= &efe
->attrtime
;
6079 ctime
= &efe
->ctime
;
6082 vfs_timestamp(&now
);
6084 /* set access time */
6085 if (udf_node
->i_flags
& IN_ACCESS
) {
6088 udf_timespec_to_timestamp(acc
, atime
);
6091 /* set modification time */
6092 if (udf_node
->i_flags
& (IN_UPDATE
| IN_MODIFY
)) {
6095 udf_timespec_to_timestamp(mod
, mtime
);
6097 /* ensure birthtime is older than set modification! */
6098 udf_timestamp_to_timespec(udf_node
->ump
, ctime
, &cur_birth
);
6099 if ((cur_birth
.tv_sec
> mod
->tv_sec
) ||
6100 ((cur_birth
.tv_sec
== mod
->tv_sec
) &&
6101 (cur_birth
.tv_nsec
> mod
->tv_nsec
))) {
6102 udf_timespec_to_timestamp(mod
, ctime
);
6106 /* update birthtime if specified */
6107 /* XXX we asume here that given birthtime is older than mod */
6108 if (birth
&& (birth
->tv_sec
!= VNOVAL
)) {
6109 udf_timespec_to_timestamp(birth
, ctime
);
6112 /* set change time */
6113 if (udf_node
->i_flags
& (IN_CHANGE
| IN_MODIFY
))
6114 udf_timespec_to_timestamp(&now
, attrtime
);
6116 /* notify updates to the node itself */
6117 if (udf_node
->i_flags
& (IN_ACCESS
| IN_MODIFY
))
6118 udf_node
->i_flags
|= IN_ACCESSED
;
6119 if (udf_node
->i_flags
& (IN_UPDATE
| IN_CHANGE
))
6120 udf_node
->i_flags
|= IN_MODIFIED
;
6122 /* clear modification flags */
6123 udf_node
->i_flags
&= ~(IN_ACCESS
| IN_CHANGE
| IN_UPDATE
| IN_MODIFY
);
6126 /* --------------------------------------------------------------------- */
6129 udf_update(struct vnode
*vp
, struct timespec
*acc
,
6130 struct timespec
*mod
, struct timespec
*birth
, int updflags
)
6132 union dscrptr
*dscrptr
;
6133 struct udf_node
*udf_node
= VTOI(vp
);
6134 struct udf_mount
*ump
= udf_node
->ump
;
6135 struct regid
*impl_id
;
6136 int mnt_async
= (vp
->v_mount
->mnt_flag
& MNT_ASYNC
);
6141 DPRINTF(CALL
, ("udf_update(node, %p, %p, %p, %d)\n", acc
, mod
, birth
,
6143 snprintb(bits
, sizeof(bits
), IN_FLAGBITS
, udf_node
->i_flags
);
6144 DPRINTF(CALL
, ("\tnode flags %s\n", bits
));
6145 DPRINTF(CALL
, ("\t\tmnt_async = %d\n", mnt_async
));
6149 udf_itimes(udf_node
, acc
, mod
, birth
);
6151 /* set our implementation id */
6153 dscrptr
= (union dscrptr
*) udf_node
->fe
;
6154 impl_id
= &udf_node
->fe
->imp_id
;
6156 dscrptr
= (union dscrptr
*) udf_node
->efe
;
6157 impl_id
= &udf_node
->efe
->imp_id
;
6161 udf_set_regid(impl_id
, IMPL_NAME
);
6162 udf_add_impl_regid(ump
, impl_id
);
6164 /* update our crc! on RMW we are not allowed to change a thing */
6165 udf_validate_tag_and_crc_sums(dscrptr
);
6167 /* if called when mounted readonly, never write back */
6168 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
)
6171 /* check if the node is dirty 'enough'*/
6172 if (updflags
& UPDATE_CLOSE
) {
6173 flags
= udf_node
->i_flags
& (IN_MODIFIED
| IN_ACCESSED
);
6175 flags
= udf_node
->i_flags
& IN_MODIFIED
;
6180 /* determine if we need to write sync or async */
6182 if ((flags
& IN_MODIFIED
) && (mnt_async
== 0)) {
6184 waitfor
= updflags
& UPDATE_WAIT
;
6185 if (updflags
& UPDATE_DIROP
)
6186 waitfor
|= UPDATE_WAIT
;
6189 return VOP_FSYNC(vp
, FSCRED
, FSYNC_WAIT
, 0,0);
6195 /* --------------------------------------------------------------------- */
6199 * Read one fid and process it into a dirent and advance to the next (*fid)
6200 * has to be allocated a logical block in size, (*dirent) struct dirent length
6204 udf_read_fid_stream(struct vnode
*vp
, uint64_t *offset
,
6205 struct fileid_desc
*fid
, struct dirent
*dirent
)
6207 struct udf_node
*dir_node
= VTOI(vp
);
6208 struct udf_mount
*ump
= dir_node
->ump
;
6209 struct file_entry
*fe
= dir_node
->fe
;
6210 struct extfile_entry
*efe
= dir_node
->efe
;
6211 uint32_t fid_size
, lb_size
;
6220 assert(*offset
!= 1);
6222 DPRINTF(FIDS
, ("read_fid_stream called at offset %"PRIu64
"\n", *offset
));
6223 /* check if we're past the end of the directory */
6225 file_size
= udf_rw64(fe
->inf_len
);
6227 assert(dir_node
->efe
);
6228 file_size
= udf_rw64(efe
->inf_len
);
6230 if (*offset
>= file_size
)
6233 /* get maximum length of FID descriptor */
6234 lb_size
= udf_rw32(ump
->logical_vol
->lb_size
);
6236 /* initialise return values */
6238 memset(dirent
, 0, sizeof(struct dirent
));
6239 memset(fid
, 0, lb_size
);
6241 enough
= (file_size
- (*offset
) >= UDF_FID_SIZE
);
6247 error
= vn_rdwr(UIO_READ
, vp
,
6248 fid
, MIN(file_size
- (*offset
), lb_size
), *offset
,
6249 UIO_SYSSPACE
, IO_ALTSEMANTICS
| IO_NODELOCKED
, FSCRED
,
6254 DPRINTF(FIDS
, ("\tfid piece read in fine\n"));
6256 * Check if we got a whole descriptor.
6257 * TODO Try to `resync' directory stream when something is very wrong.
6260 /* check if our FID header is OK */
6261 error
= udf_check_tag(fid
);
6265 DPRINTF(FIDS
, ("\ttag check ok\n"));
6267 if (udf_rw16(fid
->tag
.id
) != TAGID_FID
) {
6271 DPRINTF(FIDS
, ("\ttag checked ok: got TAGID_FID\n"));
6273 /* check for length */
6274 fid_size
= udf_fidsize(fid
);
6275 enough
= (file_size
- (*offset
) >= fid_size
);
6280 DPRINTF(FIDS
, ("\tthe complete fid is read in\n"));
6282 /* check FID contents */
6283 error
= udf_check_tag_payload((union dscrptr
*) fid
, lb_size
);
6286 /* note that is sometimes a bit quick to report */
6287 printf("UDF: BROKEN DIRECTORY ENTRY\n");
6289 /* TODO: use udf_resync_fid_stream */
6292 DPRINTF(FIDS
, ("\tpayload checked ok\n"));
6294 /* we got a whole and valid descriptor! */
6295 DPRINTF(FIDS
, ("\tinterpret FID\n"));
6297 /* create resulting dirent structure */
6298 fid_name
= (char *) fid
->data
+ udf_rw16(fid
->l_iu
);
6299 udf_to_unix_name(dirent
->d_name
, MAXNAMLEN
,
6300 fid_name
, fid
->l_fi
, &ump
->logical_vol
->desc_charset
);
6302 /* '..' has no name, so provide one */
6303 if (fid
->file_char
& UDF_FILE_CHAR_PAR
)
6304 strcpy(dirent
->d_name
, "..");
6306 dirent
->d_fileno
= udf_get_node_id(&fid
->icb
); /* inode hash XXX */
6307 dirent
->d_namlen
= strlen(dirent
->d_name
);
6308 dirent
->d_reclen
= _DIRENT_SIZE(dirent
);
6311 * Note that its not worth trying to go for the filetypes now... its
6314 dirent
->d_type
= DT_UNKNOWN
;
6316 /* initial guess for filetype we can make */
6317 if (fid
->file_char
& UDF_FILE_CHAR_DIR
)
6318 dirent
->d_type
= DT_DIR
;
6321 *offset
+= fid_size
;
6327 /* --------------------------------------------------------------------- */
6330 udf_sync_pass(struct udf_mount
*ump
, kauth_cred_t cred
, int waitfor
,
6331 int pass
, int *ndirty
)
6333 struct udf_node
*udf_node
, *n_udf_node
;
6336 int on_type
, on_flags
, on_vnode
;
6339 KASSERT(mutex_owned(&mntvnode_lock
));
6341 DPRINTF(SYNC
, ("sync_pass %d\n", pass
));
6342 udf_node
= RBTOUDFNODE(RB_TREE_MIN(&ump
->udf_node_tree
));
6343 for (;udf_node
; udf_node
= n_udf_node
) {
6344 DPRINTF(SYNC
, ("."));
6346 udf_node
->i_flags
&= ~IN_SYNCED
;
6347 vp
= udf_node
->vnode
;
6349 mutex_enter(&vp
->v_interlock
);
6350 n_udf_node
= RBTOUDFNODE(rb_tree_iterate(
6351 &ump
->udf_node_tree
, &udf_node
->rbnode
,
6355 n_udf_node
->i_flags
|= IN_SYNCED
;
6357 /* system nodes are not synced this way */
6358 if (vp
->v_vflag
& VV_SYSTEM
) {
6359 mutex_exit(&vp
->v_interlock
);
6363 /* check if its dirty enough to even try */
6364 on_type
= (waitfor
== MNT_LAZY
|| vp
->v_type
== VNON
);
6365 on_flags
= ((udf_node
->i_flags
&
6366 (IN_ACCESSED
| IN_UPDATE
| IN_MODIFIED
)) == 0);
6367 on_vnode
= LIST_EMPTY(&vp
->v_dirtyblkhd
)
6368 && UVM_OBJ_IS_CLEAN(&vp
->v_uobj
);
6369 if (on_type
|| (on_flags
|| on_vnode
)) { /* XXX */
6370 /* not dirty (enough?) */
6371 mutex_exit(&vp
->v_interlock
);
6375 mutex_exit(&mntvnode_lock
);
6376 error
= vget(vp
, LK_EXCLUSIVE
| LK_NOWAIT
| LK_INTERLOCK
);
6378 mutex_enter(&mntvnode_lock
);
6379 if (error
== ENOENT
)
6387 VOP_FSYNC(vp
, cred
, 0 | FSYNC_DATAONLY
,0,0);
6390 vdirty
= vp
->v_numoutput
;
6391 if (vp
->v_tag
== VT_UDF
)
6392 vdirty
+= udf_node
->outstanding_bufs
+
6393 udf_node
->outstanding_nodedscr
;
6395 VOP_FSYNC(vp
, cred
, 0,0,0);
6399 vdirty
= vp
->v_numoutput
;
6400 if (vp
->v_tag
== VT_UDF
)
6401 vdirty
+= udf_node
->outstanding_bufs
+
6402 udf_node
->outstanding_nodedscr
;
6408 mutex_enter(&mntvnode_lock
);
6410 DPRINTF(SYNC
, ("END sync_pass %d\n", pass
));
6415 udf_do_sync(struct udf_mount
*ump
, kauth_cred_t cred
, int waitfor
)
6419 mutex_enter(&mntvnode_lock
);
6422 DPRINTF(CALL
, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
6423 DPRINTF(SYNC
, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
6424 udf_sync_pass(ump
, cred
, waitfor
, 1, &dummy
);
6426 DPRINTF(CALL
, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
6427 DPRINTF(SYNC
, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
6428 udf_sync_pass(ump
, cred
, waitfor
, 2, &dummy
);
6430 if (waitfor
== MNT_WAIT
) {
6431 ndirty
= ump
->devvp
->v_numoutput
;
6432 DPRINTF(SYNC
, ("counting pending blocks: on devvp %d\n",
6434 udf_sync_pass(ump
, cred
, waitfor
, 3, &ndirty
);
6435 DPRINTF(SYNC
, ("counted num dirty pending blocks %d\n",
6439 /* 1/4 second wait */
6440 cv_timedwait(&ump
->dirtynodes_cv
, &mntvnode_lock
,
6446 mutex_exit(&mntvnode_lock
);
6449 /* --------------------------------------------------------------------- */
6452 * Read and write file extent in/from the buffer.
6454 * The splitup of the extent into seperate request-buffers is to minimise
6455 * copying around as much as possible.
6457 * block based file reading and writing
6461 udf_read_internal(struct udf_node
*node
, uint8_t *blob
)
6463 struct udf_mount
*ump
;
6464 struct file_entry
*fe
= node
->fe
;
6465 struct extfile_entry
*efe
= node
->efe
;
6467 uint32_t sector_size
;
6469 int icbflags
, addr_type
;
6471 /* get extent and do some paranoia checks */
6473 sector_size
= ump
->discinfo
.sector_size
;
6476 inflen
= udf_rw64(fe
->inf_len
);
6477 pos
= &fe
->data
[0] + udf_rw32(fe
->l_ea
);
6478 icbflags
= udf_rw16(fe
->icbtag
.flags
);
6481 inflen
= udf_rw64(efe
->inf_len
);
6482 pos
= &efe
->data
[0] + udf_rw32(efe
->l_ea
);
6483 icbflags
= udf_rw16(efe
->icbtag
.flags
);
6485 addr_type
= icbflags
& UDF_ICB_TAG_FLAGS_ALLOC_MASK
;
6487 assert(addr_type
== UDF_ICB_INTERN_ALLOC
);
6488 assert(inflen
< sector_size
);
6491 memset(blob
, 0, sector_size
);
6492 memcpy(blob
, pos
, inflen
);
6499 udf_write_internal(struct udf_node
*node
, uint8_t *blob
)
6501 struct udf_mount
*ump
;
6502 struct file_entry
*fe
= node
->fe
;
6503 struct extfile_entry
*efe
= node
->efe
;
6505 uint32_t sector_size
;
6507 int icbflags
, addr_type
;
6509 /* get extent and do some paranoia checks */
6511 sector_size
= ump
->discinfo
.sector_size
;
6514 inflen
= udf_rw64(fe
->inf_len
);
6515 pos
= &fe
->data
[0] + udf_rw32(fe
->l_ea
);
6516 icbflags
= udf_rw16(fe
->icbtag
.flags
);
6519 inflen
= udf_rw64(efe
->inf_len
);
6520 pos
= &efe
->data
[0] + udf_rw32(efe
->l_ea
);
6521 icbflags
= udf_rw16(efe
->icbtag
.flags
);
6523 addr_type
= icbflags
& UDF_ICB_TAG_FLAGS_ALLOC_MASK
;
6525 assert(addr_type
== UDF_ICB_INTERN_ALLOC
);
6526 assert(inflen
< sector_size
);
6529 /* memset(pos, 0, inflen); */
6530 memcpy(pos
, blob
, inflen
);
6537 udf_read_filebuf(struct udf_node
*udf_node
, struct buf
*buf
)
6539 struct buf
*nestbuf
;
6540 struct udf_mount
*ump
= udf_node
->ump
;
6543 uint32_t sector_size
;
6544 uint32_t buf_offset
, sector
, rbuflen
, rblk
;
6545 uint32_t from
, lblkno
;
6548 int error
, run_length
, what
;
6550 sector_size
= udf_node
->ump
->discinfo
.sector_size
;
6552 from
= buf
->b_blkno
;
6553 sectors
= buf
->b_bcount
/ sector_size
;
6555 what
= udf_get_c_type(udf_node
);
6557 /* assure we have enough translation slots */
6558 KASSERT(buf
->b_bcount
/ sector_size
<= UDF_MAX_MAPPINGS
);
6559 KASSERT(MAXPHYS
/ sector_size
<= UDF_MAX_MAPPINGS
);
6561 if (sectors
> UDF_MAX_MAPPINGS
) {
6562 printf("udf_read_filebuf: implementation limit on bufsize\n");
6568 mapping
= malloc(sizeof(*mapping
) * UDF_MAX_MAPPINGS
, M_TEMP
, M_WAITOK
);
6571 DPRINTF(READ
, ("\ttranslate %d-%d\n", from
, sectors
));
6572 error
= udf_translate_file_extent(udf_node
, from
, sectors
, mapping
);
6574 buf
->b_error
= error
;
6578 DPRINTF(READ
, ("\ttranslate extent went OK\n"));
6580 /* pre-check if its an internal */
6581 if (*mapping
== UDF_TRANS_INTERN
) {
6582 error
= udf_read_internal(udf_node
, (uint8_t *) buf
->b_data
);
6584 buf
->b_error
= error
;
6588 DPRINTF(READ
, ("\tnot intern\n"));
6591 if (udf_verbose
& UDF_DEBUG_TRANSLATE
) {
6592 printf("Returned translation table:\n");
6593 for (sector
= 0; sector
< sectors
; sector
++) {
6594 printf("%d : %"PRIu64
"\n", sector
, mapping
[sector
]);
6599 /* request read-in of data from disc sheduler */
6600 buf
->b_resid
= buf
->b_bcount
;
6601 for (sector
= 0; sector
< sectors
; sector
++) {
6602 buf_offset
= sector
* sector_size
;
6603 buf_pos
= (uint8_t *) buf
->b_data
+ buf_offset
;
6604 DPRINTF(READ
, ("\tprocessing rel sector %d\n", sector
));
6606 /* check if its zero or unmapped to stop reading */
6607 switch (mapping
[sector
]) {
6608 case UDF_TRANS_UNMAPPED
:
6609 case UDF_TRANS_ZERO
:
6610 /* copy zero sector TODO runlength like below */
6611 memset(buf_pos
, 0, sector_size
);
6612 DPRINTF(READ
, ("\treturning zero sector\n"));
6613 nestiobuf_done(buf
, sector_size
, 0);
6616 DPRINTF(READ
, ("\tread sector "
6617 "%"PRIu64
"\n", mapping
[sector
]));
6619 lblkno
= from
+ sector
;
6620 run_start
= mapping
[sector
];
6622 while (sector
< sectors
-1) {
6623 if (mapping
[sector
+1] != mapping
[sector
]+1)
6630 * nest an iobuf and mark it for async reading. Since
6631 * we're using nested buffers, they can't be cached by
6634 rbuflen
= run_length
* sector_size
;
6635 rblk
= run_start
* (sector_size
/DEV_BSIZE
);
6637 nestbuf
= getiobuf(NULL
, true);
6638 nestiobuf_setup(buf
, nestbuf
, buf_offset
, rbuflen
);
6639 /* nestbuf is B_ASYNC */
6641 /* identify this nestbuf */
6642 nestbuf
->b_lblkno
= lblkno
;
6643 assert(nestbuf
->b_vp
== udf_node
->vnode
);
6645 /* CD shedules on raw blkno */
6646 nestbuf
->b_blkno
= rblk
;
6647 nestbuf
->b_proc
= NULL
;
6648 nestbuf
->b_rawblkno
= rblk
;
6649 nestbuf
->b_udf_c_type
= what
;
6651 udf_discstrat_queuebuf(ump
, nestbuf
);
6655 /* if we're synchronously reading, wait for the completion */
6656 if ((buf
->b_flags
& B_ASYNC
) == 0)
6659 DPRINTF(READ
, ("\tend of read_filebuf\n"));
6660 free(mapping
, M_TEMP
);
6666 udf_write_filebuf(struct udf_node
*udf_node
, struct buf
*buf
)
6668 struct buf
*nestbuf
;
6669 struct udf_mount
*ump
= udf_node
->ump
;
6673 uint32_t buf_offset
, lb_num
, rbuflen
, rblk
;
6674 uint32_t from
, lblkno
;
6676 int error
, run_length
, what
, s
;
6678 lb_size
= udf_rw32(udf_node
->ump
->logical_vol
->lb_size
);
6680 from
= buf
->b_blkno
;
6681 num_lb
= buf
->b_bcount
/ lb_size
;
6683 what
= udf_get_c_type(udf_node
);
6685 /* assure we have enough translation slots */
6686 KASSERT(buf
->b_bcount
/ lb_size
<= UDF_MAX_MAPPINGS
);
6687 KASSERT(MAXPHYS
/ lb_size
<= UDF_MAX_MAPPINGS
);
6689 if (num_lb
> UDF_MAX_MAPPINGS
) {
6690 printf("udf_write_filebuf: implementation limit on bufsize\n");
6696 mapping
= malloc(sizeof(*mapping
) * UDF_MAX_MAPPINGS
, M_TEMP
, M_WAITOK
);
6699 DPRINTF(WRITE
, ("\ttranslate %d-%d\n", from
, num_lb
));
6700 error
= udf_translate_file_extent(udf_node
, from
, num_lb
, mapping
);
6702 buf
->b_error
= error
;
6706 DPRINTF(WRITE
, ("\ttranslate extent went OK\n"));
6708 /* if its internally mapped, we can write it in the descriptor itself */
6709 if (*mapping
== UDF_TRANS_INTERN
) {
6710 /* TODO paranoia check if we ARE going to have enough space */
6711 error
= udf_write_internal(udf_node
, (uint8_t *) buf
->b_data
);
6713 buf
->b_error
= error
;
6717 DPRINTF(WRITE
, ("\tnot intern\n"));
6719 /* request write out of data to disc sheduler */
6720 buf
->b_resid
= buf
->b_bcount
;
6721 for (lb_num
= 0; lb_num
< num_lb
; lb_num
++) {
6722 buf_offset
= lb_num
* lb_size
;
6723 DPRINTF(WRITE
, ("\tprocessing rel lb_num %d\n", lb_num
));
6726 * Mappings are not that important here. Just before we write
6727 * the lb_num we late-allocate them when needed and update the
6728 * mapping in the udf_node.
6731 /* XXX why not ignore the mapping altogether ? */
6732 DPRINTF(WRITE
, ("\twrite lb_num "
6733 "%"PRIu64
, mapping
[lb_num
]));
6735 lblkno
= from
+ lb_num
;
6736 run_start
= mapping
[lb_num
];
6738 while (lb_num
< num_lb
-1) {
6739 if (mapping
[lb_num
+1] != mapping
[lb_num
]+1)
6740 if (mapping
[lb_num
+1] != mapping
[lb_num
])
6745 DPRINTF(WRITE
, ("+ %d\n", run_length
));
6747 /* nest an iobuf on the master buffer for the extent */
6748 rbuflen
= run_length
* lb_size
;
6749 rblk
= run_start
* (lb_size
/DEV_BSIZE
);
6751 nestbuf
= getiobuf(NULL
, true);
6752 nestiobuf_setup(buf
, nestbuf
, buf_offset
, rbuflen
);
6753 /* nestbuf is B_ASYNC */
6755 /* identify this nestbuf */
6756 nestbuf
->b_lblkno
= lblkno
;
6757 KASSERT(nestbuf
->b_vp
== udf_node
->vnode
);
6759 /* CD shedules on raw blkno */
6760 nestbuf
->b_blkno
= rblk
;
6761 nestbuf
->b_proc
= NULL
;
6762 nestbuf
->b_rawblkno
= rblk
;
6763 nestbuf
->b_udf_c_type
= what
;
6765 /* increment our outstanding bufs counter */
6767 udf_node
->outstanding_bufs
++;
6770 udf_discstrat_queuebuf(ump
, nestbuf
);
6773 /* if we're synchronously writing, wait for the completion */
6774 if ((buf
->b_flags
& B_ASYNC
) == 0)
6777 DPRINTF(WRITE
, ("\tend of write_filebuf\n"));
6778 free(mapping
, M_TEMP
);
6782 /* --------------------------------------------------------------------- */