San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <unistd.h> |
| 20 | #include <errno.h> |
| 21 | #include <string.h> |
| 22 | |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame^] | 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 26 | #define LOG_TAG "Vold" |
| 27 | |
| 28 | #include <cutils/log.h> |
| 29 | |
| 30 | #include "Loop.h" |
| 31 | |
| 32 | int Loop::lookupActive(const char *loopFile, char *buffer, size_t len) { |
| 33 | int i; |
| 34 | int fd; |
| 35 | char filename[256]; |
| 36 | |
| 37 | memset(buffer, 0, len); |
| 38 | |
| 39 | for (i = 0; i < LOOP_MAX; i++) { |
| 40 | struct loop_info li; |
| 41 | int rc; |
| 42 | |
| 43 | sprintf(filename, "/dev/block/loop%d", i); |
| 44 | |
| 45 | if ((fd = open(filename, O_RDWR)) < 0) { |
| 46 | LOGE("Unable to open %s (%s)", filename, strerror(errno)); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | rc = ioctl(fd, LOOP_GET_STATUS, &li); |
| 51 | close(fd); |
| 52 | if (rc < 0 && errno == ENXIO) { |
| 53 | continue; |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | if (rc < 0) { |
| 58 | LOGE("Unable to get loop status for %s (%s)", filename, |
| 59 | strerror(errno)); |
| 60 | return -1; |
| 61 | } |
| 62 | if (!strncmp(li.lo_name, loopFile, LO_NAME_SIZE)) { |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (i == LOOP_MAX) { |
| 68 | errno = ENOENT; |
| 69 | return -1; |
| 70 | } |
| 71 | strncpy(buffer, filename, len -1); |
| 72 | return 0; |
| 73 | } |
| 74 | |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame^] | 75 | int Loop::create(const char *loopFile, char *loopDeviceBuffer, size_t len) { |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 76 | int i; |
| 77 | int fd; |
| 78 | char filename[256]; |
| 79 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 80 | for (i = 0; i < LOOP_MAX; i++) { |
| 81 | struct loop_info li; |
| 82 | int rc; |
| 83 | |
| 84 | sprintf(filename, "/dev/block/loop%d", i); |
| 85 | |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame^] | 86 | /* |
| 87 | * The kernel starts us off with 8 loop nodes, but more |
| 88 | * are created on-demand if needed. |
| 89 | */ |
| 90 | mode_t mode = 0660 | S_IFBLK; |
| 91 | dev_t dev = (7 << 8) | i; |
| 92 | if (mknod(filename, mode, dev) < 0) { |
| 93 | if (errno != EEXIST) { |
| 94 | LOGE("Error creating loop device node (%s)", strerror(errno)); |
| 95 | return -1; |
| 96 | } |
| 97 | } |
| 98 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 99 | if ((fd = open(filename, O_RDWR)) < 0) { |
| 100 | LOGE("Unable to open %s (%s)", filename, strerror(errno)); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | rc = ioctl(fd, LOOP_GET_STATUS, &li); |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 105 | if (rc < 0 && errno == ENXIO) |
| 106 | break; |
| 107 | |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame^] | 108 | close(fd); |
| 109 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 110 | if (rc < 0) { |
| 111 | LOGE("Unable to get loop status for %s (%s)", filename, |
| 112 | strerror(errno)); |
| 113 | return -1; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (i == LOOP_MAX) { |
| 118 | LOGE("Exhausted all loop devices"); |
| 119 | errno = ENOSPC; |
| 120 | return -1; |
| 121 | } |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 122 | |
San Mehat | 8da6bcb | 2010-01-09 12:24:05 -0800 | [diff] [blame^] | 123 | strncpy(loopDeviceBuffer, filename, len -1); |
| 124 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 125 | int file_fd; |
| 126 | |
San Mehat | a19b250 | 2010-01-06 10:33:53 -0800 | [diff] [blame] | 127 | if ((file_fd = open(loopFile, O_RDWR)) < 0) { |
| 128 | LOGE("Unable to open %s (%s)", loopFile, strerror(errno)); |
| 129 | close(fd); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | if (ioctl(fd, LOOP_SET_FD, file_fd) < 0) { |
| 134 | LOGE("Error setting up loopback interface (%s)", strerror(errno)); |
| 135 | close(file_fd); |
| 136 | close(fd); |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | struct loop_info li; |
| 141 | |
| 142 | memset(&li, 0, sizeof(li)); |
| 143 | strncpy(li.lo_name, loopFile, LO_NAME_SIZE); |
| 144 | |
| 145 | if (ioctl(fd, LOOP_SET_STATUS, &li) < 0) { |
| 146 | LOGE("Error setting loopback status (%s)", strerror(errno)); |
| 147 | close(file_fd); |
| 148 | close(fd); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | close(fd); |
| 153 | close(file_fd); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int Loop::destroyByDevice(const char *loopDevice) { |
| 159 | int device_fd; |
| 160 | |
| 161 | device_fd = open(loopDevice, O_RDONLY); |
| 162 | if (device_fd < 0) { |
| 163 | LOGE("Failed to open loop (%d)", errno); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | if (ioctl(device_fd, LOOP_CLR_FD, 0) < 0) { |
| 168 | LOGE("Failed to destroy loop (%d)", errno); |
| 169 | close(device_fd); |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | close(device_fd); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int Loop::destroyByFile(const char *loopFile) { |
| 178 | errno = ENOSYS; |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | int Loop::createImageFile(const char *file, size_t sizeMb) { |
| 183 | int fd; |
| 184 | |
| 185 | LOGD("Creating ASEC image file %s (%d mb)", file, sizeMb); |
| 186 | |
| 187 | if ((fd = creat(file, 0600)) < 0) { |
| 188 | LOGE("Error creating imagefile (%s)", strerror(errno)); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | if (ftruncate(fd, (sizeMb * (1024 * 1024))) < 0) { |
| 193 | LOGE("Error truncating imagefile (%s)", strerror(errno)); |
| 194 | close(fd); |
| 195 | return -1; |
| 196 | } |
| 197 | close(fd); |
| 198 | return 0; |
| 199 | } |