Fix inode link count checks in btrfsck
[btrfs-progs-unstable.git] / btrfs-show.c
blobc49626ce96a90150068e26fbb2924373d6585444
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
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"
41 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
43 struct list_head *cur;
44 struct btrfs_device *device;
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;
52 return 0;
55 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
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;
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 ");
72 super_bytes_used = pretty_sizes(device->super_bytes_used);
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);
78 free(super_bytes_used);
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++;
93 if (devs_found < total) {
94 printf("\t*** Some devices missing\n");
96 printf("\n");
99 static void print_usage(void)
101 fprintf(stderr, "usage: btrfs-show [search label or device]\n");
102 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
103 exit(1);
106 static struct option long_options[] = {
107 /* { "byte-count", 1, NULL, 'b' }, */
108 { 0, 0, 0, 0}
111 int main(int ac, char **av)
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;
120 while(1) {
121 int c;
122 c = getopt_long(ac, av, "", long_options,
123 &option_index);
124 if (c < 0)
125 break;
126 switch(c) {
127 default:
128 print_usage();
131 ac = ac - optind;
132 if (ac != 0) {
133 search = av[optind];
136 ret = btrfs_scan_one_dir("/dev", 0);
137 if (ret)
138 fprintf(stderr, "error %d while scanning\n", ret);
140 all_uuids = btrfs_scanned_uuids();
141 list_for_each(cur_uuid, all_uuids) {
142 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
143 list);
144 if (search && uuid_search(fs_devices, search) == 0)
145 continue;
146 print_one_uuid(fs_devices);
148 printf("%s\n", BTRFS_BUILD_VERSION);
149 return 0;