You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
4.7 KiB

  1. From 4c6e7c0fcc6da66cf81c0714bf907762194eedf2 Mon Sep 17 00:00:00 2001
  2. From: Chris Leech <cleech@redhat.com>
  3. Date: Tue, 13 Aug 2013 11:34:31 -0700
  4. Subject: [PATCH] idbm_rec_write, seperate old and new style writes
  5. Duplicates a small bit of code, but easier to understand and extened.
  6. ---
  7. usr/idbm.c | 129 +++++++++++++++++++++++++++++++++++------------------
  8. 1 file changed, 86 insertions(+), 43 deletions(-)
  9. diff --git a/usr/idbm.c b/usr/idbm.c
  10. index a7da540..2f5e309 100644
  11. --- a/usr/idbm.c
  12. +++ b/usr/idbm.c
  13. @@ -2030,12 +2030,7 @@ mkdir_portal:
  14. return f;
  15. }
  16. -/*
  17. - * When the disable_lock param is true, the idbm_lock/idbm_unlock needs
  18. - * to be holt by the caller, this will avoid overwriting each other in
  19. - * case of updating(read-modify-write) the recs in parallel.
  20. - */
  21. -static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
  22. +static int idbm_rec_write_new(node_rec_t *rec)
  23. {
  24. struct stat statb;
  25. FILE *f;
  26. @@ -2048,39 +2043,8 @@ static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
  27. return ISCSI_ERR_NOMEM;
  28. }
  29. - snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
  30. - if (access(portal, F_OK) != 0) {
  31. - if (mkdir(portal, 0660) != 0) {
  32. - log_error("Could not make %s: %s", portal,
  33. - strerror(errno));
  34. - rc = ISCSI_ERR_IDBM;
  35. - goto free_portal;
  36. - }
  37. - }
  38. -
  39. - snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
  40. - if (access(portal, F_OK) != 0) {
  41. - if (mkdir(portal, 0660) != 0) {
  42. - log_error("Could not make %s: %s", portal,
  43. - strerror(errno));
  44. - rc = ISCSI_ERR_IDBM;
  45. - goto free_portal;
  46. - }
  47. - }
  48. -
  49. snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
  50. rec->name, rec->conn[0].address, rec->conn[0].port);
  51. - log_debug(5, "Looking for config file %s", portal);
  52. -
  53. - if (!disable_lock) {
  54. - rc = idbm_lock();
  55. - if (rc)
  56. - goto free_portal;
  57. - }
  58. -
  59. - if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
  60. - /* drop down to old style portal as config */
  61. - goto open_conf;
  62. rc = stat(portal, &statb);
  63. if (rc) {
  64. @@ -2101,11 +2065,11 @@ static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
  65. log_error("Could not convert %s: %s", portal,
  66. strerror(errno));
  67. rc = ISCSI_ERR_IDBM;
  68. - goto unlock;
  69. + goto free_portal;
  70. }
  71. } else {
  72. rc = ISCSI_ERR_INVAL;
  73. - goto unlock;
  74. + goto free_portal;
  75. }
  76. mkdir_portal:
  77. @@ -2116,24 +2080,103 @@ mkdir_portal:
  78. log_error("Could not make dir %s: %s",
  79. portal, strerror(errno));
  80. rc = ISCSI_ERR_IDBM;
  81. - goto unlock;
  82. + goto free_portal;
  83. }
  84. }
  85. snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR,
  86. rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
  87. rec->iface.name);
  88. -open_conf:
  89. +
  90. f = fopen(portal, "w");
  91. if (!f) {
  92. log_error("Could not open %s: %s", portal, strerror(errno));
  93. rc = ISCSI_ERR_IDBM;
  94. - goto unlock;
  95. + goto free_portal;
  96. }
  97. idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
  98. fclose(f);
  99. -unlock:
  100. +free_portal:
  101. + free(portal);
  102. + return rc;
  103. +}
  104. +
  105. +static int idbm_rec_write_old(node_rec_t *rec)
  106. +{
  107. + FILE *f;
  108. + char *portal;
  109. + int rc = 0;
  110. +
  111. + portal = malloc(PATH_MAX);
  112. + if (!portal) {
  113. + log_error("Could not alloc portal");
  114. + return ISCSI_ERR_NOMEM;
  115. + }
  116. + snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
  117. + rec->name, rec->conn[0].address, rec->conn[0].port);
  118. +
  119. + f = fopen(portal, "w");
  120. + if (!f) {
  121. + log_error("Could not open %s: %sd", portal, strerror(errno));
  122. + rc = ISCSI_ERR_IDBM;
  123. + goto free_portal;
  124. + }
  125. + idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
  126. + fclose(f);
  127. +free_portal:
  128. + free(portal);
  129. + return rc;
  130. +}
  131. +
  132. +/*
  133. + * When the disable_lock param is true, the idbm_lock/idbm_unlock needs
  134. + * to be holt by the caller, this will avoid overwriting each other in
  135. + * case of updating(read-modify-write) the recs in parallel.
  136. + */
  137. +static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
  138. +{
  139. + char *portal;
  140. + int rc = 0;
  141. +
  142. + portal = malloc(PATH_MAX);
  143. + if (!portal) {
  144. + log_error("Could not alloc portal");
  145. + return ISCSI_ERR_NOMEM;
  146. + }
  147. +
  148. + snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
  149. + if (access(portal, F_OK) != 0) {
  150. + if (mkdir(portal, 0660) != 0) {
  151. + log_error("Could not make %s: %s", portal,
  152. + strerror(errno));
  153. + rc = ISCSI_ERR_IDBM;
  154. + goto free_portal;
  155. + }
  156. + }
  157. +
  158. + snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
  159. + if (access(portal, F_OK) != 0) {
  160. + if (mkdir(portal, 0660) != 0) {
  161. + log_error("Could not make %s: %s", portal,
  162. + strerror(errno));
  163. + rc = ISCSI_ERR_IDBM;
  164. + goto free_portal;
  165. + }
  166. + }
  167. +
  168. + if (!disable_lock) {
  169. + rc = idbm_lock();
  170. + if (rc)
  171. + goto free_portal;
  172. + }
  173. +
  174. + if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
  175. + /* old style portal as config */
  176. + rc = idbm_rec_write_old(rec);
  177. + else
  178. + rc = idbm_rec_write_new(rec);
  179. +
  180. if (!disable_lock)
  181. idbm_unlock();
  182. free_portal:
  183. --
  184. 2.21.0