# Udev rules for device-mapper devices.
# See /usr/share/doc/dmsetup/README.udev for further information.

KERNEL=="device-mapper", NAME="mapper/control"

SUBSYSTEM!="block", GOTO="dm_end"
KERNEL!="dm-[0-9]*", GOTO="dm_end"

# There is a new change event generated in block layer since kernel
# version 2.6.32. It adds notification for changes in read-only
# attribute. We don't want to misinterpret the regular meaning of change
# events for DM devices and we don't want to apply the rules prematurely,
# therefore we disable them in this situation.
ENV{DISK_RO}=="1", GOTO="dm_disable"

# Decode udev control flags and set environment variables appropriately.
# These flags are encoded in DM_COOKIE variable that was introduced in
# kernel version 2.6.31. Therefore, we can use this feature with
# kernels >= 2.6.31 only.
ENV{DM_COOKIE}=="?*", IMPORT{program}="/sbin/dmsetup udevflags $env{DM_COOKIE}"
ENV{DM_COOKIE}=="?*", RUN+="/sbin/dmsetup udevcomplete $env{DM_COOKIE}"

# Normally, we would test for DM_UDEV_DISABLE_DM_RULES_FLAG here and skip
# the rules if set. However, we need to set DM_* environment variables
# for now to properly filter out inappropriate events. This dependency
# might be removed in the future.

ACTION!="add|change", GOTO="dm_end"

# There is no cookie set nor any flags encoded in events not originating
# in libdevmapper so we need to detect this and try to behave correctly.
# For such spurious events, regenerate all flags from current udev database content
# (this information would normally be inaccessible for spurious ADD and CHANGE events).
ENV{DM_UDEV_PRIMARY_SOURCE_FLAG}=="1", GOTO="dm_flags_done"
IMPORT{db}="DM_UDEV_DISABLE_DM_RULES_FLAG"
IMPORT{db}="DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG"
IMPORT{db}="DM_UDEV_DISABLE_DISK_RULES_FLAG"
IMPORT{db}="DM_UDEV_DISABLE_OTHER_RULES_FLAG"
IMPORT{db}="DM_UDEV_LOW_PRIORITY_FLAG"
IMPORT{db}="DM_UDEV_DISABLE_LIBRARY_FALLBACK_FLAG"
IMPORT{db}="DM_UDEV_FLAG7"
IMPORT{db}="DM_SUBSYSTEM_UDEV_FLAG0"
IMPORT{db}="DM_SUBSYSTEM_UDEV_FLAG1"
IMPORT{db}="DM_SUBSYSTEM_UDEV_FLAG2"
IMPORT{db}="DM_SUBSYSTEM_UDEV_FLAG3"
IMPORT{db}="DM_SUBSYSTEM_UDEV_FLAG4"
IMPORT{db}="DM_SUBSYSTEM_UDEV_FLAG5"
IMPORT{db}="DM_SUBSYSTEM_UDEV_FLAG6"
IMPORT{db}="DM_SUBSYSTEM_UDEV_FLAG7"
LABEL="dm_flags_done"

# Normally, we operate on "change" events only. But when
# coldplugging, there's an "add" event present. We have to
# recognize this and do our actions in this particular
# situation, too. Also, we don't want the nodes to be
# created prematurely on "add" events while not coldplugging.
ACTION=="add", ENV{STARTUP}!="1", NAME="", GOTO="dm_end"

# "dm" sysfs subdirectory is available in newer versions of DM
# only (kernels >= 2.6.29). We have to check for its existence
# and use dmsetup tool instead to get the DM name, uuid and 
# suspended state if the "dm" subdirectory is not present.
# The "suspended" item was added even later (kernels >= 2.6.31),
# so we also have to call dmsetup if the kernel version used
# is in between these releases.
TEST=="dm", ENV{DM_NAME}="$attr{dm/name}", ENV{DM_UUID}="$attr{dm/uuid}", ENV{DM_SUSPENDED}="$attr{dm/suspended}"
TEST!="dm", IMPORT{program}="/sbin/dmsetup info -j %M -m %m -c --nameprefixes --noheadings --rows -o name,uuid,suspended"
ENV{DM_SUSPENDED}!="?*", IMPORT{program}="/sbin/dmsetup info -j %M -m %m -c --nameprefixes --noheadings --rows -o suspended"

# dmsetup tool provides suspended state information in textual
# form with values "Suspended"/"Active". We translate it to
# 0/1 respectively to be consistent with sysfs values.
ENV{DM_SUSPENDED}=="Active", ENV{DM_SUSPENDED}="0"
ENV{DM_SUSPENDED}=="Suspended", ENV{DM_SUSPENDED}="1"

ENV{DM_UDEV_RULES}="1"

ENV{DM_NAME}=="?*", SYMLINK+="mapper/$env{DM_NAME}"

# We have to ignore further rule application for inappropriate events
# and devices. But still send the notification if cookie exists.
ENV{DM_UUID}=="mpath-?*", ENV{DM_ACTION}=="PATH_FAILED", ENV{DM_UDEV_DISABLE_OTHER_RULES_FLAG}="1"
ENV{DM_UUID}=="CRYPT-TEMP-?*", ENV{DM_UDEV_DISABLE_OTHER_RULES_FLAG}="1"
ENV{DM_UUID}!="?*", ENV{DM_NAME}=="temporary-cryptsetup-?*", ENV{DM_UDEV_DISABLE_OTHER_RULES_FLAG}="1"

ENV{DM_SUSPENDED}=="1", ENV{DM_UDEV_DISABLE_DISK_RULES_FLAG}="1"

ENV{DM_UDEV_DISABLE_OTHER_RULES_FLAG}!="", ENV{DM_UDEV_DISABLE_DISK_RULES_FLAG}="1"

GOTO="dm_end"

LABEL="dm_disable"
ENV{DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG}="1"
ENV{DM_UDEV_DISABLE_DISK_RULES_FLAG}="1"
ENV{DM_UDEV_DISABLE_OTHER_RULES_FLAG}="1"

LABEL="dm_end"
