3
0

openfs.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * openfs.c --- open an ext2 filesystem
  3. *
  4. * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
  5. *
  6. * %Begin-Header%
  7. * This file may be redistributed under the terms of the GNU Public
  8. * License.
  9. * %End-Header%
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <fcntl.h>
  17. #include <time.h>
  18. #if HAVE_SYS_STAT_H
  19. #include <sys/stat.h>
  20. #endif
  21. #if HAVE_SYS_TYPES_H
  22. #include <sys/types.h>
  23. #endif
  24. #include "ext2_fs.h"
  25. #include "ext2fs.h"
  26. #include "e2image.h"
  27. blk_t ext2fs_descriptor_block_loc(ext2_filsys fs, blk_t group_block, dgrp_t i)
  28. {
  29. int bg;
  30. int has_super = 0;
  31. int ret_blk;
  32. if (!(fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) ||
  33. (i < fs->super->s_first_meta_bg))
  34. return (group_block + i + 1);
  35. bg = (fs->blocksize / sizeof (struct ext2_group_desc)) * i;
  36. if (ext2fs_bg_has_super(fs, bg))
  37. has_super = 1;
  38. ret_blk = (fs->super->s_first_data_block + has_super +
  39. (bg * fs->super->s_blocks_per_group));
  40. /*
  41. * If group_block is not the normal value, we're trying to use
  42. * the backup group descriptors and superblock --- so use the
  43. * alternate location of the second block group in the
  44. * metablock group. Ideally we should be testing each bg
  45. * descriptor block individually for correctness, but we don't
  46. * have the infrastructure in place to do that.
  47. */
  48. if (group_block != fs->super->s_first_data_block &&
  49. ((ret_blk + fs->super->s_blocks_per_group) <
  50. fs->super->s_blocks_count))
  51. ret_blk += fs->super->s_blocks_per_group;
  52. return ret_blk;
  53. }
  54. errcode_t ext2fs_open(const char *name, int flags, int superblock,
  55. unsigned int block_size, io_manager manager,
  56. ext2_filsys *ret_fs)
  57. {
  58. return ext2fs_open2(name, 0, flags, superblock, block_size,
  59. manager, ret_fs);
  60. }
  61. /*
  62. * Note: if superblock is non-zero, block-size must also be non-zero.
  63. * Superblock and block_size can be zero to use the default size.
  64. *
  65. * Valid flags for ext2fs_open()
  66. *
  67. * EXT2_FLAG_RW - Open the filesystem for read/write.
  68. * EXT2_FLAG_FORCE - Open the filesystem even if some of the
  69. * features aren't supported.
  70. * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device
  71. */
  72. errcode_t ext2fs_open2(const char *name, const char *io_options,
  73. int flags, int superblock,
  74. unsigned int block_size, io_manager manager,
  75. ext2_filsys *ret_fs)
  76. {
  77. ext2_filsys fs;
  78. errcode_t retval;
  79. unsigned long i;
  80. int groups_per_block, blocks_per_group;
  81. blk_t group_block, blk;
  82. char *dest, *cp;
  83. #ifdef EXT2FS_ENABLE_SWAPFS
  84. int j;
  85. struct ext2_group_desc *gdp;
  86. #endif
  87. EXT2_CHECK_MAGIC(manager, EXT2_ET_MAGIC_IO_MANAGER);
  88. retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
  89. if (retval)
  90. return retval;
  91. memset(fs, 0, sizeof(struct struct_ext2_filsys));
  92. fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
  93. fs->flags = flags;
  94. fs->umask = 022;
  95. retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
  96. if (retval)
  97. goto cleanup;
  98. strcpy(fs->device_name, name);
  99. cp = strchr(fs->device_name, '?');
  100. if (!io_options && cp) {
  101. *cp++ = 0;
  102. io_options = cp;
  103. }
  104. retval = manager->open(fs->device_name,
  105. (flags & EXT2_FLAG_RW) ? IO_FLAG_RW : 0,
  106. &fs->io);
  107. if (retval)
  108. goto cleanup;
  109. if (io_options &&
  110. (retval = io_channel_set_options(fs->io, io_options)))
  111. goto cleanup;
  112. fs->image_io = fs->io;
  113. fs->io->app_data = fs;
  114. retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->super);
  115. if (retval)
  116. goto cleanup;
  117. if (flags & EXT2_FLAG_IMAGE_FILE) {
  118. retval = ext2fs_get_mem(sizeof(struct ext2_image_hdr),
  119. &fs->image_header);
  120. if (retval)
  121. goto cleanup;
  122. retval = io_channel_read_blk(fs->io, 0,
  123. -(int)sizeof(struct ext2_image_hdr),
  124. fs->image_header);
  125. if (retval)
  126. goto cleanup;
  127. if (fs->image_header->magic_number != EXT2_ET_MAGIC_E2IMAGE)
  128. return EXT2_ET_MAGIC_E2IMAGE;
  129. superblock = 1;
  130. block_size = fs->image_header->fs_blocksize;
  131. }
  132. /*
  133. * If the user specifies a specific block # for the
  134. * superblock, then he/she must also specify the block size!
  135. * Otherwise, read the master superblock located at offset
  136. * SUPERBLOCK_OFFSET from the start of the partition.
  137. *
  138. * Note: we only save a backup copy of the superblock if we
  139. * are reading the superblock from the primary superblock location.
  140. */
  141. if (superblock) {
  142. if (!block_size) {
  143. retval = EXT2_ET_INVALID_ARGUMENT;
  144. goto cleanup;
  145. }
  146. io_channel_set_blksize(fs->io, block_size);
  147. group_block = superblock;
  148. fs->orig_super = 0;
  149. } else {
  150. io_channel_set_blksize(fs->io, SUPERBLOCK_OFFSET);
  151. superblock = 1;
  152. group_block = 0;
  153. retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &fs->orig_super);
  154. if (retval)
  155. goto cleanup;
  156. }
  157. retval = io_channel_read_blk(fs->io, superblock, -SUPERBLOCK_SIZE,
  158. fs->super);
  159. if (retval)
  160. goto cleanup;
  161. if (fs->orig_super)
  162. memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
  163. #ifdef EXT2FS_ENABLE_SWAPFS
  164. if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
  165. (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
  166. fs->flags |= EXT2_FLAG_SWAP_BYTES;
  167. ext2fs_swap_super(fs->super);
  168. }
  169. #endif
  170. if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
  171. retval = EXT2_ET_BAD_MAGIC;
  172. goto cleanup;
  173. }
  174. if (fs->super->s_rev_level > EXT2_LIB_CURRENT_REV) {
  175. retval = EXT2_ET_REV_TOO_HIGH;
  176. goto cleanup;
  177. }
  178. /*
  179. * Check for feature set incompatibility
  180. */
  181. if (!(flags & EXT2_FLAG_FORCE)) {
  182. if (fs->super->s_feature_incompat &
  183. ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
  184. retval = EXT2_ET_UNSUPP_FEATURE;
  185. goto cleanup;
  186. }
  187. if ((flags & EXT2_FLAG_RW) &&
  188. (fs->super->s_feature_ro_compat &
  189. ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP)) {
  190. retval = EXT2_ET_RO_UNSUPP_FEATURE;
  191. goto cleanup;
  192. }
  193. if (!(flags & EXT2_FLAG_JOURNAL_DEV_OK) &&
  194. (fs->super->s_feature_incompat &
  195. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  196. retval = EXT2_ET_UNSUPP_FEATURE;
  197. goto cleanup;
  198. }
  199. }
  200. fs->blocksize = EXT2_BLOCK_SIZE(fs->super);
  201. if (fs->blocksize == 0) {
  202. retval = EXT2_ET_CORRUPT_SUPERBLOCK;
  203. goto cleanup;
  204. }
  205. fs->fragsize = EXT2_FRAG_SIZE(fs->super);
  206. fs->inode_blocks_per_group = ((fs->super->s_inodes_per_group *
  207. EXT2_INODE_SIZE(fs->super) +
  208. EXT2_BLOCK_SIZE(fs->super) - 1) /
  209. EXT2_BLOCK_SIZE(fs->super));
  210. if (block_size) {
  211. if (block_size != fs->blocksize) {
  212. retval = EXT2_ET_UNEXPECTED_BLOCK_SIZE;
  213. goto cleanup;
  214. }
  215. }
  216. /*
  217. * Set the blocksize to the filesystem's blocksize.
  218. */
  219. io_channel_set_blksize(fs->io, fs->blocksize);
  220. /*
  221. * If this is an external journal device, don't try to read
  222. * the group descriptors, because they're not there.
  223. */
  224. if (fs->super->s_feature_incompat &
  225. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
  226. fs->group_desc_count = 0;
  227. *ret_fs = fs;
  228. return 0;
  229. }
  230. /*
  231. * Read group descriptors
  232. */
  233. blocks_per_group = EXT2_BLOCKS_PER_GROUP(fs->super);
  234. if (blocks_per_group == 0 ||
  235. blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(fs->super) ||
  236. fs->inode_blocks_per_group > EXT2_MAX_INODES_PER_GROUP(fs->super)) {
  237. retval = EXT2_ET_CORRUPT_SUPERBLOCK;
  238. goto cleanup;
  239. }
  240. fs->group_desc_count = (fs->super->s_blocks_count -
  241. fs->super->s_first_data_block +
  242. blocks_per_group - 1) / blocks_per_group;
  243. fs->desc_blocks = (fs->group_desc_count +
  244. EXT2_DESC_PER_BLOCK(fs->super) - 1)
  245. / EXT2_DESC_PER_BLOCK(fs->super);
  246. retval = ext2fs_get_mem(fs->desc_blocks * fs->blocksize,
  247. &fs->group_desc);
  248. if (retval)
  249. goto cleanup;
  250. if (!group_block)
  251. group_block = fs->super->s_first_data_block;
  252. dest = (char *) fs->group_desc;
  253. groups_per_block = fs->blocksize / sizeof(struct ext2_group_desc);
  254. for (i=0 ; i < fs->desc_blocks; i++) {
  255. blk = ext2fs_descriptor_block_loc(fs, group_block, i);
  256. retval = io_channel_read_blk(fs->io, blk, 1, dest);
  257. if (retval)
  258. goto cleanup;
  259. #ifdef EXT2FS_ENABLE_SWAPFS
  260. if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
  261. gdp = (struct ext2_group_desc *) dest;
  262. for (j=0; j < groups_per_block; j++)
  263. ext2fs_swap_group_desc(gdp++);
  264. }
  265. #endif
  266. dest += fs->blocksize;
  267. }
  268. *ret_fs = fs;
  269. return 0;
  270. cleanup:
  271. ext2fs_free(fs);
  272. return retval;
  273. }
  274. /*
  275. * Set/get the filesystem data I/O channel.
  276. *
  277. * These functions are only valid if EXT2_FLAG_IMAGE_FILE is true.
  278. */
  279. errcode_t ext2fs_get_data_io(ext2_filsys fs, io_channel *old_io)
  280. {
  281. if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
  282. return EXT2_ET_NOT_IMAGE_FILE;
  283. if (old_io) {
  284. *old_io = (fs->image_io == fs->io) ? 0 : fs->io;
  285. }
  286. return 0;
  287. }
  288. errcode_t ext2fs_set_data_io(ext2_filsys fs, io_channel new_io)
  289. {
  290. if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
  291. return EXT2_ET_NOT_IMAGE_FILE;
  292. fs->io = new_io ? new_io : fs->image_io;
  293. return 0;
  294. }
  295. errcode_t ext2fs_rewrite_to_io(ext2_filsys fs, io_channel new_io)
  296. {
  297. if ((fs->flags & EXT2_FLAG_IMAGE_FILE) == 0)
  298. return EXT2_ET_NOT_IMAGE_FILE;
  299. fs->io = fs->image_io = new_io;
  300. fs->flags |= EXT2_FLAG_DIRTY | EXT2_FLAG_RW |
  301. EXT2_FLAG_BB_DIRTY | EXT2_FLAG_IB_DIRTY;
  302. fs->flags &= ~EXT2_FLAG_IMAGE_FILE;
  303. return 0;
  304. }