etc/protocols - sync with NetBSD-8
[minix.git] / sys / fs / udf / udf_strat_direct.c
blob251efb7271151cbf176b81f8ba60ff8587f56c7c
1 /* $NetBSD: udf_strat_direct.c,v 1.13 2015/10/06 08:57:34 hannken Exp $ */
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.
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.
29 #include <sys/cdefs.h>
30 #ifndef lint
31 __KERNEL_RCSID(0, "$NetBSD: udf_strat_direct.c,v 1.13 2015/10/06 08:57:34 hannken Exp $");
32 #endif /* not lint */
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_netbsd.h"
37 #endif
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sysctl.h>
42 #include <sys/namei.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <miscfs/genfs/genfs_node.h>
47 #include <sys/mount.h>
48 #include <sys/buf.h>
49 #include <sys/file.h>
50 #include <sys/device.h>
51 #include <sys/disklabel.h>
52 #include <sys/ioctl.h>
53 #include <sys/malloc.h>
54 #include <sys/dirent.h>
55 #include <sys/stat.h>
56 #include <sys/conf.h>
57 #include <sys/kauth.h>
58 #include <sys/kthread.h>
59 #include <dev/clock_subr.h>
61 #include <fs/udf/ecma167-udf.h>
62 #include <fs/udf/udf_mount.h>
64 #include "udf.h"
65 #include "udf_subr.h"
66 #include "udf_bswap.h"
69 #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
70 #define PRIV(ump) ((struct strat_private *) ump->strategy_private)
72 /* --------------------------------------------------------------------- */
74 /* BUFQ's */
75 #define UDF_SHED_MAX 3
77 #define UDF_SHED_READING 0
78 #define UDF_SHED_WRITING 1
79 #define UDF_SHED_SEQWRITING 2
82 struct strat_private {
83 struct pool desc_pool; /* node descriptors */
86 /* --------------------------------------------------------------------- */
88 static void
89 udf_wr_nodedscr_callback(struct buf *buf)
91 struct udf_node *udf_node;
93 KASSERT(buf);
94 KASSERT(buf->b_data);
96 /* called when write action is done */
97 DPRINTF(WRITE, ("udf_wr_nodedscr_callback(): node written out\n"));
99 udf_node = VTOI(buf->b_vp);
100 if (udf_node == NULL) {
101 putiobuf(buf);
102 printf("udf_wr_node_callback: NULL node?\n");
103 return;
106 /* XXX right flags to mark dirty again on error? */
107 if (buf->b_error) {
108 /* write error on `defect free' media??? how to solve? */
109 /* XXX lookup UDF standard for unallocatable space */
110 udf_node->i_flags |= IN_MODIFIED | IN_ACCESSED;
113 /* decrement outstanding_nodedscr */
114 KASSERT(udf_node->outstanding_nodedscr >= 1);
115 udf_node->outstanding_nodedscr--;
116 if (udf_node->outstanding_nodedscr == 0) {
117 /* unlock the node */
118 UDF_UNLOCK_NODE(udf_node, 0);
119 wakeup(&udf_node->outstanding_nodedscr);
122 putiobuf(buf);
125 /* --------------------------------------------------------------------- */
127 static int
128 udf_getblank_nodedscr_direct(struct udf_strat_args *args)
130 union dscrptr **dscrptr = &args->dscr;
131 struct udf_mount *ump = args->ump;
132 struct strat_private *priv = PRIV(ump);
133 uint32_t lb_size;
135 lb_size = udf_rw32(ump->logical_vol->lb_size);
136 *dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
137 memset(*dscrptr, 0, lb_size);
139 return 0;
143 static void
144 udf_free_nodedscr_direct(struct udf_strat_args *args)
146 union dscrptr *dscr = args->dscr;
147 struct udf_mount *ump = args->ump;
148 struct strat_private *priv = PRIV(ump);
150 pool_put(&priv->desc_pool, dscr);
154 static int
155 udf_read_nodedscr_direct(struct udf_strat_args *args)
157 union dscrptr **dscrptr = &args->dscr;
158 union dscrptr *tmpdscr;
159 struct udf_mount *ump = args->ump;
160 struct long_ad *icb = args->icb;
161 struct strat_private *priv = PRIV(ump);
162 uint32_t lb_size;
163 uint32_t sector, dummy;
164 int error;
166 lb_size = udf_rw32(ump->logical_vol->lb_size);
168 error = udf_translate_vtop(ump, icb, &sector, &dummy);
169 if (error)
170 return error;
172 /* try to read in fe/efe */
173 error = udf_read_phys_dscr(ump, sector, M_UDFTEMP, &tmpdscr);
174 if (error)
175 return error;
177 *dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
178 memcpy(*dscrptr, tmpdscr, lb_size);
179 free(tmpdscr, M_UDFTEMP);
181 return 0;
185 static int
186 udf_write_nodedscr_direct(struct udf_strat_args *args)
188 struct udf_mount *ump = args->ump;
189 struct udf_node *udf_node = args->udf_node;
190 union dscrptr *dscr = args->dscr;
191 struct long_ad *icb = args->icb;
192 int waitfor = args->waitfor;
193 uint32_t logsector, sector, dummy;
194 int error, vpart __diagused;
197 * we have to decide if we write it out sequential or at its fixed
198 * position by examining the partition its (to be) written on.
200 vpart = udf_rw16(udf_node->loc.loc.part_num);
201 logsector = udf_rw32(icb->loc.lb_num);
202 KASSERT(ump->vtop_tp[vpart] != UDF_VTOP_TYPE_VIRT);
204 sector = 0;
205 error = udf_translate_vtop(ump, icb, &sector, &dummy);
206 if (error)
207 goto out;
209 if (waitfor) {
210 DPRINTF(WRITE, ("udf_write_nodedscr: sync write\n"));
212 error = udf_write_phys_dscr_sync(ump, udf_node, UDF_C_NODE,
213 dscr, sector, logsector);
214 } else {
215 DPRINTF(WRITE, ("udf_write_nodedscr: no wait, async write\n"));
217 error = udf_write_phys_dscr_async(ump, udf_node, UDF_C_NODE,
218 dscr, sector, logsector, udf_wr_nodedscr_callback);
219 /* will be UNLOCKED in call back */
220 return error;
222 out:
223 udf_node->outstanding_nodedscr--;
224 if (udf_node->outstanding_nodedscr == 0) {
225 UDF_UNLOCK_NODE(udf_node, 0);
226 wakeup(&udf_node->outstanding_nodedscr);
229 return error;
232 /* --------------------------------------------------------------------- */
234 static void
235 udf_queue_buf_direct(struct udf_strat_args *args)
237 struct udf_mount *ump = args->ump;
238 struct buf *buf = args->nestbuf;
239 struct buf *nestbuf;
240 struct desc_tag *tag;
241 struct long_ad *node_ad_cpy;
242 uint64_t *lmapping, *pmapping, *lmappos, run_start;
243 uint32_t sectornr;
244 uint32_t buf_offset, rbuflen, bpos;
245 uint16_t vpart_num;
246 uint8_t *fidblk;
247 off_t rblk;
248 int sector_size = ump->discinfo.sector_size;
249 int len, buf_len, sector, sectors, run_length;
250 int blks = sector_size / DEV_BSIZE;
251 int what, class __diagused, queue;
253 KASSERT(ump);
254 KASSERT(buf);
255 KASSERT(buf->b_iodone == nestiobuf_iodone);
257 what = buf->b_udf_c_type;
258 queue = UDF_SHED_READING;
259 if ((buf->b_flags & B_READ) == 0) {
260 /* writing */
261 queue = UDF_SHED_SEQWRITING;
262 if (what == UDF_C_ABSOLUTE)
263 queue = UDF_SHED_WRITING;
264 if (what == UDF_C_DSCR)
265 queue = UDF_SHED_WRITING;
266 if (what == UDF_C_NODE)
267 queue = UDF_SHED_WRITING;
270 /* use disc sheduler */
271 class = ump->discinfo.mmc_class;
272 KASSERT((class == MMC_CLASS_UNKN) || (class == MMC_CLASS_DISC) ||
273 (ump->discinfo.mmc_cur & MMC_CAP_HW_DEFECTFREE) ||
274 (ump->vfs_mountp->mnt_flag & MNT_RDONLY));
276 #ifndef UDF_DEBUG
277 __USE(blks);
278 #endif
279 if (queue == UDF_SHED_READING) {
280 DPRINTF(SHEDULE, ("\nudf_issue_buf READ %p : sector %d type %d,"
281 "b_resid %d, b_bcount %d, b_bufsize %d\n",
282 buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
283 buf->b_resid, buf->b_bcount, buf->b_bufsize));
284 VOP_STRATEGY(ump->devvp, buf);
285 return;
289 if (queue == UDF_SHED_WRITING) {
290 DPRINTF(SHEDULE, ("\nudf_issue_buf WRITE %p : sector %d "
291 "type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
292 buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
293 buf->b_resid, buf->b_bcount, buf->b_bufsize));
294 KASSERT(buf->b_udf_c_type == UDF_C_DSCR ||
295 buf->b_udf_c_type == UDF_C_ABSOLUTE ||
296 buf->b_udf_c_type == UDF_C_NODE);
297 udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
298 VOP_STRATEGY(ump->devvp, buf);
299 return;
302 /* UDF_SHED_SEQWRITING */
303 KASSERT(queue == UDF_SHED_SEQWRITING);
304 DPRINTF(SHEDULE, ("\nudf_issue_buf SEQWRITE %p : sector XXXX "
305 "type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
306 buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount,
307 buf->b_bufsize));
310 * Buffers should not have been allocated to disc addresses yet on
311 * this queue. Note that a buffer can get multiple extents allocated.
313 * lmapping contains lb_num relative to base partition.
315 lmapping = ump->la_lmapping;
316 node_ad_cpy = ump->la_node_ad_cpy;
318 /* logically allocate buf and map it in the file */
319 udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num);
321 /* if we have FIDs, fixup using the new allocation table */
322 if (buf->b_udf_c_type == UDF_C_FIDS) {
323 buf_len = buf->b_bcount;
324 bpos = 0;
325 lmappos = lmapping;
326 while (buf_len) {
327 sectornr = *lmappos++;
328 len = MIN(buf_len, sector_size);
329 fidblk = (uint8_t *) buf->b_data + bpos;
330 udf_fixup_fid_block(fidblk, sector_size,
331 0, len, sectornr);
332 bpos += len;
333 buf_len -= len;
336 if (buf->b_udf_c_type == UDF_C_METADATA_SBM) {
337 if (buf->b_lblkno == 0) {
338 /* update the tag location inside */
339 tag = (struct desc_tag *) buf->b_data;
340 tag->tag_loc = udf_rw32(*lmapping);
341 udf_validate_tag_and_crc_sums(buf->b_data);
344 udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
347 * Translate new mappings in lmapping to pmappings and try to
348 * conglomerate extents to reduce the number of writes.
350 * pmapping to contain lb_nums as used for disc adressing.
352 pmapping = ump->la_pmapping;
353 sectors = (buf->b_bcount + sector_size -1) / sector_size;
354 udf_translate_vtop_list(ump, sectors, vpart_num, lmapping, pmapping);
356 for (sector = 0; sector < sectors; sector++) {
357 buf_offset = sector * sector_size;
358 DPRINTF(WRITE, ("\tprocessing rel sector %d\n", sector));
360 DPRINTF(WRITE, ("\tissue write sector %"PRIu64"\n",
361 pmapping[sector]));
363 run_start = pmapping[sector];
364 run_length = 1;
365 while (sector < sectors-1) {
366 if (pmapping[sector+1] != pmapping[sector]+1)
367 break;
368 run_length++;
369 sector++;
372 /* nest an iobuf for the extent */
373 rbuflen = run_length * sector_size;
374 rblk = run_start * (sector_size/DEV_BSIZE);
376 nestbuf = getiobuf(NULL, true);
377 nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
378 /* nestbuf is B_ASYNC */
380 /* identify this nestbuf */
381 nestbuf->b_lblkno = sector;
382 assert(nestbuf->b_vp == buf->b_vp);
384 /* CD shedules on raw blkno */
385 nestbuf->b_blkno = rblk;
386 nestbuf->b_proc = NULL;
387 nestbuf->b_rawblkno = rblk;
388 nestbuf->b_udf_c_type = UDF_C_PROCESSED;
390 VOP_STRATEGY(ump->devvp, nestbuf);
395 static void
396 udf_discstrat_init_direct(struct udf_strat_args *args)
398 struct udf_mount *ump = args->ump;
399 struct strat_private *priv = PRIV(ump);
400 uint32_t lb_size;
402 KASSERT(priv == NULL);
403 ump->strategy_private = malloc(sizeof(struct strat_private),
404 M_UDFTEMP, M_WAITOK);
405 priv = ump->strategy_private;
406 memset(priv, 0 , sizeof(struct strat_private));
409 * Initialise pool for descriptors associated with nodes. This is done
410 * in lb_size units though currently lb_size is dictated to be
411 * sector_size.
413 memset(&priv->desc_pool, 0, sizeof(struct pool));
415 lb_size = udf_rw32(ump->logical_vol->lb_size);
416 pool_init(&priv->desc_pool, lb_size, 0, 0, 0, "udf_desc_pool", NULL,
417 IPL_NONE);
421 static void
422 udf_discstrat_finish_direct(struct udf_strat_args *args)
424 struct udf_mount *ump = args->ump;
425 struct strat_private *priv = PRIV(ump);
427 /* destroy our pool */
428 pool_destroy(&priv->desc_pool);
430 /* free our private space */
431 free(ump->strategy_private, M_UDFTEMP);
432 ump->strategy_private = NULL;
435 /* --------------------------------------------------------------------- */
437 struct udf_strategy udf_strat_direct =
439 udf_getblank_nodedscr_direct,
440 udf_free_nodedscr_direct,
441 udf_read_nodedscr_direct,
442 udf_write_nodedscr_direct,
443 udf_queue_buf_direct,
444 udf_discstrat_init_direct,
445 udf_discstrat_finish_direct