]> git.saurik.com Git - apple/xnu.git/blame - bsd/isofs/cd9660/cd9660_bmap.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / isofs / cd9660 / cd9660_bmap.c
CommitLineData
1c79356b 1/*
55e303ae 2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
37839358
A
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.
1c79356b 11 *
37839358
A
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
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
37839358
A
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.
1c79356b
A
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>
1c79356b
A
68#include <sys/file.h>
69
70#include <isofs/cd9660/iso.h>
71#include <isofs/cd9660/cd9660_node.h>
72
1c79356b
A
73
74/* blktooff converts a logical block number to a file offset */
75int
91447636 76cd9660_blktooff(struct vnop_blktooff_args *ap)
1c79356b
A
77{
78 register struct iso_node *ip;
55e303ae 79 register struct iso_mnt *imp;
1c79356b
A
80
81 if (ap->a_vp == NULL)
82 return (EINVAL);
83
84 ip = VTOI(ap->a_vp);
85 imp = ip->i_mnt;
86
87 *ap->a_offset = (off_t)lblktosize(imp, ap->a_lblkno);
88 return (0);
89}
90
91/* offtoblk converts a file offset to a logical block number */
92int
91447636 93cd9660_offtoblk(struct vnop_offtoblk_args *ap)
1c79356b
A
94{
95 register struct iso_node *ip;
96 register struct iso_mnt *imp;
97
98 if (ap->a_vp == NULL)
99 return (EINVAL);
100
101 ip = VTOI(ap->a_vp);
102 imp = ip->i_mnt;
103
91447636 104 *ap->a_lblkno = (daddr64_t)lblkno(imp, ap->a_offset);
1c79356b
A
105 return (0);
106}
107
108int
91447636 109cd9660_blockmap(struct vnop_blockmap_args *ap)
1c79356b
A
110{
111 struct iso_node *ip = VTOI(ap->a_vp);
112 size_t cbytes;
113 int devBlockSize = 0;
55e303ae 114 off_t offset = ap->a_foffset;
1c79356b
A
115
116 /*
117 * Check for underlying vnode requests and ensure that logical
118 * to physical mapping is requested.
119 */
120 if (ap->a_bpn == NULL)
121 return (0);
122
91447636 123 devBlockSize = vfs_devblocksize(vnode_mount(ap->a_vp));
1c79356b 124
55e303ae
A
125 /*
126 * Associated files have an Apple Double header
127 */
128 if (ip->i_flag & ISO_ASSOCIATED) {
129 if (offset < ADH_SIZE) {
130 if (ap->a_run)
131 *ap->a_run = 0;
91447636 132 *ap->a_bpn = (daddr64_t)-1;
55e303ae
A
133 goto out;
134 } else {
135 offset -= ADH_SIZE;
136 }
137 }
138
91447636 139 *ap->a_bpn = (daddr64_t)(ip->iso_start + lblkno(ip->i_mnt, offset));
1c79356b
A
140
141 /*
142 * Determine maximum number of contiguous bytes following the
143 * requested offset.
144 */
145 if (ap->a_run) {
55e303ae
A
146 if (ip->i_size > offset)
147 cbytes = ip->i_size - offset;
1c79356b
A
148 else
149 cbytes = 0;
150
151 cbytes = (cbytes + (devBlockSize - 1)) & ~(devBlockSize - 1);
152
153 *ap->a_run = MIN(cbytes, ap->a_size);
154 };
55e303ae 155out:
1c79356b 156 if (ap->a_poff)
55e303ae 157 *(int *)ap->a_poff = (long)offset & (devBlockSize - 1);
1c79356b
A
158
159 return (0);
160}
161