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