slot_map.c 7.0 KB

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