ext4_ialloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. *
  4. *
  5. * HelenOS:
  6. * Copyright (c) 2012 Martin Sucha
  7. * Copyright (c) 2012 Frantisek Princ
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * - The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /** @addtogroup lwext4
  34. * @{
  35. */
  36. /**
  37. * @file ext4_ialloc.c
  38. * @brief Inode allocation procedures.
  39. */
  40. #include <ext4_config.h>
  41. #include <ext4_types.h>
  42. #include <ext4_misc.h>
  43. #include <ext4_errno.h>
  44. #include <ext4_debug.h>
  45. #include <ext4_trans.h>
  46. #include <ext4_ialloc.h>
  47. #include <ext4_super.h>
  48. #include <ext4_crc32.h>
  49. #include <ext4_fs.h>
  50. #include <ext4_blockdev.h>
  51. #include <ext4_block_group.h>
  52. #include <ext4_bitmap.h>
  53. /**@brief Convert i-node number to relative index in block group.
  54. * @param sb Superblock
  55. * @param inode I-node number to be converted
  56. * @return Index of the i-node in the block group
  57. */
  58. static uint32_t ext4_ialloc_inode_to_bgidx(struct ext4_sblock *sb,
  59. uint32_t inode)
  60. {
  61. uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
  62. return (inode - 1) % inodes_per_group;
  63. }
  64. /**@brief Convert relative index of i-node to absolute i-node number.
  65. * @param sb Superblock
  66. * @param index Index to be converted
  67. * @return Absolute number of the i-node
  68. *
  69. */
  70. static uint32_t ext4_ialloc_bgidx_to_inode(struct ext4_sblock *sb,
  71. uint32_t index, uint32_t bgid)
  72. {
  73. uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
  74. return bgid * inodes_per_group + (index + 1);
  75. }
  76. /**@brief Compute block group number from the i-node number.
  77. * @param sb Superblock
  78. * @param inode I-node number to be found the block group for
  79. * @return Block group number computed from i-node number
  80. */
  81. static uint32_t ext4_ialloc_get_bgid_of_inode(struct ext4_sblock *sb,
  82. uint32_t inode)
  83. {
  84. uint32_t inodes_per_group = ext4_get32(sb, inodes_per_group);
  85. return (inode - 1) / inodes_per_group;
  86. }
  87. #if CONFIG_META_CSUM_ENABLE
  88. static uint32_t ext4_ialloc_bitmap_csum(struct ext4_sblock *sb, void *bitmap)
  89. {
  90. uint32_t csum = 0;
  91. if (ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM)) {
  92. uint32_t inodes_per_group =
  93. ext4_get32(sb, inodes_per_group);
  94. /* First calculate crc32 checksum against fs uuid */
  95. csum = ext4_crc32c(EXT4_CRC32_INIT, sb->uuid, sizeof(sb->uuid));
  96. /* Then calculate crc32 checksum against inode bitmap */
  97. csum = ext4_crc32c(csum, bitmap, (inodes_per_group + 7) / 8);
  98. }
  99. return csum;
  100. }
  101. #else
  102. #define ext4_ialloc_bitmap_csum(...) 0
  103. #endif
  104. void ext4_ialloc_set_bitmap_csum(struct ext4_sblock *sb, struct ext4_bgroup *bg,
  105. void *bitmap __unused)
  106. {
  107. int desc_size = ext4_sb_get_desc_size(sb);
  108. uint32_t csum = ext4_ialloc_bitmap_csum(sb, bitmap);
  109. uint16_t lo_csum = to_le16(csum & 0xFFFF),
  110. hi_csum = to_le16(csum >> 16);
  111. if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
  112. return;
  113. /* See if we need to assign a 32bit checksum */
  114. bg->inode_bitmap_csum_lo = lo_csum;
  115. if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
  116. bg->inode_bitmap_csum_hi = hi_csum;
  117. }
  118. #if CONFIG_META_CSUM_ENABLE
  119. static bool
  120. ext4_ialloc_verify_bitmap_csum(struct ext4_sblock *sb, struct ext4_bgroup *bg,
  121. void *bitmap __unused)
  122. {
  123. int desc_size = ext4_sb_get_desc_size(sb);
  124. uint32_t csum = ext4_ialloc_bitmap_csum(sb, bitmap);
  125. uint16_t lo_csum = to_le16(csum & 0xFFFF),
  126. hi_csum = to_le16(csum >> 16);
  127. if (!ext4_sb_feature_ro_com(sb, EXT4_FRO_COM_METADATA_CSUM))
  128. return true;
  129. if (bg->inode_bitmap_csum_lo != lo_csum)
  130. return false;
  131. if (desc_size == EXT4_MAX_BLOCK_GROUP_DESCRIPTOR_SIZE)
  132. if (bg->inode_bitmap_csum_hi != hi_csum)
  133. return false;
  134. return true;
  135. }
  136. #else
  137. #define ext4_ialloc_verify_bitmap_csum(...) true
  138. #endif
  139. int ext4_ialloc_free_inode(struct ext4_fs *fs, uint32_t index, bool is_dir)
  140. {
  141. struct ext4_sblock *sb = &fs->sb;
  142. /* Compute index of block group and load it */
  143. uint32_t block_group = ext4_ialloc_get_bgid_of_inode(sb, index);
  144. struct ext4_block_group_ref bg_ref;
  145. int rc = ext4_fs_get_block_group_ref(fs, block_group, &bg_ref);
  146. if (rc != EOK)
  147. return rc;
  148. struct ext4_bgroup *bg = bg_ref.block_group;
  149. /* Load i-node bitmap */
  150. ext4_fsblk_t bitmap_block_addr =
  151. ext4_bg_get_inode_bitmap(bg, sb);
  152. struct ext4_block b;
  153. rc = ext4_trans_block_get(fs->bdev, &b, bitmap_block_addr);
  154. if (rc != EOK)
  155. return rc;
  156. if (!ext4_ialloc_verify_bitmap_csum(sb, bg, b.data)) {
  157. ext4_dbg(DEBUG_IALLOC,
  158. DBG_WARN "Bitmap checksum failed."
  159. "Group: %" PRIu32"\n",
  160. bg_ref.index);
  161. }
  162. /* Free i-node in the bitmap */
  163. uint32_t index_in_group = ext4_ialloc_inode_to_bgidx(sb, index);
  164. ext4_bmap_bit_clr(b.data, index_in_group);
  165. ext4_ialloc_set_bitmap_csum(sb, bg, b.data);
  166. ext4_trans_set_block_dirty(b.buf);
  167. /* Put back the block with bitmap */
  168. rc = ext4_block_set(fs->bdev, &b);
  169. if (rc != EOK) {
  170. /* Error in saving bitmap */
  171. ext4_fs_put_block_group_ref(&bg_ref);
  172. return rc;
  173. }
  174. /* If released i-node is a directory, decrement used directories count
  175. */
  176. if (is_dir) {
  177. uint32_t bg_used_dirs = ext4_bg_get_used_dirs_count(bg, sb);
  178. bg_used_dirs--;
  179. ext4_bg_set_used_dirs_count(bg, sb, bg_used_dirs);
  180. }
  181. /* Update block group free inodes count */
  182. uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb);
  183. free_inodes++;
  184. ext4_bg_set_free_inodes_count(bg, sb, free_inodes);
  185. bg_ref.dirty = true;
  186. /* Put back the modified block group */
  187. rc = ext4_fs_put_block_group_ref(&bg_ref);
  188. if (rc != EOK)
  189. return rc;
  190. /* Update superblock free inodes count */
  191. ext4_set32(sb, free_inodes_count,
  192. ext4_get32(sb, free_inodes_count) + 1);
  193. return EOK;
  194. }
  195. int ext4_ialloc_alloc_inode(struct ext4_fs *fs, uint32_t *idx, bool is_dir)
  196. {
  197. struct ext4_sblock *sb = &fs->sb;
  198. uint32_t bgid = fs->last_inode_bg_id;
  199. uint32_t bg_count = ext4_block_group_cnt(sb);
  200. uint32_t sb_free_inodes = ext4_get32(sb, free_inodes_count);
  201. bool rewind = false;
  202. /* Try to find free i-node in all block groups */
  203. while (bgid <= bg_count) {
  204. if (bgid == bg_count) {
  205. if (rewind)
  206. break;
  207. bg_count = fs->last_inode_bg_id;
  208. bgid = 0;
  209. rewind = true;
  210. continue;
  211. }
  212. /* Load block group to check */
  213. struct ext4_block_group_ref bg_ref;
  214. int rc = ext4_fs_get_block_group_ref(fs, bgid, &bg_ref);
  215. if (rc != EOK)
  216. return rc;
  217. struct ext4_bgroup *bg = bg_ref.block_group;
  218. /* Read necessary values for algorithm */
  219. uint32_t free_inodes = ext4_bg_get_free_inodes_count(bg, sb);
  220. uint32_t used_dirs = ext4_bg_get_used_dirs_count(bg, sb);
  221. /* Check if this block group is good candidate for allocation */
  222. if (free_inodes > 0) {
  223. /* Load block with bitmap */
  224. ext4_fsblk_t bmp_blk_add = ext4_bg_get_inode_bitmap(bg, sb);
  225. struct ext4_block b;
  226. rc = ext4_trans_block_get(fs->bdev, &b, bmp_blk_add);
  227. if (rc != EOK) {
  228. ext4_fs_put_block_group_ref(&bg_ref);
  229. return rc;
  230. }
  231. if (!ext4_ialloc_verify_bitmap_csum(sb, bg, b.data)) {
  232. ext4_dbg(DEBUG_IALLOC,
  233. DBG_WARN "Bitmap checksum failed."
  234. "Group: %" PRIu32"\n",
  235. bg_ref.index);
  236. }
  237. /* Try to allocate i-node in the bitmap */
  238. uint32_t inodes_in_bg;
  239. uint32_t idx_in_bg;
  240. inodes_in_bg = ext4_inodes_in_group_cnt(sb, bgid);
  241. rc = ext4_bmap_bit_find_clr(b.data, 0, inodes_in_bg,
  242. &idx_in_bg);
  243. /* Block group has not any free i-node */
  244. if (rc == ENOSPC) {
  245. rc = ext4_block_set(fs->bdev, &b);
  246. if (rc != EOK) {
  247. ext4_fs_put_block_group_ref(&bg_ref);
  248. return rc;
  249. }
  250. rc = ext4_fs_put_block_group_ref(&bg_ref);
  251. if (rc != EOK)
  252. return rc;
  253. continue;
  254. }
  255. ext4_bmap_bit_set(b.data, idx_in_bg);
  256. /* Free i-node found, save the bitmap */
  257. ext4_ialloc_set_bitmap_csum(sb,bg,
  258. b.data);
  259. ext4_trans_set_block_dirty(b.buf);
  260. ext4_block_set(fs->bdev, &b);
  261. if (rc != EOK) {
  262. ext4_fs_put_block_group_ref(&bg_ref);
  263. return rc;
  264. }
  265. /* Modify filesystem counters */
  266. free_inodes--;
  267. ext4_bg_set_free_inodes_count(bg, sb, free_inodes);
  268. /* Increment used directories counter */
  269. if (is_dir) {
  270. used_dirs++;
  271. ext4_bg_set_used_dirs_count(bg, sb, used_dirs);
  272. }
  273. /* Decrease unused inodes count */
  274. uint32_t unused =
  275. ext4_bg_get_itable_unused(bg, sb);
  276. uint32_t free = inodes_in_bg - unused;
  277. if (idx_in_bg >= free) {
  278. unused = inodes_in_bg - (idx_in_bg + 1);
  279. ext4_bg_set_itable_unused(bg, sb, unused);
  280. }
  281. /* Save modified block group */
  282. bg_ref.dirty = true;
  283. rc = ext4_fs_put_block_group_ref(&bg_ref);
  284. if (rc != EOK)
  285. return rc;
  286. /* Update superblock */
  287. sb_free_inodes--;
  288. ext4_set32(sb, free_inodes_count, sb_free_inodes);
  289. /* Compute the absolute i-nodex number */
  290. *idx = ext4_ialloc_bgidx_to_inode(sb, idx_in_bg, bgid);
  291. fs->last_inode_bg_id = bgid;
  292. return EOK;
  293. }
  294. /* Block group not modified, put it and jump to the next block
  295. * group */
  296. ext4_fs_put_block_group_ref(&bg_ref);
  297. if (rc != EOK)
  298. return rc;
  299. ++bgid;
  300. }
  301. return ENOSPC;
  302. }
  303. /**
  304. * @}
  305. */