The Android Open Source Project | 8ac3a13 | 2009-01-20 14:04:01 -0800 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2008 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <errno.h> |
| 21 | #include <dirent.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sched.h> |
| 24 | |
| 25 | #include <sys/mount.h> |
| 26 | |
| 27 | #include <cutils/config_utils.h> |
| 28 | #include <cutils/properties.h> |
| 29 | |
| 30 | #include "vold.h" |
| 31 | #include "volmgr.h" |
| 32 | #include "blkdev.h" |
| 33 | #include "ums.h" |
| 34 | |
| 35 | #include "volmgr_ext3.h" |
| 36 | #include "volmgr_vfat.h" |
| 37 | |
| 38 | #define DEBUG_VOLMGR 0 |
| 39 | |
| 40 | static volume_t *vol_root = NULL; |
| 41 | |
| 42 | static struct volmgr_fstable_entry fs_table[] = { |
| 43 | { "ext3", ext3_identify, ext3_check, ext3_mount }, |
| 44 | { "vfat", vfat_identify, vfat_check, vfat_mount }, |
| 45 | { NULL, NULL, NULL, NULL } |
| 46 | }; |
| 47 | |
| 48 | struct _volume_state_event_map { |
| 49 | volume_state_t state; |
| 50 | char *event; |
| 51 | char *property_val; |
| 52 | }; |
| 53 | |
| 54 | static struct _volume_state_event_map volume_state_strings[] = { |
| 55 | { volstate_unknown, "volstate_unknown:", "unknown" }, |
| 56 | { volstate_nomedia, VOLD_EVT_NOMEDIA, VOLD_ES_PVAL_NOMEDIA }, |
| 57 | { volstate_unmounted, VOLD_EVT_UNMOUNTED, VOLD_ES_PVAL_UNMOUNTED }, |
| 58 | { volstate_checking, VOLD_EVT_CHECKING, VOLD_ES_PVAL_CHECKING }, |
| 59 | { volstate_mounted, VOLD_EVT_MOUNTED, VOLD_ES_PVAL_MOUNTED }, |
| 60 | { volstate_mounted_ro, VOLD_EVT_MOUNTED_RO, VOLD_ES_PVAL_MOUNTED_RO }, |
| 61 | { volstate_badremoval, VOLD_EVT_BADREMOVAL, VOLD_ES_PVAL_BADREMOVAL }, |
| 62 | { volstate_damaged, VOLD_EVT_DAMAGED, VOLD_ES_PVAL_DAMAGED }, |
| 63 | { volstate_nofs, VOLD_EVT_NOFS, VOLD_ES_PVAL_NOFS }, |
| 64 | { volstate_ums, VOLD_EVT_UMS, VOLD_ES_PVAL_UMS }, |
| 65 | { 0, NULL, NULL } |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | static int volmgr_readconfig(char *cfg_path); |
| 70 | static int volmgr_config_volume(cnode *node); |
| 71 | static volume_t *volmgr_lookup_volume_by_mediapath(char *media_path, boolean fuzzy); |
| 72 | static volume_t *volmgr_lookup_volume_by_dev(blkdev_t *dev); |
| 73 | static int _volmgr_start(volume_t *vol, blkdev_t *dev); |
| 74 | static int volmgr_start_fs(struct volmgr_fstable_entry *fs, volume_t *vol, blkdev_t *dev); |
| 75 | static void *volmgr_start_fs_thread(void *arg); |
| 76 | static void volmgr_start_fs_thread_sighandler(int signo); |
| 77 | static void volume_setstate(volume_t *vol, volume_state_t state); |
| 78 | static char *conv_volstate_to_eventstr(volume_state_t state); |
| 79 | static char *conv_volstate_to_propstr(volume_state_t state); |
| 80 | static int volume_send_state(volume_t *vol); |
| 81 | static void _cb_volstopped_for_ums_enable(volume_t *v); |
| 82 | static int _volmgr_enable_ums(volume_t *); |
| 83 | static int volmgr_shutdown_volume(volume_t *v, void (* cb) (volume_t *)); |
| 84 | static int volmgr_stop_volume(volume_t *v, void (*cb) (volume_t *, void *), void *arg, int emit_statechange); |
| 85 | static void _cb_volume_stopped_for_eject(volume_t *v, void *arg); |
| 86 | static void _cb_volume_stopped_for_shutdown(volume_t *v, void *arg); |
| 87 | static int _volmgr_consider_disk_and_vol(volume_t *vol, blkdev_t *dev); |
| 88 | static void volmgr_uncage_reaper(volume_t *vol); |
| 89 | static void volmgr_reaper_thread_sighandler(int signo); |
| 90 | |
| 91 | /* |
| 92 | * Public functions |
| 93 | */ |
| 94 | int volmgr_bootstrap(void) |
| 95 | { |
| 96 | int rc; |
| 97 | |
| 98 | if ((rc = volmgr_readconfig("/system/etc/vold.conf")) < 0) { |
| 99 | LOGE("Unable to process config\n"); |
| 100 | return rc; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | int volmgr_send_states(void) |
| 107 | { |
| 108 | volume_t *vol_scan = vol_root; |
| 109 | int rc; |
| 110 | |
| 111 | while (vol_scan) { |
| 112 | pthread_mutex_lock(&vol_scan->lock); |
| 113 | if ((rc = volume_send_state(vol_scan)) < 0) { |
| 114 | LOGE("Error sending state to framework (%d)\n", rc); |
| 115 | } |
| 116 | pthread_mutex_unlock(&vol_scan->lock); |
| 117 | vol_scan = vol_scan->next; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Called when a block device is ready to be |
| 125 | * evaluated by the volume manager. |
| 126 | */ |
| 127 | int volmgr_consider_disk(blkdev_t *dev) |
| 128 | { |
| 129 | volume_t *vol; |
| 130 | |
| 131 | if (!(vol = volmgr_lookup_volume_by_mediapath(dev->media->devpath, true))) { |
| 132 | LOG_VOL("volmgr ignoring '%s' - no matching volume found\n", dev->media->devpath); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | pthread_mutex_lock(&vol->lock); |
| 137 | int rc = _volmgr_consider_disk_and_vol(vol, dev); |
| 138 | pthread_mutex_unlock(&vol->lock); |
| 139 | return rc; |
| 140 | } |
| 141 | |
| 142 | int volmgr_start_volume_by_mountpoint(char *mount_point) |
| 143 | { |
| 144 | volume_t *v = vol_root; |
| 145 | |
| 146 | while(v) { |
| 147 | if (!strcmp(v->mount_point, mount_point)) { |
| 148 | pthread_mutex_lock(&v->lock); |
| 149 | if (!v->dev) { |
| 150 | LOGE("Cannot start volume '%s' (volume is not bound to a blkdev)\n", mount_point); |
| 151 | pthread_mutex_unlock(&v->lock); |
| 152 | return -ENOENT; |
| 153 | } |
| 154 | |
| 155 | if (_volmgr_consider_disk_and_vol(v, v->dev->disk) < 0) { |
| 156 | LOGE("volmgr failed to start volume '%s'\n", v->mount_point); |
| 157 | } |
| 158 | pthread_mutex_unlock(&v->lock); |
| 159 | return 0; |
| 160 | } |
| 161 | v = v->next; |
| 162 | } |
| 163 | |
| 164 | return -ENOENT; |
| 165 | } |
| 166 | |
| 167 | int volmgr_stop_volume_by_mountpoint(char *mount_point) |
| 168 | { |
| 169 | volume_t *v = vol_root; |
| 170 | |
| 171 | while(v) { |
| 172 | if (!strcmp(v->mount_point, mount_point)) { |
| 173 | pthread_mutex_lock(&v->lock); |
| 174 | if (volmgr_shutdown_volume(v, _cb_volstopped_for_ums_enable) < 0) |
| 175 | LOGE("unable to shutdown volume '%s'\n", v->mount_point); |
| 176 | pthread_mutex_unlock(&v->lock); |
| 177 | return 0; |
| 178 | } |
| 179 | v = v->next; |
| 180 | } |
| 181 | |
| 182 | return -ENOENT; |
| 183 | } |
| 184 | |
| 185 | int volmgr_notify_eject(blkdev_t *dev, void (* cb) (blkdev_t *)) |
| 186 | { |
| 187 | #if DEBUG_VOLMGR |
| 188 | LOG_VOL("volmgr_notify_eject(%s)\n", dev->dev_fspath); |
| 189 | #endif |
| 190 | |
| 191 | volume_t *v; |
| 192 | |
| 193 | // XXX: Partitioning support is going to need us to stop *all* |
| 194 | // devices in this volume |
| 195 | if (!(v = volmgr_lookup_volume_by_dev(dev))) { |
| 196 | if (cb) |
| 197 | cb(dev); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | pthread_mutex_lock(&v->lock); |
| 202 | if (v->state == volstate_mounted) |
| 203 | volume_setstate(v, volstate_badremoval); |
| 204 | |
| 205 | int rc = volmgr_stop_volume(v, _cb_volume_stopped_for_eject, cb, false); |
| 206 | |
| 207 | pthread_mutex_unlock(&v->lock); |
| 208 | return rc; |
| 209 | } |
| 210 | |
| 211 | static void _cb_volume_stopped_for_eject(volume_t *v, void *arg) |
| 212 | { |
| 213 | void (* eject_cb) (blkdev_t *) = arg; |
| 214 | LOG_VOL("Volume %s has been stopped for eject\n", v->mount_point); |
| 215 | |
| 216 | eject_cb(v->dev); |
| 217 | v->dev = NULL; // Clear dev because its being ejected |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Instructs the volume manager to enable or disable USB mass storage |
| 222 | * on any volumes configured to use it. |
| 223 | */ |
| 224 | int volmgr_enable_ums(boolean enable) |
| 225 | { |
| 226 | volume_t *v = vol_root; |
| 227 | |
| 228 | while(v) { |
| 229 | if (v->ums_path) { |
| 230 | int rc; |
| 231 | |
| 232 | pthread_mutex_lock(&v->lock); |
| 233 | if (enable) { |
| 234 | // Stop the volume, and enable UMS in the callback |
| 235 | if ((rc = volmgr_shutdown_volume(v, _cb_volstopped_for_ums_enable)) < 0) |
| 236 | LOGE("unable to shutdown volume '%s'\n", v->mount_point); |
| 237 | } else { |
| 238 | // Disable UMS |
| 239 | if ((rc = ums_disable(v->ums_path)) < 0) { |
| 240 | LOGE("unable to disable ums on '%s'\n", v->mount_point); |
| 241 | pthread_mutex_unlock(&v->lock); |
| 242 | continue; |
| 243 | } |
| 244 | volume_setstate(v, volstate_unmounted); |
| 245 | |
| 246 | LOG_VOL("Kick-starting volume '%s' after UMS disable\n", v->dev->disk->dev_fspath); |
| 247 | // Start volume |
| 248 | if ((rc = _volmgr_consider_disk_and_vol(v, v->dev->disk)) < 0) { |
| 249 | LOGE("volmgr failed to consider disk '%s'\n", v->dev->disk->dev_fspath); |
| 250 | } |
| 251 | } |
| 252 | pthread_mutex_unlock(&v->lock); |
| 253 | } |
| 254 | v = v->next; |
| 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Static functions |
| 261 | */ |
| 262 | |
| 263 | // vol->lock must be held! |
| 264 | static int _volmgr_consider_disk_and_vol(volume_t *vol, blkdev_t *dev) |
| 265 | { |
| 266 | int rc = 0; |
| 267 | |
| 268 | #if DEBUG_VOLMGR |
| 269 | LOG_VOL("volmgr_consider_disk_and_vol(%s, %s):\n", vol->mount_point, dev->dev_fspath); |
| 270 | #endif |
| 271 | |
| 272 | if (vol->state != volstate_nomedia && vol->state != volstate_unmounted) { |
| 273 | LOGE("Volume manager is already handling volume '%s' (currently in state %d)\n", vol->mount_point, vol->state); |
| 274 | return -EADDRINUSE; |
| 275 | } |
| 276 | |
| 277 | volume_setstate(vol, volstate_unmounted); |
| 278 | |
| 279 | LOG_VOL("Evaluating dev '%s' for mountable filesystems for '%s'\n", dev->devpath, vol->mount_point); |
| 280 | |
| 281 | if (dev->nr_parts == 0) { |
| 282 | rc = _volmgr_start(vol, dev); |
| 283 | #if DEBUG_VOLMGR |
| 284 | LOG_VOL("_volmgr_start(%s, %s) rc = %d\n", vol->mount_point, dev->dev_fspath ,rc); |
| 285 | #endif |
| 286 | } else { |
| 287 | /* |
| 288 | * Device has multiple partitions |
| 289 | * This is where interesting partition policies could be implemented. |
| 290 | * For now just try them in sequence until one succeeds |
| 291 | */ |
| 292 | |
| 293 | rc = -ENODEV; |
| 294 | int i; |
| 295 | for (i = 0; i < dev->nr_parts; i++) { |
| 296 | blkdev_t *part = blkdev_lookup_by_devno(dev->major, (i+1)); |
| 297 | if (!part) { |
| 298 | LOGE("Error - unable to lookup partition for blkdev %d:%d\n", dev->major, (i+1)); |
| 299 | continue; |
| 300 | } |
| 301 | rc = _volmgr_start(vol, part); |
| 302 | #if DEBUG_VOLMGR |
| 303 | LOG_VOL("_volmgr_start(%s, %s) rc = %d\n", vol->mount_point, part->dev_fspath, rc); |
| 304 | #endif |
| 305 | if (!rc) |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | if (rc == -ENODEV) { |
| 310 | // Assert to make sure each partition had a backing blkdev |
| 311 | LOGE("Internal consistency error\n"); |
| 312 | return 0; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (rc == -ENODATA) { |
| 317 | LOGE("Device %s contains no usable filesystems\n", dev->dev_fspath); |
| 318 | rc = 0; |
| 319 | } |
| 320 | |
| 321 | return rc; |
| 322 | } |
| 323 | |
| 324 | static void volmgr_reaper_thread_sighandler(int signo) |
| 325 | { |
| 326 | LOGE("volmgr reaper thread got signal %d\n", signo); |
| 327 | } |
| 328 | |
| 329 | static void __reaper_cleanup(void *arg) |
| 330 | { |
| 331 | volume_t *vol = (volume_t *) arg; |
| 332 | |
| 333 | LOG_VOL("__reaper_cleanup(%s):\n", vol->mount_point); |
| 334 | |
| 335 | vol->worker_running = false; |
| 336 | |
| 337 | // Wake up anyone that was waiting on this thread |
| 338 | pthread_mutex_unlock(&vol->worker_sem); |
| 339 | |
| 340 | // Unlock the volume |
| 341 | pthread_mutex_unlock(&vol->lock); |
| 342 | } |
| 343 | |
| 344 | static void *volmgr_reaper_thread(void *arg) |
| 345 | { |
| 346 | volume_t *vol = (volume_t *) arg; |
| 347 | |
| 348 | pthread_cleanup_push(__reaper_cleanup, arg); |
| 349 | pthread_mutex_lock(&vol->lock); |
| 350 | |
| 351 | vol->worker_running = true; |
| 352 | vol->worker_pid = getpid(); |
| 353 | |
| 354 | struct sigaction actions; |
| 355 | |
| 356 | memset(&actions, 0, sizeof(actions)); |
| 357 | sigemptyset(&actions.sa_mask); |
| 358 | actions.sa_flags = 0; |
| 359 | actions.sa_handler = volmgr_reaper_thread_sighandler; |
| 360 | sigaction(SIGUSR1, &actions, NULL); |
| 361 | |
| 362 | LOG_VOL("Worker thread pid %d reaping %s\n", getpid(), vol->mount_point); |
| 363 | |
| 364 | boolean send_sig_kill = false; |
| 365 | int i, rc; |
| 366 | |
| 367 | for (i = 0; i < 10; i++) { |
| 368 | rc = umount(vol->mount_point); |
| 369 | LOG_VOL("volmngr reaper umount(%s) attempt %d rc = %d\n", |
| 370 | vol->mount_point, i + 1, rc); |
| 371 | if (!rc) |
| 372 | break; |
| 373 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
| 374 | rc = 0; |
| 375 | break; |
| 376 | } |
| 377 | KillProcessesWithOpenFiles(vol->mount_point, send_sig_kill, NULL, 0); |
| 378 | sleep(1); |
| 379 | if (!send_sig_kill) |
| 380 | send_sig_kill = true; |
| 381 | } |
| 382 | |
| 383 | if (!rc) { |
| 384 | LOG_VOL("Reaper sucessfully unmounted %s\n", vol->mount_point); |
| 385 | volume_setstate(vol, volstate_unmounted); |
| 386 | } else { |
| 387 | LOGE("Unable to unmount!! (%d)\n", rc); |
| 388 | } |
| 389 | |
| 390 | out: |
| 391 | pthread_cleanup_pop(1); |
| 392 | pthread_exit(NULL); |
| 393 | return NULL; |
| 394 | } |
| 395 | |
| 396 | static void volmgr_uncage_reaper(volume_t *vol) |
| 397 | { |
| 398 | if (vol->worker_running) { |
| 399 | LOGE("Worker thread is currently running.. waiting..\n"); |
| 400 | pthread_mutex_lock(&vol->worker_sem); |
| 401 | LOG_VOL("Worker thread now available\n"); |
| 402 | } |
| 403 | |
| 404 | vol->worker_args.fs = NULL; |
| 405 | vol->worker_args.dev = NULL; |
| 406 | |
| 407 | pthread_attr_t attr; |
| 408 | pthread_attr_init(&attr); |
| 409 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 410 | |
| 411 | pthread_create(&vol->worker_thread, &attr, volmgr_reaper_thread, vol); |
| 412 | } |
| 413 | |
| 414 | static int volmgr_stop_volume(volume_t *v, void (*cb) (volume_t *, void *), void *arg, boolean emit_statechange) |
| 415 | { |
| 416 | int i, rc; |
| 417 | |
| 418 | if (v->state == volstate_mounted || v->state == volstate_badremoval) { |
| 419 | // Try to unmount right away (5 retries) |
| 420 | for (i = 0; i < 5; i++) { |
| 421 | rc = umount(v->mount_point); |
| 422 | LOG_VOL("volmngr quick stop umount(%s) attempt %d rc = %d\n", |
| 423 | v->mount_point, i + 1, rc); |
| 424 | if (!rc) |
| 425 | break; |
| 426 | if (rc && (errno == EINVAL || errno == ENOENT)) { |
| 427 | rc = 0; |
| 428 | break; |
| 429 | } |
| 430 | sched_yield(); |
| 431 | } |
| 432 | |
| 433 | if (!rc) { |
| 434 | LOG_VOL("volmgr_stop_volume(%s): Volume unmounted sucessfully\n", |
| 435 | v->mount_point); |
| 436 | if (emit_statechange) |
| 437 | volume_setstate(v, volstate_unmounted); |
| 438 | goto out_cb_immed; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Since the volume is still in use, dispatch the stopping to |
| 443 | * a thread |
| 444 | */ |
| 445 | LOG_VOL("Volume %s is busy (%d) - uncaging the reaper\n", v->mount_point, rc); |
| 446 | volmgr_uncage_reaper(v); |
| 447 | return -EINPROGRESS; |
| 448 | } else if (v->state == volstate_checking) { |
| 449 | volume_setstate(v, volstate_unmounted); |
| 450 | if (v->worker_running) { |
| 451 | LOG_VOL("Cancelling worker thread\n"); |
| 452 | pthread_kill(v->worker_thread, SIGUSR1); |
| 453 | } else |
| 454 | LOGE("Strange... we were in checking state but worker thread wasn't running..\n"); |
| 455 | goto out_cb_immed; |
| 456 | } |
| 457 | |
| 458 | LOGE("volmgr: nothing to do to stop vol '%s' (in state %d)\n", |
| 459 | v->mount_point, v->state); |
| 460 | out_cb_immed: |
| 461 | if (cb) |
| 462 | cb(v, arg); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | /* |
| 468 | * Gracefully stop a volume |
| 469 | */ |
| 470 | static int volmgr_shutdown_volume(volume_t *v, void (* cb) (volume_t *)) |
| 471 | { |
| 472 | return volmgr_stop_volume(v, NULL, cb, true); |
| 473 | } |
| 474 | |
| 475 | static void _cb_volume_stopped_for_shutdown(volume_t *v, void *arg) |
| 476 | { |
| 477 | void (* shutdown_cb) (volume_t *) = arg; |
| 478 | |
| 479 | LOG_VOL("Volume %s has been stopped for shutdown\n", v->mount_point); |
| 480 | shutdown_cb(v); |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * Called when a volume is sucessfully unmounted for UMS enable |
| 485 | */ |
| 486 | static void _cb_volstopped_for_ums_enable(volume_t *v) |
| 487 | { |
| 488 | int rc; |
| 489 | |
| 490 | if ((rc = ums_enable(v->dev->dev_fspath, v->ums_path)) < 0) { |
| 491 | LOGE("Error enabling ums (%d)\n", rc); |
| 492 | return; |
| 493 | } |
| 494 | volume_setstate(v, volstate_ums); |
| 495 | } |
| 496 | |
| 497 | static int volmgr_readconfig(char *cfg_path) |
| 498 | { |
| 499 | cnode *root = config_node("", ""); |
| 500 | cnode *node; |
| 501 | |
| 502 | config_load_file(root, cfg_path); |
| 503 | node = root->first_child; |
| 504 | |
| 505 | while (node) { |
| 506 | if (!strcmp(node->name, "volume")) |
| 507 | volmgr_config_volume(node); |
| 508 | else |
| 509 | LOGE("Skipping unknown configuration node '%s'\n", node->name); |
| 510 | node = node->next; |
| 511 | } |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int volmgr_config_volume(cnode *node) |
| 516 | { |
| 517 | volume_t *new; |
| 518 | int rc = 0; |
| 519 | |
| 520 | if (!(new = malloc(sizeof(volume_t)))) |
| 521 | return -ENOMEM; |
| 522 | memset(new, 0, sizeof(volume_t)); |
| 523 | |
| 524 | new->state = volstate_nomedia; |
| 525 | pthread_mutex_init(&new->lock, NULL); |
| 526 | pthread_mutex_init(&new->worker_sem, NULL); |
| 527 | |
| 528 | cnode *child = node->first_child; |
| 529 | |
| 530 | while (child) { |
| 531 | if (!strcmp(child->name, "media_path")) |
| 532 | new->media_path = strdup(child->value); |
| 533 | else if (!strcmp(child->name, "media_type")) { |
| 534 | if (!strcmp(child->value, "mmc")) |
| 535 | new->media_type = media_mmc; |
| 536 | else { |
| 537 | LOGE("Invalid media type '%s'\n", child->value); |
| 538 | rc = -EINVAL; |
| 539 | goto out_free; |
| 540 | } |
| 541 | } else if (!strcmp(child->name, "mount_point")) |
| 542 | new->mount_point = strdup(child->value); |
| 543 | else if (!strcmp(child->name, "ums_path")) |
| 544 | new->ums_path = strdup(child->value); |
| 545 | else |
| 546 | LOGE("Ignoring unknown config entry '%s'\n", child->name); |
| 547 | child = child->next; |
| 548 | } |
| 549 | |
| 550 | if (!new->media_path || !new->mount_point || new->media_type == media_unknown) { |
| 551 | LOGE("Required configuration parameter missing for volume\n"); |
| 552 | rc = -EINVAL; |
| 553 | goto out_free; |
| 554 | } |
| 555 | |
| 556 | if (!vol_root) |
| 557 | vol_root = new; |
| 558 | else { |
| 559 | volume_t *scan = vol_root; |
| 560 | while (scan->next) |
| 561 | scan = scan->next; |
| 562 | scan->next = new; |
| 563 | } |
| 564 | |
| 565 | return rc; |
| 566 | |
| 567 | out_free: |
| 568 | if (new->media_path) |
| 569 | free(new->media_path); |
| 570 | if (new->mount_point) |
| 571 | free(new->mount_point); |
| 572 | if (new->ums_path) |
| 573 | free(new->ums_path); |
| 574 | return rc; |
| 575 | } |
| 576 | |
| 577 | static volume_t *volmgr_lookup_volume_by_dev(blkdev_t *dev) |
| 578 | { |
| 579 | volume_t *scan = vol_root; |
| 580 | while(scan) { |
| 581 | if (scan->dev == dev) |
| 582 | return scan; |
| 583 | scan = scan->next; |
| 584 | } |
| 585 | return NULL; |
| 586 | } |
| 587 | |
| 588 | static volume_t *volmgr_lookup_volume_by_mediapath(char *media_path, boolean fuzzy) |
| 589 | { |
| 590 | volume_t *scan = vol_root; |
| 591 | volume_t *res = NULL; |
| 592 | |
| 593 | while (scan) { |
| 594 | if (fuzzy) { |
| 595 | if (!strncmp(media_path, scan->media_path, strlen(scan->media_path))) { |
| 596 | if (!res) |
| 597 | res = scan; |
| 598 | else { |
| 599 | LOGE("Warning - multiple matching volumes for media '%s' - using first\n", media_path); |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | } else if (!strcmp(media_path, scan->media_path)) |
| 604 | return scan; |
| 605 | |
| 606 | scan = scan->next; |
| 607 | } |
| 608 | return res; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Attempt to bring a volume online |
| 613 | * Returns: 0 on success, errno on failure, with the following exceptions: |
| 614 | * - ENODATA - Unsupported filesystem type / blank |
| 615 | * vol->lock MUST be held! |
| 616 | */ |
| 617 | static int _volmgr_start(volume_t *vol, blkdev_t *dev) |
| 618 | { |
| 619 | struct volmgr_fstable_entry *fs; |
| 620 | int rc = ENODATA; |
| 621 | |
| 622 | #if DEBUG_VOLMGR |
| 623 | LOG_VOL("_volmgr_start(%s, %s):\n", vol->mount_point, dev->dev_fspath); |
| 624 | #endif |
| 625 | |
| 626 | for (fs = fs_table; fs->name; fs++) { |
| 627 | if (!fs->identify_fn(dev)) |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | if (!fs) { |
| 632 | LOGE("No supported filesystems on %s\n", dev->dev_fspath); |
| 633 | volume_setstate(vol, volstate_nofs); |
| 634 | return -ENODATA; |
| 635 | } |
| 636 | |
| 637 | return volmgr_start_fs(fs, vol, dev); |
| 638 | } |
| 639 | |
| 640 | // vol->lock MUST be held! |
| 641 | static int volmgr_start_fs(struct volmgr_fstable_entry *fs, volume_t *vol, blkdev_t *dev) |
| 642 | { |
| 643 | /* |
| 644 | * Spawn a thread to do the actual checking / mounting in |
| 645 | */ |
| 646 | |
| 647 | if (vol->worker_running) { |
| 648 | LOGE("Worker thread is currently running.. waiting..\n"); |
| 649 | pthread_mutex_lock(&vol->worker_sem); |
| 650 | LOG_VOL("Worker thread now available\n"); |
| 651 | } |
| 652 | |
| 653 | vol->dev = dev; |
| 654 | |
| 655 | vol->worker_args.fs = fs; |
| 656 | vol->worker_args.dev = dev; |
| 657 | |
| 658 | pthread_attr_t attr; |
| 659 | pthread_attr_init(&attr); |
| 660 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 661 | |
| 662 | pthread_create(&vol->worker_thread, &attr, volmgr_start_fs_thread, vol); |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static void __start_fs_thread_lock_cleanup(void *arg) |
| 668 | { |
| 669 | volume_t *vol = (volume_t *) arg; |
| 670 | |
| 671 | LOG_VOL("__start_fs_thread_lock_cleanup(%s):\n", vol->mount_point); |
| 672 | |
| 673 | vol->worker_running = false; |
| 674 | |
| 675 | // Wake up anyone that was waiting on this thread |
| 676 | pthread_mutex_unlock(&vol->worker_sem); |
| 677 | |
| 678 | // Unlock the volume |
| 679 | pthread_mutex_unlock(&vol->lock); |
| 680 | } |
| 681 | |
| 682 | static void *volmgr_start_fs_thread(void *arg) |
| 683 | { |
| 684 | volume_t *vol = (volume_t *) arg; |
| 685 | |
| 686 | pthread_cleanup_push(__start_fs_thread_lock_cleanup, arg); |
| 687 | pthread_mutex_lock(&vol->lock); |
| 688 | |
| 689 | vol->worker_running = true; |
| 690 | vol->worker_pid = getpid(); |
| 691 | |
| 692 | struct sigaction actions; |
| 693 | |
| 694 | memset(&actions, 0, sizeof(actions)); |
| 695 | sigemptyset(&actions.sa_mask); |
| 696 | actions.sa_flags = 0; |
| 697 | actions.sa_handler = volmgr_start_fs_thread_sighandler; |
| 698 | sigaction(SIGUSR1, &actions, NULL); |
| 699 | |
| 700 | struct volmgr_fstable_entry *fs = vol->worker_args.fs; |
| 701 | blkdev_t *dev = vol->worker_args.dev; |
| 702 | int rc; |
| 703 | |
| 704 | LOG_VOL("Worker thread pid %d starting %s fs %s on %s\n", getpid(), fs->name, dev->dev_fspath, vol->mount_point); |
| 705 | |
| 706 | if (fs->check_fn) { |
| 707 | LOG_VOL("Starting %s filesystem check on %s\n", fs->name, dev->dev_fspath); |
| 708 | volume_setstate(vol, volstate_checking); |
| 709 | pthread_mutex_unlock(&vol->lock); |
| 710 | rc = fs->check_fn(dev); |
| 711 | pthread_mutex_lock(&vol->lock); |
| 712 | if (vol->state != volstate_checking) { |
| 713 | LOG_VOL("filesystem check aborted\n"); |
| 714 | goto out; |
| 715 | } |
| 716 | |
| 717 | if (rc < 0) { |
| 718 | LOG_VOL("%s filesystem check failed on %s\n", fs->name, dev->dev_fspath); |
| 719 | goto out_unmountable; |
| 720 | } |
| 721 | LOG_VOL("%s filesystem check of %s OK\n", fs->name, dev->dev_fspath); |
| 722 | } |
| 723 | |
| 724 | rc = fs->mount_fn(dev, vol); |
| 725 | if (!rc) { |
| 726 | LOG_VOL("Sucessfully mounted %s filesystem %s on %s\n", fs->name, dev->devpath, vol->mount_point); |
| 727 | volume_setstate(vol, volstate_mounted); |
| 728 | goto out; |
| 729 | } |
| 730 | |
| 731 | LOGE("%s filesystem mount of %s failed (%d)\n", fs->name, dev->devpath, rc); |
| 732 | |
| 733 | out_unmountable: |
| 734 | volume_setstate(vol, volstate_damaged); |
| 735 | out: |
| 736 | pthread_cleanup_pop(1); |
| 737 | pthread_exit(NULL); |
| 738 | return NULL; |
| 739 | } |
| 740 | |
| 741 | static void volmgr_start_fs_thread_sighandler(int signo) |
| 742 | { |
| 743 | LOGE("volmgr thread got signal %d\n", signo); |
| 744 | } |
| 745 | |
| 746 | static void volume_setstate(volume_t *vol, volume_state_t state) |
| 747 | { |
| 748 | LOG_VOL("Volume %s state change from %d -> %d\n", vol->mount_point, vol->state, state); |
| 749 | |
| 750 | vol->state = state; |
| 751 | |
| 752 | char *prop_val = conv_volstate_to_propstr(vol->state); |
| 753 | |
| 754 | property_set(PROP_EXTERNAL_STORAGE_STATE, prop_val); |
| 755 | volume_send_state(vol); |
| 756 | } |
| 757 | |
| 758 | static int volume_send_state(volume_t *vol) |
| 759 | { |
| 760 | char *event = conv_volstate_to_eventstr(vol->state); |
| 761 | |
| 762 | return send_msg_with_data(event, vol->mount_point); |
| 763 | } |
| 764 | |
| 765 | static char *conv_volstate_to_eventstr(volume_state_t state) |
| 766 | { |
| 767 | int i; |
| 768 | |
| 769 | for (i = 0; volume_state_strings[i].event != NULL; i++) { |
| 770 | if (volume_state_strings[i].state == state) |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | if (!volume_state_strings[i].event) |
| 775 | LOGE("conv_volstate_to_eventstr(%d): Invalid state\n", state); |
| 776 | return volume_state_strings[i].event; |
| 777 | } |
| 778 | |
| 779 | static char *conv_volstate_to_propstr(volume_state_t state) |
| 780 | { |
| 781 | int i; |
| 782 | |
| 783 | for (i = 0; volume_state_strings[i].event != NULL; i++) { |
| 784 | if (volume_state_strings[i].state == state) |
| 785 | break; |
| 786 | } |
| 787 | |
| 788 | if (!volume_state_strings[i].event) |
| 789 | LOGE("conv_volstate_to_propval(%d): Invalid state\n", state); |
| 790 | return volume_state_strings[i].property_val; |
| 791 | } |
| 792 | |