252
static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
255
char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
256
int search_len = strlen(search);
258
search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
259
uuid_unparse(fsid, uuidbuf);
260
if (!strncmp(uuidbuf, search, search_len))
263
if (strlen(label) && strcmp(label, search) == 0)
266
if (strcmp(mnt, search) == 0)
251
272
static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
274
char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
254
275
struct list_head *cur;
255
276
struct btrfs_device *device;
256
277
int search_len = strlen(search);
258
search_len = min(search_len, 37);
279
search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
259
280
uuid_unparse(fs_devices->fsid, uuidbuf);
260
281
if (!strncmp(uuidbuf, search, search_len))
356
379
uuid_unparse(fs_info->fsid, uuidbuf);
357
printf("Label: %s uuid: %s\n",
358
strlen(label) ? label : "none", uuidbuf);
380
if (label && strlen(label))
381
printf("Label: '%s' ", label);
383
printf("Label: none ");
360
printf("\tTotal devices %llu FS bytes used %s\n",
361
fs_info->num_devices,
385
printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
386
fs_info->num_devices,
362
387
pretty_size(calc_used_bytes(space_info)));
364
389
for (i = 0; i < fs_info->num_devices; i++) {
365
390
tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
366
printf("\tdevid %llu size %s used %s path %s\n",
392
/* Add check for missing devices even mounted */
393
fd = open((char *)tmp_dev_info->path, O_RDONLY);
399
printf("\tdevid %4llu size %s used %s path %s\n",
367
400
tmp_dev_info->devid,
368
401
pretty_size(tmp_dev_info->total_bytes),
369
402
pretty_size(tmp_dev_info->bytes_used),
370
403
tmp_dev_info->path);
407
printf("\t*** Some devices missing\n");
387
422
char path[PATH_MAX];
390
return BTRFS_ARG_UNKNOWN;
392
427
if (realpath(input, path)) {
393
if (is_block_device(input) == 1)
428
if (is_block_device(path) == 1)
394
429
return BTRFS_ARG_BLKDEV;
396
if (is_mount_point(input) == 1)
431
if (is_mount_point(path) == 1)
397
432
return BTRFS_ARG_MNTPOINT;
399
434
return BTRFS_ARG_UNKNOWN;
402
if (!uuid_parse(input, out))
437
if (strlen(input) == (BTRFS_UUID_UNPARSED_SIZE - 1) &&
438
!uuid_parse(input, out))
403
439
return BTRFS_ARG_UUID;
405
441
return BTRFS_ARG_UNKNOWN;
408
444
static int btrfs_scan_kernel(void *search)
410
int ret = 0, fd, type;
412
448
struct mntent *mnt;
413
449
struct btrfs_ioctl_fs_info_args fs_info_arg;
414
450
struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
415
struct btrfs_ioctl_space_args *space_info_arg;
451
struct btrfs_ioctl_space_args *space_info_arg = NULL;
416
452
char label[BTRFS_LABEL_SIZE];
419
454
f = setmntent("/proc/self/mounts", "r");
423
type = check_arg_type(search);
424
if (type == BTRFS_ARG_BLKDEV)
458
memset(label, 0, sizeof(label));
427
459
while ((mnt = getmntent(f)) != NULL) {
428
460
if (strcmp(mnt->mnt_type, "btrfs"))
430
462
ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
437
ret = uuid_parse(search, uuid);
440
if (uuid_compare(fs_info_arg.fsid, uuid))
443
case BTRFS_ARG_MNTPOINT:
444
if (strcmp(search, mnt->mnt_dir))
447
case BTRFS_ARG_UNKNOWN:
467
if (get_label_mounted(mnt->mnt_dir, label)) {
472
if (search && !match_search_item_kernel(fs_info_arg.fsid,
473
mnt->mnt_dir, label, search)) {
451
478
fd = open(mnt->mnt_dir, O_RDONLY);
452
479
if ((fd != -1) && !get_df(fd, &space_info_arg)) {
453
get_label_mounted(mnt->mnt_dir, label);
454
480
print_one_fs(&fs_info_arg, dev_info_arg,
455
481
space_info_arg, label, mnt->mnt_dir);
456
free(space_info_arg);
482
kfree(space_info_arg);
483
memset(label, 0, sizeof(label));
499
static int dev_to_fsid(char *dev, __u8 *fsid)
501
struct btrfs_super_block *disk_super;
510
fd = open(dev, O_RDONLY);
517
disk_super = (struct btrfs_super_block *)buf;
518
ret = btrfs_read_dev_super(fd, disk_super,
519
BTRFS_SUPER_INFO_OFFSET);
523
memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
465
532
static const char * const cmd_show_usage[] = {
466
"btrfs filesystem show [options|<path>|<uuid>]",
533
"btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
467
534
"Show the structure of a filesystem",
468
535
"-d|--all-devices show only disks under /dev containing btrfs filesystem",
469
536
"-m|--mounted show only mounted btrfs",
508
if (where == BTRFS_SCAN_LBLKID) {
509
if (check_argc_max(argc, optind + 1))
510
usage(cmd_show_usage);
512
if (check_argc_max(argc, optind))
513
usage(cmd_show_usage);
579
if (check_argc_max(argc, optind + 1))
580
usage(cmd_show_usage);
515
582
if (argc > optind) {
516
583
search = argv[optind];
584
if (strlen(search) == 0)
585
usage(cmd_show_usage);
517
586
type = check_arg_type(search);
518
if (type == BTRFS_ARG_UNKNOWN) {
519
fprintf(stderr, "ERROR: arg type unknown\n");
520
usage(cmd_show_usage);
588
* needs spl handling if input arg is block dev
589
* And if input arg is mount-point just print it
522
592
if (type == BTRFS_ARG_BLKDEV) {
523
ret = get_btrfs_mount(search, mp, sizeof(mp));
593
if (where == BTRFS_SCAN_DEV) {
594
/* we need to do this because
595
* legacy BTRFS_SCAN_DEV
596
* provides /dev/dm-x paths
598
if (realpath(search, path))
601
ret = get_btrfs_mount(search,
604
/* given block dev is mounted*/
606
type = BTRFS_ARG_MNTPOINT;
608
ret = dev_to_fsid(search, fsid);
611
"ERROR: No btrfs on %s\n",
615
uuid_unparse(fsid, uuid_buf);
617
type = BTRFS_ARG_UUID;
554
653
print_one_uuid(fs_devices);
656
if (search && !found)
659
while (!list_empty(all_uuids)) {
660
fs_devices = list_entry(all_uuids->next,
661
struct btrfs_fs_devices, list);
662
list_del(&fs_devices->list);
663
btrfs_close_devices(fs_devices);
558
666
printf("%s\n", BTRFS_BUILD_VERSION);
559
667
free_seen_fsid();
563
671
static const char * const cmd_sync_usage[] = {
656
764
ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
659
if (ret && e == ENOTTY) {
767
if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
660
768
fprintf(stderr, "ERROR: defrag range ioctl not "
661
769
"supported in this kernel, please try "
662
770
"without any options.\n");
757
867
close_file_or_dir(fd, dirstream);
870
if (fstat(fd, &st)) {
871
fprintf(stderr, "ERROR: failed to stat %s - %s\n",
872
argv[i], strerror(errno));
873
defrag_global_errors++;
874
close_file_or_dir(fd, dirstream);
877
if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
879
"ERROR: %s is not a directory or a regular file\n",
881
defrag_global_errors++;
882
close_file_or_dir(fd, dirstream);
763
if (fstat(fd, &st)) {
764
fprintf(stderr, "ERROR: failed to stat %s - %s\n",
765
argv[i], strerror(errno));
766
defrag_global_errors++;
767
close_file_or_dir(fd, dirstream);
770
886
if (S_ISDIR(st.st_mode)) {
771
887
ret = nftw(argv[i], defrag_callback, 10,
772
888
FTW_MOUNT | FTW_PHYS);
791
907
close_file_or_dir(fd, dirstream);
792
if (ret && e == ENOTTY) {
908
if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
793
909
fprintf(stderr, "ERROR: defrag range ioctl not "
794
910
"supported in this kernel, please try "
795
911
"without any options.\n");
870
986
if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
871
987
usage(cmd_label_usage);
874
990
return set_label(argv[1], argv[2]);
876
return get_label(argv[1]);
992
char label[BTRFS_LABEL_SIZE];
995
ret = get_label(argv[1], label);
997
fprintf(stdout, "%s\n", label);
879
1003
const struct cmd_group filesystem_cmd_group = {