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