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>
40 #include "kernel-jbd.h"
43 * This function automatically sets up the journal superblock and
44 * returns it as an allocated block.
46 errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
47 __u32 num_blocks, int flags,
51 journal_superblock_t *jsb;
53 if (num_blocks < JFS_MIN_JOURNAL_BLOCKS)
54 return EXT2_ET_JOURNAL_TOO_SMALL;
56 if ((retval = ext2fs_get_mem(fs->blocksize, &jsb)))
59 memset (jsb, 0, fs->blocksize);
61 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
62 if (flags & EXT2_MKJOURNAL_V1_SUPER)
63 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V1);
65 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
66 jsb->s_blocksize = htonl(fs->blocksize);
67 jsb->s_maxlen = htonl(num_blocks);
68 jsb->s_nr_users = htonl(1);
69 jsb->s_first = htonl(1);
70 jsb->s_sequence = htonl(1);
71 memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
73 * If we're creating an external journal device, we need to
74 * adjust these fields.
76 if (fs->super->s_feature_incompat &
77 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
79 jsb->s_first = htonl(ext2fs_journal_sb_start(fs->blocksize) + 1);
82 *ret_jsb = (char *) jsb;
87 * This function writes a journal using POSIX routines. It is used
88 * for creating external journals and creating journals on live
91 static errcode_t write_journal_file(ext2_filsys fs, char *filename,
92 blk_t num_blocks, int flags)
99 if ((retval = ext2fs_create_journal_superblock(fs, num_blocks, flags,
103 /* Open the device or journal file */
104 if ((fd = open(filename, O_WRONLY)) < 0) {
109 /* Write the superblock out */
110 retval = EXT2_ET_SHORT_WRITE;
111 ret_size = write(fd, buf, fs->blocksize);
116 if (ret_size != (int) fs->blocksize)
118 memset(buf, 0, fs->blocksize);
120 if (flags & EXT2_MKJOURNAL_LAZYINIT)
123 for (i = 1; i < num_blocks; i++) {
124 ret_size = write(fd, buf, fs->blocksize);
129 if (ret_size != (int) fs->blocksize)
138 ext2fs_free_mem(&buf);
143 * Convenience function which zeros out _num_ blocks starting at
144 * _blk_. In case of an error, the details of the error is returned
145 * via _ret_blk_ and _ret_count_ if they are non-NULL pointers.
146 * Returns 0 on success, and an error code on an error.
148 * As a special case, if the first argument is NULL, then it will
149 * attempt to free the static zeroizing buffer. (This is to keep
150 * programs that check for memory leaks happy.)
152 #define MAX_STRIDE_LENGTH (4194304 / (int) fs->blocksize)
153 errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
154 blk64_t *ret_blk, int *ret_count)
158 static int stride_length;
161 /* If fs is null, clean up the static buffer and return */
170 /* Deal with zeroing less than 1 block */
174 /* Try a zero out command, if supported */
175 retval = io_channel_zeroout(fs->io, blk, num);
179 /* Allocate the zeroizing buffer if necessary */
180 if (num > stride_length && stride_length < MAX_STRIDE_LENGTH) {
182 int new_stride = num;
184 if (new_stride > MAX_STRIDE_LENGTH)
185 new_stride = MAX_STRIDE_LENGTH;
186 p = realloc(buf, fs->blocksize * new_stride);
188 return EXT2_ET_NO_MEMORY;
190 stride_length = new_stride;
191 memset(buf, 0, fs->blocksize * stride_length);
193 /* OK, do the write loop */
196 if (blk % stride_length) {
197 count = stride_length - (blk % stride_length);
198 if (count > (num - j))
202 if (count > stride_length)
203 count = stride_length;
205 retval = io_channel_write_blk64(fs->io, blk, count, buf);
213 j += count; blk += count;
218 errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
219 blk_t *ret_blk, int *ret_count)
224 retval = ext2fs_zero_blocks2(fs, blk, num, &ret_blk2, ret_count);
226 *ret_blk = (blk_t) ret_blk2;
231 * Calculate the initial goal block to be roughly at the middle of the
232 * filesystem. Pick a group that has the largest number of free
235 static blk64_t get_midpoint_journal_block(ext2_filsys fs)
237 dgrp_t group, start, end, i, log_flex;
239 group = ext2fs_group_of_blk2(fs, (ext2fs_blocks_count(fs->super) -
240 fs->super->s_first_data_block) / 2);
241 log_flex = 1 << fs->super->s_log_groups_per_flex;
242 if (fs->super->s_log_groups_per_flex && (group > log_flex)) {
243 group = group & ~(log_flex - 1);
244 while ((group < fs->group_desc_count) &&
245 ext2fs_bg_free_blocks_count(fs, group) == 0)
247 if (group == fs->group_desc_count)
251 start = (group > 0) ? group-1 : group;
252 end = ((group+1) < fs->group_desc_count) ? group+1 : group;
254 for (i = start + 1; i <= end; i++)
255 if (ext2fs_bg_free_blocks_count(fs, i) >
256 ext2fs_bg_free_blocks_count(fs, group))
258 return ext2fs_group_first_block2(fs, group);
262 * This function creates a journal using direct I/O routines.
264 static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
265 blk_t num_blocks, blk64_t goal, int flags)
269 struct ext2_inode inode;
270 unsigned long long inode_size;
271 int falloc_flags = EXT2_FALLOCATE_FORCE_INIT;
274 if ((retval = ext2fs_create_journal_superblock(fs, num_blocks, flags,
278 if ((retval = ext2fs_read_bitmaps(fs)))
281 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
284 if (inode.i_blocks > 0) {
290 goal = get_midpoint_journal_block(fs);
292 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS)
293 inode.i_flags |= EXT4_EXTENTS_FL;
295 if (!(flags & EXT2_MKJOURNAL_LAZYINIT))
296 falloc_flags |= EXT2_FALLOCATE_ZERO_BLOCKS;
298 inode_size = (unsigned long long)fs->blocksize * num_blocks;
299 inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0);
300 inode.i_links_count = 1;
301 inode.i_mode = LINUX_S_IFREG | 0600;
302 retval = ext2fs_inode_size_set(fs, &inode, inode_size);
306 retval = ext2fs_fallocate(fs, falloc_flags, journal_ino,
307 &inode, goal, 0, num_blocks);
311 if ((retval = ext2fs_write_new_inode(fs, journal_ino, &inode)))
314 retval = ext2fs_bmap2(fs, journal_ino, &inode, NULL, 0, 0, NULL, &zblk);
318 retval = io_channel_write_blk64(fs->io, zblk, 1, buf);
322 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
323 fs->super->s_jnl_blocks[15] = inode.i_size_high;
324 fs->super->s_jnl_blocks[16] = inode.i_size;
325 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
326 ext2fs_mark_super_dirty(fs);
329 ext2fs_free_mem(&buf);
334 * Find a reasonable journal file size (in blocks) given the number of blocks
335 * in the filesystem. For very small filesystems, it is not reasonable to
336 * have a journal that fills more than half of the filesystem.
338 int ext2fs_default_journal_size(__u64 num_blocks)
340 if (num_blocks < 2048)
342 if (num_blocks < 32768)
344 if (num_blocks < 256*1024)
346 if (num_blocks < 512*1024)
348 if (num_blocks < 1024*1024)
353 int ext2fs_journal_sb_start(int blocksize)
355 if (blocksize == EXT2_MIN_BLOCK_SIZE)
361 * This function adds a journal device to a filesystem
363 errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
367 char buf[SUPERBLOCK_SIZE];
368 journal_superblock_t *jsb;
372 /* Make sure the device exists and is a block device */
373 if (stat(journal_dev->device_name, &st) < 0)
376 if (!S_ISBLK(st.st_mode))
377 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
379 /* Get the journal superblock */
380 start = ext2fs_journal_sb_start(journal_dev->blocksize);
381 if ((retval = io_channel_read_blk64(journal_dev->io, start,
386 jsb = (journal_superblock_t *) buf;
387 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
388 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
389 return EXT2_ET_NO_JOURNAL_SB;
391 if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
392 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
394 /* Check and see if this filesystem has already been added */
395 nr_users = ntohl(jsb->s_nr_users);
396 for (i=0; i < nr_users; i++) {
397 if (memcmp(fs->super->s_uuid,
398 &jsb->s_users[i*16], 16) == 0)
402 memcpy(&jsb->s_users[nr_users*16],
403 fs->super->s_uuid, 16);
404 jsb->s_nr_users = htonl(nr_users+1);
407 /* Writeback the journal superblock */
408 if ((retval = io_channel_write_blk64(journal_dev->io, start,
409 -SUPERBLOCK_SIZE, buf)))
412 fs->super->s_journal_inum = 0;
413 fs->super->s_journal_dev = st.st_rdev;
414 memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
415 sizeof(fs->super->s_journal_uuid));
416 memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
417 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
418 ext2fs_mark_super_dirty(fs);
423 * This function adds a journal inode to a filesystem, using either
424 * POSIX routines if the filesystem is mounted, or using direct I/O
425 * functions if it is not.
427 errcode_t ext2fs_add_journal_inode2(ext2_filsys fs, blk_t num_blocks,
428 blk64_t goal, int flags)
431 ext2_ino_t journal_ino;
437 if (flags & EXT2_MKJOURNAL_NO_MNT_CHECK)
439 else if ((retval = ext2fs_check_mount_point(fs->device_name,
441 jfile, sizeof(jfile)-10)))
444 if (mount_flags & EXT2_MF_MOUNTED) {
448 strcat(jfile, "/.journal");
451 * If .../.journal already exists, make sure any
452 * immutable or append-only flags are cleared.
454 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
455 (void) chflags (jfile, 0);
458 fd = open(jfile, O_RDONLY);
460 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
468 /* Create the journal file */
469 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
472 /* Note that we can't do lazy journal initialization for mounted
473 * filesystems, since the zero writing is also allocating the
474 * journal blocks. We could use fallocate, but not all kernels
475 * support that, and creating a journal on a mounted ext2
476 * filesystems is extremely rare these days... Ignore it. */
477 flags &= ~EXT2_MKJOURNAL_LAZYINIT;
479 if ((retval = write_journal_file(fs, jfile, num_blocks, flags)))
482 /* Get inode number of the journal file */
483 if (fstat(fd, &st) < 0) {
488 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
489 retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
492 if (ioctl(fd, EXT2_IOC_GETFLAGS, &f) < 0) {
496 f |= EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
497 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
510 journal_ino = st.st_ino;
511 memset(fs->super->s_jnl_blocks, 0,
512 sizeof(fs->super->s_jnl_blocks));
514 if ((mount_flags & EXT2_MF_BUSY) &&
515 !(fs->flags & EXT2_FLAG_EXCLUSIVE)) {
519 journal_ino = EXT2_JOURNAL_INO;
520 if ((retval = write_journal_inode(fs, journal_ino,
521 num_blocks, goal, flags)))
525 fs->super->s_journal_inum = journal_ino;
526 fs->super->s_journal_dev = 0;
527 memset(fs->super->s_journal_uuid, 0,
528 sizeof(fs->super->s_journal_uuid));
529 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
531 ext2fs_mark_super_dirty(fs);
539 errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t num_blocks, int flags)
541 return ext2fs_add_journal_inode2(fs, num_blocks, ~0ULL, flags);
546 main(int argc, char **argv)
553 fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
556 device_name = argv[1];
558 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
559 unix_io_manager, &fs);
561 com_err(argv[0], retval, "while opening %s", device_name);
565 retval = ext2fs_add_journal_inode(fs, JFS_MIN_JOURNAL_BLOCKS, 0);
567 com_err(argv[0], retval, "while adding journal to %s",
571 retval = ext2fs_flush(fs);
573 printf("Warning, had trouble writing out superblocks.\n");
575 ext2fs_close_free(&fs);