slot_map.c 12 KB

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