slot_map.c 12 KB

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