blob: 02ffb97c1c7d3518f31d2e8f42eb19b78730d1fd [file] [log] [blame]
Theodore Ts'od3cd93c2000-10-24 18:33:16 +00001/*
2 * mkjournal.c --- make a journal for a filesystem
3 *
4 * Copyright (C) 2000 Theodore Ts'o.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'od3cd93c2000-10-24 18:33:16 +00006 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'od3cd93c2000-10-24 18:33:16 +00009 * %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
Theodore Ts'o349a4842001-06-11 00:49:29 +000028#if HAVE_SYS_IOCTL_H
29#include <sys/ioctl.h>
30#endif
Theodore Ts'o17ee8b12000-10-26 20:45:58 +000031#if HAVE_NETINET_IN_H
32#include <netinet/in.h>
33#endif
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000034
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000035#include "ext2_fs.h"
Theodore Ts'ocdaf1fa2001-01-05 22:23:22 +000036#include "e2p/e2p.h"
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000037#include "ext2fs.h"
Theodore Ts'o58618732000-12-09 05:47:45 +000038#include "jfs_user.h"
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000039
Theodore Ts'oa1128472001-01-16 06:56:14 +000040/*
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,
JP Abgralle0ed7402014-03-19 19:08:39 -070045 __u32 num_blocks, int flags,
Theodore Ts'oa1128472001-01-16 06:56:14 +000046 char **ret_jsb)
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000047{
Theodore Ts'oa1128472001-01-16 06:56:14 +000048 errcode_t retval;
49 journal_superblock_t *jsb;
Theodore Ts'o03603942001-04-17 00:53:25 +000050
JP Abgralle0ed7402014-03-19 19:08:39 -070051 if (num_blocks < 1024)
Theodore Ts'o03603942001-04-17 00:53:25 +000052 return EXT2_ET_JOURNAL_TOO_SMALL;
53
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040054 if ((retval = ext2fs_get_mem(fs->blocksize, &jsb)))
Theodore Ts'oa1128472001-01-16 06:56:14 +000055 return retval;
56
57 memset (jsb, 0, fs->blocksize);
Theodore Ts'o58618732000-12-09 05:47:45 +000058
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000059 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
Theodore Ts'o4e246702000-12-09 14:39:16 +000060 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);
Theodore Ts'oa1128472001-01-16 06:56:14 +000064 jsb->s_blocksize = htonl(fs->blocksize);
JP Abgralle0ed7402014-03-19 19:08:39 -070065 jsb->s_maxlen = htonl(num_blocks);
Theodore Ts'od23042a2001-02-08 03:37:16 +000066 jsb->s_nr_users = htonl(1);
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000067 jsb->s_first = htonl(1);
68 jsb->s_sequence = htonl(1);
Theodore Ts'oa1128472001-01-16 06:56:14 +000069 memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
Theodore Ts'oa1128472001-01-16 06:56:14 +000070 /*
Theodore Ts'od23042a2001-02-08 03:37:16 +000071 * If we're creating an external journal device, we need to
72 * adjust these fields.
Theodore Ts'oa1128472001-01-16 06:56:14 +000073 */
74 if (fs->super->s_feature_incompat &
Theodore Ts'o4e40f6c2001-07-27 23:33:40 -040075 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
Theodore Ts'oa1128472001-01-16 06:56:14 +000076 jsb->s_nr_users = 0;
Theodore Ts'o4e40f6c2001-07-27 23:33:40 -040077 if (fs->blocksize == 1024)
78 jsb->s_first = htonl(3);
79 else
80 jsb->s_first = htonl(2);
81 }
Theodore Ts'oa1128472001-01-16 06:56:14 +000082
83 *ret_jsb = (char *) jsb;
84 return 0;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000085}
Theodore Ts'o4becab62001-01-03 19:22:42 +000086
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000087/*
Theodore Ts'o4becab62001-01-03 19:22:42 +000088 * This function writes a journal using POSIX routines. It is used
89 * for creating external journals and creating journals on live
90 * filesystems.
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000091 */
Theodore Ts'oa1128472001-01-16 06:56:14 +000092static errcode_t write_journal_file(ext2_filsys fs, char *filename,
JP Abgralle0ed7402014-03-19 19:08:39 -070093 blk_t num_blocks, int flags)
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000094{
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000095 errcode_t retval;
96 char *buf = 0;
Theodore Ts'o54434922003-12-07 01:28:50 -050097 int fd, ret_size;
98 blk_t i;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +000099
JP Abgralle0ed7402014-03-19 19:08:39 -0700100 if ((retval = ext2fs_create_journal_superblock(fs, num_blocks, flags,
101 &buf)))
Theodore Ts'oa1128472001-01-16 06:56:14 +0000102 return retval;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000103
Theodore Ts'oa1128472001-01-16 06:56:14 +0000104 /* Open the device or journal file */
105 if ((fd = open(filename, O_WRONLY)) < 0) {
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000106 retval = errno;
JP Abgralle0ed7402014-03-19 19:08:39 -0700107 goto errfree;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000108 }
109
110 /* Write the superblock out */
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000111 retval = EXT2_ET_SHORT_WRITE;
112 ret_size = write(fd, buf, fs->blocksize);
113 if (ret_size < 0) {
Theodore Ts'oa1128472001-01-16 06:56:14 +0000114 retval = errno;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000115 goto errout;
116 }
Theodore Ts'o54434922003-12-07 01:28:50 -0500117 if (ret_size != (int) fs->blocksize)
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000118 goto errout;
119 memset(buf, 0, fs->blocksize);
Theodore Ts'o4becab62001-01-03 19:22:42 +0000120
JP Abgralle0ed7402014-03-19 19:08:39 -0700121 if (flags & EXT2_MKJOURNAL_LAZYINIT)
122 goto success;
123
124 for (i = 1; i < num_blocks; i++) {
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000125 ret_size = write(fd, buf, fs->blocksize);
126 if (ret_size < 0) {
127 retval = errno;
128 goto errout;
129 }
Theodore Ts'o54434922003-12-07 01:28:50 -0500130 if (ret_size != (int) fs->blocksize)
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000131 goto errout;
132 }
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000133
JP Abgralle0ed7402014-03-19 19:08:39 -0700134success:
Theodore Ts'o4becab62001-01-03 19:22:42 +0000135 retval = 0;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000136errout:
JP Abgralle0ed7402014-03-19 19:08:39 -0700137 close(fd);
138errfree:
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400139 ext2fs_free_mem(&buf);
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000140 return retval;
141}
142
143/*
Theodore Ts'oc8608502008-04-27 16:10:32 -0400144 * Convenience function which zeros out _num_ blocks starting at
145 * _blk_. In case of an error, the details of the error is returned
146 * via _ret_blk_ and _ret_count_ if they are non-NULL pointers.
147 * Returns 0 on success, and an error code on an error.
148 *
149 * As a special case, if the first argument is NULL, then it will
150 * attempt to free the static zeroizing buffer. (This is to keep
151 * programs that check for memory leaks happy.)
152 */
153#define STRIDE_LENGTH 8
JP Abgralle0ed7402014-03-19 19:08:39 -0700154errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
155 blk64_t *ret_blk, int *ret_count)
Theodore Ts'oc8608502008-04-27 16:10:32 -0400156{
157 int j, count;
158 static char *buf;
159 errcode_t retval;
160
161 /* If fs is null, clean up the static buffer and return */
162 if (!fs) {
163 if (buf) {
164 free(buf);
165 buf = 0;
166 }
167 return 0;
168 }
169 /* Allocate the zeroizing buffer if necessary */
170 if (!buf) {
171 buf = malloc(fs->blocksize * STRIDE_LENGTH);
172 if (!buf)
173 return ENOMEM;
174 memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
175 }
176 /* OK, do the write loop */
177 j=0;
178 while (j < num) {
Theodore Ts'obc507e32008-06-17 00:18:58 -0400179 if (blk % STRIDE_LENGTH) {
Theodore Ts'oc8608502008-04-27 16:10:32 -0400180 count = STRIDE_LENGTH - (blk % STRIDE_LENGTH);
Theodore Ts'obc507e32008-06-17 00:18:58 -0400181 if (count > (num - j))
182 count = num - j;
183 } else {
Theodore Ts'oc8608502008-04-27 16:10:32 -0400184 count = num - j;
185 if (count > STRIDE_LENGTH)
186 count = STRIDE_LENGTH;
187 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700188 retval = io_channel_write_blk64(fs->io, blk, count, buf);
Theodore Ts'oc8608502008-04-27 16:10:32 -0400189 if (retval) {
190 if (ret_count)
191 *ret_count = count;
192 if (ret_blk)
193 *ret_blk = blk;
194 return retval;
195 }
196 j += count; blk += count;
197 }
198 return 0;
199}
200
JP Abgralle0ed7402014-03-19 19:08:39 -0700201errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
202 blk_t *ret_blk, int *ret_count)
203{
204 blk64_t ret_blk2;
205 errcode_t retval;
206
207 retval = ext2fs_zero_blocks2(fs, blk, num, &ret_blk2, ret_count);
208 if (retval)
209 *ret_blk = (blk_t) ret_blk2;
210 return retval;
211}
212
Theodore Ts'oc8608502008-04-27 16:10:32 -0400213/*
Theodore Ts'o4becab62001-01-03 19:22:42 +0000214 * Helper function for creating the journal using direct I/O routines
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000215 */
216struct mkjournal_struct {
217 int num_blocks;
218 int newblocks;
JP Abgralle0ed7402014-03-19 19:08:39 -0700219 blk64_t goal;
220 blk64_t blk_to_zero;
Theodore Ts'oc8608502008-04-27 16:10:32 -0400221 int zero_count;
JP Abgralle0ed7402014-03-19 19:08:39 -0700222 int flags;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000223 char *buf;
224 errcode_t err;
225};
226
Theodore Ts'o54434922003-12-07 01:28:50 -0500227static int mkjournal_proc(ext2_filsys fs,
JP Abgralle0ed7402014-03-19 19:08:39 -0700228 blk64_t *blocknr,
229 e2_blkcnt_t blockcnt,
230 blk64_t ref_block EXT2FS_ATTR((unused)),
231 int ref_offset EXT2FS_ATTR((unused)),
232 void *priv_data)
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000233{
234 struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
JP Abgralle0ed7402014-03-19 19:08:39 -0700235 blk64_t new_blk;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000236 errcode_t retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400237
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000238 if (*blocknr) {
Theodore Ts'o1af01e92008-08-27 15:11:28 -0400239 es->goal = *blocknr;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000240 return 0;
241 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700242 if (blockcnt &&
243 (EXT2FS_B2C(fs, es->goal) == EXT2FS_B2C(fs, es->goal+1)))
244 new_blk = es->goal+1;
245 else {
246 es->goal &= ~EXT2FS_CLUSTER_MASK(fs);
247 retval = ext2fs_new_block2(fs, es->goal, 0, &new_blk);
248 if (retval) {
249 es->err = retval;
250 return BLOCK_ABORT;
251 }
252 ext2fs_block_alloc_stats2(fs, new_blk, +1);
253 es->newblocks++;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000254 }
Theodore Ts'o674c0cc2008-08-27 16:23:30 -0400255 if (blockcnt >= 0)
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000256 es->num_blocks--;
257
Theodore Ts'oc8608502008-04-27 16:10:32 -0400258 retval = 0;
259 if (blockcnt <= 0)
JP Abgralle0ed7402014-03-19 19:08:39 -0700260 retval = io_channel_write_blk64(fs->io, new_blk, 1, es->buf);
261 else if (!(es->flags & EXT2_MKJOURNAL_LAZYINIT)) {
Theodore Ts'oc8608502008-04-27 16:10:32 -0400262 if (es->zero_count) {
263 if ((es->blk_to_zero + es->zero_count == new_blk) &&
264 (es->zero_count < 1024))
265 es->zero_count++;
266 else {
JP Abgralle0ed7402014-03-19 19:08:39 -0700267 retval = ext2fs_zero_blocks2(fs,
268 es->blk_to_zero,
269 es->zero_count,
270 0, 0);
Theodore Ts'oc8608502008-04-27 16:10:32 -0400271 es->zero_count = 0;
272 }
273 }
274 if (es->zero_count == 0) {
275 es->blk_to_zero = new_blk;
276 es->zero_count = 1;
277 }
278 }
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000279
280 if (blockcnt == 0)
281 memset(es->buf, 0, fs->blocksize);
282
283 if (retval) {
284 es->err = retval;
285 return BLOCK_ABORT;
286 }
Theodore Ts'o1af01e92008-08-27 15:11:28 -0400287 *blocknr = es->goal = new_blk;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000288
289 if (es->num_blocks == 0)
290 return (BLOCK_CHANGED | BLOCK_ABORT);
291 else
292 return BLOCK_CHANGED;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400293
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000294}
295
296/*
Theodore Ts'o4becab62001-01-03 19:22:42 +0000297 * This function creates a journal using direct I/O routines.
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000298 */
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000299static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
JP Abgralle0ed7402014-03-19 19:08:39 -0700300 blk_t num_blocks, int flags)
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000301{
Theodore Ts'oa1128472001-01-16 06:56:14 +0000302 char *buf;
Theodore Ts'ob55d7392008-08-28 10:20:43 -0400303 dgrp_t group, start, end, i, log_flex;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000304 errcode_t retval;
305 struct ext2_inode inode;
JP Abgralle0ed7402014-03-19 19:08:39 -0700306 unsigned long long inode_size;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000307 struct mkjournal_struct es;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000308
JP Abgralle0ed7402014-03-19 19:08:39 -0700309 if ((retval = ext2fs_create_journal_superblock(fs, num_blocks, flags,
310 &buf)))
Theodore Ts'oa1128472001-01-16 06:56:14 +0000311 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400312
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000313 if ((retval = ext2fs_read_bitmaps(fs)))
JP Abgralle0ed7402014-03-19 19:08:39 -0700314 goto out2;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000315
Theodore Ts'o4becab62001-01-03 19:22:42 +0000316 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
JP Abgralle0ed7402014-03-19 19:08:39 -0700317 goto out2;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000318
JP Abgralle0ed7402014-03-19 19:08:39 -0700319 if (inode.i_blocks > 0) {
320 retval = EEXIST;
321 goto out2;
322 }
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000323
JP Abgralle0ed7402014-03-19 19:08:39 -0700324 es.num_blocks = num_blocks;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000325 es.newblocks = 0;
326 es.buf = buf;
327 es.err = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700328 es.flags = flags;
Theodore Ts'oc8608502008-04-27 16:10:32 -0400329 es.zero_count = 0;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000330
Theodore Ts'o961306d2008-08-27 15:50:44 -0400331 if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
332 inode.i_flags |= EXT4_EXTENTS_FL;
333 if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
JP Abgralle0ed7402014-03-19 19:08:39 -0700334 goto out2;
Theodore Ts'o961306d2008-08-27 15:50:44 -0400335 }
336
Theodore Ts'o1af01e92008-08-27 15:11:28 -0400337 /*
338 * Set the initial goal block to be roughly at the middle of
339 * the filesystem. Pick a group that has the largest number
340 * of free blocks.
341 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700342 group = ext2fs_group_of_blk2(fs, (ext2fs_blocks_count(fs->super) -
Theodore Ts'o1af01e92008-08-27 15:11:28 -0400343 fs->super->s_first_data_block) / 2);
Theodore Ts'ob55d7392008-08-28 10:20:43 -0400344 log_flex = 1 << fs->super->s_log_groups_per_flex;
345 if (fs->super->s_log_groups_per_flex && (group > log_flex)) {
346 group = group & ~(log_flex - 1);
347 while ((group < fs->group_desc_count) &&
JP Abgralle0ed7402014-03-19 19:08:39 -0700348 ext2fs_bg_free_blocks_count(fs, group) == 0)
Theodore Ts'ob55d7392008-08-28 10:20:43 -0400349 group++;
350 if (group == fs->group_desc_count)
351 group = 0;
352 start = group;
353 } else
354 start = (group > 0) ? group-1 : group;
Theodore Ts'o1af01e92008-08-27 15:11:28 -0400355 end = ((group+1) < fs->group_desc_count) ? group+1 : group;
356 group = start;
357 for (i=start+1; i <= end; i++)
JP Abgralle0ed7402014-03-19 19:08:39 -0700358 if (ext2fs_bg_free_blocks_count(fs, i) >
359 ext2fs_bg_free_blocks_count(fs, group))
Theodore Ts'o1af01e92008-08-27 15:11:28 -0400360 group = i;
361
JP Abgralle0ed7402014-03-19 19:08:39 -0700362 es.goal = ext2fs_group_first_block2(fs, group);
363 retval = ext2fs_block_iterate3(fs, journal_ino, BLOCK_FLAG_APPEND,
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000364 0, mkjournal_proc, &es);
Theodore Ts'oa1128472001-01-16 06:56:14 +0000365 if (es.err) {
366 retval = es.err;
367 goto errout;
368 }
Theodore Ts'oc8608502008-04-27 16:10:32 -0400369 if (es.zero_count) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700370 retval = ext2fs_zero_blocks2(fs, es.blk_to_zero,
Theodore Ts'oc8608502008-04-27 16:10:32 -0400371 es.zero_count, 0, 0);
372 if (retval)
373 goto errout;
374 }
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000375
Theodore Ts'o4becab62001-01-03 19:22:42 +0000376 if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
Theodore Ts'oa1128472001-01-16 06:56:14 +0000377 goto errout;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000378
JP Abgralle0ed7402014-03-19 19:08:39 -0700379 inode_size = (unsigned long long)fs->blocksize * num_blocks;
380 inode.i_size = inode_size & 0xFFFFFFFF;
381 inode.i_size_high = (inode_size >> 32) & 0xFFFFFFFF;
382 if (ext2fs_needs_large_file_feature(inode_size))
383 fs->super->s_feature_ro_compat |=
384 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
Theodore Ts'o1ca10592008-04-09 11:39:11 -0400385 ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
Theodore Ts'o32138182005-09-24 20:14:51 -0400386 inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0);
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000387 inode.i_links_count = 1;
388 inode.i_mode = LINUX_S_IFREG | 0600;
389
Eric Sandeen827c1882009-06-16 21:50:53 -0500390 if ((retval = ext2fs_write_new_inode(fs, journal_ino, &inode)))
Theodore Ts'oa1128472001-01-16 06:56:14 +0000391 goto errout;
392 retval = 0;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000393
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400394 memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
JP Abgralle0ed7402014-03-19 19:08:39 -0700395 fs->super->s_jnl_blocks[15] = inode.i_size_high;
Theodore Ts'oa435ec32003-08-21 00:40:26 -0400396 fs->super->s_jnl_blocks[16] = inode.i_size;
397 fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
398 ext2fs_mark_super_dirty(fs);
399
Theodore Ts'oa1128472001-01-16 06:56:14 +0000400errout:
JP Abgralle0ed7402014-03-19 19:08:39 -0700401 ext2fs_zero_blocks2(0, 0, 0, 0, 0);
402out2:
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400403 ext2fs_free_mem(&buf);
Theodore Ts'oa1128472001-01-16 06:56:14 +0000404 return retval;
Theodore Ts'o4becab62001-01-03 19:22:42 +0000405}
406
407/*
Theodore Ts'o56d12362007-06-21 11:59:06 -0400408 * Find a reasonable journal file size (in blocks) given the number of blocks
409 * in the filesystem. For very small filesystems, it is not reasonable to
410 * have a journal that fills more than half of the filesystem.
411 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700412int ext2fs_default_journal_size(__u64 num_blocks)
Theodore Ts'o56d12362007-06-21 11:59:06 -0400413{
JP Abgralle0ed7402014-03-19 19:08:39 -0700414 if (num_blocks < 2048)
Theodore Ts'o56d12362007-06-21 11:59:06 -0400415 return -1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700416 if (num_blocks < 32768)
Theodore Ts'o56d12362007-06-21 11:59:06 -0400417 return (1024);
JP Abgralle0ed7402014-03-19 19:08:39 -0700418 if (num_blocks < 256*1024)
Theodore Ts'o56d12362007-06-21 11:59:06 -0400419 return (4096);
JP Abgralle0ed7402014-03-19 19:08:39 -0700420 if (num_blocks < 512*1024)
Theodore Ts'o56d12362007-06-21 11:59:06 -0400421 return (8192);
JP Abgralle0ed7402014-03-19 19:08:39 -0700422 if (num_blocks < 1024*1024)
Theodore Ts'o56d12362007-06-21 11:59:06 -0400423 return (16384);
424 return 32768;
425}
426
427/*
Theodore Ts'o4becab62001-01-03 19:22:42 +0000428 * This function adds a journal device to a filesystem
429 */
Theodore Ts'oa1128472001-01-16 06:56:14 +0000430errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
Theodore Ts'o4becab62001-01-03 19:22:42 +0000431{
432 struct stat st;
433 errcode_t retval;
Theodore Ts'oa1128472001-01-16 06:56:14 +0000434 char buf[1024];
435 journal_superblock_t *jsb;
Theodore Ts'o54434922003-12-07 01:28:50 -0500436 int start;
437 __u32 i, nr_users;
Theodore Ts'o4becab62001-01-03 19:22:42 +0000438
439 /* Make sure the device exists and is a block device */
Theodore Ts'o02088862001-01-18 01:44:19 +0000440 if (stat(journal_dev->device_name, &st) < 0)
Theodore Ts'o4becab62001-01-03 19:22:42 +0000441 return errno;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400442
Theodore Ts'o4becab62001-01-03 19:22:42 +0000443 if (!S_ISBLK(st.st_mode))
Theodore Ts'o03603942001-04-17 00:53:25 +0000444 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
Theodore Ts'o4becab62001-01-03 19:22:42 +0000445
Theodore Ts'oa1128472001-01-16 06:56:14 +0000446 /* Get the journal superblock */
Theodore Ts'o36131b32001-07-26 23:44:39 -0400447 start = 1;
448 if (journal_dev->blocksize == 1024)
449 start++;
JP Abgralle0ed7402014-03-19 19:08:39 -0700450 if ((retval = io_channel_read_blk64(journal_dev->io, start, -1024,
451 buf)))
Theodore Ts'o4becab62001-01-03 19:22:42 +0000452 return retval;
453
Theodore Ts'oa1128472001-01-16 06:56:14 +0000454 jsb = (journal_superblock_t *) buf;
455 if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
456 (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
Theodore Ts'o03603942001-04-17 00:53:25 +0000457 return EXT2_ET_NO_JOURNAL_SB;
Theodore Ts'o4becab62001-01-03 19:22:42 +0000458
Theodore Ts'o54434922003-12-07 01:28:50 -0500459 if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
Theodore Ts'oa1128472001-01-16 06:56:14 +0000460 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
461
462 /* Check and see if this filesystem has already been added */
463 nr_users = ntohl(jsb->s_nr_users);
464 for (i=0; i < nr_users; i++) {
465 if (memcmp(fs->super->s_uuid,
466 &jsb->s_users[i*16], 16) == 0)
467 break;
468 }
469 if (i >= nr_users) {
470 memcpy(&jsb->s_users[nr_users*16],
471 fs->super->s_uuid, 16);
472 jsb->s_nr_users = htonl(nr_users+1);
473 }
474
475 /* Writeback the journal superblock */
JP Abgralle0ed7402014-03-19 19:08:39 -0700476 if ((retval = io_channel_write_blk64(journal_dev->io, start, -1024, buf)))
Theodore Ts'o4becab62001-01-03 19:22:42 +0000477 return retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400478
Theodore Ts'o4becab62001-01-03 19:22:42 +0000479 fs->super->s_journal_inum = 0;
480 fs->super->s_journal_dev = st.st_rdev;
Theodore Ts'oa1128472001-01-16 06:56:14 +0000481 memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
Theodore Ts'o4becab62001-01-03 19:22:42 +0000482 sizeof(fs->super->s_journal_uuid));
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000483 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'o4becab62001-01-03 19:22:42 +0000484 ext2fs_mark_super_dirty(fs);
Theodore Ts'ocdaf1fa2001-01-05 22:23:22 +0000485 return 0;
Theodore Ts'o4becab62001-01-03 19:22:42 +0000486}
487
488/*
489 * This function adds a journal inode to a filesystem, using either
490 * POSIX routines if the filesystem is mounted, or using direct I/O
491 * functions if it is not.
492 */
JP Abgralle0ed7402014-03-19 19:08:39 -0700493errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t num_blocks, int flags)
Theodore Ts'o4becab62001-01-03 19:22:42 +0000494{
495 errcode_t retval;
Theodore Ts'o31dbecd2001-01-11 04:54:39 +0000496 ext2_ino_t journal_ino;
Theodore Ts'o4becab62001-01-03 19:22:42 +0000497 struct stat st;
498 char jfile[1024];
JP Abgralle0ed7402014-03-19 19:08:39 -0700499 int mount_flags;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500500 int fd = -1;
Theodore Ts'o4becab62001-01-03 19:22:42 +0000501
JP Abgralle0ed7402014-03-19 19:08:39 -0700502 if (flags & EXT2_MKJOURNAL_NO_MNT_CHECK)
503 mount_flags = 0;
504 else if ((retval = ext2fs_check_mount_point(fs->device_name,
505 &mount_flags,
506 jfile, sizeof(jfile)-10)))
Theodore Ts'o4becab62001-01-03 19:22:42 +0000507 return retval;
508
509 if (mount_flags & EXT2_MF_MOUNTED) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700510#if HAVE_EXT2_IOCTLS
511 int f = 0;
512#endif
Theodore Ts'o4becab62001-01-03 19:22:42 +0000513 strcat(jfile, "/.journal");
514
Theodore Ts'o5bc28df2001-11-09 17:34:54 -0500515 /*
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400516 * If .../.journal already exists, make sure any
Theodore Ts'o5bc28df2001-11-09 17:34:54 -0500517 * immutable or append-only flags are cleared.
518 */
519#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
520 (void) chflags (jfile, 0);
521#else
522#if HAVE_EXT2_IOCTLS
523 fd = open(jfile, O_RDONLY);
524 if (fd >= 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700525 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
Theodore Ts'o5bc28df2001-11-09 17:34:54 -0500526 close(fd);
JP Abgralle0ed7402014-03-19 19:08:39 -0700527 if (retval)
528 return retval;
Theodore Ts'o5bc28df2001-11-09 17:34:54 -0500529 }
530#endif
531#endif
532
Theodore Ts'o4becab62001-01-03 19:22:42 +0000533 /* Create the journal file */
534 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
535 return errno;
Theodore Ts'o4becab62001-01-03 19:22:42 +0000536
JP Abgralle0ed7402014-03-19 19:08:39 -0700537 /* Note that we can't do lazy journal initialization for mounted
538 * filesystems, since the zero writing is also allocating the
539 * journal blocks. We could use fallocate, but not all kernels
540 * support that, and creating a journal on a mounted ext2
541 * filesystems is extremely rare these days... Ignore it. */
542 flags &= ~EXT2_MKJOURNAL_LAZYINIT;
543
544 if ((retval = write_journal_file(fs, jfile, num_blocks, flags)))
Theodore Ts'ob23520d2001-06-22 21:52:14 -0400545 goto errout;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400546
Theodore Ts'o4becab62001-01-03 19:22:42 +0000547 /* Get inode number of the journal file */
Theodore Ts'o8bafedb2009-08-25 10:07:16 -0400548 if (fstat(fd, &st) < 0) {
549 retval = errno;
Theodore Ts'ob23520d2001-06-22 21:52:14 -0400550 goto errout;
Theodore Ts'o8bafedb2009-08-25 10:07:16 -0400551 }
Theodore Ts'o4becab62001-01-03 19:22:42 +0000552
Theodore Ts'o78332622001-06-22 21:20:47 -0400553#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
Theodore Ts'o349a4842001-06-11 00:49:29 +0000554 retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
555#else
556#if HAVE_EXT2_IOCTLS
Theodore Ts'o8bafedb2009-08-25 10:07:16 -0400557 if (ioctl(fd, EXT2_IOC_GETFLAGS, &f) < 0) {
558 retval = errno;
559 goto errout;
560 }
561 f |= EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
Theodore Ts'o349a4842001-06-11 00:49:29 +0000562 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
563#endif
564#endif
Theodore Ts'o8bafedb2009-08-25 10:07:16 -0400565 if (retval) {
566 retval = errno;
Theodore Ts'ob23520d2001-06-22 21:52:14 -0400567 goto errout;
Theodore Ts'o8bafedb2009-08-25 10:07:16 -0400568 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400569
Theodore Ts'o8bafedb2009-08-25 10:07:16 -0400570 if (close(fd) < 0) {
571 retval = errno;
572 fd = -1;
573 goto errout;
574 }
Theodore Ts'o4becab62001-01-03 19:22:42 +0000575 journal_ino = st.st_ino;
576 } else {
Theodore Ts'ocef2ac12006-04-04 19:23:41 -0400577 if ((mount_flags & EXT2_MF_BUSY) &&
578 !(fs->flags & EXT2_FLAG_EXCLUSIVE)) {
Theodore Ts'o29af3142005-07-19 15:04:22 -0500579 retval = EBUSY;
580 goto errout;
581 }
Theodore Ts'o4becab62001-01-03 19:22:42 +0000582 journal_ino = EXT2_JOURNAL_INO;
583 if ((retval = write_journal_inode(fs, journal_ino,
JP Abgralle0ed7402014-03-19 19:08:39 -0700584 num_blocks, flags)))
Theodore Ts'o4becab62001-01-03 19:22:42 +0000585 return retval;
586 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400587
Theodore Ts'o4becab62001-01-03 19:22:42 +0000588 fs->super->s_journal_inum = journal_ino;
589 fs->super->s_journal_dev = 0;
590 memset(fs->super->s_journal_uuid, 0,
591 sizeof(fs->super->s_journal_uuid));
592 fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000593
594 ext2fs_mark_super_dirty(fs);
595 return 0;
Theodore Ts'ob23520d2001-06-22 21:52:14 -0400596errout:
JP Abgralle0ed7402014-03-19 19:08:39 -0700597 if (fd >= 0)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500598 close(fd);
Theodore Ts'ob23520d2001-06-22 21:52:14 -0400599 return retval;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000600}
601
602#ifdef DEBUG
603main(int argc, char **argv)
604{
605 errcode_t retval;
606 char *device_name;
JP Abgralle0ed7402014-03-19 19:08:39 -0700607 ext2_filsys fs;
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000608
609 if (argc < 2) {
610 fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
611 exit(1);
612 }
613 device_name = argv[1];
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400614
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000615 retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
616 unix_io_manager, &fs);
617 if (retval) {
618 com_err(argv[0], retval, "while opening %s", device_name);
619 exit(1);
620 }
621
JP Abgralle0ed7402014-03-19 19:08:39 -0700622 retval = ext2fs_add_journal_inode(fs, 1024, 0);
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000623 if (retval) {
624 com_err(argv[0], retval, "while adding journal to %s",
625 device_name);
626 exit(1);
627 }
628 retval = ext2fs_flush(fs);
629 if (retval) {
630 printf("Warning, had trouble writing out superblocks.\n");
631 }
632 ext2fs_close(fs);
633 exit(0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400634
Theodore Ts'od3cd93c2000-10-24 18:33:16 +0000635}
636#endif