The Android Open Source Project | 13f797d | 2009-02-10 15:44:07 -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 | #include <fcntl.h> |
| 25 | |
| 26 | #include <sys/mount.h> |
| 27 | |
| 28 | #include <linux/loop.h> |
| 29 | |
| 30 | #include <cutils/config_utils.h> |
| 31 | #include <cutils/properties.h> |
| 32 | |
| 33 | #include "vold.h" |
| 34 | #include "devmapper.h" |
| 35 | |
| 36 | #define DEBUG_DEVMAPPER 1 |
| 37 | |
| 38 | static int loopback_start(struct devmapping *dm) |
| 39 | { |
| 40 | int i; |
| 41 | int fd; |
| 42 | char filename[255]; |
| 43 | int rc; |
| 44 | |
| 45 | #if DEBUG_DEVMAPPER |
| 46 | LOG_VOL("loopback_start(%s):", dm->type_data.loop.loop_src); |
| 47 | #endif |
| 48 | |
| 49 | for (i = 0; i < MAX_LOOP; i++) { |
| 50 | struct loop_info info; |
| 51 | |
| 52 | sprintf(filename, "/dev/block/loop%d", i); |
| 53 | |
| 54 | if ((fd = open(filename, O_RDWR)) < 0) { |
| 55 | LOGE("Unable to open %s (%s)\n", filename, strerror(errno)); |
| 56 | return -errno; |
| 57 | } |
| 58 | |
| 59 | rc = ioctl(fd, LOOP_GET_STATUS, &info); |
| 60 | if (rc < 0 && errno == ENXIO) |
| 61 | break; |
| 62 | |
| 63 | close(fd); |
| 64 | |
| 65 | if (rc < 0) { |
| 66 | LOGE("Unable to get loop status for %s (%s)\n", filename, |
| 67 | strerror(errno)); |
| 68 | return -errno; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (i == MAX_LOOP) { |
| 73 | LOGE("Out of loop devices\n"); |
| 74 | return -ENOSPC; |
| 75 | } |
| 76 | |
| 77 | int file_fd; |
| 78 | |
| 79 | if ((file_fd = open(dm->type_data.loop.loop_src, O_RDWR)) < 0) { |
| 80 | LOGE("Unable to open %s (%s)\n", dm->type_data.loop.loop_src, |
| 81 | strerror(errno)); |
| 82 | return -errno; |
| 83 | } |
| 84 | |
| 85 | if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) { |
| 86 | LOGE("Error setting up loopback interface (%s)\n", strerror(errno)); |
| 87 | return -errno; |
| 88 | } |
| 89 | |
| 90 | dm->type_data.loop.loop_dev = strdup(filename); |
| 91 | dm->type_data.loop.loop_no = i; |
| 92 | |
| 93 | close(fd); |
| 94 | close(file_fd); |
| 95 | |
| 96 | #if DEBUG_DEVMAPPER |
| 97 | LOG_VOL("Loop setup on %s for %s\n", dm->type_data.loop.loop_dev, |
| 98 | dm->type_data.loop.loop_src); |
| 99 | #endif |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int devmapper_start(struct devmapping *dm) |
| 105 | { |
| 106 | int rc; |
| 107 | char src_blkdev_path[255]; |
| 108 | |
| 109 | #if DEBUG_DEVMAPPER |
| 110 | LOG_VOL("devmapper_start()"); |
| 111 | #endif |
| 112 | |
| 113 | if (dm->src_type == dmsrc_loopback) { |
| 114 | if ((rc = loopback_start(dm)) < 0) |
| 115 | return rc; |
| 116 | } else if (dm->src_type == dmsrc_partition) { |
| 117 | LOGE("partition maps not yet supported"); |
| 118 | return -ENOSYS; |
| 119 | } else { |
| 120 | LOGE("devmapper_start(): Unsupported source type '%d'", dm->src_type); |
| 121 | return -ENOENT; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Configure the device mapper |
| 126 | */ |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | struct devmapping *devmapper_init(char *src, char *src_type, uint32_t size_mb, |
| 132 | char *target, char *params, char *tgt_fs) |
| 133 | { |
| 134 | struct devmapping *dm; |
| 135 | |
| 136 | if (!(dm = malloc(sizeof(struct devmapping)))) { |
| 137 | LOGE("devmapper_init(): out of memory"); |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | memset(dm, 0, sizeof(struct devmapping)); |
| 142 | |
| 143 | if (!strcmp(src_type, "loopback_file")) { |
| 144 | dm->src_type = dmsrc_loopback; |
| 145 | dm->type_data.loop.loop_src = strdup(src); |
| 146 | } else if (!strncmp(src_type, "partition ", strlen("partition "))) { |
| 147 | dm->src_type = dmsrc_partition; |
| 148 | char *p = strtok(src_type, " "); |
| 149 | if (!p) { |
| 150 | LOGE("Invalid partition specifier"); |
| 151 | goto out_free; |
| 152 | } |
| 153 | dm->type_data.part.part_type = strtoul(p, NULL, 0); |
| 154 | } else { |
| 155 | LOGE("Invalid src_type defined (%s)", src_type); |
| 156 | goto out_free; |
| 157 | } |
| 158 | |
| 159 | // XXX: Validate these |
| 160 | dm->size_mb = size_mb; |
| 161 | dm->target = strdup(target); |
| 162 | dm->params = strdup(params); |
| 163 | dm->tgt_fs = strdup(tgt_fs); |
| 164 | |
| 165 | return dm; |
| 166 | out_free: |
| 167 | return NULL; |
| 168 | } |