[821] | 1 | /*
|
---|
| 2 | * bmap.c --- logical to physical block mapping
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 1997 Theodore Ts'o.
|
---|
| 5 | *
|
---|
| 6 | * %Begin-Header%
|
---|
| 7 | * This file may be redistributed under the terms of the GNU Public
|
---|
| 8 | * License.
|
---|
| 9 | * %End-Header%
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | #include <stdio.h>
|
---|
| 13 | #include <string.h>
|
---|
| 14 | #if HAVE_UNISTD_H
|
---|
| 15 | #include <unistd.h>
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
| 18 | #include "ext2_fs.h"
|
---|
| 19 | #include "ext2fs.h"
|
---|
| 20 |
|
---|
| 21 | extern errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino,
|
---|
| 22 | struct ext2_inode *inode,
|
---|
| 23 | char *block_buf, int bmap_flags,
|
---|
| 24 | blk_t block, blk_t *phys_blk);
|
---|
| 25 |
|
---|
| 26 | #define inode_bmap(inode, nr) ((inode)->i_block[(nr)])
|
---|
| 27 |
|
---|
| 28 | static errcode_t block_ind_bmap(ext2_filsys fs, int flags,
|
---|
| 29 | blk_t ind, char *block_buf,
|
---|
| 30 | int *blocks_alloc,
|
---|
| 31 | blk_t nr, blk_t *ret_blk)
|
---|
| 32 | {
|
---|
| 33 | errcode_t retval;
|
---|
| 34 | blk_t b;
|
---|
| 35 |
|
---|
| 36 | if (!ind) {
|
---|
| 37 | if (flags & BMAP_SET)
|
---|
| 38 | return EXT2_ET_SET_BMAP_NO_IND;
|
---|
| 39 | *ret_blk = 0;
|
---|
| 40 | return 0;
|
---|
| 41 | }
|
---|
| 42 | retval = io_channel_read_blk(fs->io, ind, 1, block_buf);
|
---|
| 43 | if (retval)
|
---|
| 44 | return retval;
|
---|
| 45 |
|
---|
| 46 | if (flags & BMAP_SET) {
|
---|
| 47 | b = *ret_blk;
|
---|
| 48 | #if BB_BIG_ENDIAN
|
---|
| 49 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
|
---|
| 50 | (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
|
---|
| 51 | b = ext2fs_swab32(b);
|
---|
| 52 | #endif
|
---|
| 53 | ((blk_t *) block_buf)[nr] = b;
|
---|
| 54 | return io_channel_write_blk(fs->io, ind, 1, block_buf);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | b = ((blk_t *) block_buf)[nr];
|
---|
| 58 |
|
---|
| 59 | #if BB_BIG_ENDIAN
|
---|
| 60 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
|
---|
| 61 | (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
|
---|
| 62 | b = ext2fs_swab32(b);
|
---|
| 63 | #endif
|
---|
| 64 |
|
---|
| 65 | if (!b && (flags & BMAP_ALLOC)) {
|
---|
| 66 | b = nr ? ((blk_t *) block_buf)[nr-1] : 0;
|
---|
| 67 | retval = ext2fs_alloc_block(fs, b,
|
---|
| 68 | block_buf + fs->blocksize, &b);
|
---|
| 69 | if (retval)
|
---|
| 70 | return retval;
|
---|
| 71 |
|
---|
| 72 | #if BB_BIG_ENDIAN
|
---|
| 73 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
|
---|
| 74 | (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
|
---|
| 75 | ((blk_t *) block_buf)[nr] = ext2fs_swab32(b);
|
---|
| 76 | else
|
---|
| 77 | #endif
|
---|
| 78 | ((blk_t *) block_buf)[nr] = b;
|
---|
| 79 |
|
---|
| 80 | retval = io_channel_write_blk(fs->io, ind, 1, block_buf);
|
---|
| 81 | if (retval)
|
---|
| 82 | return retval;
|
---|
| 83 |
|
---|
| 84 | (*blocks_alloc)++;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | *ret_blk = b;
|
---|
| 88 | return 0;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | static errcode_t block_dind_bmap(ext2_filsys fs, int flags,
|
---|
| 92 | blk_t dind, char *block_buf,
|
---|
| 93 | int *blocks_alloc,
|
---|
| 94 | blk_t nr, blk_t *ret_blk)
|
---|
| 95 | {
|
---|
| 96 | blk_t b;
|
---|
| 97 | errcode_t retval;
|
---|
| 98 | blk_t addr_per_block;
|
---|
| 99 |
|
---|
| 100 | addr_per_block = (blk_t) fs->blocksize >> 2;
|
---|
| 101 |
|
---|
| 102 | retval = block_ind_bmap(fs, flags & ~BMAP_SET, dind, block_buf,
|
---|
| 103 | blocks_alloc, nr / addr_per_block, &b);
|
---|
| 104 | if (retval)
|
---|
| 105 | return retval;
|
---|
| 106 | retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
|
---|
| 107 | nr % addr_per_block, ret_blk);
|
---|
| 108 | return retval;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | static errcode_t block_tind_bmap(ext2_filsys fs, int flags,
|
---|
| 112 | blk_t tind, char *block_buf,
|
---|
| 113 | int *blocks_alloc,
|
---|
| 114 | blk_t nr, blk_t *ret_blk)
|
---|
| 115 | {
|
---|
| 116 | blk_t b;
|
---|
| 117 | errcode_t retval;
|
---|
| 118 | blk_t addr_per_block;
|
---|
| 119 |
|
---|
| 120 | addr_per_block = (blk_t) fs->blocksize >> 2;
|
---|
| 121 |
|
---|
| 122 | retval = block_dind_bmap(fs, flags & ~BMAP_SET, tind, block_buf,
|
---|
| 123 | blocks_alloc, nr / addr_per_block, &b);
|
---|
| 124 | if (retval)
|
---|
| 125 | return retval;
|
---|
| 126 | retval = block_ind_bmap(fs, flags, b, block_buf, blocks_alloc,
|
---|
| 127 | nr % addr_per_block, ret_blk);
|
---|
| 128 | return retval;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | errcode_t ext2fs_bmap(ext2_filsys fs, ext2_ino_t ino, struct ext2_inode *inode,
|
---|
| 132 | char *block_buf, int bmap_flags, blk_t block,
|
---|
| 133 | blk_t *phys_blk)
|
---|
| 134 | {
|
---|
| 135 | struct ext2_inode inode_buf;
|
---|
| 136 | blk_t addr_per_block;
|
---|
| 137 | blk_t b;
|
---|
| 138 | char *buf = 0;
|
---|
| 139 | errcode_t retval = 0;
|
---|
| 140 | int blocks_alloc = 0, inode_dirty = 0;
|
---|
| 141 |
|
---|
| 142 | if (!(bmap_flags & BMAP_SET))
|
---|
| 143 | *phys_blk = 0;
|
---|
| 144 |
|
---|
| 145 | /* Read inode structure if necessary */
|
---|
| 146 | if (!inode) {
|
---|
| 147 | retval = ext2fs_read_inode(fs, ino, &inode_buf);
|
---|
| 148 | if (retval)
|
---|
| 149 | return retval;
|
---|
| 150 | inode = &inode_buf;
|
---|
| 151 | }
|
---|
| 152 | addr_per_block = (blk_t) fs->blocksize >> 2;
|
---|
| 153 |
|
---|
| 154 | if (!block_buf) {
|
---|
| 155 | retval = ext2fs_get_mem(fs->blocksize * 2, &buf);
|
---|
| 156 | if (retval)
|
---|
| 157 | return retval;
|
---|
| 158 | block_buf = buf;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | if (block < EXT2_NDIR_BLOCKS) {
|
---|
| 162 | if (bmap_flags & BMAP_SET) {
|
---|
| 163 | b = *phys_blk;
|
---|
| 164 | #if BB_BIG_ENDIAN
|
---|
| 165 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
|
---|
| 166 | (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
|
---|
| 167 | b = ext2fs_swab32(b);
|
---|
| 168 | #endif
|
---|
| 169 | inode_bmap(inode, block) = b;
|
---|
| 170 | inode_dirty++;
|
---|
| 171 | goto done;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | *phys_blk = inode_bmap(inode, block);
|
---|
| 175 | b = block ? inode_bmap(inode, block-1) : 0;
|
---|
| 176 |
|
---|
| 177 | if ((*phys_blk == 0) && (bmap_flags & BMAP_ALLOC)) {
|
---|
| 178 | retval = ext2fs_alloc_block(fs, b, block_buf, &b);
|
---|
| 179 | if (retval)
|
---|
| 180 | goto done;
|
---|
| 181 | inode_bmap(inode, block) = b;
|
---|
| 182 | blocks_alloc++;
|
---|
| 183 | *phys_blk = b;
|
---|
| 184 | }
|
---|
| 185 | goto done;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | /* Indirect block */
|
---|
| 189 | block -= EXT2_NDIR_BLOCKS;
|
---|
| 190 | if (block < addr_per_block) {
|
---|
| 191 | b = inode_bmap(inode, EXT2_IND_BLOCK);
|
---|
| 192 | if (!b) {
|
---|
| 193 | if (!(bmap_flags & BMAP_ALLOC)) {
|
---|
| 194 | if (bmap_flags & BMAP_SET)
|
---|
| 195 | retval = EXT2_ET_SET_BMAP_NO_IND;
|
---|
| 196 | goto done;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | b = inode_bmap(inode, EXT2_IND_BLOCK-1);
|
---|
| 200 | retval = ext2fs_alloc_block(fs, b, block_buf, &b);
|
---|
| 201 | if (retval)
|
---|
| 202 | goto done;
|
---|
| 203 | inode_bmap(inode, EXT2_IND_BLOCK) = b;
|
---|
| 204 | blocks_alloc++;
|
---|
| 205 | }
|
---|
| 206 | retval = block_ind_bmap(fs, bmap_flags, b, block_buf,
|
---|
| 207 | &blocks_alloc, block, phys_blk);
|
---|
| 208 | goto done;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /* Doubly indirect block */
|
---|
| 212 | block -= addr_per_block;
|
---|
| 213 | if (block < addr_per_block * addr_per_block) {
|
---|
| 214 | b = inode_bmap(inode, EXT2_DIND_BLOCK);
|
---|
| 215 | if (!b) {
|
---|
| 216 | if (!(bmap_flags & BMAP_ALLOC)) {
|
---|
| 217 | if (bmap_flags & BMAP_SET)
|
---|
| 218 | retval = EXT2_ET_SET_BMAP_NO_IND;
|
---|
| 219 | goto done;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | b = inode_bmap(inode, EXT2_IND_BLOCK);
|
---|
| 223 | retval = ext2fs_alloc_block(fs, b, block_buf, &b);
|
---|
| 224 | if (retval)
|
---|
| 225 | goto done;
|
---|
| 226 | inode_bmap(inode, EXT2_DIND_BLOCK) = b;
|
---|
| 227 | blocks_alloc++;
|
---|
| 228 | }
|
---|
| 229 | retval = block_dind_bmap(fs, bmap_flags, b, block_buf,
|
---|
| 230 | &blocks_alloc, block, phys_blk);
|
---|
| 231 | goto done;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /* Triply indirect block */
|
---|
| 235 | block -= addr_per_block * addr_per_block;
|
---|
| 236 | b = inode_bmap(inode, EXT2_TIND_BLOCK);
|
---|
| 237 | if (!b) {
|
---|
| 238 | if (!(bmap_flags & BMAP_ALLOC)) {
|
---|
| 239 | if (bmap_flags & BMAP_SET)
|
---|
| 240 | retval = EXT2_ET_SET_BMAP_NO_IND;
|
---|
| 241 | goto done;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | b = inode_bmap(inode, EXT2_DIND_BLOCK);
|
---|
| 245 | retval = ext2fs_alloc_block(fs, b, block_buf, &b);
|
---|
| 246 | if (retval)
|
---|
| 247 | goto done;
|
---|
| 248 | inode_bmap(inode, EXT2_TIND_BLOCK) = b;
|
---|
| 249 | blocks_alloc++;
|
---|
| 250 | }
|
---|
| 251 | retval = block_tind_bmap(fs, bmap_flags, b, block_buf,
|
---|
| 252 | &blocks_alloc, block, phys_blk);
|
---|
| 253 | done:
|
---|
| 254 | ext2fs_free_mem(&buf);
|
---|
| 255 | if ((retval == 0) && (blocks_alloc || inode_dirty)) {
|
---|
| 256 | inode->i_blocks += (blocks_alloc * fs->blocksize) / 512;
|
---|
| 257 | retval = ext2fs_write_inode(fs, ino, inode);
|
---|
| 258 | }
|
---|
| 259 | return retval;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 |
|
---|