core: rework how we match mount units against each other

Previously to automatically create dependencies between mount units we
matched every mount unit agains all others resulting in O(n^2)
complexity. On setups with large amounts of mount units this might make
things slow.

This change replaces the matching code to use a hashtable that is keyed
by a path prefix, and points to a set of units that require that path to
be around. When a new mount unit is installed it is hence sufficient to
simply look up this set of units via its own file system paths to know
which units to order after itself.

This patch also changes all unit types to only create automatic mount
dependencies via the RequiresMountsFor= logic, and this is exposed to
the outside to make things more transparent.

With this change we still have some O(n) complexities in place when
handling mounts, but that's currently unavoidable due to kernel APIs,
and still substantially better than O(n^2) as before.

https://bugs.freedesktop.org/show_bug.cgi?id=69740
This commit is contained in:
Lennart Poettering
2013-09-26 20:20:30 +02:00
parent 6270c1bd8f
commit a57f7e2c82
17 changed files with 321 additions and 407 deletions
+10 -39
View File
@@ -258,53 +258,24 @@ static int socket_verify(Socket *s) {
return 0;
}
static bool socket_needs_mount(Socket *s, const char *prefix) {
static int socket_add_mount_links(Socket *s) {
SocketPort *p;
int r;
assert(s);
LIST_FOREACH(port, p, s->ports) {
const char *path = NULL;
if (p->type == SOCKET_SOCKET) {
if (socket_address_needs_mount(&p->address, prefix))
return true;
} else if (p->type == SOCKET_FIFO || p->type == SOCKET_SPECIAL) {
if (path_startswith(p->path, prefix))
return true;
}
}
if (p->type == SOCKET_SOCKET)
path = socket_address_get_path(&p->address);
else if (p->type == SOCKET_FIFO || p->type == SOCKET_SPECIAL)
path = p->path;
return false;
}
if (!path)
continue;
int socket_add_one_mount_link(Socket *s, Mount *m) {
int r;
assert(s);
assert(m);
if (UNIT(s)->load_state != UNIT_LOADED ||
UNIT(m)->load_state != UNIT_LOADED)
return 0;
if (!socket_needs_mount(s, m->where))
return 0;
r = unit_add_two_dependencies(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true);
if (r < 0)
return r;
return 0;
}
static int socket_add_mount_links(Socket *s) {
Unit *other;
int r;
assert(s);
LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_MOUNT]) {
r = socket_add_one_mount_link(s, MOUNT(other));
r = unit_require_mounts_for(UNIT(s), path);
if (r < 0)
return r;
}