diff --git a/include/tracefs/tracefs.h b/include/tracefs/tracefs.h
index e844c75..6120dbe 100644
--- a/include/tracefs/tracefs.h
+++ b/include/tracefs/tracefs.h
@@ -17,4 +17,6 @@ const char *tracefs_get_tracing_dir(void);
 /* tracefs_find_tracing_dir must be freed */
 char *tracefs_find_tracing_dir(void);
 
+int tracefs_write_file(const char *file, const char *str, const char *type);
+
 #endif /* _TRACE_FS_H */
diff --git a/lib/tracefs/tracefs-utils.c b/lib/tracefs/tracefs-utils.c
index c695b8b..42a3f46 100644
--- a/lib/tracefs/tracefs-utils.c
+++ b/lib/tracefs/tracefs-utils.c
@@ -7,6 +7,8 @@
 #include <sys/mount.h>
 #include <sys/stat.h>
 #include <linux/limits.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "tracefs.h"
 
@@ -178,3 +180,42 @@ void tracefs_put_tracing_file(char *name)
 {
 	free(name);
 }
+
+/*
+ * tracefs_write_file - Write in trace file
+ * @file: Full name of the trace file.
+ * @str: A null-terminated string, that will be written in the file.
+ * @type: A null-terminated string, describing the current write operation.
+ *        Used for logging purposes. If not NULL, in case of an error the
+ *        content of the file is dumped to stderror.
+ *
+ * Returns the number of written bytes, or -1 in case of an error
+ */
+int tracefs_write_file(const char *file, const char *str, const char *type)
+{
+	char buf[BUFSIZ];
+	int ret;
+	int fd;
+
+	fd = open(file, O_WRONLY | O_TRUNC);
+	if (fd < 0) {
+		warning("Failed to open '%s'", file);
+		return -1;
+	}
+	ret = write(fd, str, strlen(str));
+	close(fd);
+	if (ret < 0 && type) {
+		/* write failed */
+		fd = open(file, O_RDONLY);
+		if (fd < 0) {
+			warning("Failed to write in '%s'", file);
+			return -1;
+		}
+
+		while ((ret = read(fd, buf, BUFSIZ)) > 0)
+			fprintf(stderr, "%.*s", ret, buf);
+		warning("Failed %s of %s\n", type, file);
+		close(fd);
+	}
+	return ret;
+}
diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index 5355813..be21f3d 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -832,31 +832,6 @@ get_instance_dir(struct buffer_instance *instance)
 	return path;
 }
 
-static int write_file(const char *file, const char *str, const char *type)
-{
-	char buf[BUFSIZ];
-	int fd;
-	int ret;
-
-	fd = open(file, O_WRONLY | O_TRUNC);
-	if (fd < 0)
-		die("opening to '%s'", file);
-	ret = write(fd, str, strlen(str));
-	close(fd);
-	if (ret < 0 && type) {
-		/* write failed */
-		fd = open(file, O_RDONLY);
-		if (fd < 0)
-			die("writing to '%s'", file);
-		/* the filter has the error */
-		while ((ret = read(fd, buf, BUFSIZ)) > 0)
-			fprintf(stderr, "%.*s", ret, buf);
-		die("Failed %s of %s\n", type, file);
-		close(fd);
-	}
-	return ret;
-}
-
 static int
 write_instance_file(struct buffer_instance *instance,
 		    const char *file, const char *str, const char *type)
@@ -867,8 +842,11 @@ write_instance_file(struct buffer_instance *instance,
 
 	path = get_instance_file(instance, file);
 	ret = stat(path, &st);
-	if (ret == 0)
-		ret = write_file(path, str, type);
+	if (ret == 0) {
+		ret = tracefs_write_file(path, str, type);
+		if (ret < 0)
+			die("Failed to write file");
+	}
 	tracefs_put_tracing_file(path);
 
 	return ret;
@@ -2010,7 +1988,8 @@ static int find_trigger(const char *file, char *buf, int size, int fields)
 
 static void write_filter(const char *file, const char *filter)
 {
-	write_file(file, filter, "filter");
+	if (tracefs_write_file(file, filter, "filter") < 0)
+		die("Failed to write file");
 }
 
 static void clear_filter(const char *file)
@@ -2020,12 +1999,14 @@ static void clear_filter(const char *file)
 
 static void write_trigger(const char *file, const char *trigger)
 {
-	write_file(file, trigger, "trigger");
+	if (tracefs_write_file(file, trigger, "trigger") < 0)
+		die("Failed to write file");
 }
 
 static void write_func_filter(const char *file, const char *trigger)
 {
-	write_file(file, trigger, "function filter");
+	if (tracefs_write_file(file, trigger, "function filter") < 0)
+		die("Failed to write file");
 }
 
 static void clear_trigger(const char *file)
@@ -2117,8 +2098,10 @@ static void update_reset_files(void)
 		reset = reset_files;
 		reset_files = reset->next;
 
-		if (!keep)
-			write_file(reset->path, reset->reset, "reset");
+		if (!keep) {
+			if (tracefs_write_file(reset->path, reset->reset, "reset") < 0)
+				die("Failed to write file");
+		}
 		free(reset->path);
 		free(reset->reset);
 		free(reset);
