From owner-freebsd-hackers@freebsd.org Sun Jul 24 05:39:21 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 2B09DBA1465 for ; Sun, 24 Jul 2016 05:39:21 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id A28E91DFB for ; Sun, 24 Jul 2016 05:39:20 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id u6O5dFJL010337 (version=TLSv1 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO); Sun, 24 Jul 2016 08:39:15 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua u6O5dFJL010337 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id u6O5dFop010336; Sun, 24 Jul 2016 08:39:15 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sun, 24 Jul 2016 08:39:15 +0300 From: Konstantin Belousov To: Julian Hsiao Cc: freebsd-hackers@freebsd.org Subject: Re: fsync(1) patch to do DIOCGFLUSH on character devices Message-ID: <20160724053914.GV38613@kib.kiev.ua> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.6.1 (2016-04-27) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 24 Jul 2016 05:39:21 -0000 On Sun, Jul 24, 2016 at 03:04:34AM +0000, Julian Hsiao wrote: > Hi, > > The fsync(1) utility is a simple wrapper around fsync(2), but I didn't find > a similar utility for calling ioctl(DIOCGFLUSH) for character devices. It is not similar in any reasonable interpretation of the word. > fsync(2) is a no-op on character device files, which is a little surprising > but makes sense I suppose (would raising EINVAL be against POSIX?). POSIX does not mention special nodes at all, so the implementation is free to do whatever it wants/needs. > > However, conceptually the goal is the same: commit writes to permanent > storage, so here's a small patch to fsync(1) that calls ioctl(DIOCGFLUSH) > for character device files instead. This ioctl() does not perform what you describe, at least not on the level of the buffer cache/page cache for the device. It is the duty of the filesystem (or other in-kernel consumer) to ensure that cache is flushed when needed. The ioctl only sends BIO_FLUSH command to the driver, which does something to the hardware. Oftent this 'something' is either nop or have no observable implications. That said, I do not see the proposed change as useful. > > Incidentally, turns out you need to open character device files with O_RDWR > (or maybe O_WRONLY is enough, I didn't check) for DIOCGFLUSH, but fsync(2) > only needs O_RDONLY for plain files. This seems a little odd; does anyone > know why? > > Julian Hsiao > > Index: usr.bin/fsync/fsync.1 > =================================================================== > diff --git a/head/usr.bin/fsync/fsync.1 b/head/usr.bin/fsync/fsync.1 > --- a/head/usr.bin/fsync/fsync.1 (revision 303213) > +++ b/head/usr.bin/fsync/fsync.1 (working copy) > @@ -44,6 +44,8 @@ > .Nm > utility uses the > .Xr fsync 2 > +or the > +.Xr ioctl 2 > function call. > .Sh EXIT STATUS > If an error occurs, the > Index: usr.bin/fsync/fsync.c > =================================================================== > diff --git a/head/usr.bin/fsync/fsync.c b/head/usr.bin/fsync/fsync.c > --- a/head/usr.bin/fsync/fsync.c (revision 303213) > +++ b/head/usr.bin/fsync/fsync.c (working copy) > @@ -35,6 +35,10 @@ > #include > #include > #include > +#include > +#include > +#include > +#include > > static void usage(void); > > @@ -44,6 +48,7 @@ > int fd; > int i; > int rval; > + struct stat sb; > > if (argc < 2) { > usage(); > @@ -58,11 +63,35 @@ > rval = EX_NOINPUT; > continue; > } > + memset(&sb, 0, sizeof(sb)); > + if (fstat(fd, &sb) == -1) { > + warn("fstat %s", argv[i]); > + rval = EX_OSERR; > + continue; > + } > > - if (fsync(fd) == -1) { > - warn("fsync %s", argv[i]); > - if (rval == EX_OK) > + if (S_ISCHR(sb.st_mode)) { > + if (close(fd) == -1) { > + warn("close %s", argv[i]); > rval = EX_OSERR; > + continue; > + } > + if ((fd = open(argv[i], O_RDWR)) == -1) { > + warn("open %s", argv[i]); > + rval = EX_NOINPUT; > + continue; > + } > + > + if (ioctl(fd, DIOCGFLUSH) == -1) { > + warn("ioctl %s", argv[i]); > + rval = EX_OSERR; > + } > + } else { > + if (fsync(fd) == -1) { > + warn("fsync %s", argv[i]); > + if (rval == EX_OK) > + rval = EX_OSERR; > + } > } > close(fd); > } > > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"