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 * Helper function for creating the journal using direct I/O routines
139 */
140struct mkjournal_struct {
141	int		num_blocks;
142	int		newblocks;
143	char		*buf;
144	errcode_t	err;
145};
146
147static int mkjournal_proc(ext2_filsys	fs,
148			   blk_t	*blocknr,
149			   e2_blkcnt_t	blockcnt,
150			   blk_t	ref_block EXT2FS_ATTR((unused)),
151			   int		ref_offset EXT2FS_ATTR((unused)),
152			   void		*priv_data)
153{
154	struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
155	blk_t	new_blk;
156	static blk_t	last_blk = 0;
157	errcode_t	retval;
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	last_blk = new_blk;
183	ext2fs_block_alloc_stats(fs, new_blk, +1);
184
185	if (es->num_blocks == 0)
186		return (BLOCK_CHANGED | BLOCK_ABORT);
187	else
188		return BLOCK_CHANGED;
189
190}
191
192/*
193 * This function creates a journal using direct I/O routines.
194 */
195static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
196				     blk_t size, int flags)
197{
198	char			*buf;
199	errcode_t		retval;
200	struct ext2_inode	inode;
201	struct mkjournal_struct	es;
202
203	if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
204		return retval;
205
206	if ((retval = ext2fs_read_bitmaps(fs)))
207		return retval;
208
209	if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
210		return retval;
211
212	if (inode.i_blocks > 0)
213		return EEXIST;
214
215	es.num_blocks = size;
216	es.newblocks = 0;
217	es.buf = buf;
218	es.err = 0;
219
220	retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND,
221				       0, mkjournal_proc, &es);
222	if (es.err) {
223		retval = es.err;
224		goto errout;
225	}
226
227	if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
228		goto errout;
229
230 	inode.i_size += fs->blocksize * size;
231	inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
232	inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0);
233	inode.i_links_count = 1;
234	inode.i_mode = LINUX_S_IFREG | 0600;
235
236	if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
237		goto errout;
238	retval = 0;
239
240	memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
241	fs->super->s_jnl_blocks[16] = inode.i_size;
242	fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
243	ext2fs_mark_super_dirty(fs);
244
245errout:
246	ext2fs_free_mem(&buf);
247	return retval;
248}
249
250/*
251 * Find a reasonable journal file size (in blocks) given the number of blocks
252 * in the filesystem.  For very small filesystems, it is not reasonable to
253 * have a journal that fills more than half of the filesystem.
254 */
255int ext2fs_default_journal_size(__u64 blocks)
256{
257	if (blocks < 2048)
258		return -1;
259	if (blocks < 32768)
260		return (1024);
261	if (blocks < 256*1024)
262		return (4096);
263	if (blocks < 512*1024)
264		return (8192);
265	if (blocks < 1024*1024)
266		return (16384);
267	return 32768;
268}
269
270/*
271 * This function adds a journal device to a filesystem
272 */
273errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
274{
275	struct stat	st;
276	errcode_t	retval;
277	char		buf[1024];
278	journal_superblock_t	*jsb;
279	int		start;
280	__u32		i, nr_users;
281
282	/* Make sure the device exists and is a block device */
283	if (stat(journal_dev->device_name, &st) < 0)
284		return errno;
285
286	if (!S_ISBLK(st.st_mode))
287		return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
288
289	/* Get the journal superblock */
290	start = 1;
291	if (journal_dev->blocksize == 1024)
292		start++;
293	if ((retval = io_channel_read_blk(journal_dev->io, start, -1024, buf)))
294		return retval;
295
296	jsb = (journal_superblock_t *) buf;
297	if ((jsb->s_header.h_magic != (unsigned) ntohl(JFS_MAGIC_NUMBER)) ||
298	    (jsb->s_header.h_blocktype != (unsigned) ntohl(JFS_SUPERBLOCK_V2)))
299		return EXT2_ET_NO_JOURNAL_SB;
300
301	if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
302		return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
303
304	/* Check and see if this filesystem has already been added */
305	nr_users = ntohl(jsb->s_nr_users);
306	for (i=0; i < nr_users; i++) {
307		if (memcmp(fs->super->s_uuid,
308			   &jsb->s_users[i*16], 16) == 0)
309			break;
310	}
311	if (i >= nr_users) {
312		memcpy(&jsb->s_users[nr_users*16],
313		       fs->super->s_uuid, 16);
314		jsb->s_nr_users = htonl(nr_users+1);
315	}
316
317	/* Writeback the journal superblock */
318	if ((retval = io_channel_write_blk(journal_dev->io, start, -1024, buf)))
319		return retval;
320
321	fs->super->s_journal_inum = 0;
322	fs->super->s_journal_dev = st.st_rdev;
323	memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
324	       sizeof(fs->super->s_journal_uuid));
325	fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
326	ext2fs_mark_super_dirty(fs);
327	return 0;
328}
329
330/*
331 * This function adds a journal inode to a filesystem, using either
332 * POSIX routines if the filesystem is mounted, or using direct I/O
333 * functions if it is not.
334 */
335errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
336{
337	errcode_t		retval;
338	ext2_ino_t		journal_ino;
339	struct stat		st;
340	char			jfile[1024];
341	int			mount_flags, f;
342	int			fd = -1;
343
344	if ((retval = ext2fs_check_mount_point(fs->device_name, &mount_flags,
345					       jfile, sizeof(jfile)-10)))
346		return retval;
347
348	if (mount_flags & EXT2_MF_MOUNTED) {
349		strcat(jfile, "/.journal");
350
351		/*
352		 * If .../.journal already exists, make sure any
353		 * immutable or append-only flags are cleared.
354		 */
355#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
356		(void) chflags (jfile, 0);
357#else
358#if HAVE_EXT2_IOCTLS
359		fd = open(jfile, O_RDONLY);
360		if (fd >= 0) {
361			f = 0;
362			ioctl(fd, EXT2_IOC_SETFLAGS, &f);
363			close(fd);
364		}
365#endif
366#endif
367
368		/* Create the journal file */
369		if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
370			return errno;
371
372		if ((retval = write_journal_file(fs, jfile, size, flags)))
373			goto errout;
374
375		/* Get inode number of the journal file */
376		if (fstat(fd, &st) < 0)
377			goto errout;
378
379#if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
380		retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
381#else
382#if HAVE_EXT2_IOCTLS
383		f = EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
384		retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
385#endif
386#endif
387		if (retval)
388			goto errout;
389
390		close(fd);
391		journal_ino = st.st_ino;
392	} else {
393		if ((mount_flags & EXT2_MF_BUSY) &&
394		    !(fs->flags & EXT2_FLAG_EXCLUSIVE)) {
395			retval = EBUSY;
396			goto errout;
397		}
398		journal_ino = EXT2_JOURNAL_INO;
399		if ((retval = write_journal_inode(fs, journal_ino,
400						  size, flags)))
401			return retval;
402	}
403
404	fs->super->s_journal_inum = journal_ino;
405	fs->super->s_journal_dev = 0;
406	memset(fs->super->s_journal_uuid, 0,
407	       sizeof(fs->super->s_journal_uuid));
408	fs->super->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
409
410	ext2fs_mark_super_dirty(fs);
411	return 0;
412errout:
413	if (fd > 0)
414		close(fd);
415	return retval;
416}
417
418#ifdef DEBUG
419main(int argc, char **argv)
420{
421	errcode_t	retval;
422	char		*device_name;
423	ext2_filsys 	fs;
424
425	if (argc < 2) {
426		fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
427		exit(1);
428	}
429	device_name = argv[1];
430
431	retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
432			      unix_io_manager, &fs);
433	if (retval) {
434		com_err(argv[0], retval, "while opening %s", device_name);
435		exit(1);
436	}
437
438	retval = ext2fs_add_journal_inode(fs, 1024);
439	if (retval) {
440		com_err(argv[0], retval, "while adding journal to %s",
441			device_name);
442		exit(1);
443	}
444	retval = ext2fs_flush(fs);
445	if (retval) {
446		printf("Warning, had trouble writing out superblocks.\n");
447	}
448	ext2fs_close(fs);
449	exit(0);
450
451}
452#endif
453