opnsense-src/sys/fs/ext2fs/ext2_bmap.c

515 lines
13 KiB
C
Raw Normal View History

/*-
* SPDX-License-Identifier: BSD-3-Clause
*
1994-05-24 06:09:53 -04: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.
* 3. Neither the name of the University nor the names of its contributors
1994-05-24 06:09:53 -04: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.
*
* @(#)ufs_bmap.c 8.7 (Berkeley) 3/21/95
1999-08-27 21:08:13 -04:00
* $FreeBSD$
1994-05-24 06:09:53 -04:00
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bio.h>
1994-05-24 06:09:53 -04:00
#include <sys/buf.h>
#include <sys/endian.h>
1994-05-24 06:09:53 -04:00
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/racct.h>
1994-05-24 06:09:53 -04:00
#include <sys/resourcevar.h>
#include <sys/stat.h>
1994-05-24 06:09:53 -04:00
#include <fs/ext2fs/fs.h>
#include <fs/ext2fs/inode.h>
#include <fs/ext2fs/ext2fs.h>
#include <fs/ext2fs/ext2_dinode.h>
#include <fs/ext2fs/ext2_extern.h>
#include <fs/ext2fs/ext2_mount.h>
1994-05-24 06:09:53 -04:00
/*
* Bmap converts the logical block number of a file to its physical block
1994-05-24 06:09:53 -04: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
ext2_bmap(struct vop_bmap_args *ap)
1994-05-24 06:09:53 -04:00
{
daddr_t blkno;
int error;
1994-05-24 06:09:53 -04:00
/*
* Check for underlying vnode requests and ensure that logical
* to physical mapping is requested.
*/
if (ap->a_bop != NULL)
*ap->a_bop = &VTOI(ap->a_vp)->i_devvp->v_bufobj;
1994-05-24 06:09:53 -04:00
if (ap->a_bnp == NULL)
return (0);
if (VTOI(ap->a_vp)->i_flag & IN_E4EXTENTS)
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);
*ap->a_bnp = blkno;
return (error);
1994-05-24 06:09:53 -04:00
}
/*
* Convert the logical block number of a file to its physical block number
* on the disk within ext4 extents.
*/
int
ext4_bmapext(struct vnode *vp, int32_t bn, int64_t *bnp, int *runp, int *runb)
{
struct inode *ip;
struct m_ext2fs *fs;
struct mount *mp;
struct ext2mount *ump;
struct ext4_extent_header *ehp;
struct ext4_extent *ep;
struct ext4_extent_path *path = NULL;
daddr_t lbn;
int error, depth, maxrun = 0, bsize;
ip = VTOI(vp);
fs = ip->i_e2fs;
mp = vp->v_mount;
ump = VFSTOEXT2(mp);
lbn = bn;
ehp = (struct ext4_extent_header *)ip->i_data;
depth = le16toh(ehp->eh_depth);
bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
*bnp = -1;
if (runp != NULL) {
maxrun = mp->mnt_iosize_max / bsize - 1;
*runp = 0;
}
if (runb != NULL)
*runb = 0;
error = ext4_ext_find_extent(ip, lbn, &path);
if (error)
return (error);
ep = path[depth].ep_ext;
if(ep) {
if (lbn < le32toh(ep->e_blk)) {
if (runp != NULL) {
*runp = min(maxrun, le32toh(ep->e_blk) - lbn - 1);
}
} else if (le32toh(ep->e_blk) <= lbn &&
lbn < le32toh(ep->e_blk) + le16toh(ep->e_len)) {
*bnp = fsbtodb(fs, lbn - le32toh(ep->e_blk) +
(le32toh(ep->e_start_lo) |
(daddr_t)le16toh(ep->e_start_hi) << 32));
if (runp != NULL) {
*runp = min(maxrun,
le16toh(ep->e_len) -
(lbn - le32toh(ep->e_blk)) - 1);
}
if (runb != NULL)
*runb = min(maxrun, lbn - le32toh(ep->e_blk));
} else {
if (runb != NULL)
*runb = min(maxrun, le32toh(ep->e_blk) + lbn -
le16toh(ep->e_len));
}
}
ext4_ext_path_free(path);
return (error);
}
static int
readindir(struct vnode *vp, e2fs_lbn_t lbn, e2fs_daddr_t daddr, struct buf **bpp)
{
struct buf *bp;
struct mount *mp;
struct ext2mount *ump;
int error;
mp = vp->v_mount;
ump = VFSTOEXT2(mp);
bp = getblk(vp, lbn, mp->mnt_stat.f_iosize, 0, 0, 0);
if ((bp->b_flags & B_CACHE) == 0) {
KASSERT(daddr != 0,
("readindir: indirect block not in cache"));
bp->b_blkno = blkptrtodb(ump, daddr);
bp->b_iocmd = BIO_READ;
bp->b_flags &= ~B_INVAL;
bp->b_ioflags &= ~BIO_ERROR;
vfs_busy_pages(bp, 0);
bp->b_iooffset = dbtob(bp->b_blkno);
bstrategy(bp);
#ifdef RACCT
if (racct_enable) {
PROC_LOCK(curproc);
racct_add_buf(curproc, bp, 0);
PROC_UNLOCK(curproc);
}
#endif
curthread->td_ru.ru_inblock++;
error = bufwait(bp);
if (error != 0) {
brelse(bp);
return (error);
}
}
*bpp = bp;
return (0);
}
1994-05-24 06:09:53 -04: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.
*
* ext2_bmaparray does the bmap conversion, and if requested returns the
1994-05-24 06:09:53 -04: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
ext2_bmaparray(struct vnode *vp, daddr_t bn, daddr_t *bnp, int *runp, int *runb)
1994-05-24 06:09:53 -04:00
{
struct inode *ip;
1994-05-24 06:09:53 -04:00
struct buf *bp;
struct ext2mount *ump;
1994-05-24 06:09:53 -04:00
struct mount *mp;
struct indir a[EXT2_NIADDR + 1], *ap;
daddr_t daddr;
e2fs_lbn_t metalbn;
int error, num, maxrun = 0, bsize;
int *nump;
1994-05-24 06:09:53 -04:00
ap = NULL;
1994-05-24 06:09:53 -04:00
ip = VTOI(vp);
mp = vp->v_mount;
ump = VFSTOEXT2(mp);
1994-05-24 06:09:53 -04:00
bsize = EXT2_BLOCK_SIZE(ump->um_e2fs);
1994-05-24 06:09:53 -04:00
if (runp) {
maxrun = mp->mnt_iosize_max / bsize - 1;
*runp = 0;
}
if (runb)
*runb = 0;
ap = a;
nump = &num;
error = ext2_getlbns(vp, bn, ap, nump);
if (error)
1994-05-24 06:09:53 -04:00
return (error);
num = *nump;
if (num == 0) {
*bnp = blkptrtodb(ump, ip->i_db[bn]);
if (*bnp == 0) {
This patch corrects the first round of panics and hangs reported with the new snapshot code. Update addaliasu to correctly implement the semantics of the old checkalias function. When a device vnode first comes into existence, check to see if an anonymous vnode for the same device was created at boot time by bdevvp(). If so, adopt the bdevvp vnode rather than creating a new vnode for the device. This corrects a problem which caused the kernel to panic when taking a snapshot of the root filesystem. Change the calling convention of vn_write_suspend_wait() to be the same as vn_start_write(). Split out softdep_flushworklist() from softdep_flushfiles() so that it can be used to clear the work queue when suspending filesystem operations. Access to buffers becomes recursive so that snapshots can recursively traverse their indirect blocks using ffs_copyonwrite() when checking for the need for copy on write when flushing one of their own indirect blocks. This eliminates a deadlock between the syncer daemon and a process taking a snapshot. Ensure that softdep_process_worklist() can never block because of a snapshot being taken. This eliminates a problem with buffer starvation. Cleanup change in ffs_sync() which did not synchronously wait when MNT_WAIT was specified. The result was an unclean filesystem panic when doing forcible unmount with heavy filesystem I/O in progress. Return a zero'ed block when reading a block that was not in use at the time that a snapshot was taken. Normally, these blocks should never be read. However, the readahead code will occationally read them which can cause unexpected behavior. Clean up the debugging code that ensures that no blocks be written on a filesystem while it is suspended. Snapshots must explicitly label the blocks that they are writing during the suspension so that they do not cause a `write on suspended filesystem' panic. Reorganize ffs_copyonwrite() to eliminate a deadlock and also to prevent a race condition that would permit the same block to be copied twice. This change eliminates an unexpected soft updates inconsistency in fsck caused by the double allocation. Use bqrelse rather than brelse for buffers that will be needed soon again by the snapshot code. This improves snapshot performance.
2000-07-24 01:28:33 -04:00
*bnp = -1;
} else if (runp) {
daddr_t bnb = bn;
for (++bn; bn < EXT2_NDADDR && *runp < maxrun &&
1994-05-24 06:09:53 -04:00
is_sequential(ump, ip->i_db[bn - 1], ip->i_db[bn]);
++bn, ++*runp);
bn = bnb;
if (runb && (bn > 0)) {
for (--bn; (bn >= 0) && (*runb < maxrun) &&
is_sequential(ump, ip->i_db[bn],
ip->i_db[bn + 1]);
--bn, ++*runb);
}
}
1994-05-24 06:09:53 -04:00
return (0);
}
/* Get disk address out of indirect block array */
daddr = ip->i_ib[ap->in_off];
1994-05-24 06:09:53 -04:00
for (bp = NULL, ++ap; --num; ++ap) {
1995-05-30 04:16:23 -04:00
/*
1994-05-24 06:09:53 -04: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.
*/
metalbn = ap->in_lbn;
if ((daddr == 0 && !incore(&vp->v_bufobj, metalbn)) || metalbn == bn)
1994-05-24 06:09:53 -04: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)
bqrelse(bp);
error = readindir(vp, metalbn, daddr, &bp);
if (error != 0)
return (error);
1994-05-24 06:09:53 -04:00
daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[ap->in_off]);
if (num == 1 && daddr && runp) {
for (bn = ap->in_off + 1;
1994-05-24 06:09:53 -04:00
bn < MNINDIR(ump) && *runp < maxrun &&
is_sequential(ump,
((e2fs_daddr_t *)bp->b_data)[bn - 1],
((e2fs_daddr_t *)bp->b_data)[bn]);
1994-05-24 06:09:53 -04:00
++bn, ++*runp);
bn = ap->in_off;
if (runb && bn) {
for (--bn; bn >= 0 && *runb < maxrun &&
is_sequential(ump,
((e2fs_daddr_t *)bp->b_data)[bn],
((e2fs_daddr_t *)bp->b_data)[bn + 1]);
--bn, ++*runb);
}
}
1994-05-24 06:09:53 -04:00
}
if (bp)
bqrelse(bp);
1994-05-24 06:09:53 -04:00
This patch corrects the first round of panics and hangs reported with the new snapshot code. Update addaliasu to correctly implement the semantics of the old checkalias function. When a device vnode first comes into existence, check to see if an anonymous vnode for the same device was created at boot time by bdevvp(). If so, adopt the bdevvp vnode rather than creating a new vnode for the device. This corrects a problem which caused the kernel to panic when taking a snapshot of the root filesystem. Change the calling convention of vn_write_suspend_wait() to be the same as vn_start_write(). Split out softdep_flushworklist() from softdep_flushfiles() so that it can be used to clear the work queue when suspending filesystem operations. Access to buffers becomes recursive so that snapshots can recursively traverse their indirect blocks using ffs_copyonwrite() when checking for the need for copy on write when flushing one of their own indirect blocks. This eliminates a deadlock between the syncer daemon and a process taking a snapshot. Ensure that softdep_process_worklist() can never block because of a snapshot being taken. This eliminates a problem with buffer starvation. Cleanup change in ffs_sync() which did not synchronously wait when MNT_WAIT was specified. The result was an unclean filesystem panic when doing forcible unmount with heavy filesystem I/O in progress. Return a zero'ed block when reading a block that was not in use at the time that a snapshot was taken. Normally, these blocks should never be read. However, the readahead code will occationally read them which can cause unexpected behavior. Clean up the debugging code that ensures that no blocks be written on a filesystem while it is suspended. Snapshots must explicitly label the blocks that they are writing during the suspension so that they do not cause a `write on suspended filesystem' panic. Reorganize ffs_copyonwrite() to eliminate a deadlock and also to prevent a race condition that would permit the same block to be copied twice. This change eliminates an unexpected soft updates inconsistency in fsck caused by the double allocation. Use bqrelse rather than brelse for buffers that will be needed soon again by the snapshot code. This improves snapshot performance.
2000-07-24 01:28:33 -04: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.
*/
if ((ip->i_flags & SF_SNAPSHOT) && daddr > 0 && daddr < ump->um_seqinc) {
This patch corrects the first round of panics and hangs reported with the new snapshot code. Update addaliasu to correctly implement the semantics of the old checkalias function. When a device vnode first comes into existence, check to see if an anonymous vnode for the same device was created at boot time by bdevvp(). If so, adopt the bdevvp vnode rather than creating a new vnode for the device. This corrects a problem which caused the kernel to panic when taking a snapshot of the root filesystem. Change the calling convention of vn_write_suspend_wait() to be the same as vn_start_write(). Split out softdep_flushworklist() from softdep_flushfiles() so that it can be used to clear the work queue when suspending filesystem operations. Access to buffers becomes recursive so that snapshots can recursively traverse their indirect blocks using ffs_copyonwrite() when checking for the need for copy on write when flushing one of their own indirect blocks. This eliminates a deadlock between the syncer daemon and a process taking a snapshot. Ensure that softdep_process_worklist() can never block because of a snapshot being taken. This eliminates a problem with buffer starvation. Cleanup change in ffs_sync() which did not synchronously wait when MNT_WAIT was specified. The result was an unclean filesystem panic when doing forcible unmount with heavy filesystem I/O in progress. Return a zero'ed block when reading a block that was not in use at the time that a snapshot was taken. Normally, these blocks should never be read. However, the readahead code will occationally read them which can cause unexpected behavior. Clean up the debugging code that ensures that no blocks be written on a filesystem while it is suspended. Snapshots must explicitly label the blocks that they are writing during the suspension so that they do not cause a `write on suspended filesystem' panic. Reorganize ffs_copyonwrite() to eliminate a deadlock and also to prevent a race condition that would permit the same block to be copied twice. This change eliminates an unexpected soft updates inconsistency in fsck caused by the double allocation. Use bqrelse rather than brelse for buffers that will be needed soon again by the snapshot code. This improves snapshot performance.
2000-07-24 01:28:33 -04:00
*bnp = -1;
return (0);
}
*bnp = blkptrtodb(ump, daddr);
if (*bnp == 0) {
*bnp = -1;
}
1994-05-24 06:09:53 -04:00
return (0);
}
static e2fs_lbn_t
lbn_count(struct ext2mount *ump, int level)
{
e2fs_lbn_t blockcnt;
for (blockcnt = 1; level > 0; level--)
blockcnt *= MNINDIR(ump);
return (blockcnt);
}
int
ext2_bmap_seekdata(struct vnode *vp, off_t *offp)
{
struct buf *bp;
struct indir a[EXT2_NIADDR + 1], *ap;
struct inode *ip;
struct mount *mp;
struct ext2mount *ump;
e2fs_daddr_t bn, daddr, nextbn;
uint64_t bsize;
off_t numblks;
int error, num, num1, off;
bp = NULL;
error = 0;
ip = VTOI(vp);
mp = vp->v_mount;
ump = VFSTOEXT2(mp);
if (vp->v_type != VREG || (ip->i_flags & SF_SNAPSHOT) != 0)
return (EINVAL);
if (*offp < 0 || *offp >= ip->i_size)
return (ENXIO);
bsize = mp->mnt_stat.f_iosize;
for (bn = *offp / bsize, numblks = howmany(ip->i_size, bsize);
bn < numblks; bn = nextbn) {
if (bn < EXT2_NDADDR) {
daddr = ip->i_db[bn];
if (daddr != 0)
break;
nextbn = bn + 1;
continue;
}
ap = a;
error = ext2_getlbns(vp, bn, ap, &num);
if (error != 0)
break;
MPASS(num >= 2);
daddr = ip->i_ib[ap->in_off];
ap++, num--;
for (nextbn = EXT2_NDADDR, num1 = num - 1; num1 > 0; num1--)
nextbn += lbn_count(ump, num1);
if (daddr == 0) {
nextbn += lbn_count(ump, num);
continue;
}
for (; daddr != 0 && num > 0; ap++, num--) {
if (bp != NULL)
bqrelse(bp);
error = readindir(vp, ap->in_lbn, daddr, &bp);
if (error != 0)
return (error);
/*
* Scan the indirect block until we find a non-zero
* pointer.
*/
off = ap->in_off;
do {
daddr = le32toh(((e2fs_daddr_t *)bp->b_data)[off]);
} while (daddr == 0 && ++off < MNINDIR(ump));
nextbn += off * lbn_count(ump, num - 1);
/*
* We need to recompute the LBNs of indirect
* blocks, so restart with the updated block offset.
*/
if (off != ap->in_off)
break;
}
if (num == 0) {
/*
* We found a data block.
*/
bn = nextbn;
break;
}
}
if (bp != NULL)
bqrelse(bp);
if (bn >= numblks)
error = ENXIO;
if (error == 0 && *offp < bn * bsize)
*offp = bn * bsize;
return (error);
}
1994-05-24 06:09:53 -04:00
/*
* 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
ext2_getlbns(struct vnode *vp, daddr_t bn, struct indir *ap, int *nump)
1994-05-24 06:09:53 -04:00
{
long blockcnt;
e2fs_lbn_t metalbn, realbn;
struct ext2mount *ump;
int i, numlevels, off;
int64_t qblockcnt;
1994-05-24 06:09:53 -04:00
ump = VFSTOEXT2(vp->v_mount);
1994-05-24 06:09:53 -04:00
if (nump)
*nump = 0;
numlevels = 0;
realbn = bn;
if ((long)bn < 0)
bn = -(long)bn;
/* The first EXT2_NDADDR blocks are direct blocks. */
if (bn < EXT2_NDADDR)
1994-05-24 06:09:53 -04:00
return (0);
1995-05-30 04:16:23 -04:00
/*
1994-05-24 06:09:53 -04:00
* Determine the number of levels of indirection. After this loop
* is done, blockcnt indicates the number of data blocks possible
* 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 06:09:53 -04:00
*/
for (blockcnt = 1, i = EXT2_NIADDR, bn -= EXT2_NDADDR; ;
i--, bn -= blockcnt) {
1994-05-24 06:09:53 -04:00
if (i == 0)
return (EFBIG);
/*
* 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 06:09:53 -04:00
break;
blockcnt = qblockcnt;
1994-05-24 06:09:53 -04:00
}
/* Calculate the address of the first meta-block. */
if (realbn >= 0)
metalbn = -(realbn - bn + EXT2_NIADDR - i);
1994-05-24 06:09:53 -04:00
else
metalbn = -(-realbn - bn + EXT2_NIADDR - i);
1994-05-24 06:09:53 -04:00
1995-05-30 04:16:23 -04:00
/*
1994-05-24 06:09:53 -04: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;
ap->in_off = off = EXT2_NIADDR - i;
1994-05-24 06:09:53 -04:00
ap++;
for (++numlevels; i <= EXT2_NIADDR; i++) {
1994-05-24 06:09:53 -04: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;
blockcnt /= MNINDIR(ump);
1994-05-24 06:09:53 -04:00
}
if (nump)
*nump = numlevels;
return (0);
}