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