1*3b9d585eSjsg /* $OpenBSD: ext2fs_bmap.c,v 1.29 2024/04/13 23:44:11 jsg Exp $ */
2891e3b90Sart /* $NetBSD: ext2fs_bmap.c,v 1.5 2000/03/30 12:41:11 augustss Exp $ */
35ac2d602Sdownsj
45ac2d602Sdownsj /*
51f3ff51cSdownsj * Copyright (c) 1997 Manuel Bouyer.
65ac2d602Sdownsj * Copyright (c) 1989, 1991, 1993
75ac2d602Sdownsj * The Regents of the University of California. All rights reserved.
85ac2d602Sdownsj * (c) UNIX System Laboratories, Inc.
95ac2d602Sdownsj * All or some portions of this file are derived from material licensed
105ac2d602Sdownsj * to the University of California by American Telephone and Telegraph
115ac2d602Sdownsj * Co. or Unix System Laboratories, Inc. and are reproduced herein with
125ac2d602Sdownsj * the permission of UNIX System Laboratories, Inc.
135ac2d602Sdownsj *
145ac2d602Sdownsj * Redistribution and use in source and binary forms, with or without
155ac2d602Sdownsj * modification, are permitted provided that the following conditions
165ac2d602Sdownsj * are met:
175ac2d602Sdownsj * 1. Redistributions of source code must retain the above copyright
185ac2d602Sdownsj * notice, this list of conditions and the following disclaimer.
195ac2d602Sdownsj * 2. Redistributions in binary form must reproduce the above copyright
205ac2d602Sdownsj * notice, this list of conditions and the following disclaimer in the
215ac2d602Sdownsj * documentation and/or other materials provided with the distribution.
2229295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
235ac2d602Sdownsj * may be used to endorse or promote products derived from this software
245ac2d602Sdownsj * without specific prior written permission.
255ac2d602Sdownsj *
265ac2d602Sdownsj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
275ac2d602Sdownsj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
285ac2d602Sdownsj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
295ac2d602Sdownsj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
305ac2d602Sdownsj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
315ac2d602Sdownsj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
325ac2d602Sdownsj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
335ac2d602Sdownsj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
345ac2d602Sdownsj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
355ac2d602Sdownsj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
365ac2d602Sdownsj * SUCH DAMAGE.
375ac2d602Sdownsj *
385ac2d602Sdownsj * @(#)ufs_bmap.c 8.6 (Berkeley) 1/21/94
391f3ff51cSdownsj * Modified for ext2fs by Manuel Bouyer.
405ac2d602Sdownsj */
415ac2d602Sdownsj
425ac2d602Sdownsj #include <sys/param.h>
435ac2d602Sdownsj #include <sys/systm.h>
445ac2d602Sdownsj #include <sys/buf.h>
455ac2d602Sdownsj #include <sys/proc.h>
465ac2d602Sdownsj #include <sys/vnode.h>
475ac2d602Sdownsj #include <sys/mount.h>
48544451c3Sderaadt #include <sys/specdev.h>
495ac2d602Sdownsj
505ac2d602Sdownsj #include <ufs/ufs/quota.h>
515ac2d602Sdownsj #include <ufs/ufs/inode.h>
525ac2d602Sdownsj #include <ufs/ufs/ufsmount.h>
535ac2d602Sdownsj #include <ufs/ufs/ufs_extern.h>
54891e3b90Sart #include <ufs/ext2fs/ext2fs.h>
55ea0c9824Spelikan #include <ufs/ext2fs/ext2fs_extents.h>
565ac2d602Sdownsj #include <ufs/ext2fs/ext2fs_extern.h>
575ac2d602Sdownsj
58ea0c9824Spelikan static int ext4_bmapext(struct vnode *, daddr_t, daddr_t *, struct indir *,
59ea0c9824Spelikan int *, int *);
60ea0c9824Spelikan static int ext2fs_bmaparray(struct vnode *, daddr_t, daddr_t *, struct indir *,
61ea0c9824Spelikan int *, int *);
625ac2d602Sdownsj
635ac2d602Sdownsj /*
645ac2d602Sdownsj * Bmap converts a the logical block number of a file to its physical block
655ac2d602Sdownsj * number on the disk. The conversion is done by using the logical block
665ac2d602Sdownsj * number to index into the array of block pointers described by the dinode.
675ac2d602Sdownsj */
685ac2d602Sdownsj int
ext2fs_bmap(void * v)695f64cd9cSjasper ext2fs_bmap(void *v)
705ac2d602Sdownsj {
7199bc9d31Sderaadt struct vop_bmap_args *ap = v;
72ea0c9824Spelikan
735ac2d602Sdownsj /*
745ac2d602Sdownsj * Check for underlying vnode requests and ensure that logical
755ac2d602Sdownsj * to physical mapping is requested.
765ac2d602Sdownsj */
775ac2d602Sdownsj if (ap->a_vpp != NULL)
785ac2d602Sdownsj *ap->a_vpp = VTOI(ap->a_vp)->i_devvp;
795ac2d602Sdownsj if (ap->a_bnp == NULL)
805ac2d602Sdownsj return (0);
815ac2d602Sdownsj
82ea0c9824Spelikan if (VTOI(ap->a_vp)->i_e2din->e2di_flags & EXT4_EXTENTS) {
83ea0c9824Spelikan return (ext4_bmapext(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
84ea0c9824Spelikan ap->a_runp));
85ea0c9824Spelikan }
865ac2d602Sdownsj return (ext2fs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL,
875ac2d602Sdownsj ap->a_runp));
885ac2d602Sdownsj }
895ac2d602Sdownsj
905ac2d602Sdownsj /*
91ea0c9824Spelikan * Logical block number of a file -> physical block number on disk within ext4 extents.
92ea0c9824Spelikan */
93ea0c9824Spelikan int
ext4_bmapext(struct vnode * vp,daddr_t bn,daddr_t * bnp,struct indir * ap,int * nump,int * runp)94ea0c9824Spelikan ext4_bmapext(struct vnode *vp, daddr_t bn, daddr_t *bnp, struct indir *ap, int *nump, int *runp)
95ea0c9824Spelikan {
96ea0c9824Spelikan struct inode *ip;
97ea0c9824Spelikan struct m_ext2fs *fs;
98ea0c9824Spelikan struct ext4_extent *ep;
99ea0c9824Spelikan struct ext4_extent_path path;
100ea0c9824Spelikan daddr_t pos;
101ea0c9824Spelikan
102ea0c9824Spelikan ip = VTOI(vp);
103ea0c9824Spelikan fs = ip->i_e2fs;
104ea0c9824Spelikan
105ea0c9824Spelikan if (runp != NULL)
106ea0c9824Spelikan *runp = 0;
107ea0c9824Spelikan if (nump != NULL)
108ea0c9824Spelikan *nump = 0;
109ea0c9824Spelikan
110ea0c9824Spelikan ext4_ext_find_extent(fs, ip, bn, &path);
111ea0c9824Spelikan if ((ep = path.ep_ext) == NULL)
112ea0c9824Spelikan return (EIO);
113ea0c9824Spelikan
114ea0c9824Spelikan pos = bn - ep->e_blk + (((daddr_t)ep->e_start_hi << 32) | ep->e_start_lo);
115ea0c9824Spelikan if ((*bnp = fsbtodb(fs, pos)) == 0)
116ea0c9824Spelikan *bnp = -1;
117ea0c9824Spelikan return (0);
118ea0c9824Spelikan }
119ea0c9824Spelikan
120ea0c9824Spelikan /*
1215ac2d602Sdownsj * Indirect blocks are now on the vnode for the file. They are given negative
1225ac2d602Sdownsj * logical block numbers. Indirect blocks are addressed by the negative
1235ac2d602Sdownsj * address of the first data block to which they point. Double indirect blocks
1245ac2d602Sdownsj * are addressed by one less than the address of the first indirect block to
1255ac2d602Sdownsj * which they point. Triple indirect blocks are addressed by one less than
1265ac2d602Sdownsj * the address of the first double indirect block to which they point.
1275ac2d602Sdownsj *
1285ac2d602Sdownsj * ext2fs_bmaparray does the bmap conversion, and if requested returns the
1295ac2d602Sdownsj * array of logical blocks which must be traversed to get to a block.
1305ac2d602Sdownsj * Each entry contains the offset into that block that gets you to the
1315ac2d602Sdownsj * next block and the disk address of the block (if it is assigned).
1325ac2d602Sdownsj */
1335ac2d602Sdownsj
1345ac2d602Sdownsj int
ext2fs_bmaparray(struct vnode * vp,daddr_t bn,daddr_t * bnp,struct indir * ap,int * nump,int * runp)135ea0c9824Spelikan ext2fs_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp,
1365f64cd9cSjasper struct indir *ap, int *nump, int *runp)
1375ac2d602Sdownsj {
138891e3b90Sart struct inode *ip;
1395ac2d602Sdownsj struct buf *bp;
1405ac2d602Sdownsj struct ufsmount *ump;
1415ac2d602Sdownsj struct mount *mp;
1425ac2d602Sdownsj struct vnode *devvp;
143a1863f45Smillert struct indir a[NIADDR+1], *xap;
1441dfb1939Spedro int32_t daddr;
1455ac2d602Sdownsj long metalbn;
1465ac2d602Sdownsj int error, maxrun = 0, num;
1475ac2d602Sdownsj
1485ac2d602Sdownsj ip = VTOI(vp);
1495ac2d602Sdownsj mp = vp->v_mount;
1505ac2d602Sdownsj ump = VFSTOUFS(mp);
1515ac2d602Sdownsj #ifdef DIAGNOSTIC
1525ac2d602Sdownsj if ((ap != NULL && nump == NULL) || (ap == NULL && nump != NULL))
1535ac2d602Sdownsj panic("ext2fs_bmaparray: invalid arguments");
1545ac2d602Sdownsj #endif
1555ac2d602Sdownsj
1565ac2d602Sdownsj if (runp) {
1575ac2d602Sdownsj /*
1585ac2d602Sdownsj * XXX
1595ac2d602Sdownsj * If MAXBSIZE is the largest transfer the disks can handle,
1605ac2d602Sdownsj * we probably want maxrun to be 1 block less so that we
1615ac2d602Sdownsj * don't create a block larger than the device can handle.
1625ac2d602Sdownsj */
1635ac2d602Sdownsj *runp = 0;
1645ac2d602Sdownsj maxrun = MAXBSIZE / mp->mnt_stat.f_iosize - 1;
1655ac2d602Sdownsj }
1665ac2d602Sdownsj
1675ac2d602Sdownsj xap = ap == NULL ? a : ap;
1685ac2d602Sdownsj if (!nump)
1695ac2d602Sdownsj nump = #
1705ac2d602Sdownsj if ((error = ufs_getlbns(vp, bn, xap, nump)) != 0)
1715ac2d602Sdownsj return (error);
1725ac2d602Sdownsj
1735ac2d602Sdownsj num = *nump;
1745ac2d602Sdownsj if (num == 0) {
175f7dbefaaSpelikan *bnp = blkptrtodb(ump, letoh32(ip->i_e2fs_blocks[bn]));
1765ac2d602Sdownsj if (*bnp == 0)
1775ac2d602Sdownsj *bnp = -1;
1785ac2d602Sdownsj else if (runp)
1795ac2d602Sdownsj for (++bn; bn < NDADDR && *runp < maxrun &&
180f7dbefaaSpelikan is_sequential(ump,
181f7dbefaaSpelikan letoh32(ip->i_e2fs_blocks[bn - 1]),
182f7dbefaaSpelikan letoh32(ip->i_e2fs_blocks[bn]));
183f7dbefaaSpelikan ++bn, ++*runp)
184f7dbefaaSpelikan /* nothing */;
1855ac2d602Sdownsj return (0);
1865ac2d602Sdownsj }
1875ac2d602Sdownsj
1885ac2d602Sdownsj
1895ac2d602Sdownsj /* Get disk address out of indirect block array */
190f7dbefaaSpelikan daddr = letoh32(ip->i_e2fs_blocks[NDADDR + xap->in_off]);
1915ac2d602Sdownsj
1925ac2d602Sdownsj devvp = VFSTOUFS(vp->v_mount)->um_devvp;
193891e3b90Sart
194891e3b90Sart #ifdef DIAGNOSTIC
195891e3b90Sart if (num > NIADDR + 1 || num < 1) {
196891e3b90Sart printf("ext2fs_bmaparray: num=%d\n", num);
197891e3b90Sart panic("ext2fs_bmaparray: num");
198891e3b90Sart }
199891e3b90Sart #endif
2005ac2d602Sdownsj for (bp = NULL, ++xap; --num; ++xap) {
2015ac2d602Sdownsj /*
2025ac2d602Sdownsj * Exit the loop if there is no disk address assigned yet and
2035ac2d602Sdownsj * the indirect block isn't in the cache, or if we were
2045ac2d602Sdownsj * looking for an indirect block and we've found it.
2055ac2d602Sdownsj */
2065ac2d602Sdownsj
2075ac2d602Sdownsj metalbn = xap->in_lbn;
2085ac2d602Sdownsj if ((daddr == 0 && !incore(vp, metalbn)) || metalbn == bn)
2095ac2d602Sdownsj break;
2105ac2d602Sdownsj /*
2115ac2d602Sdownsj * If we get here, we've either got the block in the cache
2125ac2d602Sdownsj * or we have a disk address for it, go fetch it.
2135ac2d602Sdownsj */
2145ac2d602Sdownsj if (bp)
2155ac2d602Sdownsj brelse(bp);
2165ac2d602Sdownsj
2175ac2d602Sdownsj xap->in_exists = 1;
218570df5c4Scheloha bp = getblk(vp, metalbn, mp->mnt_stat.f_iosize, 0, INFSLP);
2195ac2d602Sdownsj if (bp->b_flags & (B_DONE | B_DELWRI)) {
2200674f943Smickey ;
2215ac2d602Sdownsj }
2225ac2d602Sdownsj #ifdef DIAGNOSTIC
2235ac2d602Sdownsj else if (!daddr)
2245ac2d602Sdownsj panic("ext2fs_bmaparry: indirect block not in cache");
2255ac2d602Sdownsj #endif
2265ac2d602Sdownsj else {
2275ac2d602Sdownsj bp->b_blkno = blkptrtodb(ump, daddr);
2285ac2d602Sdownsj bp->b_flags |= B_READ;
229f1993be3Svisa VOP_STRATEGY(bp->b_vp, bp);
2308f15e6a4Sguenther curproc->p_ru.ru_inblock++; /* XXX */
231c10f2b41Sderaadt bcstats.pendingreads++;
2325ac2d602Sdownsj if ((error = biowait(bp)) != 0) {
2335ac2d602Sdownsj brelse(bp);
2345ac2d602Sdownsj return (error);
2355ac2d602Sdownsj }
2365ac2d602Sdownsj }
2375ac2d602Sdownsj
238f7dbefaaSpelikan daddr = letoh32(((int32_t *)bp->b_data)[xap->in_off]);
2395ac2d602Sdownsj if (num == 1 && daddr && runp)
2405ac2d602Sdownsj for (bn = xap->in_off + 1;
2415ac2d602Sdownsj bn < MNINDIR(ump) && *runp < maxrun &&
2425970a65fSpelikan is_sequential(ump, ((u_int32_t *)bp->b_data)[bn - 1],
2435970a65fSpelikan ((u_int32_t *)bp->b_data)[bn]);
2445970a65fSpelikan ++bn, ++*runp)
2455970a65fSpelikan /* nothing */;
2465ac2d602Sdownsj }
2475ac2d602Sdownsj if (bp)
2485ac2d602Sdownsj brelse(bp);
2495ac2d602Sdownsj
2505ac2d602Sdownsj daddr = blkptrtodb(ump, daddr);
2515ac2d602Sdownsj *bnp = daddr == 0 ? -1 : daddr;
2525ac2d602Sdownsj return (0);
2535ac2d602Sdownsj }
254