btrfs-progs: ci: update test image packages - add clang and python
[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 #include <fcntl.h>
26
27 #include "utils.h"
28 #include "kerncompat.h"
29 #include "ctree.h"
30 #include "string-table.h"
31 #include "cmds-fi-usage.h"
32 #include "commands.h"
33 #include "disk-io.h"
34
35 #include "version.h"
36 #include "help.h"
37
38 /*
39  * Add the chunk info to the chunk_info list
40  */
41 static int add_info_to_list(struct chunk_info **info_ptr,
42                         int *info_count,
43                         struct btrfs_chunk *chunk)
44 {
45
46         u64 type = btrfs_stack_chunk_type(chunk);
47         u64 size = btrfs_stack_chunk_length(chunk);
48         int num_stripes = btrfs_stack_chunk_num_stripes(chunk);
49         int j;
50
51         for (j = 0 ; j < num_stripes ; j++) {
52                 int i;
53                 struct chunk_info *p = NULL;
54                 struct btrfs_stripe *stripe;
55                 u64    devid;
56
57                 stripe = btrfs_stripe_nr(chunk, j);
58                 devid = btrfs_stack_stripe_devid(stripe);
59
60                 for (i = 0 ; i < *info_count ; i++)
61                         if ((*info_ptr)[i].type == type &&
62                             (*info_ptr)[i].devid == devid &&
63                             (*info_ptr)[i].num_stripes == num_stripes ) {
64                                 p = (*info_ptr) + i;
65                                 break;
66                         }
67
68                 if (!p) {
69                         int tmp = sizeof(struct btrfs_chunk) * (*info_count + 1);
70                         struct chunk_info *res = realloc(*info_ptr, tmp);
71
72                         if (!res) {
73                                 free(*info_ptr);
74                                 error("not enough memory");
75                                 return -ENOMEM;
76                         }
77
78                         *info_ptr = res;
79                         p = res + *info_count;
80                         (*info_count)++;
81
82                         p->devid = devid;
83                         p->type = type;
84                         p->size = 0;
85                         p->num_stripes = num_stripes;
86                 }
87
88                 p->size += size;
89
90         }
91
92         return 0;
93
94 }
95
96 /*
97  *  Helper to sort the chunk type
98  */
99 static int cmp_chunk_block_group(u64 f1, u64 f2)
100 {
101
102         u64 mask;
103
104         if ((f1 & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
105                 (f2 & BTRFS_BLOCK_GROUP_TYPE_MASK))
106                         mask = BTRFS_BLOCK_GROUP_PROFILE_MASK;
107         else if (f2 & BTRFS_BLOCK_GROUP_SYSTEM)
108                         return -1;
109         else if (f1 & BTRFS_BLOCK_GROUP_SYSTEM)
110                         return +1;
111         else
112                         mask = BTRFS_BLOCK_GROUP_TYPE_MASK;
113
114         if ((f1 & mask) > (f2 & mask))
115                 return +1;
116         else if ((f1 & mask) < (f2 & mask))
117                 return -1;
118         else
119                 return 0;
120 }
121
122 /*
123  * Helper to sort the chunk
124  */
125 static int cmp_chunk_info(const void *a, const void *b)
126 {
127         return cmp_chunk_block_group(
128                 ((struct chunk_info *)a)->type,
129                 ((struct chunk_info *)b)->type);
130 }
131
132 static int load_chunk_info(int fd, struct chunk_info **info_ptr, int *info_count)
133 {
134         int ret;
135         struct btrfs_ioctl_search_args args;
136         struct btrfs_ioctl_search_key *sk = &args.key;
137         struct btrfs_ioctl_search_header *sh;
138         unsigned long off = 0;
139         int i, e;
140
141         memset(&args, 0, sizeof(args));
142
143         /*
144          * there may be more than one ROOT_ITEM key if there are
145          * snapshots pending deletion, we have to loop through
146          * them.
147          */
148         sk->tree_id = BTRFS_CHUNK_TREE_OBJECTID;
149
150         sk->min_objectid = 0;
151         sk->max_objectid = (u64)-1;
152         sk->max_type = 0;
153         sk->min_type = (u8)-1;
154         sk->min_offset = 0;
155         sk->max_offset = (u64)-1;
156         sk->min_transid = 0;
157         sk->max_transid = (u64)-1;
158         sk->nr_items = 4096;
159
160         while (1) {
161                 ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
162                 e = errno;
163                 if (e == EPERM)
164                         return -e;
165
166                 if (ret < 0) {
167                         error("cannot look up chunk tree info: %m");
168                         return 1;
169                 }
170                 /* the ioctl returns the number of item it found in nr_items */
171
172                 if (sk->nr_items == 0)
173                         break;
174
175                 off = 0;
176                 for (i = 0; i < sk->nr_items; i++) {
177                         struct btrfs_chunk *item;
178                         sh = (struct btrfs_ioctl_search_header *)(args.buf +
179                                                                   off);
180
181                         off += sizeof(*sh);
182                         item = (struct btrfs_chunk *)(args.buf + off);
183
184                         ret = add_info_to_list(info_ptr, info_count, item);
185                         if (ret) {
186                                 *info_ptr = NULL;
187                                 return 1;
188                         }
189
190                         off += btrfs_search_header_len(sh);
191
192                         sk->min_objectid = btrfs_search_header_objectid(sh);
193                         sk->min_type = btrfs_search_header_type(sh);
194                         sk->min_offset = btrfs_search_header_offset(sh)+1;
195
196                 }
197                 if (!sk->min_offset)    /* overflow */
198                         sk->min_type++;
199                 else
200                         continue;
201
202                 if (!sk->min_type)
203                         sk->min_objectid++;
204                  else
205                         continue;
206
207                 if (!sk->min_objectid)
208                         break;
209         }
210
211         qsort(*info_ptr, *info_count, sizeof(struct chunk_info),
212                 cmp_chunk_info);
213
214         return 0;
215 }
216
217 /*
218  * Helper to sort the struct btrfs_ioctl_space_info
219  */
220 static int cmp_btrfs_ioctl_space_info(const void *a, const void *b)
221 {
222         return cmp_chunk_block_group(
223                 ((struct btrfs_ioctl_space_info *)a)->flags,
224                 ((struct btrfs_ioctl_space_info *)b)->flags);
225 }
226
227 /*
228  * This function load all the information about the space usage
229  */
230 static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path)
231 {
232         struct btrfs_ioctl_space_args *sargs = NULL, *sargs_orig = NULL;
233         int ret, count;
234
235         sargs_orig = sargs = calloc(1, sizeof(struct btrfs_ioctl_space_args));
236         if (!sargs) {
237                 error("not enough memory");
238                 return NULL;
239         }
240
241         sargs->space_slots = 0;
242         sargs->total_spaces = 0;
243
244         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
245         if (ret < 0) {
246                 error("cannot get space info on '%s': %m", path);
247                 free(sargs);
248                 return NULL;
249         }
250         if (!sargs->total_spaces) {
251                 free(sargs);
252                 printf("No chunks found\n");
253                 return NULL;
254         }
255
256         count = sargs->total_spaces;
257
258         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
259                         (count * sizeof(struct btrfs_ioctl_space_info)));
260         if (!sargs) {
261                 free(sargs_orig);
262                 error("not enough memory");
263                 return NULL;
264         }
265
266         sargs->space_slots = count;
267         sargs->total_spaces = 0;
268
269         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
270         if (ret < 0) {
271                 error("cannot get space info with %u slots: %m",
272                         count);
273                 free(sargs);
274                 return NULL;
275         }
276
277         qsort(&(sargs->spaces), count, sizeof(struct btrfs_ioctl_space_info),
278                 cmp_btrfs_ioctl_space_info);
279
280         return sargs;
281 }
282
283 /*
284  * This function computes the space occupied by a *single* RAID5/RAID6 chunk.
285  * The computation is performed on the basis of the number of stripes
286  * which compose the chunk, which could be different from the number of devices
287  * if a disk is added later.
288  */
289 static void get_raid56_used(struct chunk_info *chunks, int chunkcount,
290                 u64 *raid5_used, u64 *raid6_used)
291 {
292         struct chunk_info *info_ptr = chunks;
293         *raid5_used = 0;
294         *raid6_used = 0;
295
296         while (chunkcount-- > 0) {
297                 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID5)
298                         (*raid5_used) += info_ptr->size / (info_ptr->num_stripes - 1);
299                 if (info_ptr->type & BTRFS_BLOCK_GROUP_RAID6)
300                         (*raid6_used) += info_ptr->size / (info_ptr->num_stripes - 2);
301                 info_ptr++;
302         }
303 }
304
305 #define MIN_UNALOCATED_THRESH   SZ_16M
306 static int print_filesystem_usage_overall(int fd, struct chunk_info *chunkinfo,
307                 int chunkcount, struct device_info *devinfo, int devcount,
308                 char *path, unsigned unit_mode)
309 {
310         struct btrfs_ioctl_space_args *sargs = NULL;
311         int i;
312         int ret = 0;
313         int width = 10;         /* default 10 for human units */
314         /*
315          * r_* prefix is for raw data
316          * l_* is for logical
317          */
318         u64 r_total_size = 0;   /* filesystem size, sum of device sizes */
319         u64 r_total_chunks = 0; /* sum of chunks sizes on disk(s) */
320         u64 r_total_used = 0;
321         u64 r_total_unused = 0;
322         u64 r_total_missing = 0;        /* sum of missing devices size */
323         u64 r_data_used = 0;
324         u64 r_data_chunks = 0;
325         u64 l_data_chunks = 0;
326         u64 r_metadata_used = 0;
327         u64 r_metadata_chunks = 0;
328         u64 l_metadata_chunks = 0;
329         u64 r_system_used = 0;
330         u64 r_system_chunks = 0;
331         double data_ratio;
332         double metadata_ratio;
333         /* logical */
334         u64 raid5_used = 0;
335         u64 raid6_used = 0;
336         u64 l_global_reserve = 0;
337         u64 l_global_reserve_used = 0;
338         u64 free_estimated = 0;
339         u64 free_min = 0;
340         int max_data_ratio = 1;
341         int mixed = 0;
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': %m", path);
358
359                 ret = 1;
360                 goto exit;
361         }
362         get_raid56_used(chunkinfo, chunkcount, &raid5_used, &raid6_used);
363
364         for (i = 0; i < sargs->total_spaces; i++) {
365                 int ratio;
366                 u64 flags = sargs->spaces[i].flags;
367
368                 /*
369                  * The raid5/raid6 ratio depends by the stripes number
370                  * used by every chunk. It is computed separately
371                  */
372                 if (flags & BTRFS_BLOCK_GROUP_RAID0)
373                         ratio = 1;
374                 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
375                         ratio = 2;
376                 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
377                         ratio = 0;
378                 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
379                         ratio = 0;
380                 else if (flags & BTRFS_BLOCK_GROUP_DUP)
381                         ratio = 2;
382                 else if (flags & BTRFS_BLOCK_GROUP_RAID10)
383                         ratio = 2;
384                 else
385                         ratio = 1;
386
387                 if (!ratio)
388                         warning("RAID56 detected, not implemented");
389
390                 if (ratio > max_data_ratio)
391                         max_data_ratio = ratio;
392
393                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV) {
394                         l_global_reserve = sargs->spaces[i].total_bytes;
395                         l_global_reserve_used = sargs->spaces[i].used_bytes;
396                 }
397                 if ((flags & (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA))
398                     == (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA)) {
399                         mixed = 1;
400                 }
401                 if (flags & BTRFS_BLOCK_GROUP_DATA) {
402                         r_data_used += sargs->spaces[i].used_bytes * ratio;
403                         r_data_chunks += sargs->spaces[i].total_bytes * ratio;
404                         l_data_chunks += sargs->spaces[i].total_bytes;
405                 }
406                 if (flags & BTRFS_BLOCK_GROUP_METADATA) {
407                         r_metadata_used += sargs->spaces[i].used_bytes * ratio;
408                         r_metadata_chunks += sargs->spaces[i].total_bytes * ratio;
409                         l_metadata_chunks += sargs->spaces[i].total_bytes;
410                 }
411                 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
412                         r_system_used += sargs->spaces[i].used_bytes * ratio;
413                         r_system_chunks += sargs->spaces[i].total_bytes * ratio;
414                 }
415         }
416
417         r_total_chunks = r_data_chunks + r_system_chunks;
418         r_total_used = r_data_used + r_system_used;
419         if (!mixed) {
420                 r_total_chunks += r_metadata_chunks;
421                 r_total_used += r_metadata_used;
422         }
423         r_total_unused = r_total_size - r_total_chunks;
424
425         /* Raw / Logical = raid factor, >= 1 */
426         data_ratio = (double)r_data_chunks / l_data_chunks;
427         if (mixed)
428                 metadata_ratio = data_ratio;
429         else
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         /*
448          * For mixed-bg the metadata are left out in calculations thus global
449          * reserve would be lost. Part of it could be permanently allocated,
450          * we have to subtract the used bytes so we don't go under zero free.
451          */
452         if (mixed)
453                 free_estimated -= l_global_reserve - l_global_reserve_used;
454         free_min = free_estimated;
455
456         /* Chop unallocatable space */
457         /* FIXME: must be applied per device */
458         if (r_total_unused >= MIN_UNALOCATED_THRESH) {
459                 free_estimated += r_total_unused / data_ratio;
460                 /* Match the calculation of 'df', use the highest raid ratio */
461                 free_min += r_total_unused / max_data_ratio;
462         }
463
464         if (unit_mode != UNITS_HUMAN)
465                 width = 18;
466
467         printf("Overall:\n");
468
469         printf("    Device size:\t\t%*s\n", width,
470                 pretty_size_mode(r_total_size, unit_mode));
471         printf("    Device allocated:\t\t%*s\n", width,
472                 pretty_size_mode(r_total_chunks, unit_mode));
473         printf("    Device unallocated:\t\t%*s\n", width,
474                 pretty_size_mode(r_total_unused, unit_mode | UNITS_NEGATIVE));
475         printf("    Device missing:\t\t%*s\n", width,
476                 pretty_size_mode(r_total_missing, unit_mode));
477         printf("    Used:\t\t\t%*s\n", width,
478                 pretty_size_mode(r_total_used, unit_mode));
479         printf("    Free (estimated):\t\t%*s\t(",
480                 width,
481                 pretty_size_mode(free_estimated, unit_mode));
482         printf("min: %s)\n", pretty_size_mode(free_min, unit_mode));
483         printf("    Data ratio:\t\t\t%*.2f\n",
484                 width, data_ratio);
485         printf("    Metadata ratio:\t\t%*.2f\n",
486                 width, metadata_ratio);
487         printf("    Global reserve:\t\t%*s\t(used: %s)\n", width,
488                 pretty_size_mode(l_global_reserve, unit_mode),
489                 pretty_size_mode(l_global_reserve_used, unit_mode));
490
491 exit:
492
493         if (sargs)
494                 free(sargs);
495
496         return ret;
497 }
498
499 /*
500  *  Helper to sort the device_info structure
501  */
502 static int cmp_device_info(const void *a, const void *b)
503 {
504         return strcmp(((struct device_info *)a)->path,
505                         ((struct device_info *)b)->path);
506 }
507
508 int dev_to_fsid(const char *dev, u8 *fsid)
509 {
510         struct btrfs_super_block *disk_super;
511         char buf[BTRFS_SUPER_INFO_SIZE];
512         int ret;
513         int fd;
514
515         fd = open(dev, O_RDONLY);
516         if (fd < 0) {
517                 ret = -errno;
518                 return ret;
519         }
520
521         disk_super = (struct btrfs_super_block *)buf;
522         ret = btrfs_read_dev_super(fd, disk_super,
523                                    BTRFS_SUPER_INFO_OFFSET, SBREAD_DEFAULT);
524         if (ret)
525                 goto out;
526
527         memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
528         ret = 0;
529
530 out:
531         close(fd);
532         return ret;
533 }
534
535 /*
536  *  This function loads the device_info structure and put them in an array
537  */
538 static int load_device_info(int fd, struct device_info **device_info_ptr,
539                            int *device_info_count)
540 {
541         int ret, i, ndevs;
542         struct btrfs_ioctl_fs_info_args fi_args;
543         struct btrfs_ioctl_dev_info_args dev_info;
544         struct device_info *info;
545         u8 fsid[BTRFS_UUID_SIZE];
546
547         *device_info_count = 0;
548         *device_info_ptr = NULL;
549
550         ret = ioctl(fd, BTRFS_IOC_FS_INFO, &fi_args);
551         if (ret < 0) {
552                 if (errno == EPERM)
553                         return -errno;
554                 error("cannot get filesystem info: %m");
555                 return 1;
556         }
557
558         info = calloc(fi_args.num_devices, sizeof(struct device_info));
559         if (!info) {
560                 error("not enough memory");
561                 return 1;
562         }
563
564         for (i = 0, ndevs = 0 ; i <= fi_args.max_id ; i++) {
565                 if (ndevs >= fi_args.num_devices) {
566                         error("unexpected number of devices: %d >= %llu", ndevs,
567                                 (unsigned long long)fi_args.num_devices);
568                         error(
569                 "if seed device is used, try running this command as root");
570                         goto out;
571                 }
572                 memset(&dev_info, 0, sizeof(dev_info));
573                 ret = get_device_info(fd, i, &dev_info);
574
575                 if (ret == -ENODEV)
576                         continue;
577                 if (ret) {
578                         error("cannot get info about device devid=%d", i);
579                         goto out;
580                 }
581
582                 /*
583                  * Skip seed device by checking device's fsid (requires root).
584                  * And we will skip only if dev_to_fsid is successful and dev
585                  * is a seed device.
586                  * Ignore any other error including -EACCES, which is seen when
587                  * a non-root process calls dev_to_fsid(path)->open(path).
588                  */
589                 ret = dev_to_fsid((const char *)dev_info.path, fsid);
590                 if (!ret && memcmp(fi_args.fsid, fsid, BTRFS_FSID_SIZE) != 0)
591                         continue;
592
593                 info[ndevs].devid = dev_info.devid;
594                 if (!dev_info.path[0]) {
595                         strcpy(info[ndevs].path, "missing");
596                 } else {
597                         strcpy(info[ndevs].path, (char *)dev_info.path);
598                         info[ndevs].device_size =
599                                 get_partition_size((char *)dev_info.path);
600                 }
601                 info[ndevs].size = dev_info.total_bytes;
602                 ++ndevs;
603         }
604
605         if (ndevs != fi_args.num_devices) {
606                 error("unexpected number of devices: %d != %llu", ndevs,
607                                 (unsigned long long)fi_args.num_devices);
608                 goto out;
609         }
610
611         qsort(info, fi_args.num_devices,
612                 sizeof(struct device_info), cmp_device_info);
613
614         *device_info_count = fi_args.num_devices;
615         *device_info_ptr = info;
616
617         return 0;
618
619 out:
620         free(info);
621         return ret;
622 }
623
624 int load_chunk_and_device_info(int fd, struct chunk_info **chunkinfo,
625                 int *chunkcount, struct device_info **devinfo, int *devcount)
626 {
627         int ret;
628
629         ret = load_chunk_info(fd, chunkinfo, chunkcount);
630         if (ret == -EPERM) {
631                 warning(
632 "cannot read detailed chunk info, RAID5/6 numbers will be incorrect, run as root");
633         } else if (ret) {
634                 return ret;
635         }
636
637         ret = load_device_info(fd, devinfo, devcount);
638         if (ret == -EPERM) {
639                 warning(
640                 "cannot get filesystem info from ioctl(FS_INFO), run as root");
641                 ret = 0;
642         }
643
644         return ret;
645 }
646
647 /*
648  *  This function computes the size of a chunk in a disk
649  */
650 static u64 calc_chunk_size(struct chunk_info *ci)
651 {
652         if (ci->type & BTRFS_BLOCK_GROUP_RAID0)
653                 return ci->size / ci->num_stripes;
654         else if (ci->type & BTRFS_BLOCK_GROUP_RAID1)
655                 return ci->size ;
656         else if (ci->type & BTRFS_BLOCK_GROUP_DUP)
657                 return ci->size ;
658         else if (ci->type & BTRFS_BLOCK_GROUP_RAID5)
659                 return ci->size / (ci->num_stripes -1);
660         else if (ci->type & BTRFS_BLOCK_GROUP_RAID6)
661                 return ci->size / (ci->num_stripes -2);
662         else if (ci->type & BTRFS_BLOCK_GROUP_RAID10)
663                 return ci->size / ci->num_stripes;
664         return ci->size;
665 }
666
667 /*
668  *  This function print the results of the command "btrfs fi usage"
669  *  in tabular format
670  */
671 static void _cmd_filesystem_usage_tabular(unsigned unit_mode,
672                                         struct btrfs_ioctl_space_args *sargs,
673                                         struct chunk_info *chunks_info_ptr,
674                                         int chunks_info_count,
675                                         struct device_info *device_info_ptr,
676                                         int device_info_count)
677 {
678         int i;
679         u64 total_unused = 0;
680         struct string_table *matrix = NULL;
681         int  ncols, nrows;
682         int col;
683         int unallocated_col;
684         int spaceinfos_col;
685         const int vhdr_skip = 3;        /* amount of vertical header space */
686
687         /* id, path, unallocated */
688         ncols = 3;
689         spaceinfos_col = 2;
690         /* Properly count the real space infos */
691         for (i = 0; i < sargs->total_spaces; i++) {
692                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
693                         continue;
694                 ncols++;
695         }
696
697         /* 2 for header, empty line, devices, ===, total, used */
698         nrows = vhdr_skip + device_info_count + 1 + 2;
699
700         matrix = table_create(ncols, nrows);
701         if (!matrix) {
702                 error("not enough memory");
703                 return;
704         }
705
706         /*
707          * We have to skip the global block reserve everywhere as it's an
708          * artificial blockgroup
709          */
710
711         /* header */
712         for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
713                 u64 flags = sargs->spaces[i].flags;
714
715                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
716                         continue;
717
718                 table_printf(matrix, col, 0, "<%s",
719                                 btrfs_group_type_str(flags));
720                 table_printf(matrix, col, 1, "<%s",
721                                 btrfs_group_profile_str(flags));
722                 col++;
723         }
724         unallocated_col = col;
725
726         table_printf(matrix, 0, 1, "<Id");
727         table_printf(matrix, 1, 1, "<Path");
728         table_printf(matrix, unallocated_col, 1, "<Unallocated");
729
730         /* body */
731         for (i = 0; i < device_info_count; i++) {
732                 int k;
733                 char *p;
734
735                 u64  total_allocated = 0, unused;
736
737                 p = strrchr(device_info_ptr[i].path, '/');
738                 if (!p)
739                         p = device_info_ptr[i].path;
740                 else
741                         p++;
742
743                 table_printf(matrix, 0, vhdr_skip + i, ">%llu",
744                                 device_info_ptr[i].devid);
745                 table_printf(matrix, 1, vhdr_skip + i, "<%s",
746                                 device_info_ptr[i].path);
747
748                 for (col = spaceinfos_col, k = 0; k < sargs->total_spaces; k++) {
749                         u64     flags = sargs->spaces[k].flags;
750                         u64 devid = device_info_ptr[i].devid;
751                         int     j;
752                         u64 size = 0;
753
754                         if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
755                                 continue;
756
757                         for (j = 0 ; j < chunks_info_count ; j++) {
758                                 if (chunks_info_ptr[j].type != flags )
759                                                 continue;
760                                 if (chunks_info_ptr[j].devid != devid)
761                                                 continue;
762
763                                 size += calc_chunk_size(chunks_info_ptr+j);
764                         }
765
766                         if (size)
767                                 table_printf(matrix, col, vhdr_skip+ i,
768                                         ">%s", pretty_size_mode(size, unit_mode));
769                         else
770                                 table_printf(matrix, col, vhdr_skip + i, ">-");
771
772                         total_allocated += size;
773                         col++;
774                 }
775
776                 unused = get_partition_size(device_info_ptr[i].path)
777                                 - total_allocated;
778
779                 table_printf(matrix, unallocated_col, vhdr_skip + i, ">%s",
780                         pretty_size_mode(unused, unit_mode | UNITS_NEGATIVE));
781                 total_unused += unused;
782
783         }
784
785         for (i = 0; i < spaceinfos_col; i++) {
786                 table_printf(matrix, i, vhdr_skip - 1, "*-");
787                 table_printf(matrix, i, vhdr_skip + device_info_count, "*-");
788         }
789
790         for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
791                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
792                         continue;
793
794                 table_printf(matrix, col, vhdr_skip - 1, "*-");
795                 table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
796                 col++;
797         }
798         /* One for Unallocated */
799         table_printf(matrix, col, vhdr_skip - 1, "*-");
800         table_printf(matrix, col, vhdr_skip + device_info_count, "*-");
801
802         /* footer */
803         table_printf(matrix, 1, vhdr_skip + device_info_count + 1, "<Total");
804         for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
805                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
806                         continue;
807
808                 table_printf(matrix, col++, vhdr_skip + device_info_count + 1,
809                         ">%s",
810                         pretty_size_mode(sargs->spaces[i].total_bytes, unit_mode));
811         }
812
813         table_printf(matrix, unallocated_col, vhdr_skip + device_info_count + 1,
814                 ">%s",
815                 pretty_size_mode(total_unused, unit_mode | UNITS_NEGATIVE));
816
817         table_printf(matrix, 1, vhdr_skip + device_info_count + 2, "<Used");
818         for (i = 0, col = spaceinfos_col; i < sargs->total_spaces; i++) {
819                 if (sargs->spaces[i].flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
820                         continue;
821
822                 table_printf(matrix, col++, vhdr_skip + device_info_count + 2,
823                         ">%s",
824                         pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
825         }
826
827         table_dump(matrix);
828         table_free(matrix);
829 }
830
831 /*
832  *  This function prints the unused space per every disk
833  */
834 static void print_unused(struct chunk_info *info_ptr,
835                           int info_count,
836                           struct device_info *device_info_ptr,
837                           int device_info_count,
838                           unsigned unit_mode)
839 {
840         int i;
841         for (i = 0; i < device_info_count; i++) {
842                 int     j;
843                 u64     total = 0;
844
845                 for (j = 0; j < info_count; j++)
846                         if (info_ptr[j].devid == device_info_ptr[i].devid)
847                                 total += calc_chunk_size(info_ptr+j);
848
849                 printf("   %s\t%10s\n",
850                         device_info_ptr[i].path,
851                         pretty_size_mode(device_info_ptr[i].size - total,
852                                 unit_mode));
853         }
854 }
855
856 /*
857  *  This function prints the allocated chunk per every disk
858  */
859 static void print_chunk_device(u64 chunk_type,
860                                 struct chunk_info *chunks_info_ptr,
861                                 int chunks_info_count,
862                                 struct device_info *device_info_ptr,
863                                 int device_info_count,
864                                 unsigned unit_mode)
865 {
866         int i;
867
868         for (i = 0; i < device_info_count; i++) {
869                 int     j;
870                 u64     total = 0;
871
872                 for (j = 0; j < chunks_info_count; j++) {
873
874                         if (chunks_info_ptr[j].type != chunk_type)
875                                 continue;
876                         if (chunks_info_ptr[j].devid != device_info_ptr[i].devid)
877                                 continue;
878
879                         total += calc_chunk_size(&(chunks_info_ptr[j]));
880                         //total += chunks_info_ptr[j].size;
881                 }
882
883                 if (total > 0)
884                         printf("   %s\t%10s\n",
885                                 device_info_ptr[i].path,
886                                 pretty_size_mode(total, unit_mode));
887         }
888 }
889
890 /*
891  *  This function print the results of the command "btrfs fi usage"
892  *  in linear format
893  */
894 static void _cmd_filesystem_usage_linear(unsigned unit_mode,
895                                         struct btrfs_ioctl_space_args *sargs,
896                                         struct chunk_info *info_ptr,
897                                         int info_count,
898                                         struct device_info *device_info_ptr,
899                                         int device_info_count)
900 {
901         int i;
902
903         for (i = 0; i < sargs->total_spaces; i++) {
904                 const char *description;
905                 const char *r_mode;
906                 u64 flags = sargs->spaces[i].flags;
907
908                 if (flags & BTRFS_SPACE_INFO_GLOBAL_RSV)
909                         continue;
910
911                 description = btrfs_group_type_str(flags);
912                 r_mode = btrfs_group_profile_str(flags);
913
914                 printf("%s,%s: Size:%s, ",
915                         description,
916                         r_mode,
917                         pretty_size_mode(sargs->spaces[i].total_bytes,
918                                 unit_mode));
919                 printf("Used:%s\n",
920                         pretty_size_mode(sargs->spaces[i].used_bytes, unit_mode));
921                 print_chunk_device(flags, info_ptr, info_count,
922                                 device_info_ptr, device_info_count, unit_mode);
923                 printf("\n");
924         }
925
926         printf("Unallocated:\n");
927         print_unused(info_ptr, info_count, device_info_ptr, device_info_count,
928                         unit_mode | UNITS_NEGATIVE);
929 }
930
931 static int print_filesystem_usage_by_chunk(int fd,
932                 struct chunk_info *chunkinfo, int chunkcount,
933                 struct device_info *devinfo, int devcount,
934                 char *path, unsigned unit_mode, int tabular)
935 {
936         struct btrfs_ioctl_space_args *sargs;
937         int ret = 0;
938
939         if (!chunkinfo)
940                 return 0;
941
942         sargs = load_space_info(fd, path);
943         if (!sargs) {
944                 ret = 1;
945                 goto out;
946         }
947
948         if (tabular)
949                 _cmd_filesystem_usage_tabular(unit_mode, sargs, chunkinfo,
950                                 chunkcount, devinfo, devcount);
951         else
952                 _cmd_filesystem_usage_linear(unit_mode, sargs, chunkinfo,
953                                 chunkcount, devinfo, devcount);
954
955         free(sargs);
956 out:
957         return ret;
958 }
959
960 const char * const cmd_filesystem_usage_usage[] = {
961         "btrfs filesystem usage [options] <path> [<path>..]",
962         "Show detailed information about internal filesystem usage .",
963         HELPINFO_UNITS_SHORT_LONG,
964         "-T                 show data in tabular format",
965         NULL
966 };
967
968 int cmd_filesystem_usage(int argc, char **argv)
969 {
970         int ret = 0;
971         unsigned unit_mode;
972         int i;
973         int more_than_one = 0;
974         int tabular = 0;
975
976         unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
977
978         while (1) {
979                 int c;
980
981                 c = getopt(argc, argv, "T");
982                 if (c < 0)
983                         break;
984
985                 switch (c) {
986                 case 'T':
987                         tabular = 1;
988                         break;
989                 default:
990                         usage(cmd_filesystem_usage_usage);
991                 }
992         }
993
994         if (check_argc_min(argc - optind, 1))
995                 usage(cmd_filesystem_usage_usage);
996
997         for (i = optind; i < argc; i++) {
998                 int fd;
999                 DIR *dirstream = NULL;
1000                 struct chunk_info *chunkinfo = NULL;
1001                 struct device_info *devinfo = NULL;
1002                 int chunkcount = 0;
1003                 int devcount = 0;
1004
1005                 fd = btrfs_open_dir(argv[i], &dirstream, 1);
1006                 if (fd < 0) {
1007                         ret = 1;
1008                         goto out;
1009                 }
1010                 if (more_than_one)
1011                         printf("\n");
1012
1013                 ret = load_chunk_and_device_info(fd, &chunkinfo, &chunkcount,
1014                                 &devinfo, &devcount);
1015                 if (ret)
1016                         goto cleanup;
1017
1018                 ret = print_filesystem_usage_overall(fd, chunkinfo, chunkcount,
1019                                 devinfo, devcount, argv[i], unit_mode);
1020                 if (ret)
1021                         goto cleanup;
1022                 printf("\n");
1023                 ret = print_filesystem_usage_by_chunk(fd, chunkinfo, chunkcount,
1024                                 devinfo, devcount, argv[i], unit_mode, tabular);
1025 cleanup:
1026                 close_file_or_dir(fd, dirstream);
1027                 free(chunkinfo);
1028                 free(devinfo);
1029
1030                 if (ret)
1031                         goto out;
1032                 more_than_one = 1;
1033         }
1034
1035 out:
1036         return !!ret;
1037 }
1038
1039 void print_device_chunks(struct device_info *devinfo,
1040                 struct chunk_info *chunks_info_ptr,
1041                 int chunks_info_count, unsigned unit_mode)
1042 {
1043         int i;
1044         u64 allocated = 0;
1045
1046         for (i = 0 ; i < chunks_info_count ; i++) {
1047                 const char *description;
1048                 const char *r_mode;
1049                 u64 flags;
1050                 u64 size;
1051
1052                 if (chunks_info_ptr[i].devid != devinfo->devid)
1053                         continue;
1054
1055                 flags = chunks_info_ptr[i].type;
1056
1057                 description = btrfs_group_type_str(flags);
1058                 r_mode = btrfs_group_profile_str(flags);
1059                 size = calc_chunk_size(chunks_info_ptr+i);
1060                 printf("   %s,%s:%*s%10s\n",
1061                         description,
1062                         r_mode,
1063                         (int)(20 - strlen(description) - strlen(r_mode)), "",
1064                         pretty_size_mode(size, unit_mode));
1065
1066                 allocated += size;
1067
1068         }
1069         printf("   Unallocated: %*s%10s\n",
1070                 (int)(20 - strlen("Unallocated")), "",
1071                 pretty_size_mode(devinfo->size - allocated,
1072                         unit_mode | UNITS_NEGATIVE));
1073 }
1074
1075 void print_device_sizes(struct device_info *devinfo, unsigned unit_mode)
1076 {
1077         printf("   Device size: %*s%10s\n",
1078                 (int)(20 - strlen("Device size")), "",
1079                 pretty_size_mode(devinfo->device_size, unit_mode));
1080         printf("   Device slack: %*s%10s\n",
1081                 (int)(20 - strlen("Device slack")), "",
1082                 pretty_size_mode(devinfo->device_size > 0 ?
1083                         devinfo->device_size - devinfo->size : 0,
1084                         unit_mode));
1085 }