forked from kdave/btrfs-progs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds-device.c
623 lines (541 loc) · 14.1 KB
/
cmds-device.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/stat.h>
#include <getopt.h>
#include "kerncompat.h"
#include "ctree.h"
#include "ioctl.h"
#include "utils.h"
#include "cmds-fi-usage.h"
#include "commands.h"
static const char * const device_cmd_group_usage[] = {
"btrfs device <command> [<args>]",
NULL
};
static const char * const cmd_add_dev_usage[] = {
"btrfs device add [options] <device> [<device>...] <path>",
"Add a device to a filesystem",
"-K|--nodiscard do not perform whole device TRIM",
"-f|--force force overwrite existing filesystem on the disk",
NULL
};
static int cmd_add_dev(int argc, char **argv)
{
char *mntpnt;
int i, fdmnt, ret=0, e;
DIR *dirstream = NULL;
int discard = 1;
int force = 0;
while (1) {
int c;
static const struct option long_options[] = {
{ "nodiscard", optional_argument, NULL, 'K'},
{ "force", no_argument, NULL, 'f'},
{ NULL, 0, NULL, 0}
};
c = getopt_long(argc, argv, "Kf", long_options, NULL);
if (c < 0)
break;
switch (c) {
case 'K':
discard = 0;
break;
case 'f':
force = 1;
break;
default:
usage(cmd_add_dev_usage);
}
}
argc = argc - optind;
if (check_argc_min(argc, 2))
usage(cmd_add_dev_usage);
mntpnt = argv[optind + argc - 1];
fdmnt = open_file_or_dir(mntpnt, &dirstream);
if (fdmnt < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
return 1;
}
for (i = optind; i < optind + argc - 1; i++){
struct btrfs_ioctl_vol_args ioctl_args;
int devfd, res;
u64 dev_block_count = 0;
int mixed = 0;
char *path;
res = test_dev_for_mkfs(argv[i], force);
if (res) {
ret++;
continue;
}
devfd = open(argv[i], O_RDWR);
if (devfd < 0) {
fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]);
ret++;
continue;
}
res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
0, &mixed, discard);
close(devfd);
if (res) {
ret++;
goto error_out;
}
path = canonicalize_path(argv[i]);
if (!path) {
fprintf(stderr,
"ERROR: Could not canonicalize pathname '%s': %s\n",
argv[i], strerror(errno));
ret++;
goto error_out;
}
memset(&ioctl_args, 0, sizeof(ioctl_args));
strncpy_null(ioctl_args.name, path);
res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
e = errno;
if (res < 0) {
fprintf(stderr, "ERROR: error adding the device '%s' - %s\n",
path, strerror(e));
ret++;
}
free(path);
}
error_out:
close_file_or_dir(fdmnt, dirstream);
return !!ret;
}
static int _cmd_rm_dev(int argc, char **argv, const char * const *usagestr)
{
char *mntpnt;
int i, fdmnt, ret=0, e;
DIR *dirstream = NULL;
if (check_argc_min(argc, 3))
usage(usagestr);
mntpnt = argv[argc - 1];
fdmnt = open_file_or_dir(mntpnt, &dirstream);
if (fdmnt < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n", mntpnt);
return 1;
}
for(i=1 ; i < argc - 1; i++ ){
struct btrfs_ioctl_vol_args arg;
int res;
if (!is_block_device(argv[i])) {
fprintf(stderr,
"ERROR: %s is not a block device\n", argv[i]);
ret++;
continue;
}
memset(&arg, 0, sizeof(arg));
strncpy_null(arg.name, argv[i]);
res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
e = errno;
if (res) {
const char *msg;
if (res > 0)
msg = btrfs_err_str(res);
else
msg = strerror(e);
fprintf(stderr,
"ERROR: error removing the device '%s' - %s\n",
argv[i], msg);
ret++;
}
}
close_file_or_dir(fdmnt, dirstream);
return !!ret;
}
static const char * const cmd_rm_dev_usage[] = {
"btrfs device remove <device> [<device>...] <path>",
"Remove a device from a filesystem",
NULL
};
static int cmd_rm_dev(int argc, char **argv)
{
return _cmd_rm_dev(argc, argv, cmd_rm_dev_usage);
}
static const char * const cmd_del_dev_usage[] = {
"btrfs device delete <device> [<device>...] <path>",
"Remove a device from a filesystem",
NULL
};
static int cmd_del_dev(int argc, char **argv)
{
return _cmd_rm_dev(argc, argv, cmd_del_dev_usage);
}
static const char * const cmd_scan_dev_usage[] = {
"btrfs device scan [(-d|--all-devices)|<device> [<device>...]]",
"Scan devices for a btrfs filesystem",
" -d|--all-devices (deprecated)",
NULL
};
static int cmd_scan_dev(int argc, char **argv)
{
int i;
int devstart = 1;
int all = 0;
int ret = 0;
optind = 1;
while (1) {
int c;
static const struct option long_options[] = {
{ "all-devices", no_argument, NULL, 'd'},
{ NULL, 0, NULL, 0}
};
c = getopt_long(argc, argv, "d", long_options, NULL);
if (c < 0)
break;
switch (c) {
case 'd':
all = 1;
break;
default:
usage(cmd_scan_dev_usage);
}
}
if (all && check_argc_max(argc, 2))
usage(cmd_scan_dev_usage);
if (all || argc == 1) {
printf("Scanning for Btrfs filesystems\n");
ret = btrfs_scan_lblkid();
if (ret)
fprintf(stderr, "ERROR: error %d while scanning\n", ret);
ret = btrfs_register_all_devices();
if (ret)
fprintf(stderr, "ERROR: error %d while registering\n", ret);
goto out;
}
for( i = devstart ; i < argc ; i++ ){
char *path;
if (!is_block_device(argv[i])) {
fprintf(stderr,
"ERROR: %s is not a block device\n", argv[i]);
ret = 1;
goto out;
}
path = canonicalize_path(argv[i]);
if (!path) {
fprintf(stderr,
"ERROR: Could not canonicalize path '%s': %s\n",
argv[i], strerror(errno));
ret = 1;
goto out;
}
printf("Scanning for Btrfs filesystems in '%s'\n", path);
if (btrfs_register_one_device(path) != 0) {
ret = 1;
free(path);
goto out;
}
free(path);
}
out:
return !!ret;
}
static const char * const cmd_ready_dev_usage[] = {
"btrfs device ready <device>",
"Check device to see if it has all of its devices in cache for mounting",
NULL
};
static int cmd_ready_dev(int argc, char **argv)
{
struct btrfs_ioctl_vol_args args;
int fd;
int ret;
char *path;
if (check_argc_min(argc, 2))
usage(cmd_ready_dev_usage);
fd = open("/dev/btrfs-control", O_RDWR);
if (fd < 0) {
perror("failed to open /dev/btrfs-control");
return 1;
}
path = canonicalize_path(argv[argc - 1]);
if (!path) {
fprintf(stderr,
"ERROR: Could not canonicalize pathname '%s': %s\n",
argv[argc - 1], strerror(errno));
ret = 1;
goto out;
}
if (!is_block_device(path)) {
fprintf(stderr,
"ERROR: %s is not a block device\n", path);
ret = 1;
goto out;
}
memset(&args, 0, sizeof(args));
strncpy_null(args.name, path);
ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
if (ret < 0) {
fprintf(stderr, "ERROR: unable to determine if the device '%s'"
" is ready for mounting - %s\n", path,
strerror(errno));
ret = 1;
}
out:
free(path);
close(fd);
return ret;
}
static const char * const cmd_dev_stats_usage[] = {
"btrfs device stats [-z] <path>|<device>",
"Show current device IO stats. -z to reset stats afterwards.",
NULL
};
static int cmd_dev_stats(int argc, char **argv)
{
char *dev_path;
struct btrfs_ioctl_fs_info_args fi_args;
struct btrfs_ioctl_dev_info_args *di_args = NULL;
int ret;
int fdmnt;
int i;
int c;
int err = 0;
__u64 flags = 0;
DIR *dirstream = NULL;
optind = 1;
while ((c = getopt(argc, argv, "z")) != -1) {
switch (c) {
case 'z':
flags = BTRFS_DEV_STATS_RESET;
break;
case '?':
default:
usage(cmd_dev_stats_usage);
}
}
argc = argc - optind;
if (check_argc_exact(argc, 1))
usage(cmd_dev_stats_usage);
dev_path = argv[optind];
fdmnt = open_path_or_dev_mnt(dev_path, &dirstream);
if (fdmnt < 0) {
if (errno == EINVAL)
fprintf(stderr,
"ERROR: '%s' is not a mounted btrfs device\n",
dev_path);
else
fprintf(stderr, "ERROR: can't access '%s': %s\n",
dev_path, strerror(errno));
return 1;
}
ret = get_fs_info(dev_path, &fi_args, &di_args);
if (ret) {
fprintf(stderr, "ERROR: getting dev info for devstats failed: "
"%s\n", strerror(-ret));
err = 1;
goto out;
}
if (!fi_args.num_devices) {
fprintf(stderr, "ERROR: no devices found\n");
err = 1;
goto out;
}
for (i = 0; i < fi_args.num_devices; i++) {
struct btrfs_ioctl_get_dev_stats args = {0};
__u8 path[BTRFS_DEVICE_PATH_NAME_MAX + 1];
strncpy((char *)path, (char *)di_args[i].path,
BTRFS_DEVICE_PATH_NAME_MAX);
path[BTRFS_DEVICE_PATH_NAME_MAX] = '\0';
args.devid = di_args[i].devid;
args.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
args.flags = flags;
if (ioctl(fdmnt, BTRFS_IOC_GET_DEV_STATS, &args) < 0) {
fprintf(stderr,
"ERROR: ioctl(BTRFS_IOC_GET_DEV_STATS) on %s failed: %s\n",
path, strerror(errno));
err = 1;
} else {
char *canonical_path;
canonical_path = canonicalize_path((char *)path);
if (args.nr_items >= BTRFS_DEV_STAT_WRITE_ERRS + 1)
printf("[%s].write_io_errs %llu\n",
canonical_path,
(unsigned long long) args.values[
BTRFS_DEV_STAT_WRITE_ERRS]);
if (args.nr_items >= BTRFS_DEV_STAT_READ_ERRS + 1)
printf("[%s].read_io_errs %llu\n",
canonical_path,
(unsigned long long) args.values[
BTRFS_DEV_STAT_READ_ERRS]);
if (args.nr_items >= BTRFS_DEV_STAT_FLUSH_ERRS + 1)
printf("[%s].flush_io_errs %llu\n",
canonical_path,
(unsigned long long) args.values[
BTRFS_DEV_STAT_FLUSH_ERRS]);
if (args.nr_items >= BTRFS_DEV_STAT_CORRUPTION_ERRS + 1)
printf("[%s].corruption_errs %llu\n",
canonical_path,
(unsigned long long) args.values[
BTRFS_DEV_STAT_CORRUPTION_ERRS]);
if (args.nr_items >= BTRFS_DEV_STAT_GENERATION_ERRS + 1)
printf("[%s].generation_errs %llu\n",
canonical_path,
(unsigned long long) args.values[
BTRFS_DEV_STAT_GENERATION_ERRS]);
free(canonical_path);
}
}
out:
free(di_args);
close_file_or_dir(fdmnt, dirstream);
return err;
}
const char * const cmd_device_usage_usage[] = {
"btrfs device usage [options] <path> [<path>..]",
"Show detailed information about internal allocations in devices.",
"-b|--raw raw numbers in bytes",
"-h|--human-readable",
" human friendly numbers, base 1024 (default)",
"-H human friendly numbers, base 1000",
"--iec use 1024 as a base (KiB, MiB, GiB, TiB)",
"--si use 1000 as a base (kB, MB, GB, TB)",
"-k|--kbytes show sizes in KiB, or kB with --si",
"-m|--mbytes show sizes in MiB, or MB with --si",
"-g|--gbytes show sizes in GiB, or GB with --si",
"-t|--tbytes show sizes in TiB, or TB with --si",
NULL
};
static int _cmd_device_usage(int fd, char *path, unsigned unit_mode)
{
int i;
int ret = 0;
struct chunk_info *chunkinfo = NULL;
struct device_info *devinfo = NULL;
int chunkcount = 0;
int devcount = 0;
ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount, &devinfo,
&devcount);
if (ret)
goto out;
for (i = 0; i < devcount; i++) {
printf("%s, ID: %llu\n", devinfo[i].path, devinfo[i].devid);
print_device_sizes(fd, &devinfo[i], unit_mode);
print_device_chunks(fd, &devinfo[i], chunkinfo, chunkcount,
unit_mode);
printf("\n");
}
out:
free(devinfo);
free(chunkinfo);
return ret;
}
int cmd_device_usage(int argc, char **argv)
{
unsigned unit_mode = UNITS_DEFAULT;
int ret = 0;
int i, more_than_one = 0;
optind = 1;
while (1) {
int c;
static const struct option long_options[] = {
{ "raw", no_argument, NULL, 'b'},
{ "kbytes", no_argument, NULL, 'k'},
{ "mbytes", no_argument, NULL, 'm'},
{ "gbytes", no_argument, NULL, 'g'},
{ "tbytes", no_argument, NULL, 't'},
{ "si", no_argument, NULL, GETOPT_VAL_SI},
{ "iec", no_argument, NULL, GETOPT_VAL_IEC},
{ "human-readable", no_argument, NULL,
GETOPT_VAL_HUMAN_READABLE},
{ NULL, 0, NULL, 0 }
};
c = getopt_long(argc, argv, "bhHkmgt", long_options, NULL);
if (c < 0)
break;
switch (c) {
case 'b':
unit_mode = UNITS_RAW;
break;
case 'k':
units_set_base(&unit_mode, UNITS_KBYTES);
break;
case 'm':
units_set_base(&unit_mode, UNITS_MBYTES);
break;
case 'g':
units_set_base(&unit_mode, UNITS_GBYTES);
break;
case 't':
units_set_base(&unit_mode, UNITS_TBYTES);
break;
case GETOPT_VAL_HUMAN_READABLE:
case 'h':
unit_mode = UNITS_HUMAN_BINARY;
break;
case 'H':
unit_mode = UNITS_HUMAN_DECIMAL;
break;
case GETOPT_VAL_SI:
units_set_mode(&unit_mode, UNITS_DECIMAL);
break;
case GETOPT_VAL_IEC:
units_set_mode(&unit_mode, UNITS_BINARY);
break;
default:
usage(cmd_device_usage_usage);
}
}
if (check_argc_min(argc - optind, 1))
usage(cmd_device_usage_usage);
for (i = optind; i < argc ; i++) {
int fd;
DIR *dirstream = NULL;
if (more_than_one)
printf("\n");
fd = open_file_or_dir(argv[i], &dirstream);
if (fd < 0) {
fprintf(stderr, "ERROR: can't access '%s'\n",
argv[1]);
ret = 1;
goto out;
}
ret = _cmd_device_usage(fd, argv[i], unit_mode);
close_file_or_dir(fd, dirstream);
if (ret)
goto out;
more_than_one = 1;
}
out:
return !!ret;
}
static const char device_cmd_group_info[] =
"manage and query devices in the filesystem";
const struct cmd_group device_cmd_group = {
device_cmd_group_usage, device_cmd_group_info, {
{ "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 },
{ "delete", cmd_del_dev, cmd_del_dev_usage, NULL, CMD_ALIAS },
{ "remove", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 },
{ "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 },
{ "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 },
{ "stats", cmd_dev_stats, cmd_dev_stats_usage, NULL, 0 },
{ "usage", cmd_device_usage,
cmd_device_usage_usage, NULL, 0 },
NULL_CMD_STRUCT
}
};
int cmd_device(int argc, char **argv)
{
return handle_command_group(&device_cmd_group, argc, argv);
}