btrfs-progs: Avoid interpreting options after "--" when getting unit mode
[btrfs-progs-unstable/devel.git] / cmds-filesystem.c
blob45c16d27b4b429d24332694a8c1cac860f10f7c1
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.
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.
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.
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 #include <fcntl.h>
26 #include <ftw.h>
27 #include <mntent.h>
28 #include <linux/limits.h>
29 #include <getopt.h>
31 #include "kerncompat.h"
32 #include "ctree.h"
33 #include "ioctl.h"
34 #include "utils.h"
35 #include "volumes.h"
36 #include "commands.h"
37 #include "cmds-fi-usage.h"
38 #include "list_sort.h"
39 #include "disk-io.h"
40 #include "cmds-fi-du.h"
43 * for btrfs fi show, we maintain a hash of fsids we've already printed.
44 * This way we don't print dups if a given FS is mounted more than once.
46 #define SEEN_FSID_HASH_SIZE 256
48 struct seen_fsid {
49 u8 fsid[BTRFS_FSID_SIZE];
50 struct seen_fsid *next;
53 static struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = {NULL,};
55 static int is_seen_fsid(u8 *fsid)
57 u8 hash = fsid[0];
58 int slot = hash % SEEN_FSID_HASH_SIZE;
59 struct seen_fsid *seen = seen_fsid_hash[slot];
61 return seen ? 1 : 0;
64 static int add_seen_fsid(u8 *fsid)
66 u8 hash = fsid[0];
67 int slot = hash % SEEN_FSID_HASH_SIZE;
68 struct seen_fsid *seen = seen_fsid_hash[slot];
69 struct seen_fsid *alloc;
71 if (!seen)
72 goto insert;
74 while (1) {
75 if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0)
76 return -EEXIST;
78 if (!seen->next)
79 break;
81 seen = seen->next;
84 insert:
86 alloc = malloc(sizeof(*alloc));
87 if (!alloc)
88 return -ENOMEM;
90 alloc->next = NULL;
91 memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE);
93 if (seen)
94 seen->next = alloc;
95 else
96 seen_fsid_hash[slot] = alloc;
98 return 0;
101 static void free_seen_fsid(void)
103 int slot;
104 struct seen_fsid *seen;
105 struct seen_fsid *next;
107 for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) {
108 seen = seen_fsid_hash[slot];
109 while (seen) {
110 next = seen->next;
111 free(seen);
112 seen = next;
114 seen_fsid_hash[slot] = NULL;
118 static const char * const filesystem_cmd_group_usage[] = {
119 "btrfs filesystem [<group>] <command> [<args>]",
120 NULL
123 static const char * const cmd_filesystem_df_usage[] = {
124 "btrfs filesystem df [options] <path>",
125 "Show space usage information for a mount point",
126 HELPINFO_UNITS_SHORT_LONG,
127 NULL
130 static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret)
132 u64 count = 0;
133 int ret;
134 struct btrfs_ioctl_space_args *sargs;
136 sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
137 if (!sargs)
138 return -ENOMEM;
140 sargs->space_slots = 0;
141 sargs->total_spaces = 0;
143 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
144 if (ret < 0) {
145 error("cannot get space info: %s\n", strerror(errno));
146 free(sargs);
147 return -errno;
149 /* This really should never happen */
150 if (!sargs->total_spaces) {
151 free(sargs);
152 return -ENOENT;
154 count = sargs->total_spaces;
155 free(sargs);
157 sargs = malloc(sizeof(struct btrfs_ioctl_space_args) +
158 (count * sizeof(struct btrfs_ioctl_space_info)));
159 if (!sargs)
160 return -ENOMEM;
162 sargs->space_slots = count;
163 sargs->total_spaces = 0;
164 ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
165 if (ret < 0) {
166 error("cannot get space info with %llu slots: %s",
167 count, strerror(errno));
168 free(sargs);
169 return -errno;
171 *sargs_ret = sargs;
172 return 0;
175 static void print_df(struct btrfs_ioctl_space_args *sargs, unsigned unit_mode)
177 u64 i;
178 struct btrfs_ioctl_space_info *sp = sargs->spaces;
180 for (i = 0; i < sargs->total_spaces; i++, sp++) {
181 printf("%s, %s: total=%s, used=%s\n",
182 btrfs_group_type_str(sp->flags),
183 btrfs_group_profile_str(sp->flags),
184 pretty_size_mode(sp->total_bytes, unit_mode),
185 pretty_size_mode(sp->used_bytes, unit_mode));
189 static int cmd_filesystem_df(int argc, char **argv)
191 struct btrfs_ioctl_space_args *sargs = NULL;
192 int ret;
193 int fd;
194 char *path;
195 DIR *dirstream = NULL;
196 unsigned unit_mode;
198 unit_mode = get_unit_mode_from_arg(&argc, argv, 1);
200 clean_args_no_options(argc, argv, cmd_filesystem_df_usage);
202 if (check_argc_exact(argc - optind, 1))
203 usage(cmd_filesystem_df_usage);
205 path = argv[optind];
207 fd = btrfs_open_dir(path, &dirstream, 1);
208 if (fd < 0)
209 return 1;
211 ret = get_df(fd, &sargs);
213 if (ret == 0) {
214 print_df(sargs, unit_mode);
215 free(sargs);
216 } else {
217 error("get_df failed %s", strerror(-ret));
220 close_file_or_dir(fd, dirstream);
221 return !!ret;
224 static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label,
225 char *search)
227 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
228 int search_len = strlen(search);
230 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
231 uuid_unparse(fsid, uuidbuf);
232 if (!strncmp(uuidbuf, search, search_len))
233 return 1;
235 if (*label && strcmp(label, search) == 0)
236 return 1;
238 if (strcmp(mnt, search) == 0)
239 return 1;
241 return 0;
244 static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
246 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
247 struct list_head *cur;
248 struct btrfs_device *device;
249 int search_len = strlen(search);
251 search_len = min(search_len, BTRFS_UUID_UNPARSED_SIZE);
252 uuid_unparse(fs_devices->fsid, uuidbuf);
253 if (!strncmp(uuidbuf, search, search_len))
254 return 1;
256 list_for_each(cur, &fs_devices->devices) {
257 device = list_entry(cur, struct btrfs_device, dev_list);
258 if ((device->label && strcmp(device->label, search) == 0) ||
259 strcmp(device->name, search) == 0)
260 return 1;
262 return 0;
266 * Sort devices by devid, ascending
268 static int cmp_device_id(void *priv, struct list_head *a,
269 struct list_head *b)
271 const struct btrfs_device *da = list_entry(a, struct btrfs_device,
272 dev_list);
273 const struct btrfs_device *db = list_entry(b, struct btrfs_device,
274 dev_list);
276 return da->devid < db->devid ? -1 :
277 da->devid > db->devid ? 1 : 0;
280 static void splice_device_list(struct list_head *seed_devices,
281 struct list_head *all_devices)
283 struct btrfs_device *in_all, *next_all;
284 struct btrfs_device *in_seed, *next_seed;
286 list_for_each_entry_safe(in_all, next_all, all_devices, dev_list) {
287 list_for_each_entry_safe(in_seed, next_seed, seed_devices,
288 dev_list) {
289 if (in_all->devid == in_seed->devid) {
291 * When do dev replace in a sprout fs
292 * to a dev in its seed fs, the replacing
293 * dev will reside in the sprout fs and
294 * the replaced dev will still exist
295 * in the seed fs.
296 * So pick the latest one when showing
297 * the sprout fs.
299 if (in_all->generation
300 < in_seed->generation) {
301 list_del(&in_all->dev_list);
302 free(in_all);
303 } else if (in_all->generation
304 > in_seed->generation) {
305 list_del(&in_seed->dev_list);
306 free(in_seed);
308 break;
313 list_splice(seed_devices, all_devices);
316 static void print_devices(struct btrfs_fs_devices *fs_devices,
317 u64 *devs_found, unsigned unit_mode)
319 struct btrfs_device *device;
320 struct btrfs_fs_devices *cur_fs;
321 struct list_head *all_devices;
323 all_devices = &fs_devices->devices;
324 cur_fs = fs_devices->seed;
325 /* add all devices of seed fs to the fs to be printed */
326 while (cur_fs) {
327 splice_device_list(&cur_fs->devices, all_devices);
328 cur_fs = cur_fs->seed;
331 list_sort(NULL, all_devices, cmp_device_id);
332 list_for_each_entry(device, all_devices, dev_list) {
333 printf("\tdevid %4llu size %s used %s path %s\n",
334 (unsigned long long)device->devid,
335 pretty_size_mode(device->total_bytes, unit_mode),
336 pretty_size_mode(device->bytes_used, unit_mode),
337 device->name);
339 (*devs_found)++;
343 static void print_one_uuid(struct btrfs_fs_devices *fs_devices,
344 unsigned unit_mode)
346 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
347 struct btrfs_device *device;
348 u64 devs_found = 0;
349 u64 total;
351 if (add_seen_fsid(fs_devices->fsid))
352 return;
354 uuid_unparse(fs_devices->fsid, uuidbuf);
355 device = list_entry(fs_devices->devices.next, struct btrfs_device,
356 dev_list);
357 if (device->label && device->label[0])
358 printf("Label: '%s' ", device->label);
359 else
360 printf("Label: none ");
362 total = device->total_devs;
363 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
364 (unsigned long long)total,
365 pretty_size_mode(device->super_bytes_used, unit_mode));
367 print_devices(fs_devices, &devs_found, unit_mode);
369 if (devs_found < total) {
370 printf("\t*** Some devices missing\n");
372 printf("\n");
375 /* adds up all the used spaces as reported by the space info ioctl
377 static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
379 u64 ret = 0;
380 int i;
381 for (i = 0; i < si->total_spaces; i++)
382 ret += si->spaces[i].used_bytes;
383 return ret;
386 static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
387 struct btrfs_ioctl_dev_info_args *dev_info,
388 struct btrfs_ioctl_space_args *space_info,
389 char *label, unsigned unit_mode)
391 int i;
392 int fd;
393 int missing = 0;
394 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
395 struct btrfs_ioctl_dev_info_args *tmp_dev_info;
396 int ret;
398 ret = add_seen_fsid(fs_info->fsid);
399 if (ret == -EEXIST)
400 return 0;
401 else if (ret)
402 return ret;
404 uuid_unparse(fs_info->fsid, uuidbuf);
405 if (label && *label)
406 printf("Label: '%s' ", label);
407 else
408 printf("Label: none ");
410 printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
411 fs_info->num_devices,
412 pretty_size_mode(calc_used_bytes(space_info),
413 unit_mode));
415 for (i = 0; i < fs_info->num_devices; i++) {
416 char *canonical_path;
418 tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
420 /* Add check for missing devices even mounted */
421 fd = open((char *)tmp_dev_info->path, O_RDONLY);
422 if (fd < 0) {
423 missing = 1;
424 continue;
426 close(fd);
427 canonical_path = canonicalize_path((char *)tmp_dev_info->path);
428 printf("\tdevid %4llu size %s used %s path %s\n",
429 tmp_dev_info->devid,
430 pretty_size_mode(tmp_dev_info->total_bytes, unit_mode),
431 pretty_size_mode(tmp_dev_info->bytes_used, unit_mode),
432 canonical_path);
434 free(canonical_path);
437 if (missing)
438 printf("\t*** Some devices missing\n");
439 printf("\n");
440 return 0;
443 static int btrfs_scan_kernel(void *search, unsigned unit_mode)
445 int ret = 0, fd;
446 int found = 0;
447 FILE *f;
448 struct mntent *mnt;
449 struct btrfs_ioctl_fs_info_args fs_info_arg;
450 struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
451 struct btrfs_ioctl_space_args *space_info_arg = NULL;
452 char label[BTRFS_LABEL_SIZE];
454 f = setmntent("/proc/self/mounts", "r");
455 if (f == NULL)
456 return 1;
458 memset(label, 0, sizeof(label));
459 while ((mnt = getmntent(f)) != NULL) {
460 if (strcmp(mnt->mnt_type, "btrfs"))
461 continue;
462 ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
463 &dev_info_arg);
464 if (ret) {
465 kfree(dev_info_arg);
466 goto out;
469 /* skip all fs already shown as mounted fs */
470 if (is_seen_fsid(fs_info_arg.fsid))
471 continue;
473 ret = get_label_mounted(mnt->mnt_dir, label);
474 /* provide backward kernel compatibility */
475 if (ret == -ENOTTY)
476 ret = get_label_unmounted(
477 (const char *)dev_info_arg->path, label);
479 if (ret) {
480 kfree(dev_info_arg);
481 goto out;
483 if (search && !match_search_item_kernel(fs_info_arg.fsid,
484 mnt->mnt_dir, label, search)) {
485 kfree(dev_info_arg);
486 dev_info_arg = NULL;
487 continue;
490 fd = open(mnt->mnt_dir, O_RDONLY);
491 if ((fd != -1) && !get_df(fd, &space_info_arg)) {
492 print_one_fs(&fs_info_arg, dev_info_arg,
493 space_info_arg, label, unit_mode);
494 kfree(space_info_arg);
495 memset(label, 0, sizeof(label));
496 found = 1;
498 if (fd != -1)
499 close(fd);
500 kfree(dev_info_arg);
501 dev_info_arg = NULL;
504 out:
505 endmntent(f);
506 return !found;
509 static int dev_to_fsid(char *dev, __u8 *fsid)
511 struct btrfs_super_block *disk_super;
512 char buf[BTRFS_SUPER_INFO_SIZE];
513 int ret;
514 int fd;
516 fd = open(dev, O_RDONLY);
517 if (fd < 0) {
518 ret = -errno;
519 return ret;
522 disk_super = (struct btrfs_super_block *)buf;
523 ret = btrfs_read_dev_super(fd, disk_super,
524 BTRFS_SUPER_INFO_OFFSET, 0);
525 if (ret)
526 goto out;
528 memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
529 ret = 0;
531 out:
532 close(fd);
533 return ret;
536 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
538 struct btrfs_fs_devices *cur_seed, *next_seed;
539 struct btrfs_device *device;
541 while (!list_empty(&fs_devices->devices)) {
542 device = list_entry(fs_devices->devices.next,
543 struct btrfs_device, dev_list);
544 list_del(&device->dev_list);
546 free(device->name);
547 free(device->label);
548 free(device);
551 /* free seed fs chain */
552 cur_seed = fs_devices->seed;
553 fs_devices->seed = NULL;
554 while (cur_seed) {
555 next_seed = cur_seed->seed;
556 free(cur_seed);
558 cur_seed = next_seed;
561 list_del(&fs_devices->list);
562 free(fs_devices);
565 static int copy_device(struct btrfs_device *dst,
566 struct btrfs_device *src)
568 dst->devid = src->devid;
569 memcpy(dst->uuid, src->uuid, BTRFS_UUID_SIZE);
570 if (src->name == NULL)
571 dst->name = NULL;
572 else {
573 dst->name = strdup(src->name);
574 if (!dst->name)
575 return -ENOMEM;
577 if (src->label == NULL)
578 dst->label = NULL;
579 else {
580 dst->label = strdup(src->label);
581 if (!dst->label) {
582 free(dst->name);
583 return -ENOMEM;
586 dst->total_devs = src->total_devs;
587 dst->super_bytes_used = src->super_bytes_used;
588 dst->total_bytes = src->total_bytes;
589 dst->bytes_used = src->bytes_used;
590 dst->generation = src->generation;
592 return 0;
595 static int copy_fs_devices(struct btrfs_fs_devices *dst,
596 struct btrfs_fs_devices *src)
598 struct btrfs_device *cur_dev, *dev_copy;
599 int ret = 0;
601 memcpy(dst->fsid, src->fsid, BTRFS_FSID_SIZE);
602 INIT_LIST_HEAD(&dst->devices);
603 dst->seed = NULL;
605 list_for_each_entry(cur_dev, &src->devices, dev_list) {
606 dev_copy = malloc(sizeof(*dev_copy));
607 if (!dev_copy) {
608 ret = -ENOMEM;
609 break;
612 ret = copy_device(dev_copy, cur_dev);
613 if (ret) {
614 free(dev_copy);
615 break;
618 list_add(&dev_copy->dev_list, &dst->devices);
619 dev_copy->fs_devices = dst;
622 return ret;
625 static int find_and_copy_seed(struct btrfs_fs_devices *seed,
626 struct btrfs_fs_devices *copy,
627 struct list_head *fs_uuids) {
628 struct btrfs_fs_devices *cur_fs;
630 list_for_each_entry(cur_fs, fs_uuids, list)
631 if (!memcmp(seed->fsid, cur_fs->fsid, BTRFS_FSID_SIZE))
632 return copy_fs_devices(copy, cur_fs);
634 return 1;
637 static int has_seed_devices(struct btrfs_fs_devices *fs_devices)
639 struct btrfs_device *device;
640 int dev_cnt_total, dev_cnt = 0;
642 device = list_first_entry(&fs_devices->devices, struct btrfs_device,
643 dev_list);
645 dev_cnt_total = device->total_devs;
647 list_for_each_entry(device, &fs_devices->devices, dev_list)
648 dev_cnt++;
650 return dev_cnt_total != dev_cnt;
653 static int search_umounted_fs_uuids(struct list_head *all_uuids,
654 char *search, int *found)
656 struct btrfs_fs_devices *cur_fs, *fs_copy;
657 struct list_head *fs_uuids;
658 int ret = 0;
660 fs_uuids = btrfs_scanned_uuids();
663 * The fs_uuids list is global, and open_ctree_* will
664 * modify it, make a private copy here
666 list_for_each_entry(cur_fs, fs_uuids, list) {
667 /* don't bother handle all fs, if search target specified */
668 if (search) {
669 if (uuid_search(cur_fs, search) == 0)
670 continue;
671 if (found)
672 *found = 1;
675 /* skip all fs already shown as mounted fs */
676 if (is_seen_fsid(cur_fs->fsid))
677 continue;
679 fs_copy = calloc(1, sizeof(*fs_copy));
680 if (!fs_copy) {
681 ret = -ENOMEM;
682 goto out;
685 ret = copy_fs_devices(fs_copy, cur_fs);
686 if (ret) {
687 free(fs_copy);
688 goto out;
691 list_add(&fs_copy->list, all_uuids);
694 out:
695 return ret;
698 static int map_seed_devices(struct list_head *all_uuids)
700 struct btrfs_fs_devices *cur_fs, *cur_seed;
701 struct btrfs_fs_devices *seed_copy;
702 struct btrfs_fs_devices *opened_fs;
703 struct btrfs_device *device;
704 struct btrfs_fs_info *fs_info;
705 struct list_head *fs_uuids;
706 int ret = 0;
708 fs_uuids = btrfs_scanned_uuids();
710 list_for_each_entry(cur_fs, all_uuids, list) {
711 device = list_first_entry(&cur_fs->devices,
712 struct btrfs_device, dev_list);
713 if (!device)
714 continue;
716 /* skip fs without seeds */
717 if (!has_seed_devices(cur_fs))
718 continue;
721 * open_ctree_* detects seed/sprout mapping
723 fs_info = open_ctree_fs_info(device->name, 0, 0, 0,
724 OPEN_CTREE_PARTIAL);
725 if (!fs_info)
726 continue;
729 * copy the seed chain under the opened fs
731 opened_fs = fs_info->fs_devices;
732 cur_seed = cur_fs;
733 while (opened_fs->seed) {
734 seed_copy = malloc(sizeof(*seed_copy));
735 if (!seed_copy) {
736 ret = -ENOMEM;
737 goto fail_out;
739 ret = find_and_copy_seed(opened_fs->seed, seed_copy,
740 fs_uuids);
741 if (ret) {
742 free(seed_copy);
743 goto fail_out;
746 cur_seed->seed = seed_copy;
748 opened_fs = opened_fs->seed;
749 cur_seed = cur_seed->seed;
752 close_ctree(fs_info->chunk_root);
755 out:
756 return ret;
757 fail_out:
758 close_ctree(fs_info->chunk_root);
759 goto out;
762 static const char * const cmd_filesystem_show_usage[] = {
763 "btrfs filesystem show [options] [<path>|<uuid>|<device>|label]",
764 "Show the structure of a filesystem",
765 "-d|--all-devices show only disks under /dev containing btrfs filesystem",
766 "-m|--mounted show only mounted btrfs",
767 HELPINFO_UNITS_LONG,
768 "If no argument is given, structure of all present filesystems is shown.",
769 NULL
772 static int cmd_filesystem_show(int argc, char **argv)
774 LIST_HEAD(all_uuids);
775 struct btrfs_fs_devices *fs_devices;
776 char *search = NULL;
777 int ret;
778 /* default, search both kernel and udev */
779 int where = -1;
780 int type = 0;
781 char mp[PATH_MAX];
782 char path[PATH_MAX];
783 __u8 fsid[BTRFS_FSID_SIZE];
784 char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
785 unsigned unit_mode;
786 int found = 0;
788 unit_mode = get_unit_mode_from_arg(&argc, argv, 0);
790 while (1) {
791 int c;
792 static const struct option long_options[] = {
793 { "all-devices", no_argument, NULL, 'd'},
794 { "mounted", no_argument, NULL, 'm'},
795 { NULL, 0, NULL, 0 }
798 c = getopt_long(argc, argv, "dm", long_options, NULL);
799 if (c < 0)
800 break;
801 switch (c) {
802 case 'd':
803 where = BTRFS_SCAN_LBLKID;
804 break;
805 case 'm':
806 where = BTRFS_SCAN_MOUNTED;
807 break;
808 default:
809 usage(cmd_filesystem_show_usage);
813 if (check_argc_max(argc, optind + 1))
814 usage(cmd_filesystem_show_usage);
816 if (argc > optind) {
817 search = argv[optind];
818 if (*search == 0)
819 usage(cmd_filesystem_show_usage);
820 type = check_arg_type(search);
823 * For search is a device:
824 * realpath do /dev/mapper/XX => /dev/dm-X
825 * which is required by BTRFS_SCAN_DEV
826 * For search is a mountpoint:
827 * realpath do /mnt/btrfs/ => /mnt/btrfs
828 * which shall be recognized by btrfs_scan_kernel()
830 if (realpath(search, path))
831 search = path;
834 * Needs special handling if input arg is block dev And if
835 * input arg is mount-point just print it right away
837 if (type == BTRFS_ARG_BLKDEV && where != BTRFS_SCAN_LBLKID) {
838 ret = get_btrfs_mount(search, mp, sizeof(mp));
839 if (!ret) {
840 /* given block dev is mounted */
841 search = mp;
842 type = BTRFS_ARG_MNTPOINT;
843 } else {
844 ret = dev_to_fsid(search, fsid);
845 if (ret) {
846 error("no btrfs on %s", search);
847 return 1;
849 uuid_unparse(fsid, uuid_buf);
850 search = uuid_buf;
851 type = BTRFS_ARG_UUID;
852 goto devs_only;
857 if (where == BTRFS_SCAN_LBLKID)
858 goto devs_only;
860 /* show mounted btrfs */
861 ret = btrfs_scan_kernel(search, unit_mode);
862 if (search && !ret) {
863 /* since search is found we are done */
864 goto out;
867 /* shows mounted only */
868 if (where == BTRFS_SCAN_MOUNTED)
869 goto out;
871 devs_only:
872 ret = btrfs_scan_lblkid();
874 if (ret) {
875 error("blkid device scan returned %d\n", ret);
876 return 1;
879 ret = search_umounted_fs_uuids(&all_uuids, search, &found);
880 if (ret < 0) {
881 error("searching target device returned error %d", ret);
882 return 1;
886 * The seed/sprout mapping are not detected yet,
887 * do mapping build for all umounted fs
889 ret = map_seed_devices(&all_uuids);
890 if (ret) {
891 error("mapping seed devices returned error %d", ret);
892 return 1;
895 list_for_each_entry(fs_devices, &all_uuids, list)
896 print_one_uuid(fs_devices, unit_mode);
898 if (search && !found)
899 ret = 1;
901 while (!list_empty(&all_uuids)) {
902 fs_devices = list_entry(all_uuids.next,
903 struct btrfs_fs_devices, list);
904 free_fs_devices(fs_devices);
906 out:
907 free_seen_fsid();
908 return ret;
911 static const char * const cmd_filesystem_sync_usage[] = {
912 "btrfs filesystem sync <path>",
913 "Force a sync on a filesystem",
914 NULL
917 static int cmd_filesystem_sync(int argc, char **argv)
919 int fd, res, e;
920 char *path;
921 DIR *dirstream = NULL;
923 clean_args_no_options(argc, argv, cmd_filesystem_sync_usage);
925 if (check_argc_exact(argc - optind, 1))
926 usage(cmd_filesystem_sync_usage);
928 path = argv[optind];
930 fd = btrfs_open_dir(path, &dirstream, 1);
931 if (fd < 0)
932 return 1;
934 printf("FSSync '%s'\n", path);
935 res = ioctl(fd, BTRFS_IOC_SYNC);
936 e = errno;
937 close_file_or_dir(fd, dirstream);
938 if( res < 0 ){
939 error("sync ioctl failed on '%s': %s", path, strerror(e));
940 return 1;
943 return 0;
946 static int parse_compress_type(char *s)
948 if (strcmp(optarg, "zlib") == 0)
949 return BTRFS_COMPRESS_ZLIB;
950 else if (strcmp(optarg, "lzo") == 0)
951 return BTRFS_COMPRESS_LZO;
952 else {
953 error("unknown compression type %s", s);
954 exit(1);
958 static const char * const cmd_filesystem_defrag_usage[] = {
959 "btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
960 "Defragment a file or a directory",
962 "-v be verbose",
963 "-r defragment files recursively",
964 "-c[zlib,lzo] compress the file while defragmenting",
965 "-f flush data to disk immediately after defragmenting",
966 "-s start defragment only from byte onward",
967 "-l len defragment only up to len bytes",
968 "-t size target extent size hint",
969 NULL
972 static int do_defrag(int fd, int fancy_ioctl,
973 struct btrfs_ioctl_defrag_range_args *range)
975 int ret;
977 if (!fancy_ioctl)
978 ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
979 else
980 ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, range);
982 return ret;
985 static int defrag_global_fancy_ioctl;
986 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
987 static int defrag_global_verbose;
988 static int defrag_global_errors;
989 static int defrag_callback(const char *fpath, const struct stat *sb,
990 int typeflag, struct FTW *ftwbuf)
992 int ret = 0;
993 int e = 0;
994 int fd = 0;
996 if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
997 if (defrag_global_verbose)
998 printf("%s\n", fpath);
999 fd = open(fpath, O_RDWR);
1000 if (fd < 0)
1001 goto error;
1002 ret = do_defrag(fd, defrag_global_fancy_ioctl, &defrag_global_range);
1003 e = errno;
1004 close(fd);
1005 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1006 error("defrag range ioctl not "
1007 "supported in this kernel, please try "
1008 "without any options.");
1009 defrag_global_errors++;
1010 return ENOTTY;
1012 if (ret)
1013 goto error;
1015 return 0;
1017 error:
1018 error("defrag failed on %s: %s", fpath, strerror(e));
1019 defrag_global_errors++;
1020 return 0;
1023 static int cmd_filesystem_defrag(int argc, char **argv)
1025 int fd;
1026 int flush = 0;
1027 u64 start = 0;
1028 u64 len = (u64)-1;
1029 u64 thresh = 0;
1030 int i;
1031 int recursive = 0;
1032 int ret = 0;
1033 int e = 0;
1034 int compress_type = BTRFS_COMPRESS_NONE;
1035 DIR *dirstream;
1037 defrag_global_errors = 0;
1038 defrag_global_verbose = 0;
1039 defrag_global_errors = 0;
1040 defrag_global_fancy_ioctl = 0;
1041 optind = 1;
1042 while(1) {
1043 int c = getopt(argc, argv, "vrc::fs:l:t:");
1044 if (c < 0)
1045 break;
1047 switch(c) {
1048 case 'c':
1049 compress_type = BTRFS_COMPRESS_ZLIB;
1050 if (optarg)
1051 compress_type = parse_compress_type(optarg);
1052 defrag_global_fancy_ioctl = 1;
1053 break;
1054 case 'f':
1055 flush = 1;
1056 defrag_global_fancy_ioctl = 1;
1057 break;
1058 case 'v':
1059 defrag_global_verbose = 1;
1060 break;
1061 case 's':
1062 start = parse_size(optarg);
1063 defrag_global_fancy_ioctl = 1;
1064 break;
1065 case 'l':
1066 len = parse_size(optarg);
1067 defrag_global_fancy_ioctl = 1;
1068 break;
1069 case 't':
1070 thresh = parse_size(optarg);
1071 if (thresh > (u32)-1) {
1072 warning(
1073 "target extent size %llu too big, trimmed to %u",
1074 thresh, (u32)-1);
1075 thresh = (u32)-1;
1077 defrag_global_fancy_ioctl = 1;
1078 break;
1079 case 'r':
1080 recursive = 1;
1081 break;
1082 default:
1083 usage(cmd_filesystem_defrag_usage);
1087 if (check_argc_min(argc - optind, 1))
1088 usage(cmd_filesystem_defrag_usage);
1090 memset(&defrag_global_range, 0, sizeof(defrag_global_range));
1091 defrag_global_range.start = start;
1092 defrag_global_range.len = len;
1093 defrag_global_range.extent_thresh = (u32)thresh;
1094 if (compress_type) {
1095 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
1096 defrag_global_range.compress_type = compress_type;
1098 if (flush)
1099 defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
1101 for (i = optind; i < argc; i++) {
1102 struct stat st;
1104 dirstream = NULL;
1105 fd = open_file_or_dir(argv[i], &dirstream);
1106 if (fd < 0) {
1107 error("cannot open %s: %s\n", argv[i],
1108 strerror(errno));
1109 defrag_global_errors++;
1110 close_file_or_dir(fd, dirstream);
1111 continue;
1113 if (fstat(fd, &st)) {
1114 error("failed to stat %s: %s",
1115 argv[i], strerror(errno));
1116 defrag_global_errors++;
1117 close_file_or_dir(fd, dirstream);
1118 continue;
1120 if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
1121 error("%s is not a directory or a regular file\n",
1122 argv[i]);
1123 defrag_global_errors++;
1124 close_file_or_dir(fd, dirstream);
1125 continue;
1127 if (recursive) {
1128 if (S_ISDIR(st.st_mode)) {
1129 ret = nftw(argv[i], defrag_callback, 10,
1130 FTW_MOUNT | FTW_PHYS);
1131 if (ret == ENOTTY)
1132 exit(1);
1133 /* errors are handled in the callback */
1134 ret = 0;
1135 } else {
1136 if (defrag_global_verbose)
1137 printf("%s\n", argv[i]);
1138 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1139 &defrag_global_range);
1140 e = errno;
1142 } else {
1143 if (defrag_global_verbose)
1144 printf("%s\n", argv[i]);
1145 ret = do_defrag(fd, defrag_global_fancy_ioctl,
1146 &defrag_global_range);
1147 e = errno;
1149 close_file_or_dir(fd, dirstream);
1150 if (ret && e == ENOTTY && defrag_global_fancy_ioctl) {
1151 error("defrag range ioctl not "
1152 "supported in this kernel, please try "
1153 "without any options.");
1154 defrag_global_errors++;
1155 break;
1157 if (ret) {
1158 error("defrag failed on %s: %s", argv[i], strerror(e));
1159 defrag_global_errors++;
1162 if (defrag_global_errors)
1163 fprintf(stderr, "total %d failures\n", defrag_global_errors);
1165 return !!defrag_global_errors;
1168 static const char * const cmd_filesystem_resize_usage[] = {
1169 "btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>",
1170 "Resize a filesystem",
1171 "If 'max' is passed, the filesystem will occupy all available space",
1172 "on the device 'devid'.",
1173 "[kK] means KiB, which denotes 1KiB = 1024B, 1MiB = 1024KiB, etc.",
1174 NULL
1177 static int cmd_filesystem_resize(int argc, char **argv)
1179 struct btrfs_ioctl_vol_args args;
1180 int fd, res, len, e;
1181 char *amount, *path;
1182 DIR *dirstream = NULL;
1183 struct stat st;
1185 clean_args_no_options(argc, argv, cmd_filesystem_resize_usage);
1187 if (check_argc_exact(argc - optind, 2))
1188 usage(cmd_filesystem_resize_usage);
1190 amount = argv[optind];
1191 path = argv[optind + 1];
1193 len = strlen(amount);
1194 if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
1195 error("resize value too long (%s)", amount);
1196 return 1;
1199 res = stat(path, &st);
1200 if (res < 0) {
1201 error("resize: cannot stat %s: %s", path, strerror(errno));
1202 return 1;
1204 if (!S_ISDIR(st.st_mode)) {
1205 error("resize works on mounted filesystems and accepts only\n"
1206 "directories as argument. Passing file containing a btrfs image\n"
1207 "would resize the underlying filesystem instead of the image.\n");
1208 return 1;
1211 fd = btrfs_open_dir(path, &dirstream, 1);
1212 if (fd < 0)
1213 return 1;
1215 printf("Resize '%s' of '%s'\n", path, amount);
1216 memset(&args, 0, sizeof(args));
1217 strncpy_null(args.name, amount);
1218 res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
1219 e = errno;
1220 close_file_or_dir(fd, dirstream);
1221 if( res < 0 ){
1222 switch (e) {
1223 case EFBIG:
1224 error("unable to resize '%s': no enough free space",
1225 path);
1226 break;
1227 default:
1228 error("unable to resize '%s': %s", path, strerror(e));
1229 break;
1231 return 1;
1232 } else if (res > 0) {
1233 const char *err_str = btrfs_err_str(res);
1235 if (err_str) {
1236 error("resizing of '%s' failed: %s", path, err_str);
1237 } else {
1238 error("resizing of '%s' failed: unknown error %d",
1239 path, res);
1241 return 1;
1243 return 0;
1246 static const char * const cmd_filesystem_label_usage[] = {
1247 "btrfs filesystem label [<device>|<mount_point>] [<newlabel>]",
1248 "Get or change the label of a filesystem",
1249 "With one argument, get the label of filesystem on <device>.",
1250 "If <newlabel> is passed, set the filesystem label to <newlabel>.",
1251 NULL
1254 static int cmd_filesystem_label(int argc, char **argv)
1256 clean_args_no_options(argc, argv, cmd_filesystem_label_usage);
1258 if (check_argc_min(argc - optind, 1) ||
1259 check_argc_max(argc - optind, 2))
1260 usage(cmd_filesystem_label_usage);
1262 if (argc - optind > 1) {
1263 return set_label(argv[optind], argv[optind + 1]);
1264 } else {
1265 char label[BTRFS_LABEL_SIZE];
1266 int ret;
1268 ret = get_label(argv[optind], label);
1269 if (!ret)
1270 fprintf(stdout, "%s\n", label);
1272 return ret;
1276 static const char filesystem_cmd_group_info[] =
1277 "overall filesystem tasks and information";
1279 const struct cmd_group filesystem_cmd_group = {
1280 filesystem_cmd_group_usage, filesystem_cmd_group_info, {
1281 { "df", cmd_filesystem_df, cmd_filesystem_df_usage, NULL, 0 },
1282 { "du", cmd_filesystem_du, cmd_filesystem_du_usage, NULL, 0 },
1283 { "show", cmd_filesystem_show, cmd_filesystem_show_usage, NULL,
1284 0 },
1285 { "sync", cmd_filesystem_sync, cmd_filesystem_sync_usage, NULL,
1286 0 },
1287 { "defragment", cmd_filesystem_defrag,
1288 cmd_filesystem_defrag_usage, NULL, 0 },
1289 { "balance", cmd_balance, NULL, &balance_cmd_group,
1290 CMD_HIDDEN },
1291 { "resize", cmd_filesystem_resize, cmd_filesystem_resize_usage,
1292 NULL, 0 },
1293 { "label", cmd_filesystem_label, cmd_filesystem_label_usage,
1294 NULL, 0 },
1295 { "usage", cmd_filesystem_usage,
1296 cmd_filesystem_usage_usage, NULL, 0 },
1298 NULL_CMD_STRUCT
1302 int cmd_filesystem(int argc, char **argv)
1304 return handle_command_group(&filesystem_cmd_group, argc, argv);