From owner-freebsd-bugs@FreeBSD.ORG Thu Feb 7 23:30:01 2013 Return-Path: Delivered-To: freebsd-bugs@smarthost.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 2A9BAC43 for ; Thu, 7 Feb 2013 23:30:01 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) by mx1.freebsd.org (Postfix) with ESMTP id 0C80E6FD for ; Thu, 7 Feb 2013 23:30:01 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.6/8.14.6) with ESMTP id r17NU0e0018856 for ; Thu, 7 Feb 2013 23:30:00 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.6/8.14.6/Submit) id r17NU0qj018852; Thu, 7 Feb 2013 23:30:00 GMT (envelope-from gnats) Resent-Date: Thu, 7 Feb 2013 23:30:00 GMT Resent-Message-Id: <201302072330.r17NU0qj018852@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Dan Nelson 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 ; 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 ; 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 ; 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 ; 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 To: FreeBSD-gnats-submit@freebsd.org X-Send-Pr-Version: 3.114 Subject: bin/175943: [PATCH] Add trim capability to gpart X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Dan Nelson List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Feb 2013 23:30:01 -0000 >Number: 175943 >Category: bin >Synopsis: [PATCH] Add trim capability to gpart >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >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: >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: >Unformatted: