Home | History | Annotate | Line # | Download | only in udf
udf_strat_sequential.c revision 1.18
      1 /* $NetBSD: udf_strat_sequential.c,v 1.18 2022/08/27 05:31:59 skrll 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.18 2022/08/27 05:31:59 skrll Exp $");
     32 #endif /* not lint */
     33 
     34 
     35 #if defined(_KERNEL_OPT)
     36 #include "opt_compat_netbsd.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/sysctl.h>
     42 #include <sys/namei.h>
     43 #include <sys/proc.h>
     44 #include <sys/kernel.h>
     45 #include <sys/vnode.h>
     46 #include <miscfs/genfs/genfs_node.h>
     47 #include <sys/mount.h>
     48 #include <sys/buf.h>
     49 #include <sys/file.h>
     50 #include <sys/device.h>
     51 #include <sys/disklabel.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/malloc.h>
     54 #include <sys/dirent.h>
     55 #include <sys/stat.h>
     56 #include <sys/conf.h>
     57 #include <sys/kauth.h>
     58 #include <sys/kthread.h>
     59 #include <dev/clock_subr.h>
     60 
     61 #include <fs/udf/ecma167-udf.h>
     62 #include <fs/udf/udf_mount.h>
     63 
     64 #include "udf.h"
     65 #include "udf_subr.h"
     66 #include "udf_bswap.h"
     67 
     68 
     69 #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
     70 #define PRIV(ump) ((struct strat_private *) ump->strategy_private)
     71 
     72 /* --------------------------------------------------------------------- */
     73 
     74 /* BUFQ's */
     75 #define UDF_SHED_MAX 3
     76 
     77 #define UDF_SHED_READING	0
     78 #define UDF_SHED_WRITING	1
     79 #define UDF_SHED_SEQWRITING	2
     80 
     81 struct strat_private {
     82 	struct pool		 desc_pool;	 	/* node descriptors */
     83 
     84 	lwp_t			*queue_lwp;
     85 	kcondvar_t		 discstrat_cv;		/* to wait on       */
     86 	kmutex_t		 discstrat_mutex;	/* disc strategy    */
     87 
     88 	int			 run_thread;		/* thread control */
     89 	int			 sync_req;		/* 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 reschedule 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 	putiobuf(buf);
    134 }
    135 
    136 /* --------------------------------------------------------------------- */
    137 
    138 static int
    139 udf_create_logvol_dscr_seq(struct udf_strat_args *args)
    140 {
    141 	union dscrptr   **dscrptr = &args->dscr;
    142 	struct udf_mount *ump = args->ump;
    143 	struct strat_private *priv = PRIV(ump);
    144 	uint32_t lb_size;
    145 
    146 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    147 	*dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
    148 	memset(*dscrptr, 0, lb_size);
    149 
    150 	return 0;
    151 }
    152 
    153 
    154 static void
    155 udf_free_logvol_dscr_seq(struct udf_strat_args *args)
    156 {
    157 	union dscrptr    *dscr = args->dscr;
    158 	struct udf_mount *ump  = args->ump;
    159 	struct strat_private *priv = PRIV(ump);
    160 
    161 	pool_put(&priv->desc_pool, dscr);
    162 }
    163 
    164 
    165 static int
    166 udf_read_logvol_dscr_seq(struct udf_strat_args *args)
    167 {
    168 	union dscrptr   **dscrptr = &args->dscr;
    169 	union dscrptr    *tmpdscr;
    170 	struct udf_mount *ump = args->ump;
    171 	struct long_ad   *icb = args->icb;
    172 	struct strat_private *priv = PRIV(ump);
    173 	uint32_t lb_size;
    174 	uint32_t sector, dummy;
    175 	int error;
    176 
    177 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    178 
    179 	error = udf_translate_vtop(ump, icb, &sector, &dummy);
    180 	if (error)
    181 		return error;
    182 
    183 	/* try to read in fe/efe */
    184 	error = udf_read_phys_dscr(ump, sector, M_UDFTEMP, &tmpdscr);
    185 	if (error)
    186 		return error;
    187 
    188 	*dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
    189 	memcpy(*dscrptr, tmpdscr, lb_size);
    190 	free(tmpdscr, M_UDFTEMP);
    191 
    192 	return 0;
    193 }
    194 
    195 
    196 static int
    197 udf_write_logvol_dscr_seq(struct udf_strat_args *args)
    198 {
    199 	union dscrptr    *dscr     = args->dscr;
    200 	struct udf_mount *ump      = args->ump;
    201 	struct udf_node  *udf_node = args->udf_node;
    202 	struct long_ad   *icb      = args->icb;
    203 	int               waitfor  = args->waitfor;
    204 	uint32_t logsectornr, sectornr, dummy;
    205 	int error, vpart;
    206 
    207 	/*
    208 	 * we have to decide if we write it out sequential or at its fixed
    209 	 * position by examining the partition its (to be) written on.
    210 	 */
    211 	vpart       = udf_rw16(udf_node->loc.loc.part_num);
    212 	logsectornr = udf_rw32(icb->loc.lb_num);
    213 	sectornr    = 0;
    214 	if (ump->vtop_tp[vpart] != UDF_VTOP_TYPE_VIRT) {
    215 		error = udf_translate_vtop(ump, icb, &sectornr, &dummy);
    216 		if (error)
    217 			goto out;
    218 	}
    219 
    220 	if (waitfor) {
    221 		DPRINTF(WRITE, ("udf_write_logvol_dscr: sync write\n"));
    222 
    223 		error = udf_write_phys_dscr_sync(ump, udf_node, UDF_C_NODE,
    224 			dscr, sectornr, logsectornr);
    225 	} else {
    226 		DPRINTF(WRITE, ("udf_write_logvol_dscr: no wait, async write\n"));
    227 
    228 		error = udf_write_phys_dscr_async(ump, udf_node, UDF_C_NODE,
    229 			dscr, sectornr, logsectornr, udf_wr_nodedscr_callback);
    230 		/* will be UNLOCKED in call back */
    231 		return error;
    232 	}
    233 out:
    234 	udf_node->outstanding_nodedscr--;
    235 	if (udf_node->outstanding_nodedscr == 0) {
    236 		UDF_UNLOCK_NODE(udf_node, 0);
    237 		wakeup(&udf_node->outstanding_nodedscr);
    238 	}
    239 
    240 	return error;
    241 }
    242 
    243 /* --------------------------------------------------------------------- */
    244 
    245 /*
    246  * Main file-system specific scheduler. Due to the nature of optical media
    247  * scheduling can't be performed in the traditional way. Most OS
    248  * implementations i've seen thus read or write a file atomically giving all
    249  * kinds of side effects.
    250  *
    251  * This implementation uses a kernel thread to schedule the queued requests in
    252  * such a way that is semi-optimal for optical media; this means aproximately
    253  * (R*|(Wr*|Ws*))* since switching between reading and writing is expensive in
    254  * time.
    255  */
    256 
    257 static void
    258 udf_queuebuf_seq(struct udf_strat_args *args)
    259 {
    260 	struct udf_mount *ump = args->ump;
    261 	struct buf *nestbuf = args->nestbuf;
    262 	struct strat_private *priv = PRIV(ump);
    263 	int queue;
    264 	int what;
    265 
    266 	KASSERT(ump);
    267 	KASSERT(nestbuf);
    268 	KASSERT(nestbuf->b_iodone == nestiobuf_iodone);
    269 
    270 	what = nestbuf->b_udf_c_type;
    271 	queue = UDF_SHED_READING;
    272 	if ((nestbuf->b_flags & B_READ) == 0) {
    273 		/* writing */
    274 		queue = UDF_SHED_SEQWRITING;
    275 		if (what == UDF_C_ABSOLUTE)
    276 			queue = UDF_SHED_WRITING;
    277 	}
    278 
    279 	/* use our own scheduler lists for more complex scheduling */
    280 	mutex_enter(&priv->discstrat_mutex);
    281 		bufq_put(priv->queues[queue], nestbuf);
    282 		vfs_timestamp(&priv->last_queued[queue]);
    283 	mutex_exit(&priv->discstrat_mutex);
    284 
    285 	/* signal our thread that there might be something to do */
    286 	cv_signal(&priv->discstrat_cv);
    287 }
    288 
    289 /* --------------------------------------------------------------------- */
    290 
    291 static void
    292 udf_sync_caches_seq(struct udf_strat_args *args)
    293 {
    294 	struct udf_mount *ump = args->ump;
    295 	struct strat_private *priv = PRIV(ump);
    296 
    297 	/* we might be called during unmount inadvertedly, be on safe side */
    298 	if (!priv)
    299 		return;
    300 
    301 	/* signal our thread that there might be something to do */
    302 	priv->sync_req = 1;
    303 	cv_signal(&priv->discstrat_cv);
    304 
    305 	mutex_enter(&priv->discstrat_mutex);
    306 		while (priv->sync_req) {
    307 			cv_timedwait(&priv->discstrat_cv,
    308 				&priv->discstrat_mutex, hz/8);
    309 		}
    310 	mutex_exit(&priv->discstrat_mutex);
    311 }
    312 
    313 /* --------------------------------------------------------------------- */
    314 
    315 /* TODO convert to lb_size */
    316 static void
    317 udf_VAT_mapping_update(struct udf_mount *ump, struct buf *buf, uint32_t lb_map)
    318 {
    319 	union dscrptr    *fdscr = (union dscrptr *) buf->b_data;
    320 	struct vnode     *vp = buf->b_vp;
    321 	struct udf_node  *udf_node = VTOI(vp);
    322 	uint32_t lb_num;
    323 	uint32_t udf_rw32_lbmap;
    324 	int c_type = buf->b_udf_c_type;
    325 	int error;
    326 
    327 	/* only interested when we're using a VAT */
    328 	KASSERT(ump->vat_node);
    329 	KASSERT(ump->vtop_alloc[ump->node_part] == UDF_ALLOC_VAT);
    330 
    331 	/* only nodes are recorded in the VAT */
    332 	/* NOTE: and the fileset descriptor (FIXME ?) */
    333 	if (c_type != UDF_C_NODE)
    334 		return;
    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 	union dscrptr *dscr;
    375 	struct long_ad *node_ad_cpy;
    376 	struct part_desc *pdesc;
    377 	uint64_t *lmapping, *lmappos;
    378 	uint32_t sectornr, bpos;
    379 	uint32_t ptov;
    380 	uint16_t vpart_num;
    381 	uint8_t *fidblk;
    382 	int sector_size = ump->discinfo.sector_size;
    383 	int blks = sector_size / DEV_BSIZE;
    384 	int len, buf_len;
    385 
    386 	/* if reading, just pass to the device's STRATEGY */
    387 	if (queue == UDF_SHED_READING) {
    388 		DPRINTF(SHEDULE, ("\nudf_issue_buf READ %p : sector %d type %d,"
    389 			"b_resid %d, b_bcount %d, b_bufsize %d\n",
    390 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
    391 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
    392 		VOP_STRATEGY(ump->devvp, buf);
    393 		return;
    394 	}
    395 
    396 	if (queue == UDF_SHED_WRITING) {
    397 		DPRINTF(SHEDULE, ("\nudf_issue_buf WRITE %p : sector %d "
    398 			"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
    399 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
    400 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
    401 		KASSERT(buf->b_udf_c_type == UDF_C_ABSOLUTE);
    402 
    403 		// udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
    404 		VOP_STRATEGY(ump->devvp, buf);
    405 		return;
    406 	}
    407 
    408 	KASSERT(queue == UDF_SHED_SEQWRITING);
    409 	DPRINTF(SHEDULE, ("\nudf_issue_buf SEQWRITE %p : sector XXXX "
    410 		"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
    411 		buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount,
    412 		buf->b_bufsize));
    413 
    414 	/*
    415 	 * Buffers should not have been allocated to disc addresses yet on
    416 	 * this queue. Note that a buffer can get multiple extents allocated.
    417 	 *
    418 	 * lmapping contains lb_num relative to base partition.
    419 	 */
    420 	lmapping    = ump->la_lmapping;
    421 	node_ad_cpy = ump->la_node_ad_cpy;
    422 
    423 	/* logically allocate buf and map it in the file */
    424 	udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num);
    425 
    426 	/*
    427 	 * NOTE We are using the knowledge here that sequential media will
    428 	 * always be mapped linearly. Thus no use to explicitly translate the
    429 	 * lmapping list.
    430 	 */
    431 
    432 	/* calculate offset from physical base partition */
    433 	pdesc = ump->partitions[ump->vtop[vpart_num]];
    434 	ptov  = udf_rw32(pdesc->start_loc);
    435 
    436 	/* set buffers blkno to the physical block number */
    437 	buf->b_blkno = (*lmapping + ptov) * blks;
    438 
    439 	/* fixate floating descriptors */
    440 	if (buf->b_udf_c_type == UDF_C_FLOAT_DSCR) {
    441 		/* set our tag location to the absolute position */
    442 		dscr = (union dscrptr *) buf->b_data;
    443 		dscr->tag.tag_loc = udf_rw32(*lmapping + ptov);
    444 		udf_validate_tag_and_crc_sums(dscr);
    445 	}
    446 
    447 	/* update mapping in the VAT */
    448 	if (buf->b_udf_c_type == UDF_C_NODE) {
    449 		udf_VAT_mapping_update(ump, buf, *lmapping);
    450 		udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
    451 	}
    452 
    453 	/* if we have FIDs, fixup using the new allocation table */
    454 	if (buf->b_udf_c_type == UDF_C_FIDS) {
    455 		buf_len = buf->b_bcount;
    456 		bpos = 0;
    457 		lmappos = lmapping;
    458 		while (buf_len) {
    459 			sectornr = *lmappos++;
    460 			len = MIN(buf_len, sector_size);
    461 			fidblk = (uint8_t *) buf->b_data + bpos;
    462 			udf_fixup_fid_block(fidblk, sector_size,
    463 				0, len, sectornr);
    464 			bpos += len;
    465 			buf_len -= len;
    466 		}
    467 	}
    468 
    469 	VOP_STRATEGY(ump->devvp, buf);
    470 }
    471 
    472 
    473 static void
    474 udf_doshedule(struct udf_mount *ump)
    475 {
    476 	struct buf *buf;
    477 	struct timespec now, *last;
    478 	struct strat_private *priv = PRIV(ump);
    479 	void (*b_callback)(struct buf *);
    480 	int new_queue;
    481 	int error;
    482 
    483 	buf = bufq_get(priv->queues[priv->cur_queue]);
    484 	if (buf) {
    485 		/* transfer from the current queue to the device queue */
    486 		mutex_exit(&priv->discstrat_mutex);
    487 
    488 		/* transform buffer to synchronous; XXX needed? */
    489 		b_callback = buf->b_iodone;
    490 		buf->b_iodone = NULL;
    491 		CLR(buf->b_flags, B_ASYNC);
    492 
    493 		/* issue and wait on completion */
    494 		udf_issue_buf(ump, priv->cur_queue, buf);
    495 		biowait(buf);
    496 
    497 		mutex_enter(&priv->discstrat_mutex);
    498 
    499 		/* if there is an error, repair this error, otherwise propagate */
    500 		if (buf->b_error && ((buf->b_flags & B_READ) == 0)) {
    501 			/* check what we need to do */
    502 			panic("UDF write error, can't handle yet!\n");
    503 		}
    504 
    505 		/* propagate result to higher layers */
    506 		if (b_callback) {
    507 			buf->b_iodone = b_callback;
    508 			(*buf->b_iodone)(buf);
    509 		}
    510 
    511 		return;
    512 	}
    513 
    514 	/* Check if we're idling in this state */
    515 	vfs_timestamp(&now);
    516 	last = &priv->last_queued[priv->cur_queue];
    517 	if (ump->discinfo.mmc_class == MMC_CLASS_CD) {
    518 		/* dont switch too fast for CD media; its expensive in time */
    519 		if (now.tv_sec - last->tv_sec < 3)
    520 			return;
    521 	}
    522 
    523 	/* check if we can/should switch */
    524 	new_queue = priv->cur_queue;
    525 
    526 	if (bufq_peek(priv->queues[UDF_SHED_READING]))
    527 		new_queue = UDF_SHED_READING;
    528 	if (bufq_peek(priv->queues[UDF_SHED_WRITING]))		/* only for unmount */
    529 		new_queue = UDF_SHED_WRITING;
    530 	if (bufq_peek(priv->queues[UDF_SHED_SEQWRITING]))
    531 		new_queue = UDF_SHED_SEQWRITING;
    532 	if (priv->cur_queue == UDF_SHED_READING) {
    533 		if (new_queue == UDF_SHED_SEQWRITING) {
    534 			/* TODO use flag to signal if this is needed */
    535 			mutex_exit(&priv->discstrat_mutex);
    536 
    537 			/* update trackinfo for data and metadata */
    538 			error = udf_update_trackinfo(ump,
    539 					&ump->data_track);
    540 			assert(error == 0);
    541 			error = udf_update_trackinfo(ump,
    542 					&ump->metadata_track);
    543 			assert(error == 0);
    544 			mutex_enter(&priv->discstrat_mutex);
    545 			__USE(error);
    546 		}
    547 	}
    548 
    549 	if (new_queue != priv->cur_queue) {
    550 		DPRINTF(SHEDULE, ("switching from %d to %d\n",
    551 			priv->cur_queue, new_queue));
    552 		if (new_queue == UDF_SHED_READING)
    553 			udf_mmc_synchronise_caches(ump);
    554 	}
    555 
    556 	priv->cur_queue = new_queue;
    557 }
    558 
    559 
    560 static void
    561 udf_discstrat_thread(void *arg)
    562 {
    563 	struct udf_mount *ump = (struct udf_mount *) arg;
    564 	struct strat_private *priv = PRIV(ump);
    565 	int empty;
    566 
    567 	empty = 1;
    568 	mutex_enter(&priv->discstrat_mutex);
    569 	while (priv->run_thread || !empty || priv->sync_req) {
    570 		/* process the current selected queue */
    571 		udf_doshedule(ump);
    572 		empty  = (bufq_peek(priv->queues[UDF_SHED_READING]) == NULL);
    573 		empty &= (bufq_peek(priv->queues[UDF_SHED_WRITING]) == NULL);
    574 		empty &= (bufq_peek(priv->queues[UDF_SHED_SEQWRITING]) == NULL);
    575 
    576 		/* wait for more if needed */
    577 		if (empty) {
    578 			if (priv->sync_req) {
    579 				/* on sync, we need to simulate a read->write transition */
    580 				udf_mmc_synchronise_caches(ump);
    581 				priv->cur_queue = UDF_SHED_READING;
    582 				priv->sync_req = 0;
    583 			}
    584 			cv_timedwait(&priv->discstrat_cv,
    585 				&priv->discstrat_mutex, hz/8);
    586 		}
    587 	}
    588 	mutex_exit(&priv->discstrat_mutex);
    589 
    590 	wakeup(&priv->run_thread);
    591 	kthread_exit(0);
    592 	/* not reached */
    593 }
    594 
    595 /* --------------------------------------------------------------------- */
    596 
    597 static void
    598 udf_discstrat_init_seq(struct udf_strat_args *args)
    599 {
    600 	struct udf_mount *ump = args->ump;
    601 	struct strat_private *priv = PRIV(ump);
    602 	struct disk_strategy dkstrat;
    603 	uint32_t lb_size;
    604 
    605 	KASSERT(ump);
    606 	KASSERT(ump->logical_vol);
    607 	KASSERT(priv == NULL);
    608 
    609 	lb_size = udf_rw32(ump->logical_vol->lb_size);
    610 	KASSERT(lb_size > 0);
    611 
    612 	/* initialise our memory space */
    613 	ump->strategy_private = malloc(sizeof(struct strat_private),
    614 		M_UDFTEMP, M_WAITOK);
    615 	priv = ump->strategy_private;
    616 	memset(priv, 0 , sizeof(struct strat_private));
    617 
    618 	/* initialise locks */
    619 	cv_init(&priv->discstrat_cv, "udfstrat");
    620 	mutex_init(&priv->discstrat_mutex, MUTEX_DEFAULT, IPL_NONE);
    621 
    622 	/*
    623 	 * Initialise pool for descriptors associated with nodes. This is done
    624 	 * in lb_size units though currently lb_size is dictated to be
    625 	 * sector_size.
    626 	 */
    627 	pool_init(&priv->desc_pool, lb_size, 0, 0, 0, "udf_desc_pool", NULL,
    628 	    IPL_NONE);
    629 
    630 	/*
    631 	 * remember old device strategy method and explicit set method
    632 	 * `discsort' since we have our own more complex strategy that is not
    633 	 * implementable on the CD device and other strategies will get in the
    634 	 * way.
    635 	 */
    636 	memset(&priv->old_strategy_setting, 0,
    637 		sizeof(struct disk_strategy));
    638 	VOP_IOCTL(ump->devvp, DIOCGSTRATEGY, &priv->old_strategy_setting,
    639 		FREAD | FKIOCTL, NOCRED);
    640 	memset(&dkstrat, 0, sizeof(struct disk_strategy));
    641 	strcpy(dkstrat.dks_name, "discsort");
    642 	VOP_IOCTL(ump->devvp, DIOCSSTRATEGY, &dkstrat, FWRITE | FKIOCTL,
    643 		NOCRED);
    644 
    645 	/* initialise our internal scheduler */
    646 	priv->cur_queue = UDF_SHED_READING;
    647 	bufq_alloc(&priv->queues[UDF_SHED_READING], "disksort",
    648 		BUFQ_SORT_RAWBLOCK);
    649 	bufq_alloc(&priv->queues[UDF_SHED_WRITING], "disksort",
    650 		BUFQ_SORT_RAWBLOCK);
    651 	bufq_alloc(&priv->queues[UDF_SHED_SEQWRITING], "fcfs", 0);
    652 	vfs_timestamp(&priv->last_queued[UDF_SHED_READING]);
    653 	vfs_timestamp(&priv->last_queued[UDF_SHED_WRITING]);
    654 	vfs_timestamp(&priv->last_queued[UDF_SHED_SEQWRITING]);
    655 
    656 	/* create our disk strategy thread */
    657 	priv->run_thread = 1;
    658 	priv->sync_req   = 0;
    659 	if (kthread_create(PRI_NONE, 0 /* KTHREAD_MPSAFE*/, NULL /* cpu_info*/,
    660 		udf_discstrat_thread, ump, &priv->queue_lwp,
    661 		"%s", "udf_rw")) {
    662 		panic("fork udf_rw");
    663 	}
    664 }
    665 
    666 
    667 static void
    668 udf_discstrat_finish_seq(struct udf_strat_args *args)
    669 {
    670 	struct udf_mount *ump = args->ump;
    671 	struct strat_private *priv = PRIV(ump);
    672 	int error;
    673 
    674 	if (ump == NULL)
    675 		return;
    676 
    677 	/* stop our scheduling thread */
    678 	KASSERT(priv->run_thread == 1);
    679 	priv->run_thread = 0;
    680 	wakeup(priv->queue_lwp);
    681 	do {
    682 		error = tsleep(&priv->run_thread, PRIBIO+1,
    683 			"udfshedfin", hz);
    684 	} while (error);
    685 	/* kthread should be finished now */
    686 
    687 	/* set back old device strategy method */
    688 	VOP_IOCTL(ump->devvp, DIOCSSTRATEGY, &priv->old_strategy_setting,
    689 			FWRITE, NOCRED);
    690 
    691 	/* destroy our pool */
    692 	pool_destroy(&priv->desc_pool);
    693 
    694 	mutex_destroy(&priv->discstrat_mutex);
    695 	cv_destroy(&priv->discstrat_cv);
    696 
    697 	/* free our private space */
    698 	free(ump->strategy_private, M_UDFTEMP);
    699 	ump->strategy_private = NULL;
    700 }
    701 
    702 /* --------------------------------------------------------------------- */
    703 
    704 struct udf_strategy udf_strat_sequential =
    705 {
    706 	udf_create_logvol_dscr_seq,
    707 	udf_free_logvol_dscr_seq,
    708 	udf_read_logvol_dscr_seq,
    709 	udf_write_logvol_dscr_seq,
    710 	udf_queuebuf_seq,
    711 	udf_sync_caches_seq,
    712 	udf_discstrat_init_seq,
    713 	udf_discstrat_finish_seq
    714 };
    715 
    716 
    717