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