btrfs-progs: move out print in cmd_df to another function
[platform/upstream/btrfs-progs.git] / cmds-filesystem.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 <uuid/uuid.h>
24 #include <ctype.h>
25
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "ioctl.h"
29 #include "utils.h"
30 #include "volumes.h"
31 #include "version.h"
32 #include "commands.h"
33 #include "list_sort.h"
34
35 static const char * const filesystem_cmd_group_usage[] = {
36         "btrfs filesystem [<group>] <command> [<args>]",
37         NULL
38 };
39
40 static const char * const cmd_df_usage[] = {
41         "btrfs filesystem df <path>",
42         "Show space usage information for a mount point",
43         NULL
44 };
45
46 static char *group_type_str(u64 flag)
47 {
48         switch (flag & BTRFS_BLOCK_GROUP_TYPE_MASK) {
49         case BTRFS_BLOCK_GROUP_DATA:
50                 return "Data";
51         case BTRFS_BLOCK_GROUP_SYSTEM:
52                 return "System";
53         case BTRFS_BLOCK_GROUP_METADATA:
54                 return "Metadata";
55         case BTRFS_BLOCK_GROUP_DATA|BTRFS_BLOCK_GROUP_METADATA:
56                 return "Data+Metadata";
57         default:
58                 return "unknown";
59         }
60 }
61
62 static char *group_profile_str(u64 flag)
63 {
64         switch (flag & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
65         case 0:
66                 return "single";
67         case BTRFS_BLOCK_GROUP_RAID0:
68                 return "RAID0";
69         case BTRFS_BLOCK_GROUP_RAID1:
70                 return "RAID1";
71         case BTRFS_BLOCK_GROUP_RAID5:
72                 return "RAID5";
73         case BTRFS_BLOCK_GROUP_RAID6:
74                 return "RAID6";
75         case BTRFS_BLOCK_GROUP_DUP:
76                 return "DUP";
77         case BTRFS_BLOCK_GROUP_RAID10:
78                 return "RAID10";
79         default:
80                 return "unknown";
81         }
82 }
83
84 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
85 {
86         u64 count = 0;
87         int ret, e;
88         struct btrfs_ioctl_space_args *sargs;
89
90         sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
91         if (!sargs)
92                 return -ENOMEM;
93
94         sargs->space_slots = 0;
95         sargs->total_spaces = 0;
96
97         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
98         e = errno;
99         if (ret) {
100                 fprintf(stderr, "ERROR: couldn't get space info - %s\n",
101                         strerror(e));
102                 free(sargs);
103                 return ret;
104         }
105         if (!sargs->total_spaces) {
106                 free(sargs);
107                 return 0;
108         }
109         count = sargs->total_spaces;
110         free(sargs);
111
112         sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
113                         (count * sizeof(struct btrfs_ioctl_space_info)));
114         if (!sargs)
115                 ret = -ENOMEM;
116
117         sargs->space_slots = count;
118         sargs->total_spaces = 0;
119         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
120         e = errno;
121         if (ret) {
122                 fprintf(stderr, "ERROR: get space info count %llu - %s\n",
123                                 count, strerror(e));
124                 free(sargs);
125                 return ret;
126         }
127         *sargs_ret = sargs;
128         return 0;
129 }
130
131 static void print_df(struct btrfs_ioctl_space_args *sargs)
132 {
133         u64 i;
134         struct btrfs_ioctl_space_info *sp = sargs->spaces;
135
136         for (i = 0; i < sargs->total_spaces; i++, sp++) {
137                 printf("%s, %s: total=%s, used=%s\n",
138                         group_type_str(sp->flags),
139                         group_profile_str(sp->flags),
140                         pretty_size(sp->total_bytes),
141                         pretty_size(sp->used_bytes));
142         }
143 }
144
145 static int cmd_df(int argc, char **argv)
146 {
147         struct btrfs_ioctl_space_args *sargs = NULL;
148         int ret;
149         int fd;
150         char *path;
151         DIR  *dirstream = NULL;
152
153         if (check_argc_exact(argc, 2))
154                 usage(cmd_df_usage);
155
156         path = argv[1];
157
158         fd = open_file_or_dir(path, &dirstream);
159         if (fd < 0) {
160                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
161                 return 1;
162         }
163         ret = get_df(fd, &sargs);
164
165         if (!ret && sargs) {
166                 print_df(sargs);
167                 free(sargs);
168         } else {
169                 fprintf(stderr, "ERROR: get_df failed %s\n", strerror(ret));
170         }
171
172         close_file_or_dir(fd, dirstream);
173         return !!ret;
174 }
175
176 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
177 {
178         char uuidbuf[37];
179         struct list_head *cur;
180         struct btrfs_device *device;
181         int search_len = strlen(search);
182
183         search_len = min(search_len, 37);
184         uuid_unparse(fs_devices->fsid, uuidbuf);
185         if (!strncmp(uuidbuf, search, search_len))
186                 return 1;
187
188         list_for_each(cur, &fs_devices->devices) {
189                 device = list_entry(cur, struct btrfs_device, dev_list);
190                 if ((device->label && strcmp(device->label, search) == 0) ||
191                     strcmp(device->name, search) == 0)
192                         return 1;
193         }
194         return 0;
195 }
196
197 /*
198  * Sort devices by devid, ascending
199  */
200 static int cmp_device_id(void *priv, struct list_head *a,
201                 struct list_head *b)
202 {
203         const struct btrfs_device *da = list_entry(a, struct btrfs_device,
204                         dev_list);
205         const struct btrfs_device *db = list_entry(b, struct btrfs_device,
206                         dev_list);
207
208         return da->devid < db->devid ? -1 :
209                 da->devid > db->devid ? 1 : 0;
210 }
211
212 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
213 {
214         char uuidbuf[37];
215         struct list_head *cur;
216         struct btrfs_device *device;
217         u64 devs_found = 0;
218         u64 total;
219
220         uuid_unparse(fs_devices->fsid, uuidbuf);
221         device = list_entry(fs_devices->devices.next, struct btrfs_device,
222                             dev_list);
223         if (device->label && device->label[0])
224                 printf("Label: '%s' ", device->label);
225         else
226                 printf("Label: none ");
227
228
229         total = device->total_devs;
230         printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
231                (unsigned long long)total,
232                pretty_size(device->super_bytes_used));
233
234         list_sort(NULL, &fs_devices->devices, cmp_device_id);
235         list_for_each(cur, &fs_devices->devices) {
236                 device = list_entry(cur, struct btrfs_device, dev_list);
237
238                 printf("\tdevid %4llu size %s used %s path %s\n",
239                        (unsigned long long)device->devid,
240                        pretty_size(device->total_bytes),
241                        pretty_size(device->bytes_used), device->name);
242
243                 devs_found++;
244         }
245         if (devs_found < total) {
246                 printf("\t*** Some devices missing\n");
247         }
248         printf("\n");
249 }
250
251 static const char * const cmd_show_usage[] = {
252         "btrfs filesystem show [--all-devices|<uuid>]",
253         "Show the structure of a filesystem",
254         "If no argument is given, structure of all present filesystems is shown.",
255         NULL
256 };
257
258 static int cmd_show(int argc, char **argv)
259 {
260         struct list_head *all_uuids;
261         struct btrfs_fs_devices *fs_devices;
262         struct list_head *cur_uuid;
263         char *search = NULL;
264         int ret;
265         int where = BTRFS_SCAN_PROC;
266         int searchstart = 1;
267
268         if( argc > 1 && !strcmp(argv[1],"--all-devices")){
269                 where = BTRFS_SCAN_DEV;
270                 searchstart += 1;
271         }
272
273         if (check_argc_max(argc, searchstart + 1))
274                 usage(cmd_show_usage);
275
276         ret = scan_for_btrfs(where, 0);
277
278         if (ret){
279                 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
280                 return 1;
281         }
282         
283         if(searchstart < argc)
284                 search = argv[searchstart];
285
286         all_uuids = btrfs_scanned_uuids();
287         list_for_each(cur_uuid, all_uuids) {
288                 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
289                                         list);
290                 if (search && uuid_search(fs_devices, search) == 0)
291                         continue;
292                 print_one_uuid(fs_devices);
293         }
294         printf("%s\n", BTRFS_BUILD_VERSION);
295         return 0;
296 }
297
298 static const char * const cmd_sync_usage[] = {
299         "btrfs filesystem sync <path>",
300         "Force a sync on a filesystem",
301         NULL
302 };
303
304 static int cmd_sync(int argc, char **argv)
305 {
306         int     fd, res, e;
307         char    *path;
308         DIR     *dirstream = NULL;
309
310         if (check_argc_exact(argc, 2))
311                 usage(cmd_sync_usage);
312
313         path = argv[1];
314
315         fd = open_file_or_dir(path, &dirstream);
316         if (fd < 0) {
317                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
318                 return 1;
319         }
320
321         printf("FSSync '%s'\n", path);
322         res = ioctl(fd, BTRFS_IOC_SYNC);
323         e = errno;
324         close_file_or_dir(fd, dirstream);
325         if( res < 0 ){
326                 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n", 
327                         path, strerror(e));
328                 return 1;
329         }
330
331         return 0;
332 }
333
334 static int parse_compress_type(char *s)
335 {
336         if (strcmp(optarg, "zlib") == 0)
337                 return BTRFS_COMPRESS_ZLIB;
338         else if (strcmp(optarg, "lzo") == 0)
339                 return BTRFS_COMPRESS_LZO;
340         else {
341                 fprintf(stderr, "Unknown compress type %s\n", s);
342                 exit(1);
343         };
344 }
345
346 static const char * const cmd_defrag_usage[] = {
347         "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
348         "Defragment a file or a directory",
349         "",
350         "-v             be verbose",
351         "-c[zlib,lzo]   compress the file while defragmenting",
352         "-f             flush data to disk immediately after defragmenting",
353         "-s start       defragment only from byte onward",
354         "-l len         defragment only up to len bytes",
355         "-t size        minimal size of file to be considered for defragmenting",
356         NULL
357 };
358
359 static int cmd_defrag(int argc, char **argv)
360 {
361         int fd;
362         int flush = 0;
363         u64 start = 0;
364         u64 len = (u64)-1;
365         u32 thresh = 0;
366         int i;
367         int errors = 0;
368         int ret = 0;
369         int verbose = 0;
370         int fancy_ioctl = 0;
371         struct btrfs_ioctl_defrag_range_args range;
372         int e=0;
373         int compress_type = BTRFS_COMPRESS_NONE;
374         DIR *dirstream = NULL;
375
376         optind = 1;
377         while(1) {
378                 int c = getopt(argc, argv, "vc::fs:l:t:");
379                 if (c < 0)
380                         break;
381
382                 switch(c) {
383                 case 'c':
384                         compress_type = BTRFS_COMPRESS_ZLIB;
385                         if (optarg)
386                                 compress_type = parse_compress_type(optarg);
387                         fancy_ioctl = 1;
388                         break;
389                 case 'f':
390                         flush = 1;
391                         fancy_ioctl = 1;
392                         break;
393                 case 'v':
394                         verbose = 1;
395                         break;
396                 case 's':
397                         start = parse_size(optarg);
398                         fancy_ioctl = 1;
399                         break;
400                 case 'l':
401                         len = parse_size(optarg);
402                         fancy_ioctl = 1;
403                         break;
404                 case 't':
405                         thresh = parse_size(optarg);
406                         fancy_ioctl = 1;
407                         break;
408                 default:
409                         usage(cmd_defrag_usage);
410                 }
411         }
412
413         if (check_argc_min(argc - optind, 1))
414                 usage(cmd_defrag_usage);
415
416         memset(&range, 0, sizeof(range));
417         range.start = start;
418         range.len = len;
419         range.extent_thresh = thresh;
420         if (compress_type) {
421                 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
422                 range.compress_type = compress_type;
423         }
424         if (flush)
425                 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
426
427         for (i = optind; i < argc; i++) {
428                 if (verbose)
429                         printf("%s\n", argv[i]);
430                 fd = open_file_or_dir(argv[i], &dirstream);
431                 if (fd < 0) {
432                         fprintf(stderr, "failed to open %s\n", argv[i]);
433                         perror("open:");
434                         errors++;
435                         continue;
436                 }
437                 if (!fancy_ioctl) {
438                         ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
439                         e=errno;
440                 } else {
441                         ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
442                         if (ret && errno == ENOTTY) {
443                                 fprintf(stderr, "ERROR: defrag range ioctl not "
444                                         "supported in this kernel, please try "
445                                         "without any options.\n");
446                                 errors++;
447                                 close(fd);
448                                 break;
449                         }
450                         e = errno;
451                 }
452                 if (ret) {
453                         fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
454                                 argv[i], strerror(e));
455                         errors++;
456                 }
457                 close_file_or_dir(fd, dirstream);
458         }
459         if (verbose)
460                 printf("%s\n", BTRFS_BUILD_VERSION);
461         if (errors)
462                 fprintf(stderr, "total %d failures\n", errors);
463
464         return !!errors;
465 }
466
467 static const char * const cmd_resize_usage[] = {
468         "btrfs filesystem resize [devid:][+/-]<newsize>[gkm]|[devid:]max <path>",
469         "Resize a filesystem",
470         "If 'max' is passed, the filesystem will occupy all available space",
471         "on the device 'devid'.",
472         NULL
473 };
474
475 static int cmd_resize(int argc, char **argv)
476 {
477         struct btrfs_ioctl_vol_args     args;
478         int     fd, res, len, e;
479         char    *amount, *path;
480         DIR     *dirstream = NULL;
481
482         if (check_argc_exact(argc, 3))
483                 usage(cmd_resize_usage);
484
485         amount = argv[1];
486         path = argv[2];
487
488         len = strlen(amount);
489         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
490                 fprintf(stderr, "ERROR: size value too long ('%s)\n",
491                         amount);
492                 return 1;
493         }
494
495         fd = open_file_or_dir(path, &dirstream);
496         if (fd < 0) {
497                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
498                 return 1;
499         }
500
501         printf("Resize '%s' of '%s'\n", path, amount);
502         strncpy_null(args.name, amount);
503         res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
504         e = errno;
505         close_file_or_dir(fd, dirstream);
506         if( res < 0 ){
507                 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n", 
508                         path, strerror(e));
509                 return 1;
510         }
511         return 0;
512 }
513
514 static const char * const cmd_label_usage[] = {
515         "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
516         "Get or change the label of a filesystem",
517         "With one argument, get the label of filesystem on <device>.",
518         "If <newlabel> is passed, set the filesystem label to <newlabel>.",
519         NULL
520 };
521
522 static int cmd_label(int argc, char **argv)
523 {
524         if (check_argc_min(argc, 2) || check_argc_max(argc, 3))
525                 usage(cmd_label_usage);
526
527         if (argc > 2)
528                 return set_label(argv[1], argv[2]);
529         else
530                 return get_label(argv[1]);
531 }
532
533 const struct cmd_group filesystem_cmd_group = {
534         filesystem_cmd_group_usage, NULL, {
535                 { "df", cmd_df, cmd_df_usage, NULL, 0 },
536                 { "show", cmd_show, cmd_show_usage, NULL, 0 },
537                 { "sync", cmd_sync, cmd_sync_usage, NULL, 0 },
538                 { "defragment", cmd_defrag, cmd_defrag_usage, NULL, 0 },
539                 { "balance", cmd_balance, NULL, &balance_cmd_group, 1 },
540                 { "resize", cmd_resize, cmd_resize_usage, NULL, 0 },
541                 { "label", cmd_label, cmd_label_usage, NULL, 0 },
542                 NULL_CMD_STRUCT
543         }
544 };
545
546 int cmd_filesystem(int argc, char **argv)
547 {
548         return handle_command_group(&filesystem_cmd_group, argc, argv);
549 }