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.
28 #include <sys/types.h>
31 #include <sys/ioctl.h>
34 #include <netinet/in.h>
41 #include "kernel-jbd.h"
44 * This function automatically sets up the journal superblock and
45 * returns it as an allocated block.
47 errcode_t ext2fs_create_journal_superblock2(ext2_filsys fs,
48 struct ext2fs_journal_params *jparams,
49 int flags, char **ret_jsb)
52 journal_superblock_t *jsb;
54 if (jparams->num_journal_blocks < JBD2_MIN_JOURNAL_BLOCKS)
55 return EXT2_ET_JOURNAL_TOO_SMALL;
57 if ((retval = ext2fs_get_mem(fs->blocksize, &jsb)))
60 memset (jsb, 0, fs->blocksize);
62 jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
63 if (flags & EXT2_MKJOURNAL_V1_SUPER)
64 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V1);
66 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
67 jsb->s_blocksize = htonl(fs->blocksize);
68 jsb->s_maxlen = htonl(jparams->num_journal_blocks + jparams->num_fc_blocks);
69 jsb->s_nr_users = htonl(1);
70 jsb->s_first = htonl(1);
71 jsb->s_sequence = htonl(1);
72 jsb->s_num_fc_blks = htonl(jparams->num_fc_blocks);
73 memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
75 * If we're creating an external journal device, we need to
76 * adjust these fields.
78 if (ext2fs_has_feature_journal_dev(fs->super)) {
80 jsb->s_first = htonl(ext2fs_journal_sb_start(fs->blocksize) + 1);
83 *ret_jsb = (char *) jsb;
87 errcode_t ext2fs_create_journal_superblock(ext2_filsys fs, __u32 num_blocks,
88 int flags, char **ret_sb)
90 struct ext2fs_journal_params jparams;
92 jparams.num_journal_blocks = num_blocks;
93 jparams.num_fc_blocks = 0;
95 return ext2fs_create_journal_superblock2(fs, &jparams, flags, ret_sb);
99 * This function writes a journal using POSIX routines. It is used
100 * for creating external journals and creating journals on live
103 static errcode_t write_journal_file(ext2_filsys fs, char *filename,
104 struct ext2fs_journal_params *jparams,
112 if ((retval = ext2fs_create_journal_superblock2(fs, jparams, flags,
116 /* Open the device or journal file */
117 if ((fd = open(filename, O_WRONLY)) < 0) {
122 /* Write the superblock out */
123 retval = EXT2_ET_SHORT_WRITE;
124 ret_size = write(fd, buf, fs->blocksize);
129 if (ret_size != (int) fs->blocksize)
131 memset(buf, 0, fs->blocksize);
133 if (flags & EXT2_MKJOURNAL_LAZYINIT)
136 for (i = 1; i < jparams->num_journal_blocks + jparams->num_fc_blocks; i++) {
137 ret_size = write(fd, buf, fs->blocksize);
142 if (ret_size != (int) fs->blocksize)
151 ext2fs_free_mem(&buf);
156 * Convenience function which zeros out _num_ blocks starting at
157 * _blk_. In case of an error, the details of the error is returned
158 * via _ret_blk_ and _ret_count_ if they are non-NULL pointers.
159 * Returns 0 on success, and an error code on an error.
161 * As a special case, if the first argument is NULL, then it will
162 * attempt to free the static zeroizing buffer. (This is to keep
163 * programs that check for memory leaks happy.)
165 #define MAX_STRIDE_LENGTH (4194304 / (int) fs->blocksize)
166 errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
167 blk64_t *ret_blk, int *ret_count)
171 static int stride_length;
174 /* If fs is null, clean up the static buffer and return */
184 /* Deal with zeroing less than 1 block */
188 /* Try a zero out command, if supported */
189 retval = io_channel_zeroout(fs->io, blk, num);
193 /* Allocate the zeroizing buffer if necessary */
194 if (num > stride_length && stride_length < MAX_STRIDE_LENGTH) {
196 int new_stride = num;
198 if (new_stride > MAX_STRIDE_LENGTH)
199 new_stride = MAX_STRIDE_LENGTH;
200 p = realloc(buf, fs->blocksize * new_stride);
202 return EXT2_ET_NO_MEMORY;
204 stride_length = new_stride;
205 memset(buf, 0, fs->blocksize * stride_length);
207 /* OK, do the write loop */
210 if (blk % stride_length) {
211 count = stride_length - (blk % stride_length);
212 if (count > (num - j))
216 if (count > stride_length)
217 count = stride_length;
219 retval = io_channel_write_blk64(fs->io, blk, count, buf);
227 j += count; blk += count;
232 errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
233 blk_t *ret_blk, int *ret_count)
238 retval = ext2fs_zero_blocks2(fs, blk, num, &ret_blk2, ret_count);
240 *ret_blk = (blk_t) ret_blk2;
245 * Calculate the initial goal block to be roughly at the middle of the
246 * filesystem. Pick a group that has the largest number of free
249 static blk64_t get_midpoint_journal_block(ext2_filsys fs)
251 dgrp_t group, start, end, i, log_flex;
253 group = ext2fs_group_of_blk2(fs, (ext2fs_blocks_count(fs->super) -
254 fs->super->s_first_data_block) / 2);
255 log_flex = 1U << fs->super->s_log_groups_per_flex;
256 if (fs->super->s_log_groups_per_flex && (group > log_flex)) {
257 group = group & ~(log_flex - 1);
258 while ((group < fs->group_desc_count) &&
259 ext2fs_bg_free_blocks_count(fs, group) == 0)
261 if (group == fs->group_desc_count)
265 start = (group > 0) ? group-1 : group;
266 end = ((group+1) < fs->group_desc_count) ? group+1 : group;
268 for (i = start + 1; i <= end; i++)
269 if (ext2fs_bg_free_blocks_count(fs, i) >
270 ext2fs_bg_free_blocks_count(fs, group))
272 return ext2fs_group_first_block2(fs, group);
276 * This function creates a journal using direct I/O routines.
278 static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
279 struct ext2fs_journal_params *jparams,
280 blk64_t goal, int flags)
284 struct ext2_inode inode;
285 unsigned long long inode_size;
286 int falloc_flags = EXT2_FALLOCATE_FORCE_INIT;
289 if ((retval = ext2fs_create_journal_superblock2(fs, jparams, flags,
293 if ((retval = ext2fs_read_bitmaps(fs)))
296 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
299 if (inode.i_blocks > 0) {
305 goal = get_midpoint_journal_block(fs);
307 if (ext2fs_has_feature_extents(fs->super))
308 inode.i_flags |= EXT4_EXTENTS_FL;
310 if (!(flags & EXT2_MKJOURNAL_LAZYINIT))
311 falloc_flags |= EXT2_FALLOCATE_ZERO_BLOCKS;
313 inode_size = (unsigned long long)fs->blocksize *
314 (jparams->num_journal_blocks + jparams->num_fc_blocks);
315 inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0);
316 inode.i_links_count = 1;
317 inode.i_mode = LINUX_S_IFREG | 0600;
318 retval = ext2fs_inode_size_set(fs, &inode, inode_size);
322 retval = ext2fs_fallocate(fs, falloc_flags, journal_ino,
324 jparams->num_journal_blocks + jparams->num_fc_blocks);
328 if ((retval = ext2fs_write_new_inode(fs, journal_ino, &inode)))
331 retval = ext2fs_bmap2(fs, journal_ino, &inode, NULL, 0, 0, NULL, &zblk);
335 retval = io_channel_write_blk64(fs->io, zblk, 1, buf);
339 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
340 fs->super->s_jnl_blocks[15] = inode.i_size_high;
341 fs->super->s_jnl_blocks[16] = inode.i_size;
342 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
343 ext2fs_mark_super_dirty(fs);
346 ext2fs_free_mem(&buf);
351 * Find a reasonable journal file size (in blocks) given the number of blocks
352 * in the filesystem. For very small filesystems, it is not reasonable to
353 * have a journal that fills more than half of the filesystem.
355 * n.b. comments assume 4k blocks
357 int ext2fs_default_journal_size(__u64 num_blocks)
359 if (num_blocks < 2048)
361 if (num_blocks < 32768) /* 128 MB */
362 return (1024); /* 4 MB */
363 if (num_blocks < 256*1024) /* 1 GB */
364 return (4096); /* 16 MB */
365 if (num_blocks < 512*1024) /* 2 GB */
366 return (8192); /* 32 MB */
367 if (num_blocks < 4096*1024) /* 16 GB */
368 return (16384); /* 64 MB */
369 if (num_blocks < 8192*1024) /* 32 GB */
370 return (32768); /* 128 MB */
371 if (num_blocks < 16384*1024) /* 64 GB */
372 return (65536); /* 256 MB */
373 if (num_blocks < 32768*1024) /* 128 GB */
374 return (131072); /* 512 MB */
375 return 262144; /* 1 GB */
378 errcode_t ext2fs_get_journal_params(struct ext2fs_journal_params *params,
384 memset(params, 0, sizeof(*params));
385 if (ext2fs_has_feature_journal_dev(fs->super)) {
386 total_blks = ext2fs_blocks_count(fs->super);
387 if (total_blks < JBD2_MIN_JOURNAL_BLOCKS)
388 return EXT2_ET_JOURNAL_TOO_SMALL;
390 if (!ext2fs_has_feature_fast_commit(fs->super)) {
391 params->num_journal_blocks = total_blks;
392 params->num_fc_blocks = 0;
395 params->num_journal_blocks = ext2fs_blocks_count(fs->super) *
396 EXT2_JOURNAL_TO_FC_BLKS_RATIO /
397 (EXT2_JOURNAL_TO_FC_BLKS_RATIO + 1);
398 if (JBD2_MIN_JOURNAL_BLOCKS > params->num_journal_blocks)
399 params->num_journal_blocks = JBD2_MIN_JOURNAL_BLOCKS;
400 params->num_fc_blocks = total_blks - params->num_journal_blocks;
404 ret = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
406 return EXT2_ET_JOURNAL_TOO_SMALL;
408 params->num_journal_blocks = ret;
409 if (ext2fs_has_feature_fast_commit(fs->super))
410 params->num_fc_blocks = params->num_journal_blocks /
411 EXT2_JOURNAL_TO_FC_BLKS_RATIO;
415 int ext2fs_journal_sb_start(int blocksize)
417 if (blocksize == EXT2_MIN_BLOCK_SIZE)
423 * This function adds a journal device to a filesystem
425 errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
429 char buf[SUPERBLOCK_SIZE];
430 journal_superblock_t *jsb;
434 /* Make sure the device exists and is a block device */
435 if (stat(journal_dev->device_name, &st) < 0)
438 if (!S_ISBLK(st.st_mode))
439 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
441 /* Get the journal superblock */
442 start = ext2fs_journal_sb_start(journal_dev->blocksize);
443 if ((retval = io_channel_read_blk64(journal_dev->io, start,
448 jsb = (journal_superblock_t *) buf;
449 if ((jsb->s_header.h_magic != (unsigned) ntohl(JBD2_MAGIC_NUMBER)) ||
450 (jsb->s_header.h_blocktype != (unsigned) ntohl(JBD2_SUPERBLOCK_V2)))
451 return EXT2_ET_NO_JOURNAL_SB;
453 if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
454 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
456 /* Check and see if this filesystem has already been added */
457 nr_users = ntohl(jsb->s_nr_users);
458 if (nr_users > JBD2_USERS_MAX)
459 return EXT2_ET_CORRUPT_JOURNAL_SB;
460 for (i=0; i < nr_users; i++) {
461 if (memcmp(fs->super->s_uuid,
462 &jsb->s_users[i*16], 16) == 0)
466 memcpy(&jsb->s_users[nr_users*16],
467 fs->super->s_uuid, 16);
468 jsb->s_nr_users = htonl(nr_users+1);
471 /* Writeback the journal superblock */
472 if ((retval = io_channel_write_blk64(journal_dev->io, start,
473 -SUPERBLOCK_SIZE, buf)))
476 fs->super->s_journal_inum = 0;
477 fs->super->s_journal_dev = st.st_rdev;
478 memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
479 sizeof(fs->super->s_journal_uuid));
480 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
481 ext2fs_set_feature_journal(fs->super);
482 ext2fs_mark_super_dirty(fs);
487 * This function adds a journal inode to a filesystem, using either
488 * POSIX routines if the filesystem is mounted, or using direct I/O
489 * functions if it is not.
491 errcode_t ext2fs_add_journal_inode3(ext2_filsys fs, struct ext2fs_journal_params *jparams,
492 blk64_t goal, int flags)
495 ext2_ino_t journal_ino;
501 if (flags & EXT2_MKJOURNAL_NO_MNT_CHECK)
503 else if ((retval = ext2fs_check_mount_point(fs->device_name,
505 jfile, sizeof(jfile)-10)))
508 if (mount_flags & EXT2_MF_MOUNTED) {
512 strcat(jfile, "/.journal");
515 * If .../.journal already exists, make sure any
516 * immutable or append-only flags are cleared.
518 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
519 (void) chflags (jfile, 0);
522 fd = open(jfile, O_RDONLY);
524 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
532 /* Create the journal file */
533 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
536 /* Note that we can't do lazy journal initialization for mounted
537 * filesystems, since the zero writing is also allocating the
538 * journal blocks. We could use fallocate, but not all kernels
539 * support that, and creating a journal on a mounted ext2
540 * filesystems is extremely rare these days... Ignore it. */
541 flags &= ~EXT2_MKJOURNAL_LAZYINIT;
543 if ((retval = write_journal_file(fs, jfile, jparams, flags)))
546 /* Get inode number of the journal file */
547 if (fstat(fd, &st) < 0) {
552 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
553 retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
556 if (ioctl(fd, EXT2_IOC_GETFLAGS, &f) < 0) {
560 f |= EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
561 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
574 journal_ino = st.st_ino;
575 memset(fs->super->s_jnl_blocks, 0,
576 sizeof(fs->super->s_jnl_blocks));
578 if ((mount_flags & EXT2_MF_BUSY) &&
579 !(fs->flags & EXT2_FLAG_EXCLUSIVE)) {
583 journal_ino = EXT2_JOURNAL_INO;
584 if ((retval = write_journal_inode(fs, journal_ino,
585 jparams, goal, flags)))
589 fs->super->s_journal_inum = journal_ino;
590 fs->super->s_journal_dev = 0;
591 memset(fs->super->s_journal_uuid, 0,
592 sizeof(fs->super->s_journal_uuid));
593 ext2fs_set_feature_journal(fs->super);
595 ext2fs_mark_super_dirty(fs);
603 errcode_t ext2fs_add_journal_inode2(ext2_filsys fs, blk_t num_blocks,
604 blk64_t goal, int flags)
606 struct ext2fs_journal_params jparams;
608 jparams.num_journal_blocks = num_blocks;
609 jparams.num_fc_blocks = 0;
611 return ext2fs_add_journal_inode3(fs, &jparams, goal, flags);
614 errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t num_blocks, int flags)
616 return ext2fs_add_journal_inode2(fs, num_blocks, ~0ULL, flags);
621 main(int argc, char **argv)
628 fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
631 device_name = argv[1];
633 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
634 unix_io_manager, &fs);
636 com_err(argv[0], retval, "while opening %s", device_name);
640 retval = ext2fs_add_journal_inode(fs, JBD2_MIN_JOURNAL_BLOCKS, 0);
642 com_err(argv[0], retval, "while adding journal to %s",
646 retval = ext2fs_flush(fs);
648 printf("Warning, had trouble writing out superblocks.\n");
650 ext2fs_close_free(&fs);