2 * mkjournal.c --- make a journal for a filesystem
4 * Copyright (C) 2000 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
27 #include <sys/types.h>
30 #include <sys/ioctl.h>
33 #include <netinet/in.h>
42 * This function automatically sets up the journal superblock and
43 * returns it as an allocated block.
45 errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
46 __u32 num_blocks, int flags,
50 journal_superblock_t *jsb;
52 if (num_blocks < 1024)
53 return EXT2_ET_JOURNAL_TOO_SMALL;
55 if ((retval = ext2fs_get_mem(fs->blocksize, &jsb)))
58 memset (jsb, 0, fs->blocksize);
60 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
61 if (flags & EXT2_MKJOURNAL_V1_SUPER)
62 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V1);
64 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
65 jsb->s_blocksize = htonl(fs->blocksize);
66 jsb->s_maxlen = htonl(num_blocks);
67 jsb->s_nr_users = htonl(1);
68 jsb->s_first = htonl(1);
69 jsb->s_sequence = htonl(1);
70 memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
72 * If we're creating an external journal device, we need to
73 * adjust these fields.
75 if (fs->super->s_feature_incompat &
76 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
78 if (fs->blocksize == 1024)
79 jsb->s_first = htonl(3);
81 jsb->s_first = htonl(2);
84 *ret_jsb = (char *) jsb;
89 * This function writes a journal using POSIX routines. It is used
90 * for creating external journals and creating journals on live
93 static errcode_t write_journal_file(ext2_filsys fs, char *filename,
94 blk_t num_blocks, int flags)
101 if ((retval = ext2fs_create_journal_superblock(fs, num_blocks, flags,
105 /* Open the device or journal file */
106 if ((fd = open(filename, O_WRONLY)) < 0) {
111 /* Write the superblock out */
112 retval = EXT2_ET_SHORT_WRITE;
113 ret_size = write(fd, buf, fs->blocksize);
118 if (ret_size != (int) fs->blocksize)
120 memset(buf, 0, fs->blocksize);
122 if (flags & EXT2_MKJOURNAL_LAZYINIT)
125 for (i = 1; i < num_blocks; i++) {
126 ret_size = write(fd, buf, fs->blocksize);
131 if (ret_size != (int) fs->blocksize)
140 ext2fs_free_mem(&buf);
145 * Convenience function which zeros out _num_ blocks starting at
146 * _blk_. In case of an error, the details of the error is returned
147 * via _ret_blk_ and _ret_count_ if they are non-NULL pointers.
148 * Returns 0 on success, and an error code on an error.
150 * As a special case, if the first argument is NULL, then it will
151 * attempt to free the static zeroizing buffer. (This is to keep
152 * programs that check for memory leaks happy.)
154 #define STRIDE_LENGTH 8
155 errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
156 blk64_t *ret_blk, int *ret_count)
162 /* If fs is null, clean up the static buffer and return */
170 /* Allocate the zeroizing buffer if necessary */
172 buf = malloc(fs->blocksize * STRIDE_LENGTH);
175 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
177 /* OK, do the write loop */
180 if (blk % STRIDE_LENGTH) {
181 count = STRIDE_LENGTH - (blk % STRIDE_LENGTH);
182 if (count > (num - j))
186 if (count > STRIDE_LENGTH)
187 count = STRIDE_LENGTH;
189 retval = io_channel_write_blk64(fs->io, blk, count, buf);
197 j += count; blk += count;
202 errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
203 blk_t *ret_blk, int *ret_count)
208 retval = ext2fs_zero_blocks2(fs, blk, num, &ret_blk2, ret_count);
210 *ret_blk = (blk_t) ret_blk2;
215 * Helper function for creating the journal using direct I/O routines
217 struct mkjournal_struct {
228 static int mkjournal_proc(ext2_filsys fs,
230 e2_blkcnt_t blockcnt,
231 blk64_t ref_block EXT2FS_ATTR((unused)),
232 int ref_offset EXT2FS_ATTR((unused)),
235 struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
244 (EXT2FS_B2C(fs, es->goal) == EXT2FS_B2C(fs, es->goal+1)))
245 new_blk = es->goal+1;
247 es->goal &= ~EXT2FS_CLUSTER_MASK(fs);
248 retval = ext2fs_new_block2(fs, es->goal, 0, &new_blk);
253 ext2fs_block_alloc_stats2(fs, new_blk, +1);
261 retval = io_channel_write_blk64(fs->io, new_blk, 1, es->buf);
262 else if (!(es->flags & EXT2_MKJOURNAL_LAZYINIT)) {
263 if (es->zero_count) {
264 if ((es->blk_to_zero + es->zero_count == new_blk) &&
265 (es->zero_count < 1024))
268 retval = ext2fs_zero_blocks2(fs,
275 if (es->zero_count == 0) {
276 es->blk_to_zero = new_blk;
282 memset(es->buf, 0, fs->blocksize);
288 *blocknr = es->goal = new_blk;
290 if (es->num_blocks == 0)
291 return (BLOCK_CHANGED | BLOCK_ABORT);
293 return BLOCK_CHANGED;
298 * Calculate the initial goal block to be roughly at the middle of the
299 * filesystem. Pick a group that has the largest number of free
302 static blk64_t get_midpoint_journal_block(ext2_filsys fs)
304 dgrp_t group, start, end, i, log_flex;
306 group = ext2fs_group_of_blk2(fs, (ext2fs_blocks_count(fs->super) -
307 fs->super->s_first_data_block) / 2);
308 log_flex = 1 << fs->super->s_log_groups_per_flex;
309 if (fs->super->s_log_groups_per_flex && (group > log_flex)) {
310 group = group & ~(log_flex - 1);
311 while ((group < fs->group_desc_count) &&
312 ext2fs_bg_free_blocks_count(fs, group) == 0)
314 if (group == fs->group_desc_count)
318 start = (group > 0) ? group-1 : group;
319 end = ((group+1) < fs->group_desc_count) ? group+1 : group;
321 for (i = start + 1; i <= end; i++)
322 if (ext2fs_bg_free_blocks_count(fs, i) >
323 ext2fs_bg_free_blocks_count(fs, group))
325 return ext2fs_group_first_block2(fs, group);
329 * This function creates a journal using direct I/O routines.
331 static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
332 blk_t num_blocks, blk64_t goal, int flags)
336 struct ext2_inode inode;
337 unsigned long long inode_size;
338 struct mkjournal_struct es;
340 if ((retval = ext2fs_create_journal_superblock(fs, num_blocks, flags,
344 if ((retval = ext2fs_read_bitmaps(fs)))
347 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
350 if (inode.i_blocks > 0) {
355 es.num_blocks = num_blocks;
361 es.goal = (goal != ~0ULL) ? goal : get_midpoint_journal_block(fs);
363 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
364 inode.i_flags |= EXT4_EXTENTS_FL;
365 if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
369 retval = ext2fs_block_iterate3(fs, journal_ino, BLOCK_FLAG_APPEND,
370 0, mkjournal_proc, &es);
376 retval = ext2fs_zero_blocks2(fs, es.blk_to_zero,
377 es.zero_count, 0, 0);
382 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
385 inode_size = (unsigned long long)fs->blocksize * num_blocks;
386 inode.i_size = inode_size & 0xFFFFFFFF;
387 inode.i_size_high = (inode_size >> 32) & 0xFFFFFFFF;
388 if (ext2fs_needs_large_file_feature(inode_size))
389 fs->super->s_feature_ro_compat |=
390 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
391 ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
392 inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0);
393 inode.i_links_count = 1;
394 inode.i_mode = LINUX_S_IFREG | 0600;
396 if ((retval = ext2fs_write_new_inode(fs, journal_ino, &inode)))
400 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
401 fs->super->s_jnl_blocks[15] = inode.i_size_high;
402 fs->super->s_jnl_blocks[16] = inode.i_size;
403 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
404 ext2fs_mark_super_dirty(fs);
407 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
409 ext2fs_free_mem(&buf);
414 * Find a reasonable journal file size (in blocks) given the number of blocks
415 * in the filesystem. For very small filesystems, it is not reasonable to
416 * have a journal that fills more than half of the filesystem.
418 int ext2fs_default_journal_size(__u64 num_blocks)
420 if (num_blocks < 2048)
422 if (num_blocks < 32768)
424 if (num_blocks < 256*1024)
426 if (num_blocks < 512*1024)
428 if (num_blocks < 1024*1024)
434 * This function adds a journal device to a filesystem
436 errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
441 journal_superblock_t *jsb;
445 /* Make sure the device exists and is a block device */
446 if (stat(journal_dev->device_name, &st) < 0)
449 if (!S_ISBLK(st.st_mode))
450 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
452 /* Get the journal superblock */
454 if (journal_dev->blocksize == 1024)
456 if ((retval = io_channel_read_blk64(journal_dev->io, start, -1024,
460 jsb = (journal_superblock_t *) buf;
461 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
462 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
463 return EXT2_ET_NO_JOURNAL_SB;
465 if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
466 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
468 /* Check and see if this filesystem has already been added */
469 nr_users = ntohl(jsb->s_nr_users);
470 for (i=0; i < nr_users; i++) {
471 if (memcmp(fs->super->s_uuid,
472 &jsb->s_users[i*16], 16) == 0)
476 memcpy(&jsb->s_users[nr_users*16],
477 fs->super->s_uuid, 16);
478 jsb->s_nr_users = htonl(nr_users+1);
481 /* Writeback the journal superblock */
482 if ((retval = io_channel_write_blk64(journal_dev->io, start, -1024, buf)))
485 fs->super->s_journal_inum = 0;
486 fs->super->s_journal_dev = st.st_rdev;
487 memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
488 sizeof(fs->super->s_journal_uuid));
489 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
490 ext2fs_mark_super_dirty(fs);
495 * This function adds a journal inode to a filesystem, using either
496 * POSIX routines if the filesystem is mounted, or using direct I/O
497 * functions if it is not.
499 errcode_t ext2fs_add_journal_inode2(ext2_filsys fs, blk_t num_blocks,
500 blk64_t goal, int flags)
503 ext2_ino_t journal_ino;
509 if (flags & EXT2_MKJOURNAL_NO_MNT_CHECK)
511 else if ((retval = ext2fs_check_mount_point(fs->device_name,
513 jfile, sizeof(jfile)-10)))
516 if (mount_flags & EXT2_MF_MOUNTED) {
520 strcat(jfile, "/.journal");
523 * If .../.journal already exists, make sure any
524 * immutable or append-only flags are cleared.
526 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
527 (void) chflags (jfile, 0);
530 fd = open(jfile, O_RDONLY);
532 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
540 /* Create the journal file */
541 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
544 /* Note that we can't do lazy journal initialization for mounted
545 * filesystems, since the zero writing is also allocating the
546 * journal blocks. We could use fallocate, but not all kernels
547 * support that, and creating a journal on a mounted ext2
548 * filesystems is extremely rare these days... Ignore it. */
549 flags &= ~EXT2_MKJOURNAL_LAZYINIT;
551 if ((retval = write_journal_file(fs, jfile, num_blocks, flags)))
554 /* Get inode number of the journal file */
555 if (fstat(fd, &st) < 0) {
560 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
561 retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
564 if (ioctl(fd, EXT2_IOC_GETFLAGS, &f) < 0) {
568 f |= EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
569 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
582 journal_ino = st.st_ino;
584 if ((mount_flags & EXT2_MF_BUSY) &&
585 !(fs->flags & EXT2_FLAG_EXCLUSIVE)) {
589 journal_ino = EXT2_JOURNAL_INO;
590 if ((retval = write_journal_inode(fs, journal_ino,
591 num_blocks, goal, flags)))
595 fs->super->s_journal_inum = journal_ino;
596 fs->super->s_journal_dev = 0;
597 memset(fs->super->s_journal_uuid, 0,
598 sizeof(fs->super->s_journal_uuid));
599 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
601 ext2fs_mark_super_dirty(fs);
609 errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t num_blocks, int flags)
611 return ext2fs_add_journal_inode2(fs, num_blocks, ~0ULL, flags);
616 main(int argc, char **argv)
623 fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
626 device_name = argv[1];
628 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
629 unix_io_manager, &fs);
631 com_err(argv[0], retval, "while opening %s", device_name);
635 retval = ext2fs_add_journal_inode(fs, 1024, 0);
637 com_err(argv[0], retval, "while adding journal to %s",
641 retval = ext2fs_flush(fs);
643 printf("Warning, had trouble writing out superblocks.\n");