From owner-svn-src-all@freebsd.org Thu Mar 21 11:32:53 2019 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 5BFAC1534D4D; Thu, 21 Mar 2019 11:32:53 +0000 (UTC) (envelope-from eugen@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id EAD8286F40; Thu, 21 Mar 2019 11:32:52 +0000 (UTC) (envelope-from eugen@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id B6B4582F6; Thu, 21 Mar 2019 11:32:52 +0000 (UTC) (envelope-from eugen@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x2LBWqDu000282; Thu, 21 Mar 2019 11:32:52 GMT (envelope-from eugen@FreeBSD.org) Received: (from eugen@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x2LBWq13000281; Thu, 21 Mar 2019 11:32:52 GMT (envelope-from eugen@FreeBSD.org) Message-Id: <201903211132.x2LBWq13000281@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: eugen set sender to eugen@FreeBSD.org using -f From: Eugene Grosbein Date: Thu, 21 Mar 2019 11:32:52 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r345375 - stable/11/usr.sbin/trim X-SVN-Group: stable-11 X-SVN-Commit-Author: eugen X-SVN-Commit-Paths: stable/11/usr.sbin/trim X-SVN-Commit-Revision: 345375 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: EAD8286F40 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.98 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.998,0]; NEURAL_HAM_SHORT(-0.98)[-0.982,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Mar 2019 11:32:53 -0000 Author: eugen Date: Thu Mar 21 11:32:52 2019 New Revision: 345375 URL: https://svnweb.freebsd.org/changeset/base/345375 Log: MFC r345130,r345184: trim(8): add another safety net and more user-friendly error message in verbose mode. Modified: stable/11/usr.sbin/trim/trim.c Directory Properties: stable/11/ (props changed) Modified: stable/11/usr.sbin/trim/trim.c ============================================================================== --- stable/11/usr.sbin/trim/trim.c Thu Mar 21 11:23:15 2019 (r345374) +++ stable/11/usr.sbin/trim/trim.c Thu Mar 21 11:32:52 2019 (r345375) @@ -2,7 +2,7 @@ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2019 Eugene Grosbein . - * All rights reserved. + * Contains code written by Alan Somers . * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -40,12 +40,14 @@ #include #include #include +#include #include #include #include __FBSDID("$FreeBSD$"); +static bool candelete(int fd); static off_t getsize(const char *path); static int opendev(const char *path, int flags); static int trim(const char *path, off_t offset, off_t length, bool dryrun, bool verbose); @@ -104,6 +106,23 @@ main(int argc, char **argv) /* NOTREACHED */ } + /* + * Safety net: do not allow mistakes like + * + * trim -f /dev/da0 -r rfile + * + * This would trim whole device then error on non-existing file -r. + * Following check prevents this while allowing this form still: + * + * trim -f -- /dev/da0 -r rfile + */ + + if (strcmp(argv[optind-1], "--") != 0) { + for (ch = optind; ch < argc; ch++) + if (argv[ch][0] == '-') + usage(name); + } + argv += optind; argc -= optind; @@ -117,6 +136,19 @@ main(int argc, char **argv) return (error ? EXIT_FAILURE : EXIT_SUCCESS); } +static bool +candelete(int fd) +{ + struct diocgattr_arg arg; + + strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); + arg.len = sizeof(arg.value.i); + if (ioctl(fd, DIOCGATTR, &arg) == 0) + return (arg.value.i != 0); + else + return (false); +} + static int opendev(const char *path, int flags) { @@ -193,9 +225,12 @@ trim(const char *path, off_t offset, off_t length, boo arg[1] = length; error = ioctl(fd, DIOCGDELETE, arg); - if (error < 0) - warn("ioctl(DIOCGDELETE) failed: %s", path); - + if (error < 0) { + if (errno == EOPNOTSUPP && verbose && !candelete(fd)) + warnx("%s: TRIM/UNMAP not supported by driver", path); + else + warn("ioctl(DIOCGDELETE) failed: %s", path); + } close(fd); return (error); }