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