blob: 3cad4f43d0f483f18b18f7f815388128574a0036 [file] [log] [blame]
Paul Taysomc9880282012-08-30 08:33:32 -07001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Will Drewry80fbc6c2010-08-30 10:13:34 -05002 * 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 Richardson75fcf622010-03-16 13:05:12 -07006 */
7
Will Drewry80fbc6c2010-08-30 10:13:34 -05008#include "rootdev.h"
9
10#include <ctype.h>
Bill Richardson75fcf622010-03-16 13:05:12 -070011#include <dirent.h>
Will Drewry80fbc6c2010-08-30 10:13:34 -050012#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 Richardson75fcf622010-03-16 13:05:12 -070019#include <string.h>
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -070020#include <sys/ioctl.h>
Will Drewry80fbc6c2010-08-30 10:13:34 -050021#include <sys/stat.h>
22#include <sys/types.h>
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -070023#include <sys/vfs.h>
Will Drewry80fbc6c2010-08-30 10:13:34 -050024#include <unistd.h>
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -070025#include <linux/btrfs.h>
26#include <linux/magic.h>
Bill Richardson75fcf622010-03-16 13:05:12 -070027
Paul Taysomc9880282012-08-30 08:33:32 -070028/*
Paul Taysomd4c5b8b2013-07-12 13:03:57 -070029 * Limit prevents endless looping to find slave.
30 * We currently have at most 2 levels, this allows
31 * for future growth.
Paul Taysomc9880282012-08-30 08:33:32 -070032 */
33#define MAX_SLAVE_DEPTH 8
34
Will Drewry80fbc6c2010-08-30 10:13:34 -050035static const char *kDefaultSearchPath = "/sys/block";
36static const char *kDefaultDevPath = "/dev";
Bill Richardson75fcf622010-03-16 13:05:12 -070037
Will Drewry80fbc6c2010-08-30 10:13:34 -050038/* Encode the root device structuring here for Chromium OS */
39static const char kActiveRoot[] = "/dev/ACTIVE_ROOT";
40static const char kRootDev[] = "/dev/ROOT";
41static const char kRootA[] = "/dev/ROOT0";
42static const char kRootB[] = "/dev/ROOT1";
Bill Richardson75fcf622010-03-16 13:05:12 -070043
Will Drewry80fbc6c2010-08-30 10:13:34 -050044struct part_config {
45 const char *name;
46 int offset;
47};
48
49#define CHROMEOS_PRIMARY_PARTITION 3
50static const struct part_config kPrimaryPart[] = { { kRootA, 0 },
51 { kRootDev, -3 },
52 { kRootB, 2 } };
53#define CHROMEOS_SECONDARY_PARTITION 5
54static 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. */
59static const int kPartitionEntries = 3;
60
61/* Converts a file of %u:%u -> dev_t. */
62static dev_t devt_from_file(const char *file) {
63 char candidate[10]; /* TODO(wad) system-provided constant? */
64 ssize_t bytes = 0;
Chris Masone15141a92013-08-19 18:52:52 -070065 unsigned int major_num = 0;
66 unsigned int minor_num = 0;
Will Drewry80fbc6c2010-08-30 10:13:34 -050067 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 Masone15141a92013-08-19 18:52:52 -070081 if (sscanf(candidate, "%u:%u", &major_num, &minor_num) == 2) {
Will Drewry80fbc6c2010-08-30 10:13:34 -050082 /* candidate's size artificially limits the size of the converted
83 * %u to safely convert to a signed int. */
Chris Masone15141a92013-08-19 18:52:52 -070084 dev = makedev(major_num, minor_num);
Will Drewry80fbc6c2010-08-30 10:13:34 -050085 }
86 return dev;
Bill Richardson75fcf622010-03-16 13:05:12 -070087}
88
Paul Taysomc9880282012-08-30 08:33:32 -070089/* 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 Drewry80fbc6c2010-08-30 10:13:34 -050093static int match_sysfs_device(char *name, size_t name_len,
Bryan Freedf8e5f9f2011-11-11 13:05:30 -080094 const char *basedir, dev_t *dev, int depth) {
Will Drewry80fbc6c2010-08-30 10:13:34 -050095 int found = -1;
96 size_t basedir_len;
97 DIR *dirp = NULL;
98 struct dirent *entry = NULL;
99 struct dirent *next = NULL;
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
145 while (readdir_r(dirp, entry, &next) == 0 && next) {
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 Freedf8e5f9f2011-11-11 13:05:30 -0800180 /* Prevent infinite recursion on symlink loops by limiting depth. */
181 if (depth > 5)
182 break;
183
Will Drewry80fbc6c2010-08-30 10:13:34 -0500184 /* 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 Freedf8e5f9f2011-11-11 13:05:30 -0800187 found = match_sysfs_device(name, name_len, working_path, dev, depth + 1);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500188 if (found > 0)
189 break;
190 }
191 }
192
193 free(working_path);
194 free(entry);
195 closedir(dirp);
196 return found;
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600197}
198
Will Drewry80fbc6c2010-08-30 10:13:34 -0500199const char *rootdev_get_partition(const char *dst, size_t len) {
200 const char *end = dst + strnlen(dst, len);
201 const char *part = end - 1;
202 if (!len)
203 return NULL;
Bill Richardson75fcf622010-03-16 13:05:12 -0700204
Will Drewry80fbc6c2010-08-30 10:13:34 -0500205 if (!isdigit(*part--))
206 return NULL;
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600207
Will Drewry80fbc6c2010-08-30 10:13:34 -0500208 while (part > dst && isdigit(*part)) part--;
209 part++;
Bill Richardson75fcf622010-03-16 13:05:12 -0700210
Will Drewry80fbc6c2010-08-30 10:13:34 -0500211 if (part >= end)
212 return NULL;
Bill Richardson75fcf622010-03-16 13:05:12 -0700213
Will Drewry80fbc6c2010-08-30 10:13:34 -0500214 return part;
215}
Bill Richardson75fcf622010-03-16 13:05:12 -0700216
Will Drewry80fbc6c2010-08-30 10:13:34 -0500217void rootdev_strip_partition(char *dst, size_t len) {
218 char *part = (char *)rootdev_get_partition(dst, len);
219 if (!part)
220 return;
Benjamin Gordonfc570682017-07-25 14:15:43 -0600221
222 /* For devices matching dm-NN, the digits are not a partition. */
223 if (*(part - 1) == '-')
224 return;
225
Will Drewry80fbc6c2010-08-30 10:13:34 -0500226 /* For devices that end with a digit, the kernel uses a 'p'
Benjamin Gordonfc570682017-07-25 14:15:43 -0600227 * as a separator. E.g., mmcblk1p2 and loop0p1, but not loop0. */
228 if (*(part - 1) == 'p') {
Will Drewry80fbc6c2010-08-30 10:13:34 -0500229 part--;
Benjamin Gordonfc570682017-07-25 14:15:43 -0600230 if (strncmp(dst, "loop", 4) == 0 && dst + 3 == part)
231 return;
232 }
Will Drewry80fbc6c2010-08-30 10:13:34 -0500233 *part = '\0';
234}
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600235
Will Drewry80fbc6c2010-08-30 10:13:34 -0500236int rootdev_symlink_active(const char *path) {
237 int ret = 0;
238 /* Don't overwrite an existing link. */
239 errno = 0;
240 if ((symlink(path, kActiveRoot)) && errno != EEXIST) {
241 warn("failed to symlink %s -> %s", kActiveRoot, path);
242 ret = -1;
243 }
244 return ret;
245}
246
247int rootdev_get_device(char *dst, size_t size, dev_t dev,
248 const char *search) {
249 struct stat active_root_statbuf;
250
251 if (search == NULL)
252 search = kDefaultSearchPath;
253
254 /* Check if the -s symlink exists. */
255 if ((stat(kActiveRoot, &active_root_statbuf) == 0) &&
256 active_root_statbuf.st_rdev == dev) {
257 /* Note, if the link is not fully qualified, this won't be
258 * either. */
259 ssize_t len = readlink(kActiveRoot, dst, PATH_MAX);
260 if (len > 0) {
261 dst[len] = 0;
262 return 0;
263 }
264 /* If readlink fails or is empty, fall through */
265 }
266
267 snprintf(dst, size, "%s", search);
Bryan Freedf8e5f9f2011-11-11 13:05:30 -0800268 if (match_sysfs_device(dst, size, dst, &dev, 0) <= 0) {
Will Drewry80fbc6c2010-08-30 10:13:34 -0500269 fprintf (stderr, "unable to find match\n");
270 return 1;
271 }
272
273 return 0;
274}
275
Paul Taysomc9880282012-08-30 08:33:32 -0700276/*
277 * rootdev_get_device_slave returns results in slave which
278 * may be the original device or the name of the slave.
279 *
280 * Because slave and device may point to the same data,
281 * must be careful how they are handled because slave
282 * is modified (can't use snprintf).
283 */
284void rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
285 const char *device, const char *search) {
Will Drewry80fbc6c2010-08-30 10:13:34 -0500286 char dst[PATH_MAX];
287 int len = 0;
Paul Taysomc9880282012-08-30 08:33:32 -0700288 int i;
Will Drewry80fbc6c2010-08-30 10:13:34 -0500289
290 if (search == NULL)
291 search = kDefaultSearchPath;
292
Paul Taysomc9880282012-08-30 08:33:32 -0700293 /*
294 * With stacked device mappers, we have to chain through all the levels
295 * and find the last device. For example, verity can be stacked on bootcache
296 * that is stacked on a disk partition.
297 */
Paul Taysomd4c5b8b2013-07-12 13:03:57 -0700298 if (slave != device)
299 strncpy(slave, device, size);
Paul Taysomc9880282012-08-30 08:33:32 -0700300 slave[size - 1] = '\0';
301 for (i = 0; i < MAX_SLAVE_DEPTH; i++) {
302 len = snprintf(dst, sizeof(dst), "%s/%s/slaves", search, slave);
303 if (len != strlen(device) + strlen(search) + 8) {
304 warnx("rootdev_get_device_slave: device name too long");
305 return;
306 }
307 *dev = 0;
308 if (match_sysfs_device(slave, size, dst, dev, 0) <= 0) {
309 return;
310 }
Will Drewry80fbc6c2010-08-30 10:13:34 -0500311 }
Paul Taysomc9880282012-08-30 08:33:32 -0700312 warnx("slave depth greater than %d at %s", i, slave);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500313}
314
315int rootdev_create_devices(const char *name, dev_t dev, bool symlink) {
316 int ret = 0;
Chris Masone15141a92013-08-19 18:52:52 -0700317 unsigned int major_num = major(dev);
318 unsigned int minor_num = minor(dev);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500319 int i;
320 const struct part_config *config;
321 const char *part_s = rootdev_get_partition(name, strlen(name));
322
323 if (part_s == NULL) {
324 warnx("create_devices: unable to determine partition");
325 return -1;
326 }
327
328 switch (atoi(part_s)) {
329 case CHROMEOS_PRIMARY_PARTITION:
330 config = kPrimaryPart;
331 break;
332 case CHROMEOS_SECONDARY_PARTITION:
333 config = kSecondaryPart;
334 break;
335 default:
336 warnx("create_devices: unable to determine partition: %s",
337 part_s);
338 return -1;
339 }
340
341 for (i = 0; i < kPartitionEntries; ++i) {
Chris Masone15141a92013-08-19 18:52:52 -0700342 dev = makedev(major_num, minor_num + config[i].offset);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500343 errno = 0;
344 if (mknod(config[i].name,
345 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
346 dev) && errno != EEXIST) {
347 warn("failed to create %s", config[i].name);
348 return -1;
349 }
350 }
351
352 if (symlink)
353 ret = rootdev_symlink_active(config[0].name);
354 return ret;
355}
356
357int rootdev_get_path(char *path, size_t size, const char *device,
358 dev_t dev, const char *dev_path) {
359 int path_len;
360 struct stat dev_statbuf;
361
362 if (!dev_path)
363 dev_path = kDefaultDevPath;
364
365 if (!path || !size || !device)
366 return -1;
367
368 path_len = snprintf(path, size, "%s/%s", dev_path, device);
369 if (path_len != strlen(dev_path) + 1 + strlen(device))
370 return -1;
371
372 if (stat(path, &dev_statbuf) != 0)
373 return 1;
374
375 if (dev && dev != dev_statbuf.st_rdev)
376 return 2;
377
378 return 0;
379}
380
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -0700381static int get_rootdev_btrfs(char *path, size_t size, const char *mount_path) {
382 const char *di_path;
383
384 /* Open the mount point path */
385 int fd = open(mount_path, O_RDONLY | O_CLOEXEC);
386 if (fd == -1) {
387 return -1;
388 }
389 /* Create space to hold the ioctl dev info. */
390 struct btrfs_ioctl_dev_info_args di_args;
391 /* Since we use always use single device in chromebook rootfs,
392 * the device id for this device is always 1. */
393 di_args.devid = 1;
394 /* Read the ioctl device info (btrfs). */
395 if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di_args) != 0) {
396 close(fd);
397 return -1;
398 }
399 close(fd);
400
401 di_path = (const char *)di_args.path;
402 /* Try to access the device at di_args->path to verify its existence. */
403 if (access((char *)di_path, F_OK) != 0) {
404 return -1;
405 }
406 if (strlen(di_path) >= size)
407 return -1;
408 strcpy(path, di_path);
409 return 0;
410}
411
Will Drewry80fbc6c2010-08-30 10:13:34 -0500412int rootdev_wrapper(char *path, size_t size,
413 bool full, bool strip,
414 dev_t *dev,
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -0700415 const char *mount_path,
Will Drewry80fbc6c2010-08-30 10:13:34 -0500416 const char *search, const char *dev_path) {
417 int res = 0;
418 char devname[PATH_MAX];
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -0700419 struct statfs mount_statfs;
420
Will Drewry80fbc6c2010-08-30 10:13:34 -0500421 if (!search)
422 search = kDefaultSearchPath;
423 if (!dev_path)
424 dev_path = kDefaultDevPath;
425 if (!dev)
426 return -1;
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -0700427 if (statfs(mount_path, &mount_statfs) == 0) {
428 if (mount_statfs.f_type == BTRFS_SUPER_MAGIC) {
429 /* BTRFS uses virtual device id which is different from actual
430 * device id. So we zero-out dev_t indicating that dev_t is not
431 * a valid device id. */
432 *dev = 0;
433 return get_rootdev_btrfs(path, size, mount_path);
434 }
435 }
Will Drewry80fbc6c2010-08-30 10:13:34 -0500436
437 res = rootdev_get_device(devname, sizeof(devname), *dev, search);
438 if (res != 0)
439 return res;
440
441 if (full)
Paul Taysomc9880282012-08-30 08:33:32 -0700442 rootdev_get_device_slave(devname, sizeof(devname), dev, devname,
443 search);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500444
445 /* TODO(wad) we should really just track the block dev, partition number, and
446 * dev path. When we rewrite this, we can track all the sysfs info
447 * in the class. */
448 if (strip) {
449 /* When we strip the partition, we don't want get_path to return non-zero
450 * because of dev mismatch. Passing in 0 tells it to not test. */
451 *dev = 0;
452 rootdev_strip_partition(devname, size);
453 }
454
455 res = rootdev_get_path(path, size, devname, *dev, dev_path);
456
457 return res;
458}
459
460int rootdev(char *path, size_t size, bool full, bool strip) {
461 struct stat root_statbuf;
Mike Frysinger7d9288a2014-04-01 02:17:20 -0400462 dev_t _root_dev, *root_dev = &_root_dev;
Will Drewry80fbc6c2010-08-30 10:13:34 -0500463
464 /* Yields the containing dev_t in st_dev. */
465 if (stat("/", &root_statbuf) != 0)
466 return -1;
467
Mike Frysinger7d9288a2014-04-01 02:17:20 -0400468 /* Some ABIs (like mips o32) are broken and the st_dev field isn't actually
469 * a dev_t. In that case, pass a pointer to a local dev_t who we took care
470 * of truncating the value into. On sane arches, gcc can optimize this to
471 * the same code, so should only be a penalty when the ABI is broken. */
472 if (sizeof(root_statbuf.st_dev) == sizeof(*root_dev)) {
473 /* Cast is OK since we verified size here. */
474 root_dev = (dev_t *)&root_statbuf.st_dev;
475 } else {
476 *root_dev = root_statbuf.st_dev;
477 }
478
Will Drewry80fbc6c2010-08-30 10:13:34 -0500479 return rootdev_wrapper(path,
480 size,
481 full,
482 strip,
Mike Frysinger7d9288a2014-04-01 02:17:20 -0400483 root_dev,
Xiaochu Liub9b8f1d2017-04-24 13:32:06 -0700484 "/",
Will Drewry80fbc6c2010-08-30 10:13:34 -0500485 NULL, /* default /sys dir */
486 NULL); /* default /dev dir */
Bill Richardson75fcf622010-03-16 13:05:12 -0700487}