slot_map.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * slot_map.c
  6. *
  7. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/slab.h>
  11. #include <linux/highmem.h>
  12. #include <cluster/masklog.h>
  13. #include "ocfs2.h"
  14. #include "dlmglue.h"
  15. #include "extent_map.h"
  16. #include "heartbeat.h"
  17. #include "inode.h"
  18. #include "slot_map.h"
  19. #include "super.h"
  20. #include "sysfile.h"
  21. #include "ocfs2_trace.h"
  22. #include "buffer_head_io.h"
  23. struct ocfs2_slot {
  24. int sl_valid;
  25. unsigned int sl_node_num;
  26. };
  27. struct ocfs2_slot_info {
  28. int si_extended;
  29. int si_slots_per_block;
  30. struct inode *si_inode;
  31. unsigned int si_blocks;
  32. struct buffer_head **si_bh;
  33. unsigned int si_num_slots;
  34. struct ocfs2_slot si_slots[];
  35. };
  36. static int __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  37. unsigned int node_num);
  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, NULL);
  114. if (ret == 0) {
  115. spin_lock(&osb->osb_lock);
  116. ocfs2_update_slot_info(si);
  117. spin_unlock(&osb->osb_lock);
  118. }
  119. return ret;
  120. }
  121. /* post the our slot info stuff into it's destination bh and write it
  122. * out. */
  123. static void ocfs2_update_disk_slot_extended(struct ocfs2_slot_info *si,
  124. int slot_num,
  125. struct buffer_head **bh)
  126. {
  127. int blkind = slot_num / si->si_slots_per_block;
  128. int slotno = slot_num % si->si_slots_per_block;
  129. struct ocfs2_slot_map_extended *se;
  130. BUG_ON(blkind >= si->si_blocks);
  131. se = (struct ocfs2_slot_map_extended *)si->si_bh[blkind]->b_data;
  132. se->se_slots[slotno].es_valid = si->si_slots[slot_num].sl_valid;
  133. if (si->si_slots[slot_num].sl_valid)
  134. se->se_slots[slotno].es_node_num =
  135. cpu_to_le32(si->si_slots[slot_num].sl_node_num);
  136. *bh = si->si_bh[blkind];
  137. }
  138. static void ocfs2_update_disk_slot_old(struct ocfs2_slot_info *si,
  139. int slot_num,
  140. struct buffer_head **bh)
  141. {
  142. int i;
  143. struct ocfs2_slot_map *sm;
  144. sm = (struct ocfs2_slot_map *)si->si_bh[0]->b_data;
  145. for (i = 0; i < si->si_num_slots; i++) {
  146. if (si->si_slots[i].sl_valid)
  147. sm->sm_slots[i] =
  148. cpu_to_le16(si->si_slots[i].sl_node_num);
  149. else
  150. sm->sm_slots[i] = cpu_to_le16(OCFS2_INVALID_SLOT);
  151. }
  152. *bh = si->si_bh[0];
  153. }
  154. static int ocfs2_update_disk_slot(struct ocfs2_super *osb,
  155. struct ocfs2_slot_info *si,
  156. int slot_num)
  157. {
  158. int status;
  159. struct buffer_head *bh;
  160. spin_lock(&osb->osb_lock);
  161. if (si->si_extended)
  162. ocfs2_update_disk_slot_extended(si, slot_num, &bh);
  163. else
  164. ocfs2_update_disk_slot_old(si, slot_num, &bh);
  165. spin_unlock(&osb->osb_lock);
  166. status = ocfs2_write_block(osb, bh, INODE_CACHE(si->si_inode));
  167. if (status < 0)
  168. mlog_errno(status);
  169. return status;
  170. }
  171. /*
  172. * Calculate how many bytes are needed by the slot map. Returns
  173. * an error if the slot map file is too small.
  174. */
  175. static int ocfs2_slot_map_physical_size(struct ocfs2_super *osb,
  176. struct inode *inode,
  177. unsigned long long *bytes)
  178. {
  179. unsigned long long bytes_needed;
  180. if (ocfs2_uses_extended_slot_map(osb)) {
  181. bytes_needed = osb->max_slots *
  182. sizeof(struct ocfs2_extended_slot);
  183. } else {
  184. bytes_needed = osb->max_slots * sizeof(__le16);
  185. }
  186. if (bytes_needed > i_size_read(inode)) {
  187. mlog(ML_ERROR,
  188. "Slot map file is too small! (size %llu, needed %llu)\n",
  189. i_size_read(inode), bytes_needed);
  190. return -ENOSPC;
  191. }
  192. *bytes = bytes_needed;
  193. return 0;
  194. }
  195. /* try to find global node in the slot info. Returns -ENOENT
  196. * if nothing is found. */
  197. static int __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
  198. unsigned int node_num)
  199. {
  200. int i, ret = -ENOENT;
  201. for(i = 0; i < si->si_num_slots; i++) {
  202. if (si->si_slots[i].sl_valid &&
  203. (node_num == si->si_slots[i].sl_node_num)) {
  204. ret = i;
  205. break;
  206. }
  207. }
  208. return ret;
  209. }
  210. static int __ocfs2_find_empty_slot(struct ocfs2_slot_info *si,
  211. int preferred)
  212. {
  213. int i, ret = -ENOSPC;
  214. if ((preferred >= 0) && (preferred < si->si_num_slots)) {
  215. if (!si->si_slots[preferred].sl_valid ||
  216. !si->si_slots[preferred].sl_node_num) {
  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. !si->si_slots[i].sl_node_num) {
  224. ret = i;
  225. break;
  226. }
  227. }
  228. out:
  229. return ret;
  230. }
  231. int ocfs2_node_num_to_slot(struct ocfs2_super *osb, unsigned int node_num)
  232. {
  233. int slot;
  234. struct ocfs2_slot_info *si = osb->slot_info;
  235. spin_lock(&osb->osb_lock);
  236. slot = __ocfs2_node_num_to_slot(si, node_num);
  237. spin_unlock(&osb->osb_lock);
  238. return slot;
  239. }
  240. int ocfs2_slot_to_node_num_locked(struct ocfs2_super *osb, int slot_num,
  241. unsigned int *node_num)
  242. {
  243. struct ocfs2_slot_info *si = osb->slot_info;
  244. assert_spin_locked(&osb->osb_lock);
  245. BUG_ON(slot_num < 0);
  246. BUG_ON(slot_num >= osb->max_slots);
  247. if (!si->si_slots[slot_num].sl_valid)
  248. return -ENOENT;
  249. *node_num = si->si_slots[slot_num].sl_node_num;
  250. return 0;
  251. }
  252. static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
  253. {
  254. unsigned int i;
  255. if (si == NULL)
  256. return;
  257. iput(si->si_inode);
  258. if (si->si_bh) {
  259. for (i = 0; i < si->si_blocks; i++) {
  260. if (si->si_bh[i]) {
  261. brelse(si->si_bh[i]);
  262. si->si_bh[i] = NULL;
  263. }
  264. }
  265. kfree(si->si_bh);
  266. }
  267. kfree(si);
  268. }
  269. int ocfs2_clear_slot(struct ocfs2_super *osb, int slot_num)
  270. {
  271. struct ocfs2_slot_info *si = osb->slot_info;
  272. if (si == NULL)
  273. return 0;
  274. spin_lock(&osb->osb_lock);
  275. ocfs2_invalidate_slot(si, slot_num);
  276. spin_unlock(&osb->osb_lock);
  277. return ocfs2_update_disk_slot(osb, osb->slot_info, slot_num);
  278. }
  279. static int ocfs2_map_slot_buffers(struct ocfs2_super *osb,
  280. struct ocfs2_slot_info *si)
  281. {
  282. int status = 0;
  283. u64 blkno;
  284. unsigned long long blocks, bytes = 0;
  285. unsigned int i;
  286. struct buffer_head *bh;
  287. status = ocfs2_slot_map_physical_size(osb, si->si_inode, &bytes);
  288. if (status)
  289. goto bail;
  290. blocks = ocfs2_blocks_for_bytes(si->si_inode->i_sb, bytes);
  291. BUG_ON(blocks > UINT_MAX);
  292. si->si_blocks = blocks;
  293. if (!si->si_blocks)
  294. goto bail;
  295. if (si->si_extended)
  296. si->si_slots_per_block =
  297. (osb->sb->s_blocksize /
  298. sizeof(struct ocfs2_extended_slot));
  299. else
  300. si->si_slots_per_block = osb->sb->s_blocksize / sizeof(__le16);
  301. /* The size checks above should ensure this */
  302. BUG_ON((osb->max_slots / si->si_slots_per_block) > blocks);
  303. trace_ocfs2_map_slot_buffers(bytes, si->si_blocks);
  304. si->si_bh = kcalloc(si->si_blocks, sizeof(struct buffer_head *),
  305. GFP_KERNEL);
  306. if (!si->si_bh) {
  307. status = -ENOMEM;
  308. mlog_errno(status);
  309. goto bail;
  310. }
  311. for (i = 0; i < si->si_blocks; i++) {
  312. status = ocfs2_extent_map_get_blocks(si->si_inode, i,
  313. &blkno, NULL, NULL);
  314. if (status < 0) {
  315. mlog_errno(status);
  316. goto bail;
  317. }
  318. trace_ocfs2_map_slot_buffers_block((unsigned long long)blkno, i);
  319. bh = NULL; /* Acquire a fresh bh */
  320. status = ocfs2_read_blocks(INODE_CACHE(si->si_inode), blkno,
  321. 1, &bh, OCFS2_BH_IGNORE_CACHE, NULL);
  322. if (status < 0) {
  323. mlog_errno(status);
  324. goto bail;
  325. }
  326. si->si_bh[i] = bh;
  327. }
  328. bail:
  329. return status;
  330. }
  331. int ocfs2_init_slot_info(struct ocfs2_super *osb)
  332. {
  333. int status;
  334. struct inode *inode = NULL;
  335. struct ocfs2_slot_info *si;
  336. si = kzalloc(struct_size(si, si_slots, osb->max_slots), GFP_KERNEL);
  337. if (!si) {
  338. status = -ENOMEM;
  339. mlog_errno(status);
  340. return status;
  341. }
  342. si->si_extended = ocfs2_uses_extended_slot_map(osb);
  343. si->si_num_slots = osb->max_slots;
  344. inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
  345. OCFS2_INVALID_SLOT);
  346. if (!inode) {
  347. status = -EINVAL;
  348. mlog_errno(status);
  349. goto bail;
  350. }
  351. si->si_inode = inode;
  352. status = ocfs2_map_slot_buffers(osb, si);
  353. if (status < 0) {
  354. mlog_errno(status);
  355. goto bail;
  356. }
  357. osb->slot_info = (struct ocfs2_slot_info *)si;
  358. bail:
  359. if (status < 0)
  360. __ocfs2_free_slot_info(si);
  361. return status;
  362. }
  363. void ocfs2_free_slot_info(struct ocfs2_super *osb)
  364. {
  365. struct ocfs2_slot_info *si = osb->slot_info;
  366. osb->slot_info = NULL;
  367. __ocfs2_free_slot_info(si);
  368. }
  369. int ocfs2_find_slot(struct ocfs2_super *osb)
  370. {
  371. int status;
  372. int slot;
  373. struct ocfs2_slot_info *si;
  374. si = osb->slot_info;
  375. spin_lock(&osb->osb_lock);
  376. ocfs2_update_slot_info(si);
  377. if (ocfs2_mount_local(osb))
  378. /* use slot 0 directly in local mode */
  379. slot = 0;
  380. else {
  381. /* search for ourselves first and take the slot if it already
  382. * exists. Perhaps we need to mark this in a variable for our
  383. * own journal recovery? Possibly not, though we certainly
  384. * need to warn to the user */
  385. slot = __ocfs2_node_num_to_slot(si, osb->node_num);
  386. if (slot < 0) {
  387. /* if no slot yet, then just take 1st available
  388. * one. */
  389. slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
  390. if (slot < 0) {
  391. spin_unlock(&osb->osb_lock);
  392. mlog(ML_ERROR, "no free slots available!\n");
  393. status = -EINVAL;
  394. goto bail;
  395. }
  396. } else
  397. printk(KERN_INFO "ocfs2: Slot %d on device (%s) was "
  398. "already allocated to this node!\n",
  399. slot, osb->dev_str);
  400. }
  401. ocfs2_set_slot(si, slot, osb->node_num);
  402. osb->slot_num = slot;
  403. spin_unlock(&osb->osb_lock);
  404. trace_ocfs2_find_slot(osb->slot_num);
  405. status = ocfs2_update_disk_slot(osb, si, osb->slot_num);
  406. if (status < 0) {
  407. mlog_errno(status);
  408. /*
  409. * if write block failed, invalidate slot to avoid overwrite
  410. * slot during dismount in case another node rightly has mounted
  411. */
  412. spin_lock(&osb->osb_lock);
  413. ocfs2_invalidate_slot(si, osb->slot_num);
  414. osb->slot_num = OCFS2_INVALID_SLOT;
  415. spin_unlock(&osb->osb_lock);
  416. }
  417. bail:
  418. return status;
  419. }
  420. void ocfs2_put_slot(struct ocfs2_super *osb)
  421. {
  422. int status, slot_num;
  423. struct ocfs2_slot_info *si = osb->slot_info;
  424. if (!si)
  425. return;
  426. spin_lock(&osb->osb_lock);
  427. ocfs2_update_slot_info(si);
  428. slot_num = osb->slot_num;
  429. ocfs2_invalidate_slot(si, osb->slot_num);
  430. osb->slot_num = OCFS2_INVALID_SLOT;
  431. spin_unlock(&osb->osb_lock);
  432. status = ocfs2_update_disk_slot(osb, si, slot_num);
  433. if (status < 0)
  434. mlog_errno(status);
  435. ocfs2_free_slot_info(osb);
  436. }