slot_map.c 12 KB

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