linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices
@ 2015-10-26 10:28 Zhao Lei
  2015-10-26 10:28 ` [PATCH v2 1/5] btrfs-progs: btrfs: Add missing btrfs_close_all_devices for btrfs command Zhao Lei
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Zhao Lei @ 2015-10-26 10:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: 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.

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(-)

-- 
1.8.5.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/5] btrfs-progs: btrfs: Add missing btrfs_close_all_devices for btrfs command
  2015-10-26 10:28 [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices Zhao Lei
@ 2015-10-26 10:28 ` Zhao Lei
  2015-10-26 10:28 ` [PATCH v2 2/5] btrfs-progs: Remove all btrfs_close_all_devices in sub-command Zhao Lei
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-10-26 10:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

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.

Suggested-by: David Sterba <dsterba@suse.cz>
Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 btrfs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/btrfs.c b/btrfs.c
index 63df377..9416a29 100644
--- a/btrfs.c
+++ b/btrfs.c
@@ -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);
 }
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 2/5] btrfs-progs: Remove all btrfs_close_all_devices in sub-command
  2015-10-26 10:28 [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices Zhao Lei
  2015-10-26 10:28 ` [PATCH v2 1/5] btrfs-progs: btrfs: Add missing btrfs_close_all_devices for btrfs command Zhao Lei
@ 2015-10-26 10:28 ` Zhao Lei
  2015-10-26 10:28 ` [PATCH v2 3/5] btrfs-progs: Add all missing btrfs_close_all_devices to standalone tools Zhao Lei
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-10-26 10:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: 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.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 cmds-check.c   | 1 -
 cmds-device.c  | 3 ---
 cmds-replace.c | 2 --
 3 files changed, 6 deletions(-)

diff --git a/cmds-check.c b/cmds-check.c
index 1f8caad..3af6e61 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -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);
diff --git a/cmds-device.c b/cmds-device.c
index 5f2b952..620ae8b 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -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;
 }
diff --git a/cmds-replace.c b/cmds-replace.c
index 9ab8438..fadd2cd 100644
--- a/cmds-replace.c
+++ b/cmds-replace.c
@@ -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;
 }
 
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 3/5] btrfs-progs: Add all missing btrfs_close_all_devices to standalone tools
  2015-10-26 10:28 [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices Zhao Lei
  2015-10-26 10:28 ` [PATCH v2 1/5] btrfs-progs: btrfs: Add missing btrfs_close_all_devices for btrfs command Zhao Lei
  2015-10-26 10:28 ` [PATCH v2 2/5] btrfs-progs: Remove all btrfs_close_all_devices in sub-command Zhao Lei
@ 2015-10-26 10:28 ` Zhao Lei
  2015-10-26 10:28 ` [PATCH v2 4/5] btrfs-progs: Add missing close_ctree to btrfs-select-super.c Zhao Lei
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-10-26 10:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

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

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 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(-)

diff --git a/btrfs-calc-size.c b/btrfs-calc-size.c
index 7287858..b756693 100644
--- a/btrfs-calc-size.c
+++ b/btrfs-calc-size.c
@@ -508,5 +508,6 @@ int main(int argc, char **argv)
 out:
 	close_ctree(root);
 	free(roots);
+	btrfs_close_all_devices();
 	return ret;
 }
diff --git a/btrfs-debug-tree.c b/btrfs-debug-tree.c
index 7d8e876..8adc39f 100644
--- a/btrfs-debug-tree.c
+++ b/btrfs-debug-tree.c
@@ -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;
 }
diff --git a/btrfs-find-root.c b/btrfs-find-root.c
index 01b3603..fc3812c 100644
--- a/btrfs-find-root.c
+++ b/btrfs-find-root.c
@@ -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;
 }
diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c
index d9fa6b2..0161b5c 100644
--- a/btrfs-map-logical.c
+++ b/btrfs-map-logical.c
@@ -359,5 +359,6 @@ close:
 	close_ctree(root);
 	if (ret < 0)
 		ret = 1;
