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
31 static char *unblock_path
;
34 * udev_is_available() - Checks for udev in the system.
36 * Function looks whether udev directories are available and MDADM_NO_UDEV env defined.
39 * true if udev is available,
42 bool udev_is_available(void)
46 if (stat("/dev/.udev", &stb
) != 0 &&
47 stat("/run/udev", &stb
) != 0)
49 if (check_env("MDADM_NO_UDEV") == 1)
56 static struct udev
*udev
;
57 static struct udev_monitor
*udev_monitor
;
60 * udev_release() - Drops references of udev and udev_monitor.
62 static void udev_release(void)
64 udev_monitor_unref(udev_monitor
);
69 * udev_initialize() - Initializes udev and udev_monitor structures.
71 * Function initializes udev, udev_monitor, and sets udev_monitor filter for block devices.
74 * UDEV_STATUS_SUCCESS on success
75 * UDEV_STATUS_ERROR on error
76 * UDEV_STATUS_ERROR_NO_UDEV when udev not available
78 static enum udev_status
udev_initialize(void)
80 if (!udev_is_available()) {
82 return UDEV_STATUS_ERROR_NO_UDEV
;
87 pr_err("Cannot initialize udev.\n");
88 return UDEV_STATUS_ERROR
;
91 udev_monitor
= udev_monitor_new_from_netlink(udev
, "udev");
93 pr_err("Cannot initialize udev monitor.\n");
94 udev
= udev_unref(udev
);
95 return UDEV_STATUS_ERROR
;
98 if (udev_monitor_filter_add_match_subsystem_devtype(udev_monitor
, "block", 0) < 0) {
99 pr_err("Cannot add udev monitor event filter for md devices.\n");
101 return UDEV_STATUS_ERROR
;
103 if (udev_monitor_enable_receiving(udev_monitor
) < 0) {
104 pr_err("Cannot enable receiving udev events through udev monitor.\n");
106 return UDEV_STATUS_ERROR
;
108 atexit(udev_release
);
109 return UDEV_STATUS_SUCCESS
;
113 * udev_wait_for_events() - Waits for events from udev.
114 * @seconds: Timeout in seconds.
116 * Function waits udev events, wakes up on event or timeout.
119 * UDEV_STATUS_SUCCESS on detected event
120 * UDEV_STATUS_TIMEOUT on timeout
121 * UDEV_STATUS_ERROR on error
123 enum udev_status
udev_wait_for_events(int seconds
)
130 if (!udev
|| !udev_monitor
) {
131 ret
= udev_initialize();
132 if (ret
!= UDEV_STATUS_SUCCESS
)
136 fd
= udev_monitor_get_fd(udev_monitor
);
138 pr_err("Cannot access file descriptor associated with udev monitor.\n");
139 return UDEV_STATUS_ERROR
;
143 FD_SET(fd
, &readfds
);
147 if (select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
) > 0 && FD_ISSET(fd
, &readfds
))
148 if (udev_monitor_receive_device(udev_monitor
))
149 return UDEV_STATUS_SUCCESS
; /* event detected */
150 return UDEV_STATUS_TIMEOUT
;
155 * udev_block() - Block udev from examining newly created arrays.
157 * When array is created, we don't want udev to examine it immediately.
158 * Function creates /run/mdadm/creating-mdXXX and expects that udev rule
159 * will notice it and act accordingly.
162 * UDEV_STATUS_SUCCESS when successfully blocked udev
163 * UDEV_STATUS_ERROR on error
165 enum udev_status
udev_block(char *devnm
)
168 char *path
= xcalloc(1, BUFSIZ
);
170 snprintf(path
, BUFSIZ
, "/run/mdadm/creating-%s", devnm
);
172 fd
= open(path
, O_CREAT
| O_RDWR
, 0600);
173 if (!is_fd_valid(fd
)) {
174 pr_err("Cannot block udev, error creating blocking file.\n");
175 pr_err("%s: %s\n", strerror(errno
), path
);
177 return UDEV_STATUS_ERROR
;
182 return UDEV_STATUS_SUCCESS
;
186 * udev_unblock() - Unblock udev.
188 void udev_unblock(void)
191 unlink(unblock_path
);