]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/blkmapd/device-discovery.c
blkmapd: Add complex block layout discovery and mapping daemon
[nfs-utils.git] / utils / blkmapd / device-discovery.c
1 /*
2  * device-discovery.c: main function, discovering device and processing
3  * pipe request from kernel.
4  *
5  * Copyright (c) 2010 EMC Corporation, Haiying Tang <Tang_Haiying@emc.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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.
27  */
28
29 #include <sys/types.h>
30 #include <sys/stat.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>
37 #include <scsi/sg.h>
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <dirent.h>
44 #include <ctype.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <libgen.h>
48 #include <errno.h>
49 #include <libdevmapper.h>
50
51 #include "device-discovery.h"
52
53 #define BL_PIPE_FILE    "/var/lib/nfs/rpc_pipefs/nfs/blocklayout"
54 #define PID_FILE        "/var/run/blkmapd.pid"
55
56 struct bl_disk *visible_disk_list;
57
58 struct bl_disk_path *bl_get_path(const char *filepath,
59                                  struct bl_disk_path *paths)
60 {
61         struct bl_disk_path *tmp = paths;
62
63         while (tmp) {
64                 if (!strcmp(tmp->full_path, filepath))
65                         break;
66                 tmp = tmp->next;
67         }
68         return tmp;
69 }
70
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)
73 {
74         if (!strncmp(valid_path->full_path, path->full_path,
75                      strlen(valid_path->full_path)))
76                 return 1;
77
78         return 0;
79 }
80
81 /*
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
85  * be updated.
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.
90  */
91 int bl_update_path(struct bl_disk_path *path, enum bl_path_state_e state,
92                    struct bl_disk *disk)
93 {
94         struct bl_disk_path *valid_path = disk->valid_path;
95
96         if (valid_path) {
97                 if (valid_path->state >= state) {
98                         if (bl_is_partition(valid_path, path))
99                                 return 0;
100                 }
101         }
102         return 1;
103 }
104
105 void bl_release_disk(void)
106 {
107         struct bl_disk *disk;
108         struct bl_disk_path *path = NULL;
109
110         while (visible_disk_list) {
111                 disk = visible_disk_list;
112                 path = disk->paths;
113                 while (path) {
114                         disk->paths = path->next;
115                         free(path->full_path);
116                         free(path);
117                         path = disk->paths;
118                 }
119                 if (disk->serial)
120                         free(disk->serial);
121                 visible_disk_list = disk->next;
122                 free(disk);
123         }
124 }
125
126 void bl_add_disk(char *filepath)
127 {
128         struct bl_disk *disk = NULL;
129         int fd = 0;
130         struct stat sb;
131         off_t size = 0;
132         struct bl_serial *serial = NULL;
133         enum bl_path_state_e ap_state;
134         struct bl_disk_path *diskpath = NULL, *path = NULL;
135         dev_t dev;
136
137         fd = open(filepath, O_RDONLY | O_LARGEFILE);
138         if (fd < 0)
139                 return;
140
141         if (fstat(fd, &sb)) {
142                 close(fd);
143                 return;
144         }
145
146         if (!sb.st_size)
147                 ioctl(fd, BLKGETSIZE, &size);
148         else
149                 size = sb.st_size;
150
151         if (!size) {
152                 close(fd);
153                 return;
154         }
155
156         dev = sb.st_rdev;
157         serial = bldev_read_serial(fd, filepath);
158         if (dm_is_dm_major(major(dev)))
159                 ap_state = BL_PATH_STATE_PSEUDO;
160         else
161                 ap_state = bldev_read_ap_state(fd);
162         close(fd);
163
164         if (ap_state != BL_PATH_STATE_ACTIVE)
165                 return;
166
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
170                  */
171                 if ((serial->len == disk->serial->len) &&
172                     !memcmp(serial->data, disk->serial->data, serial->len)) {
173                         diskpath = bl_get_path(filepath, disk->paths);
174                         break;
175                 }
176         }
177
178         if (disk && diskpath)
179                 return;
180
181         /* add path */
182         path = malloc(sizeof(struct bl_disk_path));
183         if (!path) {
184                 BL_LOG_ERR("%s: Out of memory!\n", __func__);
185                 goto out_err;
186         }
187         path->next = NULL;
188         path->state = ap_state;
189         path->full_path = strdup(filepath);
190         if (!path->full_path)
191                 goto out_err;
192
193         if (!disk) {            /* add disk */
194                 disk = malloc(sizeof(struct bl_disk));
195                 if (!disk) {
196                         BL_LOG_ERR("%s: Out of memory!\n", __func__);
197                         goto out_err;
198                 }
199                 disk->next = visible_disk_list;
200                 disk->dev = dev;
201                 disk->size = size;
202                 disk->serial = serial;
203                 disk->valid_path = path;
204                 disk->paths = path;
205                 visible_disk_list = disk;
206         } else {
207                 path->next = disk->paths;
208                 disk->paths = path;
209                 /* check whether we need to update disk info */
210                 if (bl_update_path(path, path->state, disk)) {
211                         disk->dev = dev;
212                         disk->size = size;
213                         disk->valid_path = path;
214                 }
215         }
216         return;
217
218  out_err:
219         if (path) {
220                 if (path->full_path)
221                         free(path->full_path);
222                 free(path);
223         }
224         return;
225 }
226
227 int bl_discover_devices(void)
228 {
229         FILE *f;
230         int n;
231         char buf[PATH_MAX], devname[PATH_MAX], fulldevname[PATH_MAX];
232
233         /* release previous list */
234         bl_release_disk();
235
236         /* scan all block devices */
237         f = fopen("/proc/partitions", "r");
238         if (f == NULL)
239                 return 0;
240
241         while (1) {
242                 if (fgets(buf, sizeof buf, f) == NULL)
243                         break;
244                 n = sscanf(buf, "%*d %*d %*d %31s", devname);
245                 if (n != 1)
246                         continue;
247                 snprintf(fulldevname, sizeof fulldevname, "/sys/block/%s",
248                          devname);
249                 if (access(fulldevname, F_OK) < 0)
250                         continue;
251                 snprintf(fulldevname, sizeof fulldevname, "/dev/%s", devname);
252                 bl_add_disk(fulldevname);
253         }
254
255         fclose(f);
256
257         return 0;
258 }
259
260 /* process kernel request
261  * return 0: request processed, and no more request waiting;
262  * return 1: request processed, and more requests waiting;
263  * return < 0: error
264  */
265 int bl_disk_inquiry_process(int fd)
266 {
267         int ret = 0;
268         struct bl_pipemsg_hdr head;
269         char *buf = NULL;
270         uint32_t major, minor;
271         uint16_t buflen;
272         struct bl_dev_msg reply;
273
274         /* read request */
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
278                  * the pipe's stream.
279                  */
280                 BL_LOG_ERR("Read pipefs head error!\n");
281                 ret = -EIO;
282                 goto out;
283         }
284
285         buflen = head.totallen;
286         buf = malloc(buflen);
287         if (!buf) {
288                 BL_LOG_ERR("%s: Out of memory!\n", __func__);
289                 ret = -ENOMEM;
290                 goto out;
291         }
292
293         if (atomicio(read, fd, buf, buflen) != buflen) {
294                 BL_LOG_ERR("Read pipefs content error!\n");
295                 ret = -EIO;
296                 goto out;
297         }
298
299         reply.status = BL_DEVICE_REQUEST_PROC;
300
301         switch (head.type) {
302         case BL_DEVICE_MOUNT:
303                 /*
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
309                  * correct priority.
310                  */
311                 bl_discover_devices();
312                 if (!process_deviceinfo(buf, buflen, &major, &minor)) {
313                         reply.status = BL_DEVICE_REQUEST_ERR;
314                         break;
315                 }
316                 reply.major = major;
317                 reply.minor = minor;
318                 break;
319         case BL_DEVICE_UMOUNT:
320                 if (!dm_device_remove_all((uint64_t *) buf))
321                         reply.status = BL_DEVICE_REQUEST_ERR;
322                 break;
323         default:
324                 reply.status = BL_DEVICE_REQUEST_ERR;
325                 break;
326         }
327
328         /* write to pipefs */
329         if (atomicio((void *)write, fd, &reply, sizeof(reply))
330             != sizeof(reply)) {
331                 BL_LOG_ERR("Write pipefs error!\n");
332                 ret = -EIO;
333         }
334
335  out:
336         if (buf)
337                 free(buf);
338         return ret;
339 }
340
341 /* TODO: set bl_process_stop to 1 in command */
342 unsigned int bl_process_stop;
343
344 int bl_run_disk_inquiry_process(int fd)
345 {
346         fd_set rset;
347         int ret;
348
349         bl_process_stop = 0;
350
351         for (;;) {
352                 if (bl_process_stop)
353                         return 1;
354                 FD_ZERO(&rset);
355                 FD_SET(fd, &rset);
356                 ret = 0;
357                 switch (select(fd + 1, &rset, NULL, NULL, NULL)) {
358                 case -1:
359                         if (errno == EINTR)
360                                 continue;
361                         else {
362                                 ret = -errno;
363                                 goto out;
364                         }
365                 case 0:
366                         goto out;
367                 default:
368                         if (FD_ISSET(fd, &rset))
369                                 ret = bl_disk_inquiry_process(fd);
370                 }
371         }
372  out:
373         return ret;
374 }
375
376 /* Daemon */
377 int main(int argc, char **argv)
378 {
379         int fd, pidfd = -1, opt, dflag = 0, fg = 0, ret = 1;
380         struct stat statbuf;
381         char pidbuf[64];
382
383         while ((opt = getopt(argc, argv, "df")) != -1) {
384                 switch (opt) {
385                 case 'd':
386                         dflag = 1;
387                         break;
388                 case 'f':
389                         fg = 1;
390                         break;
391                 }
392         }
393
394         if (fg) {
395                 openlog("blkmapd", LOG_PERROR, 0);
396         } else {
397                 if (!stat(PID_FILE, &statbuf)) {
398                         fprintf(stderr, "Pid file %s already existed\n", PID_FILE);
399                         exit(1);
400                 }
401
402                 if (daemon(0, 0) != 0) {
403                         fprintf(stderr, "Daemonize failed\n");
404                         exit(1);
405                 }
406
407                 openlog("blkmapd", LOG_PID, 0);
408                 pidfd = open(PID_FILE, O_WRONLY | O_CREAT, 0644);
409                 if (pidfd < 0) {
410                         BL_LOG_ERR("Create pid file %s failed\n", PID_FILE);
411                         exit(1);
412                 }
413
414                 if (lockf(pidfd, F_TLOCK, 0) < 0) {
415                         BL_LOG_ERR("Lock pid file %s failed\n", PID_FILE);
416                         close(pidfd);
417                         exit(1);
418                 }
419                 ftruncate(pidfd, 0);
420                 sprintf(pidbuf, "%d\n", getpid());
421                 write(pidfd, pidbuf, strlen(pidbuf));
422         }
423
424         if (dflag) {
425                 bl_discover_devices();
426                 exit(0);
427         }
428
429         /* open pipe file */
430         fd = open(BL_PIPE_FILE, O_RDWR);
431         if (fd < 0) {
432                 BL_LOG_ERR("open pipe file %s error\n", BL_PIPE_FILE);
433                 exit(1);
434         }
435
436         while (1) {
437                 /* discover device when needed */
438                 bl_discover_devices();
439
440                 ret = bl_run_disk_inquiry_process(fd);
441                 if (ret < 0) {
442                         /* what should we do with process error? */
443                         BL_LOG_ERR("inquiry process return %d\n", ret);
444                 }
445         }
446
447         if (pidfd >= 0) {
448                 close(pidfd);
449                 unlink(PID_FILE);
450         }
451
452         exit(ret);
453 }