Skip to content

Commit

Permalink
btrfs-progs: debug for a memory leak.
Browse files Browse the repository at this point in the history
Signed-off-by: Qu Wenruo <[email protected]>
  • Loading branch information
adam900710 committed Jul 29, 2024
1 parent 1535049 commit 480e8c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
58 changes: 31 additions & 27 deletions cmds/qgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,32 @@ static struct btrfs_qgroup *qgroup_tree_search(struct qgroup_lookup *root_tree,
return NULL;
}

static void __free_btrfs_qgroup(struct btrfs_qgroup *bq)
{
struct btrfs_qgroup_list *list;
while (!list_empty(&bq->qgroups)) {
list = list_entry((&bq->qgroups)->next,
struct btrfs_qgroup_list,
next_qgroup);
list_del(&list->next_qgroup);
list_del(&list->next_member);
free(list);
}
while (!list_empty(&bq->members)) {
list = list_entry((&bq->members)->next,
struct btrfs_qgroup_list,
next_member);
list_del(&list->next_qgroup);
list_del(&list->next_member);
free(list);
}
if (bq->path) {
printf("freeing path for qgroup %llu\n", bq->qgroupid);
free((void *)bq->path);
}
free(bq);
}

/*
* Lookup or insert btrfs_qgroup into qgroup_lookup.
*
Expand Down Expand Up @@ -835,15 +861,17 @@ static struct btrfs_qgroup *get_or_add_qgroup(int fd,
char *path;

uret = btrfs_util_subvolume_path_fd(fd, qgroupid, &path);
if (uret == BTRFS_UTIL_OK)
if (uret == BTRFS_UTIL_OK) {
printf("got path resolved for qgroup %llu\n", qgroupid);
bq->path = path;
else if (uret != BTRFS_UTIL_ERROR_SUBVOLUME_NOT_FOUND) {
} else if (uret != BTRFS_UTIL_ERROR_SUBVOLUME_NOT_FOUND) {
error("%s", btrfs_util_strerror(uret));
if (uret == BTRFS_UTIL_ERROR_NO_MEMORY)
return ERR_PTR(-ENOMEM);
else
return ERR_PTR(-EIO);
}
printf("no path found for qgroup %llu\n", qgroupid);
/*
* Do a correct stale detection by searching for the ROOT_ITEM of
* the subvolume.
Expand All @@ -868,7 +896,7 @@ static struct btrfs_qgroup *get_or_add_qgroup(int fd,
if (ret) {
errno = -ret;
error("failed to insert %llu into tree: %m", bq->qgroupid);
free(bq);
__free_btrfs_qgroup(bq);
return ERR_PTR(ret);
}

Expand Down Expand Up @@ -950,30 +978,6 @@ static int update_qgroup_relation(struct qgroup_lookup *qgroup_lookup,
return 0;
}

static void __free_btrfs_qgroup(struct btrfs_qgroup *bq)
{
struct btrfs_qgroup_list *list;
while (!list_empty(&bq->qgroups)) {
list = list_entry((&bq->qgroups)->next,
struct btrfs_qgroup_list,
next_qgroup);
list_del(&list->next_qgroup);
list_del(&list->next_member);
free(list);
}
while (!list_empty(&bq->members)) {
list = list_entry((&bq->members)->next,
struct btrfs_qgroup_list,
next_member);
list_del(&list->next_qgroup);
list_del(&list->next_member);
free(list);
}
if (bq->path)
free((void *)bq->path);
free(bq);
}

static void __free_all_qgroups(struct qgroup_lookup *root_tree)
{
struct btrfs_qgroup *entry;
Expand Down
3 changes: 3 additions & 0 deletions libbtrfsutil/subvolume.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "stubs.h"

#include "btrfsutil_internal.h"
Expand Down Expand Up @@ -178,6 +179,8 @@ PUBLIC enum btrfs_util_error btrfs_util_subvolume_path_fd(int fd, uint64_t id,
path = malloc(capacity);
if (!path)
return BTRFS_UTIL_ERROR_NO_MEMORY;

printf("allocated memory for qgroup %lu\n", id);
p = path + capacity - 1;
p[0] = '\0';

Expand Down

0 comments on commit 480e8c9

Please sign in to comment.