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