slot_map.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * slot_map.c
  5. *
  6. *
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/types.h>
  26. #include <linux/slab.h>
  27. #include <linux/highmem.h>
  28. #define MLOG_MASK_PREFIX ML_SUPER
  29. #include <cluster/masklog.h>
  30. #include "ocfs2.h"
  31. #include "dlmglue.h"
  32. #include "extent_map.h"
  33. #include "heartbeat.h"
  34. #include "inode.h"
  35. #include "slot_map.h"
  36. #include "super.h"
  37. #include "sysfile.h"
  38. #include "buffer_head_io.h"
  39. static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  40. s16 global);
  41. static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
  42. s16 slot_num,
  43. s16 node_num);
  44. /* post the slot information on disk into our slot_info struct. */
  45. static void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
  46. {
  47. int i;
  48. __le16 *disk_info;
  49. /* we don't read the slot block here as ocfs2_super_lock
  50. * should've made sure we have the most recent copy. */
  51. spin_lock(&si->si_lock);
  52. disk_info = (__le16 *) si->si_bh->b_data;
  53. for (i = 0; i < si->si_size; i++)
  54. si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]);
  55. spin_unlock(&si->si_lock);
  56. }
  57. int ocfs2_refresh_slot_info(struct ocfs2_super *osb)
  58. {
  59. int ret;
  60. struct ocfs2_slot_info *si = osb->slot_info;
  61. struct buffer_head *bh;
  62. if (si == NULL)
  63. return 0;
  64. bh = si->si_bh;
  65. ret = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0, si->si_inode);
  66. if (ret == 0)
  67. ocfs2_update_slot_info(si);
  68. return ret;
  69. }
  70. /* post the our slot info stuff into it's destination bh and write it
  71. * out. */
  72. static int ocfs2_update_disk_slots(struct ocfs2_super *osb,
  73. struct ocfs2_slot_info *si)
  74. {
  75. int status, i;
  76. __le16 *disk_info = (__le16 *) si->si_bh->b_data;
  77. spin_lock(&si->si_lock);
  78. for (i = 0; i < si->si_size; i++)
  79. disk_info[i] = cpu_to_le16(si->si_global_node_nums[i]);
  80. spin_unlock(&si->si_lock);
  81. status = ocfs2_write_block(osb, si->si_bh, si->si_inode);
  82. if (status < 0)
  83. mlog_errno(status);
  84. return status;
  85. }
  86. /* try to find global node in the slot info. Returns
  87. * OCFS2_INVALID_SLOT if nothing is found. */
  88. static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  89. s16 global)
  90. {
  91. int i;
  92. s16 ret = OCFS2_INVALID_SLOT;
  93. for(i = 0; i < si->si_num_slots; i++) {
  94. if (global == si->si_global_node_nums[i]) {
  95. ret = (s16) i;
  96. break;
  97. }
  98. }
  99. return ret;
  100. }
  101. static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si, s16 preferred)
  102. {
  103. int i;
  104. s16 ret = OCFS2_INVALID_SLOT;
  105. if (preferred >= 0 && preferred < si->si_num_slots) {
  106. if (OCFS2_INVALID_SLOT == si->si_global_node_nums[preferred]) {
  107. ret = preferred;
  108. goto out;
  109. }
  110. }
  111. for(i = 0; i < si->si_num_slots; i++) {
  112. if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) {
  113. ret = (s16) i;
  114. break;
  115. }
  116. }
  117. out:
  118. return ret;
  119. }
  120. s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  121. s16 global)
  122. {
  123. s16 ret;
  124. spin_lock(&si->si_lock);
  125. ret = __ocfs2_node_num_to_slot(si, global);
  126. spin_unlock(&si->si_lock);
  127. return ret;
  128. }
  129. static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
  130. {
  131. if (si == NULL)
  132. return;
  133. if (si->si_inode)
  134. iput(si->si_inode);
  135. if (si->si_bh)
  136. brelse(si->si_bh);
  137. kfree(si);
  138. }
  139. static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
  140. s16 slot_num,
  141. s16 node_num)
  142. {
  143. BUG_ON(slot_num == OCFS2_INVALID_SLOT);
  144. BUG_ON(slot_num >= si->si_num_slots);
  145. BUG_ON((node_num != O2NM_INVALID_NODE_NUM) &&
  146. (node_num >= O2NM_MAX_NODES));
  147. si->si_global_node_nums[slot_num] = node_num;
  148. }
  149. int ocfs2_clear_slot(struct ocfs2_super *osb, s16 slot_num)
  150. {
  151. struct ocfs2_slot_info *si = osb->slot_info;
  152. if (si == NULL)
  153. return 0;
  154. spin_lock(&si->si_lock);
  155. __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
  156. spin_unlock(&si->si_lock);
  157. return ocfs2_update_disk_slots(osb, osb->slot_info);
  158. }
  159. int ocfs2_init_slot_info(struct ocfs2_super *osb)
  160. {
  161. int status, i;
  162. u64 blkno;
  163. struct inode *inode = NULL;
  164. struct buffer_head *bh = NULL;
  165. struct ocfs2_slot_info *si;
  166. si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
  167. if (!si) {
  168. status = -ENOMEM;
  169. mlog_errno(status);
  170. goto bail;
  171. }
  172. spin_lock_init(&si->si_lock);
  173. si->si_num_slots = osb->max_slots;
  174. si->si_size = OCFS2_MAX_SLOTS;
  175. for(i = 0; i < si->si_num_slots; i++)
  176. si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
  177. inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
  178. OCFS2_INVALID_SLOT);
  179. if (!inode) {
  180. status = -EINVAL;
  181. mlog_errno(status);
  182. goto bail;
  183. }
  184. status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
  185. if (status < 0) {
  186. mlog_errno(status);
  187. goto bail;
  188. }
  189. status = ocfs2_read_block(osb, blkno, &bh, 0, inode);
  190. if (status < 0) {
  191. mlog_errno(status);
  192. goto bail;
  193. }
  194. si->si_inode = inode;
  195. si->si_bh = bh;
  196. osb->slot_info = si;
  197. bail:
  198. if (status < 0 && si)
  199. __ocfs2_free_slot_info(si);
  200. return status;
  201. }
  202. void ocfs2_free_slot_info(struct ocfs2_super *osb)
  203. {
  204. struct ocfs2_slot_info *si = osb->slot_info;
  205. osb->slot_info = NULL;
  206. __ocfs2_free_slot_info(si);
  207. }
  208. int ocfs2_find_slot(struct ocfs2_super *osb)
  209. {
  210. int status;
  211. s16 slot;
  212. struct ocfs2_slot_info *si;
  213. mlog_entry_void();
  214. si = osb->slot_info;
  215. ocfs2_update_slot_info(si);
  216. spin_lock(&si->si_lock);
  217. /* search for ourselves first and take the slot if it already
  218. * exists. Perhaps we need to mark this in a variable for our
  219. * own journal recovery? Possibly not, though we certainly
  220. * need to warn to the user */
  221. slot = __ocfs2_node_num_to_slot(si, osb->node_num);
  222. if (slot == OCFS2_INVALID_SLOT) {
  223. /* if no slot yet, then just take 1st available
  224. * one. */
  225. slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
  226. if (slot == OCFS2_INVALID_SLOT) {
  227. spin_unlock(&si->si_lock);
  228. mlog(ML_ERROR, "no free slots available!\n");
  229. status = -EINVAL;
  230. goto bail;
  231. }
  232. } else
  233. mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
  234. slot);
  235. __ocfs2_fill_slot(si, slot, osb->node_num);
  236. osb->slot_num = slot;
  237. spin_unlock(&si->si_lock);
  238. mlog(0, "taking node slot %d\n", osb->slot_num);
  239. status = ocfs2_update_disk_slots(osb, si);
  240. if (status < 0)
  241. mlog_errno(status);
  242. bail:
  243. mlog_exit(status);
  244. return status;
  245. }
  246. void ocfs2_put_slot(struct ocfs2_super *osb)
  247. {
  248. int status;
  249. struct ocfs2_slot_info *si = osb->slot_info;
  250. if (!si)
  251. return;
  252. ocfs2_update_slot_info(si);
  253. spin_lock(&si->si_lock);
  254. __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
  255. osb->slot_num = OCFS2_INVALID_SLOT;
  256. spin_unlock(&si->si_lock);
  257. status = ocfs2_update_disk_slots(osb, si);
  258. if (status < 0) {
  259. mlog_errno(status);
  260. goto bail;
  261. }
  262. bail:
  263. ocfs2_free_slot_info(osb);
  264. }