Whamcloud - gitweb
2be1009961b465e42a9169201876402a7609fbe8
[tools/e2fsprogs.git] / lib / ext2fs / mkjournal.c
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 Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #include <assert.h>
16 #if HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #if HAVE_ERRNO_H
20 #include <errno.h>
21 #endif
22 #include <fcntl.h>
23 #include <time.h>
24 #if HAVE_SYS_STAT_H
25 #include <sys/stat.h>
26 #endif
27 #if HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30 #if HAVE_SYS_IOCTL_H
31 #include <sys/ioctl.h>
32 #endif
33 #if HAVE_NETINET_IN_H
34 #include <netinet/in.h>
35 #endif
36 #ifdef __linux__
37 #include <sys/utsname.h>
38 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
39 #endif
40
41 #include "ext2_fs.h"
42 #include "e2p/e2p.h"
43 #include "ext2fs.h"
44
45 #include "kernel-jbd.h"
46
47 /*
48  * This function automatically sets up the journal superblock and
49  * returns it as an allocated block.
50  */
51 errcode_t ext2fs_create_journal_superblock2(ext2_filsys fs,
52                                            struct ext2fs_journal_params *jparams,
53                                            int flags, char **ret_jsb)
54 {
55         errcode_t               retval;
56         journal_superblock_t    *jsb;
57
58         if (jparams->num_journal_blocks < JBD2_MIN_JOURNAL_BLOCKS)
59                 return EXT2_ET_JOURNAL_TOO_SMALL;
60
61         if ((retval = ext2fs_get_mem(fs->blocksize, &jsb)))
62                 return retval;
63
64         memset (jsb, 0, fs->blocksize);
65
66         jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
67         if (flags & EXT2_MKJOURNAL_V1_SUPER)
68                 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V1);
69         else
70                 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
71         jsb->s_blocksize = htonl(fs->blocksize);
72         jsb->s_maxlen = htonl(jparams->num_journal_blocks + jparams->num_fc_blocks);
73         jsb->s_nr_users = htonl(1);
74         jsb->s_first = htonl(1);
75         jsb->s_sequence = htonl(1);
76         jsb->s_num_fc_blks = htonl(jparams->num_fc_blocks);
77         memcpy(jsb->s_uuid, fs->super->s_uuid, sizeof(fs->super->s_uuid));
78         /*
79          * If we're creating an external journal device, we need to
80          * adjust these fields.
81          */
82         if (ext2fs_has_feature_journal_dev(fs->super)) {
83                 jsb->s_nr_users = 0;
84                 jsb->s_first = htonl(ext2fs_journal_sb_start(fs->blocksize) + 1);
85         }
86
87         *ret_jsb = (char *) jsb;
88         return 0;
89 }
90
91 errcode_t ext2fs_create_journal_superblock(ext2_filsys fs, __u32 num_blocks,
92                                         int flags, char **ret_sb)
93 {
94         struct ext2fs_journal_params jparams;
95
96         jparams.num_journal_blocks = num_blocks;
97         jparams.num_fc_blocks = 0;
98
99         return ext2fs_create_journal_superblock2(fs, &jparams, flags, ret_sb);
100 }
101
102 /*
103  * This function writes a journal using POSIX routines.  It is used
104  * for creating external journals and creating journals on live
105  * filesystems.
106  */
107 static errcode_t write_journal_file(ext2_filsys fs, char *filename,
108                                     struct ext2fs_journal_params *jparams,
109                                     int flags)
110 {
111         errcode_t       retval;
112         char            *buf = 0;
113         int             fd, ret_size;
114         blk_t           i;
115
116         if ((retval = ext2fs_create_journal_superblock2(fs, jparams, flags,
117                                                        &buf)))
118                 return retval;
119
120         /* Open the device or journal file */
121         if ((fd = open(filename, O_WRONLY)) < 0) {
122                 retval = errno;
123                 goto errfree;
124         }
125
126         /* Write the superblock out */
127         retval = EXT2_ET_SHORT_WRITE;
128         ret_size = write(fd, buf, fs->blocksize);
129         if (ret_size < 0) {
130                 retval = errno;
131                 goto errout;
132         }
133         if (ret_size != (int) fs->blocksize)
134                 goto errout;
135         memset(buf, 0, fs->blocksize);
136
137         if (flags & EXT2_MKJOURNAL_LAZYINIT)
138                 goto success;
139
140         for (i = 1; i < jparams->num_journal_blocks + jparams->num_fc_blocks; i++) {
141                 ret_size = write(fd, buf, fs->blocksize);
142                 if (ret_size < 0) {
143                         retval = errno;
144                         goto errout;
145                 }
146                 if (ret_size != (int) fs->blocksize)
147                         goto errout;
148         }
149
150 success:
151         retval = 0;
152 errout:
153         close(fd);
154 errfree:
155         ext2fs_free_mem(&buf);
156         return retval;
157 }
158
159 /*
160  * Convenience function which zeros out _num_ blocks starting at
161  * _blk_.  In case of an error, the details of the error is returned
162  * via _ret_blk_ and _ret_count_ if they are non-NULL pointers.
163  * Returns 0 on success, and an error code on an error.
164  *
165  * As a special case, if the first argument is NULL, then it will
166  * attempt to free the static zeroizing buffer.  (This is to keep
167  * programs that check for memory leaks happy.)
168  */
169 #define MAX_STRIDE_LENGTH (4194304 / (int) fs->blocksize)
170 errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
171                               blk64_t *ret_blk, int *ret_count)
172 {
173         int             j, count;
174         static void     *buf;
175         static int      stride_length;
176         errcode_t       retval;
177
178         /* If fs is null, clean up the static buffer and return */
179         if (!fs) {
180                 if (buf) {
181                         free(buf);
182                         buf = 0;
183                         stride_length = 0;
184                 }
185                 return 0;
186         }
187
188         /* Deal with zeroing less than 1 block */
189         if (num <= 0)
190                 return 0;
191
192         /* Try a zero out command, if supported */
193         retval = io_channel_zeroout(fs->io, blk, num);
194         if (retval == 0)
195                 return 0;
196
197         /* Allocate the zeroizing buffer if necessary */
198         if (num > stride_length && stride_length < MAX_STRIDE_LENGTH) {
199                 void *p;
200                 int new_stride = num;
201
202                 if (new_stride > MAX_STRIDE_LENGTH)
203                         new_stride = MAX_STRIDE_LENGTH;
204                 p = realloc(buf, fs->blocksize * new_stride);
205                 if (!p)
206                         return EXT2_ET_NO_MEMORY;
207                 buf = p;
208                 stride_length = new_stride;
209                 memset(buf, 0, fs->blocksize * stride_length);
210         }
211         /* OK, do the write loop */
212         j=0;
213         while (j < num) {
214                 if (blk % stride_length) {
215                         count = stride_length - (blk % stride_length);
216                         if (count > (num - j))
217                                 count = num - j;
218                 } else {
219                         count = num - j;
220                         if (count > stride_length)
221                                 count = stride_length;
222                 }
223                 retval = io_channel_write_blk64(fs->io, blk, count, buf);
224                 if (retval) {
225                         if (ret_count)
226                                 *ret_count = count;
227                         if (ret_blk)
228                                 *ret_blk = blk;
229                         return retval;
230                 }
231                 j += count; blk += count;
232         }
233         return 0;
234 }
235
236 errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
237                              blk_t *ret_blk, int *ret_count)
238 {
239         blk64_t ret_blk2;
240         errcode_t retval;
241
242         retval = ext2fs_zero_blocks2(fs, blk, num, &ret_blk2, ret_count);
243         if (retval)
244                 *ret_blk = (blk_t) ret_blk2;
245         return retval;
246 }
247
248 /*
249  * Calculate the initial goal block to be roughly at the middle of the
250  * filesystem.  Pick a group that has the largest number of free
251  * blocks.
252  */
253 static blk64_t get_midpoint_journal_block(ext2_filsys fs)
254 {
255         dgrp_t  group, start, end, i, log_flex;
256
257         group = ext2fs_group_of_blk2(fs, (ext2fs_blocks_count(fs->super) -
258                                          fs->super->s_first_data_block) / 2);
259         log_flex = 1 << fs->super->s_log_groups_per_flex;
260         if (fs->super->s_log_groups_per_flex && (group > log_flex)) {
261                 group = group & ~(log_flex - 1);
262                 while ((group < fs->group_desc_count) &&
263                        ext2fs_bg_free_blocks_count(fs, group) == 0)
264                         group++;
265                 if (group == fs->group_desc_count)
266                         group = 0;
267                 start = group;
268         } else
269                 start = (group > 0) ? group-1 : group;
270         end = ((group+1) < fs->group_desc_count) ? group+1 : group;
271         group = start;
272         for (i = start + 1; i <= end; i++)
273                 if (ext2fs_bg_free_blocks_count(fs, i) >
274                     ext2fs_bg_free_blocks_count(fs, group))
275                         group = i;
276         return ext2fs_group_first_block2(fs, group);
277 }
278
279 /*
280  * This function creates a journal using direct I/O routines.
281  */
282 static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
283                                      struct ext2fs_journal_params *jparams,
284                                      blk64_t goal, int flags)
285 {
286         char                    *buf;
287         errcode_t               retval;
288         struct ext2_inode       inode;
289         unsigned long long      inode_size;
290         int                     falloc_flags = EXT2_FALLOCATE_FORCE_INIT;
291         blk64_t                 zblk;
292
293         if ((retval = ext2fs_create_journal_superblock2(fs, jparams, flags,
294                                                        &buf)))
295                 return retval;
296
297         if ((retval = ext2fs_read_bitmaps(fs)))
298                 goto out2;
299
300         if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
301                 goto out2;
302
303         if (inode.i_blocks > 0) {
304                 retval = EEXIST;
305                 goto out2;
306         }
307
308         if (goal == ~0ULL)
309                 goal = get_midpoint_journal_block(fs);
310
311         if (ext2fs_has_feature_extents(fs->super))
312                 inode.i_flags |= EXT4_EXTENTS_FL;
313
314         if (!(flags & EXT2_MKJOURNAL_LAZYINIT))
315                 falloc_flags |= EXT2_FALLOCATE_ZERO_BLOCKS;
316
317         inode_size = (unsigned long long)fs->blocksize *
318                         (jparams->num_journal_blocks + jparams->num_fc_blocks);
319         inode.i_mtime = inode.i_ctime = fs->now ? fs->now : time(0);
320         inode.i_links_count = 1;
321         inode.i_mode = LINUX_S_IFREG | 0600;
322         retval = ext2fs_inode_size_set(fs, &inode, inode_size);
323         if (retval)
324                 goto out2;
325
326         retval = ext2fs_fallocate(fs, falloc_flags, journal_ino,
327                                   &inode, goal, 0,
328                                   jparams->num_journal_blocks + jparams->num_fc_blocks);
329         if (retval)
330                 goto out2;
331
332         if ((retval = ext2fs_write_new_inode(fs, journal_ino, &inode)))
333                 goto out2;
334
335         retval = ext2fs_bmap2(fs, journal_ino, &inode, NULL, 0, 0, NULL, &zblk);
336         if (retval)
337                 goto out2;
338
339         retval = io_channel_write_blk64(fs->io, zblk, 1, buf);
340         if (retval)
341                 goto out2;
342
343         memcpy(fs->super->s_jnl_blocks, inode.i_block, EXT2_N_BLOCKS*4);
344         fs->super->s_jnl_blocks[15] = inode.i_size_high;
345         fs->super->s_jnl_blocks[16] = inode.i_size;
346         fs->super->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
347         ext2fs_mark_super_dirty(fs);
348
349 out2:
350         ext2fs_free_mem(&buf);
351         return retval;
352 }
353
354 #ifdef __linux__
355 static int parse_version_number(const char *s)
356 {
357         int     major, minor, rev;
358         char    *endptr;
359         const char *cp = s;
360
361         if (!s)
362                 return 0;
363         major = strtol(cp, &endptr, 10);
364         if (cp == endptr || *endptr != '.')
365                 return 0;
366         cp = endptr + 1;
367         minor = strtol(cp, &endptr, 10);
368         if (cp == endptr || *endptr != '.')
369                 return 0;
370         cp = endptr + 1;
371         rev = strtol(cp, &endptr, 10);
372         if (cp == endptr)
373                 return 0;
374         return KERNEL_VERSION(major, minor, rev);
375 }
376
377 int ext2fs_is_before_linux_ver(unsigned int major, unsigned int minor,
378                                unsigned int rev)
379 {
380         struct          utsname ut;
381         static int      linux_version_code = -1;
382
383         if (uname(&ut)) {
384                 perror("uname");
385                 exit(1);
386         }
387         if (linux_version_code < 0)
388                 linux_version_code = parse_version_number(ut.release);
389         if (linux_version_code == 0)
390                 return 0;
391
392         return linux_version_code < KERNEL_VERSION(major, minor, rev);
393 }
394 #else
395 int ext2fs_is_before_linux_ver(unsigned int major, unsigned int minor,
396                                unsigned int rev)
397 {
398         return 0;
399 }
400 #endif
401
402 /*
403  * Find a reasonable journal file size (in blocks) given the number of blocks
404  * in the filesystem.  For very small filesystems, it is not reasonable to
405  * have a journal that fills more than half of the filesystem.
406  *
407  * n.b. comments assume 4k blocks
408  */
409 int ext2fs_default_journal_size(__u64 num_blocks)
410 {
411         if (num_blocks < 2048)
412                 return -1;
413         if (num_blocks <= 8192)         /* 32 MB */
414                 return (1024);                  /* 4 MB */
415         if (num_blocks < 32768)         /* 128 MB */
416                 return (2048);                  /* 8 MB */
417         if (num_blocks < 256*1024)      /* 1 GB */
418                 return (4096);                  /* 16 MB */
419         if (num_blocks < 512*1024)      /* 2 GB */
420                 return (8192);                  /* 32 MB */
421         if (num_blocks < 4096*1024)     /* 16 GB */
422                 return (16384);                 /* 64 MB */
423         if (num_blocks < 8192*1024)     /* 32 GB */
424                 return (32768);                 /* 128 MB */
425         if (num_blocks < 16384*1024)    /* 64 GB */
426                 return (65536);                 /* 256 MB */
427         if (num_blocks < 32768*1024)    /* 128 GB */
428                 return (131072);                /* 512 MB */
429         return 262144;                          /* 1 GB */
430 }
431
432 errcode_t ext2fs_get_journal_params(struct ext2fs_journal_params *params,
433                 ext2_filsys fs)
434 {
435         blk_t total_blks;
436         int ret;
437
438         memset(params, 0, sizeof(*params));
439         if (ext2fs_has_feature_journal_dev(fs->super)) {
440                 total_blks = ext2fs_blocks_count(fs->super);
441                 if (total_blks < JBD2_MIN_JOURNAL_BLOCKS)
442                         return EXT2_ET_JOURNAL_TOO_SMALL;
443
444                 if (!ext2fs_has_feature_fast_commit(fs->super)) {
445                         params->num_journal_blocks = total_blks;
446                         params->num_fc_blocks = 0;
447                         return 0;
448                 }
449                 params->num_journal_blocks = ext2fs_blocks_count(fs->super) *
450                                 EXT2_JOURNAL_TO_FC_BLKS_RATIO /
451                                 (EXT2_JOURNAL_TO_FC_BLKS_RATIO + 1);
452                 if (JBD2_MIN_JOURNAL_BLOCKS > params->num_journal_blocks)
453                         params->num_journal_blocks = JBD2_MIN_JOURNAL_BLOCKS;
454                 params->num_fc_blocks = total_blks - params->num_journal_blocks;
455                 return 0;
456         }
457
458         ret = ext2fs_default_journal_size(ext2fs_blocks_count(fs->super));
459         if (ret < 0)
460                 return EXT2_ET_JOURNAL_TOO_SMALL;
461
462         params->num_journal_blocks = ret;
463         if (ext2fs_has_feature_fast_commit(fs->super))
464                 params->num_fc_blocks = params->num_journal_blocks /
465                         EXT2_JOURNAL_TO_FC_BLKS_RATIO;
466         return 0;
467 }
468
469 int ext2fs_journal_sb_start(int blocksize)
470 {
471         if (blocksize == EXT2_MIN_BLOCK_SIZE)
472                 return 2;
473         return 1;
474 }
475
476 /*
477  * This function adds a journal device to a filesystem
478  */
479 errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
480 {
481         struct stat     st;
482         errcode_t       retval;
483         char            buf[SUPERBLOCK_SIZE];
484         journal_superblock_t    *jsb;
485         int             start;
486         __u32           i, nr_users;
487
488         /* Make sure the device exists and is a block device */
489         if (stat(journal_dev->device_name, &st) < 0)
490                 return errno;
491
492         if (!S_ISBLK(st.st_mode))
493                 return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
494
495         /* Get the journal superblock */
496         start = ext2fs_journal_sb_start(journal_dev->blocksize);
497         if ((retval = io_channel_read_blk64(journal_dev->io, start,
498                                             -SUPERBLOCK_SIZE,
499                                             buf)))
500                 return retval;
501
502         jsb = (journal_superblock_t *) buf;
503         if ((jsb->s_header.h_magic != (unsigned) ntohl(JBD2_MAGIC_NUMBER)) ||
504             (jsb->s_header.h_blocktype != (unsigned) ntohl(JBD2_SUPERBLOCK_V2)))
505                 return EXT2_ET_NO_JOURNAL_SB;
506
507         if (ntohl(jsb->s_blocksize) != (unsigned long) fs->blocksize)
508                 return EXT2_ET_UNEXPECTED_BLOCK_SIZE;
509
510         /* Check and see if this filesystem has already been added */
511         nr_users = ntohl(jsb->s_nr_users);
512         if (nr_users > JBD2_USERS_MAX)
513                 return EXT2_ET_CORRUPT_JOURNAL_SB;
514         for (i=0; i < nr_users; i++) {
515                 if (memcmp(fs->super->s_uuid,
516                            &jsb->s_users[i*16], 16) == 0)
517                         break;
518         }
519         if (i >= nr_users) {
520                 memcpy(&jsb->s_users[nr_users*16],
521                        fs->super->s_uuid, 16);
522                 jsb->s_nr_users = htonl(nr_users+1);
523         }
524
525         /* Writeback the journal superblock */
526         if ((retval = io_channel_write_blk64(journal_dev->io, start,
527                                             -SUPERBLOCK_SIZE, buf)))
528                 return retval;
529
530         fs->super->s_journal_inum = 0;
531         fs->super->s_journal_dev = st.st_rdev;
532         memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
533                sizeof(fs->super->s_journal_uuid));
534         memset(fs->super->s_jnl_blocks, 0, sizeof(fs->super->s_jnl_blocks));
535         ext2fs_set_feature_journal(fs->super);
536         ext2fs_mark_super_dirty(fs);
537         return 0;
538 }
539
540 /*
541  * This function adds a journal inode to a filesystem, using either
542  * POSIX routines if the filesystem is mounted, or using direct I/O
543  * functions if it is not.
544  */
545 errcode_t ext2fs_add_journal_inode3(ext2_filsys fs, struct ext2fs_journal_params *jparams,
546                                     blk64_t goal, int flags)
547 {
548         errcode_t               retval;
549         ext2_ino_t              journal_ino;
550         struct stat             st;
551         char                    jfile[1024];
552         int                     mount_flags;
553         int                     fd = -1;
554
555         if (flags & EXT2_MKJOURNAL_NO_MNT_CHECK)
556                 mount_flags = 0;
557         else if ((retval = ext2fs_check_mount_point(fs->device_name,
558                                                     &mount_flags,
559                                                     jfile, sizeof(jfile)-10)))
560                 return retval;
561
562         if (mount_flags & EXT2_MF_MOUNTED) {
563 #if HAVE_EXT2_IOCTLS
564                 int f = 0;
565 #endif
566                 strcat(jfile, "/.journal");
567
568                 /*
569                  * If .../.journal already exists, make sure any
570                  * immutable or append-only flags are cleared.
571                  */
572 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
573                 (void) chflags (jfile, 0);
574 #else
575 #if HAVE_EXT2_IOCTLS
576                 fd = open(jfile, O_RDONLY);
577                 if (fd >= 0) {
578                         retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
579                         close(fd);
580                         if (retval)
581                                 return errno;
582                 }
583 #endif
584 #endif
585
586                 /* Create the journal file */
587                 if ((fd = open(jfile, O_CREAT|O_WRONLY, 0600)) < 0)
588                         return errno;
589
590                 /* Note that we can't do lazy journal initialization for mounted
591                  * filesystems, since the zero writing is also allocating the
592                  * journal blocks.  We could use fallocate, but not all kernels
593                  * support that, and creating a journal on a mounted ext2
594                  * filesystems is extremely rare these days...  Ignore it. */
595                 flags &= ~EXT2_MKJOURNAL_LAZYINIT;
596
597                 if ((retval = write_journal_file(fs, jfile, jparams, flags)))
598                         goto errout;
599
600                 /* Get inode number of the journal file */
601                 if (fstat(fd, &st) < 0) {
602                         retval = errno;
603                         goto errout;
604                 }
605
606 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
607                 retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
608 #else
609 #if HAVE_EXT2_IOCTLS
610                 if (ioctl(fd, EXT2_IOC_GETFLAGS, &f) < 0) {
611                         retval = errno;
612                         goto errout;
613                 }
614                 f |= EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
615                 retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
616 #endif
617 #endif
618                 if (retval) {
619                         retval = errno;
620                         goto errout;
621                 }
622
623                 if (close(fd) < 0) {
624                         retval = errno;
625                         fd = -1;
626                         goto errout;
627                 }
628                 journal_ino = st.st_ino;
629                 memset(fs->super->s_jnl_blocks, 0,
630                        sizeof(fs->super->s_jnl_blocks));
631         } else {
632                 if ((mount_flags & EXT2_MF_BUSY) &&
633                     !(fs->flags & EXT2_FLAG_EXCLUSIVE)) {
634                         retval = EBUSY;
635                         goto errout;
636                 }
637                 journal_ino = EXT2_JOURNAL_INO;
638                 if ((retval = write_journal_inode(fs, journal_ino,
639                                                   jparams, goal, flags)))
640                         return retval;
641         }
642
643         fs->super->s_journal_inum = journal_ino;
644         fs->super->s_journal_dev = 0;
645         memset(fs->super->s_journal_uuid, 0,
646                sizeof(fs->super->s_journal_uuid));
647         ext2fs_set_feature_journal(fs->super);
648
649         ext2fs_mark_super_dirty(fs);
650         return 0;
651 errout:
652         if (fd >= 0)
653                 close(fd);
654         return retval;
655 }
656
657 errcode_t ext2fs_add_journal_inode2(ext2_filsys fs, blk_t num_blocks,
658                                     blk64_t goal, int flags)
659 {
660         struct ext2fs_journal_params jparams;
661
662         jparams.num_journal_blocks = num_blocks;
663         jparams.num_fc_blocks = 0;
664
665         return ext2fs_add_journal_inode3(fs, &jparams, goal, flags);
666 }
667
668 errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t num_blocks, int flags)
669 {
670         return ext2fs_add_journal_inode2(fs, num_blocks, ~0ULL, flags);
671 }
672
673
674 #ifdef DEBUG
675 main(int argc, char **argv)
676 {
677         errcode_t       retval;
678         char            *device_name;
679         ext2_filsys     fs;
680
681         if (argc < 2) {
682                 fprintf(stderr, "Usage: %s filesystem\n", argv[0]);
683                 exit(1);
684         }
685         device_name = argv[1];
686
687         retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
688                               unix_io_manager, &fs);
689         if (retval) {
690                 com_err(argv[0], retval, "while opening %s", device_name);
691                 exit(1);
692         }
693
694         retval = ext2fs_add_journal_inode(fs, JBD2_MIN_JOURNAL_BLOCKS, 0);
695         if (retval) {
696                 com_err(argv[0], retval, "while adding journal to %s",
697                         device_name);
698                 exit(1);
699         }
700         retval = ext2fs_flush(fs);
701         if (retval) {
702                 printf("Warning, had trouble writing out superblocks.\n");
703         }
704         ext2fs_close_free(&fs);
705         exit(0);
706
707 }
708 #endif