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 <sys/inotify.h>
35 #include <linux/kdev_t.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_ioctl.h>
50 #include <libdevmapper.h>
52 #include "device-discovery.h"
54 #define EVENT_SIZE (sizeof(struct inotify_event))
55 #define EVENT_BUFSIZE (1024 * EVENT_SIZE)
57 #define BL_PIPE_FILE "/var/lib/nfs/rpc_pipefs/nfs/blocklayout"
58 #define NFSPIPE_DIR "/var/lib/nfs/rpc_pipefs/nfs"
59 #define RPCPIPE_DIR "/var/lib/nfs/rpc_pipefs"
60 #define PID_FILE "/var/run/blkmapd.pid"
62 struct bl_disk *visible_disk_list;
63 int bl_watch_fd, bl_pipe_fd, nfs_pipedir_wfd, rpc_pipedir_wfd;
65 struct bl_disk_path *bl_get_path(const char *filepath,
66 struct bl_disk_path *paths)
68 struct bl_disk_path *tmp = paths;
71 if (!strcmp(tmp->full_path, filepath))
78 /* Check whether valid_path is a substring(partition) of path */
79 int bl_is_partition(struct bl_disk_path *valid_path, struct bl_disk_path *path)
81 if (!strncmp(valid_path->full_path, path->full_path,
82 strlen(valid_path->full_path)))
89 * For multipath devices, devices state could be PASSIVE/ACTIVE/PSEUDO,
90 * where PSEUDO > ACTIVE > PASSIVE. Device with highest state is used to
91 * create pseudo device. So if state is higher, the device path needs to
93 * If device-mapper multipath support is a must, pseudo devices should
94 * exist for each multipath device. If not, active device path will be
95 * chosen for device creation.
96 * Treat partition as invalid path.
98 int bl_update_path(struct bl_disk_path *path, enum bl_path_state_e state,
101 struct bl_disk_path *valid_path = disk->valid_path;
104 if (valid_path->state >= state) {
105 if (bl_is_partition(valid_path, path))
112 void bl_release_disk(void)
114 struct bl_disk *disk;
115 struct bl_disk_path *path = NULL;
117 while (visible_disk_list) {
118 disk = visible_disk_list;
121 disk->paths = path->next;
122 free(path->full_path);
128 visible_disk_list = disk->next;
133 void bl_add_disk(char *filepath)
135 struct bl_disk *disk = NULL;
139 struct bl_serial *serial = NULL;
140 enum bl_path_state_e ap_state;
141 struct bl_disk_path *diskpath = NULL, *path = NULL;
144 fd = open(filepath, O_RDONLY | O_LARGEFILE);
148 if (fstat(fd, &sb)) {
154 ioctl(fd, BLKGETSIZE, &size);
164 serial = bldev_read_serial(fd, filepath);
165 if (dm_is_dm_major(major(dev)))
166 ap_state = BL_PATH_STATE_PSEUDO;
168 ap_state = bldev_read_ap_state(fd);
171 if (ap_state != BL_PATH_STATE_ACTIVE)
174 for (disk = visible_disk_list; disk != NULL; disk = disk->next) {
175 /* Already scanned or a partition?
176 * XXX: if released each time, maybe not need to compare
178 if ((serial->len == disk->serial->len) &&
179 !memcmp(serial->data, disk->serial->data, serial->len)) {
180 diskpath = bl_get_path(filepath, disk->paths);
185 if (disk && diskpath)
189 path = malloc(sizeof(struct bl_disk_path));
191 BL_LOG_ERR("%s: Out of memory!\n", __func__);
195 path->state = ap_state;
196 path->full_path = strdup(filepath);
197 if (!path->full_path)
200 if (!disk) { /* add disk */
201 disk = malloc(sizeof(struct bl_disk));
203 BL_LOG_ERR("%s: Out of memory!\n", __func__);
206 disk->next = visible_disk_list;
209 disk->serial = serial;
210 disk->valid_path = path;
212 visible_disk_list = disk;
214 path->next = disk->paths;
216 /* check whether we need to update disk info */
217 if (bl_update_path(path, path->state, disk)) {
220 disk->valid_path = path;
228 free(path->full_path);
234 int bl_discover_devices(void)
238 char buf[PATH_MAX], devname[PATH_MAX], fulldevname[PATH_MAX];
240 /* release previous list */
243 /* scan all block devices */
244 f = fopen("/proc/partitions", "r");
249 if (fgets(buf, sizeof buf, f) == NULL)
251 n = sscanf(buf, "%*d %*d %*d %31s", devname);
254 snprintf(fulldevname, sizeof fulldevname, "/sys/block/%s",
256 if (access(fulldevname, F_OK) < 0)
258 snprintf(fulldevname, sizeof fulldevname, "/dev/%s", devname);
259 bl_add_disk(fulldevname);
267 /* process kernel request
268 * return 0: request processed, and no more request waiting;
269 * return 1: request processed, and more requests waiting;
272 static int bl_disk_inquiry_process(int fd)
275 struct bl_pipemsg_hdr head;
277 uint32_t major, minor;
279 struct bl_dev_msg reply;
282 if (atomicio(read, fd, &head, sizeof(head)) != sizeof(head)) {
283 /* Note that an error in this or the next read is pretty
284 * catastrophic, as there is no good way to resync into
287 BL_LOG_ERR("Read pipefs head error!\n");
292 buflen = head.totallen;
293 buf = malloc(buflen);
295 BL_LOG_ERR("%s: Out of memory!\n", __func__);
300 if (atomicio(read, fd, buf, buflen) != buflen) {
301 BL_LOG_ERR("Read pipefs content error!\n");
306 reply.status = BL_DEVICE_REQUEST_PROC;
309 case BL_DEVICE_MOUNT:
311 * It shouldn't be necessary to discover devices here, since
312 * process_deviceinfo() will re-discover if it can't find
313 * the devices it needs. But in the case of multipath
314 * devices (ones that appear more than once, for example an
315 * active and a standby LUN), this will re-order them in the
318 bl_discover_devices();
319 if (!process_deviceinfo(buf, buflen, &major, &minor)) {
320 reply.status = BL_DEVICE_REQUEST_ERR;
326 case BL_DEVICE_UMOUNT:
327 if (!dm_device_remove_all((uint64_t *) buf))
328 reply.status = BL_DEVICE_REQUEST_ERR;
331 reply.status = BL_DEVICE_REQUEST_ERR;
335 /* write to pipefs */
336 if (atomicio((void *)write, fd, &reply, sizeof(reply))
338 BL_LOG_ERR("Write pipefs error!\n");
348 static void bl_watch_dir(const char* dir, int *wd)
350 *wd = inotify_add_watch(bl_watch_fd, dir, IN_CREATE|IN_DELETE);
352 BL_LOG_ERR("failed to watch %s: %s\n", dir, strerror(errno));
355 static void bl_rpcpipe_cb(void)
357 int rc, curr_byte = 0;
358 char eventArr[EVENT_BUFSIZE];
359 struct inotify_event *event;
361 rc = read(bl_watch_fd, &eventArr, EVENT_BUFSIZE);
363 BL_LOG_ERR("read event fail: %s", strerror(errno));
365 while (rc > curr_byte) {
366 event = (struct inotify_event *)&eventArr[curr_byte];
367 curr_byte += EVENT_SIZE + event->len;
368 if (event->wd == rpc_pipedir_wfd) {
369 if (strncmp(event->name, "nfs", 3))
371 if (event->mask & IN_CREATE) {
372 BL_LOG_WARNING("nfs pipe dir created\n");
373 bl_watch_dir(NFSPIPE_DIR, &nfs_pipedir_wfd);
374 bl_pipe_fd = open(BL_PIPE_FILE, O_RDWR);
375 } else if (event->mask & IN_DELETE) {
376 BL_LOG_WARNING("nfs pipe dir deleted\n");
377 inotify_rm_watch(bl_watch_fd, nfs_pipedir_wfd);
379 nfs_pipedir_wfd = -1;
382 } else if (event->wd == nfs_pipedir_wfd) {
383 if (strncmp(event->name, "blocklayout", 11))
385 if (event->mask & IN_CREATE) {
386 BL_LOG_WARNING("blocklayout pipe file created\n");
387 bl_pipe_fd = open(BL_PIPE_FILE, O_RDWR);
389 BL_LOG_ERR("open %s failed: %s\n",
390 event->name, strerror(errno));
391 } else if (event->mask & IN_DELETE) {
392 BL_LOG_WARNING("blocklayout pipe file deleted\n");
400 static int bl_event_helper(void)
407 FD_SET(bl_watch_fd, &rset);
409 FD_SET(bl_pipe_fd, &rset);
410 maxfd = (bl_watch_fd>bl_pipe_fd)?bl_watch_fd:bl_pipe_fd;
411 switch (select(maxfd + 1, &rset, NULL, NULL, NULL)) {
422 if (FD_ISSET(bl_watch_fd, &rset))
424 else if (bl_pipe_fd > 0 && FD_ISSET(bl_pipe_fd, &rset))
425 ret = bl_disk_inquiry_process(bl_pipe_fd);
435 int main(int argc, char **argv)
437 int pidfd = -1, opt, dflag = 0, fg = 0, ret = 1;
441 while ((opt = getopt(argc, argv, "df")) != -1) {
453 openlog("blkmapd", LOG_PERROR, 0);
455 if (!stat(PID_FILE, &statbuf)) {
456 fprintf(stderr, "Pid file %s already existed\n", PID_FILE);
460 if (daemon(0, 0) != 0) {
461 fprintf(stderr, "Daemonize failed\n");
465 openlog("blkmapd", LOG_PID, 0);
466 pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
468 BL_LOG_ERR("Create pid file %s failed\n", PID_FILE);
472 if (lockf(pidfd, F_TLOCK, 0) < 0) {
473 BL_LOG_ERR("Lock pid file %s failed\n", PID_FILE);
478 sprintf(pidbuf, "%d\n", getpid());
479 write(pidfd, pidbuf, strlen(pidbuf));
483 bl_discover_devices();
487 if ((bl_watch_fd = inotify_init()) < 0) {
488 BL_LOG_ERR("init inotify failed %s\n", strerror(errno));
493 bl_watch_dir(RPCPIPE_DIR, &rpc_pipedir_wfd);
494 bl_watch_dir(NFSPIPE_DIR, &nfs_pipedir_wfd);
496 bl_pipe_fd = open(BL_PIPE_FILE, O_RDWR);
498 BL_LOG_ERR("open pipe file %s failed: %s\n", BL_PIPE_FILE, strerror(errno));
501 /* discover device when needed */
502 bl_discover_devices();
504 ret = bl_event_helper();
506 /* what should we do with process error? */
507 BL_LOG_ERR("inquiry process return %d\n", ret);