slot_map.c 6.5 KB

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