From owner-svn-src-all@freebsd.org Thu Aug 1 05:30:32 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 564C3B1203; Thu, 1 Aug 2019 05:30:32 +0000 (UTC) (envelope-from dougm@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 45zf5N1M6kz48J9; Thu, 1 Aug 2019 05:30:32 +0000 (UTC) (envelope-from dougm@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 0FE01D763; Thu, 1 Aug 2019 05:30:32 +0000 (UTC) (envelope-from dougm@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x715UV5q016450; Thu, 1 Aug 2019 05:30:31 GMT (envelope-from dougm@FreeBSD.org) Received: (from dougm@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x715UVEs016449; Thu, 1 Aug 2019 05:30:31 GMT (envelope-from dougm@FreeBSD.org) Message-Id: <201908010530.x715UVEs016449@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: dougm set sender to dougm@FreeBSD.org using -f From: Doug Moore Date: Thu, 1 Aug 2019 05:30:31 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r350495 - stable/12/sbin/swapon X-SVN-Group: stable-12 X-SVN-Commit-Author: dougm X-SVN-Commit-Paths: stable/12/sbin/swapon X-SVN-Commit-Revision: 350495 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 45zf5N1M6kz48J9 X-Spamd-Bar: - Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-1.52 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-0.80)[-0.797,0]; NEURAL_HAM_SHORT(-0.73)[-0.728,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] 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, 01 Aug 2019 05:30:32 -0000 Author: dougm Date: Thu Aug 1 05:30:31 2019 New Revision: 350495 URL: https://svnweb.freebsd.org/changeset/base/350495 Log: MFC r350183, r350359 In trimming on startup, define swapon_trim() to invoke swapon before closing the fd used for trimming so that a geli device isn't detached before swapon is invoked. Add comments to explain the reason for the ordering of events. Reviewed by: alc Approved by: markj (mentor, implicit) Modified: stable/12/sbin/swapon/swapon.c Directory Properties: stable/12/ (props changed) Modified: stable/12/sbin/swapon/swapon.c ============================================================================== --- stable/12/sbin/swapon/swapon.c Thu Aug 1 03:59:25 2019 (r350494) +++ stable/12/sbin/swapon/swapon.c Thu Aug 1 05:30:31 2019 (r350495) @@ -739,16 +739,18 @@ run_cmd(int *ofd, const char *cmdline, ...) return (WEXITSTATUS(status)); } -static void -swap_trim(const char *name) +static int +swapon_trim(const char *name) { struct stat sb; off_t ioarg[2], sz; - int fd; + int error, fd; + /* Open a descriptor to create a consumer of the device. */ fd = open(name, O_WRONLY); if (fd < 0) errx(1, "Cannot open %s", name); + /* Find the device size. */ if (fstat(fd, &sb) < 0) errx(1, "Cannot stat %s", name); if (S_ISREG(sb.st_mode)) @@ -758,11 +760,24 @@ swap_trim(const char *name) err(1, "ioctl(DIOCGMEDIASIZE)"); } else errx(1, "%s has an invalid file type", name); + /* Trim the device. */ ioarg[0] = 0; ioarg[1] = sz; if (ioctl(fd, DIOCGDELETE, ioarg) != 0) warn("ioctl(DIOCGDELETE)"); + + /* Start using the device for swapping, creating a second consumer. */ + error = swapon(name); + + /* + * Do not close the device until the swap pager has attempted to create + * another consumer. For GELI devices created with the 'detach -l' + * option, removing the last consumer causes the device to be detached + * - that is, to disappear. This ordering ensures that the device will + * not be detached until swapoff is called. + */ close(fd); + return (error); } static const char * @@ -770,11 +785,9 @@ swap_on_off_sfile(const char *name, int doingall) { int error; - if (which_prog == SWAPON) { - if (Eflag) - swap_trim(name); - error = swapon(name); - } else /* SWAPOFF */ + if (which_prog == SWAPON) + error = Eflag ? swapon_trim(name) : swapon(name); + else /* SWAPOFF */ error = swapoff(name); if (error == -1) {