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