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
|
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
reiser4progs/COPYING.
backup.c -- filesystem backup methods. */
#ifndef ENABLE_MINIMAL
#include <reiser4/libreiser4.h>
/* Creates the backup of the given @fs. */
reiser4_backup_t *reiser4_backup_create(reiser4_fs_t *fs) {
reiser4_backup_t *backup;
uint32_t size;
aal_assert("vpf-1387", fs != NULL);
/* Allocating and initializing the backup. */
if (!(backup = aal_calloc(sizeof(*backup), 0)))
return NULL;
backup->fs = fs;
/* Create the backup block. */
size = reiser4_master_get_blksize(fs->master);
if (aal_block_init(&backup->hint.block,
fs->device, size, 0))
{
goto error_free_backup;
}
aal_block_fill(&backup->hint.block, 0);
/* Backup the fs. */
if (reiser4_fs_backup(fs, &backup->hint))
goto error_free_block;
reiser4_backup_mkdirty(backup);
return backup;
error_free_block:
aal_block_fini(&backup->hint.block);
error_free_backup:
aal_free(backup);
return NULL;
}
/* Reading backup blocks and comparing their content that must match for
all backup blocks. */
static errno_t cb_open_backup(uint64_t start, uint64_t width, void *data) {
reiser4_backup_t *backup = (reiser4_backup_t *)data;
aal_block_t *block = (aal_block_t *)backup->data;
aal_block_t *fblock;
errno_t res;
fblock = &backup->hint.block;
/* Reading the first backup block. */
if (fblock->nr == 0) {
fblock->nr = start;
return aal_block_read(fblock);
}
/* The first backup block has been read already. */
block->nr = start;
if ((res = aal_block_read(block)))
return res;
if (aal_memcmp(backup->hint.block.data,
block->data, block->size))
{
aal_error("Backup block %llu differ from "
"previous ones.", start);
return -EIO;
}
return 0;
}
reiser4_backup_t *reiser4_backup_open(reiser4_fs_t *fs) {
reiser4_backup_t *backup;
uint32_t size;
errno_t res;
/* Allocating and initializing the backup. */
if (!(backup = aal_calloc(sizeof(*backup), 0)))
return NULL;
backup->fs = fs;
/* Create the backup block. */
size = reiser4_master_get_blksize(fs->master);
if ((res = aal_block_init(&backup->hint.block,
fs->device, size, 0)))
{
goto error_free_backup;
}
aal_block_fill(&backup->hint.block, 0);
/* Create the block to compare with the first one. */
if (!(backup->data = aal_block_alloc(fs->device, size, 0)))
goto error_fini_backup;
aal_block_fill(backup->data, 0);
if (reiser4_backup_layout(backup->fs, cb_open_backup, backup))
goto error_free_block;
aal_block_free(backup->data);
backup->data = NULL;
return backup;
error_free_block:
aal_block_free(backup->data);
error_fini_backup:
aal_block_fini(&backup->hint.block);
error_free_backup:
aal_free(backup);
return NULL;
}
/* Check for valideness opened backup. */
errno_t reiser4_backup_valid(reiser4_backup_t *backup) {
backup_hint_t hint;
reiser4_fs_t *fs;
uint32_t size;
errno_t res;
aal_assert("vpf-1727", backup != NULL);
fs = backup->fs;
size = reiser4_master_get_blksize(fs->master);
if ((res = aal_block_init(&hint.block, fs->device, size, 0)))
return res;
aal_block_fill(&hint.block, 0);
/* Backup the fs. */
if ((res = reiser4_fs_backup(fs, &hint)))
goto error_fini_hint;
/* Compare the fresh backup with the read one. */
res = aal_memcmp(backup->hint.block.data, hint.block.data, size);
aal_block_fini(&hint.block);
return res ? -EIO: 0;
error_fini_hint:
aal_block_fini(&hint.block);
return res;
}
/* Frees the fs backup. */
void reiser4_backup_close(reiser4_backup_t *backup) {
aal_assert("vpf-1398", backup != NULL);
aal_block_fini(&backup->hint.block);
aal_free(backup);
}
/* Assign the block to @blk block number and write it. */
static errno_t cb_write_backup(blk_t blk, uint64_t count, void *data) {
aal_block_t *block = (aal_block_t *)data;
aal_block_move(block, block->device, blk);
aal_block_write(block);
return 0;
}
/* Write the backup to REISER4_BACKUPS_MAX blocks. */
errno_t reiser4_backup_sync(reiser4_backup_t *backup) {
errno_t res;
aal_assert("vpf-1410", backup != NULL);
aal_assert("vpf-1410", backup->fs != NULL);
/* Prepare the block for writing. */
if (!reiser4_backup_isdirty(backup))
return 0;
/* Write the block to all backup copies. */
res = reiser4_backup_layout(backup->fs, cb_write_backup,
&backup->hint.block);
reiser4_backup_mkclean(backup);
return res;
}
static errno_t cb_region(uint64_t blk, uint64_t count, void *data) {
/* 2nd block in the given region is a backup block. */
*((blk_t *)data) = (count == 1) ? 0 :blk + 1;
return 0;
}
errno_t reiser4_backup_layout_body(reiser4_alloc_t *alloc,
uint32_t blksize, uint64_t len,
region_func_t func, void *data)
{
blk_t copy, prev, blk;
errno_t res;
blk = 2;
prev = 0;
while (1) {
blk = BACKUP_EXP_LAYOUT(blk);
if (blk <= prev)
continue;
if (blk > len)
return 0;
reiser4_alloc_region(alloc, blk, cb_region, ©);
if (copy < REISER4_BACKUP_START(blksize))
copy = REISER4_BACKUP_START(blksize);
if (copy > len)
return 0;
if (copy <= prev)
continue;
if ((res = func(copy, 1, data)))
return res;
prev = copy;
}
}
/* Backup is saved in REISER4_BACKUPS_MAX blocks spreaded across the fs
aligned by the next bitmap block.
Note: Backup should not be touched another time -- do not open them
another time, even for the layout operation. */
errno_t reiser4_backup_layout(reiser4_fs_t *fs,
region_func_t func,
void *data)
{
count_t len, blksize;
aal_assert("vpf-1399", fs != NULL);
aal_assert("vpf-1400", func != NULL);
len = reiser4_format_get_len(fs->format);
blksize = reiser4_master_get_blksize(fs->master);
return reiser4_backup_layout_body(fs->alloc, blksize, len, func, data);
}
static errno_t cb_region_last(blk_t blk, uint64_t count, void *data) {
*((blk_t *)data) = count == 1 ? 0 :
blk + count - 1;
return 0;
}
#define REISER4_BACKUPS_MAX 16
errno_t reiser4_old_backup_layout(reiser4_fs_t *fs,
region_func_t region_func,
void *data)
{
errno_t res;
count_t len;
count_t delta;
blk_t prev = 0;
blk_t blk, copy;
aal_assert("vpf-1399", fs != NULL);
aal_assert("vpf-1400", region_func != NULL);
len = reiser4_format_get_len(fs->format);
delta = len / (REISER4_BACKUPS_MAX + 1);
for (blk = delta - 1; blk < len; blk += delta) {
reiser4_alloc_region(fs->alloc, blk, cb_region_last, ©);
/* If copy == 0 -- it is not possible to have the last copy
on this fs as the last block is the allocator one. If the
blk number for the copy is the same as the previous one,
skip another copy as fs is pretty small. */
if (!copy || copy == prev)
continue;
if ((res = region_func(copy, 1, data)))
return res;
prev = copy;
}
return 0;
}
#endif
|