]> git.saurik.com Git - apple/xnu.git/blob - bsd/isofs/cd9660/cd9660_bmap.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / isofs / cd9660 / cd9660_bmap.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* $NetBSD: cd9660_bmap.c,v 1.5 1994/12/13 22:33:12 mycroft Exp $ */
26
27 /*-
28 * Copyright (c) 1994
29 * The Regents of the University of California. All rights reserved.
30 *
31 * This code is derived from software contributed to Berkeley
32 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
33 * Support code is derived from software contributed to Berkeley
34 * by Atsushi Murai (amurai@spec.co.jp).
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)cd9660_bmap.c 8.4 (Berkeley) 12/5/94
65 */
66
67 #include <sys/param.h>
68 #include <sys/vnode.h>
69 #include <sys/mount.h>
70 #include <sys/namei.h>
71 #include <sys/buf.h>
72 #include <sys/file.h>
73
74 #include <isofs/cd9660/iso.h>
75 #include <isofs/cd9660/cd9660_node.h>
76
77 /*
78 * Bmap converts the logical block number of a file to its physical block
79 * number on the disk. The conversion is done by using the logical block
80 * number to index into the data block (extent) for the file.
81 */
82 int
83 cd9660_bmap(ap)
84 struct vop_bmap_args /* {
85 struct vnode *a_vp;
86 daddr_t a_bn;
87 struct vnode **a_vpp;
88 daddr_t *a_bnp;
89 int *a_runp;
90 } */ *ap;
91 {
92 struct iso_node *ip = VTOI(ap->a_vp);
93 daddr_t lblkno = ap->a_bn;
94 int bshift;
95
96 /*
97 * Check for underlying vnode requests and ensure that logical
98 * to physical mapping is requested.
99 */
100 if (ap->a_vpp != NULL)
101 *ap->a_vpp = ip->i_devvp;
102 if (ap->a_bnp == NULL)
103 return (0);
104
105 /*
106 * Associated files have an Apple Double header
107 */
108 if ((ip->i_flag & ISO_ASSOCIATED) && (lblkno > (ADH_BLKS - 1))) {
109 lblkno -= ADH_BLKS;
110 *ap->a_bnp = (ip->iso_start + lblkno);
111 if (ap->a_runp)
112 *ap->a_runp = 0;
113 return (0);
114 }
115
116 /*
117 * Compute the requested block number
118 */
119 bshift = ip->i_mnt->im_bshift;
120 *ap->a_bnp = (ip->iso_start + lblkno);
121
122 /*
123 * Determine maximum number of readahead blocks following the
124 * requested block.
125 */
126 if (ap->a_runp) {
127 int nblk;
128
129 nblk = (ip->i_size >> bshift) - (lblkno + 1);
130 if (nblk <= 0)
131 *ap->a_runp = 0;
132 else if (nblk >= (MAXBSIZE >> bshift))
133 *ap->a_runp = (MAXBSIZE >> bshift) - 1;
134 else
135 *ap->a_runp = nblk;
136 }
137
138 return (0);
139 }
140
141 /* blktooff converts a logical block number to a file offset */
142 int
143 cd9660_blktooff(ap)
144 struct vop_blktooff_args /* {
145 struct vnode *a_vp;
146 daddr_t a_lblkno;
147 off_t *a_offset;
148 } */ *ap;
149 {
150 register struct iso_node *ip;
151 register struct iso_mnt *imp;
152
153 if (ap->a_vp == NULL)
154 return (EINVAL);
155
156 ip = VTOI(ap->a_vp);
157 imp = ip->i_mnt;
158
159 *ap->a_offset = (off_t)lblktosize(imp, ap->a_lblkno);
160 return (0);
161 }
162
163 /* offtoblk converts a file offset to a logical block number */
164 int
165 cd9660_offtoblk(ap)
166 struct vop_offtoblk_args /* {
167 struct vnode *a_vp;
168 off_t a_offset;
169 daddr_t *a_lblkno;
170 } */ *ap;
171 {
172 register struct iso_node *ip;
173 register struct iso_mnt *imp;
174
175 if (ap->a_vp == NULL)
176 return (EINVAL);
177
178 ip = VTOI(ap->a_vp);
179 imp = ip->i_mnt;
180
181 *ap->a_lblkno = (daddr_t)lblkno(imp, ap->a_offset);
182 return (0);
183 }
184
185 int
186 cd9660_cmap(ap)
187 struct vop_cmap_args /* {
188 struct vnode *a_vp;
189 off_t a_offset;
190 size_t a_size;
191 daddr_t *a_bpn;
192 size_t *a_run;
193 void *a_poff;
194 } */ *ap;
195 {
196 struct iso_node *ip = VTOI(ap->a_vp);
197 size_t cbytes;
198 int devBlockSize = 0;
199 off_t offset = ap->a_foffset;
200
201 /*
202 * Check for underlying vnode requests and ensure that logical
203 * to physical mapping is requested.
204 */
205 if (ap->a_bpn == NULL)
206 return (0);
207
208 VOP_DEVBLOCKSIZE(ip->i_devvp, &devBlockSize);
209
210 /*
211 * Associated files have an Apple Double header
212 */
213 if (ip->i_flag & ISO_ASSOCIATED) {
214 if (offset < ADH_SIZE) {
215 if (ap->a_run)
216 *ap->a_run = 0;
217 *ap->a_bpn = -1;
218 goto out;
219 } else {
220 offset -= ADH_SIZE;
221 }
222 }
223
224 *ap->a_bpn = (daddr_t)(ip->iso_start + lblkno(ip->i_mnt, offset));
225
226 /*
227 * Determine maximum number of contiguous bytes following the
228 * requested offset.
229 */
230 if (ap->a_run) {
231 if (ip->i_size > offset)
232 cbytes = ip->i_size - offset;
233 else
234 cbytes = 0;
235
236 cbytes = (cbytes + (devBlockSize - 1)) & ~(devBlockSize - 1);
237
238 *ap->a_run = MIN(cbytes, ap->a_size);
239 };
240 out:
241 if (ap->a_poff)
242 *(int *)ap->a_poff = (long)offset & (devBlockSize - 1);
243
244 return (0);
245 }
246