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