mkjournal.c revision 4e40f6c348b9c9f4bd9c82c2b5404c2eabbdc1d3
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, (void **) &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		i, fd, ret_size;
98
99	if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
100		return retval;
101
102	/* Open the device or journal file */
103	if ((fd = open(filename, O_WRONLY)) < 0) {
104		retval = errno;
105		goto errout;
106	}
107
108	/* Write the superblock out */
109	retval = EXT2_ET_SHORT_WRITE;
110	ret_size = write(fd, buf, fs->blocksize);
111	if (ret_size < 0) {
112		retval = errno;
113		goto errout;
114	}
115	if (ret_size != fs->blocksize)
116		goto errout;
117	memset(buf, 0, fs->blocksize);
118
119	for (i = 1; i < size; i++) {
120		ret_size = write(fd, buf, fs->blocksize);
121		if (ret_size < 0) {
122			retval = errno;
123			goto errout;
124		}
125		if (ret_size != fs->blocksize)
126			goto errout;
127	}
128	close(fd);
129
130	retval = 0;
131errout:
132	ext2fs_free_mem((void **) &buf);
133	return retval;
134}
135
136/*
137 * Helper function for creating the journal using direct I/O routines
138 */
139struct mkjournal_struct {
140	int		num_blocks;
141	int		newblocks;
142	char		*buf;
143	errcode_t	err;
144};
145
146static int mkjournal_proc(ext2_filsys		fs,
147			   blk_t		*blocknr,
148			   e2_blkcnt_t		blockcnt,
149			   blk_t		ref_block,
150			   int			ref_offset,
151			   void			*priv_data)
152{
153	struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
154	blk_t	new_blk;
155	static blk_t	last_blk = 0;
156	errcode_t	retval;
157	int		group;
158
159	if (*blocknr) {
160		last_blk = *blocknr;
161		return 0;
162	}
163	retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
164	if (retval) {
165		es->err = retval;
166		return BLOCK_ABORT;
167	}
168	if (blockcnt > 0)
169		es->num_blocks--;
170
171	es->newblocks++;
172	retval = io_channel_write_blk(fs->io, new_blk, 1, es->buf);
173
174	if (blockcnt == 0)
175		memset(es->buf, 0, fs->blocksize);
176
177	if (retval) {
178		es->err = retval;
179		return BLOCK_ABORT;
180	}
181	*blocknr = new_blk;
182	ext2fs_mark_block_bitmap(fs->block_map, new_blk);
183	ext2fs_mark_bb_dirty(fs);
184	group = ext2fs_group_of_blk(fs, new_blk);
185	fs->group_desc[group].bg_free_blocks_count--;
186	fs->super->s_free_blocks_count--;
187	ext2fs_mark_super_dirty(fs);
188
189	if (es->num_blocks == 0)
190		return (BLOCK_CHANGED | BLOCK_ABORT);
191	else
192		return BLOCK_CHANGED;
193
194}
195
196/*
197 * This function creates a journal using direct I/O routines.
198 */
199static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
200				     blk_t size, int flags)
201{
202	char			*buf;
203	errcode_t		retval;
204	struct ext2_inode	inode;
205	struct mkjournal_struct	es;
206
207	if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
208		return retval;
209
210	if ((retval = ext2fs_read_bitmaps(fs)))
211		return retval;
212
213	if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
214		return retval;
215
216	if (inode.i_blocks > 0)
217		return EEXIST;
218
219	es.num_blocks = size;
220	es.newblocks = 0;
221	es.buf = buf;
222	es.err = 0;
223
224	retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND,
225				       0, mkjournal_proc, &es);
226	if (es.err) {
227		retval = es.err;
228		goto errout;
229	}
230
231	if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
232		goto errout;
233
234 	inode.i_size += fs->blocksize * size;
235	inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
236	inode.i_mtime = inode.i_ctime = time(0);
237	inode.i_links_count = 1;
238	inode.i_mode = LINUX_S_IFREG | 0600;
239
240	if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
241		goto errout;
242	retval = 0;
243
244errout:
245	ext2fs_free_mem((void **) &buf);
246	return retval;
247}
248
249/*
250 * This function adds a journal device to a filesystem
251 */
252errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
253{
254	struct stat	st;
255	errcode_t	retval;
256	char		buf[1024];
257	journal_superblock_t	*jsb;
258	int		i, start;
259	__u32		nr_users;
260
261	/* Make sure the device exists and is a block device */
262	if (stat(journal_dev->device_name, &st) < 0)
263		return errno;
264
265	if (!S_ISBLK(st.st_mode))
266		return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
267
268	/* Get the journal superblock */
269	start = 1;
270	if (journal_dev->blocksize == 1024)
271		start++;
272	if ((retval = io_channel_read_blk(journal_dev->io, start, -1024, buf)))
273		return retval;
274
275	jsb = (journal_superblock_t *) buf;
276	if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
277	    (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
278		return EXT2_ET_NO_JOURNAL_SB;
279
280	if (ntohl(jsb->s_blocksize) != fs->blocksize)
281		return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
282
283	/* Check and see if this filesystem has already been added */
284	nr_users = ntohl(jsb->s_nr_users);
285	for (i=0; i < nr_users; i++) {
286		if (memcmp(fs->super->s_uuid,
287			   &jsb->s_users[i*16], 16) == 0)
288			break;
289	}
290	if (i >= nr_users) {
291		memcpy(&jsb->s_users[nr_users*16],
292		       fs->super->s_uuid, 16);
293		jsb->s_nr_users = htonl(nr_users+1);
294	}
295
296	/* Writeback the journal superblock */
297	if ((retval = io_channel_write_blk(journal_dev->io, start, -1024, buf)))
298		return retval;
299
300	fs->super->s_journal_inum = 0;
301	fs->super->s_journal_dev = st.st_rdev;
302	memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
303	       sizeof(fs->super->s_journal_uuid));
304	fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
305	ext2fs_mark_super_dirty(fs);
306	return 0;
307}
308
309/*
310 * This function adds a journal inode to a filesystem, using either
311 * POSIX routines if the filesystem is mounted, or using direct I/O
312 * functions if it is not.
313 */
314errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
315{
316	errcode_t		retval;
317	ext2_ino_t		journal_ino;
318	struct stat		st;
319	char			jfile[1024];
320	int			fd, mount_flags, f;
321
322	if ((retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
323					       jfile, sizeof(jfile)-10)))
324		return retval;
325
326	if (mount_flags & EXT2_MF_MOUNTED) {
327		strcat(jfile, "/.journal");
328
329		/* Create the journal file */
330		if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
331			return errno;
332
333		if ((retval = write_journal_file(fs, jfile, size, flags)))
334			goto errout;
335
336		/* Get inode number of the journal file */
337		if (fstat(fd, &st) < 0)
338			goto errout;
339
340#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
341		retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
342#else
343#if HAVE_EXT2_IOCTLS
344		f = EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;;
345		retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
346#endif
347#endif
348		if (retval)
349			goto errout;
350
351		close(fd);
352		journal_ino = st.st_ino;
353	} else {
354		journal_ino = EXT2_JOURNAL_INO;
355		if ((retval = write_journal_inode(fs, journal_ino,
356						  size, flags)))
357			return retval;
358	}
359
360	fs->super->s_journal_inum = journal_ino;
361	fs->super->s_journal_dev = 0;
362	memset(fs->super->s_journal_uuid, 0,
363	       sizeof(fs->super->s_journal_uuid));
364	fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
365
366	ext2fs_mark_super_dirty(fs);
367	return 0;
368errout:
369	close(fd);
370	return retval;
371}
372
373#ifdef DEBUG
374main(int argc, char **argv)
375{
376	errcode_t	retval;
377	char		*device_name;
378	ext2_filsys 	fs;
379
380	if (argc < 2) {
381		fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
382		exit(1);
383	}
384	device_name = argv[1];
385
386	retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
387			      unix_io_manager, &fs);
388	if (retval) {
389		com_err(argv[0], retval, "while opening %s", device_name);
390		exit(1);
391	}
392
393	retval = ext2fs_add_journal_inode(fs, 1024);
394	if (retval) {
395		com_err(argv[0], retval, "while adding journal to %s",
396			device_name);
397		exit(1);
398	}
399	retval = ext2fs_flush(fs);
400	if (retval) {
401		printf("Warning, had trouble writing out superblocks.\n");
402	}
403	ext2fs_close(fs);
404	exit(0);
405
406}
407#endif
408