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