From dan@dan.emsphone.com  Thu Feb  7 23:22:43 2013
Return-Path: <dan@dan.emsphone.com>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1])
	by hub.freebsd.org (Postfix) with ESMTP id 0382A900
	for <FreeBSD-gnats-submit@freebsd.org>; Thu,  7 Feb 2013 23:22:43 +0000 (UTC)
	(envelope-from dan@dan.emsphone.com)
Received: from email2.allantgroup.com (email2.emsphone.com [199.67.51.116])
	by mx1.freebsd.org (Postfix) with ESMTP id BE6FC697
	for <FreeBSD-gnats-submit@freebsd.org>; Thu,  7 Feb 2013 23:22:42 +0000 (UTC)
Received: from dan.emsphone.com (dan.emsphone.com [172.17.17.101])
	by email2.allantgroup.com (8.14.5/8.14.5) with ESMTP id r17NLajW031362
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO)
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 7 Feb 2013 17:21:37 -0600 (CST)
	(envelope-from dan@dan.emsphone.com)
Received: from dan.emsphone.com (smmsp@localhost [127.0.0.1])
	by dan.emsphone.com (8.14.6/8.14.6) with ESMTP id r17NLauV093635
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO)
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 7 Feb 2013 17:21:36 -0600 (CST)
	(envelope-from dan@dan.emsphone.com)
Received: (from dan@localhost)
	by dan.emsphone.com (8.14.6/8.14.6/Submit) id r17NLaFc093634;
	Thu, 7 Feb 2013 17:21:36 -0600 (CST)
	(envelope-from dan)
Message-Id: <201302072321.r17NLaFc093634@dan.emsphone.com>
Date: Thu, 7 Feb 2013 17:21:36 -0600 (CST)
From: Dan Nelson <dnelson@allantgroup.com>
Reply-To: Dan Nelson <dnelson@allantgroup.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] Add trim capability to gpart
X-Send-Pr-Version: 3.114
X-GNATS-Notify:

>Number:         175943
>Category:       bin
>Synopsis:       [PATCH] Add trim capability to gpart(8)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bdrewery
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Feb 07 23:30:00 UTC 2013
>Closed-Date:    
>Last-Modified:  Mon Apr 14 13:58:45 CDT 2014
>Originator:     Dan Nelson
>Release:        FreeBSD 9.1-STABLE amd64
>Organization:
>Environment:
System: FreeBSD dan.emsphone.com 9.1-STABLE FreeBSD 9.1-STABLE #652 r246183M: Thu Jan 31 16:32:57 CST 2013 zsh@dan.emsphone.com:/usr/src/sys/amd64/compile/DANSMP amd64


	
>Description:
	

I have occasionally wanted to erase all or part of an SSD device to recover
performance after heavy random write activity.  It can sort-of be done with
either newfs -E or creating+deleting a zfs pool on the area to erase, but
both write a varying amount of data after they send their BIO_DELETE calls,
and there should be a cleaner way to do it.  Attached is a patch that adds a
"trim" verb to the gpart command, which opens the raw device and calls
g_delete on it.  

Feel free to pick a new name if you can think of a better one.  I know that
"trim" refers only to the SATA implementation, but "delete" is already
taken, "erase" implies unconditional erasure which is not true for devices
that ignore BIO_DELETE calls, and most people that want the functionality
refer to it as TRIM anyway.


>How-To-Repeat:
	
>Fix:

	


Index: geom_part.c
===================================================================
--- geom_part.c	(revision 246183)
+++ geom_part.c	(working copy)
@@ -90,6 +90,7 @@
 static void gpart_print_error(const char *);
 static void gpart_backup(struct gctl_req *, unsigned int);
 static void gpart_restore(struct gctl_req *, unsigned int);
+static void gpart_trim(struct gctl_req *, unsigned int);
 
 struct g_command PUBSYM(class_commands)[] = {
 	{ "add", 0, gpart_issue, {
@@ -159,6 +160,9 @@
 		G_OPT_SENTINEL },
 	    "[-l | -r] [-p] [geom ...]"
 	},
+	{ "trim", 0, gpart_trim, G_NULL_OPTS,
+	    "provider"
+	},
 	{ "undo", 0, gpart_issue, G_NULL_OPTS,
 	    "geom"
 	},
@@ -1279,3 +1283,22 @@
 	gctl_free(req);
 	exit(status);
 }
+
+static void
+gpart_trim(struct gctl_req *req, unsigned int fl __unused)
+{
+	const char *s;
+	int fd;
+
+	if (gctl_get_int(req, "nargs") != 1)
+		errx(EXIT_FAILURE, "Invalid number of arguments.");
+	s = gctl_get_ascii(req, "arg0");
+	if (s == NULL)
+		abort();
+	fd = g_open(s, 1);
+	if (fd == -1)
+		err(EXIT_FAILURE, "Cannot open %s", s);
+	if (g_delete(fd, 0, g_mediasize(fd)) == -1)
+		err(EXIT_FAILURE, "g_delete call failed");
+	g_close(fd);
+}
Index: gpart.8
===================================================================
--- gpart.8	(revision 246183)
+++ gpart.8	(working copy)
@@ -144,6 +144,10 @@
 .Op Fl l | r
 .Op Fl p
 .Op Ar geom ...
+.\" ==== TRIM ====
+.Nm
+.Cm trim
+.Ar provider
 .\" ==== UNDO ====
 .Nm
 .Cm undo
@@ -471,6 +475,15 @@
 .It Fl r
 Show raw partition type instead of symbolic name.
 .El
+.\" ==== TRIM ====
+.It Cm trim
+Sends a BIO_DELETE request for the contents of the provider 
+.Ar provider .
+Depending on the underlying storage device, this may may fill its blocks
+with a constant value (0x00 or 0xFF), or may do nothing.  Running this
+command on a partition on an SSD device before deleting it may improve
+performance, since the SSD can immediately reuse the blocks for subsequent
+write requests.
 .\" ==== UNDO ====
 .It Cm undo
 Revert any pending changes for geom
>Release-Note:
>Audit-Trail:

From: Dan Nelson <dnelson@allantgroup.com>
To: FreeBSD-gnats-submit@FreeBSD.org, freebsd-bugs@FreeBSD.org
Cc:  
Subject: Re: bin/175943: [PATCH] Add trim capability to gpart
Date: Thu, 7 Feb 2013 17:36:32 -0600

 Oops.  Small typo in the man page:
 
 "this may may fill its blocks" should be "this may fill its blocks"
Responsible-Changed-From-To: freebsd-bugs->freebsd-geom 
Responsible-Changed-By: ae 
Responsible-Changed-When: Tue Jun 18 13:29:58 UTC 2013 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=175943 
Responsible-Changed-From-To: freebsd-geom->smh 
Responsible-Changed-By: smh 
Responsible-Changed-When: Tue Jun 18 13:40:50 UTC 2013 
Responsible-Changed-Why:  
I'll take it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=175943 
Responsible-Changed-From-To: smh->bdrewery 
Responsible-Changed-By: bdrewery 
Responsible-Changed-When: Mon Apr 14 13:58:44 CDT 2014 
Responsible-Changed-Why:  
I'll take it. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=175943 
>Unformatted:
