btrfs-progs: fsck-tests: Introduce test case with keyed data backref with the extent...
[platform/upstream/btrfs-progs.git] / btrfs-fragments.c
index cedbc57..46c78d2 100644 (file)
@@ -14,7 +14,6 @@
  * Boston, MA 021110-1307, USA.
  */
 
-#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -191,7 +190,6 @@ list_fragments(int fd, u64 flags, char *dir)
        u64 bgused = 0;
        u64 saved_extent = 0;
        u64 saved_len = 0;
-       u64 saved_flags = 0;
        int saved_color = 0;
        u64 last_end = 0;
        u64 areas = 0;
@@ -202,7 +200,6 @@ list_fragments(int fd, u64 flags, char *dir)
 
        gdImagePtr im = NULL;
        int black = 0;
-       int white = 0;
        int width = 800;
 
        snprintf(name, sizeof(name), "%s/index.html", dir);
@@ -221,7 +218,7 @@ list_fragments(int fd, u64 flags, char *dir)
        
        memset(&args, 0, sizeof(args));
 
-       sk->tree_id = 2;
+       sk->tree_id = BTRFS_EXTENT_TREE_OBJECTID;
        sk->max_type = -1;
        sk->min_type = 0;
        sk->max_objectid = (u64)-1;
@@ -235,7 +232,7 @@ list_fragments(int fd, u64 flags, char *dir)
                ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
                if (ret < 0) {
                        fprintf(stderr, "ERROR: can't perform the search\n");
-                       return ret;
+                       goto out_close;
                }
                /* the ioctl returns the number of item it found in nr_items */
                if (sk->nr_items == 0)
@@ -248,7 +245,8 @@ list_fragments(int fd, u64 flags, char *dir)
                        sh = (struct btrfs_ioctl_search_header *)(args.buf +
                                                                  off);
                        off += sizeof(*sh);
-                       if (sh->type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
+                       if (btrfs_search_header_type(sh)
+                           == BTRFS_BLOCK_GROUP_ITEM_KEY) {
                                struct btrfs_block_group_item *bg;
 
                                if (im) {
@@ -265,30 +263,33 @@ list_fragments(int fd, u64 flags, char *dir)
                                                (args.buf + off);
                                bgflags = btrfs_block_group_flags(bg);
                                bgused = btrfs_block_group_used(bg);
-                               
+
                                printf("found block group %lld len %lld "
-                                       "flags %lld\n", sh->objectid,
-                                       sh->offset, bgflags);
+                                       "flags %lld\n",
+                                       btrfs_search_header_objectid(sh),
+                                       btrfs_search_header_offset(sh),
+                                       bgflags);
                                if (!(bgflags & flags)) {
                                        /* skip this block group */
-                                       sk->min_objectid = sh->objectid +
-                                                          sh->offset;
+                                       sk->min_objectid =
+                                           btrfs_search_header_objectid(sh) +
+                                           btrfs_search_header_offset(sh);
                                        sk->min_type = 0;
                                        sk->min_offset = 0;
                                        break;
                                }
                                im = gdImageCreate(width,
-                                       (sh->offset / 4096 + 799) / width);
+                                       (btrfs_search_header_offset(sh)
+                                        / 4096 + 799) / width);
 
-                               white = gdImageColorAllocate(im, 255, 255, 255);
                                black = gdImageColorAllocate(im, 0, 0, 0);  
 
                                for (j = 0; j < ARRAY_SIZE(colors); ++j)
                                        colors[j] = black;
 
                                init_colors(im, colors);
-                               bgstart = sh->objectid;
-                               bglen = sh->offset;
+                               bgstart = btrfs_search_header_objectid(sh);
+                               bglen = btrfs_search_header_offset(sh);
                                bgend = bgstart + bglen;
 
                                snprintf(name, sizeof(name), "bg%d.png", bgnum);
@@ -307,51 +308,57 @@ list_fragments(int fd, u64 flags, char *dir)
                                areas = 0;
                                saved_len = 0;
                        }
