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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
|
/* Copyright (C) 2001-2005 by Hans Reiser, licensing governed by
reiser4progs/COPYING.
format40.c -- default disk-layout plugin for reiser4. */
#ifndef ENABLE_MINIMAL
# include <time.h>
# include <stdlib.h>
#endif
#include "format40.h"
#include "format40_repair.h"
reiser4_core_t *format40_core = NULL;
/* All these functions are standard object getters and setters. They are
dedicated to modify object properties and get values from its fields. They
are almost the same and we will not describe each one especially. */
static uint64_t format40_get_root(reiser4_format_ent_t *entity) {
aal_assert("umka-400", entity != NULL);
return get_sb_root_block(SUPER(entity));
}
static uint16_t format40_get_height(reiser4_format_ent_t *entity) {
aal_assert("umka-1123", entity != NULL);
return get_sb_tree_height(SUPER(entity));
}
#ifndef ENABLE_MINIMAL
static uint64_t format40_get_len(reiser4_format_ent_t *entity) {
aal_assert("umka-401", entity != NULL);
return get_sb_block_count(SUPER(entity));
}
static uint64_t format40_get_free(reiser4_format_ent_t *entity) {
aal_assert("umka-402", entity != NULL);
return get_sb_free_blocks(SUPER(entity));
}
static uint32_t format40_get_stamp(reiser4_format_ent_t *entity) {
aal_assert("umka-1122", entity != NULL);
return get_sb_mkfs_id(SUPER(entity));
}
static rid_t format40_get_policy(reiser4_format_ent_t *entity) {
aal_assert("vpf-831", entity != NULL);
return get_sb_policy(SUPER(entity));
}
static uint64_t format40_start(reiser4_format_ent_t *entity) {
format40_t *format = (format40_t *)entity;
aal_assert("vpf-462", format != NULL);
aal_assert("vpf-463", format->device != NULL);
return FORMAT40_BLOCKNR(format->blksize);
}
static uint32_t format40_get_state(reiser4_format_ent_t *entity) {
aal_assert("umka-2651", entity != NULL);
return ((format40_t *)entity)->state;
}
void format40_set_state(reiser4_format_ent_t *entity, uint32_t state)
{
aal_assert("umka-2078", entity != NULL);
((format40_t *)entity)->state = state;
}
static errno_t format40_layout(reiser4_format_ent_t *entity,
region_func_t region_func,
void *data)
{
blk_t blk;
errno_t res;
format40_t *format;
aal_assert("umka-1042", entity != NULL);
aal_assert("umka-1043", region_func != NULL);
format = (format40_t *)entity;
blk = FORMAT40_BLOCKNR(format->blksize);
if ((res = region_func(0, blk, data)))
return res;
return region_func(blk, 1, data);
}
#endif
static int format40_magic(format40_super_t *super) {
return (!aal_strncmp(super->sb_magic, FORMAT40_MAGIC,
sizeof(FORMAT40_MAGIC)));
}
static errno_t format40_super_open(format40_t *format) {
errno_t res;
blk_t offset;
aal_block_t block;
offset = FORMAT40_BLOCKNR(format->blksize);
aal_block_init(&block, format->device,
format->blksize, offset);
if ((res = aal_block_read(&block))) {
aal_error("Can't read format40 super block. "
"%s.", format->device->error);
goto error_free_block;
}
if (!format40_magic((format40_super_t *)block.data)) {
res = -EINVAL;
goto error_free_block;
}
aal_memcpy(&format->super, block.data,
sizeof(format->super));
error_free_block:
aal_block_fini(&block);
return res;
}
static reiser4_format_ent_t *format40_open(aal_device_t *device,
uint32_t blksize)
{
format40_t *format;
aal_assert("umka-393", device != NULL);
/* Initializing format instance. */
if (!(format = aal_calloc(sizeof(*format), 0)))
return NULL;
format->state = 0;
format->plug = &format40_plug;
format->device = device;
format->blksize = blksize;
/* Initializing super block. */
if (format40_super_open(format)) {
aal_free(format);
return NULL;
}
return (reiser4_format_ent_t *)format;
}
static void format40_close(reiser4_format_ent_t *entity) {
aal_assert("umka-398", entity != NULL);
aal_free(entity);
}
#ifndef ENABLE_MINIMAL
static errno_t format40_clobber_block(void *entity, blk_t start,
count_t width, void *data)
{
blk_t blk;
errno_t res;
aal_block_t block;
format40_t *format;
format = (format40_t *)entity;
if ((res = aal_block_init(&block, format->device,
format->blksize, 0)))
{
return res;
}
aal_block_fill(&block, 0);
for (blk = start; blk < start + width; blk++) {
aal_block_move(&block, format->device, blk);
if ((res = aal_block_write(&block)))
goto error_free_block;
}
error_free_block:
aal_block_fini(&block);
return res;
}
/* Create format object instnace. Create on-disk format specific suber block
structures. Return format instance to caller. */
static reiser4_format_ent_t *format40_create(aal_device_t *device,
format_hint_t *desc)
{
blk_t start;
uint64_t flags;
format40_t *format;
format40_super_t *super;
aal_assert("umka-395", device != NULL);
aal_assert("vpf-1735", desc != NULL);
if (desc->key >= KEY_LAST_ID) {
aal_error("Wrong key plugin id (%u) is specified.", desc->key);
return NULL;
}
/* Initializing format instance. */
if (!(format = aal_calloc(sizeof(*format), 0)))
return NULL;
format->plug = &format40_plug;
format->device = device;
format->blksize = desc->blksize;
format40_mkdirty(format);
/* Initializing super block fields. */
super = (format40_super_t *)&format->super;
/* Setting up format40 magic. */
aal_memcpy(super->sb_magic, FORMAT40_MAGIC,
sizeof(FORMAT40_MAGIC));
/* Number of flushed is zero. */
set_sb_flushes(super, 0);
/* Tree height is 2 -- minimal possible tree height in reiser4. All
needed nodes will be created later and this value will beused for
create them correctly (with level set right). */
set_sb_tree_height(super, 2);
/* Filesystem available blocks is set to @blocks. */
set_sb_block_count(super, desc->blocks);
/* Root node pointer is set to invalid block numeber, and thus, it
shows, that filesyetem is flesh one, that is with not nodes. This
value will be used by tree to behave correctly. */
set_sb_root_block(super, INVAL_BLK);
/* Setting up tail policy to passed @desc->policy value. */
set_sb_policy(super, desc->policy);
/* Set the flags. */
/* FIXME: Hardcoded plugin ids for 2 cases. */
flags = (desc->key == KEY_LARGE_ID) ? (1 << FORMAT40_KEY_LARGE) : 0;
set_sb_flags(super, flags);
/* Initializing fsck related fields. */
srandom(time(0));
set_sb_mkfs_id(super, random());
/* Set version values. */
set_sb_version(super, FORMAT40_VERSION);
/* Clobbering format skipped area in order to let mount to detect
reiser4 correctly without specifying exact filesystem type.
Skipped area is [0-15] blocks. Clobber the master block also to
avoid mounting of the previous reiser4 if this mkfs attempt fails. */
start = FORMAT40_BLOCKNR(format->blksize);
if (format40_clobber_block((reiser4_format_ent_t *)format,
0, start, NULL))
{
aal_error("Can't clobber format skipped area [%u-%llu].",
0, start - 1);
aal_free(format);
return NULL;
}
return (reiser4_format_ent_t *)format;
}
/* All important permanent format40 data get backuped into @hint. */
static errno_t format40_backup(reiser4_format_ent_t *entity, backup_hint_t *hint) {
format40_backup_t *backup;
aal_assert("vpf-1396", entity != NULL);
aal_assert("vpf-1397", hint != NULL);
backup = (format40_backup_t *)
(hint->block.data + hint->off[BK_FORMAT]);
hint->off[BK_FORMAT + 1] = hint->off[BK_FORMAT] + sizeof(*backup);
aal_memcpy(backup->sb_magic, SUPER(entity)->sb_magic,
sizeof(SUPER(entity)->sb_magic));
backup->sb_block_count = SUPER(entity)->sb_block_count;
backup->sb_mkfs_id = SUPER(entity)->sb_mkfs_id;
backup->sb_policy = SUPER(entity)->sb_policy;
backup->sb_flags = SUPER(entity)->sb_flags;
backup->sb_reserved = 0;
/* Get rid of UPDATE_BACKUP flag. */
set_sb_version(backup, get_sb_version(SUPER(entity)));
hint->version = get_sb_version(SUPER(entity));
return 0;
}
/* This function should update all copies of the super block */
static errno_t format40_sync(reiser4_format_ent_t *entity) {
errno_t res;
blk_t offset;
aal_block_t block;
format40_t *format;
aal_assert("umka-394", entity != NULL);
format = (format40_t *)entity;
offset = FORMAT40_BLOCKNR(format->blksize);
if ((res = aal_block_init(&block, format->device,
format->blksize, offset)))
{
return res;
}
aal_block_fill(&block, 0);
aal_memcpy(block.data, &format->super,
sizeof(format->super));
if (!(res = aal_block_write(&block)))
format40_mkclean(format);
aal_block_fini(&block);
return res;
}
static errno_t format40_valid(reiser4_format_ent_t *entity) {
format40_t *format;
blk_t max_format_len;
aal_assert("umka-397", entity != NULL);
format = (format40_t *)entity;
max_format_len = aal_device_len(format->device) /
(format->blksize / format->device->blksize);
if (get_sb_block_count(SUPER(format)) > max_format_len) {
aal_error("Superblock has an invalid block count "
"%llu for max possible length %llu blocks.",
get_sb_block_count(SUPER(format)), max_format_len);
return -EINVAL;
}
if (get_sb_root_block(SUPER(format)) <= format40_start(entity) ||
get_sb_root_block(SUPER(format)) >= max_format_len)
{
aal_error("Superblock has an invalid root block "
"%llu. It must lie between %llu and %llu "
"blocks.", get_sb_root_block(SUPER(format)),
format40_start(entity), max_format_len);
return -EINVAL;
}
if (get_sb_version(SUPER(format)) > FORMAT40_VERSION) {
aal_error("The on-disk format version (%u) is greater than "
"the known version (%u). Please update reiser4progs "
"or run fsck.reiser4 --build-sb to fix the fs "
"consistency.", get_sb_version(SUPER(format)),
FORMAT40_VERSION);
return -EINVAL;
}
return 0;
}
static void format40_oid_area(reiser4_format_ent_t *entity,
void **start, uint32_t *len)
{
aal_assert("umka-732", entity != NULL);
*start = &(SUPER(entity)->sb_oid);
*len = sizeof(SUPER(entity)->sb_oid);
}
static rid_t format40_oid_pid(reiser4_format_ent_t *entity) {
return OID_REISER40_ID;
}
static rid_t format40_journal_pid(reiser4_format_ent_t *entity) {
return JOURNAL_REISER40_ID;
}
static rid_t format40_alloc_pid(reiser4_format_ent_t *entity) {
return ALLOC_REISER40_ID;
}
static void format40_set_root(reiser4_format_ent_t *entity, uint64_t root) {
aal_assert("umka-403", entity != NULL);
set_sb_root_block(SUPER(entity), root);
format40_mkdirty(entity);
}
static void format40_set_len(reiser4_format_ent_t *entity, uint64_t blocks) {
aal_assert("umka-404", entity != NULL);
set_sb_block_count(SUPER(entity), blocks);
format40_mkdirty(entity);
}
static void format40_set_free(reiser4_format_ent_t *entity, uint64_t blocks) {
aal_assert("umka-405", entity != NULL);
if (get_sb_free_blocks(SUPER(entity)) == blocks)
return;
set_sb_free_blocks(SUPER(entity), blocks);
format40_mkdirty(entity);
}
static void format40_set_height(reiser4_format_ent_t *entity, uint16_t height) {
aal_assert("umka-555", entity != NULL);
set_sb_tree_height(SUPER(entity), height);
format40_mkdirty(entity);
}
static void format40_set_stamp(reiser4_format_ent_t *entity, uint32_t mkfsid) {
aal_assert("umka-1121", entity != NULL);
set_sb_mkfs_id(SUPER(entity), mkfsid);
format40_mkdirty(entity);
}
static void format40_set_policy(reiser4_format_ent_t *entity, rid_t tail) {
aal_assert("vpf-830", entity != NULL);
set_sb_policy(SUPER(entity), tail);
format40_mkdirty(entity);
}
void format40_set_key(reiser4_format_ent_t *entity, rid_t key) {
uint64_t flags;
aal_assert("vpf-830", entity != NULL);
flags = get_sb_flags(SUPER(entity));
flags &= ~(1 << FORMAT40_KEY_LARGE);
flags |= ((key == KEY_LARGE_ID) ? (1 << FORMAT40_KEY_LARGE) : 0);
set_sb_flags(SUPER(entity), flags);
format40_mkdirty(entity);
}
uint32_t format40_version(reiser4_format_ent_t *entity) {
aal_assert("vpf-1913", entity != NULL);
return get_sb_version(SUPER(entity));
}
#endif
rid_t format40_get_key(reiser4_format_ent_t *entity) {
uint64_t flags;
aal_assert("vpf-1736", entity != NULL);
flags = get_sb_flags(SUPER(entity));
return (flags & (1 << FORMAT40_KEY_LARGE)) ?
KEY_LARGE_ID : KEY_SHORT_ID;
}
#define format40_key_pid format40_get_key
reiser4_format_plug_t format40_plug = {
.p = {
.id = {FORMAT_REISER40_ID, 0, FORMAT_PLUG_TYPE},
#ifndef ENABLE_MINIMAL
.label = "format40",
.desc = "Disk-format plugin.",
#endif
},
#ifndef ENABLE_MINIMAL
.valid = format40_valid,
.sync = format40_sync,
.create = format40_create,
.print = format40_print,
.layout = format40_layout,
.update = format40_update,
.start = format40_start,
.pack = format40_pack,
.unpack = format40_unpack,
.get_len = format40_get_len,
.get_free = format40_get_free,
.get_stamp = format40_get_stamp,
.get_policy = format40_get_policy,
.set_root = format40_set_root,
.set_len = format40_set_len,
.set_free = format40_set_free,
.set_height = format40_set_height,
.set_stamp = format40_set_stamp,
.set_policy = format40_set_policy,
.set_state = format40_set_state,
.get_state = format40_get_state,
.oid_pid = format40_oid_pid,
.oid_area = format40_oid_area,
.journal_pid = format40_journal_pid,
.alloc_pid = format40_alloc_pid,
.backup = format40_backup,
.check_backup = format40_check_backup,
.regenerate = format40_regenerate,
.check_struct = format40_check_struct,
.version = format40_version,
#endif
.open = format40_open,
.close = format40_close,
.get_root = format40_get_root,
.get_height = format40_get_height,
.key_pid = format40_key_pid,
};
|