Skip to contentSkip to conversation

btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices

PATCH Applied Zhao Lei 2 participants 7 messages Oct 26, 2015 Btrfs file system

lore
Share & tools
lore

Zhao Lei

author
#0 lore raw

v2 cover btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices

This patch add all missing close_ctree and btrfs_close_all_devices to several tools in btrfs progs, to avoid memory leak.

Changelog v1->v2:
Move btrfs_close_all_devices() from cmd-XXX into btrfs.c to make code simple, and avoid similar problem in cmd-XXX in future.

Zhao Lei (5):
btrfs-progs: btrfs: Add missing btrfs_close_all_devices for btrfs
command
btrfs-progs: Remove all btrfs_close_all_devices in sub-command
btrfs-progs: Add all missing btrfs_close_all_devices to standalone
tools
btrfs-progs: Add missing close_ctree to btrfs-select-super.c
btrfs-progs: use system's default path for math.h

 btrfs-calc-size.c    | 1 +
 btrfs-debug-tree.c   | 5 ++++-
 btrfs-find-root.c    | 1 +
 btrfs-map-logical.c  | 1 +
 btrfs-select-super.c | 3 +++
 btrfs.c              | 9 ++++++++-
 btrfstune.c          | 1 +
 cmds-check.c         | 1 -
 cmds-device.c        | 3 ---
 cmds-replace.c       | 2 --
 extent-tree.c        | 2 +-
 11 files changed, 20 insertions(+), 9 deletions(-)
Signature · 1 line
1.8.5.1
author
#1 lore raw

PATCH v2 1/5 btrfs-progs: btrfs: Add missing btrfs_close_all_devices for btrfs command

Adding a btrfs_close_all_devices() after command callback in btrfs.c can force-close all opened device before program exit, to avoid memory leak in all btrfs sub-command.

---
 btrfs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
btrfs.c +8 -1
index 63df377..9416a29 100644
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "volumes.h"
 #include "crc32c.h"
 #include "commands.h"
 #include "utils.h"
@@ -214,6 +215,7 @@ int main(int argc, char **argv)
 {
 	const struct cmd_struct *cmd;
 	const char *bname;
+	int ret;
 
 	if ((bname = strrchr(argv[0], '/')) != NULL)
 		bname++;
@@ -242,5 +244,10 @@ int main(int argc, char **argv)
 	crc32c_optimization_init();
 
 	fixup_argv0(argv, cmd->token);
-	exit(cmd->fn(argc, argv));
+
+	ret = cmd->fn(argc, argv);
+
+	btrfs_close_all_devices();
+
+	exit(ret);
 }
Signature · 1 line
1.8.5.1
author
#2 lore raw

PATCH v2 2/5 btrfs-progs: Remove all btrfs_close_all_devices in sub-command

In reply to Zhao Lei

Since we have btrfs_close_all_devices() in btrfs's main entrance, it is not necessary to call btrfs_close_all_devices() separately in each sub-command.

---
 cmds-check.c   | 1 -
 cmds-device.c  | 3 ---
 cmds-replace.c | 2 --
 3 files changed, 6 deletions(-)
cmds-check.c +0 -1
index 1f8caad..3af6e61 100644
@@ -9738,7 +9738,6 @@ out:
 	free_root_recs_tree(&root_cache);
 close_out:
 	close_ctree(root);
-	btrfs_close_all_devices();
 err_out:
 	if (ctx.progress_enabled)
 		task_deinit(ctx.info);
cmds-device.c +0 -3
index 5f2b952..620ae8b 100644
@@ -139,7 +139,6 @@ static int cmd_device_add(int argc, char **argv)
 
 error_out:
 	close_file_or_dir(fdmnt, dirstream);
-	btrfs_close_all_devices();
 	return !!ret;
 }
 
@@ -288,7 +287,6 @@ static int cmd_device_scan(int argc, char **argv)
 	}
 
 out:
-	btrfs_close_all_devices();
 	return !!ret;
 }
 
@@ -466,7 +464,6 @@ static int cmd_device_stats(int argc, char **argv)
 out:
 	free(di_args);
 	close_file_or_dir(fdmnt, dirstream);
-	btrfs_close_all_devices();
 
 	return err;
 }
cmds-replace.c +0 -2
index 9ab8438..fadd2cd 100644
@@ -330,7 +330,6 @@ static int cmd_replace_start(int argc, char **argv)
 		}
 	}
 	close_file_or_dir(fdmnt, dirstream);
-	btrfs_close_all_devices();
 	return 0;
 
 leave_with_error:
@@ -340,7 +339,6 @@ leave_with_error:
 		close(fdmnt);
 	if (fddstdev != -1)
 		close(fddstdev);
-	btrfs_close_all_devices();
 	return 1;
 }
 
Signature · 1 line
1.8.5.1
author
#3 lore raw

PATCH v2 3/5 btrfs-progs: Add all missing btrfs_close_all_devices to standalone tools

In reply to Zhao Lei

This patch add all missing btrfs_close_all_devices() to standalone tools in btrfs progs, to avoid memory leak.

