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