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%
|
2010-05-18 05:31:56 +04:00
|
|
|
* This file may be redistributed under the terms of the GNU Library
|
|
|
|
* General Public License, version 2.
|
1997-10-26 02:37:42 +04:00
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
2011-09-19 01:34:37 +04:00
|
|
|
#include "config.h"
|
1997-10-26 02:37:42 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2008-02-18 22:59:45 +03:00
|
|
|
#include <errno.h>
|
1997-10-26 02:37:42 +04:00
|
|
|
|
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
|
|
|
#include "ext2_fs.h"
|
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
|
|
|
|
|
Many files:
alloc.c, bb_inode.c, bitmaps.c, bitops.h, block.c, bmap.c, bmove.c,
brel.h, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c,
expanddir.c, ext2fs.h, ext2fsP.h, fileio.c, finddev.c, get_pathname.c,
icount.c, inode.c, irel.h, irel_ma.c, ismounted.c, link.c, lookup.c,
mkdir.c, mkjournal.c, namei.c, newdir.c, read_bb_file.c, test_io.c,
tst_iscan.c, unix_io.c, unlink.c: Change use of ino_t to ext2_ino_t,
to protect applications that attempt to compile
-D_FILE_OFFSET_BITS=64, since this inexplicably changes ino_t(!?). So
we use ext2_ino_t to avoid an unexpected ABI change.
2001-01-11 07:54:39 +03:00
|
|
|
extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
|
2008-08-28 07:07:54 +04:00
|
|
|
struct ext2_inode *inode,
|
1997-10-26 02:37:42 +04:00
|
|
|
char *block_buf, int bmap_flags,
|
|
|
|
blk_t block, blk_t *phys_blk);
|
|
|
|
|
|
|
|
#define inode_bmap(inode, nr) ((inode)->i_block[(nr)])
|
|
|
|
|
2008-08-28 07:07:54 +04:00
|
|
|
static _BMAP_INLINE_ errcode_t block_ind_bmap(ext2_filsys fs, int flags,
|
|
|
|
blk_t ind, char *block_buf,
|
1997-10-26 02:37:42 +04:00
|
|
|
int *blocks_alloc,
|
|
|
|
blk_t nr, blk_t *ret_blk)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
blk_t b;
|
|
|
|
|
|
|
|
if (!ind) {
|
2004-12-23 21:55:34 +03:00
|
|
|
if (flags & BMAP_SET)
|
|
|
|
return EXT2_ET_SET_BMAP_NO_IND;
|
1997-10-26 02:37:42 +04:00
|
|
|
*ret_blk = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
retval = io_channel_read_blk(fs->io, ind, 1, block_buf);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
2004-12-23 21:55:34 +03:00
|
|
|
if (flags & BMAP_SET) {
|
|
|
|
b = *ret_blk;
|
2007-08-11 09:56:48 +04:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
b = ext2fs_swab32(b);
|
2004-12-23 21:55:34 +03:00
|
|
|
#endif
|
|
|
|
((blk_t *) block_buf)[nr] = b;
|
|
|
|
return io_channel_write_blk(fs->io, ind, 1, block_buf);
|
|
|
|
}
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
b = ((blk_t *) block_buf)[nr];
|
|
|
|
|
2007-08-11 09:56:48 +04:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
b = ext2fs_swab32(b);
|
ChangeLog, Makefile.in, swapfs.c, unix.c:
Makefile.in: Add message.c and swapfs.c to the list of source files to
build the make depend.
swapfs.c, unix.c: Only support the -s and -S options to e2fsck if
ENABLE_SWAPFS is defined.
Many files:
ext2fs.h, bitops.h, block.c, bmap.c, closefs.c, dirblock.c, inode.c,
native.c, openfs.c, rw_bitmaps.c, swapfs.c: Only include the
byte-swapping logic if ENABLE_SWAPFS is turned on or if we're on a
big-endian machine.
initialize.c (ext2fs_initialize):Use WORDS_BIGENDIAN directly to set
EXT2_FLAG_SWAP_BYTES, instead of using ext2fs_native_flag.
native.c (ext2fs_native_flag): Use WORDS_BIGENDIAN provided by
autoconf to determine whether or not return EXT2_FLAG_SWAP_BYTES.
2001-06-11 11:00:04 +04:00
|
|
|
#endif
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2007-08-11 09:56:48 +04:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
((blk_t *) block_buf)[nr] = ext2fs_swab32(b);
|
|
|
|
#else
|
|
|
|
((blk_t *) block_buf)[nr] = b;
|
ChangeLog, Makefile.in, swapfs.c, unix.c:
Makefile.in: Add message.c and swapfs.c to the list of source files to
build the make depend.
swapfs.c, unix.c: Only support the -s and -S options to e2fsck if
ENABLE_SWAPFS is defined.
Many files:
ext2fs.h, bitops.h, block.c, bmap.c, closefs.c, dirblock.c, inode.c,
native.c, openfs.c, rw_bitmaps.c, swapfs.c: Only include the
byte-swapping logic if ENABLE_SWAPFS is turned on or if we're on a
big-endian machine.
initialize.c (ext2fs_initialize):Use WORDS_BIGENDIAN directly to set
EXT2_FLAG_SWAP_BYTES, instead of using ext2fs_native_flag.
native.c (ext2fs_native_flag): Use WORDS_BIGENDIAN provided by
autoconf to determine whether or not return EXT2_FLAG_SWAP_BYTES.
2001-06-11 11:00:04 +04:00
|
|
|
#endif
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
retval = io_channel_write_blk(fs->io, ind, 1, block_buf);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
(*blocks_alloc)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret_blk = b;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-08 07:52:56 +03:00
|
|
|
static _BMAP_INLINE_ errcode_t block_dind_bmap(ext2_filsys fs, int flags,
|
2008-08-28 07:07:54 +04:00
|
|
|
blk_t dind, char *block_buf,
|
1997-10-26 02:37:42 +04:00
|
|
|
int *blocks_alloc,
|
|
|
|
blk_t nr, blk_t *ret_blk)
|
|
|
|
{
|
2012-11-29 16:47:52 +04:00
|
|
|
blk_t b = 0;
|
1997-10-26 02:37:42 +04:00
|
|
|
errcode_t retval;
|
1998-09-03 05:22:57 +04:00
|
|
|
blk_t addr_per_block;
|
2008-08-28 07:07:54 +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
|
|
|
|
2008-08-28 07:07:54 +04:00
|
|
|
retval = block_ind_bmap(fs, flags & ~BMAP_SET, dind, block_buf,
|
2004-12-23 21:55:34 +03:00
|
|
|
blocks_alloc, nr / addr_per_block, &b);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
|
|
|
|
nr % addr_per_block, ret_blk);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2002-03-08 07:52:56 +03:00
|
|
|
static _BMAP_INLINE_ errcode_t block_tind_bmap(ext2_filsys fs, int flags,
|
2008-08-28 07:07:54 +04:00
|
|
|
blk_t tind, char *block_buf,
|
1997-10-26 02:37:42 +04:00
|
|
|
int *blocks_alloc,
|
|
|
|
blk_t nr, blk_t *ret_blk)
|
|
|
|
{
|
2012-11-29 16:47:52 +04:00
|
|
|
blk_t b = 0;
|
1997-10-26 02:37:42 +04:00
|
|
|
errcode_t retval;
|
1998-09-03 05:22:57 +04:00
|
|
|
blk_t addr_per_block;
|
2008-08-28 07:07:54 +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
|
|
|
|
2008-08-28 07:07:54 +04:00
|
|
|
retval = block_dind_bmap(fs, flags & ~BMAP_SET, tind, block_buf,
|
2004-12-23 21:55:34 +03:00
|
|
|
blocks_alloc, nr / addr_per_block, &b);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
|
|
|
|
nr % addr_per_block, ret_blk);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-07-08 06:27:42 +04:00
|
|
|
static errcode_t extent_bmap(ext2_filsys fs, ext2_ino_t ino,
|
|
|
|
struct ext2_inode *inode,
|
|
|
|
ext2_extent_handle_t handle,
|
|
|
|
char *block_buf, int bmap_flags, blk64_t block,
|
|
|
|
int *ret_flags, int *blocks_alloc,
|
|
|
|
blk64_t *phys_blk);
|
|
|
|
|
|
|
|
static errcode_t implied_cluster_alloc(ext2_filsys fs, ext2_ino_t ino,
|
|
|
|
struct ext2_inode *inode,
|
|
|
|
ext2_extent_handle_t handle,
|
2013-10-08 20:08:46 +04:00
|
|
|
blk64_t lblk, blk64_t *phys_blk)
|
2011-07-08 06:27:42 +04:00
|
|
|
{
|
|
|
|
blk64_t base_block, pblock = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
|
|
|
|
EXT4_FEATURE_RO_COMPAT_BIGALLOC))
|
|
|
|
return 0;
|
|
|
|
|
2013-10-08 20:08:46 +04:00
|
|
|
base_block = lblk & ~EXT2FS_CLUSTER_MASK(fs);
|
|
|
|
/*
|
|
|
|
* Except for the logical block (lblk) that was passed in, search all
|
|
|
|
* blocks in this logical cluster for a mapping to a physical cluster.
|
|
|
|
* If any such map exists, calculate the physical block that maps to
|
|
|
|
* the logical block and return that.
|
|
|
|
*
|
|
|
|
* The old code wouldn't even look if (block % cluster_ratio) == 0;
|
|
|
|
* this is incorrect if we're allocating blocks in reverse order.
|
|
|
|
*/
|
2011-07-08 06:27:42 +04:00
|
|
|
for (i = 0; i < EXT2FS_CLUSTER_RATIO(fs); i++) {
|
2013-10-08 20:08:46 +04:00
|
|
|
if (base_block + i == lblk)
|
|
|
|
continue;
|
2011-07-08 06:27:42 +04:00
|
|
|
extent_bmap(fs, ino, inode, handle, 0, 0,
|
|
|
|
base_block + i, 0, 0, &pblock);
|
|
|
|
if (pblock)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pblock == 0)
|
|
|
|
return 0;
|
2013-10-08 20:08:46 +04:00
|
|
|
*phys_blk = pblock - i + (lblk - base_block);
|
2011-07-08 06:27:42 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:02:29 +04:00
|
|
|
static errcode_t extent_bmap(ext2_filsys fs, ext2_ino_t ino,
|
|
|
|
struct ext2_inode *inode,
|
|
|
|
ext2_extent_handle_t handle,
|
|
|
|
char *block_buf, int bmap_flags, blk64_t block,
|
|
|
|
int *ret_flags, int *blocks_alloc,
|
|
|
|
blk64_t *phys_blk)
|
|
|
|
{
|
|
|
|
struct ext2fs_extent extent;
|
|
|
|
unsigned int offset;
|
|
|
|
errcode_t retval = 0;
|
2011-07-08 06:27:42 +04:00
|
|
|
blk64_t blk64 = 0;
|
|
|
|
int alloc = 0;
|
2011-07-07 22:02:29 +04:00
|
|
|
|
|
|
|
if (bmap_flags & BMAP_SET) {
|
|
|
|
retval = ext2fs_extent_set_bmap(handle, block,
|
|
|
|
*phys_blk, 0);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
retval = ext2fs_extent_goto(handle, block);
|
|
|
|
if (retval) {
|
|
|
|
/* If the extent is not found, return phys_blk = 0 */
|
|
|
|
if (retval == EXT2_ET_EXTENT_NOT_FOUND)
|
|
|
|
goto got_block;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
retval = ext2fs_extent_get(handle, EXT2_EXTENT_CURRENT, &extent);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
offset = block - extent.e_lblk;
|
|
|
|
if (block >= extent.e_lblk && (offset <= extent.e_len)) {
|
|
|
|
*phys_blk = extent.e_pblk + offset;
|
|
|
|
if (ret_flags && extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT)
|
|
|
|
*ret_flags |= BMAP_RET_UNINIT;
|
|
|
|
}
|
|
|
|
got_block:
|
|
|
|
if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
|
2011-07-08 06:27:42 +04:00
|
|
|
implied_cluster_alloc(fs, ino, inode, handle, block, &blk64);
|
|
|
|
if (blk64)
|
|
|
|
goto set_extent;
|
2011-07-07 22:02:29 +04:00
|
|
|
retval = extent_bmap(fs, ino, inode, handle, block_buf,
|
|
|
|
0, block-1, 0, blocks_alloc, &blk64);
|
|
|
|
if (retval)
|
|
|
|
blk64 = 0;
|
|
|
|
retval = ext2fs_alloc_block2(fs, blk64, block_buf,
|
|
|
|
&blk64);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
2011-07-08 06:27:42 +04:00
|
|
|
blk64 &= ~EXT2FS_CLUSTER_MASK(fs);
|
|
|
|
blk64 += EXT2FS_CLUSTER_MASK(fs) & block;
|
|
|
|
alloc++;
|
|
|
|
set_extent:
|
2011-07-07 22:02:29 +04:00
|
|
|
retval = ext2fs_extent_set_bmap(handle, block,
|
|
|
|
blk64, 0);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
/* Update inode after setting extent */
|
|
|
|
retval = ext2fs_read_inode(fs, ino, inode);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
2011-07-08 06:27:42 +04:00
|
|
|
*blocks_alloc += alloc;
|
2011-07-07 22:02:29 +04:00
|
|
|
*phys_blk = blk64;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:40:31 +04:00
|
|
|
int ext2fs_file_block_offset_too_big(ext2_filsys fs,
|
|
|
|
struct ext2_inode *inode,
|
|
|
|
blk64_t offset)
|
|
|
|
{
|
|
|
|
blk64_t addr_per_block, max_map_block;
|
|
|
|
|
|
|
|
/* Kernel seems to cut us off at 4294967294 blocks */
|
|
|
|
if (offset >= (1ULL << 32) - 1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (inode->i_flags & EXT4_EXTENTS_FL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
addr_per_block = fs->blocksize >> 2;
|
|
|
|
max_map_block = addr_per_block;
|
|
|
|
max_map_block += addr_per_block * addr_per_block;
|
|
|
|
max_map_block += addr_per_block * addr_per_block * addr_per_block;
|
|
|
|
max_map_block += 12;
|
|
|
|
|
|
|
|
return offset >= max_map_block;
|
|
|
|
}
|
2011-07-07 22:02:29 +04:00
|
|
|
|
2008-02-18 22:59:45 +03:00
|
|
|
errcode_t ext2fs_bmap2(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
|
|
|
|
char *block_buf, int bmap_flags, blk64_t block,
|
|
|
|
int *ret_flags, blk64_t *phys_blk)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
struct ext2_inode inode_buf;
|
2008-02-18 22:59:45 +03:00
|
|
|
ext2_extent_handle_t handle = 0;
|
1998-09-03 05:22:57 +04:00
|
|
|
blk_t addr_per_block;
|
2008-02-18 22:59:45 +03:00
|
|
|
blk_t b, blk32;
|
1997-10-26 02:37:42 +04:00
|
|
|
char *buf = 0;
|
|
|
|
errcode_t retval = 0;
|
2004-12-23 21:55:34 +03:00
|
|
|
int blocks_alloc = 0, inode_dirty = 0;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
2004-12-23 21:55:34 +03:00
|
|
|
if (!(bmap_flags & BMAP_SET))
|
|
|
|
*phys_blk = 0;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
2008-02-18 22:59:45 +03:00
|
|
|
if (ret_flags)
|
|
|
|
*ret_flags = 0;
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
/* Read inode structure if necessary */
|
|
|
|
if (!inode) {
|
|
|
|
retval = ext2fs_read_inode(fs, ino, &inode_buf);
|
2002-05-12 06:13:20 +04:00
|
|
|
if (retval)
|
1997-10-26 02:37:42 +04:00
|
|
|
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
|
|
|
|
2013-12-12 21:40:31 +04:00
|
|
|
if (ext2fs_file_block_offset_too_big(fs, inode, block))
|
|
|
|
return EXT2_ET_FILE_TOO_BIG;
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
if (!block_buf) {
|
2007-11-10 03:01:06 +03:00
|
|
|
retval = ext2fs_get_array(2, fs->blocksize, &buf);
|
1997-10-26 06:41:24 +03:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
1997-10-26 02:37:42 +04:00
|
|
|
block_buf = buf;
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:02:29 +04:00
|
|
|
if (inode->i_flags & EXT4_EXTENTS_FL) {
|
|
|
|
retval = ext2fs_extent_open2(fs, ino, inode, &handle);
|
|
|
|
if (retval)
|
|
|
|
goto done;
|
|
|
|
retval = extent_bmap(fs, ino, inode, handle, block_buf,
|
|
|
|
bmap_flags, block, ret_flags,
|
|
|
|
&blocks_alloc, phys_blk);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
if (block < EXT2_NDIR_BLOCKS) {
|
2004-12-23 21:55:34 +03:00
|
|
|
if (bmap_flags & BMAP_SET) {
|
|
|
|
b = *phys_blk;
|
|
|
|
inode_bmap(inode, block) = b;
|
|
|
|
inode_dirty++;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
*phys_blk = inode_bmap(inode, block);
|
|
|
|
b = block ? inode_bmap(inode, block-1) : 0;
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
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;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
/* Indirect block */
|
|
|
|
block -= EXT2_NDIR_BLOCKS;
|
2008-02-18 22:59:45 +03:00
|
|
|
blk32 = *phys_blk;
|
1997-10-26 02:37:42 +04:00
|
|
|
if (block < addr_per_block) {
|
|
|
|
b = inode_bmap(inode, EXT2_IND_BLOCK);
|
|
|
|
if (!b) {
|
2004-12-23 21:55:34 +03:00
|
|
|
if (!(bmap_flags & BMAP_ALLOC)) {
|
|
|
|
if (bmap_flags & BMAP_SET)
|
|
|
|
retval = EXT2_ET_SET_BMAP_NO_IND;
|
|
|
|
goto done;
|
|
|
|
}
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
retval = block_ind_bmap(fs, bmap_flags, b, block_buf,
|
2008-02-18 22:59:45 +03:00
|
|
|
&blocks_alloc, block, &blk32);
|
|
|
|
if (retval == 0)
|
|
|
|
*phys_blk = blk32;
|
1997-10-26 02:37:42 +04:00
|
|
|
goto done;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
/* Doubly indirect block */
|
|
|
|
block -= addr_per_block;
|
|
|
|
if (block < addr_per_block * addr_per_block) {
|
|
|
|
b = inode_bmap(inode, EXT2_DIND_BLOCK);
|
|
|
|
if (!b) {
|
2004-12-23 21:55:34 +03:00
|
|
|
if (!(bmap_flags & BMAP_ALLOC)) {
|
|
|
|
if (bmap_flags & BMAP_SET)
|
|
|
|
retval = EXT2_ET_SET_BMAP_NO_IND;
|
|
|
|
goto done;
|
|
|
|
}
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
retval = block_dind_bmap(fs, bmap_flags, b, block_buf,
|
2008-02-18 22:59:45 +03:00
|
|
|
&blocks_alloc, block, &blk32);
|
|
|
|
if (retval == 0)
|
|
|
|
*phys_blk = blk32;
|
1997-10-26 02:37:42 +04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Triply indirect block */
|
|
|
|
block -= addr_per_block * addr_per_block;
|
|
|
|
b = inode_bmap(inode, EXT2_TIND_BLOCK);
|
|
|
|
if (!b) {
|
2004-12-23 21:55:34 +03:00
|
|
|
if (!(bmap_flags & BMAP_ALLOC)) {
|
|
|
|
if (bmap_flags & BMAP_SET)
|
|
|
|
retval = EXT2_ET_SET_BMAP_NO_IND;
|
1997-10-26 02:37:42 +04:00
|
|
|
goto done;
|
2004-12-23 21:55:34 +03:00
|
|
|
}
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
2008-08-28 07:07:54 +04:00
|
|
|
retval = block_tind_bmap(fs, bmap_flags, b, block_buf,
|
2008-02-18 22:59:45 +03:00
|
|
|
&blocks_alloc, block, &blk32);
|
|
|
|
if (retval == 0)
|
|
|
|
*phys_blk = blk32;
|
1997-10-26 02:37:42 +04:00
|
|
|
done:
|
|
|
|
if (buf)
|
2003-08-01 17:41:07 +04:00
|
|
|
ext2fs_free_mem(&buf);
|
2008-02-18 22:59:45 +03:00
|
|
|
if (handle)
|
|
|
|
ext2fs_extent_free(handle);
|
2004-12-23 21:55:34 +03:00
|
|
|
if ((retval == 0) && (blocks_alloc || inode_dirty)) {
|
2008-04-09 19:39:11 +04:00
|
|
|
ext2fs_iblk_add_blocks(fs, inode, blocks_alloc);
|
1997-10-26 02:37:42 +04:00
|
|
|
retval = ext2fs_write_inode(fs, ino, inode);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-02-18 22:59:45 +03:00
|
|
|
errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
|
|
|
|
char *block_buf, int bmap_flags, blk_t block,
|
|
|
|
blk_t *phys_blk)
|
|
|
|
{
|
|
|
|
errcode_t ret;
|
2009-11-29 08:08:54 +03:00
|
|
|
blk64_t ret_blk = *phys_blk;
|
2008-02-18 22:59:45 +03:00
|
|
|
|
|
|
|
ret = ext2fs_bmap2(fs, ino, inode, block_buf, bmap_flags, block,
|
|
|
|
0, &ret_blk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (ret_blk >= ((long long) 1 << 32))
|
|
|
|
return EOVERFLOW;
|
|
|
|
*phys_blk = ret_blk;
|
|
|
|
return 0;
|
|
|
|
}
|