Page MenuHomeFreeBSD

D18380.id51361.diff
No OneTemporary

D18380.id51361.diff

Index: usr.sbin/trim/trim.8
===================================================================
--- usr.sbin/trim/trim.8
+++ usr.sbin/trim/trim.8
@@ -33,7 +33,7 @@
.Nd erase device blocks that have no needed contents
.Sh SYNOPSIS
.Nm
-.Op Fl Nfqv
+.Op Fl Nv
.Fl [ [lo] Xo
.Bk -words
.Sm off
@@ -50,21 +50,14 @@
The
.Nm
utility erases specified region of the device.
-It is only relevant for flash based storage devices that use wear-leveling
-algorithms.
+It is only relevant for storage that implement trim (like flash based,
+or thinly provisioned storage).
.Sy All erased data is lost.
.Pp
The following options are available:
.Bl -tag -width indent
.It Fl N
Do not actually erase anything but show what it would do (dry run).
-Implies
-.Fl v .
-This is the default. Overrides
-.Fl f .
-.It Fl f
-Perform the operation. Overrides
-.Fl N .
.It Fl l Xo
.Sm off
.Ar offset
@@ -94,17 +87,11 @@
(either upper or lower case) to indicate a multiple of
Kilobytes, Megabytes, Gigabytes or Terabytes
respectively.
-.It Fl q
-Do not output anything except of possible error messages (quiet mode).
-Overrides
-.Fl v .
.It Fl r Ar rfile
Uses the length of given
.Ar rfile
as length of the region to erase.
.Sy The whole device is erased by default.
-.It Fl v
-Show offset and length of actual region being erased, in bytes.
.El
.Pp
Later options override previous ones.
@@ -153,12 +140,13 @@
.Xr ada 4 ,
.Xr da 4 ,
.Xr ioctl 2 ,
+.Xr nda 4 ,
.Xr sysexits 3
.Sh HISTORY
The
.Nm
utility first appeared in
-.Fx 12.0 .
+.Fx 12.1 .
.Sh AUTHORS
The
.Nm
Index: usr.sbin/trim/trim.c
===================================================================
--- usr.sbin/trim/trim.c
+++ usr.sbin/trim/trim.c
@@ -42,14 +42,12 @@
#include <sysexits.h>
#include <unistd.h>
-#ifndef lint
-static const char rcsid[] =
- "$FreeBSD$";
-#endif /* not lint */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
static int trim(char *path, off_t offset, off_t length, int dryrun, int verbose);
static off_t getsize(char *path);
-static void usage(char *name) __dead2;
+static void usage(char *name);
int
main(int argc, char **argv)
@@ -62,22 +60,18 @@
error = 0;
length = offset = 0;
name = argv[0];
- dryrun = verbose = 1;
+ dryrun = verbose = 0;
- while ((ch = getopt(argc, argv, "Nfl:o:qr:v")) != -1)
+ while ((ch = getopt(argc, argv, "Nl:o:r:v")) != -1)
switch (ch) {
case 'N':
dryrun = 1;
verbose = 1;
break;
- case 'f':
- dryrun = 0;
- break;
case 'l':
case 'o':
if (expand_number(optarg, &usz) == -1 ||
- (off_t)usz < 0 ||
- (usz == 0 && ch == 'l'))
+ (off_t)usz < 0 || (usz == 0 && ch == 'l'))
errx(EX_USAGE,
"invalid %s of the region: `%s'",
ch == 'o' ? "offset" : "length",
@@ -87,9 +81,6 @@
else
length = (off_t)usz;
break;
- case 'q':
- verbose = 0;
- break;
case 'r':
if ((length = getsize(optarg)) == 0)
errx(EX_USAGE,
@@ -117,15 +108,13 @@
return (error ? EXIT_FAILURE : EXIT_SUCCESS);
}
-static off_t
-getsize(char *path)
+static int
+opendev(char *path, int flags)
{
- struct stat sb;
char *tstr;
- off_t mediasize;
int fd;
- if ((fd = open(path, O_RDONLY | O_DIRECT)) < 0) {
+ if ((fd = open(path, flags)) < 0) {
if (errno == ENOENT && path[0] != '/') {
if (asprintf(&tstr, "%s%s", _PATH_DEV, path) < 0)
errx(EX_OSERR, "no memory");
@@ -133,28 +122,32 @@
free(tstr);
}
}
-
if (fd < 0)
- err(EX_NOINPUT, "`%s'", path);
+ err(EX_NOINPUT, "open failed: %s", path);
+ return (fd);
+}
+
+static off_t
+getsize(char *path)
+{
+ struct stat sb;
+ off_t mediasize;
+ int fd;
+
+ fd = opendev(path, O_RDONLY | O_DIRECT);
if (fstat(fd, &sb) < 0)
- err(EX_IOERR, "`%s'", path);
+ err(EX_IOERR, "fstat failed: %s", path);
if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode)) {
close(fd);
return (sb.st_size);
}
- if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
- errx(EX_DATAERR,
- "invalid type of the file "
- "(not regular, directory nor special device): `%s'",
- path);
-
if (ioctl(fd, DIOCGMEDIASIZE, &mediasize) < 0)
- errx(EX_UNAVAILABLE,
- "ioctl(DIOCGMEDIASIZE) failed, probably not a disk: "
- "`%s'", path);
+ err(EX_UNAVAILABLE,
+ "ioctl(DIOCGMEDIASIZE) failed, probably not a disk: %s",
+ path);
close(fd);
return (mediasize);
@@ -164,7 +157,6 @@
trim(char *path, off_t offset, off_t length, int dryrun, int verbose)
{
off_t arg[2];
- char *tstr;
int error, fd;
if (length == 0)
@@ -172,31 +164,20 @@
if (verbose)
printf("trim `%s' offset %ju length %ju\n",
- path, (uintmax_t)offset, (uintmax_t)length);
+ path, (uintmax_t)offset, (uintmax_t)length);
if (dryrun) {
- printf("dry run: add -f to actually perform the operation\n");
+ printf("dry run: remove -N to actually perform the operation\n");
return (0);
}
- if ((fd = open(path, O_WRONLY | O_DIRECT)) < 0) {
- if (errno == ENOENT && path[0] != '/') {
- if (asprintf(&tstr, "%s%s", _PATH_DEV, path) < 0)
- errx(EX_OSERR, "no memory");
- fd = open(tstr, O_WRONLY | O_DIRECT);
- free(tstr);
- }
- }
-
- if (fd < 0)
- err(EX_NOINPUT, "`%s'", path);
+ fd = opendev(path, O_WRONLY | O_DIRECT);
arg[0] = offset;
arg[1] = length;
-
error = ioctl(fd, DIOCGDELETE, arg);
if (error < 0)
- warn("ioctl(DIOCGDELETE) failed for `%s'", path);
+ warn("ioctl(DIOCGDELETE) failed for %s", path);
close(fd);
return (error);

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 12, 1:41 PM (1 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
29579259
Default Alt Text
D18380.id51361.diff (5 KB)

Event Timeline