slot_map.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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)
  104. {
  105. int i;
  106. s16 ret = OCFS2_INVALID_SLOT;
  107. for(i = 0; i < si->si_num_slots; i++) {
  108. if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) {
  109. ret = (s16) i;
  110. break;
  111. }
  112. }
  113. return ret;
  114. }
  115. s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  116. s16 global)
  117. {
  118. s16 ret;
  119. spin_lock(&si->si_lock);
  120. ret = __ocfs2_node_num_to_slot(si, global);
  121. spin_unlock(&si->si_lock);
  122. return ret;
  123. }
  124. static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
  125. s16 slot_num,
  126. s16 node_num)
  127. {
  128. BUG_ON(slot_num == OCFS2_INVALID_SLOT);
  129. BUG_ON(slot_num >= si->si_num_slots);
  130. BUG_ON((node_num != O2NM_INVALID_NODE_NUM) &&
  131. (node_num >= O2NM_MAX_NODES));
  132. si->si_global_node_nums[slot_num] = node_num;
  133. }
  134. void ocfs2_clear_slot(struct ocfs2_slot_info *si,
  135. s16 slot_num)
  136. {
  137. spin_lock(&si->si_lock);
  138. __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
  139. spin_unlock(&si->si_lock);
  140. }
  141. int ocfs2_init_slot_info(struct ocfs2_super *osb)
  142. {
  143. int status, i;
  144. u64 blkno;
  145. struct inode *inode = NULL;
  146. struct buffer_head *bh = NULL;
  147. struct ocfs2_slot_info *si;
  148. si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
  149. if (!si) {
  150. status = -ENOMEM;
  151. mlog_errno(status);
  152. goto bail;
  153. }
  154. spin_lock_init(&si->si_lock);
  155. si->si_num_slots = osb->max_slots;
  156. si->si_size = OCFS2_MAX_SLOTS;
  157. for(i = 0; i < si->si_num_slots; i++)
  158. si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
  159. inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
  160. OCFS2_INVALID_SLOT);
  161. if (!inode) {
  162. status = -EINVAL;
  163. mlog_errno(status);
  164. goto bail;
  165. }
  166. status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
  167. if (status < 0) {
  168. mlog_errno(status);
  169. goto bail;
  170. }
  171. status = ocfs2_read_block(osb, blkno, &bh, 0, inode);
  172. if (status < 0) {
  173. mlog_errno(status);
  174. goto bail;
  175. }
  176. si->si_inode = inode;
  177. si->si_bh = bh;
  178. osb->slot_info = si;
  179. bail:
  180. if (status < 0 && si)
  181. ocfs2_free_slot_info(si);
  182. return status;
  183. }
  184. void ocfs2_free_slot_info(struct ocfs2_slot_info *si)
  185. {
  186. if (si->si_inode)
  187. iput(si->si_inode);
  188. if (si->si_bh)
  189. brelse(si->si_bh);
  190. kfree(si);
  191. }
  192. int ocfs2_find_slot(struct ocfs2_super *osb)
  193. {
  194. int status;
  195. s16 slot;
  196. struct ocfs2_slot_info *si;
  197. mlog_entry_void();
  198. si = osb->slot_info;
  199. ocfs2_update_slot_info(si);
  200. spin_lock(&si->si_lock);
  201. /* search for ourselves first and take the slot if it already
  202. * exists. Perhaps we need to mark this in a variable for our
  203. * own journal recovery? Possibly not, though we certainly
  204. * need to warn to the user */
  205. slot = __ocfs2_node_num_to_slot(si, osb->node_num);
  206. if (slot == OCFS2_INVALID_SLOT) {
  207. /* if no slot yet, then just take 1st available
  208. * one. */
  209. slot = __ocfs2_find_empty_slot(si);
  210. if (slot == OCFS2_INVALID_SLOT) {
  211. spin_unlock(&si->si_lock);
  212. mlog(ML_ERROR, "no free slots available!\n");
  213. status = -EINVAL;
  214. goto bail;
  215. }
  216. } else
  217. mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
  218. slot);
  219. __ocfs2_fill_slot(si, slot, osb->node_num);
  220. osb->slot_num = slot;
  221. spin_unlock(&si->si_lock);
  222. mlog(0, "taking node slot %d\n", osb->slot_num);
  223. status = ocfs2_update_disk_slots(osb, si);
  224. if (status < 0)
  225. mlog_errno(status);
  226. bail:
  227. mlog_exit(status);
  228. return status;
  229. }
  230. void ocfs2_put_slot(struct ocfs2_super *osb)
  231. {
  232. int status;
  233. struct ocfs2_slot_info *si = osb->slot_info;
  234. if (!si)
  235. return;
  236. ocfs2_update_slot_info(si);
  237. spin_lock(&si->si_lock);
  238. __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
  239. osb->slot_num = OCFS2_INVALID_SLOT;
  240. spin_unlock(&si->si_lock);
  241. status = ocfs2_update_disk_slots(osb, si);
  242. if (status < 0) {
  243. mlog_errno(status);
  244. goto bail;
  245. }
  246. bail:
  247. osb->slot_info = NULL;
  248. ocfs2_free_slot_info(si);
  249. }