mkjournal.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * mkjournal.c --- make a journal for a filesystem
  3. *
  4. * Copyright (C) 2000 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. #if HAVE_ERRNO_H
  17. #include <errno.h>
  18. #endif
  19. #include <fcntl.h>
  20. #include <time.h>
  21. #if HAVE_SYS_STAT_H
  22. #include <sys/stat.h>
  23. #endif
  24. #if HAVE_SYS_TYPES_H
  25. #include <sys/types.h>
  26. #endif
  27. #if HAVE_SYS_IOCTL_H
  28. #include <sys/ioctl.h>
  29. #endif
  30. #if HAVE_NETINET_IN_H
  31. #include <netinet/in.h>
  32. #endif
  33. #include "ext2_fs.h"
  34. #include "e2p/e2p.h"
  35. #include "ext2fs.h"
  36. #include "jfs_user.h"
  37. /*
  38. * This function automatically sets up the journal superblock and
  39. * returns it as an allocated block.
  40. */
  41. errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
  42. __u32 size, int flags,
  43. char **ret_jsb)
  44. {
  45. errcode_t retval;
  46. journal_superblock_t *jsb;
  47. if (size < 1024)
  48. return EXT2_ET_JOURNAL_TOO_SMALL;
  49. if ((retval = ext2fs_get_mem(fs->blocksize, &jsb)))
  50. return retval;
  51. memset (jsb, 0, fs->blocksize);
  52. jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
  53. if (flags & EXT2_MKJOURNAL_V1_SUPER)
  54. jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V1);
  55. else
  56. jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
  57. jsb->s_blocksize = htonl(fs->blocksize);
  58. jsb->s_maxlen = htonl(size);
  59. jsb->s_nr_users = htonl(1);
  60. jsb->s_first = htonl(1);
  61. jsb->s_sequence = htonl(1);
  62. memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
  63. /*
  64. * If we're creating an external journal device, we need to
  65. * adjust these fields.
  66. */
  67. if (fs->super->s_feature_incompat &
  68. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
  69. jsb->s_nr_users = 0;
  70. if (fs->blocksize == 1024)
  71. jsb->s_first = htonl(3);
  72. else
  73. jsb->s_first = htonl(2);
  74. }
  75. *ret_jsb = (char *) jsb;
  76. return 0;
  77. }
  78. /*
  79. * This function writes a journal using POSIX routines. It is used
  80. * for creating external journals and creating journals on live
  81. * filesystems.
  82. */
  83. static errcode_t write_journal_file(ext2_filsys fs, char *filename,
  84. blk_t size, int flags)
  85. {
  86. errcode_t retval;
  87. char *buf = 0;
  88. int fd, ret_size;
  89. blk_t i;
  90. if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
  91. return retval;
  92. /* Open the device or journal file */
  93. if ((fd = open(filename, O_WRONLY)) < 0) {
  94. retval = errno;
  95. goto errout;
  96. }
  97. /* Write the superblock out */
  98. retval = EXT2_ET_SHORT_WRITE;
  99. ret_size = write(fd, buf, fs->blocksize);
  100. if (ret_size < 0) {
  101. retval = errno;
  102. goto errout;
  103. }
  104. if (ret_size != (int) fs->blocksize)
  105. goto errout;
  106. memset(buf, 0, fs->blocksize);
  107. for (i = 1; i < size; i++) {
  108. ret_size = write(fd, buf, fs->blocksize);
  109. if (ret_size < 0) {
  110. retval = errno;
  111. goto errout;
  112. }
  113. if (ret_size != (int) fs->blocksize)
  114. goto errout;
  115. }
  116. close(fd);
  117. retval = 0;
  118. errout:
  119. ext2fs_free_mem(&buf);
  120. return retval;
  121. }
  122. /*
  123. * Helper function for creating the journal using direct I/O routines
  124. */
  125. struct mkjournal_struct {
  126. int num_blocks;
  127. int newblocks;
  128. char *buf;
  129. errcode_t err;
  130. };
  131. static int mkjournal_proc(ext2_filsys fs,
  132. blk_t *blocknr,
  133. e2_blkcnt_t blockcnt,
  134. blk_t ref_block EXT2FS_ATTR((unused)),
  135. int ref_offset EXT2FS_ATTR((unused)),
  136. void *priv_data)
  137. {
  138. struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
  139. blk_t new_blk;
  140. static blk_t last_blk = 0;
  141. errcode_t retval;
  142. if (*blocknr) {
  143. last_blk = *blocknr;
  144. return 0;
  145. }
  146. retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
  147. if (retval) {
  148. es->err = retval;
  149. return BLOCK_ABORT;
  150. }
  151. if (blockcnt > 0)
  152. es->num_blocks--;
  153. es->newblocks++;
  154. retval = io_channel_write_blk(fs->io, new_blk, 1, es->buf);
  155. if (blockcnt == 0)
  156. memset(es->buf, 0, fs->blocksize);
  157. if (retval) {
  158. es->err = retval;
  159. return BLOCK_ABORT;
  160. }
  161. *blocknr = new_blk;
  162. last_blk = new_blk;
  163. ext2fs_block_alloc_stats(fs, new_blk, +1);
  164. if (es->num_blocks == 0)
  165. return (BLOCK_CHANGED | BLOCK_ABORT);
  166. else
  167. return BLOCK_CHANGED;
  168. }
  169. /*
  170. * This function creates a journal using direct I/O routines.
  171. */
  172. static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
  173. blk_t size, int flags)
  174. {
  175. char *buf;
  176. errcode_t retval;
  177. struct ext2_inode inode;
  178. struct mkjournal_struct es;
  179. if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
  180. return retval;
  181. if ((retval = ext2fs_read_bitmaps(fs)))
  182. return retval;
  183. if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
  184. return retval;
  185. if (inode.i_blocks > 0)
  186. return EEXIST;
  187. es.num_blocks = size;
  188. es.newblocks = 0;
  189. es.buf = buf;
  190. es.err = 0;
  191. retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND,
  192. 0, mkjournal_proc, &es);
  193. if (es.err) {
  194. retval = es.err;
  195. goto errout;
  196. }
  197. if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
  198. goto errout;
  199. inode.i_size += fs->blocksize * size;
  200. inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
  201. inode.i_mtime = inode.i_ctime = time(0);
  202. inode.i_links_count = 1;
  203. inode.i_mode = LINUX_S_IFREG | 0600;
  204. if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
  205. goto errout;
  206. retval = 0;
  207. memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
  208. fs->super->s_jnl_blocks[16] = inode.i_size;
  209. fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
  210. ext2fs_mark_super_dirty(fs);
  211. errout:
  212. ext2fs_free_mem(&buf);
  213. return retval;
  214. }
  215. /*
  216. * This function adds a journal device to a filesystem
  217. */
  218. errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
  219. {
  220. struct stat st;
  221. errcode_t retval;
  222. char buf[1024];
  223. journal_superblock_t *jsb;
  224. int start;
  225. __u32 i, nr_users;
  226. /* Make sure the device exists and is a block device */
  227. if (stat(journal_dev->device_name, &st) < 0)
  228. return errno;
  229. if (!S_ISBLK(st.st_mode))
  230. return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
  231. /* Get the journal superblock */
  232. start = 1;
  233. if (journal_dev->blocksize == 1024)
  234. start++;
  235. if ((retval = io_channel_read_blk(journal_dev->io, start, -1024, buf)))
  236. return retval;
  237. jsb = (journal_superblock_t *) buf;
  238. if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
  239. (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
  240. return EXT2_ET_NO_JOURNAL_SB;
  241. if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
  242. return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
  243. /* Check and see if this filesystem has already been added */
  244. nr_users = ntohl(jsb->s_nr_users);
  245. for (i=0; i < nr_users; i++) {
  246. if (memcmp(fs->super->s_uuid,
  247. &jsb->s_users[i*16], 16) == 0)
  248. break;
  249. }
  250. if (i >= nr_users) {
  251. memcpy(&jsb->s_users[nr_users*16],
  252. fs->super->s_uuid, 16);
  253. jsb->s_nr_users = htonl(nr_users+1);
  254. }
  255. /* Writeback the journal superblock */
  256. if ((retval = io_channel_write_blk(journal_dev->io, start, -1024, buf)))
  257. return retval;
  258. fs->super->s_journal_inum = 0;
  259. fs->super->s_journal_dev = st.st_rdev;
  260. memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
  261. sizeof(fs->super->s_journal_uuid));
  262. fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  263. ext2fs_mark_super_dirty(fs);
  264. return 0;
  265. }
  266. /*
  267. * This function adds a journal inode to a filesystem, using either
  268. * POSIX routines if the filesystem is mounted, or using direct I/O
  269. * functions if it is not.
  270. */
  271. errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
  272. {
  273. errcode_t retval;
  274. ext2_ino_t journal_ino;
  275. struct stat st;
  276. char jfile[1024];
  277. int fd, mount_flags, f;
  278. if ((retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
  279. jfile, sizeof(jfile)-10)))
  280. return retval;
  281. if (mount_flags & EXT2_MF_MOUNTED) {
  282. strcat(jfile, "/.journal");
  283. /*
  284. * If .../.journal already exists, make sure any
  285. * immutable or append-only flags are cleared.
  286. */
  287. #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
  288. (void) chflags (jfile, 0);
  289. #else
  290. #if HAVE_EXT2_IOCTLS
  291. fd = open(jfile, O_RDONLY);
  292. if (fd >= 0) {
  293. f = 0;
  294. ioctl(fd, EXT2_IOC_SETFLAGS, &f);
  295. close(fd);
  296. }
  297. #endif
  298. #endif
  299. /* Create the journal file */
  300. if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
  301. return errno;
  302. if ((retval = write_journal_file(fs, jfile, size, flags)))
  303. goto errout;
  304. /* Get inode number of the journal file */
  305. if (fstat(fd, &st) < 0)
  306. goto errout;
  307. #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
  308. retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
  309. #else
  310. #if HAVE_EXT2_IOCTLS
  311. f = EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
  312. retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
  313. #endif
  314. #endif
  315. if (retval)
  316. goto errout;
  317. close(fd);
  318. journal_ino = st.st_ino;
  319. } else {
  320. journal_ino = EXT2_JOURNAL_INO;
  321. if ((retval = write_journal_inode(fs, journal_ino,
  322. size, flags)))
  323. return retval;
  324. }
  325. fs->super->s_journal_inum = journal_ino;
  326. fs->super->s_journal_dev = 0;
  327. memset(fs->super->s_journal_uuid, 0,
  328. sizeof(fs->super->s_journal_uuid));
  329. fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
  330. ext2fs_mark_super_dirty(fs);
  331. return 0;
  332. errout:
  333. close(fd);
  334. return retval;
  335. }
  336. #ifdef DEBUG
  337. main(int argc, char **argv)
  338. {
  339. errcode_t retval;
  340. char *device_name;
  341. ext2_filsys fs;
  342. if (argc < 2) {
  343. fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
  344. exit(1);
  345. }
  346. device_name = argv[1];
  347. retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
  348. unix_io_manager, &fs);
  349. if (retval) {
  350. com_err(argv[0], retval, "while opening %s", device_name);
  351. exit(1);
  352. }
  353. retval = ext2fs_add_journal_inode(fs, 1024);
  354. if (retval) {
  355. com_err(argv[0], retval, "while adding journal to %s",
  356. device_name);
  357. exit(1);
  358. }
  359. retval = ext2fs_flush(fs);
  360. if (retval) {
  361. printf("Warning, had trouble writing out superblocks.\n");
  362. }
  363. ext2fs_close(fs);
  364. exit(0);
  365. }
  366. #endif