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