1*0a6a1f1dSLionel Sambuc /* $NetBSD: cd9660_bmap.c,v 1.5 2014/06/14 07:39:28 hannken Exp $ */
284d9c625SLionel Sambuc
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc * Copyright (c) 1994
584d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
684d9c625SLionel Sambuc *
784d9c625SLionel Sambuc * This code is derived from software contributed to Berkeley
884d9c625SLionel Sambuc * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
984d9c625SLionel Sambuc * Support code is derived from software contributed to Berkeley
1084d9c625SLionel Sambuc * by Atsushi Murai (amurai@spec.co.jp).
1184d9c625SLionel Sambuc *
1284d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1384d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
1484d9c625SLionel Sambuc * are met:
1584d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1684d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1784d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1884d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1984d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
2084d9c625SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
2184d9c625SLionel Sambuc * may be used to endorse or promote products derived from this software
2284d9c625SLionel Sambuc * without specific prior written permission.
2384d9c625SLionel Sambuc *
2484d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2584d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2684d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2784d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2884d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2984d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3084d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3184d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3284d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3384d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3484d9c625SLionel Sambuc * SUCH DAMAGE.
3584d9c625SLionel Sambuc *
3684d9c625SLionel Sambuc * @(#)cd9660_bmap.c 8.4 (Berkeley) 12/5/94
3784d9c625SLionel Sambuc */
3884d9c625SLionel Sambuc
3984d9c625SLionel Sambuc #include <sys/cdefs.h>
40*0a6a1f1dSLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: cd9660_bmap.c,v 1.5 2014/06/14 07:39:28 hannken Exp $");
4184d9c625SLionel Sambuc
4284d9c625SLionel Sambuc #include <sys/param.h>
4384d9c625SLionel Sambuc #include <sys/namei.h>
4484d9c625SLionel Sambuc #include <sys/buf.h>
4584d9c625SLionel Sambuc #include <sys/file.h>
4684d9c625SLionel Sambuc #include <sys/vnode.h>
4784d9c625SLionel Sambuc #include <sys/mount.h>
4884d9c625SLionel Sambuc
4984d9c625SLionel Sambuc #include <fs/cd9660/iso.h>
5084d9c625SLionel Sambuc #include <fs/cd9660/cd9660_extern.h>
5184d9c625SLionel Sambuc #include <fs/cd9660/cd9660_node.h>
5284d9c625SLionel Sambuc
5384d9c625SLionel Sambuc /*
5484d9c625SLionel Sambuc * Bmap converts a the logical block number of a file to its physical block
5584d9c625SLionel Sambuc * number on the disk. The conversion is done by using the logical block
5684d9c625SLionel Sambuc * number to index into the data block (extent) for the file.
5784d9c625SLionel Sambuc */
5884d9c625SLionel Sambuc int
cd9660_bmap(void * v)5984d9c625SLionel Sambuc cd9660_bmap(void *v)
6084d9c625SLionel Sambuc {
6184d9c625SLionel Sambuc struct vop_bmap_args /* {
6284d9c625SLionel Sambuc struct vnode *a_vp;
6384d9c625SLionel Sambuc daddr_t a_bn;
6484d9c625SLionel Sambuc struct vnode **a_vpp;
6584d9c625SLionel Sambuc daddr_t *a_bnp;
6684d9c625SLionel Sambuc int *a_runp;
6784d9c625SLionel Sambuc } */ *ap = v;
6884d9c625SLionel Sambuc struct iso_node *ip = VTOI(ap->a_vp);
6984d9c625SLionel Sambuc daddr_t lblkno = ap->a_bn;
7084d9c625SLionel Sambuc int bshift;
7184d9c625SLionel Sambuc
7284d9c625SLionel Sambuc /*
7384d9c625SLionel Sambuc * Check for underlying vnode requests and ensure that logical
7484d9c625SLionel Sambuc * to physical mapping is requested.
7584d9c625SLionel Sambuc */
7684d9c625SLionel Sambuc if (ap->a_vpp != NULL)
77*0a6a1f1dSLionel Sambuc *ap->a_vpp = ip->i_mnt->im_devvp;
7884d9c625SLionel Sambuc if (ap->a_bnp == NULL)
7984d9c625SLionel Sambuc return (0);
8084d9c625SLionel Sambuc
8184d9c625SLionel Sambuc /*
8284d9c625SLionel Sambuc * Compute the requested block number
8384d9c625SLionel Sambuc */
8484d9c625SLionel Sambuc bshift = ip->i_mnt->im_bshift;
8584d9c625SLionel Sambuc *ap->a_bnp = (ip->iso_start + lblkno) << (bshift - DEV_BSHIFT);
8684d9c625SLionel Sambuc
8784d9c625SLionel Sambuc /*
8884d9c625SLionel Sambuc * Determine maximum number of readahead blocks following the
8984d9c625SLionel Sambuc * requested block.
9084d9c625SLionel Sambuc */
9184d9c625SLionel Sambuc if (ap->a_runp) {
9284d9c625SLionel Sambuc int nblk;
9384d9c625SLionel Sambuc
9484d9c625SLionel Sambuc nblk = (ip->i_size >> bshift) - (lblkno + 1);
9584d9c625SLionel Sambuc if (nblk <= 0)
9684d9c625SLionel Sambuc *ap->a_runp = 0;
9784d9c625SLionel Sambuc else if (nblk >= (MAXBSIZE >> bshift))
9884d9c625SLionel Sambuc *ap->a_runp = (MAXBSIZE >> bshift) - 1;
9984d9c625SLionel Sambuc else
10084d9c625SLionel Sambuc *ap->a_runp = nblk;
10184d9c625SLionel Sambuc }
10284d9c625SLionel Sambuc
10384d9c625SLionel Sambuc return (0);
10484d9c625SLionel Sambuc }
105