Whamcloud - gitweb
libext2fs: allocate clusters to files in expand_dir.c and mkjournal.c
[tools/e2fsprogs.git] / lib / ext2fs / mkjournal.c
index 23cc60c..de4d51c 100644 (file)
@@ -2,10 +2,10 @@
  * mkjournal.c --- make a journal for a filesystem
  *
  * Copyright (C) 2000 Theodore Ts'o.
- * 
+ *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
@@ -135,41 +135,139 @@ errout:
 }
 
 /*
+ * Convenience function which zeros out _num_ blocks starting at
+ * _blk_.  In case of an error, the details of the error is returned
+ * via _ret_blk_ and _ret_count_ if they are non-NULL pointers.
+ * Returns 0 on success, and an error code on an error.
+ *
+ * As a special case, if the first argument is NULL, then it will
+ * attempt to free the static zeroizing buffer.  (This is to keep
+ * programs that check for memory leaks happy.)
+ */
+#define STRIDE_LENGTH 8
+errcode_t ext2fs_zero_blocks2(ext2_filsys fs, blk64_t blk, int num,
+                             blk64_t *ret_blk, int *ret_count)
+{
+       int             j, count;
+       static char     *buf;
+       errcode_t       retval;
+
+       /* If fs is null, clean up the static buffer and return */
+       if (!fs) {
+               if (buf) {
+                       free(buf);
+                       buf = 0;
+               }
+               return 0;
+       }
+       /* Allocate the zeroizing buffer if necessary */
+       if (!buf) {
+               buf = malloc(fs->blocksize * STRIDE_LENGTH);
+               if (!buf)
+                       return ENOMEM;
+               memset(buf, 0, fs->blocksize * STRIDE_LENGTH);
+       }
+       /* OK, do the write loop */
+       j=0;
+       while (j < num) {
+               if (blk % STRIDE_LENGTH) {
+                       count = STRIDE_LENGTH - (blk % STRIDE_LENGTH);
+                       if (count > (num - j))
+                               count = num - j;
+               } else {
+                       count = num - j;
+                       if (count > STRIDE_LENGTH)
+                               count = STRIDE_LENGTH;
+               }
+               retval = io_channel_write_blk64(fs->io, blk, count, buf);
+               if (retval) {
+                       if (ret_count)
+                               *ret_count = count;
+                       if (ret_blk)
+                               *ret_blk = blk;
+                       return retval;
+               }
+               j += count; blk += count;
+       }
+       return 0;
+}
+
+errcode_t ext2fs_zero_blocks(ext2_filsys fs, blk_t blk, int num,
+                            blk_t *ret_blk, int *ret_count)
+{
+       blk64_t ret_blk2;
+       errcode_t retval;
+
+       retval = ext2fs_zero_blocks2(fs, blk, num, &ret_blk2, ret_count);
+       if (retval)
+               *ret_blk = (blk_t) ret_blk2;
+       return retval;
+}
+
+/*
  * Helper function for creating the journal using direct I/O routines
  */
 struct mkjournal_struct {
        int             num_blocks;
        int             newblocks;
+       blk64_t         goal;
+       blk64_t         blk_to_zero;
+       int             zero_count;
        char            *buf;
        errcode_t       err;
 };
 
 static int mkjournal_proc(ext2_filsys  fs,
-                          blk_t        *blocknr,
-                          e2_blkcnt_t  blockcnt,
-                          blk_t        ref_block EXT2FS_ATTR((unused)),
-                          int          ref_offset EXT2FS_ATTR((unused)),
-                          void         *priv_data)
+                         blk64_t       *blocknr,
+                         e2_blkcnt_t   blockcnt,
+                         blk64_t       ref_block EXT2FS_ATTR((unused)),
+                         int           ref_offset EXT2FS_ATTR((unused)),
+                         void          *priv_data)
 {
        struct mkjournal_struct *es = (struct mkjournal_struct *) priv_data;
-       blk_t   new_blk;
-       static blk_t    last_blk = 0;
+       blk64_t new_blk;
        errcode_t       retval;
-       
+
        if (*blocknr) {
-               last_blk = *blocknr;
+               es->goal = *blocknr;
                return 0;
        }
-       retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
-       if (retval) {
-               es->err = retval;
-               return BLOCK_ABORT;
+       if (blockcnt &&
+           (EXT2FS_B2C(fs, es->goal) == EXT2FS_B2C(fs, es->goal+1)))
+               new_blk = es->goal+1;
+       else {
+               es->goal &= ~EXT2FS_CLUSTER_MASK(fs);
+               retval = ext2fs_new_block2(fs, es->goal, 0, &new_blk);
+               if (retval) {
+                       es->err = retval;
+                       return BLOCK_ABORT;
+               }
        }
-       if (blockcnt > 0)
+       if (blockcnt >= 0)
                es->num_blocks--;
 
        es->newblocks++;
-       retval = io_channel_write_blk(fs->io, new_blk, 1, es->buf);
+       retval = 0;
+       if (blockcnt <= 0)
+               retval = io_channel_write_blk64(fs->io, new_blk, 1, es->buf);
+       else {
+               if (es->zero_count) {
+                       if ((es->blk_to_zero + es->zero_count == new_blk) &&
+                           (es->zero_count < 1024))
+                               es->zero_count++;
+                       else {
+                               retval = ext2fs_zero_blocks2(fs,
+                                                            es->blk_to_zero,
+                                                            es->zero_count,
+                                                            0, 0);
+                               es->zero_count = 0;
+                       }
+               }
+               if (es->zero_count == 0) {
+                       es->blk_to_zero = new_blk;
+                       es->zero_count = 1;
+               }
+       }
 
        if (blockcnt == 0)
                memset(es->buf, 0, fs->blocksize);
@@ -178,31 +276,31 @@ static int mkjournal_proc(ext2_filsys     fs,
                es->err = retval;
                return BLOCK_ABORT;
        }
-       *blocknr = new_blk;
-       last_blk = new_blk;
-       ext2fs_block_alloc_stats(fs, new_blk, +1);
+       *blocknr = es->goal = new_blk;
+       ext2fs_block_alloc_stats2(fs, new_blk, +1);
 
        if (es->num_blocks == 0)
                return (BLOCK_CHANGED | BLOCK_ABORT);
        else
                return BLOCK_CHANGED;
-       
+
 }
 
 /*
  * This function creates a journal using direct I/O routines.
  */
 static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
-                                    blk_t size, int flags)
+                                    blk64_t size, int flags)
 {
        char                    *buf;
+       dgrp_t                  group, start, end, i, log_flex;
        errcode_t               retval;
        struct ext2_inode       inode;
        struct mkjournal_struct es;
 
        if ((retval = ext2fs_create_journal_superblock(fs, size, flags, &buf)))
                return retval;
-       
+
        if ((retval = ext2fs_read_bitmaps(fs)))
                return retval;
 
@@ -216,13 +314,54 @@ static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
        es.newblocks = 0;
        es.buf = buf;
        es.err = 0;
+       es.zero_count = 0;
+
+       if (fs->super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS) {
+               inode.i_flags |= EXT4_EXTENTS_FL;
+               if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
+                       return retval;
+       }
 
-       retval = ext2fs_block_iterate2(fs, journal_ino, BLOCK_FLAG_APPEND,
+       /*
+        * Set the initial goal block to be roughly at the middle of
+        * the filesystem.  Pick a group that has the largest number
+        * of free blocks.
+        */
+       group = ext2fs_group_of_blk2(fs, (ext2fs_blocks_count(fs->super) -
+                                        fs->super->s_first_data_block) / 2);
+       log_flex = 1 << fs->super->s_log_groups_per_flex;
+       if (fs->super->s_log_groups_per_flex && (group > log_flex)) {
+               group = group & ~(log_flex - 1);
+               while ((group < fs->group_desc_count) &&
+                      ext2fs_bg_free_blocks_count(fs, group) == 0)
+                       group++;
+               if (group == fs->group_desc_count)
+                       group = 0;
+               start = group;
+       } else
+               start = (group > 0) ? group-1 : group;
+       end = ((group+1) < fs->group_desc_count) ? group+1 : group;
+       group = start;
+       for (i=start+1; i <= end; i++)
+               if (ext2fs_bg_free_blocks_count(fs, i) >
+                   ext2fs_bg_free_blocks_count(fs, group))
+                       group = i;
+
+       es.goal = (fs->super->s_blocks_per_group * group) +
+               fs->super->s_first_data_block;
+
+       retval = ext2fs_block_iterate3(fs, journal_ino, BLOCK_FLAG_APPEND,
                                       0, mkjournal_proc, &es);
        if (es.err) {
                retval = es.err;
                goto errout;
        }
+       if (es.zero_count) {
+               retval = ext2fs_zero_blocks2(fs, es.blk_to_zero,
+                                           es.zero_count, 0, 0);
+               if (retval)
+                       goto errout;
+       }
 
        if ((retval = ext2fs_read_inode(fs, journal_ino, &inode)))
                goto errout;
@@ -233,7 +372,7 @@ static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
        inode.i_links_count = 1;
        inode.i_mode = LINUX_S_IFREG | 0600;
 
-       if ((retval = ext2fs_write_inode(fs, journal_ino, &inode)))
+       if ((retval = ext2fs_write_new_inode(fs, journal_ino, &inode)))
                goto errout;
        retval = 0;
 
@@ -243,6 +382,7 @@ static errcode_t write_journal_inode(ext2_filsys fs, ext2_ino_t journal_ino,
        ext2fs_mark_super_dirty(fs);
 
 errout:
+       ext2fs_zero_blocks2(0, 0, 0, 0, 0);
        ext2fs_free_mem(&buf);
        return retval;
 }
@@ -282,7 +422,7 @@ errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
        /* Make sure the device exists and is a block device */
        if (stat(journal_dev->device_name, &st) < 0)
                return errno;
-       
+
        if (!S_ISBLK(st.st_mode))
                return EXT2_ET_JOURNAL_NOT_BLOCK; /* Must be a block device */
 
@@ -290,7 +430,8 @@ errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
        start = 1;
        if (journal_dev->blocksize == 1024)
                start++;
-       if ((retval = io_channel_read_blk(journal_dev->io, start, -1024, buf)))
+       if ((retval = io_channel_read_blk64(journal_dev->io, start, -1024,
+                                           buf)))
                return retval;
 
        jsb = (journal_superblock_t *) buf;
@@ -315,9 +456,9 @@ errcode_t ext2fs_add_journal_device(ext2_filsys fs, ext2_filsys journal_dev)
        }
 
        /* Writeback the journal superblock */
-       if ((retval = io_channel_write_blk(journal_dev->io, start, -1024, buf)))
+       if ((retval = io_channel_write_blk64(journal_dev->io, start, -1024, buf)))
                return retval;
-       
+
        fs->super->s_journal_inum = 0;
        fs->super->s_journal_dev = st.st_rdev;
        memcpy(fs->super->s_journal_uuid, jsb->s_uuid,
@@ -349,7 +490,7 @@ errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
                strcat(jfile, "/.journal");
 
                /*
-                * If .../.journal already exists, make sure any 
+                * If .../.journal already exists, make sure any
                 * immutable or append-only flags are cleared.
                 */
 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
@@ -371,23 +512,35 @@ errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
 
                if ((retval = write_journal_file(fs, jfile, size, flags)))
                        goto errout;
-               
+
                /* Get inode number of the journal file */
-               if (fstat(fd, &st) < 0)
+               if (fstat(fd, &st) < 0) {
+                       retval = errno;
                        goto errout;
+               }
 
 #if defined(HAVE_CHFLAGS) && defined(UF_NODUMP)
                retval = fchflags (fd, UF_NODUMP|UF_IMMUTABLE);
 #else
 #if HAVE_EXT2_IOCTLS
-               f = EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
+               if (ioctl(fd, EXT2_IOC_GETFLAGS, &f) < 0) {
+                       retval = errno;
+                       goto errout;
+               }
+               f |= EXT2_NODUMP_FL | EXT2_IMMUTABLE_FL;
                retval = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
 #endif
 #endif
-               if (retval)
+               if (retval) {
+                       retval = errno;
                        goto errout;
-               
-               close(fd);
+               }
+
+               if (close(fd) < 0) {
+                       retval = errno;
+                       fd = -1;
+                       goto errout;
+               }
                journal_ino = st.st_ino;
        } else {
                if ((mount_flags & EXT2_MF_BUSY) &&
@@ -400,7 +553,7 @@ errcode_t ext2fs_add_journal_inode(ext2_filsys fs, blk_t size, int flags)
                                                  size, flags)))
                        return retval;
        }
-       
+
        fs->super->s_journal_inum = journal_ino;
        fs->super->s_journal_dev = 0;
        memset(fs->super->s_journal_uuid, 0,
@@ -427,7 +580,7 @@ main(int argc, char **argv)
                exit(1);
        }
        device_name = argv[1];
-       
+
        retval = ext2fs_open (device_name, EXT2_FLAG_RW, 0, 0,
                              unix_io_manager, &fs);
        if (retval) {
@@ -447,6 +600,6 @@ main(int argc, char **argv)
        }
        ext2fs_close(fs);
        exit(0);
-       
+
 }
 #endif