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