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