1 /* $NetBSD: udf_strat_sequential.c,v 1.15 2016/05/24 09:55:57 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.15 2016/05/24 09:55:57 reinoud 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
udf_wr_nodedscr_callback(struct buf * buf)101 udf_wr_nodedscr_callback(struct buf *buf)
102 {
103 	struct udf_node *udf_node;
104 
105 	KASSERT(buf);
106 	KASSERT(buf->b_data);
107 
108 	/* called when write action is done */
109 	DPRINTF(WRITE, ("udf_wr_nodedscr_callback(): node written out\n"));
110 
111 	udf_node = VTOI(buf->b_vp);
112 	if (udf_node == NULL) {
113 		putiobuf(buf);
114 		printf("udf_wr_node_callback: NULL node?\n");
115 		return;
116 	}
117 
118 	/* XXX right flags to mark dirty again on error? */
119 	if (buf->b_error) {
120 		udf_node->i_flags |= IN_MODIFIED | IN_ACCESSED;
121 		/* XXX TODO reshedule on error */
122 	}
123 
124 	/* decrement outstanding_nodedscr */
125 	KASSERT(udf_node->outstanding_nodedscr >= 1);
126 	udf_node->outstanding_nodedscr--;
127 	if (udf_node->outstanding_nodedscr == 0) {
128 		/* first unlock the node */
129 		UDF_UNLOCK_NODE(udf_node, 0);
130 		wakeup(&udf_node->outstanding_nodedscr);
131 	}
132 
133 	putiobuf(buf);
134 }
135 
136 /* --------------------------------------------------------------------- */
137 
138 static int
udf_create_logvol_dscr_seq(struct udf_strat_args * args)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
udf_free_logvol_dscr_seq(struct udf_strat_args * args)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
udf_read_logvol_dscr_seq(struct udf_strat_args * args)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
udf_write_logvol_dscr_seq(struct udf_strat_args * args)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 sheduler. Due to the nature of optical media
247  * sheduling 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 shedule 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
udf_queuebuf_seq(struct udf_strat_args * args)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 sheduler lists for more complex sheduling */
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
udf_sync_caches_seq(struct udf_strat_args * args)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
udf_VAT_mapping_update(struct udf_mount * ump,struct buf * buf,uint32_t lb_map)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
udf_issue_buf(struct udf_mount * ump,int queue,struct buf * buf)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
udf_doshedule(struct udf_mount * ump)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 	}
553 
554 	priv->cur_queue = new_queue;
555 }
556 
557 
558 static void
udf_discstrat_thread(void * arg)559 udf_discstrat_thread(void *arg)
560 {
561 	struct udf_mount *ump = (struct udf_mount *) arg;
562 	struct strat_private *priv = PRIV(ump);
563 	int empty;
564 
565 	empty = 1;
566 	mutex_enter(&priv->discstrat_mutex);
567 	while (priv->run_thread || !empty || priv->sync_req) {
568 		/* process the current selected queue */
569 		udf_doshedule(ump);
570 		empty  = (bufq_peek(priv->queues[UDF_SHED_READING]) == NULL);
571 		empty &= (bufq_peek(priv->queues[UDF_SHED_WRITING]) == NULL);
572 		empty &= (bufq_peek(priv->queues[UDF_SHED_SEQWRITING]) == NULL);
573 
574 		/* wait for more if needed */
575 		if (empty) {
576 			if (priv->sync_req) {
577 				/* on sync, we need to simulate a read->write transition */
578 				udf_mmc_synchronise_caches(ump);
579 				priv->cur_queue = UDF_SHED_READING;
580 				priv->sync_req = 0;
581 			}
582 			cv_timedwait(&priv->discstrat_cv,
583 				&priv->discstrat_mutex, hz/8);
584 		}
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
udf_discstrat_init_seq(struct udf_strat_args * args)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 	priv->sync_req   = 0;
657 	if (kthread_create(PRI_NONE, 0 /* KTHREAD_MPSAFE*/, NULL /* cpu_info*/,
658 		udf_discstrat_thread, ump, &priv->queue_lwp,
659 		"%s", "udf_rw")) {
660 		panic("fork udf_rw");
661 	}
662 }
663 
664 
665 static void
udf_discstrat_finish_seq(struct udf_strat_args * args)666 udf_discstrat_finish_seq(struct udf_strat_args *args)
667 {
668 	struct udf_mount *ump = args->ump;
669 	struct strat_private *priv = PRIV(ump);
670 	int error;
671 
672 	if (ump == NULL)
673 		return;
674 
675 	/* stop our sheduling thread */
676 	KASSERT(priv->run_thread == 1);
677 	priv->run_thread = 0;
678 	wakeup(priv->queue_lwp);
679 	do {
680 		error = tsleep(&priv->run_thread, PRIBIO+1,
681 			"udfshedfin", hz);
682 	} while (error);
683 	/* kthread should be finished now */
684 
685 	/* set back old device strategy method */
686 	VOP_IOCTL(ump->devvp, DIOCSSTRATEGY, &priv->old_strategy_setting,
687 			FWRITE, NOCRED);
688 
689 	/* destroy our pool */
690 	pool_destroy(&priv->desc_pool);
691 
692 	mutex_destroy(&priv->discstrat_mutex);
693 	cv_destroy(&priv->discstrat_cv);
694 
695 	/* free our private space */
696 	free(ump->strategy_private, M_UDFTEMP);
697 	ump->strategy_private = NULL;
698 }
699 
700 /* --------------------------------------------------------------------- */
701 
702 struct udf_strategy udf_strat_sequential =
703 {
704 	udf_create_logvol_dscr_seq,
705 	udf_free_logvol_dscr_seq,
706 	udf_read_logvol_dscr_seq,
707 	udf_write_logvol_dscr_seq,
708 	udf_queuebuf_seq,
709 	udf_sync_caches_seq,
710 	udf_discstrat_init_seq,
711 	udf_discstrat_finish_seq
712 };
713 
714 
715