13 #include <linux/limits.h>
14 #include <linux/auto_fs4.h>
16 #include <libubox/uloop.h>
17 #include <libubox/vlist.h>
18 #include <libubox/ulog.h>
19 #include <libubox/avl-cmp.h>
22 #include "libfstools/libfstools.h"
24 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
25 #define AUTOFS_TIMEOUT 30
26 #define AUTOFS_EXPIRE_TIMER (5 * 1000)
28 struct hotplug_context {
29 struct uloop_process process;
34 struct vlist_node node;
35 struct blob_attr *msg;
42 static struct uloop_fd fd_autofs_read;
43 static int fd_autofs_write = 0;
44 static struct ubus_auto_conn conn;
45 struct blob_buf bb = { 0 };
60 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
61 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
62 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
63 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
64 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
65 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
66 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
67 [MOUNT_AUTOFS] = { .name = "autofs", .type = BLOBMSG_TYPE_INT32 },
68 [MOUNT_ANON] = { .name = "anon", .type = BLOBMSG_TYPE_INT32 },
69 [MOUNT_REMOVE] = { .name = "remove", .type = BLOBMSG_TYPE_INT32 },
77 static const struct blobmsg_policy info_policy[__INFO_MAX] = {
78 [INFO_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
82 _find_mount_point(char *device)
86 snprintf(dev, sizeof(dev), "/dev/%s", device);
88 return find_mount_point(dev, 0);
92 block(char *cmd, char *action, char *device)
97 char *argv[5] = { 0 };
102 ULOG_ERR("failed to fork block process\n");
106 argv[a++] = "/sbin/block";
110 execvp(argv[0], argv);
111 ULOG_ERR("failed to spawn %s %s %s\n", *argv, action, device);
115 waitpid(pid, &status, 0);
116 ret = WEXITSTATUS(status);
118 ULOG_ERR("failed to run block. %s/%s\n", action, device);
125 static int hotplug_call_mount(const char *action, const char *devname,
126 uloop_process_handler cb, void *priv)
128 char * const argv[] = { "hotplug-call", "mount", NULL };
129 struct hotplug_context *c = NULL;
134 c = calloc(1, sizeof(*c));
143 ULOG_ERR("fork() failed\n");
148 setenv("ACTION", action, 1);
149 setenv("DEVICE", devname, 1);
151 execv("/sbin/hotplug-call", argv);
156 c->process.pid = pid;
159 uloop_process_add(&c->process);
167 static void device_mount_remove_hotplug_cb(struct uloop_process *p, int stat)
169 struct hotplug_context *hctx = container_of(p, struct hotplug_context, process);
170 struct device *device = hctx->priv;
174 unlink(device->target);
176 mp = _find_mount_point(device->name);
178 block("autofs", "remove", device->name);
186 static void device_mount_remove(struct device *device)
188 hotplug_call_mount("remove", device->name,
189 device_mount_remove_hotplug_cb, device);
192 static void device_mount_add(struct device *device)
197 snprintf(path, sizeof(path), "/tmp/run/blockd/%s", device->name);
198 if (!lstat(device->target, &st)) {
199 if (S_ISLNK(st.st_mode))
200 unlink(device->target);
201 else if (S_ISDIR(st.st_mode))
202 rmdir(device->target);
204 if (symlink(path, device->target))
205 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device->target, path, errno);
207 hotplug_call_mount("add", device->name, NULL, NULL);
211 device_move(struct device *device_o, struct device *device_n)
215 if (device_o->autofs != device_n->autofs)
218 if (device_o->anon || device_n->anon)
221 if (device_o->autofs) {
222 unlink(device_o->target);
223 snprintf(path, sizeof(path), "/tmp/run/blockd/%s", device_n->name);
224 if (symlink(path, device_n->target))
225 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device_n->target, path, errno);
227 mkdir(device_n->target, 0755);
228 if (mount(device_o->target, device_n->target, NULL, MS_MOVE, NULL))
229 rmdir(device_n->target);
231 rmdir(device_o->target);
237 static void vlist_nop_update(struct vlist_tree *tree,
238 struct vlist_node *node_new,
239 struct vlist_node *node_old)
243 VLIST_TREE(devices, avl_strcmp, vlist_nop_update, false, false);
246 block_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
247 struct ubus_request_data *req, const char *method,
248 struct blob_attr *msg)
250 struct blob_attr *data[__MOUNT_MAX];
251 struct device *device;
252 struct blob_attr *_msg;
253 char *devname, *_name;
254 char *target = NULL, *__target;
257 blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
259 if (!data[MOUNT_DEVICE])
260 return UBUS_STATUS_INVALID_ARGUMENT;
262 devname = blobmsg_get_string(data[MOUNT_DEVICE]);
264 if (data[MOUNT_TARGET]) {
265 target = blobmsg_get_string(data[MOUNT_TARGET]);
267 snprintf(_target, sizeof(_target), "/mnt/%s",
268 blobmsg_get_string(data[MOUNT_DEVICE]));
272 if (data[MOUNT_REMOVE])
273 device = vlist_find(&devices, devname, device, node);
275 device = calloc_a(sizeof(*device), &_msg, blob_raw_len(msg),
276 &_name, strlen(devname) + 1, &__target, strlen(target) + 1);
279 return UBUS_STATUS_UNKNOWN_ERROR;
281 if (data[MOUNT_REMOVE]) {
282 vlist_delete(&devices, &device->node);
285 device_mount_remove(device);
289 struct device *old = vlist_find(&devices, devname, device, node);
291 device->autofs = data[MOUNT_AUTOFS] ? blobmsg_get_u32(data[MOUNT_AUTOFS]) : 0;
292 device->anon = data[MOUNT_ANON] ? blobmsg_get_u32(data[MOUNT_ANON]) : 0;
294 memcpy(_msg, msg, blob_raw_len(msg));
295 device->name = _name;
296 strcpy(_name, devname);
297 device->target = __target;
298 strcpy(__target, target);
300 vlist_add(&devices, &device->node, device->name);
302 if (old && !device_move(old, device)) {
303 if (device->autofs) {
304 device_mount_remove(old);
305 device_mount_add(device);
307 block("mount", NULL, NULL);
309 } else if (device->autofs) {
310 device_mount_add(device);
317 static int blockd_mount(struct ubus_context *ctx, struct ubus_object *obj,
318 struct ubus_request_data *req, const char *method,
319 struct blob_attr *msg)
321 struct blob_attr *data[__MOUNT_MAX];
322 struct device *device;
325 blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
327 if (!data[MOUNT_DEVICE])
328 return UBUS_STATUS_INVALID_ARGUMENT;
330 devname = blobmsg_get_string(data[MOUNT_DEVICE]);
332 device = vlist_find(&devices, devname, device, node);
334 return UBUS_STATUS_UNKNOWN_ERROR;
336 hotplug_call_mount("add", device->name, NULL, NULL);
341 struct blockd_umount_context {
342 struct ubus_context *ctx;
343 struct ubus_request_data req;
346 static void blockd_umount_hotplug_cb(struct uloop_process *p, int stat)
348 struct hotplug_context *hctx = container_of(p, struct hotplug_context, process);
349 struct blockd_umount_context *c = hctx->priv;
351 ubus_complete_deferred_request(c->ctx, &c->req, 0);
357 static int blockd_umount(struct ubus_context *ctx, struct ubus_object *obj,
358 struct ubus_request_data *req, const char *method,
359 struct blob_attr *msg)
361 struct blob_attr *data[__MOUNT_MAX];
362 struct blockd_umount_context *c;
366 blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
368 if (!data[MOUNT_DEVICE])
369 return UBUS_STATUS_INVALID_ARGUMENT;
371 devname = blobmsg_get_string(data[MOUNT_DEVICE]);
373 c = calloc(1, sizeof(*c));
375 return UBUS_STATUS_UNKNOWN_ERROR;
378 ubus_defer_request(ctx, req, &c->req);
380 err = hotplug_call_mount("remove", devname, blockd_umount_hotplug_cb, c);
383 return UBUS_STATUS_UNKNOWN_ERROR;
389 static void block_info_dump(struct blob_buf *b, struct device *device)
395 blob_for_each_attr(v, device->msg, rem)
396 blobmsg_add_blob(b, v);
398 mp = _find_mount_point(device->name);
400 blobmsg_add_string(b, "mount", mp);
402 } else if (device->autofs && device->target) {
403 blobmsg_add_string(b, "mount", device->target);
408 block_info(struct ubus_context *ctx, struct ubus_object *obj,
409 struct ubus_request_data *req, const char *method,
410 struct blob_attr *msg)
412 struct blob_attr *data[__INFO_MAX];
413 struct device *device = NULL;
415 blobmsg_parse(info_policy, __INFO_MAX, data, blob_data(msg), blob_len(msg));
417 if (data[INFO_DEVICE]) {
418 device = vlist_find(&devices, blobmsg_get_string(data[INFO_DEVICE]), device, node);
420 return UBUS_STATUS_INVALID_ARGUMENT;
423 blob_buf_init(&bb, 0);
425 block_info_dump(&bb, device);
429 a = blobmsg_open_array(&bb, "devices");
430 vlist_for_each_element(&devices, device, node) {
433 t = blobmsg_open_table(&bb, "");
434 block_info_dump(&bb, device);
435 blobmsg_close_table(&bb, t);
437 blobmsg_close_array(&bb, a);
439 ubus_send_reply(ctx, req, bb.head);
444 static const struct ubus_method block_methods[] = {
445 UBUS_METHOD("hotplug", block_hotplug, mount_policy),
446 UBUS_METHOD("mount", blockd_mount, mount_policy),
447 UBUS_METHOD("umount", blockd_umount, mount_policy),
448 UBUS_METHOD("info", block_info, info_policy),
451 static struct ubus_object_type block_object_type =
452 UBUS_OBJECT_TYPE("block", block_methods);
454 static struct ubus_object block_object = {
456 .type = &block_object_type,
457 .methods = block_methods,
458 .n_methods = ARRAY_SIZE(block_methods),
462 ubus_connect_handler(struct ubus_context *ctx)
466 ret = ubus_add_object(ctx, &block_object);
468 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
471 static int autofs_umount(void)
473 umount2(AUTOFS_MOUNT_PATH, MNT_DETACH);
477 static void autofs_read_handler(struct uloop_fd *u, unsigned int events)
479 union autofs_v5_packet_union pktu;
480 const struct autofs_v5_packet *pkt;
481 int cmd = AUTOFS_IOC_READY;
484 while (read(u->fd, &pktu, sizeof(pktu)) == -1) {
490 if (pktu.hdr.type != autofs_ptype_missing_indirect) {
491 ULOG_ERR("unknown packet type %d\n", pktu.hdr.type);
495 pkt = &pktu.missing_indirect;
496 ULOG_ERR("kernel is requesting a mount -> %s\n", pkt->name);
497 if (lstat(pkt->name, &st) == -1)
498 if (block("autofs", "add", (char *)pkt->name))
499 cmd = AUTOFS_IOC_FAIL;
501 if (ioctl(fd_autofs_write, cmd, pkt->wait_queue_token) < 0)
502 ULOG_ERR("failed to report back to kernel\n");
505 static void autofs_expire(struct uloop_timeout *t)
507 struct autofs_packet_expire pkt;
509 while (ioctl(fd_autofs_write, AUTOFS_IOC_EXPIRE, &pkt) == 0)
510 block("autofs", "remove", pkt.name);
512 uloop_timeout_set(t, AUTOFS_EXPIRE_TIMER);
515 struct uloop_timeout autofs_expire_timer = {
519 static int autofs_mount(void)
521 int autofs_timeout = AUTOFS_TIMEOUT;
527 if (pipe(pipefd) < 0) {
528 ULOG_ERR("failed to get kernel pipe\n");
532 snprintf(source, sizeof(source), "mountd(pid%u)", getpid());
533 snprintf(opts, sizeof(opts), "fd=%d,pgrp=%u,minproto=5,maxproto=5", pipefd[1], (unsigned) getpgrp());
534 mkdir(AUTOFS_MOUNT_PATH, 0555);
535 if (mount(source, AUTOFS_MOUNT_PATH, "autofs", 0, opts)) {
536 ULOG_ERR("unable to mount autofs on %s\n", AUTOFS_MOUNT_PATH);
542 fd_autofs_read.fd = pipefd[0];
543 fd_autofs_read.cb = autofs_read_handler;
544 uloop_fd_add(&fd_autofs_read, ULOOP_READ);
546 fd_autofs_write = open(AUTOFS_MOUNT_PATH, O_RDONLY);
547 if(fd_autofs_write < 0) {
549 ULOG_ERR("failed to open direcory\n");
553 ioctl(fd_autofs_write, AUTOFS_IOC_PROTOVER, &kproto_version);
554 if (kproto_version != 5) {
555 ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
559 if (ioctl(fd_autofs_write, AUTOFS_IOC_SETTIMEOUT, &autofs_timeout))
560 ULOG_ERR("failed to set autofs timeout\n");
562 uloop_timeout_set(&autofs_expire_timer, AUTOFS_EXPIRE_TIMER);
564 fcntl(fd_autofs_write, F_SETFD, fcntl(fd_autofs_write, F_GETFD) | FD_CLOEXEC);
565 fcntl(fd_autofs_read.fd, F_SETFD, fcntl(fd_autofs_read.fd, F_GETFD) | FD_CLOEXEC);
570 static void blockd_startup(struct uloop_timeout *t)
572 block("autofs", "start", NULL);
575 struct uloop_timeout startup = {
576 .cb = blockd_startup,
579 int main(int argc, char **argv)
581 ulog_open(ULOG_SYSLOG | ULOG_STDIO, LOG_DAEMON, "blockd");
586 conn.cb = ubus_connect_handler;
587 ubus_auto_connect(&conn);
589 uloop_timeout_set(&startup, 1000);
596 vlist_flush_all(&devices);