2005-01-06 18:27:30 +00:00
|
|
|
/*-
|
1994-05-24 10:09:53 +00:00
|
|
|
* Copyright (c) 1989, 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2016-12-02 19:47:23 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-24 10:09:53 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1997-02-10 02:22:35 +00:00
|
|
|
* @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1995-03-28 07:58:53 +00:00
|
|
|
#include <sys/systm.h>
|
2000-05-05 09:59:14 +00:00
|
|
|
#include <sys/bio.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/mount.h>
|
2016-04-07 04:23:25 +00:00
|
|
|
#include <sys/racct.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/resourcevar.h>
|
2000-07-11 22:07:57 +00:00
|
|
|
#include <sys/stat.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2010-01-14 14:30:54 +00:00
|
|
|
#include <fs/ext2fs/inode.h>
|
2013-08-12 21:34:48 +00:00
|
|
|
#include <fs/ext2fs/fs.h>
|
2010-01-14 14:30:54 +00:00
|
|
|
#include <fs/ext2fs/ext2fs.h>
|
2013-08-12 21:34:48 +00:00
|
|
|
#include <fs/ext2fs/ext2_dinode.h>
|
2010-01-14 14:30:54 +00:00
|
|
|
#include <fs/ext2fs/ext2_extern.h>
|
2013-06-03 20:02:45 +00:00
|
|
|
#include <fs/ext2fs/ext2_mount.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2013-08-12 21:34:48 +00:00
|
|
|
static int ext4_bmapext(struct vnode *, int32_t, int64_t *, int *, int *);
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
2010-01-14 14:30:54 +00:00
|
|
|
* Bmap converts the logical block number of a file to its physical block
|
1994-05-24 10:09:53 +00:00
|
|
|
* number on the disk. The conversion is done by using the logical block
|
|
|
|
* number to index into the array of block pointers described by the dinode.
|
|
|
|
*/
|
|
|
|
int
|
2013-02-10 19:49:37 +00:00
|
|
|
ext2_bmap(struct vop_bmap_args *ap)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2013-08-13 15:40:43 +00:00
|
|
|
daddr_t blkno;
|
2002-03-15 18:49:47 +00:00
|
|
|
int error;
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Check for underlying vnode requests and ensure that logical
|
|
|
|
* to physical mapping is requested.
|
|
|
|
*/
|
2004-11-15 09:18:27 +00:00
|
|
|
if (ap->a_bop != NULL)
|
|
|
|
*ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
|
1994-05-24 10:09:53 +00:00
|
|
|
if (ap->a_bnp == NULL)
|
|
|
|
return (0);
|
|
|
|
|
2014-01-28 14:39:05 +00:00
|
|
|
if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
|
2013-08-12 21:34:48 +00:00
|
|
|
error = ext4_bmapext(ap->a_vp, ap->a_bn, &blkno,
|
|
|
|
ap->a_runp, ap->a_runb);
|
|
|
|
else
|
|
|
|
error = ext2_bmaparray(ap->a_vp, ap->a_bn, &blkno,
|
|
|
|
ap->a_runp, ap->a_runb);
|
2002-03-15 18:49:47 +00:00
|
|
|
*ap->a_bnp = blkno;
|
|
|
|
return (error);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 21:34:48 +00:00
|
|
|
/*
|
2016-06-07 17:08:34 +00:00
|
|
|
* Convert the logical block number of a file to its physical block number
|
|
|
|
* on the disk within ext4 extents.
|
2013-08-12 21:34:48 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
|
|
|
|
{
|
|
|
|
struct inode *ip;
|
|
|
|
struct m_ext2fs *fs;
|
|
|
|
struct ext4_extent *ep;
|
2016-12-28 15:43:17 +00:00
|
|
|
struct ext4_extent_path path = {.ep_bp = NULL};
|
2013-08-12 21:34:48 +00:00
|
|
|
daddr_t lbn;
|
2016-06-07 18:23:22 +00:00
|
|
|
int error;
|
2013-08-12 21:34:48 +00:00
|
|
|
|
|
|
|
ip = VTOI(vp);
|
|
|
|
fs = ip->i_e2fs;
|
|
|
|
lbn = bn;
|
|
|
|
|
|
|
|
if (runp != NULL)
|
|
|
|
*runp = 0;
|
|
|
|
if (runb != NULL)
|
|
|
|
*runb = 0;
|
2016-06-07 18:23:22 +00:00
|
|
|
error = 0;
|
2013-08-12 21:34:48 +00:00
|
|
|
|
|
|
|
ext4_ext_find_extent(fs, ip, lbn, &path);
|
2016-01-11 19:14:55 +00:00
|
|
|
if (path.ep_is_sparse) {
|
|
|
|
*bnp = -1;
|
|
|
|
if (runp != NULL)
|
|
|
|
*runp = path.ep_sparse_ext.e_len -
|
|
|
|
(lbn - path.ep_sparse_ext.e_blk) - 1;
|
2016-02-11 00:34:11 +00:00
|
|
|
if (runb != NULL)
|
|
|
|
*runb = lbn - path.ep_sparse_ext.e_blk;
|
2016-01-11 19:14:55 +00:00
|
|
|
} else {
|
2016-06-08 02:39:10 +00:00
|
|
|
if (path.ep_ext == NULL) {
|
2016-06-07 17:08:34 +00:00
|
|
|
error = EIO;
|
2016-06-07 18:23:22 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ep = path.ep_ext;
|
|
|
|
*bnp = fsbtodb(fs, lbn - ep->e_blk +
|
|
|
|
(ep->e_start_lo | (daddr_t)ep->e_start_hi << 32));
|
2016-01-11 19:14:55 +00:00
|
|
|
|
2016-06-07 18:23:22 +00:00
|
|
|
if (*bnp == 0)
|
|
|
|
*bnp = -1;
|
2016-01-11 19:14:55 +00:00
|
|
|
|
2016-06-07 18:23:22 +00:00
|
|
|
if (runp != NULL)
|
|
|
|
*runp = ep->e_len - (lbn - ep->e_blk) - 1;
|
|
|
|
if (runb != NULL)
|
|
|
|
*runb = lbn - ep->e_blk;
|
2016-01-07 21:43:43 +00:00
|
|
|
}
|
2013-08-12 21:34:48 +00:00
|
|
|
|
2016-06-07 18:23:22 +00:00
|
|
|
out:
|
|
|
|
if (path.ep_bp != NULL)
|
2016-01-07 21:43:43 +00:00
|
|
|
brelse(path.ep_bp);
|
2013-08-12 21:34:48 +00:00
|
|
|
|
2016-06-07 17:08:34 +00:00
|
|
|
return (error);
|
2013-08-12 21:34:48 +00:00
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Indirect blocks are now on the vnode for the file. They are given negative
|
|
|
|
* logical block numbers. Indirect blocks are addressed by the negative
|
|
|
|
* address of the first data block to which they point. Double indirect blocks
|
|
|
|
* are addressed by one less than the address of the first indirect block to
|
|
|
|
* which they point. Triple indirect blocks are addressed by one less than
|
|
|
|
* the address of the first double indirect block to which they point.
|
|
|
|
*
|
2010-01-14 14:30:54 +00:00
|
|
|
* ext2_bmaparray does the bmap conversion, and if requested returns the
|
1994-05-24 10:09:53 +00:00
|
|
|
* array of logical blocks which must be traversed to get to a block.
|
|
|
|
* Each entry contains the offset into that block that gets you to the
|
|
|
|
* next block and the disk address of the block (if it is assigned).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2013-08-13 15:40:43 +00:00
|
|
|
ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2002-05-13 09:22:31 +00:00
|
|
|
struct inode *ip;
|
1994-05-24 10:09:53 +00:00
|
|
|
struct buf *bp;
|
2002-05-14 17:14:01 +00:00
|
|
|
struct ext2mount *ump;
|
1994-05-24 10:09:53 +00:00
|
|
|
struct mount *mp;
|
2017-02-15 19:50:26 +00:00
|
|
|
struct indir a[EXT2_NIADDR + 1], *ap;
|
2013-06-23 02:44:42 +00:00
|
|
|
daddr_t daddr;
|
|
|
|
e2fs_lbn_t metalbn;
|
2004-10-29 10:42:30 +00:00
|
|
|
int error, num, maxrun = 0, bsize;
|
2001-04-29 10:24:58 +00:00
|
|
|
int *nump;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2001-04-29 10:24:58 +00:00
|
|
|
ap = NULL;
|
1994-05-24 10:09:53 +00:00
|
|
|
ip = VTOI(vp);
|
|
|
|
mp = vp->v_mount;
|
2002-05-14 17:14:01 +00:00
|
|
|
ump = VFSTOEXT2(mp);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2004-10-29 10:42:30 +00:00
|
|
|
bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
if (runp) {
|
2004-10-29 10:42:30 +00:00
|
|
|
maxrun = mp->mnt_iosize_max / bsize - 1;
|
1998-01-24 02:01:46 +00:00
|
|
|
*runp = 0;
|
|
|
|
}
|
2016-12-28 15:43:17 +00:00
|
|
|
if (runb)
|
1998-01-24 02:01:46 +00:00
|
|
|
*runb = 0;
|
|
|
|
|
1995-09-04 00:21:16 +00:00
|
|
|
|
2001-04-29 10:24:58 +00:00
|
|
|
ap = a;
|
|
|
|
nump = #
|
2002-05-14 17:14:01 +00:00
|
|
|
error = ext2_getlbns(vp, bn, ap, nump);
|
1994-10-08 06:57:29 +00:00
|
|
|
if (error)
|
1994-05-24 10:09:53 +00:00
|
|
|
return (error);
|
|
|
|
|
|
|
|
num = *nump;
|
|
|
|
if (num == 0) {
|
|
|
|
*bnp = blkptrtodb(ump, ip->i_db[bn]);
|
2002-05-14 17:14:01 +00:00
|
|
|
if (*bnp == 0) {
|
2000-07-24 05:28:33 +00:00
|
|
|
*bnp = -1;
|
2000-07-11 22:07:57 +00:00
|
|
|
} else if (runp) {
|
2013-08-13 15:40:43 +00:00
|
|
|
daddr_t bnb = bn;
|
2016-12-28 15:43:17 +00:00
|
|
|
|
2017-02-15 19:50:26 +00:00
|
|
|
for (++bn; bn < EXT2_NDADDR && *runp < maxrun &&
|
1994-05-24 10:09:53 +00:00
|
|
|
is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
|
|
|
|
++bn, ++*runp);
|
1995-09-04 00:21:16 +00:00
|
|
|
bn = bnb;
|
|
|
|
if (runb && (bn > 0)) {
|
|
|
|
for (--bn; (bn >= 0) && (*runb < maxrun) &&
|
|
|
|
is_sequential(ump, ip->i_db[bn],
|
2013-08-13 15:40:43 +00:00
|
|
|
ip->i_db[bn + 1]);
|
1995-09-04 00:21:16 +00:00
|
|
|
--bn, ++*runb);
|
|
|
|
}
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get disk address out of indirect block array */
|
2001-04-29 10:24:58 +00:00
|
|
|
daddr = ip->i_ib[ap->in_off];
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2001-04-29 10:24:58 +00:00
|
|
|
for (bp = NULL, ++ap; --num; ++ap) {
|
1995-05-30 08:16:23 +00:00
|
|
|
/*
|
1994-05-24 10:09:53 +00:00
|
|
|
* Exit the loop if there is no disk address assigned yet and
|
|
|
|
* the indirect block isn't in the cache, or if we were
|
|
|
|
* looking for an indirect block and we've found it.
|
|
|
|
*/
|
|
|
|
|
2001-04-29 10:24:58 +00:00
|
|
|
metalbn = ap->in_lbn;
|
2004-10-22 08:47:20 +00:00
|
|
|
if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* If we get here, we've either got the block in the cache
|
|
|
|
* or we have a disk address for it, go fetch it.
|
|
|
|
*/
|
|
|
|
if (bp)
|
1996-01-19 04:00:31 +00:00
|
|
|
bqrelse(bp);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2004-10-29 10:42:30 +00:00
|
|
|
bp = getblk(vp, metalbn, bsize, 0, 0, 0);
|
1995-03-04 03:24:45 +00:00
|
|
|
if ((bp->b_flags & B_CACHE) == 0) {
|
2013-06-17 00:39:23 +00:00
|
|
|
#ifdef INVARIANTS
|
1995-03-04 03:24:45 +00:00
|
|
|
if (!daddr)
|
2012-08-18 16:13:16 +00:00
|
|
|
panic("ext2_bmaparray: indirect block not in cache");
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
bp->b_blkno = blkptrtodb(ump, daddr);
|
2000-03-20 10:44:49 +00:00
|
|
|
bp->b_iocmd = BIO_READ;
|
2000-04-02 15:24:56 +00:00
|
|
|
bp->b_flags &= ~B_INVAL;
|
|
|
|
bp->b_ioflags &= ~BIO_ERROR;
|
These changes embody the support of the fully coherent merged VM buffer cache,
much higher filesystem I/O performance, and much better paging performance. It
represents the culmination of over 6 months of R&D.
The majority of the merged VM/cache work is by John Dyson.
The following highlights the most significant changes. Additionally, there are
(mostly minor) changes to the various filesystem modules (nfs, msdosfs, etc) to
support the new VM/buffer scheme.
vfs_bio.c:
Significant rewrite of most of vfs_bio to support the merged VM buffer cache
scheme. The scheme is almost fully compatible with the old filesystem
interface. Significant improvement in the number of opportunities for write
clustering.
vfs_cluster.c, vfs_subr.c
Upgrade and performance enhancements in vfs layer code to support merged
VM/buffer cache. Fixup of vfs_cluster to eliminate the bogus pagemove stuff.
vm_object.c:
Yet more improvements in the collapse code. Elimination of some windows that
can cause list corruption.
vm_pageout.c:
Fixed it, it really works better now. Somehow in 2.0, some "enhancements"
broke the code. This code has been reworked from the ground-up.
vm_fault.c, vm_page.c, pmap.c, vm_object.c
Support for small-block filesystems with merged VM/buffer cache scheme.
pmap.c vm_map.c
Dynamic kernel VM size, now we dont have to pre-allocate excessive numbers of
kernel PTs.
vm_glue.c
Much simpler and more effective swapping code. No more gratuitous swapping.
proc.h
Fixed the problem that the p_lock flag was not being cleared on a fork.
swap_pager.c, vnode_pager.c
Removal of old vfs_bio cruft to support the past pseudo-coherency. Now the
code doesn't need it anymore.
machdep.c
Changes to better support the parameter values for the merged VM/buffer cache
scheme.
machdep.c, kern_exec.c, vm_glue.c
Implemented a seperate submap for temporary exec string space and another one
to contain process upages. This eliminates all map fragmentation problems
that previously existed.
ffs_inode.c, ufs_inode.c, ufs_readwrite.c
Changes for merged VM/buffer cache. Add "bypass" support for sneaking in on
busy buffers.
Submitted by: John Dyson and David Greenman
1995-01-09 16:06:02 +00:00
|
|
|
vfs_busy_pages(bp, 0);
|
2003-10-18 14:10:28 +00:00
|
|
|
bp->b_iooffset = dbtob(bp->b_blkno);
|
2004-10-24 20:03:41 +00:00
|
|
|
bstrategy(bp);
|
2016-04-07 04:23:25 +00:00
|
|
|
#ifdef RACCT
|
|
|
|
if (racct_enable) {
|
|
|
|
PROC_LOCK(curproc);
|
|
|
|
racct_add_buf(curproc, bp, 0);
|
|
|
|
PROC_UNLOCK(curproc);
|
|
|
|
}
|
|
|
|
#endif
|
2007-06-01 01:12:45 +00:00
|
|
|
curthread->td_ru.ru_inblock++;
|
2000-04-29 16:25:22 +00:00
|
|
|
error = bufwait(bp);
|
1994-10-08 06:57:29 +00:00
|
|
|
if (error) {
|
1994-05-24 10:09:53 +00:00
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 15:40:43 +00:00
|
|
|
daddr = ((e2fs_daddr_t *)bp->b_data)[ap->in_off];
|
1995-09-04 00:21:16 +00:00
|
|
|
if (num == 1 && daddr && runp) {
|
2001-04-29 10:24:58 +00:00
|
|
|
for (bn = ap->in_off + 1;
|
1994-05-24 10:09:53 +00:00
|
|
|
bn < MNINDIR(ump) && *runp < maxrun &&
|
1997-02-10 02:22:35 +00:00
|
|
|
is_sequential(ump,
|
2013-08-13 15:40:43 +00:00
|
|
|
((e2fs_daddr_t *)bp->b_data)[bn - 1],
|
|
|
|
((e2fs_daddr_t *)bp->b_data)[bn]);
|
1994-05-24 10:09:53 +00:00
|
|
|
++bn, ++*runp);
|
2001-04-29 10:24:58 +00:00
|
|
|
bn = ap->in_off;
|
1995-09-04 00:21:16 +00:00
|
|
|
if (runb && bn) {
|
2011-12-16 15:47:43 +00:00
|
|
|
for (--bn; bn >= 0 && *runb < maxrun &&
|
2014-03-01 21:22:20 +00:00
|
|
|
is_sequential(ump,
|
2013-08-13 15:40:43 +00:00
|
|
|
((e2fs_daddr_t *)bp->b_data)[bn],
|
|
|
|
((e2fs_daddr_t *)bp->b_data)[bn + 1]);
|
2014-03-01 21:22:20 +00:00
|
|
|
--bn, ++*runb);
|
1995-09-04 00:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
if (bp)
|
1996-01-19 04:00:31 +00:00
|
|
|
bqrelse(bp);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2000-07-24 05:28:33 +00:00
|
|
|
/*
|
|
|
|
* Since this is FFS independent code, we are out of scope for the
|
|
|
|
* definitions of BLK_NOCOPY and BLK_SNAP, but we do know that they
|
|
|
|
* will fall in the range 1..um_seqinc, so we use that test and
|
|
|
|
* return a request for a zeroed out buffer if attempts are made
|
|
|
|
* to read a BLK_NOCOPY or BLK_SNAP block.
|
|
|
|
*/
|
2016-12-28 15:43:17 +00:00
|
|
|
if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc) {
|
2000-07-24 05:28:33 +00:00
|
|
|
*bnp = -1;
|
|
|
|
return (0);
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
*bnp = blkptrtodb(ump, daddr);
|
|
|
|
if (*bnp == 0) {
|
2002-05-14 17:14:01 +00:00
|
|
|
*bnp = -1;
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an array of logical block number/offset pairs which represent the
|
|
|
|
* path of indirect blocks required to access a data block. The first "pair"
|
|
|
|
* contains the logical block number of the appropriate single, double or
|
|
|
|
* triple indirect block and the offset into the inode indirect block array.
|
|
|
|
* Note, the logical block number of the inode single/double/triple indirect
|
|
|
|
* block appears twice in the array, once with the offset into the i_ib and
|
|
|
|
* once with the offset into the page itself.
|
|
|
|
*/
|
|
|
|
int
|
2013-08-13 15:40:43 +00:00
|
|
|
ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2013-06-23 02:44:42 +00:00
|
|
|
long blockcnt;
|
|
|
|
e2fs_lbn_t metalbn, realbn;
|
2002-05-14 17:14:01 +00:00
|
|
|
struct ext2mount *ump;
|
1997-11-24 16:33:03 +00:00
|
|
|
int i, numlevels, off;
|
|
|
|
int64_t qblockcnt;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
2002-05-14 17:14:01 +00:00
|
|
|
ump = VFSTOEXT2(vp->v_mount);
|
1994-05-24 10:09:53 +00:00
|
|
|
if (nump)
|
|
|
|
*nump = 0;
|
|
|
|
numlevels = 0;
|
|
|
|
realbn = bn;
|
|
|
|
if ((long)bn < 0)
|
|
|
|
bn = -(long)bn;
|
|
|
|
|
2017-02-15 19:50:26 +00:00
|
|
|
/* The first EXT2_NDADDR blocks are direct blocks. */
|
|
|
|
if (bn < EXT2_NDADDR)
|
1994-05-24 10:09:53 +00:00
|
|
|
return (0);
|
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
/*
|
1994-05-24 10:09:53 +00:00
|
|
|
* Determine the number of levels of indirection. After this loop
|
|
|
|
* is done, blockcnt indicates the number of data blocks possible
|
2017-02-15 19:50:26 +00:00
|
|
|
* at the previous level of indirection, and EXT2_NIADDR - i is the
|
|
|
|
* number of levels of indirection needed to locate the requested block.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
2017-02-15 19:50:26 +00:00
|
|
|
for (blockcnt = 1, i = EXT2_NIADDR, bn -= EXT2_NDADDR; ;
|
|
|
|
i--, bn -= blockcnt) {
|
1994-05-24 10:09:53 +00:00
|
|
|
if (i == 0)
|
|
|
|
return (EFBIG);
|
1997-11-24 16:33:03 +00:00
|
|
|
/*
|
|
|
|
* Use int64_t's here to avoid overflow for triple indirect
|
|
|
|
* blocks when longs have 32 bits and the block size is more
|
|
|
|
* than 4K.
|
|
|
|
*/
|
|
|
|
qblockcnt = (int64_t)blockcnt * MNINDIR(ump);
|
|
|
|
if (bn < qblockcnt)
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
1997-11-24 16:33:03 +00:00
|
|
|
blockcnt = qblockcnt;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the address of the first meta-block. */
|
|
|
|
if (realbn >= 0)
|
2017-02-15 19:50:26 +00:00
|
|
|
metalbn = -(realbn - bn + EXT2_NIADDR - i);
|
1994-05-24 10:09:53 +00:00
|
|
|
else
|
2017-02-15 19:50:26 +00:00
|
|
|
metalbn = -(-realbn - bn + EXT2_NIADDR - i);
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1995-05-30 08:16:23 +00:00
|
|
|
/*
|
1994-05-24 10:09:53 +00:00
|
|
|
* At each iteration, off is the offset into the bap array which is
|
|
|
|
* an array of disk addresses at the current level of indirection.
|
|
|
|
* The logical block number and the offset in that block are stored
|
|
|
|
* into the argument array.
|
|
|
|
*/
|
|
|
|
ap->in_lbn = metalbn;
|
2017-02-15 19:50:26 +00:00
|
|
|
ap->in_off = off = EXT2_NIADDR - i;
|
1994-05-24 10:09:53 +00:00
|
|
|
ap++;
|
2017-02-15 19:50:26 +00:00
|
|
|
for (++numlevels; i <= EXT2_NIADDR; i++) {
|
1994-05-24 10:09:53 +00:00
|
|
|
/* If searching for a meta-data block, quit when found. */
|
|
|
|
if (metalbn == realbn)
|
|
|
|
break;
|
|
|
|
|
|
|
|
off = (bn / blockcnt) % MNINDIR(ump);
|
|
|
|
|
|
|
|
++numlevels;
|
|
|
|
ap->in_lbn = metalbn;
|
|
|
|
ap->in_off = off;
|
|
|
|
++ap;
|
|
|
|
|
|
|
|
metalbn -= -1 + off * blockcnt;
|
1997-11-24 16:33:03 +00:00
|
|
|
blockcnt /= MNINDIR(ump);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
if (nump)
|
|
|
|
*nump = numlevels;
|
|
|
|
return (0);
|
|
|
|
}
|