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