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