From owner-freebsd-hackers@freebsd.org Fri Jul 3 16:59:48 2015 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 2FC13994878 for ; Fri, 3 Jul 2015 16:59:48 +0000 (UTC) (envelope-from garga.bsd@gmail.com) Received: from mail-qg0-x22e.google.com (mail-qg0-x22e.google.com [IPv6:2607:f8b0:400d:c04::22e]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id DFF6518EA for ; Fri, 3 Jul 2015 16:59:47 +0000 (UTC) (envelope-from garga.bsd@gmail.com) Received: by qgeg89 with SMTP id g89so48540762qge.3 for ; Fri, 03 Jul 2015 09:59:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:content-type:content-transfer-encoding:subject :message-id:date:to:mime-version; bh=wOhp+Ytsfji2an15BNNgpyIzpnfXZeMsd3R3B41HFpM=; b=wVM2tOm6VjEjmu0SIeeqcIuzhtZofrhy/2jP+BzDpl0IdLuYS2On1GggQnODiPdAQN AQyEEos+XBVtlnnmWfpOH3dTG4b3SQiRtC5fK+8gYARYG/ff3rwOju5pFLoPgZj/QBdB O60qCoj1Xqp1pxu/ge99KFyLayTaqnNkZxiSLKg5rEr6PobhIqdUzreTTfrGabnwoI/9 G5+KdtI+IB0WqUBj7LCObvJCH7H0UyQxP03I0j9gXOmLM+H4h7Um6nl3NeXLpCZ78nXM FqMqS/wEna3XS1iaNZx6/USHf6AXyYkp2ujngXHNcQozFAtjpuy1tqv8NPOJ6wsu5Cxq GeqQ== X-Received: by 10.140.148.135 with SMTP id 129mr55529662qhu.24.1435942786938; Fri, 03 Jul 2015 09:59:46 -0700 (PDT) Received: from mbp.home (179-125-141-40.desktop.com.br. [179.125.141.40]) by mx.google.com with ESMTPSA id v64sm4686651qgv.28.2015.07.03.09.59.45 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 03 Jul 2015 09:59:46 -0700 (PDT) Sender: Renato Botelho From: Renato Botelho Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: rename() + fsync() implementation Message-Id: <2770DA5F-D7F2-46D9-9158-10C86115F8AC@FreeBSD.org> Date: Fri, 3 Jul 2015 13:59:43 -0300 To: FreeBSD Hackers Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2102\)) X-Mailer: Apple Mail (2.2102) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 Jul 2015 16:59:48 -0000 Some time ago we found a bug on pfSense and after investigating we = figured out the root cause was passwd / group related tools was not = checking if files were safe in disk, and system ended up with a 0 length = passwd/group db after a power cycle. There are more context at revision = [1]. After that, bapt@ suggest to do similar fix for cap_mkdb and = services_mkdb, that also can be found at another review [2]. Until now there are 3 places with added code similar to: /* * Make sure file is safe on disk. To improve performance we = will call * fsync() to the directory where file lies */ if (rename(tname, dbname) =3D=3D -1 || (dbname_dir =3D dirname(dbname)) =3D=3D NULL || (dbname_dir_fd =3D open(dbname_dir, O_RDONLY|O_DIRECTORY)) = =3D=3D -1 || fsync(dbname_dir_fd) !=3D 0) { if (dbname_dir_fd !=3D -1) close(dbname_dir_fd); err(1, "Cannot rename `%s' to `%s'", tname, dbname); } if (dbname_dir_fd !=3D -1) close(dbname_dir_fd); The idea is to implement a =E2=80=9Csync rename=E2=80=9D function to do = all these steps. I thought about it and IMO lib/libutil would be a good = place to implement it. But since I=E2=80=99m starting to touch src now, = I would like to hear more opinions about this. Thanks! [1] https://reviews.freebsd.org/D2978 [2] https://reviews.freebsd.org/D2982 -- Renato Botelho