From d54dcf1accf2c7afd399a5770ba91bb3497b4aec Mon Sep 17 00:00:00 2001 From: asomers Date: Fri, 16 Dec 2016 20:10:55 +0000 Subject: [PATCH] MFC r308806 Speed up pw operations that edit /etc/group or /etc/passwd r285050 fixed a bug in pw that could lead to /etc/passwd or /etc/group corruption on power loss. However, it fixed it by opening those files with O_SYNC, which is very slow, especially on ZFS. This change replaces O_SYNC with appropriately placed fsync()s instead, which is much faster. Using a ZFS tmpdir, the time to run pw's kyua tests drops from 245s to 35s. git-svn-id: svn://svn.freebsd.org/base/stable/10@310173 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- lib/libutil/gr_util.c | 2 +- lib/libutil/pw_util.c | 2 +- usr.sbin/pw/grupd.c | 1 + usr.sbin/pw/pw_nis.c | 1 + usr.sbin/pw/pwupd.c | 1 + 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/libutil/gr_util.c b/lib/libutil/gr_util.c index 80d9ee674..45bb9f80e 100644 --- a/lib/libutil/gr_util.c +++ b/lib/libutil/gr_util.c @@ -141,7 +141,7 @@ gr_tmp(int mfd) errno = ENAMETOOLONG; return (-1); } - if ((tfd = mkostemp(tempname, O_SYNC)) == -1) + if ((tfd = mkostemp(tempname, 0)) == -1) return (-1); if (mfd != -1) { while ((nr = read(mfd, buf, sizeof(buf))) > 0) diff --git a/lib/libutil/pw_util.c b/lib/libutil/pw_util.c index af749d524..51e744170 100644 --- a/lib/libutil/pw_util.c +++ b/lib/libutil/pw_util.c @@ -226,7 +226,7 @@ pw_tmp(int mfd) errno = ENAMETOOLONG; return (-1); } - if ((tfd = mkostemp(tempname, O_SYNC)) == -1) + if ((tfd = mkostemp(tempname, 0)) == -1) return (-1); if (mfd != -1) { while ((nr = read(mfd, buf, sizeof(buf))) > 0) diff --git a/usr.sbin/pw/grupd.c b/usr.sbin/pw/grupd.c index 38d5e5bc9..5d0df0263 100644 --- a/usr.sbin/pw/grupd.c +++ b/usr.sbin/pw/grupd.c @@ -77,6 +77,7 @@ gr_update(struct group * grp, char const * group) close(tfd); err(1, "gr_copy()"); } + fsync(tfd); close(tfd); if (gr_mkdb() == -1) { gr_fini(); diff --git a/usr.sbin/pw/pw_nis.c b/usr.sbin/pw/pw_nis.c index 35b26ea52..087c9460f 100644 --- a/usr.sbin/pw/pw_nis.c +++ b/usr.sbin/pw/pw_nis.c @@ -67,6 +67,7 @@ pw_nisupdate(const char * path, struct passwd * pwd, char const * user) close(tfd); err(1, "pw_copy()"); } + fsync(tfd); close(tfd); if (chmod(pw_tempname(), 0644) == -1) err(1, "chmod()"); diff --git a/usr.sbin/pw/pwupd.c b/usr.sbin/pw/pwupd.c index 3bcb95f8c..87c3c994b 100644 --- a/usr.sbin/pw/pwupd.c +++ b/usr.sbin/pw/pwupd.c @@ -114,6 +114,7 @@ pw_update(struct passwd * pwd, char const * user) close(tfd); err(1, "pw_copy()"); } + fsync(tfd); close(tfd); /* * in case of deletion of a user, the whole database -- 2.45.0