]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs.c
Upload to unstable
[bcachefs-tools-debian] / libbcachefs.c
1 #include <ctype.h>
2 #include <dirent.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/stat.h>
11 #include <sys/sysmacros.h>
12 #include <sys/types.h>
13 #include <time.h>
14 #include <unistd.h>
15
16 #include <uuid/uuid.h>
17
18 #include "libbcachefs.h"
19 #include "crypto.h"
20 #include "libbcachefs/bcachefs_format.h"
21 #include "libbcachefs/btree_cache.h"
22 #include "libbcachefs/checksum.h"
23 #include "libbcachefs/disk_groups.h"
24 #include "libbcachefs/journal_seq_blacklist.h"
25 #include "libbcachefs/opts.h"
26 #include "libbcachefs/replicas.h"
27 #include "libbcachefs/super-io.h"
28 #include "tools-util.h"
29
30 #define NSEC_PER_SEC    1000000000L
31
32 static void init_layout(struct bch_sb_layout *l,
33                         unsigned block_size,
34                         unsigned sb_size,
35                         u64 sb_start, u64 sb_end)
36 {
37         u64 sb_pos = sb_start;
38         unsigned i;
39
40         memset(l, 0, sizeof(*l));
41
42         l->magic                = BCHFS_MAGIC;
43         l->layout_type          = 0;
44         l->nr_superblocks       = 2;
45         l->sb_max_size_bits     = ilog2(sb_size);
46
47         /* Create two superblocks in the allowed range: */
48         for (i = 0; i < l->nr_superblocks; i++) {
49                 if (sb_pos != BCH_SB_SECTOR)
50                         sb_pos = round_up(sb_pos, block_size);
51
52                 l->sb_offset[i] = cpu_to_le64(sb_pos);
53                 sb_pos += sb_size;
54         }
55
56         if (sb_pos > sb_end)
57                 die("insufficient space for superblocks: start %llu end %llu > %llu size %u",
58                     sb_start, sb_pos, sb_end, sb_size);
59 }
60
61 /* minimum size filesystem we can create, given a bucket size: */
62 static u64 min_size(unsigned bucket_size)
63 {
64         return BCH_MIN_NR_NBUCKETS * bucket_size;
65 }
66
67 u64 bch2_pick_bucket_size(struct bch_opts opts, struct dev_opts *dev)
68 {
69         u64 bucket_size;
70
71         if (dev->size < min_size(opts.block_size))
72                 die("cannot format %s, too small (%llu bytes, min %llu)",
73                     dev->path, dev->size, min_size(opts.block_size));
74
75         /* Bucket size must be >= block size: */
76         bucket_size = opts.block_size;
77
78         /* Bucket size must be >= btree node size: */
79         if (opt_defined(opts, btree_node_size))
80                 bucket_size = max_t(unsigned, bucket_size,
81                                          opts.btree_node_size);
82
83         /* Want a bucket size of at least 128k, if possible: */
84         bucket_size = max(bucket_size, 128ULL << 10);
85
86         if (dev->size >= min_size(bucket_size)) {
87                 unsigned scale = max(1,
88                         ilog2(dev->size / min_size(bucket_size)) / 4);
89
90                 scale = rounddown_pow_of_two(scale);
91
92                 /* max bucket size 1 mb */
93                 bucket_size = min(bucket_size * scale, 1ULL << 20);
94         } else {
95                 do {
96                         bucket_size /= 2;
97                 } while (dev->size < min_size(bucket_size));
98         }
99
100         return bucket_size;
101 }
102
103 void bch2_check_bucket_size(struct bch_opts opts, struct dev_opts *dev)
104 {
105         if (dev->bucket_size < opts.block_size)
106                 die("Bucket size (%llu) cannot be smaller than block size (%u)",
107                     dev->bucket_size, opts.block_size);
108
109         if (opt_defined(opts, btree_node_size) &&
110             dev->bucket_size < opts.btree_node_size)
111                 die("Bucket size (%llu) cannot be smaller than btree node size (%u)",
112                     dev->bucket_size, opts.btree_node_size);
113
114         if (dev->nbuckets < BCH_MIN_NR_NBUCKETS)
115                 die("Not enough buckets: %llu, need %u (bucket size %llu)",
116                     dev->nbuckets, BCH_MIN_NR_NBUCKETS, dev->bucket_size);
117
118         if (dev->bucket_size > (u32) U16_MAX << 9)
119                 die("Bucket size (%llu) too big (max %u)",
120                     dev->bucket_size, (u32) U16_MAX << 9);
121 }
122
123 static unsigned parse_target(struct bch_sb_handle *sb,
124                              struct dev_opts *devs, size_t nr_devs,
125                              const char *s)
126 {
127         struct dev_opts *i;
128         int idx;
129
130         if (!s)
131                 return 0;
132
133         for (i = devs; i < devs + nr_devs; i++)
134                 if (!strcmp(s, i->path))
135                         return dev_to_target(i - devs);
136
137         idx = bch2_disk_path_find(sb, s);
138         if (idx >= 0)
139                 return group_to_target(idx);
140
141         die("Invalid target %s", s);
142         return 0;
143 }
144
145 struct bch_sb *bch2_format(struct bch_opt_strs  fs_opt_strs,
146                            struct bch_opts      fs_opts,
147                            struct format_opts   opts,
148                            struct dev_opts      *devs,
149                            size_t               nr_devs)
150 {
151         struct bch_sb_handle sb = { NULL };
152         struct dev_opts *i;
153         unsigned max_dev_block_size = 0;
154         unsigned opt_id;
155         u64 min_bucket_size = U64_MAX;
156
157         for (i = devs; i < devs + nr_devs; i++)
158                 max_dev_block_size = max(max_dev_block_size, get_blocksize(i->bdev->bd_buffered_fd));
159
160         /* calculate block size: */
161         if (!opt_defined(fs_opts, block_size)) {
162                 opt_set(fs_opts, block_size, max_dev_block_size);
163         } else if (fs_opts.block_size < max_dev_block_size)
164                 die("blocksize too small: %u, must be greater than device blocksize %u",
165                     fs_opts.block_size, max_dev_block_size);
166
167         /* get device size, if it wasn't specified: */
168         for (i = devs; i < devs + nr_devs; i++)
169                 if (!i->size)
170                         i->size = get_size(i->bdev->bd_buffered_fd);
171
172         /* calculate bucket sizes: */
173         for (i = devs; i < devs + nr_devs; i++)
174                 min_bucket_size = min(min_bucket_size,
175                         i->bucket_size ?: bch2_pick_bucket_size(fs_opts, i));
176
177         for (i = devs; i < devs + nr_devs; i++)
178                 if (!i->bucket_size)
179                         i->bucket_size = min_bucket_size;
180
181         for (i = devs; i < devs + nr_devs; i++) {
182                 i->nbuckets = i->size / i->bucket_size;
183                 bch2_check_bucket_size(fs_opts, i);
184         }
185
186         /* calculate btree node size: */
187         if (!opt_defined(fs_opts, btree_node_size)) {
188                 /* 256k default btree node size */
189                 opt_set(fs_opts, btree_node_size, 256 << 10);
190
191                 for (i = devs; i < devs + nr_devs; i++)
192                         fs_opts.btree_node_size =
193                                 min_t(unsigned, fs_opts.btree_node_size,
194                                       i->bucket_size);
195         }
196
197         if (uuid_is_null(opts.uuid.b))
198                 uuid_generate(opts.uuid.b);
199
200         if (bch2_sb_realloc(&sb, 0))
201                 die("insufficient memory");
202
203         sb.sb->version          = le16_to_cpu(opts.version);
204         sb.sb->version_min      = le16_to_cpu(opts.version);
205         sb.sb->magic            = BCHFS_MAGIC;
206         sb.sb->user_uuid        = opts.uuid;
207         sb.sb->nr_devices       = nr_devs;
208
209         if (opts.version == bcachefs_metadata_version_current)
210                 sb.sb->features[0] |= cpu_to_le64(BCH_SB_FEATURES_ALL);
211
212         uuid_generate(sb.sb->uuid.b);
213
214         if (opts.label)
215                 memcpy(sb.sb->label,
216                        opts.label,
217                        min(strlen(opts.label), sizeof(sb.sb->label)));
218
219         for (opt_id = 0;
220              opt_id < bch2_opts_nr;
221              opt_id++) {
222                 u64 v;
223
224                 v = bch2_opt_defined_by_id(&fs_opts, opt_id)
225                         ? bch2_opt_get_by_id(&fs_opts, opt_id)
226                         : bch2_opt_get_by_id(&bch2_opts_default, opt_id);
227
228                 __bch2_opt_set_sb(sb.sb, &bch2_opt_table[opt_id], v);
229         }
230
231         struct timespec now;
232         if (clock_gettime(CLOCK_REALTIME, &now))
233                 die("error getting current time: %m");
234
235         sb.sb->time_base_lo     = cpu_to_le64(now.tv_sec * NSEC_PER_SEC + now.tv_nsec);
236         sb.sb->time_precision   = cpu_to_le32(1);
237
238         /* Member info: */
239         struct bch_sb_field_members_v2 *mi =
240                 bch2_sb_field_resize(&sb, members_v2,
241                         (sizeof(*mi) + sizeof(struct bch_member) *
242                         nr_devs) / sizeof(u64));
243         mi->member_bytes = cpu_to_le16(sizeof(struct bch_member));
244         for (i = devs; i < devs + nr_devs; i++) {
245                 struct bch_member *m = bch2_members_v2_get_mut(sb.sb, (i - devs));
246
247                 uuid_generate(m->uuid.b);
248                 m->nbuckets     = cpu_to_le64(i->nbuckets);
249                 m->first_bucket = 0;
250                 m->bucket_size  = cpu_to_le16(i->bucket_size >> 9);
251
252                 SET_BCH_MEMBER_DISCARD(m,       i->discard);
253                 SET_BCH_MEMBER_DATA_ALLOWED(m,  i->data_allowed);
254                 SET_BCH_MEMBER_DURABILITY(m,    i->durability + 1);
255         }
256
257         /* Disk labels*/
258         for (i = devs; i < devs + nr_devs; i++) {
259                 struct bch_member *m;
260                 int idx;
261
262                 if (!i->label)
263                         continue;
264
265                 idx = bch2_disk_path_find_or_create(&sb, i->label);
266                 if (idx < 0)
267                         die("error creating disk path: %s", strerror(-idx));
268
269                 /*
270                  * Recompute mi and m after each sb modification: its location
271                  * in memory may have changed due to reallocation.
272                  */
273                 m = bch2_members_v2_get_mut(sb.sb, (i - devs));
274                 SET_BCH_MEMBER_GROUP(m, idx + 1);
275         }
276
277         SET_BCH_SB_FOREGROUND_TARGET(sb.sb,
278                 parse_target(&sb, devs, nr_devs, fs_opt_strs.foreground_target));
279         SET_BCH_SB_BACKGROUND_TARGET(sb.sb,
280                 parse_target(&sb, devs, nr_devs, fs_opt_strs.background_target));
281         SET_BCH_SB_PROMOTE_TARGET(sb.sb,
282                 parse_target(&sb, devs, nr_devs, fs_opt_strs.promote_target));
283         SET_BCH_SB_METADATA_TARGET(sb.sb,
284                 parse_target(&sb, devs, nr_devs, fs_opt_strs.metadata_target));
285
286         /* Crypt: */
287         if (opts.encrypted) {
288                 struct bch_sb_field_crypt *crypt =
289                         bch2_sb_field_resize(&sb, crypt, sizeof(*crypt) / sizeof(u64));
290
291                 bch_sb_crypt_init(sb.sb, crypt, opts.passphrase);
292                 SET_BCH_SB_ENCRYPTION_TYPE(sb.sb, 1);
293         }
294
295         bch2_sb_members_cpy_v2_v1(&sb);
296
297         for (i = devs; i < devs + nr_devs; i++) {
298                 u64 size_sectors = i->size >> 9;
299
300                 sb.sb->dev_idx = i - devs;
301
302                 if (!i->sb_offset) {
303                         i->sb_offset    = BCH_SB_SECTOR;
304                         i->sb_end       = size_sectors;
305                 }
306
307                 init_layout(&sb.sb->layout, fs_opts.block_size,
308                             opts.superblock_size,
309                             i->sb_offset, i->sb_end);
310
311                 /*
312                  * Also create a backup superblock at the end of the disk:
313                  *
314                  * If we're not creating a superblock at the default offset, it
315                  * means we're being run from the migrate tool and we could be
316                  * overwriting existing data if we write to the end of the disk:
317                  */
318                 if (i->sb_offset == BCH_SB_SECTOR) {
319                         struct bch_sb_layout *l = &sb.sb->layout;
320                         u64 backup_sb = size_sectors - (1 << l->sb_max_size_bits);
321
322                         backup_sb = rounddown(backup_sb, i->bucket_size >> 9);
323                         l->sb_offset[l->nr_superblocks++] = cpu_to_le64(backup_sb);
324                 }
325
326                 if (i->sb_offset == BCH_SB_SECTOR) {
327                         /* Zero start of disk */
328                         static const char zeroes[BCH_SB_SECTOR << 9];
329
330                         xpwrite(i->bdev->bd_buffered_fd, zeroes, BCH_SB_SECTOR << 9, 0,
331                                 "zeroing start of disk");
332                 }
333
334                 bch2_super_write(i->bdev->bd_buffered_fd, sb.sb);
335                 close(i->bdev->bd_buffered_fd);
336         }
337
338         return sb.sb;
339 }
340
341 void bch2_super_write(int fd, struct bch_sb *sb)
342 {
343         struct nonce nonce = { 0 };
344
345         unsigned i;
346         for (i = 0; i < sb->layout.nr_superblocks; i++) {
347                 sb->offset = sb->layout.sb_offset[i];
348
349                 if (sb->offset == BCH_SB_SECTOR) {
350                         /* Write backup layout */
351                         xpwrite(fd, &sb->layout, sizeof(sb->layout),
352                                 BCH_SB_LAYOUT_SECTOR << 9,
353                                 "backup layout");
354                 }
355
356                 sb->csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb), nonce, sb);
357                 xpwrite(fd, sb, vstruct_bytes(sb),
358                         le64_to_cpu(sb->offset) << 9,
359                         "superblock");
360         }
361
362         fsync(fd);
363 }
364
365 struct bch_sb *__bch2_super_read(int fd, u64 sector)
366 {
367         struct bch_sb sb, *ret;
368
369         xpread(fd, &sb, sizeof(sb), sector << 9);
370
371         if (memcmp(&sb.magic, &BCACHE_MAGIC, sizeof(sb.magic)) &&
372             memcmp(&sb.magic, &BCHFS_MAGIC, sizeof(sb.magic)))
373                 die("not a bcachefs superblock");
374
375         size_t bytes = vstruct_bytes(&sb);
376
377         ret = malloc(bytes);
378
379         xpread(fd, ret, bytes, sector << 9);
380
381         return ret;
382 }
383
384 /* ioctl interface: */
385
386 /* Global control device: */
387 int bcachectl_open(void)
388 {
389         return xopen("/dev/bcachefs-ctl", O_RDWR);
390 }
391
392 /* Filesystem handles (ioctl, sysfs dir): */
393
394 #define SYSFS_BASE "/sys/fs/bcachefs/"
395
396 void bcache_fs_close(struct bchfs_handle fs)
397 {
398         close(fs.ioctl_fd);
399         close(fs.sysfs_fd);
400 }
401
402 struct bchfs_handle bcache_fs_open(const char *path)
403 {
404         struct bchfs_handle ret;
405
406         if (!uuid_parse(path, ret.uuid.b)) {
407                 /* It's a UUID, look it up in sysfs: */
408                 char *sysfs = mprintf(SYSFS_BASE "%s", path);
409                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
410
411                 char *minor = read_file_str(ret.sysfs_fd, "minor");
412                 char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
413                 ret.ioctl_fd = xopen(ctl, O_RDWR);
414
415                 free(sysfs);
416                 free(minor);
417                 free(ctl);
418         } else {
419                 /* It's a path: */
420                 ret.ioctl_fd = open(path, O_RDONLY);
421                 if (ret.ioctl_fd < 0)
422                         die("Error opening filesystem at %s: %m", path);
423
424                 struct bch_ioctl_query_uuid uuid;
425                 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid) < 0)
426                         die("error opening %s: not a bcachefs filesystem", path);
427
428                 ret.uuid = uuid.uuid;
429
430                 char uuid_str[40];
431                 uuid_unparse(uuid.uuid.b, uuid_str);
432
433                 char *sysfs = mprintf(SYSFS_BASE "%s", uuid_str);
434                 ret.sysfs_fd = xopen(sysfs, O_RDONLY);
435                 free(sysfs);
436         }
437
438         return ret;
439 }
440
441 /*
442  * Given a path to a block device, open the filesystem it belongs to; also
443  * return the device's idx:
444  */
445 struct bchfs_handle bchu_fs_open_by_dev(const char *path, int *idx)
446 {
447         char buf[1024], *uuid_str;
448
449         struct stat stat = xstat(path);
450
451         if (!S_ISBLK(stat.st_mode))
452                 die("%s is not a block device", path);
453
454         char *sysfs = mprintf("/sys/dev/block/%u:%u/bcachefs",
455                               major(stat.st_dev),
456                               minor(stat.st_dev));
457         ssize_t len = readlink(sysfs, buf, sizeof(buf));
458         free(sysfs);
459
460         if (len > 0) {
461                 char *p = strrchr(buf, '/');
462                 if (!p || sscanf(p + 1, "dev-%u", idx) != 1)
463                         die("error parsing sysfs");
464
465                 *p = '\0';
466                 p = strrchr(buf, '/');
467                 uuid_str = p + 1;
468         } else {
469                 struct bch_opts opts = bch2_opts_empty();
470
471                 opt_set(opts, noexcl,   true);
472                 opt_set(opts, nochanges, true);
473
474                 struct bch_sb_handle sb;
475                 int ret = bch2_read_super(path, &opts, &sb);
476                 if (ret)
477                         die("Error opening %s: %s", path, strerror(-ret));
478
479                 *idx = sb.sb->dev_idx;
480                 uuid_str = buf;
481                 uuid_unparse(sb.sb->user_uuid.b, uuid_str);
482
483                 bch2_free_super(&sb);
484         }
485
486         return bcache_fs_open(uuid_str);
487 }
488
489 int bchu_dev_path_to_idx(struct bchfs_handle fs, const char *dev_path)
490 {
491         int idx;
492         struct bchfs_handle fs2 = bchu_fs_open_by_dev(dev_path, &idx);
493
494         if (memcmp(&fs.uuid, &fs2.uuid, sizeof(fs.uuid)))
495                 idx = -1;
496         bcache_fs_close(fs2);
497         return idx;
498 }
499
500 int bchu_data(struct bchfs_handle fs, struct bch_ioctl_data cmd)
501 {
502         int progress_fd = xioctl(fs.ioctl_fd, BCH_IOCTL_DATA, &cmd);
503
504         while (1) {
505                 struct bch_ioctl_data_event e;
506
507                 if (read(progress_fd, &e, sizeof(e)) != sizeof(e))
508                         die("error reading from progress fd %m");
509
510                 if (e.type)
511                         continue;
512
513                 if (e.p.data_type == U8_MAX)
514                         break;
515
516                 printf("\33[2K\r");
517
518                 printf("%llu%% complete: current position %s",
519                        e.p.sectors_total
520                        ? e.p.sectors_done * 100 / e.p.sectors_total
521                        : 0,
522                        bch2_data_types[e.p.data_type]);
523
524                 switch (e.p.data_type) {
525                 case BCH_DATA_btree:
526                 case BCH_DATA_user:
527                         printf(" %s:%llu:%llu",
528                                bch2_btree_id_str(e.p.btree_id),
529                                e.p.pos.inode,
530                                e.p.pos.offset);
531                 }
532
533                 fflush(stdout);
534                 sleep(1);
535         }
536         printf("\nDone\n");
537
538         close(progress_fd);
539         return 0;
540 }
541
542 /* option parsing */
543
544 void bch2_opt_strs_free(struct bch_opt_strs *opts)
545 {
546         unsigned i;
547
548         for (i = 0; i < bch2_opts_nr; i++) {
549                 free(opts->by_id[i]);
550                 opts->by_id[i] = NULL;
551         }
552 }
553
554 struct bch_opt_strs bch2_cmdline_opts_get(int *argc, char *argv[],
555                                           unsigned opt_types)
556 {
557         struct bch_opt_strs opts;
558         unsigned i = 1;
559
560         memset(&opts, 0, sizeof(opts));
561
562         while (i < *argc) {
563                 char *optstr = strcmp_prefix(argv[i], "--");
564                 char *valstr = NULL, *p;
565                 int optid, nr_args = 1;
566
567                 if (!optstr) {
568                         i++;
569                         continue;
570                 }
571
572                 optstr = strdup(optstr);
573
574                 p = optstr;
575                 while (isalpha(*p) || *p == '_')
576                         p++;
577
578                 if (*p == '=') {
579                         *p = '\0';
580                         valstr = p + 1;
581                 }
582
583                 optid = bch2_opt_lookup(optstr);
584                 if (optid < 0 ||
585                     !(bch2_opt_table[optid].flags & opt_types)) {
586                         i++;
587                         goto next;
588                 }
589
590                 if (!valstr &&
591                     bch2_opt_table[optid].type != BCH_OPT_BOOL) {
592                         nr_args = 2;
593                         valstr = argv[i + 1];
594                 }
595
596                 if (!valstr)
597                         valstr = "1";
598
599                 opts.by_id[optid] = strdup(valstr);
600
601                 *argc -= nr_args;
602                 memmove(&argv[i],
603                         &argv[i + nr_args],
604                         sizeof(char *) * (*argc - i));
605                 argv[*argc] = NULL;
606 next:
607                 free(optstr);
608         }
609
610         return opts;
611 }
612
613 struct bch_opts bch2_parse_opts(struct bch_opt_strs strs)
614 {
615         struct bch_opts opts = bch2_opts_empty();
616         struct printbuf err = PRINTBUF;
617         unsigned i;
618         int ret;
619         u64 v;
620
621         for (i = 0; i < bch2_opts_nr; i++) {
622                 if (!strs.by_id[i])
623                         continue;
624
625                 ret = bch2_opt_parse(NULL,
626                                      &bch2_opt_table[i],
627                                      strs.by_id[i], &v, &err);
628                 if (ret < 0)
629                         die("Invalid option %s", err.buf);
630
631                 bch2_opt_set_by_id(&opts, i, v);
632         }
633
634         printbuf_exit(&err);
635         return opts;
636 }
637
638 #define newline(c)              \
639         do {                    \
640                 printf("\n");   \
641                 c = 0;          \
642         } while(0)
643 void bch2_opts_usage(unsigned opt_types)
644 {
645         const struct bch_option *opt;
646         unsigned i, c = 0, helpcol = 30;
647
648
649
650         for (opt = bch2_opt_table;
651              opt < bch2_opt_table + bch2_opts_nr;
652              opt++) {
653                 if (!(opt->flags & opt_types))
654                         continue;
655
656                 c += printf("      --%s", opt->attr.name);
657
658                 switch (opt->type) {
659                 case BCH_OPT_BOOL:
660                         break;
661                 case BCH_OPT_STR:
662                         c += printf("=(");
663                         for (i = 0; opt->choices[i]; i++) {
664                                 if (i)
665                                         c += printf("|");
666                                 c += printf("%s", opt->choices[i]);
667                         }
668                         c += printf(")");
669                         break;
670                 default:
671                         c += printf("=%s", opt->hint);
672                         break;
673                 }
674
675                 if (opt->help) {
676                         const char *l = opt->help;
677
678                         if (c >= helpcol)
679                                 newline(c);
680
681                         while (1) {
682                                 const char *n = strchrnul(l, '\n');
683
684                                 while (c < helpcol) {
685                                         putchar(' ');
686                                         c++;
687                                 }
688                                 printf("%.*s", (int) (n - l), l);
689                                 newline(c);
690
691                                 if (!*n)
692                                         break;
693                                 l = n + 1;
694                         }
695                 } else {
696                         newline(c);
697                 }
698         }
699 }
700
701 dev_names bchu_fs_get_devices(struct bchfs_handle fs)
702 {
703         DIR *dir = fdopendir(fs.sysfs_fd);
704         struct dirent *d;
705         dev_names devs;
706
707         darray_init(&devs);
708
709         while ((errno = 0), (d = readdir(dir))) {
710                 struct dev_name n = { 0, NULL, NULL };
711
712                 if (sscanf(d->d_name, "dev-%u", &n.idx) != 1)
713                         continue;
714
715                 char *block_attr = mprintf("dev-%u/block", n.idx);
716
717                 char sysfs_block_buf[4096];
718                 ssize_t r = readlinkat(fs.sysfs_fd, block_attr,
719                                        sysfs_block_buf, sizeof(sysfs_block_buf));
720                 if (r > 0) {
721                         sysfs_block_buf[r] = '\0';
722                         n.dev = strdup(basename(sysfs_block_buf));
723                 }
724
725                 free(block_attr);
726
727                 char *label_attr = mprintf("dev-%u/label", n.idx);
728                 n.label = read_file_str(fs.sysfs_fd, label_attr);
729                 free(label_attr);
730
731                 darray_push(&devs, n);
732         }
733
734         closedir(dir);
735
736         return devs;
737 }