2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Eugene Grosbein <eugen@FreeBSD.org>.
5 * Contains code written by Alan Somers <asomers@FreeBSD.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/ioctl.h>
47 #include <sys/cdefs.h>
48 static bool candelete(int fd);
49 static off_t getsize(const char *path);
50 static int opendev(const char *path, int flags);
51 static int trim(const char *path, off_t offset, off_t length, bool dryrun, bool verbose);
52 static void usage(const char *name);
55 main(int argc, char **argv)
66 dryrun = verbose = true;
68 while ((ch = getopt(argc, argv, "Nfl:o:qr:v")) != -1)
79 if (expand_number(optarg, &usz) == -1 ||
80 (off_t)usz < 0 || (usz == 0 && ch == 'l'))
82 "invalid %s of the region: %s",
83 ch == 'o' ? "offset" : "length",
94 if ((length = getsize(optarg)) == 0)
96 "invalid zero length reference file"
97 " for the region: %s", optarg);
108 * Safety net: do not allow mistakes like
110 * trim -f /dev/da0 -r rfile
112 * This would trim whole device then error on non-existing file -r.
113 * Following check prevents this while allowing this form still:
115 * trim -f -- /dev/da0 -r rfile
118 if (strcmp(argv[optind-1], "--") != 0) {
119 for (ch = optind; ch < argc; ch++)
120 if (argv[ch][0] == '-')
130 while ((fname = *argv++) != NULL)
131 if (trim(fname, offset, length, dryrun, verbose) < 0)
134 return (error ? EXIT_FAILURE : EXIT_SUCCESS);
140 struct diocgattr_arg arg;
142 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
143 arg.len = sizeof(arg.value.i);
144 if (ioctl(fd, DIOCGATTR, &arg) == 0)
145 return (arg.value.i != 0);
151 opendev(const char *path, int flags)
156 if ((fd = open(path, flags)) < 0) {
157 if (errno == ENOENT && path[0] != '/') {
158 if (asprintf(&tstr, "%s%s", _PATH_DEV, path) < 0)
159 errx(EX_OSERR, "no memory");
160 fd = open(tstr, flags);
166 err(EX_NOINPUT, "open failed: %s", path);
172 getsize(const char *path)
178 fd = opendev(path, O_RDONLY | O_DIRECT);
180 if (fstat(fd, &sb) < 0)
181 err(EX_IOERR, "fstat failed: %s", path);
183 if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode)) {
188 if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
190 "invalid type of the file "
191 "(not regular, directory nor special device): %s",
194 if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0)
196 "ioctl(DIOCGMEDIASIZE) failed, probably not a disk: "
204 trim(const char *path, off_t offset, off_t length, bool dryrun, bool verbose)
210 length = getsize(path);
213 printf("trim %s offset %ju length %ju\n",
214 path, (uintmax_t)offset, (uintmax_t)length);
217 printf("dry run: add -f to actually perform the operation\n");
221 fd = opendev(path, O_RDWR | O_DIRECT);
225 error = ioctl(fd, DIOCGDELETE, arg);
227 if (errno == EOPNOTSUPP && verbose && !candelete(fd))
228 warnx("%s: TRIM/UNMAP not supported by driver", path);
230 warn("ioctl(DIOCGDELETE) failed: %s", path);
237 usage(const char *name)
239 (void)fprintf(stderr,
240 "usage: %s [-[lo] offset[K|k|M|m|G|g|T|t]] [-r rfile] [-Nfqv] device ...\n",