author | Alberto Bertogli
<albertito@blitiri.com.ar> 2009-08-02 00:58:26 UTC |
committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2009-08-02 08:00:07 UTC |
parent | 3a35fadd7df5953ff2c89345d7ccf5db5577d5dd |
libjio/journal.c | +19 | -4 |
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;