summaryrefslogtreecommitdiff
path: root/lib/ext2fs/mkjournal.c
blob: fcb01e347af6d4693c03cfdfe868e812faa46a43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * mkjournal.c --- make a journal for a filesystem
 *
 * Copyright (C) 2000 Theodore Ts'o.
 * 
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include <stdio.h>
#include <string.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#include <fcntl.h>
#include <time.h>
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#if EXT2_FLAT_INCLUDES
#include "ext2_fs.h"
#else
#include <linux/ext2_fs.h>
#endif

#include "ext2fs.h"
#include "jfs_user.h"

static void init_journal_superblock(journal_superblock_t *jsb,
				    __u32 blocksize, __u32 size)
{
	memset (jsb, 0, sizeof(*jsb));

	jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
	jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
	jsb->s_blocksize = htonl(blocksize);
	jsb->s_maxlen = htonl(size);
	jsb->s_first = htonl(1);
	jsb->s_sequence = htonl(1);
}
	
/*
 * This function adds a journal device to a filesystem
 */
errcode_t ext2fs_add_journal_device(ext2_filsys fs, char *device,
				    blk_t size)
{
	journal_superblock_t	jsb;
	struct stat	st;
	errcode_t	retval;
	char		*buf = 0;
	blk_t		dev_size;
	int		i, fd, ret_size;

	/* Make sure the device exists and is a block device */
	if (stat(device, &st) < 0)
		return errno;
	if (!S_ISBLK(st.st_mode))
		return EXT2_JOURNAL_NOT_BLOCK;	/* Must be a block device */
	
	/* Get the size of the device */
	if ((retval = ext2fs_get_device_size(device, fs->blocksize,
					     &dev_size)))
		return retval;
	
	if (!size)
		size = dev_size; /* Default to the size of the device */
	else if (size > dev_size) 
		return EINVAL;	/* Requested size bigger than device */

	init_journal_superblock(&jsb, fs->blocksize, size);

	/* Create a block buffer */
	buf = malloc(fs->blocksize);
	if (!buf)
		return ENOMEM;

	/* Open the device */
	if ((fd = open(device, O_WRONLY)) < 0) {
		retval = errno;
		goto errout;
	}

	/* Write the superblock out */
	memset(buf, 0, fs->blocksize);
	memcpy(buf, &jsb, sizeof(jsb));
	retval = EXT2_ET_SHORT_WRITE;
	ret_size = write(fd, buf, fs->blocksize);
	if (ret_size < 0) {
		errno = retval;
		goto errout;
	}
	if (ret_size != fs->blocksize)
		goto errout;
	memset(buf, 0, fs->blocksize);
	
	for (i=1; i < size; i++) {
		ret_size = write(fd, buf, fs->blocksize);
		if (ret_size < 0) {
			retval = errno;
			goto errout;
		}
		if (ret_size != fs->blocksize)
			goto errout;
	}
	close(fd);

	fs->super->s_journal_dev = st.st_rdev;
	fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
	ext2fs_mark_super_dirty(fs);

	return 0;
errout:
	if (buf)
		free(buf);
	return retval;
}

/*
 * Helper function for creating the journal in the filesystem
 */
struct mkjournal_struct {
	int		num_blocks;
	int		newblocks;
	char		*buf;
	errcode_t	err;
};

static int mkjournal_proc(ext2_filsys		fs,
			   blk_t		*blocknr,
			   e2_blkcnt_t		blockcnt,
			   blk_t		ref_block,
			   int			ref_offset,
			   void			*priv_data)
{
	struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
	blk_t	new_blk;
	static blk_t	last_blk = 0;
	char		*block;
	errcode_t	retval;
	int		group;
	
	if (*blocknr) {
		last_blk = *blocknr;
		return 0;
	}
	retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
	if (retval) {
		es->err = retval;
		return BLOCK_ABORT;
	}
	if (blockcnt > 0)
		es->num_blocks--;

	es->newblocks++;
	retval = io_channel_write_blk(fs->io, new_blk, 1, es->buf);

	if (blockcnt == 0)
		memset(es->buf, 0, fs->blocksize);

	if (retval) {
		es->err = retval;
		return BLOCK_ABORT;
	}
	*blocknr = new_blk;
	ext2fs_mark_block_bitmap(fs->block_map, new_blk);
	ext2fs_mark_bb_dirty(fs);
	group = ext2fs_group_of_blk(fs, new_blk);
	fs->group_desc[group].bg_free_blocks_count--;
	fs->super->s_free_blocks_count--;
	ext2fs_mark_super_dirty(fs);

	if (es->num_blocks == 0)
		return (BLOCK_CHANGED | BLOCK_ABORT);
	else
		return BLOCK_CHANGED;
	
}

/*
 * This function adds a journal inode to a filesystem
 */
errcode_t ext2fs_add_journal_fs(ext2_filsys fs, blk_t size)
{
	journal_superblock_t	jsb;
	errcode_t		retval;
	struct ext2_inode	inode;
	struct mkjournal_struct	es;
	char			*buf;

	init_journal_superblock(&jsb, fs->blocksize, size);

	if ((retval = ext2fs_read_bitmaps(fs)))
		return retval;

	if ((retval = ext2fs_read_inode(fs, EXT2_JOURNAL_INO, &inode)))
		return retval;

	if (inode.i_blocks > 0)
		return EEXIST;

	/* Create the block buffer */
	buf = malloc(fs->blocksize);
	if (!buf)
		return ENOMEM;

	memset(buf, 0, fs->blocksize);
	memcpy(buf, &jsb, sizeof(jsb));

	es.num_blocks = size;
	es.newblocks = 0;
	es.buf = buf;
	es.err = 0;

	retval = ext2fs_block_iterate2(fs, EXT2_JOURNAL_INO, BLOCK_FLAG_APPEND,
				       0, mkjournal_proc, &es);
	free(buf);
	if (es.err)
		return es.err;

	if ((retval = ext2fs_read_inode(fs, EXT2_JOURNAL_INO, &inode)))
		return retval;

 	inode.i_size += fs->blocksize * size;
	inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
	inode.i_mtime = inode.i_ctime = time(0);
	inode.i_links_count = 1;
	inode.i_mode = LINUX_S_IFREG | 0600;

	if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)))
		return retval;

	fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
	fs->super->s_journal_inum = EXT2_JOURNAL_INO;

	ext2fs_mark_super_dirty(fs);
	return 0;
}

#ifdef DEBUG
main(int argc, char **argv)
{
	errcode_t	retval;
	char		*device_name;
	ext2_filsys 	fs;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
		exit(1);
	}
	device_name = argv[1];
	
	retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
			      unix_io_manager, &fs);
	if (retval) {
		com_err(argv[0], retval, "while opening %s", device_name);
		exit(1);
	}

	retval = ext2fs_add_journal_fs(fs, 1024);
	if (retval) {
		com_err(argv[0], retval, "while adding journal to %s",
			device_name);
		exit(1);
	}
	retval = ext2fs_flush(fs);
	if (retval) {
		printf("Warning, had trouble writing out superblocks.\n");
	}
	ext2fs_close(fs);
	exit(0);
	
}
#endif