xref: /openbsd-src/sys/isofs/cd9660/cd9660_bmap.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: cd9660_bmap.c,v 1.3 1997/11/08 17:21:05 niklas Exp $	*/
2 /*	$NetBSD: cd9660_bmap.c,v 1.7 1997/01/24 00:27:29 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley
9  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
10  * Support code is derived from software contributed to Berkeley
11  * by Atsushi Murai (amurai@spec.co.jp).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)cd9660_bmap.c	8.4 (Berkeley) 12/5/94
42  */
43 
44 #include <sys/param.h>
45 #include <sys/namei.h>
46 #include <sys/buf.h>
47 #include <sys/file.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 
51 #include <isofs/cd9660/iso.h>
52 #include <isofs/cd9660/cd9660_extern.h>
53 #include <isofs/cd9660/cd9660_node.h>
54 
55 /*
56  * Bmap converts a the logical block number of a file to its physical block
57  * number on the disk. The conversion is done by using the logical block
58  * number to index into the data block (extent) for the file.
59  */
60 int
61 cd9660_bmap(v)
62 	void *v;
63 {
64 	struct vop_bmap_args /* {
65 		struct vnode *a_vp;
66 		daddr_t  a_bn;
67 		struct vnode **a_vpp;
68 		daddr_t *a_bnp;
69 		int *a_runp;
70 	} */ *ap = v;
71 	struct iso_node *ip = VTOI(ap->a_vp);
72 	daddr_t lblkno = ap->a_bn;
73 	int bshift;
74 
75 	/*
76 	 * Check for underlying vnode requests and ensure that logical
77 	 * to physical mapping is requested.
78 	 */
79 	if (ap->a_vpp != NULL)
80 		*ap->a_vpp = ip->i_devvp;
81 	if (ap->a_bnp == NULL)
82 		return (0);
83 
84 	/*
85 	 * Compute the requested block number
86 	 */
87 	bshift = ip->i_mnt->im_bshift;
88 	*ap->a_bnp = (ip->iso_start + lblkno) << (bshift - DEV_BSHIFT);
89 
90 	/*
91 	 * Determine maximum number of readahead blocks following the
92 	 * requested block.
93 	 */
94 	if (ap->a_runp) {
95 		int nblk;
96 
97 		nblk = (ip->i_size >> bshift) - (lblkno + 1);
98 		if (nblk <= 0)
99 			*ap->a_runp = 0;
100 		else if (nblk >= (MAXBSIZE >> bshift))
101 			*ap->a_runp = (MAXBSIZE >> bshift) - 1;
102 		else
103 			*ap->a_runp = nblk;
104 	}
105 
106 	return (0);
107 }
108