xref: /netbsd-src/sys/fs/udf/udf_strat_direct.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /* $NetBSD: udf_strat_direct.c,v 1.7 2008/12/16 16:18:25 pooka 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.7 2008/12/16 16:18:25 pooka Exp $");
32 #endif /* not lint */
33 
34 
35 #if defined(_KERNEL_OPT)
36 #include "opt_compat_netbsd.h"
37 #endif
38 
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>
60 
61 #include <fs/udf/ecma167-udf.h>
62 #include <fs/udf/udf_mount.h>
63 
64 #include "udf.h"
65 #include "udf_subr.h"
66 #include "udf_bswap.h"
67 
68 
69 #define VTOI(vnode) ((struct udf_node *) vnode->v_data)
70 #define PRIV(ump) ((struct strat_private *) ump->strategy_private)
71 
72 /* --------------------------------------------------------------------- */
73 
74 /* BUFQ's */
75 #define UDF_SHED_MAX 3
76 
77 #define UDF_SHED_READING	0
78 #define UDF_SHED_WRITING	1
79 #define UDF_SHED_SEQWRITING	2
80 
81 
82 struct strat_private {
83 	struct pool		desc_pool;	 /* node descriptors */
84 };
85 
86 /* --------------------------------------------------------------------- */
87 
88 static void
89 udf_wr_nodedscr_callback(struct buf *buf)
90 {
91 	struct udf_node *udf_node;
92 
93 	KASSERT(buf);
94 	KASSERT(buf->b_data);
95 
96 	/* called when write action is done */
97 	DPRINTF(WRITE, ("udf_wr_nodedscr_callback(): node written out\n"));
98 
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;
104 	}
105 
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;
111 	}
112 
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 		KASSERT(udf_node->i_flags & IN_CALLBACK_ULK);
119 		UDF_UNLOCK_NODE(udf_node, IN_CALLBACK_ULK);
120 
121 		wakeup(&udf_node->outstanding_nodedscr);
122 	}
123 	/* unreference the vnode so it can be recycled */
124 	holdrele(udf_node->vnode);
125 
126 	putiobuf(buf);
127 }
128 
129 /* --------------------------------------------------------------------- */
130 
131 static int
132 udf_getblank_nodedscr_direct(struct udf_strat_args *args)
133 {
134 	union dscrptr   **dscrptr = &args->dscr;
135 	struct udf_mount *ump = args->ump;
136 	struct strat_private *priv = PRIV(ump);
137 	uint32_t lb_size;
138 
139 	lb_size = udf_rw32(ump->logical_vol->lb_size);
140 	*dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
141 	memset(*dscrptr, 0, lb_size);
142 
143 	return 0;
144 }
145 
146 
147 static void
148 udf_free_nodedscr_direct(struct udf_strat_args *args)
149 {
150 	union dscrptr    *dscr = args->dscr;
151 	struct udf_mount *ump  = args->ump;
152 	struct strat_private *priv = PRIV(ump);
153 
154 	pool_put(&priv->desc_pool, dscr);
155 }
156 
157 
158 static int
159 udf_read_nodedscr_direct(struct udf_strat_args *args)
160 {
161 	union dscrptr   **dscrptr = &args->dscr;
162 	union dscrptr    *tmpdscr;
163 	struct udf_mount *ump = args->ump;
164 	struct long_ad   *icb = args->icb;
165 	struct strat_private *priv = PRIV(ump);
166 	uint32_t lb_size;
167 	uint32_t sector, dummy;
168 	int error;
169 
170 	lb_size = udf_rw32(ump->logical_vol->lb_size);
171 
172 	error = udf_translate_vtop(ump, icb, &sector, &dummy);
173 	if (error)
174 		return error;
175 
176 	/* try to read in fe/efe */
177 	error = udf_read_phys_dscr(ump, sector, M_UDFTEMP, &tmpdscr);
178 	if (error)
179 		return error;
180 
181 	*dscrptr = pool_get(&priv->desc_pool, PR_WAITOK);
182 	memcpy(*dscrptr, tmpdscr, lb_size);
183 	free(tmpdscr, M_UDFTEMP);
184 
185 	return 0;
186 }
187 
188 
189 static int
190 udf_write_nodedscr_direct(struct udf_strat_args *args)
191 {
192 	struct udf_mount *ump      = args->ump;
193 	struct udf_node  *udf_node = args->udf_node;
194 	union dscrptr    *dscr     = args->dscr;
195 	struct long_ad   *icb      = args->icb;
196 	int               waitfor  = args->waitfor;
197 	uint32_t logsector, sector, dummy;
198 	int error, vpart;
199 
200 	/*
201 	 * we have to decide if we write it out sequential or at its fixed
202 	 * position by examining the partition its (to be) written on.
203 	 */
204 	vpart     = udf_rw16(udf_node->loc.loc.part_num);
205 	logsector = udf_rw32(icb->loc.lb_num);
206 	KASSERT(ump->vtop_tp[vpart] != UDF_VTOP_TYPE_VIRT);
207 
208 	sector = 0;
209 	error  = udf_translate_vtop(ump, icb, &sector, &dummy);
210 	if (error)
211 		goto out;
212 
213 	/* add reference to the vnode to prevent recycling */
214 	vhold(udf_node->vnode);
215 
216 	if (waitfor) {
217 		DPRINTF(WRITE, ("udf_write_nodedscr: sync write\n"));
218 
219 		error = udf_write_phys_dscr_sync(ump, udf_node, UDF_C_NODE,
220 			dscr, sector, logsector);
221 	} else {
222 		DPRINTF(WRITE, ("udf_write_nodedscr: no wait, async write\n"));
223 
224 		error = udf_write_phys_dscr_async(ump, udf_node, UDF_C_NODE,
225 			dscr, sector, logsector, udf_wr_nodedscr_callback);
226 		/* will be UNLOCKED in call back */
227 		return error;
228 	}
229 
230 	holdrele(udf_node->vnode);
231 out:
232 	udf_node->outstanding_nodedscr--;
233 	if (udf_node->outstanding_nodedscr == 0) {
234 		UDF_UNLOCK_NODE(udf_node, 0);
235 		wakeup(&udf_node->outstanding_nodedscr);
236 	}
237 
238 	return error;
239 }
240 
241 /* --------------------------------------------------------------------- */
242 
243 static void
244 udf_queue_buf_direct(struct udf_strat_args *args)
245 {
246 	struct udf_mount *ump = args->ump;
247 	struct buf *buf = args->nestbuf;
248 	struct buf *nestbuf;
249 	struct desc_tag *tag;
250 	struct long_ad *node_ad_cpy;
251 	uint64_t *lmapping, *pmapping, *lmappos, blknr, run_start;
252 	uint32_t our_sectornr, sectornr;
253 	uint32_t lb_size, buf_offset, rbuflen, bpos;
254 	uint16_t vpart_num;
255 	uint8_t *fidblk;
256 	off_t rblk;
257 	int sector_size = ump->discinfo.sector_size;
258 	int blks = sector_size / DEV_BSIZE;
259 	int len, buf_len, sector, sectors, run_length;
260 	int what, class, queue;
261 
262 	KASSERT(ump);
263 	KASSERT(buf);
264 	KASSERT(buf->b_iodone == nestiobuf_iodone);
265 
266 	what = buf->b_udf_c_type;
267 	queue = UDF_SHED_READING;
268 	if ((buf->b_flags & B_READ) == 0) {
269 		/* writing */
270 		queue = UDF_SHED_SEQWRITING;
271 		if (what == UDF_C_DSCR)
272 			queue = UDF_SHED_WRITING;
273 		if (what == UDF_C_NODE)
274 			queue = UDF_SHED_WRITING;
275 	}
276 
277 	/* use disc sheduler */
278 	class = ump->discinfo.mmc_class;
279 	KASSERT((class == MMC_CLASS_UNKN) || (class == MMC_CLASS_DISC) ||
280 		(ump->discinfo.mmc_cur & MMC_CAP_HW_DEFECTFREE) ||
281 		(ump->vfs_mountp->mnt_flag & MNT_RDONLY));
282 
283 	if (queue == UDF_SHED_READING) {
284 		DPRINTF(SHEDULE, ("\nudf_issue_buf READ %p : sector %d type %d,"
285 			"b_resid %d, b_bcount %d, b_bufsize %d\n",
286 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
287 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
288 		VOP_STRATEGY(ump->devvp, buf);
289 		return;
290 	}
291 
292 	/* (sectorsize == lb_size) for UDF */
293 	lb_size      = udf_rw32(ump->logical_vol->lb_size);
294 	blknr        = buf->b_blkno;
295 	our_sectornr = blknr / blks;
296 
297 	if (queue == UDF_SHED_WRITING) {
298 		DPRINTF(SHEDULE, ("\nudf_issue_buf WRITE %p : sector %d "
299 			"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
300 			buf, (uint32_t) buf->b_blkno / blks, buf->b_udf_c_type,
301 			buf->b_resid, buf->b_bcount, buf->b_bufsize));
302 		/* if we have FIDs fixup using buffer's sector number(s) */
303 		if (buf->b_udf_c_type == UDF_C_FIDS) {
304 			panic("UDF_C_FIDS in SHED_WRITING!\n");
305 			buf_len = buf->b_bcount;
306 			sectornr = our_sectornr;
307 			bpos = 0;
308 			while (buf_len) {
309 				len = MIN(buf_len, sector_size);
310 				fidblk = (uint8_t *) buf->b_data + bpos;
311 				udf_fixup_fid_block(fidblk, sector_size,
312 					0, len, sectornr);
313 				sectornr++;
314 				bpos += len;
315 				buf_len -= len;
316 			}
317 		}
318 		udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
319 		VOP_STRATEGY(ump->devvp, buf);
320 		return;
321 	}
322 
323 	/* UDF_SHED_SEQWRITING */
324 	KASSERT(queue == UDF_SHED_SEQWRITING);
325 	DPRINTF(SHEDULE, ("\nudf_issue_buf SEQWRITE %p : sector XXXX "
326 		"type %d, b_resid %d, b_bcount %d, b_bufsize %d\n",
327 		buf, buf->b_udf_c_type, buf->b_resid, buf->b_bcount,
328 		buf->b_bufsize));
329 
330 	/*
331 	 * Buffers should not have been allocated to disc addresses yet on
332 	 * this queue. Note that a buffer can get multiple extents allocated.
333 	 *
334 	 * lmapping contains lb_num relative to base partition.
335 	 */
336 	lmapping    = ump->la_lmapping;
337 	node_ad_cpy = ump->la_node_ad_cpy;
338 
339 	/* logically allocate buf and map it in the file */
340 	udf_late_allocate_buf(ump, buf, lmapping, node_ad_cpy, &vpart_num);
341 
342 	/* if we have FIDs, fixup using the new allocation table */
343 	if (buf->b_udf_c_type == UDF_C_FIDS) {
344 		buf_len = buf->b_bcount;
345 		bpos = 0;
346 		lmappos = lmapping;
347 		while (buf_len) {
348 			sectornr = *lmappos++;
349 			len = MIN(buf_len, sector_size);
350 			fidblk = (uint8_t *) buf->b_data + bpos;
351 			udf_fixup_fid_block(fidblk, sector_size,
352 				0, len, sectornr);
353 			bpos += len;
354 			buf_len -= len;
355 		}
356 	}
357 	if (buf->b_udf_c_type == UDF_C_METADATA_SBM) {
358 		if (buf->b_lblkno == 0) {
359 			/* update the tag location inside */
360 			tag = (struct desc_tag *) buf->b_data;
361 			tag->tag_loc = udf_rw32(*lmapping);
362 			udf_validate_tag_and_crc_sums(buf->b_data);
363 		}
364 	}
365 	udf_fixup_node_internals(ump, buf->b_data, buf->b_udf_c_type);
366 
367 	/*
368 	 * Translate new mappings in lmapping to pmappings and try to
369 	 * conglomerate extents to reduce the number of writes.
370 	 *
371 	 * pmapping to contain lb_nums as used for disc adressing.
372 	 */
373 	pmapping = ump->la_pmapping;
374 	sectors  = (buf->b_bcount + sector_size -1) / sector_size;
375 	udf_translate_vtop_list(ump, sectors, vpart_num, lmapping, pmapping);
376 
377 	for (sector = 0; sector < sectors; sector++) {
378 		buf_offset = sector * sector_size;
379 		DPRINTF(WRITE, ("\tprocessing rel sector %d\n", sector));
380 
381 		DPRINTF(WRITE, ("\tissue write sector %"PRIu64"\n",
382 			pmapping[sector]));
383 
384 		run_start  = pmapping[sector];
385 		run_length = 1;
386 		while (sector < sectors-1) {
387 			if (pmapping[sector+1] != pmapping[sector]+1)
388 				break;
389 			run_length++;
390 			sector++;
391 		}
392 
393 		/* nest an iobuf for the extent */
394 		rbuflen = run_length *  sector_size;
395 		rblk    = run_start  * (sector_size/DEV_BSIZE);
396 
397 		nestbuf = getiobuf(NULL, true);
398 		nestiobuf_setup(buf, nestbuf, buf_offset, rbuflen);
399 		/* nestbuf is B_ASYNC */
400 
401 		/* identify this nestbuf */
402 		nestbuf->b_lblkno   = sector;
403 		assert(nestbuf->b_vp == buf->b_vp);
404 
405 		/* CD shedules on raw blkno */
406 		nestbuf->b_blkno      = rblk;
407 		nestbuf->b_proc       = NULL;
408 		nestbuf->b_rawblkno   = rblk;
409 		nestbuf->b_udf_c_type = UDF_C_PROCESSED;
410 
411 		VOP_STRATEGY(ump->devvp, nestbuf);
412 	}
413 }
414 
415 
416 static void
417 udf_discstrat_init_direct(struct udf_strat_args *args)
418 {
419 	struct udf_mount  *ump = args->ump;
420 	struct strat_private *priv = PRIV(ump);
421 	uint32_t lb_size;
422 
423 	KASSERT(priv == NULL);
424 	ump->strategy_private = malloc(sizeof(struct strat_private),
425 		M_UDFTEMP, M_WAITOK);
426 	priv = ump->strategy_private;
427 	memset(priv, 0 , sizeof(struct strat_private));
428 
429 	/*
430 	 * Initialise pool for descriptors associated with nodes. This is done
431 	 * in lb_size units though currently lb_size is dictated to be
432 	 * sector_size.
433 	 */
434 	memset(&priv->desc_pool, 0, sizeof(struct pool));
435 
436 	lb_size = udf_rw32(ump->logical_vol->lb_size);
437 	pool_init(&priv->desc_pool, lb_size, 0, 0, 0, "udf_desc_pool", NULL,
438 	    IPL_NONE);
439 }
440 
441 
442 static void
443 udf_discstrat_finish_direct(struct udf_strat_args *args)
444 {
445 	struct udf_mount *ump = args->ump;
446 	struct strat_private *priv = PRIV(ump);
447 
448 	/* destroy our pool */
449 	pool_destroy(&priv->desc_pool);
450 
451 	/* free our private space */
452 	free(ump->strategy_private, M_UDFTEMP);
453 	ump->strategy_private = NULL;
454 }
455 
456 /* --------------------------------------------------------------------- */
457 
458 struct udf_strategy udf_strat_direct =
459 {
460 	udf_getblank_nodedscr_direct,
461 	udf_free_nodedscr_direct,
462 	udf_read_nodedscr_direct,
463 	udf_write_nodedscr_direct,
464 	udf_queue_buf_direct,
465 	udf_discstrat_init_direct,
466 	udf_discstrat_finish_direct
467 };
468 
469