Changeset aab85d90 in mainline for uspace/lib/ext4/src/ialloc.c
- Timestamp:
- 2018-08-27T14:17:14Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c606e33, e43d658
- Parents:
- 8867cf6
- git-author:
- Jiri Svoboda <jiri@…> (2018-08-26 17:15:23)
- git-committer:
- Jiri Svoboda <jiri@…> (2018-08-27 14:17:14)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ext4/src/ialloc.c
r8867cf6 raab85d90 1 1 /* 2 * Copyright (c) 2018 Jiri Svoboda 2 3 * Copyright (c) 2012 Frantisek Princ 3 4 * All rights reserved. … … 311 312 } 312 313 314 /** Allocate a specific I-node. 315 * 316 * @param fs Filesystem to allocate i-node on 317 * @param inode I-node to allocate 318 * @param is_dir Flag if allocated i-node will be file or directory 319 * 320 * @return Error code 321 * 322 */ 323 errno_t ext4_ialloc_alloc_this_inode(ext4_filesystem_t *fs, uint32_t inode, 324 bool is_dir) 325 { 326 ext4_superblock_t *sb = fs->superblock; 327 328 uint32_t bgid = ext4_ialloc_get_bgid_of_inode(sb, inode); 329 uint32_t sb_free_inodes = ext4_superblock_get_free_inodes_count(sb); 330 331 /* Load block group */ 332 ext4_block_group_ref_t *bg_ref; 333 errno_t rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref); 334 if (rc != EOK) 335 return rc; 336 337 ext4_block_group_t *bg = bg_ref->block_group; 338 339 /* Read necessary values for algorithm */ 340 uint32_t free_inodes = ext4_block_group_get_free_inodes_count(bg, sb); 341 uint32_t used_dirs = ext4_block_group_get_used_dirs_count(bg, sb); 342 343 /* Load block with bitmap */ 344 uint32_t bitmap_block_addr = ext4_block_group_get_inode_bitmap( 345 bg_ref->block_group, sb); 346 347 block_t *bitmap_block; 348 rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 349 BLOCK_FLAGS_NONE); 350 if (rc != EOK) { 351 ext4_filesystem_put_block_group_ref(bg_ref); 352 return rc; 353 } 354 355 /* Allocate i-node in the bitmap */ 356 uint32_t index_in_group = ext4_ialloc_inode2index_in_group(sb, inode); 357 ext4_bitmap_set_bit(bitmap_block->data, index_in_group); 358 359 /* Save the bitmap */ 360 bitmap_block->dirty = true; 361 362 rc = block_put(bitmap_block); 363 if (rc != EOK) { 364 ext4_filesystem_put_block_group_ref(bg_ref); 365 return rc; 366 } 367 368 /* Modify filesystem counters */ 369 free_inodes--; 370 ext4_block_group_set_free_inodes_count(bg, sb, free_inodes); 371 372 /* Increment used directories counter */ 373 if (is_dir) { 374 used_dirs++; 375 ext4_block_group_set_used_dirs_count(bg, sb, used_dirs); 376 } 377 378 /* Decrease unused inodes count */ 379 if (ext4_block_group_has_flag(bg, 380 EXT4_BLOCK_GROUP_ITABLE_ZEROED)) { 381 uint32_t unused = 382 ext4_block_group_get_itable_unused(bg, sb); 383 384 uint32_t inodes_in_group = 385 ext4_superblock_get_inodes_in_group(sb, bgid); 386 387 uint32_t free = inodes_in_group - unused; 388 389 if (index_in_group >= free) { 390 unused = inodes_in_group - (index_in_group + 1); 391 ext4_block_group_set_itable_unused(bg, sb, unused); 392 } 393 } 394 395 /* Save modified block group */ 396 bg_ref->dirty = true; 397 398 rc = ext4_filesystem_put_block_group_ref(bg_ref); 399 if (rc != EOK) 400 return rc; 401 402 /* Update superblock */ 403 sb_free_inodes--; 404 ext4_superblock_set_free_inodes_count(sb, sb_free_inodes); 405 406 return EOK; 407 } 408 313 409 /** 314 410 * @}
Note:
See TracChangeset
for help on using the changeset viewer.