blob: 5ef0c3d0beaddbe7831a9b20f07778d0ce240362 [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>
Will Drewry80fbc6c2010-08-30 10:13:34 -050020#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
Bill Richardson75fcf622010-03-16 13:05:12 -070023
Paul Taysomc9880282012-08-30 08:33:32 -070024/*
25 * A depth of more than about 4 slave devices
26 * will run out of kernel stack space, so setting
27 * the serach depth to 8 covers all possible cases.
28 */
29#define MAX_SLAVE_DEPTH 8
30
Will Drewry80fbc6c2010-08-30 10:13:34 -050031static const char *kDefaultSearchPath = "/sys/block";
32static const char *kDefaultDevPath = "/dev";
Bill Richardson75fcf622010-03-16 13:05:12 -070033
Will Drewry80fbc6c2010-08-30 10:13:34 -050034/* Encode the root device structuring here for Chromium OS */
35static const char kActiveRoot[] = "/dev/ACTIVE_ROOT";
36static const char kRootDev[] = "/dev/ROOT";
37static const char kRootA[] = "/dev/ROOT0";
38static const char kRootB[] = "/dev/ROOT1";
Bill Richardson75fcf622010-03-16 13:05:12 -070039
Will Drewry80fbc6c2010-08-30 10:13:34 -050040struct part_config {
41 const char *name;
42 int offset;
43};
44
45#define CHROMEOS_PRIMARY_PARTITION 3
46static const struct part_config kPrimaryPart[] = { { kRootA, 0 },
47 { kRootDev, -3 },
48 { kRootB, 2 } };
49#define CHROMEOS_SECONDARY_PARTITION 5
50static const struct part_config kSecondaryPart[] = { { kRootB, 0 },
51 { kRootDev, -5 },
52 { kRootA, -2 } };
53
54/* The number of entries in a part_config so we could add RootC easily. */
55static const int kPartitionEntries = 3;
56
57/* Converts a file of %u:%u -> dev_t. */
58static dev_t devt_from_file(const char *file) {
59 char candidate[10]; /* TODO(wad) system-provided constant? */
60 ssize_t bytes = 0;
61 unsigned int major = 0;
62 unsigned int minor = 0;
63 dev_t dev = 0;
64 int fd = -1;
65
66 /* Never hang. Either get the data or return 0. */
67 fd = open(file, O_NONBLOCK | O_RDONLY);
68 if (fd < 0)
69 return 0;
70 bytes = read(fd, candidate, sizeof(candidate));
71 close(fd);
72
73 /* 0:0 should be considered the minimum size. */
74 if (bytes < 3)
75 return 0;
76 candidate[bytes] = 0;
77 if (sscanf(candidate, "%u:%u", &major, &minor) == 2) {
78 /* candidate's size artificially limits the size of the converted
79 * %u to safely convert to a signed int. */
80 dev = makedev(major, minor);
81 }
82 return dev;
Bill Richardson75fcf622010-03-16 13:05:12 -070083}
84
Paul Taysomc9880282012-08-30 08:33:32 -070085/* Walks sysfs and recurses into any directory/link that represents
86 * a block device to find sub-devices (partitions) for dev.
87 * If dev == 0, the name fo the first device in the directory will be returned.
88 * Returns the device's name in "name" */
Will Drewry80fbc6c2010-08-30 10:13:34 -050089static int match_sysfs_device(char *name, size_t name_len,
Bryan Freedf8e5f9f2011-11-11 13:05:30 -080090 const char *basedir, dev_t *dev, int depth) {
Will Drewry80fbc6c2010-08-30 10:13:34 -050091 int found = -1;
92 size_t basedir_len;
93 DIR *dirp = NULL;
94 struct dirent *entry = NULL;
95 struct dirent *next = NULL;
96 char *working_path = NULL;
97 long working_path_size = 0;
98
99 if (!name || !name_len || !basedir || !dev) {
100 warnx("match_sysfs_device: invalid arguments supplied");
101 return -1;
102 }
103 basedir_len = strlen(basedir);
104 if (!basedir_len) {
105 warnx("match_sysfs_device: basedir must not be empty");
106 return -1;
107 }
108
109 errno = 0;
110 dirp = opendir(basedir);
111 if (!dirp) {
112 /* Don't complain if the directory doesn't exist. */
113 if (errno != ENOENT)
114 warn("match_sysfs_device:opendir(%s)", basedir);
115 return found;
116 }
117
118 /* Grab a platform appropriate path to work with.
119 * Ideally, this won't vary under sys/block. */
120 working_path_size = pathconf(basedir, _PC_NAME_MAX) + 1;
121 /* Fallback to PATH_MAX on any pathconf error. */
122 if (working_path_size < 0)
123 working_path_size = PATH_MAX;
124
125 working_path = malloc(working_path_size);
126 if (!working_path) {
127 warn("malloc(dirent)");
128 closedir(dirp);
129 return found;
130 }
131
132 /* Allocate a properly sized entry. */
133 entry = malloc(offsetof(struct dirent, d_name) + working_path_size);
134 if (!entry) {
135 warn("malloc(dirent)");
136 free(working_path);
137 closedir(dirp);
138 return found;
139 }
140
141 while (readdir_r(dirp, entry, &next) == 0 && next) {
142 size_t candidate_len = strlen(entry->d_name);
143 size_t path_len = 0;
144 dev_t found_devt = 0;
145 /* Ignore the usual */
146 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
147 continue;
148 /* TODO(wad) determine how to best bubble up this case. */
149 if (candidate_len > name_len)
150 continue;
151 /* Only traverse directories or symlinks (to directories ideally) */
152 switch (entry->d_type) {
153 case DT_UNKNOWN:
154 case DT_DIR:
155 case DT_LNK:
156 break;
157 default:
158 continue;
159 }
160 /* Determine path to block device number */
161 path_len = snprintf(working_path, working_path_size, "%s/%s/dev",
162 basedir, entry->d_name);
163 /* Ignore if truncation occurs. */
164 if (path_len != candidate_len + basedir_len + 5)
165 continue;
166
167 found_devt = devt_from_file(working_path);
168 /* *dev == 0 is a wildcard. */
169 if (!*dev || found_devt == *dev) {
170 snprintf(name, name_len, "%s", entry->d_name);
171 *dev = found_devt;
172 found = 1;
173 break;
174 }
175
Bryan Freedf8e5f9f2011-11-11 13:05:30 -0800176 /* Prevent infinite recursion on symlink loops by limiting depth. */
177 if (depth > 5)
178 break;
179
Will Drewry80fbc6c2010-08-30 10:13:34 -0500180 /* Recurse one level for devices that may have a matching partition. */
181 if (major(found_devt) == major(*dev) && minor(*dev) > minor(found_devt)) {
182 sprintf(working_path, "%s/%s", basedir, entry->d_name);
Bryan Freedf8e5f9f2011-11-11 13:05:30 -0800183 found = match_sysfs_device(name, name_len, working_path, dev, depth + 1);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500184 if (found > 0)
185 break;
186 }
187 }
188
189 free(working_path);
190 free(entry);
191 closedir(dirp);
192 return found;
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600193}
194
Will Drewry80fbc6c2010-08-30 10:13:34 -0500195const char *rootdev_get_partition(const char *dst, size_t len) {
196 const char *end = dst + strnlen(dst, len);
197 const char *part = end - 1;
198 if (!len)
199 return NULL;
Bill Richardson75fcf622010-03-16 13:05:12 -0700200
Will Drewry80fbc6c2010-08-30 10:13:34 -0500201 if (!isdigit(*part--))
202 return NULL;
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600203
Will Drewry80fbc6c2010-08-30 10:13:34 -0500204 while (part > dst && isdigit(*part)) part--;
205 part++;
Bill Richardson75fcf622010-03-16 13:05:12 -0700206
Will Drewry80fbc6c2010-08-30 10:13:34 -0500207 if (part >= end)
208 return NULL;
Bill Richardson75fcf622010-03-16 13:05:12 -0700209
Will Drewry80fbc6c2010-08-30 10:13:34 -0500210 return part;
211}
Bill Richardson75fcf622010-03-16 13:05:12 -0700212
Will Drewry80fbc6c2010-08-30 10:13:34 -0500213void rootdev_strip_partition(char *dst, size_t len) {
214 char *part = (char *)rootdev_get_partition(dst, len);
215 if (!part)
216 return;
217 /* For devices that end with a digit, the kernel uses a 'p'
218 * as a separator. E.g., mmcblk1p2. */
219 if (*(part - 1) == 'p')
220 part--;
221 *part = '\0';
222}
Kobi Cohen-Araziafb2fb52010-07-26 12:48:36 -0600223
Will Drewry80fbc6c2010-08-30 10:13:34 -0500224int rootdev_symlink_active(const char *path) {
225 int ret = 0;
226 /* Don't overwrite an existing link. */
227 errno = 0;
228 if ((symlink(path, kActiveRoot)) && errno != EEXIST) {
229 warn("failed to symlink %s -> %s", kActiveRoot, path);
230 ret = -1;
231 }
232 return ret;
233}
234
235int rootdev_get_device(char *dst, size_t size, dev_t dev,
236 const char *search) {
237 struct stat active_root_statbuf;
238
239 if (search == NULL)
240 search = kDefaultSearchPath;
241
242 /* Check if the -s symlink exists. */
243 if ((stat(kActiveRoot, &active_root_statbuf) == 0) &&
244 active_root_statbuf.st_rdev == dev) {
245 /* Note, if the link is not fully qualified, this won't be
246 * either. */
247 ssize_t len = readlink(kActiveRoot, dst, PATH_MAX);
248 if (len > 0) {
249 dst[len] = 0;
250 return 0;
251 }
252 /* If readlink fails or is empty, fall through */
253 }
254
255 snprintf(dst, size, "%s", search);
Bryan Freedf8e5f9f2011-11-11 13:05:30 -0800256 if (match_sysfs_device(dst, size, dst, &dev, 0) <= 0) {
Will Drewry80fbc6c2010-08-30 10:13:34 -0500257 fprintf (stderr, "unable to find match\n");
258 return 1;
259 }
260
261 return 0;
262}
263
Paul Taysomc9880282012-08-30 08:33:32 -0700264/*
265 * rootdev_get_device_slave returns results in slave which
266 * may be the original device or the name of the slave.
267 *
268 * Because slave and device may point to the same data,
269 * must be careful how they are handled because slave
270 * is modified (can't use snprintf).
271 */
272void rootdev_get_device_slave(char *slave, size_t size, dev_t *dev,
273 const char *device, const char *search) {
Will Drewry80fbc6c2010-08-30 10:13:34 -0500274 char dst[PATH_MAX];
275 int len = 0;
Paul Taysomc9880282012-08-30 08:33:32 -0700276 int i;
Will Drewry80fbc6c2010-08-30 10:13:34 -0500277
278 if (search == NULL)
279 search = kDefaultSearchPath;
280
Paul Taysomc9880282012-08-30 08:33:32 -0700281 /*
282 * With stacked device mappers, we have to chain through all the levels
283 * and find the last device. For example, verity can be stacked on bootcache
284 * that is stacked on a disk partition.
285 */
286 strncpy(slave, device, size);
287 slave[size - 1] = '\0';
288 for (i = 0; i < MAX_SLAVE_DEPTH; i++) {
289 len = snprintf(dst, sizeof(dst), "%s/%s/slaves", search, slave);
290 if (len != strlen(device) + strlen(search) + 8) {
291 warnx("rootdev_get_device_slave: device name too long");
292 return;
293 }
294 *dev = 0;
295 if (match_sysfs_device(slave, size, dst, dev, 0) <= 0) {
296 return;
297 }
Will Drewry80fbc6c2010-08-30 10:13:34 -0500298 }
Paul Taysomc9880282012-08-30 08:33:32 -0700299 warnx("slave depth greater than %d at %s", i, slave);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500300}
301
302int rootdev_create_devices(const char *name, dev_t dev, bool symlink) {
303 int ret = 0;
304 unsigned int major = major(dev);
305 unsigned int minor = minor(dev);
306 int i;
307 const struct part_config *config;
308 const char *part_s = rootdev_get_partition(name, strlen(name));
309
310 if (part_s == NULL) {
311 warnx("create_devices: unable to determine partition");
312 return -1;
313 }
314
315 switch (atoi(part_s)) {
316 case CHROMEOS_PRIMARY_PARTITION:
317 config = kPrimaryPart;
318 break;
319 case CHROMEOS_SECONDARY_PARTITION:
320 config = kSecondaryPart;
321 break;
322 default:
323 warnx("create_devices: unable to determine partition: %s",
324 part_s);
325 return -1;
326 }
327
328 for (i = 0; i < kPartitionEntries; ++i) {
329 dev = makedev(major, minor + config[i].offset);
330 errno = 0;
331 if (mknod(config[i].name,
332 S_IFBLK | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
333 dev) && errno != EEXIST) {
334 warn("failed to create %s", config[i].name);
335 return -1;
336 }
337 }
338
339 if (symlink)
340 ret = rootdev_symlink_active(config[0].name);
341 return ret;
342}
343
344int rootdev_get_path(char *path, size_t size, const char *device,
345 dev_t dev, const char *dev_path) {
346 int path_len;
347 struct stat dev_statbuf;
348
349 if (!dev_path)
350 dev_path = kDefaultDevPath;
351
352 if (!path || !size || !device)
353 return -1;
354
355 path_len = snprintf(path, size, "%s/%s", dev_path, device);
356 if (path_len != strlen(dev_path) + 1 + strlen(device))
357 return -1;
358
359 if (stat(path, &dev_statbuf) != 0)
360 return 1;
361
362 if (dev && dev != dev_statbuf.st_rdev)
363 return 2;
364
365 return 0;
366}
367
368int rootdev_wrapper(char *path, size_t size,
369 bool full, bool strip,
370 dev_t *dev,
371 const char *search, const char *dev_path) {
372 int res = 0;
373 char devname[PATH_MAX];
374 if (!search)
375 search = kDefaultSearchPath;
376 if (!dev_path)
377 dev_path = kDefaultDevPath;
378 if (!dev)
379 return -1;
380
381 res = rootdev_get_device(devname, sizeof(devname), *dev, search);
382 if (res != 0)
383 return res;
384
385 if (full)
Paul Taysomc9880282012-08-30 08:33:32 -0700386 rootdev_get_device_slave(devname, sizeof(devname), dev, devname,
387 search);
Will Drewry80fbc6c2010-08-30 10:13:34 -0500388
389 /* TODO(wad) we should really just track the block dev, partition number, and
390 * dev path. When we rewrite this, we can track all the sysfs info
391 * in the class. */
392 if (strip) {
393 /* When we strip the partition, we don't want get_path to return non-zero
394 * because of dev mismatch. Passing in 0 tells it to not test. */
395 *dev = 0;
396 rootdev_strip_partition(devname, size);
397 }
398
399 res = rootdev_get_path(path, size, devname, *dev, dev_path);
400
401 return res;
402}
403
404int rootdev(char *path, size_t size, bool full, bool strip) {
405 struct stat root_statbuf;
406
407 /* Yields the containing dev_t in st_dev. */
408 if (stat("/", &root_statbuf) != 0)
409 return -1;
410
411 return rootdev_wrapper(path,
412 size,
413 full,
414 strip,
415 &root_statbuf.st_dev,
416 NULL, /* default /sys dir */
417 NULL); /* default /dev dir */
Bill Richardson75fcf622010-03-16 13:05:12 -0700418}