Userspace utilities to manage btrfs filesystems
Go to file
Filipe David Borba Manana 1bef332a03 Btrfs-progs: fix restore command leaving corrupted files
When there are files that have parts shared with snapshots, the
restore command was incorrectly restoring them, as it was not
taking into account the offset and number of bytes fields from
the file extent item. Besides leaving the recovered file corrupt,
it was also inneficient as it read and wrote more data than needed
(with each extent copy overwriting portions of the one previously
written).

The following steps show how to reproduce this corruption issue:

$ mkfs.btrfs -f  /dev/sdb3
$ mount /dev/sdb3 /mnt/btrfs
$ perl -e '$d = "\x41" . ("\x00" x (1024*1024+349)); open($f,">","/mnt/btrfs/foobar"); print $f $d; close($f);'
$ du -b /mnt/btrfs/foobar
1048926 /mnt/btrfs/foobar
$ md5sum /mnt/btrfs/foobar
f9f778f3a7410c40e4ed104a3a63c3c4  /mnt/btrfs/foobar

$ btrfs subvolume snapshot /mnt/btrfs /mnt/btrfs/my_snap
$ perl -e 'open($f, "+<", "/mnt/btrfs/foobar"); seek($f, 4096, 0); print $f "\xff"; close($f);'
$ md5sum /mnt/btrfs/foobar
b983fcefd4622a03a78936484c40272b  /mnt/btrfs/foobar
$ umount /mnt/btrfs

$ btrfs restore /dev/sdb3 /tmp/copy
$ du -b /tmp/copy/foobar
1048926 /tmp/copy/foobar
$ md5sum /tmp/copy/foobar
88db338cbc1c44dfabae083f1ce642d5  /tmp/copy/foobar
$ od -t x1 -j 8192 -N 4 /tmp/copy/foobar
0020000 41 00 00 00
0020004
$ mount /dev/sdb3 /mnt/btrfs
$ od -t x1 -j 8192 -N 4 /mnt/btrfs/foobar
0020000 00 00 00 00
0020004
$ md5sum /mnt/btrfs/foobar
b983fcefd4622a03a78936484c40272b  /mnt/btrfs/foobar

Tested this change with zlib, lzo compression and file sizes larger
than 1GiB, and found no regression or other corruption issues (so far
at least).

