slot_map.c 12 KB

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