btrfs-progs: cmd fi usage: switch to common error message wrapper
[platform/upstream/btrfs-progs.git] / cmds-fi-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 #include <getopt.h>
25
26 #include "utils.h"
27 #include "kerncompat.h"
28 #include "ctree.h"
29 #include "string-table.h"
30 #include "cmds-fi-usage.h"
31 #include "commands.h"
32
33 #include "version.h"
34
35 /*
36  * Add the chunk info to the chunk_info list
37  */
38 static int add_info_to_list(struct chunk_info **info_ptr,
39                         int *info_count,
40                         struct btrfs_chunk *chunk)
41 {
42
43         u64 type = btrfs_stack_chunk_type(chunk);
44         u64 size = btrfs_stack_chunk_length(chunk);
45         int num_stripes = btrfs_stack_chunk_num_stripes(chunk);
46         int j;
47
48         for (j = 0 ; j < num_stripes ; j++) {
49                 int i;
50                 struct chunk_info *p = NULL;
51                 struct btrfs_stripe *stripe;
52                 u64    devid;
53
54                 stripe = btrfs_stripe_nr(chunk, j);
55                 devid = btrfs_stack_stripe_devid(stripe);
56
57                 for (i = 0 ; i < *info_count ; i++)
58                         if ((*info_ptr)[i].type == type &&
59                             (*info_ptr)[i].devid == devid &&
60                             (*info_ptr)[i].num_stripes == num_stripes ) {
61                                 p = (*info_ptr) + i;
62                                 break;
63                         }
64
65                 if (!p) {
66                         int size = sizeof(struct btrfs_chunk) * (*info_count+1);
67                         struct chunk_info *res = realloc(*info_ptr, size);
68
69                         if (!res) {
70                                 free(*info_ptr);
71                                 error("not enough memory");
72                                 return -ENOMEM;
73                         }
74
75                         *info_ptr = res;
76                         p = res + *info_count;
77                         (*info_count)++;
78
79                         p->devid = devid;
80                         p->type = type;
81                         p->size = 0;
82                         p->num_stripes = num_stripes;
83                 }
84
85                 p->size += size;
86
87         }
88
89         return 0;
90
91 }
92
93 /*
94  *  Helper to sort the chunk type
95  */
96 static int cmp_chunk_block_group(u64 f1, u64 f2)
97 {
98
99         u64 mask;
100
101         if ((f1 & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
102                 (f2 & BTRFS_BLOCK_GROUP_TYPE_MASK))
103                         mask = BTRFS_BLOCK_GROUP_PROFILE_MASK;
104         else if (f2 & BTRFS_BLOCK_GROUP_SYSTEM)
105                         return -1;
106         else if (f1 & BTRFS_BLOCK_GROUP_SYSTEM)
107                         return +1;
108         else
109                         mask = BTRFS_BLOCK_GROUP_TYPE_MASK;
110
111         if ((f1 & mask) > (f2 & mask))
112                 return +1;
113         else if ((f1 & mask) < (f2 & mask))
114                 return -1;
115         else
116                 return 0;
117 }
118
119 /*
120  * Helper to sort the chunk
121  */
122 static int cmp_chunk_info(const void *a, const void *b)
123 {
124         return cmp_chunk_block_group(
125                 ((struct chunk_info *)a)->type,
126                 ((struct chunk_info *)b)->type);
127 }
128
129 static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
130 {
131         int ret;
132         struct btrfs_ioctl_search_args args;
133         struct btrfs_ioctl_search_key *sk = &args.key;
134         struct btrfs_ioctl_search_header *sh;
135         unsigned long off = 0;
136         int i, e;
137
138         memset(&args, 0, sizeof(args));
139
140         /*
141          * there may be more than one ROOT_ITEM key if there are
142          * snapshots pending deletion, we have to loop through
143          * them.
144          */
145         sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
146
147         sk->min_objectid = 0;
148         sk->max_objectid = (u64)-1;
149         sk->max_type = 0;
150         sk->min_type = (u8)-1;
151         sk->min_offset = 0;
152         sk->max_offset = (u64)-1;
153         sk->min_transid = 0;
154         sk->max_transid = (u64)-1;
155         sk->nr_items = 4096;
156
157         while (1) {
158                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
159                 e = errno;
160                 if (e == EPERM)
161                         return -e;
162
163                 if (ret < 0) {
164                         error("cannot look up chunk tree info: %s",
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 = NULL;
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 = NULL, *sargs_orig = NULL;
231         int e, ret, count;
232
233         sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
234         if (!sargs) {
235                 error("not enough memory");
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                 error("cannot get space info on '%s': %s", path, strerror(e));
246                 free(sargs);
247                 return NULL;
248         }
249         if (!sargs->total_spaces) {
250                 free(sargs);
251                 printf("No chunks found\n");
252                 return NULL;
253         }
254
255         count = sargs->total_spaces;
256
257         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
258                         (count * sizeof(struct btrfs_ioctl_space_info)));
259         if (!sargs) {
260                 free(sargs_orig);
261                 error("not enough memory");
262                 return NULL;
263         }
264
265         sargs->space_slots = count;
266         sargs->total_spaces = 0;
267
268         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
269         e = errno;
270
271         if (ret) {
272                 error("cannot get space info with %u slots: %s",
273                         count, strerror(e));
274                 free(sargs);
275                 return NULL;
276         }
277
278         qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
279                 cmp_btrfs_ioctl_space_info);
280
281         return sargs;
282 }
283
284 /*
285  * This function computes the space occuped by a *single* RAID5/RAID6 chunk.
286  * The computation is performed on the basis of the number of stripes
287  * which compose the chunk, which could be different from the number of devices
288  * if a disk is added later.
289  */
290 static void get_raid56_used(int fd, struct chunk_info *chunks, int chunkcount,
291                 u64 *raid5_used, u64 *raid6_used)
292 {
293         struct chunk_info *info_ptr = chunks;
294         *raid5_used = 0;
295         *raid6_used = 0;
296
297         while (chunkcount-- > 0) {
298                 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID5)
299                         (*raid5_used) += info_ptr->size / (info_ptr->num_stripes - 1);
300                 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID6)
301                         (*raid6_used) += info_ptr->size / (info_ptr->num_stripes - 2);
302                 info_ptr++;
303         }
304 }
305
306 #define MIN_UNALOCATED_THRESH   (16 * 1024 * 1024)
307 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
308                 int chunkcount, struct device_info *devinfo, int devcount,
309                 char *path, unsigned unit_mode)
310 {
311         struct btrfs_ioctl_space_args *sargs = NULL;
312         int i;
313         int ret = 0;
314         int width = 10;         /* default 10 for human units */
315         /*
316          * r_* prefix is for raw data
317          * l_* is for logical
318          */
319         u64 r_total_size = 0;   /* filesystem size, sum of device sizes */
320         u64 r_total_chunks = 0; /* sum of chunks sizes on disk(s) */
321         u64 r_total_used = 0;
322         u64 r_total_unused = 0;
323         u64 r_total_missing = 0;        /* sum of missing devices size */
324         u64 r_data_used = 0;
325         u64 r_data_chunks = 0;
326         u64 l_data_chunks = 0;
327         u64 r_metadata_used = 0;
328         u64 r_metadata_chunks = 0;
329         u64 l_metadata_chunks = 0;
330         u64 r_system_used = 0;
331         u64 r_system_chunks = 0;
332         double data_ratio;
333         double metadata_ratio;
334         /* logical */
335         u64 raid5_used = 0;
336         u64 raid6_used = 0;
337         u64 l_global_reserve = 0;
338         u64 l_global_reserve_used = 0;
339         u64 free_estimated = 0;
340         u64 free_min = 0;
341         int max_data_ratio = 1;
342
343         sargs = load_space_info(fd, path);
344         if (!sargs) {
345                 ret = 1;
346                 goto exit;
347         }
348
349         r_total_size = 0;
350         for (i = 0; i < devcount; i++) {
351                 r_total_size += devinfo[i].size;
352                 if (!devinfo[i].device_size)
353                         r_total_missing += devinfo[i].size;
354         }
355
356         if (r_total_size == 0) {
357                 error("cannot get space info on '%s': %s",
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                         warning("RAID56 detected, not implemented");
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                         warning("MIXED blockgroups not handled");
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 (unit_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, unit_mode));
459         printf("    Device allocated:\t\t%*s\n", width,
460                 pretty_size_mode(r_total_chunks, unit_mode));
461         printf("    Device unallocated:\t\t%*s\n", width,
462                 pretty_size_mode(r_total_unused, unit_mode));
463         printf("    Device missing:\t\t%*s\n", width,
464                 pretty_size_mode(r_total_missing, unit_mode));
465         printf("    Used:\t\t\t%*s\n", width,
466                 pretty_size_mode(r_total_used, unit_mode));
467         printf("    Free (estimated):\t\t%*s\t(",
468                 width,
469                 pretty_size_mode(free_estimated, unit_mode));
470         printf("min: %s)\n", pretty_size_mode(free_min, unit_mode));
471         printf("    Data ratio:\t\t\t%*.2f\n",
472                 width, data_ratio);
473         printf("    Metadata ratio:\t\t%*.2f\n",
474                 width, metadata_ratio);
475         printf("    Global reserve:\t\t%*s\t(used: %s)\n", width,
476                 pretty_size_mode(l_global_reserve, unit_mode),
477                 pretty_size_mode(l_global_reserve_used, unit_mode));
478
479 exit:
480
481         if (sargs)
482                 free(sargs);
483
484         return ret;
485 }
486
487 /*
488  *  Helper to sort the device_info structure
489  */
490 static int cmp_device_info(const void *a, const void *b)
491 {
492         return strcmp(((struct device_info *)a)->path,
493                         ((struct device_info *)b)->path);
494 }
495
496 /*
497  *  This function loads the device_info structure and put them in an array
498  */
499 static int load_device_info(int fd, struct device_info **device_info_ptr,
500                            int *device_info_count)
501 {
502         int ret, i, ndevs;
503         struct btrfs_ioctl_fs_info_args fi_args;
504         struct btrfs_ioctl_dev_info_args dev_info;
505         struct device_info *info;
506
507         *device_info_count = 0;
508         *device_info_ptr = NULL;
509
510         ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
511         if (ret < 0) {
512                 if (errno == EPERM)
513                         return -errno;
514                 error("cannot get filesystem info: %s",
515                                 strerror(errno));
516                 return 1;
517         }
518
519         info = calloc(fi_args.num_devices, sizeof(struct device_info));
520         if (!info) {
521                 error("not enough memory");
522                 return 1;
523         }
524
525         for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
526                 BUG_ON(ndevs >= fi_args.num_devices);
527                 memset(&dev_info, 0, sizeof(dev_info));
528                 ret = get_device_info(fd, i, &dev_info);
529
530                 if (ret == -ENODEV)
531                         continue;
532                 if (ret) {
533                         error("cannot get info about device devid=%d", i);
534                         free(info);
535                         return ret;
536                 }
537
538                 info[ndevs].devid = dev_info.devid;
539                 if (!dev_info.path[0]) {
540                         strcpy(info[ndevs].path, "missing");
541                 } else {
542                         strcpy(info[ndevs].path, (char *)dev_info.path);
543                         info[ndevs].device_size =
544                                 get_partition_size((char *)dev_info.path);
545                 }
546                 info[ndevs].size = dev_info.total_bytes;
547                 ++ndevs;
548         }
549
550         BUG_ON(ndevs != fi_args.num_devices);
551         qsort(info, fi_args.num_devices,
552                 sizeof(struct device_info), cmp_device_info);
553
554         *device_info_count = fi_args.num_devices;
555         *device_info_ptr = info;
556
557         return 0;
558 }
559
560 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
561                 int *chunkcount, struct device_info **devinfo, int *devcount)
562 {
563         int ret;
564
565         ret = load_chunk_info(fd, chunkinfo, chunkcount);
566         if (ret == -EPERM) {
567                 warning(
568 "cannot read detailed chunk info, RAID5/6 numbers will be incorrect, run as root");
569         } else if (ret) {
570                 return ret;
571         }
572
573         ret = load_device_info(fd, devinfo, devcount);
574         if (ret == -EPERM) {
575                 warning(
576                 "cannot get filesystem info from ioctl(FS_INFO), run as root");
577                 ret = 0;
578         }
579
580         return ret;
581 }
582
583 /*
584  *  This function computes the size of a chunk in a disk
585  */
586 static u64 calc_chunk_size(struct chunk_info *ci)
587 {
588         if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
589                 return ci->size / ci->num_stripes;
590         else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
591                 return ci->size ;
592         else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
593                 return ci->size ;
594         else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
595                 return ci->size / (ci->num_stripes -1);
596         else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
597                 return ci->size / (ci->num_stripes -2);
598         else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
599                 return ci->size / ci->num_stripes;
600         return ci->size;
601 }
602
603 /*
604  *  This function print the results of the command "btrfs fi usage"
605  *  in tabular format
606  */
607 static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
608                                         struct btrfs_ioctl_space_args *sargs,
609                                         struct chunk_info *chunks_info_ptr,
610                                         int chunks_info_count,
611                                         struct device_info *device_info_ptr,
612                                         int device_info_count)
613 {
614         int i;
615         u64 total_unused = 0;
616         struct string_table *matrix = NULL;
617         int  ncols, nrows;
618         int col;
619         int unallocated_col;
620         int spaceinfos_col;
621         const int vhdr_skip = 3;        /* amount of vertical header space */
622
623         /* id, path, unallocated */
624         ncols = 3;
625         spaceinfos_col = 2;
626         /* Properly count the real space infos */
627         for (i = 0; i < sargs->total_spaces; i++) {
628                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
629                         continue;
630                 ncols++;
631         }
632
633         /* 2 for header, empty line, devices, ===, total, used */
634         nrows = vhdr_skip + device_info_count + 1 + 2;
635
636         matrix = table_create(ncols, nrows);
637         if (!matrix) {
638                 error("not enough memory");
639                 return;
640         }
641
642         /*
643          * We have to skip the global block reserve everywhere as it's an
644          * artificial blockgroup
645          */
646
647         /* header */
648         for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
649                 u64 flags = sargs->spaces[i].flags;
650
651                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
652                         continue;
653
654                 table_printf(matrix, col, 0, "<%s",
655                                 btrfs_group_type_str(flags));
656                 table_printf(matrix, col, 1, "<%s",
657                                 btrfs_group_profile_str(flags));
658                 col++;
659         }
660         unallocated_col = col;
661
662         table_printf(matrix, 0, 1, "<Id");
663         table_printf(matrix, 1, 1, "<Path");
664         table_printf(matrix, unallocated_col, 1, "<Unallocated");
665
666         /* body */
667         for (i = 0; i < device_info_count; i++) {
668                 int k;
669                 char *p;
670
671                 u64  total_allocated = 0, unused;
672
673                 p = strrchr(device_info_ptr[i].path, '/');
674                 if (!p)
675                         p = device_info_ptr[i].path;
676                 else
677                         p++;
678
679                 table_printf(matrix, 0, vhdr_skip + i, ">%llu",
680                                 device_info_ptr[i].devid);
681                 table_printf(matrix, 1, vhdr_skip + i, "<%s",
682                                 device_info_ptr[i].path);
683
684                 for (col = spaceinfos_col, k = 0; k < sargs->total_spaces; k++) {
685                         u64     flags = sargs->spaces[k].flags;
686                         u64 devid = device_info_ptr[i].devid;
687                         int     j;
688                         u64 size = 0;
689
690                         if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
691                                 continue;
692
693                         for (j = 0 ; j < chunks_info_count ; j++) {
694                                 if (chunks_info_ptr[j].type != flags )
695                                                 continue;
696                                 if (chunks_info_ptr[j].devid != devid)
697                                                 continue;
698
699                                 size += calc_chunk_size(chunks_info_ptr+j);
700                         }
701
702                         if (size)
703                                 table_printf(matrix, col, vhdr_skip+ i,
704                                         ">%s", pretty_size_mode(size, unit_mode));
705                         else
706                                 table_printf(matrix, col, vhdr_skip + i, ">-");
707
708                         total_allocated += size;
709                         col++;
710                 }
711
712                 unused = get_partition_size(device_info_ptr[i].path)
713                                 - total_allocated;
714
715                 table_printf(matrix, unallocated_col, vhdr_skip + i,
716                                ">%s", pretty_size_mode(unused, unit_mode));
717                 total_unused += unused;
718
719         }
720
721         for (i = 0; i < spaceinfos_col; i++) {
722                 table_printf(matrix, i, vhdr_skip - 1, "*-");
723                 table_printf(matrix, i, vhdr_skip + device_info_count, "*-");
724         }
725
726         for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
727                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
728                         continue;
729
730                 table_printf(matrix, col, vhdr_skip - 1, "*-");
731                 table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
732                 col++;
733         }
734         /* One for Unallocated */
735         table_printf(matrix, col, vhdr_skip - 1, "*-");
736         table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
737
738         /* footer */
739         table_printf(matrix, 1, vhdr_skip + device_info_count + 1, "<Total");
740         for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
741                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
742                         continue;
743
744                 table_printf(matrix, col++, vhdr_skip + device_info_count + 1,
745                         ">%s",
746                         pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));
747         }
748
749         table_printf(matrix, unallocated_col, vhdr_skip + device_info_count + 1,
750                         ">%s", pretty_size_mode(total_unused, unit_mode));
751
752         table_printf(matrix, 1, vhdr_skip + device_info_count + 2, "<Used");
753         for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
754                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
755                         continue;
756
757                 table_printf(matrix, col++, vhdr_skip + device_info_count + 2,
758                         ">%s",
759                         pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
760         }
761
762         table_dump(matrix);
763         table_free(matrix);
764 }
765
766 /*
767  *  This function prints the unused space per every disk
768  */
769 static void print_unused(struct chunk_info *info_ptr,
770                           int info_count,
771                           struct device_info *device_info_ptr,
772                           int device_info_count,
773                           unsigned unit_mode)
774 {
775         int i;
776         for (i = 0; i < device_info_count; i++) {
777                 int     j;
778                 u64     total = 0;
779
780                 for (j = 0; j < info_count; j++)
781                         if (info_ptr[j].devid == device_info_ptr[i].devid)
782                                 total += calc_chunk_size(info_ptr+j);
783
784                 printf("   %s\t%10s\n",
785                         device_info_ptr[i].path,
786                         pretty_size_mode(device_info_ptr[i].size - total,
787                                 unit_mode));
788         }
789 }
790
791 /*
792  *  This function prints the allocated chunk per every disk
793  */
794 static void print_chunk_device(u64 chunk_type,
795                                 struct chunk_info *chunks_info_ptr,
796                                 int chunks_info_count,
797                                 struct device_info *device_info_ptr,
798                                 int device_info_count,
799                                 unsigned unit_mode)
800 {
801         int i;
802
803         for (i = 0; i < device_info_count; i++) {
804                 int     j;
805                 u64     total = 0;
806
807                 for (j = 0; j < chunks_info_count; j++) {
808
809                         if (chunks_info_ptr[j].type != chunk_type)
810                                 continue;
811                         if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
812                                 continue;
813
814                         total += calc_chunk_size(&(chunks_info_ptr[j]));
815                         //total += chunks_info_ptr[j].size;
816                 }
817
818                 if (total > 0)
819                         printf("   %s\t%10s\n",
820                                 device_info_ptr[i].path,
821                                 pretty_size_mode(total, unit_mode));
822         }
823 }
824
825 /*
826  *  This function print the results of the command "btrfs fi usage"
827  *  in linear format
828  */
829 static void _cmd_filesystem_usage_linear(unsigned unit_mode,
830                                         struct btrfs_ioctl_space_args *sargs,
831                                         struct chunk_info *info_ptr,
832                                         int info_count,
833                                         struct device_info *device_info_ptr,
834                                         int device_info_count)
835 {
836         int i;
837
838         for (i = 0; i < sargs->total_spaces; i++) {
839                 const char *description;
840                 const char *r_mode;
841                 u64 flags = sargs->spaces[i].flags;
842
843                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
844                         continue;
845
846                 description = btrfs_group_type_str(flags);
847                 r_mode = btrfs_group_profile_str(flags);
848
849                 printf("%s,%s: Size:%s, ",
850                         description,
851                         r_mode,
852                         pretty_size_mode(sargs->spaces[i].total_bytes,
853                                 unit_mode));
854                 printf("Used:%s\n",
855                         pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
856                 print_chunk_device(flags, info_ptr, info_count,
857                                 device_info_ptr, device_info_count, unit_mode);
858                 printf("\n");
859         }
860
861         printf("Unallocated:\n");
862         print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
863                         unit_mode);
864 }
865
866 static int print_filesystem_usage_by_chunk(int fd,
867                 struct chunk_info *chunkinfo, int chunkcount,
868                 struct device_info *devinfo, int devcount,
869                 char *path, unsigned unit_mode, int tabular)
870 {
871         struct btrfs_ioctl_space_args *sargs;
872         int ret = 0;
873
874         if (!chunkinfo)
875                 return 0;
876
877         sargs = load_space_info(fd, path);
878         if (!sargs) {
879                 ret = 1;
880                 goto out;
881         }
882
883         if (tabular)
884                 _cmd_filesystem_usage_tabular(unit_mode, sargs, chunkinfo,
885                                 chunkcount, devinfo, devcount);
886         else
887                 _cmd_filesystem_usage_linear(unit_mode, sargs, chunkinfo,
888                                 chunkcount, devinfo, devcount);
889
890         free(sargs);
891 out:
892         return ret;
893 }
894
895 const char * const cmd_filesystem_usage_usage[] = {
896         "btrfs filesystem usage [options] <path> [<path>..]",
897         "Show detailed information about internal filesystem usage .",
898         HELPINFO_UNITS_SHORT_LONG,
899         "-T                 show data in tabular format",
900         NULL
901 };
902
903 int cmd_filesystem_usage(int argc, char **argv)
904 {
905         int ret = 0;
906         unsigned unit_mode;
907         int i;
908         int more_than_one = 0;
909         int tabular = 0;
910
911         unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
912
913         optind = 1;
914         while (1) {
915                 int c;
916
917                 c = getopt(argc, argv, "T");
918                 if (c < 0)
919                         break;
920
921                 switch (c) {
922                 case 'T':
923                         tabular = 1;
924                         break;
925                 default:
926                         usage(cmd_filesystem_usage_usage);
927                 }
928         }
929
930         if (check_argc_min(argc - optind, 1))
931                 usage(cmd_filesystem_usage_usage);
932
933         for (i = optind; i < argc; i++) {
934                 int fd;
935                 DIR *dirstream = NULL;
936                 struct chunk_info *chunkinfo = NULL;
937                 struct device_info *devinfo = NULL;
938                 int chunkcount = 0;
939                 int devcount = 0;
940
941                 fd = btrfs_open_dir(argv[i], &dirstream, 1);
942                 if (fd < 0) {
943                         ret = 1;
944                         goto out;
945                 }
946                 if (more_than_one)
947                         printf("\n");
948
949                 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
950                                 &devinfo, &devcount);
951                 if (ret)
952                         goto cleanup;
953
954                 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
955                                 devinfo, devcount, argv[i], unit_mode);
956                 if (ret)
957                         goto cleanup;
958                 printf("\n");
959                 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
960                                 devinfo, devcount, argv[i], unit_mode, tabular);
961 cleanup:
962                 close_file_or_dir(fd, dirstream);
963                 free(chunkinfo);
964                 free(devinfo);
965
966                 if (ret)
967                         goto out;
968                 more_than_one = 1;
969         }
970
971 out:
972         return !!ret;
973 }
974
975 void print_device_chunks(int fd, struct device_info *devinfo,
976                 struct chunk_info *chunks_info_ptr,
977                 int chunks_info_count, unsigned unit_mode)
978 {
979         int i;
980         u64 allocated = 0;
981
982         for (i = 0 ; i < chunks_info_count ; i++) {
983                 const char *description;
984                 const char *r_mode;
985                 u64 flags;
986                 u64 size;
987
988                 if (chunks_info_ptr[i].devid != devinfo->devid)
989                         continue;
990
991                 flags = chunks_info_ptr[i].type;
992
993                 description = btrfs_group_type_str(flags);
994                 r_mode = btrfs_group_profile_str(flags);
995                 size = calc_chunk_size(chunks_info_ptr+i);
996                 printf("   %s,%s:%*s%10s\n",
997                         description,
998                         r_mode,
999                         (int)(20 - strlen(description) - strlen(r_mode)), "",
1000                         pretty_size_mode(size, unit_mode));
1001
1002                 allocated += size;
1003
1004         }
1005         printf("   Unallocated: %*s%10s\n",
1006                 (int)(20 - strlen("Unallocated")), "",
1007                 pretty_size_mode(devinfo->size - allocated, unit_mode));
1008 }
1009
1010 void print_device_sizes(int fd, struct device_info *devinfo, unsigned unit_mode)
1011 {
1012         printf("   Device size: %*s%10s\n",
1013                 (int)(20 - strlen("Device size")), "",
1014                 pretty_size_mode(devinfo->device_size, unit_mode));
1015 #if 0
1016         /*
1017          * The term has not seen an agreement and we don't want to change it
1018          * once it's in non-development branches or even released.
1019          */
1020         printf("   FS occupied: %*s%10s\n",
1021                 (int)(20 - strlen("FS occupied")), "",
1022                 pretty_size_mode(devinfo->size, unit_mode));
1023 #endif
1024 }