mkjournal.c revision 961306d31ad36a9eef4e515343c9e355afc6e19c
1/* 2 * mkjournal.c --- make a journal for a filesystem 3 * 4 * Copyright (C) 2000 Theodore Ts'o. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Public 8 * License. 9 * %End-Header% 10 */ 11 12#include <stdio.h> 13#include <string.h> 14#if HAVE_UNISTD_H 15#include <unistd.h> 16#endif 17#if HAVE_ERRNO_H 18#include <errno.h> 19#endif 20#include <fcntl.h> 21#include <time.h> 22#if HAVE_SYS_STAT_H 23#include <sys/stat.h> 24#endif 25#if HAVE_SYS_TYPES_H 26#include <sys/types.h> 27#endif 28#if HAVE_SYS_IOCTL_H 29#include <sys/ioctl.h> 30#endif 31#if HAVE_NETINET_IN_H 32#include <netinet/in.h> 33#endif 34 35#include "ext2_fs.h" 36#include "e2p/e2p.h" 37#include "ext2fs.h" 38#include "jfs_user.h" 39 40/* 41 * This function automatically sets up the journal superblock and 42 * returns it as an allocated block. 43 */ 44errcode_t ext2fs_create_journal_superblock(ext2_filsys fs, 45 __u32 size, int flags, 46 char **ret_jsb) 47{ 48 errcode_t retval; 49 journal_superblock_t *jsb; 50 51 if (size < 1024) 52 return EXT2_ET_JOURNAL_TOO_SMALL; 53 54 if ((retval = ext2fs_get_mem(fs->blocksize, &jsb))) 55 return retval; 56 57 memset (jsb, 0, fs->blocksize); 58 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); 62 else 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)); 70 /* 71 * If we're creating an external journal device, we need to 72 * adjust these fields. 73 */ 74 if (fs->super->s_feature_incompat & 75 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { 76 jsb->s_nr_users = 0; 77 if (fs->blocksize == 1024) 78 jsb->s_first = htonl(3); 79 else 80 jsb->s_first = htonl(2); 81 } 82 83 *ret_jsb = (char *) jsb; 84 return 0; 85} 86 87/* 88 * This function writes a journal using POSIX routines. It is used 89 * for creating external journals and creating journals on live 90 * filesystems. 91 */ 92static errcode_t write_journal_file(ext2_filsys fs, char *filename, 93 blk_t size, int flags) 94{ 95 errcode_t retval; 96 char *buf = 0; 97 int fd, ret_size; 98 blk_t i; 99 100 if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf))) 101 return retval; 102 103 /* Open the device or journal file */ 104 if ((fd = open(filename, O_WRONLY)) < 0) { 105 retval = errno; 106 goto errout; 107 } 108 109 /* Write the superblock out */ 110 retval = EXT2_ET_SHORT_WRITE; 111 ret_size = write(fd, buf, fs->blocksize); 112 if (ret_size < 0) { 113 retval = errno; 114 goto errout; 115 } 116 if (ret_size != (int) fs->blocksize) 117 goto errout; 118 memset(buf, 0, fs->blocksize); 119 120 for (i = 1; i < size; i++) { 121 ret_size = write(fd, buf, fs->blocksize); 122 if (ret_size < 0) { 123 retval = errno; 124 goto errout; 125 } 126 if (ret_size != (int) fs->blocksize) 127 goto errout; 128 } 129 close(fd); 130 131 retval = 0; 132errout: 133 ext2fs_free_mem(&buf); 134 return retval; 135} 136 137/* 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. 142 * 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.) 146 */ 147#define STRIDE_LENGTH 8 148errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num, 149 blk_t *ret_blk, int *ret_count) 150{ 151 int j, count; 152 static char *buf; 153 errcode_t retval; 154 155 /* If fs is null, clean up the static buffer and return */ 156 if (!fs) { 157 if (buf) { 158 free(buf); 159 buf = 0; 160 } 161 return 0; 162 } 163 /* Allocate the zeroizing buffer if necessary */ 164 if (!buf) { 165 buf = malloc(fs->blocksize * STRIDE_LENGTH); 166 if (!buf) 167 return ENOMEM; 168 memset(buf, 0, fs->blocksize * STRIDE_LENGTH); 169 } 170 /* OK, do the write loop */ 171 j=0; 172 while (j < num) { 173 if (blk % STRIDE_LENGTH) { 174 count = STRIDE_LENGTH - (blk % STRIDE_LENGTH); 175 if (count > (num - j)) 176 count = num - j; 177 } else { 178 count = num - j; 179 if (count > STRIDE_LENGTH) 180 count = STRIDE_LENGTH; 181 } 182 retval = io_channel_write_blk(fs->io, blk, count, buf); 183 if (retval) { 184 if (ret_count) 185 *ret_count = count; 186 if (ret_blk) 187 *ret_blk = blk; 188 return retval; 189 } 190 j += count; blk += count; 191 } 192 return 0; 193} 194 195/* 196 * Helper function for creating the journal using direct I/O routines 197 */ 198struct mkjournal_struct { 199 int num_blocks; 200 int newblocks; 201 blk_t goal; 202 blk_t blk_to_zero; 203 int zero_count; 204 char *buf; 205 errcode_t err; 206}; 207 208static int mkjournal_proc(ext2_filsys fs, 209 blk_t *blocknr, 210 e2_blkcnt_t blockcnt, 211 blk_t ref_block EXT2FS_ATTR((unused)), 212 int ref_offset EXT2FS_ATTR((unused)), 213 void *priv_data) 214{ 215 struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data; 216 blk_t new_blk; 217 errcode_t retval; 218 219 if (*blocknr) { 220 es->goal = *blocknr; 221 return 0; 222 } 223 retval = ext2fs_new_block(fs, es->goal, 0, &new_blk); 224 if (retval) { 225 es->err = retval; 226 return BLOCK_ABORT; 227 } 228 if (blockcnt >= 0) 229 es->num_blocks--; 230 231 es->newblocks++; 232 retval = 0; 233 if (blockcnt <= 0) 234 retval = io_channel_write_blk(fs->io, new_blk, 1, es->buf); 235 else { 236 if (es->zero_count) { 237 if ((es->blk_to_zero + es->zero_count == new_blk) && 238 (es->zero_count < 1024)) 239 es->zero_count++; 240 else { 241 retval = ext2fs_zero_blocks(fs, 242 es->blk_to_zero, 243 es->zero_count, 244 0, 0); 245 es->zero_count = 0; 246 } 247 } 248 if (es->zero_count == 0) { 249 es->blk_to_zero = new_blk; 250 es->zero_count = 1; 251 } 252 } 253 254 if (blockcnt == 0) 255 memset(es->buf, 0, fs->blocksize); 256 257 if (retval) { 258 es->err = retval; 259 return BLOCK_ABORT; 260 } 261 *blocknr = es->goal = new_blk; 262 ext2fs_block_alloc_stats(fs, new_blk, +1); 263 264 if (es->num_blocks == 0) 265 return (BLOCK_CHANGED | BLOCK_ABORT); 266 else 267 return BLOCK_CHANGED; 268 269} 270 271/* 272 * This function creates a journal using direct I/O routines. 273 */ 274static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino, 275 blk_t size, int flags) 276{ 277 char *buf; 278 dgrp_t group, start, end, i; 279 errcode_t retval; 280 struct ext2_inode inode; 281 struct mkjournal_struct es; 282 283 if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf))) 284 return retval; 285 286 if ((retval = ext2fs_read_bitmaps(fs))) 287 return retval; 288 289 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode))) 290 return retval; 291 292 if (inode.i_blocks > 0) 293 return EEXIST; 294 295 es.num_blocks = size; 296 es.newblocks = 0; 297 es.buf = buf; 298 es.err = 0; 299 es.zero_count = 0; 300 301 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) { 302 inode.i_flags |= EXT4_EXTENTS_FL; 303 if ((retval = ext2fs_write_inode(fs, journal_ino, &inode))) 304 return retval; 305 } 306 307 /* 308 * Set the initial goal block to be roughly at the middle of 309 * the filesystem. Pick a group that has the largest number 310 * of free blocks. 311 */ 312 group = ext2fs_group_of_blk(fs, (fs->super->s_blocks_count - 313 fs->super->s_first_data_block) / 2); 314 start = (group > 0) ? group-1 : group; 315 end = ((group+1) < fs->group_desc_count) ? group+1 : group; 316 group = start; 317 for (i=start+1; i <= end; i++) 318 if (fs->group_desc[i].bg_free_blocks_count > 319 fs->group_desc[group].bg_free_blocks_count) 320 group = i; 321 322 es.goal = (fs->super->s_blocks_per_group * group) + 323 fs->super->s_first_data_block; 324 325 retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND, 326 0, mkjournal_proc, &es); 327 if (es.err) { 328 retval = es.err; 329 goto errout; 330 } 331 if (es.zero_count) { 332 retval = ext2fs_zero_blocks(fs, es.blk_to_zero, 333 es.zero_count, 0, 0); 334 if (retval) 335 goto errout; 336 } 337 338 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode))) 339 goto errout; 340 341 inode.i_size += fs->blocksize * size; 342 ext2fs_iblk_add_blocks(fs, &inode, es.newblocks); 343 inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0); 344 inode.i_links_count = 1; 345 inode.i_mode = LINUX_S_IFREG | 0600; 346 347 if ((retval = ext2fs_write_inode(fs, journal_ino, &inode))) 348 goto errout; 349 retval = 0; 350 351 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4); 352 fs->super->s_jnl_blocks[16] = inode.i_size; 353 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS; 354 ext2fs_mark_super_dirty(fs); 355 356errout: 357 ext2fs_free_mem(&buf); 358 return retval; 359} 360 361/* 362 * Find a reasonable journal file size (in blocks) given the number of blocks 363 * in the filesystem. For very small filesystems, it is not reasonable to 364 * have a journal that fills more than half of the filesystem. 365 */ 366int ext2fs_default_journal_size(__u64 blocks) 367{ 368 if (blocks < 2048) 369 return -1; 370 if (blocks < 32768) 371 return (1024); 372 if (blocks < 256*1024) 373 return (4096); 374 if (blocks < 512*1024) 375 return (8192); 376 if (blocks < 1024*1024) 377 return (16384); 378 return 32768; 379} 380 381/* 382 * This function adds a journal device to a filesystem 383 */ 384errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev) 385{ 386 struct stat st; 387 errcode_t retval; 388 char buf[1024]; 389 journal_superblock_t *jsb; 390 int start; 391 __u32 i, nr_users; 392 393 /* Make sure the device exists and is a block device */ 394 if (stat(journal_dev->device_name, &st) < 0) 395 return errno; 396 397 if (!S_ISBLK(st.st_mode)) 398 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */ 399 400 /* Get the journal superblock */ 401 start = 1; 402 if (journal_dev->blocksize == 1024) 403 start++; 404 if ((retval = io_channel_read_blk(journal_dev->io, start, -1024, buf))) 405 return retval; 406 407 jsb = (journal_superblock_t *) buf; 408 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) || 409 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2))) 410 return EXT2_ET_NO_JOURNAL_SB; 411 412 if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize) 413 return EXT2_ET_UNEXPECTED_BLOCK_SIZE; 414 415 /* Check and see if this filesystem has already been added */ 416 nr_users = ntohl(jsb->s_nr_users); 417 for (i=0; i < nr_users; i++) { 418 if (memcmp(fs->super->s_uuid, 419 &jsb->s_users[i*16], 16) == 0) 420 break; 421 } 422 if (i >= nr_users) { 423 memcpy(&jsb->s_users[nr_users*16], 424 fs->super->s_uuid, 16); 425 jsb->s_nr_users = htonl(nr_users+1); 426 } 427 428 /* Writeback the journal superblock */ 429 if ((retval = io_channel_write_blk(journal_dev->io, start, -1024, buf))) 430 return retval; 431 432 fs->super->s_journal_inum = 0; 433 fs->super->s_journal_dev = st.st_rdev; 434 memcpy(fs->super->s_journal_uuid, jsb->s_uuid, 435 sizeof(fs->super->s_journal_uuid)); 436 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; 437 ext2fs_mark_super_dirty(fs); 438 return 0; 439} 440 441/* 442 * This function adds a journal inode to a filesystem, using either 443 * POSIX routines if the filesystem is mounted, or using direct I/O 444 * functions if it is not. 445 */ 446errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags) 447{ 448 errcode_t retval; 449 ext2_ino_t journal_ino; 450 struct stat st; 451 char jfile[1024]; 452 int mount_flags, f; 453 int fd = -1; 454 455 if ((retval = ext2fs_check_mount_point(fs->device_name, &mount_flags, 456 jfile, sizeof(jfile)-10))) 457 return retval; 458 459 if (mount_flags & EXT2_MF_MOUNTED) { 460 strcat(jfile, "/.journal"); 461 462 /* 463 * If .../.journal already exists, make sure any 464 * immutable or append-only flags are cleared. 465 */ 466#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP) 467 (void) chflags (jfile, 0); 468#else 469#if HAVE_EXT2_IOCTLS 470 fd = open(jfile, O_RDONLY); 471 if (fd >= 0) { 472 f = 0; 473 ioctl(fd, EXT2_IOC_SETFLAGS, &f); 474 close(fd); 475 } 476#endif 477#endif 478 479 /* Create the journal file */ 480 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0) 481 return errno; 482 483 if ((retval = write_journal_file(fs, jfile, size, flags))) 484 goto errout; 485 486 /* Get inode number of the journal file */ 487 if (fstat(fd, &st) < 0) 488 goto errout; 489 490#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP) 491 retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE); 492#else 493#if HAVE_EXT2_IOCTLS 494 f = EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL; 495 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f); 496#endif 497#endif 498 if (retval) 499 goto errout; 500 501 close(fd); 502 journal_ino = st.st_ino; 503 } else { 504 if ((mount_flags & EXT2_MF_BUSY) && 505 !(fs->flags & EXT2_FLAG_EXCLUSIVE)) { 506 retval = EBUSY; 507 goto errout; 508 } 509 journal_ino = EXT2_JOURNAL_INO; 510 if ((retval = write_journal_inode(fs, journal_ino, 511 size, flags))) 512 return retval; 513 } 514 515 fs->super->s_journal_inum = journal_ino; 516 fs->super->s_journal_dev = 0; 517 memset(fs->super->s_journal_uuid, 0, 518 sizeof(fs->super->s_journal_uuid)); 519 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; 520 521 ext2fs_mark_super_dirty(fs); 522 return 0; 523errout: 524 if (fd > 0) 525 close(fd); 526 return retval; 527} 528 529#ifdef DEBUG 530main(int argc, char **argv) 531{ 532 errcode_t retval; 533 char *device_name; 534 ext2_filsys fs; 535 536 if (argc < 2) { 537 fprintf(stderr, "Usage: %s filesystem\n", argv[0]); 538 exit(1); 539 } 540 device_name = argv[1]; 541 542 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0, 543 unix_io_manager, &fs); 544 if (retval) { 545 com_err(argv[0], retval, "while opening %s", device_name); 546 exit(1); 547 } 548 549 retval = ext2fs_add_journal_inode(fs, 1024); 550 if (retval) { 551 com_err(argv[0], retval, "while adding journal to %s", 552 device_name); 553 exit(1); 554 } 555 retval = ext2fs_flush(fs); 556 if (retval) { 557 printf("Warning, had trouble writing out superblocks.\n"); 558 } 559 ext2fs_close(fs); 560 exit(0); 561 562} 563#endif 564