udf_strat_direct.c revision 1.1.12.2 1 /* $NetBSD: udf_strat_direct.c,v 1.1.12.2 2008/12/13 01:14:59 haad Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2008 Reinoud Zandijk
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __KERNEL_RCSID(0, "$NetBSD: udf_strat_direct.c,v 1.1.12.2 2008/12/13 01:14:59 haad Exp $");
32 #endif /* not lint */
33
34
35 #if defined(_KERNEL_OPT)
36 #include "opt_quota.h"
37 #include "opt_compat_netbsd.h"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysctl.h>
43 #include <sys/namei.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <miscfs/genfs/genfs_node.h>
48 #include <sys/mount.h>
49 #include <sys/buf.h>
50 #include <sys/file.h>
51 #include <sys/device.h>
52 #include <sys/disklabel.h>
53 #include <sys/ioctl.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/stat.h>
57 #include <sys/conf.h>
58 #include <sys/kauth.h>
59 #include <sys/kthread.h>
60 #include <dev/clock_subr.h>
61
62 #include <fs/udf/ecma167-udf.h>
63 #include <fs/udf/udf_mount.h>
64
65 #include "udf.h"
66 #include "udf_subr.h"
67 #include "udf_bswap.h"
68
69
70 #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
71 #define PRIV(ump) ((struct strat_private *) ump->strategy_private)
72
73 /* --------------------------------------------------------------------- */
74
75 /* BUFQ's */
76 #define UDF_SHED_MAX 3
77
78 #define UDF_SHED_READING 0
79 #define UDF_SHED_WRITING 1
80 #define UDF_SHED_SEQWRITING 2
81
82
83 struct strat_private {
84 struct pool desc_pool; /* node descriptors */
85 };
86
87 /* --------------------------------------------------------------------- */
88
89 static void
90 udf_wr_nodedscr_callback(struct buf *buf)
91 {
92 struct udf_node *udf_node;
93
94 KASSERT(buf);
95 KASSERT(buf->b_data);
96
97 /* called when write action is done */
98 DPRINTF(WRITE, ("udf_wr_nodedscr_callback(): node written out\n"));
99
100 udf_node = VTOI(buf->b_vp);
101 if (udf_node == NULL) {
102 putiobuf(buf);
103 printf("udf_wr_node_callback: NULL node?\n");
104 return;
105 }
106
107 /* XXX right flags to mark dirty again on error? */
108 if (buf->b_error) {
109 /* write error on `defect free' media??? how to solve? */
110 /* XXX lookup UDF standard for unallocatable space */
111 udf_node->i_flags |= IN_MODIFIED | IN_ACCESSED;
112 }
113
114 /* decrement outstanding_nodedscr */
115 KASSERT(udf_node->outstanding_nodedscr >= 1);
116 udf_node->outstanding_nodedscr--;
117 if (udf_node->outstanding_nodedscr == 0) {
118 /* unlock the node */
119 KASSERT(udf_node->i_flags & IN_CALLBACK_ULK);
120 UDF_UNLOCK_NODE(udf_node, IN_CALLBACK_ULK);
121
122 wakeup(&udf_node->outstanding_nodedscr);
123 }
124 /* unreference the vnode so it can be recycled */
125 holdrele(udf_node->vnode);
126
127 putiobuf(buf);
128 }
129
130 /* --------------------------------------------------------------------- */
131
132 static int
133 udf_getblank_nodedscr_direct(struct udf_strat_args *args)
134 {
135 union dscrptr **dscrptr = &args->dscr;
136 struct udf_mount *ump = args->ump;
137 struct strat_private *priv = PRIV(ump);
138 uint32_t lb_size;
139
140 lb_size = udf_rw32(ump->logical_vol->lb_size);
141 *dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
142 memset(*dscrptr, 0, lb_size);
143
144 return 0;
145 }
146
147
148 static void
149 udf_free_nodedscr_direct(struct udf_strat_args *args)
150 {
151 union dscrptr *dscr = args->dscr;
152 struct udf_mount *ump = args->ump;
153 struct strat_private *priv = PRIV(ump);
154
155 pool_put(&priv->desc_pool, dscr);
156 }
157
158
159 static int
160 udf_read_nodedscr_direct(struct udf_strat_args *args)
161 {
162 union dscrptr **dscrptr = &args->dscr;
163 union dscrptr *tmpdscr;
164 struct udf_mount *ump = args->ump;
165 struct long_ad *icb = args->icb;
166 struct strat_private *priv = PRIV(ump);
167 uint32_t lb_size;
168 uint32_t sector, dummy;
169 int error;
170
171 lb_size = udf_rw32(ump->logical_vol->lb_size);
172
173 error = udf_translate_vtop(ump, icb, §or, &dummy);
174 if (error)
175 return error;
176
177 /* try to read in fe/efe */
178 error = udf_read_phys_dscr(ump, sector, M_UDFTEMP, &tmpdscr);
179 if (error)
180 return error;
181
182 *dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
183 memcpy(*dscrptr, tmpdscr, lb_size);
184 free(tmpdscr, M_UDFTEMP);
185
186 return 0;
187 }
188
189
190 static int
191 udf_write_nodedscr_direct(struct udf_strat_args *args)
192 {
193 struct udf_mount *ump = args->ump;
194 struct udf_node *udf_node = args->udf_node;
195 union dscrptr *dscr = args->dscr;
196 struct long_ad *icb = args->icb;
197 int waitfor = args->waitfor;
198 uint32_t logsector, sector, dummy;
199 int error, vpart;
200
201 /*
202 * we have to decide if we write it out sequential or at its fixed
203 * position by examining the partition its (to be) written on.
204 */
205 vpart = udf_rw16(udf_node->loc.loc.part_num);
206 logsector = udf_rw32(icb->loc.lb_num);
207 KASSERT(ump->vtop_tp[vpart] != UDF_VTOP_TYPE_VIRT);
208
209 sector = 0;
210 error = udf_translate_vtop(ump, icb, §or, &dummy);
211 if (error)
212 goto out;
213
214 /* add reference to the vnode to prevent recycling */
215 vhold(udf_node->vnode);
216
217 if (waitfor) {
218 DPRINTF(WRITE, ("udf_write_nodedscr: sync write\n"));
219
220 error = udf_write_phys_dscr_sync(ump, udf_node, UDF_C_NODE,
221 dscr, sector, logsector);
222 } else {
223 DPRINTF(WRITE, ("udf_write_nodedscr: no wait, async write\n"));
224
225 error = udf_write_phys_dscr_async(ump, udf_node, UDF_C_NODE,
226 dscr, sector, logsector, udf_wr_nodedscr_callback);
227 /* will be UNLOCKED in call back */
228 return error;
229 }
230
231 holdrele(udf_node->vnode);
232 out:
233 udf_node->outstanding_nodedscr--;
234 if (udf_node->outstanding_nodedscr == 0) {
235 UDF_UNLOCK_NODE(udf_node, 0);
236 wakeup(&udf_node->outstanding_nodedscr);
237 }
238
239 return error;
240 }
241
242 /* --------------------------------------------------------------------- */
243
244 static void
245 udf_queue_buf_direct(struct udf_strat_args *args)
246 {
247 struct udf_mount *ump = args->ump;
248 struct buf *buf = args->nestbuf;
249 struct buf *nestbuf;
250 struct desc_tag *tag;
251 struct long_ad *node_ad_cpy;
252 uint64_t *lmapping, *pmapping, *lmappos, blknr, run_start;
253 uint32_t our_sectornr, sectornr;
254 uint32_t lb_size, buf_offset, rbuflen, bpos;
255 uint16_t vpart_num;
256 uint8_t *fidblk;
257 off_t rblk;
258 int sector_size = ump->discinfo.sector_size;
259 int blks = sector_size / DEV_BSIZE;
260 int len, buf_len, sector, sectors, run_length;
261 int what, class, queue;
262
263 KASSERT(ump);
264 KASSERT(buf);
265 KASSERT(buf->b_iodone == nestiobuf_iodone);
266
267 what = buf->b_udf_c_type;
268 queue = UDF_SHED_READING;
269 if ((buf->b_flags & B_READ) == 0) {
270 /* writing */
271 queue = UDF_SHED_SEQWRITING;
272 if (what == UDF_C_DSCR)
273 queue = UDF_SHED_WRITING;
274 if (what == UDF_C_NODE)
275 queue = UDF_SHED_WRITING;
276 }
277
278 /* use disc sheduler */
279 class = ump->discinfo.mmc_class;
280 KASSERT((class == MMC_CLASS_UNKN) || (class == MMC_CLASS_DISC) ||
281 (ump->discinfo.mmc_cur & MMC_CAP_HW_DEFECTFREE) ||
282 (ump->vfs_mountp->mnt_flag & MNT_RDONLY));
283
284 if (queue == UDF_SHED_READING) {
285 DPRINTF(SHEDULE, ("\nudf_issue_buf READ %p : sector %d type %d,"
286 "b_resid %d, b_bcount %d, b_bufsize %d\n",
287 buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
288 buf->b_resid, buf->b_bcount, buf->b_bufsize));
289 VOP_STRATEGY(ump->devvp, buf);
290 return;
291 }
292
293 /* (sectorsize == lb_size) for UDF */
294 lb_size = udf_rw32(ump->logical_vol->lb_size);
295 blknr = buf->b_blkno;
296 our_sectornr = blknr / blks;
297
298 if (queue == UDF_SHED_WRITING) {
299 DPRINTF(SHEDULE, ("\nudf_issue_buf WRITE %p : sector %d "
300 "type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
301 buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
302 buf->b_resid, buf->b_bcount, buf->b_bufsize));
303 /* if we have FIDs fixup using buffer's sector number(s) */
304 if (buf->b_udf_c_type == UDF_C_FIDS) {
305 panic("UDF_C_FIDS in SHED_WRITING!\n");
306 buf_len = buf->b_bcount;
307 sectornr = our_sectornr;
308 bpos = 0;
309 while (buf_len) {
310 len = MIN(buf_len, sector_size);
311 fidblk = (uint8_t *) buf->b_data + bpos;
312 udf_fixup_fid_block(fidblk, sector_size,
313 0, len, sectornr);
314 sectornr++;
315 bpos += len;
316 buf_len -= len;
317 }
318 }
319 udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
320 VOP_STRATEGY(ump->devvp, buf);
321 return;
322 }
323
324 /* UDF_SHED_SEQWRITING */
325 KASSERT(queue == UDF_SHED_SEQWRITING);
326 DPRINTF(SHEDULE, ("\nudf_issue_buf SEQWRITE %p : sector XXXX "
327 "type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
328 buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount,
329 buf->b_bufsize));
330
331 /*
332 * Buffers should not have been allocated to disc addresses yet on
333 * this queue. Note that a buffer can get multiple extents allocated.
334 *
335 * lmapping contains lb_num relative to base partition.
336 */
337 lmapping = ump->la_lmapping;
338 node_ad_cpy = ump->la_node_ad_cpy;
339
340 /* logically allocate buf and map it in the file */
341 udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num);
342
343 /* if we have FIDs, fixup using the new allocation table */
344 if (buf->b_udf_c_type == UDF_C_FIDS) {
345 buf_len = buf->b_bcount;
346 bpos = 0;
347 lmappos = lmapping;
348 while (buf_len) {
349 sectornr = *lmappos++;
350 len = MIN(buf_len, sector_size);
351 fidblk = (uint8_t *) buf->b_data + bpos;
352 udf_fixup_fid_block(fidblk, sector_size,
353 0, len, sectornr);
354 bpos += len;
355 buf_len -= len;
356 }
357 }
358 if (buf->b_udf_c_type == UDF_C_METADATA_SBM) {
359 if (buf->b_lblkno == 0) {
360 /* update the tag location inside */
361 tag = (struct desc_tag *) buf->b_data;
362 tag->tag_loc = udf_rw32(*lmapping);
363 udf_validate_tag_and_crc_sums(buf->b_data);
364 }
365 }
366 udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
367
368 /*
369 * Translate new mappings in lmapping to pmappings and try to
370 * conglomerate extents to reduce the number of writes.
371 *
372 * pmapping to contain lb_nums as used for disc adressing.
373 */
374 pmapping = ump->la_pmapping;
375 sectors = (buf->b_bcount + sector_size -1) / sector_size;
376 udf_translate_vtop_list(ump, sectors, vpart_num, lmapping, pmapping);
377
378 for (sector = 0; sector < sectors; sector++) {
379 buf_offset = sector * sector_size;
380 DPRINTF(WRITE, ("\tprocessing rel sector %d\n", sector));
381
382 DPRINTF(WRITE, ("\tissue write sector %"PRIu64"\n",
383 pmapping[sector]));
384
385 run_start = pmapping[sector];
386 run_length = 1;
387 while (sector < sectors-1) {
388 if (pmapping[sector+1] != pmapping[sector]+1)
389 break;
390 run_length++;
391 sector++;
392 }
393
394 /* nest an iobuf for the extent */
395 rbuflen = run_length * sector_size;
396 rblk = run_start * (sector_size/DEV_BSIZE);
397
398 nestbuf = getiobuf(NULL, true);
399 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
400 /* nestbuf is B_ASYNC */
401
402 /* identify this nestbuf */
403 nestbuf->b_lblkno = sector;
404 assert(nestbuf->b_vp == buf->b_vp);
405
406 /* CD shedules on raw blkno */
407 nestbuf->b_blkno = rblk;
408 nestbuf->b_proc = NULL;
409 nestbuf->b_rawblkno = rblk;
410 nestbuf->b_udf_c_type = UDF_C_PROCESSED;
411
412 VOP_STRATEGY(ump->devvp, nestbuf);
413 }
414 }
415
416
417 static void
418 udf_discstrat_init_direct(struct udf_strat_args *args)
419 {
420 struct udf_mount *ump = args->ump;
421 struct strat_private *priv = PRIV(ump);
422 uint32_t lb_size;
423
424 KASSERT(priv == NULL);
425 ump->strategy_private = malloc(sizeof(struct strat_private),
426 M_UDFTEMP, M_WAITOK);
427 priv = ump->strategy_private;
428 memset(priv, 0 , sizeof(struct strat_private));
429
430 /*
431 * Initialise pool for descriptors associated with nodes. This is done
432 * in lb_size units though currently lb_size is dictated to be
433 * sector_size.
434 */
435 memset(&priv->desc_pool, 0, sizeof(struct pool));
436
437 lb_size = udf_rw32(ump->logical_vol->lb_size);
438 pool_init(&priv->desc_pool, lb_size, 0, 0, 0, "udf_desc_pool", NULL,
439 IPL_NONE);
440 }
441
442
443 static void
444 udf_discstrat_finish_direct(struct udf_strat_args *args)
445 {
446 struct udf_mount *ump = args->ump;
447 struct strat_private *priv = PRIV(ump);
448
449 /* destroy our pool */
450 pool_destroy(&priv->desc_pool);
451
452 /* free our private space */
453 free(ump->strategy_private, M_UDFTEMP);
454 ump->strategy_private = NULL;
455 }
456
457 /* --------------------------------------------------------------------- */
458
459 struct udf_strategy udf_strat_direct =
460 {
461 udf_getblank_nodedscr_direct,
462 udf_free_nodedscr_direct,
463 udf_read_nodedscr_direct,
464 udf_write_nodedscr_direct,
465 udf_queue_buf_direct,
466 udf_discstrat_init_direct,
467 udf_discstrat_finish_direct
468 };
469
470