From: "Darrick J. Wong" <darrick.wong@oracle.com>
To: sandeen@redhat.com, darrick.wong@oracle.com
Cc: linux-xfs@vger.kernel.org, Dave Chinner <dchinner@redhat.com>
Subject: [PATCH 08/12] xfs_spaceman: add FITRIM support
Date: Thu, 15 Jun 2017 13:36:38 -0700 [thread overview]
Message-ID: <149755899832.3625.846462761036158527.stgit@birch.djwong.org> (raw)
In-Reply-To: <149755894900.3625.8076720525369136771.stgit@birch.djwong.org>
From: Dave Chinner <dchinner@redhat.com>
Add support for discarding free space extents via the FITRIM
command. Make it easy to discard a single range, an entire AG or all
the freespace in the filesystem.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
spaceman/Makefile | 2 -
spaceman/init.c | 1
spaceman/space.h | 1
spaceman/trim.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 134 insertions(+), 1 deletion(-)
create mode 100644 spaceman/trim.c
diff --git a/spaceman/Makefile b/spaceman/Makefile
index df59edf..c63a4fc 100644
--- a/spaceman/Makefile
+++ b/spaceman/Makefile
@@ -7,7 +7,7 @@ include $(TOPDIR)/include/builddefs
LTCOMMAND = xfs_spaceman
HFILES = init.h space.h
-CFILES = init.c file.c
+CFILES = init.c file.c trim.c
LLDLIBS = $(LIBXCMD)
LTDEPENDENCIES = $(LIBXCMD)
diff --git a/spaceman/init.c b/spaceman/init.c
index 8cbfbda..107a692 100644
--- a/spaceman/init.c
+++ b/spaceman/init.c
@@ -40,6 +40,7 @@ init_commands(void)
print_init();
help_init();
quit_init();
+ trim_init();
}
static int
diff --git a/spaceman/space.h b/spaceman/space.h
index 7bfe874..8cd3953 100644
--- a/spaceman/space.h
+++ b/spaceman/space.h
@@ -34,5 +34,6 @@ extern int addfile(char *, int , xfs_fsop_geom_t *);
extern void print_init(void);
extern void help_init(void);
extern void quit_init(void);
+extern void trim_init(void);
#endif /* XFS_SPACEMAN_SPACE_H_ */
diff --git a/spaceman/trim.c b/spaceman/trim.c
new file mode 100644
index 0000000..c0bc7f2
--- /dev/null
+++ b/spaceman/trim.c
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2012 Red Hat, Inc.
+ * All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libxfs.h"
+#include <linux/fs.h>
+#include "command.h"
+#include "init.h"
+#include "space.h"
+#include "input.h"
+
+static cmdinfo_t trim_cmd;
+
+/*
+ * Trim unused space in xfs filesystem.
+ */
+static int
+trim_f(
+ int argc,
+ char **argv)
+{
+ struct fstrim_range trim = {0};
+ xfs_agnumber_t agno = 0;
+ off64_t offset = 0;
+ ssize_t length = 0;
+ ssize_t minlen = 0;
+ int aflag = 0;
+ int fflag = 0;
+ int ret;
+ int c;
+
+ while ((c = getopt(argc, argv, "a:fm:")) != EOF) {
+ switch (c) {
+ case 'a':
+ aflag = 1;
+ agno = cvt_u32(optarg, 10);
+ if (errno) {
+ printf(_("bad agno value %s\n"), optarg);
+ return command_usage(&trim_cmd);
+ }
+ break;
+ case 'f':
+ fflag = 1;
+ break;
+ case 'm':
+ minlen = cvtnum(file->geom.blocksize,
+ file->geom.sectsize, optarg);
+ break;
+ default:
+ return command_usage(&trim_cmd);
+ }
+ }
+
+ if (aflag && fflag)
+ return command_usage(&trim_cmd);
+
+ if (optind != argc - 2 && !(aflag || fflag))
+ return command_usage(&trim_cmd);
+ if (optind != argc) {
+ offset = cvtnum(file->geom.blocksize, file->geom.sectsize,
+ argv[optind]);
+ length = cvtnum(file->geom.blocksize, file->geom.sectsize,
+ argv[optind + 1]);
+ } else if (agno) {
+ offset = agno * file->geom.agblocks * file->geom.blocksize;
+ length = file->geom.agblocks * file->geom.blocksize;
+ } else {
+ offset = 0;
+ length = file->geom.datablocks * file->geom.blocksize;
+ }
+
+ trim.start = offset;
+ trim.len = length;
+ trim.minlen = minlen;
+
+ ret = ioctl(file->fd, FITRIM, (unsigned long)&trim);
+ if (ret < 0) {
+ fprintf(stderr, "%s: ioctl(FITRIM) [\"%s\"]: "
+ "%s\n", progname, file->name, strerror(errno));
+ exitcode = 1;
+ }
+ return 0;
+}
+
+static void
+trim_help(void)
+{
+ printf(_(
+"\n"
+"Discard filesystem free space\n"
+"\n"
+" -a agno -- trim all the freespace in the given AG agno\n"
+" -f -- trim all the freespace in the entire filesystem\n"
+" offset length -- trim the freespace in the range {offset, length}\n"
+" -m minlen -- skip freespace extents smaller than minlen\n"
+"\n"
+"One of -a, -f, or the offset/length pair are required.\n"
+"\n"));
+
+}
+
+void
+trim_init(void)
+{
+ trim_cmd.name = "trim";
+ trim_cmd.altname = "tr";
+ trim_cmd.cfunc = trim_f;
+ trim_cmd.argmin = 1;
+ trim_cmd.argmax = 4;
+ trim_cmd.args = "[-m minlen] ( -a agno | -f | offset length )";
+ trim_cmd.flags = CMD_FLAG_ONESHOT;
+ trim_cmd.oneline = _("Discard filesystem free space");
+ trim_cmd.help = trim_help;
+
+ add_command(&trim_cmd);
+}
+
next prev parent reply other threads:[~2017-06-15 20:36 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-15 20:35 [PATCH v9 00/12] xfsprogs 4.12: GETFSMAP support Darrick J. Wong
2017-06-15 20:35 ` [PATCH 01/12] xfs_io: refactor numlen into a library function Darrick J. Wong
2017-06-21 20:40 ` Eric Sandeen
2017-06-15 20:36 ` [PATCH 02/12] libxcmd: add cvt{int, long} to convert strings to int and long Darrick J. Wong
2017-06-21 20:16 ` Eric Sandeen
2017-06-21 20:29 ` Darrick J. Wong
2017-06-21 20:33 ` Eric Sandeen
2017-06-21 20:38 ` Darrick J. Wong
2017-06-21 21:19 ` [PATCH v2 " Darrick J. Wong
2017-06-15 20:36 ` [PATCH 03/12] libxfs: use crc32c slice-by-8 variant by default Darrick J. Wong
2017-06-21 20:42 ` Eric Sandeen
2017-06-15 20:36 ` [PATCH 04/12] xfs: introduce the XFS_IOC_GETFSMAP ioctl Darrick J. Wong
2017-06-21 20:43 ` Eric Sandeen
2017-06-15 20:36 ` [PATCH 05/12] xfs_io: support the new getfsmap ioctl Darrick J. Wong
2017-06-21 20:51 ` Eric Sandeen
2017-06-21 20:54 ` Darrick J. Wong
2017-06-15 20:36 ` [PATCH 06/12] xfs_repair: replace rmap_compare with libxfs version Darrick J. Wong
2017-06-15 20:36 ` [PATCH 07/12] xfs_spaceman: space management tool Darrick J. Wong
2017-06-21 21:12 ` Eric Sandeen
2017-06-15 20:36 ` Darrick J. Wong [this message]
2017-06-21 21:21 ` [PATCH 08/12] xfs_spaceman: add FITRIM support Eric Sandeen
2017-06-15 20:36 ` [PATCH 09/12] xfs_spaceman: add new speculative prealloc control Darrick J. Wong
2017-06-21 21:26 ` Eric Sandeen
2017-06-15 20:36 ` [PATCH 10/12] xfs_spaceman: Free space mapping command Darrick J. Wong
2017-06-21 21:32 ` Eric Sandeen
2017-06-15 20:36 ` [PATCH 11/12] xfs_spaceman: add a man page Darrick J. Wong
2017-06-21 21:45 ` Eric Sandeen
2017-06-15 20:37 ` [PATCH 12/12] xfs_spaceman: add group summary mode Darrick J. Wong
2017-06-21 21:53 ` Eric Sandeen
2017-06-21 21:58 ` Darrick J. Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=149755899832.3625.846462761036158527.stgit@birch.djwong.org \
--to=darrick.wong@oracle.com \
--cc=dchinner@redhat.com \
--cc=linux-xfs@vger.kernel.org \
--cc=sandeen@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).