All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Add test 248: Check filesystem FITRIM implementation
@ 2010-11-26 14:12 Lukas Czerner
  2010-12-06 10:20 ` Lukas Czerner
  2010-12-09  5:15 ` Eric Sandeen
  0 siblings, 2 replies; 12+ messages in thread
From: Lukas Czerner @ 2010-11-26 14:12 UTC (permalink / raw)
  To: xfs; +Cc: hch, lczerner, esandeen

FITRIM ioctl  is used on a mounted filesystem to discard (or "trim")
blocks which are not in use by the filesystem.  This is useful for
solid-state drives (SSDs) and thinly-provi-sioned storage. This test
helps to verify filesystem FITRIM implementation to assure that it
does not corrupts data.

This test creates checksums of all files in /usr/share/doc directory and
run several processes which clear its working directory on SCRATCH_MNT,
then copy everything from /usr/share/doc into its working directory, create
list of files in working directory and its checksums and compare it with the
original list of checksums. Every process works in the loop so it repeat
remove->copy->check, while fstrim tool is running simultaneously.

Fstrim is just a helper tool which uses FITRIM ioctl to actually do the
filesystem discard.

I found this very useful because when the FITRIM is really buggy (thus
data-destroying) the 248 test will notice, because checksums will most
likely change.

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
---
 248          |  163 +++++++++++++++++++++++++++++++++++++
 248.out      |    3 +
 group        |    1 +
 src/Makefile |    2 +-
 src/fstrim.c |  257 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 425 insertions(+), 1 deletions(-)
 create mode 100755 248
 create mode 100644 248.out
 create mode 100644 src/fstrim.c

diff --git a/248 b/248
new file mode 100755
index 0000000..0d2f17f
--- /dev/null
+++ b/248
@@ -0,0 +1,163 @@
+#!/bin/bash
+# FS QA Test No. 248
+#
+# This test was created in order to verify filesystem FITRIM implementation.
+# By many concurrent copy and remove operations and checking that files
+# does not change after copied into SCRATCH_MNT test if FITIM implementation
+# corrupts the filesystem (data/metadata).
+#
+#-----------------------------------------------------------------------
+# Copyright 2010 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
+#
+# 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
+#-----------------------------------------------------------------------
+
+owner=lczerner@redhat.com
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=`mktemp -d`
+status=1    # failure is the default!
+trap "_cleanup; exit \$status" 0 1 3
+trap "_destroy; exit \$status" 2 15
+chpid=0
+mypid=$$
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+# real QA test starts here
+_supported_fs generic
+_supported_os Linux
+_require_scratch
+_scratch_mkfs >/dev/null 2>&1
+_scratch_mount
+
+_cleanup()
+{
+	rm -rf $tmp
+}
+
+_destroy()
+{
+	kill $pids $fstrim_pid
+	wait $pids $fstrim_pid
+	rm -rf $tmp
+}
+
+_destroy_fstrim()
+{
+	kill $fpid
+	wait $fpid
+}
+
+fstrim_loop()
+{
+	trap "_destroy_fstrim; exit \$status" 2 15
+	fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
+
+	while true ; do
+		step=1048576
+		start=0
+		$here/src/fstrim $SCRATCH_MNT &
+		fpid=$!
+		wait $fpid
+		while [ $start -lt $fsize ] ; do
+			$here/src/fstrim -s ${start}k -l ${step}k $SCRATCH_MNT &
+			fpid=$!
+			wait $fpid
+			start=$(( $start + $step ))
+		done
+	done
+}
+
+function check_sums() {
+	dir=$1
+
+	(
+	cd $SCRATCH_MNT/$p
+	find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
+	)
+
+	diff $tmp/content.sums $tmp/stress.$$.$p
+	if [ $? -ne 0 ]; then
+		echo "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
+		kill $mypid
+	fi
+	rm -f $tmp/stress.$$.$p
+}
+
+function run_process() {
+	local p=$1
+	repeat=10
+
+	sleep $((5*$p))s &
+	export chpid=$! && wait $chpid &> /dev/null
+	chpid=0
+
+	while [ $repeat -gt 0 ]; do
+
+		# Remove old directories.
+		rm -rf $SCRATCH_MNT/$p
+		export chpid=$! && wait $chpid &> /dev/null
+
+		# Copy content -> partition.
+		mkdir $SCRATCH_MNT/$p
+		cp -ax $content/* $SCRATCH_MNT/$p
+		export chpid=$! && wait $chpid &> /dev/null
+
+		check_sums
+		repeat=$(( $repeat - 1 ))
+	done
+}
+
+nproc=20
+content=/usr/share/doc
+
+# Check for FITRIM support
+echo -n "Checking FITRIM support: "
+$here/src/fstrim -l 10M $SCRATCH_MNT
+[ $? -ne 0 ] && exit
+echo "done."
+
+mkdir -p $tmp
+
+(
+cd $content
+find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
+)
+
+echo -n "Running the test: "
+pids=""
+fstrim_loop &
+fstrim_pid=$!
+p=1
+while [ $p -le $nproc ]; do
+	run_process $p &
+	pids="$pids $!"
+	p=$(($p+1))
+done
+echo "done."
+
+wait $pids
+kill $fstrim_pid
+wait $fstrim_pid
+
+status=0
+_check_scratch_fs
+
+exit
diff --git a/248.out b/248.out
new file mode 100644
index 0000000..880d9c7
--- /dev/null
+++ b/248.out
@@ -0,0 +1,3 @@
+QA output created by 248
+Checking FITRIM support: done.
+Running the test: done.
diff --git a/group b/group
index 0f94dd9..fea84ed 100644
--- a/group
+++ b/group
@@ -361,3 +361,4 @@ deprecated
 245 auto quick dir
 246 auto quick rw
 247 auto quick rw
+248 ioctl
diff --git a/src/Makefile b/src/Makefile
index b827bd0..885fd65 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -17,7 +17,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
 	locktest unwritten_mmap bulkstat_unlink_test t_stripealign \
 	bulkstat_unlink_test_modified t_dir_offset t_futimens t_immutable \
-	stale_handle
+	stale_handle fstrim
 
 SUBDIRS =
 
diff --git a/src/fstrim.c b/src/fstrim.c
new file mode 100644
index 0000000..6686c99
--- /dev/null
+++ b/src/fstrim.c
@@ -0,0 +1,257 @@
+/*
+ * fstrim.c -- discard the part (or whole) of mounted filesystem.
+ *
+ * Copyright (C) 2009 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
+ *
+ * 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, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>.
+ *
+ * This program uses FITRIM ioctl to discard parts or the whole filesystem
+ * online (mounted). You can specify range (start and lenght) to be
+ * discarded, or simply discard while filesystem.
+ *
+ * Usage: fstrim [options] <mount point>
+ */
+
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <getopt.h>
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <linux/fs.h>
+
+#ifndef FITRIM
+struct fstrim_range {
+	uint64_t start;
+	uint64_t len;
+	uint64_t minlen;
+};
+#define FITRIM		_IOWR('X', 121, struct fstrim_range)
+#endif
+
+const char *program_name = "fstrim";
+
+struct options {
+	struct fstrim_range *range;
+	char mpoint[PATH_MAX];
+	char verbose;
+};
+
+static void usage(void)
+{
+	fprintf(stderr,
+		"Usage: %s [-s start] [-l length] [-m minimum-extent]"
+		" [-v] {mountpoint}\n\t"
+		"-s Starting Byte to discard from\n\t"
+		"-l Number of Bytes to discard from the start\n\t"
+		"-m Minimum extent length to discard\n\t"
+		"-v Verbose - number of discarded bytes\n",
+		program_name);
+}
+
+static void err_exit(const char *fmt, ...)
+{
+	va_list pvar;
+	va_start(pvar, fmt);
+	vfprintf(stderr, fmt, pvar);
+	va_end(pvar);
+	usage();
+	exit(EXIT_FAILURE);
+}
+
+/**
+ * Get the number from argument. It can be number followed by
+ * units: k|K, m|M, g|G, t|T
+ */
+static unsigned long long get_number(char **optarg)
+{
+	char *opt, *end;
+	unsigned long long number, max;
+
+	/* get the max to avoid overflow */
+	max = ULLONG_MAX / 1024;
+	number = 0;
+	opt = *optarg;
+
+	if (*opt == '-') {
+		err_exit("%s: %s (%s)\n", program_name,
+			 strerror(ERANGE), *optarg);
+	}
+
+	errno = 0;
+	number = strtoul(opt, &end , 0);
+	if (errno)
+		err_exit("%s: %s (%s)\n", program_name,
+			 strerror(errno), *optarg);
+
+	/*
+	 * Convert units to numbers. Fall-through stack is used for units
+	 * so absence of breaks is intentional.
+	 */
+	switch (*end) {
+	case 'T': /* terabytes */
+	case 't':
+		if (number > max)
+			err_exit("%s: %s (%s)\n", program_name,
+				 strerror(ERANGE), *optarg);
+		number *= 1024;
+	case 'G': /* gigabytes */
+	case 'g':
+		if (number > max)
+			err_exit("%s: %s (%s)\n", program_name,
+				 strerror(ERANGE), *optarg);
+		number *= 1024;
+	case 'M': /* megabytes */
+	case 'm':
+		if (number > max)
+			err_exit("%s: %s (%s)\n", program_name,
+				 strerror(ERANGE), *optarg);
+		number *= 1024;
+	case 'K': /* kilobytes */
+	case 'k':
+		if (number > max)
+			err_exit("%s: %s (%s)\n", program_name,
+				 strerror(ERANGE), *optarg);
+		number *= 1024;
+		end++;
+	case '\0': /* end of the string */
+		break;
+	default:
+		err_exit("%s: %s (%s)\n", program_name,
+			 strerror(EINVAL), *optarg);
+		return 0;
+	}
+
+	if (*end != '\0') {
+		err_exit("%s: %s (%s)\n", program_name,
+			 strerror(EINVAL), *optarg);
+	}
+
+	return number;
+}
+
+static int parse_opts(int argc, char **argv, struct options *opts)
+{
+	int c;
+
+	while ((c = getopt(argc, argv, "s:l:m:v")) != EOF) {
+		switch (c) {
+		case 's': /* starting point */
+			opts->range->start = get_number(&optarg);
+			break;
+		case 'l': /* length */
+			opts->range->len = get_number(&optarg);
+			break;
+		case 'm': /* minlen */
+			opts->range->minlen = get_number(&optarg);
+			break;
+		case 'v': /* verbose */
+			opts->verbose = 1;
+			break;
+		default:
+			return EXIT_FAILURE;
+		}
+	}
+
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	struct options *opts;
+	struct stat sb;
+	int fd, err = 0, ret = EXIT_FAILURE;
+
+	opts = malloc(sizeof(struct options));
+	if (!opts)
+		err_exit("%s: malloc(): %s\n", program_name, strerror(errno));
+
+	opts->range = NULL;
+	opts->verbose = 0;
+
+	if (argc > 1)
+		strncpy(opts->mpoint, argv[argc - 1], sizeof(opts->mpoint));
+
+	opts->range = calloc(1, sizeof(struct fstrim_range));
+	if (!opts->range) {
+		fprintf(stderr, "%s: calloc(): %s\n", program_name,
+			strerror(errno));
+		goto free_opts;
+	}
+
+	opts->range->len = ULLONG_MAX;
+
+	if (argc > 2)
+		err = parse_opts(argc, argv, opts);
+
+	if (err) {
+		usage();
+		goto free_opts;
+	}
+
+	if (strnlen(opts->mpoint, 1) < 1) {
+		fprintf(stderr, "%s: You have to specify mount point.\n",
+			program_name);
+		usage();
+		goto free_opts;
+	}
+
+	if (stat(opts->mpoint, &sb) == -1) {
+		fprintf(stderr, "%s: %s: %s\n", program_name,
+			opts->mpoint, strerror(errno));
+		usage();
+		goto free_opts;
+	}
+
+	if (!S_ISDIR(sb.st_mode)) {
+		fprintf(stderr, "%s: %s: (%s)\n", program_name,
+			opts->mpoint, strerror(ENOTDIR));
+		usage();
+		goto free_opts;
+	}
+
+	fd = open(opts->mpoint, O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, "%s: open(%s): %s\n", program_name,
+			opts->mpoint, strerror(errno));
+		goto free_opts;
+	}
+
+	if (ioctl(fd, FITRIM, opts->range)) {
+		fprintf(stderr, "%s: FSTRIM: %s\n", program_name,
+			strerror(errno));
+		goto free_opts;
+	}
+
+	if ((opts->verbose) && (opts->range))
+		fprintf(stdout, "%llu Bytes was trimmed\n", opts->range->len);
+
+	ret = EXIT_SUCCESS;
+
+free_opts:
+	if (opts) {
+		if (opts->range)
+			free(opts->range);
+		free(opts);
+	}
+
+	return ret;
+}
-- 
1.7.2.3

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-11-26 14:12 [PATCH] Add test 248: Check filesystem FITRIM implementation Lukas Czerner
@ 2010-12-06 10:20 ` Lukas Czerner
  2010-12-09  5:15 ` Eric Sandeen
  1 sibling, 0 replies; 12+ messages in thread
