slot_map.c 12 KB

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