1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- #include "ext4_utils/wipe.h"
- #include "ext4_utils/ext4_utils.h"
- #include <android-base/file.h>
- #if WIPE_IS_SUPPORTED
- #if defined(__linux__)
- #include <linux/fs.h>
- #include <sys/ioctl.h>
- #ifndef BLKDISCARD
- #define BLKDISCARD _IO(0x12,119)
- #endif
- #ifndef BLKSECDISCARD
- #define BLKSECDISCARD _IO(0x12,125)
- #endif
- int wipe_block_device(int fd, s64 len)
- {
- u64 range[2];
- int ret;
- if (!is_block_device_fd(fd)) {
-
- return 0;
- }
- range[0] = 0;
- range[1] = len;
- ret = ioctl(fd, BLKSECDISCARD, &range);
- if (ret < 0) {
- range[0] = 0;
- range[1] = len;
- ret = ioctl(fd, BLKDISCARD, &range);
- if (ret < 0) {
- warn("Discard failed\n");
- return 1;
- } else {
- char buf[4096] = {0};
- if (!android::base::WriteFully(fd, buf, 4096)) {
- warn("Writing zeros failed\n");
- return 1;
- }
- fsync(fd);
- warn("Wipe via secure discard failed, used discard instead\n");
- return 0;
- }
- }
- return 0;
- }
- #else
- #error "Missing block device wiping implementation for this platform!"
- #endif
- #else
- int wipe_block_device(int fd __attribute__((unused)), s64 len __attribute__((unused)))
- {
-
- return 1;
- }
- #endif
|