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