Skip to content

Commit 5e64472

Browse files
committed
btrfs-progs: enhance device preparation for discard=ignore cases
[BUG] For qemu/KVM guests where the storage has 'discard=ignore' set, mkfs.btrfs won't properly wipe out any existing super blocks from other filesystems: # mkfs.ext4 -F /dev/vdc # mkfs.btrfs -f /dev/vdc # wipefs /dev/vdc DEVICE OFFSET TYPE UUID LABEL vdc 0x438 ext4 19b84ba1-e783-406d-a2f0-269aca433892 vdc 0x10040 btrfs 8ab186d0-9f11-4132-a815-5397b99fcd1f Note that the ext4 super block is still there. [CAUSE] With 'discard=ignore' setting, the discard operation is accepted but no data will be wiped out. Furthermore, mkfs.btrfs do the zero write before discard, but that zero write itself is buffered write, and discard will drop any page cache of the block device, so such zero write never reaches the storage. [FIX] Fix the problem and hardening the data wipe out by: - Re-order the device preparation Instead of zero-write then discard, change it to discard then zero-write. So even if discard did nothing, the zero writes won't be discarded. - Use BLKZEROOUT ioctl when possible This ioctl will ensure the content is zeroed out. If the device supports WRITE_ZEROS and can do it cheaply, this will not take up extra space. Although this ioctl is only supported on block device. So the zero-buffered write is still needed as a fallback - Sync after the zero writes Although the zero writes are already after discard, be extra safe to make sure the zeros reach the storage. Issue: #1198 Signed-off-by: Qu Wenruo <wqu@suse.com>
1 parent 2b80ee1 commit 5e64472

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

‎common/device-utils.c‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
#define BLKDISCARD _IO(0x12,119)
4949
#endif
5050

51+
#ifndef BLKZEROOUT
52+
#define BLKZEROOUT _IO(0x12,127)
53+
#endif
54+
5155
/*
5256
* Discard the given range in one go
5357
*/
@@ -127,6 +131,15 @@ int device_zero_blocks(int fd, off_t start, size_t len, bool direct)
127131

128132
#define ZERO_DEV_BYTES SZ_2M
129133

134+
static int device_zeroout_range(int fd, u64 start, u64 len)
135+
{
136+
u64 range[2] = { start, len };
137+
138+
if (ioctl(fd, BLKZEROOUT, &range) < 0)
139+
return -errno;
140+
return 0;
141+
}
142+
130143
/*
131144
* Zero blocks in the range from start but not after the given device size.
132145
* (On SPARC the disk labels are preserved too.)
@@ -135,6 +148,7 @@ static int zero_dev_clamped(int fd, struct btrfs_zoned_device_info *zinfo,
135148
off_t start, ssize_t len, u64 dev_size)
136149
{
137150
off_t end = max(start, start + len);
151+
int ret;
138152

139153
#ifdef __sparc__
140154
/* Don't overwrite the disk labels on SPARC. */
@@ -148,7 +162,17 @@ static int zero_dev_clamped(int fd, struct btrfs_zoned_device_info *zinfo,
148162
if (zinfo && zinfo->model == ZONED_HOST_MANAGED)
149163
return zero_zone_blocks(fd, zinfo, start, end - start);
150164

151-
return device_zero_blocks(fd, start, end - start, false);
165+
if (!device_zeroout_range(fd, start, end - start))
166+
return 0;
167+
168+
ret = device_zero_blocks(fd, start, end - start, false);
169+
if (ret < 0)
170+
return ret;
171+
172+
ret = fsync(fd);
173+
if (ret < 0)
174+
return -errno;
175+
return 0;
152176
}
153177

154178
/*
@@ -279,6 +303,13 @@ int btrfs_prepare_device(int fd, const char *file, u64 *byte_count_ret,
279303
}
280304
}
281305

306+
/*
307+
* Discard drops the page cache and may not zero the data (e.g. QEMU
308+
* discard=ignore), so it must come before zeroing.
309+
*/
310+
if (!(opflags & PREP_DEVICE_ZONED) && (opflags & PREP_DEVICE_DISCARD))
311+
prepare_discard_device(file, fd, byte_count, opflags);
312+
282313
ret = zero_dev_clamped(fd, zinfo, 0, ZERO_DEV_BYTES, byte_count);
283314
for (i = 0 ; !ret && i < BTRFS_SUPER_MIRROR_MAX; i++)
284315
ret = zero_dev_clamped(fd, zinfo, btrfs_sb_offset(i),
@@ -293,9 +324,6 @@ int btrfs_prepare_device(int fd, const char *file, u64 *byte_count_ret,
293324
goto err;
294325
}
295326

296-
if (!(opflags & PREP_DEVICE_ZONED) && (opflags & PREP_DEVICE_DISCARD))
297-
prepare_discard_device(file, fd, byte_count, opflags);
298-
299327
ret = btrfs_wipe_existing_sb(fd, zinfo);
300328
if (ret < 0) {
301329
error("cannot wipe superblocks on %s", file);

0 commit comments

Comments
 (0)