slot_map.c 10 KB

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