1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
/*
* image.c --- writes out the critical parts of the filesystem as a
* flat file.
*
* Copyright (C) 2000 Theodore Ts'o.
*
* Note: this uses the POSIX IO interfaces, unlike most of the other
* functions in this library. So sue me.
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Library
* General Public License, version 2.
* %End-Header%
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#include <fcntl.h>
#include <time.h>
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include "ext2_fs.h"
#include "ext2fs.h"
#ifndef HAVE_TYPE_SSIZE_T
typedef int ssize_t;
#endif
/*
* This function returns 1 if the specified block is all zeros
*/
static int check_zero_block(char *buf, int blocksize)
{
char *cp = buf;
int left = blocksize;
while (left > 0) {
if (*cp++)
return 0;
left--;
}
return 1;
}
/*
* Write the inode table out as a single block.
*/
#define BUF_BLOCKS 32
errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
{
dgrp_t group;
ssize_t left, c, d;
char *buf, *cp;
blk64_t blk;
ssize_t actual;
errcode_t retval;
ext2_loff_t r;
buf = malloc(fs->blocksize * BUF_BLOCKS);
if (!buf)
return ENOMEM;
for (group = 0; group < fs->group_desc_count; group++) {
blk = ext2fs_inode_table_loc(fs, group);
if (!blk) {
retval = EXT2_ET_MISSING_INODE_TABLE;
goto errout;
}
left = fs->inode_blocks_per_group;
if ((blk < fs->super->s_first_data_block) ||
(blk + left - 1 >= ext2fs_blocks_count(fs->super))) {
retval = EXT2_ET_GDESC_BAD_INODE_TABLE;
goto errout;
}
while (left) {
c = BUF_BLOCKS;
if (c > left)
c = left;
retval = io_channel_read_blk64(fs->io, blk, c, buf);
if (retval)
goto errout;
cp = buf;
while (c) {
if (!(flags & IMAGER_FLAG_SPARSEWRITE)) {
d = c;
goto skip_sparse;
}
/* Skip zero blocks */
if (check_zero_block(cp, fs->blocksize)) {
c--;
blk++;
left--;
cp += fs->blocksize;
r = ext2fs_llseek(fd, fs->blocksize,
SEEK_CUR);
if (r < 0) {
retval = errno;
goto errout;
}
continue;
}
/* Find non-zero blocks */
for (d = 1; d < c; d++) {
if (check_zero_block(cp +
d * fs->blocksize,
fs->blocksize))
break;
}
skip_sparse:
actual = write(fd, cp, d * fs->blocksize);
if (actual == -1) {
retval = errno;
goto errout;
}
if (actual != d * fs->blocksize) {
retval = EXT2_ET_SHORT_WRITE;
goto errout;
}
blk += d;
left -= d;
cp += d * fs->blocksize;
c -= d;
}
}
}
retval = 0;
errout:
free(buf);
return retval;
}
/*
* Read in the inode table and stuff it into place
*/
errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
int flags EXT2FS_ATTR((unused)))
{
dgrp_t group;
ssize_t c, left;
char *buf;
blk64_t blk;
ssize_t actual;
errcode_t retval;
buf = malloc(fs->blocksize * BUF_BLOCKS);
if (!buf)
return ENOMEM;
for (group = 0; group < fs->group_desc_count; group++) {
blk = ext2fs_inode_table_loc(fs, group);
if (!blk) {
retval = EXT2_ET_MISSING_INODE_TABLE;
goto errout;
}
left = fs->inode_blocks_per_group;
while (left) {
c = BUF_BLOCKS;
if (c > left)
c = left;
actual = read(fd, buf, fs->blocksize * c);
if (actual == -1) {
retval = errno;
goto errout;
}
if (actual != fs->blocksize * c) {
retval = EXT2_ET_SHORT_READ;
goto errout;
}
retval = io_channel_write_blk64(fs->io, blk, c, buf);
if (retval)
goto errout;
blk += c;
left -= c;
}
}
retval = ext2fs_flush_icache(fs);
errout:
free(buf);
return retval;
}
/*
* Write out superblock and group descriptors
*/
errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
int flags EXT2FS_ATTR((unused)))
{
char *buf, *cp;
ssize_t actual;
errcode_t retval;
#ifdef WORDS_BIGENDIAN
unsigned int groups_per_block;
struct ext2_group_desc *gdp;
int j;
#endif
if (fs->group_desc == NULL)
return EXT2_ET_NO_GDESC;
buf = malloc(fs->blocksize);
if (!buf)
return ENOMEM;
/*
* Write out the superblock
*/
memset(buf, 0, fs->blocksize);
#ifdef WORDS_BIGENDIAN
/*
* We're writing out superblock so let's convert
* it to little endian and then back if needed
*/
ext2fs_swap_super(fs->super);
memcpy(buf, fs->super, SUPERBLOCK_SIZE);
ext2fs_swap_super(fs->super);
#else
memcpy(buf, fs->super, SUPERBLOCK_SIZE);
#endif
actual = write(fd, buf, fs->blocksize);
if (actual == -1) {
retval = errno;
goto errout;
}
if (actual != (ssize_t) fs->blocksize) {
retval = EXT2_ET_SHORT_WRITE;
goto errout;
}
/*
* Now write out the block group descriptors
*/
cp = (char *) fs->group_desc;
#ifdef WORDS_BIGENDIAN
/*
* Convert group descriptors to little endian and back
* if needed
*/
groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
for (j=0; j < groups_per_block*fs->desc_blocks; j++) {
gdp = ext2fs_group_desc(fs, fs->group_desc, j);
if (gdp)
ext2fs_swap_group_desc2(fs, gdp);
}
#endif
actual = write(fd, cp, (ssize_t)fs->blocksize * fs->desc_blocks);
#ifdef WORDS_BIGENDIAN
groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
for (j=0; j < groups_per_block*fs->desc_blocks; j++) {
gdp = ext2fs_group_desc(fs, fs->group_desc, j);
if (gdp)
ext2fs_swap_group_desc2(fs, gdp);
}
#endif
if (actual == -1) {
retval = errno;
goto errout;
}
if (actual != (ssize_t)(fs->blocksize * fs->desc_blocks)) {
retval = EXT2_ET_SHORT_WRITE;
goto errout;
}
retval = 0;
errout:
free(buf);
return retval;
}
/*
* Read the superblock and group descriptors and overwrite them.
*/
errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
int flags EXT2FS_ATTR((unused)))
{
char *buf;
ssize_t actual, size;
errcode_t retval;
size = (ssize_t)fs->blocksize * (fs->desc_blocks + 1);
buf = malloc(size);
if (!buf)
return ENOMEM;
/*
* Read it all in.
*/
actual = read(fd, buf, size);
if (actual == -1) {
retval = errno;
goto errout;
}
if (actual != size) {
retval = EXT2_ET_SHORT_READ;
goto errout;
}
/*
* Now copy in the superblock and group descriptors
*/
memcpy(fs->super, buf, SUPERBLOCK_SIZE);
memcpy(fs->group_desc, buf + fs->blocksize,
(ssize_t)fs->blocksize * fs->desc_blocks);
retval = 0;
errout:
free(buf);
return retval;
}
/*
* Write the block/inode bitmaps.
*/
errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
{
ext2fs_generic_bitmap bmap;
errcode_t retval;
ssize_t actual;
size_t c;
__u64 itr, cnt, size, total_size;
char buf[1024];
if (flags & IMAGER_FLAG_INODEMAP) {
if (!fs->inode_map) {
retval = ext2fs_read_inode_bitmap(fs);
if (retval)
return retval;
}
bmap = fs->inode_map;
itr = 1;
cnt = (__u64)EXT2_INODES_PER_GROUP(fs->super) *
fs->group_desc_count;
size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
} else {
if (!fs->block_map) {
retval = ext2fs_read_block_bitmap(fs);
if (retval)
return retval;
}
bmap = fs->block_map;
itr = fs->super->s_first_data_block;
cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count);
size = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
}
total_size = size * fs->group_desc_count;
while (cnt > 0) {
size = sizeof(buf);
if (size > (cnt >> 3))
size = (cnt >> 3);
if (size == 0)
break;
retval = ext2fs_get_generic_bmap_range(bmap, itr,
size << 3, buf);
if (retval)
return retval;
actual = write(fd, buf, size);
if (actual == -1)
return errno;
if (actual != (int) size)
return EXT2_ET_SHORT_READ;
itr += size << 3;
cnt -= size << 3;
}
size = total_size % fs->blocksize;
memset(buf, 0, sizeof(buf));
if (size) {
size = fs->blocksize - size;
while (size) {
c = size;
if (c > (int) sizeof(buf))
c = sizeof(buf);
actual = write(fd, buf, c);
if (actual < 0)
return errno;
if ((size_t) actual != c)
return EXT2_ET_SHORT_WRITE;
size -= c;
}
}
return 0;
}
/*
* Read the block/inode bitmaps.
*/
errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
{
ext2fs_generic_bitmap bmap;
errcode_t retval;
__u64 itr, cnt;
char buf[1024];
unsigned int size;
ssize_t actual;
if (flags & IMAGER_FLAG_INODEMAP) {
if (!fs->inode_map) {
retval = ext2fs_read_inode_bitmap(fs);
if (retval)
return retval;
}
bmap = fs->inode_map;
itr = 1;
cnt = (__u64)EXT2_INODES_PER_GROUP(fs->super) *
fs->group_desc_count;
size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
} else {
if (!fs->block_map) {
retval = ext2fs_read_block_bitmap(fs);
if (retval)
return retval;
}
bmap = fs->block_map;
itr = fs->super->s_first_data_block;
cnt = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count);
size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
}
while (cnt > 0) {
size = sizeof(buf);
if (size > (cnt >> 3))
size = (cnt >> 3);
if (size == 0)
break;
actual = read(fd, buf, size);
if (actual == -1)
return errno;
if (actual != (int) size)
return EXT2_ET_SHORT_READ;
retval = ext2fs_set_generic_bmap_range(bmap, itr,
size << 3, buf);
if (retval)
return retval;
itr += size << 3;
cnt -= size << 3;
}
return 0;
}
|