Home | History | Annotate | Line # | Download | only in udf
udf_strat_sequential.c revision 1.5.4.3
      1 /* $NetBSD: udf_strat_sequential.c,v 1.5.4.3 2009/06/01 17:11:35 liamjfoy 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_sequential.c,v 1.5.4.3 2009/06/01 17:11:35 liamjfoy 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 struct strat_private {
     83 	struct pool		 desc_pool;	 	/* node descriptors */
     84 
     85 	lwp_t			*queue_lwp;
     86 	kcondvar_t		 discstrat_cv;		/* to wait on       */
     87 	kmutex_t		 discstrat_mutex;	/* disc strategy    */
     88 
     89 	int			 run_thread;		/* thread control */
     90 	int			 cur_queue;
     91 
     92 	struct disk_strategy	 old_strategy_setting;
     93 	struct bufq_state	*queues[UDF_SHED_MAX];
     94 	struct timespec		 last_queued[UDF_SHED_MAX];
     95 };
     96 
     97 
     98 /* --------------------------------------------------------------------- */
     99 
    100 static void
    101 udf_wr_nodedscr_callback(struct buf *buf)
    102 {
    103 	struct udf_node *udf_node;
    104 
    105 	KASSERT(buf);
    106 	KASSERT(buf->b_data);
    107 
    108 	/* called when write action is done */
    109 	DPRINTF(WRITE, ("udf_wr_nodedscr_callback(): node written out\n"));
    110 
    111 	udf_node = VTOI(buf->b_vp);
    112 	if (udf_node == NULL) {
    113 		putiobuf(buf);
    114 		printf("udf_wr_node_callback: NULL node?\n");
    115 		return;
    116 	}
    117 
    118 	/* XXX right flags to mark dirty again on error? */
    119 	if (buf->b_error) {
    120 		udf_node->i_flags |= IN_MODIFIED | IN_ACCESSED;
    121 		/* XXX TODO reshedule on error */
    122 	}
    123 
    124 	/* decrement outstanding_nodedscr */
    125 	KASSERT(udf_node->outstanding_nodedscr >= 1);
    126 	udf_node->outstanding_nodedscr--;
    127 	if (udf_node->outstanding_nodedscr == 0) {
    128 		/* first unlock the node */
    129 		UDF_UNLOCK_NODE(udf_node, 0);
    130 		wakeup(&udf_node->outstanding_nodedscr);
    131 	}
    132 
    133 	/* unreference the vnode so it can be recycled */
    134 	holdrele(udf_node->vnode);
    135 
    136 	putiobuf(buf);
    137 }
    138 
    139 /* --------------------------------------------------------------------- */
    140 
    141 static int
    142 udf_create_logvol_dscr_seq(struct udf_strat_args *args)
    143 {
    144 	union dscrptr   **dscrptr = &args->dscr;
    145 	struct udf_mount *ump = args->ump;
    146 	struct strat_private *priv = PRIV(ump);
    147 	uint32_t lb_size;
    148 
    149 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    150 	*dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
    151 	memset(*dscrptr, 0, lb_size);
    152 
    153 	return 0;
    154 }
    155 
    156 
    157 static void
    158 udf_free_logvol_dscr_seq(struct udf_strat_args *args)
    159 {
    160 	union dscrptr    *dscr = args->dscr;
    161 	struct udf_mount *ump  = args->ump;
    162 	struct strat_private *priv = PRIV(ump);
    163 
    164 	pool_put(&priv->desc_pool, dscr);
    165 }
    166 
    167 
    168 static int
    169 udf_read_logvol_dscr_seq(struct udf_strat_args *args)
    170 {
    171 	union dscrptr   **dscrptr = &args->dscr;
    172 	union dscrptr    *tmpdscr;
    173 	struct udf_mount *ump = args->ump;
    174 	struct long_ad   *icb = args->icb;
    175 	struct strat_private *priv = PRIV(ump);
    176 	uint32_t lb_size;
    177 	uint32_t sector, dummy;
    178 	int error;
    179 
    180 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    181 
    182 	error = udf_translate_vtop(ump, icb, &sector, &dummy);
    183 	if (error)
    184 		return error;
    185 
    186 	/* try to read in fe/efe */
    187 	error = udf_read_phys_dscr(ump, sector, M_UDFTEMP, &tmpdscr);
    188 	if (error)
    189 		return error;
    190 
    191 	*dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
    192 	memcpy(*dscrptr, tmpdscr, lb_size);
    193 	free(tmpdscr, M_UDFTEMP);
    194 
    195 	return 0;
    196 }
    197 
    198 
    199 static int
    200 udf_write_logvol_dscr_seq(struct udf_strat_args *args)
    201 {
    202 	union dscrptr    *dscr     = args->dscr;
    203 	struct udf_mount *ump      = args->ump;
    204 	struct udf_node  *udf_node = args->udf_node;
    205 	struct long_ad   *icb      = args->icb;
    206 	int               waitfor  = args->waitfor;
    207 	uint32_t logsectornr, sectornr, dummy;
    208 	int error, vpart;
    209 
    210 	/*
    211 	 * we have to decide if we write it out sequential or at its fixed
    212 	 * position by examining the partition its (to be) written on.
    213 	 */
    214 	vpart       = udf_rw16(udf_node->loc.loc.part_num);
    215 	logsectornr = udf_rw32(icb->loc.lb_num);
    216 	sectornr    = 0;
    217 	if (ump->vtop_tp[vpart] != UDF_VTOP_TYPE_VIRT) {
    218 		error = udf_translate_vtop(ump, icb, &sectornr, &dummy);
    219 		if (error)
    220 			goto out;
    221 	}
    222 
    223 	/* add reference to the vnode to prevent recycling */
    224 	vhold(udf_node->vnode);
    225 
    226 	if (waitfor) {
    227 		DPRINTF(WRITE, ("udf_write_logvol_dscr: sync write\n"));
    228 
    229 		error = udf_write_phys_dscr_sync(ump, udf_node, UDF_C_NODE,
    230 			dscr, sectornr, logsectornr);
    231 	} else {
    232 		DPRINTF(WRITE, ("udf_write_logvol_dscr: no wait, async write\n"));
    233 
    234 		error = udf_write_phys_dscr_async(ump, udf_node, UDF_C_NODE,
    235 			dscr, sectornr, logsectornr, udf_wr_nodedscr_callback);
    236 		/* will be UNLOCKED in call back */
    237 		return error;
    238 	}
    239 
    240 	holdrele(udf_node->vnode);
    241 out:
    242 	udf_node->outstanding_nodedscr--;
    243 	if (udf_node->outstanding_nodedscr == 0) {
    244 		UDF_UNLOCK_NODE(udf_node, 0);
    245 		wakeup(&udf_node->outstanding_nodedscr);
    246 	}
    247 
    248 	return error;
    249 }
    250 
    251 /* --------------------------------------------------------------------- */
    252 
    253 /*
    254  * Main file-system specific sheduler. Due to the nature of optical media
    255  * sheduling can't be performed in the traditional way. Most OS
    256  * implementations i've seen thus read or write a file atomically giving all
    257  * kinds of side effects.
    258  *
    259  * This implementation uses a kernel thread to shedule the queued requests in
    260  * such a way that is semi-optimal for optical media; this means aproximately
    261  * (R*|(Wr*|Ws*))* since switching between reading and writing is expensive in
    262  * time.
    263  */
    264 
    265 static void
    266 udf_queuebuf_seq(struct udf_strat_args *args)
    267 {
    268 	struct udf_mount *ump = args->ump;
    269 	struct buf *nestbuf = args->nestbuf;
    270 	struct strat_private *priv = PRIV(ump);
    271 	int queue;
    272 	int what;
    273 
    274 	KASSERT(ump);
    275 	KASSERT(nestbuf);
    276 	KASSERT(nestbuf->b_iodone == nestiobuf_iodone);
    277 
    278 	what = nestbuf->b_udf_c_type;
    279 	queue = UDF_SHED_READING;
    280 	if ((nestbuf->b_flags & B_READ) == 0) {
    281 		/* writing */
    282 		queue = UDF_SHED_SEQWRITING;
    283 		if (what == UDF_C_ABSOLUTE)
    284 			queue = UDF_SHED_WRITING;
    285 	}
    286 
    287 	/* use our own sheduler lists for more complex sheduling */
    288 	mutex_enter(&priv->discstrat_mutex);
    289 		BUFQ_PUT(priv->queues[queue], nestbuf);
    290 		vfs_timestamp(&priv->last_queued[queue]);
    291 	mutex_exit(&priv->discstrat_mutex);
    292 
    293 	/* signal our thread that there might be something to do */
    294 	cv_signal(&priv->discstrat_cv);
    295 }
    296 
    297 /* --------------------------------------------------------------------- */
    298 
    299 /* TODO convert to lb_size */
    300 static void
    301 udf_VAT_mapping_update(struct udf_mount *ump, struct buf *buf, uint32_t lb_map)
    302 {
    303 	union dscrptr    *fdscr = (union dscrptr *) buf->b_data;
    304 	struct vnode     *vp = buf->b_vp;
    305 	struct udf_node  *udf_node = VTOI(vp);
    306 	uint32_t lb_size, blks;
    307 	uint32_t lb_num;
    308 	uint32_t udf_rw32_lbmap;
    309 	int c_type = buf->b_udf_c_type;
    310 	int error;
    311 
    312 	/* only interested when we're using a VAT */
    313 	KASSERT(ump->vat_node);
    314 	KASSERT(ump->vtop_alloc[ump->node_part] == UDF_ALLOC_VAT);
    315 
    316 	/* only nodes are recorded in the VAT */
    317 	/* NOTE: and the fileset descriptor (FIXME ?) */
    318 	if (c_type != UDF_C_NODE)
    319 		return;
    320 
    321 	/* we now have an UDF FE/EFE node on media with VAT (or VAT itself) */
    322 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    323 	blks = lb_size / DEV_BSIZE;
    324 
    325 	udf_rw32_lbmap = udf_rw32(lb_map);
    326 
    327 	/* if we're the VAT itself, only update our assigned sector number */
    328 	if (udf_node == ump->vat_node) {
    329 		fdscr->tag.tag_loc = udf_rw32_lbmap;
    330 		udf_validate_tag_sum(fdscr);
    331 		DPRINTF(TRANSLATE, ("VAT assigned to sector %u\n",
    332 			udf_rw32(udf_rw32_lbmap)));
    333 		/* no use mapping the VAT node in the VAT */
    334 		return;
    335 	}
    336 
    337 	/* record new position in VAT file */
    338 	lb_num = udf_rw32(fdscr->tag.tag_loc);
    339 
    340 	/* lb_num = udf_rw32(udf_node->write_loc.loc.lb_num); */
    341 
    342 	DPRINTF(TRANSLATE, ("VAT entry change (log %u -> phys %u)\n",
    343 			lb_num, lb_map));
    344 
    345 	/* VAT should be the longer than this write, can't go wrong */
    346 	KASSERT(lb_num <= ump->vat_entries);
    347 
    348 	mutex_enter(&ump->allocate_mutex);
    349 	error = udf_vat_write(ump->vat_node,
    350 			(uint8_t *) &udf_rw32_lbmap, 4,
    351 			ump->vat_offset + lb_num * 4);
    352 	mutex_exit(&ump->allocate_mutex);
    353 
    354 	if (error)
    355 		panic( "udf_VAT_mapping_update: HELP! i couldn't "
    356 			"write in the VAT file ?\n");
    357 }
    358 
    359 
    360 static void
    361 udf_issue_buf(struct udf_mount *ump, int queue, struct buf *buf)
    362 {
    363 	union dscrptr *dscr;
    364 	struct long_ad *node_ad_cpy;
    365 	struct part_desc *pdesc;
    366 	uint64_t *lmapping, *lmappos, blknr;
    367 	uint32_t our_sectornr, sectornr, bpos;
    368 	uint32_t ptov;
    369 	uint16_t vpart_num;
    370 	uint8_t *fidblk;
    371 	int sector_size = ump->discinfo.sector_size;
    372 	int blks = sector_size / DEV_BSIZE;
    373 	int len, buf_len;
    374 
    375 	/* if reading, just pass to the device's STRATEGY */
    376 	if (queue == UDF_SHED_READING) {
    377 		DPRINTF(SHEDULE, ("\nudf_issue_buf READ %p : sector %d type %d,"
    378 			"b_resid %d, b_bcount %d, b_bufsize %d\n",
    379 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
    380 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
    381 		VOP_STRATEGY(ump->devvp, buf);
    382 		return;
    383 	}
    384 
    385 	blknr        = buf->b_blkno;
    386 	our_sectornr = blknr / blks;
    387 
    388 	if (queue == UDF_SHED_WRITING) {
    389 		DPRINTF(SHEDULE, ("\nudf_issue_buf WRITE %p : sector %d "
    390 			"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
    391 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
    392 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
    393 		KASSERT(buf->b_udf_c_type == UDF_C_ABSOLUTE);
    394 
    395 		// udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
    396 		VOP_STRATEGY(ump->devvp, buf);
    397 		return;
    398 	}
    399 
    400 	KASSERT(queue == UDF_SHED_SEQWRITING);
    401 	DPRINTF(SHEDULE, ("\nudf_issue_buf SEQWRITE %p : sector XXXX "
    402 		"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
    403 		buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount,
    404 		buf->b_bufsize));
    405 
    406 	/*
    407 	 * Buffers should not have been allocated to disc addresses yet on
    408 	 * this queue. Note that a buffer can get multiple extents allocated.
    409 	 *
    410 	 * lmapping contains lb_num relative to base partition.
    411 	 */
    412 	lmapping    = ump->la_lmapping;
    413 	node_ad_cpy = ump->la_node_ad_cpy;
    414 
    415 	/* logically allocate buf and map it in the file */
    416 	udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num);
    417 
    418 	/*
    419 	 * NOTE We are using the knowledge here that sequential media will
    420 	 * always be mapped linearly. Thus no use to explicitly translate the
    421 	 * lmapping list.
    422 	 */
    423 
    424 	/* calculate offset from physical base partition */
    425 	pdesc = ump->partitions[ump->vtop[vpart_num]];
    426 	ptov  = udf_rw32(pdesc->start_loc);
    427 
    428 	/* set buffers blkno to the physical block number */
    429 	buf->b_blkno = (*lmapping + ptov) * blks;
    430 
    431 	/* fixate floating descriptors */
    432 	if (buf->b_udf_c_type == UDF_C_FLOAT_DSCR) {
    433 		/* set our tag location to the absolute position */
    434 		dscr = (union dscrptr *) buf->b_data;
    435 		dscr->tag.tag_loc = udf_rw32(*lmapping + ptov);
    436 		udf_validate_tag_and_crc_sums(dscr);
    437 	}
    438 
    439 	/* update mapping in the VAT */
    440 	if (buf->b_udf_c_type == UDF_C_NODE) {
    441 		udf_VAT_mapping_update(ump, buf, *lmapping);
    442 		udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
    443 	}
    444 
    445 	/* if we have FIDs, fixup using the new allocation table */
    446 	if (buf->b_udf_c_type == UDF_C_FIDS) {
    447 		buf_len = buf->b_bcount;
    448 		bpos = 0;
    449 		lmappos = lmapping;
    450 		while (buf_len) {
    451 			sectornr = *lmappos++;
    452 			len = MIN(buf_len, sector_size);
    453 			fidblk = (uint8_t *) buf->b_data + bpos;
    454 			udf_fixup_fid_block(fidblk, sector_size,
    455 				0, len, sectornr);
    456 			bpos += len;
    457 			buf_len -= len;
    458 		}
    459 	}
    460 
    461 	VOP_STRATEGY(ump->devvp, buf);
    462 }
    463 
    464 
    465 static void
    466 udf_doshedule(struct udf_mount *ump)
    467 {
    468 	struct buf *buf;
    469 	struct timespec now, *last;
    470 	struct strat_private *priv = PRIV(ump);
    471 	void (*b_callback)(struct buf *);
    472 	int new_queue;
    473 	int error;
    474 
    475 	buf = BUFQ_GET(priv->queues[priv->cur_queue]);
    476 	if (buf) {
    477 		/* transfer from the current queue to the device queue */
    478 		mutex_exit(&priv->discstrat_mutex);
    479 
    480 		/* transform buffer to synchronous; XXX needed? */
    481 		b_callback = buf->b_iodone;
    482 		buf->b_iodone = NULL;
    483 		CLR(buf->b_flags, B_ASYNC);
    484 
    485 		/* issue and wait on completion */
    486 		udf_issue_buf(ump, priv->cur_queue, buf);
    487 		biowait(buf);
    488 
    489 		mutex_enter(&priv->discstrat_mutex);
    490 
    491 		/* if there is an error, repair this error, otherwise propagate */
    492 		if (buf->b_error && ((buf->b_flags & B_READ) == 0)) {
    493 			/* check what we need to do */
    494 			panic("UDF write error, can't handle yet!\n");
    495 		}
    496 
    497 		/* propagate result to higher layers */
    498 		if (b_callback) {
    499 			buf->b_iodone = b_callback;
    500 			(*buf->b_iodone)(buf);
    501 		}
    502 
    503 		return;
    504 	}
    505 
    506 	/* Check if we're idling in this state */
    507 	vfs_timestamp(&now);
    508 	last = &priv->last_queued[priv->cur_queue];
    509 	if (ump->discinfo.mmc_class == MMC_CLASS_CD) {
    510 		/* dont switch too fast for CD media; its expensive in time */
    511 		if (now.tv_sec - last->tv_sec < 3)
    512 			return;
    513 	}
    514 
    515 	/* check if we can/should switch */
    516 	new_queue = priv->cur_queue;
    517 
    518 	if (BUFQ_PEEK(priv->queues[UDF_SHED_READING]))
    519 		new_queue = UDF_SHED_READING;
    520 	if (BUFQ_PEEK(priv->queues[UDF_SHED_WRITING]))		/* only for unmount */
    521 		new_queue = UDF_SHED_WRITING;
    522 	if (BUFQ_PEEK(priv->queues[UDF_SHED_SEQWRITING]))
    523 		new_queue = UDF_SHED_SEQWRITING;
    524 	if (priv->cur_queue == UDF_SHED_READING) {
    525 		if (new_queue == UDF_SHED_SEQWRITING) {
    526 			/* TODO use flag to signal if this is needed */
    527 			mutex_exit(&priv->discstrat_mutex);
    528 
    529 			/* update trackinfo for data and metadata */
    530 			error = udf_update_trackinfo(ump,
    531 					&ump->data_track);
    532 			assert(error == 0);
    533 			error = udf_update_trackinfo(ump,
    534 					&ump->metadata_track);
    535 			assert(error == 0);
    536 			mutex_enter(&priv->discstrat_mutex);
    537 		}
    538 	}
    539 
    540 	if (new_queue != priv->cur_queue) {
    541 		DPRINTF(SHEDULE, ("switching from %d to %d\n",
    542 			priv->cur_queue, new_queue));
    543 	}
    544 
    545 	priv->cur_queue = new_queue;
    546 }
    547 
    548 
    549 static void
    550 udf_discstrat_thread(void *arg)
    551 {
    552 	struct udf_mount *ump = (struct udf_mount *) arg;
    553 	struct strat_private *priv = PRIV(ump);
    554 	int empty;
    555 
    556 	empty = 1;
    557 	mutex_enter(&priv->discstrat_mutex);
    558 	while (priv->run_thread || !empty) {
    559 		/* process the current selected queue */
    560 		udf_doshedule(ump);
    561 		empty  = (BUFQ_PEEK(priv->queues[UDF_SHED_READING]) == NULL);
    562 		empty &= (BUFQ_PEEK(priv->queues[UDF_SHED_WRITING]) == NULL);
    563 		empty &= (BUFQ_PEEK(priv->queues[UDF_SHED_SEQWRITING]) == NULL);
    564 
    565 		/* wait for more if needed */
    566 		if (empty)
    567 			cv_timedwait(&priv->discstrat_cv,
    568 				&priv->discstrat_mutex, hz/8);
    569 	}
    570 	mutex_exit(&priv->discstrat_mutex);
    571 
    572 	wakeup(&priv->run_thread);
    573 	kthread_exit(0);
    574 	/* not reached */
    575 }
    576 
    577 /* --------------------------------------------------------------------- */
    578 
    579 static void
    580 udf_discstrat_init_seq(struct udf_strat_args *args)
    581 {
    582 	struct udf_mount *ump = args->ump;
    583 	struct strat_private *priv = PRIV(ump);
    584 	struct disk_strategy dkstrat;
    585 	uint32_t lb_size;
    586 
    587 	KASSERT(ump);
    588 	KASSERT(ump->logical_vol);
    589 	KASSERT(priv == NULL);
    590 
    591 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    592 	KASSERT(lb_size > 0);
    593 
    594 	/* initialise our memory space */
    595 	ump->strategy_private = malloc(sizeof(struct strat_private),
    596 		M_UDFTEMP, M_WAITOK);
    597 	priv = ump->strategy_private;
    598 	memset(priv, 0 , sizeof(struct strat_private));
    599 
    600 	/* initialise locks */
    601 	cv_init(&priv->discstrat_cv, "udfstrat");
    602 	mutex_init(&priv->discstrat_mutex, MUTEX_DEFAULT, IPL_NONE);
    603 
    604 	/*
    605 	 * Initialise pool for descriptors associated with nodes. This is done
    606 	 * in lb_size units though currently lb_size is dictated to be
    607 	 * sector_size.
    608 	 */
    609 	pool_init(&priv->desc_pool, lb_size, 0, 0, 0, "udf_desc_pool", NULL,
    610 	    IPL_NONE);
    611 
    612 	/*
    613 	 * remember old device strategy method and explicit set method
    614 	 * `discsort' since we have our own more complex strategy that is not
    615 	 * implementable on the CD device and other strategies will get in the
    616 	 * way.
    617 	 */
    618 	memset(&priv->old_strategy_setting, 0,
    619 		sizeof(struct disk_strategy));
    620 	VOP_IOCTL(ump->devvp, DIOCGSTRATEGY, &priv->old_strategy_setting,
    621 		FREAD | FKIOCTL, NOCRED);
    622 	memset(&dkstrat, 0, sizeof(struct disk_strategy));
    623 	strcpy(dkstrat.dks_name, "discsort");
    624 	VOP_IOCTL(ump->devvp, DIOCSSTRATEGY, &dkstrat, FWRITE | FKIOCTL,
    625 		NOCRED);
    626 
    627 	/* initialise our internal sheduler */
    628 	priv->cur_queue = UDF_SHED_READING;
    629 	bufq_alloc(&priv->queues[UDF_SHED_READING], "disksort",
    630 		BUFQ_SORT_RAWBLOCK);
    631 	bufq_alloc(&priv->queues[UDF_SHED_WRITING], "disksort",
    632 		BUFQ_SORT_RAWBLOCK);
    633 	bufq_alloc(&priv->queues[UDF_SHED_SEQWRITING], "fcfs", 0);
    634 	vfs_timestamp(&priv->last_queued[UDF_SHED_READING]);
    635 	vfs_timestamp(&priv->last_queued[UDF_SHED_WRITING]);
    636 	vfs_timestamp(&priv->last_queued[UDF_SHED_SEQWRITING]);
    637 
    638 	/* create our disk strategy thread */
    639 	priv->run_thread = 1;
    640 	if (kthread_create(PRI_NONE, 0 /* KTHREAD_MPSAFE*/, NULL /* cpu_info*/,
    641 		udf_discstrat_thread, ump, &priv->queue_lwp,
    642 		"%s", "udf_rw")) {
    643 		panic("fork udf_rw");
    644 	}
    645 }
    646 
    647 
    648 static void
    649 udf_discstrat_finish_seq(struct udf_strat_args *args)
    650 {
    651 	struct udf_mount *ump = args->ump;
    652 	struct strat_private *priv = PRIV(ump);
    653 	int error;
    654 
    655 	if (ump == NULL)
    656 		return;
    657 
    658 	/* stop our sheduling thread */
    659 	KASSERT(priv->run_thread == 1);
    660 	priv->run_thread = 0;
    661 	wakeup(priv->queue_lwp);
    662 	do {
    663 		error = tsleep(&priv->run_thread, PRIBIO+1,
    664 			"udfshedfin", hz);
    665 	} while (error);
    666 	/* kthread should be finished now */
    667 
    668 	/* set back old device strategy method */
    669 	VOP_IOCTL(ump->devvp, DIOCSSTRATEGY, &priv->old_strategy_setting,
    670 			FWRITE, NOCRED);
    671 
    672 	/* destroy our pool */
    673 	pool_destroy(&priv->desc_pool);
    674 
    675 	/* free our private space */
    676 	free(ump->strategy_private, M_UDFTEMP);
    677 	ump->strategy_private = NULL;
    678 }
    679 
    680 /* --------------------------------------------------------------------- */
    681 
    682 struct udf_strategy udf_strat_sequential =
    683 {
    684 	udf_create_logvol_dscr_seq,
    685 	udf_free_logvol_dscr_seq,
    686 	udf_read_logvol_dscr_seq,
    687 	udf_write_logvol_dscr_seq,
    688 	udf_queuebuf_seq,
    689 	udf_discstrat_init_seq,
    690 	udf_discstrat_finish_seq
    691 };
    692 
    693 
    694