#define AUTOFS_TIMEOUT 30
#define AUTOFS_EXPIRE_TIMER (5 * 1000)
+struct hotplug_context {
+ struct uloop_process process;
+ void *priv;
+};
+
struct device {
struct vlist_node node;
struct blob_attr *msg;
[MOUNT_REMOVE] = { .name = "remove", .type = BLOBMSG_TYPE_INT32 },
};
+enum {
+ INFO_DEVICE,
+ __INFO_MAX
+};
+
+static const struct blobmsg_policy info_policy[__INFO_MAX] = {
+ [INFO_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
+};
+
static char*
_find_mount_point(char *device)
{
return ret;
}
-static void
-device_free(struct device *device)
+static int hotplug_call_mount(const char *action, const char *devname,
+ uloop_process_handler cb, void *priv)
{
- char *mp;
+ char * const argv[] = { "hotplug-call", "mount", NULL };
+ struct hotplug_context *c = NULL;
+ pid_t pid;
+ int err;
+
+ if (cb) {
+ c = calloc(1, sizeof(*c));
+ if (!c)
+ return -ENOMEM;
+ }
- if (!device->autofs)
- return;
+ pid = fork();
+ switch (pid) {
+ case -1:
+ err = -errno;
+ ULOG_ERR("fork() failed\n");
+ return err;
+ case 0:
+ uloop_end();
- block("autofs", "unavailable", device->name);
+ setenv("ACTION", action, 1);
+ setenv("DEVICE", devname, 1);
+
+ execv("/sbin/hotplug-call", argv);
+ exit(-1);
+ break;
+ default:
+ if (c) {
+ c->process.pid = pid;
+ c->process.cb = cb;
+ c->priv = priv;
+ uloop_process_add(&c->process);
+ }
+ break;
+ }
+
+ return 0;
+}
+
+static void device_mount_remove_hotplug_cb(struct uloop_process *p, int stat)
+{
+ struct hotplug_context *hctx = container_of(p, struct hotplug_context, process);
+ struct device *device = hctx->priv;
+ char *mp;
if (device->target)
unlink(device->target);
block("autofs", "remove", device->name);
free(mp);
}
+
+ free(device);
+ free(hctx);
}
-static void
-device_add(struct device *device)
+static void device_mount_remove(struct device *device)
{
- char path[64];
+ hotplug_call_mount("remove", device->name,
+ device_mount_remove_hotplug_cb, device);
+}
- if (!device->autofs)
- return;
+static void device_mount_add(struct device *device)
+{
+ struct stat st;
+ char path[64];
snprintf(path, sizeof(path), "/tmp/run/blockd/%s", device->name);
+ if (!lstat(device->target, &st)) {
+ if (S_ISLNK(st.st_mode))
+ unlink(device->target);
+ else if (S_ISDIR(st.st_mode))
+ rmdir(device->target);
+ }
if (symlink(path, device->target))
- ULOG_ERR("failed to symlink %s->%s\n", device->target, path);
+ ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device->target, path, errno);
else
- block("autofs", "available", device->name);
+ hotplug_call_mount("add", device->name, NULL, NULL);
}
static int
unlink(device_o->target);
snprintf(path, sizeof(path), "/tmp/run/blockd/%s", device_n->name);
if (symlink(path, device_n->target))
- ULOG_ERR("failed to symlink %s->%s\n", device_n->target, path);
+ ULOG_ERR("failed to symlink %s->%s (%d) - %m\n", device_n->target, path, errno);
} else {
mkdir(device_n->target, 0755);
if (mount(device_o->target, device_n->target, NULL, MS_MOVE, NULL))
return 0;
}
-static void
-devices_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
- struct vlist_node *node_old)
+static void vlist_nop_update(struct vlist_tree *tree,
+ struct vlist_node *node_new,
+ struct vlist_node *node_old)
{
- struct device *device_o = NULL, *device_n = NULL;
-
- if (node_old)
- device_o = container_of(node_old, struct device, node);
-
- if (node_new)
- device_n = container_of(node_new, struct device, node);
-
- if (device_o && device_n) {
- if (device_move(device_o, device_n)) {
- device_free(device_o);
- device_add(device_n);
- if (!device_n->autofs)
- block("mount", NULL, NULL);
- }
- } else if (device_n) {
- device_add(device_n);
- } else {
- device_free(device_o);
- }
-
- if (device_o)
- free(device_o);
}
-VLIST_TREE(devices, avl_strcmp, devices_update_cb, false, false);
+VLIST_TREE(devices, avl_strcmp, vlist_nop_update, false, false);
static int
block_hotplug(struct ubus_context *ctx, struct ubus_object *obj,
if (!device)
return UBUS_STATUS_UNKNOWN_ERROR;
- vlist_update(&devices);
if (data[MOUNT_REMOVE]) {
vlist_delete(&devices, &device->node);
- } else {
- if (data[MOUNT_AUTOFS])
- device->autofs = blobmsg_get_u32(data[MOUNT_AUTOFS]);
- else
- device->autofs = 0;
- if (data[MOUNT_ANON])
- device->anon = blobmsg_get_u32(data[MOUNT_ANON]);
+
+ if (device->autofs)
+ device_mount_remove(device);
else
- device->anon = 0;
+ free(device);
+ } else {
+ struct device *old = vlist_find(&devices, devname, device, node);
+
+ device->autofs = data[MOUNT_AUTOFS] ? blobmsg_get_u32(data[MOUNT_AUTOFS]) : 0;
+ device->anon = data[MOUNT_ANON] ? blobmsg_get_u32(data[MOUNT_ANON]) : 0;
device->msg = _msg;
memcpy(_msg, msg, blob_raw_len(msg));
device->name = _name;
strcpy(_name, devname);
device->target = __target;
strcpy(__target, target);
- vlist_add(&devices, &device->node, blobmsg_get_string(data[MOUNT_DEVICE]));
+
+ vlist_add(&devices, &device->node, device->name);
+
+ if (old && !device_move(old, device)) {
+ if (device->autofs) {
+ device_mount_remove(old);
+ device_mount_add(device);
+ } else {
+ block("mount", NULL, NULL);
+ }
+ } else if (device->autofs) {
+ device_mount_add(device);
+ }
+ }
+
+ return 0;
+}
+
+static int blockd_mount(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *data[__MOUNT_MAX];
+ struct device *device;
+ char *devname;
+
+ blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
+
+ if (!data[MOUNT_DEVICE])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ devname = blobmsg_get_string(data[MOUNT_DEVICE]);
+
+ device = vlist_find(&devices, devname, device, node);
+ if (!device)
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ hotplug_call_mount("add", device->name, NULL, NULL);
+
+ return 0;
+}
+
+struct blockd_umount_context {
+ struct ubus_context *ctx;
+ struct ubus_request_data req;
+};
+
+static void blockd_umount_hotplug_cb(struct uloop_process *p, int stat)
+{
+ struct hotplug_context *hctx = container_of(p, struct hotplug_context, process);
+ struct blockd_umount_context *c = hctx->priv;
+
+ ubus_complete_deferred_request(c->ctx, &c->req, 0);
+
+ free(c);
+ free(hctx);
+}
+
+static int blockd_umount(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ struct blob_attr *data[__MOUNT_MAX];
+ struct blockd_umount_context *c;
+ char *devname;
+ int err;
+
+ blobmsg_parse(mount_policy, __MOUNT_MAX, data, blob_data(msg), blob_len(msg));
+
+ if (!data[MOUNT_DEVICE])
+ return UBUS_STATUS_INVALID_ARGUMENT;
+
+ devname = blobmsg_get_string(data[MOUNT_DEVICE]);
+
+ c = calloc(1, sizeof(*c));
+ if (!c)
+ return UBUS_STATUS_UNKNOWN_ERROR;
+
+ c->ctx = ctx;
+ ubus_defer_request(ctx, req, &c->req);
+
+ err = hotplug_call_mount("remove", devname, blockd_umount_hotplug_cb, c);
+ if (err) {
+ free(c);
+ return UBUS_STATUS_UNKNOWN_ERROR;
}
- vlist_flush(&devices);
return 0;
}
+static void block_info_dump(struct blob_buf *b, struct device *device)
+{
+ struct blob_attr *v;
+ char *mp;
+ int rem;
+
+ blob_for_each_attr(v, device->msg, rem)
+ blobmsg_add_blob(b, v);
+
+ mp = _find_mount_point(device->name);
+ if (mp) {
+ blobmsg_add_string(b, "mount", mp);
+ free(mp);
+ } else if (device->autofs && device->target) {
+ blobmsg_add_string(b, "mount", device->target);
+ }
+}
+
static int
block_info(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
- struct device *device;
- void *a;
+ struct blob_attr *data[__INFO_MAX];
+ struct device *device = NULL;
+
+ blobmsg_parse(info_policy, __INFO_MAX, data, blob_data(msg), blob_len(msg));
+
+ if (data[INFO_DEVICE]) {
+ device = vlist_find(&devices, blobmsg_get_string(data[INFO_DEVICE]), device, node);
+ if (!device)
+ return UBUS_STATUS_INVALID_ARGUMENT;
+ }
blob_buf_init(&bb, 0);
- a = blobmsg_open_array(&bb, "devices");
- vlist_for_each_element(&devices, device, node) {
- void *t = blobmsg_open_table(&bb, "");
- struct blob_attr *v;
- char *mp;
- int rem;
-
- blob_for_each_attr(v, device->msg, rem)
- blobmsg_add_blob(&bb, v);
-
- mp = _find_mount_point(device->name);
- if (mp) {
- blobmsg_add_string(&bb, "mount", mp);
- free(mp);
+ if (device) {
+ block_info_dump(&bb, device);
+ } else {
+ void *a;
+
+ a = blobmsg_open_array(&bb, "devices");
+ vlist_for_each_element(&devices, device, node) {
+ void *t;
+
+ t = blobmsg_open_table(&bb, "");
+ block_info_dump(&bb, device);
+ blobmsg_close_table(&bb, t);
}
- blobmsg_close_table(&bb, t);
+ blobmsg_close_array(&bb, a);
}
- blobmsg_close_array(&bb, a);
ubus_send_reply(ctx, req, bb.head);
return 0;
static const struct ubus_method block_methods[] = {
UBUS_METHOD("hotplug", block_hotplug, mount_policy),
- UBUS_METHOD_NOARG("info", block_info),
+ UBUS_METHOD("mount", blockd_mount, mount_policy),
+ UBUS_METHOD("umount", blockd_umount, mount_policy),
+ UBUS_METHOD("info", block_info, info_policy),
};
static struct ubus_object_type block_object_type =