xref: /netbsd-src/sys/fs/cd9660/cd9660_bmap.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: cd9660_bmap.c,v 1.3 2005/12/11 12:24:25 christos 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. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)cd9660_bmap.c	8.4 (Berkeley) 12/5/94
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_bmap.c,v 1.3 2005/12/11 12:24:25 christos Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/namei.h>
44 #include <sys/buf.h>
45 #include <sys/file.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 
49 #include <fs/cd9660/iso.h>
50 #include <fs/cd9660/cd9660_extern.h>
51 #include <fs/cd9660/cd9660_node.h>
52 
53 /*
54  * Bmap converts a the logical block number of a file to its physical block
55  * number on the disk. The conversion is done by using the logical block
56  * number to index into the data block (extent) for the file.
57  */
58 int
59 cd9660_bmap(v)
60 	void *v;
61 {
62 	struct vop_bmap_args /* {
63 		struct vnode *a_vp;
64 		daddr_t  a_bn;
65 		struct vnode **a_vpp;
66 		daddr_t *a_bnp;
67 		int *a_runp;
68 	} */ *ap = v;
69 	struct iso_node *ip = VTOI(ap->a_vp);
70 	daddr_t lblkno = ap->a_bn;
71 	int bshift;
72 
73 	/*
74 	 * Check for underlying vnode requests and ensure that logical
75 	 * to physical mapping is requested.
76 	 */
77 	if (ap->a_vpp != NULL)
78 		*ap->a_vpp = ip->i_devvp;
79 	if (ap->a_bnp == NULL)
80 		return (0);
81 
82 	/*
83 	 * Compute the requested block number
84 	 */
85 	bshift = ip->i_mnt->im_bshift;
86 	*ap->a_bnp = (ip->iso_start + lblkno) << (bshift - DEV_BSHIFT);
87 
88 	/*
89 	 * Determine maximum number of readahead blocks following the
90 	 * requested block.
91 	 */
92 	if (ap->a_runp) {
93 		int nblk;
94 
95 		nblk = (ip->i_size >> bshift) - (lblkno + 1);
96 		if (nblk <= 0)
97 			*ap->a_runp = 0;
98 		else if (nblk >= (MAXBSIZE >> bshift))
99 			*ap->a_runp = (MAXBSIZE >> bshift) - 1;
100 		else
101 			*ap->a_runp = nblk;
102 	}
103 
104 	return (0);
105 }
106