slot_map.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. unsigned int si_blocks;
  42. struct buffer_head **si_bh;
  43. unsigned int si_num_slots;
  44. unsigned int si_size;
  45. s16 si_global_node_nums[OCFS2_MAX_SLOTS];
  46. };
  47. static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  48. s16 global);
  49. static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
  50. s16 slot_num,
  51. s16 node_num);
  52. /*
  53. * Post the slot information on disk into our slot_info struct.
  54. * Must be protected by osb_lock.
  55. */
  56. static void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
  57. {
  58. int i;
  59. __le16 *disk_info;
  60. /* we don't read the slot block here as ocfs2_super_lock
  61. * should've made sure we have the most recent copy. */
  62. disk_info = (__le16 *) si->si_bh[0]->b_data;
  63. for (i = 0; i < si->si_size; i++)
  64. si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]);
  65. }
  66. int ocfs2_refresh_slot_info(struct ocfs2_super *osb)
  67. {
  68. int ret;
  69. struct ocfs2_slot_info *si = osb->slot_info;
  70. if (si == NULL)
  71. return 0;
  72. BUG_ON(si->si_blocks == 0);
  73. BUG_ON(si->si_bh == NULL);
  74. mlog(0, "Refreshing slot map, reading %u block(s)\n",
  75. si->si_blocks);
  76. /*
  77. * We pass -1 as blocknr because we expect all of si->si_bh to
  78. * be !NULL. Thus, ocfs2_read_blocks() will ignore blocknr. If
  79. * this is not true, the read of -1 (UINT64_MAX) will fail.
  80. */
  81. ret = ocfs2_read_blocks(osb, -1, si->si_blocks, si->si_bh, 0,
  82. si->si_inode);
  83. if (ret == 0) {
  84. spin_lock(&osb->osb_lock);
  85. ocfs2_update_slot_info(si);
  86. spin_unlock(&osb->osb_lock);
  87. }
  88. return ret;
  89. }
  90. /* post the our slot info stuff into it's destination bh and write it
  91. * out. */
  92. static int ocfs2_update_disk_slots(struct ocfs2_super *osb,
  93. struct ocfs2_slot_info *si)
  94. {
  95. int status, i;
  96. __le16 *disk_info = (__le16 *) si->si_bh[0]->b_data;
  97. spin_lock(&osb->osb_lock);
  98. for (i = 0; i < si->si_size; i++)
  99. disk_info[i] = cpu_to_le16(si->si_global_node_nums[i]);
  100. spin_unlock(&osb->osb_lock);
  101. status = ocfs2_write_block(osb, si->si_bh[0], si->si_inode);
  102. if (status < 0)
  103. mlog_errno(status);
  104. return status;
  105. }
  106. /*
  107. * Calculate how many bytes are needed by the slot map. Returns
  108. * an error if the slot map file is too small.
  109. */
  110. static int ocfs2_slot_map_physical_size(struct ocfs2_super *osb,
  111. struct inode *inode,
  112. unsigned long long *bytes)
  113. {
  114. unsigned long long bytes_needed;
  115. bytes_needed = osb->max_slots * sizeof(__le16);
  116. if (bytes_needed > i_size_read(inode)) {
  117. mlog(ML_ERROR,
  118. "Slot map file is too small! (size %llu, needed %llu)\n",
  119. i_size_read(inode), bytes_needed);
  120. return -ENOSPC;
  121. }
  122. *bytes = bytes_needed;
  123. return 0;
  124. }
  125. /* try to find global node in the slot info. Returns
  126. * OCFS2_INVALID_SLOT if nothing is found. */
  127. static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  128. s16 global)
  129. {
  130. int i;
  131. s16 ret = OCFS2_INVALID_SLOT;
  132. for(i = 0; i < si->si_num_slots; i++) {
  133. if (global == si->si_global_node_nums[i]) {
  134. ret = (s16) i;
  135. break;
  136. }
  137. }
  138. return ret;
  139. }
  140. static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si,
  141. s16 preferred)
  142. {
  143. int i;
  144. s16 ret = OCFS2_INVALID_SLOT;
  145. if (preferred >= 0 && preferred < si->si_num_slots) {
  146. if (OCFS2_INVALID_SLOT == si->si_global_node_nums[preferred]) {
  147. ret = preferred;
  148. goto out;
  149. }
  150. }
  151. for(i = 0; i < si->si_num_slots; i++) {
  152. if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) {
  153. ret = (s16) i;
  154. break;
  155. }
  156. }
  157. out:
  158. return ret;
  159. }
  160. int ocfs2_node_num_to_slot(struct ocfs2_super *osb, unsigned int node_num)
  161. {
  162. s16 slot;
  163. struct ocfs2_slot_info *si = osb->slot_info;
  164. spin_lock(&osb->osb_lock);
  165. slot = __ocfs2_node_num_to_slot(si, node_num);
  166. spin_unlock(&osb->osb_lock);
  167. if (slot == OCFS2_INVALID_SLOT)
  168. return -ENOENT;
  169. return slot;
  170. }
  171. int ocfs2_slot_to_node_num_locked(struct ocfs2_super *osb, int slot_num,
  172. unsigned int *node_num)
  173. {
  174. struct ocfs2_slot_info *si = osb->slot_info;
  175. assert_spin_locked(&osb->osb_lock);
  176. BUG_ON(slot_num < 0);
  177. BUG_ON(slot_num > osb->max_slots);
  178. if (si->si_global_node_nums[slot_num] == OCFS2_INVALID_SLOT)
  179. return -ENOENT;
  180. *node_num = si->si_global_node_nums[slot_num];
  181. return 0;
  182. }
  183. static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
  184. {
  185. unsigned int i;
  186. if (si == NULL)
  187. return;
  188. if (si->si_inode)
  189. iput(si->si_inode);
  190. if (si->si_bh) {
  191. for (i = 0; i < si->si_blocks; i++) {
  192. if (si->si_bh[i]) {
  193. brelse(si->si_bh[i]);
  194. si->si_bh[i] = NULL;
  195. }
  196. }
  197. kfree(si->si_bh);
  198. }
  199. kfree(si);
  200. }
  201. static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
  202. s16 slot_num,
  203. s16 node_num)
  204. {
  205. BUG_ON(slot_num == OCFS2_INVALID_SLOT);
  206. BUG_ON(slot_num >= si->si_num_slots);
  207. BUG_ON((node_num != O2NM_INVALID_NODE_NUM) &&
  208. (node_num >= O2NM_MAX_NODES));
  209. si->si_global_node_nums[slot_num] = node_num;
  210. }
  211. int ocfs2_clear_slot(struct ocfs2_super *osb, s16 slot_num)
  212. {
  213. struct ocfs2_slot_info *si = osb->slot_info;
  214. if (si == NULL)
  215. return 0;
  216. spin_lock(&osb->osb_lock);
  217. __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
  218. spin_unlock(&osb->osb_lock);
  219. return ocfs2_update_disk_slots(osb, osb->slot_info);
  220. }
  221. static int ocfs2_map_slot_buffers(struct ocfs2_super *osb,
  222. struct ocfs2_slot_info *si)
  223. {
  224. int status = 0;
  225. u64 blkno;
  226. unsigned long long blocks, bytes;
  227. unsigned int i;
  228. struct buffer_head *bh;
  229. status = ocfs2_slot_map_physical_size(osb, si->si_inode, &bytes);
  230. if (status)
  231. goto bail;
  232. blocks = ocfs2_blocks_for_bytes(si->si_inode->i_sb, bytes);
  233. BUG_ON(blocks > UINT_MAX);
  234. si->si_blocks = blocks;
  235. if (!si->si_blocks)
  236. goto bail;
  237. mlog(0, "Slot map needs %u buffers for %llu bytes\n",
  238. si->si_blocks, bytes);
  239. si->si_bh = kzalloc(sizeof(struct buffer_head *) * si->si_blocks,
  240. GFP_KERNEL);
  241. if (!si->si_bh) {
  242. status = -ENOMEM;
  243. mlog_errno(status);
  244. goto bail;
  245. }
  246. for (i = 0; i < si->si_blocks; i++) {
  247. status = ocfs2_extent_map_get_blocks(si->si_inode, i,
  248. &blkno, NULL, NULL);
  249. if (status < 0) {
  250. mlog_errno(status);
  251. goto bail;
  252. }
  253. mlog(0, "Reading slot map block %u at %llu\n", i,
  254. (unsigned long long)blkno);
  255. bh = NULL; /* Acquire a fresh bh */
  256. status = ocfs2_read_block(osb, blkno, &bh, 0, si->si_inode);
  257. if (status < 0) {
  258. mlog_errno(status);
  259. goto bail;
  260. }
  261. si->si_bh[i] = bh;
  262. }
  263. bail:
  264. return status;
  265. }
  266. int ocfs2_init_slot_info(struct ocfs2_super *osb)
  267. {
  268. int status, i;
  269. struct inode *inode = NULL;
  270. struct ocfs2_slot_info *si;
  271. si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
  272. if (!si) {
  273. status = -ENOMEM;
  274. mlog_errno(status);
  275. goto bail;
  276. }
  277. si->si_num_slots = osb->max_slots;
  278. si->si_size = OCFS2_MAX_SLOTS;
  279. for(i = 0; i < si->si_num_slots; i++)
  280. si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
  281. inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
  282. OCFS2_INVALID_SLOT);
  283. if (!inode) {
  284. status = -EINVAL;
  285. mlog_errno(status);
  286. goto bail;
  287. }
  288. si->si_inode = inode;
  289. status = ocfs2_map_slot_buffers(osb, si);
  290. if (status < 0) {
  291. mlog_errno(status);
  292. goto bail;
  293. }
  294. osb->slot_info = (struct ocfs2_slot_info *)si;
  295. bail:
  296. if (status < 0 && si)
  297. __ocfs2_free_slot_info(si);
  298. return status;
  299. }
  300. void ocfs2_free_slot_info(struct ocfs2_super *osb)
  301. {
  302. struct ocfs2_slot_info *si = osb->slot_info;
  303. osb->slot_info = NULL;
  304. __ocfs2_free_slot_info(si);
  305. }
  306. int ocfs2_find_slot(struct ocfs2_super *osb)
  307. {
  308. int status;
  309. s16 slot;
  310. struct ocfs2_slot_info *si;
  311. mlog_entry_void();
  312. si = osb->slot_info;
  313. spin_lock(&osb->osb_lock);
  314. ocfs2_update_slot_info(si);
  315. /* search for ourselves first and take the slot if it already
  316. * exists. Perhaps we need to mark this in a variable for our
  317. * own journal recovery? Possibly not, though we certainly
  318. * need to warn to the user */
  319. slot = __ocfs2_node_num_to_slot(si, osb->node_num);
  320. if (slot == OCFS2_INVALID_SLOT) {
  321. /* if no slot yet, then just take 1st available
  322. * one. */
  323. slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
  324. if (slot == OCFS2_INVALID_SLOT) {
  325. spin_unlock(&osb->osb_lock);
  326. mlog(ML_ERROR, "no free slots available!\n");
  327. status = -EINVAL;
  328. goto bail;
  329. }
  330. } else
  331. mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
  332. slot);
  333. __ocfs2_fill_slot(si, slot, osb->node_num);
  334. osb->slot_num = slot;
  335. spin_unlock(&osb->osb_lock);
  336. mlog(0, "taking node slot %d\n", osb->slot_num);
  337. status = ocfs2_update_disk_slots(osb, si);
  338. if (status < 0)
  339. mlog_errno(status);
  340. bail:
  341. mlog_exit(status);
  342. return status;
  343. }
  344. void ocfs2_put_slot(struct ocfs2_super *osb)
  345. {
  346. int status;
  347. struct ocfs2_slot_info *si = osb->slot_info;
  348. if (!si)
  349. return;
  350. spin_lock(&osb->osb_lock);
  351. ocfs2_update_slot_info(si);
  352. __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
  353. osb->slot_num = OCFS2_INVALID_SLOT;
  354. spin_unlock(&osb->osb_lock);
  355. status = ocfs2_update_disk_slots(osb, si);
  356. if (status < 0) {
  357. mlog_errno(status);
  358. goto bail;
  359. }
  360. bail:
  361. ocfs2_free_slot_info(osb);
  362. }