Skip to contentSkip to conversation

Implement atomic update operation for sa_db_file

PATCH In review Hal Rosenstock 2 participants 4 messages Nov 13, 2013 – Nov 22, 2013 3 messages not in the archive InfiniBand

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Implement atomic update operation for sa_db_file

From: Vladimir Koushnir <vladimirk-VPRAkNaXOzVWk0Htik3J/[email protected]>

---
 opensm/osm_sa.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
opensm/osm_sa.c +16 -4
index 8c5ef5d..d5c1275 100644
@@ -525,25 +525,37 @@ opensm_dump_to_file(osm_opensm_t * p_osm, const char *file_name,
 		    void (*dump_func) (osm_opensm_t * p_osm, FILE * file))
 {
 	char path[1024];
+	char path_tmp[1032];
 	FILE *file;
+	int status = 0;
 
 	snprintf(path, sizeof(path), "%s/%s",
 		 p_osm->subn.opt.dump_files_dir, file_name);
 
-	file = fopen(path, "w");
+	snprintf(path_tmp, sizeof(path_tmp), "%s.tmp", path);
+
+	file = fopen(path_tmp, "w");
 	if (!file) {
 		OSM_LOG(&p_osm->log, OSM_LOG_ERROR, "ERR 4C01: "
 			"cannot open file \'%s\': %s\n",
-			file_name, strerror(errno));
+			path_tmp, strerror(errno));
 		return -1;
 	}
 
-	chmod(path, S_IRUSR | S_IWUSR);
+	chmod(path_tmp, S_IRUSR | S_IWUSR);
 
 	dump_func(p_osm, file);
 
 	fclose(file);
-	return 0;
+
+	status = rename(path_tmp, path);
+	if (status) {
+		OSM_LOG(&p_osm->log, OSM_LOG_ERROR, "ERR 4C0B: "
+			"Failed to rename file:%s (err:%s)\n",
+			path_tmp, strerror(errno));
+	}
+
+	return status;
 }
 
 static void mcast_mgr_dump_one_port(cl_map_item_t * p_map_item, void *cxt)
Signature · 6 lines
1.7.8.2

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 5283A88D.9020608-LDSdmyG8hGV8YrgS2mwiifqBs+8SCbDb@public.gmane.org

#2 lore raw
Quoted 44 lines · Hal Rosenstock

From: Vladimir Koushnir <vladimirk-VPRAkNaXOzVWk0Htik3J/[email protected]>

---
  opensm/osm_sa.c |   20 ++++++++++++++++----
  1 files changed, 16 insertions(+), 4 deletions(-)
opensm/osm_sa.c +9 -4
index 8c5ef5d..d5c1275 100644
@@ -525,25 +525,37 @@ opensm_dump_to_file(osm_opensm_t * p_osm, const char *file_name,
  		    void (*dump_func) (osm_opensm_t * p_osm, FILE * file))
  {
  	char path[1024];
+	char path_tmp[1032];
  	FILE *file;
+	int status = 0;
 
  	snprintf(path, sizeof(path), "%s/%s",
  		 p_osm->subn.opt.dump_files_dir, file_name);
 
-	file = fopen(path, "w");
+	snprintf(path_tmp, sizeof(path_tmp), "%s.tmp", path);
+
+	file = fopen(path_tmp, "w");
  	if (!file) {
  		OSM_LOG(&p_osm->log, OSM_LOG_ERROR, "ERR 4C01: "
  			"cannot open file \'%s\': %s\n",
-			file_name, strerror(errno));
+			path_tmp, strerror(errno));
  		return -1;
  	}
 
-	chmod(path, S_IRUSR | S_IWUSR);
+	chmod(path_tmp, S_IRUSR | S_IWUSR);
 
  	dump_func(p_osm, file);
 
  	fclose(file);
-	return 0;
+
+	status = rename(path_tmp, path);

opensm/osm_sa.c opensm_dump_to_file(osm_opensm_t * p_osm, const char *file_name, · line 561

Quoted 10 lines
opensm/osm_sa.c +7 -0
+	if (status) {
+		OSM_LOG(&p_osm->log, OSM_LOG_ERROR, "ERR 4C0B: "
+			"Failed to rename file:%s (err:%s)\n",
+			path_tmp, strerror(errno));
+	}
+
+	return status;
  }
 
  static void mcast_mgr_dump_one_port(cl_map_item_t * p_map_item, void *cxt)

Isn't an fdatasync() call missing after dump_func() and before fclose() ? According to Theodore Ts'o calling fdatasync() or fsync() before fclose() is essential during an atomic update. See also http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/ for more information.

Bart.

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Hi Bart,

Quoted 5 lines · Bart Van Assche

Isn't an fdatasync() call missing after dump_func() and before fclose() ? According to Theodore Ts'o calling fdatasync() or fsync() before fclose() is essential during an atomic update. See also
http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/ for more information.

Thanks for the pointer. Yes, an fsync is appropriate here. Do you want to send a patch for that or should I just cruft one up ?

-- Hal

Quoted 1 line

Bart.

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 528F70BC.1050907-LDSdmyG8hGV8YrgS2mwiifqBs+8SCbDb@public.gmane.org

#6 lore raw
Quoted 11 lines · Hal Rosenstock

Hi Bart,

Quoted 5 lines · Bart Van Assche

Isn't an fdatasync() call missing after dump_func() and before fclose() ? According to Theodore Ts'o calling fdatasync() or fsync() before fclose() is essential during an atomic update. See also
http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/ for more information.

Thanks for the pointer. Yes, an fsync is appropriate here. Do you want to send a patch for that or should I just cruft one up ?

If you can look into this that's fine for me. I'm already involved in too many different open source projects :-)

Bart.

--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html