Home | History | Annotate | Line # | Download | only in udf
udf_strat_rmw.c revision 1.19
      1  1.19  reinoud /* $NetBSD: udf_strat_rmw.c,v 1.19 2009/05/20 15:30:26 reinoud 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.19  reinoud __KERNEL_RCSID(0, "$NetBSD: udf_strat_rmw.c,v 1.19 2009/05/20 15:30:26 reinoud 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 #define BTOE(buf) ((struct udf_eccline *) ((buf)->b_private))
     72   1.1  reinoud 
     73   1.1  reinoud /* --------------------------------------------------------------------- */
     74   1.1  reinoud 
     75   1.1  reinoud #define UDF_MAX_PACKET_SIZE	64			/* DONT change this */
     76   1.1  reinoud 
     77   1.1  reinoud /* sheduler states */
     78  1.11  reinoud #define UDF_SHED_WAITING	1			/* waiting on timeout */
     79  1.11  reinoud #define UDF_SHED_READING	2
     80  1.11  reinoud #define UDF_SHED_WRITING	3
     81  1.11  reinoud #define UDF_SHED_SEQWRITING	4
     82  1.11  reinoud #define UDF_SHED_IDLE		5			/* resting */
     83  1.11  reinoud #define UDF_SHED_FREE		6			/* recycleable */
     84  1.11  reinoud #define UDF_SHED_MAX		6+1
     85   1.1  reinoud 
     86   1.1  reinoud /* flags */
     87   1.1  reinoud #define ECC_LOCKED		0x01			/* prevent access   */
     88   1.1  reinoud #define ECC_WANTED		0x02			/* trying access    */
     89   1.1  reinoud #define ECC_SEQWRITING		0x04			/* sequential queue */
     90   1.1  reinoud #define ECC_FLOATING		0x08			/* not queued yet   */
     91   1.1  reinoud 
     92  1.13  reinoud #define ECC_WAITTIME		10
     93  1.11  reinoud 
     94   1.1  reinoud 
     95   1.1  reinoud TAILQ_HEAD(ecclineq, udf_eccline);
     96   1.1  reinoud struct udf_eccline {
     97   1.1  reinoud 	struct udf_mount	 *ump;
     98   1.1  reinoud 	uint64_t		  present;		/* preserve these */
     99   1.1  reinoud 	uint64_t		  readin;		/* bitmap */
    100   1.1  reinoud 	uint64_t		  dirty;		/* bitmap */
    101   1.1  reinoud 	uint64_t		  error;		/* bitmap */
    102   1.1  reinoud 	uint32_t		  refcnt;
    103   1.1  reinoud 
    104  1.11  reinoud 	struct timespec		  wait_time;
    105   1.1  reinoud 	uint32_t		  flags;
    106   1.1  reinoud 	uint32_t		  start_sector;		/* physical */
    107   1.1  reinoud 
    108   1.1  reinoud 	struct buf		 *buf;
    109   1.1  reinoud 	void			 *blob;
    110   1.1  reinoud 
    111   1.1  reinoud 	struct buf		 *bufs[UDF_MAX_PACKET_SIZE];
    112   1.1  reinoud 	uint32_t		  bufs_bpos[UDF_MAX_PACKET_SIZE];
    113   1.1  reinoud 	int			  bufs_len[UDF_MAX_PACKET_SIZE];
    114   1.1  reinoud 
    115   1.1  reinoud 	int			  queued_on;		/* on which BUFQ list */
    116   1.1  reinoud 	LIST_ENTRY(udf_eccline)   hashchain;		/* on sector lookup  */
    117   1.1  reinoud };
    118   1.1  reinoud 
    119   1.1  reinoud 
    120   1.1  reinoud struct strat_private {
    121   1.1  reinoud 	lwp_t			 *queue_lwp;
    122   1.1  reinoud 	kcondvar_t		  discstrat_cv;		/* to wait on       */
    123   1.1  reinoud 	kmutex_t		  discstrat_mutex;	/* disc strategy    */
    124   1.1  reinoud 	kmutex_t		  seqwrite_mutex;	/* protect mappings */
    125   1.1  reinoud 
    126  1.13  reinoud 	int			  thread_running;	/* thread control */
    127   1.1  reinoud 	int			  run_thread;		/* thread control */
    128   1.1  reinoud 	int			  thread_finished;	/* thread control */
    129   1.1  reinoud 	int			  cur_queue;
    130   1.1  reinoud 
    131   1.1  reinoud 	int			  num_floating;
    132   1.1  reinoud 	int			  num_queued[UDF_SHED_MAX];
    133   1.1  reinoud 	struct bufq_state	 *queues[UDF_SHED_MAX];
    134   1.1  reinoud 	struct timespec		  last_queued[UDF_SHED_MAX];
    135   1.1  reinoud 	struct disk_strategy	  old_strategy_setting;
    136   1.1  reinoud 
    137   1.1  reinoud 	struct pool		  eccline_pool;
    138   1.1  reinoud 	struct pool		  ecclineblob_pool;
    139   1.1  reinoud 	LIST_HEAD(, udf_eccline)  eccline_hash[UDF_ECCBUF_HASHSIZE];
    140   1.1  reinoud };
    141   1.1  reinoud 
    142   1.1  reinoud /* --------------------------------------------------------------------- */
    143   1.1  reinoud 
    144   1.1  reinoud #define UDF_LOCK_ECCLINE(eccline) udf_lock_eccline(eccline)
    145   1.1  reinoud #define UDF_UNLOCK_ECCLINE(eccline) udf_unlock_eccline(eccline)
    146   1.1  reinoud 
    147   1.1  reinoud /* can be called with or without discstrat lock */
    148   1.1  reinoud static void
    149   1.1  reinoud udf_lock_eccline(struct udf_eccline *eccline)
    150   1.1  reinoud {
    151   1.1  reinoud 	struct strat_private *priv = PRIV(eccline->ump);
    152   1.1  reinoud 	int waslocked, ret;
    153   1.1  reinoud 
    154   1.1  reinoud 	waslocked = mutex_owned(&priv->discstrat_mutex);
    155   1.1  reinoud 	if (!waslocked)
    156   1.1  reinoud 		mutex_enter(&priv->discstrat_mutex);
    157   1.1  reinoud 
    158   1.1  reinoud 	/* wait until its unlocked first */
    159   1.1  reinoud 	while (eccline->flags & ECC_LOCKED) {
    160   1.1  reinoud 		eccline->flags |= ECC_WANTED;
    161   1.1  reinoud 		ret = cv_timedwait(&priv->discstrat_cv, &priv->discstrat_mutex,
    162   1.1  reinoud 			hz/8);
    163   1.1  reinoud 		if (ret == EWOULDBLOCK)
    164   1.1  reinoud 			DPRINTF(LOCKING, ("eccline lock helt, waiting for "
    165   1.1  reinoud 				"release"));
    166   1.1  reinoud 	}
    167   1.1  reinoud 	eccline->flags |= ECC_LOCKED;
    168   1.1  reinoud 	eccline->flags &= ~ECC_WANTED;
    169   1.1  reinoud 
    170   1.1  reinoud 	if (!waslocked)
    171   1.1  reinoud 		mutex_exit(&priv->discstrat_mutex);
    172   1.1  reinoud }
    173   1.1  reinoud 
    174   1.1  reinoud 
    175   1.1  reinoud /* can be called with or without discstrat lock */
    176   1.1  reinoud static void
    177   1.1  reinoud udf_unlock_eccline(struct udf_eccline *eccline)
    178   1.1  reinoud {
    179   1.1  reinoud 	struct strat_private *priv = PRIV(eccline->ump);
    180   1.1  reinoud 	int waslocked;
    181   1.1  reinoud 
    182   1.1  reinoud 	waslocked = mutex_owned(&priv->discstrat_mutex);
    183   1.1  reinoud 	if (!waslocked)
    184   1.1  reinoud 		mutex_enter(&priv->discstrat_mutex);
    185   1.1  reinoud 
    186   1.1  reinoud 	eccline->flags &= ~ECC_LOCKED;
    187   1.1  reinoud 	cv_broadcast(&priv->discstrat_cv);
    188   1.1  reinoud 
    189   1.1  reinoud 	if (!waslocked)
    190   1.1  reinoud 		mutex_exit(&priv->discstrat_mutex);
    191   1.1  reinoud }
    192   1.1  reinoud 
    193   1.1  reinoud 
    194   1.1  reinoud /* NOTE discstrat_mutex should be held! */
    195   1.1  reinoud static void
    196   1.1  reinoud udf_dispose_eccline(struct udf_eccline *eccline)
    197   1.1  reinoud {
    198   1.1  reinoud 	struct strat_private *priv = PRIV(eccline->ump);
    199   1.1  reinoud 	struct buf *ret;
    200   1.1  reinoud 
    201   1.1  reinoud 	KASSERT(mutex_owned(&priv->discstrat_mutex));
    202   1.1  reinoud 
    203   1.1  reinoud 	KASSERT(eccline->refcnt == 0);
    204   1.1  reinoud 	KASSERT(eccline->dirty  == 0);
    205   1.1  reinoud 
    206   1.3  reinoud 	DPRINTF(ECCLINE, ("dispose eccline with start sector %d, "
    207   1.1  reinoud 		"present %0"PRIx64"\n", eccline->start_sector,
    208   1.1  reinoud 		eccline->present));
    209   1.1  reinoud 
    210   1.1  reinoud 	if (eccline->queued_on) {
    211  1.17     yamt 		ret = bufq_cancel(priv->queues[eccline->queued_on], eccline->buf);
    212   1.1  reinoud 		KASSERT(ret == eccline->buf);
    213   1.1  reinoud 		priv->num_queued[eccline->queued_on]--;
    214   1.1  reinoud 	}
    215   1.1  reinoud 	LIST_REMOVE(eccline, hashchain);
    216   1.1  reinoud 
    217   1.1  reinoud 	if (eccline->flags & ECC_FLOATING) {
    218   1.1  reinoud 		eccline->flags &= ~ECC_FLOATING;
    219   1.1  reinoud 		priv->num_floating--;
    220   1.1  reinoud 	}
    221   1.1  reinoud 
    222   1.1  reinoud 	putiobuf(eccline->buf);
    223   1.1  reinoud 	pool_put(&priv->ecclineblob_pool, eccline->blob);
    224   1.1  reinoud 	pool_put(&priv->eccline_pool, eccline);
    225   1.1  reinoud }
    226   1.1  reinoud 
    227   1.1  reinoud 
    228   1.1  reinoud /* NOTE discstrat_mutex should be held! */
    229   1.1  reinoud static void
    230   1.1  reinoud udf_push_eccline(struct udf_eccline *eccline, int newqueue)
    231   1.1  reinoud {
    232   1.1  reinoud 	struct strat_private *priv = PRIV(eccline->ump);
    233   1.1  reinoud 	struct buf *ret;
    234   1.1  reinoud 	int curqueue;
    235   1.1  reinoud 
    236   1.1  reinoud 	KASSERT(mutex_owned(&priv->discstrat_mutex));
    237   1.1  reinoud 
    238   1.1  reinoud 	DPRINTF(PARANOIA, ("DEBUG: buf %p pushed on queue %d\n", eccline->buf, newqueue));
    239   1.1  reinoud 
    240   1.1  reinoud 	/* requeue */
    241   1.1  reinoud 	curqueue = eccline->queued_on;
    242   1.1  reinoud 	if (curqueue) {
    243  1.17     yamt 		ret = bufq_cancel(priv->queues[curqueue], eccline->buf);
    244   1.1  reinoud 
    245  1.17     yamt 		DPRINTF(PARANOIA, ("push_eccline bufq_cancel returned %p when "
    246   1.1  reinoud 			"requested to remove %p from queue %d\n", ret,
    247   1.1  reinoud 			eccline->buf, curqueue));
    248   1.1  reinoud #ifdef DIAGNOSTIC
    249   1.1  reinoud 		if (ret == NULL) {
    250   1.1  reinoud 			int i;
    251   1.1  reinoud 
    252   1.1  reinoud 			printf("udf_push_eccline: bufq_cancel can't find "
    253   1.1  reinoud 				"buffer; dumping queues\n");
    254   1.1  reinoud 			for (i = 1; i < UDF_SHED_MAX; i++) {
    255   1.1  reinoud 				printf("queue %d\n\t", i);
    256  1.17     yamt 				ret = bufq_get(priv->queues[i]);
    257   1.1  reinoud 				while (ret) {
    258   1.1  reinoud 					printf("%p ", ret);
    259   1.1  reinoud 					if (ret == eccline->buf)
    260   1.1  reinoud 						printf("[<-] ");
    261  1.17     yamt 					ret = bufq_get(priv->queues[i]);
    262   1.1  reinoud 				}
    263   1.1  reinoud 				printf("\n");
    264   1.1  reinoud 			}
    265   1.1  reinoud 			panic("fatal queue bug; exit");
    266   1.1  reinoud 		}
    267   1.1  reinoud #endif
    268   1.1  reinoud 
    269   1.1  reinoud 		KASSERT(ret == eccline->buf);
    270   1.1  reinoud 		priv->num_queued[curqueue]--;
    271   1.1  reinoud 	}
    272   1.1  reinoud 
    273  1.10  reinoud 	/* set buffer block numbers to make sure its queued correctly */
    274  1.10  reinoud 	eccline->buf->b_lblkno   = eccline->start_sector;
    275  1.10  reinoud 	eccline->buf->b_blkno    = eccline->start_sector;
    276  1.10  reinoud 	eccline->buf->b_rawblkno = eccline->start_sector;
    277  1.10  reinoud 
    278  1.17     yamt 	bufq_put(priv->queues[newqueue], eccline->buf);
    279   1.1  reinoud 	eccline->queued_on = newqueue;
    280   1.1  reinoud 	priv->num_queued[newqueue]++;
    281   1.1  reinoud 	vfs_timestamp(&priv->last_queued[newqueue]);
    282   1.1  reinoud 
    283   1.1  reinoud 	if (eccline->flags & ECC_FLOATING) {
    284   1.1  reinoud 		eccline->flags &= ~ECC_FLOATING;
    285   1.1  reinoud 		priv->num_floating--;
    286   1.1  reinoud 	}
    287   1.1  reinoud 
    288  1.14  reinoud 	/* tickle disc strategy statemachine */
    289  1.14  reinoud 	if (newqueue != UDF_SHED_IDLE)
    290   1.1  reinoud 		cv_signal(&priv->discstrat_cv);
    291   1.1  reinoud }
    292   1.1  reinoud 
    293   1.1  reinoud 
    294   1.1  reinoud static struct udf_eccline *
    295   1.1  reinoud udf_pop_eccline(struct strat_private *priv, int queued_on)
    296   1.1  reinoud {
    297   1.1  reinoud 	struct udf_eccline *eccline;
    298   1.1  reinoud 	struct buf *buf;
    299   1.1  reinoud 
    300   1.1  reinoud 	KASSERT(mutex_owned(&priv->discstrat_mutex));
    301   1.1  reinoud 
    302  1.17     yamt 	buf = bufq_get(priv->queues[queued_on]);
    303   1.1  reinoud 	if (!buf) {
    304   1.1  reinoud 		KASSERT(priv->num_queued[queued_on] == 0);
    305   1.1  reinoud 		return NULL;
    306   1.1  reinoud 	}
    307   1.1  reinoud 
    308   1.1  reinoud 	eccline = BTOE(buf);
    309   1.1  reinoud 	KASSERT(eccline->queued_on == queued_on);
    310   1.1  reinoud 	eccline->queued_on = 0;
    311   1.1  reinoud 	priv->num_queued[queued_on]--;
    312   1.1  reinoud 
    313   1.1  reinoud 	if (eccline->flags & ECC_FLOATING)
    314   1.1  reinoud 		panic("popping already marked floating eccline");
    315   1.1  reinoud 	eccline->flags |= ECC_FLOATING;
    316   1.1  reinoud 	priv->num_floating++;
    317   1.1  reinoud 
    318   1.1  reinoud 	DPRINTF(PARANOIA, ("DEBUG: buf %p popped from queue %d\n",
    319   1.1  reinoud 		eccline->buf, queued_on));
    320   1.1  reinoud 
    321   1.1  reinoud 	return eccline;
    322   1.1  reinoud }
    323   1.1  reinoud 
    324   1.1  reinoud 
    325   1.1  reinoud static struct udf_eccline *
    326   1.1  reinoud udf_geteccline(struct udf_mount *ump, uint32_t sector, int flags)
    327   1.1  reinoud {
    328   1.1  reinoud 	struct strat_private *priv = PRIV(ump);
    329   1.1  reinoud 	struct udf_eccline *eccline;
    330   1.1  reinoud 	uint32_t start_sector, lb_size, blobsize;
    331   1.1  reinoud 	uint8_t *eccline_blob;
    332   1.1  reinoud 	int line, line_offset;
    333   1.1  reinoud 	int num_busy, ret;
    334   1.1  reinoud 
    335   1.1  reinoud 	line_offset  = sector % ump->packet_size;
    336   1.1  reinoud 	start_sector = sector - line_offset;
    337   1.1  reinoud 	line = (start_sector/ump->packet_size) & UDF_ECCBUF_HASHMASK;
    338   1.1  reinoud 
    339   1.1  reinoud 	mutex_enter(&priv->discstrat_mutex);
    340  1.13  reinoud 	KASSERT(priv->thread_running);
    341   1.1  reinoud 
    342   1.1  reinoud retry:
    343   1.3  reinoud 	DPRINTF(ECCLINE, ("get line sector %d, line %d\n", sector, line));
    344   1.1  reinoud 	LIST_FOREACH(eccline, &priv->eccline_hash[line], hashchain) {
    345   1.1  reinoud 		if (eccline->start_sector == start_sector) {
    346   1.3  reinoud 			DPRINTF(ECCLINE, ("\tfound eccline, start_sector %d\n",
    347   1.1  reinoud 				eccline->start_sector));
    348   1.1  reinoud 
    349   1.1  reinoud 			UDF_LOCK_ECCLINE(eccline);
    350   1.1  reinoud 			/* move from freelist (!) */
    351   1.1  reinoud 			if (eccline->queued_on == UDF_SHED_FREE) {
    352   1.3  reinoud 				DPRINTF(ECCLINE, ("was on freelist\n"));
    353   1.1  reinoud 				KASSERT(eccline->refcnt == 0);
    354   1.1  reinoud 				udf_push_eccline(eccline, UDF_SHED_IDLE);
    355   1.1  reinoud 			}
    356   1.1  reinoud 			eccline->refcnt++;
    357   1.1  reinoud 			mutex_exit(&priv->discstrat_mutex);
    358   1.1  reinoud 			return eccline;
    359   1.1  reinoud 		}
    360   1.1  reinoud 	}
    361   1.1  reinoud 
    362   1.3  reinoud 	DPRINTF(ECCLINE, ("\tnot found in eccline cache\n"));
    363   1.1  reinoud 	/* not found in eccline cache */
    364   1.1  reinoud 
    365   1.1  reinoud 	lb_size  = udf_rw32(ump->logical_vol->lb_size);
    366   1.1  reinoud 	blobsize = ump->packet_size * lb_size;
    367   1.1  reinoud 
    368   1.1  reinoud 	/* dont allow too many pending requests */
    369   1.3  reinoud 	DPRINTF(ECCLINE, ("\tallocating new eccline\n"));
    370   1.1  reinoud 	num_busy = (priv->num_queued[UDF_SHED_SEQWRITING] + priv->num_floating);
    371   1.1  reinoud 	if ((flags & ECC_SEQWRITING) && (num_busy > UDF_ECCLINE_MAXBUSY)) {
    372   1.1  reinoud 		ret = cv_timedwait(&priv->discstrat_cv,
    373   1.1  reinoud 			&priv->discstrat_mutex, hz/8);
    374   1.1  reinoud 		goto retry;
    375   1.1  reinoud 	}
    376   1.1  reinoud 
    377   1.1  reinoud 	eccline_blob = pool_get(&priv->ecclineblob_pool, PR_NOWAIT);
    378   1.1  reinoud 	eccline = pool_get(&priv->eccline_pool, PR_NOWAIT);
    379   1.1  reinoud 	if ((eccline_blob == NULL) || (eccline == NULL)) {
    380   1.1  reinoud 		if (eccline_blob)
    381   1.1  reinoud 			pool_put(&priv->ecclineblob_pool, eccline_blob);
    382   1.1  reinoud 		if (eccline)
    383   1.1  reinoud 			pool_put(&priv->eccline_pool, eccline);
    384   1.1  reinoud 
    385   1.1  reinoud 		/* out of memory for now; canibalise freelist */
    386   1.1  reinoud 		eccline = udf_pop_eccline(priv, UDF_SHED_FREE);
    387   1.1  reinoud 		if (eccline == NULL) {
    388   1.1  reinoud 			/* serious trouble; wait and retry */
    389   1.1  reinoud 			cv_timedwait(&priv->discstrat_cv,
    390   1.1  reinoud 				&priv->discstrat_mutex, hz/8);
    391   1.1  reinoud 			goto retry;
    392   1.1  reinoud 		}
    393   1.1  reinoud 		/* push back line if we're waiting for it */
    394   1.1  reinoud 		if (eccline->flags & ECC_WANTED) {
    395   1.1  reinoud 			udf_push_eccline(eccline, UDF_SHED_IDLE);
    396   1.1  reinoud 			goto retry;
    397   1.1  reinoud 		}
    398   1.1  reinoud 
    399   1.1  reinoud 		/* unlink this entry */
    400   1.1  reinoud 		LIST_REMOVE(eccline, hashchain);
    401   1.1  reinoud 
    402   1.1  reinoud 		KASSERT(eccline->flags & ECC_FLOATING);
    403   1.1  reinoud 
    404   1.1  reinoud 		eccline_blob = eccline->blob;
    405   1.1  reinoud 		memset(eccline, 0, sizeof(struct udf_eccline));
    406   1.1  reinoud 		eccline->flags = ECC_FLOATING;
    407   1.1  reinoud 	} else {
    408   1.1  reinoud 		memset(eccline, 0, sizeof(struct udf_eccline));
    409   1.1  reinoud 		eccline->flags = ECC_FLOATING;
    410   1.1  reinoud 		priv->num_floating++;
    411   1.1  reinoud 	}
    412   1.1  reinoud 
    413   1.1  reinoud 	eccline->queued_on = 0;
    414   1.1  reinoud 	eccline->blob = eccline_blob;
    415   1.1  reinoud 	eccline->buf  = getiobuf(NULL, true);
    416   1.1  reinoud 	eccline->buf->b_private = eccline;	/* IMPORTANT */
    417   1.1  reinoud 
    418   1.1  reinoud 	/* initialise eccline blob */
    419   1.1  reinoud 	memset(eccline->blob, 0, blobsize);
    420   1.1  reinoud 
    421   1.1  reinoud 	eccline->ump = ump;
    422   1.1  reinoud 	eccline->present = eccline->readin = eccline->dirty = 0;
    423   1.1  reinoud 	eccline->error = 0;
    424   1.1  reinoud 	eccline->refcnt = 0;
    425  1.10  reinoud 
    426  1.10  reinoud 	eccline->start_sector    = start_sector;
    427  1.10  reinoud 	eccline->buf->b_lblkno   = start_sector;
    428  1.10  reinoud 	eccline->buf->b_blkno    = start_sector;
    429  1.10  reinoud 	eccline->buf->b_rawblkno = start_sector;
    430   1.1  reinoud 
    431   1.1  reinoud 	LIST_INSERT_HEAD(&priv->eccline_hash[line], eccline, hashchain);
    432   1.1  reinoud 
    433   1.1  reinoud 	/*
    434   1.1  reinoud 	 * TODO possible optimalisation for checking overlap with partitions
    435   1.1  reinoud 	 * to get a clue on future eccline usage
    436   1.1  reinoud 	 */
    437   1.1  reinoud 	eccline->refcnt++;
    438   1.1  reinoud 	UDF_LOCK_ECCLINE(eccline);
    439   1.1  reinoud 
    440   1.1  reinoud 	mutex_exit(&priv->discstrat_mutex);
    441   1.1  reinoud 
    442   1.1  reinoud 	return eccline;
    443   1.1  reinoud }
    444   1.1  reinoud 
    445   1.1  reinoud 
    446   1.1  reinoud static void
    447   1.1  reinoud udf_puteccline(struct udf_eccline *eccline)
    448   1.1  reinoud {
    449   1.1  reinoud 	struct strat_private *priv = PRIV(eccline->ump);
    450   1.1  reinoud 	struct udf_mount *ump = eccline->ump;
    451   1.1  reinoud 	uint64_t allbits = ((uint64_t) 1 << ump->packet_size)-1;
    452   1.1  reinoud 
    453   1.1  reinoud 	mutex_enter(&priv->discstrat_mutex);
    454   1.1  reinoud 
    455   1.1  reinoud 	/* clear directly all readin requests from present ones */
    456   1.1  reinoud 	if (eccline->readin & eccline->present) {
    457   1.1  reinoud 		/* clear all read bits that are already read in */
    458   1.1  reinoud 		eccline->readin &= (~eccline->present) & allbits;
    459   1.1  reinoud 		wakeup(eccline);
    460   1.1  reinoud 	}
    461   1.1  reinoud 
    462   1.3  reinoud 	DPRINTF(ECCLINE, ("put eccline start sector %d, refcnt %d\n",
    463   1.1  reinoud 		eccline->start_sector, eccline->refcnt));
    464   1.1  reinoud 
    465  1.11  reinoud 	/* if we have active nodes we dont set it on seqwriting */
    466   1.1  reinoud 	if (eccline->refcnt > 1)
    467   1.1  reinoud 		eccline->flags &= ~ECC_SEQWRITING;
    468   1.1  reinoud 
    469  1.11  reinoud 	vfs_timestamp(&eccline->wait_time);
    470  1.11  reinoud 	eccline->wait_time.tv_sec += ECC_WAITTIME;
    471  1.11  reinoud 	udf_push_eccline(eccline, UDF_SHED_WAITING);
    472   1.1  reinoud 
    473   1.1  reinoud 	KASSERT(eccline->refcnt >= 1);
    474   1.1  reinoud 	eccline->refcnt--;
    475   1.1  reinoud 	UDF_UNLOCK_ECCLINE(eccline);
    476   1.1  reinoud 
    477  1.13  reinoud 	wakeup(eccline);
    478   1.1  reinoud 	mutex_exit(&priv->discstrat_mutex);
    479   1.1  reinoud }
    480   1.1  reinoud 
    481   1.1  reinoud /* --------------------------------------------------------------------- */
    482   1.1  reinoud 
    483   1.1  reinoud static int
    484   1.5  reinoud udf_create_nodedscr_rmw(struct udf_strat_args *args)
    485   1.1  reinoud {
    486   1.1  reinoud 	union dscrptr   **dscrptr  = &args->dscr;
    487   1.1  reinoud 	struct udf_mount *ump      = args->ump;
    488   1.1  reinoud 	struct long_ad   *icb      = args->icb;
    489   1.1  reinoud 	struct udf_eccline *eccline;
    490   1.1  reinoud 	uint64_t bit;
    491   1.1  reinoud 	uint32_t sectornr, lb_size, dummy;
    492   1.1  reinoud 	uint8_t *mem;
    493   1.1  reinoud 	int error, eccsect;
    494   1.1  reinoud 
    495   1.1  reinoud 	error = udf_translate_vtop(ump, icb, &sectornr, &dummy);
    496   1.1  reinoud 	if (error)
    497   1.1  reinoud 		return error;
    498   1.1  reinoud 
    499   1.1  reinoud 	lb_size  = udf_rw32(ump->logical_vol->lb_size);
    500   1.1  reinoud 
    501   1.1  reinoud 	/* get our eccline */
    502   1.1  reinoud 	eccline = udf_geteccline(ump, sectornr, 0);
    503   1.1  reinoud 	eccsect = sectornr - eccline->start_sector;
    504   1.1  reinoud 
    505   1.1  reinoud 	bit = (uint64_t) 1 << eccsect;
    506   1.1  reinoud 	eccline->readin  &= ~bit;	/* just in case */
    507   1.1  reinoud 	eccline->present |=  bit;
    508   1.1  reinoud 	eccline->dirty   &= ~bit;	/* Err... euhm... clean? */
    509   1.1  reinoud 
    510   1.1  reinoud 	eccline->refcnt++;
    511   1.1  reinoud 
    512   1.1  reinoud 	/* clear space */
    513   1.1  reinoud 	mem = ((uint8_t *) eccline->blob) + eccsect * lb_size;
    514   1.1  reinoud 	memset(mem, 0, lb_size);
    515   1.1  reinoud 
    516   1.1  reinoud 	udf_puteccline(eccline);
    517   1.1  reinoud 
    518   1.1  reinoud 	*dscrptr = (union dscrptr *) mem;
    519   1.1  reinoud 	return 0;
    520   1.1  reinoud }
    521   1.1  reinoud 
    522   1.1  reinoud 
    523   1.1  reinoud static void
    524   1.5  reinoud udf_free_nodedscr_rmw(struct udf_strat_args *args)
    525   1.1  reinoud {
    526   1.1  reinoud 	struct udf_mount *ump  = args->ump;
    527   1.1  reinoud 	struct long_ad   *icb  = args->icb;
    528   1.1  reinoud 	struct udf_eccline *eccline;
    529   1.1  reinoud 	uint64_t bit;
    530   1.1  reinoud 	uint32_t sectornr, dummy;
    531   1.1  reinoud 	int error, eccsect;
    532   1.1  reinoud 
    533   1.1  reinoud 	error = udf_translate_vtop(ump, icb, &sectornr, &dummy);
    534   1.1  reinoud 	if (error)
    535   1.1  reinoud 		return;
    536   1.1  reinoud 
    537   1.1  reinoud 	/* get our eccline */
    538   1.1  reinoud 	eccline = udf_geteccline(ump, sectornr, 0);
    539   1.1  reinoud 	eccsect = sectornr - eccline->start_sector;
    540   1.1  reinoud 
    541   1.1  reinoud 	bit = (uint64_t) 1 << eccsect;
    542   1.1  reinoud 	eccline->readin &= ~bit;	/* just in case */
    543   1.1  reinoud 
    544   1.1  reinoud 	KASSERT(eccline->refcnt >= 1);
    545   1.1  reinoud 	eccline->refcnt--;
    546   1.1  reinoud 
    547   1.1  reinoud 	udf_puteccline(eccline);
    548   1.1  reinoud }
    549   1.1  reinoud 
    550   1.1  reinoud 
    551   1.1  reinoud static int
    552   1.5  reinoud udf_read_nodedscr_rmw(struct udf_strat_args *args)
    553   1.1  reinoud {
    554   1.1  reinoud 	union dscrptr   **dscrptr = &args->dscr;
    555   1.1  reinoud 	struct udf_mount *ump = args->ump;
    556   1.1  reinoud 	struct long_ad   *icb = args->icb;
    557   1.1  reinoud 	struct udf_eccline *eccline;
    558   1.1  reinoud 	uint64_t bit;
    559   1.1  reinoud 	uint32_t sectornr, dummy;
    560   1.1  reinoud 	uint8_t *pos;
    561   1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
    562   1.1  reinoud 	int lb_size = udf_rw32(ump->logical_vol->lb_size);
    563   1.1  reinoud 	int i, error, dscrlen, eccsect;
    564   1.1  reinoud 
    565   1.1  reinoud 	lb_size = lb_size;
    566   1.1  reinoud 	KASSERT(sector_size == lb_size);
    567   1.1  reinoud 	error = udf_translate_vtop(ump, icb, &sectornr, &dummy);
    568   1.1  reinoud 	if (error)
    569   1.1  reinoud 		return error;
    570   1.1  reinoud 
    571   1.1  reinoud 	/* get our eccline */
    572   1.1  reinoud 	eccline = udf_geteccline(ump, sectornr, 0);
    573   1.1  reinoud 	eccsect = sectornr - eccline->start_sector;
    574   1.1  reinoud 
    575   1.1  reinoud 	bit = (uint64_t) 1 << eccsect;
    576   1.1  reinoud 	if ((eccline->present & bit) == 0) {
    577   1.1  reinoud 		/* mark bit for readin */
    578   1.1  reinoud 		eccline->readin |= bit;
    579   1.1  reinoud 		eccline->refcnt++;	/* prevent recycling */
    580   1.1  reinoud 		KASSERT(eccline->bufs[eccsect] == NULL);
    581   1.1  reinoud 		udf_puteccline(eccline);
    582   1.1  reinoud 
    583   1.1  reinoud 		/* wait for completion; XXX remodel to lock bit code */
    584   1.1  reinoud 		error = 0;
    585   1.1  reinoud 		while ((eccline->present & bit) == 0) {
    586   1.1  reinoud 			tsleep(eccline, PRIBIO+1, "udflvdrd", hz/8);
    587   1.1  reinoud 			if (eccline->error & bit) {
    588   1.1  reinoud 				KASSERT(eccline->refcnt >= 1);
    589   1.1  reinoud 				eccline->refcnt--;	/* undo temp refcnt */
    590   1.1  reinoud 				*dscrptr = NULL;
    591   1.1  reinoud 				return EIO;		/* XXX error code */
    592   1.1  reinoud 			}
    593   1.1  reinoud 		}
    594   1.1  reinoud 
    595   1.1  reinoud 		/* reget our line */
    596   1.1  reinoud 		eccline = udf_geteccline(ump, sectornr, 0);
    597   1.1  reinoud 		KASSERT(eccline->refcnt >= 1);
    598   1.1  reinoud 		eccline->refcnt--;	/* undo refcnt */
    599   1.1  reinoud 	}
    600   1.1  reinoud 
    601   1.1  reinoud 	*dscrptr = (union dscrptr *)
    602   1.1  reinoud 		(((uint8_t *) eccline->blob) + eccsect * sector_size);
    603   1.1  reinoud 
    604   1.1  reinoud 	/* code from read_phys_descr */
    605   1.1  reinoud 	/* check if its a valid tag */
    606   1.1  reinoud 	error = udf_check_tag(*dscrptr);
    607   1.1  reinoud 	if (error) {
    608   1.1  reinoud 		/* check if its an empty block */
    609   1.1  reinoud 		pos = (uint8_t *) *dscrptr;
    610   1.1  reinoud 		for (i = 0; i < sector_size; i++, pos++) {
    611   1.1  reinoud 			if (*pos) break;
    612   1.1  reinoud 		}
    613   1.1  reinoud 		if (i == sector_size) {
    614   1.1  reinoud 			/* return no error but with no dscrptr */
    615   1.1  reinoud 			error = 0;
    616   1.1  reinoud 		}
    617   1.1  reinoud 		*dscrptr = NULL;
    618   1.1  reinoud 		udf_puteccline(eccline);
    619   1.1  reinoud 		return error;
    620   1.1  reinoud 	}
    621   1.1  reinoud 
    622   1.1  reinoud 	/* calculate descriptor size */
    623   1.1  reinoud 	dscrlen = udf_tagsize(*dscrptr, sector_size);
    624   1.1  reinoud 	error = udf_check_tag_payload(*dscrptr, dscrlen);
    625   1.1  reinoud 	if (error) {
    626   1.1  reinoud 		*dscrptr = NULL;
    627   1.1  reinoud 		udf_puteccline(eccline);
    628   1.1  reinoud 		return error;
    629   1.1  reinoud 	}
    630   1.1  reinoud 
    631   1.1  reinoud 	eccline->refcnt++;
    632   1.1  reinoud 	udf_puteccline(eccline);
    633   1.1  reinoud 
    634   1.1  reinoud 	return 0;
    635   1.1  reinoud }
    636   1.1  reinoud 
    637   1.1  reinoud 
    638   1.1  reinoud static int
    639   1.5  reinoud udf_write_nodedscr_rmw(struct udf_strat_args *args)
    640   1.1  reinoud {
    641   1.1  reinoud 	union dscrptr    *dscrptr = args->dscr;
    642   1.1  reinoud 	struct udf_mount *ump = args->ump;
    643   1.1  reinoud 	struct long_ad   *icb = args->icb;
    644   1.1  reinoud 	struct udf_node *udf_node = args->udf_node;
    645   1.1  reinoud 	struct udf_eccline *eccline;
    646   1.1  reinoud 	uint64_t bit;
    647   1.1  reinoud 	uint32_t sectornr, logsectornr, dummy;
    648   1.1  reinoud 	// int waitfor  = args->waitfor;
    649   1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
    650   1.1  reinoud 	int lb_size = udf_rw32(ump->logical_vol->lb_size);
    651   1.1  reinoud 	int error, eccsect;
    652   1.1  reinoud 
    653   1.1  reinoud 	lb_size = lb_size;
    654   1.1  reinoud 	KASSERT(sector_size == lb_size);
    655   1.1  reinoud 	sectornr    = 0;
    656   1.1  reinoud 	error = udf_translate_vtop(ump, icb, &sectornr, &dummy);
    657   1.1  reinoud 	if (error)
    658   1.1  reinoud 		return error;
    659   1.1  reinoud 
    660   1.5  reinoud 	/* add reference to the vnode to prevent recycling */
    661   1.5  reinoud 	vhold(udf_node->vnode);
    662   1.5  reinoud 
    663   1.1  reinoud 	/* get our eccline */
    664   1.1  reinoud 	eccline = udf_geteccline(ump, sectornr, 0);
    665   1.1  reinoud 	eccsect = sectornr - eccline->start_sector;
    666   1.1  reinoud 
    667   1.1  reinoud 	bit = (uint64_t) 1 << eccsect;
    668   1.1  reinoud 
    669   1.1  reinoud 	/* old callback still pending? */
    670   1.1  reinoud 	if (eccline->bufs[eccsect]) {
    671   1.5  reinoud 		DPRINTF(WRITE, ("udf_write_nodedscr_rmw: writing descriptor"
    672   1.1  reinoud 					" over buffer?\n"));
    673   1.1  reinoud 		nestiobuf_done(eccline->bufs[eccsect],
    674   1.1  reinoud 				eccline->bufs_len[eccsect],
    675   1.1  reinoud 				0);
    676   1.1  reinoud 		eccline->bufs[eccsect] = NULL;
    677   1.1  reinoud 	}
    678   1.1  reinoud 
    679   1.1  reinoud 	/* set sector number in the descriptor and validate */
    680   1.1  reinoud 	dscrptr = (union dscrptr *)
    681   1.1  reinoud 		(((uint8_t *) eccline->blob) + eccsect * sector_size);
    682   1.1  reinoud 	KASSERT(dscrptr == args->dscr);
    683   1.1  reinoud 
    684   1.1  reinoud 	logsectornr = udf_rw32(icb->loc.lb_num);
    685   1.1  reinoud 	dscrptr->tag.tag_loc = udf_rw32(logsectornr);
    686   1.1  reinoud 	udf_validate_tag_and_crc_sums(dscrptr);
    687   1.1  reinoud 
    688   1.1  reinoud 	udf_fixup_node_internals(ump, (uint8_t *) dscrptr, UDF_C_NODE);
    689   1.1  reinoud 
    690   1.1  reinoud 	/* set our flags */
    691   1.1  reinoud 	KASSERT(eccline->present & bit);
    692   1.1  reinoud 	eccline->dirty |= bit;
    693   1.1  reinoud 
    694   1.1  reinoud 	KASSERT(udf_tagsize(dscrptr, sector_size) <= sector_size);
    695   1.1  reinoud 
    696   1.1  reinoud 	udf_puteccline(eccline);
    697   1.1  reinoud 
    698   1.5  reinoud 	holdrele(udf_node->vnode);
    699   1.5  reinoud 	udf_node->outstanding_nodedscr--;
    700   1.5  reinoud 	if (udf_node->outstanding_nodedscr == 0) {
    701  1.19  reinoud 		UDF_UNLOCK_NODE(udf_node, 0);
    702   1.5  reinoud 		wakeup(&udf_node->outstanding_nodedscr);
    703   1.5  reinoud 	}
    704   1.5  reinoud 
    705   1.1  reinoud 	/* XXX waitfor not used */
    706   1.1  reinoud 	return 0;
    707   1.1  reinoud }
    708   1.1  reinoud 
    709   1.1  reinoud 
    710   1.1  reinoud static void
    711   1.1  reinoud udf_queuebuf_rmw(struct udf_strat_args *args)
    712   1.1  reinoud {
    713   1.1  reinoud 	struct udf_mount *ump = args->ump;
    714   1.1  reinoud 	struct buf *buf = args->nestbuf;
    715   1.6  reinoud 	struct desc_tag *tag;
    716   1.1  reinoud 	struct strat_private *priv = PRIV(ump);
    717   1.1  reinoud 	struct udf_eccline *eccline;
    718   1.1  reinoud 	struct long_ad *node_ad_cpy;
    719   1.1  reinoud 	uint64_t bit, *lmapping, *pmapping, *lmappos, *pmappos, blknr;
    720   1.6  reinoud 	uint32_t buf_len, len, sectors, sectornr, our_sectornr;
    721   1.1  reinoud 	uint32_t bpos;
    722   1.6  reinoud 	uint16_t vpart_num;
    723   1.1  reinoud 	uint8_t *fidblk, *src, *dst;
    724   1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
    725   1.1  reinoud 	int blks = sector_size / DEV_BSIZE;
    726   1.1  reinoud 	int eccsect, what, queue, error;
    727   1.1  reinoud 
    728   1.1  reinoud 	KASSERT(ump);
    729   1.1  reinoud 	KASSERT(buf);
    730   1.1  reinoud 	KASSERT(buf->b_iodone == nestiobuf_iodone);
    731   1.1  reinoud 
    732   1.1  reinoud 	blknr        = buf->b_blkno;
    733   1.1  reinoud 	our_sectornr = blknr / blks;
    734   1.1  reinoud 
    735   1.1  reinoud 	what = buf->b_udf_c_type;
    736   1.1  reinoud 	queue = UDF_SHED_READING;
    737   1.1  reinoud 	if ((buf->b_flags & B_READ) == 0) {
    738   1.1  reinoud 		/* writing */
    739   1.1  reinoud 		queue = UDF_SHED_SEQWRITING;
    740  1.18  reinoud 		if (what == UDF_C_ABSOLUTE)
    741  1.18  reinoud 			queue = UDF_SHED_WRITING;
    742   1.1  reinoud 		if (what == UDF_C_DSCR)
    743   1.1  reinoud 			queue = UDF_SHED_WRITING;
    744   1.1  reinoud 		if (what == UDF_C_NODE)
    745   1.1  reinoud 			queue = UDF_SHED_WRITING;
    746   1.1  reinoud 	}
    747   1.1  reinoud 
    748   1.1  reinoud 	if (queue == UDF_SHED_READING) {
    749   1.3  reinoud 		DPRINTF(SHEDULE, ("\nudf_queuebuf_rmw READ %p : sector %d type %d,"
    750   1.1  reinoud 			"b_resid %d, b_bcount %d, b_bufsize %d\n",
    751   1.1  reinoud 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
    752   1.1  reinoud 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
    753   1.1  reinoud 
    754   1.1  reinoud 		/* mark bits for reading */
    755   1.1  reinoud 		buf_len = buf->b_bcount;
    756   1.1  reinoud 		sectornr = our_sectornr;
    757   1.1  reinoud 		eccline = udf_geteccline(ump, sectornr, 0);
    758   1.1  reinoud 		eccsect = sectornr - eccline->start_sector;
    759   1.1  reinoud 		bpos = 0;
    760   1.1  reinoud 		while (buf_len) {
    761   1.1  reinoud 			len = MIN(buf_len, sector_size);
    762   1.1  reinoud 			if (eccsect == ump->packet_size) {
    763   1.1  reinoud 				udf_puteccline(eccline);
    764   1.1  reinoud 				eccline = udf_geteccline(ump, sectornr, 0);
    765   1.1  reinoud 				eccsect = sectornr - eccline->start_sector;
    766   1.1  reinoud 			}
    767   1.1  reinoud 			bit = (uint64_t) 1 << eccsect;
    768   1.1  reinoud 			error = eccline->error & bit ? EIO : 0;
    769   1.1  reinoud 			if (eccline->present & bit) {
    770   1.1  reinoud 				src = (uint8_t *) eccline->blob +
    771   1.1  reinoud 					eccsect * sector_size;
    772   1.1  reinoud 				dst = (uint8_t *) buf->b_data + bpos;
    773   1.1  reinoud 				if (!error)
    774   1.1  reinoud 					memcpy(dst, src, len);
    775   1.1  reinoud 				nestiobuf_done(buf, len, error);
    776   1.1  reinoud 			} else {
    777   1.1  reinoud 				eccline->readin |= bit;
    778   1.1  reinoud 				KASSERT(eccline->bufs[eccsect] == NULL);
    779   1.1  reinoud 				eccline->bufs[eccsect] = buf;
    780   1.1  reinoud 				eccline->bufs_bpos[eccsect] = bpos;
    781   1.1  reinoud 				eccline->bufs_len[eccsect] = len;
    782   1.1  reinoud 			}
    783   1.1  reinoud 			bpos += sector_size;
    784   1.1  reinoud 			eccsect++;
    785   1.1  reinoud 			sectornr++;
    786   1.1  reinoud 			buf_len -= len;
    787   1.1  reinoud 		}
    788   1.1  reinoud 		udf_puteccline(eccline);
    789   1.1  reinoud 		return;
    790   1.1  reinoud 	}
    791   1.1  reinoud 
    792   1.1  reinoud 	if (queue == UDF_SHED_WRITING) {
    793   1.3  reinoud 		DPRINTF(SHEDULE, ("\nudf_queuebuf_rmw WRITE %p : sector %d "
    794   1.1  reinoud 			"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
    795   1.1  reinoud 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
    796   1.1  reinoud 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
    797   1.1  reinoud 		/* if we have FIDs fixup using buffer's sector number(s) */
    798   1.1  reinoud 		if (buf->b_udf_c_type == UDF_C_FIDS) {
    799   1.1  reinoud 			panic("UDF_C_FIDS in SHED_WRITING!\n");
    800   1.1  reinoud #if 0
    801   1.1  reinoud 			buf_len = buf->b_bcount;
    802   1.1  reinoud 			sectornr = our_sectornr;
    803   1.1  reinoud 			bpos = 0;
    804   1.1  reinoud 			while (buf_len) {
    805   1.1  reinoud 				len = MIN(buf_len, sector_size);
    806   1.1  reinoud 				fidblk = (uint8_t *) buf->b_data + bpos;
    807   1.1  reinoud 				udf_fixup_fid_block(fidblk, sector_size,
    808   1.1  reinoud 					0, len, sectornr);
    809   1.1  reinoud 				sectornr++;
    810   1.1  reinoud 				bpos += len;
    811   1.1  reinoud 				buf_len -= len;
    812   1.1  reinoud 			}
    813   1.1  reinoud #endif
    814   1.1  reinoud 		}
    815   1.1  reinoud 		udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
    816   1.1  reinoud 
    817   1.1  reinoud 		/* copy parts into the bufs and set for writing */
    818   1.1  reinoud 		buf_len = buf->b_bcount;
    819   1.1  reinoud 		sectornr = our_sectornr;
    820   1.1  reinoud 		eccline = udf_geteccline(ump, sectornr, 0);
    821   1.1  reinoud 		eccsect = sectornr - eccline->start_sector;
    822   1.1  reinoud 		bpos = 0;
    823   1.1  reinoud 		while (buf_len) {
    824   1.1  reinoud 			len = MIN(buf_len, sector_size);
    825   1.1  reinoud 			if (eccsect == ump->packet_size) {
    826   1.1  reinoud 				udf_puteccline(eccline);
    827   1.1  reinoud 				eccline = udf_geteccline(ump, sectornr, 0);
    828   1.1  reinoud 				eccsect = sectornr - eccline->start_sector;
    829   1.1  reinoud 			}
    830   1.1  reinoud 			bit = (uint64_t) 1 << eccsect;
    831   1.1  reinoud 			KASSERT((eccline->readin & bit) == 0);
    832   1.1  reinoud 			eccline->present |= bit;
    833   1.1  reinoud 			eccline->dirty   |= bit;
    834   1.1  reinoud 			if (eccline->bufs[eccsect]) {
    835   1.1  reinoud 				/* old callback still pending */
    836   1.1  reinoud 				nestiobuf_done(eccline->bufs[eccsect],
    837   1.1  reinoud 						eccline->bufs_len[eccsect],
    838   1.1  reinoud 						0);
    839   1.1  reinoud 				eccline->bufs[eccsect] = NULL;
    840   1.1  reinoud 			}
    841   1.1  reinoud 
    842   1.2  reinoud 			src = (uint8_t *) buf->b_data + bpos;
    843   1.2  reinoud 			dst = (uint8_t *) eccline->blob + eccsect * sector_size;
    844   1.2  reinoud 			if (len != sector_size)
    845   1.2  reinoud 				memset(dst, 0, sector_size);
    846   1.2  reinoud 			memcpy(dst, src, len);
    847   1.2  reinoud 
    848   1.1  reinoud 			/* note that its finished for this extent */
    849   1.1  reinoud 			eccline->bufs[eccsect] = NULL;
    850   1.1  reinoud 			nestiobuf_done(buf, len, 0);
    851   1.1  reinoud 
    852   1.1  reinoud 			bpos += sector_size;
    853   1.1  reinoud 			eccsect++;
    854   1.1  reinoud 			sectornr++;
    855   1.1  reinoud 			buf_len -= len;
    856   1.1  reinoud 		}
    857   1.1  reinoud 		udf_puteccline(eccline);
    858   1.1  reinoud 		return;
    859   1.1  reinoud 
    860   1.1  reinoud 	}
    861   1.1  reinoud 
    862   1.1  reinoud 	/* sequential writing */
    863   1.1  reinoud 	KASSERT(queue == UDF_SHED_SEQWRITING);
    864   1.3  reinoud 	DPRINTF(SHEDULE, ("\nudf_queuebuf_rmw SEQWRITE %p : sector XXXX "
    865   1.1  reinoud 		"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
    866   1.1  reinoud 		buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount,
    867   1.1  reinoud 		buf->b_bufsize));
    868   1.1  reinoud 	/*
    869   1.1  reinoud 	 * Buffers should not have been allocated to disc addresses yet on
    870   1.1  reinoud 	 * this queue. Note that a buffer can get multiple extents allocated.
    871   1.1  reinoud 	 * Note that it *looks* like the normal writing but its different in
    872   1.1  reinoud 	 * the details.
    873   1.1  reinoud 	 *
    874   1.6  reinoud 	 * lmapping contains lb_num relative to base partition.
    875   1.6  reinoud 	 *
    876   1.6  reinoud 	 * XXX should we try to claim/organize the allocated memory to
    877   1.6  reinoud 	 * block-aligned pieces?
    878   1.1  reinoud 	 */
    879   1.1  reinoud 	mutex_enter(&priv->seqwrite_mutex);
    880   1.1  reinoud 
    881   1.1  reinoud 	lmapping    = ump->la_lmapping;
    882   1.1  reinoud 	node_ad_cpy = ump->la_node_ad_cpy;
    883   1.1  reinoud 
    884   1.6  reinoud 	/* logically allocate buf and map it in the file */
    885   1.6  reinoud 	udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num);
    886   1.1  reinoud 
    887   1.1  reinoud 	/* if we have FIDs, fixup using the new allocation table */
    888   1.1  reinoud 	if (buf->b_udf_c_type == UDF_C_FIDS) {
    889   1.1  reinoud 		buf_len = buf->b_bcount;
    890   1.1  reinoud 		bpos = 0;
    891   1.1  reinoud 		lmappos = lmapping;
    892   1.1  reinoud 		while (buf_len) {
    893   1.1  reinoud 			sectornr = *lmappos++;
    894   1.1  reinoud 			len = MIN(buf_len, sector_size);
    895   1.1  reinoud 			fidblk = (uint8_t *) buf->b_data + bpos;
    896   1.1  reinoud 			udf_fixup_fid_block(fidblk, sector_size,
    897   1.1  reinoud 				0, len, sectornr);
    898   1.1  reinoud 			bpos += len;
    899   1.1  reinoud 			buf_len -= len;
    900   1.1  reinoud 		}
    901   1.1  reinoud 	}
    902   1.6  reinoud 	if (buf->b_udf_c_type == UDF_C_METADATA_SBM) {
    903   1.6  reinoud 		if (buf->b_lblkno == 0) {
    904   1.6  reinoud 			/* update the tag location inside */
    905   1.6  reinoud 			tag = (struct desc_tag *) buf->b_data;
    906   1.7  reinoud 			tag->tag_loc = udf_rw32(*lmapping);
    907   1.6  reinoud 			udf_validate_tag_and_crc_sums(buf->b_data);
    908   1.6  reinoud 		}
    909   1.6  reinoud 	}
    910   1.1  reinoud 	udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
    911   1.1  reinoud 
    912   1.6  reinoud 	/*
    913   1.6  reinoud 	 * Translate new mappings in lmapping to pmappings.
    914   1.6  reinoud 	 * pmapping to contain lb_nums as used for disc adressing.
    915   1.6  reinoud 	 */
    916   1.6  reinoud 	pmapping = ump->la_pmapping;
    917   1.6  reinoud 	sectors  = (buf->b_bcount + sector_size -1) / sector_size;
    918   1.6  reinoud 	udf_translate_vtop_list(ump, sectors, vpart_num, lmapping, pmapping);
    919   1.6  reinoud 
    920   1.1  reinoud 	/* copy parts into the bufs and set for writing */
    921   1.1  reinoud 	pmappos = pmapping;
    922   1.1  reinoud 	buf_len = buf->b_bcount;
    923   1.1  reinoud 	sectornr = *pmappos++;
    924   1.1  reinoud 	eccline = udf_geteccline(ump, sectornr, ECC_SEQWRITING);
    925   1.1  reinoud 	eccsect = sectornr - eccline->start_sector;
    926   1.1  reinoud 	bpos = 0;
    927   1.1  reinoud 	while (buf_len) {
    928   1.1  reinoud 		len = MIN(buf_len, sector_size);
    929   1.1  reinoud 		eccsect = sectornr - eccline->start_sector;
    930   1.1  reinoud 		if ((eccsect < 0) || (eccsect >= ump->packet_size)) {
    931   1.1  reinoud 			eccline->flags |= ECC_SEQWRITING;
    932   1.1  reinoud 			udf_puteccline(eccline);
    933   1.1  reinoud 			eccline = udf_geteccline(ump, sectornr, ECC_SEQWRITING);
    934   1.1  reinoud 			eccsect = sectornr - eccline->start_sector;
    935   1.1  reinoud 		}
    936   1.1  reinoud 		bit = (uint64_t) 1 << eccsect;
    937   1.1  reinoud 		KASSERT((eccline->readin & bit) == 0);
    938   1.1  reinoud 		eccline->present |= bit;
    939   1.1  reinoud 		eccline->dirty   |= bit;
    940   1.1  reinoud 		eccline->bufs[eccsect] = NULL;
    941   1.1  reinoud 
    942   1.1  reinoud 		src = (uint8_t *) buf->b_data + bpos;
    943   1.1  reinoud 		dst = (uint8_t *)
    944   1.1  reinoud 			eccline->blob + eccsect * sector_size;
    945   1.1  reinoud 		if (len != sector_size)
    946   1.1  reinoud 			memset(dst, 0, sector_size);
    947   1.1  reinoud 		memcpy(dst, src, len);
    948   1.1  reinoud 
    949   1.1  reinoud 		/* note that its finished for this extent */
    950   1.1  reinoud 		nestiobuf_done(buf, len, 0);
    951   1.1  reinoud 
    952   1.1  reinoud 		bpos += sector_size;
    953   1.1  reinoud 		sectornr = *pmappos++;
    954   1.1  reinoud 		buf_len -= len;
    955   1.1  reinoud 	}
    956   1.1  reinoud 	eccline->flags |= ECC_SEQWRITING;
    957   1.1  reinoud 	udf_puteccline(eccline);
    958   1.1  reinoud 	mutex_exit(&priv->seqwrite_mutex);
    959   1.1  reinoud }
    960   1.1  reinoud 
    961   1.1  reinoud /* --------------------------------------------------------------------- */
    962   1.1  reinoud 
    963   1.1  reinoud static void
    964   1.1  reinoud udf_shedule_read_callback(struct buf *buf)
    965   1.1  reinoud {
    966   1.1  reinoud 	struct udf_eccline *eccline = BTOE(buf);
    967   1.1  reinoud 	struct udf_mount *ump = eccline->ump;
    968   1.1  reinoud 	uint64_t bit;
    969   1.1  reinoud 	uint8_t *src, *dst;
    970   1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
    971   1.1  reinoud 	int error, i, len;
    972   1.1  reinoud 
    973   1.3  reinoud 	DPRINTF(ECCLINE, ("read callback called\n"));
    974   1.1  reinoud 	/* post process read action */
    975   1.1  reinoud 	error = buf->b_error;
    976   1.1  reinoud 	for (i = 0; i < ump->packet_size; i++) {
    977   1.1  reinoud 		bit = (uint64_t) 1 << i;
    978   1.1  reinoud 		src = (uint8_t *) buf->b_data +   i * sector_size;
    979   1.1  reinoud 		dst = (uint8_t *) eccline->blob + i * sector_size;
    980   1.1  reinoud 		if (eccline->present & bit)
    981   1.1  reinoud 			continue;
    982   1.8  reinoud 		eccline->present |= bit;
    983   1.8  reinoud 		if (error)
    984   1.1  reinoud 			eccline->error |= bit;
    985   1.1  reinoud 		if (eccline->bufs[i]) {
    986   1.1  reinoud 			dst = (uint8_t *) eccline->bufs[i]->b_data +
    987   1.1  reinoud 				eccline->bufs_bpos[i];
    988   1.1  reinoud 			len = eccline->bufs_len[i];
    989   1.1  reinoud 			if (!error)
    990   1.1  reinoud 				memcpy(dst, src, len);
    991   1.1  reinoud 			nestiobuf_done(eccline->bufs[i], len, error);
    992   1.1  reinoud 			eccline->bufs[i] = NULL;
    993   1.1  reinoud 		}
    994   1.1  reinoud 
    995   1.1  reinoud 	}
    996   1.1  reinoud 	KASSERT(buf->b_data == eccline->blob);
    997   1.1  reinoud 	KASSERT(eccline->present == ((uint64_t) 1 << ump->packet_size)-1);
    998   1.1  reinoud 
    999   1.1  reinoud 	/*
   1000   1.1  reinoud 	 * XXX TODO what to do on read errors? read in all sectors
   1001   1.1  reinoud 	 * synchronously and allocate a sparable entry?
   1002   1.1  reinoud 	 */
   1003   1.1  reinoud 
   1004   1.1  reinoud 	udf_puteccline(eccline);
   1005   1.3  reinoud 	DPRINTF(ECCLINE, ("read callback finished\n"));
   1006   1.1  reinoud }
   1007   1.1  reinoud 
   1008   1.1  reinoud 
   1009   1.1  reinoud static void
   1010   1.1  reinoud udf_shedule_write_callback(struct buf *buf)
   1011   1.1  reinoud {
   1012   1.1  reinoud 	struct udf_eccline *eccline = BTOE(buf);
   1013   1.1  reinoud 	struct udf_mount *ump = eccline->ump;
   1014   1.1  reinoud 	uint64_t bit;
   1015   1.1  reinoud 	int error, i, len;
   1016   1.1  reinoud 
   1017   1.3  reinoud 	DPRINTF(ECCLINE, ("write callback called\n"));
   1018   1.1  reinoud 	/* post process write action */
   1019   1.1  reinoud 	error = buf->b_error;
   1020   1.1  reinoud 	for (i = 0; i < ump->packet_size; i++) {
   1021   1.1  reinoud 		bit = (uint64_t) 1 << i;
   1022   1.1  reinoud 		if ((eccline->dirty & bit) == 0)
   1023   1.1  reinoud 			continue;
   1024   1.1  reinoud 		if (error) {
   1025   1.1  reinoud 			eccline->error |= bit;
   1026   1.1  reinoud 		} else {
   1027   1.1  reinoud 			eccline->dirty &= ~bit;
   1028   1.1  reinoud 		}
   1029   1.1  reinoud 		if (eccline->bufs[i]) {
   1030   1.1  reinoud 			len = eccline->bufs_len[i];
   1031   1.1  reinoud 			nestiobuf_done(eccline->bufs[i], len, error);
   1032   1.1  reinoud 			eccline->bufs[i] = NULL;
   1033   1.1  reinoud 		}
   1034   1.1  reinoud 	}
   1035   1.1  reinoud 	KASSERT(eccline->dirty == 0);
   1036   1.1  reinoud 
   1037   1.1  reinoud 	KASSERT(error == 0);
   1038   1.1  reinoud 	/*
   1039  1.13  reinoud 	 * XXX TODO on write errors allocate a sparable entry and reissue
   1040   1.1  reinoud 	 */
   1041   1.1  reinoud 
   1042   1.1  reinoud 	udf_puteccline(eccline);
   1043   1.1  reinoud }
   1044   1.1  reinoud 
   1045   1.1  reinoud 
   1046   1.1  reinoud static void
   1047   1.1  reinoud udf_issue_eccline(struct udf_eccline *eccline, int queued_on)
   1048   1.1  reinoud {
   1049   1.1  reinoud 	struct udf_mount *ump = eccline->ump;
   1050   1.1  reinoud 	struct strat_private *priv = PRIV(ump);
   1051   1.1  reinoud 	struct buf *buf, *nestbuf;
   1052   1.1  reinoud 	uint64_t bit, allbits = ((uint64_t) 1 << ump->packet_size)-1;
   1053   1.1  reinoud 	uint32_t start;
   1054   1.1  reinoud 	int sector_size = ump->discinfo.sector_size;
   1055   1.1  reinoud 	int blks = sector_size / DEV_BSIZE;
   1056   1.1  reinoud 	int i;
   1057   1.1  reinoud 
   1058   1.1  reinoud 	if (queued_on == UDF_SHED_READING) {
   1059   1.3  reinoud 		DPRINTF(SHEDULE, ("udf_issue_eccline reading : "));
   1060   1.1  reinoud 		/* read all bits that are not yet present */
   1061   1.1  reinoud 		eccline->readin = (~eccline->present) & allbits;
   1062   1.1  reinoud 		KASSERT(eccline->readin);
   1063   1.1  reinoud 		start = eccline->start_sector;
   1064   1.1  reinoud 		buf = eccline->buf;
   1065   1.1  reinoud 		buf->b_flags    = B_READ | B_ASYNC;
   1066   1.4  reinoud 		SET(buf->b_cflags, BC_BUSY);	/* mark buffer busy */
   1067   1.1  reinoud 		buf->b_oflags   = 0;
   1068   1.1  reinoud 		buf->b_iodone   = udf_shedule_read_callback;
   1069   1.1  reinoud 		buf->b_data     = eccline->blob;
   1070   1.1  reinoud 		buf->b_bcount   = ump->packet_size * sector_size;
   1071   1.1  reinoud 		buf->b_resid    = buf->b_bcount;
   1072   1.1  reinoud 		buf->b_bufsize  = buf->b_bcount;
   1073   1.1  reinoud 		buf->b_private  = eccline;
   1074   1.1  reinoud 		BIO_SETPRIO(buf, BPRIO_DEFAULT);
   1075   1.1  reinoud 		buf->b_lblkno   = buf->b_blkno = buf->b_rawblkno = start * blks;
   1076   1.1  reinoud 		buf->b_proc     = NULL;
   1077   1.1  reinoud 
   1078   1.1  reinoud 		if (eccline->present != 0) {
   1079   1.1  reinoud 			for (i = 0; i < ump->packet_size; i++) {
   1080   1.1  reinoud 				bit = (uint64_t) 1 << i;
   1081   1.1  reinoud 				if (eccline->present & bit) {
   1082   1.1  reinoud 					nestiobuf_done(buf, sector_size, 0);
   1083   1.1  reinoud 					continue;
   1084   1.1  reinoud 				}
   1085   1.1  reinoud 				nestbuf = getiobuf(NULL, true);
   1086   1.1  reinoud 				nestiobuf_setup(buf, nestbuf, i * sector_size,
   1087   1.1  reinoud 					sector_size);
   1088   1.1  reinoud 				/* adjust blocknumber to read */
   1089   1.1  reinoud 				nestbuf->b_blkno = buf->b_blkno + i*blks;
   1090   1.1  reinoud 				nestbuf->b_rawblkno = buf->b_rawblkno + i*blks;
   1091   1.1  reinoud 
   1092   1.3  reinoud 				DPRINTF(SHEDULE, ("sector %d ",
   1093   1.3  reinoud 					start + i));
   1094   1.1  reinoud 				/* call asynchronous */
   1095   1.1  reinoud 				VOP_STRATEGY(ump->devvp, nestbuf);
   1096   1.1  reinoud 			}
   1097   1.3  reinoud 			DPRINTF(SHEDULE, ("\n"));
   1098   1.1  reinoud 			return;
   1099   1.1  reinoud 		}
   1100   1.1  reinoud 	} else {
   1101   1.1  reinoud 		/* write or seqwrite */
   1102   1.3  reinoud 		DPRINTF(SHEDULE, ("udf_issue_eccline writing or seqwriting : "));
   1103  1.13  reinoud 		DPRINTF(SHEDULE, ("\n\tpresent %"PRIx64", readin %"PRIx64", "
   1104  1.13  reinoud 			"dirty %"PRIx64"\n\t", eccline->present, eccline->readin,
   1105  1.13  reinoud 			eccline->dirty));
   1106   1.1  reinoud 		if (eccline->present != allbits) {
   1107   1.1  reinoud 			/* requeue to read-only */
   1108   1.3  reinoud 			DPRINTF(SHEDULE, ("\n\t-> not complete, requeue to "
   1109   1.3  reinoud 				"reading\n"));
   1110   1.1  reinoud 			udf_push_eccline(eccline, UDF_SHED_READING);
   1111   1.1  reinoud 			return;
   1112   1.1  reinoud 		}
   1113   1.1  reinoud 		start = eccline->start_sector;
   1114   1.1  reinoud 		buf = eccline->buf;
   1115   1.1  reinoud 		buf->b_flags    = B_WRITE | B_ASYNC;
   1116   1.4  reinoud 		SET(buf->b_cflags, BC_BUSY);	/* mark buffer busy */
   1117   1.1  reinoud 		buf->b_oflags   = 0;
   1118   1.1  reinoud 		buf->b_iodone   = udf_shedule_write_callback;
   1119   1.1  reinoud 		buf->b_data     = eccline->blob;
   1120   1.1  reinoud 		buf->b_bcount   = ump->packet_size * sector_size;
   1121   1.1  reinoud 		buf->b_resid    = buf->b_bcount;
   1122   1.1  reinoud 		buf->b_bufsize  = buf->b_bcount;
   1123   1.1  reinoud 		buf->b_private  = eccline;
   1124   1.1  reinoud 		BIO_SETPRIO(buf, BPRIO_DEFAULT);
   1125   1.1  reinoud 		buf->b_lblkno   = buf->b_blkno = buf->b_rawblkno = start * blks;
   1126   1.1  reinoud 		buf->b_proc     = NULL;
   1127   1.1  reinoud 	}
   1128   1.1  reinoud 
   1129   1.1  reinoud 	mutex_exit(&priv->discstrat_mutex);
   1130   1.1  reinoud 		/* call asynchronous */
   1131   1.3  reinoud 		DPRINTF(SHEDULE, ("sector %d for %d\n",
   1132   1.3  reinoud 			start, ump->packet_size));
   1133   1.1  reinoud 		VOP_STRATEGY(ump->devvp, buf);
   1134   1.1  reinoud 	mutex_enter(&priv->discstrat_mutex);
   1135   1.1  reinoud }
   1136   1.1  reinoud 
   1137   1.1  reinoud 
   1138   1.1  reinoud static void
   1139   1.1  reinoud udf_discstrat_thread(void *arg)
   1140   1.1  reinoud {
   1141   1.1  reinoud 	struct udf_mount *ump = (struct udf_mount *) arg;
   1142   1.1  reinoud 	struct strat_private *priv = PRIV(ump);
   1143   1.1  reinoud 	struct udf_eccline *eccline;
   1144   1.1  reinoud 	struct timespec now, *last;
   1145  1.11  reinoud 	uint64_t allbits = ((uint64_t) 1 << ump->packet_size)-1;
   1146  1.11  reinoud 	int new_queue, wait, work, num, cnt;
   1147   1.1  reinoud 
   1148   1.1  reinoud 	work = 1;
   1149  1.13  reinoud 	priv->thread_running = 1;
   1150   1.1  reinoud 	mutex_enter(&priv->discstrat_mutex);
   1151   1.1  reinoud 	priv->num_floating = 0;
   1152   1.1  reinoud 	while (priv->run_thread || work || priv->num_floating) {
   1153  1.11  reinoud 		/* get our time */
   1154  1.11  reinoud 		vfs_timestamp(&now);
   1155  1.11  reinoud 
   1156  1.11  reinoud 		/* maintenance: handle eccline state machine */
   1157  1.11  reinoud 		num = priv->num_queued[UDF_SHED_WAITING];
   1158  1.11  reinoud 		cnt = 0;
   1159  1.11  reinoud 		while (cnt < num) {
   1160  1.11  reinoud 			eccline = udf_pop_eccline(priv, UDF_SHED_WAITING);
   1161  1.11  reinoud 			/* requeue */
   1162  1.11  reinoud 			new_queue = UDF_SHED_FREE;
   1163  1.11  reinoud 			if (eccline->refcnt > 0)
   1164  1.11  reinoud 				new_queue = UDF_SHED_IDLE;
   1165  1.11  reinoud 			if (eccline->flags & ECC_WANTED)
   1166  1.11  reinoud 				new_queue = UDF_SHED_IDLE;
   1167  1.11  reinoud 			if (eccline->readin)
   1168  1.11  reinoud 				new_queue = UDF_SHED_READING;
   1169  1.11  reinoud 			if (eccline->dirty) {
   1170  1.11  reinoud 				new_queue = UDF_SHED_WAITING;
   1171  1.13  reinoud 				if ((eccline->wait_time.tv_sec - now.tv_sec <= 0) ||
   1172  1.13  reinoud 				   ((eccline->present == allbits) &&
   1173  1.13  reinoud 				    (eccline->flags & ECC_SEQWRITING)))
   1174  1.13  reinoud 				{
   1175  1.11  reinoud 					new_queue = UDF_SHED_WRITING;
   1176  1.11  reinoud 					if (eccline->flags & ECC_SEQWRITING)
   1177  1.11  reinoud 						new_queue = UDF_SHED_SEQWRITING;
   1178  1.11  reinoud 					if (eccline->present != allbits)
   1179  1.11  reinoud 						new_queue = UDF_SHED_READING;
   1180  1.11  reinoud 				}
   1181  1.11  reinoud 			}
   1182  1.11  reinoud 			udf_push_eccline(eccline, new_queue);
   1183  1.11  reinoud 			cnt++;
   1184  1.11  reinoud 		}
   1185  1.11  reinoud 
   1186   1.1  reinoud 		/* maintenance: free exess ecclines */
   1187   1.1  reinoud 		while (priv->num_queued[UDF_SHED_FREE] > UDF_ECCLINE_MAXFREE) {
   1188   1.1  reinoud 			eccline = udf_pop_eccline(priv, UDF_SHED_FREE);
   1189   1.1  reinoud 			KASSERT(eccline);
   1190   1.1  reinoud 			KASSERT(eccline->refcnt == 0);
   1191  1.11  reinoud 			if (eccline->flags & ECC_WANTED) {
   1192  1.11  reinoud 				udf_push_eccline(eccline, UDF_SHED_IDLE);
   1193  1.11  reinoud 				DPRINTF(ECCLINE, ("Tried removing, pushed back to free list\n"));
   1194  1.11  reinoud 			} else {
   1195  1.11  reinoud 				DPRINTF(ECCLINE, ("Removing entry from free list\n"));
   1196  1.11  reinoud 				udf_dispose_eccline(eccline);
   1197  1.11  reinoud 			}
   1198   1.1  reinoud 		}
   1199   1.1  reinoud 
   1200  1.11  reinoud 		/* process the current selected queue */
   1201   1.1  reinoud 		/* get our time */
   1202   1.1  reinoud 		vfs_timestamp(&now);
   1203   1.1  reinoud 		last = &priv->last_queued[priv->cur_queue];
   1204   1.1  reinoud 
   1205   1.1  reinoud 		/* get our line */
   1206   1.1  reinoud 		eccline = udf_pop_eccline(priv, priv->cur_queue);
   1207   1.1  reinoud 		if (eccline) {
   1208   1.1  reinoud 			wait = 0;
   1209   1.1  reinoud 			new_queue = priv->cur_queue;
   1210   1.3  reinoud 			DPRINTF(ECCLINE, ("UDF_ISSUE_ECCLINE\n"));
   1211   1.1  reinoud 
   1212   1.1  reinoud 			/* complete the `get' by locking and refcounting it */
   1213   1.1  reinoud 			UDF_LOCK_ECCLINE(eccline);
   1214   1.1  reinoud 			eccline->refcnt++;
   1215   1.1  reinoud 
   1216   1.1  reinoud 			udf_issue_eccline(eccline, priv->cur_queue);
   1217   1.1  reinoud 		} else {
   1218  1.13  reinoud 			/* don't switch too quickly */
   1219  1.13  reinoud 			if (now.tv_sec - last->tv_sec < 2) {
   1220  1.13  reinoud 				/* wait some time */
   1221  1.13  reinoud 				cv_timedwait(&priv->discstrat_cv,
   1222  1.13  reinoud 					&priv->discstrat_mutex, hz);
   1223  1.13  reinoud 				/* we assume there is work to be done */
   1224  1.13  reinoud 				work = 1;
   1225  1.13  reinoud 				continue;
   1226  1.13  reinoud 			}
   1227  1.13  reinoud 
   1228  1.13  reinoud 			/* XXX select on queue lengths ? */
   1229   1.1  reinoud 			wait = 1;
   1230   1.1  reinoud 			/* check if we can/should switch */
   1231   1.1  reinoud 			new_queue = priv->cur_queue;
   1232  1.17     yamt 			if (bufq_peek(priv->queues[UDF_SHED_READING]))
   1233   1.1  reinoud 				new_queue = UDF_SHED_READING;
   1234  1.17     yamt 			if (bufq_peek(priv->queues[UDF_SHED_WRITING]))
   1235   1.1  reinoud 				new_queue = UDF_SHED_WRITING;
   1236  1.17     yamt 			if (bufq_peek(priv->queues[UDF_SHED_SEQWRITING]))
   1237   1.1  reinoud 				new_queue = UDF_SHED_SEQWRITING;
   1238   1.1  reinoud 		}
   1239   1.1  reinoud 
   1240   1.1  reinoud 		/* give room */
   1241   1.1  reinoud 		mutex_exit(&priv->discstrat_mutex);
   1242   1.1  reinoud 
   1243   1.1  reinoud 		if (new_queue != priv->cur_queue) {
   1244   1.1  reinoud 			wait = 0;
   1245   1.1  reinoud 			DPRINTF(SHEDULE, ("switching from %d to %d\n",
   1246   1.1  reinoud 				priv->cur_queue, new_queue));
   1247   1.1  reinoud 			priv->cur_queue = new_queue;
   1248   1.1  reinoud 		}
   1249   1.1  reinoud 		mutex_enter(&priv->discstrat_mutex);
   1250   1.1  reinoud 
   1251   1.1  reinoud 		/* wait for more if needed */
   1252   1.1  reinoud 		if (wait)
   1253   1.1  reinoud 			cv_timedwait(&priv->discstrat_cv,
   1254  1.13  reinoud 				&priv->discstrat_mutex, hz/4);	/* /8 */
   1255   1.1  reinoud 
   1256  1.17     yamt 		work  = (bufq_peek(priv->queues[UDF_SHED_WAITING]) != NULL);
   1257  1.17     yamt 		work |= (bufq_peek(priv->queues[UDF_SHED_READING]) != NULL);
   1258  1.17     yamt 		work |= (bufq_peek(priv->queues[UDF_SHED_WRITING]) != NULL);
   1259  1.17     yamt 		work |= (bufq_peek(priv->queues[UDF_SHED_SEQWRITING]) != NULL);
   1260   1.1  reinoud 
   1261   1.1  reinoud 		DPRINTF(PARANOIA, ("work : (%d, %d, %d) -> work %d, float %d\n",
   1262  1.17     yamt 			(bufq_peek(priv->queues[UDF_SHED_READING]) != NULL),
   1263  1.17     yamt 			(bufq_peek(priv->queues[UDF_SHED_WRITING]) != NULL),
   1264  1.17     yamt 			(bufq_peek(priv->queues[UDF_SHED_SEQWRITING]) != NULL),
   1265   1.1  reinoud 			work, priv->num_floating));
   1266   1.1  reinoud 	}
   1267   1.1  reinoud 
   1268   1.1  reinoud 	mutex_exit(&priv->discstrat_mutex);
   1269   1.1  reinoud 
   1270   1.1  reinoud 	/* tear down remaining ecclines */
   1271   1.1  reinoud 	mutex_enter(&priv->discstrat_mutex);
   1272  1.11  reinoud 	KASSERT(priv->num_queued[UDF_SHED_WAITING] == 0);
   1273   1.1  reinoud 	KASSERT(priv->num_queued[UDF_SHED_IDLE] == 0);
   1274   1.1  reinoud 	KASSERT(priv->num_queued[UDF_SHED_READING] == 0);
   1275   1.1  reinoud 	KASSERT(priv->num_queued[UDF_SHED_WRITING] == 0);
   1276   1.1  reinoud 	KASSERT(priv->num_queued[UDF_SHED_SEQWRITING] == 0);
   1277   1.1  reinoud 
   1278  1.17     yamt 	KASSERT(bufq_peek(priv->queues[UDF_SHED_WAITING]) == NULL);
   1279  1.17     yamt 	KASSERT(bufq_peek(priv->queues[UDF_SHED_IDLE]) == NULL);
   1280  1.17     yamt 	KASSERT(bufq_peek(priv->queues[UDF_SHED_READING]) == NULL);
   1281  1.17     yamt 	KASSERT(bufq_peek(priv->queues[UDF_SHED_WRITING]) == NULL);
   1282  1.17     yamt 	KASSERT(bufq_peek(priv->queues[UDF_SHED_SEQWRITING]) == NULL);
   1283   1.1  reinoud 	eccline = udf_pop_eccline(priv, UDF_SHED_FREE);
   1284   1.1  reinoud 	while (eccline) {
   1285   1.1  reinoud 		udf_dispose_eccline(eccline);
   1286   1.1  reinoud 		eccline = udf_pop_eccline(priv, UDF_SHED_FREE);
   1287   1.1  reinoud 	}
   1288   1.1  reinoud 	KASSERT(priv->num_queued[UDF_SHED_FREE] == 0);
   1289   1.1  reinoud 	mutex_exit(&priv->discstrat_mutex);
   1290   1.1  reinoud 
   1291  1.13  reinoud 	priv->thread_running  = 0;
   1292   1.1  reinoud 	priv->thread_finished = 1;
   1293   1.1  reinoud 	wakeup(&priv->run_thread);
   1294   1.1  reinoud 	kthread_exit(0);
   1295   1.1  reinoud 	/* not reached */
   1296   1.1  reinoud }
   1297   1.1  reinoud 
   1298   1.1  reinoud /* --------------------------------------------------------------------- */
   1299   1.1  reinoud 
   1300   1.1  reinoud /*
   1301   1.1  reinoud  * Buffer memory pool allocator.
   1302   1.1  reinoud  */
   1303   1.1  reinoud 
   1304   1.1  reinoud static void *
   1305   1.1  reinoud ecclinepool_page_alloc(struct pool *pp, int flags)
   1306   1.1  reinoud {
   1307   1.1  reinoud         return (void *)uvm_km_alloc(kernel_map,
   1308   1.1  reinoud             MAXBSIZE, MAXBSIZE,
   1309   1.1  reinoud             ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
   1310   1.1  reinoud 	    	| UVM_KMF_WIRED /* UVM_KMF_PAGABLE? */);
   1311   1.1  reinoud }
   1312   1.1  reinoud 
   1313   1.1  reinoud static void
   1314   1.1  reinoud ecclinepool_page_free(struct pool *pp, void *v)
   1315   1.1  reinoud {
   1316   1.1  reinoud         uvm_km_free(kernel_map, (vaddr_t)v, MAXBSIZE, UVM_KMF_WIRED);
   1317   1.1  reinoud }
   1318   1.1  reinoud 
   1319   1.1  reinoud static struct pool_allocator ecclinepool_allocator = {
   1320   1.1  reinoud         .pa_alloc = ecclinepool_page_alloc,
   1321   1.1  reinoud         .pa_free  = ecclinepool_page_free,
   1322   1.1  reinoud         .pa_pagesz = MAXBSIZE,
   1323   1.1  reinoud };
   1324   1.1  reinoud 
   1325   1.1  reinoud 
   1326   1.1  reinoud static void
   1327   1.1  reinoud udf_discstrat_init_rmw(struct udf_strat_args *args)
   1328   1.1  reinoud {
   1329   1.1  reinoud 	struct udf_mount *ump = args->ump;
   1330   1.1  reinoud 	struct strat_private *priv = PRIV(ump);
   1331   1.1  reinoud 	uint32_t lb_size, blobsize, hashline;
   1332   1.1  reinoud 	int i;
   1333   1.1  reinoud 
   1334   1.1  reinoud 	KASSERT(ump);
   1335   1.1  reinoud 	KASSERT(ump->logical_vol);
   1336   1.1  reinoud 	KASSERT(priv == NULL);
   1337   1.1  reinoud 
   1338   1.1  reinoud 	lb_size = udf_rw32(ump->logical_vol->lb_size);
   1339   1.1  reinoud 	blobsize = ump->packet_size * lb_size;
   1340   1.1  reinoud 	KASSERT(lb_size > 0);
   1341   1.1  reinoud 	KASSERT(ump->packet_size <= 64);
   1342   1.1  reinoud 
   1343   1.1  reinoud 	/* initialise our memory space */
   1344   1.1  reinoud 	ump->strategy_private = malloc(sizeof(struct strat_private),
   1345   1.1  reinoud 		M_UDFTEMP, M_WAITOK);
   1346   1.1  reinoud 	priv = ump->strategy_private;
   1347   1.1  reinoud 	memset(priv, 0 , sizeof(struct strat_private));
   1348   1.1  reinoud 
   1349   1.1  reinoud 	/* initialise locks */
   1350   1.1  reinoud 	cv_init(&priv->discstrat_cv, "udfstrat");
   1351  1.15  reinoud 	mutex_init(&priv->discstrat_mutex, MUTEX_DEFAULT, IPL_NONE);
   1352   1.1  reinoud 	mutex_init(&priv->seqwrite_mutex, MUTEX_DEFAULT, IPL_NONE);
   1353   1.1  reinoud 
   1354   1.1  reinoud 	/* initialise struct eccline pool */
   1355   1.1  reinoud 	pool_init(&priv->eccline_pool, sizeof(struct udf_eccline),
   1356   1.1  reinoud 		0, 0, 0, "udf_eccline_pool", NULL, IPL_NONE);
   1357   1.1  reinoud 
   1358   1.1  reinoud 	/* initialise eccline blob pool */
   1359  1.12  reinoud         ecclinepool_allocator.pa_pagesz = blobsize;
   1360   1.1  reinoud 	pool_init(&priv->ecclineblob_pool, blobsize,
   1361  1.12  reinoud 		0, 0, 0, "udf_eccline_blob", &ecclinepool_allocator, IPL_NONE);
   1362   1.1  reinoud 
   1363   1.1  reinoud 	/* initialise main queues */
   1364   1.1  reinoud 	for (i = 0; i < UDF_SHED_MAX; i++) {
   1365   1.1  reinoud 		priv->num_queued[i] = 0;
   1366   1.1  reinoud 		vfs_timestamp(&priv->last_queued[i]);
   1367   1.1  reinoud 	}
   1368  1.11  reinoud 	bufq_alloc(&priv->queues[UDF_SHED_WAITING], "fcfs",
   1369  1.11  reinoud 		BUFQ_SORT_RAWBLOCK);
   1370   1.1  reinoud 	bufq_alloc(&priv->queues[UDF_SHED_READING], "disksort",
   1371   1.1  reinoud 		BUFQ_SORT_RAWBLOCK);
   1372   1.1  reinoud 	bufq_alloc(&priv->queues[UDF_SHED_WRITING], "disksort",
   1373   1.1  reinoud 		BUFQ_SORT_RAWBLOCK);
   1374   1.1  reinoud 	bufq_alloc(&priv->queues[UDF_SHED_SEQWRITING], "disksort", 0);
   1375   1.1  reinoud 
   1376   1.1  reinoud 	/* initialise administrative queues */
   1377   1.1  reinoud 	bufq_alloc(&priv->queues[UDF_SHED_IDLE], "fcfs", 0);
   1378   1.1  reinoud 	bufq_alloc(&priv->queues[UDF_SHED_FREE], "fcfs", 0);
   1379   1.1  reinoud 
   1380   1.1  reinoud 	for (hashline = 0; hashline < UDF_ECCBUF_HASHSIZE; hashline++) {
   1381   1.1  reinoud 		LIST_INIT(&priv->eccline_hash[hashline]);
   1382   1.1  reinoud 	}
   1383   1.1  reinoud 
   1384   1.1  reinoud 	/* create our disk strategy thread */
   1385   1.1  reinoud 	priv->cur_queue = UDF_SHED_READING;
   1386   1.1  reinoud 	priv->thread_finished = 0;
   1387  1.13  reinoud 	priv->thread_running  = 0;
   1388   1.1  reinoud 	priv->run_thread      = 1;
   1389   1.1  reinoud 	if (kthread_create(PRI_NONE, 0 /* KTHREAD_MPSAFE*/, NULL /* cpu_info*/,
   1390   1.1  reinoud 		udf_discstrat_thread, ump, &priv->queue_lwp,
   1391   1.1  reinoud 		"%s", "udf_rw")) {
   1392   1.1  reinoud 		panic("fork udf_rw");
   1393   1.1  reinoud 	}
   1394  1.13  reinoud 
   1395  1.13  reinoud 	/* wait for thread to spin up */
   1396  1.13  reinoud 	while (!priv->thread_running) {
   1397  1.13  reinoud 		tsleep(&priv->thread_running, PRIBIO+1, "udfshedstart", hz);
   1398  1.13  reinoud 	}
   1399   1.1  reinoud }
   1400   1.1  reinoud 
   1401   1.1  reinoud 
   1402   1.1  reinoud static void
   1403   1.1  reinoud udf_discstrat_finish_rmw(struct udf_strat_args *args)
   1404   1.1  reinoud {
   1405   1.1  reinoud 	struct udf_mount *ump = args->ump;
   1406   1.1  reinoud 	struct strat_private *priv = PRIV(ump);
   1407   1.1  reinoud 	int error;
   1408   1.1  reinoud 
   1409   1.1  reinoud 	if (ump == NULL)
   1410   1.1  reinoud 		return;
   1411   1.1  reinoud 
   1412   1.1  reinoud 	/* stop our sheduling thread */
   1413   1.1  reinoud 	KASSERT(priv->run_thread == 1);
   1414   1.1  reinoud 	priv->run_thread = 0;
   1415   1.1  reinoud 	wakeup(priv->queue_lwp);
   1416   1.1  reinoud 	while (!priv->thread_finished) {
   1417   1.1  reinoud 		error = tsleep(&priv->run_thread, PRIBIO+1,
   1418   1.1  reinoud 			"udfshedfin", hz);
   1419   1.1  reinoud 	}
   1420   1.1  reinoud 	/* kthread should be finished now */
   1421   1.1  reinoud 
   1422   1.1  reinoud 	/* cleanup our pools */
   1423   1.1  reinoud 	pool_destroy(&priv->eccline_pool);
   1424   1.1  reinoud 	pool_destroy(&priv->ecclineblob_pool);
   1425   1.1  reinoud 
   1426   1.1  reinoud 	cv_destroy(&priv->discstrat_cv);
   1427   1.1  reinoud 	mutex_destroy(&priv->discstrat_mutex);
   1428   1.1  reinoud 	mutex_destroy(&priv->seqwrite_mutex);
   1429   1.1  reinoud 
   1430   1.1  reinoud 	/* free our private space */
   1431   1.1  reinoud 	free(ump->strategy_private, M_UDFTEMP);
   1432   1.1  reinoud 	ump->strategy_private = NULL;
   1433   1.1  reinoud }
   1434   1.1  reinoud 
   1435   1.1  reinoud /* --------------------------------------------------------------------- */
   1436   1.1  reinoud 
   1437   1.1  reinoud struct udf_strategy udf_strat_rmw =
   1438   1.1  reinoud {
   1439   1.5  reinoud 	udf_create_nodedscr_rmw,
   1440   1.5  reinoud 	udf_free_nodedscr_rmw,
   1441   1.5  reinoud 	udf_read_nodedscr_rmw,
   1442   1.5  reinoud 	udf_write_nodedscr_rmw,
   1443   1.1  reinoud 	udf_queuebuf_rmw,
   1444   1.1  reinoud 	udf_discstrat_init_rmw,
   1445   1.1  reinoud 	udf_discstrat_finish_rmw
   1446   1.1  reinoud };
   1447   1.1  reinoud 
   1448