summaryrefslogtreecommitdiffstats
path: root/sys-utils/fstrim.c
diff options
context:
space:
mode:
authorKarel Zak2012-09-28 01:10:08 +0200
committerKarel Zak2012-09-28 01:10:08 +0200
commitfce05e9617b72555863153aac9950a844cca573f (patch)
tree1ae94489bdfa3d163eac8f9e5e94c1bb67f39985 /sys-utils/fstrim.c
parentfdisk: add GPT support (diff)
downloadkernel-qcow2-util-linux-fce05e9617b72555863153aac9950a844cca573f.tar.gz
kernel-qcow2-util-linux-fce05e9617b72555863153aac9950a844cca573f.tar.xz
kernel-qcow2-util-linux-fce05e9617b72555863153aac9950a844cca573f.zip
Revert "blkdiscard: new command"
This was only attempt as alternative implementation, unfortunately pushed publicly... sorry. This reverts commit c84ed54cefdcaa07f3d1f40a65434c9d4277da08.
Diffstat (limited to 'sys-utils/fstrim.c')
-rw-r--r--sys-utils/fstrim.c154
1 files changed, 35 insertions, 119 deletions
diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
index cfe2770d2..332601df7 100644
--- a/sys-utils/fstrim.c
+++ b/sys-utils/fstrim.c
@@ -1,7 +1,7 @@
/*
* fstrim.c -- discard the part (or whole) of mounted filesystem.
*
- * Copyright (C) 2010,2012 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2010 Red Hat, Inc. All rights reserved.
* Written by Lukas Czerner <lczerner@redhat.com>
* Karel Zak <kzak@redhat.com>
*
@@ -23,6 +23,7 @@
* online (mounted). You can specify range (start and length) to be
* discarded, or simply discard whole filesystem.
*/
+
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
@@ -40,7 +41,6 @@
#include "strutils.h"
#include "c.h"
#include "closestream.h"
-#include "blkdev.h"
#ifndef FITRIM
struct fstrim_range {
@@ -48,107 +48,34 @@ struct fstrim_range {
uint64_t len;
uint64_t minlen;
};
-# define FITRIM _IOWR('X', 121, struct fstrim_range)
-#endif
-
-#ifndef BLKDISCARD
-# define BLKDISCARD _IO(0x12,119)
-#endif
-#ifndef BLKSECDISCARD
-# define BLKSECDISCARD _IO(0x12,125)
+#define FITRIM _IOWR('X', 121, struct fstrim_range)
#endif
-static int is_blk = 0;
-
static void __attribute__((__noreturn__)) usage(FILE *out)
{
fputs(USAGE_HEADER, out);
-
- if (is_blk) {
- fprintf(out, _(" %s [options] <device>\n"), program_invocation_short_name);
- fputs(USAGE_OPTIONS, out);
- fputs(_(" -s, --secure perform secure discard\n"), out);
- } else {
- fprintf(out, _(" %s [options] <mountpoint>\n"), program_invocation_short_name);
- fputs(USAGE_OPTIONS, out);
- fputs(_(" -m, --minimum <num> minimum extent length to discard\n"), out);
- }
+ fprintf(out,
+ _(" %s [options] <mount point>\n"), program_invocation_short_name);
+ fputs(USAGE_OPTIONS, out);
fputs(_(" -o, --offset <num> offset in bytes to discard from\n"
" -l, --length <num> length of bytes to discard from the offset\n"
+ " -m, --minimum <num> minimum extent length to discard\n"
" -v, --verbose print number of discarded bytes\n"), out);
fputs(USAGE_SEPARATOR, out);
fputs(USAGE_HELP, out);
fputs(USAGE_VERSION, out);
- fprintf(out, USAGE_MAN_TAIL(is_blk ? "blkdiscard(8)" : "fstrim(8)"));
+ fprintf(out, USAGE_MAN_TAIL("fstrim(8)"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
-static void do_fstrim(const char *path, uint64_t off, uint64_t len, uint64_t minlen)
-{
- int fd;
- struct stat sb;
- struct fstrim_range range;
-
- range.start = off;
- range.len = len;
- range.minlen = minlen;
-
- fd = open(path, O_RDONLY);
- if (fd < 0)
- err(EXIT_FAILURE, _("cannot open %s"), path);
- if (fstat(fd, &sb) == -1)
- err(EXIT_FAILURE, _("stat failed %s"), path);
- if (!S_ISDIR(sb.st_mode))
- errx(EXIT_FAILURE, _("%s: not a directory"), path);
- if (ioctl(fd, FITRIM, &range))
- err(EXIT_FAILURE, _("%s: FITRIM ioctl failed"), path);
- close(fd);
-}
-
-static void do_blkdiscard(const char *path, uint64_t off, uint64_t len, int sec)
-{
- int fd, secsize;
- struct stat sb;
- uint64_t blksize, range[2], end;
-
- fd = open(path, O_WRONLY);
- if (fd < 0)
- err(EXIT_FAILURE, _("cannot open %s"), path);
- if (fstat(fd, &sb) == -1)
- err(EXIT_FAILURE, _("stat failed %s"), path);
- if (!S_ISBLK(sb.st_mode))
- errx(EXIT_FAILURE, _("%s: not a block device"), path);
- if (blkdev_get_size(fd, (unsigned long long *) &blksize) != 0)
- err(EXIT_FAILURE, _("%s: failed to get device size"), path);
- if (blkdev_get_sector_size(fd, &secsize) != 0)
- err(EXIT_FAILURE, _("%s: failed to get sector size"), path);
-
- /* align range to the sector size */
- range[0] = (off + secsize - 1) & ~(secsize - 1);
- range[1] = len & ~(secsize - 1);
-
- /* is the range end behind the end of the device ?*/
- end = range[0] + range[1];
- if (end < range[0] || end > blksize)
- range[1] = blksize - range[0];
-
- if (sec) {
- if (ioctl(fd, BLKSECDISCARD, &range))
- err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path);
- } else {
- if (ioctl(fd, BLKDISCARD, &range))
- err(EXIT_FAILURE, _("%s: BLKDISCARD ioctl failed"), path);
- }
- close(fd);
-}
-
int main(int argc, char **argv)
{
char *path;
- int c, verbose = 0, secure = 0;
- uint64_t len = UINT64_MAX, off = 0, minlen = 0;
+ int c, fd, verbose = 0;
+ struct fstrim_range range;
+ struct stat sb;
- static const struct option fs_longopts[] = {
+ static const struct option longopts[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'V' },
{ "offset", 1, 0, 'o' },
@@ -157,32 +84,16 @@ int main(int argc, char **argv)
{ "verbose", 0, 0, 'v' },
{ NULL, 0, 0, 0 }
};
- static const struct option blk_longopts[] = {
- { "help", 0, 0, 'h' },
- { "version", 0, 0, 'V' },
- { "offset", 1, 0, 'o' },
- { "length", 1, 0, 'l' },
- { "secure", 0, 0, 's' },
- { "verbose", 0, 0, 'v' },
- { NULL, 0, 0, 0 }
- };
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
atexit(close_stdout);
- if (strcmp(program_invocation_short_name, "blkdiscard") == 0)
- is_blk = 1;
-
- do {
- if (is_blk)
- c = getopt_long(argc, argv, "hVo:l:sv", blk_longopts, NULL);
- else
- c = getopt_long(argc, argv, "hVo:l:m:v", fs_longopts, NULL);
- if (c == -1)
- break;
+ memset(&range, 0, sizeof(range));
+ range.len = ULLONG_MAX;
+ while ((c = getopt_long(argc, argv, "hVo:l:m:v", longopts, NULL)) != -1) {
switch(c) {
case 'h':
usage(stdout);
@@ -191,20 +102,17 @@ int main(int argc, char **argv)
printf(UTIL_LINUX_VERSION);
return EXIT_SUCCESS;
case 'l':
- len = strtosize_or_err(optarg,
+ range.len = strtosize_or_err(optarg,
_("failed to parse length"));
break;
case 'o':
- off = strtosize_or_err(optarg,
+ range.start = strtosize_or_err(optarg,
_("failed to parse offset"));
break;
case 'm':
- minlen = strtosize_or_err(optarg,
+ range.minlen = strtosize_or_err(optarg,
_("failed to parse minimum extent length"));
break;
- case 's':
- secure = 1;
- break;
case 'v':
verbose = 1;
break;
@@ -212,11 +120,11 @@ int main(int argc, char **argv)
usage(stderr);
break;
}
- } while (1);
+ }
if (optind == argc)
- errx(EXIT_FAILURE, is_blk ? _("no device specified.") :
- _("no mountpoint specified"));
+ errx(EXIT_FAILURE, _("no mountpoint specified."));
+
path = argv[optind++];
if (optind != argc) {
@@ -224,14 +132,22 @@ int main(int argc, char **argv)
usage(stderr);
}
- if (is_blk)
- do_blkdiscard(path, off, len, secure);
- else
- do_fstrim(path, off, len, minlen);
+ if (stat(path, &sb) == -1)
+ err(EXIT_FAILURE, _("stat failed %s"), path);
+ if (!S_ISDIR(sb.st_mode))
+ errx(EXIT_FAILURE, _("%s: not a directory"), path);
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ err(EXIT_FAILURE, _("cannot open %s"), path);
+
+ if (ioctl(fd, FITRIM, &range))
+ err(EXIT_FAILURE, _("%s: FITRIM ioctl failed"), path);
if (verbose)
/* TRANSLATORS: The standard value here is a very large number. */
- printf(_("%s: %" PRIu64 " bytes were trimmed\n"), path, len);
-
+ printf(_("%s: %" PRIu64 " bytes were trimmed\n"),
+ path, (uint64_t) range.len);
+ close(fd);
return EXIT_SUCCESS;
}