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