1997-10-26 02:37:42 +04:00
|
|
|
/*
|
ChangeLog, debugfs.c:
debugfs.c (dump_inode): Remove #ifdef for i_version vs. i_generation
since we know it will always be i_generation now.
ChangeLog, e2fsck.h, flushb.c, scantest.c:
e2fsck.h, flushb.c, scantest.c: Remove uneeded include of linux/fs.h
bmap.c:
Fix silly spelling typo.
ChangeLog, getsize.c, ismounted.c, swapfs.c:
getsize.c, ismounted.c: Remove unneeded include of linux/fs.h
swapfs.c: Remove #ifdef HAVE_EXT2_INODE_VERSION since it's not needed
any more; we know it will always be i_generation. Add support for
swapping the high bits of the uid and gid.
2000-02-02 19:19:59 +03:00
|
|
|
* bmap.c --- logical to physical block mapping
|
1997-10-26 02:37:42 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 1997 Theodore Ts'o.
|
|
|
|
*
|
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Public
|
|
|
|
* License.
|
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
#if EXT2_FLAT_INCLUDES
|
|
|
|
#include "ext2_fs.h"
|
|
|
|
#else
|
1997-10-26 02:37:42 +04:00
|
|
|
#include <linux/ext2_fs.h>
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
#endif
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
#include "ext2fs.h"
|
|
|
|
|
1997-10-26 04:53:39 +03:00
|
|
|
#if defined(__GNUC__) && !defined(NO_INLINE_FUNCS)
|
1997-10-26 02:37:42 +04:00
|
|
|
#define _BMAP_INLINE_ __inline__
|
|
|
|
#else
|
|
|
|
#define _BMAP_INLINE_
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern errcode_t ext2fs_bmap(ext2_filsys fs, ino_t ino,
|
|
|
|
struct ext2_inode *inode,
|
|
|
|
char *block_buf, int bmap_flags,
|
|
|
|
blk_t block, blk_t *phys_blk);
|
|
|
|
|
|
|
|
#define BMAP_ALLOC 1
|
|
|
|
|
|
|
|
#define inode_bmap(inode, nr) ((inode)->i_block[(nr)])
|
|
|
|
|
|
|
|
static errcode_t _BMAP_INLINE_ block_ind_bmap(ext2_filsys fs, int flags,
|
|
|
|
blk_t ind, char *block_buf,
|
|
|
|
int *blocks_alloc,
|
|
|
|
blk_t nr, blk_t *ret_blk)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
blk_t b;
|
|
|
|
|
|
|
|
if (!ind) {
|
|
|
|
*ret_blk = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
retval = io_channel_read_blk(fs->io, ind, 1, block_buf);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
b = ((blk_t *) block_buf)[nr];
|
|
|
|
|
|
|
|
if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
|
|
|
|
(fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
|
|
|
|
b = ext2fs_swab32(b);
|
|
|
|
|
|
|
|
if (!b && (flags & BMAP_ALLOC)) {
|
|
|
|
b = nr ? ((blk_t *) block_buf)[nr-1] : 0;
|
|
|
|
retval = ext2fs_alloc_block(fs, b,
|
|
|
|
block_buf + fs->blocksize, &b);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
|
|
|
|
(fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
|
|
|
|
((blk_t *) block_buf)[nr] = ext2fs_swab32(b);
|
|
|
|
else
|
|
|
|
((blk_t *) block_buf)[nr] = b;
|
|
|
|
|
|
|
|
retval = io_channel_write_blk(fs->io, ind, 1, block_buf);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
(*blocks_alloc)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret_blk = b;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static errcode_t _BMAP_INLINE_ block_dind_bmap(ext2_filsys fs, int flags,
|
|
|
|
blk_t dind, char *block_buf,
|
|
|
|
int *blocks_alloc,
|
|
|
|
blk_t nr, blk_t *ret_blk)
|
|
|
|
{
|
|
|
|
blk_t b;
|
|
|
|
errcode_t retval;
|
1998-09-03 05:22:57 +04:00
|
|
|
blk_t addr_per_block;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
1998-09-03 05:22:57 +04:00
|
|
|
addr_per_block = (blk_t) fs->blocksize >> 2;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
retval = block_ind_bmap(fs, flags, dind, block_buf, blocks_alloc,
|
|
|
|
nr / addr_per_block, &b);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
|
|
|
|
nr % addr_per_block, ret_blk);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static errcode_t _BMAP_INLINE_ block_tind_bmap(ext2_filsys fs, int flags,
|
|
|
|
blk_t tind, char *block_buf,
|
|
|
|
int *blocks_alloc,
|
|
|
|
blk_t nr, blk_t *ret_blk)
|
|
|
|
{
|
|
|
|
blk_t b;
|
|
|
|
errcode_t retval;
|
1998-09-03 05:22:57 +04:00
|
|
|
blk_t addr_per_block;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
1998-09-03 05:22:57 +04:00
|
|
|
addr_per_block = (blk_t) fs->blocksize >> 2;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
retval = block_dind_bmap(fs, flags, tind, block_buf, blocks_alloc,
|
|
|
|
nr / addr_per_block, &b);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
|
|
|
|
nr % addr_per_block, ret_blk);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
errcode_t ext2fs_bmap(ext2_filsys fs, ino_t ino, struct ext2_inode *inode,
|
|
|
|
char *block_buf, int bmap_flags, blk_t block,
|
|
|
|
blk_t *phys_blk)
|
|
|
|
{
|
|
|
|
struct ext2_inode inode_buf;
|
1998-09-03 05:22:57 +04:00
|
|
|
blk_t addr_per_block;
|
1997-10-26 02:37:42 +04:00
|
|
|
blk_t b;
|
|
|
|
char *buf = 0;
|
|
|
|
errcode_t retval = 0;
|
|
|
|
int blocks_alloc = 0;
|
|
|
|
|
|
|
|
*phys_blk = 0;
|
|
|
|
|
|
|
|
/* Read inode structure if necessary */
|
|
|
|
if (!inode) {
|
|
|
|
retval = ext2fs_read_inode(fs, ino, &inode_buf);
|
|
|
|
if (!retval)
|
|
|
|
return retval;
|
|
|
|
inode = &inode_buf;
|
|
|
|
}
|
1998-09-03 05:22:57 +04:00
|
|
|
addr_per_block = (blk_t) fs->blocksize >> 2;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
if (!block_buf) {
|
1997-10-26 06:41:24 +03:00
|
|
|
retval = ext2fs_get_mem(fs->blocksize * 2, (void **) &buf);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
1997-10-26 02:37:42 +04:00
|
|
|
block_buf = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block < EXT2_NDIR_BLOCKS) {
|
|
|
|
*phys_blk = inode_bmap(inode, block);
|
|
|
|
b = block ? inode_bmap(inode, block-1) : 0;
|
|
|
|
|
|
|
|
if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
|
|
|
|
retval = ext2fs_alloc_block(fs, b, block_buf, &b);
|
|
|
|
if (retval)
|
|
|
|
goto done;
|
|
|
|
inode_bmap(inode, block) = b;
|
|
|
|
blocks_alloc++;
|
|
|
|
*phys_blk = b;
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indirect block */
|
|
|
|
block -= EXT2_NDIR_BLOCKS;
|
|
|
|
if (block < addr_per_block) {
|
|
|
|
b = inode_bmap(inode, EXT2_IND_BLOCK);
|
|
|
|
if (!b) {
|
|
|
|
if (!(bmap_flags & BMAP_ALLOC))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
b = inode_bmap(inode, EXT2_IND_BLOCK-1);
|
|
|
|
retval = ext2fs_alloc_block(fs, b, block_buf, &b);
|
|
|
|
if (retval)
|
|
|
|
goto done;
|
|
|
|
inode_bmap(inode, EXT2_IND_BLOCK) = b;
|
|
|
|
blocks_alloc++;
|
|
|
|
}
|
|
|
|
retval = block_ind_bmap(fs, bmap_flags, b, block_buf,
|
|
|
|
&blocks_alloc, block, phys_blk);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Doubly indirect block */
|
|
|
|
block -= addr_per_block;
|
|
|
|
if (block < addr_per_block * addr_per_block) {
|
|
|
|
b = inode_bmap(inode, EXT2_DIND_BLOCK);
|
|
|
|
if (!b) {
|
|
|
|
if (!(bmap_flags & BMAP_ALLOC))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
b = inode_bmap(inode, EXT2_IND_BLOCK);
|
|
|
|
retval = ext2fs_alloc_block(fs, b, block_buf, &b);
|
|
|
|
if (retval)
|
|
|
|
goto done;
|
|
|
|
inode_bmap(inode, EXT2_DIND_BLOCK) = b;
|
|
|
|
blocks_alloc++;
|
|
|
|
}
|
|
|
|
retval = block_dind_bmap(fs, bmap_flags, b, block_buf,
|
|
|
|
&blocks_alloc, block, phys_blk);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Triply indirect block */
|
|
|
|
block -= addr_per_block * addr_per_block;
|
|
|
|
b = inode_bmap(inode, EXT2_TIND_BLOCK);
|
|
|
|
if (!b) {
|
|
|
|
if (!(bmap_flags & BMAP_ALLOC))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
b = inode_bmap(inode, EXT2_DIND_BLOCK);
|
|
|
|
retval = ext2fs_alloc_block(fs, b, block_buf, &b);
|
|
|
|
if (retval)
|
|
|
|
goto done;
|
|
|
|
inode_bmap(inode, EXT2_TIND_BLOCK) = b;
|
|
|
|
blocks_alloc++;
|
|
|
|
}
|
|
|
|
retval = block_tind_bmap(fs, bmap_flags, b, block_buf,
|
|
|
|
&blocks_alloc, block, phys_blk);
|
|
|
|
done:
|
|
|
|
if (buf)
|
1997-10-26 06:41:24 +03:00
|
|
|
ext2fs_free_mem((void **) &buf);
|
1997-10-26 02:37:42 +04:00
|
|
|
if ((retval == 0) && blocks_alloc) {
|
|
|
|
inode->i_blocks += (blocks_alloc * fs->blocksize) / 512;
|
|
|
|
retval = ext2fs_write_inode(fs, ino, inode);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|