/*
* Copyright (c) 2006, 2008 Reinoud Zandijk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
if ((udf_verbose & UDF_DEBUG_VOLUMES) == 0)
return;
printf("Device/media info :\n");
printf("\tMMC profile 0x%02x\n", di->mmc_profile);
printf("\tderived class %d\n", di->mmc_class);
printf("\tsector size %d\n", di->sector_size);
printf("\tdisc state %d\n", di->disc_state);
printf("\tlast ses state %d\n", di->last_session_state);
printf("\tbg format state %d\n", di->bg_format_state);
printf("\tfrst track %d\n", di->first_track);
printf("\tfst on last ses %d\n", di->first_track_last_session);
printf("\tlst on last ses %d\n", di->last_track_last_session);
printf("\tlink block penalty %d\n", di->link_block_penalty);
snprintb(bits, sizeof(bits), MMC_DFLAGS_FLAGBITS, di->disc_flags);
printf("\tdisc flags %s\n", bits);
printf("\tdisc id %x\n", di->disc_id);
printf("\tdisc barcode %"PRIx64"\n", di->disc_barcode);
/* not called often */
int
udf_update_discinfo(struct udf_mount *ump)
{
struct vnode *devvp = ump->devvp;
uint64_t psize;
unsigned secsize;
struct mmc_discinfo *di;
int error;
/* TODO support for resizable vnd */
ti->track_size = di->last_possible_lba;
ti->next_writable = di->last_possible_lba;
ti->last_recorded = ti->next_writable;
ti->free_blocks = 0;
return 0;
}
int
udf_setup_writeparams(struct udf_mount *ump)
{
struct mmc_writeparams mmc_writeparams;
int error;
if (ump->discinfo.mmc_class == MMC_CLASS_DISC)
return 0;
/*
* only CD burning normally needs setting up, but other disc types
* might need other settings to be made. The MMC framework will set up
* the necessary recording parameters according to the disc
* characteristics read in. Modifications can be made in the discinfo
* structure passed to change the nature of the disc.
*/
/*
* UDF dictates first track to determine track mode for the whole
* disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1]
* To prevent problems with a `reserved' track in front we start with
* the 2nd track and if that is not valid, go for the 1st.
*/
mmc_writeparams.tracknr = 2;
mmc_writeparams.data_mode = MMC_DATAMODE_DEFAULT; /* XA disc */
mmc_writeparams.track_mode = MMC_TRACKMODE_DEFAULT; /* data */
/* track/session searching for mounting */
int
udf_search_tracks(struct udf_mount *ump, struct udf_args *args,
int *first_tracknr, int *last_tracknr)
{
struct mmc_trackinfo trackinfo;
uint32_t tracknr, start_track, num_tracks;
int error;
/* if negative, sessionnr is relative to last session */
if (args->sessionnr < 0) {
args->sessionnr += ump->discinfo.num_sessions;
}
/* sanity */
if (args->sessionnr < 0)
args->sessionnr = 0;
if (args->sessionnr > ump->discinfo.num_sessions)
args->sessionnr = ump->discinfo.num_sessions;
/* search the tracks for this session, zero session nr indicates last */
if (args->sessionnr == 0)
args->sessionnr = ump->discinfo.num_sessions;
if (ump->discinfo.last_session_state == MMC_STATE_EMPTY)
args->sessionnr--;
/* sanity again */
if (args->sessionnr < 0)
args->sessionnr = 0;
/* search the first and last track of the specified session */
num_tracks = ump->discinfo.num_tracks;
start_track = ump->discinfo.first_track;
/* search for first track of this session */
for (tracknr = start_track; tracknr <= num_tracks; tracknr++) {
/* get track info */
trackinfo.tracknr = tracknr;
error = udf_update_trackinfo(ump, &trackinfo);
if (error)
return error;
if (trackinfo.sessionnr == args->sessionnr)
break;
}
*first_tracknr = tracknr;
/* search for last track of this session */
for (;tracknr <= num_tracks; tracknr++) {
/* get track info */
trackinfo.tracknr = tracknr;
error = udf_update_trackinfo(ump, &trackinfo);
if (error || (trackinfo.sessionnr != args->sessionnr)) {
tracknr--;
break;
}
}
if (tracknr > num_tracks)
tracknr--;
*last_tracknr = tracknr;
if (*last_tracknr < *first_tracknr) {
printf( "udf_search_tracks: sanity check on drive+disc failed, "
"drive returned garbage\n");
return EINVAL;
}
/*
* NOTE: this is the only routine in this file that directly peeks into the
* metadata file but since its at a larval state of the mount it can't hurt.
*
* XXX candidate for udf_allocation.c
* XXX clean me up!, change to new node reading code.
*/
/*
* in the CD/(HD)DVD/BD recordable device model a few tracks within
* the last session might be open but in the UDF device model at most
* three tracks can be open: a reserved track for delayed ISO VRS
* writing, a data track and a metadata track. We search here for the
* data track and the metadata track. Note that the reserved track is
* troublesome but can be detected by its small size of < 512 sectors.
*/
/* update discinfo since it might have changed */
error = udf_update_discinfo(ump);
if (error)
return error;
/* fetch info on first and possibly only track */
trackinfo.tracknr = start_track;
error = udf_update_trackinfo(ump, &trackinfo);
if (error)
return error;
/* copy results to our mount point */
ump->data_track = trackinfo;
ump->metadata_track = trackinfo;
/* if not sequential, we're done */
if (num_tracks == 1)
return 0;
for (tracknr = start_track;tracknr <= num_tracks; tracknr++) {
/* get track info */
trackinfo.tracknr = tracknr;
error = udf_update_trackinfo(ump, &trackinfo);
if (error)
return error;
/*
* If this track is marked damaged, ask for repair. This is an
* optional command, so ignore its error but report warning.
*/
if (trackinfo.flags & MMC_TRACKINFO_DAMAGED) {
memset(&mmc_op, 0, sizeof(mmc_op));
mmc_op.operation = MMC_OP_REPAIRTRACK;
mmc_op.mmc_profile = ump->discinfo.mmc_profile;
mmc_op.tracknr = tracknr;
error = VOP_IOCTL(devvp, MMCOP, &mmc_op, FKIOCTL, NOCRED);
if (error)
(void)printf("Drive can't explicitly repair "
"damaged track %d, but it might "
"autorepair\n", tracknr);
/* reget track info */
error = udf_update_trackinfo(ump, &trackinfo);
if (error)
return error;
}
if ((trackinfo.flags & MMC_TRACKINFO_NWA_VALID) == 0)
continue;
/*
* Check if the blob starts with a good UDF tag. Tags are protected by a
* checksum over the header except one byte at position 4 that is the checksum
* itself.
*/
/* check TAG header checksum */
pos = (uint8_t *) tag;
sum = 0;
for(cnt = 0; cnt < 16; cnt++) {
if (cnt != 4)
sum += *pos;
pos++;
}
if (sum != tag->cksum) {
/* bad tag header checksum; this is not a valid tag */
return EINVAL;
}
return 0;
}
/*
* check tag payload will check descriptor CRC as specified.
* If the descriptor is too long, it will return EIO otherwise EINVAL.
*/
/* check payload CRC if applicable */
if (crc_len == 0)
return 0;
if (crc_len > max_length)
return EIO;
crc = udf_cksum(((uint8_t *) tag) + UDF_DESC_TAG_LENGTH, crc_len);
if (crc != udf_rw16(tag->desc_crc)) {
/* bad payload CRC; this is a broken tag */
return EINVAL;
}
/* search the first and last track of the specified session */
error = udf_search_tracks(ump, args, &first_tracknr, &last_tracknr);
if (!error) {
first_track.tracknr = first_tracknr;
error = udf_update_trackinfo(ump, &first_track);
}
if (!error) {
last_track.tracknr = last_tracknr;
error = udf_update_trackinfo(ump, &last_track);
}
if ((!error) && (first_tracknr != last_tracknr)) {
second_track.tracknr = first_tracknr+1;
error = udf_update_trackinfo(ump, &second_track);
}
if (error) {
printf("UDF mount: reading disc geometry failed\n");
return 0;
}
track_start = first_track.track_start;
/* `end' is not as straitforward as start. */
track_end = last_track.track_start
+ last_track.track_size - last_track.free_blocks - 1;
if (ump->discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
/* end of track is not straitforward here */
if (last_track.flags & MMC_TRACKINFO_LRA_VALID)
track_end = last_track.last_recorded;
else if (last_track.flags & MMC_TRACKINFO_NWA_VALID)
track_end = last_track.next_writable
- ump->discinfo.link_block_penalty;
}
/* its no use reading a blank track */
first_anchor = 0;
if (first_track.flags & MMC_TRACKINFO_BLANK)
first_anchor = 1;
/* get our packet size */
ump->packet_size = first_track.packet_size;
if (first_track.flags & MMC_TRACKINFO_BLANK)
ump->packet_size = second_track.packet_size;
if (ump->packet_size <= 1) {
/* take max, but not bigger than 64 */
ump->packet_size = MAXPHYS / ump->discinfo.sector_size;
ump->packet_size = MIN(ump->packet_size, 64);
}
KASSERT(ump->packet_size >= 1);
/* read anchors start+256, start+512, end-256, end */
positions[0] = track_start+256;
positions[1] = track_end-256;
positions[2] = track_end;
positions[3] = track_start+512; /* [UDF 2.60/6.11.2] */
/* XXX shouldn't +512 be preferred over +256 for compat with Roxio CD */
ok = 0;
anchorsp = ump->anchors;
for (anch = first_anchor; anch < 4; anch++) {
DPRINTF(VOLUMES, ("Read anchor %d at sector %d\n", anch,
positions[anch]));
error = udf_read_anchor(ump, positions[anch], anchorsp);
if (!error) {
anchorsp++;
ok++;
}
}
/* VATs are only recorded on sequential media, but initialise */
ump->first_possible_vat_location = track_start + 2;
ump->last_possible_vat_location = track_end;
/* we dont try to be smart; we just record the parts */
#define UDF_UPDATE_DSCR(name, dscr) \
if (name) \
free(name, M_UDFVOLD); \
name = dscr;
static int
udf_process_vds_descriptor(struct udf_mount *ump, union dscrptr *dscr)
{
uint16_t phys_part, raw_phys_part;
DPRINTF(VOLUMES, ("\tprocessing VDS descr %d\n",
udf_rw16(dscr->tag.id)));
switch (udf_rw16(dscr->tag.id)) {
case TAGID_PRI_VOL : /* primary partition */
UDF_UPDATE_DSCR(ump->primary_vol, &dscr->pvd);
break;
case TAGID_LOGVOL : /* logical volume */
UDF_UPDATE_DSCR(ump->logical_vol, &dscr->lvd);
break;
case TAGID_UNALLOC_SPACE : /* unallocated space */
UDF_UPDATE_DSCR(ump->unallocated, &dscr->usd);
break;
case TAGID_IMP_VOL : /* implementation */
/* XXX do we care about multiple impl. descr ? */
UDF_UPDATE_DSCR(ump->implementation, &dscr->ivd);
break;
case TAGID_PARTITION : /* physical partition */
/* not much use if its not allocated */
if ((udf_rw16(dscr->pd.flags) & UDF_PART_FLAG_ALLOCATED) == 0) {
free(dscr, M_UDFVOLD);
break;
}
/*
* BUGALERT: some rogue implementations use random physical
* partition numbers to break other implementations so lookup
* the number.
*/
raw_phys_part = udf_rw16(dscr->pd.part_num);
phys_part = udf_find_raw_phys(ump, raw_phys_part);
if (phys_part == UDF_PARTITIONS) {
free(dscr, M_UDFVOLD);
return EINVAL;
}
UDF_UPDATE_DSCR(ump->partitions[phys_part], &dscr->pd);
break;
case TAGID_VOL : /* volume space extender; rare */
DPRINTF(VOLUMES, ("VDS extender ignored\n"));
free(dscr, M_UDFVOLD);
break;
default :
DPRINTF(VOLUMES, ("Unhandled VDS type %d\n",
udf_rw16(dscr->tag.id)));
free(dscr, M_UDFVOLD);
}
/*
* read in VDS space provided by the anchors; if one descriptor read
* fails, try the mirror sector.
*
* check if 2nd anchor is different from 1st; if so, go for 2nd. This
* avoids the `compatibility features' of DirectCD that may confuse
* stuff completely.
*/
if (anchor2) {
size = sizeof(struct extent_ad);
if (memcmp(&anchor->main_vds_ex, &anchor2->main_vds_ex, size))
anchor = anchor2;
/* reserve is specified to be a literal copy of main */
}
/*
* Read in the logical volume integrity sequence pointed to by our logical
* volume descriptor. Its a sequence that can be extended using fields in the
* integrity descriptor itself. On sequential media only one is found, on
* rewritable media a sequence of descriptors can be found as a form of
* history keeping and on non sequential write-once media the chain is vital
* to allow more and more descriptors to be written. The last descriptor
* written in an extent needs to claim space for a new extent.
*/
static int
udf_retrieve_lvint(struct udf_mount *ump)
{
union dscrptr *dscr;
struct logvol_int_desc *lvint;
struct udf_lvintq *trace;
uint32_t lb_size, lbnum, len;
int dscr_type, error, trace_len;
lb_size = udf_rw32(ump->logical_vol->lb_size);
len = udf_rw32(ump->logical_vol->integrity_seq_loc.len);
lbnum = udf_rw32(ump->logical_vol->integrity_seq_loc.loc);
/* proceed sequential */
lbnum += 1;
len -= lb_size;
/* are we linking to a new piece? */
if (dscr && lvint->next_extent.len) {
len = udf_rw32(lvint->next_extent.len);
lbnum = udf_rw32(lvint->next_extent.loc);
/* just one element? its not legal but be bug compatible */
if (sumext == 1) {
/* overwrite the only entry */
DPRINTF(VOLUMES, ("\tLinux bugcompat overwriting sole entry\n"));
trace = &ump->lvint_trace[0];
trace->wpos = 0;
return 0;
}
losing = MIN(sumext, UDF_LVINT_LOSSAGE);
/* no sense wiping too much */
if (sumext == UDF_LVINT_LOSSAGE)
losing = UDF_LVINT_LOSSAGE/2;
/* patch up if first entry was on error */
if (bufs[0] == NULL) {
for (cnt = 0; cnt < cpy_len; cnt++)
if (bufs[cnt] != NULL)
break;
last_dscr = bufs[cnt];
for (; cnt > 0; cnt--) {
bufs[cnt] = last_dscr;
}
}
static int
udf_writeout_lvint(struct udf_mount *ump, int lvflag)
{
struct udf_lvintq *trace;
struct timeval now_v;
struct timespec now_s;
uint32_t sector;
int logvol_integrity;
int space, error;
DPRINTF(VOLUMES, ("writing out logvol integrity descriptor\n"));
/* get free space in last chunk */
trace = ump->lvint_trace;
while (trace->wpos > (trace->end - trace->start)) {
DPRINTF(VOLUMES, ("skip : start = %d, end = %d, pos = %d, "
"wpos = %d\n", trace->start, trace->end,
trace->pos, trace->wpos));
trace++;
}
/* check if there is space to append */
space = (trace->end - trace->start) - trace->wpos;
DPRINTF(VOLUMES, ("write start = %d, end = %d, pos = %d, wpos = %d, "
"space = %d\n", trace->start, trace->end, trace->pos,
trace->wpos, space));
/* get state */
logvol_integrity = udf_rw32(ump->logvol_integrity->integrity_type);
if (logvol_integrity == UDF_INTEGRITY_CLOSED) {
if ((space < 3) && (lvflag & UDF_APPENDONLY_LVINT)) {
/* TODO extent LVINT space if possible */
return EROFS;
}
}
if (space < 1) {
if (lvflag & UDF_APPENDONLY_LVINT)
return EROFS;
/* loose history by re-writing extents */
error = udf_loose_lvint_history(ump);
if (error)
return error;
/* force a sane minimum for descriptors CRC length */
/* see UDF 2.3.1.2 and 2.3.8.1 */
KASSERT(udf_rw16(dscr->sbd.tag.id) == TAGID_SPACE_BITMAP);
if (udf_rw16(dscr->sbd.tag.desc_crc_len) == 0)
dscr->sbd.tag.desc_crc_len = udf_rw16(8);
/* force a sane minimum for descriptors CRC length */
/* see UDF 2.3.1.2 and 2.3.8.1 */
KASSERT(udf_rw16(dscr->sbd.tag.id) == TAGID_SPACE_BITMAP);
if (udf_rw16(dscr->sbd.tag.desc_crc_len) == 0)
dscr->sbd.tag.desc_crc_len = udf_rw16(8);
int
udf_write_metadata_partition_spacetable(struct udf_mount *ump, int waitfor)
{
struct udf_node *bitmap_node;
union dscrptr *dscr;
uint64_t new_inflen;
int dummy, error;
bitmap_node = ump->metadatabitmap_node;
/* only write out when metadata bitmap node is known */
if (bitmap_node == NULL)
return 0;
if (!bitmap_node->fe) {
KASSERT(bitmap_node->efe);
}
/* reduce length to zero */
dscr = (union dscrptr *) ump->metadata_unalloc_dscr;
new_inflen = udf_tagsize(dscr, 1);
DPRINTF(VOLUMES, ("Resize and write out metadata space bitmap "
" for %"PRIu64" bytes\n", new_inflen));
error = udf_resize_node(bitmap_node, new_inflen, &dummy);
if (error)
printf("Error resizing metadata space bitmap\n");
/*
* We need at least one logvol integrity descriptor recorded. Note
* that its OK to have an open logical volume integrity here. The VAT
* will close/update the integrity.
*/
if (ump->logvol_integrity == NULL)
return EINVAL;
/*
* BUGALERT: some rogue implementations use random physical
* partition numbers to break other implementations so lookup
* the number.
*/
phys_part = udf_find_raw_phys(ump, raw_phys_part);
DPRINTF(VOLUMES, ("\t%d -> %d(%d) type %d\n", log_part,
raw_phys_part, phys_part, pmap_type));
if (phys_part == UDF_PARTITIONS)
return EINVAL;
if (pmap_type == UDF_VTOP_TYPE_UNKNOWN)
return EINVAL;
#if 0
/* read-only access won't benefit from the other schedulers */
if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
ump->strategy = &udf_strat_direct;
#endif
/*
* Update logical volume name in all structures that keep a record of it. We
* use memmove since each of them might be specified as a source.
*
* Note that it doesn't update the VAT structure!
*/
/* logvol's id might be specified as original so use memmove here */
memmove(lvd->logvol_id, logvol_id, 128);
if (fsd)
memmove(fsd->logvol_id, logvol_id, 128);
if (lvi)
memmove(lvi->logvol_id, logvol_id, 128);
}
static int
udf_create_parentfid(struct udf_mount *ump, struct fileid_desc *fid,
struct long_ad *parent, uint64_t unique_id)
{
/* the size of an empty FID is 38 but needs to be a multiple of 4 */
int fidsize = 40;
/*
* Extended attribute support. UDF knows of 3 places for extended attributes:
*
* (a) inside the file's (e)fe in the length of the extended attribute area
* before the allocation descriptors/filedata
*
* (b) in a file referenced by (e)fe->ext_attr_icb and
*
* (c) in the e(fe)'s associated stream directory that can hold various
* sub-files. In the stream directory a few fixed named subfiles are reserved
* for NT/Unix ACL's and OS/2 attributes.
*
* NOTE: Extended attributes are read randomly but always written
* *atomically*. For ACL's this interface is probably different but not known
* to me yet.
*
* Order of extended attributes in a space:
* ECMA 167 EAs
* Non block aligned Implementation Use EAs
* Block aligned Implementation Use EAs
* Application Use EAs
*/
static int
udf_impl_extattr_check(struct impl_extattr_entry *implext)
{
uint16_t *spos;
/* get mountpoint */
sector_size = node->ump->discinfo.sector_size;
/* get information from fe/efe */
if (node->fe) {
l_ea = udf_rw32(node->fe->l_ea);
eahdr = (struct extattrhdr_desc *) node->fe->data;
} else {
assert(node->efe);
l_ea = udf_rw32(node->efe->l_ea);
eahdr = (struct extattrhdr_desc *) node->efe->data;
}
/* something recorded here? */
if (l_ea == 0)
return ENOENT;
/* check extended attribute tag; what to do if it fails? */
error = udf_check_tag(eahdr);
if (error)
return EINVAL;
if (udf_rw16(eahdr->tag.id) != TAGID_EXTATTR_HDR)
return EINVAL;
error = udf_check_tag_payload(eahdr, sizeof(struct extattrhdr_desc));
if (error)
return EINVAL;
DPRINTF(EXTATTR, ("Found %d bytes of extended attributes\n", l_ea));
/* looking for Ecma-167 attributes? */
offset = sizeof(struct extattrhdr_desc);
/* looking for either implementation use or application use */
if (sattr == 2048) { /* [4/48.10.8] */
offset = udf_rw32(eahdr->impl_attr_loc);
if (offset == UDF_IMPL_ATTR_LOC_NOT_PRESENT)
return ENOENT;
}
if (sattr == 65536) { /* [4/48.10.9] */
offset = udf_rw32(eahdr->appl_attr_loc);
if (offset == UDF_APPL_ATTR_LOC_NOT_PRESENT)
return ENOENT;
}
/* paranoia check offset and l_ea */
if (l_ea + offset >= sector_size - sizeof(struct extattr_entry))
return EINVAL;
DPRINTF(EXTATTR, ("Starting at offset %d\n", offset));
/* get complete attribute length and check for roque values */
a_l = udf_rw32(attrhdr->a_l);
DPRINTF(EXTATTR, ("attribute %d:%d, len %d/%d\n",
udf_rw32(attrhdr->type),
attrhdr->subtype, a_l, l_ea));
if ((a_l == 0) || (a_l > l_ea))
return EINVAL;
if (udf_rw32(attrhdr->type) != sattr)
goto next_attribute;
/* we might have found it! */
if (udf_rw32(attrhdr->type) < 2048) { /* Ecma-167 attribute */
*offsetp = offset;
*lengthp = a_l;
return 0; /* success */
}
/*
* Implementation use and application use extended attributes
* have a name to identify. They share the same structure only
* UDF implementation use extended attributes have a checksum
* we need to check
*/
DPRINTF(EXTATTR, ("named attribute %s\n", implext->imp_id.id));
if (strcmp(implext->imp_id.id, sattrname) == 0) {
/* we have found our appl/implementation attribute */
*offsetp = offset;
*lengthp = a_l;
return 0; /* success */
}
next_attribute:
/* next attribute */
pos += a_l;
l_ea -= a_l;
offset += a_l;
}
/* not found */
return ENOENT;
}
/* do the `dance` again backwards */
if (udf_rw16(ump->logical_vol->tag.descriptor_ver) != 2) {
if (impl_attr_loc == l_ea)
impl_attr_loc = UDF_IMPL_ATTR_LOC_NOT_PRESENT;
if (appl_attr_loc == l_ea)
appl_attr_loc = UDF_APPL_ATTR_LOC_NOT_PRESENT;
}
/*
* we have found our "VAT LVExtension attribute. BUT due to a
* bug in the specification it might not be word aligned so
* copy first to avoid panics on some machines (!!)
*/
DPRINTF(VOLUMES, ("Found VAT LVExtension attr\n"));
lvextpos = implext->data + udf_rw32(implext->iu_l);
memcpy(&lvext, lvextpos, sizeof(lvext));
/* check if it was updated the last time */
if (udf_rw64(lvext.unique_id_chk) == vat_uniqueid) {
lvinfo->num_files = lvext.num_files;
lvinfo->num_directories = lvext.num_directories;
udf_update_logvolname(ump, lvext.logvol_id);
} else {
DPRINTF(VOLUMES, ("VAT LVExtension out of date\n"));
/* replace VAT LVExt by free space EA */
memset(implext->imp_id.id, 0, UDF_REGID_ID_SIZE);
strcpy(implext->imp_id.id, "*UDF FreeEASpace");
udf_calc_impl_extattr_checksum(implext);
}
/* get mountpoint and lvinfo */
ump = vat_node->ump;
lvinfo = ump->logvol_info;
/* get information from fe/efe */
if (vat_node->fe) {
vat_uniqueid = udf_rw64(vat_node->fe->unique_id);
ea_start = vat_node->fe->data;
} else {
vat_uniqueid = udf_rw64(vat_node->efe->unique_id);
ea_start = vat_node->efe->data;
}
error = udf_extattr_search_intern(vat_node, 2048, extstr, &offset, &a_l);
if (error)
return error;
/* found, it existed */
/* paranoia */
implext = (struct impl_extattr_entry *) (ea_start + offset);
error = udf_impl_extattr_check(implext);
if (error) {
DPRINTF(VOLUMES, ("VAT LVExtension bad on update\n"));
return error;
}
/* it is correct */
/*
* we have found our "VAT LVExtension attribute. BUT due to a
* bug in the specification it might not be word aligned so
* copy first to avoid panics on some machines (!!)
*/
DPRINTF(VOLUMES, ("Updating VAT LVExtension attr\n"));
lvextpos = (uintptr_t)implext->data + udf_rw32(implext->iu_l);
/* get our new unique_id */
unique_id = udf_advance_uniqueid(ump);
/* get information from fe/efe */
if (vat_node->fe) {
icbtag = &vat_node->fe->icbtag;
vat_node->fe->unique_id = udf_rw64(unique_id);
} else {
icbtag = &vat_node->efe->icbtag;
vat_node->efe->unique_id = udf_rw64(unique_id);
}
/* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
filetype = icbtag->file_type;
KASSERT((filetype == 0) || (filetype == UDF_ICB_FILETYPE_VAT));
/* allocate piece to process head or tail of VAT file */
raw_vat = malloc(lb_size, M_TEMP, M_WAITOK);
if (filetype == 0) {
/*
* Update "*UDF VAT LVExtension" extended attribute from the
* lvint if present.
*/
udf_update_vat_extattr_from_lvid(vat_node);
/* set vnode type to regular file or we can't read from it! */
vat_node->vnode->v_type = VREG;
/* get information from fe/efe */
if (vat_node->fe) {
vat_length = udf_rw64(vat_node->fe->inf_len);
icbtag = &vat_node->fe->icbtag;
mtime = &vat_node->fe->mtime;
unique_id = udf_rw64(vat_node->fe->unique_id);
} else {
vat_length = udf_rw64(vat_node->efe->inf_len);
icbtag = &vat_node->efe->icbtag;
mtime = &vat_node->efe->mtime;
unique_id = udf_rw64(vat_node->efe->unique_id);
}
/* Check icb filetype! it has to be 0 or UDF_ICB_FILETYPE_VAT */
filetype = icbtag->file_type;
if ((filetype != 0) && (filetype != UDF_ICB_FILETYPE_VAT))
return ENOENT;
vat_table = malloc(vat_table_alloc_len, M_UDFVOLD, M_WAITOK);
if (vat_table == NULL) {
printf("allocation of %d bytes failed for VAT\n",
vat_table_alloc_len);
return ENOMEM;
}
/* allocate piece to read in head or tail of VAT file */
raw_vat = malloc(sector_size, M_TEMP, M_WAITOK);
/*
* check contents of the file if its the old 1.50 VAT table format.
* Its notoriously broken and allthough some implementations support an
* extension as defined in the UDF 1.50 errata document, its doubtful
* to be useable since a lot of implementations don't maintain it.
*/
lvinfo = ump->logvol_info;
static int
udf_search_vat(struct udf_mount *ump, union udf_pmap *mapping)
{
struct udf_node *vat_node, *accepted_vat_node;
struct long_ad icb_loc;
uint32_t early_vat_loc, late_vat_loc, vat_loc;
int error;
/* mapping info not needed */
mapping = mapping;
DPRINTF(VOLUMES, ("Searching VAT\n"));
/*
* Start reading forward in blocks from the first possible vat
* location. If not found in this block, start again a bit before
* until we get a hit.
*/
late_vat_loc = ump->last_possible_vat_location;
early_vat_loc = MAX(late_vat_loc - 64, ump->first_possible_vat_location);
DPRINTF(VOLUMES, ("\tfull range %d to %d\n", early_vat_loc, late_vat_loc));
accepted_vat_node = NULL;
do {
vat_loc = early_vat_loc;
DPRINTF(VOLUMES, ("\tchecking range %d to %d\n",
early_vat_loc, late_vat_loc));
do {
DPRINTF(VOLUMES, ("\t\tChecking for VAT at sector %d\n",
vat_loc));
icb_loc.loc.part_num = udf_rw16(UDF_VTOP_RAWPART);
icb_loc.loc.lb_num = udf_rw32(vat_loc);
error = udf_get_node(ump, &icb_loc, &vat_node,
LK_EXCLUSIVE);
if (!error) {
error = udf_check_for_vat(vat_node);
vat_node->i_flags = 0; /* reset access */
}
if (!error) {
DPRINTFIF(VOLUMES, !error,
("VAT candidate accepted at %d\n",
vat_loc));
if (accepted_vat_node)
vput(accepted_vat_node->vnode);
accepted_vat_node = vat_node;
accepted_vat_node->i_flags |= IN_NO_DELETE;
vat_node = NULL;
}
if (vat_node)
vput(vat_node->vnode);
vat_loc++; /* walk forward */
} while (vat_loc <= late_vat_loc);
if (accepted_vat_node)
break;
/* keep our last accepted VAT node around */
if (accepted_vat_node) {
/* revert no delete flag again to avoid potential side effects */
accepted_vat_node->i_flags &= ~IN_NO_DELETE;
static int
udf_read_sparables(struct udf_mount *ump, union udf_pmap *mapping)
{
union dscrptr *dscr;
struct part_map_spare *pms = &mapping->pms;
uint32_t lb_num;
int spar, error;
/*
* The partition mapping passed on to us specifies the information we
* need to locate and initialise the sparable partition mapping
* information we need.
*/
static int
udf_read_metadata_nodes(struct udf_mount *ump, union udf_pmap *mapping)
{
struct part_map_meta *pmm = &mapping->pmm;
struct long_ad icb_loc;
struct vnode *vp;
uint16_t raw_phys_part, phys_part;
int error;
/*
* BUGALERT: some rogue implementations use random physical
* partition numbers to break other implementations so lookup
* the number.
*/
/* extract our allocation parameters set up on format */
ump->metadata_alloc_unit_size = udf_rw32(mapping->pmm.alloc_unit_size);
ump->metadata_alignment_unit_size = udf_rw16(mapping->pmm.alignment_unit_size);
ump->metadata_flags = mapping->pmm.flags;
int
udf_read_vds_tables(struct udf_mount *ump)
{
union udf_pmap *mapping;
/* struct udf_args *args = &ump->mount_args; */
uint32_t n_pm;
uint32_t log_part;
uint8_t *pmap_pos;
int pmap_size;
int error;
/* Iterate (again) over the part mappings for locations */
n_pm = udf_rw32(ump->logical_vol->n_pm); /* num partmaps */
pmap_pos = ump->logical_vol->maps;
for (log_part = 0; log_part < n_pm; log_part++) {
mapping = (union udf_pmap *) pmap_pos;
switch (ump->vtop_tp[log_part]) {
case UDF_VTOP_TYPE_PHYS :
/* nothing */
break;
case UDF_VTOP_TYPE_VIRT :
/* search and load VAT */
error = udf_search_vat(ump, mapping);
if (error)
return ENOENT;
break;
case UDF_VTOP_TYPE_SPARABLE :
/* load one of the sparable tables */
error = udf_read_sparables(ump, mapping);
if (error)
return ENOENT;
break;
case UDF_VTOP_TYPE_META :
/* load the associated file descriptors */
error = udf_read_metadata_nodes(ump, mapping);
if (error)
return ENOENT;
break;
default:
break;
}
pmap_size = pmap_pos[1];
pmap_pos += pmap_size;
}
/* read in and check unallocated and free space info if writing */
if ((ump->vfs_mountp->mnt_flag & MNT_RDONLY) == 0) {
error = udf_read_physical_partition_spacetables(ump);
if (error)
return error;
/* also read in metadata partition spacebitmap if defined */
error = udf_read_metadata_partition_spacetable(ump);
return error;
}
int
udf_read_rootdirs(struct udf_mount *ump)
{
union dscrptr *dscr;
/* struct udf_args *args = &ump->mount_args; */
struct udf_node *rootdir_node, *streamdir_node;
struct long_ad fsd_loc, *dir_loc;
uint32_t lb_num, dummy;
uint32_t fsd_len;
int dscr_type;
int error;
/* TODO implement FSD reading in separate function like integrity? */
/* get fileset descriptor sequence */
fsd_loc = ump->logical_vol->lv_fsd_loc;
fsd_len = udf_rw32(fsd_loc.len);
dscr = NULL;
error = 0;
while (fsd_len || error) {
DPRINTF(VOLUMES, ("fsd_len = %d\n", fsd_len));
/* translate fsd_loc to lb_num */
error = udf_translate_vtop(ump, &fsd_loc, &lb_num, &dummy);
if (error)
break;
DPRINTF(VOLUMES, ("Reading FSD at lb %d\n", lb_num));
error = udf_read_phys_dscr(ump, lb_num, M_UDFVOLD, &dscr);
/* end markers */
if (error || (dscr == NULL))
break;
/* continue to the next fsd */
fsd_len -= ump->discinfo.sector_size;
fsd_loc.loc.lb_num = udf_rw32(udf_rw32(fsd_loc.loc.lb_num)+1);
/* follow up to fsd->next_ex (long_ad) if its not null */
if (udf_rw32(ump->fileset_desc->next_ex.len)) {
DPRINTF(VOLUMES, ("follow up FSD extent\n"));
fsd_loc = ump->fileset_desc->next_ex;
fsd_len = udf_rw32(ump->fileset_desc->next_ex.len);
}
}
if (dscr)
free(dscr, M_UDFVOLD);
/* there has to be one */
if (ump->fileset_desc == NULL)
return ENOENT;
/*
* Now the FSD is known, read in the rootdirectory and if one exists,
* the system stream dir. Some files in the system streamdir are not
* wanted in this implementation since they are not maintained. If
* writing is enabled we'll delete these files if they exist.
*/
/* try to read in the rootdir */
dir_loc = &ump->fileset_desc->rootdir_icb;
error = udf_get_node(ump, dir_loc, &rootdir_node, LK_EXCLUSIVE);
if (error)
return ENOENT;
/* apparently it reads in fine */
/*
* Try the system stream directory; not very likely in the ones we
* test, but for completeness.
*/
dir_loc = &ump->fileset_desc->streamdir_icb;
if (udf_rw32(dir_loc->len)) {
printf("udf_read_rootdirs: streamdir defined ");
error = udf_get_node(ump, dir_loc, &streamdir_node,
LK_EXCLUSIVE);
if (error) {
printf("but error in streamdir reading\n");
} else {
printf("but ignored\n");
/*
* TODO process streamdir `baddies' i.e. files we dont
* want if R/W
*/
}
}
DPRINTF(VOLUMES, ("Rootdir(s) read in fine\n"));
/* release the vnodes again; they'll be auto-recycled later */
if (streamdir_node) {
vput(streamdir_node->vnode);
}
if (rootdir_node) {
vput(rootdir_node->vnode);
}
/* To make absolutely sure we are NOT returning zero, add one :) */
long
udf_get_node_id(const struct long_ad *icbptr)
{
/* ought to be enough since each mountpoint has its own chain */
return udf_rw32(icbptr->loc.lb_num) + 1;
}
int
udf_compare_icb(const struct long_ad *a, const struct long_ad *b)
{
if (udf_rw16(a->loc.part_num) < udf_rw16(b->loc.part_num))
return -1;
if (udf_rw16(a->loc.part_num) > udf_rw16(b->loc.part_num))
return 1;
if (udf_rw32(a->loc.lb_num) < udf_rw32(b->loc.lb_num))
return -1;
if (udf_rw32(a->loc.lb_num) > udf_rw32(b->loc.lb_num))
return 1;
/* we have to copy the ISO VRS from a former session */
DPRINTF(VOLUMES, ("validate_session_start: "
"blank or reserved track, copying VRS\n"));
/* sessionnr should be the session we're mounting */
sessionnr = ump->mount_args.sessionnr;
/* start at the first track */
tracknr = ump->discinfo.first_track;
while (tracknr <= ump->discinfo.num_tracks) {
trackinfo.tracknr = tracknr;
error = udf_update_trackinfo(ump, &trackinfo);
if (error) {
DPRINTF(VOLUMES, ("failed to get trackinfo; aborting\n"));
return error;
}
if (trackinfo.sessionnr == sessionnr)
break;
tracknr++;
}
if (trackinfo.sessionnr != sessionnr) {
DPRINTF(VOLUMES, ("failed to get trackinfo; aborting\n"));
return ENOENT;
}
DPRINTF(VOLUMES, ("found possible former ISO VRS at\n"));
udf_dump_trackinfo(&trackinfo);
/*
* location of iso9660 vrs is defined as first sector AFTER 32kb,
* minimum ISO `sector size' 2048
*/
sector_size = ump->discinfo.sector_size;
iso9660_vrs = ((32*1024 + sector_size - 1) / sector_size)
+ trackinfo.track_start;
DPRINTF(VOLUMES, ("Got VRS of %d sectors long\n", vrs_len));
/*
* location of iso9660 vrs is defined as first sector AFTER 32kb,
* minimum ISO `sector size' 2048
*/
sector_size = ump->discinfo.sector_size;
iso9660_vrs = ((32*1024 + sector_size - 1) / sector_size)
+ write_track_start;
/* write out 32 kb */
blank = malloc(sector_size, M_TEMP, M_WAITOK);
memset(blank, 0, sector_size);
error = 0;
for (sector = write_track_start; sector < iso9660_vrs; sector ++) {
error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE,
blank, sector, 1);
if (error)
break;
}
if (!error) {
/* write out our ISO VRS */
KASSERT(sector == iso9660_vrs);
error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE, buffer,
sector, vrs_len);
sector += vrs_len;
}
if (!error) {
/* fill upto the first anchor at S+256 */
for (; sector < write_track_start+256; sector++) {
error = udf_write_phys_sectors(ump, UDF_C_ABSOLUTE,
blank, sector, 1);
if (error)
break;
}
}
if (!error) {
/* write out anchor; write at ABSOLUTE place! */
error = udf_write_phys_dscr_sync(ump, NULL, UDF_C_ABSOLUTE,
(union dscrptr *) ump->anchors[0], sector, sector);
if (error)
printf("writeout of anchor failed!\n");
}
free(blank, M_TEMP);
free(buffer, M_TEMP);
if (error)
printf("udf_open_session: error writing iso vrs! : "
"leaving disc in compromised state!\n");
/* can we open it ? */
if (ump->vfs_mountp->mnt_flag & MNT_RDONLY)
return EROFS;
/* setup write parameters */
DPRINTF(VOLUMES, ("Setting up write parameters\n"));
if ((error = udf_setup_writeparams(ump)) != 0)
return error;
/* determine data and metadata tracks (most likely same) */
error = udf_search_writing_tracks(ump);
if (error) {
/* most likely lack of space */
printf("udf_open_logvol: error searching writing tracks\n");
return EROFS;
}
/* writeout/update lvint on disc or only in memory */
DPRINTF(VOLUMES, ("Opening logical volume\n"));
if (ump->lvopen & UDF_OPEN_SESSION) {
/* TODO optional track reservation opening */
error = udf_validate_session_start(ump);
if (error)
return error;
/* determine data and metadata tracks again */
error = udf_search_writing_tracks(ump);
if (ump->lvclose & UDF_WRITE_VAT) {
/*
* we writeout the VAT to get a self-sustained session
* for fsck
*/
DPRINTF(VOLUMES, ("lvclose & UDF_WRITE_VAT\n"));
/* write out the VAT data and all its descriptors */
DPRINTF(VOLUMES, ("writeout vat_node\n"));
udf_writeout_vat(ump);
/* force everything to be synchronized on the device */
(void) udf_synchronise_caches(ump);
}
}
/* mark it open */
ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_OPEN);
/* do we need to write it out? */
if (ump->lvopen & UDF_WRITE_LVINT) {
error = udf_writeout_lvint(ump, ump->lvopen);
/* if we couldn't write it mark it closed again */
if (error) {
ump->logvol_integrity->integrity_type =
udf_rw32(UDF_INTEGRITY_CLOSED);
return error;
}
}
return 0;
}
int
udf_close_logvol(struct udf_mount *ump, int mntflags)
{
struct vnode *devvp = ump->devvp;
struct mmc_op mmc_op;
uint32_t phys;
int logvol_integrity;
int error = 0, error1 = 0, error2 = 0;
int tracknr;
int nvats, n, relblk, wrtrack_skew, nok;
/* writeout/update lvint or write out VAT */
DPRINTF(VOLUMES, ("udf_close_logvol: closing logical volume\n"));
#ifdef DIAGNOSTIC
if (ump->lvclose & UDF_CLOSE_SESSION)
KASSERT(ump->lvclose & UDF_WRITE_VAT);
#endif
if (ump->lvclose & UDF_WRITE_VAT) {
DPRINTF(VOLUMES, ("lvclose & UDF_WRITE_VAT\n"));
/* write out the VAT data and all its descriptors */
DPRINTF(VOLUMES, ("writeout vat_node\n"));
udf_writeout_vat(ump);
/*
* For bug-compatibility with Windows, the last VAT sector
* must be a multiple of 16/32 from the start of the track.
* To allow for scratches, write out at least a 32 pieces.
*/
phys = ump->data_track.track_start;
wrtrack_skew = phys % 32;
#if notyet
/*
* TODO calculate the available space and if the disc is
* almost full, write out till end-256-1 with banks, write
* AVDP and fill up with VATs, then close session and close
* disc.
*/
if (ump->lvclose & UDF_FINALISE_DISC) {
error = udf_write_phys_dscr_sync(ump, NULL,
UDF_C_FLOAT_DSCR,
(union dscrptr *) ump->anchors[0],
0, 0);
if (error)
printf("writeout of anchor failed!\n");
/* pad space with VAT ICBs */
nvats = 256;
}
#endif
/* write out a number of VAT nodes */
nok = 0;
for (n = 0; n < nvats; n++) {
/* will now only write last FE/EFE */
ump->vat_node->i_flags |= IN_MODIFIED;
error = VOP_FSYNC(ump->vat_node->vnode,
FSCRED, FSYNC_WAIT, 0, 0);
if (!error)
nok++;
}
/* force everything to be synchronized on the device */
(void) udf_synchronise_caches(ump);
if (nok < 14) {
/* arbitrary; but at least one or two CD frames */
printf("writeout of at least 14 VATs failed\n");
return error;
}
}
/* NOTE the disc is in a (minimal) valid state now; no erroring out */
/* finish closing of session */
if (ump->lvclose & UDF_CLOSE_SESSION) {
DPRINTF(VOLUMES, ("udf_close_logvol: closing session "
"as requested\n"));
error = udf_validate_session_start(ump);
if (error)
return error;
(void) udf_synchronise_caches(ump);
/* close all associated tracks */
tracknr = ump->discinfo.first_track_last_session;
error = 0;
while (tracknr <= ump->discinfo.last_track_last_session) {
DPRINTF(VOLUMES, ("\tclosing possible open "
"track %d\n", tracknr));
memset(&mmc_op, 0, sizeof(mmc_op));
mmc_op.operation = MMC_OP_CLOSETRACK;
mmc_op.mmc_profile = ump->discinfo.mmc_profile;
mmc_op.tracknr = tracknr;
error = VOP_IOCTL(devvp, MMCOP, &mmc_op,
FKIOCTL, NOCRED);
if (error)
printf("udf_close_logvol: closing of "
"track %d failed\n", tracknr);
tracknr ++;
}
if (!error) {
DPRINTF(VOLUMES, ("closing session\n"));
memset(&mmc_op, 0, sizeof(mmc_op));
mmc_op.operation = MMC_OP_CLOSESESSION;
mmc_op.mmc_profile = ump->discinfo.mmc_profile;
mmc_op.sessionnr = ump->discinfo.num_sessions;
error = VOP_IOCTL(devvp, MMCOP, &mmc_op,
FKIOCTL, NOCRED);
if (error)
printf("udf_close_logvol: closing of session"
"failed\n");
}
if (!error)
ump->lvopen |= UDF_OPEN_SESSION;
if (error) {
printf("udf_close_logvol: leaving disc as it is\n");
ump->lvclose &= ~UDF_FINALISE_DISC;
}
}
/* mark it closed */
ump->logvol_integrity->integrity_type = udf_rw32(UDF_INTEGRITY_CLOSED);
/* do we need to write out the logical volume integrity? */
if (ump->lvclose & UDF_WRITE_LVINT)
error = udf_writeout_lvint(ump, ump->lvopen);
if (error) {
/* HELP now what? mark it open again for now */
ump->logvol_integrity->integrity_type =
udf_rw32(UDF_INTEGRITY_OPEN);
return error;
}
/*
* Genfs interface. These four functions are the only ones defined though not
* documented... great....
*/
/*
* Called for allocating an extent of the file either by VOP_WRITE() or by
* genfs filling up gaps.
*/
static int
udf_gop_alloc(struct vnode *vp, off_t off,
off_t len, int flags, kauth_cred_t cred)
{
struct udf_node *udf_node = VTOI(vp);
struct udf_mount *ump = udf_node->ump;
uint64_t lb_start, lb_end;
uint32_t lb_size, num_lb;
int udf_c_type, vpart_num, can_fail;
int error;
DPRINTF(ALLOC, ("udf_gop_alloc called for offset %"PRIu64" for %"PRIu64" bytes, %s\n",
off, len, flags? "SYNC":"NONE"));
/*
* request the pages of our vnode and see how many pages will need to
* be allocated and reserve that space
*/
lb_size = udf_rw32(udf_node->ump->logical_vol->lb_size);
lb_start = off / lb_size;
lb_end = (off + len + lb_size -1) / lb_size;
num_lb = lb_end - lb_start;
/* CRC length for an anchor is 512 - tag length; defined in Ecma 167 */
dscr->tag.desc_crc_len = udf_rw16(512-UDF_DESC_TAG_LENGTH);
(void) udf_validate_tag_and_crc_sums(dscr);
static uint32_t
udf_icb_to_unix_filetype(uint32_t icbftype)
{
switch (icbftype) {
case UDF_ICB_FILETYPE_DIRECTORY :
case UDF_ICB_FILETYPE_STREAMDIR :
return S_IFDIR;
case UDF_ICB_FILETYPE_FIFO :
return S_IFIFO;
case UDF_ICB_FILETYPE_CHARDEVICE :
return S_IFCHR;
case UDF_ICB_FILETYPE_BLOCKDEVICE :
return S_IFBLK;
case UDF_ICB_FILETYPE_RANDOMACCESS :
case UDF_ICB_FILETYPE_REALTIME :
return S_IFREG;
case UDF_ICB_FILETYPE_SYMLINK :
return S_IFLNK;
case UDF_ICB_FILETYPE_SOCKET :
return S_IFSOCK;
}
/* no idea what this is */
return 0;
}
/*
* Calculate the time zone. The timezone is 12 bit signed 2's
* compliment, so we gotta do some extra magic to handle it right.
*/
tz = udf_rw16(timestamp->type_tz);
tz &= 0x0fff; /* only lower 12 bits are significant */
if (tz & 0x0800) /* sign extension */
tz |= 0xf000;
/* TODO check timezone conversion */
/* check if we are specified a timezone to convert */
if (udf_rw16(timestamp->type_tz) & 0x1000) {
if ((int16_t) tz != -2047)
secs -= (int16_t) tz * 60;
} else {
secs -= ump->mount_args.gmtoff;
}
/* make sure we have a dirhash to work on */
dirh = dir_node->dir_hash;
KASSERT(dirh);
KASSERT(dirh->refcnt > 0);
if (dirh->flags & DIRH_BROKEN)
return EIO;
if (dirh->flags & DIRH_COMPLETE)
return 0;
/* make sure we have a clean dirhash to add to */
dirhash_purge_entries(dirh);
/* get directory filesize */
if (fe) {
file_size = udf_rw64(fe->inf_len);
} else {
assert(efe);
file_size = udf_rw64(efe->inf_len);
}
/* allocate temporary space for fid */
lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
/* allocate temporary space for dirent */
dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
error = 0;
diroffset = 0;
while (diroffset < file_size) {
/* transfer a new fid/dirent */
pre_diroffset = diroffset;
error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
if (error) {
/* TODO what to do? continue but not add? */
dirh->flags |= DIRH_BROKEN;
dirhash_purge_entries(dirh);
break;
}
if ((fid->file_char & UDF_FILE_CHAR_DEL)) {
/* register deleted extent for reuse */
dirhash_enter_freed(dirh, pre_diroffset,
udf_fidsize(fid));
} else {
/* append to the dirhash */
dirhash_enter(dirh, dirent, pre_diroffset,
udf_fidsize(fid), 0);
}
}
dirh->flags |= DIRH_COMPLETE;
/* get our dirhash and make sure its read in */
dirhash_get(&dir_node->dir_hash);
error = udf_dirhash_fill(dir_node);
if (error) {
dirhash_put(dir_node->dir_hash);
return error;
}
dirh = dir_node->dir_hash;
/*
* Always use strategy type 4 unless on WORM which we don't support
* (yet). Fill in defaults and set for internal allocation of data.
*/
icb->strat_type = udf_rw16(4);
icb->max_num_entries = udf_rw16(1);
icb->file_type = file_type; /* 8 bit */
icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC);
fe->perm = udf_rw32(0x7fff); /* all is allowed */
fe->link_cnt = udf_rw16(0); /* explicit setting */
fe->ckpoint = udf_rw32(1); /* user supplied file version */
/*
* Always use strategy type 4 unless on WORM which we don't support
* (yet). Fill in defaults and set for internal allocation of data.
*/
icb->strat_type = udf_rw16(4);
icb->max_num_entries = udf_rw16(1);
icb->file_type = file_type; /* 8 bit */
icb->flags = udf_rw16(UDF_ICB_INTERN_ALLOC);
efe->perm = udf_rw32(0x7fff); /* all is allowed */
efe->link_cnt = udf_rw16(0); /* explicit setting */
efe->ckpoint = udf_rw32(1); /* user supplied file version */
/* get our dirhash and make sure its read in */
dirhash_get(&dir_node->dir_hash);
error = udf_dirhash_fill(dir_node);
if (error) {
dirhash_put(dir_node->dir_hash);
return error;
}
dirh = dir_node->dir_hash;
/* get directory filesize */
if (!fe) {
assert(dir_node->efe);
}
/* allocate temporary space for fid and dirents */
lb_size = udf_rw32(dir_node->ump->logical_vol->lb_size);
fid = malloc(lb_size, M_UDFTEMP, M_WAITOK);
dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
s_dirent = malloc(sizeof(struct dirent), M_UDFTEMP, M_WAITOK);
/* convert given unix name to canonical unix name */
udf_osta_charset(&osta_charspec);
unix_to_udf_name((char *) fid->data, &fid->l_fi,
cnp->cn_nameptr, cnp->cn_namelen, &osta_charspec);
udf_to_unix_name(s_dirent->d_name, NAME_MAX,
(char *) fid->data, fid->l_fi,
&osta_charspec);
s_dirent->d_namlen = strlen(s_dirent->d_name);
/* search our dirhash hits */
found = 0;
dirh_ep = NULL;
for (;;) {
hit = dirhash_lookup(dirh, s_dirent->d_name, s_dirent->d_namlen, &dirh_ep);
/* if no hit, abort the search */
if (!hit)
break;
/* check this hit */
diroffset = dirh_ep->offset;
/* transfer a new fid/dirent */
error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
if (error)
break;
/* see if its our entry */
KASSERT(dirent->d_namlen == s_dirent->d_namlen);
if (strncmp(dirent->d_name, s_dirent->d_name, s_dirent->d_namlen) == 0) {
found = 1;
break;
}
}
if (!found)
error = ENOENT;
if (error)
goto error_out;
/* get reference count of attached node */
if (udf_node->fe) {
refcnt = udf_rw16(udf_node->fe->link_cnt);
} else {
KASSERT(udf_node->efe);
refcnt = udf_rw16(udf_node->efe->link_cnt);
}
#ifdef UDF_COMPLETE_DELETE
/* subtract reference counter in attached node */
refcnt -= 1;
if (udf_node->fe) {
udf_node->fe->link_cnt = udf_rw16(refcnt);
} else {
udf_node->efe->link_cnt = udf_rw16(refcnt);
}
/* prevent writeout when refcnt == 0 */
if (refcnt == 0)
udf_node->i_flags |= IN_DELETED;
if (fid->file_char & UDF_FILE_CHAR_DIR) {
int drefcnt;
/* subtract reference counter in directory node */
/* note subtract 2 (?) for its was also backreferenced */
if (dir_node->fe) {
drefcnt = udf_rw16(dir_node->fe->link_cnt);
drefcnt -= 1;
dir_node->fe->link_cnt = udf_rw16(drefcnt);
} else {
KASSERT(dir_node->efe);
drefcnt = udf_rw16(dir_node->efe->link_cnt);
drefcnt -= 1;
dir_node->efe->link_cnt = udf_rw16(drefcnt);
}
}
udf_node->i_flags |= IN_MODIFIED;
dir_node->i_flags |= IN_MODIFIED;
#endif
/* if it is/was a hardlink adjust the file count */
if (refcnt > 0)
udf_adjust_filecount(udf_node, -1);
/* remove from the dirhash */
dirhash_remove(dirh, dirent, diroffset,
udf_fidsize(fid));
/* get our dirhash and make sure its read in */
dirhash_get(&dir_node->dir_hash);
error = udf_dirhash_fill(dir_node);
if (error) {
dirhash_put(dir_node->dir_hash);
return error;
}
dirh = dir_node->dir_hash;
/* get new parent's unique ID */
fe = new_parent_node->fe;
efe = new_parent_node->efe;
if (fe) {
new_parent_unique_id = udf_rw64(fe->unique_id);
} else {
assert(efe);
new_parent_unique_id = udf_rw64(efe->unique_id);
}
/* get directory filesize */
fe = dir_node->fe;
efe = dir_node->efe;
if (!fe) {
assert(efe);
}
/*
* NOTE the standard does not dictate the FID entry '..' should be
* first, though in practice it will most likely be.
*/
/* search our dirhash hits */
found = 0;
dirh_ep = NULL;
for (;;) {
hit = dirhash_lookup(dirh, name, namelen, &dirh_ep);
/* if no hit, abort the search */
if (!hit)
break;
/* check this hit */
diroffset = dirh_ep->offset;
/* transfer a new fid/dirent */
error = udf_read_fid_stream(dvp, &diroffset, fid, dirent);
if (error)
break;
/* see if its our entry */
KASSERT(dirent->d_namlen == namelen);
if (strncmp(dirent->d_name, name, namelen) == 0) {
found = 1;
break;
}
}
if (!found)
error = ENOENT;
if (error)
goto error_out;
/* update our ICB to the new parent, hit of lower 32 bits of uniqueid */
fid->icb = new_parent_node->write_loc;
fid->icb.longad_uniqueid = udf_rw32(new_parent_unique_id);
/*
* We are not allowed to split the fid tag itself over an logical block so
* check the space remaining in the logical block.
*
* We try to select the smallest candidate for recycling or when none is
* found, append a new one at the end of the directory.
*/
/* get our dirhash and make sure its read in */
dirhash_get(&dir_node->dir_hash);
error = udf_dirhash_fill(dir_node);
if (error) {
dirhash_put(dir_node->dir_hash);
return error;
}
dirh = dir_node->dir_hash;
/* get info */
lb_size = udf_rw32(ump->logical_vol->lb_size);
udf_osta_charset(&osta_charspec);
#ifndef UDF_COMPLETE_DELETE
/* transfer a new fid/dirent */
error = udf_read_fid_stream(vp, &fid_pos, fid, dirent);
if (error)
goto error_out;
/* only reuse entries that are wiped */
/* check if the len + loc are marked zero */
if (udf_rw32(fid->icb.len) != 0)
continue;
if (udf_rw32(fid->icb.loc.lb_num) != 0)
continue;
if (udf_rw16(fid->icb.loc.part_num) != 0)
continue;
#endif /* UDF_COMPLETE_DELETE */
/* select if not splitting the tag and its smaller */
if ((size_diff >= 0) &&
(size_diff < chosen_size_diff) &&
(lb_rest >= sizeof(struct desc_tag)))
{
/* UDF 2.3.4.2+3 specifies rules for iu size */
if ((size_diff == 0) || (size_diff >= 32)) {
chosen_fid_pos = fid_pos;
chosen_size = this_fidsize;
chosen_size_diff = size_diff;
}
}
}
/* extend directory if no other candidate found */
if (chosen_size == 0) {
chosen_fid_pos = dir_size;
chosen_size = fidsize;
chosen_size_diff = 0;
/* special case UDF 2.00+ 2.3.4.4, no splitting up fid tag */
if (addr_type == UDF_ICB_INTERN_ALLOC) {
/* pre-grow directory to see if we're to switch */
udf_grow_node(dir_node, dir_size + chosen_size);
/* make sure the next fid desc_tag won't be split */
if (addr_type != UDF_ICB_INTERN_ALLOC) {
end_fid_pos = chosen_fid_pos + chosen_size;
lb_rest = lb_size - (end_fid_pos % lb_size);
/* pad with implementation use regid if needed */
if (lb_rest < sizeof(struct desc_tag))
chosen_size += 32;
}
}
chosen_size_diff = chosen_size - fidsize;
/* mark not deleted if it was... just in case, but do warn */
if (udf_node->i_flags & IN_DELETED) {
printf("udf: warning, marking a file undeleted\n");
udf_node->i_flags &= ~IN_DELETED;
}
if (file_char & UDF_FILE_CHAR_DIR) {
/* add reference counter in directory node for '..' */
if (dir_node->fe) {
refcnt = udf_rw16(dir_node->fe->link_cnt);
refcnt++;
dir_node->fe->link_cnt = udf_rw16(refcnt);
} else {
KASSERT(dir_node->efe);
refcnt = udf_rw16(dir_node->efe->link_cnt);
refcnt++;
dir_node->efe->link_cnt = udf_rw16(refcnt);
}
}
/* append to the dirhash */
/* NOTE do not use dirent anymore or it won't match later! */
udf_to_unix_name(dirent.d_name, NAME_MAX,
(char *) fid->data + udf_rw16(fid->l_iu), fid->l_fi, &osta_charspec);
dirent.d_namlen = strlen(dirent.d_name);
dirhash_enter(dirh, &dirent, chosen_fid_pos,
udf_fidsize(fid), 1);
/*
* Each node can have an attached streamdir node though not recursively. These
* are otherwise known as named substreams/named extended attributes that have
* no size limitations.
*
* `Normal' extended attributes are indicated with a number and are recorded
* in either the fe/efe descriptor itself for small descriptors or recorded in
* the attached extended attribute file. Since these spaces can get
* fragmented, care ought to be taken.
*
* Since the size of the space reserved for allocation descriptors is limited,
* there is a mechanim provided for extending this space; this is done by a
* special extent to allow shrinking of the allocations without breaking the
* linkage to the allocation extent descriptor.
*/
int
udf_loadvnode(struct mount *mp, struct vnode *vp,
const void *key, size_t key_len, const void **new_key)
{
union dscrptr *dscr;
struct udf_mount *ump;
struct udf_node *udf_node;
struct long_ad node_icb_loc, icb_loc, next_icb_loc, last_fe_icb_loc;
uint64_t file_size;
uint32_t lb_size, sector, dummy;
int udf_file_type, dscr_type, strat, strat4096, needs_indirect;
int slot, eof, error;
int num_indir_followed = 0;
/* check if we're fetching the root */
if (ump->fileset_desc)
if (memcmp(&udf_node->loc, &ump->fileset_desc->rootdir_icb,
sizeof(struct long_ad)) == 0)
vp->v_vflag |= VV_ROOT;
DPRINTF(NODE, ("\tstart reading descriptors\n"));
do {
/* try to read in fe/efe */
error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
/* blank sector marks end of sequence, check this */
if ((dscr == NULL) && (!strat4096))
error = ENOENT;
/* break if read error or blank sector */
if (error || (dscr == NULL))
break;
/* process descriptor based on the descriptor type */
dscr_type = udf_rw16(dscr->tag.id);
DPRINTF(NODE, ("\tread descriptor %d\n", dscr_type));
/* if dealing with an indirect entry, follow the link */
if (dscr_type == TAGID_INDIRECTENTRY) {
needs_indirect = 0;
next_icb_loc = dscr->inde.indirect_icb;
udf_free_logvol_dscr(ump, &icb_loc, dscr);
icb_loc = next_icb_loc;
if (++num_indir_followed > UDF_MAX_INDIRS_FOLLOW) {
error = EMLINK;
break;
}
continue;
}
/* only file entries and extended file entries allowed here */
if ((dscr_type != TAGID_FENTRY) &&
(dscr_type != TAGID_EXTFENTRY)) {
udf_free_logvol_dscr(ump, &icb_loc, dscr);
error = ENOENT;
break;
}
KASSERT(udf_tagsize(dscr, lb_size) == lb_size);
/* choose this one */
last_fe_icb_loc = icb_loc;
/* record and process/update (ext)fentry */
if (dscr_type == TAGID_FENTRY) {
if (udf_node->fe)
udf_free_logvol_dscr(ump, &last_fe_icb_loc,
udf_node->fe);
udf_node->fe = &dscr->fe;
strat = udf_rw16(udf_node->fe->icbtag.strat_type);
udf_file_type = udf_node->fe->icbtag.file_type;
file_size = udf_rw64(udf_node->fe->inf_len);
} else {
if (udf_node->efe)
udf_free_logvol_dscr(ump, &last_fe_icb_loc,
udf_node->efe);
udf_node->efe = &dscr->efe;
strat = udf_rw16(udf_node->efe->icbtag.strat_type);
udf_file_type = udf_node->efe->icbtag.file_type;
file_size = udf_rw64(udf_node->efe->inf_len);
}
/* check recording strategy (structure) */
/*
* Strategy 4096 is a daisy linked chain terminating with an
* unrecorded sector or a TERM descriptor. The next
* descriptor is to be found in the sector that follows the
* current sector.
*/
if (strat == 4096) {
strat4096 = 1;
needs_indirect = 1;
/* assert no references to dscr anymore beyond this point */
assert((udf_node->fe) || (udf_node->efe));
dscr = NULL;
/*
* Remember where to record an updated version of the descriptor. If
* there is a sequence of indirect entries, icb_loc will have been
* updated. It's the write discipline to allocate new space and to make
* sure the chain is maintained.
*
* `needs_indirect' flags if the next location is to be filled with
* an indirect entry.
*/
udf_node->write_loc = icb_loc;
udf_node->needs_indirect = needs_indirect;
/*
* Go through all allocations extents of this descriptor and when
* encountering a redirect read in the allocation extension. These are
* daisy-chained.
*/
UDF_LOCK_NODE(udf_node, 0);
udf_node->num_extensions = 0;
if (UDF_EXT_FLAGS(udf_rw32(icb_loc.len)) != UDF_EXT_REDIRECT)
continue;
DPRINTF(NODE, ("\tgot redirect extent\n"));
if (udf_node->num_extensions >= UDF_MAX_ALLOC_EXTENTS) {
DPRINTF(ALLOC, ("udf_get_node: implementation limit, "
"too many allocation extensions on "
"udf_node\n"));
error = EINVAL;
break;
}
/* length can only be *one* lb : UDF 2.50/2.3.7.1 */
if (UDF_EXT_LEN(udf_rw32(icb_loc.len)) != lb_size) {
DPRINTF(ALLOC, ("udf_get_node: bad allocation "
"extension size in udf_node\n"));
error = EINVAL;
break;
}
DPRINTF(NODE, ("read allocation extent at lb_num %d\n",
UDF_EXT_LEN(udf_rw32(icb_loc.loc.lb_num))));
/* load in allocation extent */
error = udf_read_logvol_dscr(ump, &icb_loc, &dscr);
if (error || (dscr == NULL))
break;
/* process read-in descriptor */
dscr_type = udf_rw16(dscr->tag.id);
/* second round of cleanup code */
if (error) {
/* recycle udf_node */
udf_dispose_node(udf_node);
return EINVAL; /* error code ok? */
}
DPRINTF(NODE, ("\tnode read in fine\n"));
/*
* Translate UDF filetypes into vnode types.
*
* Systemfiles like the meta main and mirror files are not treated as
* normal files, so we type them as having no type. UDF dictates that
* they are not allowed to be visible.
*/
switch (udf_file_type) {
case UDF_ICB_FILETYPE_DIRECTORY :
case UDF_ICB_FILETYPE_STREAMDIR :
vp->v_type = VDIR;
break;
case UDF_ICB_FILETYPE_BLOCKDEVICE :
vp->v_type = VBLK;
break;
case UDF_ICB_FILETYPE_CHARDEVICE :
vp->v_type = VCHR;
break;
case UDF_ICB_FILETYPE_SOCKET :
vp->v_type = VSOCK;
break;
case UDF_ICB_FILETYPE_FIFO :
vp->v_type = VFIFO;
break;
case UDF_ICB_FILETYPE_SYMLINK :
vp->v_type = VLNK;
break;
case UDF_ICB_FILETYPE_VAT :
case UDF_ICB_FILETYPE_META_MAIN :
case UDF_ICB_FILETYPE_META_MIRROR :
vp->v_type = VNON;
break;
case UDF_ICB_FILETYPE_RANDOMACCESS :
case UDF_ICB_FILETYPE_REALTIME :
vp->v_type = VREG;
break;
default:
/* YIKES, something else */
vp->v_type = VNON;
}
/* TODO specfs, fifofs etc etc. vnops setting */
/* don't forget to set vnode's v_size */
uvm_vnp_setsize(vp, file_size);
/* TODO ext attr and streamdir udf_nodes */
*new_key = &udf_node->loc.loc;
return 0;
}
int
udf_get_node(struct udf_mount *ump, struct long_ad *node_icb_loc,
struct udf_node **udf_noderes, int lktype)
{
int error;
struct vnode *vp;
if (udf_node->i_flags & IN_DELETED) {
DPRINTF(NODE, ("\tnode deleted; not writing out\n"));
udf_cleanup_reservation(udf_node);
return 0;
}
/* lock node; unlocked in callback */
UDF_LOCK_NODE(udf_node, 0);
/* remove pending reservations, we're written out */
udf_cleanup_reservation(udf_node);
/* at least one descriptor writeout */
udf_node->outstanding_nodedscr = 1;
/* we're going to write out the descriptor so clear the flags */
udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED);
/* if we were rebuild, write out the allocation extents */
if (udf_node->i_flags & IN_NODE_REBUILD) {
/* mark outstanding node descriptors and issue them */
udf_node->outstanding_nodedscr += udf_node->num_extensions;
for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
loc = &udf_node->ext_loc[extnr];
dscr = (union dscrptr *) udf_node->ext[extnr];
error = udf_write_logvol_dscr(udf_node, dscr, loc, 0);
if (error)
return error;
}
/* mark allocation extents written out */
udf_node->i_flags &= ~(IN_NODE_REBUILD);
}
/* dissociate our udf_node from the vnode */
genfs_node_destroy(udf_node->vnode);
mutex_enter(vp->v_interlock);
vp->v_data = NULL;
mutex_exit(vp->v_interlock);
/* free associated memory and the node itself */
for (extnr = 0; extnr < udf_node->num_extensions; extnr++) {
udf_free_logvol_dscr(udf_node->ump, &udf_node->ext_loc[extnr],
udf_node->ext[extnr]);
udf_node->ext[extnr] = (void *) 0xdeadcccc;
}
if (udf_node->fe)
udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
udf_node->fe);
if (udf_node->efe)
udf_free_logvol_dscr(udf_node->ump, &udf_node->loc,
udf_node->efe);
/* get parent's unique ID for referring '..' if its a directory */
if (dir_node->fe) {
parent_unique_id = udf_rw64(dir_node->fe->unique_id);
parent_gid = (gid_t) udf_rw32(dir_node->fe->gid);
} else {
parent_unique_id = udf_rw64(dir_node->efe->unique_id);
parent_gid = (gid_t) udf_rw32(dir_node->efe->gid);
}
/* get descriptor */
udf_create_logvol_dscr(ump, udf_node, &node_icb_loc, &dscr);
/* choose a fe or an efe for it */
if (udf_rw16(ump->logical_vol->tag.descriptor_ver) == 2) {
udf_node->fe = &dscr->fe;
fid_size = udf_create_new_fe(ump, udf_node->fe,
udf_file_type, &udf_node->loc,
&dir_node->loc, parent_unique_id);
/* TODO add extended attribute for creation time */
} else {
udf_node->efe = &dscr->efe;
fid_size = udf_create_new_efe(ump, udf_node->efe,
udf_file_type, &udf_node->loc,
&dir_node->loc, parent_unique_id);
}
KASSERT(dscr->tag.tag_loc == udf_node->loc.loc.lb_num);
/* update vnode's size and type */
vp->v_type = vap->va_type;
uvm_vnp_setsize(vp, fid_size);
/* set access mode */
udf_setaccessmode(udf_node, vap->va_mode);
/* paranoia check on integrity; should be open!; we could panic */
lvint = udf_rw32(udf_node->ump->logvol_integrity->integrity_type);
if (lvint == UDF_INTEGRITY_CLOSED)
printf("\tIntegrity was CLOSED!\n");
/* whatever the node type, change its size to zero */
(void) udf_resize_node(udf_node, 0, &dummy);
/* force it to be `clean'; no use writing it out */
udf_node->i_flags &= ~(IN_MODIFIED | IN_ACCESSED | IN_ACCESS |
IN_CHANGE | IN_UPDATE | IN_MODIFY);
/* set new filesize; node but be LOCKED on entry and is locked on exit */
int
udf_resize_node(struct udf_node *udf_node, uint64_t new_size, int *extended)
{
struct file_entry *fe = udf_node->fe;
struct extfile_entry *efe = udf_node->efe;
uint64_t file_size;
int error;
/* protect against rogue values */
if (!udf_node)
return;
fe = udf_node->fe;
efe = udf_node->efe;
if (!(udf_node->i_flags & (IN_ACCESS|IN_CHANGE|IN_UPDATE|IN_MODIFY)))
return;
/* get descriptor information */
if (fe) {
atime = &fe->atime;
mtime = &fe->mtime;
attrtime = &fe->attrtime;
filedata = fe->data;
/* initial save dummy setting */
ctime = &fe_ctime;
/* check our extended attribute if present */
error = udf_extattr_search_intern(udf_node,
UDF_FILETIMES_ATTR_NO, "", &offset, &a_l);
if (!error) {
ft_extattr = (struct filetimes_extattr_entry *)
(filedata + offset);
if (ft_extattr->existence & UDF_FILETIMES_FILE_CREATION)
ctime = &ft_extattr->times[0];
}
/* TODO create the extended attribute if not found ? */
} else {
assert(udf_node->efe);
atime = &efe->atime;
mtime = &efe->mtime;
attrtime = &efe->attrtime;
ctime = &efe->ctime;
}
vfs_timestamp(&now);
/* set access time */
if (udf_node->i_flags & IN_ACCESS) {
if (acc == NULL)
acc = &now;
udf_timespec_to_timestamp(acc, atime);
}
/* set modification time */
if (udf_node->i_flags & (IN_UPDATE | IN_MODIFY)) {
if (mod == NULL)
mod = &now;
udf_timespec_to_timestamp(mod, mtime);
/* ensure birthtime is older than set modification! */
udf_timestamp_to_timespec(udf_node->ump, ctime, &cur_birth);
if ((cur_birth.tv_sec > mod->tv_sec) ||
((cur_birth.tv_sec == mod->tv_sec) &&
(cur_birth.tv_nsec > mod->tv_nsec))) {
udf_timespec_to_timestamp(mod, ctime);
}
}
/* update birthtime if specified */
/* XXX we assume here that given birthtime is older than mod */
if (birth && (birth->tv_sec != VNOVAL)) {
udf_timespec_to_timestamp(birth, ctime);
}
/* set change time */
if (udf_node->i_flags & (IN_CHANGE | IN_MODIFY))
udf_timespec_to_timestamp(&now, attrtime);
/* notify updates to the node itself */
if (udf_node->i_flags & (IN_ACCESS | IN_MODIFY))
udf_node->i_flags |= IN_ACCESSED;
if (udf_node->i_flags & (IN_UPDATE | IN_CHANGE))
udf_node->i_flags |= IN_MODIFIED;
/*
* Read one fid and process it into a dirent and advance to the next (*fid)
* has to be allocated a logical block in size, (*dirent) struct dirent length
*/
DPRINTF(FIDS, ("read_fid_stream called at offset %"PRIu64"\n", *offset));
/* check if we're past the end of the directory */
if (fe) {
file_size = udf_rw64(fe->inf_len);
} else {
assert(dir_node->efe);
file_size = udf_rw64(efe->inf_len);
}
if (*offset >= file_size)
return EINVAL;
/* get maximum length of FID descriptor */
lb_size = udf_rw32(ump->logical_vol->lb_size);
DPRINTF(FIDS, ("\tfid piece read in fine\n"));
/*
* Check if we got a whole descriptor.
* TODO Try to `resync' directory stream when something is very wrong.
*/
/* check if our FID header is OK */
error = udf_check_tag(fid);
if (error) {
goto brokendir;
}
DPRINTF(FIDS, ("\ttag check ok\n"));
/* Fill the rbtree with nodes to sync. */
vfs_vnode_iterator_init(ump->vfs_mountp, &marker);
while ((vp = vfs_vnode_iterator_next(marker,
udf_sync_selector, NULL)) != NULL) {
udf_node = VTOI(vp);
udf_node->i_flags |= IN_SYNCED;
rb_tree_insert_node(&ump->udf_node_tree, udf_node);
}
vfs_vnode_iterator_destroy(marker);
dummy = 0;
DPRINTF(CALL, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
DPRINTF(SYNC, ("issue VOP_FSYNC(DATA only) on all nodes\n"));
udf_sync_pass(ump, cred, 1, &dummy);
DPRINTF(CALL, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
DPRINTF(SYNC, ("issue VOP_FSYNC(COMPLETE) on all finished nodes\n"));
udf_sync_pass(ump, cred, 2, &dummy);
if (waitfor == MNT_WAIT) {
recount:
ndirty = ump->devvp->v_numoutput;
DPRINTF(SYNC, ("counting pending blocks: on devvp %d\n",
ndirty));
udf_sync_pass(ump, cred, 3, &ndirty);
DPRINTF(SYNC, ("counted num dirty pending blocks %d\n",
ndirty));
if (ndirty) {
/* 1/4 second wait */
kpause("udfsync2", false, hz/4, NULL);
goto recount;
}
}
/*
* Read and write file extent in/from the buffer.
*
* The splitup of the extent into separate request-buffers is to minimise
* copying around as much as possible.
*
* block based file reading and writing
*/
/* request read-in of data from disc scheduler */
buf->b_resid = buf->b_bcount;
for (sector = 0; sector < sectors; sector++) {
buf_offset = sector * sector_size;
buf_pos = (uint8_t *) buf->b_data + buf_offset;
DPRINTF(READ, ("\tprocessing rel sector %d\n", sector));
/* check if its zero or unmapped to stop reading */
switch (mapping[sector]) {
case UDF_TRANS_UNMAPPED:
case UDF_TRANS_ZERO:
/* copy zero sector TODO runlength like below */
memset(buf_pos, 0, sector_size);
DPRINTF(READ, ("\treturning zero sector\n"));
nestiobuf_done(buf, sector_size, 0);
break;
default :
DPRINTF(READ, ("\tread sector "
"%"PRIu64"\n", mapping[sector]));
lblkno = from + sector;
run_start = mapping[sector];
run_length = 1;
while (sector < sectors-1) {
if (mapping[sector+1] != mapping[sector]+1)
break;
run_length++;
sector++;
}
/*
* nest an iobuf and mark it for async reading. Since
* we're using nested buffers, they can't be cached by
* design.
*/
rbuflen = run_length * sector_size;
rblk = run_start * (sector_size/DEV_BSIZE);
/* if its internally mapped, we can write it in the descriptor itself */
if (*mapping == UDF_TRANS_INTERN) {
/* TODO paranoia check if we ARE going to have enough space */
error = udf_write_internal(udf_node, (uint8_t *) buf->b_data);
if (error)
buf->b_error = error;
biodone(buf);
goto out;
}
DPRINTF(WRITE, ("\tnot intern\n"));
/* request write out of data to disc scheduler */
buf->b_resid = buf->b_bcount;
for (lb_num = 0; lb_num < num_lb; lb_num++) {
buf_offset = lb_num * lb_size;
DPRINTF(WRITE, ("\tprocessing rel lb_num %d\n", lb_num));
/*
* Mappings are not that important here. Just before we write
* the lb_num we late-allocate them when needed and update the
* mapping in the udf_node.
*/
/* XXX why not ignore the mapping altogether ? */
DPRINTF(WRITE, ("\twrite lb_num "
"%"PRIu64, mapping[lb_num]));
lblkno = from + lb_num;
run_start = mapping[lb_num];
run_length = 1;
while (lb_num < num_lb-1) {
if (mapping[lb_num+1] != mapping[lb_num]+1)
if (mapping[lb_num+1] != mapping[lb_num])
break;
run_length++;
lb_num++;
}
DPRINTF(WRITE, ("+ %d\n", run_length));
/* nest an iobuf on the master buffer for the extent */
rbuflen = run_length * lb_size;
rblk = run_start * (lb_size/DEV_BSIZE);