zed.d/history_event-zfs-list-cacher.sh.in: parallelise, simplify

This:
  (a) improves the error log message,
  (b) locks per pool instead of globally,
  (c) locks the actual output file instead of /var/lock/zfs-list,
      which would otherwise linger there forever (well, still will,
      but you can remove it and it won't come back), and
  (d) preserves attributes of the output file
      instead of reverting them to 0:0 644

It is imperative that the previous commit
("zed-functions.sh: zed_lock(): don't truncate lock")
be included in any series that contains this one

Reviewed-by: Tony Hutter <hutter2@llnl.gov>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #12042
This commit is contained in:
наб 2021-05-14 04:22:34 +02:00 committed by Brian Behlendorf
parent e20c9330d7
commit 1fcfc21cd8
1 changed files with 9 additions and 10 deletions

View File

@ -3,9 +3,8 @@
# Track changes to enumerated pools for use in early-boot # Track changes to enumerated pools for use in early-boot
set -ef set -ef
FSLIST_DIR="@sysconfdir@/zfs/zfs-list.cache" FSLIST="@sysconfdir@/zfs/zfs-list.cache/${ZEVENT_POOL}"
FSLIST_TMP="@runstatedir@/zfs-list.cache.new" FSLIST_TMP="@runstatedir@/zfs-list.cache@${ZEVENT_POOL}"
FSLIST="${FSLIST_DIR}/${ZEVENT_POOL}"
# If the pool specific cache file is not writeable, abort # If the pool specific cache file is not writeable, abort
[ -w "${FSLIST}" ] || exit 0 [ -w "${FSLIST}" ] || exit 0
@ -19,15 +18,15 @@ zed_check_cmd "${ZFS}" sort diff
# If we are acting on a snapshot, we have nothing to do # If we are acting on a snapshot, we have nothing to do
[ "${ZEVENT_HISTORY_DSNAME%@*}" = "${ZEVENT_HISTORY_DSNAME}" ] || exit 0 [ "${ZEVENT_HISTORY_DSNAME%@*}" = "${ZEVENT_HISTORY_DSNAME}" ] || exit 0
# We obtain a lock on zfs-list to avoid any simultaneous writes. # We lock the output file to avoid simultaneous writes.
# If we run into trouble, log and drop the lock # If we run into trouble, log and drop the lock
abort_alter() { abort_alter() {
zed_log_msg "Error updating zfs-list.cache!" zed_log_msg "Error updating zfs-list.cache for ${ZEVENT_POOL}!"
zed_unlock zfs-list zed_unlock "${FSLIST}"
} }
finished() { finished() {
zed_unlock zfs-list zed_unlock "${FSLIST}"
trap - EXIT trap - EXIT
exit 0 exit 0
} }
@ -37,7 +36,7 @@ case "${ZEVENT_HISTORY_INTERNAL_NAME}" in
;; ;;
export) export)
zed_lock zfs-list zed_lock "${FSLIST}"
trap abort_alter EXIT trap abort_alter EXIT
echo > "${FSLIST}" echo > "${FSLIST}"
finished finished
@ -63,7 +62,7 @@ case "${ZEVENT_HISTORY_INTERNAL_NAME}" in
;; ;;
esac esac
zed_lock zfs-list zed_lock "${FSLIST}"
trap abort_alter EXIT trap abort_alter EXIT
PROPS="name,mountpoint,canmount,atime,relatime,devices,exec\ PROPS="name,mountpoint,canmount,atime,relatime,devices,exec\
@ -79,7 +78,7 @@ PROPS="name,mountpoint,canmount,atime,relatime,devices,exec\
sort "${FSLIST_TMP}" -o "${FSLIST_TMP}" sort "${FSLIST_TMP}" -o "${FSLIST_TMP}"
# Don't modify the file if it hasn't changed # Don't modify the file if it hasn't changed
diff -q "${FSLIST_TMP}" "${FSLIST}" || mv "${FSLIST_TMP}" "${FSLIST}" diff -q "${FSLIST_TMP}" "${FSLIST}" || cat "${FSLIST_TMP}" > "${FSLIST}"
rm -f "${FSLIST_TMP}" rm -f "${FSLIST_TMP}"
finished finished