1c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross/*
2c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * Copyright (C) 2011 The Android Open Source Project
3c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross *
4c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * Licensed under the Apache License, Version 2.0 (the "License");
5c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * you may not use this file except in compliance with the License.
6c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * You may obtain a copy of the License at
7c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross *
8c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross *      http://www.apache.org/licenses/LICENSE-2.0
9c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross *
10c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * Unless required by applicable law or agreed to in writing, software
11c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * distributed under the License is distributed on an "AS IS" BASIS,
12c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * See the License for the specific language governing permissions and
14c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross * limitations under the License.
15c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross */
16c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
17c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#include "ext4_utils.h"
18c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#include "wipe.h"
19c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
20c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#if defined(__linux__)
21c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
22c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#include <linux/fs.h>
23c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#include <sys/ioctl.h>
24c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
25c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#ifndef BLKDISCARD
26c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#define BLKDISCARD _IO(0x12,119)
27c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#endif
28c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
29c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#ifndef BLKSECDISCARD
30c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#define BLKSECDISCARD _IO(0x12,125)
31c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#endif
32c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
333e620597671c7efa678c1cd804202cff513bc751Ken Sumrallint wipe_block_device(int fd, s64 len)
34c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross{
35c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	u64 range[2];
36c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	int ret;
37c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
38c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	range[0] = 0;
39c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	range[1] = len;
40c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	ret = ioctl(fd, BLKSECDISCARD, &range);
41c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	if (ret < 0) {
42c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross		range[0] = 0;
43c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross		range[1] = len;
44c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross		ret = ioctl(fd, BLKDISCARD, &range);
45c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross		if (ret < 0) {
46427c3a2177eb4d34c88406bb86107548737168ccKen Sumrall			warn("Discard failed\n");
47c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross			return 1;
48c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross		} else {
49c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross			warn("Wipe via secure discard failed, used discard instead\n");
50c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross			return 0;
51c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross		}
52c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	}
53c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
54c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	return 0;
55c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross}
56c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#else
573e620597671c7efa678c1cd804202cff513bc751Ken Sumrallint wipe_block_device(int fd, s64 len)
58c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross{
59c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	error("wipe not supported on non-linux platforms");
60c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross	return 1;
61c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross}
62c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross#endif
63c2470654d4b4db09a7052fc5fa108ac21f1b1948Colin Cross
64