From: Zheng Liu <wenqing.lz@xxxxxxxxxx> When ext2fs_file_open() is called with EXT2_FILE_SPAESE flag, ext2fs_file_read() will return EXT2_ET_READ_HOLE_FOUND if it meets a hole/uninitialized block. CC: "Theodore Ts'o" <tytso@xxxxxxx> Signed-off-by: Zheng Liu <wenqing.lz@xxxxxxxxxx> --- debugfs/dump.c | 44 ++++++++++++++++++++++++++++++++++++-------- lib/ext2fs/ext2_err.et.in | 3 +++ lib/ext2fs/ext2fs.h | 1 + lib/ext2fs/fileio.c | 11 +++++++++-- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/debugfs/dump.c b/debugfs/dump.c index a15a0b7..a34b293 100644 --- a/debugfs/dump.c +++ b/debugfs/dump.c @@ -107,26 +107,54 @@ static void dump_file(const char *cmdname, ext2_ino_t ino, int fd, struct ext2_inode inode; char buf[8192]; ext2_file_t e2_file; - int nbytes; + ext2_off64_t ret_pos; + int nbytes, flags = 0; unsigned int got; if (debugfs_read_inode(ino, &inode, cmdname)) return; - retval = ext2fs_file_open(current_fs, ino, 0, &e2_file); + /* + * we need to handle stdout because this function is called by + * do_cat() and do_dump(). + */ + if (fd != 1) + flags = EXT2_FILE_SPARSE; + retval = ext2fs_file_open(current_fs, ino, flags, &e2_file); if (retval) { com_err(cmdname, retval, "while opening ext2 file"); return; } while (1) { retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got); - if (retval) + if (retval && retval != EXT2_ET_READ_HOLE_FOUND) com_err(cmdname, retval, "while reading ext2 file"); - if (got == 0) - break; - nbytes = write(fd, buf, got); - if ((unsigned) nbytes != got) - com_err(cmdname, errno, "while writing file"); + if (retval & EXT2_ET_READ_HOLE_FOUND) { + if (got) { + nbytes = write(fd, buf, got); + if ((unsigned) nbytes != got) + com_err(cmdname, errno, + "while writing file"); + } + retval = ext2fs_file_llseek(e2_file, + EXT2_SEEK_OFFSET_INVALID, + EXT2_SEEK_DATA, &ret_pos); + if (retval == EXT2_ET_SEEK_BEYOND_EOF) + break; + if (retval) + com_err(cmdname, retval, + "while lseeking ext2 file"); + ret_pos = lseek64(fd, ret_pos, SEEK_SET); + if (ret_pos < 0) + com_err(cmdname, retval, + "while lseeking target file"); + } else { + if (got == 0) + break; + nbytes = write(fd, buf, got); + if ((unsigned) nbytes != got) + com_err(cmdname, errno, "while writing file"); + } } retval = ext2fs_file_close(e2_file); if (retval) { diff --git a/lib/ext2fs/ext2_err.et.in b/lib/ext2fs/ext2_err.et.in index f763938..0d6a031 100644 --- a/lib/ext2fs/ext2_err.et.in +++ b/lib/ext2fs/ext2_err.et.in @@ -476,4 +476,7 @@ ec EXT2_ET_MMP_CSUM_INVALID, ec EXT2_ET_SEEK_BEYOND_EOF, "lseek beyond the EOF" +ec EXT2_ET_READ_HOLE_FOUND, + "We read a hole/uninitialized block with EXT2_FILE_SPARSE flag" + end diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h index 474049f..8aa24b3 100644 --- a/lib/ext2fs/ext2fs.h +++ b/lib/ext2fs/ext2fs.h @@ -149,6 +149,7 @@ typedef struct ext2_struct_dblist *ext2_dblist; #define EXT2_FILE_WRITE 0x0001 #define EXT2_FILE_CREATE 0x0002 +#define EXT2_FILE_SPARSE 0x0004 #define EXT2_FILE_MASK 0x00FF diff --git a/lib/ext2fs/fileio.c b/lib/ext2fs/fileio.c index ab33290..6281db7 100644 --- a/lib/ext2fs/fileio.c +++ b/lib/ext2fs/fileio.c @@ -185,11 +185,12 @@ static errcode_t load_buffer(ext2_file_t file, int dontfill) { ext2_filsys fs = file->fs; errcode_t retval; + int ret_flags = 0; if (!(file->flags & EXT2_FILE_BUF_VALID)) { retval = ext2fs_bmap2(fs, file->ino, &file->inode, - BMAP_BUFFER, 0, file->blockno, 0, - &file->physblock); + BMAP_BUFFER, 0, file->blockno, + &ret_flags, &file->physblock); if (retval) return retval; if (!dontfill) { @@ -203,6 +204,12 @@ static errcode_t load_buffer(ext2_file_t file, int dontfill) memset(file->buf, 0, fs->blocksize); } file->flags |= EXT2_FILE_BUF_VALID; + if (file->flags & EXT2_FILE_SPARSE) { + if (file->physblock == 0 || + ret_flags & BMAP_RET_UNINIT) { + return EXT2_ET_READ_HOLE_FOUND; + } + } } return 0; } -- 1.7.12.rc2.18.g61b472e -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html