1*0a6a1f1dSLionel Sambuc /* $NetBSD: udf_strat_rmw.c,v 1.27 2015/10/06 08:57:34 hannken Exp $ */
29f988b79SJean-Baptiste Boric
39f988b79SJean-Baptiste Boric /*
49f988b79SJean-Baptiste Boric * Copyright (c) 2006, 2008 Reinoud Zandijk
59f988b79SJean-Baptiste Boric * All rights reserved.
69f988b79SJean-Baptiste Boric *
79f988b79SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
89f988b79SJean-Baptiste Boric * modification, are permitted provided that the following conditions
99f988b79SJean-Baptiste Boric * are met:
109f988b79SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
119f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
129f988b79SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
139f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in the
149f988b79SJean-Baptiste Boric * documentation and/or other materials provided with the distribution.
159f988b79SJean-Baptiste Boric *
169f988b79SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
179f988b79SJean-Baptiste Boric * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
189f988b79SJean-Baptiste Boric * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199f988b79SJean-Baptiste Boric * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
209f988b79SJean-Baptiste Boric * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
219f988b79SJean-Baptiste Boric * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
229f988b79SJean-Baptiste Boric * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239f988b79SJean-Baptiste Boric * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249f988b79SJean-Baptiste Boric * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
259f988b79SJean-Baptiste Boric * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269f988b79SJean-Baptiste Boric *
279f988b79SJean-Baptiste Boric */
289f988b79SJean-Baptiste Boric
299f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
309f988b79SJean-Baptiste Boric #ifndef lint
31*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: udf_strat_rmw.c,v 1.27 2015/10/06 08:57:34 hannken Exp $");
329f988b79SJean-Baptiste Boric #endif /* not lint */
339f988b79SJean-Baptiste Boric
349f988b79SJean-Baptiste Boric
359f988b79SJean-Baptiste Boric #if defined(_KERNEL_OPT)
369f988b79SJean-Baptiste Boric #include "opt_compat_netbsd.h"
379f988b79SJean-Baptiste Boric #endif
389f988b79SJean-Baptiste Boric
399f988b79SJean-Baptiste Boric #include <sys/param.h>
409f988b79SJean-Baptiste Boric #include <sys/systm.h>
419f988b79SJean-Baptiste Boric #include <sys/sysctl.h>
429f988b79SJean-Baptiste Boric #include <sys/namei.h>
439f988b79SJean-Baptiste Boric #include <sys/proc.h>
449f988b79SJean-Baptiste Boric #include <sys/kernel.h>
459f988b79SJean-Baptiste Boric #include <sys/vnode.h>
469f988b79SJean-Baptiste Boric #include <miscfs/genfs/genfs_node.h>
479f988b79SJean-Baptiste Boric #include <sys/mount.h>
489f988b79SJean-Baptiste Boric #include <sys/buf.h>
499f988b79SJean-Baptiste Boric #include <sys/file.h>
509f988b79SJean-Baptiste Boric #include <sys/device.h>
519f988b79SJean-Baptiste Boric #include <sys/disklabel.h>
529f988b79SJean-Baptiste Boric #include <sys/ioctl.h>
539f988b79SJean-Baptiste Boric #include <sys/malloc.h>
549f988b79SJean-Baptiste Boric #include <sys/dirent.h>
559f988b79SJean-Baptiste Boric #include <sys/stat.h>
569f988b79SJean-Baptiste Boric #include <sys/conf.h>
579f988b79SJean-Baptiste Boric #include <sys/kauth.h>
589f988b79SJean-Baptiste Boric #include <sys/kthread.h>
599f988b79SJean-Baptiste Boric #include <dev/clock_subr.h>
609f988b79SJean-Baptiste Boric
619f988b79SJean-Baptiste Boric #include <fs/udf/ecma167-udf.h>
629f988b79SJean-Baptiste Boric #include <fs/udf/udf_mount.h>
639f988b79SJean-Baptiste Boric
649f988b79SJean-Baptiste Boric #include "udf.h"
659f988b79SJean-Baptiste Boric #include "udf_subr.h"
669f988b79SJean-Baptiste Boric #include "udf_bswap.h"
679f988b79SJean-Baptiste Boric
689f988b79SJean-Baptiste Boric
699f988b79SJean-Baptiste Boric #define VTOI(vnode) ((struct udf_node *) (vnode)->v_data)
709f988b79SJean-Baptiste Boric #define PRIV(ump) ((struct strat_private *) (ump)->strategy_private)
719f988b79SJean-Baptiste Boric #define BTOE(buf) ((struct udf_eccline *) ((buf)->b_private))
729f988b79SJean-Baptiste Boric
739f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
749f988b79SJean-Baptiste Boric
759f988b79SJean-Baptiste Boric #define UDF_MAX_PACKET_SIZE 64 /* DONT change this */
769f988b79SJean-Baptiste Boric
779f988b79SJean-Baptiste Boric /* sheduler states */
789f988b79SJean-Baptiste Boric #define UDF_SHED_WAITING 1 /* waiting on timeout */
799f988b79SJean-Baptiste Boric #define UDF_SHED_READING 2
809f988b79SJean-Baptiste Boric #define UDF_SHED_WRITING 3
819f988b79SJean-Baptiste Boric #define UDF_SHED_SEQWRITING 4
829f988b79SJean-Baptiste Boric #define UDF_SHED_IDLE 5 /* refcnt'd */
839f988b79SJean-Baptiste Boric #define UDF_SHED_FREE 6 /* recycleable */
849f988b79SJean-Baptiste Boric #define UDF_SHED_MAX 6+1
859f988b79SJean-Baptiste Boric
869f988b79SJean-Baptiste Boric /* flags */
879f988b79SJean-Baptiste Boric #define ECC_LOCKED 0x01 /* prevent access */
889f988b79SJean-Baptiste Boric #define ECC_WANTED 0x02 /* trying access */
899f988b79SJean-Baptiste Boric #define ECC_SEQWRITING 0x04 /* sequential queue */
909f988b79SJean-Baptiste Boric #define ECC_FLOATING 0x08 /* not queued yet */
919f988b79SJean-Baptiste Boric
929f988b79SJean-Baptiste Boric #define ECC_WAITTIME 10
939f988b79SJean-Baptiste Boric
949f988b79SJean-Baptiste Boric
959f988b79SJean-Baptiste Boric TAILQ_HEAD(ecclineq, udf_eccline);
969f988b79SJean-Baptiste Boric struct udf_eccline {
979f988b79SJean-Baptiste Boric struct udf_mount *ump;
989f988b79SJean-Baptiste Boric uint64_t present; /* preserve these */
999f988b79SJean-Baptiste Boric uint64_t readin; /* bitmap */
1009f988b79SJean-Baptiste Boric uint64_t dirty; /* bitmap */
1019f988b79SJean-Baptiste Boric uint64_t error; /* bitmap */
1029f988b79SJean-Baptiste Boric uint32_t refcnt;
1039f988b79SJean-Baptiste Boric
1049f988b79SJean-Baptiste Boric struct timespec wait_time;
1059f988b79SJean-Baptiste Boric uint32_t flags;
1069f988b79SJean-Baptiste Boric uint32_t start_sector; /* physical */
1079f988b79SJean-Baptiste Boric
1089f988b79SJean-Baptiste Boric const char *fname;
1099f988b79SJean-Baptiste Boric int sline;
1109f988b79SJean-Baptiste Boric
1119f988b79SJean-Baptiste Boric struct buf *buf;
1129f988b79SJean-Baptiste Boric void *blob;
1139f988b79SJean-Baptiste Boric
1149f988b79SJean-Baptiste Boric struct buf *bufs[UDF_MAX_PACKET_SIZE];
1159f988b79SJean-Baptiste Boric uint32_t bufs_bpos[UDF_MAX_PACKET_SIZE];
1169f988b79SJean-Baptiste Boric int bufs_len[UDF_MAX_PACKET_SIZE];
1179f988b79SJean-Baptiste Boric
1189f988b79SJean-Baptiste Boric int queued_on; /* on which BUFQ list */
1199f988b79SJean-Baptiste Boric LIST_ENTRY(udf_eccline) hashchain; /* on sector lookup */
1209f988b79SJean-Baptiste Boric };
1219f988b79SJean-Baptiste Boric
1229f988b79SJean-Baptiste Boric
1239f988b79SJean-Baptiste Boric struct strat_private {
1249f988b79SJean-Baptiste Boric lwp_t *queue_lwp;
1259f988b79SJean-Baptiste Boric kcondvar_t discstrat_cv; /* to wait on */
1269f988b79SJean-Baptiste Boric kmutex_t discstrat_mutex; /* disc strategy */
1279f988b79SJean-Baptiste Boric kmutex_t seqwrite_mutex; /* protect mappings */
1289f988b79SJean-Baptiste Boric
1299f988b79SJean-Baptiste Boric int thread_running; /* thread control */
1309f988b79SJean-Baptiste Boric int run_thread; /* thread control */
1319f988b79SJean-Baptiste Boric int thread_finished; /* thread control */
1329f988b79SJean-Baptiste Boric int cur_queue;
1339f988b79SJean-Baptiste Boric
1349f988b79SJean-Baptiste Boric int num_floating;
1359f988b79SJean-Baptiste Boric int num_queued[UDF_SHED_MAX];
1369f988b79SJean-Baptiste Boric struct bufq_state *queues[UDF_SHED_MAX];
1379f988b79SJean-Baptiste Boric struct timespec last_queued[UDF_SHED_MAX];
1389f988b79SJean-Baptiste Boric struct disk_strategy old_strategy_setting;
1399f988b79SJean-Baptiste Boric
1409f988b79SJean-Baptiste Boric struct pool eccline_pool;
1419f988b79SJean-Baptiste Boric struct pool ecclineblob_pool;
1429f988b79SJean-Baptiste Boric LIST_HEAD(, udf_eccline) eccline_hash[UDF_ECCBUF_HASHSIZE];
1439f988b79SJean-Baptiste Boric };
1449f988b79SJean-Baptiste Boric
1459f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
1469f988b79SJean-Baptiste Boric
1479f988b79SJean-Baptiste Boric #define UDF_LOCK_ECCLINE(eccline) udf_lock_eccline(eccline, __FILE__, __LINE__)
1489f988b79SJean-Baptiste Boric #define UDF_UNLOCK_ECCLINE(eccline) udf_unlock_eccline(eccline, __FILE__, __LINE__)
1499f988b79SJean-Baptiste Boric
1509f988b79SJean-Baptiste Boric /* can be called with or without discstrat lock */
1519f988b79SJean-Baptiste Boric static void
udf_lock_eccline(struct udf_eccline * eccline,const char * fname,int sline)1529f988b79SJean-Baptiste Boric udf_lock_eccline(struct udf_eccline *eccline, const char *fname, int sline)
1539f988b79SJean-Baptiste Boric {
1549f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(eccline->ump);
1559f988b79SJean-Baptiste Boric int waslocked, ret;
1569f988b79SJean-Baptiste Boric
1579f988b79SJean-Baptiste Boric KASSERT(mutex_owned(&priv->discstrat_mutex));
1589f988b79SJean-Baptiste Boric
1599f988b79SJean-Baptiste Boric waslocked = mutex_owned(&priv->discstrat_mutex);
1609f988b79SJean-Baptiste Boric if (!waslocked)
1619f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
1629f988b79SJean-Baptiste Boric
1639f988b79SJean-Baptiste Boric /* wait until its unlocked first */
1649f988b79SJean-Baptiste Boric eccline->refcnt++;
1659f988b79SJean-Baptiste Boric while (eccline->flags & ECC_LOCKED) {
1669f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("waiting for lock at %s:%d\n",
1679f988b79SJean-Baptiste Boric fname, sline));
1689f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("was locked at %s:%d\n",
1699f988b79SJean-Baptiste Boric eccline->fname, eccline->sline));
1709f988b79SJean-Baptiste Boric eccline->flags |= ECC_WANTED;
1719f988b79SJean-Baptiste Boric ret = cv_timedwait(&priv->discstrat_cv, &priv->discstrat_mutex,
1729f988b79SJean-Baptiste Boric hz/8);
1739f988b79SJean-Baptiste Boric if (ret == EWOULDBLOCK)
1749f988b79SJean-Baptiste Boric DPRINTF(LOCKING, ("eccline lock helt, waiting for "
1759f988b79SJean-Baptiste Boric "release"));
1769f988b79SJean-Baptiste Boric }
1779f988b79SJean-Baptiste Boric eccline->flags |= ECC_LOCKED;
1789f988b79SJean-Baptiste Boric eccline->flags &= ~ECC_WANTED;
1799f988b79SJean-Baptiste Boric eccline->refcnt--;
1809f988b79SJean-Baptiste Boric
1819f988b79SJean-Baptiste Boric eccline->fname = fname;
1829f988b79SJean-Baptiste Boric eccline->sline = sline;
1839f988b79SJean-Baptiste Boric
1849f988b79SJean-Baptiste Boric if (!waslocked)
1859f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
1869f988b79SJean-Baptiste Boric }
1879f988b79SJean-Baptiste Boric
1889f988b79SJean-Baptiste Boric
1899f988b79SJean-Baptiste Boric /* can be called with or without discstrat lock */
1909f988b79SJean-Baptiste Boric static void
udf_unlock_eccline(struct udf_eccline * eccline,const char * fname,int sline)1919f988b79SJean-Baptiste Boric udf_unlock_eccline(struct udf_eccline *eccline, const char *fname, int sline)
1929f988b79SJean-Baptiste Boric {
1939f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(eccline->ump);
1949f988b79SJean-Baptiste Boric int waslocked;
1959f988b79SJean-Baptiste Boric
1969f988b79SJean-Baptiste Boric KASSERT(mutex_owned(&priv->discstrat_mutex));
1979f988b79SJean-Baptiste Boric
1989f988b79SJean-Baptiste Boric waslocked = mutex_owned(&priv->discstrat_mutex);
1999f988b79SJean-Baptiste Boric if (!waslocked)
2009f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
2019f988b79SJean-Baptiste Boric
2029f988b79SJean-Baptiste Boric eccline->flags &= ~ECC_LOCKED;
2039f988b79SJean-Baptiste Boric cv_broadcast(&priv->discstrat_cv);
2049f988b79SJean-Baptiste Boric
2059f988b79SJean-Baptiste Boric if (!waslocked)
2069f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
2079f988b79SJean-Baptiste Boric }
2089f988b79SJean-Baptiste Boric
2099f988b79SJean-Baptiste Boric
2109f988b79SJean-Baptiste Boric /* NOTE discstrat_mutex should be held! */
2119f988b79SJean-Baptiste Boric static void
udf_dispose_eccline(struct udf_eccline * eccline)2129f988b79SJean-Baptiste Boric udf_dispose_eccline(struct udf_eccline *eccline)
2139f988b79SJean-Baptiste Boric {
2149f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(eccline->ump);
2159f988b79SJean-Baptiste Boric
2169f988b79SJean-Baptiste Boric KASSERT(mutex_owned(&priv->discstrat_mutex));
2179f988b79SJean-Baptiste Boric
2189f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("dispose eccline with start sector %d, "
2199f988b79SJean-Baptiste Boric "present %0"PRIx64"\n", eccline->start_sector,
2209f988b79SJean-Baptiste Boric eccline->present));
2219f988b79SJean-Baptiste Boric
2229f988b79SJean-Baptiste Boric KASSERT(eccline->refcnt == 0);
2239f988b79SJean-Baptiste Boric KASSERT(eccline->dirty == 0);
2249f988b79SJean-Baptiste Boric KASSERT(eccline->queued_on == 0);
2259f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_FLOATING);
2269f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_LOCKED);
2279f988b79SJean-Baptiste Boric
2289f988b79SJean-Baptiste Boric LIST_REMOVE(eccline, hashchain);
2299f988b79SJean-Baptiste Boric priv->num_floating--;
2309f988b79SJean-Baptiste Boric
2319f988b79SJean-Baptiste Boric putiobuf(eccline->buf);
2329f988b79SJean-Baptiste Boric pool_put(&priv->ecclineblob_pool, eccline->blob);
2339f988b79SJean-Baptiste Boric pool_put(&priv->eccline_pool, eccline);
2349f988b79SJean-Baptiste Boric }
2359f988b79SJean-Baptiste Boric
2369f988b79SJean-Baptiste Boric
2379f988b79SJean-Baptiste Boric /* NOTE discstrat_mutex should be held! */
2389f988b79SJean-Baptiste Boric static void
udf_push_eccline(struct udf_eccline * eccline,int newqueue)2399f988b79SJean-Baptiste Boric udf_push_eccline(struct udf_eccline *eccline, int newqueue)
2409f988b79SJean-Baptiste Boric {
2419f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(eccline->ump);
2429f988b79SJean-Baptiste Boric
2439f988b79SJean-Baptiste Boric KASSERT(mutex_owned(&priv->discstrat_mutex));
2449f988b79SJean-Baptiste Boric
2459f988b79SJean-Baptiste Boric DPRINTF(PARANOIA, ("DEBUG: buf %p pushed on queue %d\n", eccline->buf, newqueue));
2469f988b79SJean-Baptiste Boric
2479f988b79SJean-Baptiste Boric KASSERT(eccline->queued_on == 0);
2489f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_FLOATING);
2499f988b79SJean-Baptiste Boric
2509f988b79SJean-Baptiste Boric /* set buffer block numbers to make sure its queued correctly */
2519f988b79SJean-Baptiste Boric eccline->buf->b_lblkno = eccline->start_sector;
2529f988b79SJean-Baptiste Boric eccline->buf->b_blkno = eccline->start_sector;
2539f988b79SJean-Baptiste Boric eccline->buf->b_rawblkno = eccline->start_sector;
2549f988b79SJean-Baptiste Boric
2559f988b79SJean-Baptiste Boric vfs_timestamp(&priv->last_queued[newqueue]);
2569f988b79SJean-Baptiste Boric eccline->flags &= ~ECC_FLOATING;
2579f988b79SJean-Baptiste Boric priv->num_floating--;
2589f988b79SJean-Baptiste Boric eccline->queued_on = newqueue;
2599f988b79SJean-Baptiste Boric priv->num_queued[newqueue]++;
2609f988b79SJean-Baptiste Boric bufq_put(priv->queues[newqueue], eccline->buf);
2619f988b79SJean-Baptiste Boric
2629f988b79SJean-Baptiste Boric UDF_UNLOCK_ECCLINE(eccline);
2639f988b79SJean-Baptiste Boric
2649f988b79SJean-Baptiste Boric /* XXX tickle disc strategy statemachine */
2659f988b79SJean-Baptiste Boric if (newqueue != UDF_SHED_IDLE)
2669f988b79SJean-Baptiste Boric cv_signal(&priv->discstrat_cv);
2679f988b79SJean-Baptiste Boric }
2689f988b79SJean-Baptiste Boric
2699f988b79SJean-Baptiste Boric
2709f988b79SJean-Baptiste Boric static struct udf_eccline *
udf_peek_eccline(struct strat_private * priv,int queued_on)2719f988b79SJean-Baptiste Boric udf_peek_eccline(struct strat_private *priv, int queued_on)
2729f988b79SJean-Baptiste Boric {
2739f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
2749f988b79SJean-Baptiste Boric struct buf *buf;
2759f988b79SJean-Baptiste Boric
2769f988b79SJean-Baptiste Boric KASSERT(mutex_owned(&priv->discstrat_mutex));
2779f988b79SJean-Baptiste Boric
2789f988b79SJean-Baptiste Boric for(;;) {
2799f988b79SJean-Baptiste Boric buf = bufq_peek(priv->queues[queued_on]);
2809f988b79SJean-Baptiste Boric /* could have been a race, but we'll revisit later */
2819f988b79SJean-Baptiste Boric if (buf == NULL)
2829f988b79SJean-Baptiste Boric return NULL;
2839f988b79SJean-Baptiste Boric
2849f988b79SJean-Baptiste Boric eccline = BTOE(buf);
2859f988b79SJean-Baptiste Boric UDF_LOCK_ECCLINE(eccline);
2869f988b79SJean-Baptiste Boric
2879f988b79SJean-Baptiste Boric /* might have changed before we obtained the lock */
2889f988b79SJean-Baptiste Boric if (eccline->queued_on == queued_on)
2899f988b79SJean-Baptiste Boric break;
2909f988b79SJean-Baptiste Boric
2919f988b79SJean-Baptiste Boric UDF_UNLOCK_ECCLINE(eccline);
2929f988b79SJean-Baptiste Boric }
2939f988b79SJean-Baptiste Boric
2949f988b79SJean-Baptiste Boric KASSERT(eccline->queued_on == queued_on);
2959f988b79SJean-Baptiste Boric KASSERT((eccline->flags & ECC_FLOATING) == 0);
2969f988b79SJean-Baptiste Boric
2979f988b79SJean-Baptiste Boric DPRINTF(PARANOIA, ("DEBUG: buf %p peeked at queue %d\n",
2989f988b79SJean-Baptiste Boric eccline->buf, queued_on));
2999f988b79SJean-Baptiste Boric
3009f988b79SJean-Baptiste Boric return eccline;
3019f988b79SJean-Baptiste Boric }
3029f988b79SJean-Baptiste Boric
3039f988b79SJean-Baptiste Boric
3049f988b79SJean-Baptiste Boric static struct udf_eccline *
udf_pop_eccline(struct strat_private * priv,int queued_on)3059f988b79SJean-Baptiste Boric udf_pop_eccline(struct strat_private *priv, int queued_on)
3069f988b79SJean-Baptiste Boric {
3079f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
3089f988b79SJean-Baptiste Boric struct buf *buf;
3099f988b79SJean-Baptiste Boric
3109f988b79SJean-Baptiste Boric KASSERT(mutex_owned(&priv->discstrat_mutex));
3119f988b79SJean-Baptiste Boric
3129f988b79SJean-Baptiste Boric for(;;) {
3139f988b79SJean-Baptiste Boric buf = bufq_get(priv->queues[queued_on]);
3149f988b79SJean-Baptiste Boric if (buf == NULL) {
3159f988b79SJean-Baptiste Boric // KASSERT(priv->num_queued[queued_on] == 0);
3169f988b79SJean-Baptiste Boric return NULL;
3179f988b79SJean-Baptiste Boric }
3189f988b79SJean-Baptiste Boric
3199f988b79SJean-Baptiste Boric eccline = BTOE(buf);
3209f988b79SJean-Baptiste Boric UDF_LOCK_ECCLINE(eccline);
3219f988b79SJean-Baptiste Boric
3229f988b79SJean-Baptiste Boric /* might have changed before we obtained the lock */
3239f988b79SJean-Baptiste Boric if (eccline->queued_on == queued_on)
3249f988b79SJean-Baptiste Boric break;
3259f988b79SJean-Baptiste Boric
3269f988b79SJean-Baptiste Boric UDF_UNLOCK_ECCLINE(eccline);
3279f988b79SJean-Baptiste Boric }
3289f988b79SJean-Baptiste Boric
3299f988b79SJean-Baptiste Boric KASSERT(eccline->queued_on == queued_on);
3309f988b79SJean-Baptiste Boric KASSERT((eccline->flags & ECC_FLOATING) == 0);
3319f988b79SJean-Baptiste Boric
3329f988b79SJean-Baptiste Boric priv->num_queued[queued_on]--;
3339f988b79SJean-Baptiste Boric eccline->queued_on = 0;
3349f988b79SJean-Baptiste Boric
3359f988b79SJean-Baptiste Boric eccline->flags |= ECC_FLOATING;
3369f988b79SJean-Baptiste Boric priv->num_floating++;
3379f988b79SJean-Baptiste Boric
3389f988b79SJean-Baptiste Boric DPRINTF(PARANOIA, ("DEBUG: buf %p popped from queue %d\n",
3399f988b79SJean-Baptiste Boric eccline->buf, queued_on));
3409f988b79SJean-Baptiste Boric
3419f988b79SJean-Baptiste Boric return eccline;
3429f988b79SJean-Baptiste Boric }
3439f988b79SJean-Baptiste Boric
3449f988b79SJean-Baptiste Boric
3459f988b79SJean-Baptiste Boric static void
udf_unqueue_eccline(struct strat_private * priv,struct udf_eccline * eccline)3469f988b79SJean-Baptiste Boric udf_unqueue_eccline(struct strat_private *priv, struct udf_eccline *eccline)
3479f988b79SJean-Baptiste Boric {
3489f988b79SJean-Baptiste Boric struct buf *ret __diagused;
3499f988b79SJean-Baptiste Boric
3509f988b79SJean-Baptiste Boric UDF_LOCK_ECCLINE(eccline);
3519f988b79SJean-Baptiste Boric if (eccline->queued_on == 0) {
3529f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_FLOATING);
3539f988b79SJean-Baptiste Boric return;
3549f988b79SJean-Baptiste Boric }
3559f988b79SJean-Baptiste Boric
3569f988b79SJean-Baptiste Boric ret = bufq_cancel(priv->queues[eccline->queued_on], eccline->buf);
3579f988b79SJean-Baptiste Boric KASSERT(ret == eccline->buf);
3589f988b79SJean-Baptiste Boric
3599f988b79SJean-Baptiste Boric priv->num_queued[eccline->queued_on]--;
3609f988b79SJean-Baptiste Boric eccline->queued_on = 0;
3619f988b79SJean-Baptiste Boric
3629f988b79SJean-Baptiste Boric eccline->flags |= ECC_FLOATING;
3639f988b79SJean-Baptiste Boric priv->num_floating++;
3649f988b79SJean-Baptiste Boric }
3659f988b79SJean-Baptiste Boric
3669f988b79SJean-Baptiste Boric
3679f988b79SJean-Baptiste Boric static struct udf_eccline *
udf_geteccline(struct udf_mount * ump,uint32_t sector,int flags)3689f988b79SJean-Baptiste Boric udf_geteccline(struct udf_mount *ump, uint32_t sector, int flags)
3699f988b79SJean-Baptiste Boric {
3709f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(ump);
3719f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
3729f988b79SJean-Baptiste Boric uint32_t start_sector, lb_size, blobsize;
3739f988b79SJean-Baptiste Boric uint8_t *eccline_blob;
3749f988b79SJean-Baptiste Boric int line, line_offset;
3759f988b79SJean-Baptiste Boric int num_busy;
3769f988b79SJean-Baptiste Boric
3779f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
3789f988b79SJean-Baptiste Boric
3799f988b79SJean-Baptiste Boric /* lookup in our line cache hashtable */
3809f988b79SJean-Baptiste Boric line_offset = sector % ump->packet_size;
3819f988b79SJean-Baptiste Boric start_sector = sector - line_offset;
3829f988b79SJean-Baptiste Boric line = (start_sector/ump->packet_size) & UDF_ECCBUF_HASHMASK;
3839f988b79SJean-Baptiste Boric
3849f988b79SJean-Baptiste Boric KASSERT(priv->thread_running);
3859f988b79SJean-Baptiste Boric
3869f988b79SJean-Baptiste Boric retry:
3879f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("get line sector %d, line %d\n", sector, line));
3889f988b79SJean-Baptiste Boric LIST_FOREACH(eccline, &priv->eccline_hash[line], hashchain) {
3899f988b79SJean-Baptiste Boric if (eccline->start_sector == start_sector) {
3909f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("\tfound eccline, start_sector %d\n",
3919f988b79SJean-Baptiste Boric eccline->start_sector));
3929f988b79SJean-Baptiste Boric udf_unqueue_eccline(priv, eccline);
3939f988b79SJean-Baptiste Boric
3949f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
3959f988b79SJean-Baptiste Boric return eccline;
3969f988b79SJean-Baptiste Boric }
3979f988b79SJean-Baptiste Boric }
3989f988b79SJean-Baptiste Boric
3999f988b79SJean-Baptiste Boric /* not found in eccline cache */
4009f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("\tnot found in eccline cache\n"));
4019f988b79SJean-Baptiste Boric
4029f988b79SJean-Baptiste Boric lb_size = udf_rw32(ump->logical_vol->lb_size);
4039f988b79SJean-Baptiste Boric blobsize = ump->packet_size * lb_size;
4049f988b79SJean-Baptiste Boric
4059f988b79SJean-Baptiste Boric /* dont allow too many pending requests */
4069f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("\tallocating new eccline\n"));
4079f988b79SJean-Baptiste Boric num_busy = (priv->num_queued[UDF_SHED_SEQWRITING] + priv->num_floating);
4089f988b79SJean-Baptiste Boric if ((flags & ECC_SEQWRITING) && (num_busy > UDF_ECCLINE_MAXBUSY)) {
4099f988b79SJean-Baptiste Boric cv_timedwait(&priv->discstrat_cv,
4109f988b79SJean-Baptiste Boric &priv->discstrat_mutex, hz/8);
4119f988b79SJean-Baptiste Boric goto retry;
4129f988b79SJean-Baptiste Boric }
4139f988b79SJean-Baptiste Boric
4149f988b79SJean-Baptiste Boric eccline_blob = pool_get(&priv->ecclineblob_pool, PR_NOWAIT);
4159f988b79SJean-Baptiste Boric eccline = pool_get(&priv->eccline_pool, PR_NOWAIT);
4169f988b79SJean-Baptiste Boric if ((eccline_blob == NULL) || (eccline == NULL)) {
4179f988b79SJean-Baptiste Boric if (eccline_blob)
4189f988b79SJean-Baptiste Boric pool_put(&priv->ecclineblob_pool, eccline_blob);
4199f988b79SJean-Baptiste Boric if (eccline)
4209f988b79SJean-Baptiste Boric pool_put(&priv->eccline_pool, eccline);
4219f988b79SJean-Baptiste Boric
4229f988b79SJean-Baptiste Boric /* out of memory for now; canibalise freelist */
4239f988b79SJean-Baptiste Boric eccline = udf_pop_eccline(priv, UDF_SHED_FREE);
4249f988b79SJean-Baptiste Boric if (eccline == NULL) {
4259f988b79SJean-Baptiste Boric /* serious trouble; wait and retry */
4269f988b79SJean-Baptiste Boric cv_timedwait(&priv->discstrat_cv,
4279f988b79SJean-Baptiste Boric &priv->discstrat_mutex, hz/8);
4289f988b79SJean-Baptiste Boric goto retry;
4299f988b79SJean-Baptiste Boric }
4309f988b79SJean-Baptiste Boric
4319f988b79SJean-Baptiste Boric /* push back line if we're waiting for it or its locked */
4329f988b79SJean-Baptiste Boric if (eccline->flags & ECC_WANTED) {
4339f988b79SJean-Baptiste Boric /* we won a race, but someone else needed it */
4349f988b79SJean-Baptiste Boric udf_push_eccline(eccline, UDF_SHED_FREE);
4359f988b79SJean-Baptiste Boric goto retry;
4369f988b79SJean-Baptiste Boric }
4379f988b79SJean-Baptiste Boric
4389f988b79SJean-Baptiste Boric /* unlink this entry */
4399f988b79SJean-Baptiste Boric LIST_REMOVE(eccline, hashchain);
4409f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_FLOATING);
4419f988b79SJean-Baptiste Boric KASSERT(eccline->queued_on == 0);
4429f988b79SJean-Baptiste Boric
4439f988b79SJean-Baptiste Boric eccline_blob = eccline->blob;
4449f988b79SJean-Baptiste Boric eccline->flags = ECC_FLOATING | ECC_LOCKED;
4459f988b79SJean-Baptiste Boric } else {
4469f988b79SJean-Baptiste Boric eccline->flags = ECC_FLOATING | ECC_LOCKED;
4479f988b79SJean-Baptiste Boric priv->num_floating++;
4489f988b79SJean-Baptiste Boric }
4499f988b79SJean-Baptiste Boric
4509f988b79SJean-Baptiste Boric eccline->queued_on = 0;
4519f988b79SJean-Baptiste Boric eccline->blob = eccline_blob;
4529f988b79SJean-Baptiste Boric eccline->buf = getiobuf(NULL, true);
4539f988b79SJean-Baptiste Boric eccline->buf->b_private = eccline; /* IMPORTANT */
4549f988b79SJean-Baptiste Boric
4559f988b79SJean-Baptiste Boric /* initialise eccline blob */
4569f988b79SJean-Baptiste Boric /* XXX memset expensive and strictly not needed XXX */
4579f988b79SJean-Baptiste Boric memset(eccline->blob, 0, blobsize);
4589f988b79SJean-Baptiste Boric
4599f988b79SJean-Baptiste Boric eccline->ump = ump;
4609f988b79SJean-Baptiste Boric eccline->present = eccline->readin = eccline->dirty = 0;
4619f988b79SJean-Baptiste Boric eccline->error = 0;
4629f988b79SJean-Baptiste Boric eccline->refcnt = 0;
4639f988b79SJean-Baptiste Boric memset(eccline->bufs, 0, UDF_MAX_PACKET_SIZE * sizeof(struct buf *));
4649f988b79SJean-Baptiste Boric
4659f988b79SJean-Baptiste Boric eccline->start_sector = start_sector;
4669f988b79SJean-Baptiste Boric eccline->buf->b_lblkno = start_sector;
4679f988b79SJean-Baptiste Boric eccline->buf->b_blkno = start_sector;
4689f988b79SJean-Baptiste Boric eccline->buf->b_rawblkno = start_sector;
4699f988b79SJean-Baptiste Boric
4709f988b79SJean-Baptiste Boric LIST_INSERT_HEAD(&priv->eccline_hash[line], eccline, hashchain);
4719f988b79SJean-Baptiste Boric
4729f988b79SJean-Baptiste Boric /*
4739f988b79SJean-Baptiste Boric * TODO possible optimalisation for checking overlap with partitions
4749f988b79SJean-Baptiste Boric * to get a clue on future eccline usage
4759f988b79SJean-Baptiste Boric */
4769f988b79SJean-Baptiste Boric
4779f988b79SJean-Baptiste Boric KASSERT(eccline->refcnt == 0);
4789f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_FLOATING);
4799f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_LOCKED);
4809f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
4819f988b79SJean-Baptiste Boric
4829f988b79SJean-Baptiste Boric return eccline;
4839f988b79SJean-Baptiste Boric }
4849f988b79SJean-Baptiste Boric
4859f988b79SJean-Baptiste Boric
4869f988b79SJean-Baptiste Boric static void
udf_puteccline(struct udf_eccline * eccline)4879f988b79SJean-Baptiste Boric udf_puteccline(struct udf_eccline *eccline)
4889f988b79SJean-Baptiste Boric {
4899f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(eccline->ump);
4909f988b79SJean-Baptiste Boric struct udf_mount *ump = eccline->ump;
4919f988b79SJean-Baptiste Boric uint64_t allbits = ((uint64_t) 1 << ump->packet_size)-1;
4929f988b79SJean-Baptiste Boric int new_queue;
4939f988b79SJean-Baptiste Boric
4949f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
4959f988b79SJean-Baptiste Boric
4969f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("put eccline start sector %d, refcnt %d\n",
4979f988b79SJean-Baptiste Boric eccline->start_sector, eccline->refcnt));
4989f988b79SJean-Baptiste Boric
4999f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_LOCKED);
5009f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_FLOATING);
5019f988b79SJean-Baptiste Boric
5029f988b79SJean-Baptiste Boric /* clear all read bits that are already read in */
5039f988b79SJean-Baptiste Boric if (eccline->readin & eccline->present)
5049f988b79SJean-Baptiste Boric eccline->readin &= (~eccline->present) & allbits;
5059f988b79SJean-Baptiste Boric
5069f988b79SJean-Baptiste Boric /* if we have active nodes we dont set it on seqwriting */
5079f988b79SJean-Baptiste Boric if (eccline->refcnt > 1)
5089f988b79SJean-Baptiste Boric eccline->flags &= ~ECC_SEQWRITING;
5099f988b79SJean-Baptiste Boric
5109f988b79SJean-Baptiste Boric /* select state */
5119f988b79SJean-Baptiste Boric new_queue = UDF_SHED_FREE;
5129f988b79SJean-Baptiste Boric if (eccline->refcnt > 0)
5139f988b79SJean-Baptiste Boric new_queue = UDF_SHED_IDLE;
5149f988b79SJean-Baptiste Boric if (eccline->flags & ECC_WANTED)
5159f988b79SJean-Baptiste Boric new_queue = UDF_SHED_IDLE;
5169f988b79SJean-Baptiste Boric if (eccline->readin)
5179f988b79SJean-Baptiste Boric new_queue = UDF_SHED_READING;
5189f988b79SJean-Baptiste Boric if (eccline->dirty) {
5199f988b79SJean-Baptiste Boric new_queue = UDF_SHED_WAITING;
5209f988b79SJean-Baptiste Boric vfs_timestamp(&eccline->wait_time);
5219f988b79SJean-Baptiste Boric eccline->wait_time.tv_sec += ECC_WAITTIME;
5229f988b79SJean-Baptiste Boric
5239f988b79SJean-Baptiste Boric if (eccline->present == allbits) {
5249f988b79SJean-Baptiste Boric new_queue = UDF_SHED_WRITING;
5259f988b79SJean-Baptiste Boric if (eccline->flags & ECC_SEQWRITING)
5269f988b79SJean-Baptiste Boric new_queue = UDF_SHED_SEQWRITING;
5279f988b79SJean-Baptiste Boric }
5289f988b79SJean-Baptiste Boric }
5299f988b79SJean-Baptiste Boric udf_push_eccline(eccline, new_queue);
5309f988b79SJean-Baptiste Boric
5319f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
5329f988b79SJean-Baptiste Boric }
5339f988b79SJean-Baptiste Boric
5349f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
5359f988b79SJean-Baptiste Boric
5369f988b79SJean-Baptiste Boric static int
udf_create_nodedscr_rmw(struct udf_strat_args * args)5379f988b79SJean-Baptiste Boric udf_create_nodedscr_rmw(struct udf_strat_args *args)
5389f988b79SJean-Baptiste Boric {
5399f988b79SJean-Baptiste Boric union dscrptr **dscrptr = &args->dscr;
5409f988b79SJean-Baptiste Boric struct udf_mount *ump = args->ump;
5419f988b79SJean-Baptiste Boric struct long_ad *icb = args->icb;
5429f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
5439f988b79SJean-Baptiste Boric uint64_t bit;
5449f988b79SJean-Baptiste Boric uint32_t sectornr, lb_size, dummy;
5459f988b79SJean-Baptiste Boric uint8_t *mem;
5469f988b79SJean-Baptiste Boric int error, eccsect;
5479f988b79SJean-Baptiste Boric
5489f988b79SJean-Baptiste Boric error = udf_translate_vtop(ump, icb, §ornr, &dummy);
5499f988b79SJean-Baptiste Boric if (error)
5509f988b79SJean-Baptiste Boric return error;
5519f988b79SJean-Baptiste Boric
5529f988b79SJean-Baptiste Boric lb_size = udf_rw32(ump->logical_vol->lb_size);
5539f988b79SJean-Baptiste Boric
5549f988b79SJean-Baptiste Boric /* get our eccline */
5559f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
5569f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
5579f988b79SJean-Baptiste Boric
5589f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << eccsect;
5599f988b79SJean-Baptiste Boric eccline->readin &= ~bit; /* just in case */
5609f988b79SJean-Baptiste Boric eccline->present |= bit;
5619f988b79SJean-Baptiste Boric eccline->dirty &= ~bit; /* Err... euhm... clean? */
5629f988b79SJean-Baptiste Boric
5639f988b79SJean-Baptiste Boric eccline->refcnt++;
5649f988b79SJean-Baptiste Boric
5659f988b79SJean-Baptiste Boric /* clear space */
5669f988b79SJean-Baptiste Boric mem = ((uint8_t *) eccline->blob) + eccsect * lb_size;
5679f988b79SJean-Baptiste Boric memset(mem, 0, lb_size);
5689f988b79SJean-Baptiste Boric
5699f988b79SJean-Baptiste Boric udf_puteccline(eccline);
5709f988b79SJean-Baptiste Boric
5719f988b79SJean-Baptiste Boric *dscrptr = (union dscrptr *) mem;
5729f988b79SJean-Baptiste Boric return 0;
5739f988b79SJean-Baptiste Boric }
5749f988b79SJean-Baptiste Boric
5759f988b79SJean-Baptiste Boric
5769f988b79SJean-Baptiste Boric static void
udf_free_nodedscr_rmw(struct udf_strat_args * args)5779f988b79SJean-Baptiste Boric udf_free_nodedscr_rmw(struct udf_strat_args *args)
5789f988b79SJean-Baptiste Boric {
5799f988b79SJean-Baptiste Boric struct udf_mount *ump = args->ump;
5809f988b79SJean-Baptiste Boric struct long_ad *icb = args->icb;
5819f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
5829f988b79SJean-Baptiste Boric uint64_t bit;
5839f988b79SJean-Baptiste Boric uint32_t sectornr, dummy;
5849f988b79SJean-Baptiste Boric int error, eccsect;
5859f988b79SJean-Baptiste Boric
5869f988b79SJean-Baptiste Boric error = udf_translate_vtop(ump, icb, §ornr, &dummy);
5879f988b79SJean-Baptiste Boric if (error)
5889f988b79SJean-Baptiste Boric return;
5899f988b79SJean-Baptiste Boric
5909f988b79SJean-Baptiste Boric /* get our eccline */
5919f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
5929f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
5939f988b79SJean-Baptiste Boric
5949f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << eccsect;
5959f988b79SJean-Baptiste Boric KASSERT(eccline->present & bit);
5969f988b79SJean-Baptiste Boric
5979f988b79SJean-Baptiste Boric eccline->readin &= ~bit; /* just in case */
5989f988b79SJean-Baptiste Boric /* XXX eccline->dirty? */
5999f988b79SJean-Baptiste Boric
6009f988b79SJean-Baptiste Boric KASSERT(eccline->refcnt >= 1);
6019f988b79SJean-Baptiste Boric eccline->refcnt--;
6029f988b79SJean-Baptiste Boric
6039f988b79SJean-Baptiste Boric udf_puteccline(eccline);
6049f988b79SJean-Baptiste Boric }
6059f988b79SJean-Baptiste Boric
6069f988b79SJean-Baptiste Boric
6079f988b79SJean-Baptiste Boric static int
udf_read_nodedscr_rmw(struct udf_strat_args * args)6089f988b79SJean-Baptiste Boric udf_read_nodedscr_rmw(struct udf_strat_args *args)
6099f988b79SJean-Baptiste Boric {
6109f988b79SJean-Baptiste Boric union dscrptr **dscrptr = &args->dscr;
6119f988b79SJean-Baptiste Boric struct udf_mount *ump = args->ump;
6129f988b79SJean-Baptiste Boric struct long_ad *icb = args->icb;
6139f988b79SJean-Baptiste Boric struct strat_private *priv;
6149f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
6159f988b79SJean-Baptiste Boric uint64_t bit;
6169f988b79SJean-Baptiste Boric uint32_t sectornr, dummy;
6179f988b79SJean-Baptiste Boric uint8_t *pos;
6189f988b79SJean-Baptiste Boric int sector_size = ump->discinfo.sector_size;
619*0a6a1f1dSLionel Sambuc int lb_size __diagused = udf_rw32(ump->logical_vol->lb_size);
6209f988b79SJean-Baptiste Boric int i, error, dscrlen, eccsect;
6219f988b79SJean-Baptiste Boric
6229f988b79SJean-Baptiste Boric KASSERT(sector_size == lb_size);
6239f988b79SJean-Baptiste Boric error = udf_translate_vtop(ump, icb, §ornr, &dummy);
6249f988b79SJean-Baptiste Boric if (error)
6259f988b79SJean-Baptiste Boric return error;
6269f988b79SJean-Baptiste Boric
6279f988b79SJean-Baptiste Boric /* get our eccline */
6289f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
6299f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
6309f988b79SJean-Baptiste Boric
6319f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << eccsect;
6329f988b79SJean-Baptiste Boric if ((eccline->present & bit) == 0) {
6339f988b79SJean-Baptiste Boric /* mark bit for readin */
6349f988b79SJean-Baptiste Boric eccline->readin |= bit;
6359f988b79SJean-Baptiste Boric eccline->refcnt++; /* prevent recycling */
6369f988b79SJean-Baptiste Boric KASSERT(eccline->bufs[eccsect] == NULL);
6379f988b79SJean-Baptiste Boric udf_puteccline(eccline);
6389f988b79SJean-Baptiste Boric
6399f988b79SJean-Baptiste Boric /* wait for completion */
6409f988b79SJean-Baptiste Boric priv = PRIV(eccline->ump);
6419f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
6429f988b79SJean-Baptiste Boric while (((eccline->present | eccline->error) & bit) == 0) {
6439f988b79SJean-Baptiste Boric error = cv_timedwait(&priv->discstrat_cv,
6449f988b79SJean-Baptiste Boric &priv->discstrat_mutex,
6459f988b79SJean-Baptiste Boric hz/8);
6469f988b79SJean-Baptiste Boric if (error == EWOULDBLOCK)
6479f988b79SJean-Baptiste Boric DPRINTF(LOCKING, ("eccline waiting for read\n"));
6489f988b79SJean-Baptiste Boric }
6499f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
6509f988b79SJean-Baptiste Boric
6519f988b79SJean-Baptiste Boric /* reget our line */
6529f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
6539f988b79SJean-Baptiste Boric KASSERT(eccline->refcnt >= 1);
6549f988b79SJean-Baptiste Boric eccline->refcnt--; /* undo refcnt */
6559f988b79SJean-Baptiste Boric
6569f988b79SJean-Baptiste Boric if (eccline->error & bit) {
6579f988b79SJean-Baptiste Boric *dscrptr = NULL;
6589f988b79SJean-Baptiste Boric udf_puteccline(eccline);
6599f988b79SJean-Baptiste Boric return EIO; /* XXX error code */
6609f988b79SJean-Baptiste Boric }
6619f988b79SJean-Baptiste Boric }
6629f988b79SJean-Baptiste Boric
6639f988b79SJean-Baptiste Boric *dscrptr = (union dscrptr *)
6649f988b79SJean-Baptiste Boric (((uint8_t *) eccline->blob) + eccsect * sector_size);
6659f988b79SJean-Baptiste Boric
6669f988b79SJean-Baptiste Boric /* code from read_phys_descr */
6679f988b79SJean-Baptiste Boric /* check if its a valid tag */
6689f988b79SJean-Baptiste Boric error = udf_check_tag(*dscrptr);
6699f988b79SJean-Baptiste Boric if (error) {
6709f988b79SJean-Baptiste Boric /* check if its an empty block */
6719f988b79SJean-Baptiste Boric pos = (uint8_t *) *dscrptr;
6729f988b79SJean-Baptiste Boric for (i = 0; i < sector_size; i++, pos++) {
6739f988b79SJean-Baptiste Boric if (*pos) break;
6749f988b79SJean-Baptiste Boric }
6759f988b79SJean-Baptiste Boric if (i == sector_size) {
6769f988b79SJean-Baptiste Boric /* return no error but with no dscrptr */
6779f988b79SJean-Baptiste Boric error = 0;
6789f988b79SJean-Baptiste Boric }
6799f988b79SJean-Baptiste Boric *dscrptr = NULL;
6809f988b79SJean-Baptiste Boric udf_puteccline(eccline);
6819f988b79SJean-Baptiste Boric return error;
6829f988b79SJean-Baptiste Boric }
6839f988b79SJean-Baptiste Boric
6849f988b79SJean-Baptiste Boric /* calculate descriptor size */
6859f988b79SJean-Baptiste Boric dscrlen = udf_tagsize(*dscrptr, sector_size);
6869f988b79SJean-Baptiste Boric error = udf_check_tag_payload(*dscrptr, dscrlen);
6879f988b79SJean-Baptiste Boric if (error) {
6889f988b79SJean-Baptiste Boric *dscrptr = NULL;
6899f988b79SJean-Baptiste Boric udf_puteccline(eccline);
6909f988b79SJean-Baptiste Boric return error;
6919f988b79SJean-Baptiste Boric }
6929f988b79SJean-Baptiste Boric
6939f988b79SJean-Baptiste Boric /* we have a hold since it has a node descriptor */
6949f988b79SJean-Baptiste Boric eccline->refcnt++;
6959f988b79SJean-Baptiste Boric udf_puteccline(eccline);
6969f988b79SJean-Baptiste Boric
6979f988b79SJean-Baptiste Boric return 0;
6989f988b79SJean-Baptiste Boric }
6999f988b79SJean-Baptiste Boric
7009f988b79SJean-Baptiste Boric
7019f988b79SJean-Baptiste Boric static int
udf_write_nodedscr_rmw(struct udf_strat_args * args)7029f988b79SJean-Baptiste Boric udf_write_nodedscr_rmw(struct udf_strat_args *args)
7039f988b79SJean-Baptiste Boric {
7049f988b79SJean-Baptiste Boric union dscrptr *dscrptr = args->dscr;
7059f988b79SJean-Baptiste Boric struct udf_mount *ump = args->ump;
7069f988b79SJean-Baptiste Boric struct long_ad *icb = args->icb;
7079f988b79SJean-Baptiste Boric struct udf_node *udf_node = args->udf_node;
7089f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
7099f988b79SJean-Baptiste Boric uint64_t bit;
7109f988b79SJean-Baptiste Boric uint32_t sectornr, logsectornr, dummy;
7119f988b79SJean-Baptiste Boric // int waitfor = args->waitfor;
7129f988b79SJean-Baptiste Boric int sector_size = ump->discinfo.sector_size;
713*0a6a1f1dSLionel Sambuc int lb_size __diagused = udf_rw32(ump->logical_vol->lb_size);
7149f988b79SJean-Baptiste Boric int error, eccsect;
7159f988b79SJean-Baptiste Boric
7169f988b79SJean-Baptiste Boric KASSERT(sector_size == lb_size);
7179f988b79SJean-Baptiste Boric sectornr = 0;
7189f988b79SJean-Baptiste Boric error = udf_translate_vtop(ump, icb, §ornr, &dummy);
7199f988b79SJean-Baptiste Boric if (error)
7209f988b79SJean-Baptiste Boric return error;
7219f988b79SJean-Baptiste Boric
7229f988b79SJean-Baptiste Boric /* get our eccline */
7239f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
7249f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
7259f988b79SJean-Baptiste Boric
7269f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << eccsect;
7279f988b79SJean-Baptiste Boric
7289f988b79SJean-Baptiste Boric /* old callback still pending? */
7299f988b79SJean-Baptiste Boric if (eccline->bufs[eccsect]) {
7309f988b79SJean-Baptiste Boric DPRINTF(WRITE, ("udf_write_nodedscr_rmw: writing descriptor"
7319f988b79SJean-Baptiste Boric " over buffer?\n"));
7329f988b79SJean-Baptiste Boric nestiobuf_done(eccline->bufs[eccsect],
7339f988b79SJean-Baptiste Boric eccline->bufs_len[eccsect],
7349f988b79SJean-Baptiste Boric 0);
7359f988b79SJean-Baptiste Boric eccline->bufs[eccsect] = NULL;
7369f988b79SJean-Baptiste Boric }
7379f988b79SJean-Baptiste Boric
7389f988b79SJean-Baptiste Boric /* set sector number in the descriptor and validate */
7399f988b79SJean-Baptiste Boric dscrptr = (union dscrptr *)
7409f988b79SJean-Baptiste Boric (((uint8_t *) eccline->blob) + eccsect * sector_size);
7419f988b79SJean-Baptiste Boric KASSERT(dscrptr == args->dscr);
7429f988b79SJean-Baptiste Boric
7439f988b79SJean-Baptiste Boric logsectornr = udf_rw32(icb->loc.lb_num);
7449f988b79SJean-Baptiste Boric dscrptr->tag.tag_loc = udf_rw32(logsectornr);
7459f988b79SJean-Baptiste Boric udf_validate_tag_and_crc_sums(dscrptr);
7469f988b79SJean-Baptiste Boric
7479f988b79SJean-Baptiste Boric udf_fixup_node_internals(ump, (uint8_t *) dscrptr, UDF_C_NODE);
7489f988b79SJean-Baptiste Boric
7499f988b79SJean-Baptiste Boric /* set our flags */
7509f988b79SJean-Baptiste Boric KASSERT(eccline->present & bit);
7519f988b79SJean-Baptiste Boric eccline->dirty |= bit;
7529f988b79SJean-Baptiste Boric
7539f988b79SJean-Baptiste Boric KASSERT(udf_tagsize(dscrptr, sector_size) <= sector_size);
7549f988b79SJean-Baptiste Boric
7559f988b79SJean-Baptiste Boric udf_node->outstanding_nodedscr--;
7569f988b79SJean-Baptiste Boric if (udf_node->outstanding_nodedscr == 0) {
7579f988b79SJean-Baptiste Boric /* XXX still using wakeup! */
7589f988b79SJean-Baptiste Boric UDF_UNLOCK_NODE(udf_node, 0);
7599f988b79SJean-Baptiste Boric wakeup(&udf_node->outstanding_nodedscr);
7609f988b79SJean-Baptiste Boric }
7619f988b79SJean-Baptiste Boric udf_puteccline(eccline);
7629f988b79SJean-Baptiste Boric
7639f988b79SJean-Baptiste Boric /* XXX waitfor not used */
7649f988b79SJean-Baptiste Boric return 0;
7659f988b79SJean-Baptiste Boric }
7669f988b79SJean-Baptiste Boric
7679f988b79SJean-Baptiste Boric
7689f988b79SJean-Baptiste Boric static void
udf_queuebuf_rmw(struct udf_strat_args * args)7699f988b79SJean-Baptiste Boric udf_queuebuf_rmw(struct udf_strat_args *args)
7709f988b79SJean-Baptiste Boric {
7719f988b79SJean-Baptiste Boric struct udf_mount *ump = args->ump;
7729f988b79SJean-Baptiste Boric struct buf *buf = args->nestbuf;
7739f988b79SJean-Baptiste Boric struct desc_tag *tag;
7749f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(ump);
7759f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
7769f988b79SJean-Baptiste Boric struct long_ad *node_ad_cpy;
7779f988b79SJean-Baptiste Boric uint64_t bit, *lmapping, *pmapping, *lmappos, *pmappos, blknr;
7789f988b79SJean-Baptiste Boric uint32_t buf_len, len, sectors, sectornr, our_sectornr;
7799f988b79SJean-Baptiste Boric uint32_t bpos;
7809f988b79SJean-Baptiste Boric uint16_t vpart_num;
7819f988b79SJean-Baptiste Boric uint8_t *fidblk, *src, *dst;
7829f988b79SJean-Baptiste Boric int sector_size = ump->discinfo.sector_size;
7839f988b79SJean-Baptiste Boric int blks = sector_size / DEV_BSIZE;
7849f988b79SJean-Baptiste Boric int eccsect, what, queue, error;
7859f988b79SJean-Baptiste Boric
7869f988b79SJean-Baptiste Boric KASSERT(ump);
7879f988b79SJean-Baptiste Boric KASSERT(buf);
7889f988b79SJean-Baptiste Boric KASSERT(buf->b_iodone == nestiobuf_iodone);
7899f988b79SJean-Baptiste Boric
7909f988b79SJean-Baptiste Boric blknr = buf->b_blkno;
7919f988b79SJean-Baptiste Boric our_sectornr = blknr / blks;
7929f988b79SJean-Baptiste Boric
7939f988b79SJean-Baptiste Boric what = buf->b_udf_c_type;
7949f988b79SJean-Baptiste Boric queue = UDF_SHED_READING;
7959f988b79SJean-Baptiste Boric if ((buf->b_flags & B_READ) == 0) {
7969f988b79SJean-Baptiste Boric /* writing */
7979f988b79SJean-Baptiste Boric queue = UDF_SHED_SEQWRITING;
7989f988b79SJean-Baptiste Boric if (what == UDF_C_ABSOLUTE)
7999f988b79SJean-Baptiste Boric queue = UDF_SHED_WRITING;
8009f988b79SJean-Baptiste Boric if (what == UDF_C_DSCR)
8019f988b79SJean-Baptiste Boric queue = UDF_SHED_WRITING;
8029f988b79SJean-Baptiste Boric if (what == UDF_C_NODE)
8039f988b79SJean-Baptiste Boric queue = UDF_SHED_WRITING;
8049f988b79SJean-Baptiste Boric }
8059f988b79SJean-Baptiste Boric
8069f988b79SJean-Baptiste Boric if (queue == UDF_SHED_READING) {
8079f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("\nudf_queuebuf_rmw READ %p : sector %d type %d,"
8089f988b79SJean-Baptiste Boric "b_resid %d, b_bcount %d, b_bufsize %d\n",
8099f988b79SJean-Baptiste Boric buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
8109f988b79SJean-Baptiste Boric buf->b_resid, buf->b_bcount, buf->b_bufsize));
8119f988b79SJean-Baptiste Boric
8129f988b79SJean-Baptiste Boric /* mark bits for reading */
8139f988b79SJean-Baptiste Boric buf_len = buf->b_bcount;
8149f988b79SJean-Baptiste Boric sectornr = our_sectornr;
8159f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
8169f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
8179f988b79SJean-Baptiste Boric bpos = 0;
8189f988b79SJean-Baptiste Boric while (buf_len) {
8199f988b79SJean-Baptiste Boric len = MIN(buf_len, sector_size);
8209f988b79SJean-Baptiste Boric if ((eccsect < 0) || (eccsect >= ump->packet_size)) {
8219f988b79SJean-Baptiste Boric udf_puteccline(eccline);
8229f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
8239f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
8249f988b79SJean-Baptiste Boric }
8259f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << eccsect;
8269f988b79SJean-Baptiste Boric error = eccline->error & bit ? EIO : 0;
8279f988b79SJean-Baptiste Boric if (eccline->present & bit) {
8289f988b79SJean-Baptiste Boric src = (uint8_t *) eccline->blob +
8299f988b79SJean-Baptiste Boric eccsect * sector_size;
8309f988b79SJean-Baptiste Boric dst = (uint8_t *) buf->b_data + bpos;
8319f988b79SJean-Baptiste Boric if (!error)
8329f988b79SJean-Baptiste Boric memcpy(dst, src, len);
8339f988b79SJean-Baptiste Boric nestiobuf_done(buf, len, error);
8349f988b79SJean-Baptiste Boric } else {
8359f988b79SJean-Baptiste Boric eccline->readin |= bit;
8369f988b79SJean-Baptiste Boric KASSERT(eccline->bufs[eccsect] == NULL);
8379f988b79SJean-Baptiste Boric eccline->bufs[eccsect] = buf;
8389f988b79SJean-Baptiste Boric eccline->bufs_bpos[eccsect] = bpos;
8399f988b79SJean-Baptiste Boric eccline->bufs_len[eccsect] = len;
8409f988b79SJean-Baptiste Boric }
8419f988b79SJean-Baptiste Boric bpos += sector_size;
8429f988b79SJean-Baptiste Boric eccsect++;
8439f988b79SJean-Baptiste Boric sectornr++;
8449f988b79SJean-Baptiste Boric buf_len -= len;
8459f988b79SJean-Baptiste Boric }
8469f988b79SJean-Baptiste Boric udf_puteccline(eccline);
8479f988b79SJean-Baptiste Boric return;
8489f988b79SJean-Baptiste Boric }
8499f988b79SJean-Baptiste Boric
8509f988b79SJean-Baptiste Boric if (queue == UDF_SHED_WRITING) {
8519f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("\nudf_queuebuf_rmw WRITE %p : sector %d "
8529f988b79SJean-Baptiste Boric "type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
8539f988b79SJean-Baptiste Boric buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
8549f988b79SJean-Baptiste Boric buf->b_resid, buf->b_bcount, buf->b_bufsize));
8559f988b79SJean-Baptiste Boric
8569f988b79SJean-Baptiste Boric /* if we have FIDs fixup using buffer's sector number(s) */
8579f988b79SJean-Baptiste Boric if (buf->b_udf_c_type == UDF_C_FIDS)
8589f988b79SJean-Baptiste Boric panic("UDF_C_FIDS in SHED_WRITING!\n");
8599f988b79SJean-Baptiste Boric
8609f988b79SJean-Baptiste Boric udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
8619f988b79SJean-Baptiste Boric
8629f988b79SJean-Baptiste Boric /* copy parts into the bufs and set for writing */
8639f988b79SJean-Baptiste Boric buf_len = buf->b_bcount;
8649f988b79SJean-Baptiste Boric sectornr = our_sectornr;
8659f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
8669f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
8679f988b79SJean-Baptiste Boric bpos = 0;
8689f988b79SJean-Baptiste Boric while (buf_len) {
8699f988b79SJean-Baptiste Boric len = MIN(buf_len, sector_size);
8709f988b79SJean-Baptiste Boric if ((eccsect < 0) || (eccsect >= ump->packet_size)) {
8719f988b79SJean-Baptiste Boric udf_puteccline(eccline);
8729f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, 0);
8739f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
8749f988b79SJean-Baptiste Boric }
8759f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << eccsect;
8769f988b79SJean-Baptiste Boric KASSERT((eccline->readin & bit) == 0);
8779f988b79SJean-Baptiste Boric eccline->present |= bit;
8789f988b79SJean-Baptiste Boric eccline->dirty |= bit;
8799f988b79SJean-Baptiste Boric if (eccline->bufs[eccsect]) {
8809f988b79SJean-Baptiste Boric /* old callback still pending */
8819f988b79SJean-Baptiste Boric nestiobuf_done(eccline->bufs[eccsect],
8829f988b79SJean-Baptiste Boric eccline->bufs_len[eccsect],
8839f988b79SJean-Baptiste Boric 0);
8849f988b79SJean-Baptiste Boric eccline->bufs[eccsect] = NULL;
8859f988b79SJean-Baptiste Boric }
8869f988b79SJean-Baptiste Boric
8879f988b79SJean-Baptiste Boric src = (uint8_t *) buf->b_data + bpos;
8889f988b79SJean-Baptiste Boric dst = (uint8_t *) eccline->blob + eccsect * sector_size;
8899f988b79SJean-Baptiste Boric if (len != sector_size)
8909f988b79SJean-Baptiste Boric memset(dst, 0, sector_size);
8919f988b79SJean-Baptiste Boric memcpy(dst, src, len);
8929f988b79SJean-Baptiste Boric
8939f988b79SJean-Baptiste Boric /* note that its finished for this extent */
8949f988b79SJean-Baptiste Boric eccline->bufs[eccsect] = NULL;
8959f988b79SJean-Baptiste Boric nestiobuf_done(buf, len, 0);
8969f988b79SJean-Baptiste Boric
8979f988b79SJean-Baptiste Boric bpos += sector_size;
8989f988b79SJean-Baptiste Boric eccsect++;
8999f988b79SJean-Baptiste Boric sectornr++;
9009f988b79SJean-Baptiste Boric buf_len -= len;
9019f988b79SJean-Baptiste Boric }
9029f988b79SJean-Baptiste Boric udf_puteccline(eccline);
9039f988b79SJean-Baptiste Boric return;
9049f988b79SJean-Baptiste Boric
9059f988b79SJean-Baptiste Boric }
9069f988b79SJean-Baptiste Boric
9079f988b79SJean-Baptiste Boric /* sequential writing */
9089f988b79SJean-Baptiste Boric KASSERT(queue == UDF_SHED_SEQWRITING);
9099f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("\nudf_queuebuf_rmw SEQWRITE %p : sector XXXX "
9109f988b79SJean-Baptiste Boric "type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
9119f988b79SJean-Baptiste Boric buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount,
9129f988b79SJean-Baptiste Boric buf->b_bufsize));
9139f988b79SJean-Baptiste Boric /*
9149f988b79SJean-Baptiste Boric * Buffers should not have been allocated to disc addresses yet on
9159f988b79SJean-Baptiste Boric * this queue. Note that a buffer can get multiple extents allocated.
9169f988b79SJean-Baptiste Boric * Note that it *looks* like the normal writing but its different in
9179f988b79SJean-Baptiste Boric * the details.
9189f988b79SJean-Baptiste Boric *
9199f988b79SJean-Baptiste Boric * lmapping contains lb_num relative to base partition.
9209f988b79SJean-Baptiste Boric *
9219f988b79SJean-Baptiste Boric * XXX should we try to claim/organize the allocated memory to
9229f988b79SJean-Baptiste Boric * block-aligned pieces?
9239f988b79SJean-Baptiste Boric */
9249f988b79SJean-Baptiste Boric mutex_enter(&priv->seqwrite_mutex);
9259f988b79SJean-Baptiste Boric
9269f988b79SJean-Baptiste Boric lmapping = ump->la_lmapping;
9279f988b79SJean-Baptiste Boric node_ad_cpy = ump->la_node_ad_cpy;
9289f988b79SJean-Baptiste Boric
9299f988b79SJean-Baptiste Boric /* logically allocate buf and map it in the file */
9309f988b79SJean-Baptiste Boric udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num);
9319f988b79SJean-Baptiste Boric
9329f988b79SJean-Baptiste Boric /* if we have FIDs, fixup using the new allocation table */
9339f988b79SJean-Baptiste Boric if (buf->b_udf_c_type == UDF_C_FIDS) {
9349f988b79SJean-Baptiste Boric buf_len = buf->b_bcount;
9359f988b79SJean-Baptiste Boric bpos = 0;
9369f988b79SJean-Baptiste Boric lmappos = lmapping;
9379f988b79SJean-Baptiste Boric while (buf_len) {
9389f988b79SJean-Baptiste Boric sectornr = *lmappos++;
9399f988b79SJean-Baptiste Boric len = MIN(buf_len, sector_size);
9409f988b79SJean-Baptiste Boric fidblk = (uint8_t *) buf->b_data + bpos;
9419f988b79SJean-Baptiste Boric udf_fixup_fid_block(fidblk, sector_size,
9429f988b79SJean-Baptiste Boric 0, len, sectornr);
9439f988b79SJean-Baptiste Boric bpos += len;
9449f988b79SJean-Baptiste Boric buf_len -= len;
9459f988b79SJean-Baptiste Boric }
9469f988b79SJean-Baptiste Boric }
9479f988b79SJean-Baptiste Boric if (buf->b_udf_c_type == UDF_C_METADATA_SBM) {
9489f988b79SJean-Baptiste Boric if (buf->b_lblkno == 0) {
9499f988b79SJean-Baptiste Boric /* update the tag location inside */
9509f988b79SJean-Baptiste Boric tag = (struct desc_tag *) buf->b_data;
9519f988b79SJean-Baptiste Boric tag->tag_loc = udf_rw32(*lmapping);
9529f988b79SJean-Baptiste Boric udf_validate_tag_and_crc_sums(buf->b_data);
9539f988b79SJean-Baptiste Boric }
9549f988b79SJean-Baptiste Boric }
9559f988b79SJean-Baptiste Boric udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
9569f988b79SJean-Baptiste Boric
9579f988b79SJean-Baptiste Boric /*
9589f988b79SJean-Baptiste Boric * Translate new mappings in lmapping to pmappings.
9599f988b79SJean-Baptiste Boric * pmapping to contain lb_nums as used for disc adressing.
9609f988b79SJean-Baptiste Boric */
9619f988b79SJean-Baptiste Boric pmapping = ump->la_pmapping;
9629f988b79SJean-Baptiste Boric sectors = (buf->b_bcount + sector_size -1) / sector_size;
9639f988b79SJean-Baptiste Boric udf_translate_vtop_list(ump, sectors, vpart_num, lmapping, pmapping);
9649f988b79SJean-Baptiste Boric
9659f988b79SJean-Baptiste Boric /* copy parts into the bufs and set for writing */
9669f988b79SJean-Baptiste Boric pmappos = pmapping;
9679f988b79SJean-Baptiste Boric buf_len = buf->b_bcount;
9689f988b79SJean-Baptiste Boric sectornr = *pmappos++;
9699f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, ECC_SEQWRITING);
9709f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
9719f988b79SJean-Baptiste Boric bpos = 0;
9729f988b79SJean-Baptiste Boric while (buf_len) {
9739f988b79SJean-Baptiste Boric len = MIN(buf_len, sector_size);
9749f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
9759f988b79SJean-Baptiste Boric if ((eccsect < 0) || (eccsect >= ump->packet_size)) {
9769f988b79SJean-Baptiste Boric eccline->flags |= ECC_SEQWRITING;
9779f988b79SJean-Baptiste Boric udf_puteccline(eccline);
9789f988b79SJean-Baptiste Boric eccline = udf_geteccline(ump, sectornr, ECC_SEQWRITING);
9799f988b79SJean-Baptiste Boric eccsect = sectornr - eccline->start_sector;
9809f988b79SJean-Baptiste Boric }
9819f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << eccsect;
9829f988b79SJean-Baptiste Boric KASSERT((eccline->readin & bit) == 0);
9839f988b79SJean-Baptiste Boric eccline->present |= bit;
9849f988b79SJean-Baptiste Boric eccline->dirty |= bit;
9859f988b79SJean-Baptiste Boric eccline->bufs[eccsect] = NULL;
9869f988b79SJean-Baptiste Boric
9879f988b79SJean-Baptiste Boric src = (uint8_t *) buf->b_data + bpos;
9889f988b79SJean-Baptiste Boric dst = (uint8_t *)
9899f988b79SJean-Baptiste Boric eccline->blob + eccsect * sector_size;
9909f988b79SJean-Baptiste Boric if (len != sector_size)
9919f988b79SJean-Baptiste Boric memset(dst, 0, sector_size);
9929f988b79SJean-Baptiste Boric memcpy(dst, src, len);
9939f988b79SJean-Baptiste Boric
9949f988b79SJean-Baptiste Boric /* note that its finished for this extent */
9959f988b79SJean-Baptiste Boric nestiobuf_done(buf, len, 0);
9969f988b79SJean-Baptiste Boric
9979f988b79SJean-Baptiste Boric bpos += sector_size;
9989f988b79SJean-Baptiste Boric sectornr = *pmappos++;
9999f988b79SJean-Baptiste Boric buf_len -= len;
10009f988b79SJean-Baptiste Boric }
10019f988b79SJean-Baptiste Boric eccline->flags |= ECC_SEQWRITING;
10029f988b79SJean-Baptiste Boric udf_puteccline(eccline);
10039f988b79SJean-Baptiste Boric mutex_exit(&priv->seqwrite_mutex);
10049f988b79SJean-Baptiste Boric }
10059f988b79SJean-Baptiste Boric
10069f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
10079f988b79SJean-Baptiste Boric
10089f988b79SJean-Baptiste Boric static void
udf_shedule_read_callback(struct buf * buf)10099f988b79SJean-Baptiste Boric udf_shedule_read_callback(struct buf *buf)
10109f988b79SJean-Baptiste Boric {
10119f988b79SJean-Baptiste Boric struct udf_eccline *eccline = BTOE(buf);
10129f988b79SJean-Baptiste Boric struct udf_mount *ump = eccline->ump;
10139f988b79SJean-Baptiste Boric uint64_t bit;
10149f988b79SJean-Baptiste Boric uint8_t *src, *dst;
10159f988b79SJean-Baptiste Boric int sector_size = ump->discinfo.sector_size;
10169f988b79SJean-Baptiste Boric int error, i, len;
10179f988b79SJean-Baptiste Boric
10189f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("read callback called on buf %p\n", buf));
10199f988b79SJean-Baptiste Boric
10209f988b79SJean-Baptiste Boric /* post process read action */
10219f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_LOCKED);
10229f988b79SJean-Baptiste Boric error = buf->b_error;
10239f988b79SJean-Baptiste Boric for (i = 0; i < ump->packet_size; i++) {
10249f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << i;
10259f988b79SJean-Baptiste Boric src = (uint8_t *) buf->b_data + i * sector_size;
10269f988b79SJean-Baptiste Boric dst = (uint8_t *) eccline->blob + i * sector_size;
10279f988b79SJean-Baptiste Boric if (eccline->present & bit)
10289f988b79SJean-Baptiste Boric continue;
10299f988b79SJean-Baptiste Boric eccline->present |= bit;
10309f988b79SJean-Baptiste Boric if (error)
10319f988b79SJean-Baptiste Boric eccline->error |= bit;
10329f988b79SJean-Baptiste Boric if (eccline->bufs[i]) {
10339f988b79SJean-Baptiste Boric dst = (uint8_t *) eccline->bufs[i]->b_data +
10349f988b79SJean-Baptiste Boric eccline->bufs_bpos[i];
10359f988b79SJean-Baptiste Boric len = eccline->bufs_len[i];
10369f988b79SJean-Baptiste Boric if (!error)
10379f988b79SJean-Baptiste Boric memcpy(dst, src, len);
10389f988b79SJean-Baptiste Boric nestiobuf_done(eccline->bufs[i], len, error);
10399f988b79SJean-Baptiste Boric eccline->bufs[i] = NULL;
10409f988b79SJean-Baptiste Boric }
10419f988b79SJean-Baptiste Boric
10429f988b79SJean-Baptiste Boric }
10439f988b79SJean-Baptiste Boric KASSERT(buf->b_data == eccline->blob);
10449f988b79SJean-Baptiste Boric KASSERT(eccline->present == ((uint64_t) 1 << ump->packet_size)-1);
10459f988b79SJean-Baptiste Boric
10469f988b79SJean-Baptiste Boric /*
10479f988b79SJean-Baptiste Boric * XXX TODO what to do on read errors? read in all sectors
10489f988b79SJean-Baptiste Boric * synchronously and allocate a sparable entry?
10499f988b79SJean-Baptiste Boric */
10509f988b79SJean-Baptiste Boric
10519f988b79SJean-Baptiste Boric udf_puteccline(eccline);
10529f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("read callback finished\n"));
10539f988b79SJean-Baptiste Boric }
10549f988b79SJean-Baptiste Boric
10559f988b79SJean-Baptiste Boric
10569f988b79SJean-Baptiste Boric static void
udf_shedule_write_callback(struct buf * buf)10579f988b79SJean-Baptiste Boric udf_shedule_write_callback(struct buf *buf)
10589f988b79SJean-Baptiste Boric {
10599f988b79SJean-Baptiste Boric struct udf_eccline *eccline = BTOE(buf);
10609f988b79SJean-Baptiste Boric struct udf_mount *ump = eccline->ump;
10619f988b79SJean-Baptiste Boric uint64_t bit;
10629f988b79SJean-Baptiste Boric int error, i;
10639f988b79SJean-Baptiste Boric
10649f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("write callback called on buf %p\n", buf));
10659f988b79SJean-Baptiste Boric
10669f988b79SJean-Baptiste Boric /* post process write action */
10679f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_LOCKED);
10689f988b79SJean-Baptiste Boric error = buf->b_error;
10699f988b79SJean-Baptiste Boric for (i = 0; i < ump->packet_size; i++) {
10709f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << i;
10719f988b79SJean-Baptiste Boric if ((eccline->dirty & bit) == 0)
10729f988b79SJean-Baptiste Boric continue;
10739f988b79SJean-Baptiste Boric if (error) {
10749f988b79SJean-Baptiste Boric eccline->error |= bit;
10759f988b79SJean-Baptiste Boric } else {
10769f988b79SJean-Baptiste Boric eccline->dirty &= ~bit;
10779f988b79SJean-Baptiste Boric }
10789f988b79SJean-Baptiste Boric
10799f988b79SJean-Baptiste Boric KASSERT(eccline->bufs[i] == 0);
10809f988b79SJean-Baptiste Boric }
10819f988b79SJean-Baptiste Boric KASSERT(eccline->dirty == 0);
10829f988b79SJean-Baptiste Boric KASSERT(error == 0);
10839f988b79SJean-Baptiste Boric
10849f988b79SJean-Baptiste Boric /*
10859f988b79SJean-Baptiste Boric * XXX TODO on write errors allocate a sparable entry and reissue
10869f988b79SJean-Baptiste Boric */
10879f988b79SJean-Baptiste Boric
10889f988b79SJean-Baptiste Boric udf_puteccline(eccline);
10899f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("write callback finished\n"));
10909f988b79SJean-Baptiste Boric }
10919f988b79SJean-Baptiste Boric
10929f988b79SJean-Baptiste Boric
10939f988b79SJean-Baptiste Boric static void
udf_issue_eccline(struct udf_eccline * eccline,int queued_on)10949f988b79SJean-Baptiste Boric udf_issue_eccline(struct udf_eccline *eccline, int queued_on)
10959f988b79SJean-Baptiste Boric {
10969f988b79SJean-Baptiste Boric struct udf_mount *ump = eccline->ump;
10979f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(ump);
10989f988b79SJean-Baptiste Boric struct buf *buf, *nestbuf;
10999f988b79SJean-Baptiste Boric uint64_t bit, allbits = ((uint64_t) 1 << ump->packet_size)-1;
11009f988b79SJean-Baptiste Boric uint32_t start;
11019f988b79SJean-Baptiste Boric int sector_size = ump->discinfo.sector_size;
11029f988b79SJean-Baptiste Boric int blks = sector_size / DEV_BSIZE;
11039f988b79SJean-Baptiste Boric int i;
11049f988b79SJean-Baptiste Boric
11059f988b79SJean-Baptiste Boric KASSERT(eccline->flags & ECC_LOCKED);
11069f988b79SJean-Baptiste Boric
11079f988b79SJean-Baptiste Boric if (queued_on == UDF_SHED_READING) {
11089f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("udf_issue_eccline reading : "));
11099f988b79SJean-Baptiste Boric /* read all bits that are not yet present */
11109f988b79SJean-Baptiste Boric eccline->readin = (~eccline->present) & allbits;
11119f988b79SJean-Baptiste Boric KASSERT(eccline->readin);
11129f988b79SJean-Baptiste Boric start = eccline->start_sector;
11139f988b79SJean-Baptiste Boric buf = eccline->buf;
11149f988b79SJean-Baptiste Boric buf->b_flags = B_READ | B_ASYNC;
11159f988b79SJean-Baptiste Boric SET(buf->b_cflags, BC_BUSY); /* mark buffer busy */
11169f988b79SJean-Baptiste Boric buf->b_oflags = 0;
11179f988b79SJean-Baptiste Boric buf->b_iodone = udf_shedule_read_callback;
11189f988b79SJean-Baptiste Boric buf->b_data = eccline->blob;
11199f988b79SJean-Baptiste Boric buf->b_bcount = ump->packet_size * sector_size;
11209f988b79SJean-Baptiste Boric buf->b_resid = buf->b_bcount;
11219f988b79SJean-Baptiste Boric buf->b_bufsize = buf->b_bcount;
11229f988b79SJean-Baptiste Boric buf->b_private = eccline;
11239f988b79SJean-Baptiste Boric BIO_SETPRIO(buf, BPRIO_DEFAULT);
11249f988b79SJean-Baptiste Boric buf->b_lblkno = buf->b_blkno = buf->b_rawblkno = start * blks;
11259f988b79SJean-Baptiste Boric buf->b_proc = NULL;
11269f988b79SJean-Baptiste Boric
11279f988b79SJean-Baptiste Boric if (eccline->present != 0) {
11289f988b79SJean-Baptiste Boric for (i = 0; i < ump->packet_size; i++) {
11299f988b79SJean-Baptiste Boric bit = (uint64_t) 1 << i;
11309f988b79SJean-Baptiste Boric if (eccline->present & bit) {
11319f988b79SJean-Baptiste Boric nestiobuf_done(buf, sector_size, 0);
11329f988b79SJean-Baptiste Boric continue;
11339f988b79SJean-Baptiste Boric }
11349f988b79SJean-Baptiste Boric nestbuf = getiobuf(NULL, true);
11359f988b79SJean-Baptiste Boric nestiobuf_setup(buf, nestbuf, i * sector_size,
11369f988b79SJean-Baptiste Boric sector_size);
11379f988b79SJean-Baptiste Boric /* adjust blocknumber to read */
11389f988b79SJean-Baptiste Boric nestbuf->b_blkno = buf->b_blkno + i*blks;
11399f988b79SJean-Baptiste Boric nestbuf->b_rawblkno = buf->b_rawblkno + i*blks;
11409f988b79SJean-Baptiste Boric
11419f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("sector %d ", start + i));
11429f988b79SJean-Baptiste Boric
11439f988b79SJean-Baptiste Boric /* mutex dance since it could lock */
11449f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
11459f988b79SJean-Baptiste Boric /* call asynchronous */
11469f988b79SJean-Baptiste Boric VOP_STRATEGY(ump->devvp, nestbuf);
11479f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
11489f988b79SJean-Baptiste Boric }
11499f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("\n"));
11509f988b79SJean-Baptiste Boric return;
11519f988b79SJean-Baptiste Boric }
11529f988b79SJean-Baptiste Boric } else {
11539f988b79SJean-Baptiste Boric /* write or seqwrite */
11549f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("udf_issue_eccline writing or seqwriting : "));
11559f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("\n\tpresent %"PRIx64", readin %"PRIx64", "
11569f988b79SJean-Baptiste Boric "dirty %"PRIx64"\n\t", eccline->present, eccline->readin,
11579f988b79SJean-Baptiste Boric eccline->dirty));
11589f988b79SJean-Baptiste Boric KASSERT(eccline->present == allbits);
11599f988b79SJean-Baptiste Boric
11609f988b79SJean-Baptiste Boric start = eccline->start_sector;
11619f988b79SJean-Baptiste Boric buf = eccline->buf;
11629f988b79SJean-Baptiste Boric buf->b_flags = B_WRITE | B_ASYNC;
11639f988b79SJean-Baptiste Boric SET(buf->b_cflags, BC_BUSY); /* mark buffer busy */
11649f988b79SJean-Baptiste Boric buf->b_oflags = 0;
11659f988b79SJean-Baptiste Boric buf->b_iodone = udf_shedule_write_callback;
11669f988b79SJean-Baptiste Boric buf->b_data = eccline->blob;
11679f988b79SJean-Baptiste Boric buf->b_bcount = ump->packet_size * sector_size;
11689f988b79SJean-Baptiste Boric buf->b_resid = buf->b_bcount;
11699f988b79SJean-Baptiste Boric buf->b_bufsize = buf->b_bcount;
11709f988b79SJean-Baptiste Boric buf->b_private = eccline;
11719f988b79SJean-Baptiste Boric BIO_SETPRIO(buf, BPRIO_DEFAULT);
11729f988b79SJean-Baptiste Boric buf->b_lblkno = buf->b_blkno = buf->b_rawblkno = start * blks;
11739f988b79SJean-Baptiste Boric buf->b_proc = NULL;
11749f988b79SJean-Baptiste Boric }
11759f988b79SJean-Baptiste Boric
11769f988b79SJean-Baptiste Boric /* mutex dance since it could lock */
11779f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
11789f988b79SJean-Baptiste Boric /* call asynchronous */
11799f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("sector %d for %d\n",
11809f988b79SJean-Baptiste Boric start, ump->packet_size));
11819f988b79SJean-Baptiste Boric VOP_STRATEGY(ump->devvp, buf);
11829f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
11839f988b79SJean-Baptiste Boric }
11849f988b79SJean-Baptiste Boric
11859f988b79SJean-Baptiste Boric
11869f988b79SJean-Baptiste Boric static void
udf_discstrat_thread(void * arg)11879f988b79SJean-Baptiste Boric udf_discstrat_thread(void *arg)
11889f988b79SJean-Baptiste Boric {
11899f988b79SJean-Baptiste Boric struct udf_mount *ump = (struct udf_mount *) arg;
11909f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(ump);
11919f988b79SJean-Baptiste Boric struct udf_eccline *eccline;
11929f988b79SJean-Baptiste Boric struct timespec now, *last;
11939f988b79SJean-Baptiste Boric uint64_t allbits = ((uint64_t) 1 << ump->packet_size)-1;
11949f988b79SJean-Baptiste Boric int new_queue, wait, work;
11959f988b79SJean-Baptiste Boric
11969f988b79SJean-Baptiste Boric work = 1;
11979f988b79SJean-Baptiste Boric priv->thread_running = 1;
11989f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
11999f988b79SJean-Baptiste Boric priv->num_floating = 0;
12009f988b79SJean-Baptiste Boric while (priv->run_thread || work || priv->num_floating) {
12019f988b79SJean-Baptiste Boric /* get our time */
12029f988b79SJean-Baptiste Boric vfs_timestamp(&now);
12039f988b79SJean-Baptiste Boric
12049f988b79SJean-Baptiste Boric /* maintenance: handle eccline state machine */
12059f988b79SJean-Baptiste Boric for(;;) {
12069f988b79SJean-Baptiste Boric /* only peek at it */
12079f988b79SJean-Baptiste Boric eccline = udf_peek_eccline(priv, UDF_SHED_WAITING);
12089f988b79SJean-Baptiste Boric if (eccline == NULL)
12099f988b79SJean-Baptiste Boric break;
12109f988b79SJean-Baptiste Boric
12119f988b79SJean-Baptiste Boric /* if not reading, wait until the time has come */
12129f988b79SJean-Baptiste Boric if ((priv->cur_queue != UDF_SHED_READING) &&
12139f988b79SJean-Baptiste Boric (eccline->wait_time.tv_sec - now.tv_sec > 0)) {
12149f988b79SJean-Baptiste Boric UDF_UNLOCK_ECCLINE(eccline);
12159f988b79SJean-Baptiste Boric /* all others are later, so break off */
12169f988b79SJean-Baptiste Boric break;
12179f988b79SJean-Baptiste Boric }
12189f988b79SJean-Baptiste Boric
12199f988b79SJean-Baptiste Boric /* release */
12209f988b79SJean-Baptiste Boric UDF_UNLOCK_ECCLINE(eccline);
12219f988b79SJean-Baptiste Boric
12229f988b79SJean-Baptiste Boric /* do get it */
12239f988b79SJean-Baptiste Boric eccline = udf_pop_eccline(priv, UDF_SHED_WAITING);
12249f988b79SJean-Baptiste Boric
12259f988b79SJean-Baptiste Boric /* requeue according to state */
12269f988b79SJean-Baptiste Boric new_queue = UDF_SHED_FREE; /* unlikely */
12279f988b79SJean-Baptiste Boric if (eccline->refcnt > 0)
12289f988b79SJean-Baptiste Boric new_queue = UDF_SHED_IDLE;
12299f988b79SJean-Baptiste Boric if (eccline->flags & ECC_WANTED)
12309f988b79SJean-Baptiste Boric new_queue = UDF_SHED_IDLE;
12319f988b79SJean-Baptiste Boric if (eccline->readin)
12329f988b79SJean-Baptiste Boric new_queue = UDF_SHED_READING;
12339f988b79SJean-Baptiste Boric if (eccline->dirty) {
12349f988b79SJean-Baptiste Boric new_queue = UDF_SHED_READING;
12359f988b79SJean-Baptiste Boric if (eccline->present == allbits) {
12369f988b79SJean-Baptiste Boric new_queue = UDF_SHED_WRITING;
12379f988b79SJean-Baptiste Boric if (eccline->flags & ECC_SEQWRITING)
12389f988b79SJean-Baptiste Boric new_queue = UDF_SHED_SEQWRITING;
12399f988b79SJean-Baptiste Boric }
12409f988b79SJean-Baptiste Boric }
12419f988b79SJean-Baptiste Boric udf_push_eccline(eccline, new_queue);
12429f988b79SJean-Baptiste Boric }
12439f988b79SJean-Baptiste Boric
12449f988b79SJean-Baptiste Boric /* maintenance: free excess ecclines */
12459f988b79SJean-Baptiste Boric while (priv->num_queued[UDF_SHED_FREE] > UDF_ECCLINE_MAXFREE) {
12469f988b79SJean-Baptiste Boric eccline = udf_pop_eccline(priv, UDF_SHED_FREE);
12479f988b79SJean-Baptiste Boric KASSERT(eccline);
12489f988b79SJean-Baptiste Boric KASSERT(eccline->refcnt == 0);
12499f988b79SJean-Baptiste Boric if (eccline->flags & ECC_WANTED) {
12509f988b79SJean-Baptiste Boric /* we won the race, but we dont want to win */
12519f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("Tried removing, pushed back to free list\n"));
12529f988b79SJean-Baptiste Boric udf_push_eccline(eccline, UDF_SHED_IDLE);
12539f988b79SJean-Baptiste Boric } else {
12549f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("Removing entry from free list\n"));
12559f988b79SJean-Baptiste Boric udf_dispose_eccline(eccline);
12569f988b79SJean-Baptiste Boric }
12579f988b79SJean-Baptiste Boric }
12589f988b79SJean-Baptiste Boric
12599f988b79SJean-Baptiste Boric /* process the current selected queue */
12609f988b79SJean-Baptiste Boric /* get our time */
12619f988b79SJean-Baptiste Boric vfs_timestamp(&now);
12629f988b79SJean-Baptiste Boric last = &priv->last_queued[priv->cur_queue];
12639f988b79SJean-Baptiste Boric
12649f988b79SJean-Baptiste Boric /* get our line */
12659f988b79SJean-Baptiste Boric eccline = udf_pop_eccline(priv, priv->cur_queue);
12669f988b79SJean-Baptiste Boric if (eccline) {
12679f988b79SJean-Baptiste Boric wait = 0;
12689f988b79SJean-Baptiste Boric new_queue = priv->cur_queue;
12699f988b79SJean-Baptiste Boric DPRINTF(ECCLINE, ("UDF_ISSUE_ECCLINE\n"));
12709f988b79SJean-Baptiste Boric
12719f988b79SJean-Baptiste Boric udf_issue_eccline(eccline, priv->cur_queue);
12729f988b79SJean-Baptiste Boric } else {
12739f988b79SJean-Baptiste Boric /* don't switch too quickly */
12749f988b79SJean-Baptiste Boric if (now.tv_sec - last->tv_sec < 2) {
12759f988b79SJean-Baptiste Boric /* wait some time */
12769f988b79SJean-Baptiste Boric cv_timedwait(&priv->discstrat_cv,
12779f988b79SJean-Baptiste Boric &priv->discstrat_mutex, hz);
12789f988b79SJean-Baptiste Boric /* we assume there is work to be done */
12799f988b79SJean-Baptiste Boric work = 1;
12809f988b79SJean-Baptiste Boric continue;
12819f988b79SJean-Baptiste Boric }
12829f988b79SJean-Baptiste Boric
12839f988b79SJean-Baptiste Boric /* XXX select on queue lengths ? */
12849f988b79SJean-Baptiste Boric wait = 1;
12859f988b79SJean-Baptiste Boric /* check if we can/should switch */
12869f988b79SJean-Baptiste Boric new_queue = priv->cur_queue;
12879f988b79SJean-Baptiste Boric if (bufq_peek(priv->queues[UDF_SHED_READING]))
12889f988b79SJean-Baptiste Boric new_queue = UDF_SHED_READING;
12899f988b79SJean-Baptiste Boric if (bufq_peek(priv->queues[UDF_SHED_WRITING]))
12909f988b79SJean-Baptiste Boric new_queue = UDF_SHED_WRITING;
12919f988b79SJean-Baptiste Boric if (bufq_peek(priv->queues[UDF_SHED_SEQWRITING]))
12929f988b79SJean-Baptiste Boric new_queue = UDF_SHED_SEQWRITING;
12939f988b79SJean-Baptiste Boric }
12949f988b79SJean-Baptiste Boric
12959f988b79SJean-Baptiste Boric /* give room */
12969f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
12979f988b79SJean-Baptiste Boric
12989f988b79SJean-Baptiste Boric if (new_queue != priv->cur_queue) {
12999f988b79SJean-Baptiste Boric wait = 0;
13009f988b79SJean-Baptiste Boric DPRINTF(SHEDULE, ("switching from %d to %d\n",
13019f988b79SJean-Baptiste Boric priv->cur_queue, new_queue));
13029f988b79SJean-Baptiste Boric priv->cur_queue = new_queue;
13039f988b79SJean-Baptiste Boric }
13049f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
13059f988b79SJean-Baptiste Boric
13069f988b79SJean-Baptiste Boric /* wait for more if needed */
13079f988b79SJean-Baptiste Boric if (wait)
13089f988b79SJean-Baptiste Boric cv_timedwait(&priv->discstrat_cv,
13099f988b79SJean-Baptiste Boric &priv->discstrat_mutex, hz/4); /* /8 */
13109f988b79SJean-Baptiste Boric
13119f988b79SJean-Baptiste Boric work = (bufq_peek(priv->queues[UDF_SHED_WAITING]) != NULL);
13129f988b79SJean-Baptiste Boric work |= (bufq_peek(priv->queues[UDF_SHED_READING]) != NULL);
13139f988b79SJean-Baptiste Boric work |= (bufq_peek(priv->queues[UDF_SHED_WRITING]) != NULL);
13149f988b79SJean-Baptiste Boric work |= (bufq_peek(priv->queues[UDF_SHED_SEQWRITING]) != NULL);
13159f988b79SJean-Baptiste Boric
13169f988b79SJean-Baptiste Boric DPRINTF(PARANOIA, ("work : (%d, %d, %d) -> work %d, float %d\n",
13179f988b79SJean-Baptiste Boric (bufq_peek(priv->queues[UDF_SHED_READING]) != NULL),
13189f988b79SJean-Baptiste Boric (bufq_peek(priv->queues[UDF_SHED_WRITING]) != NULL),
13199f988b79SJean-Baptiste Boric (bufq_peek(priv->queues[UDF_SHED_SEQWRITING]) != NULL),
13209f988b79SJean-Baptiste Boric work, priv->num_floating));
13219f988b79SJean-Baptiste Boric }
13229f988b79SJean-Baptiste Boric
13239f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
13249f988b79SJean-Baptiste Boric
13259f988b79SJean-Baptiste Boric /* tear down remaining ecclines */
13269f988b79SJean-Baptiste Boric mutex_enter(&priv->discstrat_mutex);
13279f988b79SJean-Baptiste Boric KASSERT(bufq_peek(priv->queues[UDF_SHED_WAITING]) == NULL);
13289f988b79SJean-Baptiste Boric KASSERT(bufq_peek(priv->queues[UDF_SHED_IDLE]) == NULL);
13299f988b79SJean-Baptiste Boric KASSERT(bufq_peek(priv->queues[UDF_SHED_READING]) == NULL);
13309f988b79SJean-Baptiste Boric KASSERT(bufq_peek(priv->queues[UDF_SHED_WRITING]) == NULL);
13319f988b79SJean-Baptiste Boric KASSERT(bufq_peek(priv->queues[UDF_SHED_SEQWRITING]) == NULL);
13329f988b79SJean-Baptiste Boric
13339f988b79SJean-Baptiste Boric KASSERT(priv->num_queued[UDF_SHED_WAITING] == 0);
13349f988b79SJean-Baptiste Boric KASSERT(priv->num_queued[UDF_SHED_IDLE] == 0);
13359f988b79SJean-Baptiste Boric KASSERT(priv->num_queued[UDF_SHED_READING] == 0);
13369f988b79SJean-Baptiste Boric KASSERT(priv->num_queued[UDF_SHED_WRITING] == 0);
13379f988b79SJean-Baptiste Boric KASSERT(priv->num_queued[UDF_SHED_SEQWRITING] == 0);
13389f988b79SJean-Baptiste Boric
13399f988b79SJean-Baptiste Boric eccline = udf_pop_eccline(priv, UDF_SHED_FREE);
13409f988b79SJean-Baptiste Boric while (eccline) {
13419f988b79SJean-Baptiste Boric udf_dispose_eccline(eccline);
13429f988b79SJean-Baptiste Boric eccline = udf_pop_eccline(priv, UDF_SHED_FREE);
13439f988b79SJean-Baptiste Boric }
13449f988b79SJean-Baptiste Boric KASSERT(priv->num_queued[UDF_SHED_FREE] == 0);
13459f988b79SJean-Baptiste Boric mutex_exit(&priv->discstrat_mutex);
13469f988b79SJean-Baptiste Boric
13479f988b79SJean-Baptiste Boric priv->thread_running = 0;
13489f988b79SJean-Baptiste Boric priv->thread_finished = 1;
13499f988b79SJean-Baptiste Boric wakeup(&priv->run_thread);
13509f988b79SJean-Baptiste Boric kthread_exit(0);
13519f988b79SJean-Baptiste Boric /* not reached */
13529f988b79SJean-Baptiste Boric }
13539f988b79SJean-Baptiste Boric
13549f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
13559f988b79SJean-Baptiste Boric
13569f988b79SJean-Baptiste Boric /*
13579f988b79SJean-Baptiste Boric * Buffer memory pool allocator.
13589f988b79SJean-Baptiste Boric */
13599f988b79SJean-Baptiste Boric
13609f988b79SJean-Baptiste Boric static void *
ecclinepool_page_alloc(struct pool * pp,int flags)13619f988b79SJean-Baptiste Boric ecclinepool_page_alloc(struct pool *pp, int flags)
13629f988b79SJean-Baptiste Boric {
13639f988b79SJean-Baptiste Boric return (void *)uvm_km_alloc(kernel_map,
13649f988b79SJean-Baptiste Boric MAXBSIZE, MAXBSIZE,
13659f988b79SJean-Baptiste Boric ((flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK)
13669f988b79SJean-Baptiste Boric | UVM_KMF_WIRED /* UVM_KMF_PAGABLE? */);
13679f988b79SJean-Baptiste Boric }
13689f988b79SJean-Baptiste Boric
13699f988b79SJean-Baptiste Boric static void
ecclinepool_page_free(struct pool * pp,void * v)13709f988b79SJean-Baptiste Boric ecclinepool_page_free(struct pool *pp, void *v)
13719f988b79SJean-Baptiste Boric {
13729f988b79SJean-Baptiste Boric uvm_km_free(kernel_map, (vaddr_t)v, MAXBSIZE, UVM_KMF_WIRED);
13739f988b79SJean-Baptiste Boric }
13749f988b79SJean-Baptiste Boric
13759f988b79SJean-Baptiste Boric static struct pool_allocator ecclinepool_allocator = {
13769f988b79SJean-Baptiste Boric .pa_alloc = ecclinepool_page_alloc,
13779f988b79SJean-Baptiste Boric .pa_free = ecclinepool_page_free,
13789f988b79SJean-Baptiste Boric .pa_pagesz = MAXBSIZE,
13799f988b79SJean-Baptiste Boric };
13809f988b79SJean-Baptiste Boric
13819f988b79SJean-Baptiste Boric
13829f988b79SJean-Baptiste Boric static void
udf_discstrat_init_rmw(struct udf_strat_args * args)13839f988b79SJean-Baptiste Boric udf_discstrat_init_rmw(struct udf_strat_args *args)
13849f988b79SJean-Baptiste Boric {
13859f988b79SJean-Baptiste Boric struct udf_mount *ump = args->ump;
13869f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(ump);
13879f988b79SJean-Baptiste Boric uint32_t lb_size, blobsize, hashline;
13889f988b79SJean-Baptiste Boric int i;
13899f988b79SJean-Baptiste Boric
13909f988b79SJean-Baptiste Boric KASSERT(ump);
13919f988b79SJean-Baptiste Boric KASSERT(ump->logical_vol);
13929f988b79SJean-Baptiste Boric KASSERT(priv == NULL);
13939f988b79SJean-Baptiste Boric
13949f988b79SJean-Baptiste Boric lb_size = udf_rw32(ump->logical_vol->lb_size);
13959f988b79SJean-Baptiste Boric blobsize = ump->packet_size * lb_size;
13969f988b79SJean-Baptiste Boric KASSERT(lb_size > 0);
13979f988b79SJean-Baptiste Boric KASSERT(ump->packet_size <= 64);
13989f988b79SJean-Baptiste Boric
13999f988b79SJean-Baptiste Boric /* initialise our memory space */
14009f988b79SJean-Baptiste Boric ump->strategy_private = malloc(sizeof(struct strat_private),
14019f988b79SJean-Baptiste Boric M_UDFTEMP, M_WAITOK);
14029f988b79SJean-Baptiste Boric priv = ump->strategy_private;
14039f988b79SJean-Baptiste Boric memset(priv, 0 , sizeof(struct strat_private));
14049f988b79SJean-Baptiste Boric
14059f988b79SJean-Baptiste Boric /* initialise locks */
14069f988b79SJean-Baptiste Boric cv_init(&priv->discstrat_cv, "udfstrat");
14079f988b79SJean-Baptiste Boric mutex_init(&priv->discstrat_mutex, MUTEX_DEFAULT, IPL_NONE);
14089f988b79SJean-Baptiste Boric mutex_init(&priv->seqwrite_mutex, MUTEX_DEFAULT, IPL_NONE);
14099f988b79SJean-Baptiste Boric
14109f988b79SJean-Baptiste Boric /* initialise struct eccline pool */
14119f988b79SJean-Baptiste Boric pool_init(&priv->eccline_pool, sizeof(struct udf_eccline),
14129f988b79SJean-Baptiste Boric 0, 0, 0, "udf_eccline_pool", NULL, IPL_NONE);
14139f988b79SJean-Baptiste Boric
14149f988b79SJean-Baptiste Boric /* initialise eccline blob pool */
14159f988b79SJean-Baptiste Boric ecclinepool_allocator.pa_pagesz = blobsize;
14169f988b79SJean-Baptiste Boric pool_init(&priv->ecclineblob_pool, blobsize,
14179f988b79SJean-Baptiste Boric 0, 0, 0, "udf_eccline_blob", &ecclinepool_allocator, IPL_NONE);
14189f988b79SJean-Baptiste Boric
14199f988b79SJean-Baptiste Boric /* initialise main queues */
14209f988b79SJean-Baptiste Boric for (i = 0; i < UDF_SHED_MAX; i++) {
14219f988b79SJean-Baptiste Boric priv->num_queued[i] = 0;
14229f988b79SJean-Baptiste Boric vfs_timestamp(&priv->last_queued[i]);
14239f988b79SJean-Baptiste Boric }
14249f988b79SJean-Baptiste Boric bufq_alloc(&priv->queues[UDF_SHED_WAITING], "fcfs",
14259f988b79SJean-Baptiste Boric BUFQ_SORT_RAWBLOCK);
14269f988b79SJean-Baptiste Boric bufq_alloc(&priv->queues[UDF_SHED_READING], "disksort",
14279f988b79SJean-Baptiste Boric BUFQ_SORT_RAWBLOCK);
14289f988b79SJean-Baptiste Boric bufq_alloc(&priv->queues[UDF_SHED_WRITING], "disksort",
14299f988b79SJean-Baptiste Boric BUFQ_SORT_RAWBLOCK);
14309f988b79SJean-Baptiste Boric bufq_alloc(&priv->queues[UDF_SHED_SEQWRITING], "disksort", 0);
14319f988b79SJean-Baptiste Boric
14329f988b79SJean-Baptiste Boric /* initialise administrative queues */
14339f988b79SJean-Baptiste Boric bufq_alloc(&priv->queues[UDF_SHED_IDLE], "fcfs", 0);
14349f988b79SJean-Baptiste Boric bufq_alloc(&priv->queues[UDF_SHED_FREE], "fcfs", 0);
14359f988b79SJean-Baptiste Boric
14369f988b79SJean-Baptiste Boric for (hashline = 0; hashline < UDF_ECCBUF_HASHSIZE; hashline++) {
14379f988b79SJean-Baptiste Boric LIST_INIT(&priv->eccline_hash[hashline]);
14389f988b79SJean-Baptiste Boric }
14399f988b79SJean-Baptiste Boric
14409f988b79SJean-Baptiste Boric /* create our disk strategy thread */
14419f988b79SJean-Baptiste Boric priv->cur_queue = UDF_SHED_READING;
14429f988b79SJean-Baptiste Boric priv->thread_finished = 0;
14439f988b79SJean-Baptiste Boric priv->thread_running = 0;
14449f988b79SJean-Baptiste Boric priv->run_thread = 1;
14459f988b79SJean-Baptiste Boric if (kthread_create(PRI_NONE, 0 /* KTHREAD_MPSAFE*/, NULL /* cpu_info*/,
14469f988b79SJean-Baptiste Boric udf_discstrat_thread, ump, &priv->queue_lwp,
14479f988b79SJean-Baptiste Boric "%s", "udf_rw")) {
14489f988b79SJean-Baptiste Boric panic("fork udf_rw");
14499f988b79SJean-Baptiste Boric }
14509f988b79SJean-Baptiste Boric
14519f988b79SJean-Baptiste Boric /* wait for thread to spin up */
14529f988b79SJean-Baptiste Boric while (!priv->thread_running) {
14539f988b79SJean-Baptiste Boric tsleep(&priv->thread_running, PRIBIO+1, "udfshedstart", hz);
14549f988b79SJean-Baptiste Boric }
14559f988b79SJean-Baptiste Boric }
14569f988b79SJean-Baptiste Boric
14579f988b79SJean-Baptiste Boric
14589f988b79SJean-Baptiste Boric static void
udf_discstrat_finish_rmw(struct udf_strat_args * args)14599f988b79SJean-Baptiste Boric udf_discstrat_finish_rmw(struct udf_strat_args *args)
14609f988b79SJean-Baptiste Boric {
14619f988b79SJean-Baptiste Boric struct udf_mount *ump = args->ump;
14629f988b79SJean-Baptiste Boric struct strat_private *priv = PRIV(ump);
14639f988b79SJean-Baptiste Boric
14649f988b79SJean-Baptiste Boric if (ump == NULL)
14659f988b79SJean-Baptiste Boric return;
14669f988b79SJean-Baptiste Boric
14679f988b79SJean-Baptiste Boric /* stop our sheduling thread */
14689f988b79SJean-Baptiste Boric KASSERT(priv->run_thread == 1);
14699f988b79SJean-Baptiste Boric priv->run_thread = 0;
14709f988b79SJean-Baptiste Boric wakeup(priv->queue_lwp);
14719f988b79SJean-Baptiste Boric while (!priv->thread_finished) {
14729f988b79SJean-Baptiste Boric tsleep(&priv->run_thread, PRIBIO + 1, "udfshedfin", hz);
14739f988b79SJean-Baptiste Boric }
14749f988b79SJean-Baptiste Boric /* kthread should be finished now */
14759f988b79SJean-Baptiste Boric
14769f988b79SJean-Baptiste Boric /* cleanup our pools */
14779f988b79SJean-Baptiste Boric pool_destroy(&priv->eccline_pool);
14789f988b79SJean-Baptiste Boric pool_destroy(&priv->ecclineblob_pool);
14799f988b79SJean-Baptiste Boric
14809f988b79SJean-Baptiste Boric cv_destroy(&priv->discstrat_cv);
14819f988b79SJean-Baptiste Boric mutex_destroy(&priv->discstrat_mutex);
14829f988b79SJean-Baptiste Boric mutex_destroy(&priv->seqwrite_mutex);
14839f988b79SJean-Baptiste Boric
14849f988b79SJean-Baptiste Boric /* free our private space */
14859f988b79SJean-Baptiste Boric free(ump->strategy_private, M_UDFTEMP);
14869f988b79SJean-Baptiste Boric ump->strategy_private = NULL;
14879f988b79SJean-Baptiste Boric }
14889f988b79SJean-Baptiste Boric
14899f988b79SJean-Baptiste Boric /* --------------------------------------------------------------------- */
14909f988b79SJean-Baptiste Boric
14919f988b79SJean-Baptiste Boric struct udf_strategy udf_strat_rmw =
14929f988b79SJean-Baptiste Boric {
14939f988b79SJean-Baptiste Boric udf_create_nodedscr_rmw,
14949f988b79SJean-Baptiste Boric udf_free_nodedscr_rmw,
14959f988b79SJean-Baptiste Boric udf_read_nodedscr_rmw,
14969f988b79SJean-Baptiste Boric udf_write_nodedscr_rmw,
14979f988b79SJean-Baptiste Boric udf_queuebuf_rmw,
14989f988b79SJean-Baptiste Boric udf_discstrat_init_rmw,
14999f988b79SJean-Baptiste Boric udf_discstrat_finish_rmw
15009f988b79SJean-Baptiste Boric };
15019f988b79SJean-Baptiste Boric
1502