Home | History | Annotate | Line # | Download | only in udf
udf_strat_direct.c revision 1.1.10.3
      1 /* $NetBSD: udf_strat_direct.c,v 1.1.10.3 2008/09/18 04:36:56 wrstuden Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2006, 2008 Reinoud Zandijk
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 #ifndef lint
     31 __KERNEL_RCSID(0, "$NetBSD: udf_strat_direct.c,v 1.1.10.3 2008/09/18 04:36:56 wrstuden Exp $");
     32 #endif /* not lint */
     33 
     34 
     35 #if defined(_KERNEL_OPT)
     36 #include "opt_quota.h"
     37 #include "opt_compat_netbsd.h"
     38 #endif
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/sysctl.h>
     43 #include <sys/namei.h>
     44 #include <sys/proc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/vnode.h>
     47 #include <miscfs/genfs/genfs_node.h>
     48 #include <sys/mount.h>
     49 #include <sys/buf.h>
     50 #include <sys/file.h>
     51 #include <sys/device.h>
     52 #include <sys/disklabel.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/malloc.h>
     55 #include <sys/dirent.h>
     56 #include <sys/stat.h>
     57 #include <sys/conf.h>
     58 #include <sys/kauth.h>
     59 #include <sys/kthread.h>
     60 #include <dev/clock_subr.h>
     61 
     62 #include <fs/udf/ecma167-udf.h>
     63 #include <fs/udf/udf_mount.h>
     64 
     65 #include "udf.h"
     66 #include "udf_subr.h"
     67 #include "udf_bswap.h"
     68 
     69 
     70 #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
     71 #define PRIV(ump) ((struct strat_private *) ump->strategy_private)
     72 
     73 /* --------------------------------------------------------------------- */
     74 
     75 /* BUFQ's */
     76 #define UDF_SHED_MAX 3
     77 
     78 #define UDF_SHED_READING	0
     79 #define UDF_SHED_WRITING	1
     80 #define UDF_SHED_SEQWRITING	2
     81 
     82 
     83 struct strat_private {
     84 	struct pool		desc_pool;	 /* node descriptors */
     85 };
     86 
     87 /* --------------------------------------------------------------------- */
     88 
     89 static void
     90 udf_wr_nodedscr_callback(struct buf *buf)
     91 {
     92 	struct udf_node *udf_node;
     93 
     94 	KASSERT(buf);
     95 	KASSERT(buf->b_data);
     96 
     97 	/* called when write action is done */
     98 	DPRINTF(WRITE, ("udf_wr_nodedscr_callback(): node written out\n"));
     99 
    100 	udf_node = VTOI(buf->b_vp);
    101 	if (udf_node == NULL) {
    102 		putiobuf(buf);
    103 		printf("udf_wr_node_callback: NULL node?\n");
    104 		return;
    105 	}
    106 
    107 	/* XXX right flags to mark dirty again on error? */
    108 	if (buf->b_error) {
    109 		/* write error on `defect free' media??? how to solve? */
    110 		/* XXX lookup UDF standard for unallocatable space */
    111 		udf_node->i_flags |= IN_MODIFIED | IN_ACCESSED;
    112 	}
    113 
    114 	/* decrement outstanding_nodedscr */
    115 	KASSERT(udf_node->outstanding_nodedscr >= 1);
    116 	udf_node->outstanding_nodedscr--;
    117 	if (udf_node->outstanding_nodedscr == 0) {
    118 		/* unlock the node */
    119 		KASSERT(udf_node->i_flags & IN_CALLBACK_ULK);
    120 		UDF_UNLOCK_NODE(udf_node, IN_CALLBACK_ULK);
    121 
    122 		wakeup(&udf_node->outstanding_nodedscr);
    123 	}
    124 	/* unreference the vnode so it can be recycled */
    125 	holdrele(udf_node->vnode);
    126 
    127 	putiobuf(buf);
    128 }
    129 
    130 /* --------------------------------------------------------------------- */
    131 
    132 static int
    133 udf_getblank_nodedscr_direct(struct udf_strat_args *args)
    134 {
    135 	union dscrptr   **dscrptr = &args->dscr;
    136 	struct udf_mount *ump = args->ump;
    137 	struct strat_private *priv = PRIV(ump);
    138 	uint32_t lb_size;
    139 
    140 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    141 	*dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
    142 	memset(*dscrptr, 0, lb_size);
    143 
    144 	return 0;
    145 }
    146 
    147 
    148 static void
    149 udf_free_nodedscr_direct(struct udf_strat_args *args)
    150 {
    151 	union dscrptr    *dscr = args->dscr;
    152 	struct udf_mount *ump  = args->ump;
    153 	struct strat_private *priv = PRIV(ump);
    154 
    155 	pool_put(&priv->desc_pool, dscr);
    156 }
    157 
    158 
    159 static int
    160 udf_read_nodedscr_direct(struct udf_strat_args *args)
    161 {
    162 	union dscrptr   **dscrptr = &args->dscr;
    163 	union dscrptr    *tmpdscr;
    164 	struct udf_mount *ump = args->ump;
    165 	struct long_ad   *icb = args->icb;
    166 	struct strat_private *priv = PRIV(ump);
    167 	uint32_t lb_size;
    168 	uint32_t sector, dummy;
    169 	int error;
    170 
    171 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    172 
    173 	error = udf_translate_vtop(ump, icb, &sector, &dummy);
    174 	if (error)
    175 		return error;
    176 
    177 	/* try to read in fe/efe */
    178 	error = udf_read_phys_dscr(ump, sector, M_UDFTEMP, &tmpdscr);
    179 	if (error)
    180 		return error;
    181 
    182 	*dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
    183 	memcpy(*dscrptr, tmpdscr, lb_size);
    184 	free(tmpdscr, M_UDFTEMP);
    185 
    186 	return 0;
    187 }
    188 
    189 
    190 static int
    191 udf_write_nodedscr_direct(struct udf_strat_args *args)
    192 {
    193 	struct udf_mount *ump      = args->ump;
    194 	struct udf_node  *udf_node = args->udf_node;
    195 	union dscrptr    *dscr     = args->dscr;
    196 	struct long_ad   *icb      = args->icb;
    197 	int               waitfor  = args->waitfor;
    198 	uint32_t logsector, sector, dummy;
    199 	int error, vpart;
    200 
    201 	/*
    202 	 * we have to decide if we write it out sequential or at its fixed
    203 	 * position by examining the partition its (to be) written on.
    204 	 */
    205 	vpart     = udf_rw16(udf_node->loc.loc.part_num);
    206 	logsector = udf_rw32(icb->loc.lb_num);
    207 	KASSERT(ump->vtop_tp[vpart] != UDF_VTOP_TYPE_VIRT);
    208 
    209 	sector = 0;
    210 	error  = udf_translate_vtop(ump, icb, &sector, &dummy);
    211 	if (error)
    212 		goto out;
    213 
    214 	/* add reference to the vnode to prevent recycling */
    215 	vhold(udf_node->vnode);
    216 
    217 	if (waitfor) {
    218 		DPRINTF(WRITE, ("udf_write_nodedscr: sync write\n"));
    219 
    220 		error = udf_write_phys_dscr_sync(ump, udf_node, UDF_C_NODE,
    221 			dscr, sector, logsector);
    222 	} else {
    223 		DPRINTF(WRITE, ("udf_write_nodedscr: no wait, async write\n"));
    224 
    225 		error = udf_write_phys_dscr_async(ump, udf_node, UDF_C_NODE,
    226 			dscr, sector, logsector, udf_wr_nodedscr_callback);
    227 		/* will be UNLOCKED in call back */
    228 		return error;
    229 	}
    230 
    231 	holdrele(udf_node->vnode);
    232 out:
    233 	udf_node->outstanding_nodedscr--;
    234 	if (udf_node->outstanding_nodedscr == 0) {
    235 		UDF_UNLOCK_NODE(udf_node, 0);
    236 		wakeup(&udf_node->outstanding_nodedscr);
    237 	}
    238 
    239 	return error;
    240 }
    241 
    242 /* --------------------------------------------------------------------- */
    243 
    244 static void
    245 udf_queue_buf_direct(struct udf_strat_args *args)
    246 {
    247 	struct udf_mount *ump = args->ump;
    248 	struct buf *buf = args->nestbuf;
    249 	struct buf *nestbuf;
    250 	struct desc_tag *tag;
    251 	struct long_ad *node_ad_cpy;
    252 	uint64_t *lmapping, *pmapping, *lmappos, blknr, run_start;
    253 	uint32_t our_sectornr, sectornr;
    254 	uint32_t lb_size, buf_offset, rbuflen, bpos;
    255 	uint16_t vpart_num;
    256 	uint8_t *fidblk;
    257 	off_t rblk;
    258 	int sector_size = ump->discinfo.sector_size;
    259 	int blks = sector_size / DEV_BSIZE;
    260 	int len, buf_len, sector, sectors, run_length;
    261 	int what, class, queue;
    262 
    263 	KASSERT(ump);
    264 	KASSERT(buf);
    265 	KASSERT(buf->b_iodone == nestiobuf_iodone);
    266 
    267 	what = buf->b_udf_c_type;
    268 	queue = UDF_SHED_READING;
    269 	if ((buf->b_flags & B_READ) == 0) {
    270 		/* writing */
    271 		queue = UDF_SHED_SEQWRITING;
    272 		if (what == UDF_C_DSCR)
    273 			queue = UDF_SHED_WRITING;
    274 		if (what == UDF_C_NODE)
    275 			queue = UDF_SHED_WRITING;
    276 	}
    277 
    278 	/* use disc sheduler */
    279 	class = ump->discinfo.mmc_class;
    280 	KASSERT((class == MMC_CLASS_UNKN) || (class == MMC_CLASS_DISC) ||
    281 		(ump->discinfo.mmc_cur & MMC_CAP_HW_DEFECTFREE));
    282 
    283 	if (queue == UDF_SHED_READING) {
    284 		DPRINTF(SHEDULE, ("\nudf_issue_buf READ %p : sector %d type %d,"
    285 			"b_resid %d, b_bcount %d, b_bufsize %d\n",
    286 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
    287 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
    288 		VOP_STRATEGY(ump->devvp, buf);
    289 		return;
    290 	}
    291 
    292 	/* (sectorsize == lb_size) for UDF */
    293 	lb_size      = udf_rw32(ump->logical_vol->lb_size);
    294 	blknr        = buf->b_blkno;
    295 	our_sectornr = blknr / blks;
    296 
    297 	if (queue == UDF_SHED_WRITING) {
    298 		DPRINTF(SHEDULE, ("\nudf_issue_buf WRITE %p : sector %d "
    299 			"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
    300 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
    301 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
    302 		/* if we have FIDs fixup using buffer's sector number(s) */
    303 		if (buf->b_udf_c_type == UDF_C_FIDS) {
    304 			panic("UDF_C_FIDS in SHED_WRITING!\n");
    305 			buf_len = buf->b_bcount;
    306 			sectornr = our_sectornr;
    307 			bpos = 0;
    308 			while (buf_len) {
    309 				len = MIN(buf_len, sector_size);
    310 				fidblk = (uint8_t *) buf->b_data + bpos;
    311 				udf_fixup_fid_block(fidblk, sector_size,
    312 					0, len, sectornr);
    313 				sectornr++;
    314 				bpos += len;
    315 				buf_len -= len;
    316 			}
    317 		}
    318 		udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
    319 		VOP_STRATEGY(ump->devvp, buf);
    320 		return;
    321 	}
    322 
    323 	/* UDF_SHED_SEQWRITING */
    324 	KASSERT(queue == UDF_SHED_SEQWRITING);
    325 	DPRINTF(SHEDULE, ("\nudf_issue_buf SEQWRITE %p : sector XXXX "
    326 		"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
    327 		buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount,
    328 		buf->b_bufsize));
    329 
    330 	/*
    331 	 * Buffers should not have been allocated to disc addresses yet on
    332 	 * this queue. Note that a buffer can get multiple extents allocated.
    333 	 *
    334 	 * lmapping contains lb_num relative to base partition.
    335 	 */
    336 	lmapping    = ump->la_lmapping;
    337 	node_ad_cpy = ump->la_node_ad_cpy;
    338 
    339 	/* logically allocate buf and map it in the file */
    340 	udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num);
    341 
    342 	/* if we have FIDs, fixup using the new allocation table */
    343 	if (buf->b_udf_c_type == UDF_C_FIDS) {
    344 		buf_len = buf->b_bcount;
    345 		bpos = 0;
    346 		lmappos = lmapping;
    347 		while (buf_len) {
    348 			sectornr = *lmappos++;
    349 			len = MIN(buf_len, sector_size);
    350 			fidblk = (uint8_t *) buf->b_data + bpos;
    351 			udf_fixup_fid_block(fidblk, sector_size,
    352 				0, len, sectornr);
    353 			bpos += len;
    354 			buf_len -= len;
    355 		}
    356 	}
    357 	if (buf->b_udf_c_type == UDF_C_METADATA_SBM) {
    358 		if (buf->b_lblkno == 0) {
    359 			/* update the tag location inside */
    360 			tag = (struct desc_tag *) buf->b_data;
    361 			tag->tag_loc = udf_rw32(*lmapping);
    362 			udf_validate_tag_and_crc_sums(buf->b_data);
    363 		}
    364 	}
    365 	udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
    366 
    367 	/*
    368 	 * Translate new mappings in lmapping to pmappings and try to
    369 	 * conglomerate extents to reduce the number of writes.
    370 	 *
    371 	 * pmapping to contain lb_nums as used for disc adressing.
    372 	 */
    373 	pmapping = ump->la_pmapping;
    374 	sectors  = (buf->b_bcount + sector_size -1) / sector_size;
    375 	udf_translate_vtop_list(ump, sectors, vpart_num, lmapping, pmapping);
    376 
    377 	for (sector = 0; sector < sectors; sector++) {
    378 		buf_offset = sector * sector_size;
    379 		DPRINTF(WRITE, ("\tprocessing rel sector %d\n", sector));
    380 
    381 		DPRINTF(WRITE, ("\tissue write sector %"PRIu64"\n",
    382 			pmapping[sector]));
    383 
    384 		run_start  = pmapping[sector];
    385 		run_length = 1;
    386 		while (sector < sectors-1) {
    387 			if (pmapping[sector+1] != pmapping[sector]+1)
    388 				break;
    389 			run_length++;
    390 			sector++;
    391 		}
    392 
    393 		/* nest an iobuf for the extent */
    394 		rbuflen = run_length *  sector_size;
    395 		rblk    = run_start  * (sector_size/DEV_BSIZE);
    396 
    397 		nestbuf = getiobuf(NULL, true);
    398 		nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
    399 		/* nestbuf is B_ASYNC */
    400 
    401 		/* identify this nestbuf */
    402 		nestbuf->b_lblkno   = sector;
    403 		assert(nestbuf->b_vp == buf->b_vp);
    404 
    405 		/* CD shedules on raw blkno */
    406 		nestbuf->b_blkno      = rblk;
    407 		nestbuf->b_proc       = NULL;
    408 		nestbuf->b_rawblkno   = rblk;
    409 		nestbuf->b_udf_c_type = UDF_C_PROCESSED;
    410 
    411 		VOP_STRATEGY(ump->devvp, nestbuf);
    412 	}
    413 }
    414 
    415 
    416 static void
    417 udf_discstrat_init_direct(struct udf_strat_args *args)
    418 {
    419 	struct udf_mount  *ump = args->ump;
    420 	struct strat_private *priv = PRIV(ump);
    421 	uint32_t lb_size;
    422 
    423 	KASSERT(priv == NULL);
    424 	ump->strategy_private = malloc(sizeof(struct strat_private),
    425 		M_UDFTEMP, M_WAITOK);
    426 	priv = ump->strategy_private;
    427 	memset(priv, 0 , sizeof(struct strat_private));
    428 
    429 	/*
    430 	 * Initialise pool for descriptors associated with nodes. This is done
    431 	 * in lb_size units though currently lb_size is dictated to be
    432 	 * sector_size.
    433 	 */
    434 	memset(&priv->desc_pool, 0, sizeof(struct pool));
    435 
    436 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    437 	pool_init(&priv->desc_pool, lb_size, 0, 0, 0, "udf_desc_pool", NULL,
    438 	    IPL_NONE);
    439 }
    440 
    441 
    442 static void
    443 udf_discstrat_finish_direct(struct udf_strat_args *args)
    444 {
    445 	struct udf_mount *ump = args->ump;
    446 	struct strat_private *priv = PRIV(ump);
    447 
    448 	/* destroy our pool */
    449 	pool_destroy(&priv->desc_pool);
    450 
    451 	/* free our private space */
    452 	free(ump->strategy_private, M_UDFTEMP);
    453 	ump->strategy_private = NULL;
    454 }
    455 
    456 /* --------------------------------------------------------------------- */
    457 
    458 struct udf_strategy udf_strat_direct =
    459 {
    460 	udf_getblank_nodedscr_direct,
    461 	udf_free_nodedscr_direct,
    462 	udf_read_nodedscr_direct,
    463 	udf_write_nodedscr_direct,
    464 	udf_queue_buf_direct,
    465 	udf_discstrat_init_direct,
    466 	udf_discstrat_finish_direct
    467 };
    468 
    469