btrfs-progs: fix wrong num_devices for btrfs fi show with seed devices
[platform/upstream/btrfs-progs.git] / cmds-fi-disk_usage.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/ioctl.h>
22 #include <errno.h>
23 #include <stdarg.h>
24
25 #include "utils.h"
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "string-table.h"
29 #include "cmds-fi-disk_usage.h"
30 #include "commands.h"
31
32 #include "version.h"
33
34 /*
35  * Add the chunk info to the chunk_info list
36  */
37 static int add_info_to_list(struct chunk_info **info_ptr,
38                         int *info_count,
39                         struct btrfs_chunk *chunk)
40 {
41
42         u64 type = btrfs_stack_chunk_type(chunk);
43         u64 size = btrfs_stack_chunk_length(chunk);
44         int num_stripes = btrfs_stack_chunk_num_stripes(chunk);
45         int j;
46
47         for (j = 0 ; j < num_stripes ; j++) {
48                 int i;
49                 struct chunk_info *p = 0;
50                 struct btrfs_stripe *stripe;
51                 u64    devid;
52
53                 stripe = btrfs_stripe_nr(chunk, j);
54                 devid = btrfs_stack_stripe_devid(stripe);
55
56                 for (i = 0 ; i < *info_count ; i++)
57                         if ((*info_ptr)[i].type == type &&
58                             (*info_ptr)[i].devid == devid &&
59                             (*info_ptr)[i].num_stripes == num_stripes ) {
60                                 p = (*info_ptr) + i;
61                                 break;
62                         }
63
64                 if (!p) {
65                         int size = sizeof(struct btrfs_chunk) * (*info_count+1);
66                         struct chunk_info *res = realloc(*info_ptr, size);
67
68                         if (!res) {
69                                 free(*info_ptr);
70                                 fprintf(stderr, "ERROR: not enough memory\n");
71                                 return -ENOMEM;
72                         }
73
74                         *info_ptr = res;
75                         p = res + *info_count;
76                         (*info_count)++;
77
78                         p->devid = devid;
79                         p->type = type;
80                         p->size = 0;
81                         p->num_stripes = num_stripes;
82                 }
83
84                 p->size += size;
85
86         }
87
88         return 0;
89
90 }
91
92 /*
93  *  Helper to sort the chunk type
94  */
95 static int cmp_chunk_block_group(u64 f1, u64 f2)
96 {
97
98         u64 mask;
99
100         if ((f1 & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
101                 (f2 & BTRFS_BLOCK_GROUP_TYPE_MASK))
102                         mask = BTRFS_BLOCK_GROUP_PROFILE_MASK;
103         else if (f2 & BTRFS_BLOCK_GROUP_SYSTEM)
104                         return -1;
105         else if (f1 & BTRFS_BLOCK_GROUP_SYSTEM)
106                         return +1;
107         else
108                         mask = BTRFS_BLOCK_GROUP_TYPE_MASK;
109
110         if ((f1 & mask) > (f2 & mask))
111                 return +1;
112         else if ((f1 & mask) < (f2 & mask))
113                 return -1;
114         else
115                 return 0;
116 }
117
118 /*
119  * Helper to sort the chunk
120  */
121 static int cmp_chunk_info(const void *a, const void *b)
122 {
123         return cmp_chunk_block_group(
124                 ((struct chunk_info *)a)->type,
125                 ((struct chunk_info *)b)->type);
126 }
127
128 static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
129 {
130         int ret;
131         struct btrfs_ioctl_search_args args;
132         struct btrfs_ioctl_search_key *sk = &args.key;
133         struct btrfs_ioctl_search_header *sh;
134         unsigned long off = 0;
135         int i, e;
136
137         memset(&args, 0, sizeof(args));
138
139         /*
140          * there may be more than one ROOT_ITEM key if there are
141          * snapshots pending deletion, we have to loop through
142          * them.
143          */
144         sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
145
146         sk->min_objectid = 0;
147         sk->max_objectid = (u64)-1;
148         sk->max_type = 0;
149         sk->min_type = (u8)-1;
150         sk->min_offset = 0;
151         sk->max_offset = (u64)-1;
152         sk->min_transid = 0;
153         sk->max_transid = (u64)-1;
154         sk->nr_items = 4096;
155
156         while (1) {
157                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
158                 e = errno;
159                 if (e == EPERM)
160                         return -e;
161
162                 if (ret < 0) {
163                         fprintf(stderr,
164                                 "ERROR: can't perform the search - %s\n",
165                                 strerror(e));
166                         return 1;
167                 }
168                 /* the ioctl returns the number of item it found in nr_items */
169
170                 if (sk->nr_items == 0)
171                         break;
172
173                 off = 0;
174                 for (i = 0; i < sk->nr_items; i++) {
175                         struct btrfs_chunk *item;
176                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
177                                                                   off);
178
179                         off += sizeof(*sh);
180                         item = (struct btrfs_chunk *)(args.buf + off);
181
182                         ret = add_info_to_list(info_ptr, info_count, item);
183                         if (ret) {
184                                 *info_ptr = 0;
185                                 return 1;
186                         }
187
188                         off += sh->len;
189
190                         sk->min_objectid = sh->objectid;
191                         sk->min_type = sh->type;
192                         sk->min_offset = sh->offset+1;
193
194                 }
195                 if (!sk->min_offset)    /* overflow */
196                         sk->min_type++;
197                 else
198                         continue;
199
200                 if (!sk->min_type)
201                         sk->min_objectid++;
202                  else
203                         continue;
204
205                 if (!sk->min_objectid)
206                         break;
207         }
208
209         qsort(*info_ptr, *info_count, sizeof(struct chunk_info),
210                 cmp_chunk_info);
211
212         return 0;
213 }
214
215 /*
216  * Helper to sort the struct btrfs_ioctl_space_info
217  */
218 static int cmp_btrfs_ioctl_space_info(const void *a, const void *b)
219 {
220         return cmp_chunk_block_group(
221                 ((struct btrfs_ioctl_space_info *)a)->flags,
222                 ((struct btrfs_ioctl_space_info *)b)->flags);
223 }
224
225 /*
226  * This function load all the information about the space usage
227  */
228 static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path)
229 {
230         struct btrfs_ioctl_space_args *sargs = 0, *sargs_orig = 0;
231         int e, ret, count;
232
233         sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
234         if (!sargs) {
235                 fprintf(stderr, "ERROR: not enough memory\n");
236                 return NULL;
237         }
238
239         sargs->space_slots = 0;
240         sargs->total_spaces = 0;
241
242         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
243         e = errno;
244         if (ret) {
245                 fprintf(stderr,
246                         "ERROR: couldn't get space info on '%s' - %s\n",
247                         path, strerror(e));
248                 free(sargs);
249                 return NULL;
250         }
251         if (!sargs->total_spaces) {
252                 free(sargs);
253                 printf("No chunks found\n");
254                 return NULL;
255         }
256
257         count = sargs->total_spaces;
258
259         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
260                         (count * sizeof(struct btrfs_ioctl_space_info)));
261         if (!sargs) {
262                 free(sargs_orig);
263                 fprintf(stderr, "ERROR: not enough memory\n");
264                 return NULL;
265         }
266
267         sargs->space_slots = count;
268         sargs->total_spaces = 0;
269
270         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
271         e = errno;
272
273         if (ret) {
274                 fprintf(stderr,
275                         "ERROR: couldn't get space info on '%s' - %s\n",
276                         path, strerror(e));
277                 free(sargs);
278                 return NULL;
279         }
280
281         qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
282                 cmp_btrfs_ioctl_space_info);
283
284         return sargs;
285 }
286
287 /*
288  * This function computes the space occuped by a *single* RAID5/RAID6 chunk.
289  * The computation is performed on the basis of the number of stripes
290  * which compose the chunk, which could be different from the number of devices
291  * if a disk is added later.
292  */
293 static void get_raid56_used(int fd, struct chunk_info *chunks, int chunkcount,
294                 u64 *raid5_used, u64 *raid6_used)
295 {
296         struct chunk_info *info_ptr = chunks;
297         *raid5_used = 0;
298         *raid6_used = 0;
299
300         while (chunkcount-- > 0) {
301                 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID5)
302                         (*raid5_used) += info_ptr->size / (info_ptr->num_stripes - 1);
303                 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID6)
304                         (*raid6_used) += info_ptr->size / (info_ptr->num_stripes - 2);
305                 info_ptr++;
306         }
307 }
308
309 #define MIN_UNALOCATED_THRESH   (16 * 1024 * 1024)
310 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
311                 int chunkcount, struct device_info *devinfo, int devcount,
312                 char *path, int mode)
313 {
314         struct btrfs_ioctl_space_args *sargs = 0;
315         int i;
316         int ret = 0;
317         int width = 10;         /* default 10 for human units */
318         /*
319          * r_* prefix is for raw data
320          * l_* is for logical
321          */
322         u64 r_total_size = 0;   /* filesystem size, sum of device sizes */
323         u64 r_total_chunks = 0; /* sum of chunks sizes on disk(s) */
324         u64 r_total_used = 0;
325         u64 r_total_unused = 0;
326         u64 r_data_used = 0;
327         u64 r_data_chunks = 0;
328         u64 l_data_chunks = 0;
329         u64 r_metadata_used = 0;
330         u64 r_metadata_chunks = 0;
331         u64 l_metadata_chunks = 0;
332         u64 r_system_used = 0;
333         u64 r_system_chunks = 0;
334         double data_ratio;
335         double metadata_ratio;
336         /* logical */
337         u64 raid5_used = 0;
338         u64 raid6_used = 0;
339         u64 l_global_reserve = 0;
340         u64 l_global_reserve_used = 0;
341         u64 free_estimated = 0;
342         u64 free_min = 0;
343         int max_data_ratio = 1;
344
345         sargs = load_space_info(fd, path);
346         if (!sargs) {
347                 ret = 1;
348                 goto exit;
349         }
350
351         r_total_size = 0;
352         for (i = 0; i < devcount; i++)
353                 r_total_size += devinfo[i].device_size;
354
355         if (r_total_size == 0) {
356                 fprintf(stderr,
357                         "ERROR: couldn't get space info on '%s' - %s\n",
358                         path, strerror(errno));
359
360                 ret = 1;
361                 goto exit;
362         }
363         get_raid56_used(fd, chunkinfo, chunkcount, &raid5_used, &raid6_used);
364
365         for (i = 0; i < sargs->total_spaces; i++) {
366                 int ratio;
367                 u64 flags = sargs->spaces[i].flags;
368
369                 /*
370                  * The raid5/raid6 ratio depends by the stripes number
371                  * used by every chunk. It is computed separately
372                  */
373                 if (flags & BTRFS_BLOCK_GROUP_RAID0)
374                         ratio = 1;
375                 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
376                         ratio = 2;
377                 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
378                         ratio = 0;
379                 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
380                         ratio = 0;
381                 else if (flags & BTRFS_BLOCK_GROUP_DUP)
382                         ratio = 2;
383                 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
384                         ratio = 2;
385                 else
386                         ratio = 1;
387
388                 if (!ratio)
389                         fprintf(stderr, "WARNING: RAID56 detected, not implemented\n");
390
391                 if (ratio > max_data_ratio)
392                         max_data_ratio = ratio;
393
394                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV) {
395                         l_global_reserve = sargs->spaces[i].total_bytes;
396                         l_global_reserve_used = sargs->spaces[i].used_bytes;
397                 }
398                 if ((flags & (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA))
399                         == (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA)) {
400                         fprintf(stderr, "WARNING: MIXED blockgroups not handled\n");
401                 }
402
403                 if (flags & BTRFS_BLOCK_GROUP_DATA) {
404                         r_data_used += sargs->spaces[i].used_bytes * ratio;
405                         r_data_chunks += sargs->spaces[i].total_bytes * ratio;
406                         l_data_chunks += sargs->spaces[i].total_bytes;
407                 }
408                 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
409                         r_metadata_used += sargs->spaces[i].used_bytes * ratio;
410                         r_metadata_chunks += sargs->spaces[i].total_bytes * ratio;
411                         l_metadata_chunks += sargs->spaces[i].total_bytes;
412                 }
413                 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
414                         r_system_used += sargs->spaces[i].used_bytes * ratio;
415                         r_system_chunks += sargs->spaces[i].total_bytes * ratio;
416                 }
417         }
418
419         r_total_chunks = r_data_chunks + r_metadata_chunks + r_system_chunks;
420         r_total_used = r_data_used + r_metadata_used + r_system_used;
421         r_total_unused = r_total_size - r_total_chunks;
422
423         /* Raw / Logical = raid factor, >= 1 */
424         data_ratio = (double)r_data_chunks / l_data_chunks;
425         metadata_ratio = (double)r_metadata_chunks / l_metadata_chunks;
426
427 #if 0
428         /* add the raid5/6 allocated space */
429         total_chunks += raid5_used + raid6_used;
430 #endif
431
432         /*
433          * We're able to fill at least DATA for the unused space
434          *
435          * With mixed raid levels, this gives a rough estimate but more
436          * accurate than just counting the logical free space
437          * (l_data_chunks - l_data_used)
438          *
439          * In non-mixed case there's no difference.
440          */
441         free_estimated = (r_data_chunks - r_data_used) / data_ratio;
442         free_min = free_estimated;
443
444         /* Chop unallocatable space */
445         /* FIXME: must be applied per device */
446         if (r_total_unused >= MIN_UNALOCATED_THRESH) {
447                 free_estimated += r_total_unused / data_ratio;
448                 /* Match the calculation of 'df', use the highest raid ratio */
449                 free_min += r_total_unused / max_data_ratio;
450         }
451
452         if (mode != UNITS_HUMAN)
453                 width = 18;
454
455         printf("Overall:\n");
456
457         printf("    Device size:\t\t%*s\n", width,
458                 pretty_size_mode(r_total_size, mode));
459         printf("    Device allocated:\t\t%*s\n", width,
460                 pretty_size_mode(r_total_chunks, mode));
461         printf("    Device unallocated:\t\t%*s\n", width,
462                 pretty_size_mode(r_total_unused, mode));
463         printf("    Used:\t\t\t%*s\n", width,
464                 pretty_size_mode(r_total_used, mode));
465         printf("    Free (estimated):\t\t%*s\t(",
466                 width,
467                 pretty_size_mode(free_estimated, mode));
468         printf("min: %s)\n", pretty_size_mode(free_min, mode));
469         printf("    Data ratio:\t\t\t%*.2f\n",
470                 width, data_ratio);
471         printf("    Metadata ratio:\t\t%*.2f\n",
472                 width, metadata_ratio);
473         printf("    Global reserve:\t\t%*s\t(used: %s)\n", width,
474                 pretty_size_mode(l_global_reserve, mode),
475                 pretty_size_mode(l_global_reserve_used, mode));
476
477 exit:
478
479         if (sargs)
480                 free(sargs);
481
482         return ret;
483 }
484
485 /*
486  *  Helper to sort the device_info structure
487  */
488 static int cmp_device_info(const void *a, const void *b)
489 {
490         return strcmp(((struct device_info *)a)->path,
491                         ((struct device_info *)b)->path);
492 }
493
494 /*
495  *  This function loads the device_info structure and put them in an array
496  */
497 static int load_device_info(int fd, struct device_info **device_info_ptr,
498                            int *device_info_count)
499 {
500         int ret, i, ndevs, e;
501         struct btrfs_ioctl_fs_info_args fi_args;
502         struct btrfs_ioctl_dev_info_args dev_info;
503         struct device_info *info;
504
505         *device_info_count = 0;
506         *device_info_ptr = 0;
507
508         ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
509         e = errno;
510         if (e == EPERM)
511                 return -e;
512         if (ret < 0) {
513                 fprintf(stderr, "ERROR: cannot get filesystem info - %s\n",
514                                 strerror(e));
515                 return 1;
516         }
517
518         info = calloc(fi_args.num_devices, sizeof(struct device_info));
519         if (!info) {
520                 fprintf(stderr, "ERROR: not enough memory\n");
521                 return 1;
522         }
523
524         for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
525                 BUG_ON(ndevs >= fi_args.num_devices);
526                 memset(&dev_info, 0, sizeof(dev_info));
527                 ret = get_device_info(fd, i, &dev_info);
528
529                 if (ret == -ENODEV)
530                         continue;
531                 if (ret) {
532                         fprintf(stderr,
533                             "ERROR: cannot get info about device devid=%d\n",
534                             i);
535                         free(info);
536                         return ret;
537                 }
538
539                 info[ndevs].devid = dev_info.devid;
540                 strcpy(info[ndevs].path, (char *)dev_info.path);
541                 info[ndevs].device_size = get_partition_size((char *)dev_info.path);
542                 info[ndevs].size = dev_info.total_bytes;
543                 ++ndevs;
544         }
545
546         BUG_ON(ndevs != fi_args.num_devices);
547         qsort(info, fi_args.num_devices,
548                 sizeof(struct device_info), cmp_device_info);
549
550         *device_info_count = fi_args.num_devices;
551         *device_info_ptr = info;
552
553         return 0;
554 }
555
556 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
557                 int *chunkcount, struct device_info **devinfo, int *devcount)
558 {
559         int ret;
560
561         ret = load_chunk_info(fd, chunkinfo, chunkcount);
562         if (ret == -EPERM) {
563                 fprintf(stderr,
564                         "WARNING: can't read detailed chunk info, RAID5/6 numbers will be incorrect, run as root\n");
565         } else if (ret) {
566                 return ret;
567         }
568
569         ret = load_device_info(fd, devinfo, devcount);
570         if (ret == -EPERM) {
571                 fprintf(stderr,
572                         "WARNING: can't get filesystem info from ioctl(FS_INFO), run as root\n");
573                 ret = 0;
574         }
575
576         return ret;
577 }
578
579 /*
580  *  This function computes the size of a chunk in a disk
581  */
582 static u64 calc_chunk_size(struct chunk_info *ci)
583 {
584         if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
585                 return ci->size / ci->num_stripes;
586         else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
587                 return ci->size ;
588         else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
589                 return ci->size ;
590         else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
591                 return ci->size / (ci->num_stripes -1);
592         else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
593                 return ci->size / (ci->num_stripes -2);
594         else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
595                 return ci->size / ci->num_stripes;
596         return ci->size;
597 }
598
599 /*
600  *  This function print the results of the command "btrfs fi usage"
601  *  in tabular format
602  */
603 static void _cmd_filesystem_usage_tabular(int mode,
604                                         struct btrfs_ioctl_space_args *sargs,
605                                         struct chunk_info *chunks_info_ptr,
606                                         int chunks_info_count,
607                                         struct device_info *device_info_ptr,
608                                         int device_info_count)
609 {
610         int i;
611         u64 total_unused = 0;
612         struct string_table *matrix = 0;
613         int  ncols, nrows;
614
615         ncols = sargs->total_spaces + 2;
616         nrows = 2 + 1 + device_info_count + 1 + 2;
617
618         matrix = table_create(ncols, nrows);
619         if (!matrix) {
620                 fprintf(stderr, "ERROR: not enough memory\n");
621                 return;
622         }
623
624         /* header */
625         for (i = 0; i < sargs->total_spaces; i++) {
626                 const char *description;
627                 u64 flags = sargs->spaces[i].flags;
628
629                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
630                         continue;
631
632                 description = btrfs_group_type_str(flags);
633
634                 table_printf(matrix, 1+i, 0, "<%s", description);
635         }
636
637         for (i = 0; i < sargs->total_spaces; i++) {
638                 const char *r_mode;
639
640                 u64 flags = sargs->spaces[i].flags;
641                 r_mode = btrfs_group_profile_str(flags);
642
643                 table_printf(matrix, 1+i, 1, "<%s", r_mode);
644         }
645
646         table_printf(matrix, 1+sargs->total_spaces, 1, "<Unallocated");
647
648         /* body */
649         for (i = 0; i < device_info_count; i++) {
650                 int k, col;
651                 char *p;
652
653                 u64  total_allocated = 0, unused;
654
655                 p = strrchr(device_info_ptr[i].path, '/');
656                 if (!p)
657                         p = device_info_ptr[i].path;
658                 else
659                         p++;
660
661                 table_printf(matrix, 0, i + 3, "<%s", device_info_ptr[i].path);
662
663                 for (col = 1, k = 0 ; k < sargs->total_spaces ; k++)  {
664                         u64     flags = sargs->spaces[k].flags;
665                         u64 devid = device_info_ptr[i].devid;
666                         int     j;
667                         u64 size = 0;
668
669                         for (j = 0 ; j < chunks_info_count ; j++) {
670                                 if (chunks_info_ptr[j].type != flags )
671                                                 continue;
672                                 if (chunks_info_ptr[j].devid != devid)
673                                                 continue;
674
675                                 size += calc_chunk_size(chunks_info_ptr+j);
676                         }
677
678                         if (size)
679                                 table_printf(matrix, col, i+3,
680                                         ">%s", pretty_size_mode(size, mode));
681                         else
682                                 table_printf(matrix, col, i+3, ">-");
683
684                         total_allocated += size;
685                         col++;
686                 }
687
688                 unused = get_partition_size(device_info_ptr[i].path)
689                                 - total_allocated;
690
691                 table_printf(matrix, sargs->total_spaces + 1, i + 3,
692                                ">%s", pretty_size_mode(unused, mode));
693                 total_unused += unused;
694
695         }
696
697         for (i = 0; i <= sargs->total_spaces; i++)
698                 table_printf(matrix, i + 1, device_info_count + 3, "=");
699
700         /* footer */
701         table_printf(matrix, 0, device_info_count + 4, "<Total");
702         for (i = 0; i < sargs->total_spaces; i++)
703                 table_printf(matrix, 1 + i, device_info_count + 4, ">%s",
704                         pretty_size_mode(sargs->spaces[i].total_bytes, mode));
705
706         table_printf(matrix, sargs->total_spaces + 1, device_info_count + 4,
707                         ">%s", pretty_size_mode(total_unused, mode));
708
709         table_printf(matrix, 0, device_info_count + 5, "<Used");
710         for (i = 0; i < sargs->total_spaces; i++)
711                 table_printf(matrix, 1 + i, device_info_count+5, ">%s",
712                         pretty_size_mode(sargs->spaces[i].used_bytes, mode));
713
714         table_dump(matrix);
715         table_free(matrix);
716 }
717
718 /*
719  *  This function prints the unused space per every disk
720  */
721 static void print_unused(struct chunk_info *info_ptr,
722                           int info_count,
723                           struct device_info *device_info_ptr,
724                           int device_info_count,
725                           int mode)
726 {
727         int i;
728         for (i = 0; i < device_info_count; i++) {
729                 int     j;
730                 u64     total = 0;
731
732                 for (j = 0; j < info_count; j++)
733                         if (info_ptr[j].devid == device_info_ptr[i].devid)
734                                 total += calc_chunk_size(info_ptr+j);
735
736                 printf("   %s\t%10s\n",
737                         device_info_ptr[i].path,
738                         pretty_size_mode(device_info_ptr[i].size - total, mode));
739         }
740 }
741
742 /*
743  *  This function prints the allocated chunk per every disk
744  */
745 static void print_chunk_device(u64 chunk_type,
746                                 struct chunk_info *chunks_info_ptr,
747                                 int chunks_info_count,
748                                 struct device_info *device_info_ptr,
749                                 int device_info_count,
750                                 int mode)
751 {
752         int i;
753
754         for (i = 0; i < device_info_count; i++) {
755                 int     j;
756                 u64     total = 0;
757
758                 for (j = 0; j < chunks_info_count; j++) {
759
760                         if (chunks_info_ptr[j].type != chunk_type)
761                                 continue;
762                         if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
763                                 continue;
764
765                         total += calc_chunk_size(&(chunks_info_ptr[j]));
766                         //total += chunks_info_ptr[j].size;
767                 }
768
769                 if (total > 0)
770                         printf("   %s\t%10s\n",
771                                 device_info_ptr[i].path,
772                                 pretty_size_mode(total, mode));
773         }
774 }
775
776 /*
777  *  This function print the results of the command "btrfs fi usage"
778  *  in linear format
779  */
780 static void _cmd_filesystem_usage_linear(int mode,
781                                         struct btrfs_ioctl_space_args *sargs,
782                                         struct chunk_info *info_ptr,
783                                         int info_count,
784                                         struct device_info *device_info_ptr,
785                                         int device_info_count)
786 {
787         int i;
788
789         for (i = 0; i < sargs->total_spaces; i++) {
790                 const char *description;
791                 const char *r_mode;
792                 u64 flags = sargs->spaces[i].flags;
793
794                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
795                         continue;
796
797                 description = btrfs_group_type_str(flags);
798                 r_mode = btrfs_group_profile_str(flags);
799
800                 printf("%s,%s: Size:%s, ",
801                         description,
802                         r_mode,
803                         pretty_size_mode(sargs->spaces[i].total_bytes,
804                             mode));
805                 printf("Used:%s\n",
806                         pretty_size_mode(sargs->spaces[i].used_bytes, mode));
807                 print_chunk_device(flags, info_ptr, info_count,
808                                 device_info_ptr, device_info_count, mode);
809                 printf("\n");
810         }
811
812         printf("Unallocated:\n");
813         print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
814                         mode);
815 }
816
817 static int print_filesystem_usage_by_chunk(int fd,
818                 struct chunk_info *chunkinfo, int chunkcount,
819                 struct device_info *devinfo, int devcount,
820                 char *path, int mode, int tabular)
821 {
822         struct btrfs_ioctl_space_args *sargs;
823         int ret = 0;
824
825         if (!chunkinfo)
826                 return 0;
827
828         sargs = load_space_info(fd, path);
829         if (!sargs) {
830                 ret = 1;
831                 goto out;
832         }
833
834         if (tabular)
835                 _cmd_filesystem_usage_tabular(mode, sargs, chunkinfo,
836                                 chunkcount, devinfo, devcount);
837         else
838                 _cmd_filesystem_usage_linear(mode, sargs, chunkinfo,
839                                 chunkcount, devinfo, devcount);
840
841         free(sargs);
842 out:
843         return ret;
844 }
845
846 const char * const cmd_filesystem_usage_usage[] = {
847         "btrfs filesystem usage [-b][-t] <path> [<path>..]",
848         "Show in which disk the chunks are allocated.",
849         "",
850         "-b\tSet byte as unit",
851         "-t\tShow data in tabular format",
852         NULL
853 };
854
855 int cmd_filesystem_usage(int argc, char **argv)
856 {
857         int mode = UNITS_HUMAN;
858         int ret = 0;
859         int     i, more_than_one = 0;
860         int     tabular = 0;
861
862         optind = 1;
863         while (1) {
864                 int c = getopt(argc, argv, "bt");
865
866                 if (c < 0)
867                         break;
868
869                 switch (c) {
870                 case 'b':
871                         mode = UNITS_RAW;
872                         break;
873                 case 't':
874                         tabular = 1;
875                         break;
876                 default:
877                         usage(cmd_filesystem_usage_usage);
878                 }
879         }
880
881         if (check_argc_min(argc - optind, 1))
882                 usage(cmd_filesystem_usage_usage);
883
884         for (i = optind; i < argc; i++) {
885                 int fd;
886                 DIR *dirstream = NULL;
887                 struct chunk_info *chunkinfo = NULL;
888                 struct device_info *devinfo = NULL;
889                 int chunkcount = 0;
890                 int devcount = 0;
891
892                 fd = open_file_or_dir(argv[i], &dirstream);
893                 if (fd < 0) {
894                         fprintf(stderr, "ERROR: can't access '%s'\n",
895                                 argv[i]);
896                         ret = 1;
897                         goto out;
898                 }
899                 if (more_than_one)
900                         printf("\n");
901
902                 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
903                                 &devinfo, &devcount);
904                 if (ret)
905                         goto cleanup;
906
907                 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
908                                 devinfo, devcount, argv[i], mode);
909                 if (ret)
910                         goto cleanup;
911                 printf("\n");
912                 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
913                                 devinfo, devcount, argv[i], mode, tabular);
914 cleanup:
915                 close_file_or_dir(fd, dirstream);
916                 free(chunkinfo);
917                 free(devinfo);
918
919                 if (ret)
920                         goto out;
921                 more_than_one = 1;
922         }
923
924 out:
925         return !!ret;
926 }
927
928 void print_device_chunks(int fd, struct device_info *devinfo,
929                 struct chunk_info *chunks_info_ptr,
930                 int chunks_info_count, int mode)
931 {
932         int i;
933         u64 allocated = 0;
934
935         for (i = 0 ; i < chunks_info_count ; i++) {
936                 const char *description;
937                 const char *r_mode;
938                 u64 flags;
939                 u64 size;
940
941                 if (chunks_info_ptr[i].devid != devinfo->devid)
942                         continue;
943
944                 flags = chunks_info_ptr[i].type;
945
946                 description = btrfs_group_type_str(flags);
947                 r_mode = btrfs_group_profile_str(flags);
948                 size = calc_chunk_size(chunks_info_ptr+i);
949                 printf("   %s,%s:%*s%10s\n",
950                         description,
951                         r_mode,
952                         (int)(20 - strlen(description) - strlen(r_mode)), "",
953                         pretty_size_mode(size, mode));
954
955                 allocated += size;
956
957         }
958         printf("   Unallocated: %*s%10s\n",
959                 (int)(20 - strlen("Unallocated")), "",
960                 pretty_size_mode(devinfo->size - allocated, mode));
961 }
962
963 void print_device_sizes(int fd, struct device_info *devinfo, int mode)
964 {
965         printf("   Device size: %*s%10s\n",
966                 (int)(20 - strlen("Device size")), "",
967                 pretty_size_mode(devinfo->device_size, mode));
968 #if 0
969         /*
970          * The term has not seen an agreement and we don't want to change it
971          * once it's in non-development branches or even released.
972          */
973         printf("   FS occupied: %*s%10s\n",
974                 (int)(20 - strlen("FS occupied")), "",
975                 pretty_size_mode(devinfo->size, mode));
976 #endif
977 }