1 /* blkdiscard - discard device sectors
2 *
3 * Copyright 2020 Patrick Oppenlander <patrick.oppenlander@gmail.com>
4 *
5 * See http://man7.org/linux/man-pages/man8/blkdiscard.8.html
6 *
7 * The -v and -p options are not supported.
8 * Size parsing does not match util-linux where MB, GB, TB are multiples of
9 * 1000 and MiB, TiB, GiB are multipes of 1024.
10
11 USE_BLKDISCARD(NEWTOY(blkdiscard, "<1>1f(force)l(length)#<0o(offset)#<0s(secure)z(zeroout)[!sz]", TOYFLAG_BIN))
12
13 config BLKDISCARD
14 bool "blkdiscard"
15 default y
16 help
17 usage: blkdiscard [-olszf] DEVICE
18
19 Discard device sectors.
20
21 -o, --offset OFF Byte offset to start discarding at (default 0)
22 -l, --length LEN Bytes to discard (default all)
23 -s, --secure Perform secure discard
24 -z, --zeroout Zero-fill rather than discard
25 -f, --force Disable check for mounted filesystem
26
27 OFF and LEN must be aligned to the device sector size.
28 By default entire device is discarded.
29 WARNING: All discarded data is permanently lost!
30 */
31
32 #define FOR_blkdiscard
33 #include "toys.h"
34
35 #include <linux/fs.h>
36
GLOBALS(long o,l;)37 GLOBALS(
38 long o, l;
39 )
40
41 void blkdiscard_main(void)
42 {
43 int fd = xopen(*toys.optargs, O_WRONLY|O_EXCL*!FLAG(f));
44 unsigned long long ol[2];
45
46 // TODO: if numeric arg was long long array could live in TT.
47 ol[0] = TT.o;
48 if (FLAG(l)) ol[1] = TT.l;
49 else {
50 xioctl(fd, BLKGETSIZE64, ol+1);
51 ol[1] -= ol[0];
52 }
53 xioctl(fd, FLAG(s) ? BLKSECDISCARD : FLAG(z) ? BLKZEROOUT : BLKDISCARD, ol);
54 close(fd);
55 }
56