1/*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*	$NetBSD: cd9660_bmap.c,v 1.5 1994/12/13 22:33:12 mycroft Exp $	*/
29
30/*-
31 * Copyright (c) 1994
32 *	The Regents of the University of California.  All rights reserved.
33 *
34 * This code is derived from software contributed to Berkeley
35 * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
36 * Support code is derived from software contributed to Berkeley
37 * by Atsushi Murai (amurai@spec.co.jp).
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 *    notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 *    notice, this list of conditions and the following disclaimer in the
46 *    documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 *    must display the following acknowledgement:
49 *	This product includes software developed by the University of
50 *	California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 *    may be used to endorse or promote products derived from this software
53 *    without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 *	@(#)cd9660_bmap.c	8.4 (Berkeley) 12/5/94
68 */
69
70#include <sys/param.h>
71#include <sys/vnode.h>
72#include <sys/mount.h>
73#include <sys/namei.h>
74#include <sys/file.h>
75
76#include <isofs/cd9660/iso.h>
77#include <isofs/cd9660/cd9660_node.h>
78
79
80/* blktooff converts a logical block number to a file offset */
81int
82cd9660_blktooff(struct vnop_blktooff_args *ap)
83{
84	register struct iso_node *ip;
85	register struct iso_mnt *imp;
86
87	if (ap->a_vp == NULL)
88		return (EINVAL);
89
90	ip = VTOI(ap->a_vp);
91	imp = ip->i_mnt;
92
93	*ap->a_offset = (off_t)lblktosize(imp, ap->a_lblkno);
94	return (0);
95}
96
97/* offtoblk converts a file offset to a logical block number */
98int
99cd9660_offtoblk(struct vnop_offtoblk_args *ap)
100{
101	register struct iso_node *ip;
102	register struct iso_mnt *imp;
103
104	if (ap->a_vp == NULL)
105		return (EINVAL);
106
107	ip = VTOI(ap->a_vp);
108	imp = ip->i_mnt;
109
110	*ap->a_lblkno = (daddr64_t)lblkno(imp, ap->a_offset);
111	return (0);
112}
113
114int
115cd9660_blockmap(struct vnop_blockmap_args *ap)
116{
117	struct iso_node *ip = VTOI(ap->a_vp);
118	size_t cbytes;
119	int devBlockSize = 0;
120	off_t offset = ap->a_foffset;
121
122	/*
123	 * Check for underlying vnode requests and ensure that logical
124	 * to physical mapping is requested.
125	 */
126	if (ap->a_bpn == NULL)
127		return (0);
128
129	devBlockSize = vfs_devblocksize(vnode_mount(ap->a_vp));
130
131	/*
132	 * Associated files have an Apple Double header
133	 */
134	if (ip->i_flag & ISO_ASSOCIATED) {
135		if (offset < ADH_SIZE) {
136			if (ap->a_run)
137				*ap->a_run = 0;
138			*ap->a_bpn = (daddr64_t)-1;
139			goto out;
140		} else {
141			offset -= ADH_SIZE;
142		}
143	}
144
145	*ap->a_bpn = (daddr64_t)(ip->iso_start + lblkno(ip->i_mnt, offset));
146
147	/*
148	 * Determine maximum number of contiguous bytes following the
149	 * requested offset.
150	 */
151	if (ap->a_run) {
152		if (ip->i_size > offset)
153			cbytes = ip->i_size - offset;
154		else
155			cbytes = 0;
156
157		cbytes = (cbytes + (devBlockSize - 1)) & ~(devBlockSize - 1);
158
159		*ap->a_run = MIN(cbytes, ap->a_size);
160	};
161out:
162	if (ap->a_poff)
163		*(int *)ap->a_poff = (long)offset & (devBlockSize - 1);
164
165	return (0);
166}
167
168