Whamcloud - gitweb
libext2fs: file IO routines should handle uninit blocks
authorDarrick J. Wong <darrick.wong@oracle.com>
Wed, 3 Dec 2014 03:57:14 +0000 (22:57 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Wed, 3 Dec 2014 03:57:14 +0000 (22:57 -0500)
The file IO routines do not handle uninit blocks at all.  The read
method should check for the uninit flag and return a buffer of zeroes,
and the write routine should convert unwritten extents.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/fileio.c
tests/f_uninit_cat/expect [new file with mode: 0644]
tests/f_uninit_cat/image.gz [new file with mode: 0644]
tests/f_uninit_cat/name [new file with mode: 0644]
tests/f_uninit_cat/script [new file with mode: 0755]

index 1d5032a..d0a05d6 100644 (file)
@@ -123,6 +123,8 @@ errcode_t ext2fs_file_flush(ext2_file_t file)
 {
        errcode_t       retval;
        ext2_filsys fs;
+       int             ret_flags;
+       blk64_t         dontcare;
 
        EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
        fs = file->fs;
@@ -131,6 +133,22 @@ errcode_t ext2fs_file_flush(ext2_file_t file)
            !(file->flags & EXT2_FILE_BUF_DIRTY))
                return 0;
 
+       /* Is this an uninit block? */
+       if (file->physblock && file->inode.i_flags & EXT4_EXTENTS_FL) {
+               retval = ext2fs_bmap2(fs, file->ino, &file->inode, BMAP_BUFFER,
+                                     0, file->blockno, &ret_flags, &dontcare);
+               if (retval)
+                       return retval;
+               if (ret_flags & BMAP_RET_UNINIT) {
+                       retval = ext2fs_bmap2(fs, file->ino, &file->inode,
+                                             BMAP_BUFFER, BMAP_SET,
+                                             file->blockno, 0,
+                                             &file->physblock);
+                       if (retval)
+                               return retval;
+               }
+       }
+
        /*
         * OK, the physical block hasn't been allocated yet.
         * Allocate it.
@@ -185,15 +203,17 @@ static errcode_t load_buffer(ext2_file_t file, int dontfill)
 {
        ext2_filsys     fs = file->fs;
        errcode_t       retval;
+       int             ret_flags;
 
        if (!(file->flags & EXT2_FILE_BUF_VALID)) {
                retval = ext2fs_bmap2(fs, file->ino, &file->inode,
-                                    BMAP_BUFFER, 0, file->blockno, 0,
+                                    BMAP_BUFFER, 0, file->blockno, &ret_flags,
                                     &file->physblock);
                if (retval)
                        return retval;
                if (!dontfill) {
-                       if (file->physblock) {
+                       if (file->physblock &&
+                           !(ret_flags & BMAP_RET_UNINIT)) {
                                retval = io_channel_read_blk64(fs->io,
                                                               file->physblock,
                                                               1, file->buf);
diff --git a/tests/f_uninit_cat/expect b/tests/f_uninit_cat/expect
new file mode 100644 (file)
index 0000000..0c0a5cf
Binary files /dev/null and b/tests/f_uninit_cat/expect differ
diff --git a/tests/f_uninit_cat/image.gz b/tests/f_uninit_cat/image.gz
new file mode 100644 (file)
index 0000000..d2ae66c
Binary files /dev/null and b/tests/f_uninit_cat/image.gz differ
diff --git a/tests/f_uninit_cat/name b/tests/f_uninit_cat/name
new file mode 100644 (file)
index 0000000..f6b5674
--- /dev/null
@@ -0,0 +1 @@
+cat a file with uninit blocks
diff --git a/tests/f_uninit_cat/script b/tests/f_uninit_cat/script
new file mode 100755 (executable)
index 0000000..1655bb0
--- /dev/null
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+if test -x $DEBUGFS_EXE; then
+FSCK_OPT=-fy
+IMAGE=$test_dir/image.gz
+
+gzip -d < $IMAGE > $TMPFILE
+#e2label $TMPFILE test_filesys
+
+# Run fsck to fix things?
+EXP=$test_dir/expect
+OUT=$test_name.log
+rm -rf $test_name.failed $test_name.ok
+
+$FSCK $FSCK_OPT -N test_filesys $TMPFILE 2>&1 | sed -f $cmd_dir/filter.sed > $OUT
+echo "Exit status is $?" >> $OUT
+
+echo "debugfs cat uninit file" >> $OUT
+echo "ex /a" > $TMPFILE.cmd
+echo "cat /a" >> $TMPFILE.cmd
+$DEBUGFS_EXE -w -f $TMPFILE.cmd $TMPFILE >> $OUT.new 2>&1
+echo >> $OUT.new
+sed -f $cmd_dir/filter.sed < $OUT.new >> $OUT
+rm -rf $OUT.new $TMPFILE
+
+# Figure out what happened
+if cmp -s $EXP $OUT; then
+       echo "$test_name: $test_description: ok"
+       touch $test_name.ok
+else
+       echo "$test_name: $test_description: failed"
+       diff -u $EXP $OUT >> $test_name.failed
+fi
+unset EXP OUT FSCK_OPT IMAGE
+else #if test -a -x $DEBUGFS_EXE; then
+        echo "$test_name: $test_description: skipped"
+fi