master xplshn/aruu / cmd / linux / blkdiscard.c
 1/* See LICENSE file for copyright and license details. */
 2
 3
 4#include <sys/ioctl.h>
 5#include <sys/mount.h>
 6#include <sys/stat.h>
 7#include <sys/types.h>
 8
 9#include <fcntl.h>
10#include <stdint.h>
11#include <unistd.h>
12
13#include "util.h"
14
15#define OFFSET_IDX	0
16#define LENGTH_IDX	1
17
18#define BLKDISCARD	_IO(0x12, 119)
19
20static void
21usage(void)
22{
23	eprintf("usage: %s device\n", argv0);
24}
25
26// ?man blkdiscard: discard sectors on a device
27// ?man arguments: device
28// ?man discard sectors on a block device
29int
30main(int argc, char *argv[])
31{
32	uint64_t range[2];
33	int fd;
34
35	ARGBEGIN {
36	default:
37		usage();
38	} ARGEND
39
40	if (argc != 1)
41		usage();
42
43	fd = open(argv[0], O_RDWR);
44	if (fd < 0)
45		eprintf("open: %s:", argv[0]);
46	range[OFFSET_IDX] = 0;
47	if (ioctl(fd, (int)BLKGETSIZE64, &range[LENGTH_IDX]) < 0)
48		eprintf("BLKGETSIZE64: %s:", argv[0]);
49	if (ioctl(fd, (int)BLKDISCARD, range) < 0)
50		eprintf("BLKDISCARD: %s:", argv[0]);
51	close(fd);
52	return 0;
53}