btrfs-progs: fix scrub error return from pthread_mutex_lock
[platform/upstream/btrfs-progs.git] / btrfs-show.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _GNU_SOURCE
20 #ifndef __CHECKER__
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
23 #include "ioctl.h"
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <getopt.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <dirent.h>
33 #include <uuid/uuid.h>
34 #include "kerncompat.h"
35 #include "ctree.h"
36 #include "transaction.h"
37 #include "utils.h"
38 #include "volumes.h"
39 #include "version.h"
40
41 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
42 {
43         struct list_head *cur;
44         struct btrfs_device *device;
45
46         list_for_each(cur, &fs_devices->devices) {
47                 device = list_entry(cur, struct btrfs_device, dev_list);
48                 if ((device->label && strcmp(device->label, search) == 0) ||
49                     strcmp(device->name, search) == 0)
50                         return 1;
51         }
52         return 0;
53 }
54
55 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
56 {
57         char uuidbuf[37];
58         struct list_head *cur;
59         struct btrfs_device *device;
60         char *super_bytes_used;
61         u64 devs_found = 0;
62         u64 total;
63
64         uuid_unparse(fs_devices->fsid, uuidbuf);
65         device = list_entry(fs_devices->devices.next, struct btrfs_device,
66                             dev_list);
67         if (device->label && device->label[0])
68                 printf("Label: %s ", device->label);
69         else
70                 printf("Label: none ");
71
72         super_bytes_used = pretty_sizes(device->super_bytes_used);
73
74         total = device->total_devs;
75         printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
76                (unsigned long long)total, super_bytes_used);
77
78         free(super_bytes_used);
79
80         list_for_each(cur, &fs_devices->devices) {
81                 char *total_bytes;
82                 char *bytes_used;
83                 device = list_entry(cur, struct btrfs_device, dev_list);
84                 total_bytes = pretty_sizes(device->total_bytes);
85                 bytes_used = pretty_sizes(device->bytes_used);
86                 printf("\tdevid %4llu size %s used %s path %s\n",
87                        (unsigned long long)device->devid,
88                        total_bytes, bytes_used, device->name);
89                 free(total_bytes);
90                 free(bytes_used);
91                 devs_found++;
92         }
93         if (devs_found < total) {
94                 printf("\t*** Some devices missing\n");
95         }
96         printf("\n");
97 }
98
99 static void print_usage(void)
100 {
101         fprintf(stderr, "usage: btrfs-show [search label or device]\n");
102         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
103         exit(1);
104 }
105
106 static struct option long_options[] = {
107         /* { "byte-count", 1, NULL, 'b' }, */
108         { 0, 0, 0, 0}
109 };
110
111 int main(int ac, char **av)
112 {
113         struct list_head *all_uuids;
114         struct btrfs_fs_devices *fs_devices;
115         struct list_head *cur_uuid;
116         char *search = NULL;
117         int ret;
118         int option_index = 0;
119
120         printf( "**\n"
121                 "** WARNING: this program is considered deprecated\n"
122                 "** Please consider to switch to the btrfs utility\n"
123                 "**\n");
124
125         while(1) {
126                 int c;
127                 c = getopt_long(ac, av, "", long_options,
128                                 &option_index);
129                 if (c < 0)
130                         break;
131                 switch(c) {
132                         default:
133                                 print_usage();
134                 }
135         }
136         ac = ac - optind;
137         if (ac != 0) {
138                 search = av[optind];
139         }
140
141         ret = btrfs_scan_one_dir("/dev", 0);
142         if (ret)
143                 fprintf(stderr, "error %d while scanning\n", ret);
144
145         all_uuids = btrfs_scanned_uuids();
146         list_for_each(cur_uuid, all_uuids) {
147                 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
148                                         list);
149                 if (search && uuid_search(fs_devices, search) == 0)
150                         continue;
151                 print_one_uuid(fs_devices);
152         }
153         printf("%s\n", BTRFS_BUILD_VERSION);
154         return 0;
155 }
156