Signed-off-by: Filipe David Borba Manana <fdmanana@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Chris Mason <chris.mason@fusionio.com>
2013-10-16 08:20:03 -04:00
man Btrfs-progs: Add Makefile infrastructure for subdirs 2013-09-10 01:34:50 +02:00
.gitignore btrfs-progs: get C=1 sparse checking working again 2013-09-03 19:41:12 +02:00
.hgignore Add .hgignore file to ignore verison.h 2008-07-23 11:36:13 -04:00
.hgtags Added tag v0.16 for changeset 2b26e4f8c71e 2008-08-04 15:46:36 -04:00
bcp Btrfs-progs: Correct path munging in bcp 2011-10-25 09:18:58 -04:00
bitops.h Btrfs-progs: add a free space cache checker to fsck V2 2013-04-23 18:56:21 +02:00
btrfs-calc-size.c btrfs-progs: per-thread, per-call pretty buffer 2013-08-09 14:32:33 +02:00
btrfs-convert.c Btrfs-progs: mkfs can now create fs with skinny extents 2013-09-03 19:41:13 +02:00
btrfs-corrupt-block.c btrfs-progs: drop unused parameter from btrfs_release_path 2013-09-03 19:40:46 +02:00
btrfs-crc.c btrfs-progs: Fix getopt on arm/ppc platforms 2013-08-09 14:32:34 +02:00
btrfs-debug-tree.c btrfs-progs: drop unused parameter from btrfs_release_path 2013-09-03 19:40:46 +02:00
btrfs-find-root.c btrfs-print: define void function args 2013-09-03 19:41:04 +02:00
btrfs-fragments.c Btrfs-progs: fix closing of opendir() 2013-08-09 14:32:36 +02:00
btrfs-image.c btrfs-progs: drop unused parameter from btrfs_release_path 2013-09-03 19:40:46 +02:00
btrfs-list.c btrfs-print: define void function args 2013-09-03 19:41:04 +02:00
btrfs-list.h btrfs-progs: mark static & remove unused from non-kernel code 2013-09-03 19:40:54 +02:00
btrfs-map-logical.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
btrfs-select-super.c btrfs-progs: btrfs-select-super output is confusing when it fails 2013-04-23 18:56:20 +02:00
btrfs-show-super.c btrfs-progs: remove variable length stack arrays 2013-09-03 19:41:03 +02:00
btrfs-zero-log.c btrfs-progs: separate super_copy out of fs_info 2013-03-10 16:12:21 +01:00
btrfs.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
btrfsck.h Btrfs-progs: Add chunk rebuild function for RAID1/SINGLE/DUP 2013-07-03 14:06:55 -04:00
btrfstune.c btrfs-progs: mark static & remove unused from non-kernel code 2013-09-03 19:40:54 +02:00
cmds-balance.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-check.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-chunk.c btrfs-progs: fix endian bugs in chunk rebuilding 2013-09-03 19:41:05 +02:00
cmds-device.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-filesystem.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-inspect.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-qgroup.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-quota.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-receive.c btrfs-progs: mark static & remove unused from non-kernel code 2013-09-03 19:40:54 +02:00
cmds-replace.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-restore.c Btrfs-progs: fix restore command leaving corrupted files 2013-10-16 08:20:03 -04:00
cmds-scrub.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
cmds-send.c btrfs-progs: drop weird indirections & dead code from send/receive 2013-09-03 19:40:47 +02:00
cmds-subvolume.c btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
commands.h btrfs-progs: use NULL instead of 0 2013-09-03 19:41:11 +02:00
COPYING fix COPYING 2007-06-12 15:41:08 -04:00
crc32c.c btrfs-progs: make many private symbols static 2013-09-03 19:41:08 +02:00
crc32c.h btrfs-progs: libify some parts of btrfs-progs 2013-02-27 15:24:25 +01:00
ctree.c btrfs-progs: make many private symbols static 2013-09-03 19:41:08 +02:00
ctree.h btrfs-progs: add ULL to u64 constant 2013-09-03 19:41:01 +02:00
dir-item.c btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
dir-test.c btrfs-progs: drop unused parameter from btrfs_release_path 2013-09-03 19:40:46 +02:00
disk-io.c btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
disk-io.h btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
extent_io.c btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
extent_io.h btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
extent-cache.c Btrfs-progs: extend the extent cache for the device extent 2013-07-03 14:06:54 -04:00
extent-cache.h Btrfs-progs: extend the extent cache for the device extent 2013-07-03 14:06:54 -04:00
extent-tree.c btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
file-item.c btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
free-space-cache.c btrfs-progs: make many private symbols static 2013-09-03 19:41:08 +02:00
free-space-cache.h Btrfs-progs: add a free space cache checker to fsck V2 2013-04-23 18:56:21 +02:00
hash.h Switch btrfs_name_hash() to crc32c 2008-08-20 20:11:23 -04:00
hasher.c Unaligned access fixes 2008-02-15 11:19:58 -05:00
help.c btrfs-progs: make many private symbols static 2013-09-03 19:41:08 +02:00
inode-item.c btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
inode-map.c btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
INSTALL Removing btrfsctl, btrfs-vol, btrfs-show 2013-03-19 18:21:41 +01:00
ioctl-test.c btrfs-progs: mark static & remove unused from non-kernel code 2013-09-03 19:40:54 +02:00
ioctl.h Btrfs-progs: added "btrfs quota rescan" -w switch (wait) 2013-09-03 19:40:50 +02:00
kerncompat.h btrfs-progs: get C=1 sparse checking working again 2013-09-03 19:41:12 +02:00
list.h Btrfs-progs: introduce list_{first, next}_entry/list_splice_tail{_init} 2013-07-03 14:06:55 -04:00
Makefile Btrfs-progs: Add Makefile infrastructure for subdirs 2013-09-10 01:34:50 +02:00
math.h Btrfs-progs: cleanup duplicated division functions 2013-08-09 14:32:28 +02:00
mkfs.c Btrfs-progs: mkfs can now create fs with skinny extents 2013-09-03 19:41:13 +02:00
print-tree.c btrfs-progs: fix in-place byte swapping 2013-09-03 19:41:06 +02:00
print-tree.h Btrfs: add btrfs-debug-tree -b <block number> to print a single block 2010-04-06 09:30:17 -04:00
qgroup.c btrfs-progs: fix qgroup realloc inheritance 2013-09-03 19:41:07 +02:00
qgroup.h btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
quick-test.c btrfs-progs: mark static & remove unused from non-kernel code 2013-09-03 19:40:54 +02:00
radix-tree.c btrfs-progs: make many private symbols static 2013-09-03 19:41:08 +02:00
radix-tree.h btrfs-progs: libify some parts of btrfs-progs 2013-02-27 15:24:25 +01:00
raid6.c btrfs-progs: give raid6.c its exported prototypes 2013-09-03 19:41:11 +02:00
random-test.c btrfs-progs: drop unused parameter from btrfs_release_path 2013-09-03 19:40:46 +02:00
rbtree.c Btrfs-progs: introduce common insert/search/delete functions for rb-tree 2013-07-03 14:06:54 -04:00
rbtree.h Btrfs-progs: introduce common insert/search/delete functions for rb-tree 2013-07-03 14:06:54 -04:00
repair.c Btrfs-progs: introduce common insert/search/delete functions for rb-tree 2013-07-03 14:06:54 -04:00
repair.h Btrfsck: add the ability to prune corrupt extent allocation tree blocks 2012-02-21 14:37:21 -05:00
root-tree.c btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00
send-stream.c Btrfs-progs: remove some unused code 2013-04-23 18:56:25 +02:00
send-stream.h Btrfs-progs: btrfs-receive optionally honors the end-cmd 2013-04-23 18:56:24 +02:00
send-test.c btrfs-progs: mark static & remove unused from non-kernel code 2013-09-03 19:40:54 +02:00
send-utils.c Btrfs-progs: use UUID tree for send/receive 2013-08-09 14:32:31 +02:00
send-utils.h Btrfs-progs: use UUID tree for send/receive 2013-08-09 14:32:31 +02:00
send.h btrfs-progs: make libbtrfs usable from C++ 2013-02-19 11:15:30 +01:00
show-blocks Fix showblocks to display different colors for different metadata roots 2009-03-09 13:00:44 -04:00
transaction.h btrfs-progs: add malloc check in transaction.h/btrfs_start_transaction 2013-01-18 18:27:21 +01:00
utils.c Btrfs-progs: mkfs can now create fs with skinny extents 2013-09-03 19:41:13 +02:00
utils.h Btrfs-progs: mkfs can now create fs with skinny extents 2013-09-03 19:41:13 +02:00
uuid-tree.c btrfs-progs: fix in-place byte swapping 2013-09-03 19:41:06 +02:00
version.sh version.sh: fix version when built from tarball 2013-01-17 18:13:03 +01:00
volumes.c btrfs-progs: remove variable length stack arrays 2013-09-03 19:41:03 +02:00
volumes.h btrfs-progs: mark static & remove unused from shared kernel code 2013-09-03 19:40:53 +02:00