12 #include <linux/limits.h>
13 #include <linux/auto_fs4.h>
15 #include <libubox/uloop.h>
16 #include <libubox/vlist.h>
17 #include <libubox/ulog.h>
18 #include <libubox/avl-cmp.h>
21 #include "libfstools/libfstools.h"
23 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
24 #define AUTOFS_TIMEOUT 30
25 #define AUTOFS_EXPIRE_TIMER (5 * 1000)
27 struct hotplug_context
{
28 struct uloop_process process
;
33 struct vlist_node node
;
34 struct blob_attr
*msg
;
41 static struct uloop_fd fd_autofs_read
;
42 static int fd_autofs_write
= 0;
43 static struct ubus_auto_conn conn
;
44 struct blob_buf bb
= { 0 };
59 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
60 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
61 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
62 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
63 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
64 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
65 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
66 [MOUNT_AUTOFS
] = { .name
= "autofs", .type
= BLOBMSG_TYPE_INT32
},
67 [MOUNT_ANON
] = { .name
= "anon", .type
= BLOBMSG_TYPE_INT32
},
68 [MOUNT_REMOVE
] = { .name
= "remove", .type
= BLOBMSG_TYPE_INT32
},
76 static const struct blobmsg_policy info_policy
[__INFO_MAX
] = {
77 [INFO_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
81 _find_mount_point(char *device
)
85 if (asprintf(&dev
, "/dev/%s", device
) == -1)
88 mp
= find_mount_point(dev
, 0);
95 block(char *cmd
, char *action
, char *device
)
100 char *argv
[5] = { 0 };
105 ULOG_ERR("failed to fork block process\n");
109 argv
[a
++] = "/sbin/block";
113 execvp(argv
[0], argv
);
114 ULOG_ERR("failed to spawn %s %s %s\n", *argv
, action
, device
);
118 waitpid(pid
, &status
, 0);
119 ret
= WEXITSTATUS(status
);
121 ULOG_ERR("failed to run block. %s/%s\n", action
, device
);
128 static int hotplug_call_mount(const char *action
, const char *devname
,
129 uloop_process_handler cb
, void *priv
)
131 char * const argv
[] = { "hotplug-call", "mount", NULL
};
132 struct hotplug_context
*c
= NULL
;
137 c
= calloc(1, sizeof(*c
));
146 ULOG_ERR("fork() failed\n");
151 setenv("ACTION", action
, 1);
152 setenv("DEVICE", devname
, 1);
154 execv("/sbin/hotplug-call", argv
);
159 c
->process
.pid
= pid
;
162 uloop_process_add(&c
->process
);
170 static void device_mount_remove_hotplug_cb(struct uloop_process
*p
, int stat
)
172 struct hotplug_context
*hctx
= container_of(p
, struct hotplug_context
, process
);
173 struct device
*device
= hctx
->priv
;
177 unlink(device
->target
);
179 mp
= _find_mount_point(device
->name
);
181 block("autofs", "remove", device
->name
);
189 static void device_mount_remove(struct device
*device
)
191 hotplug_call_mount("remove", device
->name
,
192 device_mount_remove_hotplug_cb
, device
);
195 static void device_mount_add(struct device
*device
)
200 if (asprintf(&path
, "/tmp/run/blockd/%s", device
->name
) == -1)
203 if (!lstat(device
->target
, &st
)) {
204 if (S_ISLNK(st
.st_mode
))
205 unlink(device
->target
);
206 else if (S_ISDIR(st
.st_mode
))
207 rmdir(device
->target
);
209 if (symlink(path
, device
->target
))
210 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device
->target
, path
, errno
);
212 hotplug_call_mount("add", device
->name
, NULL
, NULL
);
218 device_move(struct device
*device_o
, struct device
*device_n
)
222 if (device_o
->autofs
!= device_n
->autofs
)
225 if (device_o
->anon
|| device_n
->anon
)
228 if (device_o
->autofs
) {
229 unlink(device_o
->target
);
230 if (asprintf(&path
, "/tmp/run/blockd/%s", device_n
->name
) == -1)
233 if (symlink(path
, device_n
->target
))
234 ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device_n
->target
, path
, errno
);
238 mkdir(device_n
->target
, 0755);
239 if (mount(device_o
->target
, device_n
->target
, NULL
, MS_MOVE
, NULL
))
240 rmdir(device_n
->target
);
242 rmdir(device_o
->target
);
248 static void vlist_nop_update(struct vlist_tree
*tree
,
249 struct vlist_node
*node_new
,
250 struct vlist_node
*node_old
)
254 VLIST_TREE(devices
, avl_strcmp
, vlist_nop_update
, false, false);
257 block_hotplug(struct ubus_context
*ctx
, struct ubus_object
*obj
,
258 struct ubus_request_data
*req
, const char *method
,
259 struct blob_attr
*msg
)
261 struct blob_attr
*data
[__MOUNT_MAX
];
262 struct device
*device
;
263 struct blob_attr
*_msg
;
264 char *devname
, *_name
;
265 char *target
= NULL
, *__target
;
266 char *_target
= NULL
;
268 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
270 if (!data
[MOUNT_DEVICE
])
271 return UBUS_STATUS_INVALID_ARGUMENT
;
273 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
275 if (data
[MOUNT_TARGET
]) {
276 target
= blobmsg_get_string(data
[MOUNT_TARGET
]);
278 if (asprintf(&_target
, "/mnt/%s",
279 blobmsg_get_string(data
[MOUNT_DEVICE
])) == -1)
285 if (data
[MOUNT_REMOVE
])
286 device
= vlist_find(&devices
, devname
, device
, node
);
288 device
= calloc_a(sizeof(*device
), &_msg
, blob_raw_len(msg
),
289 &_name
, strlen(devname
) + 1, &__target
, strlen(target
) + 1);
295 return UBUS_STATUS_UNKNOWN_ERROR
;
298 if (data
[MOUNT_REMOVE
]) {
299 vlist_delete(&devices
, &device
->node
);
302 device_mount_remove(device
);
309 struct device
*old
= vlist_find(&devices
, devname
, device
, node
);
311 device
->autofs
= data
[MOUNT_AUTOFS
] ? blobmsg_get_u32(data
[MOUNT_AUTOFS
]) : 0;
312 device
->anon
= data
[MOUNT_ANON
] ? blobmsg_get_u32(data
[MOUNT_ANON
]) : 0;
314 memcpy(_msg
, msg
, blob_raw_len(msg
));
315 device
->name
= _name
;
316 strcpy(_name
, devname
);
317 device
->target
= __target
;
318 strcpy(__target
, target
);
322 vlist_add(&devices
, &device
->node
, device
->name
);
324 if (old
&& !device_move(old
, device
)) {
325 if (device
->autofs
) {
326 device_mount_remove(old
);
327 device_mount_add(device
);
329 block("mount", NULL
, NULL
);
331 } else if (device
->autofs
) {
332 device_mount_add(device
);
339 static int blockd_mount(struct ubus_context
*ctx
, struct ubus_object
*obj
,
340 struct ubus_request_data
*req
, const char *method
,
341 struct blob_attr
*msg
)
343 struct blob_attr
*data
[__MOUNT_MAX
];
344 struct device
*device
;
347 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
349 if (!data
[MOUNT_DEVICE
])
350 return UBUS_STATUS_INVALID_ARGUMENT
;
352 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
354 device
= vlist_find(&devices
, devname
, device
, node
);
356 return UBUS_STATUS_UNKNOWN_ERROR
;
358 hotplug_call_mount("add", device
->name
, NULL
, NULL
);
363 struct blockd_umount_context
{
364 struct ubus_context
*ctx
;
365 struct ubus_request_data req
;
368 static void blockd_umount_hotplug_cb(struct uloop_process
*p
, int stat
)
370 struct hotplug_context
*hctx
= container_of(p
, struct hotplug_context
, process
);
371 struct blockd_umount_context
*c
= hctx
->priv
;
373 ubus_complete_deferred_request(c
->ctx
, &c
->req
, 0);
379 static int blockd_umount(struct ubus_context
*ctx
, struct ubus_object
*obj
,
380 struct ubus_request_data
*req
, const char *method
,
381 struct blob_attr
*msg
)
383 struct blob_attr
*data
[__MOUNT_MAX
];
384 struct blockd_umount_context
*c
;
388 blobmsg_parse(mount_policy
, __MOUNT_MAX
, data
, blob_data(msg
), blob_len(msg
));
390 if (!data
[MOUNT_DEVICE
])
391 return UBUS_STATUS_INVALID_ARGUMENT
;
393 devname
= blobmsg_get_string(data
[MOUNT_DEVICE
]);
395 c
= calloc(1, sizeof(*c
));
397 return UBUS_STATUS_UNKNOWN_ERROR
;
400 ubus_defer_request(ctx
, req
, &c
->req
);
402 err
= hotplug_call_mount("remove", devname
, blockd_umount_hotplug_cb
, c
);
405 return UBUS_STATUS_UNKNOWN_ERROR
;
411 static void block_info_dump(struct blob_buf
*b
, struct device
*device
)
417 blob_for_each_attr(v
, device
->msg
, rem
)
418 blobmsg_add_blob(b
, v
);
420 mp
= _find_mount_point(device
->name
);
422 blobmsg_add_string(b
, "mount", mp
);
424 } else if (device
->autofs
&& device
->target
) {
425 blobmsg_add_string(b
, "mount", device
->target
);
430 block_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
431 struct ubus_request_data
*req
, const char *method
,
432 struct blob_attr
*msg
)
434 struct blob_attr
*data
[__INFO_MAX
];
435 struct device
*device
= NULL
;
437 blobmsg_parse(info_policy
, __INFO_MAX
, data
, blob_data(msg
), blob_len(msg
));
439 if (data
[INFO_DEVICE
]) {
440 device
= vlist_find(&devices
, blobmsg_get_string(data
[INFO_DEVICE
]), device
, node
);
442 return UBUS_STATUS_INVALID_ARGUMENT
;
445 blob_buf_init(&bb
, 0);
447 block_info_dump(&bb
, device
);
451 a
= blobmsg_open_array(&bb
, "devices");
452 vlist_for_each_element(&devices
, device
, node
) {
455 t
= blobmsg_open_table(&bb
, "");
456 block_info_dump(&bb
, device
);
457 blobmsg_close_table(&bb
, t
);
459 blobmsg_close_array(&bb
, a
);
461 ubus_send_reply(ctx
, req
, bb
.head
);
466 static const struct ubus_method block_methods
[] = {
467 UBUS_METHOD("hotplug", block_hotplug
, mount_policy
),
468 UBUS_METHOD("mount", blockd_mount
, mount_policy
),
469 UBUS_METHOD("umount", blockd_umount
, mount_policy
),
470 UBUS_METHOD("info", block_info
, info_policy
),
473 static struct ubus_object_type block_object_type
=
474 UBUS_OBJECT_TYPE("block", block_methods
);
476 static struct ubus_object block_object
= {
478 .type
= &block_object_type
,
479 .methods
= block_methods
,
480 .n_methods
= ARRAY_SIZE(block_methods
),
484 ubus_connect_handler(struct ubus_context
*ctx
)
488 ret
= ubus_add_object(ctx
, &block_object
);
490 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
493 static int autofs_umount(void)
495 umount2(AUTOFS_MOUNT_PATH
, MNT_DETACH
);
499 static void autofs_read_handler(struct uloop_fd
*u
, unsigned int events
)
501 union autofs_v5_packet_union pktu
;
502 const struct autofs_v5_packet
*pkt
;
503 int cmd
= AUTOFS_IOC_READY
;
506 while (read(u
->fd
, &pktu
, sizeof(pktu
)) == -1) {
512 if (pktu
.hdr
.type
!= autofs_ptype_missing_indirect
) {
513 ULOG_ERR("unknown packet type %d\n", pktu
.hdr
.type
);
517 pkt
= &pktu
.missing_indirect
;
518 ULOG_ERR("kernel is requesting a mount -> %s\n", pkt
->name
);
519 if (lstat(pkt
->name
, &st
) == -1)
520 if (block("autofs", "add", (char *)pkt
->name
))
521 cmd
= AUTOFS_IOC_FAIL
;
523 if (ioctl(fd_autofs_write
, cmd
, pkt
->wait_queue_token
) < 0)
524 ULOG_ERR("failed to report back to kernel\n");
527 static void autofs_expire(struct uloop_timeout
*t
)
529 struct autofs_packet_expire pkt
;
531 while (ioctl(fd_autofs_write
, AUTOFS_IOC_EXPIRE
, &pkt
) == 0)
532 block("autofs", "remove", pkt
.name
);
534 uloop_timeout_set(t
, AUTOFS_EXPIRE_TIMER
);
537 struct uloop_timeout autofs_expire_timer
= {
541 static int autofs_mount(void)
543 int autofs_timeout
= AUTOFS_TIMEOUT
;
549 if (pipe(pipefd
) < 0) {
550 ULOG_ERR("failed to get kernel pipe\n");
554 snprintf(source
, sizeof(source
), "mountd(pid%u)", getpid());
555 snprintf(opts
, sizeof(opts
), "fd=%d,pgrp=%u,minproto=5,maxproto=5", pipefd
[1], (unsigned) getpgrp());
556 mkdir(AUTOFS_MOUNT_PATH
, 0555);
557 if (mount(source
, AUTOFS_MOUNT_PATH
, "autofs", 0, opts
)) {
558 ULOG_ERR("unable to mount autofs on %s\n", AUTOFS_MOUNT_PATH
);
564 fd_autofs_read
.fd
= pipefd
[0];
565 fd_autofs_read
.cb
= autofs_read_handler
;
566 uloop_fd_add(&fd_autofs_read
, ULOOP_READ
);
568 fd_autofs_write
= open(AUTOFS_MOUNT_PATH
, O_RDONLY
);
569 if(fd_autofs_write
< 0) {
571 ULOG_ERR("failed to open direcory\n");
575 ioctl(fd_autofs_write
, AUTOFS_IOC_PROTOVER
, &kproto_version
);
576 if (kproto_version
!= 5) {
577 ULOG_ERR("only kernel protocol version 5 is tested. You have %d.\n",
581 if (ioctl(fd_autofs_write
, AUTOFS_IOC_SETTIMEOUT
, &autofs_timeout
))
582 ULOG_ERR("failed to set autofs timeout\n");
584 uloop_timeout_set(&autofs_expire_timer
, AUTOFS_EXPIRE_TIMER
);
586 fcntl(fd_autofs_write
, F_SETFD
, fcntl(fd_autofs_write
, F_GETFD
) | FD_CLOEXEC
);
587 fcntl(fd_autofs_read
.fd
, F_SETFD
, fcntl(fd_autofs_read
.fd
, F_GETFD
) | FD_CLOEXEC
);
592 static void blockd_startup(struct uloop_timeout
*t
)
594 block("autofs", "start", NULL
);
597 struct uloop_timeout startup
= {
598 .cb
= blockd_startup
,
601 int main(int argc
, char **argv
)
603 ulog_open(ULOG_SYSLOG
| ULOG_STDIO
, LOG_DAEMON
, "blockd");
608 conn
.cb
= ubus_connect_handler
;
609 ubus_auto_connect(&conn
);
611 uloop_timeout_set(&startup
, 1000);
618 vlist_flush_all(&devices
);