From e1bf1a6012134f673f137745c0d3cbd7f5169b40 Mon Sep 17 00:00:00 2001
From: Alberto Bertogli <albertito@blitiri.com.ar>
Date: Sat, 1 Aug 2009 21:58:26 -0300
Subject: [PATCH 64/74] journal_new(): Check plockf() return value

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>
---
 libjio/journal.c |   23 +++++++++++++++++++----
 1 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/libjio/journal.c b/libjio/journal.c
index 7e5c41d..570c8f0 100644
--- a/libjio/journal.c
+++ b/libjio/journal.c
@@ -250,6 +250,24 @@ static int is_broken(struct jfs *fs)
 	return access(broken_path, F_OK) == 0;
 }
 
+/* Open and lock (exclusive) the given file name. Returns the file descriptor,
+ * or -1 on error. */
+static int open_and_lockw(const char *name, int flags, int mode)
+{
+	int fd;
+
+	fd = open(name, flags, mode);
+	if (fd < 0)
+		return -1;
+
+	if (plockf(fd, F_LOCKW, 0, 0) != 0) {
+		close(fd);
+		return -1;
+	}
+
+	return fd;
+}
+
 
 /*
  * Journal functions
@@ -283,7 +301,7 @@ struct journal_op *journal_new(struct jfs *fs, unsigned int flags)
 
 	/* open the transaction file */
 	get_jtfile(fs, id, name);
-	fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0600);
+	fd = open_and_lockw(name, O_RDWR | O_CREAT | O_TRUNC, 0600);
 	if (fd < 0)
 		goto error;
 
@@ -296,9 +314,6 @@ struct journal_op *journal_new(struct jfs *fs, unsigned int flags)
 
 	fiu_exit_on("jio/commit/created_tf");
 
-	/* and lock it, just in case */
-	plockf(fd, F_LOCKW, 0, 0);
-
 	/* save the header */
 	hdr.ver = 1;
 	hdr.trans_id = id;
-- 
1.6.2.2.646.gb214


