fstools: add btrfs support
[project/fstools.git] / blockd.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <sys/mount.h>
4 #include <sys/wait.h>
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10
11 #include <errno.h>
12
13 #include <linux/limits.h>
14 #include <linux/auto_fs4.h>
15
16 #include <libubox/uloop.h>
17 #include <libubox/vlist.h>
18 #include <libubox/ulog.h>
19 #include <libubox/avl-cmp.h>
20 #include <libubus.h>
21
22 #include "libfstools/libfstools.h"
23
24 #define AUTOFS_MOUNT_PATH       "/tmp/run/blockd/"
25 #define AUTOFS_TIMEOUT          30
26 #define AUTOFS_EXPIRE_TIMER     (5 * 1000)
27
28 struct device {
29         struct vlist_node node;
30         struct blob_attr *msg;
31         char *name;
32         char *target;
33         int autofs;
34         int anon;
35 };
36
37 static struct uloop_fd fd_autofs_read;
38 static int fd_autofs_write = 0;
39 static struct ubus_auto_conn conn;
40 struct blob_buf bb = { 0 };
41
42 enum {
43         MOUNT_UUID,
44         MOUNT_LABEL,
45         MOUNT_ENABLE,
46         MOUNT_TARGET,
47         MOUNT_DEVICE,
48         MOUNT_OPTIONS,
49         MOUNT_AUTOFS,
50         MOUNT_ANON,
51         MOUNT_REMOVE,
52         __MOUNT_MAX
53 };
54
55 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
56         [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
57         [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
58         [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
59         [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
60         [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
61         [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
62         [MOUNT_AUTOFS] = { .name = "autofs", .type = BLOBMSG_TYPE_INT32 },
63         [MOUNT_ANON] = { .name = "anon", .type = BLOBMSG_TYPE_INT32 },
64         [MOUNT_REMOVE] = { .name = "remove", .type = BLOBMSG_TYPE_INT32 },
65 };
66
67 static char*
68 _find_mount_point(char *device)
69 {
70         char dev[32] = { 0 };
71
72         snprintf(dev, sizeof(dev), "/dev/%s", device);
73
74         return find_mount_point(dev, 0);
75 }
76
77 static int
78 block(char *cmd, char *action, char *device)
79 {
80         pid_t pid = fork();
81         int ret = -1;
82         int status;
83         char *argv[5] = { 0 };
84         int a = 0;
85
86         switch (pid) {
87         case -1:
88                 ULOG_ERR("failed to fork block process\n");
89                 break;
90
91         case 0:
92                 argv[a++] = "/sbin/block";
93                 argv[a++] = cmd;
94                 argv[a++] = action;
95                 argv[a++] = device;
96                 execvp(argv[0], argv);
97                 ULOG_ERR("failed to spawn %s %s %s\n", *argv, action, device);
98                 exit(-1);
99
100         default:
101                 waitpid(pid, &status, 0);
102                 ret = WEXITSTATUS(status);
103                 if (ret)
104                         ULOG_ERR("failed to run block. %s/%s\n", action, device);
105                 break;
106         }
107
108         return ret;
109 }
110
111 static void
112 device_free(struct device *device)
113 {
114         struct blob_attr *data[__MOUNT_MAX];
115         char *target = NULL;
116         char *path = NULL, _path[64], *mp;
117
118         blobmsg_parse(mount_policy, __MOUNT_MAX, data,
119                       blob_data(device->msg), blob_len(device->msg));
120
121         if (data[MOUNT_AUTOFS]) {
122                 target = device->target;
123                 snprintf(_path, sizeof(_path), "/tmp/run/blockd/%s",
124                          blobmsg_get_string(data[MOUNT_DEVICE]));
125                 path = _path;
126         } else {
127                 path = target = device->target;
128         }
129
130         mp = _find_mount_point(device->name);
131         if (path && mp)
132                 if (umount2(path, MNT_DETACH))
133                         ULOG_ERR("failed to unmount %s\n", path);
134         free(mp);
135
136         if (!target)
137                 return;
138
139         if (data[MOUNT_AUTOFS])
140                 unlink(target);
141         else
142                 rmdir(target);
143 }
144
145 static void
146 device_add(struct device *device)
147 {
148         struct blob_attr *data[__MOUNT_MAX];
149         char path[64];
150
151         blobmsg_parse(mount_policy, __MOUNT_MAX, data,
152                       blob_data(device->msg), blob_len(device->msg));
153
154         if (!data[MOUNT_AUTOFS])
155                 return;
156
157         snprintf(path, sizeof(path), "/tmp/run/blockd/%s",
158                  blobmsg_get_string(data[MOUNT_DEVICE]));
159         if (symlink(path, device->target))
160                 ULOG_ERR("failed to symlink %s->%s\n", device->target, path);
161 }
162
163 static int
164 device_move(struct device *device_o, struct device *device_n)
165 {
166         char path[64];
167
168         if (device_o->autofs != device_n->autofs)
169                 return -1;
170
171         if (device_o->anon || device_n->anon)
172                 return -1;
173
174         if (device_o->autofs) {
175                 unlink(device_o->target);
176                 snprintf(path, sizeof(path), "/tmp/run/blockd/%s", device_n->name);
177                 if (symlink(path, device_n->target))
178                         ULOG_ERR("failed to symlink %s->%s\n", device_n->target, path);
179         } else {
180                 mkdir(device_n->target, 0755);
181                 if (mount(device_o->target, device_n->target, NULL, MS_MOVE, NULL))
182                         rmdir(device_n->target);
183                 else
184                         rmdir(device_o->target);
185         }
186
187         return 0;
188 }
189
190 static void
191 devices_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
192                   struct vlist_node *node_old)
193 {
194         struct device *device_o = NULL, *device_n = NULL;
195
196         if (node_old)
197                 device_o = container_of(node_old, struct device, node);
198
199         if (node_new)
200                 device_n = container_of(node_new, struct device, node);
201
202         if (device_o && device_n) {
203                 if (device_move(device_o, device_n)) {
204                         device_free(device_o);
205                         device_add(device_n);
206                         if (!device_n->autofs)
207                                 block("mount", NULL, NULL);
208                 }
209         } else if (device_n) {
210                 device_add(device_n);
211         } else {
212                 device_free(device_o);
213         }
214
215         if (device_o)
216                 free(device_o);
217 }
218
219 VLIST_TREE(devices, avl_strcmp, devices_update_cb, false, false);
220
221 static int
222 block_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
223               struct ubus_request_data *req, const char *method,
224               struct blob_attr *msg)
225 {
226         struct blob_attr *data[__MOUNT_MAX];
227         struct device *device;
228         struct blob_attr *_msg;
229         char *devname, *_name;
230         char *target = NULL, *__target;
231         char _target[32];
232
233         blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
234
235         if (!data[MOUNT_DEVICE])
236                 return UBUS_STATUS_INVALID_ARGUMENT;
237
238         devname = blobmsg_get_string(data[MOUNT_DEVICE]);
239
240         if (data[MOUNT_TARGET]) {
241                 target = blobmsg_get_string(data[MOUNT_TARGET]);
242         } else {
243                 snprintf(_target, sizeof(_target), "/mnt/%s",
244                          blobmsg_get_string(data[MOUNT_DEVICE]));
245                 target = _target;
246         }
247
248         if (data[MOUNT_REMOVE])
249                 device = vlist_find(&devices, devname, device, node);
250         else
251                 device = calloc_a(sizeof(*device), &_msg, blob_raw_len(msg),
252                                   &_name, strlen(devname) + 1, &__target, strlen(target) + 1);
253
254         if (!device)
255                 return UBUS_STATUS_UNKNOWN_ERROR;
256
257         vlist_update(&devices);
258         if (data[MOUNT_REMOVE]) {
259                 vlist_delete(&devices, &device->node);
260         } else {
261                 if (data[MOUNT_AUTOFS])
262                         device->autofs = blobmsg_get_u32(data[MOUNT_AUTOFS]);
263                 else
264                         device->autofs = 0;
265                 if (data[MOUNT_ANON])
266                         device->anon = blobmsg_get_u32(data[MOUNT_ANON]);
267                 else
268                         device->anon = 0;
269                 device->msg = _msg;
270                 memcpy(_msg, msg, blob_raw_len(msg));
271                 device->name = _name;
272                 strcpy(_name, devname);
273                 device->target = __target;
274                 strcpy(__target, target);
275                 vlist_add(&devices, &device->node, blobmsg_get_string(data[MOUNT_DEVICE]));
276         }
277         vlist_flush(&devices);
278
279         return 0;
280 }
281
282 static int
283 block_info(struct ubus_context *ctx, struct ubus_object *obj,
284            struct ubus_request_data *req, const char *method,
285            struct blob_attr *msg)
286 {
287         struct device *device;
288         void *a;
289
290         blob_buf_init(&bb, 0);
291         a = blobmsg_open_array(&bb, "devices");
292         vlist_for_each_element(&devices, device, node) {
293                 void *t = blobmsg_open_table(&bb, "");
294                 struct blob_attr *v;
295                 char *mp;
296                 int rem;
297
298                 blob_for_each_attr(v, device->msg, rem)
299                         blobmsg_add_blob(&bb, v);
300
301                 mp = _find_mount_point(device->name);
302                 if (mp) {
303                         blobmsg_add_string(&bb, "mount", mp);
304                         free(mp);
305                 }
306                 blobmsg_close_table(&bb, t);
307         }
308         blobmsg_close_array(&bb, a);
309         ubus_send_reply(ctx, req, bb.head);
310
311         return 0;
312 }
313
314 static const struct ubus_method block_methods[] = {
315         UBUS_METHOD("hotplug", block_hotplug, mount_policy),
316         UBUS_METHOD_NOARG("info", block_info),
317 };
318
319 static struct ubus_object_type block_object_type =
320         UBUS_OBJECT_TYPE("block", block_methods);
321
322 static struct ubus_object block_object = {
323         .name = "block",
324         .type = &block_object_type,
325         .methods = block_methods,
326         .n_methods = ARRAY_SIZE(block_methods),
327 };
328
329 static void
330 ubus_connect_handler(struct ubus_context *ctx)
331 {
332         int ret;
333
334         ret = ubus_add_object(ctx, &block_object);
335         if (ret)
336                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
337 }
338
339 static int autofs_umount(void)
340 {
341         umount2(AUTOFS_MOUNT_PATH, MNT_DETACH);
342         return 0;
343 }
344
345 static void autofs_read_handler(struct uloop_fd *u, unsigned int events)
346 {
347         union autofs_v5_packet_union pktu;
348         const struct autofs_v5_packet *pkt;
349         int cmd = AUTOFS_IOC_READY;
350         struct stat st;
351
352         while (read(u->fd, &pktu, sizeof(pktu)) == -1) {
353                 if (errno != EINTR)
354                         return;
355                 continue;
356         }
357
358         if (pktu.hdr.type != autofs_ptype_missing_indirect) {
359                 ULOG_ERR("unknown packet type %d\n", pktu.hdr.type);
360                 return;
361         }
362
363         pkt = &pktu.missing_indirect;
364         ULOG_ERR("kernel is requesting a mount -> %s\n", pkt->name);
365         if (lstat(pkt->name, &st) == -1)
366                 if (block("autofs", "add", (char *)pkt->name))
367                         cmd = AUTOFS_IOC_FAIL;
368
369         if (ioctl(fd_autofs_write, cmd, pkt->wait_queue_token) < 0)
370                 ULOG_ERR("failed to report back to kernel\n");
371 }
372
373 static void autofs_expire(struct uloop_timeout *t)
374 {
375         struct autofs_packet_expire pkt;
376
377         while (ioctl(fd_autofs_write, AUTOFS_IOC_EXPIRE, &pkt) == 0)
378                 block("autofs", "remove", pkt.name);
379
380         uloop_timeout_set(t, AUTOFS_EXPIRE_TIMER);
381 }
382
383 struct uloop_timeout autofs_expire_timer = {
384         .cb = autofs_expire,
385 };
386
387 static int autofs_mount(void)
388 {
389         int autofs_timeout = AUTOFS_TIMEOUT;
390         int kproto_version;
391         int pipefd[2];
392         char source[64];
393         char opts[64];
394
395         if (pipe(pipefd) < 0) {
396                 ULOG_ERR("failed to get kernel pipe\n");
397                 return -1;
398         }
399
400         snprintf(source, sizeof(source), "mountd(pid%u)", getpid());
401         snprintf(opts, sizeof(opts), "fd=%d,pgrp=%u,minproto=5,maxproto=5", pipefd[1], (unsigned) getpgrp());
402         mkdir(AUTOFS_MOUNT_PATH, 0555);
403         if (mount(source, AUTOFS_MOUNT_PATH, "autofs", 0, opts)) {
404                 ULOG_ERR("unable to mount autofs on %s\n", AUTOFS_MOUNT_PATH);
405                 close(pipefd[0]);
406                 close(pipefd[1]);
407                 return -1;
408         }
409         close(pipefd[1]);
410         fd_autofs_read.fd = pipefd[0];
411         fd_autofs_read.cb = autofs_read_handler;
412         uloop_fd_add(&fd_autofs_read, ULOOP_READ);
413
414         fd_autofs_write = open(AUTOFS_MOUNT_PATH, O_RDONLY);
415         if(fd_autofs_write < 0) {
416                 autofs_umount();
417                 ULOG_ERR("failed to open direcory\n");
418                 return -1;
419         }
420
421         ioctl(fd_autofs_write, AUTOFS_IOC_PROTOVER, &kproto_version);
422         if (kproto_version != 5) {
423                 ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
424                         kproto_version);
425                 exit(1);
426         }
427         if (ioctl(fd_autofs_write, AUTOFS_IOC_SETTIMEOUT, &autofs_timeout))
428                 ULOG_ERR("failed to set autofs timeout\n");
429
430         uloop_timeout_set(&autofs_expire_timer, AUTOFS_EXPIRE_TIMER);
431
432         fcntl(fd_autofs_write, F_SETFD, fcntl(fd_autofs_write, F_GETFD) | FD_CLOEXEC);
433         fcntl(fd_autofs_read.fd, F_SETFD, fcntl(fd_autofs_read.fd, F_GETFD) | FD_CLOEXEC);
434
435         return 0;
436 }
437
438 static void blockd_startup(struct uloop_timeout *t)
439 {
440         block("autofs", "start", NULL);
441 }
442
443 struct uloop_timeout startup = {
444         .cb = blockd_startup,
445 };
446
447 int main(int argc, char **argv)
448 {
449         ulog_open(ULOG_SYSLOG | ULOG_STDIO, LOG_DAEMON, "blockd");
450         uloop_init();
451
452         autofs_mount();
453
454         conn.cb = ubus_connect_handler;
455         ubus_auto_connect(&conn);
456
457         uloop_timeout_set(&startup, 1000);
458
459         uloop_run();
460         uloop_done();
461
462         autofs_umount();
463
464         vlist_flush_all(&devices);
465
466         return 0;
467 }