slot_map.c 7.9 KB

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