initialize.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * initialize.c --- initialize a filesystem handle given superblock
  3. * parameters. Used by mke2fs when initializing a filesystem.
  4. *
  5. * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #if HAVE_UNISTD_H
  15. #include <unistd.h>
  16. #endif
  17. #include <fcntl.h>
  18. #include <time.h>
  19. #if HAVE_SYS_STAT_H
  20. #include <sys/stat.h>
  21. #endif
  22. #if HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #include "ext2_fs.h"
  26. #include "ext2fs.h"
  27. #if defined(__linux__) && defined(EXT2_OS_LINUX)
  28. #define CREATOR_OS EXT2_OS_LINUX
  29. #else
  30. #if defined(__GNU__) && defined(EXT2_OS_HURD)
  31. #define CREATOR_OS EXT2_OS_HURD
  32. #else
  33. #if defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD)
  34. #define CREATOR_OS EXT2_OS_FREEBSD
  35. #else
  36. #if defined(LITES) && defined(EXT2_OS_LITES)
  37. #define CREATOR_OS EXT2_OS_LITES
  38. #else
  39. #define CREATOR_OS EXT2_OS_LINUX /* by default */
  40. #endif /* defined(LITES) && defined(EXT2_OS_LITES) */
  41. #endif /* defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD) */
  42. #endif /* defined(__GNU__) && defined(EXT2_OS_HURD) */
  43. #endif /* defined(__linux__) && defined(EXT2_OS_LINUX) */
  44. /*
  45. * Note we override the kernel include file's idea of what the default
  46. * check interval (never) should be. It's a good idea to check at
  47. * least *occasionally*, specially since servers will never rarely get
  48. * to reboot, since Linux is so robust these days. :-)
  49. *
  50. * 180 days (six months) seems like a good value.
  51. */
  52. #ifdef EXT2_DFL_CHECKINTERVAL
  53. #undef EXT2_DFL_CHECKINTERVAL
  54. #endif
  55. #define EXT2_DFL_CHECKINTERVAL (86400L * 180L)
  56. /*
  57. * Calculate the number of GDT blocks to reserve for online filesystem growth.
  58. * The absolute maximum number of GDT blocks we can reserve is determined by
  59. * the number of block pointers that can fit into a single block.
  60. */
  61. static int calc_reserved_gdt_blocks(ext2_filsys fs)
  62. {
  63. struct ext2_super_block *sb = fs->super;
  64. unsigned long bpg = sb->s_blocks_per_group;
  65. unsigned int gdpb = fs->blocksize / sizeof(struct ext2_group_desc);
  66. unsigned long max_blocks = 0xffffffff;
  67. unsigned long rsv_groups;
  68. int rsv_gdb;
  69. /* We set it at 1024x the current filesystem size, or
  70. * the upper block count limit (2^32), whichever is lower.
  71. */
  72. if (sb->s_blocks_count < max_blocks / 1024)
  73. max_blocks = sb->s_blocks_count * 1024;
  74. rsv_groups = (max_blocks - sb->s_first_data_block + bpg - 1) / bpg;
  75. rsv_gdb = (rsv_groups + gdpb - 1) / gdpb - fs->desc_blocks;
  76. if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb))
  77. rsv_gdb = EXT2_ADDR_PER_BLOCK(sb);
  78. #ifdef RES_GDT_DEBUG
  79. printf("max_blocks %lu, rsv_groups = %lu, rsv_gdb = %lu\n",
  80. max_blocks, rsv_groups, rsv_gdb);
  81. #endif
  82. return rsv_gdb;
  83. }
  84. errcode_t ext2fs_initialize(const char *name, int flags,
  85. struct ext2_super_block *param,
  86. io_manager manager, ext2_filsys *ret_fs)
  87. {
  88. ext2_filsys fs;
  89. errcode_t retval;
  90. struct ext2_super_block *super;
  91. int frags_per_block;
  92. unsigned int rem;
  93. unsigned int overhead = 0;
  94. blk_t group_block;
  95. unsigned int ipg;
  96. dgrp_t i;
  97. blk_t numblocks;
  98. int rsv_gdt;
  99. char *buf;
  100. if (!param || !param->s_blocks_count)
  101. return EXT2_ET_INVALID_ARGUMENT;
  102. retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
  103. if (retval)
  104. return retval;
  105. memset(fs, 0, sizeof(struct struct_ext2_filsys));
  106. fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
  107. fs->flags = flags | EXT2_FLAG_RW;
  108. fs->umask = 022;
  109. #ifdef WORDS_BIGENDIAN
  110. fs->flags |= EXT2_FLAG_SWAP_BYTES;
  111. #endif
  112. retval = manager->open(name, IO_FLAG_RW, &fs->io);
  113. if (retval)
  114. goto cleanup;
  115. fs->image_io = fs->io;
  116. fs->io->app_data = fs;
  117. retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
  118. if (retval)
  119. goto cleanup;
  120. strcpy(fs->device_name, name);
  121. retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super);
  122. if (retval)
  123. goto cleanup;
  124. fs->super = super;
  125. memset(super, 0, SUPERBLOCK_SIZE);
  126. #define set_field(field, default) (super->field = param->field ? \
  127. param->field : (default))
  128. super->s_magic = EXT2_SUPER_MAGIC;
  129. super->s_state = EXT2_VALID_FS;
  130. set_field(s_log_block_size, 0); /* default blocksize: 1024 bytes */
  131. set_field(s_log_frag_size, 0); /* default fragsize: 1024 bytes */
  132. set_field(s_first_data_block, super->s_log_block_size ? 0 : 1);
  133. set_field(s_max_mnt_count, EXT2_DFL_MAX_MNT_COUNT);
  134. set_field(s_errors, EXT2_ERRORS_DEFAULT);
  135. set_field(s_feature_compat, 0);
  136. set_field(s_feature_incompat, 0);
  137. set_field(s_feature_ro_compat, 0);
  138. set_field(s_first_meta_bg, 0);
  139. if (super->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
  140. retval = EXT2_ET_UNSUPP_FEATURE;
  141. goto cleanup;
  142. }
  143. if (super->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
  144. retval = EXT2_ET_RO_UNSUPP_FEATURE;
  145. goto cleanup;
  146. }
  147. set_field(s_rev_level, EXT2_GOOD_OLD_REV);
  148. if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
  149. set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
  150. set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
  151. }
  152. set_field(s_checkinterval, EXT2_DFL_CHECKINTERVAL);
  153. super->s_mkfs_time = super->s_lastcheck = time(NULL);
  154. super->s_creator_os = CREATOR_OS;
  155. fs->blocksize = EXT2_BLOCK_SIZE(super);
  156. fs->fragsize = EXT2_FRAG_SIZE(super);
  157. frags_per_block = fs->blocksize / fs->fragsize;
  158. /* default: (fs->blocksize*8) blocks/group, up to 2^16 (GDT limit) */
  159. set_field(s_blocks_per_group, fs->blocksize * 8);
  160. if (super->s_blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(super))
  161. super->s_blocks_per_group = EXT2_MAX_BLOCKS_PER_GROUP(super);
  162. super->s_frags_per_group = super->s_blocks_per_group * frags_per_block;
  163. super->s_blocks_count = param->s_blocks_count;
  164. super->s_r_blocks_count = param->s_r_blocks_count;
  165. if (super->s_r_blocks_count >= param->s_blocks_count) {
  166. retval = EXT2_ET_INVALID_ARGUMENT;
  167. goto cleanup;
  168. }
  169. /*
  170. * If we're creating an external journal device, we don't need
  171. * to bother with the rest.
  172. */
  173. if (super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
  174. fs->group_desc_count = 0;
  175. ext2fs_mark_super_dirty(fs);
  176. *ret_fs = fs;
  177. return 0;
  178. }
  179. retry:
  180. fs->group_desc_count = (super->s_blocks_count -
  181. super->s_first_data_block +
  182. EXT2_BLOCKS_PER_GROUP(super) - 1)
  183. / EXT2_BLOCKS_PER_GROUP(super);
  184. if (fs->group_desc_count == 0) {
  185. retval = EXT2_ET_TOOSMALL;
  186. goto cleanup;
  187. }
  188. fs->desc_blocks = (fs->group_desc_count +
  189. EXT2_DESC_PER_BLOCK(super) - 1)
  190. / EXT2_DESC_PER_BLOCK(super);
  191. i = fs->blocksize >= 4096 ? 1 : 4096 / fs->blocksize;
  192. set_field(s_inodes_count, super->s_blocks_count / i);
  193. /*
  194. * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
  195. * that we have enough inodes for the filesystem(!)
  196. */
  197. if (super->s_inodes_count < EXT2_FIRST_INODE(super)+1)
  198. super->s_inodes_count = EXT2_FIRST_INODE(super)+1;
  199. /*
  200. * There should be at least as many inodes as the user
  201. * requested. Figure out how many inodes per group that
  202. * should be. But make sure that we don't allocate more than
  203. * one bitmap's worth of inodes each group.
  204. */
  205. ipg = (super->s_inodes_count + fs->group_desc_count - 1) /
  206. fs->group_desc_count;
  207. if (ipg > fs->blocksize * 8) {
  208. if (super->s_blocks_per_group >= 256) {
  209. /* Try again with slightly different parameters */
  210. super->s_blocks_per_group -= 8;
  211. super->s_blocks_count = param->s_blocks_count;
  212. super->s_frags_per_group = super->s_blocks_per_group *
  213. frags_per_block;
  214. goto retry;
  215. } else
  216. return EXT2_ET_TOO_MANY_INODES;
  217. }
  218. if (ipg > (unsigned) EXT2_MAX_INODES_PER_GROUP(super))
  219. ipg = EXT2_MAX_INODES_PER_GROUP(super);
  220. super->s_inodes_per_group = ipg;
  221. if (super->s_inodes_count > ipg * fs->group_desc_count)
  222. super->s_inodes_count = ipg * fs->group_desc_count;
  223. /*
  224. * Make sure the number of inodes per group completely fills
  225. * the inode table blocks in the descriptor. If not, add some
  226. * additional inodes/group. Waste not, want not...
  227. */
  228. fs->inode_blocks_per_group = (((super->s_inodes_per_group *
  229. EXT2_INODE_SIZE(super)) +
  230. EXT2_BLOCK_SIZE(super) - 1) /
  231. EXT2_BLOCK_SIZE(super));
  232. super->s_inodes_per_group = ((fs->inode_blocks_per_group *
  233. EXT2_BLOCK_SIZE(super)) /
  234. EXT2_INODE_SIZE(super));
  235. /*
  236. * Finally, make sure the number of inodes per group is a
  237. * multiple of 8. This is needed to simplify the bitmap
  238. * splicing code.
  239. */
  240. super->s_inodes_per_group &= ~7;
  241. fs->inode_blocks_per_group = (((super->s_inodes_per_group *
  242. EXT2_INODE_SIZE(super)) +
  243. EXT2_BLOCK_SIZE(super) - 1) /
  244. EXT2_BLOCK_SIZE(super));
  245. /*
  246. * adjust inode count to reflect the adjusted inodes_per_group
  247. */
  248. super->s_inodes_count = super->s_inodes_per_group *
  249. fs->group_desc_count;
  250. super->s_free_inodes_count = super->s_inodes_count;
  251. /*
  252. * check the number of reserved group descriptor table blocks
  253. */
  254. if (super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)
  255. rsv_gdt = calc_reserved_gdt_blocks(fs);
  256. else
  257. rsv_gdt = 0;
  258. set_field(s_reserved_gdt_blocks, rsv_gdt);
  259. if (super->s_reserved_gdt_blocks > EXT2_ADDR_PER_BLOCK(super)) {
  260. retval = EXT2_ET_RES_GDT_BLOCKS;
  261. goto cleanup;
  262. }
  263. /*
  264. * Overhead is the number of bookkeeping blocks per group. It
  265. * includes the superblock backup, the group descriptor
  266. * backups, the inode bitmap, the block bitmap, and the inode
  267. * table.
  268. */
  269. overhead = (int) (2 + fs->inode_blocks_per_group);
  270. if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
  271. overhead += 1 + fs->desc_blocks + super->s_reserved_gdt_blocks;
  272. /* This can only happen if the user requested too many inodes */
  273. if (overhead > super->s_blocks_per_group)
  274. return EXT2_ET_TOO_MANY_INODES;
  275. /*
  276. * See if the last group is big enough to support the
  277. * necessary data structures. If not, we need to get rid of
  278. * it.
  279. */
  280. rem = ((super->s_blocks_count - super->s_first_data_block) %
  281. super->s_blocks_per_group);
  282. if ((fs->group_desc_count == 1) && rem && (rem < overhead))
  283. return EXT2_ET_TOOSMALL;
  284. if (rem && (rem < overhead+50)) {
  285. super->s_blocks_count -= rem;
  286. goto retry;
  287. }
  288. /*
  289. * At this point we know how big the filesystem will be. So
  290. * we can do any and all allocations that depend on the block
  291. * count.
  292. */
  293. retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
  294. if (retval)
  295. goto cleanup;
  296. sprintf(buf, "block bitmap for %s", fs->device_name);
  297. retval = ext2fs_allocate_block_bitmap(fs, buf, &fs->block_map);
  298. if (retval)
  299. goto cleanup;
  300. sprintf(buf, "inode bitmap for %s", fs->device_name);
  301. retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
  302. if (retval)
  303. goto cleanup;
  304. ext2fs_free_mem(&buf);
  305. retval = ext2fs_get_mem((size_t) fs->desc_blocks * fs->blocksize,
  306. &fs->group_desc);
  307. if (retval)
  308. goto cleanup;
  309. memset(fs->group_desc, 0, (size_t) fs->desc_blocks * fs->blocksize);
  310. /*
  311. * Reserve the superblock and group descriptors for each
  312. * group, and fill in the correct group statistics for group.
  313. * Note that although the block bitmap, inode bitmap, and
  314. * inode table have not been allocated (and in fact won't be
  315. * by this routine), they are accounted for nevertheless.
  316. */
  317. group_block = super->s_first_data_block;
  318. super->s_free_blocks_count = 0;
  319. for (i = 0; i < fs->group_desc_count; i++) {
  320. numblocks = ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
  321. super->s_free_blocks_count += numblocks;
  322. fs->group_desc[i].bg_free_blocks_count = numblocks;
  323. fs->group_desc[i].bg_free_inodes_count =
  324. fs->super->s_inodes_per_group;
  325. fs->group_desc[i].bg_used_dirs_count = 0;
  326. group_block += super->s_blocks_per_group;
  327. }
  328. ext2fs_mark_super_dirty(fs);
  329. ext2fs_mark_bb_dirty(fs);
  330. ext2fs_mark_ib_dirty(fs);
  331. io_channel_set_blksize(fs->io, fs->blocksize);
  332. *ret_fs = fs;
  333. return 0;
  334. cleanup:
  335. ext2fs_free(fs);
  336. return retval;
  337. }