Home | History | Annotate | Line # | Download | only in newfs_udf
newfs_udf.c revision 1.12.4.1
      1  1.12.4.1     yamt /* $NetBSD: newfs_udf.c,v 1.12.4.1 2014/05/22 11:37:31 yamt Exp $ */
      2       1.1  reinoud 
      3       1.1  reinoud /*
      4  1.12.4.1     yamt  * Copyright (c) 2006, 2008, 2013 Reinoud Zandijk
      5       1.1  reinoud  * All rights reserved.
      6       1.1  reinoud  *
      7       1.1  reinoud  * Redistribution and use in source and binary forms, with or without
      8       1.1  reinoud  * modification, are permitted provided that the following conditions
      9       1.1  reinoud  * are met:
     10       1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     11       1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     12       1.1  reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  reinoud  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  reinoud  *    documentation and/or other materials provided with the distribution.
     15       1.1  reinoud  *
     16       1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17       1.1  reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18       1.1  reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19       1.1  reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20       1.1  reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21       1.1  reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22       1.1  reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23       1.1  reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24       1.1  reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25       1.1  reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1  reinoud  *
     27       1.1  reinoud  */
     28       1.1  reinoud 
     29       1.1  reinoud /*
     30       1.1  reinoud  * TODO
     31       1.8  reinoud  * - implement metadata formatting for BD-R
     32       1.1  reinoud  * - implement support for a read-only companion partition?
     33       1.1  reinoud  */
     34       1.1  reinoud 
     35       1.1  reinoud #define _EXPOSE_MMC
     36       1.1  reinoud #if 0
     37       1.1  reinoud # define DEBUG
     38       1.1  reinoud #endif
     39       1.1  reinoud 
     40       1.1  reinoud #include <stdio.h>
     41       1.1  reinoud #include <stdlib.h>
     42       1.1  reinoud #include <dirent.h>
     43       1.1  reinoud #include <inttypes.h>
     44       1.1  reinoud #include <stdint.h>
     45       1.1  reinoud #include <string.h>
     46       1.1  reinoud #include <errno.h>
     47       1.1  reinoud #include <fcntl.h>
     48       1.1  reinoud #include <unistd.h>
     49       1.1  reinoud #include <util.h>
     50       1.1  reinoud #include <time.h>
     51       1.1  reinoud #include <assert.h>
     52       1.3  reinoud #include <err.h>
     53       1.1  reinoud 
     54       1.1  reinoud #include <sys/ioctl.h>
     55       1.1  reinoud #include <sys/stat.h>
     56       1.1  reinoud #include <sys/types.h>
     57       1.1  reinoud #include <sys/cdio.h>
     58       1.1  reinoud #include <sys/disklabel.h>
     59       1.1  reinoud #include <sys/dkio.h>
     60       1.1  reinoud #include <sys/param.h>
     61       1.1  reinoud #include <sys/queue.h>
     62       1.1  reinoud 
     63       1.1  reinoud #include <fs/udf/ecma167-udf.h>
     64       1.1  reinoud #include <fs/udf/udf_mount.h>
     65       1.5    pooka 
     66       1.5    pooka #include "mountprog.h"
     67       1.1  reinoud #include "udf_create.h"
     68  1.12.4.1     yamt #include "udf_write.h"
     69  1.12.4.1     yamt #include "newfs_udf.h"
     70       1.1  reinoud 
     71       1.1  reinoud /* prototypes */
     72       1.1  reinoud int newfs_udf(int argc, char **argv);
     73       1.1  reinoud static void usage(void) __attribute__((__noreturn__));
     74       1.1  reinoud 
     75  1.12.4.1     yamt 
     76  1.12.4.1     yamt /* queue for temporary storage of sectors to be written out */
     77  1.12.4.1     yamt struct wrsect {
     78  1.12.4.1     yamt 	uint64_t  sectornr;
     79  1.12.4.1     yamt 	uint8_t	 *sector_data;
     80  1.12.4.1     yamt 	TAILQ_ENTRY(wrsect) next;
     81  1.12.4.1     yamt };
     82  1.12.4.1     yamt 
     83  1.12.4.1     yamt /* write queue and track blocking skew */
     84  1.12.4.1     yamt TAILQ_HEAD(wrsect_list, wrsect) write_queue;
     85       1.1  reinoud 
     86       1.1  reinoud 
     87       1.1  reinoud /* global variables describing disc and format requests */
     88       1.1  reinoud int	 fd;				/* device: file descriptor */
     89       1.1  reinoud char	*dev;				/* device: name		   */
     90       1.1  reinoud struct mmc_discinfo mmc_discinfo;	/* device: disc info	   */
     91       1.1  reinoud 
     92       1.1  reinoud char	*format_str;			/* format: string representation */
     93       1.1  reinoud int	 format_flags;			/* format: attribute flags	 */
     94       1.1  reinoud int	 media_accesstype;		/* derived from current mmc cap  */
     95       1.1  reinoud int	 check_surface;			/* for rewritables               */
     96  1.12.4.1     yamt int	 imagefile_secsize;		/* for files			 */
     97  1.12.4.1     yamt int	 emul_packetsize;		/* for discs and files		 */
     98       1.1  reinoud 
     99       1.1  reinoud int	 wrtrack_skew;
    100       1.4  reinoud int	 meta_perc = UDF_META_PERC;
    101       1.4  reinoud float	 meta_fract = (float) UDF_META_PERC / 100.0;
    102       1.1  reinoud 
    103       1.1  reinoud 
    104       1.1  reinoud /* --------------------------------------------------------------------- */
    105       1.1  reinoud 
    106       1.1  reinoud /*
    107       1.1  reinoud  * write queue implementation
    108       1.1  reinoud  */
    109       1.1  reinoud 
    110  1.12.4.1     yamt int
    111  1.12.4.1     yamt udf_write_sector(void *sector, uint64_t location)
    112       1.1  reinoud {
    113       1.1  reinoud 	struct wrsect *pos, *seekpos;
    114       1.1  reinoud 
    115       1.1  reinoud 
    116       1.1  reinoud 	/* search location */
    117       1.1  reinoud 	TAILQ_FOREACH_REVERSE(seekpos, &write_queue, wrsect_list, next) {
    118       1.1  reinoud 		if (seekpos->sectornr <= location)
    119       1.1  reinoud 			break;
    120       1.1  reinoud 	}
    121       1.1  reinoud 	if ((seekpos == NULL) || (seekpos->sectornr != location)) {
    122       1.1  reinoud 		pos = calloc(1, sizeof(struct wrsect));
    123       1.1  reinoud 		if (pos == NULL)
    124       1.1  reinoud 			return ENOMEM;
    125       1.1  reinoud 		/* allocate space for copy of sector data */
    126       1.1  reinoud 		pos->sector_data = calloc(1, context.sector_size);
    127       1.1  reinoud 		if (pos->sector_data == NULL)
    128       1.1  reinoud 			return ENOMEM;
    129       1.1  reinoud 		pos->sectornr = location;
    130       1.1  reinoud 
    131       1.1  reinoud 		if (seekpos) {
    132       1.1  reinoud 			TAILQ_INSERT_AFTER(&write_queue, seekpos, pos, next);
    133       1.1  reinoud 		} else {
    134       1.1  reinoud 			TAILQ_INSERT_HEAD(&write_queue, pos, next);
    135       1.1  reinoud 		}
    136       1.1  reinoud 	} else {
    137       1.1  reinoud 		pos = seekpos;
    138       1.1  reinoud 	}
    139       1.1  reinoud 	memcpy(pos->sector_data, sector, context.sector_size);
    140       1.1  reinoud 
    141       1.1  reinoud 	return 0;
    142       1.1  reinoud }
    143       1.1  reinoud 
    144       1.1  reinoud 
    145       1.1  reinoud /*
    146       1.1  reinoud  * Now all write requests are queued in the TAILQ, write them out to the
    147       1.1  reinoud  * disc/file image. Special care needs to be taken for devices that are only
    148       1.1  reinoud  * strict overwritable i.e. only in packet size chunks
    149       1.1  reinoud  *
    150       1.1  reinoud  * XXX support for growing vnd?
    151       1.1  reinoud  */
    152       1.1  reinoud 
    153  1.12.4.1     yamt int
    154       1.1  reinoud writeout_write_queue(void)
    155       1.1  reinoud {
    156       1.1  reinoud 	struct wrsect *pos;
    157       1.1  reinoud 	uint64_t offset;
    158  1.12.4.1     yamt 	uint64_t line_start, new_line_start;
    159  1.12.4.1     yamt 	uint32_t line_len, line_offset, relpos;
    160       1.1  reinoud 	uint32_t blockingnr;
    161       1.1  reinoud 	uint8_t *linebuf, *adr;
    162       1.1  reinoud 
    163       1.1  reinoud 	blockingnr  = layout.blockingnr;
    164       1.1  reinoud 	line_len    = blockingnr   * context.sector_size;
    165       1.1  reinoud 	line_offset = wrtrack_skew * context.sector_size;
    166       1.1  reinoud 
    167       1.1  reinoud 	linebuf     = malloc(line_len);
    168       1.1  reinoud 	if (linebuf == NULL)
    169       1.1  reinoud 		return ENOMEM;
    170       1.1  reinoud 
    171       1.1  reinoud 	pos = TAILQ_FIRST(&write_queue);
    172       1.1  reinoud 	bzero(linebuf, line_len);
    173       1.1  reinoud 
    174       1.1  reinoud 	/*
    175       1.1  reinoud 	 * Always writing out in whole lines now; this is slightly wastefull
    176       1.1  reinoud 	 * on logical overwrite volumes but it reduces complexity and the loss
    177       1.1  reinoud 	 * is near zero compared to disc size.
    178       1.1  reinoud 	 */
    179       1.1  reinoud 	line_start = (pos->sectornr - wrtrack_skew) / blockingnr;
    180       1.1  reinoud 	TAILQ_FOREACH(pos, &write_queue, next) {
    181       1.1  reinoud 		new_line_start = (pos->sectornr - wrtrack_skew) / blockingnr;
    182       1.1  reinoud 		if (new_line_start != line_start) {
    183       1.1  reinoud 			/* write out */
    184       1.1  reinoud 			offset = (uint64_t) line_start * line_len + line_offset;
    185       1.1  reinoud #ifdef DEBUG
    186       1.1  reinoud 			printf("WRITEOUT %08"PRIu64" + %02d -- "
    187       1.1  reinoud 				"[%08"PRIu64"..%08"PRIu64"]\n",
    188       1.1  reinoud 				offset / context.sector_size, blockingnr,
    189       1.1  reinoud 				offset / context.sector_size,
    190       1.1  reinoud 				offset / context.sector_size + blockingnr-1);
    191       1.1  reinoud #endif
    192       1.1  reinoud 			if (pwrite(fd, linebuf, line_len, offset) < 0) {
    193       1.1  reinoud 				perror("Writing failed");
    194       1.1  reinoud 				return errno;
    195       1.1  reinoud 			}
    196       1.1  reinoud 			line_start = new_line_start;
    197       1.1  reinoud 			bzero(linebuf, line_len);
    198       1.1  reinoud 		}
    199       1.1  reinoud 
    200       1.1  reinoud 		relpos = (pos->sectornr - wrtrack_skew) % blockingnr;
    201       1.1  reinoud 		adr = linebuf + relpos * context.sector_size;
    202       1.1  reinoud 		memcpy(adr, pos->sector_data, context.sector_size);
    203       1.1  reinoud 	}
    204       1.1  reinoud 	/* writeout last chunk */
    205       1.1  reinoud 	offset = (uint64_t) line_start * line_len + line_offset;
    206       1.1  reinoud #ifdef DEBUG
    207       1.1  reinoud 	printf("WRITEOUT %08"PRIu64" + %02d -- [%08"PRIu64"..%08"PRIu64"]\n",
    208       1.1  reinoud 		offset / context.sector_size, blockingnr,
    209       1.1  reinoud 		offset / context.sector_size,
    210       1.1  reinoud 		offset / context.sector_size + blockingnr-1);
    211       1.1  reinoud #endif
    212       1.1  reinoud 	if (pwrite(fd, linebuf, line_len, offset) < 0) {
    213       1.1  reinoud 		perror("Writing failed");
    214       1.1  reinoud 		return errno;
    215       1.1  reinoud 	}
    216       1.1  reinoud 
    217       1.1  reinoud 	/* success */
    218       1.1  reinoud 	return 0;
    219       1.1  reinoud }
    220       1.1  reinoud 
    221       1.1  reinoud /* --------------------------------------------------------------------- */
    222       1.1  reinoud 
    223       1.1  reinoud /*
    224       1.1  reinoud  * mmc_discinfo and mmc_trackinfo readers modified from origional in udf main
    225       1.1  reinoud  * code in sys/fs/udf/
    226       1.1  reinoud  */
    227       1.1  reinoud 
    228       1.1  reinoud #ifdef DEBUG
    229       1.1  reinoud static void
    230       1.1  reinoud udf_dump_discinfo(struct mmc_discinfo *di)
    231       1.1  reinoud {
    232       1.1  reinoud 	char bits[128];
    233       1.1  reinoud 
    234       1.1  reinoud 	printf("Device/media info  :\n");
    235       1.1  reinoud 	printf("\tMMC profile        0x%02x\n", di->mmc_profile);
    236       1.1  reinoud 	printf("\tderived class      %d\n", di->mmc_class);
    237       1.1  reinoud 	printf("\tsector size        %d\n", di->sector_size);
    238       1.1  reinoud 	printf("\tdisc state         %d\n", di->disc_state);
    239       1.1  reinoud 	printf("\tlast ses state     %d\n", di->last_session_state);
    240       1.1  reinoud 	printf("\tbg format state    %d\n", di->bg_format_state);
    241       1.1  reinoud 	printf("\tfrst track         %d\n", di->first_track);
    242       1.1  reinoud 	printf("\tfst on last ses    %d\n", di->first_track_last_session);
    243       1.1  reinoud 	printf("\tlst on last ses    %d\n", di->last_track_last_session);
    244       1.1  reinoud 	printf("\tlink block penalty %d\n", di->link_block_penalty);
    245       1.1  reinoud 	snprintb(bits, sizeof(bits), MMC_DFLAGS_FLAGBITS, (uint64_t) di->disc_flags);
    246       1.1  reinoud 	printf("\tdisc flags         %s\n", bits);
    247       1.1  reinoud 	printf("\tdisc id            %x\n", di->disc_id);
    248       1.1  reinoud 	printf("\tdisc barcode       %"PRIx64"\n", di->disc_barcode);
    249       1.1  reinoud 
    250       1.1  reinoud 	printf("\tnum sessions       %d\n", di->num_sessions);
    251       1.1  reinoud 	printf("\tnum tracks         %d\n", di->num_tracks);
    252       1.1  reinoud 
    253       1.1  reinoud 	snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cur);
    254       1.1  reinoud 	printf("\tcapabilities cur   %s\n", bits);
    255       1.1  reinoud 	snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cap);
    256       1.1  reinoud 	printf("\tcapabilities cap   %s\n", bits);
    257       1.1  reinoud 	printf("\n");
    258       1.1  reinoud 	printf("\tlast_possible_lba  %d\n", di->last_possible_lba);
    259       1.1  reinoud 	printf("\n");
    260       1.1  reinoud }
    261       1.1  reinoud #else
    262       1.1  reinoud #define udf_dump_discinfo(a);
    263       1.1  reinoud #endif
    264       1.1  reinoud 
    265       1.1  reinoud /* --------------------------------------------------------------------- */
    266       1.1  reinoud 
    267       1.1  reinoud static int
    268       1.1  reinoud udf_update_discinfo(struct mmc_discinfo *di)
    269       1.1  reinoud {
    270  1.12.4.1     yamt 	struct stat st;
    271       1.1  reinoud 	struct disklabel  disklab;
    272       1.1  reinoud 	struct partition *dp;
    273  1.12.4.1     yamt 	off_t size, sectors, secsize;
    274       1.1  reinoud 	int partnr, error;
    275       1.1  reinoud 
    276       1.1  reinoud 	memset(di, 0, sizeof(struct mmc_discinfo));
    277       1.1  reinoud 
    278       1.1  reinoud 	/* check if we're on a MMC capable device, i.e. CD/DVD */
    279       1.1  reinoud 	error = ioctl(fd, MMCGETDISCINFO, di);
    280       1.1  reinoud 	if (error == 0)
    281       1.1  reinoud 		return 0;
    282       1.1  reinoud 
    283  1.12.4.1     yamt 	/* (re)fstat the file */
    284       1.1  reinoud 	fstat(fd, &st);
    285  1.12.4.1     yamt 
    286  1.12.4.1     yamt 	if (S_ISREG(st.st_mode)) {
    287  1.12.4.1     yamt 		/* file support; we pick the minimum sector size allowed */
    288  1.12.4.1     yamt 		size = st.st_size;
    289  1.12.4.1     yamt 		secsize = imagefile_secsize;
    290  1.12.4.1     yamt 		sectors = size / secsize;
    291  1.12.4.1     yamt 	} else {
    292  1.12.4.1     yamt 		/*
    293  1.12.4.1     yamt 		 * disc partition support; note we can't use DIOCGPART in
    294  1.12.4.1     yamt 		 * userland so get disc label and use the stat info to get the
    295  1.12.4.1     yamt 		 * partition number.
    296  1.12.4.1     yamt 		 */
    297  1.12.4.1     yamt 		if (ioctl(fd, DIOCGDINFO, &disklab) == -1) {
    298  1.12.4.1     yamt 			/* failed to get disclabel! */
    299  1.12.4.1     yamt 			perror("disklabel");
    300  1.12.4.1     yamt 			return errno;
    301  1.12.4.1     yamt 		}
    302  1.12.4.1     yamt 
    303  1.12.4.1     yamt 		/* get disk partition it refers to */
    304  1.12.4.1     yamt 		fstat(fd, &st);
    305  1.12.4.1     yamt 		partnr = DISKPART(st.st_rdev);
    306  1.12.4.1     yamt 		dp = &disklab.d_partitions[partnr];
    307  1.12.4.1     yamt 
    308  1.12.4.1     yamt 		/* TODO problem with last_possible_lba on resizable VND */
    309  1.12.4.1     yamt 		if (dp->p_size == 0) {
    310  1.12.4.1     yamt 			perror("faulty disklabel partition returned, "
    311  1.12.4.1     yamt 				"check label\n");
    312  1.12.4.1     yamt 			return EIO;
    313  1.12.4.1     yamt 		}
    314  1.12.4.1     yamt 
    315  1.12.4.1     yamt 		sectors = dp->p_size;
    316  1.12.4.1     yamt 		secsize = disklab.d_secsize;
    317  1.12.4.1     yamt 	}
    318       1.1  reinoud 
    319       1.1  reinoud 	/* set up a disc info profile for partitions */
    320       1.1  reinoud 	di->mmc_profile		= 0x01;	/* disc type */
    321       1.1  reinoud 	di->mmc_class		= MMC_CLASS_DISC;
    322       1.1  reinoud 	di->disc_state		= MMC_STATE_CLOSED;
    323       1.1  reinoud 	di->last_session_state	= MMC_STATE_CLOSED;
    324       1.1  reinoud 	di->bg_format_state	= MMC_BGFSTATE_COMPLETED;
    325       1.1  reinoud 	di->link_block_penalty	= 0;
    326       1.1  reinoud 
    327       1.1  reinoud 	di->mmc_cur     = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE |
    328       1.1  reinoud 		MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE;
    329       1.1  reinoud 	di->mmc_cap    = di->mmc_cur;
    330       1.1  reinoud 	di->disc_flags = MMC_DFLAGS_UNRESTRICTED;
    331       1.1  reinoud 
    332  1.12.4.1     yamt 	di->last_possible_lba = sectors - 1;
    333  1.12.4.1     yamt 	di->sector_size       = secsize;
    334       1.1  reinoud 
    335       1.1  reinoud 	di->num_sessions = 1;
    336       1.1  reinoud 	di->num_tracks   = 1;
    337       1.1  reinoud 
    338       1.1  reinoud 	di->first_track  = 1;
    339       1.1  reinoud 	di->first_track_last_session = di->last_track_last_session = 1;
    340       1.1  reinoud 
    341       1.1  reinoud 	return 0;
    342       1.1  reinoud }
    343       1.1  reinoud 
    344       1.1  reinoud 
    345  1.12.4.1     yamt int
    346       1.1  reinoud udf_update_trackinfo(struct mmc_discinfo *di, struct mmc_trackinfo *ti)
    347       1.1  reinoud {
    348       1.1  reinoud 	int error, class;
    349       1.1  reinoud 
    350       1.1  reinoud 	class = di->mmc_class;
    351       1.1  reinoud 	if (class != MMC_CLASS_DISC) {
    352       1.1  reinoud 		/* tracknr specified in struct ti */
    353       1.1  reinoud 		error = ioctl(fd, MMCGETTRACKINFO, ti);
    354       1.1  reinoud 		return error;
    355       1.1  reinoud 	}
    356       1.1  reinoud 
    357       1.1  reinoud 	/* discs partition support */
    358       1.1  reinoud 	if (ti->tracknr != 1)
    359       1.1  reinoud 		return EIO;
    360       1.1  reinoud 
    361       1.1  reinoud 	/* create fake ti (TODO check for resized vnds) */
    362       1.1  reinoud 	ti->sessionnr  = 1;
    363       1.1  reinoud 
    364       1.1  reinoud 	ti->track_mode = 0;	/* XXX */
    365       1.1  reinoud 	ti->data_mode  = 0;	/* XXX */
    366       1.1  reinoud 	ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID;
    367       1.1  reinoud 
    368       1.1  reinoud 	ti->track_start    = 0;
    369  1.12.4.1     yamt 	ti->packet_size    = emul_packetsize;
    370       1.1  reinoud 
    371       1.1  reinoud 	/* TODO support for resizable vnd */
    372       1.1  reinoud 	ti->track_size    = di->last_possible_lba;
    373       1.1  reinoud 	ti->next_writable = di->last_possible_lba;
    374       1.1  reinoud 	ti->last_recorded = ti->next_writable;
    375       1.1  reinoud 	ti->free_blocks   = 0;
    376       1.1  reinoud 
    377       1.1  reinoud 	return 0;
    378       1.1  reinoud }
    379       1.1  reinoud 
    380       1.1  reinoud 
    381       1.1  reinoud static int
    382       1.1  reinoud udf_setup_writeparams(struct mmc_discinfo *di)
    383       1.1  reinoud {
    384       1.1  reinoud 	struct mmc_writeparams mmc_writeparams;
    385       1.1  reinoud 	int error;
    386       1.1  reinoud 
    387       1.1  reinoud 	if (di->mmc_class == MMC_CLASS_DISC)
    388       1.1  reinoud 		return 0;
    389       1.1  reinoud 
    390       1.1  reinoud 	/*
    391       1.1  reinoud 	 * only CD burning normally needs setting up, but other disc types
    392       1.1  reinoud 	 * might need other settings to be made. The MMC framework will set up
    393       1.1  reinoud 	 * the nessisary recording parameters according to the disc
    394       1.1  reinoud 	 * characteristics read in. Modifications can be made in the discinfo
    395       1.1  reinoud 	 * structure passed to change the nature of the disc.
    396       1.1  reinoud 	 */
    397       1.1  reinoud 	memset(&mmc_writeparams, 0, sizeof(struct mmc_writeparams));
    398       1.1  reinoud 	mmc_writeparams.mmc_class  = di->mmc_class;
    399       1.1  reinoud 	mmc_writeparams.mmc_cur    = di->mmc_cur;
    400       1.1  reinoud 
    401       1.1  reinoud 	/*
    402       1.1  reinoud 	 * UDF dictates first track to determine track mode for the whole
    403       1.1  reinoud 	 * disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1]
    404       1.1  reinoud 	 * To prevent problems with a `reserved' track in front we start with
    405       1.1  reinoud 	 * the 2nd track and if that is not valid, go for the 1st.
    406       1.1  reinoud 	 */
    407       1.1  reinoud 	mmc_writeparams.tracknr = 2;
    408       1.1  reinoud 	mmc_writeparams.data_mode  = MMC_DATAMODE_DEFAULT;	/* XA disc */
    409       1.1  reinoud 	mmc_writeparams.track_mode = MMC_TRACKMODE_DEFAULT;	/* data */
    410       1.1  reinoud 
    411       1.1  reinoud 	error = ioctl(fd, MMCSETUPWRITEPARAMS, &mmc_writeparams);
    412       1.1  reinoud 	if (error) {
    413       1.1  reinoud 		mmc_writeparams.tracknr = 1;
    414       1.1  reinoud 		error = ioctl(fd, MMCSETUPWRITEPARAMS, &mmc_writeparams);
    415       1.1  reinoud 	}
    416       1.1  reinoud 	return error;
    417       1.1  reinoud }
    418       1.1  reinoud 
    419       1.1  reinoud 
    420       1.1  reinoud static void
    421       1.1  reinoud udf_synchronise_caches(void)
    422       1.1  reinoud {
    423       1.1  reinoud 	struct mmc_op mmc_op;
    424       1.1  reinoud 
    425       1.1  reinoud 	bzero(&mmc_op, sizeof(struct mmc_op));
    426       1.1  reinoud 	mmc_op.operation = MMC_OP_SYNCHRONISECACHE;
    427       1.1  reinoud 
    428       1.1  reinoud 	/* this device might not know this ioct, so just be ignorant */
    429       1.1  reinoud 	(void) ioctl(fd, MMCOP, &mmc_op);
    430       1.1  reinoud }
    431       1.1  reinoud 
    432       1.1  reinoud /* --------------------------------------------------------------------- */
    433       1.1  reinoud 
    434       1.1  reinoud static int
    435       1.1  reinoud udf_prepare_disc(void)
    436       1.1  reinoud {
    437       1.1  reinoud 	struct mmc_trackinfo ti;
    438       1.1  reinoud 	struct mmc_op        op;
    439       1.1  reinoud 	int tracknr, error;
    440       1.1  reinoud 
    441       1.1  reinoud 	/* If the last track is damaged, repair it */
    442       1.1  reinoud 	ti.tracknr = mmc_discinfo.last_track_last_session;
    443       1.1  reinoud 	error = udf_update_trackinfo(&mmc_discinfo, &ti);
    444       1.1  reinoud 	if (error)
    445       1.1  reinoud 		return error;
    446       1.1  reinoud 
    447       1.1  reinoud 	if (ti.flags & MMC_TRACKINFO_DAMAGED) {
    448       1.1  reinoud 		/*
    449       1.1  reinoud 		 * Need to repair last track before anything can be done.
    450       1.1  reinoud 		 * this is an optional command, so ignore its error but report
    451       1.1  reinoud 		 * warning.
    452       1.1  reinoud 		 */
    453       1.1  reinoud 		memset(&op, 0, sizeof(op));
    454       1.1  reinoud 		op.operation   = MMC_OP_REPAIRTRACK;
    455       1.1  reinoud 		op.mmc_profile = mmc_discinfo.mmc_profile;
    456       1.1  reinoud 		op.tracknr     = ti.tracknr;
    457       1.1  reinoud 		error = ioctl(fd, MMCOP, &op);
    458       1.1  reinoud 
    459       1.1  reinoud 		if (error)
    460       1.1  reinoud 			(void)printf("Drive can't explicitly repair last "
    461       1.1  reinoud 				"damaged track, but it might autorepair\n");
    462       1.1  reinoud 	}
    463       1.1  reinoud 	/* last track (if any) might not be damaged now, operations are ok now */
    464       1.1  reinoud 
    465       1.1  reinoud 	/* setup write parameters from discinfo */
    466       1.1  reinoud 	error = udf_setup_writeparams(&mmc_discinfo);
    467       1.1  reinoud 	if (error)
    468       1.1  reinoud 		return error;
    469       1.1  reinoud 
    470       1.1  reinoud 	/* if the drive is not sequential, we're done */
    471       1.1  reinoud 	if ((mmc_discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) == 0)
    472       1.1  reinoud 		return 0;
    473       1.1  reinoud 
    474       1.1  reinoud #ifdef notyet
    475       1.1  reinoud 	/* if last track is not the reserved but an empty track, unreserve it */
    476       1.1  reinoud 	if (ti.flags & MMC_TRACKINFO_BLANK) {
    477       1.1  reinoud 		if (ti.flags & MMC_TRACKINFO_RESERVED == 0) {
    478       1.1  reinoud 			memset(&op, 0, sizeof(op));
    479       1.1  reinoud 			op.operation   = MMC_OP_UNRESERVETRACK;
    480       1.1  reinoud 			op.mmc_profile = mmc_discinfo.mmc_profile;
    481       1.1  reinoud 			op.tracknr     = ti.tracknr;
    482       1.1  reinoud 			error = ioctl(fd, MMCOP, &op);
    483       1.1  reinoud 			if (error)
    484       1.1  reinoud 				return error;
    485       1.1  reinoud 
    486       1.1  reinoud 			/* update discinfo since it changed by the operation */
    487       1.1  reinoud 			error = udf_update_discinfo(&mmc_discinfo);
    488       1.1  reinoud 			if (error)
    489       1.1  reinoud 				return error;
    490       1.1  reinoud 		}
    491       1.1  reinoud 	}
    492       1.1  reinoud #endif
    493       1.1  reinoud 
    494       1.1  reinoud 	/* close the last session if its still open */
    495       1.1  reinoud 	if (mmc_discinfo.last_session_state == MMC_STATE_INCOMPLETE) {
    496       1.1  reinoud 		printf("Closing last open session if present\n");
    497       1.1  reinoud 		/* close all associated tracks */
    498       1.1  reinoud 		tracknr = mmc_discinfo.first_track_last_session;
    499       1.1  reinoud 		while (tracknr <= mmc_discinfo.last_track_last_session) {
    500       1.1  reinoud 			ti.tracknr = tracknr;
    501       1.1  reinoud 			error = udf_update_trackinfo(&mmc_discinfo, &ti);
    502       1.1  reinoud 			if (error)
    503       1.1  reinoud 				return error;
    504       1.1  reinoud 			printf("\tClosing open track %d\n", tracknr);
    505       1.1  reinoud 			memset(&op, 0, sizeof(op));
    506       1.1  reinoud 			op.operation   = MMC_OP_CLOSETRACK;
    507       1.1  reinoud 			op.mmc_profile = mmc_discinfo.mmc_profile;
    508       1.1  reinoud 			op.tracknr     = tracknr;
    509       1.1  reinoud 			error = ioctl(fd, MMCOP, &op);
    510       1.1  reinoud 			if (error)
    511       1.1  reinoud 				return error;
    512       1.1  reinoud 			tracknr ++;
    513       1.1  reinoud 		}
    514       1.1  reinoud 		printf("Closing session\n");
    515       1.1  reinoud 		memset(&op, 0, sizeof(op));
    516       1.1  reinoud 		op.operation   = MMC_OP_CLOSESESSION;
    517       1.1  reinoud 		op.mmc_profile = mmc_discinfo.mmc_profile;
    518       1.1  reinoud 		op.sessionnr   = mmc_discinfo.num_sessions;
    519       1.1  reinoud 		error = ioctl(fd, MMCOP, &op);
    520       1.1  reinoud 		if (error)
    521       1.1  reinoud 			return error;
    522       1.1  reinoud 
    523       1.1  reinoud 		/* update discinfo since it changed by the operations */
    524       1.1  reinoud 		error = udf_update_discinfo(&mmc_discinfo);
    525       1.1  reinoud 		if (error)
    526       1.1  reinoud 			return error;
    527       1.1  reinoud 	}
    528       1.1  reinoud 
    529       1.1  reinoud 	if (format_flags & FORMAT_TRACK512) {
    530       1.1  reinoud 		/* get last track again */
    531       1.1  reinoud 		ti.tracknr = mmc_discinfo.last_track_last_session;
    532       1.1  reinoud 		error = udf_update_trackinfo(&mmc_discinfo, &ti);
    533       1.1  reinoud 		if (error)
    534       1.1  reinoud 			return error;
    535       1.1  reinoud 
    536       1.1  reinoud 		/* Split up the space at 512 for iso cd9660 hooking */
    537       1.1  reinoud 		memset(&op, 0, sizeof(op));
    538       1.1  reinoud 		op.operation   = MMC_OP_RESERVETRACK_NWA;	/* UPTO nwa */
    539       1.1  reinoud 		op.mmc_profile = mmc_discinfo.mmc_profile;
    540       1.1  reinoud 		op.extent      = 512;				/* size */
    541       1.1  reinoud 		error = ioctl(fd, MMCOP, &op);
    542       1.1  reinoud 		if (error)
    543       1.1  reinoud 			return error;
    544       1.1  reinoud 	}
    545       1.1  reinoud 
    546       1.1  reinoud 	return 0;
    547       1.1  reinoud }
    548       1.1  reinoud 
    549       1.1  reinoud /* --------------------------------------------------------------------- */
    550       1.1  reinoud 
    551  1.12.4.1     yamt int
    552       1.1  reinoud udf_surface_check(void)
    553       1.1  reinoud {
    554       1.1  reinoud 	uint32_t loc, block_bytes;
    555       1.6    lukem 	uint32_t sector_size, blockingnr, bpos;
    556       1.1  reinoud 	uint8_t *buffer;
    557       1.1  reinoud 	int error, num_errors;
    558       1.1  reinoud 
    559       1.1  reinoud 	sector_size = context.sector_size;
    560       1.1  reinoud 	blockingnr  = layout.blockingnr;
    561       1.1  reinoud 
    562       1.1  reinoud 	block_bytes = layout.blockingnr * sector_size;
    563       1.1  reinoud 	if ((buffer = malloc(block_bytes)) == NULL)
    564       1.1  reinoud 		return ENOMEM;
    565       1.1  reinoud 
    566       1.1  reinoud 	/* set all one to not kill Flash memory? */
    567       1.1  reinoud 	for (bpos = 0; bpos < block_bytes; bpos++)
    568       1.1  reinoud 		buffer[bpos] = 0x00;
    569       1.1  reinoud 
    570       1.1  reinoud 	printf("\nChecking disc surface : phase 1 - writing\n");
    571       1.1  reinoud 	num_errors = 0;
    572       1.1  reinoud 	loc = layout.first_lba;
    573       1.1  reinoud 	while (loc <= layout.last_lba) {
    574       1.1  reinoud 		/* write blockingnr sectors */
    575       1.1  reinoud 		error = pwrite(fd, buffer, block_bytes, loc*sector_size);
    576       1.1  reinoud 		printf("   %08d + %d (%02d %%)\r", loc, blockingnr,
    577       1.1  reinoud 			(int)((100.0 * loc)/layout.last_lba));
    578       1.1  reinoud 		fflush(stdout);
    579       1.1  reinoud 		if (error == -1) {
    580       1.1  reinoud 			/* block is bad */
    581       1.1  reinoud 			printf("BAD block at %08d + %d         \n",
    582       1.1  reinoud 				loc, layout.blockingnr);
    583       1.9      wiz 			if ((error = udf_register_bad_block(loc))) {
    584       1.9      wiz 				free(buffer);
    585       1.1  reinoud 				return error;
    586       1.9      wiz 			}
    587       1.1  reinoud 			num_errors ++;
    588       1.1  reinoud 		}
    589       1.1  reinoud 		loc += layout.blockingnr;
    590       1.1  reinoud 	}
    591       1.1  reinoud 
    592       1.1  reinoud 	printf("\nChecking disc surface : phase 2 - reading\n");
    593       1.1  reinoud 	num_errors = 0;
    594       1.1  reinoud 	loc = layout.first_lba;
    595       1.1  reinoud 	while (loc <= layout.last_lba) {
    596       1.1  reinoud 		/* read blockingnr sectors */
    597       1.1  reinoud 		error = pread(fd, buffer, block_bytes, loc*sector_size);
    598       1.1  reinoud 		printf("   %08d + %d (%02d %%)\r", loc, blockingnr,
    599       1.1  reinoud 			(int)((100.0 * loc)/layout.last_lba));
    600       1.1  reinoud 		fflush(stdout);
    601       1.1  reinoud 		if (error == -1) {
    602       1.1  reinoud 			/* block is bad */
    603       1.1  reinoud 			printf("BAD block at %08d + %d         \n",
    604       1.1  reinoud 				loc, layout.blockingnr);
    605       1.9      wiz 			if ((error = udf_register_bad_block(loc))) {
    606       1.9      wiz 				free(buffer);
    607       1.1  reinoud 				return error;
    608       1.9      wiz 			}
    609       1.1  reinoud 			num_errors ++;
    610       1.1  reinoud 		}
    611       1.1  reinoud 		loc += layout.blockingnr;
    612       1.1  reinoud 	}
    613       1.1  reinoud 	printf("Scan complete : %d bad blocks found\n", num_errors);
    614       1.1  reinoud 	free(buffer);
    615       1.1  reinoud 
    616       1.1  reinoud 	return 0;
    617       1.1  reinoud }
    618       1.1  reinoud 
    619       1.1  reinoud 
    620       1.1  reinoud /* --------------------------------------------------------------------- */
    621       1.1  reinoud 
    622  1.12.4.1     yamt static int
    623       1.1  reinoud udf_do_newfs(void)
    624       1.1  reinoud {
    625  1.12.4.1     yamt 	int error;
    626       1.1  reinoud 
    627  1.12.4.1     yamt 	error = udf_do_newfs_prefix();
    628       1.1  reinoud 	if (error)
    629       1.1  reinoud 		return error;
    630  1.12.4.1     yamt 	error = udf_do_rootdir();
    631       1.1  reinoud 	if (error)
    632       1.1  reinoud 		return error;
    633  1.12.4.1     yamt 	error = udf_do_newfs_postfix();
    634       1.1  reinoud 
    635  1.12.4.1     yamt 	return error;
    636       1.3  reinoud }
    637       1.3  reinoud 
    638       1.3  reinoud 
    639       1.3  reinoud 
    640       1.3  reinoud /* --------------------------------------------------------------------- */
    641       1.3  reinoud 
    642       1.1  reinoud static void
    643       1.1  reinoud usage(void)
    644       1.1  reinoud {
    645       1.4  reinoud 	(void)fprintf(stderr, "Usage: %s [-cFM] [-L loglabel] "
    646  1.12.4.1     yamt 	    "[-P discid] [-S sectorsize] [-s size] [-p perc] "
    647       1.4  reinoud 	    "[-t gmtoff] [-v min_udf] [-V max_udf] special\n", getprogname());
    648       1.1  reinoud 	exit(EXIT_FAILURE);
    649       1.1  reinoud }
    650       1.1  reinoud 
    651       1.1  reinoud 
    652       1.1  reinoud int
    653       1.1  reinoud main(int argc, char **argv)
    654       1.1  reinoud {
    655       1.1  reinoud 	struct tm *tm;
    656       1.1  reinoud 	struct stat st;
    657       1.1  reinoud 	time_t now;
    658  1.12.4.1     yamt 	off_t setsize;
    659  1.12.4.1     yamt 	char  scrap[255], *colon;
    660       1.1  reinoud 	int ch, req_enable, req_disable, force;
    661       1.1  reinoud 	int error;
    662       1.1  reinoud 
    663       1.1  reinoud 	setprogname(argv[0]);
    664       1.1  reinoud 
    665       1.1  reinoud 	/* initialise */
    666       1.1  reinoud 	format_str    = strdup("");
    667       1.1  reinoud 	req_enable    = req_disable = 0;
    668       1.1  reinoud 	format_flags  = FORMAT_INVALID;
    669       1.1  reinoud 	force         = 0;
    670       1.1  reinoud 	check_surface = 0;
    671  1.12.4.1     yamt 	setsize       = 0;
    672  1.12.4.1     yamt 	imagefile_secsize = 512;	/* minimum allowed sector size */
    673  1.12.4.1     yamt 	emul_packetsize   = 32;		/* reasonable default */
    674       1.1  reinoud 
    675       1.1  reinoud 	srandom((unsigned long) time(NULL));
    676       1.1  reinoud 	udf_init_create_context();
    677  1.12.4.1     yamt 	context.app_name         = "*NetBSD newfs";
    678       1.1  reinoud 	context.app_version_main = APP_VERSION_MAIN;
    679       1.1  reinoud 	context.app_version_sub  = APP_VERSION_SUB;
    680  1.12.4.1     yamt 	context.impl_name        = IMPL_NAME;
    681       1.1  reinoud 
    682       1.1  reinoud 	/* minimum and maximum UDF versions we advise */
    683       1.1  reinoud 	context.min_udf = 0x201;
    684       1.1  reinoud 	context.max_udf = 0x201;
    685       1.1  reinoud 
    686       1.1  reinoud 	/* use user's time zone as default */
    687       1.1  reinoud 	(void)time(&now);
    688       1.1  reinoud 	tm = localtime(&now);
    689       1.1  reinoud 	context.gmtoff = tm->tm_gmtoff;
    690       1.1  reinoud 
    691       1.1  reinoud 	/* process options */
    692  1.12.4.1     yamt 	while ((ch = getopt(argc, argv, "cFL:Mp:P:s:S:B:t:v:V:")) != -1) {
    693       1.1  reinoud 		switch (ch) {
    694       1.1  reinoud 		case 'c' :
    695       1.1  reinoud 			check_surface = 1;
    696       1.1  reinoud 			break;
    697       1.1  reinoud 		case 'F' :
    698       1.1  reinoud 			force = 1;
    699       1.1  reinoud 			break;
    700       1.1  reinoud 		case 'L' :
    701       1.1  reinoud 			if (context.logvol_name) free(context.logvol_name);
    702       1.1  reinoud 			context.logvol_name = strdup(optarg);
    703       1.1  reinoud 			break;
    704       1.1  reinoud 		case 'M' :
    705       1.1  reinoud 			req_disable |= FORMAT_META;
    706       1.1  reinoud 			break;
    707       1.4  reinoud 		case 'p' :
    708       1.4  reinoud 			meta_perc = a_num(optarg, "meta_perc");
    709       1.4  reinoud 			/* limit to `sensible` values */
    710       1.4  reinoud 			meta_perc = MIN(meta_perc, 99);
    711       1.4  reinoud 			meta_perc = MAX(meta_perc, 1);
    712       1.4  reinoud 			meta_fract = (float) meta_perc/100.0;
    713       1.4  reinoud 			break;
    714       1.1  reinoud 		case 'v' :
    715       1.3  reinoud 			context.min_udf = a_udf_version(optarg, "min_udf");
    716       1.1  reinoud 			if (context.min_udf > context.max_udf)
    717       1.1  reinoud 				context.max_udf = context.min_udf;
    718       1.1  reinoud 			break;
    719       1.1  reinoud 		case 'V' :
    720       1.3  reinoud 			context.max_udf = a_udf_version(optarg, "max_udf");
    721       1.1  reinoud 			if (context.min_udf > context.max_udf)
    722       1.1  reinoud 				context.min_udf = context.max_udf;
    723       1.1  reinoud 			break;
    724       1.1  reinoud 		case 'P' :
    725  1.12.4.1     yamt 			/* check if there is a ':' in the name */
    726  1.12.4.1     yamt 			if ((colon = strstr(optarg, ":"))) {
    727  1.12.4.1     yamt 				if (context.volset_name)
    728  1.12.4.1     yamt 					free(context.volset_name);
    729  1.12.4.1     yamt 				*colon = 0;
    730  1.12.4.1     yamt 				context.volset_name = strdup(optarg);
    731  1.12.4.1     yamt 				optarg = colon+1;
    732  1.12.4.1     yamt 			}
    733  1.12.4.1     yamt 			if (context.primary_name)
    734  1.12.4.1     yamt 				free(context.primary_name);
    735  1.12.4.1     yamt 			if ((strstr(optarg, ":"))) {
    736  1.12.4.1     yamt 				perror("primary name can't have ':' in its name");
    737  1.12.4.1     yamt 				return EXIT_FAILURE;
    738  1.12.4.1     yamt 			}
    739       1.1  reinoud 			context.primary_name = strdup(optarg);
    740       1.1  reinoud 			break;
    741       1.1  reinoud 		case 's' :
    742  1.12.4.1     yamt 			/* support for files, set file size */
    743  1.12.4.1     yamt 			/* XXX support for formatting recordables on vnd/file? */
    744  1.12.4.1     yamt 			if (dehumanize_number(optarg, &setsize) < 0) {
    745  1.12.4.1     yamt 				perror("can't parse size argument");
    746  1.12.4.1     yamt 				return EXIT_FAILURE;
    747  1.12.4.1     yamt 			}
    748  1.12.4.1     yamt 			setsize = MAX(0, setsize);
    749       1.1  reinoud 			break;
    750       1.1  reinoud 		case 'S' :
    751  1.12.4.1     yamt 			imagefile_secsize = a_num(optarg, "secsize");
    752  1.12.4.1     yamt 			imagefile_secsize = MAX(512, imagefile_secsize);
    753  1.12.4.1     yamt 			break;
    754  1.12.4.1     yamt 		case 'B' :
    755  1.12.4.1     yamt 			emul_packetsize = a_num(optarg,
    756  1.12.4.1     yamt 				"blockingnr, packetsize");
    757  1.12.4.1     yamt 			emul_packetsize = MAX(emul_packetsize, 1);
    758  1.12.4.1     yamt 			emul_packetsize = MIN(emul_packetsize, 32);
    759       1.1  reinoud 			break;
    760       1.1  reinoud 		case 't' :
    761       1.1  reinoud 			/* time zone overide */
    762       1.1  reinoud 			context.gmtoff = a_num(optarg, "gmtoff");
    763       1.1  reinoud 			break;
    764       1.1  reinoud 		default  :
    765       1.1  reinoud 			usage();
    766       1.1  reinoud 			/* NOTREACHED */
    767       1.1  reinoud 		}
    768       1.1  reinoud 	}
    769       1.1  reinoud 
    770       1.1  reinoud 	if (optind + 1 != argc)
    771       1.1  reinoud 		usage();
    772       1.1  reinoud 
    773       1.1  reinoud 	/* get device and directory specifier */
    774       1.1  reinoud 	dev = argv[optind];
    775       1.1  reinoud 
    776       1.1  reinoud 	/* open device */
    777       1.1  reinoud 	if ((fd = open(dev, O_RDWR, 0)) == -1) {
    778  1.12.4.1     yamt 		/* check if we need to create a file */
    779  1.12.4.1     yamt 		fd = open(dev, O_RDONLY, 0);
    780  1.12.4.1     yamt 		if (fd > 0) {
    781  1.12.4.1     yamt 			perror("device is there but can't be opened for read/write");
    782  1.12.4.1     yamt 			return EXIT_FAILURE;
    783  1.12.4.1     yamt 		}
    784  1.12.4.1     yamt 		if (!force) {
    785  1.12.4.1     yamt 			perror("can't open device");
    786  1.12.4.1     yamt 			return EXIT_FAILURE;
    787  1.12.4.1     yamt 		}
    788  1.12.4.1     yamt 		if (setsize == 0) {
    789  1.12.4.1     yamt 			perror("need to create image file but no size specified");
    790  1.12.4.1     yamt 			return EXIT_FAILURE;
    791  1.12.4.1     yamt 		}
    792  1.12.4.1     yamt 		/* need to create a file */
    793  1.12.4.1     yamt 		fd = open(dev, O_RDWR | O_CREAT | O_TRUNC, 0777);
    794  1.12.4.1     yamt 		if (fd == -1) {
    795  1.12.4.1     yamt 			perror("can't create image file");
    796  1.12.4.1     yamt 			return EXIT_FAILURE;
    797  1.12.4.1     yamt 		}
    798       1.1  reinoud 	}
    799       1.1  reinoud 
    800       1.1  reinoud 	/* stat the device */
    801       1.1  reinoud 	if (fstat(fd, &st) != 0) {
    802       1.1  reinoud 		perror("can't stat the device");
    803       1.1  reinoud 		close(fd);
    804       1.1  reinoud 		return EXIT_FAILURE;
    805       1.1  reinoud 	}
    806       1.1  reinoud 
    807  1.12.4.1     yamt 	if (S_ISREG(st.st_mode)) {
    808  1.12.4.1     yamt 		if (setsize == 0)
    809  1.12.4.1     yamt 			setsize = st.st_size;
    810  1.12.4.1     yamt 		/* sanitise arguments */
    811  1.12.4.1     yamt 		imagefile_secsize &= ~511;
    812  1.12.4.1     yamt 		setsize &= ~(imagefile_secsize-1);
    813  1.12.4.1     yamt 
    814  1.12.4.1     yamt 		if (ftruncate(fd, setsize)) {
    815  1.12.4.1     yamt 			perror("can't resize file");
    816  1.12.4.1     yamt 			return EXIT_FAILURE;
    817  1.12.4.1     yamt 		}
    818  1.12.4.1     yamt 	}
    819  1.12.4.1     yamt 
    820      1.10  reinoud 	/* formatting can only be done on raw devices */
    821  1.12.4.1     yamt 	if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) {
    822       1.1  reinoud 		printf("%s is not a raw device\n", dev);
    823       1.1  reinoud 		close(fd);
    824       1.1  reinoud 		return EXIT_FAILURE;
    825       1.1  reinoud 	}
    826       1.1  reinoud 
    827       1.1  reinoud 	/* just in case something went wrong, synchronise the drive's cache */
    828       1.1  reinoud 	udf_synchronise_caches();
    829       1.1  reinoud 
    830  1.12.4.1     yamt 	/* get 'disc' information */
    831       1.1  reinoud 	error = udf_update_discinfo(&mmc_discinfo);
    832       1.1  reinoud 	if (error) {
    833       1.1  reinoud 		perror("can't retrieve discinfo");
    834       1.1  reinoud 		close(fd);
    835       1.1  reinoud 		return EXIT_FAILURE;
    836       1.1  reinoud 	}
    837       1.1  reinoud 
    838       1.1  reinoud 	/* derive disc identifiers when not specified and check given */
    839       1.1  reinoud 	error = udf_proces_names();
    840       1.1  reinoud 	if (error) {
    841       1.1  reinoud 		/* error message has been printed */
    842       1.1  reinoud 		close(fd);
    843       1.1  reinoud 		return EXIT_FAILURE;
    844       1.1  reinoud 	}
    845       1.1  reinoud 
    846       1.1  reinoud 	/* derive newfs disc format from disc profile */
    847       1.1  reinoud 	error = udf_derive_format(req_enable, req_disable, force);
    848       1.1  reinoud 	if (error)  {
    849       1.1  reinoud 		/* error message has been printed */
    850       1.1  reinoud 		close(fd);
    851       1.1  reinoud 		return EXIT_FAILURE;
    852       1.1  reinoud 	}
    853       1.1  reinoud 
    854       1.1  reinoud 	udf_dump_discinfo(&mmc_discinfo);
    855       1.1  reinoud 	printf("Formatting disc compatible with UDF version %x to %x\n\n",
    856       1.1  reinoud 		context.min_udf, context.max_udf);
    857       1.1  reinoud 	(void)snprintb(scrap, sizeof(scrap), FORMAT_FLAGBITS,
    858       1.1  reinoud 	    (uint64_t) format_flags);
    859       1.4  reinoud 	printf("UDF properties       %s\n", scrap);
    860       1.4  reinoud 	printf("Volume set          `%s'\n", context.volset_name);
    861       1.4  reinoud 	printf("Primary volume      `%s`\n", context.primary_name);
    862       1.4  reinoud 	printf("Logical volume      `%s`\n", context.logvol_name);
    863       1.4  reinoud 	if (format_flags & FORMAT_META)
    864       1.4  reinoud 		printf("Metadata percentage  %d %%\n", meta_perc);
    865       1.1  reinoud 	printf("\n");
    866       1.1  reinoud 
    867       1.1  reinoud 	/* prepare disc if nessisary (recordables mainly) */
    868       1.1  reinoud 	error = udf_prepare_disc();
    869       1.1  reinoud 	if (error) {
    870       1.1  reinoud 		perror("preparing disc failed");
    871       1.1  reinoud 		close(fd);
    872       1.1  reinoud 		return EXIT_FAILURE;
    873       1.1  reinoud 	};
    874       1.1  reinoud 
    875  1.12.4.1     yamt 	/* setup sector writeout queue's */
    876  1.12.4.1     yamt 	TAILQ_INIT(&write_queue);
    877  1.12.4.1     yamt 
    878  1.12.4.1     yamt 	/* perform the newfs itself */
    879       1.1  reinoud 	error = udf_do_newfs();
    880  1.12.4.1     yamt 	if (!error) {
    881  1.12.4.1     yamt 		/* write out sectors */
    882  1.12.4.1     yamt 		error = writeout_write_queue();
    883  1.12.4.1     yamt 	}
    884       1.1  reinoud 
    885       1.1  reinoud 	/* in any case, synchronise the drive's cache to prevent lockups */
    886       1.1  reinoud 	udf_synchronise_caches();
    887       1.1  reinoud 
    888       1.1  reinoud 	close(fd);
    889       1.1  reinoud 	if (error)
    890       1.1  reinoud 		return EXIT_FAILURE;
    891       1.1  reinoud 
    892       1.1  reinoud 	return EXIT_SUCCESS;
    893       1.1  reinoud }
    894       1.1  reinoud 
    895       1.1  reinoud /* --------------------------------------------------------------------- */
    896       1.1  reinoud 
    897