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 Public
26 #include <sys/types.h>
29 #include <sys/ioctl.h>
32 #include <netinet/in.h>
41 * This function automatically sets up the journal superblock and
42 * returns it as an allocated block.
44 errcode_t ext2fs_create_journal_superblock(ext2_filsys fs,
45 __u32 size, int flags,
49 journal_superblock_t *jsb;
52 return EXT2_ET_JOURNAL_TOO_SMALL;
54 if ((retval = ext2fs_get_mem(fs->blocksize, &jsb)))
57 memset (jsb, 0, fs->blocksize);
59 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
60 if (flags & EXT2_MKJOURNAL_V1_SUPER)
61 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V1);
63 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
64 jsb->s_blocksize = htonl(fs->blocksize);
65 jsb->s_maxlen = htonl(size);
66 jsb->s_nr_users = htonl(1);
67 jsb->s_first = htonl(1);
68 jsb->s_sequence = htonl(1);
69 memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
71 * If we're creating an external journal device, we need to
72 * adjust these fields.
74 if (fs->super->s_feature_incompat &
75 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
77 if (fs->blocksize == 1024)
78 jsb->s_first = htonl(3);
80 jsb->s_first = htonl(2);
83 *ret_jsb = (char *) jsb;
88 * This function writes a journal using POSIX routines. It is used
89 * for creating external journals and creating journals on live
92 static errcode_t write_journal_file(ext2_filsys fs, char *filename,
93 blk_t size, int flags)
100 if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
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 for (i = 1; i < size; i++) {
121 ret_size = write(fd, buf, fs->blocksize);
126 if (ret_size != (int) fs->blocksize)
133 ext2fs_free_mem(&buf);
138 * Convenience function which zeros out _num_ blocks starting at
139 * _blk_. In case of an error, the details of the error is returned
140 * via _ret_blk_ and _ret_count_ if they are non-NULL pointers.
141 * Returns 0 on success, and an error code on an error.
143 * As a special case, if the first argument is NULL, then it will
144 * attempt to free the static zeroizing buffer. (This is to keep
145 * programs that check for memory leaks happy.)
147 #define STRIDE_LENGTH 8
148 errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
149 blk_t *ret_blk, int *ret_count)
155 /* If fs is null, clean up the static buffer and return */
163 /* Allocate the zeroizing buffer if necessary */
165 buf = malloc(fs->blocksize * STRIDE_LENGTH);
168 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
170 /* OK, do the write loop */
173 if (blk % STRIDE_LENGTH) {
174 count = STRIDE_LENGTH - (blk % STRIDE_LENGTH);
175 if (count > (num - j))
179 if (count > STRIDE_LENGTH)
180 count = STRIDE_LENGTH;
182 retval = io_channel_write_blk(fs->io, blk, count, buf);
190 j += count; blk += count;
196 * Helper function for creating the journal using direct I/O routines
198 struct mkjournal_struct {
207 static int mkjournal_proc(ext2_filsys fs,
209 e2_blkcnt_t blockcnt,
210 blk_t ref_block EXT2FS_ATTR((unused)),
211 int ref_offset EXT2FS_ATTR((unused)),
214 struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
216 static blk_t last_blk = 0;
223 retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
234 retval = io_channel_write_blk(fs->io, new_blk, 1, es->buf);
236 if (es->zero_count) {
237 if ((es->blk_to_zero + es->zero_count == new_blk) &&
238 (es->zero_count < 1024))
241 retval = ext2fs_zero_blocks(fs,
248 if (es->zero_count == 0) {
249 es->blk_to_zero = new_blk;
255 memset(es->buf, 0, fs->blocksize);
263 ext2fs_block_alloc_stats(fs, new_blk, +1);
265 if (es->num_blocks == 0)
266 return (BLOCK_CHANGED | BLOCK_ABORT);
268 return BLOCK_CHANGED;
273 * This function creates a journal using direct I/O routines.
275 static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
276 blk_t size, int flags)
280 struct ext2_inode inode;
281 struct mkjournal_struct es;
283 if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
286 if ((retval = ext2fs_read_bitmaps(fs)))
289 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
292 if (inode.i_blocks > 0)
295 es.num_blocks = size;
301 retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND,
302 0, mkjournal_proc, &es);
308 retval = ext2fs_zero_blocks(fs, es.blk_to_zero,
309 es.zero_count, 0, 0);
314 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
317 inode.i_size += fs->blocksize * size;
318 ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
319 inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0);
320 inode.i_links_count = 1;
321 inode.i_mode = LINUX_S_IFREG | 0600;
323 if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
327 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
328 fs->super->s_jnl_blocks[16] = inode.i_size;
329 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
330 ext2fs_mark_super_dirty(fs);
333 ext2fs_free_mem(&buf);
338 * Find a reasonable journal file size (in blocks) given the number of blocks
339 * in the filesystem. For very small filesystems, it is not reasonable to
340 * have a journal that fills more than half of the filesystem.
342 int ext2fs_default_journal_size(__u64 blocks)
348 if (blocks < 256*1024)
350 if (blocks < 512*1024)
352 if (blocks < 1024*1024)
358 * This function adds a journal device to a filesystem
360 errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
365 journal_superblock_t *jsb;
369 /* Make sure the device exists and is a block device */
370 if (stat(journal_dev->device_name, &st) < 0)
373 if (!S_ISBLK(st.st_mode))
374 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
376 /* Get the journal superblock */
378 if (journal_dev->blocksize == 1024)
380 if ((retval = io_channel_read_blk(journal_dev->io, start, -1024, buf)))
383 jsb = (journal_superblock_t *) buf;
384 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
385 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
386 return EXT2_ET_NO_JOURNAL_SB;
388 if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
389 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
391 /* Check and see if this filesystem has already been added */
392 nr_users = ntohl(jsb->s_nr_users);
393 for (i=0; i < nr_users; i++) {
394 if (memcmp(fs->super->s_uuid,
395 &jsb->s_users[i*16], 16) == 0)
399 memcpy(&jsb->s_users[nr_users*16],
400 fs->super->s_uuid, 16);
401 jsb->s_nr_users = htonl(nr_users+1);
404 /* Writeback the journal superblock */
405 if ((retval = io_channel_write_blk(journal_dev->io, start, -1024, buf)))
408 fs->super->s_journal_inum = 0;
409 fs->super->s_journal_dev = st.st_rdev;
410 memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
411 sizeof(fs->super->s_journal_uuid));
412 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
413 ext2fs_mark_super_dirty(fs);
418 * This function adds a journal inode to a filesystem, using either
419 * POSIX routines if the filesystem is mounted, or using direct I/O
420 * functions if it is not.
422 errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
425 ext2_ino_t journal_ino;
431 if ((retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
432 jfile, sizeof(jfile)-10)))
435 if (mount_flags & EXT2_MF_MOUNTED) {
436 strcat(jfile, "/.journal");
439 * If .../.journal already exists, make sure any
440 * immutable or append-only flags are cleared.
442 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
443 (void) chflags (jfile, 0);
446 fd = open(jfile, O_RDONLY);
449 ioctl(fd, EXT2_IOC_SETFLAGS, &f);
455 /* Create the journal file */
456 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
459 if ((retval = write_journal_file(fs, jfile, size, flags)))
462 /* Get inode number of the journal file */
463 if (fstat(fd, &st) < 0)
466 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
467 retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
470 f = EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
471 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
478 journal_ino = st.st_ino;
480 if ((mount_flags & EXT2_MF_BUSY) &&
481 !(fs->flags & EXT2_FLAG_EXCLUSIVE)) {
485 journal_ino = EXT2_JOURNAL_INO;
486 if ((retval = write_journal_inode(fs, journal_ino,
491 fs->super->s_journal_inum = journal_ino;
492 fs->super->s_journal_dev = 0;
493 memset(fs->super->s_journal_uuid, 0,
494 sizeof(fs->super->s_journal_uuid));
495 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
497 ext2fs_mark_super_dirty(fs);
506 main(int argc, char **argv)
513 fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
516 device_name = argv[1];
518 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
519 unix_io_manager, &fs);
521 com_err(argv[0], retval, "while opening %s", device_name);
525 retval = ext2fs_add_journal_inode(fs, 1024);
527 com_err(argv[0], retval, "while adding journal to %s",
531 retval = ext2fs_flush(fs);
533 printf("Warning, had trouble writing out superblocks.\n");