btrfs-progs: fi usage: properly count real space infos
[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 = 0;
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                                 fprintf(stderr, "ERROR: not enough memory\n");
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                         fprintf(stderr,
165                                 "ERROR: can't perform the search - %s\n",
166                                 strerror(e));
167                         return 1;
168                 }
169                 /* the ioctl returns the number of item it found in nr_items */
170
171                 if (sk->nr_items == 0)
172                         break;
173
174                 off = 0;
175                 for (i = 0; i < sk->nr_items; i++) {
176                         struct btrfs_chunk *item;
177                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
178                                                                   off);
179
180                         off += sizeof(*sh);
181                         item = (struct btrfs_chunk *)(args.buf + off);
182
183                         ret = add_info_to_list(info_ptr, info_count, item);
184                         if (ret) {
185                                 *info_ptr = 0;
186                                 return 1;
187                         }
188
189                         off += sh->len;
190
191                         sk->min_objectid = sh->objectid;
192                         sk->min_type = sh->type;
193                         sk->min_offset = sh->offset+1;
194
195                 }
196                 if (!sk->min_offset)    /* overflow */
197                         sk->min_type++;
198                 else
199                         continue;
200
201                 if (!sk->min_type)
202                         sk->min_objectid++;
203                  else
204                         continue;
205
206                 if (!sk->min_objectid)
207                         break;
208         }
209
210         qsort(*info_ptr, *info_count, sizeof(struct chunk_info),
211                 cmp_chunk_info);
212
213         return 0;
214 }
215
216 /*
217  * Helper to sort the struct btrfs_ioctl_space_info
218  */
219 static int cmp_btrfs_ioctl_space_info(const void *a, const void *b)
220 {
221         return cmp_chunk_block_group(
222                 ((struct btrfs_ioctl_space_info *)a)->flags,
223                 ((struct btrfs_ioctl_space_info *)b)->flags);
224 }
225
226 /*
227  * This function load all the information about the space usage
228  */
229 static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path)
230 {
231         struct btrfs_ioctl_space_args *sargs = 0, *sargs_orig = 0;
232         int e, ret, count;
233
234         sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
235         if (!sargs) {
236                 fprintf(stderr, "ERROR: not enough memory\n");
237                 return NULL;
238         }
239
240         sargs->space_slots = 0;
241         sargs->total_spaces = 0;
242
243         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
244         e = errno;
245         if (ret) {
246                 fprintf(stderr,
247                         "ERROR: couldn't get space info on '%s' - %s\n",
248                         path, strerror(e));
249                 free(sargs);
250                 return NULL;
251         }
252         if (!sargs->total_spaces) {
253                 free(sargs);
254                 printf("No chunks found\n");
255                 return NULL;
256         }
257
258         count = sargs->total_spaces;
259
260         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
261                         (count * sizeof(struct btrfs_ioctl_space_info)));
262         if (!sargs) {
263                 free(sargs_orig);
264                 fprintf(stderr, "ERROR: not enough memory\n");
265                 return NULL;
266         }
267
268         sargs->space_slots = count;
269         sargs->total_spaces = 0;
270
271         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
272         e = errno;
273
274         if (ret) {
275                 fprintf(stderr,
276                         "ERROR: couldn't get space info on '%s' - %s\n",
277                         path, strerror(e));
278                 free(sargs);
279                 return NULL;
280         }
281
282         qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
283                 cmp_btrfs_ioctl_space_info);
284
285         return sargs;
286 }
287
288 /*
289  * This function computes the space occuped by a *single* RAID5/RAID6 chunk.
290  * The computation is performed on the basis of the number of stripes
291  * which compose the chunk, which could be different from the number of devices
292  * if a disk is added later.
293  */
294 static void get_raid56_used(int fd, struct chunk_info *chunks, int chunkcount,
295                 u64 *raid5_used, u64 *raid6_used)
296 {
297         struct chunk_info *info_ptr = chunks;
298         *raid5_used = 0;
299         *raid6_used = 0;
300
301         while (chunkcount-- > 0) {
302                 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID5)
303                         (*raid5_used) += info_ptr->size / (info_ptr->num_stripes - 1);
304                 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID6)
305                         (*raid6_used) += info_ptr->size / (info_ptr->num_stripes - 2);
306                 info_ptr++;
307         }
308 }
309
310 #define MIN_UNALOCATED_THRESH   (16 * 1024 * 1024)
311 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
312                 int chunkcount, struct device_info *devinfo, int devcount,
313                 char *path, unsigned unit_mode)
314 {
315         struct btrfs_ioctl_space_args *sargs = 0;
316         int i;
317         int ret = 0;
318         int width = 10;         /* default 10 for human units */
319         /*
320          * r_* prefix is for raw data
321          * l_* is for logical
322          */
323         u64 r_total_size = 0;   /* filesystem size, sum of device sizes */
324         u64 r_total_chunks = 0; /* sum of chunks sizes on disk(s) */
325         u64 r_total_used = 0;
326         u64 r_total_unused = 0;
327         u64 r_total_missing = 0;        /* sum of missing devices size */
328         u64 r_data_used = 0;
329         u64 r_data_chunks = 0;
330         u64 l_data_chunks = 0;
331         u64 r_metadata_used = 0;
332         u64 r_metadata_chunks = 0;
333         u64 l_metadata_chunks = 0;
334         u64 r_system_used = 0;
335         u64 r_system_chunks = 0;
336         double data_ratio;
337         double metadata_ratio;
338         /* logical */
339         u64 raid5_used = 0;
340         u64 raid6_used = 0;
341         u64 l_global_reserve = 0;
342         u64 l_global_reserve_used = 0;
343         u64 free_estimated = 0;
344         u64 free_min = 0;
345         int max_data_ratio = 1;
346
347         sargs = load_space_info(fd, path);
348         if (!sargs) {
349                 ret = 1;
350                 goto exit;
351         }
352
353         r_total_size = 0;
354         for (i = 0; i < devcount; i++) {
355                 r_total_size += devinfo[i].size;
356                 if (!devinfo[i].device_size)
357                         r_total_missing += devinfo[i].size;
358         }
359
360         if (r_total_size == 0) {
361                 fprintf(stderr,
362                         "ERROR: couldn't get space info on '%s' - %s\n",
363                         path, strerror(errno));
364
365                 ret = 1;
366                 goto exit;
367         }
368         get_raid56_used(fd, chunkinfo, chunkcount, &raid5_used, &raid6_used);
369
370         for (i = 0; i < sargs->total_spaces; i++) {
371                 int ratio;
372                 u64 flags = sargs->spaces[i].flags;
373
374                 /*
375                  * The raid5/raid6 ratio depends by the stripes number
376                  * used by every chunk. It is computed separately
377                  */
378                 if (flags & BTRFS_BLOCK_GROUP_RAID0)
379                         ratio = 1;
380                 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
381                         ratio = 2;
382                 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
383                         ratio = 0;
384                 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
385                         ratio = 0;
386                 else if (flags & BTRFS_BLOCK_GROUP_DUP)
387                         ratio = 2;
388                 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
389                         ratio = 2;
390                 else
391                         ratio = 1;
392
393                 if (!ratio)
394                         fprintf(stderr, "WARNING: RAID56 detected, not implemented\n");
395
396                 if (ratio > max_data_ratio)
397                         max_data_ratio = ratio;
398
399                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV) {
400                         l_global_reserve = sargs->spaces[i].total_bytes;
401                         l_global_reserve_used = sargs->spaces[i].used_bytes;
402                 }
403                 if ((flags & (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA))
404                         == (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA)) {
405                         fprintf(stderr, "WARNING: MIXED blockgroups not handled\n");
406                 }
407
408                 if (flags & BTRFS_BLOCK_GROUP_DATA) {
409                         r_data_used += sargs->spaces[i].used_bytes * ratio;
410                         r_data_chunks += sargs->spaces[i].total_bytes * ratio;
411                         l_data_chunks += sargs->spaces[i].total_bytes;
412                 }
413                 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
414                         r_metadata_used += sargs->spaces[i].used_bytes * ratio;
415                         r_metadata_chunks += sargs->spaces[i].total_bytes * ratio;
416                         l_metadata_chunks += sargs->spaces[i].total_bytes;
417                 }
418                 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
419                         r_system_used += sargs->spaces[i].used_bytes * ratio;
420                         r_system_chunks += sargs->spaces[i].total_bytes * ratio;
421                 }
422         }
423
424         r_total_chunks = r_data_chunks + r_metadata_chunks + r_system_chunks;
425         r_total_used = r_data_used + r_metadata_used + r_system_used;
426         r_total_unused = r_total_size - r_total_chunks;
427
428         /* Raw / Logical = raid factor, >= 1 */
429         data_ratio = (double)r_data_chunks / l_data_chunks;
430         metadata_ratio = (double)r_metadata_chunks / l_metadata_chunks;
431
432 #if 0
433         /* add the raid5/6 allocated space */
434         total_chunks += raid5_used + raid6_used;
435 #endif
436
437         /*
438          * We're able to fill at least DATA for the unused space
439          *
440          * With mixed raid levels, this gives a rough estimate but more
441          * accurate than just counting the logical free space
442          * (l_data_chunks - l_data_used)
443          *
444          * In non-mixed case there's no difference.
445          */
446         free_estimated = (r_data_chunks - r_data_used) / data_ratio;
447         free_min = free_estimated;
448
449         /* Chop unallocatable space */
450         /* FIXME: must be applied per device */
451         if (r_total_unused >= MIN_UNALOCATED_THRESH) {
452                 free_estimated += r_total_unused / data_ratio;
453                 /* Match the calculation of 'df', use the highest raid ratio */
454                 free_min += r_total_unused / max_data_ratio;
455         }
456
457         if (unit_mode != UNITS_HUMAN)
458                 width = 18;
459
460         printf("Overall:\n");
461
462         printf("    Device size:\t\t%*s\n", width,
463                 pretty_size_mode(r_total_size, unit_mode));
464         printf("    Device allocated:\t\t%*s\n", width,
465                 pretty_size_mode(r_total_chunks, unit_mode));
466         printf("    Device unallocated:\t\t%*s\n", width,
467                 pretty_size_mode(r_total_unused, unit_mode));
468         printf("    Device missing:\t\t%*s\n", width,
469                 pretty_size_mode(r_total_missing, unit_mode));
470         printf("    Used:\t\t\t%*s\n", width,
471                 pretty_size_mode(r_total_used, unit_mode));
472         printf("    Free (estimated):\t\t%*s\t(",
473                 width,
474                 pretty_size_mode(free_estimated, unit_mode));
475         printf("min: %s)\n", pretty_size_mode(free_min, unit_mode));
476         printf("    Data ratio:\t\t\t%*.2f\n",
477                 width, data_ratio);
478         printf("    Metadata ratio:\t\t%*.2f\n",
479                 width, metadata_ratio);
480         printf("    Global reserve:\t\t%*s\t(used: %s)\n", width,
481                 pretty_size_mode(l_global_reserve, unit_mode),
482                 pretty_size_mode(l_global_reserve_used, unit_mode));
483
484 exit:
485
486         if (sargs)
487                 free(sargs);
488
489         return ret;
490 }
491
492 /*
493  *  Helper to sort the device_info structure
494  */
495 static int cmp_device_info(const void *a, const void *b)
496 {
497         return strcmp(((struct device_info *)a)->path,
498                         ((struct device_info *)b)->path);
499 }
500
501 /*
502  *  This function loads the device_info structure and put them in an array
503  */
504 static int load_device_info(int fd, struct device_info **device_info_ptr,
505                            int *device_info_count)
506 {
507         int ret, i, ndevs;
508         struct btrfs_ioctl_fs_info_args fi_args;
509         struct btrfs_ioctl_dev_info_args dev_info;
510         struct device_info *info;
511
512         *device_info_count = 0;
513         *device_info_ptr = 0;
514
515         ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
516         if (ret < 0) {
517                 if (errno == EPERM)
518                         return -errno;
519                 fprintf(stderr, "ERROR: cannot get filesystem info - %s\n",
520                                 strerror(errno));
521                 return 1;
522         }
523
524         info = calloc(fi_args.num_devices, sizeof(struct device_info));
525         if (!info) {
526                 fprintf(stderr, "ERROR: not enough memory\n");
527                 return 1;
528         }
529
530         for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
531                 BUG_ON(ndevs >= fi_args.num_devices);
532                 memset(&dev_info, 0, sizeof(dev_info));
533                 ret = get_device_info(fd, i, &dev_info);
534
535                 if (ret == -ENODEV)
536                         continue;
537                 if (ret) {
538                         fprintf(stderr,
539                             "ERROR: cannot get info about device devid=%d\n",
540                             i);
541                         free(info);
542                         return ret;
543                 }
544
545                 info[ndevs].devid = dev_info.devid;
546                 if (!dev_info.path[0]) {
547                         strcpy(info[ndevs].path, "missing");
548                 } else {
549                         strcpy(info[ndevs].path, (char *)dev_info.path);
550                         info[ndevs].device_size =
551                                 get_partition_size((char *)dev_info.path);
552                 }
553                 info[ndevs].size = dev_info.total_bytes;
554                 ++ndevs;
555         }
556
557         BUG_ON(ndevs != fi_args.num_devices);
558         qsort(info, fi_args.num_devices,
559                 sizeof(struct device_info), cmp_device_info);
560
561         *device_info_count = fi_args.num_devices;
562         *device_info_ptr = info;
563
564         return 0;
565 }
566
567 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
568                 int *chunkcount, struct device_info **devinfo, int *devcount)
569 {
570         int ret;
571
572         ret = load_chunk_info(fd, chunkinfo, chunkcount);
573         if (ret == -EPERM) {
574                 fprintf(stderr,
575                         "WARNING: can't read detailed chunk info, RAID5/6 numbers will be incorrect, run as root\n");
576         } else if (ret) {
577                 return ret;
578         }
579
580         ret = load_device_info(fd, devinfo, devcount);
581         if (ret == -EPERM) {
582                 fprintf(stderr,
583                         "WARNING: can't get filesystem info from ioctl(FS_INFO), run as root\n");
584                 ret = 0;
585         }
586
587         return ret;
588 }
589
590 /*
591  *  This function computes the size of a chunk in a disk
592  */
593 static u64 calc_chunk_size(struct chunk_info *ci)
594 {
595         if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
596                 return ci->size / ci->num_stripes;
597         else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
598                 return ci->size ;
599         else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
600                 return ci->size ;
601         else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
602                 return ci->size / (ci->num_stripes -1);
603         else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
604                 return ci->size / (ci->num_stripes -2);
605         else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
606                 return ci->size / ci->num_stripes;
607         return ci->size;
608 }
609
610 /*
611  *  This function print the results of the command "btrfs fi usage"
612  *  in tabular format
613  */
614 static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
615                                         struct btrfs_ioctl_space_args *sargs,
616                                         struct chunk_info *chunks_info_ptr,
617                                         int chunks_info_count,
618                                         struct device_info *device_info_ptr,
619                                         int device_info_count)
620 {
621         int i;
622         u64 total_unused = 0;
623         struct string_table *matrix = 0;
624         int  ncols, nrows;
625         int col;
626         int unallocated_col;
627
628         /* path, unallocated */
629         ncols = 2;
630         /* Properly count the real space infos */
631         for (i = 0; i < sargs->total_spaces; i++) {
632                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
633                         continue;
634                 ncols++;
635         }
636
637         /* 2 for header, empty line, devices, ===, total, used */
638         nrows = 2 + 1 + device_info_count + 1 + 2;
639
640         matrix = table_create(ncols, nrows);
641         if (!matrix) {
642                 fprintf(stderr, "ERROR: not enough memory\n");
643                 return;
644         }
645
646         /*
647          * We have to skip the global block reserve everywhere as it's an
648          * artificial blockgroup
649          */
650
651         /* header */
652         for (i = 0, col = 1; i < sargs->total_spaces; i++) {
653                 u64 flags = sargs->spaces[i].flags;
654
655                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
656                         continue;
657
658                 table_printf(matrix, col, 0, "<%s",
659                                 btrfs_group_type_str(flags));
660                 table_printf(matrix, col, 1, "<%s",
661                                 btrfs_group_profile_str(flags));
662                 col++;
663         }
664         unallocated_col = col;
665
666         table_printf(matrix, 0, 1, "<Path");
667         table_printf(matrix, unallocated_col, 1, "<Unallocated");
668
669         /* body */
670         for (i = 0; i < device_info_count; i++) {
671                 int k;
672                 char *p;
673
674                 u64  total_allocated = 0, unused;
675
676                 p = strrchr(device_info_ptr[i].path, '/');
677                 if (!p)
678                         p = device_info_ptr[i].path;
679                 else
680                         p++;
681
682                 table_printf(matrix, 0, i + 3, "<%s", device_info_ptr[i].path);
683
684                 for (col = 1, 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, i+3,
704                                         ">%s", pretty_size_mode(size, unit_mode));
705                         else
706                                 table_printf(matrix, col, i+3, ">-");
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, i + 3,
716                                ">%s", pretty_size_mode(unused, unit_mode));
717                 total_unused += unused;
718
719         }
720
721         for (i = 0, col = 1; i < sargs->total_spaces; i++) {
722                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
723                         continue;
724
725                 table_printf(matrix, col++, device_info_count + 3, "=");
726         }
727         /* One for Unallocated */
728         table_printf(matrix, col, device_info_count + 3, "=");
729
730         /* footer */
731         table_printf(matrix, 0, device_info_count + 4, "<Total");
732         for (i = 0, col = 1; i < sargs->total_spaces; i++) {
733                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
734                         continue;
735
736                 table_printf(matrix, col++, device_info_count + 4, ">%s",
737                         pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));
738         }
739
740         table_printf(matrix, unallocated_col, device_info_count + 4,
741                         ">%s", pretty_size_mode(total_unused, unit_mode));
742
743         table_printf(matrix, 0, device_info_count + 5, "<Used");
744         for (i = 0, col = 1; i < sargs->total_spaces; i++) {
745                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
746                         continue;
747
748                 table_printf(matrix, col++, device_info_count+5, ">%s",
749                         pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
750         }
751
752         table_dump(matrix);
753         table_free(matrix);
754 }
755
756 /*
757  *  This function prints the unused space per every disk
758  */
759 static void print_unused(struct chunk_info *info_ptr,
760                           int info_count,
761                           struct device_info *device_info_ptr,
762                           int device_info_count,
763                           unsigned unit_mode)
764 {
765         int i;
766         for (i = 0; i < device_info_count; i++) {
767                 int     j;
768                 u64     total = 0;
769
770                 for (j = 0; j < info_count; j++)
771                         if (info_ptr[j].devid == device_info_ptr[i].devid)
772                                 total += calc_chunk_size(info_ptr+j);
773
774                 printf("   %s\t%10s\n",
775                         device_info_ptr[i].path,
776                         pretty_size_mode(device_info_ptr[i].size - total,
777                                 unit_mode));
778         }
779 }
780
781 /*
782  *  This function prints the allocated chunk per every disk
783  */
784 static void print_chunk_device(u64 chunk_type,
785                                 struct chunk_info *chunks_info_ptr,
786                                 int chunks_info_count,
787                                 struct device_info *device_info_ptr,
788                                 int device_info_count,
789                                 unsigned unit_mode)
790 {
791         int i;
792
793         for (i = 0; i < device_info_count; i++) {
794                 int     j;
795                 u64     total = 0;
796
797                 for (j = 0; j < chunks_info_count; j++) {
798
799                         if (chunks_info_ptr[j].type != chunk_type)
800                                 continue;
801                         if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
802                                 continue;
803
804                         total += calc_chunk_size(&(chunks_info_ptr[j]));
805                         //total += chunks_info_ptr[j].size;
806                 }
807
808                 if (total > 0)
809                         printf("   %s\t%10s\n",
810                                 device_info_ptr[i].path,
811                                 pretty_size_mode(total, unit_mode));
812         }
813 }
814
815 /*
816  *  This function print the results of the command "btrfs fi usage"
817  *  in linear format
818  */
819 static void _cmd_filesystem_usage_linear(unsigned unit_mode,
820                                         struct btrfs_ioctl_space_args *sargs,
821                                         struct chunk_info *info_ptr,
822                                         int info_count,
823                                         struct device_info *device_info_ptr,
824                                         int device_info_count)
825 {
826         int i;
827
828         for (i = 0; i < sargs->total_spaces; i++) {
829                 const char *description;
830                 const char *r_mode;
831                 u64 flags = sargs->spaces[i].flags;
832
833                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
834                         continue;
835
836                 description = btrfs_group_type_str(flags);
837                 r_mode = btrfs_group_profile_str(flags);
838
839                 printf("%s,%s: Size:%s, ",
840                         description,
841                         r_mode,
842                         pretty_size_mode(sargs->spaces[i].total_bytes,
843                                 unit_mode));
844                 printf("Used:%s\n",
845                         pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
846                 print_chunk_device(flags, info_ptr, info_count,
847                                 device_info_ptr, device_info_count, unit_mode);
848                 printf("\n");
849         }
850
851         printf("Unallocated:\n");
852         print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
853                         unit_mode);
854 }
855
856 static int print_filesystem_usage_by_chunk(int fd,
857                 struct chunk_info *chunkinfo, int chunkcount,
858                 struct device_info *devinfo, int devcount,
859                 char *path, unsigned unit_mode, int tabular)
860 {
861         struct btrfs_ioctl_space_args *sargs;
862         int ret = 0;
863
864         if (!chunkinfo)
865                 return 0;
866
867         sargs = load_space_info(fd, path);
868         if (!sargs) {
869                 ret = 1;
870                 goto out;
871         }
872
873         if (tabular)
874                 _cmd_filesystem_usage_tabular(unit_mode, sargs, chunkinfo,
875                                 chunkcount, devinfo, devcount);
876         else
877                 _cmd_filesystem_usage_linear(unit_mode, sargs, chunkinfo,
878                                 chunkcount, devinfo, devcount);
879
880         free(sargs);
881 out:
882         return ret;
883 }
884
885 const char * const cmd_filesystem_usage_usage[] = {
886         "btrfs filesystem usage [options] <path> [<path>..]",
887         "Show detailed information about internal filesystem usage .",
888         HELPINFO_OUTPUT_UNIT_DF,
889         "-T                 show data in tabular format",
890         NULL
891 };
892
893 int cmd_filesystem_usage(int argc, char **argv)
894 {
895         int ret = 0;
896         unsigned unit_mode;
897         int i;
898         int more_than_one = 0;
899         int tabular = 0;
900
901         unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
902
903         optind = 1;
904         while (1) {
905                 int c;
906
907                 c = getopt(argc, argv, "T");
908                 if (c < 0)
909                         break;
910
911                 switch (c) {
912                 case 'T':
913                         tabular = 1;
914                         break;
915                 default:
916                         usage(cmd_filesystem_usage_usage);
917                 }
918         }
919
920         if (check_argc_min(argc - optind, 1))
921                 usage(cmd_filesystem_usage_usage);
922
923         for (i = optind; i < argc; i++) {
924                 int fd;
925                 DIR *dirstream = NULL;
926                 struct chunk_info *chunkinfo = NULL;
927                 struct device_info *devinfo = NULL;
928                 int chunkcount = 0;
929                 int devcount = 0;
930
931                 fd = btrfs_open_dir(argv[i], &dirstream, 1);
932                 if (fd < 0) {
933                         ret = 1;
934                         goto out;
935                 }
936                 if (more_than_one)
937                         printf("\n");
938
939                 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
940                                 &devinfo, &devcount);
941                 if (ret)
942                         goto cleanup;
943
944                 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
945                                 devinfo, devcount, argv[i], unit_mode);
946                 if (ret)
947                         goto cleanup;
948                 printf("\n");
949                 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
950                                 devinfo, devcount, argv[i], unit_mode, tabular);
951 cleanup:
952                 close_file_or_dir(fd, dirstream);
953                 free(chunkinfo);
954                 free(devinfo);
955
956                 if (ret)
957                         goto out;
958                 more_than_one = 1;
959         }
960
961 out:
962         return !!ret;
963 }
964
965 void print_device_chunks(int fd, struct device_info *devinfo,
966                 struct chunk_info *chunks_info_ptr,
967                 int chunks_info_count, unsigned unit_mode)
968 {
969         int i;
970         u64 allocated = 0;
971
972         for (i = 0 ; i < chunks_info_count ; i++) {
973                 const char *description;
974                 const char *r_mode;
975                 u64 flags;
976                 u64 size;
977
978                 if (chunks_info_ptr[i].devid != devinfo->devid)
979                         continue;
980
981                 flags = chunks_info_ptr[i].type;
982
983                 description = btrfs_group_type_str(flags);
984                 r_mode = btrfs_group_profile_str(flags);
985                 size = calc_chunk_size(chunks_info_ptr+i);
986                 printf("   %s,%s:%*s%10s\n",
987                         description,
988                         r_mode,
989                         (int)(20 - strlen(description) - strlen(r_mode)), "",
990                         pretty_size_mode(size, unit_mode));
991
992                 allocated += size;
993
994         }
995         printf("   Unallocated: %*s%10s\n",
996                 (int)(20 - strlen("Unallocated")), "",
997                 pretty_size_mode(devinfo->size - allocated, unit_mode));
998 }
999
1000 void print_device_sizes(int fd, struct device_info *devinfo, unsigned unit_mode)
1001 {
1002         printf("   Device size: %*s%10s\n",
1003                 (int)(20 - strlen("Device size")), "",
1004                 pretty_size_mode(devinfo->device_size, unit_mode));
1005 #if 0
1006         /*
1007          * The term has not seen an agreement and we don't want to change it
1008          * once it's in non-development branches or even released.
1009          */
1010         printf("   FS occupied: %*s%10s\n",
1011                 (int)(20 - strlen("FS occupied")), "",
1012                 pretty_size_mode(devinfo->size, unit_mode));
1013 #endif
1014 }