-                       if (im && sh->type == BTRFS_EXTENT_ITEM_KEY) {
-                               u64 e_flags;
+                       if (im && btrfs_search_header_type(sh)
+                                       == BTRFS_EXTENT_ITEM_KEY) {
                                int c;
                                struct btrfs_extent_item *item;
 
                                item = (struct btrfs_extent_item *)
                                                (args.buf + off);
-                               e_flags = btrfs_stack_extent_flags(item);
 
                                if (use_color)
-                                       c = colors[get_color(item, sh->len)];
+                                       c = colors[get_color(item,
+                                               btrfs_search_header_len(sh))];
                                else
                                        c = black;
-                               if (sh->objectid > bgend) {
+                               if (btrfs_search_header_objectid(sh) > bgend) {
                                        printf("WARN: extent %lld is without "
-                                               "block group\n", sh->objectid);
+                                               "block group\n",
+                                               btrfs_search_header_objectid(sh));
                                        goto skip;
                                }
-                               if (sh->objectid == bgend) {
-                                       saved_extent = sh->objectid;
-                                       saved_len = sh->offset;
-                                       saved_flags = e_flags;
+                               if (btrfs_search_header_objectid(sh) == bgend) {
+                                       saved_extent =
+                                               btrfs_search_header_objectid(sh);
+                                       saved_len =
+                                               btrfs_search_header_offset(sh);
                                        saved_color = c;
                                        goto skip;
                                }
-                               px = (sh->objectid - bgstart) / 4096;
-                               for (j = 0; j < sh->offset / 4096; ++j) {
+                               px = (btrfs_search_header_objectid(sh)
+                                       - bgstart) / 4096;
+                               for (j = 0;
+                                    j < btrfs_search_header_offset(sh) / 4096;
+                                    ++j) {
                                        int x = (px + j) % width;
                                        int y = (px + j) / width;
                                        gdImageSetPixel(im, x, y, c);
                                }
-                               if (sh->objectid != last_end)
+                               if (btrfs_search_header_objectid(sh) != last_end)
                                        ++areas;
-                               last_end = sh->objectid + sh->offset;
+                               last_end = btrfs_search_header_objectid(sh)
+                                       + btrfs_search_header_offset(sh);
 skip:;
                        }
-                       off += sh->len;
+                       off += btrfs_search_header_len(sh);
 
                        /*
                         * record the mins in sk so we can make sure the
                         * next search doesn't repeat this root
                         */
-                       sk->min_objectid = sh->objectid;
-                       sk->min_type = sh->type;
-                       sk->min_offset = sh->offset;
+                       sk->min_objectid = btrfs_search_header_objectid(sh);
+                       sk->min_type = btrfs_search_header_type(sh);
+                       sk->min_offset = btrfs_search_header_offset(sh);
                }
                sk->nr_items = 4096;
 
@@ -379,12 +386,14 @@ skip:;
                fprintf(html, "</p>");
        }
        fprintf(html, "</body></html>\n");
-       
+
+out_close:
+       fclose(html);
+
        return ret;
 }
 
-void
-usage(void)
+void fragments_usage(void)
 {
        printf("usage: btrfs-fragments [options] <path>\n");
        printf("         -c               use color\n");
@@ -427,22 +436,19 @@ int main(int argc, char **argv)
                        break;
                case 'h':
                default:
-                       usage();
+                       fragments_usage();
                }
        }
 
-       if (optind < argc) {
-               path = argv[optind++];
-       } else {
-               usage();
-               exit(1);
-       }
+       set_argv0(argv);
+       if (check_argc_min(argc - optind, 1))
+               fragments_usage();
 
-       fd = open_file_or_dir(path, &dirstream);
-       if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", path);
+       path = argv[optind++];
+
+       fd = btrfs_open_dir(path, &dirstream, 1);
+       if (fd < 0)
                exit(1);
-       }
 
        if (flags == 0)
                flags = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;