---
 btrfs-calc-size.c    | 1 +
 btrfs-debug-tree.c   | 5 ++++-
 btrfs-find-root.c    | 1 +
 btrfs-map-logical.c  | 1 +
 btrfs-select-super.c | 2 ++
 btrfstune.c          | 1 +
 6 files changed, 10 insertions(+), 1 deletion(-)
btrfs-calc-size.c +1 -0
index 7287858..b756693 100644
@@ -508,5 +508,6 @@ int main(int argc, char **argv)
 out:
 	close_ctree(root);
 	free(roots);
+	btrfs_close_all_devices();
 	return ret;
 }
btrfs-debug-tree.c +4 -1
index 7d8e876..8adc39f 100644
@@ -28,6 +28,7 @@
 #include "disk-io.h"
 #include "print-tree.h"
 #include "transaction.h"
+#include "volumes.h"
 #include "utils.h"
 
 static int print_usage(int ret)
@@ -428,5 +429,7 @@ no_node:
 	printf("uuid %s\n", uuidbuf);
 	printf("%s\n", PACKAGE_STRING);
 close_root:
-	return close_ctree(root);
+	ret = close_ctree(root);
+	btrfs_close_all_devices();
+	return ret;
 }
btrfs-find-root.c +1 -0
index 01b3603..fc3812c 100644
@@ -216,5 +216,6 @@ int main(int argc, char **argv)
 out:
 	btrfs_find_root_free(&result);
 	close_ctree(root);
+	btrfs_close_all_devices();
 	return ret;
 }
btrfs-map-logical.c +1 -0
index d9fa6b2..0161b5c 100644
@@ -359,5 +359,6 @@ close:
 	close_ctree(root);
 	if (ret < 0)
 		ret = 1;
+	btrfs_close_all_devices();
 	return ret;
 }
btrfs-select-super.c +2 -0
index b790f3e..bd44978 100644
@@ -23,6 +23,7 @@
 #include <sys/stat.h>
 #include "kerncompat.h"
 #include "ctree.h"
+#include "volumes.h"
 #include "disk-io.h"
 #include "print-tree.h"
 #include "transaction.h"
@@ -101,5 +102,6 @@ int main(int ac, char **av)
 	 */
 	printf("using SB copy %llu, bytenr %llu\n", (unsigned long long)num,
 	       (unsigned long long)bytenr);
+	btrfs_close_all_devices();
 	return ret;
 }
btrfstune.c +1 -0
index c248ee6..0907aa9 100644
@@ -548,6 +548,7 @@ int main(int argc, char *argv[])
 	}
 out:
 	close_ctree(root);
+	btrfs_close_all_devices();
 
 	return ret;
 }
Signature · 1 line
1.8.5.1
author
#4 lore raw

PATCH v2 4/5 btrfs-progs: Add missing close_ctree to btrfs-select-super.c

In reply to Zhao Lei

Add missing close_ctree() to btrfs-select-super.c to avoid memory leak.

---
 btrfs-select-super.c | 1 +
 1 file changed, 1 insertion(+)
btrfs-select-super.c +1 -0
index bd44978..df74153 100644
@@ -102,6 +102,7 @@ int main(int ac, char **av)
 	 */
 	printf("using SB copy %llu, bytenr %llu\n", (unsigned long long)num,
 	       (unsigned long long)bytenr);
+	close_ctree(root);
 	btrfs_close_all_devices();
 	return ret;
 }
Signature · 1 line
1.8.5.1
author
#5 lore raw

PATCH v2 5/5 btrfs-progs: use system's default path for math.h

In reply to Zhao Lei

Line of
#include "math.h"
in extent-tree.c using quotas is history reason,
(we have cuseom math.h in source before)

Now it is better to use "<>" instead of quotas for this header file.

---
 extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
extent-tree.c +1 -1
index 0c8152a..0d605e1 100644
@@ -19,6 +19,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <math.h>
 #include "kerncompat.h"
 #include "radix-tree.h"
 #include "ctree.h"
@@ -28,7 +29,6 @@
 #include "crc32c.h"
 #include "volumes.h"
 #include "free-space-cache.h"
-#include "math.h"
 #include "utils.h"
 
 #define PENDING_EXTENT_INSERT 0
Signature · 1 line
1.8.5.1
Applied
#6 lore raw
In reply to Zhao Lei
Quoted 7 lines · Zhao Lei

This patch add all missing close_ctree and btrfs_close_all_devices to several tools in btrfs progs, to avoid memory leak.

Changelog v1->v2:
Move btrfs_close_all_devices() from cmd-XXX into btrfs.c to make code simple, and avoid similar problem in cmd-XXX in future.

Quoted 8 lines

Zhao Lei (5):
btrfs-progs: btrfs: Add missing btrfs_close_all_devices for btrfs
command
btrfs-progs: Remove all btrfs_close_all_devices in sub-command
btrfs-progs: Add all missing btrfs_close_all_devices to standalone
tools
btrfs-progs: Add missing close_ctree to btrfs-select-super.c
btrfs-progs: use system's default path for math.h

Applied, thanks.