Home | History | Annotate | Line # | Download | only in udf
udf_readwrite.c revision 1.4.2.1
      1  1.4.2.1     haad /* $NetBSD: udf_readwrite.c,v 1.4.2.1 2008/10/19 22:17:18 haad Exp $ */
      2      1.1  reinoud 
      3      1.1  reinoud /*
      4      1.1  reinoud  * Copyright (c) 2007, 2008 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 #include <sys/cdefs.h>
     30      1.1  reinoud #ifndef lint
     31  1.4.2.1     haad __KERNEL_RCSID(0, "$NetBSD: udf_readwrite.c,v 1.4.2.1 2008/10/19 22:17:18 haad Exp $");
     32      1.1  reinoud #endif /* not lint */
     33      1.1  reinoud 
     34      1.1  reinoud 
     35      1.1  reinoud #if defined(_KERNEL_OPT)
     36      1.1  reinoud #include "opt_quota.h"
     37      1.1  reinoud #include "opt_compat_netbsd.h"
     38      1.1  reinoud #endif
     39      1.1  reinoud 
     40      1.1  reinoud #include <sys/param.h>
     41      1.1  reinoud #include <sys/systm.h>
     42      1.1  reinoud #include <sys/sysctl.h>
     43      1.1  reinoud #include <sys/namei.h>
     44      1.1  reinoud #include <sys/proc.h>
     45      1.1  reinoud #include <sys/kernel.h>
     46      1.1  reinoud #include <sys/vnode.h>
     47      1.1  reinoud #include <miscfs/genfs/genfs_node.h>
     48      1.1  reinoud #include <sys/mount.h>
     49      1.1  reinoud #include <sys/buf.h>
     50      1.1  reinoud #include <sys/file.h>
     51      1.1  reinoud #include <sys/device.h>
     52      1.1  reinoud #include <sys/disklabel.h>
     53      1.1  reinoud #include <sys/ioctl.h>
     54      1.1  reinoud #include <sys/malloc.h>
     55      1.1  reinoud #include <sys/dirent.h>
     56      1.1  reinoud #include <sys/stat.h>
     57      1.1  reinoud #include <sys/conf.h>
     58      1.1  reinoud #include <sys/kauth.h>
     59      1.1  reinoud #include <sys/kthread.h>
     60      1.1  reinoud #include <dev/clock_subr.h>
     61      1.1  reinoud 
     62      1.1  reinoud #include <fs/udf/ecma167-udf.h>
     63      1.1  reinoud #include <fs/udf/udf_mount.h>
     64      1.1  reinoud 
     65      1.1  reinoud #include "udf.h"
     66      1.1  reinoud #include "udf_subr.h"
     67      1.1  reinoud #include "udf_bswap.h"
     68      1.1  reinoud 
     69      1.1  reinoud 
     70      1.1  reinoud #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
     71      1.1  reinoud 
     72      1.1  reinoud /* --------------------------------------------------------------------- */
     73      1.1  reinoud 
     74      1.1  reinoud void
     75      1.1  reinoud udf_fixup_fid_block(uint8_t *blob, int lb_size,
     76      1.1  reinoud 	int rfix_pos, int max_rfix_pos, uint32_t lb_num)
     77      1.1  reinoud {
     78      1.1  reinoud 	struct fileid_desc *fid;
     79      1.1  reinoud 	uint8_t *fid_pos;
     80      1.1  reinoud 	int fid_len, found;
     81      1.1  reinoud 
     82      1.1  reinoud 	/* needs to be word aligned */
     83      1.1  reinoud 	KASSERT(rfix_pos % 4 == 0);
     84      1.1  reinoud 
     85      1.1  reinoud 	/* first resync with the FID stream !!! */
     86      1.1  reinoud 	found = 0;
     87      1.1  reinoud 	while (rfix_pos + sizeof(struct desc_tag) <= max_rfix_pos) {
     88      1.1  reinoud 		fid_pos = blob + rfix_pos;
     89      1.1  reinoud 		fid = (struct fileid_desc *) fid_pos;
     90      1.1  reinoud 		if (udf_rw16(fid->tag.id) == TAGID_FID) {
     91      1.1  reinoud 			if (udf_check_tag((union dscrptr *) fid) == 0)
     92      1.1  reinoud 				found = 1;
     93      1.1  reinoud 		}
     94      1.1  reinoud 		if (found)
     95      1.1  reinoud 			break;
     96      1.1  reinoud 		/* try next location; can only be 4 bytes aligned */
     97      1.1  reinoud 		rfix_pos += 4;
     98      1.1  reinoud 	}
     99      1.1  reinoud 
    100      1.1  reinoud 	/* walk over the fids */
    101      1.1  reinoud 	fid_pos = blob + rfix_pos;
    102      1.1  reinoud 	while (rfix_pos + sizeof(struct desc_tag) <= max_rfix_pos) {
    103      1.1  reinoud 		fid = (struct fileid_desc *) fid_pos;
    104      1.1  reinoud 		if (udf_rw16(fid->tag.id) != TAGID_FID) {
    105      1.1  reinoud 			/* end of FID stream; end of directory or currupted */
    106      1.1  reinoud 			break;
    107      1.1  reinoud 		}
    108      1.1  reinoud 
    109      1.1  reinoud 		/* update sector number and recalculate checkum */
    110      1.1  reinoud 		fid->tag.tag_loc = udf_rw32(lb_num);
    111      1.1  reinoud 		udf_validate_tag_sum((union dscrptr *) fid);
    112      1.1  reinoud 
    113      1.1  reinoud 		/* if the FID crosses the memory, we're done! */
    114      1.1  reinoud 		if (rfix_pos + UDF_FID_SIZE >= max_rfix_pos)
    115      1.1  reinoud 			break;
    116      1.1  reinoud 
    117      1.1  reinoud 		fid_len = udf_fidsize(fid);
    118      1.1  reinoud 		fid_pos  += fid_len;
    119      1.1  reinoud 		rfix_pos += fid_len;
    120      1.1  reinoud 	}
    121      1.1  reinoud }
    122      1.1  reinoud 
    123      1.1  reinoud 
    124      1.1  reinoud void
    125      1.1  reinoud udf_fixup_internal_extattr(uint8_t *blob, uint32_t lb_num)
    126      1.1  reinoud {
    127      1.1  reinoud 	struct desc_tag        *tag;
    128      1.1  reinoud 	struct file_entry      *fe;
    129      1.1  reinoud 	struct extfile_entry   *efe;
    130      1.1  reinoud 	struct extattrhdr_desc *eahdr;
    131      1.2  reinoud 	int l_ea;
    132      1.1  reinoud 
    133      1.1  reinoud 	/* get information from fe/efe */
    134      1.1  reinoud 	tag = (struct desc_tag *) blob;
    135      1.1  reinoud 	switch (udf_rw16(tag->id)) {
    136      1.1  reinoud 	case TAGID_FENTRY :
    137      1.1  reinoud 		fe = (struct file_entry *) blob;
    138      1.1  reinoud 		l_ea  = udf_rw32(fe->l_ea);
    139      1.1  reinoud 		eahdr = (struct extattrhdr_desc *) fe->data;
    140      1.1  reinoud 		break;
    141      1.1  reinoud 	case TAGID_EXTFENTRY :
    142      1.1  reinoud 		efe = (struct extfile_entry *) blob;
    143      1.1  reinoud 		l_ea  = udf_rw32(efe->l_ea);
    144      1.1  reinoud 		eahdr = (struct extattrhdr_desc *) efe->data;
    145      1.1  reinoud 		break;
    146      1.1  reinoud 	case TAGID_INDIRECTENTRY :
    147      1.1  reinoud 	case TAGID_ALLOCEXTENT :
    148      1.1  reinoud 	case TAGID_EXTATTR_HDR :
    149      1.1  reinoud 		return;
    150      1.1  reinoud 	default:
    151      1.3    perry 		panic("%s: passed bad tag\n", __func__);
    152      1.1  reinoud 	}
    153      1.1  reinoud 
    154      1.1  reinoud 	/* something recorded here? (why am i called?) */
    155      1.1  reinoud 	if (l_ea == 0)
    156      1.1  reinoud 		return;
    157      1.1  reinoud 
    158      1.2  reinoud #if 0
    159      1.1  reinoud 	/* check extended attribute tag */
    160      1.1  reinoud 	/* TODO XXX what to do when we encounter an error here? */
    161      1.1  reinoud 	error = udf_check_tag(eahdr);
    162      1.1  reinoud 	if (error)
    163      1.1  reinoud 		return;	/* for now */
    164      1.1  reinoud 	if (udf_rw16(eahdr->tag.id) != TAGID_EXTATTR_HDR)
    165      1.1  reinoud 		return;	/* for now */
    166      1.1  reinoud 	error = udf_check_tag_payload(eahdr, sizeof(struct extattrhdr_desc));
    167      1.1  reinoud 	if (error)
    168      1.1  reinoud 		return; /* for now */
    169      1.2  reinoud #endif
    170      1.1  reinoud 
    171      1.1  reinoud 	DPRINTF(EXTATTR, ("node fixup: found %d bytes of extended attributes\n",
    172      1.1  reinoud 		l_ea));
    173      1.1  reinoud 
    174      1.1  reinoud 	/* fixup eahdr tag */
    175      1.1  reinoud 	eahdr->tag.tag_loc = udf_rw32(lb_num);
    176      1.2  reinoud 	udf_validate_tag_and_crc_sums((union dscrptr *) eahdr);
    177      1.1  reinoud }
    178      1.1  reinoud 
    179      1.1  reinoud 
    180      1.1  reinoud void
    181      1.1  reinoud udf_fixup_node_internals(struct udf_mount *ump, uint8_t *blob, int udf_c_type)
    182      1.1  reinoud {
    183  1.4.2.1     haad 	struct desc_tag *tag, *sbm_tag;
    184      1.1  reinoud 	struct file_entry *fe;
    185      1.1  reinoud 	struct extfile_entry *efe;
    186  1.4.2.1     haad 	struct alloc_ext_entry *ext;
    187      1.1  reinoud 	uint32_t lb_size, lb_num;
    188  1.4.2.1     haad 	uint32_t intern_pos, max_intern_pos;
    189  1.4.2.1     haad 	int icbflags, addr_type, file_type, intern, has_fids, has_sbm, l_ea;
    190      1.1  reinoud 
    191      1.1  reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    192      1.1  reinoud 	/* if its not a node we're done */
    193      1.1  reinoud 	if (udf_c_type != UDF_C_NODE)
    194      1.1  reinoud 		return;
    195      1.1  reinoud 
    196      1.1  reinoud 	/* NOTE this could also be done in write_internal */
    197      1.1  reinoud 	/* start of a descriptor */
    198  1.4.2.1     haad 	l_ea      = 0;
    199  1.4.2.1     haad 	has_fids  = 0;
    200  1.4.2.1     haad 	has_sbm   = 0;
    201  1.4.2.1     haad 	intern    = 0;
    202  1.4.2.1     haad 	file_type = 0;
    203  1.4.2.1     haad 	max_intern_pos = intern_pos = lb_num = 0;	/* shut up gcc! */
    204      1.1  reinoud 
    205      1.1  reinoud 	tag = (struct desc_tag *) blob;
    206      1.1  reinoud 	switch (udf_rw16(tag->id)) {
    207      1.1  reinoud 	case TAGID_FENTRY :
    208      1.1  reinoud 		fe = (struct file_entry *) tag;
    209      1.1  reinoud 		l_ea = udf_rw32(fe->l_ea);
    210      1.1  reinoud 		icbflags  = udf_rw16(fe->icbtag.flags);
    211      1.1  reinoud 		addr_type = (icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK);
    212  1.4.2.1     haad 		file_type = fe->icbtag.file_type;
    213  1.4.2.1     haad 		intern = (addr_type == UDF_ICB_INTERN_ALLOC);
    214  1.4.2.1     haad 		intern_pos  = UDF_FENTRY_SIZE + l_ea;
    215  1.4.2.1     haad 		max_intern_pos = intern_pos + udf_rw64(fe->inf_len);
    216      1.1  reinoud 		lb_num = udf_rw32(fe->tag.tag_loc);
    217      1.1  reinoud 		break;
    218      1.1  reinoud 	case TAGID_EXTFENTRY :
    219      1.1  reinoud 		efe = (struct extfile_entry *) tag;
    220      1.1  reinoud 		l_ea = udf_rw32(efe->l_ea);
    221      1.1  reinoud 		icbflags  = udf_rw16(efe->icbtag.flags);
    222      1.1  reinoud 		addr_type = (icbflags & UDF_ICB_TAG_FLAGS_ALLOC_MASK);
    223  1.4.2.1     haad 		file_type = efe->icbtag.file_type;
    224  1.4.2.1     haad 		intern = (addr_type == UDF_ICB_INTERN_ALLOC);
    225  1.4.2.1     haad 		intern_pos  = UDF_EXTFENTRY_SIZE + l_ea;
    226  1.4.2.1     haad 		max_intern_pos = intern_pos + udf_rw64(efe->inf_len);
    227      1.1  reinoud 		lb_num = udf_rw32(efe->tag.tag_loc);
    228      1.1  reinoud 		break;
    229      1.1  reinoud 	case TAGID_INDIRECTENTRY :
    230      1.1  reinoud 	case TAGID_EXTATTR_HDR :
    231  1.4.2.1     haad 		break;
    232  1.4.2.1     haad 	case TAGID_ALLOCEXTENT :
    233  1.4.2.1     haad 		/* force crclen to 8 for UDF version < 2.01 */
    234  1.4.2.1     haad 		ext = (struct alloc_ext_entry *) tag;
    235  1.4.2.1     haad 		if (udf_rw16(ump->logvol_info->min_udf_readver) <= 0x200)
    236  1.4.2.1     haad 			ext->tag.desc_crc_len = udf_rw16(8);
    237      1.1  reinoud 		break;
    238      1.1  reinoud 	default:
    239      1.3    perry 		panic("%s: passed bad tag\n", __func__);
    240      1.1  reinoud 		break;
    241      1.1  reinoud 	}
    242      1.1  reinoud 
    243  1.4.2.1     haad 	/* determine what to fix if its internally recorded */
    244  1.4.2.1     haad 	if (intern) {
    245  1.4.2.1     haad 		has_fids = (file_type == UDF_ICB_FILETYPE_DIRECTORY) ||
    246  1.4.2.1     haad 			   (file_type == UDF_ICB_FILETYPE_STREAMDIR);
    247  1.4.2.1     haad 		has_sbm  = (file_type == UDF_ICB_FILETYPE_META_BITMAP);
    248  1.4.2.1     haad 	}
    249  1.4.2.1     haad 
    250      1.1  reinoud 	/* fixup internal extended attributes if present */
    251      1.1  reinoud 	if (l_ea)
    252      1.1  reinoud 		udf_fixup_internal_extattr(blob, lb_num);
    253      1.1  reinoud 
    254  1.4.2.1     haad 	/* fixup fids lb numbers */
    255  1.4.2.1     haad 	if (has_fids)
    256  1.4.2.1     haad 		udf_fixup_fid_block(blob, lb_size, intern_pos,
    257  1.4.2.1     haad 			max_intern_pos, lb_num);
    258  1.4.2.1     haad 
    259  1.4.2.1     haad 	/* fixup space bitmap descriptor */
    260  1.4.2.1     haad 	if (has_sbm) {
    261  1.4.2.1     haad 		sbm_tag = (struct desc_tag *) (blob + intern_pos);
    262  1.4.2.1     haad 		sbm_tag->tag_loc = tag->tag_loc;
    263  1.4.2.1     haad 		udf_validate_tag_and_crc_sums((uint8_t *) sbm_tag);
    264      1.1  reinoud 	}
    265  1.4.2.1     haad 
    266      1.1  reinoud 	udf_validate_tag_and_crc_sums(blob);
    267      1.1  reinoud }
    268      1.1  reinoud 
    269      1.1  reinoud /* --------------------------------------------------------------------- */
    270      1.1  reinoud 
    271      1.1  reinoud /*
    272      1.1  reinoud  * Set of generic descriptor readers and writers and their helper functions.
    273      1.1  reinoud  * Descriptors inside `logical space' i.e. inside logically mapped partitions
    274      1.1  reinoud  * can never be longer than one logical sector.
    275      1.1  reinoud  *
    276      1.1  reinoud  * NOTE that these functions *can* be used by the sheduler backends to read
    277      1.1  reinoud  * node descriptors too.
    278      1.1  reinoud  *
    279      1.1  reinoud  * For reading, the size of allocated piece is returned in multiple of sector
    280      1.1  reinoud  * size due to udf_calc_udf_malloc_size().
    281      1.1  reinoud  */
    282      1.1  reinoud 
    283      1.1  reinoud 
    284      1.1  reinoud /* SYNC reading of n blocks from specified sector */
    285      1.1  reinoud /* NOTE only used by udf_read_phys_dscr */
    286      1.1  reinoud static int
    287      1.1  reinoud udf_read_phys_sectors(struct udf_mount *ump, int what, void *blob,
    288      1.1  reinoud 	uint32_t start, uint32_t sectors)
    289      1.1  reinoud {
    290      1.1  reinoud 	struct buf *buf, *nestbuf;
    291      1.1  reinoud 	uint32_t buf_offset;
    292      1.1  reinoud 	off_t lblkno, rblkno;
    293      1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
    294      1.1  reinoud 	int blks = sector_size / DEV_BSIZE;
    295      1.1  reinoud 	int piece;
    296      1.1  reinoud 	int error;
    297      1.1  reinoud 
    298      1.1  reinoud 	DPRINTF(READ, ("udf_intbreadn() : sectors = %d, sector_size = %d\n",
    299      1.1  reinoud 		sectors, sector_size));
    300      1.1  reinoud 	buf = getiobuf(ump->devvp, true);
    301      1.1  reinoud 	buf->b_flags    = B_READ;
    302      1.1  reinoud 	buf->b_cflags   = BC_BUSY;	/* needed? */
    303      1.1  reinoud 	buf->b_iodone   = NULL;
    304      1.1  reinoud 	buf->b_data     = blob;
    305      1.1  reinoud 	buf->b_bcount   = sectors * sector_size;
    306      1.1  reinoud 	buf->b_resid    = buf->b_bcount;
    307      1.1  reinoud 	buf->b_bufsize  = buf->b_bcount;
    308      1.1  reinoud 	buf->b_private  = NULL;	/* not needed yet */
    309      1.1  reinoud 	BIO_SETPRIO(buf, BPRIO_DEFAULT);
    310      1.1  reinoud 	buf->b_lblkno   = buf->b_blkno = buf->b_rawblkno = start * blks;
    311      1.1  reinoud 	buf->b_proc     = NULL;
    312      1.1  reinoud 
    313      1.1  reinoud 	error = 0;
    314      1.1  reinoud 	buf_offset = 0;
    315      1.1  reinoud 	rblkno = start;
    316      1.1  reinoud 	lblkno = 0;
    317      1.1  reinoud 	while ((sectors > 0) && (error == 0)) {
    318      1.1  reinoud 		piece = MIN(MAXPHYS/sector_size, sectors);
    319      1.1  reinoud 		DPRINTF(READ, ("read in %d + %d\n", (uint32_t) rblkno, piece));
    320      1.1  reinoud 
    321      1.1  reinoud 		nestbuf = getiobuf(NULL, true);
    322      1.1  reinoud 		nestiobuf_setup(buf, nestbuf, buf_offset, piece * sector_size);
    323      1.1  reinoud 		/* nestbuf is B_ASYNC */
    324      1.1  reinoud 
    325      1.1  reinoud 		/* identify this nestbuf */
    326      1.1  reinoud 		nestbuf->b_lblkno   = lblkno;
    327      1.1  reinoud 
    328      1.1  reinoud 		/* CD shedules on raw blkno */
    329      1.1  reinoud 		nestbuf->b_blkno      = rblkno * blks;
    330      1.1  reinoud 		nestbuf->b_proc       = NULL;
    331      1.1  reinoud 		nestbuf->b_rawblkno   = rblkno * blks;
    332      1.1  reinoud 		nestbuf->b_udf_c_type = what;
    333      1.1  reinoud 
    334      1.1  reinoud 		udf_discstrat_queuebuf(ump, nestbuf);
    335      1.1  reinoud 
    336      1.1  reinoud 		lblkno     += piece;
    337      1.1  reinoud 		rblkno     += piece;
    338      1.1  reinoud 		buf_offset += piece * sector_size;
    339      1.1  reinoud 		sectors    -= piece;
    340      1.1  reinoud 	}
    341      1.1  reinoud 	error = biowait(buf);
    342      1.1  reinoud 	putiobuf(buf);
    343      1.1  reinoud 
    344      1.1  reinoud 	return error;
    345      1.1  reinoud }
    346      1.1  reinoud 
    347      1.1  reinoud 
    348      1.1  reinoud /* synchronous generic descriptor read */
    349      1.1  reinoud int
    350      1.1  reinoud udf_read_phys_dscr(struct udf_mount *ump, uint32_t sector,
    351      1.1  reinoud 		    struct malloc_type *mtype, union dscrptr **dstp)
    352      1.1  reinoud {
    353      1.1  reinoud 	union dscrptr *dst, *new_dst;
    354      1.1  reinoud 	uint8_t *pos;
    355      1.1  reinoud 	int sectors, dscrlen;
    356      1.1  reinoud 	int i, error, sector_size;
    357      1.1  reinoud 
    358      1.1  reinoud 	sector_size = ump->discinfo.sector_size;
    359      1.1  reinoud 
    360      1.1  reinoud 	*dstp = dst = NULL;
    361      1.1  reinoud 	dscrlen = sector_size;
    362      1.1  reinoud 
    363      1.1  reinoud 	/* read initial piece */
    364      1.1  reinoud 	dst = malloc(sector_size, mtype, M_WAITOK);
    365      1.1  reinoud 	error = udf_read_phys_sectors(ump, UDF_C_DSCR, dst, sector, 1);
    366      1.1  reinoud 	DPRINTFIF(DESCRIPTOR, error, ("read error (%d)\n", error));
    367      1.1  reinoud 
    368      1.1  reinoud 	if (!error) {
    369      1.1  reinoud 		/* check if its a valid tag */
    370      1.1  reinoud 		error = udf_check_tag(dst);
    371      1.1  reinoud 		if (error) {
    372      1.1  reinoud 			/* check if its an empty block */
    373      1.1  reinoud 			pos = (uint8_t *) dst;
    374      1.1  reinoud 			for (i = 0; i < sector_size; i++, pos++) {
    375      1.1  reinoud 				if (*pos) break;
    376      1.1  reinoud 			}
    377      1.1  reinoud 			if (i == sector_size) {
    378      1.1  reinoud 				/* return no error but with no dscrptr */
    379      1.1  reinoud 				/* dispose first block */
    380      1.1  reinoud 				free(dst, mtype);
    381      1.1  reinoud 				return 0;
    382      1.1  reinoud 			}
    383      1.1  reinoud 		}
    384      1.1  reinoud 		/* calculate descriptor size */
    385      1.1  reinoud 		dscrlen = udf_tagsize(dst, sector_size);
    386      1.1  reinoud 	}
    387      1.1  reinoud 	DPRINTFIF(DESCRIPTOR, error, ("bad tag checksum\n"));
    388      1.1  reinoud 
    389      1.1  reinoud 	if (!error && (dscrlen > sector_size)) {
    390      1.1  reinoud 		DPRINTF(DESCRIPTOR, ("multi block descriptor read\n"));
    391      1.1  reinoud 		/*
    392      1.1  reinoud 		 * Read the rest of descriptor. Since it is only used at mount
    393      1.1  reinoud 		 * time its overdone to define and use a specific udf_intbreadn
    394      1.1  reinoud 		 * for this alone.
    395      1.1  reinoud 		 */
    396      1.1  reinoud 
    397      1.1  reinoud 		new_dst = realloc(dst, dscrlen, mtype, M_WAITOK);
    398      1.1  reinoud 		if (new_dst == NULL) {
    399      1.1  reinoud 			free(dst, mtype);
    400      1.1  reinoud 			return ENOMEM;
    401      1.1  reinoud 		}
    402      1.1  reinoud 		dst = new_dst;
    403      1.1  reinoud 
    404      1.1  reinoud 		sectors = (dscrlen + sector_size -1) / sector_size;
    405      1.1  reinoud 		DPRINTF(DESCRIPTOR, ("dscrlen = %d (%d blk)\n", dscrlen, sectors));
    406      1.1  reinoud 
    407      1.1  reinoud 		pos = (uint8_t *) dst + sector_size;
    408      1.1  reinoud 		error = udf_read_phys_sectors(ump, UDF_C_DSCR, pos,
    409      1.1  reinoud 				sector + 1, sectors-1);
    410      1.1  reinoud 
    411      1.1  reinoud 		DPRINTFIF(DESCRIPTOR, error, ("read error on multi (%d)\n",
    412      1.1  reinoud 		    error));
    413      1.1  reinoud 	}
    414      1.1  reinoud 	if (!error) {
    415      1.1  reinoud 		error = udf_check_tag_payload(dst, dscrlen);
    416      1.1  reinoud 		DPRINTFIF(DESCRIPTOR, error, ("bad payload check sum\n"));
    417      1.1  reinoud 	}
    418      1.1  reinoud 	if (error && dst) {
    419      1.1  reinoud 		free(dst, mtype);
    420      1.1  reinoud 		dst = NULL;
    421      1.1  reinoud 	}
    422      1.1  reinoud 	*dstp = dst;
    423      1.1  reinoud 
    424      1.1  reinoud 	return error;
    425      1.1  reinoud }
    426      1.1  reinoud 
    427      1.1  reinoud 
    428      1.1  reinoud static void
    429      1.1  reinoud udf_write_phys_buf(struct udf_mount *ump, int what, struct buf *buf)
    430      1.1  reinoud {
    431      1.1  reinoud 	struct buf *nestbuf;
    432      1.1  reinoud 	uint32_t buf_offset;
    433      1.1  reinoud 	off_t lblkno, rblkno;
    434      1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
    435      1.1  reinoud 	int blks = sector_size / DEV_BSIZE;
    436      1.1  reinoud 	uint32_t sectors;
    437      1.1  reinoud 	int piece;
    438      1.1  reinoud 	int error;
    439      1.1  reinoud 
    440      1.1  reinoud 	sectors = buf->b_bcount / sector_size;
    441      1.1  reinoud 	DPRINTF(WRITE, ("udf_intbwriten() : sectors = %d, sector_size = %d\n",
    442      1.1  reinoud 		sectors, sector_size));
    443      1.1  reinoud 
    444      1.1  reinoud 	/* don't forget to increase pending count for the bwrite itself */
    445      1.1  reinoud /* panic("NO WRITING\n"); */
    446      1.1  reinoud 	if (buf->b_vp) {
    447      1.1  reinoud 		mutex_enter(&buf->b_vp->v_interlock);
    448      1.1  reinoud 		buf->b_vp->v_numoutput++;
    449      1.1  reinoud 		mutex_exit(&buf->b_vp->v_interlock);
    450      1.1  reinoud 	}
    451      1.1  reinoud 
    452      1.1  reinoud 	error = 0;
    453      1.1  reinoud 	buf_offset = 0;
    454      1.1  reinoud 	rblkno = buf->b_blkno / blks;
    455      1.1  reinoud 	lblkno = 0;
    456      1.1  reinoud 	while ((sectors > 0) && (error == 0)) {
    457      1.1  reinoud 		piece = MIN(MAXPHYS/sector_size, sectors);
    458      1.1  reinoud 		DPRINTF(WRITE, ("write out %d + %d\n",
    459      1.1  reinoud 		    (uint32_t) rblkno, piece));
    460      1.1  reinoud 
    461      1.1  reinoud 		nestbuf = getiobuf(NULL, true);
    462      1.1  reinoud 		nestiobuf_setup(buf, nestbuf, buf_offset, piece * sector_size);
    463      1.1  reinoud 		/* nestbuf is B_ASYNC */
    464      1.1  reinoud 
    465      1.1  reinoud 		/* identify this nestbuf */
    466      1.1  reinoud 		nestbuf->b_lblkno   = lblkno;
    467      1.1  reinoud 
    468      1.1  reinoud 		/* CD shedules on raw blkno */
    469      1.1  reinoud 		nestbuf->b_blkno      = rblkno * blks;
    470      1.1  reinoud 		nestbuf->b_proc       = NULL;
    471      1.1  reinoud 		nestbuf->b_rawblkno   = rblkno * blks;
    472      1.1  reinoud 		nestbuf->b_udf_c_type = what;
    473      1.1  reinoud 
    474      1.1  reinoud 		udf_discstrat_queuebuf(ump, nestbuf);
    475      1.1  reinoud 
    476      1.1  reinoud 		lblkno     += piece;
    477      1.1  reinoud 		rblkno     += piece;
    478      1.1  reinoud 		buf_offset += piece * sector_size;
    479      1.1  reinoud 		sectors    -= piece;
    480      1.1  reinoud 	}
    481      1.1  reinoud }
    482      1.1  reinoud 
    483      1.1  reinoud 
    484      1.1  reinoud /* synchronous generic descriptor write */
    485      1.1  reinoud int
    486      1.1  reinoud udf_write_phys_dscr_sync(struct udf_mount *ump, struct udf_node *udf_node, int what,
    487      1.1  reinoud 		     union dscrptr *dscr, uint32_t sector, uint32_t logsector)
    488      1.1  reinoud {
    489      1.1  reinoud 	struct vnode *vp;
    490      1.1  reinoud 	struct buf *buf;
    491      1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
    492      1.1  reinoud 	int blks = sector_size / DEV_BSIZE;
    493      1.1  reinoud 	int dscrlen;
    494      1.1  reinoud 	int error;
    495      1.1  reinoud 
    496      1.1  reinoud 	/* set sector number in the descriptor and validate */
    497      1.1  reinoud 	dscr->tag.tag_loc = udf_rw32(logsector);
    498      1.1  reinoud 	udf_validate_tag_and_crc_sums(dscr);
    499      1.1  reinoud 
    500      1.1  reinoud 	/* calculate descriptor size */
    501      1.1  reinoud 	dscrlen = udf_tagsize(dscr, sector_size);
    502      1.1  reinoud 
    503      1.1  reinoud 	/* get transfer buffer */
    504      1.1  reinoud 	vp = udf_node ? udf_node->vnode : ump->devvp;
    505      1.1  reinoud 	buf = getiobuf(vp, true);
    506      1.1  reinoud 	buf->b_flags    = B_WRITE;
    507      1.1  reinoud 	buf->b_cflags   = BC_BUSY;	/* needed? */
    508      1.1  reinoud 	buf->b_iodone   = NULL;
    509      1.1  reinoud 	buf->b_data     = (void *) dscr;
    510      1.1  reinoud 	buf->b_bcount   = dscrlen;
    511      1.1  reinoud 	buf->b_resid    = buf->b_bcount;
    512      1.1  reinoud 	buf->b_bufsize  = buf->b_bcount;
    513      1.1  reinoud 	buf->b_private  = NULL;	/* not needed yet */
    514      1.1  reinoud 	BIO_SETPRIO(buf, BPRIO_DEFAULT);
    515      1.1  reinoud 	buf->b_lblkno   = buf->b_blkno = buf->b_rawblkno = sector * blks;
    516      1.1  reinoud 	buf->b_proc     = NULL;
    517      1.1  reinoud 
    518      1.1  reinoud 	/* do the write, wait and return error */
    519      1.1  reinoud 	udf_write_phys_buf(ump, what, buf);
    520      1.1  reinoud 	error = biowait(buf);
    521      1.1  reinoud 	putiobuf(buf);
    522      1.1  reinoud 
    523      1.1  reinoud 	return error;
    524      1.1  reinoud }
    525      1.1  reinoud 
    526      1.1  reinoud 
    527      1.1  reinoud /* asynchronous generic descriptor write */
    528      1.1  reinoud int
    529      1.1  reinoud udf_write_phys_dscr_async(struct udf_mount *ump, struct udf_node *udf_node,
    530      1.1  reinoud 		      int what, union dscrptr *dscr,
    531      1.1  reinoud 		      uint32_t sector, uint32_t logsector,
    532      1.1  reinoud 		      void (*dscrwr_callback)(struct buf *))
    533      1.1  reinoud {
    534      1.1  reinoud 	struct vnode *vp;
    535      1.1  reinoud 	struct buf *buf;
    536      1.1  reinoud 	int dscrlen;
    537      1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
    538      1.1  reinoud 	int blks = sector_size / DEV_BSIZE;
    539      1.1  reinoud 
    540      1.1  reinoud 	KASSERT(dscrwr_callback);
    541      1.1  reinoud 	DPRINTF(NODE, ("udf_write_phys_dscr_async() called\n"));
    542      1.1  reinoud 
    543      1.1  reinoud 	/* set sector number in the descriptor and validate */
    544      1.1  reinoud 	dscr->tag.tag_loc = udf_rw32(logsector);
    545      1.1  reinoud 	udf_validate_tag_and_crc_sums(dscr);
    546      1.1  reinoud 
    547      1.1  reinoud 	/* calculate descriptor size */
    548      1.1  reinoud 	dscrlen = udf_tagsize(dscr, sector_size);
    549      1.1  reinoud 
    550      1.1  reinoud 	/* get transfer buffer */
    551      1.1  reinoud 	vp = udf_node ? udf_node->vnode : ump->devvp;
    552      1.1  reinoud 	buf = getiobuf(vp, true);
    553      1.1  reinoud 	buf->b_flags    = B_WRITE; // | B_ASYNC;
    554      1.1  reinoud 	buf->b_cflags   = BC_BUSY;
    555      1.1  reinoud 	buf->b_iodone	= dscrwr_callback;
    556      1.1  reinoud 	buf->b_data     = dscr;
    557      1.1  reinoud 	buf->b_bcount   = dscrlen;
    558      1.1  reinoud 	buf->b_resid    = buf->b_bcount;
    559      1.1  reinoud 	buf->b_bufsize  = buf->b_bcount;
    560      1.1  reinoud 	buf->b_private  = NULL;	/* not needed yet */
    561      1.1  reinoud 	BIO_SETPRIO(buf, BPRIO_DEFAULT);
    562      1.1  reinoud 	buf->b_lblkno   = buf->b_blkno = buf->b_rawblkno = sector * blks;
    563      1.1  reinoud 	buf->b_proc     = NULL;
    564      1.1  reinoud 
    565      1.1  reinoud 	/* do the write and return no error */
    566      1.1  reinoud 	udf_write_phys_buf(ump, what, buf);
    567      1.1  reinoud 	return 0;
    568      1.1  reinoud }
    569      1.1  reinoud 
    570      1.1  reinoud /* --------------------------------------------------------------------- */
    571      1.1  reinoud 
    572      1.1  reinoud /* disc strategy dispatchers */
    573      1.1  reinoud 
    574      1.1  reinoud int
    575      1.1  reinoud udf_create_logvol_dscr(struct udf_mount *ump, struct udf_node *udf_node, struct long_ad *icb,
    576      1.1  reinoud 	union dscrptr **dscrptr)
    577      1.1  reinoud {
    578      1.1  reinoud 	struct udf_strategy *strategy = ump->strategy;
    579      1.1  reinoud 	struct udf_strat_args args;
    580      1.1  reinoud 	int error;
    581      1.1  reinoud 
    582      1.4  reinoud 	KASSERT(strategy);
    583      1.1  reinoud 	args.ump  = ump;
    584      1.1  reinoud 	args.udf_node = udf_node;
    585      1.1  reinoud 	args.icb  = icb;
    586      1.1  reinoud 	args.dscr = NULL;
    587      1.1  reinoud 
    588      1.1  reinoud 	error = (strategy->create_logvol_dscr)(&args);
    589      1.1  reinoud 	*dscrptr = args.dscr;
    590      1.1  reinoud 
    591      1.1  reinoud 	return error;
    592      1.1  reinoud }
    593      1.1  reinoud 
    594      1.1  reinoud 
    595      1.1  reinoud void
    596      1.1  reinoud udf_free_logvol_dscr(struct udf_mount *ump, struct long_ad *icb,
    597      1.1  reinoud 	void *dscr)
    598      1.1  reinoud {
    599      1.1  reinoud 	struct udf_strategy *strategy = ump->strategy;
    600      1.1  reinoud 	struct udf_strat_args args;
    601      1.1  reinoud 
    602      1.4  reinoud 	KASSERT(strategy);
    603      1.1  reinoud 	args.ump  = ump;
    604      1.1  reinoud 	args.icb  = icb;
    605      1.1  reinoud 	args.dscr = dscr;
    606      1.1  reinoud 
    607      1.1  reinoud 	(strategy->free_logvol_dscr)(&args);
    608      1.1  reinoud }
    609      1.1  reinoud 
    610      1.1  reinoud 
    611      1.1  reinoud int
    612      1.1  reinoud udf_read_logvol_dscr(struct udf_mount *ump, struct long_ad *icb,
    613      1.1  reinoud 	union dscrptr **dscrptr)
    614      1.1  reinoud {
    615      1.1  reinoud 	struct udf_strategy *strategy = ump->strategy;
    616      1.1  reinoud 	struct udf_strat_args args;
    617      1.1  reinoud 	int error;
    618      1.1  reinoud 
    619      1.4  reinoud 	KASSERT(strategy);
    620      1.1  reinoud 	args.ump  = ump;
    621      1.1  reinoud 	args.icb  = icb;
    622      1.1  reinoud 	args.dscr = NULL;
    623      1.1  reinoud 
    624      1.1  reinoud 	error = (strategy->read_logvol_dscr)(&args);
    625      1.1  reinoud 	*dscrptr = args.dscr;
    626      1.1  reinoud 
    627      1.1  reinoud 	return error;
    628      1.1  reinoud }
    629      1.1  reinoud 
    630      1.1  reinoud 
    631      1.1  reinoud int
    632      1.1  reinoud udf_write_logvol_dscr(struct udf_node *udf_node, union dscrptr *dscr,
    633      1.1  reinoud 	struct long_ad *icb, int waitfor)
    634      1.1  reinoud {
    635      1.1  reinoud 	struct udf_strategy *strategy = udf_node->ump->strategy;
    636      1.1  reinoud 	struct udf_strat_args args;
    637      1.1  reinoud 	int error;
    638      1.1  reinoud 
    639      1.4  reinoud 	KASSERT(strategy);
    640      1.1  reinoud 	args.ump      = udf_node->ump;
    641      1.1  reinoud 	args.udf_node = udf_node;
    642      1.1  reinoud 	args.icb      = icb;
    643      1.1  reinoud 	args.dscr     = dscr;
    644      1.1  reinoud 	args.waitfor  = waitfor;
    645      1.1  reinoud 
    646      1.1  reinoud 	error = (strategy->write_logvol_dscr)(&args);
    647      1.1  reinoud 	return error;
    648      1.1  reinoud }
    649      1.1  reinoud 
    650      1.1  reinoud 
    651      1.1  reinoud void
    652      1.1  reinoud udf_discstrat_queuebuf(struct udf_mount *ump, struct buf *nestbuf)
    653      1.1  reinoud {
    654      1.1  reinoud 	struct udf_strategy *strategy = ump->strategy;
    655      1.1  reinoud 	struct udf_strat_args args;
    656      1.1  reinoud 
    657      1.4  reinoud 	KASSERT(strategy);
    658      1.1  reinoud 	args.ump = ump;
    659      1.1  reinoud 	args.nestbuf = nestbuf;
    660      1.1  reinoud 
    661      1.1  reinoud 	(strategy->queuebuf)(&args);
    662      1.1  reinoud }
    663      1.1  reinoud 
    664      1.1  reinoud 
    665      1.1  reinoud void
    666      1.1  reinoud udf_discstrat_init(struct udf_mount *ump)
    667      1.1  reinoud {
    668      1.1  reinoud 	struct udf_strategy *strategy = ump->strategy;
    669      1.1  reinoud 	struct udf_strat_args args;
    670      1.1  reinoud 
    671      1.4  reinoud 	KASSERT(strategy);
    672      1.1  reinoud 	args.ump = ump;
    673      1.1  reinoud 	(strategy->discstrat_init)(&args);
    674      1.1  reinoud }
    675      1.1  reinoud 
    676      1.1  reinoud 
    677      1.1  reinoud void udf_discstrat_finish(struct udf_mount *ump)
    678      1.1  reinoud {
    679      1.1  reinoud 	struct udf_strategy *strategy = ump->strategy;
    680      1.1  reinoud 	struct udf_strat_args args;
    681      1.1  reinoud 
    682      1.4  reinoud 	/* strategy might not have been set, so ignore if not set */
    683      1.4  reinoud 	if (strategy) {
    684      1.4  reinoud 		args.ump = ump;
    685      1.4  reinoud 		(strategy->discstrat_finish)(&args);
    686      1.4  reinoud 	}
    687      1.1  reinoud }
    688      1.1  reinoud 
    689      1.1  reinoud /* --------------------------------------------------------------------- */
    690      1.1  reinoud 
    691