2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2022 Mateusz Grzonka <mateusz.grzonka@intel.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 static char *unblock_path
;
37 * udev_is_available() - Checks for udev in the system.
39 * Function looks whether udev directories are available and MDADM_NO_UDEV env defined.
42 * true if udev is available,
45 bool udev_is_available(void)
49 if (stat("/dev/.udev", &stb
) != 0 &&
50 stat("/run/udev", &stb
) != 0)
52 if (check_env("MDADM_NO_UDEV") == 1)
59 static struct udev
*udev
;
60 static struct udev_monitor
*udev_monitor
;
63 * udev_release() - Drops references of udev and udev_monitor.
65 static void udev_release(void)
67 udev_monitor_unref(udev_monitor
);
72 * udev_initialize() - Initializes udev and udev_monitor structures.
74 * Function initializes udev, udev_monitor, and sets udev_monitor filter for block devices.
77 * UDEV_STATUS_SUCCESS on success
78 * UDEV_STATUS_ERROR on error
79 * UDEV_STATUS_ERROR_NO_UDEV when udev not available
81 static enum udev_status
udev_initialize(void)
83 if (!udev_is_available()) {
85 return UDEV_STATUS_ERROR_NO_UDEV
;
90 pr_err("Cannot initialize udev.\n");
91 return UDEV_STATUS_ERROR
;
94 udev_monitor
= udev_monitor_new_from_netlink(udev
, "udev");
96 pr_err("Cannot initialize udev monitor.\n");
97 udev
= udev_unref(udev
);
98 return UDEV_STATUS_ERROR
;
101 if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor
, "block", 0) < 0) {
102 pr_err("Cannot add udev monitor event filter for md devices.\n");
104 return UDEV_STATUS_ERROR
;
106 if (udev_monitor_enable_receiving(udev_monitor
) < 0) {
107 pr_err("Cannot enable receiving udev events through udev monitor.\n");
109 return UDEV_STATUS_ERROR
;
111 atexit(udev_release
);
112 return UDEV_STATUS_SUCCESS
;
116 * udev_wait_for_events() - Waits for events from udev.
117 * @seconds: Timeout in seconds.
119 * Function waits udev events, wakes up on event or timeout.
122 * UDEV_STATUS_SUCCESS on detected event
123 * UDEV_STATUS_TIMEOUT on timeout
124 * UDEV_STATUS_ERROR on error
126 enum udev_status
udev_wait_for_events(int seconds
)
133 if (!udev
|| !udev_monitor
) {
134 ret
= udev_initialize();
135 if (ret
!= UDEV_STATUS_SUCCESS
)
139 fd
= udev_monitor_get_fd(udev_monitor
);
141 pr_err("Cannot access file descriptor associated with udev monitor.\n");
142 return UDEV_STATUS_ERROR
;
146 FD_SET(fd
, &readfds
);
150 if (select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) > 0 && FD_ISSET(fd
, &readfds
))
151 if (udev_monitor_receive_device(udev_monitor
))
152 return UDEV_STATUS_SUCCESS
; /* event detected */
153 return UDEV_STATUS_TIMEOUT
;
158 * udev_block() - Block udev from examining newly created arrays.
160 * When array is created, we don't want udev to examine it immediately.
161 * Function creates /run/mdadm/creating-mdXXX and expects that udev rule
162 * will notice it and act accordingly.
165 * UDEV_STATUS_SUCCESS when successfully blocked udev
166 * UDEV_STATUS_ERROR on error
168 enum udev_status
udev_block(char *devnm
)
171 char *path
= xcalloc(1, BUFSIZ
);
173 snprintf(path
, BUFSIZ
, "/run/mdadm/creating-%s", devnm
);
175 fd
= open(path
, O_CREAT
| O_RDWR
, 0600);
176 if (!is_fd_valid(fd
)) {
177 pr_err("Cannot block udev, error creating blocking file.\n");
178 pr_err("%s: %s\n", strerror(errno
), path
);
180 return UDEV_STATUS_ERROR
;
185 return UDEV_STATUS_SUCCESS
;
189 * udev_unblock() - Unblock udev.
191 void udev_unblock(void)
194 unlink(unblock_path
);