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, (void **) &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)
99 if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
102 /* Open the device or journal file */
103 if ((fd = open(filename, O_WRONLY)) < 0) {
108 /* Write the superblock out */
109 retval = EXT2_ET_SHORT_WRITE;
110 ret_size = write(fd, buf, fs->blocksize);
115 if (ret_size != fs->blocksize)
117 memset(buf, 0, fs->blocksize);
119 for (i = 1; i < size; i++) {
120 ret_size = write(fd, buf, fs->blocksize);
125 if (ret_size != fs->blocksize)
132 ext2fs_free_mem((void **) &buf);
137 * Helper function for creating the journal using direct I/O routines
139 struct mkjournal_struct {
146 static int mkjournal_proc(ext2_filsys fs,
148 e2_blkcnt_t blockcnt,
153 struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
155 static blk_t last_blk = 0;
162 retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
171 retval = io_channel_write_blk(fs->io, new_blk, 1, es->buf);
174 memset(es->buf, 0, fs->blocksize);
182 ext2fs_block_alloc_stats(fs, new_blk, +1);
184 if (es->num_blocks == 0)
185 return (BLOCK_CHANGED | BLOCK_ABORT);
187 return BLOCK_CHANGED;
192 * This function creates a journal using direct I/O routines.
194 static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
195 blk_t size, int flags)
199 struct ext2_inode inode;
200 struct mkjournal_struct es;
202 if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
205 if ((retval = ext2fs_read_bitmaps(fs)))
208 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
211 if (inode.i_blocks > 0)
214 es.num_blocks = size;
219 retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND,
220 0, mkjournal_proc, &es);
226 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
229 inode.i_size += fs->blocksize * size;
230 inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
231 inode.i_mtime = inode.i_ctime = time(0);
232 inode.i_links_count = 1;
233 inode.i_mode = LINUX_S_IFREG | 0600;
235 if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
240 ext2fs_free_mem((void **) &buf);
245 * This function adds a journal device to a filesystem
247 errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
252 journal_superblock_t *jsb;
256 /* Make sure the device exists and is a block device */
257 if (stat(journal_dev->device_name, &st) < 0)
260 if (!S_ISBLK(st.st_mode))
261 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
263 /* Get the journal superblock */
265 if (journal_dev->blocksize == 1024)
267 if ((retval = io_channel_read_blk(journal_dev->io, start, -1024, buf)))
270 jsb = (journal_superblock_t *) buf;
271 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
272 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
273 return EXT2_ET_NO_JOURNAL_SB;
275 if (ntohl(jsb->s_blocksize) != fs->blocksize)
276 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
278 /* Check and see if this filesystem has already been added */
279 nr_users = ntohl(jsb->s_nr_users);
280 for (i=0; i < nr_users; i++) {
281 if (memcmp(fs->super->s_uuid,
282 &jsb->s_users[i*16], 16) == 0)
286 memcpy(&jsb->s_users[nr_users*16],
287 fs->super->s_uuid, 16);
288 jsb->s_nr_users = htonl(nr_users+1);
291 /* Writeback the journal superblock */
292 if ((retval = io_channel_write_blk(journal_dev->io, start, -1024, buf)))
295 fs->super->s_journal_inum = 0;
296 fs->super->s_journal_dev = st.st_rdev;
297 memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
298 sizeof(fs->super->s_journal_uuid));
299 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
300 ext2fs_mark_super_dirty(fs);
305 * This function adds a journal inode to a filesystem, using either
306 * POSIX routines if the filesystem is mounted, or using direct I/O
307 * functions if it is not.
309 errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
312 ext2_ino_t journal_ino;
315 int fd, mount_flags, f;
317 if ((retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
318 jfile, sizeof(jfile)-10)))
321 if (mount_flags & EXT2_MF_MOUNTED) {
322 strcat(jfile, "/.journal");
325 * If .../.journal already exists, make sure any
326 * immutable or append-only flags are cleared.
328 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
329 (void) chflags (jfile, 0);
332 fd = open(jfile, O_RDONLY);
335 ioctl(fd, EXT2_IOC_SETFLAGS, &f);
341 /* Create the journal file */
342 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
345 if ((retval = write_journal_file(fs, jfile, size, flags)))
348 /* Get inode number of the journal file */
349 if (fstat(fd, &st) < 0)
352 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
353 retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
356 f = EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
357 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
364 journal_ino = st.st_ino;
366 journal_ino = EXT2_JOURNAL_INO;
367 if ((retval = write_journal_inode(fs, journal_ino,
372 fs->super->s_journal_inum = journal_ino;
373 fs->super->s_journal_dev = 0;
374 memset(fs->super->s_journal_uuid, 0,
375 sizeof(fs->super->s_journal_uuid));
376 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
378 ext2fs_mark_super_dirty(fs);
386 main(int argc, char **argv)
393 fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
396 device_name = argv[1];
398 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
399 unix_io_manager, &fs);
401 com_err(argv[0], retval, "while opening %s", device_name);
405 retval = ext2fs_add_journal_inode(fs, 1024);
407 com_err(argv[0], retval, "while adding journal to %s",
411 retval = ext2fs_flush(fs);
413 printf("Warning, had trouble writing out superblocks.\n");