1 diff --git a/Makefile b/Makefile
2 index 8097b5a..525676e 100644
5 @@ -4,7 +4,7 @@ CFLAGS = -g -Werror -Os
6 objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
7 root-tree.o dir-item.o file-item.o inode-item.o \
8 inode-map.o crc32c.o rbtree.o extent-cache.o extent_io.o \
10 + volumes.o utils.o btrfs-list.o
13 CHECKFLAGS=-D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \
14 @@ -16,7 +16,9 @@ prefix ?= /usr/local
15 bindir = $(prefix)/bin
18 -progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck
19 +progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck \
23 # make C=1 to enable sparse
25 @@ -35,6 +37,10 @@ all: version $(progs) manpages
29 +btrfs: $(objects) btrfs.o btrfs_cmds.o
30 + gcc $(CFLAGS) -o btrfs btrfs.o btrfs_cmds.o \
31 + $(objects) $(LDFLAGS) $(LIBS)
33 btrfsctl: $(objects) btrfsctl.o
34 gcc $(CFLAGS) -o btrfsctl btrfsctl.o $(objects) $(LDFLAGS) $(LIBS)
36 @@ -56,6 +62,9 @@ btrfs-debug-tree: $(objects) debug-tree.o
37 btrfstune: $(objects) btrfstune.o
38 gcc $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS)
40 +btrfs-map-logical: $(objects) btrfs-map-logical.o
41 + gcc $(CFLAGS) -o btrfs-map-logical $(objects) btrfs-map-logical.o $(LDFLAGS) $(LIBS)
43 btrfs-image: $(objects) btrfs-image.o
44 gcc $(CFLAGS) -o btrfs-image $(objects) btrfs-image.o -lpthread -lz $(LDFLAGS) $(LIBS)
46 @@ -68,6 +77,9 @@ quick-test: $(objects) quick-test.o
47 convert: $(objects) convert.o
48 gcc $(CFLAGS) -o btrfs-convert $(objects) convert.o -lext2fs $(LDFLAGS) $(LIBS)
50 +ioctl-test: $(objects) ioctl-test.o
51 + gcc $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS)
56 diff --git a/btrfs-defrag.c b/btrfs-defrag.c
58 index 0000000..8f1525a
63 + * Copyright (C) 2010 Oracle. All rights reserved.
65 + * This program is free software; you can redistribute it and/or
66 + * modify it under the terms of the GNU General Public
67 + * License v2 as published by the Free Software Foundation.
69 + * This program is distributed in the hope that it will be useful,
70 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
71 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
72 + * General Public License for more details.
74 + * You should have received a copy of the GNU General Public
75 + * License along with this program; if not, write to the
76 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
77 + * Boston, MA 021110-1307, USA.
81 +#include <sys/ioctl.h>
82 +#include <sys/mount.h>
87 +#include <sys/types.h>
88 +#include <sys/stat.h>
95 +#include "kerncompat.h"
97 +#include "transaction.h"
101 diff --git a/btrfs-list.c b/btrfs-list.c
103 index 0000000..7741705
108 + * Copyright (C) 2010 Oracle. All rights reserved.
110 + * This program is free software; you can redistribute it and/or
111 + * modify it under the terms of the GNU General Public
112 + * License v2 as published by the Free Software Foundation.
114 + * This program is distributed in the hope that it will be useful,
115 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
116 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
117 + * General Public License for more details.
119 + * You should have received a copy of the GNU General Public
120 + * License along with this program; if not, write to the
121 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
122 + * Boston, MA 021110-1307, USA.
126 +#include <sys/ioctl.h>
127 +#include <sys/mount.h>
132 +#include <sys/types.h>
133 +#include <sys/stat.h>
138 +#include "kerncompat.h"
140 +#include "transaction.h"
142 +#include "version.h"
144 +/* we store all the roots we find in an rbtree so that we can
145 + * search for them later.
147 +struct root_lookup {
148 + struct rb_root root;
152 + * one of these for each root we find.
155 + struct rb_node rb_node;
157 + /* this root's id */
160 + /* the id of the root that references this one */
163 + /* the dir id we're in from ref_tree */
166 + /* path from the subvol we live in to this root, including the
167 + * root's name. This is null until we do the extra lookup ioctl.
171 + /* the name of this root in the directory it lives in */
175 +static void root_lookup_init(struct root_lookup *tree)
177 + tree->root.rb_node = NULL;
180 +static int comp_entry(struct root_info *entry, u64 root_id, u64 ref_tree)
182 + if (entry->root_id > root_id)
184 + if (entry->root_id < root_id)
186 + if (entry->ref_tree > ref_tree)
188 + if (entry->ref_tree < ref_tree)
194 + * insert a new root into the tree. returns the existing root entry
195 + * if one is already there. Both root_id and ref_tree are used
198 +static struct rb_node *tree_insert(struct rb_root *root, u64 root_id,
199 + u64 ref_tree, struct rb_node *node)
201 + struct rb_node ** p = &root->rb_node;
202 + struct rb_node * parent = NULL;
203 + struct root_info *entry;
208 + entry = rb_entry(parent, struct root_info, rb_node);
210 + comp = comp_entry(entry, root_id, ref_tree);
213 + p = &(*p)->rb_left;
215 + p = &(*p)->rb_right;
220 + entry = rb_entry(parent, struct root_info, rb_node);
221 + rb_link_node(node, parent, p);
222 + rb_insert_color(node, root);
227 + * find a given root id in the tree. We return the smallest one,
228 + * rb_next can be used to move forward looking for more if required
230 +static struct root_info *tree_search(struct rb_root *root, u64 root_id)
232 + struct rb_node * n = root->rb_node;
233 + struct root_info *entry;
236 + entry = rb_entry(n, struct root_info, rb_node);
238 + if (entry->root_id < root_id)
240 + else if (entry->root_id > root_id)
243 + struct root_info *prev;
244 + struct rb_node *prev_n;
246 + prev_n = rb_prev(n);
249 + prev = rb_entry(prev_n, struct root_info,
251 + if (prev->root_id != root_id)
263 + * this allocates a new root in the lookup tree.
265 + * root_id should be the object id of the root
267 + * ref_tree is the objectid of the referring root.
269 + * dir_id is the directory in ref_tree where this root_id can be found.
271 + * name is the name of root_id in that directory
273 + * name_len is the length of name
275 +static int add_root(struct root_lookup *root_lookup,
276 + u64 root_id, u64 ref_tree, u64 dir_id, char *name,
279 + struct root_info *ri;
280 + struct rb_node *ret;
281 + ri = malloc(sizeof(*ri) + name_len + 1);
283 + printf("memory allocation failed\n");
286 + memset(ri, 0, sizeof(*ri) + name_len + 1);
288 + ri->dir_id = dir_id;
289 + ri->root_id = root_id;
290 + ri->ref_tree = ref_tree;
291 + strncpy(ri->name, name, name_len);
293 + ret = tree_insert(&root_lookup->root, root_id, ref_tree, &ri->rb_node);
295 + printf("failed to insert tree %llu\n", (unsigned long long)root_id);
302 + * for a given root_info, search through the root_lookup tree to construct
303 + * the full path name to it.
305 + * This can't be called until all the root_info->path fields are filled
306 + * in by lookup_ino_path
308 +static int resolve_root(struct root_lookup *rl, struct root_info *ri)
311 + char *full_path = NULL;
313 + struct root_info *found;
316 + * we go backwards from the root_info object and add pathnames
317 + * from parent directories as we go.
323 + int add_len = strlen(found->path);
325 + /* room for / and for null */
326 + tmp = malloc(add_len + 2 + len);
328 + memcpy(tmp + add_len + 1, full_path, len);
329 + tmp[add_len] = '/';
330 + memcpy(tmp, found->path, add_len);
331 + tmp [add_len + len + 1] = '\0';
334 + len += add_len + 1;
336 + full_path = strdup(found->path);
340 + next = found->ref_tree;
341 + /* if the ref_tree refers to ourselves, we're at the top */
342 + if (next == found->root_id) {
348 + * if the ref_tree wasn't in our tree of roots, we're
351 + found = tree_search(&rl->root, next);
357 + printf("ID %llu top level %llu path %s\n", ri->root_id, top_id,
364 + * for a single root_info, ask the kernel to give us a path name
365 + * inside it's ref_root for the dir_id where it lives.
367 + * This fills in root_info->path with the path to the directory and and
368 + * appends this root's name.
370 +static int lookup_ino_path(int fd, struct root_info *ri)
372 + struct btrfs_ioctl_ino_lookup_args args;
378 + memset(&args, 0, sizeof(args));
379 + args.treeid = ri->ref_tree;
380 + args.objectid = ri->dir_id;
382 + ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
384 + fprintf(stderr, "ERROR: Failed to lookup path for root %llu\n",
385 + (unsigned long long)ri->ref_tree);
389 + if (args.name[0]) {
391 + * we're in a subdirectory of ref_tree, the kernel ioctl
392 + * puts a / in there for us
394 + ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1);
396 + perror("malloc failed");
399 + strcpy(ri->path, args.name);
400 + strcat(ri->path, ri->name);
402 + /* we're at the root of ref_tree */
403 + ri->path = strdup(ri->name);
405 + perror("strdup failed");
412 +/* finding the generation for a given path is a two step process.
413 + * First we use the inode loookup routine to find out the root id
415 + * Then we use the tree search ioctl to scan all the root items for a
416 + * given root id and spit out the latest generation we can find
418 +static u64 find_root_gen(int fd)
420 + struct btrfs_ioctl_ino_lookup_args ino_args;
422 + struct btrfs_ioctl_search_args args;
423 + struct btrfs_ioctl_search_key *sk = &args.key;
424 + struct btrfs_ioctl_search_header *sh;
425 + unsigned long off = 0;
429 + memset(&ino_args, 0, sizeof(ino_args));
430 + ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID;
432 + /* this ioctl fills in ino_args->treeid */
433 + ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args);
435 + fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu\n",
436 + (unsigned long long)BTRFS_FIRST_FREE_OBJECTID);
440 + memset(&args, 0, sizeof(args));
445 + * there may be more than one ROOT_ITEM key if there are
446 + * snapshots pending deletion, we have to loop through
449 + sk->min_objectid = ino_args.treeid;
450 + sk->max_objectid = ino_args.treeid;
451 + sk->max_type = BTRFS_ROOT_ITEM_KEY;
452 + sk->min_type = BTRFS_ROOT_ITEM_KEY;
453 + sk->max_offset = (u64)-1;
454 + sk->max_transid = (u64)-1;
455 + sk->nr_items = 4096;
458 + ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
460 + fprintf(stderr, "ERROR: can't perform the search\n");
463 + /* the ioctl returns the number of item it found in nr_items */
464 + if (sk->nr_items == 0)
468 + for (i = 0; i < sk->nr_items; i++) {
469 + struct btrfs_root_item *item;
470 + sh = (struct btrfs_ioctl_search_header *)(args.buf +
473 + off += sizeof(*sh);
474 + item = (struct btrfs_root_item *)(args.buf + off);
477 + sk->min_objectid = sh->objectid;
478 + sk->min_type = sh->type;
479 + sk->min_offset = sh->offset;
481 + if (sh->objectid > ino_args.treeid)
484 + if (sh->objectid == ino_args.treeid &&
485 + sh->type == BTRFS_ROOT_ITEM_KEY) {
486 + max_found = max(max_found,
487 + btrfs_root_generation(item));
490 + if (sk->min_offset < (u64)-1)
495 + if (sk->min_type != BTRFS_ROOT_ITEM_KEY)
497 + if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY)
503 +/* pass in a directory id and this will return
504 + * the full path of the parent directory inside its
507 + * It may return NULL if it is in the root, or an ERR_PTR if things
510 +static char *__ino_resolve(int fd, u64 dirid)
512 + struct btrfs_ioctl_ino_lookup_args args;
516 + memset(&args, 0, sizeof(args));
517 + args.objectid = dirid;
519 + ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args);
521 + fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu\n",
522 + (unsigned long long)dirid);
523 + return ERR_PTR(ret);
526 + if (args.name[0]) {
528 + * we're in a subdirectory of ref_tree, the kernel ioctl
529 + * puts a / in there for us
531 + full = strdup(args.name);
533 + perror("malloc failed");
534 + return ERR_PTR(-ENOMEM);
537 + /* we're at the root of ref_tree */
544 + * simple string builder, returning a new string with both
547 +char *build_name(char *dirid, char *name)
551 + return strdup(name);
553 + full = malloc(strlen(dirid) + strlen(name) + 1);
556 + strcpy(full, dirid);
557 + strcat(full, name);
562 + * given an inode number, this returns the full path name inside the subvolume
563 + * to that file/directory. cache_dirid and cache_name are used to
564 + * cache the results so we can avoid tree searches if a later call goes
565 + * to the same directory or file name
567 +static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name)
575 + struct btrfs_ioctl_search_args args;
576 + struct btrfs_ioctl_search_key *sk = &args.key;
577 + struct btrfs_ioctl_search_header *sh;
578 + unsigned long off = 0;
581 + memset(&args, 0, sizeof(args));
586 + * step one, we search for the inode back ref. We just use the first
589 + sk->min_objectid = ino;
590 + sk->max_objectid = ino;
591 + sk->max_type = BTRFS_INODE_REF_KEY;
592 + sk->max_offset = (u64)-1;
593 + sk->min_type = BTRFS_INODE_REF_KEY;
594 + sk->max_transid = (u64)-1;
597 + ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
599 + fprintf(stderr, "ERROR: can't perform the search\n");
602 + /* the ioctl returns the number of item it found in nr_items */
603 + if (sk->nr_items == 0)
607 + sh = (struct btrfs_ioctl_search_header *)(args.buf + off);
609 + if (sh->type == BTRFS_INODE_REF_KEY) {
610 + struct btrfs_inode_ref *ref;
611 + dirid = sh->offset;
613 + ref = (struct btrfs_inode_ref *)(sh + 1);
614 + namelen = btrfs_stack_inode_ref_name_len(ref);
616 + name = (char *)(ref + 1);
617 + name = strndup(name, namelen);
619 + /* use our cached value */
620 + if (dirid == *cache_dirid && *cache_name) {
621 + dirname = *cache_name;
628 + * the inode backref gives us the file name and the parent directory id.
629 + * From here we use __ino_resolve to get the path to the parent
631 + dirname = __ino_resolve(fd, dirid);
633 + full = build_name(dirname, name);
634 + if (*cache_name && dirname != *cache_name)
637 + *cache_name = dirname;
638 + *cache_dirid = dirid;
644 +int list_subvols(int fd)
646 + struct root_lookup root_lookup;
649 + struct btrfs_ioctl_search_args args;
650 + struct btrfs_ioctl_search_key *sk = &args.key;
651 + struct btrfs_ioctl_search_header *sh;
652 + struct btrfs_root_ref *ref;
653 + unsigned long off = 0;
659 + root_lookup_init(&root_lookup);
661 + memset(&args, 0, sizeof(args));
663 + /* search in the tree of tree roots */
667 + * set the min and max to backref keys. The search will
668 + * only send back this type of key now.
670 + sk->max_type = BTRFS_ROOT_BACKREF_KEY;
671 + sk->min_type = BTRFS_ROOT_BACKREF_KEY;
674 + * set all the other params to the max, we'll take any objectid
677 + sk->max_objectid = (u64)-1;
678 + sk->max_offset = (u64)-1;
679 + sk->max_transid = (u64)-1;
681 + /* just a big number, doesn't matter much */
682 + sk->nr_items = 4096;
685 + ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
687 + fprintf(stderr, "ERROR: can't perform the search\n");
690 + /* the ioctl returns the number of item it found in nr_items */
691 + if (sk->nr_items == 0)
697 + * for each item, pull the key out of the header and then
698 + * read the root_ref item it contains
700 + for (i = 0; i < sk->nr_items; i++) {
701 + sh = (struct btrfs_ioctl_search_header *)(args.buf +
703 + off += sizeof(*sh);
704 + if (sh->type == BTRFS_ROOT_BACKREF_KEY) {
705 + ref = (struct btrfs_root_ref *)(args.buf + off);
706 + name_len = btrfs_stack_root_ref_name_len(ref);
707 + name = (char *)(ref + 1);
708 + dir_id = btrfs_stack_root_ref_dirid(ref);
710 + add_root(&root_lookup, sh->objectid, sh->offset,
711 + dir_id, name, name_len);
717 + * record the mins in sk so we can make sure the
718 + * next search doesn't repeat this root
720 + sk->min_objectid = sh->objectid;
721 + sk->min_type = sh->type;
722 + sk->min_offset = sh->offset;
724 + sk->nr_items = 4096;
725 + /* this iteration is done, step forward one root for the next
728 + if (sk->min_objectid < (u64)-1) {
729 + sk->min_objectid++;
730 + sk->min_type = BTRFS_ROOT_BACKREF_KEY;
731 + sk->min_offset = 0;
736 + * now we have an rbtree full of root_info objects, but we need to fill
737 + * in their path names within the subvol that is referencing each one.
739 + n = rb_first(&root_lookup.root);
741 + struct root_info *entry;
743 + entry = rb_entry(n, struct root_info, rb_node);
744 + ret = lookup_ino_path(fd, entry);
750 + /* now that we have all the subvol-relative paths filled in,
751 + * we have to string the subvols together so that we can get
752 + * a path all the way back to the FS root
754 + n = rb_last(&root_lookup.root);
756 + struct root_info *entry;
757 + entry = rb_entry(n, struct root_info, rb_node);
758 + resolve_root(&root_lookup, entry);
765 +static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh,
766 + struct btrfs_file_extent_item *item,
767 + u64 found_gen, u64 *cache_dirid,
768 + char **cache_dir_name, u64 *cache_ino,
769 + char **cache_full_name)
775 + int compressed = 0;
779 + if (sh->objectid == *cache_ino) {
780 + name = *cache_full_name;
781 + } else if (*cache_full_name) {
782 + free(*cache_full_name);
783 + *cache_full_name = NULL;
786 + name = ino_resolve(fd, sh->objectid, cache_dirid,
788 + *cache_full_name = name;
789 + *cache_ino = sh->objectid;
794 + type = btrfs_stack_file_extent_type(item);
795 + compressed = btrfs_stack_file_extent_compression(item);
797 + if (type == BTRFS_FILE_EXTENT_REG ||
798 + type == BTRFS_FILE_EXTENT_PREALLOC) {
799 + disk_start = btrfs_stack_file_extent_disk_bytenr(item);
800 + disk_offset = btrfs_stack_file_extent_offset(item);
801 + len = btrfs_stack_file_extent_num_bytes(item);
802 + } else if (type == BTRFS_FILE_EXTENT_INLINE) {
805 + len = btrfs_stack_file_extent_ram_bytes(item);
807 + printf("inode %llu file offset %llu len %llu disk start %llu "
808 + "offset %llu gen %llu flags ",
809 + (unsigned long long)sh->objectid,
810 + (unsigned long long)sh->offset,
811 + (unsigned long long)len,
812 + (unsigned long long)disk_start,
813 + (unsigned long long)disk_offset,
814 + (unsigned long long)found_gen);
817 + printf("COMPRESS");
820 + if (type == BTRFS_FILE_EXTENT_PREALLOC) {
821 + printf("%sPREALLOC", flags ? "|" : "");
824 + if (type == BTRFS_FILE_EXTENT_INLINE) {
825 + printf("%sINLINE", flags ? "|" : "");
831 + printf(" %s\n", name);
835 +int find_updated_files(int fd, u64 root_id, u64 oldest_gen)
838 + struct btrfs_ioctl_search_args args;
839 + struct btrfs_ioctl_search_key *sk = &args.key;
840 + struct btrfs_ioctl_search_header *sh;
841 + struct btrfs_file_extent_item *item;
842 + unsigned long off = 0;
846 + u64 cache_dirid = 0;
848 + char *cache_dir_name = NULL;
849 + char *cache_full_name = NULL;
850 + struct btrfs_file_extent_item backup;
852 + memset(&backup, 0, sizeof(backup));
853 + memset(&args, 0, sizeof(args));
855 + sk->tree_id = root_id;
858 + * set all the other params to the max, we'll take any objectid
861 + sk->max_objectid = (u64)-1;
862 + sk->max_offset = (u64)-1;
863 + sk->max_transid = (u64)-1;
864 + sk->max_type = BTRFS_EXTENT_DATA_KEY;
865 + sk->min_transid = oldest_gen;
866 + /* just a big number, doesn't matter much */
867 + sk->nr_items = 4096;
869 + max_found = find_root_gen(fd);
871 + ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
873 + fprintf(stderr, "ERROR: can't perform the search\n");
876 + /* the ioctl returns the number of item it found in nr_items */
877 + if (sk->nr_items == 0)
883 + * for each item, pull the key out of the header and then
884 + * read the root_ref item it contains
886 + for (i = 0; i < sk->nr_items; i++) {
887 + sh = (struct btrfs_ioctl_search_header *)(args.buf +
889 + off += sizeof(*sh);
892 + * just in case the item was too big, pass something other
898 + item = (struct btrfs_file_extent_item *)(args.buf +
900 + found_gen = btrfs_stack_file_extent_generation(item);
901 + if (sh->type == BTRFS_EXTENT_DATA_KEY &&
902 + found_gen >= oldest_gen) {
903 + print_one_extent(fd, sh, item, found_gen,
904 + &cache_dirid, &cache_dir_name,
905 + &cache_ino, &cache_full_name);
910 + * record the mins in sk so we can make sure the
911 + * next search doesn't repeat this root
913 + sk->min_objectid = sh->objectid;
914 + sk->min_offset = sh->offset;
915 + sk->min_type = sh->type;
917 + sk->nr_items = 4096;
918 + if (sk->min_offset < (u64)-1)
920 + else if (sk->min_objectid < (u64)-1) {
921 + sk->min_objectid++;
922 + sk->min_offset = 0;
927 + free(cache_dir_name);
928 + free(cache_full_name);
929 + printf("transid marker was %llu\n", (unsigned long long)max_found);
932 diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c
934 index 0000000..a109c6a
936 +++ b/btrfs-map-logical.c
939 + * Copyright (C) 2009 Oracle. All rights reserved.
941 + * This program is free software; you can redistribute it and/or
942 + * modify it under the terms of the GNU General Public
943 + * License v2 as published by the Free Software Foundation.
945 + * This program is distributed in the hope that it will be useful,
946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
948 + * General Public License for more details.
950 + * You should have received a copy of the GNU General Public
951 + * License along with this program; if not, write to the
952 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
953 + * Boston, MA 021110-1307, USA.
956 +#define _XOPEN_SOURCE 500
957 +#define _GNU_SOURCE 1
963 +#include "kerncompat.h"
965 +#include "volumes.h"
966 +#include "disk-io.h"
967 +#include "print-tree.h"
968 +#include "transaction.h"
970 +#include "version.h"
972 +/* we write the mirror info to stdout unless they are dumping the data
975 +static FILE *info_file;
977 +struct extent_buffer *debug_read_block(struct btrfs_root *root, u64 bytenr,
978 + u32 blocksize, int copy)
982 + struct extent_buffer *eb;
984 + struct btrfs_multi_bio *multi = NULL;
985 + struct btrfs_device *device;
987 + int mirror_num = 1;
989 + eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
994 + length = blocksize;
996 + ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
997 + eb->start, &length, &multi, mirror_num);
999 + device = multi->stripes[0].dev;
1000 + eb->fd = device->fd;
1001 + device->total_ios++;
1002 + eb->dev_bytenr = multi->stripes[0].physical;
1004 + fprintf(info_file, "mirror %d logical %Lu physical %Lu "
1005 + "device %s\n", mirror_num, bytenr, eb->dev_bytenr,
1009 + if (!copy || mirror_num == copy)
1010 + ret = read_extent_from_disk(eb);
1012 + num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
1013 + eb->start, eb->len);
1014 + if (num_copies == 1)
1018 + if (mirror_num > num_copies)
1024 +static void print_usage(void)
1026 + fprintf(stderr, "usage: btrfs-map-logical [options] mount_point\n");
1027 + fprintf(stderr, "\t-l Logical extent to map\n");
1028 + fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n");
1029 + fprintf(stderr, "\t-o Output file to hold the extent\n");
1030 + fprintf(stderr, "\t-s Number of bytes to read\n");
1034 +static struct option long_options[] = {
1035 + /* { "byte-count", 1, NULL, 'b' }, */
1036 + { "logical", 1, NULL, 'l' },
1037 + { "copy", 1, NULL, 'c' },
1038 + { "output", 1, NULL, 'c' },
1039 + { "bytes", 1, NULL, 'b' },
1043 +int main(int ac, char **av)
1045 + struct cache_tree root_cache;
1046 + struct btrfs_root *root;
1047 + struct extent_buffer *eb;
1049 + char *output_file = NULL;
1052 + int option_index = 0;
1060 + c = getopt_long(ac, av, "l:c:o:b:", long_options,
1066 + logical = atoll(optarg);
1067 + if (logical == 0) {
1069 + "invalid extent number\n");
1074 + copy = atoi(optarg);
1077 + "invalid copy number\n");
1082 + bytes = atoll(optarg);
1085 + "invalid byte count\n");
1090 + output_file = strdup(optarg);
1106 + radix_tree_init();
1107 + cache_tree_init(&root_cache);
1109 + root = open_ctree(dev, 0, 0);
1111 + fprintf(stderr, "Open ctree failed\n");
1115 + if (output_file) {
1116 + if (strcmp(output_file, "-") == 0) {
1118 + info_file = stderr;
1120 + out_fd = open(output_file, O_RDWR | O_CREAT, 0600);
1123 + err = ftruncate(out_fd, 0);
1128 + info_file = stdout;
1133 + bytes = root->sectorsize;
1135 + bytes = (bytes + root->sectorsize - 1) / root->sectorsize;
1136 + bytes *= root->sectorsize;
1138 + while (bytes > 0) {
1139 + eb = debug_read_block(root, logical, root->sectorsize, copy);
1140 + if (eb && output_file) {
1141 + err = write(out_fd, eb->data, eb->len);
1142 + if (err < 0 || err != eb->len) {
1143 + fprintf(stderr, "output file write failed\n");
1144 + goto out_close_fd;
1147 + free_extent_buffer(eb);
1148 + logical += root->sectorsize;
1149 + bytes -= root->sectorsize;
1153 + if (output_file && out_fd != 1)
1156 + close_ctree(root);
1159 diff --git a/btrfs.c b/btrfs.c
1160 new file mode 100644
1161 index 0000000..ab5e57f
1166 + * This program is free software; you can redistribute it and/or
1167 + * modify it under the terms of the GNU General Public
1168 + * License v2 as published by the Free Software Foundation.
1170 + * This program is distributed in the hope that it will be useful,
1171 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1172 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1173 + * General Public License for more details.
1175 + * You should have received a copy of the GNU General Public
1176 + * License along with this program; if not, write to the
1177 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1178 + * Boston, MA 021110-1307, USA.
1183 +#include <stdlib.h>
1184 +#include <string.h>
1186 +#include "kerncompat.h"
1187 +#include "btrfs_cmds.h"
1188 +#include "version.h"
1190 +typedef int (*CommandFunction)(int argc, char **argv);
1193 + CommandFunction func; /* function which implements the command */
1194 + int nargs; /* if == 999, any number of arguments
1195 + if >= 0, number of arguments,
1196 + if < 0, _minimum_ number of arguments */
1197 + char *verb; /* verb */
1198 + char *help; /* help lines; form the 2nd onward they are
1201 + /* the following fields are run-time filled by the program */
1202 + char **cmds; /* array of subcommands */
1203 + int ncmds; /* number of subcommand */
1206 +static struct Command commands[] = {
1209 + avoid short commands different for the case only
1212 + "subvolume snapshot", "<source> [<dest>/]<name>\n"
1213 + "Create a writable snapshot of the subvolume <source> with\n"
1214 + "the name <name> in the <dest> directory."
1216 + { do_delete_subvolume, 1,
1217 + "subvolume delete", "<subvolume>\n"
1218 + "Delete the subvolume <subvolume>."
1220 + { do_create_subvol, 1,
1221 + "subvolume create", "[<dest>/]<name>\n"
1222 + "Create a subvolume in <dest> (or the current directory if\n"
1225 + { do_subvol_list, 1, "subvolume list", "<path>\n"
1226 + "List the snapshot/subvolume of a filesystem."
1228 + { do_find_newer, 2, "subvolume find-new", "<path> <last_gen>\n"
1229 + "List the recently modified files in a filesystem."
1232 + "filesystem defragment", "[-vcf] [-s start] [-l len] [-t size] <file>|<dir> [<file>|<dir>...]\n"
1233 + "Defragment a file or a directory."
1235 + { do_set_default_subvol, 2,
1236 + "subvolume set-default", "<id> <path>\n"
1237 + "Set the subvolume of the filesystem <path> which will be mounted\n"
1241 + "filesystem sync", "<path>\n"
1242 + "Force a sync on the filesystem <path>."
1245 + "filesystem resize", "[+/-]<newsize>[gkm]|max <filesystem>\n"
1246 + "Resize the file system. If 'max' is passed, the filesystem\n"
1247 + "will occupe all available space on the device."
1249 + { do_show_filesystem, 999,
1250 + "filesystem show", "[<uuid>|<label>]\n"
1251 + "Show the info of a btrfs filesystem. If no <uuid> or <label>\n"
1252 + "is passed, info of all the btrfs filesystem are shown."
1254 + { do_df_filesystem, 1,
1255 + "filesystem df", "<path>\n"
1256 + "Show space usage information for a mount point\n."
1259 + "filesystem balance", "<path>\n"
1260 + "Balance the chunks across the device."
1263 + 999, "device scan", "[<device> [<device>..]\n"
1264 + "Scan all device for or the passed device for a btrfs\n"
1267 + { do_add_volume, -2,
1268 + "device add", "<dev> [<dev>..] <path>\n"
1269 + "Add a device to a filesystem."
1271 + { do_remove_volume, -2,
1272 + "device delete", "<dev> [<dev>..] <path>\n"
1273 + "Remove a device from a filesystem."
1276 + { 2, "filesystem label", "<label> <path>\n"
1277 + "Set the label of a filesystem"
1283 +static char *get_prgname(char *programname)
1286 + np = strrchr(programname,'/');
1295 +static void print_help(char *programname, struct Command *cmd)
1299 + printf("\t%s %s ", programname, cmd->verb );
1301 + for(pc = cmd->help; *pc; pc++){
1309 +static void help(char *np)
1311 + struct Command *cp;
1313 + printf("Usage:\n");
1314 + for( cp = commands; cp->verb; cp++ )
1315 + print_help(np, cp);
1317 + printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
1318 + printf("\n%s\n", BTRFS_BUILD_VERSION);
1321 +static int split_command(char *cmd, char ***commands)
1326 + for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
1327 + if ( *p && *p != ' ' )
1330 + /* c + 2 so that we have room for the null */
1331 + (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
1332 + (*commands)[c] = strndup(s, l);
1339 + (*commands)[c] = 0;
1344 + This function checks if the passed command is ambiguous
1346 +static int check_ambiguity(struct Command *cmd, char **argv){
1348 + struct Command *cp;
1349 + /* check for ambiguity */
1350 + for( i = 0 ; i < cmd->ncmds ; i++ ){
1352 + for( match = 0, cp = commands; cp->verb; cp++ ){
1356 + if( cp->ncmds < i )
1359 + for( skip = 0, j = 0 ; j < i ; j++ )
1360 + if( strcmp(cmd->cmds[j], cp->cmds[j])){
1367 + if( !strcmp(cmd->cmds[i], cp->cmds[i]))
1369 + for(s2 = cp->cmds[i], s1 = argv[i+1];
1370 + *s1 == *s2 && *s1; s1++, s2++ ) ;
1376 + fprintf(stderr, "ERROR: in command '");
1377 + for( j = 0 ; j <= i ; j++ )
1378 + fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
1379 + fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
1387 + * This function, compacts the program name and the command in the first
1388 + * element of the '*av' array
1390 +static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
1396 + ret = (char **)malloc(sizeof(char*)*(*ac+1));
1397 + newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
1398 + if( !ret || !newname ){
1405 + for(i=0; i < *ac ; i++ )
1406 + ret[i+1] = (*av)[i];
1408 + strcpy(newname, prgname);
1409 + strcat(newname, " ");
1410 + strcat(newname, cmd->verb);
1423 + This function perform the following jobs:
1424 + - show the help if '--help' or 'help' or '-h' are passed
1425 + - verify that a command is not ambiguous, otherwise show which
1426 + part of the command is ambiguous
1427 + - if after a (even partial) command there is '--help' show the help
1428 + for all the matching commands
1429 + - if the command doesn't' match show an error
1430 + - finally, if a command match, they return which command is matched and
1433 + The function return 0 in case of help is requested; <0 in case
1434 + of uncorrect command; >0 in case of matching commands
1435 + argc, argv are the arg-counter and arg-vector (input)
1436 + *nargs_ is the number of the arguments after the command (output)
1437 + **cmd_ is the invoked command (output)
1438 + ***args_ are the arguments after the command
1441 +static int parse_args(int argc, char **argv,
1442 + CommandFunction *func_,
1443 + int *nargs_, char **cmd_, char ***args_ )
1445 + struct Command *cp;
1446 + struct Command *matchcmd=0;
1447 + char *prgname = get_prgname(argv[0]);
1448 + int i=0, helprequested=0;
1450 + if( argc < 2 || !strcmp(argv[1], "help") ||
1451 + !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
1456 + for( cp = commands; cp->verb; cp++ )
1458 + cp->ncmds = split_command(cp->verb, &(cp->cmds));
1460 + for( cp = commands; cp->verb; cp++ ){
1463 + if( argc-1 < cp->ncmds )
1465 + for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
1470 + for(s2 = cp->cmds[i], s1 = argv[i+1];
1471 + *s1 == *s2 && *s1;
1479 + /* If you understand why this code works ...
1480 + you are a genious !! */
1481 + if(argc>i+1 && !strcmp(argv[i+1],"--help")){
1482 + if(!helprequested)
1483 + printf("Usage:\n");
1484 + print_help(prgname, cp);
1493 + *nargs_ = argc-matchcmd->ncmds-1;
1494 + *cmd_ = matchcmd->verb;
1495 + *args_ = argv+matchcmd->ncmds+1;
1496 + *func_ = cp->func;
1501 + if(helprequested){
1502 + printf("\n%s\n", BTRFS_BUILD_VERSION);
1507 + fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
1512 + if(check_ambiguity(matchcmd, argv))
1515 + /* check the number of argument */
1516 + if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
1517 + fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
1518 + matchcmd->verb, -matchcmd->nargs);
1521 + if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
1522 + fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
1523 + matchcmd->verb, matchcmd->nargs);
1527 + if (prepare_args( nargs_, args_, prgname, matchcmd )){
1528 + fprintf(stderr, "ERROR: not enough memory\\n");
1535 +int main(int ac, char **av )
1538 + char *cmd=0, **args=0;
1540 + CommandFunction func=0;
1542 + r = parse_args(ac, av, &func, &nargs, &cmd, &args);
1544 + /* error or no command to parse*/
1548 + exit(func(nargs, args));
1552 diff --git a/btrfs_cmds.c b/btrfs_cmds.c
1553 new file mode 100644
1554 index 0000000..8031c58
1559 + * This program is free software; you can redistribute it and/or
1560 + * modify it under the terms of the GNU General Public
1561 + * License v2 as published by the Free Software Foundation.
1563 + * This program is distributed in the hope that it will be useful,
1564 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1565 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1566 + * General Public License for more details.
1568 + * You should have received a copy of the GNU General Public
1569 + * License along with this program; if not, write to the
1570 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1571 + * Boston, MA 021110-1307, USA.
1576 +#include <stdlib.h>
1577 +#include <string.h>
1578 +#include <sys/ioctl.h>
1579 +#include <sys/types.h>
1580 +#include <dirent.h>
1581 +#include <sys/stat.h>
1582 +#include <unistd.h>
1584 +#include <libgen.h>
1585 +#include <limits.h>
1586 +#include <uuid/uuid.h>
1591 +#include "kerncompat.h"
1593 +#include "transaction.h"
1595 +#include "version.h"
1597 +#include "volumes.h"
1599 +#include "btrfs_cmds.h"
1602 +#define BLKGETSIZE64 0
1603 +#define BTRFS_IOC_SNAP_CREATE 0
1604 +#define BTRFS_VOL_NAME_MAX 255
1605 +struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; };
1606 +static inline int ioctl(int fd, int define, void *arg) { return 0; }
1610 + * test if path is a subvolume:
1611 + * this function return
1612 + * 0-> path exists but it is not a subvolume
1613 + * 1-> path exists and it is a subvolume
1614 + * -1 -> path is unaccessible
1616 +static int test_issubvolume(char *path)
1622 + res = stat(path, &st);
1626 + return (st.st_ino == 256) && S_ISDIR(st.st_mode);
1631 + * test if path is a directory
1632 + * this function return
1633 + * 0-> path exists but it is not a directory
1634 + * 1-> path exists and it is a directory
1635 + * -1 -> path is unaccessible
1637 +static int test_isdir(char *path)
1642 + res = stat(path, &st);
1646 + return S_ISDIR(st.st_mode);
1650 +static int open_file_or_dir(const char *fname)
1657 + ret = stat(fname, &st);
1661 + if (S_ISDIR(st.st_mode)) {
1662 + dirstream = opendir(fname);
1666 + fd = dirfd(dirstream);
1668 + fd = open(fname, O_RDWR);
1676 +static u64 parse_size(char *s)
1678 + int len = strlen(s);
1682 + if (!isdigit(s[len - 1])) {
1683 + c = tolower(s[len - 1]);
1694 + fprintf(stderr, "Unknown size descriptor %c\n", c);
1697 + s[len - 1] = '\0';
1699 + return atoll(s) * mult;
1702 +int do_defrag(int ac, char **av)
1708 + u64 len = (u64)-1;
1714 + int fancy_ioctl = 0;
1715 + struct btrfs_ioctl_defrag_range_args range;
1719 + int c = getopt(ac, av, "vcfs:l:t:");
1735 + start = parse_size(optarg);
1739 + len = parse_size(optarg);
1743 + thresh = parse_size(optarg);
1747 + fprintf(stderr, "Invalid arguments for defragment\n");
1752 + if (ac - optind == 0) {
1753 + fprintf(stderr, "Invalid arguments for defragment\n");
1758 + memset(&range, 0, sizeof(range));
1759 + range.start = start;
1761 + range.extent_thresh = thresh;
1763 + range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
1765 + range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
1767 + for (i = optind; i < ac; i++) {
1769 + printf("%s\n", av[i]);
1770 + fd = open_file_or_dir(av[i]);
1772 + fprintf(stderr, "failed to open %s\n", av[i]);
1777 + if (!fancy_ioctl) {
1778 + ret = ioctl(fd, BTRFS_IOC_DEFRAG, NULL);
1780 + ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE, &range);
1781 + if (ret && errno == ENOTTY) {
1782 + fprintf(stderr, "defrag range ioctl not "
1783 + "supported in this kernel, please try "
1784 + "without any options.\n");
1790 + fprintf(stderr, "ioctl failed on %s ret %d errno %d\n",
1791 + av[i], ret, errno);
1797 + printf("%s\n", BTRFS_BUILD_VERSION);
1799 + fprintf(stderr, "total %d failures\n", errors);
1804 + return errors + 20;
1807 +int do_find_newer(int argc, char **argv)
1815 + last_gen = atoll(argv[2]);
1817 + ret = test_issubvolume(subvol);
1819 + fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
1823 + fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
1827 + fd = open_file_or_dir(subvol);
1829 + fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
1832 + ret = find_updated_files(fd, 0, last_gen);
1838 +int do_subvol_list(int argc, char **argv)
1846 + ret = test_issubvolume(subvol);
1848 + fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
1852 + fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
1856 + fd = open_file_or_dir(subvol);
1858 + fprintf(stderr, "ERROR: can't access '%s'\n", subvol);
1861 + ret = list_subvols(fd);
1867 +int do_clone(int argc, char **argv)
1869 + char *subvol, *dst;
1870 + int res, fd, fddst, len;
1876 + struct btrfs_ioctl_vol_args args;
1878 + res = test_issubvolume(subvol);
1880 + fprintf(stderr, "ERROR: error accessing '%s'\n", subvol);
1884 + fprintf(stderr, "ERROR: '%s' is not a subvolume\n", subvol);
1888 + res = test_isdir(dst);
1890 + fprintf(stderr, "ERROR: '%s' exists and it is not a directory\n", dst);
1895 + newname = strdup(subvol);
1896 + newname = basename(newname);
1899 + newname = strdup(dst);
1900 + newname = basename(newname);
1901 + dstdir = strdup(dst);
1902 + dstdir = dirname(dstdir);
1905 + if( !strcmp(newname,".") || !strcmp(newname,"..") ||
1906 + strchr(newname, '/') ){
1907 + fprintf(stderr, "ERROR: incorrect snapshot name ('%s')\n",
1912 + len = strlen(newname);
1913 + if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
1914 + fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
1919 + fddst = open_file_or_dir(dstdir);
1921 + fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
1925 + fd = open_file_or_dir(subvol);
1928 + fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
1932 + printf("Create a snapshot of '%s' in '%s/%s'\n",
1933 + subvol, dstdir, newname);
1935 + strcpy(args.name, newname);
1936 + res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE, &args);
1942 + fprintf( stderr, "ERROR: cannot snapshot '%s'\n",subvol);
1950 +int do_delete_subvolume(int argc, char **argv)
1953 + struct btrfs_ioctl_vol_args args;
1954 + char *dname, *vname, *cpath;
1955 + char *path = argv[1];
1957 + res = test_issubvolume(path);
1959 + fprintf(stderr, "ERROR: error accessing '%s'\n", path);
1963 + fprintf(stderr, "ERROR: '%s' is not a subvolume\n", path);
1967 + cpath = realpath(path, 0);
1968 + dname = strdup(cpath);
1969 + dname = dirname(dname);
1970 + vname = strdup(cpath);
1971 + vname = basename(vname);
1974 + if( !strcmp(vname,".") || !strcmp(vname,"..") ||
1975 + strchr(vname, '/') ){
1976 + fprintf(stderr, "ERROR: incorrect subvolume name ('%s')\n",
1981 + len = strlen(vname);
1982 + if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
1983 + fprintf(stderr, "ERROR: snapshot name too long ('%s)\n",
1988 + fd = open_file_or_dir(dname);
1991 + fprintf(stderr, "ERROR: can't access to '%s'\n", dname);
1995 + printf("Delete subvolume '%s/%s'\n", dname, vname);
1996 + strcpy(args.name, vname);
1997 + res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
2002 + fprintf( stderr, "ERROR: cannot delete '%s/%s'\n",dname, vname);
2010 +int do_create_subvol(int argc, char **argv)
2012 + int res, fddst, len;
2015 + struct btrfs_ioctl_vol_args args;
2016 + char *dst = argv[1];
2018 + res = test_isdir(dst);
2020 + fprintf(stderr, "ERROR: '%s' exists\n", dst);
2024 + newname = strdup(dst);
2025 + newname = basename(newname);
2026 + dstdir = strdup(dst);
2027 + dstdir = dirname(dstdir);
2029 + if( !strcmp(newname,".") || !strcmp(newname,"..") ||
2030 + strchr(newname, '/') ){
2031 + fprintf(stderr, "ERROR: uncorrect subvolume name ('%s')\n",
2036 + len = strlen(newname);
2037 + if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
2038 + fprintf(stderr, "ERROR: subvolume name too long ('%s)\n",
2043 + fddst = open_file_or_dir(dstdir);
2045 + fprintf(stderr, "ERROR: can't access to '%s'\n", dstdir);
2049 + printf("Create subvolume '%s/%s'\n", dstdir, newname);
2050 + strcpy(args.name, newname);
2051 + res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
2056 + fprintf( stderr, "ERROR: cannot create subvolume\n");
2064 +int do_fssync(int argc, char **argv)
2067 + char *path = argv[1];
2069 + fd = open_file_or_dir(path);
2071 + fprintf(stderr, "ERROR: can't access to '%s'\n", path);
2075 + printf("FSSync '%s'\n", path);
2076 + res = ioctl(fd, BTRFS_IOC_SYNC);
2079 + fprintf(stderr, "ERROR: unable to fs-syncing '%s'\n", path);
2086 +int do_scan(int argc, char **argv)
2092 + printf("Scanning for Btrfs filesystems\n");
2093 + ret = btrfs_scan_one_dir("/dev", 1);
2095 + fprintf(stderr, "ERROR: error %d while scanning\n", ret);
2101 + fd = open("/dev/btrfs-control", O_RDWR);
2103 + perror("failed to open /dev/btrfs-control");
2107 + for( i = 1 ; i < argc ; i++ ){
2108 + struct btrfs_ioctl_vol_args args;
2111 + printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
2113 + strcpy(args.name, argv[i]);
2115 + * FIXME: which are the error code returned by this ioctl ?
2116 + * it seems that is impossible to understand if there no is
2117 + * a btrfs filesystem from an I/O error !!!
2119 + ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
2123 + fprintf(stderr, "ERROR: unable to scan the device '%s'\n", argv[i]);
2133 +int do_resize(int argc, char **argv)
2136 + struct btrfs_ioctl_vol_args args;
2138 + char *amount=argv[1], *path=argv[2];
2140 + fd = open_file_or_dir(path);
2142 + fprintf(stderr, "ERROR: can't access to '%s'\n", path);
2145 + len = strlen(amount);
2146 + if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
2147 + fprintf(stderr, "ERROR: size value too long ('%s)\n",
2152 + printf("Resize '%s' of '%s'\n", path, amount);
2153 + strcpy(args.name, amount);
2154 + res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
2157 + fprintf(stderr, "ERROR: unable to resize '%s'\n", path);
2163 +static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search)
2165 + struct list_head *cur;
2166 + struct btrfs_device *device;
2168 + list_for_each(cur, &fs_devices->devices) {
2169 + device = list_entry(cur, struct btrfs_device, dev_list);
2170 + if ((device->label && strcmp(device->label, search) == 0) ||
2171 + strcmp(device->name, search) == 0)
2177 +static void print_one_uuid(struct btrfs_fs_devices *fs_devices)
2180 + struct list_head *cur;
2181 + struct btrfs_device *device;
2182 + char *super_bytes_used;
2183 + u64 devs_found = 0;
2186 + uuid_unparse(fs_devices->fsid, uuidbuf);
2187 + device = list_entry(fs_devices->devices.next, struct btrfs_device,
2189 + if (device->label && device->label[0])
2190 + printf("Label: '%s' ", device->label);
2192 + printf("Label: none ");
2194 + super_bytes_used = pretty_sizes(device->super_bytes_used);
2196 + total = device->total_devs;
2197 + printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
2198 + (unsigned long long)total, super_bytes_used);
2200 + free(super_bytes_used);
2202 + list_for_each(cur, &fs_devices->devices) {
2203 + char *total_bytes;
2205 + device = list_entry(cur, struct btrfs_device, dev_list);
2206 + total_bytes = pretty_sizes(device->total_bytes);
2207 + bytes_used = pretty_sizes(device->bytes_used);
2208 + printf("\tdevid %4llu size %s used %s path %s\n",
2209 + (unsigned long long)device->devid,
2210 + total_bytes, bytes_used, device->name);
2211 + free(total_bytes);
2215 + if (devs_found < total) {
2216 + printf("\t*** Some devices missing\n");
2221 +int do_show_filesystem(int argc, char **argv)
2223 + struct list_head *all_uuids;
2224 + struct btrfs_fs_devices *fs_devices;
2225 + struct list_head *cur_uuid;
2226 + char *search = argv[1];
2229 + ret = btrfs_scan_one_dir("/dev", 0);
2231 + fprintf(stderr, "ERROR: error %d while scanning\n", ret);
2235 + all_uuids = btrfs_scanned_uuids();
2236 + list_for_each(cur_uuid, all_uuids) {
2237 + fs_devices = list_entry(cur_uuid, struct btrfs_fs_devices,
2239 + if (search && uuid_search(fs_devices, search) == 0)
2241 + print_one_uuid(fs_devices);
2243 + printf("%s\n", BTRFS_BUILD_VERSION);
2247 +int do_add_volume(int nargs, char **args)
2250 + char *mntpnt = args[nargs-1];
2251 + int i, fdmnt, ret=0;
2254 + fdmnt = open_file_or_dir(mntpnt);
2256 + fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
2260 + for(i=1 ; i < (nargs-1) ; i++ ){
2261 + struct btrfs_ioctl_vol_args ioctl_args;
2263 + u64 dev_block_count = 0;
2266 + devfd = open(args[i], O_RDWR);
2268 + fprintf(stderr, "ERROR: Unable to open device '%s'\n", args[i]);
2273 + ret = fstat(devfd, &st);
2275 + fprintf(stderr, "ERROR: Unable to stat '%s'\n", args[i]);
2280 + if (!S_ISBLK(st.st_mode)) {
2281 + fprintf(stderr, "ERROR: '%s' is not a block device\n", args[i]);
2287 + res = btrfs_prepare_device(devfd, args[i], 1, &dev_block_count);
2289 + fprintf(stderr, "ERROR: Unable to init '%s'\n", args[i]);
2296 + strcpy(ioctl_args.name, args[i]);
2297 + res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
2299 + fprintf(stderr, "ERROR: error adding the device '%s'\n", args[i]);
2313 +int do_balance(int argc, char **argv)
2317 + struct btrfs_ioctl_vol_args args;
2318 + char *path = argv[1];
2320 + fdmnt = open_file_or_dir(path);
2322 + fprintf(stderr, "ERROR: can't access to '%s'\n", path);
2326 + memset(&args, 0, sizeof(args));
2327 + ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args);
2330 + fprintf(stderr, "ERROR: balancing '%s'\n", path);
2336 +int do_remove_volume(int nargs, char **args)
2339 + char *mntpnt = args[nargs-1];
2340 + int i, fdmnt, ret=0;
2342 + fdmnt = open_file_or_dir(mntpnt);
2344 + fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt);
2348 + for(i=1 ; i < (nargs-1) ; i++ ){
2349 + struct btrfs_ioctl_vol_args arg;
2352 + strcpy(arg.name, args[i]);
2353 + res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
2355 + fprintf(stderr, "ERROR: error removing the device '%s'\n", args[i]);
2367 +int do_set_default_subvol(int nargs, char **argv)
2371 + char *path = argv[2];
2372 + char *subvolid = argv[1];
2374 + fd = open_file_or_dir(path);
2376 + fprintf(stderr, "ERROR: can't access to '%s'\n", path);
2380 + objectid = (unsigned long long)strtoll(subvolid, NULL, 0);
2381 + if (errno == ERANGE) {
2382 + fprintf(stderr, "ERROR: invalid tree id (%s)\n",subvolid);
2385 + ret = ioctl(fd, BTRFS_IOC_DEFAULT_SUBVOL, &objectid);
2388 + fprintf(stderr, "ERROR: unable to set a new default subvolume\n");
2394 +int do_df_filesystem(int nargs, char **argv)
2396 + struct btrfs_ioctl_space_args *sargs;
2400 + char *path = argv[1];
2402 + fd = open_file_or_dir(path);
2404 + fprintf(stderr, "ERROR: can't access to '%s'\n", path);
2408 + sargs = malloc(sizeof(struct btrfs_ioctl_space_args));
2412 + sargs->space_slots = 0;
2413 + sargs->total_spaces = 0;
2415 + ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
2420 + if (!sargs->total_spaces)
2423 + count = sargs->total_spaces;
2425 + sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) +
2426 + (count * sizeof(struct btrfs_ioctl_space_info)));
2430 + sargs->space_slots = count;
2431 + sargs->total_spaces = 0;
2433 + ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs);
2439 + for (i = 0; i < sargs->total_spaces; i++) {
2440 + char description[80];
2441 + char *total_bytes;
2444 + u64 flags = sargs->spaces[i].flags;
2446 + memset(description, 0, 80);
2448 + if (flags & BTRFS_BLOCK_GROUP_DATA) {
2449 + snprintf(description, 5, "%s", "Data");
2451 + } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2452 + snprintf(description, 7, "%s", "System");
2454 + } else if (flags & BTRFS_BLOCK_GROUP_METADATA) {
2455 + snprintf(description, 9, "%s", "Metadata");
2459 + if (flags & BTRFS_BLOCK_GROUP_RAID0) {
2460 + snprintf(description+written, 8, "%s", ", RAID0");
2462 + } else if (flags & BTRFS_BLOCK_GROUP_RAID1) {
2463 + snprintf(description+written, 8, "%s", ", RAID1");
2465 + } else if (flags & BTRFS_BLOCK_GROUP_DUP) {
2466 + snprintf(description+written, 6, "%s", ", DUP");
2468 + } else if (flags & BTRFS_BLOCK_GROUP_RAID10) {
2469 + snprintf(description+written, 9, "%s", ", RAID10");
2473 + total_bytes = pretty_sizes(sargs->spaces[i].total_bytes);
2474 + used_bytes = pretty_sizes(sargs->spaces[i].used_bytes);
2475 + printf("%s: total=%s, used=%s\n", description, total_bytes,
2482 diff --git a/btrfs_cmds.h b/btrfs_cmds.h
2483 new file mode 100644
2484 index 0000000..7bde191
2489 + * This program is free software; you can redistribute it and/or
2490 + * modify it under the terms of the GNU General Public
2491 + * License v2 as published by the Free Software Foundation.
2493 + * This program is distributed in the hope that it will be useful,
2494 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2495 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2496 + * General Public License for more details.
2498 + * You should have received a copy of the GNU General Public
2499 + * License along with this program; if not, write to the
2500 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
2501 + * Boston, MA 021110-1307, USA.
2505 +int do_clone(int nargs, char **argv);
2506 +int do_delete_subvolume(int nargs, char **argv);
2507 +int do_create_subvol(int nargs, char **argv);
2508 +int do_fssync(int nargs, char **argv);
2509 +int do_defrag(int argc, char **argv);
2510 +int do_show_filesystem(int nargs, char **argv);
2511 +int do_add_volume(int nargs, char **args);
2512 +int do_balance(int nargs, char **argv);
2513 +int do_remove_volume(int nargs, char **args);
2514 +int do_scan(int nargs, char **argv);
2515 +int do_resize(int nargs, char **argv);
2516 +int do_subvol_list(int nargs, char **argv);
2517 +int do_set_default_subvol(int nargs, char **argv);
2518 +int list_subvols(int fd);
2519 +int do_df_filesystem(int nargs, char **argv);
2520 +int find_updated_files(int fd, u64 root_id, u64 oldest_gen);
2521 +int do_find_newer(int argc, char **argv);
2522 diff --git a/btrfsck.c b/btrfsck.c
2523 index 40c90f8..73f1836 100644
2526 @@ -36,7 +36,7 @@ static u64 total_fs_tree_bytes = 0;
2527 static u64 btree_space_waste = 0;
2528 static u64 data_bytes_allocated = 0;
2529 static u64 data_bytes_referenced = 0;
2530 -int found_old_backref = 0;
2531 +static int found_old_backref = 0;
2533 struct extent_backref {
2534 struct list_head list;
2535 @@ -100,7 +100,11 @@ struct inode_backref {
2536 #define REF_ERR_DUP_INODE_REF (1 << 5)
2537 #define REF_ERR_INDEX_UNMATCH (1 << 6)
2538 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
2539 -#define REF_ERR_NAME_TOO_LONG (1 << 8)
2540 +#define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
2541 +#define REF_ERR_NO_ROOT_REF (1 << 9)
2542 +#define REF_ERR_NO_ROOT_BACKREF (1 << 10)
2543 +#define REF_ERR_DUP_ROOT_REF (1 << 11)
2544 +#define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
2546 struct inode_record {
2547 struct list_head backrefs;
2548 @@ -144,6 +148,29 @@ struct inode_record {
2549 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
2550 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
2552 +struct root_backref {
2553 + struct list_head list;
2554 + unsigned int found_dir_item:1;
2555 + unsigned int found_dir_index:1;
2556 + unsigned int found_back_ref:1;
2557 + unsigned int found_forward_ref:1;
2558 + unsigned int reachable:1;
2567 +struct root_record {
2568 + struct list_head backrefs;
2569 + struct cache_extent cache;
2570 + unsigned int found_root_item:1;
2576 struct cache_extent cache;
2578 @@ -151,6 +178,7 @@ struct ptr_node {
2580 struct shared_node {
2581 struct cache_extent cache;
2582 + struct cache_tree root_cache;
2583 struct cache_tree inode_cache;
2584 struct inode_record *current;
2586 @@ -258,6 +286,14 @@ static void free_inode_rec(struct inode_record *rec)
2590 +static int can_free_inode_rec(struct inode_record *rec)
2592 + if (!rec->errors && rec->checked && rec->found_inode_item &&
2593 + rec->nlink == rec->found_link && list_empty(&rec->backrefs))
2598 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
2599 struct inode_record *rec)
2601 @@ -309,8 +345,7 @@ static void maybe_free_inode_rec(struct cache_tree *inode_cache,
2604 BUG_ON(rec->refs != 1);
2605 - if (!rec->errors && rec->nlink == rec->found_link &&
2606 - list_empty(&rec->backrefs)) {
2607 + if (can_free_inode_rec(rec)) {
2608 cache = find_cache_extent(inode_cache, rec->ino, 1);
2609 node = container_of(cache, struct ptr_node, cache);
2610 BUG_ON(node->data != rec);
2611 @@ -338,14 +373,12 @@ static int check_orphan_item(struct btrfs_root *root, u64 ino)
2615 -static int process_inode_item(struct btrfs_root *root,
2616 - struct extent_buffer *eb,
2617 +static int process_inode_item(struct extent_buffer *eb,
2618 int slot, struct btrfs_key *key,
2619 struct shared_node *active_node)
2621 struct inode_record *rec;
2622 struct btrfs_inode_item *item;
2625 rec = active_node->current;
2626 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
2627 @@ -361,11 +394,8 @@ static int process_inode_item(struct btrfs_root *root,
2628 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
2630 rec->found_inode_item = 1;
2631 - if (rec->nlink == 0) {
2632 - ret = check_orphan_item(root, rec->ino);
2633 - if (ret == -ENOENT)
2634 - rec->errors |= I_ERR_NO_ORPHAN_ITEM;
2636 + if (rec->nlink == 0)
2637 + rec->errors |= I_ERR_NO_ORPHAN_ITEM;
2638 maybe_free_inode_rec(&active_node->inode_cache, rec);
2641 @@ -443,10 +473,9 @@ static int add_inode_backref(struct cache_tree *inode_cache,
2644 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
2645 - struct shared_node *dst_node)
2646 + struct cache_tree *dst_cache)
2648 struct inode_backref *backref;
2649 - struct cache_tree *dst_cache = &dst_node->inode_cache;
2652 list_for_each_entry(backref, &src->backrefs, list) {
2653 @@ -510,14 +539,8 @@ static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
2654 dst->errors |= I_ERR_DUP_INODE_ITEM;
2658 - if (src->checked) {
2660 - if (dst_node->current == dst)
2661 - dst_node->current = NULL;
2664 - maybe_free_inode_rec(dst_cache, dst);
2669 @@ -537,8 +560,9 @@ static int splice_shared_node(struct shared_node *src_node,
2670 if (src_node->current)
2671 current_ino = src_node->current->ino;
2673 - src = &src_node->inode_cache;
2674 - dst = &dst_node->inode_cache;
2675 + src = &src_node->root_cache;
2676 + dst = &dst_node->root_cache;
2678 cache = find_first_cache_extent(src, 0);
2680 node = container_of(cache, struct ptr_node, cache);
2681 @@ -557,14 +581,28 @@ static int splice_shared_node(struct shared_node *src_node,
2683 ret = insert_existing_cache_extent(dst, &ins->cache);
2684 if (ret == -EEXIST) {
2685 + WARN_ON(src == &src_node->root_cache);
2686 conflict = get_inode_rec(dst, rec->ino, 1);
2687 - merge_inode_recs(rec, conflict, dst_node);
2688 + merge_inode_recs(rec, conflict, dst);
2689 + if (rec->checked) {
2690 + conflict->checked = 1;
2691 + if (dst_node->current == conflict)
2692 + dst_node->current = NULL;
2694 + maybe_free_inode_rec(dst, conflict);
2695 free_inode_rec(rec);
2702 + if (src == &src_node->root_cache) {
2703 + src = &src_node->inode_cache;
2704 + dst = &dst_node->inode_cache;
2708 if (current_ino > 0 && (!dst_node->current ||
2709 current_ino > dst_node->current->ino)) {
2710 if (dst_node->current) {
2711 @@ -616,6 +654,7 @@ static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
2712 node = calloc(1, sizeof(*node));
2713 node->cache.start = bytenr;
2714 node->cache.size = 1;
2715 + cache_tree_init(&node->root_cache);
2716 cache_tree_init(&node->inode_cache);
2719 @@ -646,6 +685,7 @@ static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
2720 if (wc->root_level == wc->active_node &&
2721 btrfs_root_refs(&root->root_item) == 0) {
2722 if (--node->refs == 0) {
2723 + free_inode_recs(&node->root_cache);
2724 free_inode_recs(&node->inode_cache);
2725 remove_cache_extent(&wc->shared, &node->cache);
2727 @@ -708,10 +748,12 @@ static int process_dir_item(struct extent_buffer *eb,
2729 struct btrfs_dir_item *di;
2730 struct inode_record *rec;
2731 + struct cache_tree *root_cache;
2732 struct cache_tree *inode_cache;
2733 struct btrfs_key location;
2734 char namebuf[BTRFS_NAME_LEN];
2736 + root_cache = &active_node->root_cache;
2737 inode_cache = &active_node->inode_cache;
2738 rec = active_node->current;
2739 rec->found_dir_item = 1;
2740 @@ -740,7 +782,9 @@ static int process_dir_item(struct extent_buffer *eb,
2741 key->objectid, key->offset, namebuf,
2742 len, filetype, key->type, error);
2743 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
2744 - /* fixme: check root back & forward references */
2745 + add_inode_backref(root_cache, location.objectid,
2746 + key->objectid, key->offset, namebuf,
2747 + len, filetype, key->type, error);
2749 fprintf(stderr, "warning line %d\n", __LINE__);
2751 @@ -977,8 +1021,7 @@ static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
2752 ret = process_inode_ref(eb, i, &key, active_node);
2754 case BTRFS_INODE_ITEM_KEY:
2755 - ret = process_inode_item(root, eb, i, &key,
2757 + ret = process_inode_item(eb, i, &key, active_node);
2759 case BTRFS_EXTENT_DATA_KEY:
2760 ret = process_file_extent(root, eb, i, &key,
2761 @@ -1176,13 +1219,23 @@ static int check_inode_recs(struct btrfs_root *root,
2762 node = container_of(cache, struct ptr_node, cache);
2764 remove_cache_extent(inode_cache, &node->cache);
2766 if (rec->ino == root_dirid ||
2767 rec->ino == BTRFS_ORPHAN_OBJECTID) {
2769 free_inode_rec(rec);
2773 + if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
2774 + ret = check_orphan_item(root, rec->ino);
2776 + rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
2777 + if (can_free_inode_rec(rec)) {
2778 + free_inode_rec(rec);
2784 if (!rec->found_inode_item)
2785 rec->errors |= I_ERR_NO_INODE_ITEM;
2786 @@ -1205,13 +1258,314 @@ static int check_inode_recs(struct btrfs_root *root,
2787 backref->namelen, backref->name,
2788 backref->filetype, backref->errors);
2791 free_inode_rec(rec);
2793 return (error > 0) ? -1 : 0;
2796 +static struct root_record *get_root_rec(struct cache_tree *root_cache,
2799 + struct cache_extent *cache;
2800 + struct root_record *rec = NULL;
2803 + cache = find_cache_extent(root_cache, objectid, 1);
2805 + rec = container_of(cache, struct root_record, cache);
2807 + rec = calloc(1, sizeof(*rec));
2808 + rec->objectid = objectid;
2809 + INIT_LIST_HEAD(&rec->backrefs);
2810 + rec->cache.start = objectid;
2811 + rec->cache.size = 1;
2813 + ret = insert_existing_cache_extent(root_cache, &rec->cache);
2819 +static struct root_backref *get_root_backref(struct root_record *rec,
2820 + u64 ref_root, u64 dir, u64 index,
2821 + const char *name, int namelen)
2823 + struct root_backref *backref;
2825 + list_for_each_entry(backref, &rec->backrefs, list) {
2826 + if (backref->ref_root != ref_root || backref->dir != dir ||
2827 + backref->namelen != namelen)
2829 + if (memcmp(name, backref->name, namelen))
2834 + backref = malloc(sizeof(*backref) + namelen + 1);
2835 + memset(backref, 0, sizeof(*backref));
2836 + backref->ref_root = ref_root;
2837 + backref->dir = dir;
2838 + backref->index = index;
2839 + backref->namelen = namelen;
2840 + memcpy(backref->name, name, namelen);
2841 + backref->name[namelen] = '\0';
2842 + list_add_tail(&backref->list, &rec->backrefs);
2846 +static void free_root_recs(struct cache_tree *root_cache)
2848 + struct cache_extent *cache;
2849 + struct root_record *rec;
2850 + struct root_backref *backref;
2853 + cache = find_first_cache_extent(root_cache, 0);
2856 + rec = container_of(cache, struct root_record, cache);
2857 + remove_cache_extent(root_cache, &rec->cache);
2859 + while (!list_empty(&rec->backrefs)) {
2860 + backref = list_entry(rec->backrefs.next,
2861 + struct root_backref, list);
2862 + list_del(&backref->list);
2869 +static int add_root_backref(struct cache_tree *root_cache,
2870 + u64 root_id, u64 ref_root, u64 dir, u64 index,
2871 + const char *name, int namelen,
2872 + int item_type, int errors)
2874 + struct root_record *rec;
2875 + struct root_backref *backref;
2877 + rec = get_root_rec(root_cache, root_id);
2878 + backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
2880 + backref->errors |= errors;
2882 + if (item_type != BTRFS_DIR_ITEM_KEY) {
2883 + if (backref->found_dir_index || backref->found_back_ref ||
2884 + backref->found_forward_ref) {
2885 + if (backref->index != index)
2886 + backref->errors |= REF_ERR_INDEX_UNMATCH;
2888 + backref->index = index;
2892 + if (item_type == BTRFS_DIR_ITEM_KEY) {
2893 + backref->found_dir_item = 1;
2894 + backref->reachable = 1;
2896 + } else if (item_type == BTRFS_DIR_INDEX_KEY) {
2897 + backref->found_dir_index = 1;
2898 + } else if (item_type == BTRFS_ROOT_REF_KEY) {
2899 + if (backref->found_forward_ref)
2900 + backref->errors |= REF_ERR_DUP_ROOT_REF;
2901 + backref->found_forward_ref = 1;
2902 + } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
2903 + if (backref->found_back_ref)
2904 + backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
2905 + backref->found_back_ref = 1;
2913 +static int merge_root_recs(struct btrfs_root *root,
2914 + struct cache_tree *src_cache,
2915 + struct cache_tree *dst_cache)
2917 + struct cache_extent *cache;
2918 + struct ptr_node *node;
2919 + struct inode_record *rec;
2920 + struct inode_backref *backref;
2922 + if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2923 + free_inode_recs(src_cache);
2928 + cache = find_first_cache_extent(src_cache, 0);
2931 + node = container_of(cache, struct ptr_node, cache);
2933 + remove_cache_extent(src_cache, &node->cache);
2936 + list_for_each_entry(backref, &rec->backrefs, list) {
2937 + BUG_ON(backref->found_inode_ref);
2938 + if (backref->found_dir_item)
2939 + add_root_backref(dst_cache, rec->ino,
2940 + root->root_key.objectid, backref->dir,
2941 + backref->index, backref->name,
2942 + backref->namelen, BTRFS_DIR_ITEM_KEY,
2944 + if (backref->found_dir_index)
2945 + add_root_backref(dst_cache, rec->ino,
2946 + root->root_key.objectid, backref->dir,
2947 + backref->index, backref->name,
2948 + backref->namelen, BTRFS_DIR_INDEX_KEY,
2951 + free_inode_rec(rec);
2956 +static int check_root_refs(struct btrfs_root *root,
2957 + struct cache_tree *root_cache)
2959 + struct root_record *rec;
2960 + struct root_record *ref_root;
2961 + struct root_backref *backref;
2962 + struct cache_extent *cache;
2968 + rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
2969 + rec->found_ref = 1;
2971 + /* fixme: this can not detect circular references */
2974 + cache = find_first_cache_extent(root_cache, 0);
2978 + rec = container_of(cache, struct root_record, cache);
2979 + cache = next_cache_extent(cache);
2981 + if (rec->found_ref == 0)
2984 + list_for_each_entry(backref, &rec->backrefs, list) {
2985 + if (!backref->reachable)
2988 + ref_root = get_root_rec(root_cache,
2989 + backref->ref_root);
2990 + if (ref_root->found_ref > 0)
2993 + backref->reachable = 0;
2995 + if (rec->found_ref == 0)
3001 + cache = find_first_cache_extent(root_cache, 0);
3005 + rec = container_of(cache, struct root_record, cache);
3006 + cache = next_cache_extent(cache);
3008 + if (rec->found_ref == 0 &&
3009 + rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
3010 + rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
3011 + ret = check_orphan_item(root->fs_info->tree_root,
3016 + fprintf(stderr, "fs tree %llu not referenced\n",
3017 + (unsigned long long)rec->objectid);
3021 + if (rec->found_ref > 0 && !rec->found_root_item)
3023 + list_for_each_entry(backref, &rec->backrefs, list) {
3024 + if (!backref->found_dir_item)
3025 + backref->errors |= REF_ERR_NO_DIR_ITEM;
3026 + if (!backref->found_dir_index)
3027 + backref->errors |= REF_ERR_NO_DIR_INDEX;
3028 + if (!backref->found_back_ref)
3029 + backref->errors |= REF_ERR_NO_ROOT_BACKREF;
3030 + if (!backref->found_forward_ref)
3031 + backref->errors |= REF_ERR_NO_ROOT_REF;
3032 + if (backref->reachable && backref->errors)
3039 + fprintf(stderr, "fs tree %llu refs %u %s\n",
3040 + (unsigned long long)rec->objectid, rec->found_ref,
3041 + rec->found_root_item ? "" : "not found");
3043 + list_for_each_entry(backref, &rec->backrefs, list) {
3044 + if (!backref->reachable)
3046 + if (!backref->errors && rec->found_root_item)
3048 + fprintf(stderr, "\tunresolved ref root %llu dir %llu"
3049 + " index %llu namelen %u name %s error %x\n",
3050 + (unsigned long long)backref->ref_root,
3051 + (unsigned long long)backref->dir,
3052 + (unsigned long long)backref->index,
3053 + backref->namelen, backref->name,
3057 + return errors > 0 ? 1 : 0;
3060 +static int process_root_ref(struct extent_buffer *eb, int slot,
3061 + struct btrfs_key *key,
3062 + struct cache_tree *root_cache)
3068 + struct btrfs_root_ref *ref;
3069 + char namebuf[BTRFS_NAME_LEN];
3072 + ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
3074 + dirid = btrfs_root_ref_dirid(eb, ref);
3075 + index = btrfs_root_ref_sequence(eb, ref);
3076 + name_len = btrfs_root_ref_name_len(eb, ref);
3078 + if (name_len <= BTRFS_NAME_LEN) {
3082 + len = BTRFS_NAME_LEN;
3083 + error = REF_ERR_NAME_TOO_LONG;
3085 + read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
3087 + if (key->type == BTRFS_ROOT_REF_KEY) {
3088 + add_root_backref(root_cache, key->offset, key->objectid, dirid,
3089 + index, namebuf, len, key->type, error);
3091 + add_root_backref(root_cache, key->objectid, key->offset, dirid,
3092 + index, namebuf, len, key->type, error);
3097 static int check_fs_root(struct btrfs_root *root,
3098 + struct cache_tree *root_cache,
3099 struct walk_control *wc)
3102 @@ -1219,10 +1573,18 @@ static int check_fs_root(struct btrfs_root *root,
3104 struct btrfs_path path;
3105 struct shared_node root_node;
3106 + struct root_record *rec;
3107 struct btrfs_root_item *root_item = &root->root_item;
3109 + if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
3110 + rec = get_root_rec(root_cache, root->root_key.objectid);
3111 + if (btrfs_root_refs(root_item) > 0)
3112 + rec->found_root_item = 1;
3115 btrfs_init_path(&path);
3116 memset(&root_node, 0, sizeof(root_node));
3117 + cache_tree_init(&root_node.root_cache);
3118 cache_tree_init(&root_node.inode_cache);
3120 level = btrfs_header_level(root->node);
3121 @@ -1266,6 +1628,8 @@ static int check_fs_root(struct btrfs_root *root,
3123 btrfs_release_path(root, &path);
3125 + merge_root_recs(root, &root_node.root_cache, root_cache);
3127 if (root_node.current) {
3128 root_node.current->checked = 1;
3129 maybe_free_inode_rec(&root_node.inode_cache,
3130 @@ -1280,13 +1644,15 @@ static int fs_root_objectid(u64 objectid)
3132 if (objectid == BTRFS_FS_TREE_OBJECTID ||
3133 objectid == BTRFS_TREE_RELOC_OBJECTID ||
3134 + objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
3135 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
3136 - objectid < BTRFS_LAST_FREE_OBJECTID))
3137 + objectid <= BTRFS_LAST_FREE_OBJECTID))
3142 -static int check_fs_roots(struct btrfs_root *root)
3143 +static int check_fs_roots(struct btrfs_root *root,
3144 + struct cache_tree *root_cache)
3146 struct btrfs_path path;
3147 struct btrfs_key key;
3148 @@ -1319,10 +1685,14 @@ static int check_fs_roots(struct btrfs_root *root)
3149 fs_root_objectid(key.objectid)) {
3150 tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
3152 - ret = check_fs_root(tmp_root, &wc);
3153 + ret = check_fs_root(tmp_root, root_cache, &wc);
3156 btrfs_free_fs_root(root->fs_info, tmp_root);
3157 + } else if (key.type == BTRFS_ROOT_REF_KEY ||
3158 + key.type == BTRFS_ROOT_BACKREF_KEY) {
3159 + process_root_ref(leaf, path.slots[0], &key,
3164 @@ -1895,7 +2265,6 @@ static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
3169 static int add_pending(struct cache_tree *pending,
3170 struct cache_tree *seen, u64 bytenr, u32 size)
3172 @@ -2443,6 +2812,7 @@ static void print_usage(void)
3174 int main(int ac, char **av)
3176 + struct cache_tree root_cache;
3177 struct btrfs_root *root;
3180 @@ -2450,6 +2820,7 @@ int main(int ac, char **av)
3184 + cache_tree_init(&root_cache);
3185 root = open_ctree(av[1], 0, 0);
3188 @@ -2458,10 +2829,15 @@ int main(int ac, char **av)
3189 ret = check_extents(root);
3192 - ret = check_fs_roots(root);
3193 + ret = check_fs_roots(root, &root_cache);
3197 + ret = check_root_refs(root, &root_cache);
3199 + free_root_recs(&root_cache);
3202 if (found_old_backref) {
3204 * there was a disk format change when mixed
3205 diff --git a/btrfsctl.c b/btrfsctl.c
3206 index b323818..be6bf25 100644
3213 +#include <stdlib.h>
3214 #include "kerncompat.h"
3216 #include "transaction.h"
3217 @@ -46,7 +47,7 @@ static inline int ioctl(int fd, int define, void *arg) { return 0; }
3218 static void print_usage(void)
3220 printf("usage: btrfsctl [ -d file|dir] [ -s snap_name subvol|tree ]\n");
3221 - printf(" [-r size] [-A device] [-a] [-c]\n");
3222 + printf(" [-r size] [-A device] [-a] [-c] [-D dir .]\n");
3223 printf("\t-d filename: defragments one file\n");
3224 printf("\t-d directory: defragments the entire Btree\n");
3225 printf("\t-s snap_name dir: creates a new snapshot of dir\n");
3226 @@ -55,6 +56,9 @@ static void print_usage(void)
3227 printf("\t-A device: scans the device file for a Btrfs filesystem\n");
3228 printf("\t-a: scans all devices for Btrfs filesystems\n");
3229 printf("\t-c: forces a single FS sync\n");
3230 + printf("\t-D: delete snapshot\n");
3231 + printf("\t-m [tree id] directory: set the default mounted subvolume"
3232 + " to the [tree id] or the directory\n");
3233 printf("%s\n", BTRFS_BUILD_VERSION);
3236 @@ -100,6 +104,7 @@ int main(int ac, char **av)
3237 unsigned long command = 0;
3242 if (ac == 2 && strcmp(av[1], "-a") == 0) {
3243 fprintf(stderr, "Scanning for Btrfs filesystems\n");
3244 @@ -158,6 +163,18 @@ int main(int ac, char **av)
3247 command = BTRFS_IOC_DEFRAG;
3248 + } else if (strcmp(av[i], "-D") == 0) {
3249 + if (i >= ac - 1) {
3250 + fprintf(stderr, "-D requires an arg\n");
3253 + command = BTRFS_IOC_SNAP_DESTROY;
3255 + len = strlen(name);
3256 + if (len == 0 || len >= BTRFS_VOL_NAME_MAX) {
3257 + fprintf(stderr, "-D size too long\n");
3260 } else if (strcmp(av[i], "-A") == 0) {
3262 fprintf(stderr, "-A requires an arg\n");
3263 @@ -178,6 +195,16 @@ int main(int ac, char **av)
3264 command = BTRFS_IOC_RESIZE;
3265 } else if (strcmp(av[i], "-c") == 0) {
3266 command = BTRFS_IOC_SYNC;
3267 + } else if (strcmp(av[i], "-m") == 0) {
3268 + command = BTRFS_IOC_DEFAULT_SUBVOL;
3269 + if (i == ac - 3) {
3270 + objectid = (unsigned long long)
3271 + strtoll(av[i + 1], NULL, 0);
3272 + if (errno == ERANGE) {
3273 + fprintf(stderr, "invalid tree id\n");
3280 @@ -206,6 +233,9 @@ int main(int ac, char **av)
3281 if (command == BTRFS_IOC_SNAP_CREATE) {
3283 ret = ioctl(snap_fd, command, &args);
3284 + } else if (command == BTRFS_IOC_DEFAULT_SUBVOL) {
3285 + printf("objectid is %llu\n", objectid);
3286 + ret = ioctl(fd, command, &objectid);
3288 ret = ioctl(fd, command, &args);
3290 diff --git a/convert.c b/convert.c
3291 index d2c9efa..d037c98 100644
3294 @@ -370,7 +370,6 @@ static int record_file_extent(struct btrfs_trans_handle *trans,
3295 struct btrfs_extent_item *ei;
3296 u32 blocksize = root->sectorsize;
3300 if (disk_bytenr == 0) {
3301 ret = btrfs_insert_file_extent(trans, root, objectid,
3302 @@ -432,9 +431,6 @@ static int record_file_extent(struct btrfs_trans_handle *trans,
3303 nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
3304 btrfs_set_stack_inode_nbytes(inode, nbytes);
3306 - bytes_used = btrfs_root_used(&root->root_item);
3307 - btrfs_set_root_used(&root->root_item, bytes_used + num_bytes);
3309 btrfs_release_path(root, &path);
3311 ins_key.objectid = disk_bytenr;
3312 @@ -454,9 +450,6 @@ static int record_file_extent(struct btrfs_trans_handle *trans,
3314 btrfs_mark_buffer_dirty(leaf);
3316 - bytes_used = btrfs_super_bytes_used(&info->super_copy);
3317 - btrfs_set_super_bytes_used(&info->super_copy, bytes_used +
3319 ret = btrfs_update_block_group(trans, root, disk_bytenr,
3322 diff --git a/ctree.h b/ctree.h
3323 index a9062ea..64ecf12 100644
3326 @@ -1047,6 +1047,7 @@ BTRFS_SETGET_STACK_FUNCS(block_group_flags,
3328 /* struct btrfs_inode_ref */
3329 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
3330 +BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
3331 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
3333 /* struct btrfs_inode_item */
3334 @@ -1325,6 +1326,10 @@ BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
3335 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
3336 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
3338 +BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
3339 +BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
3340 +BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
3342 /* struct btrfs_dir_item */
3343 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
3344 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
3345 @@ -1572,6 +1577,7 @@ static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
3347 /* struct btrfs_file_extent_item */
3348 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
3349 +BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item, type, 8);
3351 static inline unsigned long btrfs_file_extent_inline_start(struct
3352 btrfs_file_extent_item *e)
3353 @@ -1588,18 +1594,30 @@ static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
3355 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
3357 +BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr, struct btrfs_file_extent_item,
3359 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
3361 +BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation, struct btrfs_file_extent_item,
3363 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
3364 disk_num_bytes, 64);
3365 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
3367 +BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset, struct btrfs_file_extent_item,
3369 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
3371 +BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes, struct btrfs_file_extent_item,
3373 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
3375 +BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes, struct btrfs_file_extent_item,
3377 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
3379 +BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression, struct btrfs_file_extent_item,
3381 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
3383 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
3384 diff --git a/ioctl-test.c b/ioctl-test.c
3385 new file mode 100644
3386 index 0000000..7cf3bc2
3391 +#include <stdlib.h>
3392 +#include "kerncompat.h"
3395 +unsigned long ioctls[] = {
3396 + BTRFS_IOC_SNAP_CREATE,
3399 + BTRFS_IOC_SCAN_DEV,
3400 + BTRFS_IOC_TRANS_START,
3401 + BTRFS_IOC_TRANS_END,
3404 + BTRFS_IOC_ADD_DEV,
3406 + BTRFS_IOC_BALANCE,
3407 + BTRFS_IOC_SUBVOL_CREATE,
3408 + BTRFS_IOC_SNAP_DESTROY,
3409 + BTRFS_IOC_DEFRAG_RANGE,
3410 + BTRFS_IOC_TREE_SEARCH,
3411 + BTRFS_IOC_INO_LOOKUP,
3412 + BTRFS_IOC_DEFAULT_SUBVOL,
3413 + BTRFS_IOC_SPACE_INFO,
3416 +int main(int ac, char **av)
3419 + while(ioctls[i]) {
3420 + printf("%lu\n" ,ioctls[i]);
3426 diff --git a/ioctl.h b/ioctl.h
3427 index a084f33..776d7a9 100644
3430 @@ -30,6 +30,108 @@ struct btrfs_ioctl_vol_args {
3431 char name[BTRFS_PATH_NAME_MAX + 1];
3434 +struct btrfs_ioctl_search_key {
3435 + /* which root are we searching. 0 is the tree of tree roots */
3438 + /* keys returned will be >= min and <= max */
3439 + __u64 min_objectid;
3440 + __u64 max_objectid;
3442 + /* keys returned will be >= min and <= max */
3446 + /* max and min transids to search for */
3447 + __u64 min_transid;
3448 + __u64 max_transid;
3450 + /* keys returned will be >= min and <= max */
3455 + * how many items did userland ask for, and how many are we
3460 + /* align to 64 bits */
3463 + /* some extra for later */
3470 +struct btrfs_ioctl_search_header {
3476 +} __attribute__((may_alias));
3478 +#define BTRFS_SEARCH_ARGS_BUFSIZE (4096 - sizeof(struct btrfs_ioctl_search_key))
3480 + * the buf is an array of search headers where
3481 + * each header is followed by the actual item
3482 + * the type field is expanded to 32 bits for alignment
3484 +struct btrfs_ioctl_search_args {
3485 + struct btrfs_ioctl_search_key key;
3486 + char buf[BTRFS_SEARCH_ARGS_BUFSIZE];
3489 +#define BTRFS_INO_LOOKUP_PATH_MAX 4080
3490 +struct btrfs_ioctl_ino_lookup_args {
3493 + char name[BTRFS_INO_LOOKUP_PATH_MAX];
3496 +/* flags for the defrag range ioctl */
3497 +#define BTRFS_DEFRAG_RANGE_COMPRESS 1
3498 +#define BTRFS_DEFRAG_RANGE_START_IO 2
3500 +struct btrfs_ioctl_defrag_range_args {
3501 + /* start of the defrag operation */
3504 + /* number of bytes to defrag, use (u64)-1 to say all */
3508 + * flags for the operation, which can include turning
3509 + * on compression for this one defrag
3514 + * any extent bigger than this will be considered
3515 + * already defragged. Use 0 to take the kernel default
3516 + * Use 1 to say every single extent must be rewritten
3518 + __u32 extent_thresh;
3520 + /* spare for later */
3524 +struct btrfs_ioctl_space_info {
3526 + __u64 total_bytes;
3530 +struct btrfs_ioctl_space_args {
3531 + __u64 space_slots;
3532 + __u64 total_spaces;
3533 + struct btrfs_ioctl_space_info spaces[0];
3536 #define BTRFS_IOC_SNAP_CREATE _IOW(BTRFS_IOCTL_MAGIC, 1, \
3537 struct btrfs_ioctl_vol_args)
3538 #define BTRFS_IOC_DEFRAG _IOW(BTRFS_IOCTL_MAGIC, 2, \
3539 @@ -56,4 +158,15 @@ struct btrfs_ioctl_vol_args {
3540 /* 13 is for CLONE_RANGE */
3541 #define BTRFS_IOC_SUBVOL_CREATE _IOW(BTRFS_IOCTL_MAGIC, 14, \
3542 struct btrfs_ioctl_vol_args)
3543 +#define BTRFS_IOC_SNAP_DESTROY _IOW(BTRFS_IOCTL_MAGIC, 15, \
3544 + struct btrfs_ioctl_vol_args)
3545 +#define BTRFS_IOC_DEFRAG_RANGE _IOW(BTRFS_IOCTL_MAGIC, 16, \
3546 + struct btrfs_ioctl_defrag_range_args)
3547 +#define BTRFS_IOC_TREE_SEARCH _IOWR(BTRFS_IOCTL_MAGIC, 17, \
3548 + struct btrfs_ioctl_search_args)
3549 +#define BTRFS_IOC_INO_LOOKUP _IOWR(BTRFS_IOCTL_MAGIC, 18, \
3550 + struct btrfs_ioctl_ino_lookup_args)
3551 +#define BTRFS_IOC_DEFAULT_SUBVOL _IOW(BTRFS_IOCTL_MAGIC, 19, u64)
3552 +#define BTRFS_IOC_SPACE_INFO _IOWR(BTRFS_IOCTL_MAGIC, 20, \
3553 + struct btrfs_ioctl_space_args)
3555 diff --git a/man/Makefile b/man/Makefile
3556 index 4e8893b..4a90b75 100644
3559 @@ -7,13 +7,16 @@ mandir = $(prefix)/man
3560 man8dir = $(mandir)/man8
3562 MANPAGES = mkfs.btrfs.8.gz btrfsctl.8.gz btrfsck.8.gz btrfs-image.8.gz \
3564 + btrfs-show.8.gz btrfs.8.gz
3568 mkfs.btrfs.8.gz: mkfs.btrfs.8.in
3569 $(GZIP) -n -c mkfs.btrfs.8.in > mkfs.btrfs.8.gz
3571 +btrfs.8.gz: btrfs.8.in
3572 + $(GZIP) -n -c btrfs.8.in > btrfs.8.gz
3574 btrfsctl.8.gz: btrfsctl.8.in
3575 $(GZIP) -n -c btrfsctl.8.in > btrfsctl.8.gz
3577 diff --git a/man/btrfs.8.in b/man/btrfs.8.in
3578 new file mode 100644
3579 index 0000000..26ef982
3581 +++ b/man/btrfs.8.in
3583 +.TH BTRFS 8 "" "btrfs" "btrfs"
3585 +.\" Man page written by Goffredo Baroncelli <kreijack@inwind.it> (Feb 2010)
3588 +btrfs \- control a btrfs filesystem
3590 +\fBbtrfs\fP \fBsubvolume snapshot\fP\fI <source> [<dest>/]<name>\fP
3592 +\fBbtrfs\fP \fBsubvolume delete\fP\fI <subvolume>\fP
3594 +\fBbtrfs\fP \fBsubvolume create\fP\fI [<dest>/]<name>\fP
3596 +\fBbtrfs\fP \fBsubvolume list\fP\fI <path>\fP
3598 +\fBbtrfs\fP \fBsubvolume set-default\fP\fI <id> <path>\fP
3600 +\fBbtrfs\fP \fBfilesystem defrag\fP\fI <file>|<dir> [<file>|<dir>...]\fP
3602 +\fBbtrfs\fP \fBfilesystem sync\fP\fI <path> \fP
3604 +\fBbtrfs\fP \fBfilesystem resize\fP\fI [+/\-]<size>[gkm]|max <filesystem>\fP
3606 +\fBbtrfs\fP \fBdevice scan\fP\fI [<device> [<device>..]]\fP
3608 +\fBbtrfs\fP \fBdevice show\fP\fI <dev>|<label> [<dev>|<label>...]\fP
3610 +\fBbtrfs\fP \fBdevice balance\fP\fI <path> \fP
3612 +\fBbtrfs\fP \fBdevice add\fP\fI <dev> [<dev>..] <path> \fP
3614 +\fBbtrfs\fP \fBdevice delete\fP\fI <dev> [<dev>..] <path> \fP]
3617 +\fBbtrfs\fP \fBhelp|\-\-help|\-h \fP\fI\fP
3621 +is used to control the filesystem and the files and directories stored. It is
3622 +the tool to create or destroy a snapshot or a subvolume for the
3623 +filesystem, to defrag a file or a directory, flush the data to the disk,
3624 +to resize the filesystem, to scan the device.
3626 +It is possible to abbreviate the commands unless the commands are ambiguous.
3627 +For example: it is possible to run
3630 +.I btrfs subvolume snapshot.
3633 +is not allowed, because
3635 +may be interpreted both as
3643 +If a command is terminated by
3645 +, the relevant help is showed. If the passed command matches more commands,
3646 +the help of all the matched commands are showed. For example
3647 +.I btrfs dev --help
3648 +shows the help of all
3655 +\fBsubvolume snapshot\fR\fI <source> [<dest>/]<name>\fR
3656 +Create a writable snapshot of the subvolume \fI<source>\fR with the name
3657 +\fI<name>\fR in the \fI<dest>\fR directory. If \fI<source>\fR is not a
3658 +subvolume, \fBbtrfs\fR returns an error.
3661 +\fBsubvolume delete\fR\fI <subvolume>\fR
3662 +Delete the subvolume \fI<subvolume>\fR. If \fI<subvolume>\fR is not a
3663 +subvolume, \fBbtrfs\fR returns an error.
3666 +\fBsubvolume create\fR\fI [<dest>/]<name>\fR
3667 +Create a subvolume in \fI<dest>\fR (or in the current directory if
3668 +\fI<dest>\fR is omitted).
3671 +\fBsubvolume list\fR\fI <path>\fR
3672 +List the subvolumes present in the filesystem \fI<path>\fR. For every
3673 +subvolume is showed the subvolume ID (second column),
3674 +the ID of the \fItop level\fR
3675 +subvolume (fifth column), and the path (seventh column) relative to the
3676 +\fItop level\fR subvolume.
3677 +These <ID> may be used by the \fBsubvolume set-default\fR command, or at
3678 +mount time via the \fIsubvol=\fR option.
3681 +\fBsubvolume set-default\fR\fI <id> <path>\fR
3682 +Set the subvolume of the filesystem \fI<path>\fR which is mounted as
3683 +\fIdefault\fR. The subvolume is identified by \fB<id>\fR, which
3684 +is returned by the \fBsubvolume list\fR command.
3687 +\fBfilesystem defragment\fP\fI <file>|<dir> [<file>|<dir>...]\fR
3688 +Defragment files and/or directories.
3691 +\fBdevice scan\fR \fI[<device> [<device>..]]\fR
3692 +Scan devices for a btrfs filesystem. If no devices are passed, \fBbtrfs\fR scans
3693 +all the block devices.
3696 +\fBfilesystem sync\fR\fI <path> \fR
3697 +Force a sync for the filesystem identified by \fI<path>\fR.
3701 +.\" Some wording are extracted by the resize2fs man page
3704 +\fBfilesystem resize\fR\fI [+/\-]<size>[gkm]|max <path>\fR
3705 +Resize a filesystem identified by \fI<path>\fR.
3706 +The \fI<size>\fR parameter specifies the new size of the filesystem.
3707 +If the prefix \fI+\fR or \fI\-\fR is present the size is increased or decreased
3708 +by the quantity \fI<size>\fR.
3709 +If no units are specified, the unit of the \fI<size>\fR parameter defaults to
3710 +bytes. Optionally, the size parameter may be suffixed by one of the following
3711 +the units designators: 'K', 'M', or 'G', kilobytes, megabytes, or gigabytes,
3714 +If 'max' is passed, the filesystem will occupy all available space on the
3717 +The \fBresize\fR command \fBdoes not\fR manipulate the size of underlying
3718 +partition. If you wish to enlarge/reduce a filesystem, you must make sure you
3719 +can expand the partition before enlarging the filesystem and shrink the
3720 +partition after reducing the size of the filesystem.
3723 +\fBfilesystem show\fR [<uuid>|<label>]\fR
3724 +Show the btrfs filesystem with some additional info. If no UUID or label is
3725 +passed, \fBbtrfs\fR show info of all the btrfs filesystem.
3728 +\fBdevice balance\fR \fI<path>\fR
3729 +Balance the chunks of the filesystem identified by \fI<path>\fR
3730 +across the devices.
3733 +\fBdevice add\fR\fI <dev> [<dev>..] <path>\fR
3734 +Add device(s) to the filesystem identified by \fI<path>\fR.
3737 +\fBdevice delete\fR\fI <dev> [<dev>..] <path>\fR
3738 +Remove device(s) from a filesystem identified by \fI<path>\fR.
3742 +\fBbtrfs\fR returns a zero exist status if it succeeds. Non zero is returned in
3747 +is part of btrfs-progs. Btrfs filesystem is currently under heavy development,
3748 +and not suitable for any uses other than benchmarking and review.
3749 +Please refer to the btrfs wiki http://btrfs.wiki.kernel.org for