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