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