2 * device-discovery.c: main function, discovering device and processing
3 * pipe request from kernel.
5 * Copyright (c) 2010 EMC Corporation, Haiying Tang <Tang_Haiying@emc.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <sys/mount.h>
33 #include <sys/select.h>
34 #include <linux/kdev_t.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_ioctl.h>
49 #include <libdevmapper.h>
51 #include "device-discovery.h"
53 #define BL_PIPE_FILE "/var/lib/nfs/rpc_pipefs/nfs/blocklayout"
54 #define PID_FILE "/var/run/blkmapd.pid"
56 struct bl_disk *visible_disk_list;
58 struct bl_disk_path *bl_get_path(const char *filepath,
59 struct bl_disk_path *paths)
61 struct bl_disk_path *tmp = paths;
64 if (!strcmp(tmp->full_path, filepath))
71 /* Check whether valid_path is a substring(partition) of path */
72 int bl_is_partition(struct bl_disk_path *valid_path, struct bl_disk_path *path)
74 if (!strncmp(valid_path->full_path, path->full_path,
75 strlen(valid_path->full_path)))
82 * For multipath devices, devices state could be PASSIVE/ACTIVE/PSEUDO,
83 * where PSEUDO > ACTIVE > PASSIVE. Device with highest state is used to
84 * create pseudo device. So if state is higher, the device path needs to
86 * If device-mapper multipath support is a must, pseudo devices should
87 * exist for each multipath device. If not, active device path will be
88 * chosen for device creation.
89 * Treat partition as invalid path.
91 int bl_update_path(struct bl_disk_path *path, enum bl_path_state_e state,
94 struct bl_disk_path *valid_path = disk->valid_path;
97 if (valid_path->state >= state) {
98 if (bl_is_partition(valid_path, path))
105 void bl_release_disk(void)
107 struct bl_disk *disk;
108 struct bl_disk_path *path = NULL;
110 while (visible_disk_list) {
111 disk = visible_disk_list;
114 disk->paths = path->next;
115 free(path->full_path);
121 visible_disk_list = disk->next;
126 void bl_add_disk(char *filepath)
128 struct bl_disk *disk = NULL;
132 struct bl_serial *serial = NULL;
133 enum bl_path_state_e ap_state;
134 struct bl_disk_path *diskpath = NULL, *path = NULL;
137 fd = open(filepath, O_RDONLY | O_LARGEFILE);
141 if (fstat(fd, &sb)) {
147 ioctl(fd, BLKGETSIZE, &size);
157 serial = bldev_read_serial(fd, filepath);
158 if (dm_is_dm_major(major(dev)))
159 ap_state = BL_PATH_STATE_PSEUDO;
161 ap_state = bldev_read_ap_state(fd);
164 if (ap_state != BL_PATH_STATE_ACTIVE)
167 for (disk = visible_disk_list; disk != NULL; disk = disk->next) {
168 /* Already scanned or a partition?
169 * XXX: if released each time, maybe not need to compare
171 if ((serial->len == disk->serial->len) &&
172 !memcmp(serial->data, disk->serial->data, serial->len)) {
173 diskpath = bl_get_path(filepath, disk->paths);
178 if (disk && diskpath)
182 path = malloc(sizeof(struct bl_disk_path));
184 BL_LOG_ERR("%s: Out of memory!\n", __func__);
188 path->state = ap_state;
189 path->full_path = strdup(filepath);
190 if (!path->full_path)
193 if (!disk) { /* add disk */
194 disk = malloc(sizeof(struct bl_disk));
196 BL_LOG_ERR("%s: Out of memory!\n", __func__);
199 disk->next = visible_disk_list;
202 disk->serial = serial;
203 disk->valid_path = path;
205 visible_disk_list = disk;
207 path->next = disk->paths;
209 /* check whether we need to update disk info */
210 if (bl_update_path(path, path->state, disk)) {
213 disk->valid_path = path;
221 free(path->full_path);
227 int bl_discover_devices(void)
231 char buf[PATH_MAX], devname[PATH_MAX], fulldevname[PATH_MAX];
233 /* release previous list */
236 /* scan all block devices */
237 f = fopen("/proc/partitions", "r");
242 if (fgets(buf, sizeof buf, f) == NULL)
244 n = sscanf(buf, "%*d %*d %*d %31s", devname);
247 snprintf(fulldevname, sizeof fulldevname, "/sys/block/%s",
249 if (access(fulldevname, F_OK) < 0)
251 snprintf(fulldevname, sizeof fulldevname, "/dev/%s", devname);
252 bl_add_disk(fulldevname);
260 /* process kernel request
261 * return 0: request processed, and no more request waiting;
262 * return 1: request processed, and more requests waiting;
265 int bl_disk_inquiry_process(int fd)
268 struct bl_pipemsg_hdr head;
270 uint32_t major, minor;
272 struct bl_dev_msg reply;
275 if (atomicio(read, fd, &head, sizeof(head)) != sizeof(head)) {
276 /* Note that an error in this or the next read is pretty
277 * catastrophic, as there is no good way to resync into
280 BL_LOG_ERR("Read pipefs head error!\n");
285 buflen = head.totallen;
286 buf = malloc(buflen);
288 BL_LOG_ERR("%s: Out of memory!\n", __func__);
293 if (atomicio(read, fd, buf, buflen) != buflen) {
294 BL_LOG_ERR("Read pipefs content error!\n");
299 reply.status = BL_DEVICE_REQUEST_PROC;
302 case BL_DEVICE_MOUNT:
304 * It shouldn't be necessary to discover devices here, since
305 * process_deviceinfo() will re-discover if it can't find
306 * the devices it needs. But in the case of multipath
307 * devices (ones that appear more than once, for example an
308 * active and a standby LUN), this will re-order them in the
311 bl_discover_devices();
312 if (!process_deviceinfo(buf, buflen, &major, &minor)) {
313 reply.status = BL_DEVICE_REQUEST_ERR;
319 case BL_DEVICE_UMOUNT:
320 if (!dm_device_remove_all((uint64_t *) buf))
321 reply.status = BL_DEVICE_REQUEST_ERR;
324 reply.status = BL_DEVICE_REQUEST_ERR;
328 /* write to pipefs */
329 if (atomicio((void *)write, fd, &reply, sizeof(reply))
331 BL_LOG_ERR("Write pipefs error!\n");
341 /* TODO: set bl_process_stop to 1 in command */
342 unsigned int bl_process_stop;
344 int bl_run_disk_inquiry_process(int fd)
357 switch (select(fd + 1, &rset, NULL, NULL, NULL)) {
368 if (FD_ISSET(fd, &rset))
369 ret = bl_disk_inquiry_process(fd);
377 int main(int argc, char **argv)
379 int fd, pidfd = -1, opt, dflag = 0, fg = 0, ret = 1;
383 while ((opt = getopt(argc, argv, "df")) != -1) {
395 openlog("blkmapd", LOG_PERROR, 0);
397 if (!stat(PID_FILE, &statbuf)) {
398 fprintf(stderr, "Pid file %s already existed\n", PID_FILE);
402 if (daemon(0, 0) != 0) {
403 fprintf(stderr, "Daemonize failed\n");
407 openlog("blkmapd", LOG_PID, 0);
408 pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
410 BL_LOG_ERR("Create pid file %s failed\n", PID_FILE);
414 if (lockf(pidfd, F_TLOCK, 0) < 0) {
415 BL_LOG_ERR("Lock pid file %s failed\n", PID_FILE);
420 sprintf(pidbuf, "%d\n", getpid());
421 write(pidfd, pidbuf, strlen(pidbuf));
425 bl_discover_devices();
430 fd = open(BL_PIPE_FILE, O_RDWR);
432 BL_LOG_ERR("open pipe file %s error\n", BL_PIPE_FILE);
437 /* discover device when needed */
438 bl_discover_devices();
440 ret = bl_run_disk_inquiry_process(fd);
442 /* what should we do with process error? */
443 BL_LOG_ERR("inquiry process return %d\n", ret);