+	btrfs_close_all_devices();
 	return ret;
 }
diff --git a/btrfs-select-super.c b/btrfs-select-super.c
index b790f3e..bd44978 100644
--- a/btrfs-select-super.c
+++ b/btrfs-select-super.c
@@ -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;
 }
diff --git a/btrfstune.c b/btrfstune.c
index c248ee6..0907aa9 100644
--- a/btrfstune.c
+++ b/btrfstune.c
@@ -548,6 +548,7 @@ int main(int argc, char *argv[])
 	}
 out:
 	close_ctree(root);
+	btrfs_close_all_devices();
 
 	return ret;
 }
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 4/5] btrfs-progs: Add missing close_ctree to btrfs-select-super.c
  2015-10-26 10:28 [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices Zhao Lei
                   ` (2 preceding siblings ...)
  2015-10-26 10:28 ` [PATCH v2 3/5] btrfs-progs: Add all missing btrfs_close_all_devices to standalone tools Zhao Lei
@ 2015-10-26 10:28 ` Zhao Lei
  2015-10-26 10:28 ` [PATCH v2 5/5] btrfs-progs: use system's default path for math.h Zhao Lei
  2015-10-26 14:57 ` [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices David Sterba
  5 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-10-26 10:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

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

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 btrfs-select-super.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/btrfs-select-super.c b/btrfs-select-super.c
index bd44978..df74153 100644
--- a/btrfs-select-super.c
+++ b/btrfs-select-super.c
@@ -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;
 }
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v2 5/5] btrfs-progs: use system's default path for math.h
  2015-10-26 10:28 [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices Zhao Lei
                   ` (3 preceding siblings ...)
  2015-10-26 10:28 ` [PATCH v2 4/5] btrfs-progs: Add missing close_ctree to btrfs-select-super.c Zhao Lei
@ 2015-10-26 10:28 ` Zhao Lei
  2015-10-26 14:57 ` [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices David Sterba
  5 siblings, 0 replies; 7+ messages in thread
From: Zhao Lei @ 2015-10-26 10:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: 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.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 extent-tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/extent-tree.c b/extent-tree.c
index 0c8152a..0d605e1 100644
--- a/extent-tree.c
+++ b/extent-tree.c
@@ -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
-- 
1.8.5.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices
  2015-10-26 10:28 [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices Zhao Lei
                   ` (4 preceding siblings ...)
  2015-10-26 10:28 ` [PATCH v2 5/5] btrfs-progs: use system's default path for math.h Zhao Lei
@ 2015-10-26 14:57 ` David Sterba
  5 siblings, 0 replies; 7+ messages in thread
From: David Sterba @ 2015-10-26 14:57 UTC (permalink / raw)
  To: Zhao Lei; +Cc: linux-btrfs

On Mon, Oct 26, 2015 at 06:28:17PM +0800, Zhao Lei wrote:
> 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

Applied, thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-10-26 14:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-26 10:28 [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices Zhao Lei
2015-10-26 10:28 ` [PATCH v2 1/5] btrfs-progs: btrfs: Add missing btrfs_close_all_devices for btrfs command Zhao Lei
2015-10-26 10:28 ` [PATCH v2 2/5] btrfs-progs: Remove all btrfs_close_all_devices in sub-command Zhao Lei
2015-10-26 10:28 ` [PATCH v2 3/5] btrfs-progs: Add all missing btrfs_close_all_devices to standalone tools Zhao Lei
2015-10-26 10:28 ` [PATCH v2 4/5] btrfs-progs: Add missing close_ctree to btrfs-select-super.c Zhao Lei
2015-10-26 10:28 ` [PATCH v2 5/5] btrfs-progs: use system's default path for math.h Zhao Lei
2015-10-26 14:57 ` [PATCH v2 0/5] btrfs-progs: Add all missing close_ctree and btrfs_close_all_devices David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).