1*84d9c625SLionel Sambuc /* $NetBSD: ext2fs_bmap.c,v 1.26 2013/01/22 09:39:15 dholland Exp $ */
2d65f6f70SBen Gras
3d65f6f70SBen Gras /*
4d65f6f70SBen Gras * Copyright (c) 1989, 1991, 1993
5d65f6f70SBen Gras * The Regents of the University of California. All rights reserved.
6d65f6f70SBen Gras * (c) UNIX System Laboratories, Inc.
7d65f6f70SBen Gras * All or some portions of this file are derived from material licensed
8d65f6f70SBen Gras * to the University of California by American Telephone and Telegraph
9d65f6f70SBen Gras * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10d65f6f70SBen Gras * the permission of UNIX System Laboratories, Inc.
11d65f6f70SBen Gras *
12d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
13d65f6f70SBen Gras * modification, are permitted provided that the following conditions
14d65f6f70SBen Gras * are met:
15d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
16d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
17d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
18d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
19d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
20d65f6f70SBen Gras * 3. Neither the name of the University nor the names of its contributors
21d65f6f70SBen Gras * may be used to endorse or promote products derived from this software
22d65f6f70SBen Gras * without specific prior written permission.
23d65f6f70SBen Gras *
24d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25d65f6f70SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26d65f6f70SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27d65f6f70SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28d65f6f70SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29d65f6f70SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30d65f6f70SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31d65f6f70SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32d65f6f70SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33d65f6f70SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34d65f6f70SBen Gras * SUCH DAMAGE.
35d65f6f70SBen Gras *
36d65f6f70SBen Gras * @(#)ufs_bmap.c 8.6 (Berkeley) 1/21/94
37d65f6f70SBen Gras * Modified for ext2fs by Manuel Bouyer.
38d65f6f70SBen Gras */
39d65f6f70SBen Gras
40d65f6f70SBen Gras /*
41d65f6f70SBen Gras * Copyright (c) 1997 Manuel Bouyer.
42d65f6f70SBen Gras *
43d65f6f70SBen Gras * Redistribution and use in source and binary forms, with or without
44d65f6f70SBen Gras * modification, are permitted provided that the following conditions
45d65f6f70SBen Gras * are met:
46d65f6f70SBen Gras * 1. Redistributions of source code must retain the above copyright
47d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer.
48d65f6f70SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
49d65f6f70SBen Gras * notice, this list of conditions and the following disclaimer in the
50d65f6f70SBen Gras * documentation and/or other materials provided with the distribution.
51d65f6f70SBen Gras *
52d65f6f70SBen Gras * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53d65f6f70SBen Gras * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54d65f6f70SBen Gras * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55d65f6f70SBen Gras * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56d65f6f70SBen Gras * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57d65f6f70SBen Gras * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58d65f6f70SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59d65f6f70SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60d65f6f70SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61d65f6f70SBen Gras * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62d65f6f70SBen Gras *
63d65f6f70SBen Gras * @(#)ufs_bmap.c 8.6 (Berkeley) 1/21/94
64d65f6f70SBen Gras * Modified for ext2fs by Manuel Bouyer.
65d65f6f70SBen Gras */
66d65f6f70SBen Gras
67d65f6f70SBen Gras #include <sys/cdefs.h>
68*84d9c625SLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: ext2fs_bmap.c,v 1.26 2013/01/22 09:39:15 dholland Exp $");
69d65f6f70SBen Gras
70d65f6f70SBen Gras #include <sys/param.h>
71d65f6f70SBen Gras #include <sys/systm.h>
72d65f6f70SBen Gras #include <sys/buf.h>
73d65f6f70SBen Gras #include <sys/proc.h>
74d65f6f70SBen Gras #include <sys/vnode.h>
75d65f6f70SBen Gras #include <sys/mount.h>
76d65f6f70SBen Gras #include <sys/resourcevar.h>
77d65f6f70SBen Gras #include <sys/trace.h>
78d65f6f70SBen Gras
79d65f6f70SBen Gras #include <miscfs/specfs/specdev.h>
80d65f6f70SBen Gras
81d65f6f70SBen Gras #include <ufs/ufs/inode.h>
82d65f6f70SBen Gras #include <ufs/ufs/ufsmount.h>
83d65f6f70SBen Gras #include <ufs/ufs/ufs_extern.h>
84d65f6f70SBen Gras #include <ufs/ext2fs/ext2fs.h>
85d65f6f70SBen Gras #include <ufs/ext2fs/ext2fs_extern.h>
86d65f6f70SBen Gras
87d65f6f70SBen Gras static int ext2fs_bmaparray(struct vnode *, daddr_t, daddr_t *,
88d65f6f70SBen Gras struct indir *, int *, int *);
89d65f6f70SBen Gras
90d65f6f70SBen Gras #define is_sequential(ump, a, b) ((b) == (a) + ump->um_seqinc)
91d65f6f70SBen Gras
92d65f6f70SBen Gras /*
93d65f6f70SBen Gras * Bmap converts a the logical block number of a file to its physical block
94d65f6f70SBen Gras * number on the disk. The conversion is done by using the logical block
95d65f6f70SBen Gras * number to index into the array of block pointers described by the dinode.
96d65f6f70SBen Gras */
97d65f6f70SBen Gras int
ext2fs_bmap(void * v)98d65f6f70SBen Gras ext2fs_bmap(void *v)
99d65f6f70SBen Gras {
100d65f6f70SBen Gras struct vop_bmap_args /* {
101d65f6f70SBen Gras struct vnode *a_vp;
102d65f6f70SBen Gras daddr_t a_bn;
103d65f6f70SBen Gras struct vnode **a_vpp;
104d65f6f70SBen Gras daddr_t *a_bnp;
105d65f6f70SBen Gras int *a_runp;
106d65f6f70SBen Gras } */ *ap = v;
107d65f6f70SBen Gras /*
108d65f6f70SBen Gras * Check for underlying vnode requests and ensure that logical
109d65f6f70SBen Gras * to physical mapping is requested.
110d65f6f70SBen Gras */
111d65f6f70SBen Gras if (ap->a_vpp != NULL)
112d65f6f70SBen Gras *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
113d65f6f70SBen Gras if (ap->a_bnp == NULL)
114d65f6f70SBen Gras return (0);
115d65f6f70SBen Gras
116d65f6f70SBen Gras return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
117d65f6f70SBen Gras ap->a_runp));
118d65f6f70SBen Gras }
119d65f6f70SBen Gras
120d65f6f70SBen Gras /*
121d65f6f70SBen Gras * Indirect blocks are now on the vnode for the file. They are given negative
122d65f6f70SBen Gras * logical block numbers. Indirect blocks are addressed by the negative
123d65f6f70SBen Gras * address of the first data block to which they point. Double indirect blocks
124d65f6f70SBen Gras * are addressed by one less than the address of the first indirect block to
125d65f6f70SBen Gras * which they point. Triple indirect blocks are addressed by one less than
126d65f6f70SBen Gras * the address of the first double indirect block to which they point.
127d65f6f70SBen Gras *
128d65f6f70SBen Gras * ext2fs_bmaparray does the bmap conversion, and if requested returns the
129d65f6f70SBen Gras * array of logical blocks which must be traversed to get to a block.
130d65f6f70SBen Gras * Each entry contains the offset into that block that gets you to the
131d65f6f70SBen Gras * next block and the disk address of the block (if it is assigned).
132d65f6f70SBen Gras */
133d65f6f70SBen Gras
134d65f6f70SBen Gras int
ext2fs_bmaparray(struct vnode * vp,daddr_t bn,daddr_t * bnp,struct indir * ap,int * nump,int * runp)135d65f6f70SBen Gras ext2fs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap,
136d65f6f70SBen Gras int *nump, int *runp)
137d65f6f70SBen Gras {
138d65f6f70SBen Gras struct inode *ip;
139d65f6f70SBen Gras struct buf *bp, *cbp;
140d65f6f70SBen Gras struct ufsmount *ump;
141d65f6f70SBen Gras struct mount *mp;
142*84d9c625SLionel Sambuc struct indir a[EXT2FS_NIADDR+1], *xap;
143d65f6f70SBen Gras daddr_t daddr;
144d65f6f70SBen Gras daddr_t metalbn;
145d65f6f70SBen Gras int error, maxrun = 0, num;
146d65f6f70SBen Gras
147d65f6f70SBen Gras ip = VTOI(vp);
148d65f6f70SBen Gras mp = vp->v_mount;
149d65f6f70SBen Gras ump = ip->i_ump;
150d65f6f70SBen Gras #ifdef DIAGNOSTIC
151d65f6f70SBen Gras if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
152d65f6f70SBen Gras panic("ext2fs_bmaparray: invalid arguments");
153d65f6f70SBen Gras #endif
154d65f6f70SBen Gras
155d65f6f70SBen Gras if (runp) {
156d65f6f70SBen Gras /*
157d65f6f70SBen Gras * XXX
158d65f6f70SBen Gras * If MAXBSIZE is the largest transfer the disks can handle,
159d65f6f70SBen Gras * we probably want maxrun to be 1 block less so that we
160d65f6f70SBen Gras * don't create a block larger than the device can handle.
161d65f6f70SBen Gras */
162d65f6f70SBen Gras *runp = 0;
163d65f6f70SBen Gras maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
164d65f6f70SBen Gras }
165d65f6f70SBen Gras
166*84d9c625SLionel Sambuc if (bn >= 0 && bn < EXT2FS_NDADDR) {
167d65f6f70SBen Gras /* XXX ondisk32 */
168d65f6f70SBen Gras *bnp = blkptrtodb(ump, fs2h32(ip->i_e2fs_blocks[bn]));
169d65f6f70SBen Gras if (*bnp == 0)
170d65f6f70SBen Gras *bnp = -1;
171d65f6f70SBen Gras else if (runp)
172d65f6f70SBen Gras /* XXX ondisk32 */
173*84d9c625SLionel Sambuc for (++bn; bn < EXT2FS_NDADDR && *runp < maxrun &&
174d65f6f70SBen Gras is_sequential(ump, (daddr_t)fs2h32(ip->i_e2fs_blocks[bn - 1]),
175d65f6f70SBen Gras (daddr_t)fs2h32(ip->i_e2fs_blocks[bn]));
176d65f6f70SBen Gras ++bn, ++*runp);
177d65f6f70SBen Gras return (0);
178d65f6f70SBen Gras }
179d65f6f70SBen Gras
180d65f6f70SBen Gras xap = ap == NULL ? a : ap;
181d65f6f70SBen Gras if (!nump)
182d65f6f70SBen Gras nump = #
183d65f6f70SBen Gras if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
184d65f6f70SBen Gras return (error);
185d65f6f70SBen Gras
186d65f6f70SBen Gras num = *nump;
187d65f6f70SBen Gras
188d65f6f70SBen Gras /* Get disk address out of indirect block array */
189d65f6f70SBen Gras /* XXX ondisk32 */
190*84d9c625SLionel Sambuc daddr = fs2h32(ip->i_e2fs_blocks[EXT2FS_NDADDR + xap->in_off]);
191d65f6f70SBen Gras
192d65f6f70SBen Gras #ifdef DIAGNOSTIC
193*84d9c625SLionel Sambuc if (num > EXT2FS_NIADDR + 1 || num < 1) {
194d65f6f70SBen Gras printf("ext2fs_bmaparray: num=%d\n", num);
195d65f6f70SBen Gras panic("ext2fs_bmaparray: num");
196d65f6f70SBen Gras }
197d65f6f70SBen Gras #endif
198d65f6f70SBen Gras for (bp = NULL, ++xap; --num; ++xap) {
199d65f6f70SBen Gras /*
200d65f6f70SBen Gras * Exit the loop if there is no disk address assigned yet and
201d65f6f70SBen Gras * the indirect block isn't in the cache, or if we were
202d65f6f70SBen Gras * looking for an indirect block and we've found it.
203d65f6f70SBen Gras */
204d65f6f70SBen Gras
205d65f6f70SBen Gras metalbn = xap->in_lbn;
206d65f6f70SBen Gras if (metalbn == bn)
207d65f6f70SBen Gras break;
208d65f6f70SBen Gras if (daddr == 0) {
209d65f6f70SBen Gras mutex_enter(&bufcache_lock);
210d65f6f70SBen Gras cbp = incore(vp, metalbn);
211d65f6f70SBen Gras mutex_exit(&bufcache_lock);
212d65f6f70SBen Gras if (cbp == NULL)
213d65f6f70SBen Gras break;
214d65f6f70SBen Gras }
215d65f6f70SBen Gras /*
216d65f6f70SBen Gras * If we get here, we've either got the block in the cache
217d65f6f70SBen Gras * or we have a disk address for it, go fetch it.
218d65f6f70SBen Gras */
219d65f6f70SBen Gras if (bp)
220d65f6f70SBen Gras brelse(bp, 0);
221d65f6f70SBen Gras
222d65f6f70SBen Gras xap->in_exists = 1;
223d65f6f70SBen Gras bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, 0);
224d65f6f70SBen Gras if (bp == NULL) {
225d65f6f70SBen Gras
226d65f6f70SBen Gras /*
227d65f6f70SBen Gras * getblk() above returns NULL only iff we are
228d65f6f70SBen Gras * pagedaemon. See the implementation of getblk
229d65f6f70SBen Gras * for detail.
230d65f6f70SBen Gras */
231d65f6f70SBen Gras
232d65f6f70SBen Gras return (ENOMEM);
233d65f6f70SBen Gras }
234d65f6f70SBen Gras if (bp->b_oflags & (BO_DONE | BO_DELWRI)) {
235d65f6f70SBen Gras trace(TR_BREADHIT, pack(vp, size), metalbn);
236d65f6f70SBen Gras }
237d65f6f70SBen Gras #ifdef DIAGNOSTIC
238d65f6f70SBen Gras else if (!daddr)
239d65f6f70SBen Gras panic("ext2fs_bmaparry: indirect block not in cache");
240d65f6f70SBen Gras #endif
241d65f6f70SBen Gras else {
242d65f6f70SBen Gras trace(TR_BREADMISS, pack(vp, size), metalbn);
243d65f6f70SBen Gras bp->b_blkno = blkptrtodb(ump, daddr);
244d65f6f70SBen Gras bp->b_flags |= B_READ;
245d65f6f70SBen Gras VOP_STRATEGY(vp, bp);
246d65f6f70SBen Gras curlwp->l_ru.ru_inblock++; /* XXX */
247d65f6f70SBen Gras if ((error = biowait(bp)) != 0) {
248d65f6f70SBen Gras brelse(bp, 0);
249d65f6f70SBen Gras return (error);
250d65f6f70SBen Gras }
251d65f6f70SBen Gras }
252d65f6f70SBen Gras
253d65f6f70SBen Gras /* XXX ondisk32 */
254d65f6f70SBen Gras daddr = fs2h32(((int32_t *)bp->b_data)[xap->in_off]);
255d65f6f70SBen Gras if (num == 1 && daddr && runp)
256d65f6f70SBen Gras /* XXX ondisk32 */
257d65f6f70SBen Gras for (bn = xap->in_off + 1;
258d65f6f70SBen Gras bn < MNINDIR(ump) && *runp < maxrun &&
259d65f6f70SBen Gras is_sequential(ump, ((int32_t *)bp->b_data)[bn - 1],
260d65f6f70SBen Gras ((int32_t *)bp->b_data)[bn]);
261d65f6f70SBen Gras ++bn, ++*runp);
262d65f6f70SBen Gras }
263d65f6f70SBen Gras if (bp)
264d65f6f70SBen Gras brelse(bp, 0);
265d65f6f70SBen Gras
266d65f6f70SBen Gras daddr = blkptrtodb(ump, daddr);
267d65f6f70SBen Gras *bnp = daddr == 0 ? -1 : daddr;
268d65f6f70SBen Gras return (0);
269d65f6f70SBen Gras }
270