Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Implements root device discovery via sysfs with optional bells and whistles. |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 8 | #include "rootdev.h" |
| 9 | |
| 10 | #include <ctype.h> |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 11 | #include <dirent.h> |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 12 | #include <err.h> |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdbool.h> |
| 16 | #include <stddef.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 19 | #include <string.h> |
Xiaochu Liu | b9b8f1d | 2017-04-24 13:32:06 -0700 | [diff] [blame] | 20 | #include <sys/ioctl.h> |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 21 | #include <sys/stat.h> |
Yunlian Jiang | 8dc336a | 2018-07-18 20:57:29 -0700 | [diff] [blame] | 22 | #include <sys/sysmacros.h> |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 23 | #include <sys/types.h> |
Xiaochu Liu | b9b8f1d | 2017-04-24 13:32:06 -0700 | [diff] [blame] | 24 | #include <sys/vfs.h> |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 25 | #include <unistd.h> |
Xiaochu Liu | b9b8f1d | 2017-04-24 13:32:06 -0700 | [diff] [blame] | 26 | #include <linux/btrfs.h> |
| 27 | #include <linux/magic.h> |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 28 | |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 29 | /* |
Paul Taysom | d4c5b8b | 2013-07-12 13:03:57 -0700 | [diff] [blame] | 30 | * Limit prevents endless looping to find slave. |
| 31 | * We currently have at most 2 levels, this allows |
| 32 | * for future growth. |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 33 | */ |
| 34 | #define MAX_SLAVE_DEPTH 8 |
| 35 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 36 | static const char *kDefaultSearchPath = "/sys/block"; |
| 37 | static const char *kDefaultDevPath = "/dev"; |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 38 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 39 | /* Encode the root device structuring here for Chromium OS */ |
| 40 | static const char kActiveRoot[] = "/dev/ACTIVE_ROOT"; |
| 41 | static const char kRootDev[] = "/dev/ROOT"; |
| 42 | static const char kRootA[] = "/dev/ROOT0"; |
| 43 | static const char kRootB[] = "/dev/ROOT1"; |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 44 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 45 | struct part_config { |
| 46 | const char *name; |
| 47 | int offset; |
| 48 | }; |
| 49 | |
| 50 | #define CHROMEOS_PRIMARY_PARTITION 3 |
| 51 | static const struct part_config kPrimaryPart[] = { { kRootA, 0 }, |
| 52 | { kRootDev, -3 }, |
| 53 | { kRootB, 2 } }; |
| 54 | #define CHROMEOS_SECONDARY_PARTITION 5 |
| 55 | static const struct part_config kSecondaryPart[] = { { kRootB, 0 }, |
| 56 | { kRootDev, -5 }, |
| 57 | { kRootA, -2 } }; |
| 58 | |
| 59 | /* The number of entries in a part_config so we could add RootC easily. */ |
| 60 | static const int kPartitionEntries = 3; |
| 61 | |
| 62 | /* Converts a file of %u:%u -> dev_t. */ |
| 63 | static dev_t devt_from_file(const char *file) { |
| 64 | char candidate[10]; /* TODO(wad) system-provided constant? */ |
| 65 | ssize_t bytes = 0; |
Chris Masone | 15141a9 | 2013-08-19 18:52:52 -0700 | [diff] [blame] | 66 | unsigned int major_num = 0; |
| 67 | unsigned int minor_num = 0; |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 68 | dev_t dev = 0; |
| 69 | int fd = -1; |
| 70 | |
| 71 | /* Never hang. Either get the data or return 0. */ |
| 72 | fd = open(file, O_NONBLOCK | O_RDONLY); |
| 73 | if (fd < 0) |
| 74 | return 0; |
| 75 | bytes = read(fd, candidate, sizeof(candidate)); |
| 76 | close(fd); |
| 77 | |
| 78 | /* 0:0 should be considered the minimum size. */ |
| 79 | if (bytes < 3) |
| 80 | return 0; |
| 81 | candidate[bytes] = 0; |
Chris Masone | 15141a9 | 2013-08-19 18:52:52 -0700 | [diff] [blame] | 82 | if (sscanf(candidate, "%u:%u", &major_num, &minor_num) == 2) { |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 83 | /* candidate's size artificially limits the size of the converted |
| 84 | * %u to safely convert to a signed int. */ |
Chris Masone | 15141a9 | 2013-08-19 18:52:52 -0700 | [diff] [blame] | 85 | dev = makedev(major_num, minor_num); |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 86 | } |
| 87 | return dev; |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 90 | /* Walks sysfs and recurses into any directory/link that represents |
| 91 | * a block device to find sub-devices (partitions) for dev. |
| 92 | * If dev == 0, the name fo the first device in the directory will be returned. |
| 93 | * Returns the device's name in "name" */ |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 94 | static int match_sysfs_device(char *name, size_t name_len, |
Bryan Freed | f8e5f9f | 2011-11-11 13:05:30 -0800 | [diff] [blame] | 95 | const char *basedir, dev_t *dev, int depth) { |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 96 | int found = -1; |
| 97 | size_t basedir_len; |
| 98 | DIR *dirp = NULL; |
| 99 | struct dirent *entry = NULL; |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 100 | char *working_path = NULL; |
| 101 | long working_path_size = 0; |
| 102 | |
| 103 | if (!name || !name_len || !basedir || !dev) { |
| 104 | warnx("match_sysfs_device: invalid arguments supplied"); |
| 105 | return -1; |
| 106 | } |
| 107 | basedir_len = strlen(basedir); |
| 108 | if (!basedir_len) { |
| 109 | warnx("match_sysfs_device: basedir must not be empty"); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | errno = 0; |
| 114 | dirp = opendir(basedir); |
| 115 | if (!dirp) { |
| 116 | /* Don't complain if the directory doesn't exist. */ |
| 117 | if (errno != ENOENT) |
| 118 | warn("match_sysfs_device:opendir(%s)", basedir); |
| 119 | return found; |
| 120 | } |
| 121 | |
| 122 | /* Grab a platform appropriate path to work with. |
| 123 | * Ideally, this won't vary under sys/block. */ |
| 124 | working_path_size = pathconf(basedir, _PC_NAME_MAX) + 1; |
| 125 | /* Fallback to PATH_MAX on any pathconf error. */ |
| 126 | if (working_path_size < 0) |
| 127 | working_path_size = PATH_MAX; |
| 128 | |
| 129 | working_path = malloc(working_path_size); |
| 130 | if (!working_path) { |
| 131 | warn("malloc(dirent)"); |
| 132 | closedir(dirp); |
| 133 | return found; |
| 134 | } |
| 135 | |
| 136 | /* Allocate a properly sized entry. */ |
| 137 | entry = malloc(offsetof(struct dirent, d_name) + working_path_size); |
| 138 | if (!entry) { |
| 139 | warn("malloc(dirent)"); |
| 140 | free(working_path); |
| 141 | closedir(dirp); |
| 142 | return found; |
| 143 | } |
| 144 | |
Yunlian Jiang | 182cfd3 | 2018-07-02 10:47:09 -0700 | [diff] [blame] | 145 | while ((entry = readdir(dirp))) { |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 146 | size_t candidate_len = strlen(entry->d_name); |
| 147 | size_t path_len = 0; |
| 148 | dev_t found_devt = 0; |
| 149 | /* Ignore the usual */ |
| 150 | if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) |
| 151 | continue; |
| 152 | /* TODO(wad) determine how to best bubble up this case. */ |
| 153 | if (candidate_len > name_len) |
| 154 | continue; |
| 155 | /* Only traverse directories or symlinks (to directories ideally) */ |
| 156 | switch (entry->d_type) { |
| 157 | case DT_UNKNOWN: |
| 158 | case DT_DIR: |
| 159 | case DT_LNK: |
| 160 | break; |
| 161 | default: |
| 162 | continue; |
| 163 | } |
| 164 | /* Determine path to block device number */ |
| 165 | path_len = snprintf(working_path, working_path_size, "%s/%s/dev", |
| 166 | basedir, entry->d_name); |
| 167 | /* Ignore if truncation occurs. */ |
| 168 | if (path_len != candidate_len + basedir_len + 5) |
| 169 | continue; |
| 170 | |
| 171 | found_devt = devt_from_file(working_path); |
| 172 | /* *dev == 0 is a wildcard. */ |
| 173 | if (!*dev || found_devt == *dev) { |
| 174 | snprintf(name, name_len, "%s", entry->d_name); |
| 175 | *dev = found_devt; |
| 176 | found = 1; |
| 177 | break; |
| 178 | } |
| 179 | |
Bryan Freed | f8e5f9f | 2011-11-11 13:05:30 -0800 | [diff] [blame] | 180 | /* Prevent infinite recursion on symlink loops by limiting depth. */ |
| 181 | if (depth > 5) |
| 182 | break; |
| 183 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 184 | /* Recurse one level for devices that may have a matching partition. */ |
| 185 | if (major(found_devt) == major(*dev) && minor(*dev) > minor(found_devt)) { |
| 186 | sprintf(working_path, "%s/%s", basedir, entry->d_name); |
Bryan Freed | f8e5f9f | 2011-11-11 13:05:30 -0800 | [diff] [blame] | 187 | found = match_sysfs_device(name, name_len, working_path, dev, depth + 1); |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 188 | if (found > 0) |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | free(working_path); |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 194 | closedir(dirp); |
| 195 | return found; |
Kobi Cohen-Arazi | afb2fb5 | 2010-07-26 12:48:36 -0600 | [diff] [blame] | 196 | } |
| 197 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 198 | const char *rootdev_get_partition(const char *dst, size_t len) { |
| 199 | const char *end = dst + strnlen(dst, len); |
| 200 | const char *part = end - 1; |
| 201 | if (!len) |
| 202 | return NULL; |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 203 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 204 | if (!isdigit(*part--)) |
| 205 | return NULL; |
Kobi Cohen-Arazi | afb2fb5 | 2010-07-26 12:48:36 -0600 | [diff] [blame] | 206 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 207 | while (part > dst && isdigit(*part)) part--; |
| 208 | part++; |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 209 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 210 | if (part >= end) |
| 211 | return NULL; |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 212 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 213 | return part; |
| 214 | } |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 215 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 216 | void rootdev_strip_partition(char *dst, size_t len) { |
| 217 | char *part = (char *)rootdev_get_partition(dst, len); |
| 218 | if (!part) |
| 219 | return; |
Benjamin Gordon | fc57068 | 2017-07-25 14:15:43 -0600 | [diff] [blame] | 220 | |
| 221 | /* For devices matching dm-NN, the digits are not a partition. */ |
| 222 | if (*(part - 1) == '-') |
| 223 | return; |
| 224 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 225 | /* For devices that end with a digit, the kernel uses a 'p' |
Benjamin Gordon | fc57068 | 2017-07-25 14:15:43 -0600 | [diff] [blame] | 226 | * as a separator. E.g., mmcblk1p2 and loop0p1, but not loop0. */ |
| 227 | if (*(part - 1) == 'p') { |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 228 | part--; |
Benjamin Gordon | fc57068 | 2017-07-25 14:15:43 -0600 | [diff] [blame] | 229 | if (strncmp(dst, "loop", 4) == 0 && dst + 3 == part) |
| 230 | return; |
| 231 | } |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 232 | *part = '\0'; |
| 233 | } |
Kobi Cohen-Arazi | afb2fb5 | 2010-07-26 12:48:36 -0600 | [diff] [blame] | 234 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 235 | int rootdev_symlink_active(const char *path) { |
| 236 | int ret = 0; |
| 237 | /* Don't overwrite an existing link. */ |
| 238 | errno = 0; |
| 239 | if ((symlink(path, kActiveRoot)) && errno != EEXIST) { |
| 240 | warn("failed to symlink %s -> %s", kActiveRoot, path); |
| 241 | ret = -1; |
| 242 | } |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | int rootdev_get_device(char *dst, size_t size, dev_t dev, |
| 247 | const char *search) { |
| 248 | struct stat active_root_statbuf; |
| 249 | |
| 250 | if (search == NULL) |
| 251 | search = kDefaultSearchPath; |
| 252 | |
| 253 | /* Check if the -s symlink exists. */ |
| 254 | if ((stat(kActiveRoot, &active_root_statbuf) == 0) && |
| 255 | active_root_statbuf.st_rdev == dev) { |
| 256 | /* Note, if the link is not fully qualified, this won't be |
| 257 | * either. */ |
| 258 | ssize_t len = readlink(kActiveRoot, dst, PATH_MAX); |
| 259 | if (len > 0) { |
| 260 | dst[len] = 0; |
| 261 | return 0; |
| 262 | } |
| 263 | /* If readlink fails or is empty, fall through */ |
| 264 | } |
| 265 | |
| 266 | snprintf(dst, size, "%s", search); |
Bryan Freed | f8e5f9f | 2011-11-11 13:05:30 -0800 | [diff] [blame] | 267 | if (match_sysfs_device(dst, size, dst, &dev, 0) <= 0) { |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 268 | fprintf (stderr, "unable to find match\n"); |
| 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 275 | /* |
| 276 | * rootdev_get_device_slave returns results in slave which |
| 277 | * may be the original device or the name of the slave. |
| 278 | * |
| 279 | * Because slave and device may point to the same data, |
| 280 | * must be careful how they are handled because slave |
| 281 | * is modified (can't use snprintf). |
| 282 | */ |
| 283 | void rootdev_get_device_slave(char *slave, size_t size, dev_t *dev, |
| 284 | const char *device, const char *search) { |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 285 | char dst[PATH_MAX]; |
| 286 | int len = 0; |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 287 | int i; |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 288 | |
| 289 | if (search == NULL) |
| 290 | search = kDefaultSearchPath; |
| 291 | |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 292 | /* |
| 293 | * With stacked device mappers, we have to chain through all the levels |
| 294 | * and find the last device. For example, verity can be stacked on bootcache |
| 295 | * that is stacked on a disk partition. |
| 296 | */ |
Paul Taysom | d4c5b8b | 2013-07-12 13:03:57 -0700 | [diff] [blame] | 297 | if (slave != device) |
| 298 | strncpy(slave, device, size); |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 299 | slave[size - 1] = '\0'; |
| 300 | for (i = 0; i < MAX_SLAVE_DEPTH; i++) { |
| 301 | len = snprintf(dst, sizeof(dst), "%s/%s/slaves", search, slave); |
| 302 | if (len != strlen(device) + strlen(search) + 8) { |
| 303 | warnx("rootdev_get_device_slave: device name too long"); |
| 304 | return; |
| 305 | } |
| 306 | *dev = 0; |
| 307 | if (match_sysfs_device(slave, size, dst, dev, 0) <= 0) { |
| 308 | return; |
| 309 | } |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 310 | } |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 311 | warnx("slave depth greater than %d at %s", i, slave); |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | int rootdev_create_devices(const char *name, dev_t dev, bool symlink) { |
| 315 | int ret = 0; |
Chris Masone | 15141a9 | 2013-08-19 18:52:52 -0700 | [diff] [blame] | 316 | unsigned int major_num = major(dev); |
| 317 | unsigned int minor_num = minor(dev); |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 318 | int i; |
| 319 | const struct part_config *config; |
| 320 | const char *part_s = rootdev_get_partition(name, strlen(name)); |
| 321 | |
| 322 | if (part_s == NULL) { |
| 323 | warnx("create_devices: unable to determine partition"); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | switch (atoi(part_s)) { |
| 328 | case CHROMEOS_PRIMARY_PARTITION: |
| 329 | config = kPrimaryPart; |
| 330 | break; |
| 331 | case CHROMEOS_SECONDARY_PARTITION: |
| 332 | config = kSecondaryPart; |
| 333 | break; |
| 334 | default: |
| 335 | warnx("create_devices: unable to determine partition: %s", |
| 336 | part_s); |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | for (i = 0; i < kPartitionEntries; ++i) { |
Chris Masone | 15141a9 | 2013-08-19 18:52:52 -0700 | [diff] [blame] | 341 | dev = makedev(major_num, minor_num + config[i].offset); |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 342 | errno = 0; |
| 343 | if (mknod(config[i].name, |
| 344 | S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, |
| 345 | dev) && errno != EEXIST) { |
| 346 | warn("failed to create %s", config[i].name); |
| 347 | return -1; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (symlink) |
| 352 | ret = rootdev_symlink_active(config[0].name); |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | int rootdev_get_path(char *path, size_t size, const char *device, |
| 357 | dev_t dev, const char *dev_path) { |
| 358 | int path_len; |
| 359 | struct stat dev_statbuf; |
| 360 | |
| 361 | if (!dev_path) |
| 362 | dev_path = kDefaultDevPath; |
| 363 | |
| 364 | if (!path || !size || !device) |
| 365 | return -1; |
| 366 | |
| 367 | path_len = snprintf(path, size, "%s/%s", dev_path, device); |
| 368 | if (path_len != strlen(dev_path) + 1 + strlen(device)) |
| 369 | return -1; |
| 370 | |
| 371 | if (stat(path, &dev_statbuf) != 0) |
| 372 | return 1; |
| 373 | |
| 374 | if (dev && dev != dev_statbuf.st_rdev) |
| 375 | return 2; |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
Xiaochu Liu | b9b8f1d | 2017-04-24 13:32:06 -0700 | [diff] [blame] | 380 | static int get_rootdev_btrfs(char *path, size_t size, const char *mount_path) { |
| 381 | const char *di_path; |
| 382 | |
| 383 | /* Open the mount point path */ |
| 384 | int fd = open(mount_path, O_RDONLY | O_CLOEXEC); |
| 385 | if (fd == -1) { |
| 386 | return -1; |
| 387 | } |
| 388 | /* Create space to hold the ioctl dev info. */ |
| 389 | struct btrfs_ioctl_dev_info_args di_args; |
| 390 | /* Since we use always use single device in chromebook rootfs, |
| 391 | * the device id for this device is always 1. */ |
| 392 | di_args.devid = 1; |
| 393 | /* Read the ioctl device info (btrfs). */ |
| 394 | if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di_args) != 0) { |
| 395 | close(fd); |
| 396 | return -1; |
| 397 | } |
| 398 | close(fd); |
| 399 | |
| 400 | di_path = (const char *)di_args.path; |
| 401 | /* Try to access the device at di_args->path to verify its existence. */ |
| 402 | if (access((char *)di_path, F_OK) != 0) { |
| 403 | return -1; |
| 404 | } |
| 405 | if (strlen(di_path) >= size) |
| 406 | return -1; |
| 407 | strcpy(path, di_path); |
| 408 | return 0; |
| 409 | } |
| 410 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 411 | int rootdev_wrapper(char *path, size_t size, |
| 412 | bool full, bool strip, |
| 413 | dev_t *dev, |
Xiaochu Liu | b9b8f1d | 2017-04-24 13:32:06 -0700 | [diff] [blame] | 414 | const char *mount_path, |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 415 | const char *search, const char *dev_path) { |
| 416 | int res = 0; |
| 417 | char devname[PATH_MAX]; |
Xiaochu Liu | b9b8f1d | 2017-04-24 13:32:06 -0700 | [diff] [blame] | 418 | struct statfs mount_statfs; |
| 419 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 420 | if (!search) |
| 421 | search = kDefaultSearchPath; |
| 422 | if (!dev_path) |
| 423 | dev_path = kDefaultDevPath; |
| 424 | if (!dev) |
| 425 | return -1; |
Xiaochu Liu | b9b8f1d | 2017-04-24 13:32:06 -0700 | [diff] [blame] | 426 | if (statfs(mount_path, &mount_statfs) == 0) { |
| 427 | if (mount_statfs.f_type == BTRFS_SUPER_MAGIC) { |
| 428 | /* BTRFS uses virtual device id which is different from actual |
| 429 | * device id. So we zero-out dev_t indicating that dev_t is not |
| 430 | * a valid device id. */ |
| 431 | *dev = 0; |
| 432 | return get_rootdev_btrfs(path, size, mount_path); |
| 433 | } |
| 434 | } |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 435 | |
| 436 | res = rootdev_get_device(devname, sizeof(devname), *dev, search); |
| 437 | if (res != 0) |
| 438 | return res; |
| 439 | |
| 440 | if (full) |
Paul Taysom | c988028 | 2012-08-30 08:33:32 -0700 | [diff] [blame] | 441 | rootdev_get_device_slave(devname, sizeof(devname), dev, devname, |
| 442 | search); |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 443 | |
| 444 | /* TODO(wad) we should really just track the block dev, partition number, and |
| 445 | * dev path. When we rewrite this, we can track all the sysfs info |
| 446 | * in the class. */ |
| 447 | if (strip) { |
| 448 | /* When we strip the partition, we don't want get_path to return non-zero |
| 449 | * because of dev mismatch. Passing in 0 tells it to not test. */ |
| 450 | *dev = 0; |
| 451 | rootdev_strip_partition(devname, size); |
| 452 | } |
| 453 | |
| 454 | res = rootdev_get_path(path, size, devname, *dev, dev_path); |
| 455 | |
| 456 | return res; |
| 457 | } |
| 458 | |
| 459 | int rootdev(char *path, size_t size, bool full, bool strip) { |
| 460 | struct stat root_statbuf; |
Mike Frysinger | 7d9288a | 2014-04-01 02:17:20 -0400 | [diff] [blame] | 461 | dev_t _root_dev, *root_dev = &_root_dev; |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 462 | |
| 463 | /* Yields the containing dev_t in st_dev. */ |
| 464 | if (stat("/", &root_statbuf) != 0) |
| 465 | return -1; |
| 466 | |
Mike Frysinger | 7d9288a | 2014-04-01 02:17:20 -0400 | [diff] [blame] | 467 | /* Some ABIs (like mips o32) are broken and the st_dev field isn't actually |
| 468 | * a dev_t. In that case, pass a pointer to a local dev_t who we took care |
| 469 | * of truncating the value into. On sane arches, gcc can optimize this to |
| 470 | * the same code, so should only be a penalty when the ABI is broken. */ |
| 471 | if (sizeof(root_statbuf.st_dev) == sizeof(*root_dev)) { |
| 472 | /* Cast is OK since we verified size here. */ |
| 473 | root_dev = (dev_t *)&root_statbuf.st_dev; |
| 474 | } else { |
| 475 | *root_dev = root_statbuf.st_dev; |
| 476 | } |
| 477 | |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 478 | return rootdev_wrapper(path, |
| 479 | size, |
| 480 | full, |
| 481 | strip, |
Mike Frysinger | 7d9288a | 2014-04-01 02:17:20 -0400 | [diff] [blame] | 482 | root_dev, |
Xiaochu Liu | b9b8f1d | 2017-04-24 13:32:06 -0700 | [diff] [blame] | 483 | "/", |
Will Drewry | 80fbc6c | 2010-08-30 10:13:34 -0500 | [diff] [blame] | 484 | NULL, /* default /sys dir */ |
| 485 | NULL); /* default /dev dir */ |
Bill Richardson | 75fcf62 | 2010-03-16 13:05:12 -0700 | [diff] [blame] | 486 | } |