From: Lukas Czerner @ 2010-12-06 10:20 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: hch, esandeen, xfs

On Fri, 26 Nov 2010, Lukas Czerner wrote:

> FITRIM ioctl  is used on a mounted filesystem to discard (or "trim")
> blocks which are not in use by the filesystem.  This is useful for
> solid-state drives (SSDs) and thinly-provi-sioned storage. This test
> helps to verify filesystem FITRIM implementation to assure that it
> does not corrupts data.
> 
> This test creates checksums of all files in /usr/share/doc directory and
> run several processes which clear its working directory on SCRATCH_MNT,
> then copy everything from /usr/share/doc into its working directory, create
> list of files in working directory and its checksums and compare it with the
> original list of checksums. Every process works in the loop so it repeat
> remove->copy->check, while fstrim tool is running simultaneously.
> 
> Fstrim is just a helper tool which uses FITRIM ioctl to actually do the
> filesystem discard.
> 
> I found this very useful because when the FITRIM is really buggy (thus
> data-destroying) the 248 test will notice, because checksums will most
> likely change.

Hello, what is the status of this patch ? It would be nice to get some
response before someone add a new test and the patch becomes obsolete
:).

Thanks!

-Lukas

> 
> Signed-off-by: Lukas Czerner <lczerner@redhat.com>
> ---
>  248          |  163 +++++++++++++++++++++++++++++++++++++
>  248.out      |    3 +
>  group        |    1 +
>  src/Makefile |    2 +-
>  src/fstrim.c |  257 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 425 insertions(+), 1 deletions(-)
>  create mode 100755 248
>  create mode 100644 248.out
>  create mode 100644 src/fstrim.c
> 
> diff --git a/248 b/248
> new file mode 100755
> index 0000000..0d2f17f
> --- /dev/null
> +++ b/248
> @@ -0,0 +1,163 @@
> +#!/bin/bash
> +# FS QA Test No. 248
> +#
> +# This test was created in order to verify filesystem FITRIM implementation.
> +# By many concurrent copy and remove operations and checking that files
> +# does not change after copied into SCRATCH_MNT test if FITIM implementation
> +# corrupts the filesystem (data/metadata).
> +#
> +#-----------------------------------------------------------------------
> +# Copyright 2010 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
> +#
> +# 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
> +#-----------------------------------------------------------------------
> +
> +owner=lczerner@redhat.com
> +
> +seq=`basename $0`
> +echo "QA output created by $seq"
> +
> +here=`pwd`
> +tmp=`mktemp -d`
> +status=1    # failure is the default!
> +trap "_cleanup; exit \$status" 0 1 3
> +trap "_destroy; exit \$status" 2 15
> +chpid=0
> +mypid=$$
> +
> +# get standard environment, filters and checks
> +. ./common.rc
> +. ./common.filter
> +
> +# real QA test starts here
> +_supported_fs generic
> +_supported_os Linux
> +_require_scratch
> +_scratch_mkfs >/dev/null 2>&1
> +_scratch_mount
> +
> +_cleanup()
> +{
> +	rm -rf $tmp
> +}
> +
> +_destroy()
> +{
> +	kill $pids $fstrim_pid
> +	wait $pids $fstrim_pid
> +	rm -rf $tmp
> +}
> +
> +_destroy_fstrim()
> +{
> +	kill $fpid
> +	wait $fpid
> +}
> +
> +fstrim_loop()
> +{
> +	trap "_destroy_fstrim; exit \$status" 2 15
> +	fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
> +
> +	while true ; do
> +		step=1048576
> +		start=0
> +		$here/src/fstrim $SCRATCH_MNT &
> +		fpid=$!
> +		wait $fpid
> +		while [ $start -lt $fsize ] ; do
> +			$here/src/fstrim -s ${start}k -l ${step}k $SCRATCH_MNT &
> +			fpid=$!
> +			wait $fpid
> +			start=$(( $start + $step ))
> +		done
> +	done
> +}
> +
> +function check_sums() {
> +	dir=$1
> +
> +	(
> +	cd $SCRATCH_MNT/$p
> +	find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
> +	)
> +
> +	diff $tmp/content.sums $tmp/stress.$$.$p
> +	if [ $? -ne 0 ]; then
> +		echo "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
> +		kill $mypid
> +	fi
> +	rm -f $tmp/stress.$$.$p
> +}
> +
> +function run_process() {
> +	local p=$1
> +	repeat=10
> +
> +	sleep $((5*$p))s &
> +	export chpid=$! && wait $chpid &> /dev/null
> +	chpid=0
> +
> +	while [ $repeat -gt 0 ]; do
> +
> +		# Remove old directories.
> +		rm -rf $SCRATCH_MNT/$p
> +		export chpid=$! && wait $chpid &> /dev/null
> +
> +		# Copy content -> partition.
> +		mkdir $SCRATCH_MNT/$p
> +		cp -ax $content/* $SCRATCH_MNT/$p
> +		export chpid=$! && wait $chpid &> /dev/null
> +
> +		check_sums
> +		repeat=$(( $repeat - 1 ))
> +	done
> +}
> +
> +nproc=20
> +content=/usr/share/doc
> +
> +# Check for FITRIM support
> +echo -n "Checking FITRIM support: "
> +$here/src/fstrim -l 10M $SCRATCH_MNT
> +[ $? -ne 0 ] && exit
> +echo "done."
> +
> +mkdir -p $tmp
> +
> +(
> +cd $content
> +find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
> +)
> +
> +echo -n "Running the test: "
> +pids=""
> +fstrim_loop &
> +fstrim_pid=$!
> +p=1
> +while [ $p -le $nproc ]; do
> +	run_process $p &
> +	pids="$pids $!"
> +	p=$(($p+1))
> +done
> +echo "done."
> +
> +wait $pids
> +kill $fstrim_pid
> +wait $fstrim_pid
> +
> +status=0
> +_check_scratch_fs
> +
> +exit
> diff --git a/248.out b/248.out
> new file mode 100644
> index 0000000..880d9c7
> --- /dev/null
> +++ b/248.out
> @@ -0,0 +1,3 @@
> +QA output created by 248
> +Checking FITRIM support: done.
> +Running the test: done.
> diff --git a/group b/group
> index 0f94dd9..fea84ed 100644
> --- a/group
> +++ b/group
> @@ -361,3 +361,4 @@ deprecated
>  245 auto quick dir
>  246 auto quick rw
>  247 auto quick rw
> +248 ioctl
> diff --git a/src/Makefile b/src/Makefile
> index b827bd0..885fd65 100644
> --- a/src/Makefile
> +++ b/src/Makefile
> @@ -17,7 +17,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
>  	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
>  	locktest unwritten_mmap bulkstat_unlink_test t_stripealign \
>  	bulkstat_unlink_test_modified t_dir_offset t_futimens t_immutable \
> -	stale_handle
> +	stale_handle fstrim
>  
>  SUBDIRS =
>  
> diff --git a/src/fstrim.c b/src/fstrim.c
> new file mode 100644
> index 0000000..6686c99
> --- /dev/null
> +++ b/src/fstrim.c
> @@ -0,0 +1,257 @@
> +/*
> + * fstrim.c -- discard the part (or whole) of mounted filesystem.
> + *
> + * Copyright (C) 2009 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
> + *
> + * 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, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>.
> + *
> + * This program uses FITRIM ioctl to discard parts or the whole filesystem
> + * online (mounted). You can specify range (start and lenght) to be
> + * discarded, or simply discard while filesystem.
> + *
> + * Usage: fstrim [options] <mount point>
> + */
> +
> +#include <string.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <errno.h>
> +#include <stdio.h>
> +#include <stdint.h>
> +#include <fcntl.h>
> +#include <limits.h>
> +#include <stdarg.h>
> +#include <getopt.h>
> +
> +#include <sys/ioctl.h>
> +#include <sys/stat.h>
> +#include <linux/fs.h>
> +
> +#ifndef FITRIM
> +struct fstrim_range {
> +	uint64_t start;
> +	uint64_t len;
> +	uint64_t minlen;
> +};
> +#define FITRIM		_IOWR('X', 121, struct fstrim_range)
> +#endif
> +
> +const char *program_name = "fstrim";
> +
> +struct options {
> +	struct fstrim_range *range;
> +	char mpoint[PATH_MAX];
> +	char verbose;
> +};
> +
> +static void usage(void)
> +{
> +	fprintf(stderr,
> +		"Usage: %s [-s start] [-l length] [-m minimum-extent]"
> +		" [-v] {mountpoint}\n\t"
> +		"-s Starting Byte to discard from\n\t"
> +		"-l Number of Bytes to discard from the start\n\t"
> +		"-m Minimum extent length to discard\n\t"
> +		"-v Verbose - number of discarded bytes\n",
> +		program_name);
> +}
> +
> +static void err_exit(const char *fmt, ...)
> +{
> +	va_list pvar;
> +	va_start(pvar, fmt);
> +	vfprintf(stderr, fmt, pvar);
> +	va_end(pvar);
> +	usage();
> +	exit(EXIT_FAILURE);
> +}
> +
> +/**
> + * Get the number from argument. It can be number followed by
> + * units: k|K, m|M, g|G, t|T
> + */
> +static unsigned long long get_number(char **optarg)
> +{
> +	char *opt, *end;
> +	unsigned long long number, max;
> +
> +	/* get the max to avoid overflow */
> +	max = ULLONG_MAX / 1024;
> +	number = 0;
> +	opt = *optarg;
> +
> +	if (*opt == '-') {
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(ERANGE), *optarg);
> +	}
> +
> +	errno = 0;
> +	number = strtoul(opt, &end , 0);
> +	if (errno)
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(errno), *optarg);
> +
> +	/*
> +	 * Convert units to numbers. Fall-through stack is used for units
> +	 * so absence of breaks is intentional.
> +	 */
> +	switch (*end) {
> +	case 'T': /* terabytes */
> +	case 't':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'G': /* gigabytes */
> +	case 'g':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'M': /* megabytes */
> +	case 'm':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'K': /* kilobytes */
> +	case 'k':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +		end++;
> +	case '\0': /* end of the string */
> +		break;
> +	default:
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(EINVAL), *optarg);
> +		return 0;
> +	}
> +
> +	if (*end != '\0') {
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(EINVAL), *optarg);
> +	}
> +
> +	return number;
> +}
> +
> +static int parse_opts(int argc, char **argv, struct options *opts)
> +{
> +	int c;
> +
> +	while ((c = getopt(argc, argv, "s:l:m:v")) != EOF) {
> +		switch (c) {
> +		case 's': /* starting point */
> +			opts->range->start = get_number(&optarg);
> +			break;
> +		case 'l': /* length */
> +			opts->range->len = get_number(&optarg);
> +			break;
> +		case 'm': /* minlen */
> +			opts->range->minlen = get_number(&optarg);
> +			break;
> +		case 'v': /* verbose */
> +			opts->verbose = 1;
> +			break;
> +		default:
> +			return EXIT_FAILURE;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +int main(int argc, char **argv)
> +{
> +	struct options *opts;
> +	struct stat sb;
> +	int fd, err = 0, ret = EXIT_FAILURE;
> +
> +	opts = malloc(sizeof(struct options));
> +	if (!opts)
> +		err_exit("%s: malloc(): %s\n", program_name, strerror(errno));
> +
> +	opts->range = NULL;
> +	opts->verbose = 0;
> +
> +	if (argc > 1)
> +		strncpy(opts->mpoint, argv[argc - 1], sizeof(opts->mpoint));
> +
> +	opts->range = calloc(1, sizeof(struct fstrim_range));
> +	if (!opts->range) {
> +		fprintf(stderr, "%s: calloc(): %s\n", program_name,
> +			strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	opts->range->len = ULLONG_MAX;
> +
> +	if (argc > 2)
> +		err = parse_opts(argc, argv, opts);
> +
> +	if (err) {
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (strnlen(opts->mpoint, 1) < 1) {
> +		fprintf(stderr, "%s: You have to specify mount point.\n",
> +			program_name);
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (stat(opts->mpoint, &sb) == -1) {
> +		fprintf(stderr, "%s: %s: %s\n", program_name,
> +			opts->mpoint, strerror(errno));
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (!S_ISDIR(sb.st_mode)) {
> +		fprintf(stderr, "%s: %s: (%s)\n", program_name,
> +			opts->mpoint, strerror(ENOTDIR));
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	fd = open(opts->mpoint, O_RDONLY);
> +	if (fd < 0) {
> +		fprintf(stderr, "%s: open(%s): %s\n", program_name,
> +			opts->mpoint, strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	if (ioctl(fd, FITRIM, opts->range)) {
> +		fprintf(stderr, "%s: FSTRIM: %s\n", program_name,
> +			strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	if ((opts->verbose) && (opts->range))
> +		fprintf(stdout, "%llu Bytes was trimmed\n", opts->range->len);
> +
> +	ret = EXIT_SUCCESS;
> +
> +free_opts:
> +	if (opts) {
> +		if (opts->range)
> +			free(opts->range);
> +		free(opts);
> +	}
> +
> +	return ret;
> +}
> 

-- 

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-11-26 14:12 [PATCH] Add test 248: Check filesystem FITRIM implementation Lukas Czerner
  2010-12-06 10:20 ` Lukas Czerner
@ 2010-12-09  5:15 ` Eric Sandeen
  2010-12-09 11:02   ` Lukas Czerner
  1 sibling, 1 reply; 12+ messages in thread
From: Eric Sandeen @ 2010-12-09  5:15 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: hch, esandeen, xfs

On 11/26/10 8:12 AM, Lukas Czerner wrote:
> FITRIM ioctl  is used on a mounted filesystem to discard (or "trim")
> blocks which are not in use by the filesystem.  This is useful for
> solid-state drives (SSDs) and thinly-provi-sioned storage. This test
> helps to verify filesystem FITRIM implementation to assure that it
> does not corrupts data.
> 
> This test creates checksums of all files in /usr/share/doc directory and
> run several processes which clear its working directory on SCRATCH_MNT,
> then copy everything from /usr/share/doc into its working directory, create
> list of files in working directory and its checksums and compare it with the
> original list of checksums. Every process works in the loop so it repeat
> remove->copy->check, while fstrim tool is running simultaneously.
> 
> Fstrim is just a helper tool which uses FITRIM ioctl to actually do the
> filesystem discard.
> 
> I found this very useful because when the FITRIM is really buggy (thus
> data-destroying) the 248 test will notice, because checksums will most
> likely change.
> 
> Signed-off-by: Lukas Czerner <lczerner@redhat.com>
> ---
>  248          |  163 +++++++++++++++++++++++++++++++++++++
>  248.out      |    3 +
>  group        |    1 +
>  src/Makefile |    2 +-
>  src/fstrim.c |  257 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 425 insertions(+), 1 deletions(-)
>  create mode 100755 248
>  create mode 100644 248.out
>  create mode 100644 src/fstrim.c
> 
> diff --git a/248 b/248
> new file mode 100755
> index 0000000..0d2f17f
> --- /dev/null
> +++ b/248
> @@ -0,0 +1,163 @@
> +#!/bin/bash
> +# FS QA Test No. 248
> +#
> +# This test was created in order to verify filesystem FITRIM implementation.
> +# By many concurrent copy and remove operations and checking that files
> +# does not change after copied into SCRATCH_MNT test if FITIM implementation

typo up there in "FITIM", just FWIW.

> +# corrupts the filesystem (data/metadata).
> +#
> +#-----------------------------------------------------------------------
> +# Copyright 2010 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
> +#
> +# 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
> +#-----------------------------------------------------------------------
> +
> +owner=lczerner@redhat.com
> +
> +seq=`basename $0`
> +echo "QA output created by $seq"
> +
> +here=`pwd`
> +tmp=`mktemp -d`
> +status=1    # failure is the default!
> +trap "_cleanup; exit \$status" 0 1 3
> +trap "_destroy; exit \$status" 2 15
> +chpid=0
> +mypid=$$
> +
> +# get standard environment, filters and checks
> +. ./common.rc
> +. ./common.filter
> +
> +# real QA test starts here
> +_supported_fs generic
> +_supported_os Linux
> +_require_scratch
> +_scratch_mkfs >/dev/null 2>&1
> +_scratch_mount
> +
> +_cleanup()
> +{
> +	rm -rf $tmp
> +}
> +
> +_destroy()
> +{
> +	kill $pids $fstrim_pid
> +	wait $pids $fstrim_pid
> +	rm -rf $tmp
> +}
> +
> +_destroy_fstrim()
> +{
> +	kill $fpid
> +	wait $fpid
> +}
> +
> +fstrim_loop()
> +{
> +	trap "_destroy_fstrim; exit \$status" 2 15
> +	fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
> +
> +	while true ; do
> +		step=1048576
> +		start=0
> +		$here/src/fstrim $SCRATCH_MNT &
> +		fpid=$!
> +		wait $fpid
> +		while [ $start -lt $fsize ] ; do
> +			$here/src/fstrim -s ${start}k -l ${step}k $SCRATCH_MNT &
> +			fpid=$!
> +			wait $fpid
> +			start=$(( $start + $step ))
> +		done

I may be dense here but 

a) why do you background and then immediately wait?
b) does this start a whole-device trim followed by several
smaller range-trims?

I could do with some comments, I suppose.

> +	done
> +}
> +
> +function check_sums() {
> +	dir=$1
> +
> +	(
> +	cd $SCRATCH_MNT/$p
> +	find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
> +	)
> +
> +	diff $tmp/content.sums $tmp/stress.$$.$p
> +	if [ $? -ne 0 ]; then
> +		echo "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
> +		kill $mypid

what is $mypid?  Oh right $$ ...  why not:

_fail "!!!Checksums has changed - Filesystem possibly corrupted!!!"

> +	fi
> +	rm -f $tmp/stress.$$.$p
> +}
> +
> +function run_process() {
> +	local p=$1
> +	repeat=10
> +
> +	sleep $((5*$p))s &
> +	export chpid=$! && wait $chpid &> /dev/null

I guess I don't sight-read bash very well.  What's going
on with all the backgrounding/waiting here?

> +	chpid=0
> +
> +	while [ $repeat -gt 0 ]; do
> +
> +		# Remove old directories.
> +		rm -rf $SCRATCH_MNT/$p
> +		export chpid=$! && wait $chpid &> /dev/null

and here?

> +		# Copy content -> partition.
> +		mkdir $SCRATCH_MNT/$p
> +		cp -ax $content/* $SCRATCH_MNT/$p
> +		export chpid=$! && wait $chpid &> /dev/null
> +
> +		check_sums
> +		repeat=$(( $repeat - 1 ))
> +	done
> +}
> +
> +nproc=20
> +content=/usr/share/doc
> +
> +# Check for FITRIM support
> +echo -n "Checking FITRIM support: "
> +$here/src/fstrim -l 10M $SCRATCH_MNT
> +[ $? -ne 0 ] && exit
> +echo "done."
> +
> +mkdir -p $tmp
> +
> +(
> +cd $content
> +find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
> +)
> +
> +echo -n "Running the test: "
> +pids=""
> +fstrim_loop &
> +fstrim_pid=$!
> +p=1
> +while [ $p -le $nproc ]; do
> +	run_process $p &
> +	pids="$pids $!"
> +	p=$(($p+1))
> +done
> +echo "done."
> +
> +wait $pids
> +kill $fstrim_pid
> +wait $fstrim_pid
> +
> +status=0
> +_check_scratch_fs

Scratch fs should get checked automatically after the test I think?
I guess other tests do this, but I'm not sure it's necessary
unless filesystems are made, remounted, etc in a loop during
the test

> +exit
> diff --git a/248.out b/248.out
> new file mode 100644
> index 0000000..880d9c7
> --- /dev/null
> +++ b/248.out
> @@ -0,0 +1,3 @@
> +QA output created by 248
> +Checking FITRIM support: done.
> +Running the test: done.
> diff --git a/group b/group
> index 0f94dd9..fea84ed 100644
> --- a/group
> +++ b/group
> @@ -361,3 +361,4 @@ deprecated
>  245 auto quick dir
>  246 auto quick rw
>  247 auto quick rw
> +248 ioctl

I suppose maybe a trim group would be worthwhile at some point ...

> diff --git a/src/Makefile b/src/Makefile
> index b827bd0..885fd65 100644
> --- a/src/Makefile
> +++ b/src/Makefile
> @@ -17,7 +17,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
>  	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
>  	locktest unwritten_mmap bulkstat_unlink_test t_stripealign \
>  	bulkstat_unlink_test_modified t_dir_offset t_futimens t_immutable \
> -	stale_handle
> +	stale_handle fstrim
>  
>  SUBDIRS =
>  
> diff --git a/src/fstrim.c b/src/fstrim.c
> new file mode 100644
> index 0000000..6686c99
> --- /dev/null
> +++ b/src/fstrim.c
> @@ -0,0 +1,257 @@
> +/*
> + * fstrim.c -- discard the part (or whole) of mounted filesystem.
> + *
> + * Copyright (C) 2009 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>

2010 I think :)

> + *
> + * 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, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>.
> + *
> + * This program uses FITRIM ioctl to discard parts or the whole filesystem
> + * online (mounted). You can specify range (start and lenght) to be
> + * discarded, or simply discard while filesystem.
> + *
> + * Usage: fstrim [options] <mount point>
> + */
> +
> +#include <string.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <errno.h>
> +#include <stdio.h>
> +#include <stdint.h>
> +#include <fcntl.h>
> +#include <limits.h>
> +#include <stdarg.h>
> +#include <getopt.h>
> +
> +#include <sys/ioctl.h>
> +#include <sys/stat.h>
> +#include <linux/fs.h>
> +
> +#ifndef FITRIM
> +struct fstrim_range {
> +	uint64_t start;
> +	uint64_t len;
> +	uint64_t minlen;
> +};
> +#define FITRIM		_IOWR('X', 121, struct fstrim_range)
> +#endif
> +
> +const char *program_name = "fstrim";
> +
> +struct options {
> +	struct fstrim_range *range;
> +	char mpoint[PATH_MAX];
> +	char verbose;
> +};
> +
> +static void usage(void)
> +{
> +	fprintf(stderr,
> +		"Usage: %s [-s start] [-l length] [-m minimum-extent]"
> +		" [-v] {mountpoint}\n\t"
> +		"-s Starting Byte to discard from\n\t"
> +		"-l Number of Bytes to discard from the start\n\t"
> +		"-m Minimum extent length to discard\n\t"
> +		"-v Verbose - number of discarded bytes\n",
> +		program_name);
> +}
> +
> +static void err_exit(const char *fmt, ...)
> +{
> +	va_list pvar;
> +	va_start(pvar, fmt);
> +	vfprintf(stderr, fmt, pvar);
> +	va_end(pvar);
> +	usage();
> +	exit(EXIT_FAILURE);
> +}
> +
> +/**
> + * Get the number from argument. It can be number followed by
> + * units: k|K, m|M, g|G, t|T
> + */
> +static unsigned long long get_number(char **optarg)
> +{
> +	char *opt, *end;
> +	unsigned long long number, max;
> +
> +	/* get the max to avoid overflow */
> +	max = ULLONG_MAX / 1024;
> +	number = 0;
> +	opt = *optarg;
> +
> +	if (*opt == '-') {
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(ERANGE), *optarg);
> +	}
> +
> +	errno = 0;
> +	number = strtoul(opt, &end , 0);
> +	if (errno)
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(errno), *optarg);
> +
> +	/*
> +	 * Convert units to numbers. Fall-through stack is used for units
> +	 * so absence of breaks is intentional.
> +	 */

wish we had a library for this sort of thing :(

> +	switch (*end) {
> +	case 'T': /* terabytes */
> +	case 't':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'G': /* gigabytes */
> +	case 'g':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'M': /* megabytes */
> +	case 'm':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'K': /* kilobytes */
> +	case 'k':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +		end++;
> +	case '\0': /* end of the string */
> +		break;
> +	default:
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(EINVAL), *optarg);
> +		return 0;
> +	}
> +
> +	if (*end != '\0') {
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(EINVAL), *optarg);
> +	}
> +
> +	return number;
> +}
> +
> +static int parse_opts(int argc, char **argv, struct options *opts)
> +{
> +	int c;
> +
> +	while ((c = getopt(argc, argv, "s:l:m:v")) != EOF) {
> +		switch (c) {
> +		case 's': /* starting point */
> +			opts->range->start = get_number(&optarg);
> +			break;
> +		case 'l': /* length */
> +			opts->range->len = get_number(&optarg);
> +			break;
> +		case 'm': /* minlen */
> +			opts->range->minlen = get_number(&optarg);
> +			break;
> +		case 'v': /* verbose */
> +			opts->verbose = 1;
> +			break;
> +		default:
> +			return EXIT_FAILURE;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +int main(int argc, char **argv)
> +{
> +	struct options *opts;
> +	struct stat sb;
> +	int fd, err = 0, ret = EXIT_FAILURE;
> +
> +	opts = malloc(sizeof(struct options));
> +	if (!opts)
> +		err_exit("%s: malloc(): %s\n", program_name, strerror(errno));
> +
> +	opts->range = NULL;
> +	opts->verbose = 0;
> +
> +	if (argc > 1)
> +		strncpy(opts->mpoint, argv[argc - 1], sizeof(opts->mpoint));
> +
> +	opts->range = calloc(1, sizeof(struct fstrim_range));
> +	if (!opts->range) {
> +		fprintf(stderr, "%s: calloc(): %s\n", program_name,
> +			strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	opts->range->len = ULLONG_MAX;
> +
> +	if (argc > 2)
> +		err = parse_opts(argc, argv, opts);
> +
> +	if (err) {
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (strnlen(opts->mpoint, 1) < 1) {
> +		fprintf(stderr, "%s: You have to specify mount point.\n",
> +			program_name);
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (stat(opts->mpoint, &sb) == -1) {
> +		fprintf(stderr, "%s: %s: %s\n", program_name,
> +			opts->mpoint, strerror(errno));
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (!S_ISDIR(sb.st_mode)) {
> +		fprintf(stderr, "%s: %s: (%s)\n", program_name,
> +			opts->mpoint, strerror(ENOTDIR));
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	fd = open(opts->mpoint, O_RDONLY);
> +	if (fd < 0) {
> +		fprintf(stderr, "%s: open(%s): %s\n", program_name,
> +			opts->mpoint, strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	if (ioctl(fd, FITRIM, opts->range)) {
> +		fprintf(stderr, "%s: FSTRIM: %s\n", program_name,
> +			strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	if ((opts->verbose) && (opts->range))
> +		fprintf(stdout, "%llu Bytes was trimmed\n", opts->range->len);

"Bytes were trimmed," just FWIW

> +
> +	ret = EXIT_SUCCESS;
> +
> +free_opts:
> +	if (opts) {
> +		if (opts->range)
> +			free(opts->range);
> +		free(opts);
> +	}
> +
> +	return ret;
> +}

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-12-09  5:15 ` Eric Sandeen
@ 2010-12-09 11:02   ` Lukas Czerner
  2010-12-09 11:11     ` Lukas Czerner
  0 siblings, 1 reply; 12+ messages in thread
From: Lukas Czerner @ 2010-12-09 11:02 UTC (permalink / raw)
  To: Eric Sandeen; +Cc: hch, Lukas Czerner, esandeen, xfs

On Wed, 8 Dec 2010, Eric Sandeen wrote:

> On 11/26/10 8:12 AM, Lukas Czerner wrote:
> > FITRIM ioctl  is used on a mounted filesystem to discard (or "trim")
> > blocks which are not in use by the filesystem.  This is useful for
> > solid-state drives (SSDs) and thinly-provi-sioned storage. This test
> > helps to verify filesystem FITRIM implementation to assure that it
> > does not corrupts data.
> > 
> > This test creates checksums of all files in /usr/share/doc directory and
> > run several processes which clear its working directory on SCRATCH_MNT,
> > then copy everything from /usr/share/doc into its working directory, create
> > list of files in working directory and its checksums and compare it with the
> > original list of checksums. Every process works in the loop so it repeat
> > remove->copy->check, while fstrim tool is running simultaneously.
> > 
> > Fstrim is just a helper tool which uses FITRIM ioctl to actually do the
> > filesystem discard.
> > 
> > I found this very useful because when the FITRIM is really buggy (thus
> > data-destroying) the 248 test will notice, because checksums will most
> > likely change.
> > 
> > Signed-off-by: Lukas Czerner <lczerner@redhat.com>

-snip-

> > +
> > +fstrim_loop()
> > +{
> > +	trap "_destroy_fstrim; exit \$status" 2 15
> > +	fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
> > +
> > +	while true ; do
> > +		step=1048576
> > +		start=0
> > +		$here/src/fstrim $SCRATCH_MNT &
> > +		fpid=$!
> > +		wait $fpid
> > +		while [ $start -lt $fsize ] ; do
> > +			$here/src/fstrim -s ${start}k -l ${step}k $SCRATCH_MNT &
> > +			fpid=$!
> > +			wait $fpid
> > +			start=$(( $start + $step ))
> > +		done
> 
> I may be dense here but 
> 
> a) why do you background and then immediately wait?
> b) does this start a whole-device trim followed by several
> smaller range-trims?
> 
> I could do with some comments, I suppose.

Hi Eric,

all the waiting is done because Bash is incredibly stupid. As you know,
fstrim_loop is run at background and when the test is over, or when it
is killed (with ^C), because of trap, it tries to kill fstrim_loop.
However, it does not kill currently running commands, so fstrim might be
still running making it impossible to umount the SCRATCH_MNT.

So this way, I can kill the running process directly. I believe I am not
the first one running into this troubles, so maybe there is a better way
?

> 
> > +	done
> > +}
> > +
> > +function check_sums() {
> > +	dir=$1
> > +
> > +	(
> > +	cd $SCRATCH_MNT/$p
> > +	find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
> > +	)
> > +
> > +	diff $tmp/content.sums $tmp/stress.$$.$p
> > +	if [ $? -ne 0 ]; then
> > +		echo "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
> > +		kill $mypid
> 
> what is $mypid?  Oh right $$ ...  why not:
> 
> _fail "!!!Checksums has changed - Filesystem possibly corrupted!!!"

oh, that's better, thanks.

> 
> > +	fi
> > +	rm -f $tmp/stress.$$.$p
> > +}
> > +
> > +function run_process() {
> > +	local p=$1
> > +	repeat=10
> > +
> > +	sleep $((5*$p))s &
> > +	export chpid=$! && wait $chpid &> /dev/null
> 
> I guess I don't sight-read bash very well.  What's going
> on with all the backgrounding/waiting here?
> 
> > +	chpid=0
> > +
> > +	while [ $repeat -gt 0 ]; do
> > +
> > +		# Remove old directories.
> > +		rm -rf $SCRATCH_MNT/$p
> > +		export chpid=$! && wait $chpid &> /dev/null
> 
> and here?

The same thing here, the process will still be running when xfstests
will attempt to umount SCRATCH_MNT, resulting in error. This way I can
kill it directly.

> 
> > +		# Copy content -> partition.
> > +		mkdir $SCRATCH_MNT/$p
> > +		cp -ax $content/* $SCRATCH_MNT/$p
> > +		export chpid=$! && wait $chpid &> /dev/null
> > +
> > +		check_sums
> > +		repeat=$(( $repeat - 1 ))
> > +	done
> > +}
> > +
> > +nproc=20
> > +content=/usr/share/doc
> > +
> > +# Check for FITRIM support
> > +echo -n "Checking FITRIM support: "
> > +$here/src/fstrim -l 10M $SCRATCH_MNT
> > +[ $? -ne 0 ] && exit
> > +echo "done."
> > +
> > +mkdir -p $tmp
> > +
> > +(
> > +cd $content
> > +find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
> > +)
> > +
> > +echo -n "Running the test: "
> > +pids=""
> > +fstrim_loop &
> > +fstrim_pid=$!
> > +p=1
> > +while [ $p -le $nproc ]; do
> > +	run_process $p &
> > +	pids="$pids $!"
> > +	p=$(($p+1))
> > +done
> > +echo "done."
> > +
> > +wait $pids
> > +kill $fstrim_pid
> > +wait $fstrim_pid
> > +
> > +status=0
> > +_check_scratch_fs
> 
> Scratch fs should get checked automatically after the test I think?
> I guess other tests do this, but I'm not sure it's necessary
> unless filesystems are made, remounted, etc in a loop during
> the test

Ok, I'll get rid of it.

> 
> > +exit
> > diff --git a/248.out b/248.out
> > new file mode 100644
> > index 0000000..880d9c7
> > --- /dev/null
> > +++ b/248.out
> > @@ -0,0 +1,3 @@
> > +QA output created by 248
> > +Checking FITRIM support: done.
> > +Running the test: done.
> > diff --git a/group b/group
> > index 0f94dd9..fea84ed 100644
> > --- a/group
> > +++ b/group
> > @@ -361,3 +361,4 @@ deprecated
> >  245 auto quick dir
> >  246 auto quick rw
> >  247 auto quick rw
> > +248 ioctl
> 
> I suppose maybe a trim group would be worthwhile at some point ...

Added.

> 
> > diff --git a/src/Makefile b/src/Makefile
> > index b827bd0..885fd65 100644
> > --- a/src/Makefile
> > +++ b/src/Makefile
> > @@ -17,7 +17,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
> >  	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
> >  	locktest unwritten_mmap bulkstat_unlink_test t_stripealign \
> >  	bulkstat_unlink_test_modified t_dir_offset t_futimens t_immutable \
> > -	stale_handle
> > +	stale_handle fstrim
> >  
> >  SUBDIRS =
> >  
> > diff --git a/src/fstrim.c b/src/fstrim.c
> > new file mode 100644
> > index 0000000..6686c99
> > --- /dev/null
> > +++ b/src/fstrim.c
> > @@ -0,0 +1,257 @@
> > +/*
> > + * fstrim.c -- discard the part (or whole) of mounted filesystem.
> > + *
> > + * Copyright (C) 2009 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
> 
> 2010 I think :)

Hmm, still living in the past :).

Thanks for review Eric!

-Lukas

-snip-

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-12-09 11:02   ` Lukas Czerner
@ 2010-12-09 11:11     ` Lukas Czerner
  0 siblings, 0 replies; 12+ messages in thread
From: Lukas Czerner @ 2010-12-09 11:11 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: hch, Eric Sandeen, esandeen, xfs

On Thu, 9 Dec 2010, Lukas Czerner wrote:

> On Wed, 8 Dec 2010, Eric Sandeen wrote:
> 
> > On 11/26/10 8:12 AM, Lukas Czerner wrote:
> > > FITRIM ioctl  is used on a mounted filesystem to discard (or "trim")
> > > blocks which are not in use by the filesystem.  This is useful for
> > > solid-state drives (SSDs) and thinly-provi-sioned storage. This test
> > > helps to verify filesystem FITRIM implementation to assure that it
> > > does not corrupts data.
> > > 
> > > This test creates checksums of all files in /usr/share/doc directory and
> > > run several processes which clear its working directory on SCRATCH_MNT,
> > > then copy everything from /usr/share/doc into its working directory, create
> > > list of files in working directory and its checksums and compare it with the
> > > original list of checksums. Every process works in the loop so it repeat
> > > remove->copy->check, while fstrim tool is running simultaneously.
> > > 
> > > Fstrim is just a helper tool which uses FITRIM ioctl to actually do the
> > > filesystem discard.
> > > 
> > > I found this very useful because when the FITRIM is really buggy (thus
> > > data-destroying) the 248 test will notice, because checksums will most
> > > likely change.
> > > 
> > > Signed-off-by: Lukas Czerner <lczerner@redhat.com>
> 
> -snip-
> 
> > > +
> > > +fstrim_loop()
> > > +{
> > > +	trap "_destroy_fstrim; exit \$status" 2 15
> > > +	fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
> > > +
> > > +	while true ; do
> > > +		step=1048576
> > > +		start=0
> > > +		$here/src/fstrim $SCRATCH_MNT &
> > > +		fpid=$!
> > > +		wait $fpid
> > > +		while [ $start -lt $fsize ] ; do
> > > +			$here/src/fstrim -s ${start}k -l ${step}k $SCRATCH_MNT &
> > > +			fpid=$!
> > > +			wait $fpid
> > > +			start=$(( $start + $step ))
> > > +		done
> > 
> > I may be dense here but 
> > 
> > a) why do you background and then immediately wait?
> > b) does this start a whole-device trim followed by several
> > smaller range-trims?

I forgot about this one. The reason for this is, that this way we can
get better test coverage of the kernel code, because the code path of
smaller trims (especially not ag-aligned) might be slightly different,
so we test that case as well.

-Lukas

> > 
> > I could do with some comments, I suppose.
> 
> Hi Eric,
> 
> all the waiting is done because Bash is incredibly stupid. As you know,
> fstrim_loop is run at background and when the test is over, or when it
> is killed (with ^C), because of trap, it tries to kill fstrim_loop.
> However, it does not kill currently running commands, so fstrim might be
> still running making it impossible to umount the SCRATCH_MNT.
> 
> So this way, I can kill the running process directly. I believe I am not
> the first one running into this troubles, so maybe there is a better way
> ?
> 

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-12-10 15:53 Lukas Czerner
  2010-12-17  9:32 ` Lukas Czerner
  2010-12-20 17:41 ` Christoph Hellwig
@ 2010-12-21 15:04 ` Christoph Hellwig
  2 siblings, 0 replies; 12+ messages in thread
From: Christoph Hellwig @ 2010-12-21 15:04 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: hch, esandeen, xfs

Another one I only noticed now:

fstrim.c: In function 'main':
fstrim.c:245: warning: format '%lu' expects type 'long unsigned int',
but argument 3 has type 'uint64_t'

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-12-21 11:03     ` Phil Karn
@ 2010-12-21 11:22       ` Christoph Hellwig
  0 siblings, 0 replies; 12+ messages in thread
From: Christoph Hellwig @ 2010-12-21 11:22 UTC (permalink / raw)
  To: karn; +Cc: xfs

On Tue, Dec 21, 2010 at 03:03:03AM -0800, Phil Karn wrote:
> What is the plan for TRIM support in XFS? I don't see any mention in the
> 2.6.36.2 sources, so I guess the code you guys have been discussing must not
> be considered stable yet.
>
> How will it eventually work? Will the filesystem automatically issue the
> commands to the drive whenever a file is deleted, or will it always require
> some sort of prodding from a userspace program?


I submitted a patch to add FITRIM support, which should go into 2.6.38
if I get a positive review.  That is the variant that is triggered by
the fstim userspace program and what the test we're discussing here
exercises.  I've also prototyped various forms of online discard, but
none of them is quite ready yet.  I've started porting some allocator
patches from Dave forward, which are a requirement for reliable online
discard.  After that I need to decide which of the variants we'll
actually use. So far the "dumb" discard at transaction commit time shows
the best results.

> And what kind of experiences are people having with XFS on current SSDs? I
> just put an Intel drive in my newly upgraded box and I'm wondering if this
> blazing performance will screech to a halt when my cumulative writes exceed
> its 120 GB capacity. Does anyone have a standalone "drive optimizer" that
> understands XFS, i.e, that can examine an unmounted file system and issue
> the appropriate TRIM commands for every block on the free list?

The intel SSDs do quite fine without constant trimming, as do most
current SSDs.  I only know of one sample that areally needs periodic
trims, but AFAIK it's not generally available yet (sorry, can't name the
vendor due to NDAs).  But even with the current ones a TRIM once in a
while is useful, but about once every couple of weeks is enough.  For a
system not constantly loaded you can use Mark Lord's wiper.sh script,
but be aware it only works on disks / partitions and not on LVM volumes.
Once the FITRIM support is in you can use that for the same purpose,
with the benefit of also working over LVM, and also working nicely on
a system under heavy use, as it doesn't have to allocate all free space
as the userspace hack done by wiper.sh does.

> Or is the Intel drive somehow really good at avoiding write amplification
> even without TRIM help from the file system? I've heard that may be the
> case, but I don't understand how that can be.

No, the Intel disks actually degrade a lot after heavy use.
Unfortunately even regular trims don't always seem to solve this.

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-12-21 10:41   ` Lukas Czerner
@ 2010-12-21 11:03     ` Phil Karn
  2010-12-21 11:22       ` Christoph Hellwig
  0 siblings, 1 reply; 12+ messages in thread
From: Phil Karn @ 2010-12-21 11:03 UTC (permalink / raw)
  To: xfs


[-- Attachment #1.1: Type: text/plain, Size: 1012 bytes --]

What is the plan for TRIM support in XFS? I don't see any mention in the
2.6.36.2 sources, so I guess the code you guys have been discussing must not
be considered stable yet.

How will it eventually work? Will the filesystem automatically issue the
commands to the drive whenever a file is deleted, or will it always require
some sort of prodding from a userspace program?

And what kind of experiences are people having with XFS on current SSDs? I
just put an Intel drive in my newly upgraded box and I'm wondering if this
blazing performance will screech to a halt when my cumulative writes exceed
its 120 GB capacity. Does anyone have a standalone "drive optimizer" that
understands XFS, i.e, that can examine an unmounted file system and issue
the appropriate TRIM commands for every block on the free list?

Or is the Intel drive somehow really good at avoiding write amplification
even without TRIM help from the file system? I've heard that may be the
case, but I don't understand how that can be.

Phil

[-- Attachment #1.2: Type: text/html, Size: 1081 bytes --]

[-- Attachment #2: Type: text/plain, Size: 121 bytes --]

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-12-20 17:41 ` Christoph Hellwig
@ 2010-12-21 10:41   ` Lukas Czerner
  2010-12-21 11:03     ` Phil Karn
  0 siblings, 1 reply; 12+ messages in thread
From: Lukas Czerner @ 2010-12-21 10:41 UTC (permalink / raw)
  To: Christoph Hellwig; +Cc: Lukas Czerner, esandeen, xfs

On Mon, 20 Dec 2010, Christoph Hellwig wrote:

> Hi Lukas,
> 
> I've looked over it a bit again, and I can't really comment on the code
> too much, as my bash fu is nowhere near a level to actually make sense
> of most of the testcase.   I have however ran it in a few setups and
> can comment based on that.
> 
> First your current first discard to check if we actually support it is
> not handled correctly.  Running the test on a filesystem that doesn't
> support it currently fails the test for me with:
> 
> @@ -1,3 +1,2 @@
> QA output created by 249
> -Checking FITRIM support: done.
> -Running the test: done.
> +Checking FITRIM support: fstrim: FSTRIM: Inappropriate ioctl for device
> 
> This should be easily fixable by redirecting the output of the test
> command to /dev/null and do a _notrun if it exited with an error value,
> just like other tests that require non-standard behaviour.
> 
> Second using data from /usr/share/doc seems a bit non-deterministic to
> me, as the content will be different on every system.  Any chance you
> could just use the xfstests source code instead?
> 
> Third the testcase runs forever, e.g. 93 minutes in my KVM setup, even
> using a no-op discard implementation.  While this is useful for burn in
> testing having a shorter run time version that can be run automatically
> would be really useful.  I tried to figure out what paramters control
> the runtime, but as mentioned above my bash skill really aren't enough
> to do that.
You can lower the "nproc" variable, optionally you can also lower
"repeat" variable in run_process().



Hi Christoph,

thanks a lot for review. I certainly can use xfstests source code, so
I'll change that. Regarding the duration of the test, it is kind of hard
to determine the right amount of load, because the devices and machines
differs a lot. It also involves a LOT of copying files so I guess it
might be a little slow. However I need to be careful with reducing
number of concurrent processes, because with less stress it might be
harder to hit potential problem. (you can always

But I'll try to test it an find just the right balance. Thanks again.

-Lukas

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-12-10 15:53 Lukas Czerner
  2010-12-17  9:32 ` Lukas Czerner
@ 2010-12-20 17:41 ` Christoph Hellwig
  2010-12-21 10:41   ` Lukas Czerner
  2010-12-21 15:04 ` Christoph Hellwig
  2 siblings, 1 reply; 12+ messages in thread
From: Christoph Hellwig @ 2010-12-20 17:41 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: esandeen, xfs

Hi Lukas,

I've looked over it a bit again, and I can't really comment on the code
too much, as my bash fu is nowhere near a level to actually make sense
of most of the testcase.   I have however ran it in a few setups and
can comment based on that.

First your current first discard to check if we actually support it is
not handled correctly.  Running the test on a filesystem that doesn't
support it currently fails the test for me with:

@@ -1,3 +1,2 @@
QA output created by 249
-Checking FITRIM support: done.
-Running the test: done.
+Checking FITRIM support: fstrim: FSTRIM: Inappropriate ioctl for device

This should be easily fixable by redirecting the output of the test
command to /dev/null and do a _notrun if it exited with an error value,
just like other tests that require non-standard behaviour.

Second using data from /usr/share/doc seems a bit non-deterministic to
me, as the content will be different on every system.  Any chance you
could just use the xfstests source code instead?

Third the testcase runs forever, e.g. 93 minutes in my KVM setup, even
using a no-op discard implementation.  While this is useful for burn in
testing having a shorter run time version that can be run automatically
would be really useful.  I tried to figure out what paramters control
the runtime, but as mentioned above my bash skill really aren't enough
to do that.

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] Add test 248: Check filesystem FITRIM implementation
  2010-12-10 15:53 Lukas Czerner
@ 2010-12-17  9:32 ` Lukas Czerner
  2010-12-20 17:41 ` Christoph Hellwig
  2010-12-21 15:04 ` Christoph Hellwig
  2 siblings, 0 replies; 12+ messages in thread
From: Lukas Czerner @ 2010-12-17  9:32 UTC (permalink / raw)
  To: Lukas Czerner; +Cc: hch, esandeen, xfs

On Fri, 10 Dec 2010, Lukas Czerner wrote:

> FITRIM ioctl  is used on a mounted filesystem to discard (or "trim")
> blocks which are not in use by the filesystem.  This is useful for
> solid-state drives (SSDs) and thinly-provi-sioned storage. This test
> helps to verify filesystem FITRIM implementation to assure that it
> does not corrupts data.
> 
> This test creates checksums of all files in /usr/share/doc directory and
> run several processes which clear its working directory on SCRATCH_MNT,
> then copy everything from /usr/share/doc into its working directory, create
> list of files in working directory and its checksums and compare it with the
> original list of checksums. Every process works in the loop so it repeat
> remove->copy->check, while fstrim tool is running simultaneously.
> 
> Fstrim is just a helper tool which uses FITRIM ioctl to actually do the
> filesystem discard.
> 
> I found this very useful because when the FITRIM is really buggy (thus
> data-destroying) the 248 test will notice, because checksums will most
> likely change.

Common guys, how many times I need to repost this ? :) Or do anyone have
objections ?

-Lukas

> 
> Signed-off-by: Lukas Czerner <lczerner@redhat.com>
> ---
>  248          |  184 +++++++++++++++++++++++++++++++++++++++++
>  248.out      |    3 +
>  group        |    1 +
>  src/Makefile |    2 +-
>  src/fstrim.c |  257 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  5 files changed, 446 insertions(+), 1 deletions(-)
>  create mode 100755 248
>  create mode 100644 248.out
>  create mode 100644 src/fstrim.c
> 
> diff --git a/248 b/248
> new file mode 100755
> index 0000000..a6cfce2
> --- /dev/null
> +++ b/248
> @@ -0,0 +1,184 @@
> +#!/bin/bash
> +# FS QA Test No. 248
> +#
> +# This test was created in order to verify filesystem FITRIM implementation.
> +# By many concurrent copy and remove operations and checking that files
> +# does not change after copied into SCRATCH_MNT test if FITRIM implementation
> +# corrupts the filesystem (data/metadata).
> +#
> +#-----------------------------------------------------------------------
> +# Copyright 2010 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
> +#
> +# 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
> +#-----------------------------------------------------------------------
> +
> +owner=lczerner@redhat.com
> +
> +seq=`basename $0`
> +echo "QA output created by $seq"
> +
> +here=`pwd`
> +tmp=`mktemp -d`
> +status=1    # failure is the default!
> +trap "_cleanup; exit \$status" 0 1 3
> +trap "_destroy; exit \$status" 2 15
> +chpid=0
> +mypid=$$
> +
> +# get standard environment, filters and checks
> +. ./common.rc
> +. ./common.filter
> +
> +# real QA test starts here
> +_supported_fs generic
> +_supported_os Linux
> +_require_scratch
> +_scratch_mkfs >/dev/null 2>&1
> +_scratch_mount
> +
> +_cleanup()
> +{
> +	rm -rf $tmp
> +}
> +
> +_destroy()
> +{
> +	kill $pids $fstrim_pid
> +	wait $pids $fstrim_pid
> +	rm -rf $tmp
> +}
> +
> +_destroy_fstrim()
> +{
> +	kill $fpid
> +	wait $fpid
> +}
> +
> +_fail()
> +{
> +	echo "$1"
> +	kill $mypid
> +}
> +
> +##
> +# Background FSTRIM loop. We are trimming the device in the loop and for
> +# better test coverage, we are doing whole device trim followed by several
> +# smaller trims.
> +##
> +fstrim_loop()
> +{
> +	trap "_destroy_fstrim; exit \$status" 2 15
> +	fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
> +
> +	while true ; do
> +		step=1048576
> +		start=0
> +		$here/src/fstrim $SCRATCH_MNT &
> +		fpid=$!
> +		wait $fpid
> +		while [ $start -lt $fsize ] ; do
> +			$here/src/fstrim -s ${start}k -l ${step}k $SCRATCH_MNT &
> +			fpid=$!
> +
> +			##
> +			# All the waiting is done because Bash is incredibly
> +			# stupid. As you know, fstrim_loop is run at background
> +			# and when the test is over, or when it is killed (with
> +			# ^C), because of trap, it tries to kill fstrim_loop.
> +			# However, it does not kill currently running commands,
> +			# so fstrim might be still running making it impossible
> +			# to umount the SCRATCH_MNT and hence resulting in
> +			# error.
> +			##
> +			wait $fpid
> +			start=$(( $start + $step ))
> +		done
> +	done
> +}
> +
> +function check_sums() {
> +	dir=$1
> +
> +	(
> +	cd $SCRATCH_MNT/$p
> +	find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
> +	)
> +
> +	diff $tmp/content.sums $tmp/stress.$$.$p
> +	if [ $? -ne 0 ]; then
> +		_fail "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
> +	fi
> +	rm -f $tmp/stress.$$.$p
> +}
> +
> +function run_process() {
> +	local p=$1
> +	repeat=10
> +	trap "kill $chpid; wait $chpid" 2 15
> +
> +	sleep $((10*$p))s &
> +	export chpid=$! && wait $chpid &> /dev/null
> +	chpid=0
> +
> +	while [ $repeat -gt 0 ]; do
> +
> +		# Remove old directories.
> +		rm -rf $SCRATCH_MNT/$p
> +		export chpid=$! && wait $chpid &> /dev/null
> +
> +		# Copy content -> partition.
> +		mkdir $SCRATCH_MNT/$p
> +		cp -ax $content/* $SCRATCH_MNT/$p
> +		export chpid=$! && wait $chpid &> /dev/null
> +
> +		check_sums
> +		repeat=$(( $repeat - 1 ))
> +	done
> +}
> +
> +nproc=20
> +content=/usr/share/doc
> +
> +# Check for FITRIM support
> +echo -n "Checking FITRIM support: "
> +$here/src/fstrim -l 10M $SCRATCH_MNT
> +[ $? -ne 0 ] && exit
> +echo "done."
> +
> +mkdir -p $tmp
> +
> +(
> +cd $content
> +find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
> +)
> +
> +echo -n "Running the test: "
> +pids=""
> +fstrim_loop &
> +fstrim_pid=$!
> +p=1
> +while [ $p -le $nproc ]; do
> +	run_process $p &
> +	pids="$pids $!"
> +	p=$(($p+1))
> +done
> +echo "done."
> +
> +wait $pids
> +kill $fstrim_pid
> +wait $fstrim_pid
> +
> +status=0
> +
> +exit
> diff --git a/248.out b/248.out
> new file mode 100644
> index 0000000..880d9c7
> --- /dev/null
> +++ b/248.out
> @@ -0,0 +1,3 @@
> +QA output created by 248
> +Checking FITRIM support: done.
> +Running the test: done.
> diff --git a/group b/group
> index 0f94dd9..19eec07 100644
> --- a/group
> +++ b/group
> @@ -361,3 +361,4 @@ deprecated
>  245 auto quick dir
>  246 auto quick rw
>  247 auto quick rw
> +248 ioctl trim
> diff --git a/src/Makefile b/src/Makefile
> index b827bd0..885fd65 100644
> --- a/src/Makefile
> +++ b/src/Makefile
> @@ -17,7 +17,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
>  	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
>  	locktest unwritten_mmap bulkstat_unlink_test t_stripealign \
>  	bulkstat_unlink_test_modified t_dir_offset t_futimens t_immutable \
> -	stale_handle
> +	stale_handle fstrim
>  
>  SUBDIRS =
>  
> diff --git a/src/fstrim.c b/src/fstrim.c
> new file mode 100644
> index 0000000..45ad841
> --- /dev/null
> +++ b/src/fstrim.c
> @@ -0,0 +1,257 @@
> +/*
> + * fstrim.c -- discard the part (or whole) of mounted filesystem.
> + *
> + * Copyright (C) 2009 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
> + *
> + * 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, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>.
> + *
> + * This program uses FITRIM ioctl to discard parts or the whole filesystem
> + * online (mounted). You can specify range (start and lenght) to be
> + * discarded, or simply discard while filesystem.
> + *
> + * Usage: fstrim [options] <mount point>
> + */
> +
> +#include <string.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <errno.h>
> +#include <stdio.h>
> +#include <stdint.h>
> +#include <fcntl.h>
> +#include <limits.h>
> +#include <stdarg.h>
> +#include <getopt.h>
> +
> +#include <sys/ioctl.h>
> +#include <sys/stat.h>
> +#include <linux/fs.h>
> +
> +#ifndef FITRIM
> +struct fstrim_range {
> +	uint64_t start;
> +	uint64_t len;
> +	uint64_t minlen;
> +};
> +#define FITRIM		_IOWR('X', 121, struct fstrim_range)
> +#endif
> +
> +const char *program_name = "fstrim";
> +
> +struct options {
> +	struct fstrim_range *range;
> +	char mpoint[PATH_MAX];
> +	char verbose;
> +};
> +
> +static void usage(void)
> +{
> +	fprintf(stderr,
> +		"Usage: %s [-s start] [-l length] [-m minimum-extent]"
> +		" [-v] {mountpoint}\n\t"
> +		"-s Starting Byte to discard from\n\t"
> +		"-l Number of Bytes to discard from the start\n\t"
> +		"-m Minimum extent length to discard\n\t"
> +		"-v Verbose - number of discarded bytes\n",
> +		program_name);
> +}
> +
> +static void err_exit(const char *fmt, ...)
> +{
> +	va_list pvar;
> +	va_start(pvar, fmt);
> +	vfprintf(stderr, fmt, pvar);
> +	va_end(pvar);
> +	usage();
> +	exit(EXIT_FAILURE);
> +}
> +
> +/**
> + * Get the number from argument. It can be number followed by
> + * units: k|K, m|M, g|G, t|T
> + */
> +static unsigned long long get_number(char **optarg)
> +{
> +	char *opt, *end;
> +	unsigned long long number, max;
> +
> +	/* get the max to avoid overflow */
> +	max = ULLONG_MAX / 1024;
> +	number = 0;
> +	opt = *optarg;
> +
> +	if (*opt == '-') {
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(ERANGE), *optarg);
> +	}
> +
> +	errno = 0;
> +	number = strtoul(opt, &end , 0);
> +	if (errno)
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(errno), *optarg);
> +
> +	/*
> +	 * Convert units to numbers. Fall-through stack is used for units
> +	 * so absence of breaks is intentional.
> +	 */
> +	switch (*end) {
> +	case 'T': /* terabytes */
> +	case 't':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'G': /* gigabytes */
> +	case 'g':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'M': /* megabytes */
> +	case 'm':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +	case 'K': /* kilobytes */
> +	case 'k':
> +		if (number > max)
> +			err_exit("%s: %s (%s)\n", program_name,
> +				 strerror(ERANGE), *optarg);
> +		number *= 1024;
> +		end++;
> +	case '\0': /* end of the string */
> +		break;
> +	default:
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(EINVAL), *optarg);
> +		return 0;
> +	}
> +
> +	if (*end != '\0') {
> +		err_exit("%s: %s (%s)\n", program_name,
> +			 strerror(EINVAL), *optarg);
> +	}
> +
> +	return number;
> +}
> +
> +static int parse_opts(int argc, char **argv, struct options *opts)
> +{
> +	int c;
> +
> +	while ((c = getopt(argc, argv, "s:l:m:v")) != EOF) {
> +		switch (c) {
> +		case 's': /* starting point */
> +			opts->range->start = get_number(&optarg);
> +			break;
> +		case 'l': /* length */
> +			opts->range->len = get_number(&optarg);
> +			break;
> +		case 'm': /* minlen */
> +			opts->range->minlen = get_number(&optarg);
> +			break;
> +		case 'v': /* verbose */
> +			opts->verbose = 1;
> +			break;
> +		default:
> +			return EXIT_FAILURE;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +int main(int argc, char **argv)
> +{
> +	struct options *opts;
> +	struct stat sb;
> +	int fd, err = 0, ret = EXIT_FAILURE;
> +
> +	opts = malloc(sizeof(struct options));
> +	if (!opts)
> +		err_exit("%s: malloc(): %s\n", program_name, strerror(errno));
> +
> +	opts->range = NULL;
> +	opts->verbose = 0;
> +
> +	if (argc > 1)
> +		strncpy(opts->mpoint, argv[argc - 1], sizeof(opts->mpoint));
> +
> +	opts->range = calloc(1, sizeof(struct fstrim_range));
> +	if (!opts->range) {
> +		fprintf(stderr, "%s: calloc(): %s\n", program_name,
> +			strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	opts->range->len = ULLONG_MAX;
> +
> +	if (argc > 2)
> +		err = parse_opts(argc, argv, opts);
> +
> +	if (err) {
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (strnlen(opts->mpoint, 1) < 1) {
> +		fprintf(stderr, "%s: You have to specify mount point.\n",
> +			program_name);
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (stat(opts->mpoint, &sb) == -1) {
> +		fprintf(stderr, "%s: %s: %s\n", program_name,
> +			opts->mpoint, strerror(errno));
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	if (!S_ISDIR(sb.st_mode)) {
> +		fprintf(stderr, "%s: %s: (%s)\n", program_name,
> +			opts->mpoint, strerror(ENOTDIR));
> +		usage();
> +		goto free_opts;
> +	}
> +
> +	fd = open(opts->mpoint, O_RDONLY);
> +	if (fd < 0) {
> +		fprintf(stderr, "%s: open(%s): %s\n", program_name,
> +			opts->mpoint, strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	if (ioctl(fd, FITRIM, opts->range)) {
> +		fprintf(stderr, "%s: FSTRIM: %s\n", program_name,
> +			strerror(errno));
> +		goto free_opts;
> +	}
> +
> +	if ((opts->verbose) && (opts->range))
> +		fprintf(stdout, "%lu Bytes were trimmed\n", opts->range->len);
> +
> +	ret = EXIT_SUCCESS;
> +
> +free_opts:
> +	if (opts) {
> +		if (opts->range)
> +			free(opts->range);
> +		free(opts);
> +	}
> +
> +	return ret;
> +}
> 

-- 

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH] Add test 248: Check filesystem FITRIM implementation
@ 2010-12-10 15:53 Lukas Czerner
  2010-12-17  9:32 ` Lukas Czerner
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Lukas Czerner @ 2010-12-10 15:53 UTC (permalink / raw)
  To: xfs; +Cc: hch, lczerner, esandeen

FITRIM ioctl  is used on a mounted filesystem to discard (or "trim")
blocks which are not in use by the filesystem.  This is useful for
solid-state drives (SSDs) and thinly-provi-sioned storage. This test
helps to verify filesystem FITRIM implementation to assure that it
does not corrupts data.

This test creates checksums of all files in /usr/share/doc directory and
run several processes which clear its working directory on SCRATCH_MNT,
then copy everything from /usr/share/doc into its working directory, create
list of files in working directory and its checksums and compare it with the
original list of checksums. Every process works in the loop so it repeat
remove->copy->check, while fstrim tool is running simultaneously.

Fstrim is just a helper tool which uses FITRIM ioctl to actually do the
filesystem discard.

I found this very useful because when the FITRIM is really buggy (thus
data-destroying) the 248 test will notice, because checksums will most
likely change.

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
---
 248          |  184 +++++++++++++++++++++++++++++++++++++++++
 248.out      |    3 +
 group        |    1 +
 src/Makefile |    2 +-
 src/fstrim.c |  257 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 446 insertions(+), 1 deletions(-)
 create mode 100755 248
 create mode 100644 248.out
 create mode 100644 src/fstrim.c

diff --git a/248 b/248
new file mode 100755
index 0000000..a6cfce2
--- /dev/null
+++ b/248
@@ -0,0 +1,184 @@
+#!/bin/bash
+# FS QA Test No. 248
+#
+# This test was created in order to verify filesystem FITRIM implementation.
+# By many concurrent copy and remove operations and checking that files
+# does not change after copied into SCRATCH_MNT test if FITRIM implementation
+# corrupts the filesystem (data/metadata).
+#
+#-----------------------------------------------------------------------
+# Copyright 2010 (C) Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
+#
+# 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
+#-----------------------------------------------------------------------
+
+owner=lczerner@redhat.com
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=`mktemp -d`
+status=1    # failure is the default!
+trap "_cleanup; exit \$status" 0 1 3
+trap "_destroy; exit \$status" 2 15
+chpid=0
+mypid=$$
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+# real QA test starts here
+_supported_fs generic
+_supported_os Linux
+_require_scratch
+_scratch_mkfs >/dev/null 2>&1
+_scratch_mount
+
+_cleanup()
+{
+	rm -rf $tmp
+}
+
+_destroy()
+{
+	kill $pids $fstrim_pid
+	wait $pids $fstrim_pid
+	rm -rf $tmp
+}
+
+_destroy_fstrim()
+{
+	kill $fpid
+	wait $fpid
+}
+
+_fail()
+{
+	echo "$1"
+	kill $mypid
+}
+
+##
+# Background FSTRIM loop. We are trimming the device in the loop and for
+# better test coverage, we are doing whole device trim followed by several
+# smaller trims.
+##
+fstrim_loop()
+{
+	trap "_destroy_fstrim; exit \$status" 2 15
+	fsize=$(df | grep $SCRATCH_MNT | grep $SCRATCH_DEV  | awk '{print $2}')
+
+	while true ; do
+		step=1048576
+		start=0
+		$here/src/fstrim $SCRATCH_MNT &
+		fpid=$!
+		wait $fpid
+		while [ $start -lt $fsize ] ; do
+			$here/src/fstrim -s ${start}k -l ${step}k $SCRATCH_MNT &
+			fpid=$!
+
+			##
+			# All the waiting is done because Bash is incredibly
+			# stupid. As you know, fstrim_loop is run at background
+			# and when the test is over, or when it is killed (with
+			# ^C), because of trap, it tries to kill fstrim_loop.
+			# However, it does not kill currently running commands,
+			# so fstrim might be still running making it impossible
+			# to umount the SCRATCH_MNT and hence resulting in
+			# error.
+			##
+			wait $fpid
+			start=$(( $start + $step ))
+		done
+	done
+}
+
+function check_sums() {
+	dir=$1
+
+	(
+	cd $SCRATCH_MNT/$p
+	find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/stress.$$.$p
+	)
+
+	diff $tmp/content.sums $tmp/stress.$$.$p
+	if [ $? -ne 0 ]; then
+		_fail "!!!Checksums has changed - Filesystem possibly corrupted!!!\n"
+	fi
+	rm -f $tmp/stress.$$.$p
+}
+
+function run_process() {
+	local p=$1
+	repeat=10
+	trap "kill $chpid; wait $chpid" 2 15
+
+	sleep $((10*$p))s &
+	export chpid=$! && wait $chpid &> /dev/null
+	chpid=0
+
+	while [ $repeat -gt 0 ]; do
+
+		# Remove old directories.
+		rm -rf $SCRATCH_MNT/$p
+		export chpid=$! && wait $chpid &> /dev/null
+
+		# Copy content -> partition.
+		mkdir $SCRATCH_MNT/$p
+		cp -ax $content/* $SCRATCH_MNT/$p
+		export chpid=$! && wait $chpid &> /dev/null
+
+		check_sums
+		repeat=$(( $repeat - 1 ))
+	done
+}
+
+nproc=20
+content=/usr/share/doc
+
+# Check for FITRIM support
+echo -n "Checking FITRIM support: "
+$here/src/fstrim -l 10M $SCRATCH_MNT
+[ $? -ne 0 ] && exit
+echo "done."
+
+mkdir -p $tmp
+
+(
+cd $content
+find -P . -xdev -type f -print0 | xargs -0 md5sum | sort -o $tmp/content.sums
+)
+
+echo -n "Running the test: "
+pids=""
+fstrim_loop &
+fstrim_pid=$!
+p=1
+while [ $p -le $nproc ]; do
+	run_process $p &
+	pids="$pids $!"
+	p=$(($p+1))
+done
+echo "done."
+
+wait $pids
+kill $fstrim_pid
+wait $fstrim_pid
+
+status=0
+
+exit
diff --git a/248.out b/248.out
new file mode 100644
index 0000000..880d9c7
--- /dev/null
+++ b/248.out
@@ -0,0 +1,3 @@
+QA output created by 248
+Checking FITRIM support: done.
+Running the test: done.
diff --git a/group b/group
index 0f94dd9..19eec07 100644
--- a/group
+++ b/group
@@ -361,3 +361,4 @@ deprecated
 245 auto quick dir
 246 auto quick rw
 247 auto quick rw
+248 ioctl trim
diff --git a/src/Makefile b/src/Makefile
index b827bd0..885fd65 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -17,7 +17,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
 	locktest unwritten_mmap bulkstat_unlink_test t_stripealign \
 	bulkstat_unlink_test_modified t_dir_offset t_futimens t_immutable \
-	stale_handle
+	stale_handle fstrim
 
 SUBDIRS =
 
diff --git a/src/fstrim.c b/src/fstrim.c
new file mode 100644
index 0000000..45ad841
--- /dev/null
+++ b/src/fstrim.c
@@ -0,0 +1,257 @@
+/*
+ * fstrim.c -- discard the part (or whole) of mounted filesystem.
+ *
+ * Copyright (C) 2009 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
+ *
+ * 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, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will 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, see <http://www.gnu.org/licenses/>.
+ *
+ * This program uses FITRIM ioctl to discard parts or the whole filesystem
+ * online (mounted). You can specify range (start and lenght) to be
+ * discarded, or simply discard while filesystem.
+ *
+ * Usage: fstrim [options] <mount point>
+ */
+
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <getopt.h>
+
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <linux/fs.h>
+
+#ifndef FITRIM
+struct fstrim_range {
+	uint64_t start;
+	uint64_t len;
+	uint64_t minlen;
+};
+#define FITRIM		_IOWR('X', 121, struct fstrim_range)
+#endif
+
+const char *program_name = "fstrim";
+
+struct options {
+	struct fstrim_range *range;
+	char mpoint[PATH_MAX];
+	char verbose;
+};
+
+static void usage(void)
+{
+	fprintf(stderr,
+		"Usage: %s [-s start] [-l length] [-m minimum-extent]"
+		" [-v] {mountpoint}\n\t"
+		"-s Starting Byte to discard from\n\t"
+		"-l Number of Bytes to discard from the start\n\t"
+		"-m Minimum extent length to discard\n\t"
+		"-v Verbose - number of discarded bytes\n",
+		program_name);
+}
+
+static void err_exit(const char *fmt, ...)
+{
+	va_list pvar;
+	va_start(pvar, fmt);
+	vfprintf(stderr, fmt, pvar);
+	va_end(pvar);
+	usage();
+	exit(EXIT_FAILURE);
+}
+
+/**
+ * Get the number from argument. It can be number followed by
+ * units: k|K, m|M, g|G, t|T
+ */
+static unsigned long long get_number(char **optarg)
+{
+	char *opt, *end;
+	unsigned long long number, max;
+
+	/* get the max to avoid overflow */
+	max = ULLONG_MAX / 1024;
+	number = 0;
+	opt = *optarg;
+
+	if (*opt == '-') {
+		err_exit("%s: %s (%s)\n", program_name,
+			 strerror(ERANGE), *optarg);
+	}
+
+	errno = 0;
+	number = strtoul(opt, &end , 0);
+	if (errno)
+		err_exit("%s: %s (%s)\n", program_name,
+			 strerror(errno), *optarg);
+
+	/*
+	 * Convert units to numbers. Fall-through stack is used for units
+	 * so absence of breaks is intentional.
+	 */
+	switch (*end) {
+	case 'T': /* terabytes */
+	case 't':
+		if (number > max)
+			err_exit("%s: %s (%s)\n", program_name,
+				 strerror(ERANGE), *optarg);
+		number *= 1024;
+	case 'G': /* gigabytes */
+	case 'g':
+		if (number > max)
+			err_exit("%s: %s (%s)\n", program_name,
+				 strerror(ERANGE), *optarg);
+		number *= 1024;
+	case 'M': /* megabytes */
+	case 'm':
+		if (number > max)
+			err_exit("%s: %s (%s)\n", program_name,
+				 strerror(ERANGE), *optarg);
+		number *= 1024;
+	case 'K': /* kilobytes */
+	case 'k':
+		if (number > max)
+			err_exit("%s: %s (%s)\n", program_name,
+				 strerror(ERANGE), *optarg);
+		number *= 1024;
+		end++;
+	case '\0': /* end of the string */
+		break;
+	default:
+		err_exit("%s: %s (%s)\n", program_name,
+			 strerror(EINVAL), *optarg);
+		return 0;
+	}
+
+	if (*end != '\0') {
+		err_exit("%s: %s (%s)\n", program_name,
+			 strerror(EINVAL), *optarg);
+	}
+
+	return number;
+}
+
+static int parse_opts(int argc, char **argv, struct options *opts)
+{
+	int c;
+
+	while ((c = getopt(argc, argv, "s:l:m:v")) != EOF) {
+		switch (c) {
+		case 's': /* starting point */
+			opts->range->start = get_number(&optarg);
+			break;
+		case 'l': /* length */
+			opts->range->len = get_number(&optarg);
+			break;
+		case 'm': /* minlen */
+			opts->range->minlen = get_number(&optarg);
+			break;
+		case 'v': /* verbose */
+			opts->verbose = 1;
+			break;
+		default:
+			return EXIT_FAILURE;
+		}
+	}
+
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	struct options *opts;
+	struct stat sb;
+	int fd, err = 0, ret = EXIT_FAILURE;
+
+	opts = malloc(sizeof(struct options));
+	if (!opts)
+		err_exit("%s: malloc(): %s\n", program_name, strerror(errno));
+
+	opts->range = NULL;
+	opts->verbose = 0;
+
+	if (argc > 1)
+		strncpy(opts->mpoint, argv[argc - 1], sizeof(opts->mpoint));
+
+	opts->range = calloc(1, sizeof(struct fstrim_range));
+	if (!opts->range) {
+		fprintf(stderr, "%s: calloc(): %s\n", program_name,
+			strerror(errno));
+		goto free_opts;
+	}
+
+	opts->range->len = ULLONG_MAX;
+
+	if (argc > 2)
+		err = parse_opts(argc, argv, opts);
+
+	if (err) {
+		usage();
+		goto free_opts;
+	}
+
+	if (strnlen(opts->mpoint, 1) < 1) {
+		fprintf(stderr, "%s: You have to specify mount point.\n",
+			program_name);
+		usage();
+		goto free_opts;
+	}
+
+	if (stat(opts->mpoint, &sb) == -1) {
+		fprintf(stderr, "%s: %s: %s\n", program_name,
+			opts->mpoint, strerror(errno));
+		usage();
+		goto free_opts;
+	}
+
+	if (!S_ISDIR(sb.st_mode)) {
+		fprintf(stderr, "%s: %s: (%s)\n", program_name,
+			opts->mpoint, strerror(ENOTDIR));
+		usage();
+		goto free_opts;
+	}
+
+	fd = open(opts->mpoint, O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, "%s: open(%s): %s\n", program_name,
+			opts->mpoint, strerror(errno));
+		goto free_opts;
+	}
+
+	if (ioctl(fd, FITRIM, opts->range)) {
+		fprintf(stderr, "%s: FSTRIM: %s\n", program_name,
+			strerror(errno));
+		goto free_opts;
+	}
+
+	if ((opts->verbose) && (opts->range))
+		fprintf(stdout, "%lu Bytes were trimmed\n", opts->range->len);
+
+	ret = EXIT_SUCCESS;
+
+free_opts:
+	if (opts) {
+		if (opts->range)
+			free(opts->range);
+		free(opts);
+	}
+
+	return ret;
+}
-- 
1.7.2.3

_______________________________________________
xfs mailing list
xfs@oss.sgi.com
http://oss.sgi.com/mailman/listinfo/xfs

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-12-21 15:02 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-26 14:12 [PATCH] Add test 248: Check filesystem FITRIM implementation Lukas Czerner
2010-12-06 10:20 ` Lukas Czerner
2010-12-09  5:15 ` Eric Sandeen
2010-12-09 11:02   ` Lukas Czerner
2010-12-09 11:11     ` Lukas Czerner
2010-12-10 15:53 Lukas Czerner
2010-12-17  9:32 ` Lukas Czerner
2010-12-20 17:41 ` Christoph Hellwig
2010-12-21 10:41   ` Lukas Czerner
2010-12-21 11:03     ` Phil Karn
2010-12-21 11:22       ` Christoph Hellwig
2010-12-21 15:04 ` Christoph Hellwig

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.