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