| 1 | /*
|
|---|
| 2 | * Copyright (c) 2015 Michal Koutny
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <assert.h>
|
|---|
| 30 | #include <errno.h>
|
|---|
| 31 | #include <fibril_synch.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <vfs/vfs.h>
|
|---|
| 34 | #include <str.h>
|
|---|
| 35 |
|
|---|
| 36 | #include "log.h"
|
|---|
| 37 | #include "sysman.h"
|
|---|
| 38 | #include "unit.h"
|
|---|
| 39 |
|
|---|
| 40 | static const char *section_name = "Mount";
|
|---|
| 41 |
|
|---|
| 42 | static config_item_t unit_configuration[] = {
|
|---|
| 43 | { "What", &config_parse_string, offsetof(unit_mnt_t, device), NULL },
|
|---|
| 44 | { "Where", &config_parse_string, offsetof(unit_mnt_t, mountpoint), NULL },
|
|---|
| 45 | { "Type", &config_parse_string, offsetof(unit_mnt_t, type), NULL },
|
|---|
| 46 | { "Autostart", &config_parse_bool, offsetof(unit_mnt_t, autostart), "true" },
|
|---|
| 47 | { "Blocking", &config_parse_bool, offsetof(unit_mnt_t, blocking), "true" },
|
|---|
| 48 | CONFIGURATION_ITEM_SENTINEL
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | typedef struct {
|
|---|
| 52 | char *type;
|
|---|
| 53 | char *mountpoint;
|
|---|
| 54 | char *device;
|
|---|
| 55 | char *options;
|
|---|
| 56 | unsigned int flags;
|
|---|
| 57 | unsigned int instance;
|
|---|
| 58 |
|
|---|
| 59 | unit_t *unit;
|
|---|
| 60 | bool owner;
|
|---|
| 61 | } mount_data_t;
|
|---|
| 62 |
|
|---|
| 63 | static void mount_data_destroy(mount_data_t **mnt_data_ptr)
|
|---|
| 64 | {
|
|---|
| 65 | assert(mnt_data_ptr);
|
|---|
| 66 | if (*mnt_data_ptr == NULL) {
|
|---|
| 67 | return;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | mount_data_t *mnt_data = *mnt_data_ptr;
|
|---|
| 71 | free(mnt_data->type);
|
|---|
| 72 | free(mnt_data->mountpoint);
|
|---|
| 73 | free(mnt_data->device);
|
|---|
| 74 | free(mnt_data->options);
|
|---|
| 75 |
|
|---|
| 76 | free(mnt_data);
|
|---|
| 77 | *mnt_data_ptr = NULL;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | static bool mount_data_copy(mount_data_t *src, mount_data_t **dst_ptr)
|
|---|
| 81 | {
|
|---|
| 82 | mount_data_t *dst = malloc(sizeof(mount_data_t));
|
|---|
| 83 | if (dst == NULL) {
|
|---|
| 84 | goto fail;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | dst->type = str_dup(src->type);
|
|---|
| 88 | if (dst->type == NULL)
|
|---|
| 89 | goto fail;
|
|---|
| 90 |
|
|---|
| 91 | dst->mountpoint = str_dup(src->mountpoint);
|
|---|
| 92 | if (dst->mountpoint == NULL)
|
|---|
| 93 | goto fail;
|
|---|
| 94 |
|
|---|
| 95 | dst->device = str_dup(src->device);
|
|---|
| 96 | if (dst->device == NULL)
|
|---|
| 97 | goto fail;
|
|---|
| 98 |
|
|---|
| 99 | dst->options = src->options ? str_dup(src->options) : NULL;
|
|---|
| 100 | if (src->options != NULL && dst->options == NULL)
|
|---|
| 101 | goto fail;
|
|---|
| 102 |
|
|---|
| 103 | dst->flags = src->flags;
|
|---|
| 104 | dst->instance = src->instance;
|
|---|
| 105 | dst->unit = src->unit;
|
|---|
| 106 | dst->owner = true;
|
|---|
| 107 |
|
|---|
| 108 | *dst_ptr = dst;
|
|---|
| 109 | return true;
|
|---|
| 110 |
|
|---|
| 111 | fail:
|
|---|
| 112 | mount_data_destroy(&dst);
|
|---|
| 113 | return false;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static void unit_mnt_init(unit_t *unit)
|
|---|
| 117 | {
|
|---|
| 118 | unit_mnt_t *u_mnt = CAST_MNT(unit);
|
|---|
| 119 | assert(u_mnt);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | static void unit_mnt_destroy(unit_t *unit)
|
|---|
| 123 | {
|
|---|
| 124 | assert(unit->type == UNIT_MOUNT);
|
|---|
| 125 | unit_mnt_t *u_mnt = CAST_MNT(unit);
|
|---|
| 126 |
|
|---|
| 127 | free(u_mnt->type);
|
|---|
| 128 | free(u_mnt->mountpoint);
|
|---|
| 129 | free(u_mnt->device);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | static errno_t unit_mnt_load(unit_t *unit, ini_configuration_t *ini_conf,
|
|---|
| 133 | text_parse_t *text_parse)
|
|---|
| 134 | {
|
|---|
| 135 | unit_mnt_t *u_mnt = CAST_MNT(unit);
|
|---|
| 136 | assert(u_mnt);
|
|---|
| 137 |
|
|---|
| 138 | ini_section_t *section = ini_get_section(ini_conf, section_name);
|
|---|
| 139 | if (section == NULL) {
|
|---|
| 140 | sysman_log(LVL_ERROR,
|
|---|
| 141 | "Expected section '%s' in configuration of unit '%s'",
|
|---|
| 142 | section_name, unit_name(unit));
|
|---|
| 143 | return ENOENT;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | return config_load_ini_section(unit_configuration, section, u_mnt,
|
|---|
| 147 | text_parse);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | static errno_t mount_exec(void *arg)
|
|---|
| 151 | {
|
|---|
| 152 | mount_data_t *mnt_data = arg;
|
|---|
| 153 | sysman_log(LVL_DEBUG2, "%s(%p, %p, %p, %p, %x, %u)",
|
|---|
| 154 | __func__,
|
|---|
| 155 | mnt_data->type, mnt_data->mountpoint, mnt_data->device, mnt_data->options,
|
|---|
| 156 | mnt_data->flags, mnt_data->instance);
|
|---|
| 157 |
|
|---|
| 158 | errno_t rc = vfs_mount_path(mnt_data->type, mnt_data->mountpoint, mnt_data->device,
|
|---|
| 159 | mnt_data->options ? mnt_data->options : "",
|
|---|
| 160 | mnt_data->flags, mnt_data->instance);
|
|---|
| 161 |
|
|---|
| 162 | if (rc == EOK) {
|
|---|
| 163 | sysman_log(LVL_DEBUG, "Mount ('%s') mounted",
|
|---|
| 164 | unit_name(mnt_data->unit));
|
|---|
| 165 | /*
|
|---|
| 166 | * Emulate future VFS broker fibril that notifies about created
|
|---|
| 167 | * exposee.
|
|---|
| 168 | * Difference: It'll notify exposee name only, we'll have to
|
|---|
| 169 | * match it...
|
|---|
| 170 | */
|
|---|
| 171 | sysman_raise_event(&sysman_event_unit_exposee_created,
|
|---|
| 172 | mnt_data->unit);
|
|---|
| 173 | } else {
|
|---|
| 174 | sysman_log(LVL_ERROR, "Mount ('%s') failed (%i)",
|
|---|
| 175 | unit_name(mnt_data->unit), rc);
|
|---|
| 176 | /*
|
|---|
| 177 | * Think about analogy of this event, probably timeout or sthing
|
|---|
| 178 | */
|
|---|
| 179 | sysman_raise_event(&sysman_event_unit_failed,
|
|---|
| 180 | mnt_data->unit);
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if (mnt_data->owner) {
|
|---|
| 184 | mount_data_destroy(&mnt_data);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | static errno_t unit_mnt_start(unit_t *unit)
|
|---|
| 191 | {
|
|---|
| 192 | unit_mnt_t *u_mnt = CAST_MNT(unit);
|
|---|
| 193 | assert(u_mnt);
|
|---|
| 194 | /* autostart implies blocking */
|
|---|
| 195 | assert(!u_mnt->autostart || u_mnt->blocking);
|
|---|
| 196 |
|
|---|
| 197 | assert(unit->state == STATE_STOPPED);
|
|---|
| 198 |
|
|---|
| 199 | mount_data_t mnt_data;
|
|---|
| 200 | memset(&mnt_data, 0, sizeof(mnt_data));
|
|---|
| 201 | mnt_data.type = u_mnt->type;
|
|---|
| 202 | mnt_data.mountpoint = u_mnt->mountpoint;
|
|---|
| 203 | mnt_data.device = u_mnt->device;
|
|---|
| 204 | /*
|
|---|
| 205 | * TODO use other mount parameters
|
|---|
| 206 | * mnt_data.options = u_mnt->options;
|
|---|
| 207 | * mnt_data.instance = u_mnt->instance;
|
|---|
| 208 | */
|
|---|
| 209 |
|
|---|
| 210 | mnt_data.flags |= u_mnt->blocking ? IPC_FLAG_BLOCKING : 0;
|
|---|
| 211 | mnt_data.flags |= u_mnt->autostart ? IPC_AUTOSTART : 0;
|
|---|
| 212 | mnt_data.unit = unit;
|
|---|
| 213 |
|
|---|
| 214 | if (u_mnt->blocking) {
|
|---|
| 215 | mount_data_t *heap_mnt_data = NULL;
|
|---|
| 216 | if (!mount_data_copy(&mnt_data, &heap_mnt_data)) {
|
|---|
| 217 | return ENOMEM;
|
|---|
| 218 | }
|
|---|
| 219 | fid_t fib = fibril_create(&mount_exec, heap_mnt_data);
|
|---|
| 220 | unit->state = STATE_STARTING;
|
|---|
| 221 | fibril_add_ready(fib);
|
|---|
| 222 | } else {
|
|---|
| 223 | unit->state = STATE_STARTING;
|
|---|
| 224 | mount_exec(&mnt_data);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | return EOK;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | static errno_t unit_mnt_stop(unit_t *unit)
|
|---|
| 231 | {
|
|---|
| 232 | unit_mnt_t *u_mnt = CAST_MNT(unit);
|
|---|
| 233 | assert(u_mnt);
|
|---|
| 234 | /* autostart implies blocking */
|
|---|
| 235 | assert(!u_mnt->autostart || u_mnt->blocking);
|
|---|
| 236 |
|
|---|
| 237 | // note: we should never hit STATE_STARTING, since it'd mean there are
|
|---|
| 238 | // two jobs running at once (unless job cancellation is implemented)
|
|---|
| 239 | assert(unit->state == STATE_STARTED);
|
|---|
| 240 |
|
|---|
| 241 | /*
|
|---|
| 242 | * We don't expect unmount to be blocking, since if some files are
|
|---|
| 243 | * being used, it'd return EBUSY immediately. That's why we call
|
|---|
| 244 | * unmount synchronously in the event loop fibril.
|
|---|
| 245 | */
|
|---|
| 246 | errno_t rc = vfs_unmount_path(u_mnt->mountpoint);
|
|---|
| 247 |
|
|---|
| 248 | if (rc == EOK) {
|
|---|
| 249 | unit->state = STATE_STOPPED;
|
|---|
| 250 | return EOK;
|
|---|
| 251 | } else if (rc == EBUSY) {
|
|---|
| 252 | assert(unit->state == STATE_STARTED);
|
|---|
| 253 | return EBUSY;
|
|---|
| 254 | } else {
|
|---|
| 255 | /*
|
|---|
| 256 | * Mount may be still usable, but be conservative and mark unit
|
|---|
| 257 | * as failed.
|
|---|
| 258 | */
|
|---|
| 259 | unit->state = STATE_FAILED;
|
|---|
| 260 | return rc;
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | static void unit_mnt_exposee_created(unit_t *unit)
|
|---|
| 265 | {
|
|---|
| 266 | assert(CAST_MNT(unit));
|
|---|
| 267 | assert(unit->state == STATE_STOPPED || unit->state == STATE_STARTING);
|
|---|
| 268 |
|
|---|
| 269 | if (str_cmp(unit_name(unit), "rootfs.mnt") == 0) {
|
|---|
| 270 | sysman_log_tofile();
|
|---|
| 271 | }
|
|---|
| 272 | unit->state = STATE_STARTED;
|
|---|
| 273 | unit_notify_state(unit);
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | static void unit_mnt_fail(unit_t *unit)
|
|---|
| 277 | {
|
|---|
| 278 | assert(CAST_MNT(unit));
|
|---|
| 279 | assert(unit->state == STATE_STARTING);
|
|---|
| 280 |
|
|---|
| 281 | unit->state = STATE_FAILED;
|
|---|
| 282 | unit_notify_state(unit);
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | DEFINE_UNIT_VMT(unit_mnt);
|
|---|