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