Btrfs-progs: rearrange files in the repo
[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 "btrfs_cmds.h"
35 #include "btrfslabel.h"
36
37 int do_df_filesystem(int nargs, char **argv)
38 {
39         struct btrfs_ioctl_space_args *sargs;
40         u64 count = 0, i;
41         int ret;
42         int fd;
43         int e;
44         char *path = argv[1];
45
46         fd = open_file_or_dir(path);
47         if (fd < 0) {
48                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
49                 return 12;
50         }
51
52         sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
53         if (!sargs)
54                 return -ENOMEM;
55
56         sargs->space_slots = 0;
57         sargs->total_spaces = 0;
58
59         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
60         e = errno;
61         if (ret) {
62                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
63                         path, strerror(e));
64                 free(sargs);
65                 return ret;
66         }
67         if (!sargs->total_spaces)
68                 return 0;
69
70         count = sargs->total_spaces;
71
72         sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
73                         (count * sizeof(struct btrfs_ioctl_space_info)));
74         if (!sargs)
75                 return -ENOMEM;
76
77         sargs->space_slots = count;
78         sargs->total_spaces = 0;
79
80         ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
81         e = errno;
82         if (ret) {
83                 fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n",
84                         path, strerror(e));
85                 close(fd);
86                 free(sargs);
87                 return ret;
88         }
89
90         for (i = 0; i < sargs->total_spaces; i++) {
91                 char description[80];
92                 char *total_bytes;
93                 char *used_bytes;
94                 int written = 0;
95                 u64 flags = sargs->spaces[i].flags;
96
97                 memset(description, 0, 80);
98
99                 if (flags & BTRFS_BLOCK_GROUP_DATA) {
100                         if (flags & BTRFS_BLOCK_GROUP_METADATA) {
101                                 snprintf(description, 14, "%s",
102                                          "Data+Metadata");
103                                 written += 13;
104                         } else {
105                                 snprintf(description, 5, "%s", "Data");
106                                 written += 4;
107                         }
108                 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
109                         snprintf(description, 7, "%s", "System");
110                         written += 6;
111                 } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
112                         snprintf(description, 9, "%s", "Metadata");
113                         written += 8;
114                 }
115
116                 if (flags & BTRFS_BLOCK_GROUP_RAID0) {
117                         snprintf(description+written, 8, "%s", ", RAID0");
118                         written += 7;
119                 } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
120                         snprintf(description+written, 8, "%s", ", RAID1");
121                         written += 7;
122                 } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
123                         snprintf(description+written, 6, "%s", ", DUP");
124                         written += 5;
125                 } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
126                         snprintf(description+written, 9, "%s", ", RAID10");
127                         written += 8;
128                 }
129
130                 total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
131                 used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
132                 printf("%s: total=%s, used=%s\n", description, total_bytes,
133                        used_bytes);
134         }
135         free(sargs);
136
137         return 0;
138 }
139
140 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
141 {
142         struct list_head *cur;
143         struct btrfs_device *device;
144
145         list_for_each(cur, &fs_devices->devices) {
146                 device = list_entry(cur, struct btrfs_device, dev_list);
147                 if ((device->label && strcmp(device->label, search) == 0) ||
148                     strcmp(device->name, search) == 0)
149                         return 1;
150         }
151         return 0;
152 }
153
154 static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
155 {
156         char uuidbuf[37];
157         struct list_head *cur;
158         struct btrfs_device *device;
159         char *super_bytes_used;
160         u64 devs_found = 0;
161         u64 total;
162
163         uuid_unparse(fs_devices->fsid, uuidbuf);
164         device = list_entry(fs_devices->devices.next, struct btrfs_device,
165                             dev_list);
166         if (device->label && device->label[0])
167                 printf("Label: '%s' ", device->label);
168         else
169                 printf("Label: none ");
170
171         super_bytes_used = pretty_sizes(device->super_bytes_used);
172
173         total = device->total_devs;
174         printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
175                (unsigned long long)total, super_bytes_used);
176
177         free(super_bytes_used);
178
179         list_for_each(cur, &fs_devices->devices) {
180                 char *total_bytes;
181                 char *bytes_used;
182                 device = list_entry(cur, struct btrfs_device, dev_list);
183                 total_bytes = pretty_sizes(device->total_bytes);
184                 bytes_used = pretty_sizes(device->bytes_used);
185                 printf("\tdevid %4llu size %s used %s path %s\n",
186                        (unsigned long long)device->devid,
187                        total_bytes, bytes_used, device->name);
188                 free(total_bytes);
189                 free(bytes_used);
190                 devs_found++;
191         }
192         if (devs_found < total) {
193                 printf("\t*** Some devices missing\n");
194         }
195         printf("\n");
196 }
197
198 int do_show_filesystem(int argc, char **argv)
199 {
200         struct list_head *all_uuids;
201         struct btrfs_fs_devices *fs_devices;
202         struct list_head *cur_uuid;
203         char *search = 0;
204         int ret;
205         int checklist = 1;
206         int searchstart = 1;
207
208         if( argc >= 2 && !strcmp(argv[1],"--all-devices")){
209                 checklist = 0;
210                 searchstart += 1;
211         }
212
213         if( argc > searchstart+1 ){
214                 fprintf(stderr, "ERROR: too many arguments\n");
215                 return 22;
216         }       
217
218         if(checklist)
219                 ret = btrfs_scan_block_devices(0);
220         else
221                 ret = btrfs_scan_one_dir("/dev", 0);
222
223         if (ret){
224                 fprintf(stderr, "ERROR: error %d while scanning\n", ret);
225                 return 18;
226         }
227         
228         if(searchstart < argc)
229                 search = argv[searchstart];
230
231         all_uuids = btrfs_scanned_uuids();
232         list_for_each(cur_uuid, all_uuids) {
233                 fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
234                                         list);
235                 if (search && uuid_search(fs_devices, search) == 0)
236                         continue;
237                 print_one_uuid(fs_devices);
238         }
239         printf("%s\n", BTRFS_BUILD_VERSION);
240         return 0;
241 }
242
243 int do_fssync(int argc, char **argv)
244 {
245         int     fd, res, e;
246         char    *path = argv[1];
247
248         fd = open_file_or_dir(path);
249         if (fd < 0) {
250                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
251                 return 12;
252         }
253
254         printf("FSSync '%s'\n", path);
255         res = ioctl(fd, BTRFS_IOC_SYNC);
256         e = errno;
257         close(fd);
258         if( res < 0 ){
259                 fprintf(stderr, "ERROR: unable to fs-syncing '%s' - %s\n", 
260                         path, strerror(e));
261                 return 16;
262         }
263
264         return 0;
265 }
266
267 static u64 parse_size(char *s)
268 {
269         int len = strlen(s);
270         char c;
271         u64 mult = 1;
272
273         if (!isdigit(s[len - 1])) {
274                 c = tolower(s[len - 1]);
275                 switch (c) {
276                 case 'g':
277                         mult *= 1024;
278                 case 'm':
279                         mult *= 1024;
280                 case 'k':
281                         mult *= 1024;
282                 case 'b':
283                         break;
284                 default:
285                         fprintf(stderr, "Unknown size descriptor %c\n", c);
286                         exit(1);
287                 }
288                 s[len - 1] = '\0';
289         }
290         return atoll(s) * mult;
291 }
292
293 static int parse_compress_type(char *s)
294 {
295         if (strcmp(optarg, "zlib") == 0)
296                 return BTRFS_COMPRESS_ZLIB;
297         else if (strcmp(optarg, "lzo") == 0)
298                 return BTRFS_COMPRESS_LZO;
299         else {
300                 fprintf(stderr, "Unknown compress type %s\n", s);
301                 exit(1);
302         };
303 }
304
305 int do_defrag(int ac, char **av)
306 {
307         int fd;
308         int flush = 0;
309         u64 start = 0;
310         u64 len = (u64)-1;
311         u32 thresh = 0;
312         int i;
313         int errors = 0;
314         int ret = 0;
315         int verbose = 0;
316         int fancy_ioctl = 0;
317         struct btrfs_ioctl_defrag_range_args range;
318         int e=0;
319         int compress_type = BTRFS_COMPRESS_NONE;
320
321         optind = 1;
322         while(1) {
323                 int c = getopt(ac, av, "vc::fs:l:t:");
324                 if (c < 0)
325                         break;
326                 switch(c) {
327                 case 'c':
328                         compress_type = BTRFS_COMPRESS_ZLIB;
329                         if (optarg)
330                                 compress_type = parse_compress_type(optarg);
331                         fancy_ioctl = 1;
332                         break;
333                 case 'f':
334                         flush = 1;
335                         fancy_ioctl = 1;
336                         break;
337                 case 'v':
338                         verbose = 1;
339                         break;
340                 case 's':
341                         start = parse_size(optarg);
342                         fancy_ioctl = 1;
343                         break;
344                 case 'l':
345                         len = parse_size(optarg);
346                         fancy_ioctl = 1;
347                         break;
348                 case 't':
349                         thresh = parse_size(optarg);
350                         fancy_ioctl = 1;
351                         break;
352                 default:
353                         fprintf(stderr, "Invalid arguments for defragment\n");
354                         free(av);
355                         return 1;
356                 }
357         }
358         if (ac - optind == 0) {
359                 fprintf(stderr, "Invalid arguments for defragment\n");
360                 free(av);
361                 return 1;
362         }
363
364         memset(&range, 0, sizeof(range));
365         range.start = start;
366         range.len = len;
367         range.extent_thresh = thresh;
368         if (compress_type) {
369                 range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
370                 range.compress_type = compress_type;
371         }
372         if (flush)
373                 range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
374
375         for (i = optind; i < ac; i++) {
376                 if (verbose)
377                         printf("%s\n", av[i]);
378                 fd = open_file_or_dir(av[i]);
379                 if (fd < 0) {
380                         fprintf(stderr, "failed to open %s\n", av[i]);
381                         perror("open:");
382                         errors++;
383                         continue;
384                 }
385                 if (!fancy_ioctl) {
386                         ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
387                         e=errno;
388                 } else {
389                         ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
390                         if (ret && errno == ENOTTY) {
391                                 fprintf(stderr, "ERROR: defrag range ioctl not "
392                                         "supported in this kernel, please try "
393                                         "without any options.\n");
394                                 errors++;
395                                 close(fd);
396                                 break;
397                         }
398                 }
399                 if (ret) {
400                         fprintf(stderr, "ERROR: defrag failed on %s - %s\n",
401                                 av[i], strerror(e));
402                         errors++;
403                 }
404                 close(fd);
405         }
406         if (verbose)
407                 printf("%s\n", BTRFS_BUILD_VERSION);
408         if (errors) {
409                 fprintf(stderr, "total %d failures\n", errors);
410                 exit(1);
411         }
412
413         free(av);
414         return errors + 20;
415 }
416
417 int do_balance(int argc, char **argv)
418 {
419
420         int     fdmnt, ret=0, e;
421         struct btrfs_ioctl_vol_args args;
422         char    *path = argv[1];
423
424         fdmnt = open_file_or_dir(path);
425         if (fdmnt < 0) {
426                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
427                 return 12;
428         }
429
430         memset(&args, 0, sizeof(args));
431         ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
432         e = errno;
433         close(fdmnt);
434         if(ret<0){
435                 fprintf(stderr, "ERROR: error during balancing '%s' - %s\n", 
436                         path, strerror(e));
437
438                 return 19;
439         }
440         return 0;
441 }
442
443 int do_resize(int argc, char **argv)
444 {
445
446         struct btrfs_ioctl_vol_args     args;
447         int     fd, res, len, e;
448         char    *amount=argv[1], *path=argv[2];
449
450         fd = open_file_or_dir(path);
451         if (fd < 0) {
452                 fprintf(stderr, "ERROR: can't access to '%s'\n", path);
453                 return 12;
454         }
455         len = strlen(amount);
456         if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
457                 fprintf(stderr, "ERROR: size value too long ('%s)\n",
458                         amount);
459                 return 14;
460         }
461
462         printf("Resize '%s' of '%s'\n", path, amount);
463         strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
464         res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
465         e = errno;
466         close(fd);
467         if( res < 0 ){
468                 fprintf(stderr, "ERROR: unable to resize '%s' - %s\n", 
469                         path, strerror(e));
470                 return 30;
471         }
472         return 0;
473 }
474
475 int do_change_label(int nargs, char **argv)
476 {
477         /* check the number of argument */
478         if ( nargs > 3 ){
479                 fprintf(stderr, "ERROR: '%s' requires maximum 2 args\n",
480                         argv[0]);
481                 return -2;
482         }else if (nargs == 2){
483                 return get_label(argv[1]);
484         } else {        /* nargs == 0 */
485                 return set_label(argv[1], argv[2]);
486         }
487 }
488