slot_map.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. /* Use the slot information we've collected to create a map of mounted
  45. * nodes. Should be holding an EX on super block. assumes slot info is
  46. * up to date. Note that we call this *after* we find a slot, so our
  47. * own node should be set in the map too... */
  48. void ocfs2_populate_mounted_map(struct ocfs2_super *osb)
  49. {
  50. int i;
  51. struct ocfs2_slot_info *si = osb->slot_info;
  52. spin_lock(&si->si_lock);
  53. for (i = 0; i < si->si_size; i++)
  54. if (si->si_global_node_nums[i] != OCFS2_INVALID_SLOT)
  55. ocfs2_node_map_set_bit(osb, &osb->mounted_map,
  56. si->si_global_node_nums[i]);
  57. spin_unlock(&si->si_lock);
  58. }
  59. /* post the slot information on disk into our slot_info struct. */
  60. void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
  61. {
  62. int i;
  63. __le16 *disk_info;
  64. /* we don't read the slot block here as ocfs2_super_lock
  65. * should've made sure we have the most recent copy. */
  66. spin_lock(&si->si_lock);
  67. disk_info = (__le16 *) si->si_bh->b_data;
  68. for (i = 0; i < si->si_size; i++)
  69. si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]);
  70. spin_unlock(&si->si_lock);
  71. }
  72. /* post the our slot info stuff into it's destination bh and write it
  73. * out. */
  74. int ocfs2_update_disk_slots(struct ocfs2_super *osb,
  75. struct ocfs2_slot_info *si)
  76. {
  77. int status, i;
  78. __le16 *disk_info = (__le16 *) si->si_bh->b_data;
  79. spin_lock(&si->si_lock);
  80. for (i = 0; i < si->si_size; i++)
  81. disk_info[i] = cpu_to_le16(si->si_global_node_nums[i]);
  82. spin_unlock(&si->si_lock);
  83. status = ocfs2_write_block(osb, si->si_bh, si->si_inode);
  84. if (status < 0)
  85. mlog_errno(status);
  86. return status;
  87. }
  88. /* try to find global node in the slot info. Returns
  89. * OCFS2_INVALID_SLOT if nothing is found. */
  90. static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  91. s16 global)
  92. {
  93. int i;
  94. s16 ret = OCFS2_INVALID_SLOT;
  95. for(i = 0; i < si->si_num_slots; i++) {
  96. if (global == si->si_global_node_nums[i]) {
  97. ret = (s16) i;
  98. break;
  99. }
  100. }
  101. return ret;
  102. }
  103. static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si, s16 preferred)
  104. {
  105. int i;
  106. s16 ret = OCFS2_INVALID_SLOT;
  107. if (preferred >= 0 && preferred < si->si_num_slots) {
  108. if (OCFS2_INVALID_SLOT == si->si_global_node_nums[preferred]) {
  109. ret = preferred;
  110. goto out;
  111. }
  112. }
  113. for(i = 0; i < si->si_num_slots; i++) {
  114. if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) {
  115. ret = (s16) i;
  116. break;
  117. }
  118. }
  119. out:
  120. return ret;
  121. }
  122. s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  123. s16 global)
  124. {
  125. s16 ret;
  126. spin_lock(&si->si_lock);
  127. ret = __ocfs2_node_num_to_slot(si, global);
  128. spin_unlock(&si->si_lock);
  129. return ret;
  130. }
  131. static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
  132. s16 slot_num,
  133. s16 node_num)
  134. {
  135. BUG_ON(slot_num == OCFS2_INVALID_SLOT);
  136. BUG_ON(slot_num >= si->si_num_slots);
  137. BUG_ON((node_num != O2NM_INVALID_NODE_NUM) &&
  138. (node_num >= O2NM_MAX_NODES));
  139. si->si_global_node_nums[slot_num] = node_num;
  140. }
  141. void ocfs2_clear_slot(struct ocfs2_slot_info *si,
  142. s16 slot_num)
  143. {
  144. spin_lock(&si->si_lock);
  145. __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
  146. spin_unlock(&si->si_lock);
  147. }
  148. int ocfs2_init_slot_info(struct ocfs2_super *osb)
  149. {
  150. int status, i;
  151. u64 blkno;
  152. struct inode *inode = NULL;
  153. struct buffer_head *bh = NULL;
  154. struct ocfs2_slot_info *si;
  155. si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
  156. if (!si) {
  157. status = -ENOMEM;
  158. mlog_errno(status);
  159. goto bail;
  160. }
  161. spin_lock_init(&si->si_lock);
  162. si->si_num_slots = osb->max_slots;
  163. si->si_size = OCFS2_MAX_SLOTS;
  164. for(i = 0; i < si->si_num_slots; i++)
  165. si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
  166. inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
  167. OCFS2_INVALID_SLOT);
  168. if (!inode) {
  169. status = -EINVAL;
  170. mlog_errno(status);
  171. goto bail;
  172. }
  173. status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
  174. if (status < 0) {
  175. mlog_errno(status);
  176. goto bail;
  177. }
  178. status = ocfs2_read_block(osb, blkno, &bh, 0, inode);
  179. if (status < 0) {
  180. mlog_errno(status);
  181. goto bail;
  182. }
  183. si->si_inode = inode;
  184. si->si_bh = bh;
  185. osb->slot_info = si;
  186. bail:
  187. if (status < 0 && si)
  188. ocfs2_free_slot_info(si);
  189. return status;
  190. }
  191. void ocfs2_free_slot_info(struct ocfs2_slot_info *si)
  192. {
  193. if (si->si_inode)
  194. iput(si->si_inode);
  195. if (si->si_bh)
  196. brelse(si->si_bh);
  197. kfree(si);
  198. }
  199. int ocfs2_find_slot(struct ocfs2_super *osb)
  200. {
  201. int status;
  202. s16 slot;
  203. struct ocfs2_slot_info *si;
  204. mlog_entry_void();
  205. si = osb->slot_info;
  206. ocfs2_update_slot_info(si);
  207. spin_lock(&si->si_lock);
  208. /* search for ourselves first and take the slot if it already
  209. * exists. Perhaps we need to mark this in a variable for our
  210. * own journal recovery? Possibly not, though we certainly
  211. * need to warn to the user */
  212. slot = __ocfs2_node_num_to_slot(si, osb->node_num);
  213. if (slot == OCFS2_INVALID_SLOT) {
  214. /* if no slot yet, then just take 1st available
  215. * one. */
  216. slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
  217. if (slot == OCFS2_INVALID_SLOT) {
  218. spin_unlock(&si->si_lock);
  219. mlog(ML_ERROR, "no free slots available!\n");
  220. status = -EINVAL;
  221. goto bail;
  222. }
  223. } else
  224. mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
  225. slot);
  226. __ocfs2_fill_slot(si, slot, osb->node_num);
  227. osb->slot_num = slot;
  228. spin_unlock(&si->si_lock);
  229. mlog(0, "taking node slot %d\n", osb->slot_num);
  230. status = ocfs2_update_disk_slots(osb, si);
  231. if (status < 0)
  232. mlog_errno(status);
  233. bail:
  234. mlog_exit(status);
  235. return status;
  236. }
  237. void ocfs2_put_slot(struct ocfs2_super *osb)
  238. {
  239. int status;
  240. struct ocfs2_slot_info *si = osb->slot_info;
  241. if (!si)
  242. return;
  243. ocfs2_update_slot_info(si);
  244. spin_lock(&si->si_lock);
  245. __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
  246. osb->slot_num = OCFS2_INVALID_SLOT;
  247. spin_unlock(&si->si_lock);
  248. status = ocfs2_update_disk_slots(osb, si);
  249. if (status < 0) {
  250. mlog_errno(status);
  251. goto bail;
  252. }
  253. bail:
  254. osb->slot_info = NULL;
  255. ocfs2_free_slot_info(si);
  256. }