Line data Source code
1 : /*
2 : * Copyright (C) 2013 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #ifndef __MINGW32__ /* Only for Unix */
21 :
22 : #include "support.h"
23 :
24 : /**
25 : * Exit codes.
26 : */
27 : int exit_success = 0;
28 : int exit_failure = 1;
29 : int exit_sync_needed = 2;
30 :
31 1719437 : int open_noatime(const char* file, int flags)
32 : {
33 : #ifdef O_NOATIME
34 1719437 : int f = open(file, flags | O_NOATIME);
35 :
36 : /* only root is allowed to use O_NOATIME, in case retry without it */
37 1719500 : if (f == -1 && errno == EPERM)
38 0 : f = open(file, flags);
39 1719484 : return f;
40 : #else
41 : return open(file, flags);
42 : #endif
43 : }
44 :
45 0 : int dirent_hidden(struct dirent* dd)
46 : {
47 0 : return dd->d_name[0] == '.';
48 : }
49 :
50 0 : const char* stat_desc(struct stat* st)
51 : {
52 0 : if (S_ISREG(st->st_mode))
53 0 : return "regular";
54 0 : if (S_ISDIR(st->st_mode))
55 0 : return "directory";
56 0 : if (S_ISCHR(st->st_mode))
57 0 : return "character";
58 0 : if (S_ISBLK(st->st_mode))
59 0 : return "block-device";
60 0 : if (S_ISFIFO(st->st_mode))
61 0 : return "fifo";
62 0 : if (S_ISLNK(st->st_mode))
63 0 : return "link";
64 0 : if (S_ISLNK(st->st_mode))
65 0 : return "symbolic-link";
66 0 : if (S_ISSOCK(st->st_mode))
67 0 : return "socket";
68 0 : return "unknown";
69 : }
70 :
71 : /**
72 : * Get the device file from the device number.
73 : *
74 : * It uses /proc/self/mountinfo.
75 : *
76 : * Null devices (major==0) are resolved to the device indicated in mountinfo.
77 : */
78 : #if HAVE_LINUX_DEVICE
79 4 : static int devresolve_proc(uint64_t device, char* path, size_t path_size)
80 : {
81 : FILE* f;
82 : char match[32];
83 :
84 : /* generate the matching string */
85 4 : snprintf(match, sizeof(match), "%u:%u", major(device), minor(device));
86 :
87 4 : f = fopen("/proc/self/mountinfo", "r");
88 4 : if (!f) {
89 0 : log_tag("resolve:proc:%u:%u: failed to open /proc/self/mountinfo\n", major(device), minor(device));
90 0 : return -1;
91 : }
92 :
93 : /*
94 : * mountinfo format
95 : * 0 - mount ID
96 : * 1 - parent ID
97 : * 2 - major:minor
98 : * 3 - root
99 : * 4 - mount point
100 : * 5 - options
101 : * 6 - "-" (separator)
102 : * 7 - fs
103 : * 8 - mount source - /dev/device
104 : */
105 :
106 : while (1) {
107 : char buf[256];
108 : char* first_map[8];
109 : unsigned first_mac;
110 : char* second_map[8];
111 : unsigned second_mac;
112 : char* s;
113 : struct stat st;
114 : char* separator;
115 : char* majorminor;
116 : char* mountpoint;
117 : char* mountsource;
118 :
119 104 : s = fgets(buf, sizeof(buf), f);
120 104 : if (s == 0)
121 4 : break;
122 :
123 : /* find the separator position */
124 100 : separator = strstr(s, " - ");
125 100 : if (!separator)
126 32 : continue;
127 :
128 : /* skip the separator */
129 100 : *separator = 0;
130 100 : separator += 3;
131 :
132 : /* split the line */
133 100 : first_mac = strsplit(first_map, 8, s, " \t\r\n");
134 100 : second_mac = strsplit(second_map, 8, separator, " \t\r\n");
135 :
136 : /* if too short, it's the wrong line */
137 100 : if (first_mac < 5)
138 0 : continue;
139 100 : if (second_mac < 2)
140 0 : continue;
141 :
142 100 : majorminor = first_map[2];
143 100 : mountpoint = first_map[4];
144 100 : mountsource = second_map[1];
145 :
146 : /* accept only /dev/... mountsource */
147 : /* this excludes ZFS that has the bare label, like "tank" */
148 100 : if (strncmp(mountsource, "/dev/", 5) != 0)
149 32 : continue;
150 :
151 : /* compare major:minor from mountinfo */
152 68 : if (strcmp(majorminor, match) == 0) {
153 0 : pathcpy(path, path_size, mountsource);
154 :
155 0 : log_tag("resolve:proc:%u:%u:%s: found mountinfo device %s\n", major(device), minor(device), path, match);
156 :
157 0 : fclose(f);
158 0 : return 0;
159 : }
160 :
161 : /* get the device of the mount point */
162 : /* in Btrfs it could be different than the one in mountinfo */
163 68 : if (stat(mountpoint, &st) == 0 && st.st_dev == device) {
164 0 : pathcpy(path, path_size, mountsource);
165 :
166 0 : log_tag("resolve:proc:%u:%u:%s: found mountpoint %s\n", major(device), minor(device), path, mountpoint);
167 :
168 0 : fclose(f);
169 0 : return 0;
170 : }
171 100 : }
172 :
173 4 : log_tag("resolve:proc:%u:%u: not found\n", major(device), minor(device));
174 :
175 4 : fclose(f);
176 4 : return -1;
177 : }
178 : #endif
179 :
180 : /**
181 : * Get the device of a virtual superblock.
182 : *
183 : * This is intended to resolve the case of Btrfs filesystems that
184 : * create a virtual superblock (major==0) not backed by any low
185 : * level device.
186 : *
187 : * See:
188 : * Bug 711881 - too funny btrfs st_dev numbers
189 : * https://bugzilla.redhat.com/show_bug.cgi?id=711881
190 : */
191 : #if HAVE_LINUX_DEVICE
192 4 : static int devdereference(uint64_t device, uint64_t* new_device)
193 : {
194 : char path[PATH_MAX];
195 : struct stat st;
196 :
197 : /* use the proc interface to get the device containing the filesystem */
198 4 : if (devresolve_proc(device, path, sizeof(path)) != 0) {
199 : /* LCOV_EXCL_START */
200 : return -1;
201 : /* LCOV_EXCL_STOP */
202 : }
203 :
204 : /* check the device */
205 0 : if (stat(path, &st) != 0) {
206 : /* LCOV_EXCL_START */
207 : log_tag("dereference:%u:%u: failed to stat %s\n", major(device), minor(device), path);
208 : return -1;
209 : /* LCOV_EXCL_STOP */
210 : }
211 :
212 0 : if (major(st.st_rdev) == 0) {
213 : /* LCOV_EXCL_START */
214 : log_tag("dereference:%u:%u: still null device %s -> %u:%u\n", major(device), minor(device), path, major(st.st_rdev), minor(st.st_rdev));
215 : return -1;
216 : /* LCOV_EXCL_STOP */
217 : }
218 :
219 0 : *new_device = st.st_rdev;
220 0 : log_tag("dereference:%u:%u: found %u:%u\n", major(device), minor(device), major(st.st_rdev), minor(st.st_rdev));
221 0 : return 0;
222 : }
223 : #endif
224 :
225 : /**
226 : * Read a file extracting the specified tag TAG=VALUE format.
227 : * Return !=0 on error.
228 : */
229 : #if HAVE_LINUX_DEVICE
230 191 : static int tagread(const char* path, const char* tag, char* value, size_t value_size)
231 : {
232 : int f;
233 : int ret;
234 : int len;
235 : char buf[512];
236 : size_t tag_len;
237 : char* i;
238 : char* e;
239 :
240 191 : f = open(path, O_RDONLY);
241 191 : if (f == -1) {
242 : /* LCOV_EXCL_START */
243 : log_fatal("Failed to open '%s'.\n", path);
244 : return 0;
245 : /* LCOV_EXCL_STOP */
246 : }
247 :
248 191 : len = read(f, buf, sizeof(buf));
249 191 : if (len < 0) {
250 : /* LCOV_EXCL_START */
251 : close(f);
252 : log_fatal("Failed to read '%s'.\n", path);
253 : return -1;
254 : /* LCOV_EXCL_STOP */
255 : }
256 191 : if (len == sizeof(buf)) {
257 : /* LCOV_EXCL_START */
258 : close(f);
259 : log_fatal("Too long read '%s'.\n", path);
260 : return -1;
261 : /* LCOV_EXCL_STOP */
262 : }
263 :
264 191 : ret = close(f);
265 191 : if (ret != 0) {
266 : /* LCOV_EXCL_START */
267 : log_fatal("Failed to close '%s'.\n", path);
268 : return -1;
269 : /* LCOV_EXCL_STOP */
270 : }
271 :
272 191 : buf[len] = 0;
273 191 : tag_len = strlen(tag);
274 :
275 3324 : for (i = buf; *i; ++i) {
276 3324 : char* p = i;
277 :
278 : /* start with a space */
279 3324 : if (p != buf) {
280 3133 : if (!isspace(*p))
281 2751 : continue;
282 382 : ++p;
283 : }
284 :
285 573 : if (strncmp(p, tag, tag_len) != 0)
286 382 : continue;
287 191 : p += tag_len;
288 :
289 : /* end with a = */
290 191 : if (*p != '=')
291 0 : continue;
292 191 : ++p;
293 :
294 : /* found */
295 191 : i = p;
296 191 : break;
297 : }
298 191 : if (!*i) {
299 : /* LCOV_EXCL_START */
300 : log_fatal("Missing tag '%s' for '%s'.\n", tag, path);
301 : return -1;
302 : /* LCOV_EXCL_STOP */
303 : }
304 :
305 : /* terminate at the first space */
306 191 : e = i;
307 1020 : while (*e != 0 && !isspace(*e))
308 638 : ++e;
309 191 : *e = 0;
310 :
311 191 : if (!*i) {
312 : /* LCOV_EXCL_START */
313 : log_fatal("Empty tag '%s' for '%s'.\n", tag, path);
314 : return -1;
315 : /* LCOV_EXCL_STOP */
316 : }
317 :
318 191 : pathprint(value, value_size, "%s", i);
319 :
320 191 : return 0;
321 : }
322 : #endif
323 :
324 : /**
325 : * Get the device file from the device number.
326 : *
327 : * It uses /sys/dev/block/.../uevent.
328 : *
329 : * For null device (major==0) it fails.
330 : */
331 : #if HAVE_LINUX_DEVICE
332 191 : static int devresolve_sys(dev_t device, char* path, size_t path_size)
333 : {
334 : struct stat st;
335 : char buf[PATH_MAX];
336 :
337 : /* default device path from device number */
338 191 : pathprint(path, path_size, "/sys/dev/block/%u:%u/uevent", major(device), minor(device));
339 :
340 191 : if (tagread(path, "DEVNAME", buf, sizeof(buf)) != 0) {
341 : /* LCOV_EXCL_START */
342 : log_tag("resolve:sys:%u:%u: failed to read DEVNAME tag '%s'\n", major(device), minor(device), path);
343 : return -1;
344 : /* LCOV_EXCL_STOP */
345 : }
346 :
347 : /* set the real device path */
348 191 : pathprint(path, path_size, "/dev/%s", buf);
349 :
350 : /* check the device */
351 191 : if (stat(path, &st) != 0) {
352 : /* LCOV_EXCL_START */
353 : log_tag("resolve:sys:%u:%u: failed to stat '%s'\n", major(device), minor(device), path);
354 : return -1;
355 : /* LCOV_EXCL_STOP */
356 : }
357 191 : if (st.st_rdev != device) {
358 : /* LCOV_EXCL_START */
359 : log_tag("resolve:sys:%u:%u: unexpected device '%u:%u' for '%s'.\n", major(device), minor(device), major(st.st_rdev), minor(st.st_rdev), path);
360 : return -1;
361 : /* LCOV_EXCL_STOP */
362 : }
363 :
364 191 : log_tag("resolve:sys:%u:%u:%s: found\n", major(device), minor(device), path);
365 :
366 191 : return 0;
367 : }
368 : #endif
369 :
370 : /**
371 : * Get the device file from the device number.
372 : */
373 : #if HAVE_LINUX_DEVICE
374 191 : static int devresolve(uint64_t device, char* path, size_t path_size)
375 : {
376 191 : if (devresolve_sys(device, path, path_size) == 0)
377 191 : return 0;
378 :
379 0 : return -1;
380 : }
381 : #endif
382 :
383 : /**
384 : * Cache used by blkid.
385 : */
386 : #if HAVE_BLKID
387 : static blkid_cache cache = 0;
388 : #endif
389 :
390 : /**
391 : * Get the UUID using the /dev/disk/by-uuid/ links.
392 : * It doesn't require root permission, and the uuid are always updated.
393 : * It doesn't work with Btrfs file-systems that don't export the main UUID
394 : * in /dev/disk/by-uuid/.
395 : */
396 : #if HAVE_LINUX_DEVICE
397 5626 : static int devuuid_dev(uint64_t device, char* uuid, size_t uuid_size)
398 : {
399 : int ret;
400 : DIR* d;
401 : struct dirent* dd;
402 : struct stat st;
403 :
404 : /* scan the UUID directory searching for the device */
405 5626 : d = opendir("/dev/disk/by-uuid");
406 5626 : if (!d) {
407 0 : log_tag("uuid:by-uuidd:%u:%u: opendir(/dev/disk/by-uuid) failed, %s\n", major(device), minor(device), strerror(errno));
408 : /* directory missing?, likely we are not in Linux */
409 0 : return -1;
410 : }
411 :
412 95642 : while ((dd = readdir(d)) != 0) {
413 : /* skip "." and ".." files, UUIDs never start with '.' */
414 90016 : if (dd->d_name[0] == '.')
415 11252 : continue;
416 :
417 78764 : ret = fstatat(dirfd(d), dd->d_name, &st, 0);
418 78764 : if (ret != 0) {
419 0 : log_tag("uuid:by-uuidd:%u:%u: fstatat(%s) failed, %s\n", major(device), minor(device), dd->d_name, strerror(errno));
420 : /* generic error, ignore and continue the search */
421 0 : continue;
422 : }
423 :
424 : /* if it matches, we have the uuid */
425 78764 : if (S_ISBLK(st.st_mode) && st.st_rdev == (dev_t)device) {
426 : char buf[PATH_MAX];
427 : char path[PATH_MAX];
428 :
429 : /* resolve the link */
430 5626 : pathprint(path, sizeof(path), "/dev/disk/by-uuid/%s", dd->d_name);
431 5626 : ret = readlink(path, buf, sizeof(buf));
432 5626 : if (ret < 0 || ret == sizeof(buf)) {
433 0 : log_tag("uuid:by-uuidd:%u:%u: readlink(/dev/disk/by-uuid/%s) failed, %s\n", major(device), minor(device), dd->d_name, strerror(errno));
434 : /* generic error, ignore and continue the search */
435 0 : continue;
436 : }
437 5626 : buf[ret] = 0;
438 :
439 : /* found */
440 5626 : pathcpy(uuid, uuid_size, dd->d_name);
441 :
442 5626 : log_tag("uuid:by-uuid:%u:%u:%s: found %s\n", major(device), minor(device), uuid, buf);
443 :
444 5626 : closedir(d);
445 5626 : return 0;
446 : }
447 : }
448 :
449 0 : log_tag("uuid:by-uuidd:%u:%u: /dev/disk/by-uuid doesn't contain a matching block device\n", major(device), minor(device));
450 :
451 : /* not found */
452 0 : closedir(d);
453 0 : return -1;
454 : }
455 : #endif
456 :
457 : /**
458 : * Get the UUID using liblkid.
459 : * It uses a cache to work without root permission, resulting in UUID
460 : * not necessarily recent.
461 : * We could call blkid_probe_all() to refresh the UUID, but it would
462 : * require root permission to read the superblocks, and resulting in
463 : * all the disks spinning.
464 : */
465 : #if HAVE_BLKID
466 0 : static int devuuid_blkid(uint64_t device, char* uuid, size_t uuid_size)
467 : {
468 : char* devname;
469 : char* uuidname;
470 :
471 0 : devname = blkid_devno_to_devname(device);
472 0 : if (!devname) {
473 0 : log_tag("uuid:blkid:%u:%u: blkid_devno_to_devname() failed, %s\n", major(device), minor(device), strerror(errno));
474 : /* device mapping failed */
475 0 : return -1;
476 : }
477 :
478 0 : uuidname = blkid_get_tag_value(cache, "UUID", devname);
479 0 : if (!uuidname) {
480 0 : log_tag("uuid:blkid:%u:%u: blkid_get_tag_value(UUID,%s) failed, %s\n", major(device), minor(device), devname, strerror(errno));
481 : /* uuid mapping failed */
482 0 : free(devname);
483 0 : return -1;
484 : }
485 :
486 0 : pathcpy(uuid, uuid_size, uuidname);
487 :
488 0 : log_tag("uuid:blkid:%u:%u:%s: found %s\n", major(device), minor(device), uuid, devname);
489 :
490 0 : free(devname);
491 0 : free(uuidname);
492 0 : return 0;
493 : }
494 : #endif
495 :
496 5630 : int devuuid(uint64_t device, char* uuid, size_t uuid_size)
497 : {
498 : #if HAVE_LINUX_DEVICE
499 : /* if the major is the null device */
500 5630 : if (major(device) == 0) {
501 : /* obtain the real device */
502 4 : if (devdereference(device, &device) != 0) {
503 : /* LCOV_EXCL_START */
504 : return -1;
505 : /* LCOV_EXCL_STOP */
506 : }
507 : }
508 : #endif
509 :
510 : /* first try with the /dev/disk/by-uuid version */
511 : #if HAVE_LINUX_DEVICE
512 5626 : if (devuuid_dev(device, uuid, uuid_size) == 0)
513 5626 : return 0;
514 : #else
515 : log_tag("uuid:by-uuidd:%u:%u: by-uuid not supported\n", major(device), minor(device));
516 : #endif
517 :
518 : /* fall back to blkid for other cases */
519 : #if HAVE_BLKID
520 0 : if (devuuid_blkid(device, uuid, uuid_size) == 0)
521 0 : return 0;
522 : #else
523 : log_tag("uuid:blkid:%u:%u: blkid not supported\n", major(device), minor(device));
524 : #endif
525 :
526 0 : log_tag("uuid:notfound:%u:%u:\n", major(device), minor(device));
527 :
528 : /* not supported */
529 : (void)uuid;
530 : (void)uuid_size;
531 0 : return -1;
532 : }
533 :
534 11432 : int filephy(const char* path, uint64_t size, uint64_t* physical)
535 : {
536 : #if HAVE_LINUX_FIEMAP_H
537 : /* In Linux get the real physical address of the file */
538 : /* Note that FIEMAP doesn't require root permission */
539 : int f;
540 : struct {
541 : struct fiemap fiemap;
542 : struct fiemap_extent extent;
543 : } fm;
544 : unsigned int blknum;
545 :
546 11432 : f = open(path, O_RDONLY);
547 11432 : if (f == -1) {
548 0 : return -1;
549 : }
550 :
551 : /* first try with FIEMAP */
552 : /* if works for ext2, ext3, ext4, xfs, btrfs */
553 11432 : memset(&fm, 0, sizeof(fm));
554 11432 : fm.fiemap.fm_start = 0;
555 11432 : fm.fiemap.fm_length = ~0ULL;
556 11432 : fm.fiemap.fm_flags = FIEMAP_FLAG_SYNC; /* required to ensure that just created files report a valid address and not 0 */
557 11432 : fm.fiemap.fm_extent_count = 1; /* we are interested only at the first block */
558 :
559 11432 : if (ioctl(f, FS_IOC_FIEMAP, &fm) != -1) {
560 11432 : uint32_t flags = fm.fiemap.fm_extents[0].fe_flags;
561 11432 : uint64_t offset = fm.fiemap.fm_extents[0].fe_physical;
562 :
563 : /* check some condition for validating the offset */
564 11432 : if (flags & FIEMAP_EXTENT_DATA_INLINE) {
565 : /* if the data is inline, we don't have an offset to report */
566 0 : *physical = FILEPHY_WITHOUT_OFFSET;
567 11432 : } else if (flags & FIEMAP_EXTENT_UNKNOWN) {
568 : /* if the offset is unknown, we don't have an offset to report */
569 0 : *physical = FILEPHY_WITHOUT_OFFSET;
570 11432 : } else if (offset == 0) {
571 : /* 0 is the general fallback for file-systems when */
572 : /* they don't have an offset to report */
573 14 : *physical = FILEPHY_WITHOUT_OFFSET;
574 : } else {
575 : /* finally report the real offset */
576 11418 : *physical = offset + FILEPHY_REAL_OFFSET;
577 : }
578 :
579 11432 : if (close(f) == -1)
580 0 : return -1;
581 11432 : return 0;
582 : }
583 :
584 : /* if the file is empty, FIBMAP doesn't work, and we don't even try to use it */
585 0 : if (size == 0) {
586 0 : *physical = FILEPHY_WITHOUT_OFFSET;
587 0 : if (close(f) == -1)
588 0 : return -1;
589 0 : return 0;
590 : }
591 :
592 : /* then try with FIBMAP */
593 : /* it works for jfs, reiserfs, ntfs-3g */
594 : /* in exfat it always returns 0, that it's anyway better than the fake inodes */
595 0 : blknum = 0; /* first block */
596 0 : if (ioctl(f, FIBMAP, &blknum) != -1) {
597 0 : *physical = blknum + FILEPHY_REAL_OFFSET;
598 0 : if (close(f) == -1)
599 0 : return -1;
600 0 : return 0;
601 : }
602 :
603 : /* otherwise don't use anything, and keep the directory traversal order */
604 : /* at now this should happen only for vfat */
605 : /* and it's surely better than using fake inodes */
606 0 : *physical = FILEPHY_UNREPORTED_OFFSET;
607 0 : if (close(f) == -1)
608 0 : return -1;
609 : #else
610 : /* In a generic Unix use a dummy value for all the files */
611 : /* We don't want to risk to use the inode without knowing */
612 : /* if it really improves performance. */
613 : /* In this way we keep them in the directory traversal order */
614 : /* that at least keeps files in the same directory together. */
615 : /* Note also that in newer file-system with snapshot, like ZFS, */
616 : /* the inode doesn't represent evenmore the disk position, because files */
617 : /* are not overwritten in place, but rewritten in another location */
618 : /* of the disk. */
619 : *physical = FILEPHY_UNREPORTED_OFFSET;
620 :
621 : (void)path; /* not used here */
622 : (void)size;
623 : #endif
624 :
625 0 : return 0;
626 : }
627 :
628 5483 : int fsinfo(const char* path, int* has_persistent_inode, uint64_t* total_space, uint64_t* free_space)
629 : {
630 : char type[64];
631 : const char* ptype;
632 :
633 : #if HAVE_STATFS
634 : struct statfs st;
635 :
636 5483 : if (statfs(path, &st) != 0) {
637 : char dir[PATH_MAX];
638 : char* slash;
639 :
640 24 : if (errno != ENOENT) {
641 0 : return -1;
642 : }
643 :
644 : /* if it doesn't exist, we assume a file */
645 : /* and we check for the containing dir */
646 24 : if (strlen(path) + 1 > sizeof(dir)) {
647 0 : errno = ENAMETOOLONG;
648 0 : return -1;
649 : }
650 :
651 24 : strcpy(dir, path);
652 :
653 24 : slash = strrchr(dir, '/');
654 24 : if (!slash)
655 0 : return -1;
656 :
657 24 : *slash = 0;
658 24 : if (statfs(dir, &st) != 0)
659 0 : return -1;
660 : }
661 : #endif
662 :
663 : /* to get the fs type check "man stat" or "stat -f -t FILE" */
664 5483 : if (has_persistent_inode) {
665 : #if HAVE_STATFS && HAVE_STRUCT_STATFS_F_TYPE
666 575 : switch (st.f_type) {
667 : case 0x65735546 : /* FUSE, "fuseblk" in the stat command */
668 : case 0x4d44 : /* VFAT, "msdos" in the stat command */
669 0 : *has_persistent_inode = 0;
670 0 : break;
671 : default :
672 : /* by default assume yes */
673 575 : *has_persistent_inode = 1;
674 575 : break;
675 : }
676 : #else
677 : /* by default assume yes */
678 : *has_persistent_inode = 1;
679 : #endif
680 : }
681 :
682 5483 : if (total_space) {
683 : #if HAVE_STATFS
684 4908 : *total_space = st.f_bsize * (uint64_t)st.f_blocks;
685 : #else
686 : *total_space = 0;
687 : #endif
688 : }
689 :
690 5483 : if (free_space) {
691 : #if HAVE_STATFS
692 4908 : *free_space = st.f_bsize * (uint64_t)st.f_bfree;
693 : #else
694 : *free_space = 0;
695 : #endif
696 : }
697 :
698 : #if HAVE_STATFS && HAVE_STRUCT_STATFS_F_FSTYPENAME
699 : /* get the filesystem type directly from the struct (Mac OS X) */
700 : (void)type;
701 : ptype = st.f_fstypename;
702 : #elif HAVE_STATFS && HAVE_STRUCT_STATFS_F_TYPE
703 : /* get the filesystem type from f_type (Linux) */
704 : /* from: https://github.com/influxdata/gopsutil/blob/master/disk/disk_linux.go */
705 5483 : switch (st.f_type) {
706 0 : case 0x65735546 : ptype = "fuseblk"; break;
707 0 : case 0x4D44 : ptype = "vfat/msdos"; break;
708 5483 : case 0xEF53 : ptype = "ext2/3/4"; break;
709 0 : case 0x6969 : ptype = "nfs"; break; /* remote */
710 0 : case 0x6E667364 : ptype = "nfsd"; break; /* remote */
711 0 : case 0x517B : ptype = "smb"; break; /* remote */
712 0 : case 0x5346544E : ptype = "ntfs"; break;
713 0 : case 0x52654973 : ptype = "reiserfs"; break;
714 0 : case 0x3153464A : ptype = "jfs"; break;
715 0 : case 0x58465342 : ptype = "xfs"; break;
716 0 : case 0x9123683E : ptype = "btrfs"; break;
717 0 : case 0x2FC12FC1 : ptype = "zfs"; break;
718 : default :
719 0 : snprintf(type, sizeof(type), "0x%X", (unsigned)st.f_type);
720 0 : ptype = type;
721 : }
722 : #else
723 : (void)type;
724 : ptype = "unknown";
725 : #endif
726 :
727 5483 : log_tag("statfs:%s: %s \n", ptype, path);
728 :
729 5483 : return 0;
730 : }
731 :
732 3817537 : uint64_t tick(void)
733 : {
734 : #if HAVE_MACH_ABSOLUTE_TIME
735 : /* for Mac OS X */
736 : return mach_absolute_time();
737 : #elif HAVE_CLOCK_GETTIME && (defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW))
738 : /* for Linux */
739 : struct timespec tv;
740 :
741 : /* nanosecond precision with clock_gettime() */
742 : #if defined(CLOCK_MONOTONIC_RAW)
743 3817537 : if (clock_gettime(CLOCK_MONOTONIC_RAW, &tv) != 0) {
744 : #else
745 : if (clock_gettime(CLOCK_MONOTONIC, &tv) != 0) {
746 : #endif
747 0 : return 0;
748 : }
749 :
750 3817537 : return tv.tv_sec * 1000000000ULL + tv.tv_nsec;
751 : #else
752 : /* other platforms */
753 : struct timeval tv;
754 :
755 : /* microsecond precision with gettimeofday() */
756 : if (gettimeofday(&tv, 0) != 0) {
757 : return 0;
758 : }
759 :
760 : return tv.tv_sec * 1000000ULL + tv.tv_usec;
761 : #endif
762 : }
763 :
764 65 : uint64_t tick_ms(void)
765 : {
766 : struct timeval tv;
767 :
768 65 : if (gettimeofday(&tv, 0) != 0)
769 0 : return 0;
770 :
771 65 : return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
772 : }
773 :
774 282 : int randomize(void* ptr, size_t size)
775 : {
776 : int f;
777 : ssize_t ret;
778 :
779 282 : f = open("/dev/urandom", O_RDONLY);
780 282 : if (f == -1)
781 0 : return -1;
782 :
783 282 : ret = read(f, ptr, size);
784 282 : if (ret < 0 || (size_t)ret != size) {
785 0 : close(f);
786 0 : return -1;
787 : }
788 :
789 282 : if (close(f) != 0)
790 0 : return -1;
791 :
792 282 : return 0;
793 : }
794 :
795 : /**
796 : * Read a file extracting the contained device number in %u:%u format.
797 : * Return 0 on error.
798 : */
799 : #if HAVE_LINUX_DEVICE
800 144 : static dev_t devread(const char* path)
801 : {
802 : int f;
803 : int ret;
804 : int len;
805 : char buf[64];
806 : char* e;
807 : unsigned ma;
808 : unsigned mi;
809 :
810 144 : f = open(path, O_RDONLY);
811 144 : if (f == -1) {
812 : /* LCOV_EXCL_START */
813 : log_fatal("Failed to open '%s'.\n", path);
814 : return 0;
815 : /* LCOV_EXCL_STOP */
816 : }
817 :
818 144 : len = read(f, buf, sizeof(buf));
819 144 : if (len < 0) {
820 : /* LCOV_EXCL_START */
821 : close(f);
822 : log_fatal("Failed to read '%s'.\n", path);
823 : return 0;
824 : /* LCOV_EXCL_STOP */
825 : }
826 144 : if (len == sizeof(buf)) {
827 : /* LCOV_EXCL_START */
828 : close(f);
829 : log_fatal("Too long read '%s'.\n", path);
830 : return 0;
831 : /* LCOV_EXCL_STOP */
832 : }
833 :
834 144 : ret = close(f);
835 144 : if (ret != 0) {
836 : /* LCOV_EXCL_START */
837 : log_fatal("Failed to close '%s'.\n", path);
838 : return 0;
839 : /* LCOV_EXCL_STOP */
840 : }
841 :
842 144 : buf[len] = 0;
843 :
844 144 : ma = strtoul(buf, &e, 10);
845 144 : if (*e != ':') {
846 : /* LCOV_EXCL_START */
847 : log_fatal("Invalid format in '%s' for '%s'.\n", path, buf);
848 : return 0;
849 : /* LCOV_EXCL_STOP */
850 : }
851 :
852 144 : mi = strtoul(e + 1, &e, 10);
853 144 : if (*e != 0 && !isspace(*e)) {
854 : /* LCOV_EXCL_START */
855 : log_fatal("Invalid format in '%s' for '%s'.\n", path, buf);
856 : return 0;
857 : /* LCOV_EXCL_STOP */
858 : }
859 :
860 144 : return makedev(ma, mi);
861 : }
862 : #endif
863 :
864 : /**
865 : * Read a device tree filling the specified list of disk_t entries.
866 : */
867 : #if HAVE_LINUX_DEVICE
868 130 : static int devtree(const char* name, const char* custom, dev_t device, devinfo_t* parent, tommy_list* list)
869 : {
870 : char path[PATH_MAX];
871 : DIR* d;
872 130 : int slaves = 0;
873 :
874 130 : pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/slaves", major(device), minor(device));
875 :
876 : /* check if there is a slaves list */
877 130 : d = opendir(path);
878 130 : if (d != 0) {
879 : struct dirent* dd;
880 :
881 325 : while ((dd = readdir(d)) != 0) {
882 195 : if (dd->d_name[0] != '.') {
883 : /* for each slave, expand the full potential tree */
884 65 : pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/slaves/%s/dev", major(device), minor(device), dd->d_name);
885 :
886 65 : device = devread(path);
887 65 : if (!device) {
888 : /* LCOV_EXCL_START */
889 : closedir(d);
890 : return -1;
891 : /* LCOV_EXCL_STOP */
892 : }
893 :
894 65 : if (devtree(name, custom, device, parent, list) != 0) {
895 : /* LCOV_EXCL_START */
896 : closedir(d);
897 : return -1;
898 : /* LCOV_EXCL_STOP */
899 : }
900 :
901 65 : ++slaves;
902 : }
903 : }
904 :
905 65 : closedir(d);
906 : }
907 :
908 : /* if no slaves found */
909 130 : if (!slaves) {
910 : /* this is a raw device */
911 : devinfo_t* devinfo;
912 :
913 : /* check if it's a real device */
914 65 : pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/device", major(device), minor(device));
915 65 : if (access(path, F_OK) != 0) {
916 : /* get the parent device */
917 65 : pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/../dev", major(device), minor(device));
918 :
919 65 : device = devread(path);
920 65 : if (!device) {
921 : /* LCOV_EXCL_START */
922 : return -1;
923 : /* LCOV_EXCL_STOP */
924 : }
925 : }
926 :
927 : /* get the device file */
928 65 : if (devresolve(device, path, sizeof(path)) != 0) {
929 : /* LCOV_EXCL_START */
930 : log_fatal("Failed to resolve device '%u:%u'.\n", major(device), minor(device));
931 : return -1;
932 : /* LCOV_EXCL_STOP */
933 : }
934 :
935 65 : devinfo = calloc_nofail(1, sizeof(devinfo_t));
936 :
937 65 : devinfo->device = device;
938 65 : pathcpy(devinfo->name, sizeof(devinfo->name), name);
939 65 : pathcpy(devinfo->smartctl, sizeof(devinfo->smartctl), custom);
940 65 : pathcpy(devinfo->file, sizeof(devinfo->file), path);
941 65 : devinfo->parent = parent;
942 :
943 : /* insert in the list */
944 65 : tommy_list_insert_tail(list, &devinfo->node, devinfo);
945 : }
946 :
947 130 : return 0;
948 : }
949 : #endif
950 :
951 : /**
952 : * Scan all the devices.
953 : *
954 : * If a device is already in, it's not added again.
955 : */
956 : #if HAVE_LINUX_DEVICE
957 1 : static int devscan(tommy_list* list)
958 : {
959 : char dir[PATH_MAX];
960 : DIR* d;
961 : struct dirent* dd;
962 :
963 1 : pathprint(dir, sizeof(dir), "/sys/dev/block/");
964 :
965 : /* check if there is a slaves list */
966 1 : d = opendir(dir);
967 1 : if (d == 0) {
968 : /* LCOV_EXCL_START */
969 : log_fatal("Failed to open dir '%s'.\n", dir);
970 : return -1;
971 : /* LCOV_EXCL_STOP */
972 : }
973 :
974 74 : while ((dd = readdir(d)) != 0) {
975 : char path[PATH_MAX];
976 : tommy_node* i;
977 : dev_t device;
978 : devinfo_t* devinfo;
979 :
980 72 : if (dd->d_name[0] == '.')
981 61 : continue;
982 :
983 70 : pathprint(path, sizeof(path), "/sys/dev/block/%s/device", dd->d_name);
984 :
985 : /* check if it's a real device */
986 70 : if (access(path, F_OK) != 0)
987 56 : continue;
988 :
989 14 : pathprint(path, sizeof(path), "/sys/dev/block/%s/dev", dd->d_name);
990 :
991 14 : device = devread(path);
992 14 : if (!device) {
993 : /* LCOV_EXCL_START */
994 : log_tag("scan:skip: Skipping device %s because failed to read its device number.\n", dd->d_name);
995 : continue;
996 : /* LCOV_EXCL_STOP */
997 : }
998 :
999 : /* check if already present */
1000 482 : for (i = tommy_list_head(list); i != 0; i = i->next) {
1001 469 : devinfo = i->data;
1002 469 : if (devinfo->device == device)
1003 1 : break;
1004 : }
1005 :
1006 : /* if already present */
1007 14 : if (i != 0)
1008 1 : continue;
1009 :
1010 : /* get the device file */
1011 13 : if (devresolve(device, path, sizeof(path)) != 0) {
1012 : /* LCOV_EXCL_START */
1013 : log_tag("scan:skip: Skipping device %u:%u because failed to resolve.\n", major(device), minor(device));
1014 : continue;
1015 : /* LCOV_EXCL_STOP */
1016 : }
1017 :
1018 13 : devinfo = calloc_nofail(1, sizeof(devinfo_t));
1019 :
1020 13 : devinfo->device = device;
1021 13 : pathcpy(devinfo->file, sizeof(devinfo->file), path);
1022 :
1023 : /* insert in the list */
1024 13 : tommy_list_insert_tail(list, &devinfo->node, devinfo);
1025 : }
1026 :
1027 1 : closedir(d);
1028 1 : return 0;
1029 : }
1030 : #endif
1031 :
1032 : /**
1033 : * Get SMART attributes.
1034 : */
1035 : #if HAVE_LINUX_DEVICE
1036 43 : static int devsmart(dev_t device, const char* name, const char* custom, uint64_t* smart, char* serial, char* vendor, char* model)
1037 : {
1038 : char cmd[128];
1039 : char file[128];
1040 : FILE* f;
1041 : int ret;
1042 :
1043 43 : if (devresolve(device, file, sizeof(file)) != 0) {
1044 : /* LCOV_EXCL_START */
1045 : log_fatal("Failed to resolve device '%u:%u'.\n", major(device), minor(device));
1046 : return -1;
1047 : /* LCOV_EXCL_STOP */
1048 : }
1049 :
1050 : /* if there is a custom command */
1051 43 : if (custom[0]) {
1052 : char option[128];
1053 5 : snprintf(option, sizeof(option), custom, file);
1054 5 : snprintf(cmd, sizeof(cmd), "smartctl -a %s", option);
1055 : } else {
1056 38 : snprintf(cmd, sizeof(cmd), "smartctl -a %s", file);
1057 : }
1058 :
1059 43 : log_tag("smartctl:%s:%s:run: %s\n", file, name, cmd);
1060 :
1061 43 : f = popen(cmd, "r");
1062 43 : if (!f) {
1063 : /* LCOV_EXCL_START */
1064 : log_fatal("Failed to run '%s' (from popen).\n", cmd);
1065 : return -1;
1066 : /* LCOV_EXCL_STOP */
1067 : }
1068 :
1069 43 : if (smartctl_attribute(f, file, name, smart, serial, vendor, model) != 0) {
1070 : /* LCOV_EXCL_START */
1071 : pclose(f);
1072 : return -1;
1073 : /* LCOV_EXCL_STOP */
1074 : }
1075 :
1076 43 : ret = pclose(f);
1077 :
1078 43 : log_tag("smartctl:%s:%s:ret: %x\n", file, name, ret);
1079 :
1080 43 : if (!WIFEXITED(ret)) {
1081 : /* LCOV_EXCL_START */
1082 : log_fatal("Failed to run '%s' (not exited).\n", cmd);
1083 : return -1;
1084 : /* LCOV_EXCL_STOP */
1085 : }
1086 43 : if (WEXITSTATUS(ret) == 127) {
1087 : /* LCOV_EXCL_START */
1088 : log_fatal("Failed to run '%s' (from sh).\n", cmd);
1089 : return -1;
1090 : /* LCOV_EXCL_STOP */
1091 : }
1092 :
1093 : /* store the return smartctl return value */
1094 0 : smart[SMART_FLAGS] = WEXITSTATUS(ret);
1095 :
1096 0 : return 0;
1097 : }
1098 : #endif
1099 :
1100 : /**
1101 : * Spin down a specific device.
1102 : */
1103 : #if HAVE_LINUX_DEVICE
1104 5 : static int devdown(dev_t device, const char* name, const char* custom)
1105 : {
1106 : char cmd[128];
1107 : char file[128];
1108 : FILE* f;
1109 : int ret;
1110 :
1111 5 : if (devresolve(device, file, sizeof(file)) != 0) {
1112 : /* LCOV_EXCL_START */
1113 : log_fatal("Failed to resolve device '%u:%u'.\n", major(device), minor(device));
1114 : return -1;
1115 : /* LCOV_EXCL_STOP */
1116 : }
1117 :
1118 : /* if there is a custom command */
1119 5 : if (custom[0]) {
1120 : char option[128];
1121 5 : snprintf(option, sizeof(option), custom, file);
1122 5 : snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s", option);
1123 : } else {
1124 0 : snprintf(cmd, sizeof(cmd), "smartctl -s standby,now %s", file);
1125 : }
1126 :
1127 5 : log_tag("smartctl:%s:%s:run: %s\n", file, name, cmd);
1128 :
1129 5 : f = popen(cmd, "r");
1130 5 : if (!f) {
1131 : /* LCOV_EXCL_START */
1132 : log_fatal("Failed to run '%s' (from popen).\n", cmd);
1133 : return -1;
1134 : /* LCOV_EXCL_STOP */
1135 : }
1136 :
1137 5 : if (smartctl_flush(f, file, name) != 0) {
1138 : /* LCOV_EXCL_START */
1139 : pclose(f);
1140 : return -1;
1141 : /* LCOV_EXCL_STOP */
1142 : }
1143 :
1144 5 : ret = pclose(f);
1145 :
1146 5 : log_tag("smartctl:%s:%s:ret: %x\n", file, name, ret);
1147 :
1148 5 : if (!WIFEXITED(ret)) {
1149 : /* LCOV_EXCL_START */
1150 : log_fatal("Failed to run '%s' (not exited).\n", cmd);
1151 : return -1;
1152 : /* LCOV_EXCL_STOP */
1153 : }
1154 5 : if (WEXITSTATUS(ret) == 127) {
1155 : /* LCOV_EXCL_START */
1156 : log_fatal("Failed to run '%s' (from sh).\n", cmd);
1157 : return -1;
1158 : /* LCOV_EXCL_STOP */
1159 : }
1160 0 : if (WEXITSTATUS(ret) != 0) {
1161 : /* LCOV_EXCL_START */
1162 : log_fatal("Failed to run '%s' with return code %xh.\n", cmd, WEXITSTATUS(ret));
1163 : return -1;
1164 : /* LCOV_EXCL_STOP */
1165 : }
1166 :
1167 0 : return 0;
1168 : }
1169 : #endif
1170 :
1171 : /**
1172 : * Spin up a device.
1173 : *
1174 : * There isn't a defined way to spin up a device,
1175 : * so we just do a generic write.
1176 : */
1177 30 : static int devup(const char* mountpoint)
1178 : {
1179 : int ret;
1180 : char path[PATH_MAX];
1181 :
1182 : /* add a temporary name used for writing */
1183 30 : pathprint(path, sizeof(path), "%s.snapraid-spinup", mountpoint);
1184 :
1185 : /* do a generic write, and immediately undo it */
1186 30 : ret = mkdir(path, 0);
1187 30 : if (ret != 0 && errno != EEXIST) {
1188 : /* LCOV_EXCL_START */
1189 : log_fatal("Failed to create dir '%s'.\n", path);
1190 : return -1;
1191 : /* LCOV_EXCL_STOP */
1192 : }
1193 :
1194 : /* remove the just created dir */
1195 30 : rmdir(path);
1196 :
1197 30 : return 0;
1198 : }
1199 :
1200 : /**
1201 : * Thread for spinning up.
1202 : *
1203 : * Note that filling up the devinfo object is done inside this thread,
1204 : * to avoid to block the main thread if the device need to be spin up
1205 : * to handle stat/resolve requests.
1206 : */
1207 30 : static void* thread_spinup(void* arg)
1208 : {
1209 30 : devinfo_t* devinfo = arg;
1210 : struct stat st;
1211 : uint64_t start;
1212 :
1213 30 : start = tick_ms();
1214 :
1215 : /* first get the device number, this usually doesn't trigger a thread_spinup */
1216 30 : if (stat(devinfo->mount, &st) != 0) {
1217 : /* LCOV_EXCL_START */
1218 : log_fatal("Failed to stat device '%s'.\n", devinfo->mount);
1219 : return (void*)-1;
1220 : /* LCOV_EXCL_STOP */
1221 : }
1222 :
1223 : /* set the device number for printing */
1224 30 : devinfo->device = st.st_dev;
1225 :
1226 30 : if (devup(devinfo->mount) != 0) {
1227 : /* LCOV_EXCL_START */
1228 : return (void*)-1;
1229 : /* LCOV_EXCL_STOP */
1230 : }
1231 :
1232 30 : msg_status("Spunup device '%u:%u' for disk '%s' in %" PRIu64 " ms.\n", major(devinfo->device), minor(devinfo->device), devinfo->name, tick_ms() - start);
1233 :
1234 30 : return 0;
1235 : }
1236 :
1237 : /**
1238 : * Thread for spinning down.
1239 : */
1240 5 : static void* thread_spindown(void* arg)
1241 : {
1242 : #if HAVE_LINUX_DEVICE
1243 5 : devinfo_t* devinfo = arg;
1244 : uint64_t start;
1245 :
1246 5 : start = tick_ms();
1247 :
1248 5 : if (devdown(devinfo->device, devinfo->name, devinfo->smartctl) != 0) {
1249 : /* LCOV_EXCL_START */
1250 : return (void*)-1;
1251 : /* LCOV_EXCL_STOP */
1252 : }
1253 :
1254 0 : msg_status("Spundown device '%s' for disk '%s' in %" PRIu64 " ms.\n", devinfo->file, devinfo->name, tick_ms() - start);
1255 :
1256 0 : return 0;
1257 : #else
1258 : (void)arg;
1259 : return (void*)-1;
1260 : #endif
1261 : }
1262 :
1263 : /**
1264 : * Thread for getting smart info.
1265 : */
1266 43 : static void* thread_smart(void* arg)
1267 : {
1268 : #if HAVE_LINUX_DEVICE
1269 43 : devinfo_t* devinfo = arg;
1270 :
1271 43 : if (devsmart(devinfo->device, devinfo->name, devinfo->smartctl, devinfo->smart, devinfo->smart_serial, devinfo->smart_vendor, devinfo->smart_model) != 0) {
1272 : /* LCOV_EXCL_START */
1273 : return (void*)-1;
1274 : /* LCOV_EXCL_STOP */
1275 : }
1276 :
1277 0 : return 0;
1278 : #else
1279 : (void)arg;
1280 : return (void*)-1;
1281 : #endif
1282 : }
1283 :
1284 3 : static int device_thread(tommy_list* list, void* (*func)(void* arg))
1285 : {
1286 3 : int fail = 0;
1287 : tommy_node* i;
1288 :
1289 : #if HAVE_PTHREAD
1290 : /* start all threads */
1291 81 : for (i = tommy_list_head(list); i != 0; i = i->next) {
1292 78 : devinfo_t* devinfo = i->data;
1293 :
1294 78 : thread_create(&devinfo->thread, 0, func, devinfo);
1295 : }
1296 :
1297 : /* join all threads */
1298 81 : for (i = tommy_list_head(list); i != 0; i = i->next) {
1299 78 : devinfo_t* devinfo = i->data;
1300 : void* retval;
1301 :
1302 78 : thread_join(devinfo->thread, &retval);
1303 :
1304 78 : if (retval != 0)
1305 48 : ++fail;
1306 : }
1307 : #else
1308 : for (i = tommy_list_head(list); i != 0; i = i->next) {
1309 : devinfo_t* devinfo = i->data;
1310 :
1311 : if (func(devinfo) != 0)
1312 : ++fail;
1313 : }
1314 : #endif
1315 3 : if (fail != 0) {
1316 : /* LCOV_EXCL_START */
1317 : return -1;
1318 : /* LCOV_EXCL_STOP */
1319 : }
1320 :
1321 1 : return 0;
1322 : }
1323 :
1324 4 : int devquery(tommy_list* high, tommy_list* low, int operation, int others)
1325 : {
1326 : tommy_node* i;
1327 4 : void* (*func)(void* arg) = 0;
1328 :
1329 : #if HAVE_LINUX_DEVICE
1330 4 : if (operation != DEVICE_UP) {
1331 : struct stat st;
1332 : /* sysfs interface is required */
1333 3 : if (stat("/sys/dev/block", &st) != 0) {
1334 : /* LCOV_EXCL_START */
1335 : log_fatal("Missing interface /sys/dev/block.\n");
1336 : return -1;
1337 : /* LCOV_EXCL_STOP */
1338 : }
1339 :
1340 : /* for each device */
1341 136 : for (i = tommy_list_head(high); i != 0; i = i->next) {
1342 65 : devinfo_t* devinfo = i->data;
1343 65 : uint64_t device = devinfo->device;
1344 :
1345 : /* if the major is the null device, find the real one */
1346 65 : if (major(device) == 0) {
1347 : /* obtain the real device */
1348 0 : if (devdereference(device, &device) != 0) {
1349 : /* LCOV_EXCL_START */
1350 : log_fatal("Failed to dereference device '%u:%u'.\n", major(device), minor(device));
1351 : return -1;
1352 : /* LCOV_EXCL_STOP */
1353 : }
1354 : }
1355 :
1356 : /* get the device file */
1357 65 : if (devresolve(device, devinfo->file, sizeof(devinfo->file)) != 0) {
1358 : /* LCOV_EXCL_START */
1359 : log_fatal("Failed to resolve device '%u:%u'.\n", major(device), minor(device));
1360 : return -1;
1361 : /* LCOV_EXCL_STOP */
1362 : }
1363 :
1364 : /* expand the tree of devices */
1365 65 : if (devtree(devinfo->name, devinfo->smartctl, device, devinfo, low) != 0) {
1366 : /* LCOV_EXCL_START */
1367 : log_fatal("Failed to expand device '%u:%u'.\n", major(device), minor(device));
1368 : return -1;
1369 : /* LCOV_EXCL_STOP */
1370 : }
1371 : }
1372 : }
1373 : #endif
1374 :
1375 4 : if (operation == DEVICE_UP) {
1376 : /* duplicate the high */
1377 31 : for (i = tommy_list_head(high); i != 0; i = i->next) {
1378 30 : devinfo_t* devinfo = i->data;
1379 : devinfo_t* entry;
1380 :
1381 30 : entry = calloc_nofail(1, sizeof(devinfo_t));
1382 :
1383 30 : entry->device = devinfo->device;
1384 30 : pathcpy(entry->name, sizeof(entry->name), devinfo->name);
1385 30 : pathcpy(entry->mount, sizeof(entry->mount), devinfo->mount);
1386 :
1387 : /* insert in the high */
1388 30 : tommy_list_insert_tail(low, &entry->node, entry);
1389 : }
1390 : }
1391 :
1392 : #if HAVE_LINUX_DEVICE
1393 : /* add other devices */
1394 4 : if (others) {
1395 1 : if (devscan(low) != 0) {
1396 : /* LCOV_EXCL_START */
1397 : log_fatal("Failed to list other devices.\n");
1398 : return -1;
1399 : /* LCOV_EXCL_STOP */
1400 : }
1401 : }
1402 : #else
1403 : (void)others;
1404 : #endif
1405 :
1406 4 : switch (operation) {
1407 1 : case DEVICE_UP : func = thread_spinup; break;
1408 1 : case DEVICE_DOWN : func = thread_spindown; break;
1409 1 : case DEVICE_SMART : func = thread_smart; break;
1410 : }
1411 :
1412 4 : if (!func)
1413 1 : return 0;
1414 :
1415 3 : return device_thread(low, func);
1416 : }
1417 :
1418 271 : void os_init(int opt)
1419 : {
1420 : #if HAVE_BLKID
1421 : int ret;
1422 271 : ret = blkid_get_cache(&cache, NULL);
1423 271 : if (ret != 0) {
1424 : /* LCOV_EXCL_START */
1425 : log_fatal("WARNING Failed to get blkid cache\n");
1426 : /* LCOV_EXCL_STOP */
1427 : }
1428 : #endif
1429 :
1430 : (void)opt;
1431 271 : }
1432 :
1433 248 : void os_done(void)
1434 : {
1435 : #if HAVE_BLKID
1436 248 : if (cache != 0)
1437 248 : blkid_put_cache(cache);
1438 : #endif
1439 248 : }
1440 :
1441 : /* LCOV_EXCL_START */
1442 : void os_abort(void)
1443 : {
1444 : #if HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS
1445 : void* stack[32];
1446 : char** messages;
1447 : size_t size;
1448 : unsigned i;
1449 : #endif
1450 :
1451 : printf("Stacktrace of " PACKAGE " v" VERSION);
1452 : #ifdef _linux
1453 : printf(", linux");
1454 : #endif
1455 : #ifdef __GNUC__
1456 : printf(", gcc " __VERSION__);
1457 : #endif
1458 : printf(", %d-bit", (int)sizeof(void *) * 8);
1459 : printf(", PATH_MAX=%d", PATH_MAX);
1460 : printf("\n");
1461 :
1462 : #if HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS
1463 : size = backtrace(stack, 32);
1464 :
1465 : messages = backtrace_symbols(stack, size);
1466 :
1467 : for (i = 1; i < size; ++i) {
1468 : const char* msg;
1469 :
1470 : if (messages)
1471 : msg = messages[i];
1472 : else
1473 : msg = "<unknown>";
1474 :
1475 : printf("[bt] %02u: %s\n", i, msg);
1476 :
1477 : if (messages) {
1478 : int ret;
1479 : char addr2line[1024];
1480 : size_t j = 0;
1481 : while (msg[j] != '(' && msg[j] != ' ' && msg[j] != 0)
1482 : ++j;
1483 :
1484 : snprintf(addr2line, sizeof(addr2line), "addr2line %p -e %.*s", stack[i], (unsigned)j, msg);
1485 :
1486 : ret = system(addr2line);
1487 : if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0)
1488 : printf("exit:%d\n", WEXITSTATUS(ret));
1489 : if (WIFSIGNALED(ret))
1490 : printf("signal:%d\n", WTERMSIG(ret));
1491 : }
1492 : }
1493 : #endif
1494 :
1495 : printf("Please report this error to the SnapRAID Forum:\n");
1496 : printf("https://sourceforge.net/p/snapraid/discussion/1677233/\n");
1497 :
1498 : abort();
1499 : }
1500 : /* LCOV_EXCL_STOP */
1501 :
1502 1 : void os_clear(void)
1503 : {
1504 : /* ANSI codes */
1505 1 : printf("\033[H"); /* cursor at topleft */
1506 1 : printf("\033[2J"); /* clear screen */
1507 1 : }
1508 :
1509 7 : size_t direct_size(void)
1510 : {
1511 : long size;
1512 :
1513 7 : size = sysconf(_SC_PAGESIZE);
1514 :
1515 7 : if (size == -1) {
1516 : /* LCOV_EXCL_START */
1517 : log_fatal("No page size\n");
1518 : exit(EXIT_FAILURE);
1519 : /* LCOV_EXCL_STOP */
1520 : }
1521 :
1522 7 : return size;
1523 : }
1524 :
1525 : #endif
1526 :
|