+ return 0;
+}
+
+static int mount_add_path_links(Mount *m) {
+ Meta *other;
+ int r;
+
+ /* Adds in link to other mount points, that might lie below or
+ * above us in the hierarchy */
+
+ LIST_FOREACH(units_per_type, other, UNIT(m)->meta.manager->units_per_type[UNIT_MOUNT]) {
+ Mount *n;
+
+ n = (Mount*) other;
+
+ if (n == m)
+ continue;
+
+ if (path_startswith(m->where, n->where)) {
+
+ if ((r = unit_add_dependency(UNIT(m), UNIT_AFTER, (Unit*) other)) < 0)
+ return r;
+
+ if ((r = unit_add_dependency(UNIT(m), UNIT_REQUIRES, (Unit*) other)) < 0)
+ return r;
+
+ } else if (startswith(n->where, m->where)) {
+
+ if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, (Unit*) other)) < 0)
+ return r;
+
+ if ((r = unit_add_dependency((Unit*) other, UNIT_REQUIRES, UNIT(m))) < 0)
+ return r;
+ }
+ }
+
+ return 0;
+}
+
+static int mount_add_one(Manager *m, const char *what, const char *where, bool live, bool set_flags) {
+ char *e;
+ int r;
+ Unit *u;
+ bool delete;
+
+ assert(m);
+ assert(what);
+ assert(where);
+
+ /* probably some kind of swap, which we don't cover for now */
+ if (where[0] != '/')
+ return 0;
+
+ if (streq(where, "/"))
+ e = strdup("-.mount");
+ else
+ e = unit_name_escape_path(where+1, ".mount");
+
+ if (!e)
+ return -ENOMEM;
+
+ if (!(u = manager_get_unit(m, e))) {
+ delete = true;
+
+ if (!(u = unit_new(m))) {
+ free(e);
+ return -ENOMEM;
+ }
+
+ r = unit_add_name(u, e);
+ free(e);
+
+ if (r < 0)
+ goto fail;
+
+ if (!(MOUNT(u)->what = strdup(what)) ||
+ !(MOUNT(u)->where = strdup(where))) {
+ r = -ENOMEM;
+ goto fail;
+ }
+
+ if ((r = unit_set_description(u, where)) < 0)
+ goto fail;
+
+ unit_add_to_load_queue(u);
+ } else {
+ delete = false;
+ free(e);
+ }
+
+ if (set_flags)
+ MOUNT(u)->still_exists = true;
+
+ if (live) {
+ if (set_flags)
+ MOUNT(u)->just_created = !MOUNT(u)->from_proc_self_mountinfo;
+ MOUNT(u)->from_proc_self_mountinfo = true;
+ } else {
+ if (set_flags)
+ MOUNT(u)->just_created = !MOUNT(u)->from_etc_fstab;
+ MOUNT(u)->from_etc_fstab = true;
+ }
+
+ if ((r = mount_add_node_links(MOUNT(u))) < 0)
+ goto fail;
+
+ if ((r = mount_add_path_links(MOUNT(u))) < 0)
+ goto fail;
+
+ unit_add_to_dbus_queue(u);
+
+ return 0;
+
+fail:
+ if (delete && u)
+ unit_free(u);
+
+ return 0;
+}
+
+static char *fstab_node_to_udev_node(char *p) {
+ char *dn, *t;
+ int r;
+
+ /* FIXME: to follow udev's logic 100% we need to leave valid
+ * UTF8 chars unescaped */
+
+ if (startswith(p, "LABEL=")) {
+
+ if (!(t = xescape(p+6, "/ ")))
+ return NULL;
+
+ r = asprintf(&dn, "/dev/disk/by-label/%s", t);
+ free(t);
+
+ if (r < 0)
+ return NULL;
+
+ return dn;
+ }
+
+ if (startswith(p, "UUID=")) {
+
+ if (!(t = xescape(p+5, "/ ")))
+ return NULL;
+
+ r = asprintf(&dn, "/dev/disk/by-uuid/%s", ascii_strlower(t));
+ free(t);
+
+ if (r < 0)
+ return NULL;
+
+ return dn;
+ }
+
+ return strdup(p);
+}
+
+static int mount_load_etc_fstab(Manager *m, bool set_flags) {
+ FILE *f;
+ int r;
+ struct mntent* me;
+
+ assert(m);
+
+ errno = 0;
+ if (!(f = setmntent("/etc/fstab", "r")))
+ return -errno;
+
+ while ((me = getmntent(f))) {
+ char *where, *what;
+
+ if (!(what = fstab_node_to_udev_node(me->mnt_fsname))) {
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (!(where = strdup(me->mnt_dir))) {
+ free(what);
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (what[0] == '/')
+ path_kill_slashes(what);
+
+ if (where[0] == '/')
+ path_kill_slashes(where);
+
+ r = mount_add_one(m, what, where, false, set_flags);
+ free(what);
+ free(where);
+
+ if (r < 0)
+ goto finish;
+ }
+
+ r = 0;
+finish:
+
+ endmntent(f);