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