vote.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * vote.c
  5. *
  6. * description here
  7. *
  8. * Copyright (C) 2003, 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 <linux/smp_lock.h>
  29. #include <linux/kthread.h>
  30. #include <cluster/heartbeat.h>
  31. #include <cluster/nodemanager.h>
  32. #include <cluster/tcp.h>
  33. #include <dlm/dlmapi.h>
  34. #define MLOG_MASK_PREFIX ML_VOTE
  35. #include <cluster/masklog.h>
  36. #include "ocfs2.h"
  37. #include "alloc.h"
  38. #include "dlmglue.h"
  39. #include "extent_map.h"
  40. #include "heartbeat.h"
  41. #include "inode.h"
  42. #include "journal.h"
  43. #include "slot_map.h"
  44. #include "vote.h"
  45. #include "buffer_head_io.h"
  46. #define OCFS2_MESSAGE_TYPE_VOTE (0x1)
  47. #define OCFS2_MESSAGE_TYPE_RESPONSE (0x2)
  48. struct ocfs2_msg_hdr
  49. {
  50. __be32 h_response_id; /* used to lookup message handle on sending
  51. * node. */
  52. __be32 h_request;
  53. __be64 h_blkno;
  54. __be32 h_generation;
  55. __be32 h_node_num; /* node sending this particular message. */
  56. };
  57. /* OCFS2_MAX_FILENAME_LEN is 255 characters, but we want to align this
  58. * for the network. */
  59. #define OCFS2_VOTE_FILENAME_LEN 256
  60. struct ocfs2_vote_msg
  61. {
  62. struct ocfs2_msg_hdr v_hdr;
  63. union {
  64. __be32 v_generic1;
  65. __be32 v_orphaned_slot; /* Used during delete votes */
  66. __be32 v_nlink; /* Used during unlink votes */
  67. } md1; /* Message type dependant 1 */
  68. };
  69. /* Responses are given these values to maintain backwards
  70. * compatibility with older ocfs2 versions */
  71. #define OCFS2_RESPONSE_OK (0)
  72. #define OCFS2_RESPONSE_BUSY (-16)
  73. #define OCFS2_RESPONSE_BAD_MSG (-22)
  74. struct ocfs2_response_msg
  75. {
  76. struct ocfs2_msg_hdr r_hdr;
  77. __be32 r_response;
  78. __be32 r_orphaned_slot;
  79. };
  80. struct ocfs2_vote_work {
  81. struct list_head w_list;
  82. struct ocfs2_vote_msg w_msg;
  83. };
  84. enum ocfs2_vote_request {
  85. OCFS2_VOTE_REQ_INVALID = 0,
  86. OCFS2_VOTE_REQ_DELETE,
  87. OCFS2_VOTE_REQ_MOUNT,
  88. OCFS2_VOTE_REQ_UMOUNT,
  89. OCFS2_VOTE_REQ_LAST
  90. };
  91. static inline int ocfs2_is_valid_vote_request(int request)
  92. {
  93. return OCFS2_VOTE_REQ_INVALID < request &&
  94. request < OCFS2_VOTE_REQ_LAST;
  95. }
  96. typedef void (*ocfs2_net_response_callback)(void *priv,
  97. struct ocfs2_response_msg *resp);
  98. struct ocfs2_net_response_cb {
  99. ocfs2_net_response_callback rc_cb;
  100. void *rc_priv;
  101. };
  102. struct ocfs2_net_wait_ctxt {
  103. struct list_head n_list;
  104. u32 n_response_id;
  105. wait_queue_head_t n_event;
  106. struct ocfs2_node_map n_node_map;
  107. int n_response; /* an agreggate response. 0 if
  108. * all nodes are go, < 0 on any
  109. * negative response from any
  110. * node or network error. */
  111. struct ocfs2_net_response_cb *n_callback;
  112. };
  113. static void ocfs2_process_mount_request(struct ocfs2_super *osb,
  114. unsigned int node_num)
  115. {
  116. mlog(0, "MOUNT vote from node %u\n", node_num);
  117. /* The other node only sends us this message when he has an EX
  118. * on the superblock, so our recovery threads (if having been
  119. * launched) are waiting on it.*/
  120. ocfs2_recovery_map_clear(osb, node_num);
  121. ocfs2_node_map_set_bit(osb, &osb->mounted_map, node_num);
  122. /* We clear the umount map here because a node may have been
  123. * previously mounted, safely unmounted but never stopped
  124. * heartbeating - in which case we'd have a stale entry. */
  125. ocfs2_node_map_clear_bit(osb, &osb->umount_map, node_num);
  126. }
  127. static void ocfs2_process_umount_request(struct ocfs2_super *osb,
  128. unsigned int node_num)
  129. {
  130. mlog(0, "UMOUNT vote from node %u\n", node_num);
  131. ocfs2_node_map_clear_bit(osb, &osb->mounted_map, node_num);
  132. ocfs2_node_map_set_bit(osb, &osb->umount_map, node_num);
  133. }
  134. void ocfs2_mark_inode_remotely_deleted(struct inode *inode)
  135. {
  136. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  137. assert_spin_locked(&oi->ip_lock);
  138. /* We set the SKIP_DELETE flag on the inode so we don't try to
  139. * delete it in delete_inode ourselves, thus avoiding
  140. * unecessary lock pinging. If the other node failed to wipe
  141. * the inode as a result of a crash, then recovery will pick
  142. * up the slack. */
  143. oi->ip_flags |= OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE;
  144. }
  145. static int ocfs2_process_delete_request(struct inode *inode,
  146. int *orphaned_slot)
  147. {
  148. int response = OCFS2_RESPONSE_BUSY;
  149. mlog(0, "DELETE vote on inode %lu, read lnk_cnt = %u, slot = %d\n",
  150. inode->i_ino, inode->i_nlink, *orphaned_slot);
  151. spin_lock(&OCFS2_I(inode)->ip_lock);
  152. /* Whatever our vote response is, we want to make sure that
  153. * the orphaned slot is recorded properly on this node *and*
  154. * on the requesting node. Technically, if the requesting node
  155. * did not know which slot the inode is orphaned in but we
  156. * respond with BUSY he doesn't actually need the orphaned
  157. * slot, but it doesn't hurt to do it here anyway. */
  158. if ((*orphaned_slot) != OCFS2_INVALID_SLOT) {
  159. mlog_bug_on_msg(OCFS2_I(inode)->ip_orphaned_slot !=
  160. OCFS2_INVALID_SLOT &&
  161. OCFS2_I(inode)->ip_orphaned_slot !=
  162. (*orphaned_slot),
  163. "Inode %llu: This node thinks it's "
  164. "orphaned in slot %d, messaged it's in %d\n",
  165. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  166. OCFS2_I(inode)->ip_orphaned_slot,
  167. *orphaned_slot);
  168. mlog(0, "Setting orphaned slot for inode %llu to %d\n",
  169. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  170. *orphaned_slot);
  171. OCFS2_I(inode)->ip_orphaned_slot = *orphaned_slot;
  172. } else {
  173. mlog(0, "Sending back orphaned slot %d for inode %llu\n",
  174. OCFS2_I(inode)->ip_orphaned_slot,
  175. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  176. *orphaned_slot = OCFS2_I(inode)->ip_orphaned_slot;
  177. }
  178. /* vote no if the file is still open. */
  179. if (OCFS2_I(inode)->ip_open_count) {
  180. mlog(0, "open count = %u\n",
  181. OCFS2_I(inode)->ip_open_count);
  182. spin_unlock(&OCFS2_I(inode)->ip_lock);
  183. goto done;
  184. }
  185. spin_unlock(&OCFS2_I(inode)->ip_lock);
  186. /* directories are a bit ugly... What if someone is sitting in
  187. * it? We want to make sure the inode is removed completely as
  188. * a result of the iput in process_vote. */
  189. if (S_ISDIR(inode->i_mode) && (atomic_read(&inode->i_count) != 1)) {
  190. mlog(0, "i_count = %u\n", atomic_read(&inode->i_count));
  191. goto done;
  192. }
  193. if (filemap_fdatawrite(inode->i_mapping)) {
  194. mlog(ML_ERROR, "Could not sync inode %llu for delete!\n",
  195. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  196. goto done;
  197. }
  198. sync_mapping_buffers(inode->i_mapping);
  199. truncate_inode_pages(inode->i_mapping, 0);
  200. ocfs2_extent_map_trunc(inode, 0);
  201. spin_lock(&OCFS2_I(inode)->ip_lock);
  202. /* double check open count - someone might have raced this
  203. * thread into ocfs2_file_open while we were writing out
  204. * data. If we're to allow a wipe of this inode now, we *must*
  205. * hold the spinlock until we've marked it. */
  206. if (OCFS2_I(inode)->ip_open_count) {
  207. mlog(0, "Raced to wipe! open count = %u\n",
  208. OCFS2_I(inode)->ip_open_count);
  209. spin_unlock(&OCFS2_I(inode)->ip_lock);
  210. goto done;
  211. }
  212. /* Mark the inode as being wiped from disk. */
  213. ocfs2_mark_inode_remotely_deleted(inode);
  214. spin_unlock(&OCFS2_I(inode)->ip_lock);
  215. /* Not sure this is necessary anymore. */
  216. d_prune_aliases(inode);
  217. /* If we get here, then we're voting 'yes', so commit the
  218. * delete on our side. */
  219. response = OCFS2_RESPONSE_OK;
  220. done:
  221. return response;
  222. }
  223. static void ocfs2_process_vote(struct ocfs2_super *osb,
  224. struct ocfs2_vote_msg *msg)
  225. {
  226. int net_status, vote_response;
  227. int orphaned_slot = 0;
  228. unsigned int node_num, generation;
  229. u64 blkno;
  230. enum ocfs2_vote_request request;
  231. struct inode *inode = NULL;
  232. struct ocfs2_msg_hdr *hdr = &msg->v_hdr;
  233. struct ocfs2_response_msg response;
  234. /* decode the network mumbo jumbo into local variables. */
  235. request = be32_to_cpu(hdr->h_request);
  236. blkno = be64_to_cpu(hdr->h_blkno);
  237. generation = be32_to_cpu(hdr->h_generation);
  238. node_num = be32_to_cpu(hdr->h_node_num);
  239. if (request == OCFS2_VOTE_REQ_DELETE)
  240. orphaned_slot = be32_to_cpu(msg->md1.v_orphaned_slot);
  241. mlog(0, "processing vote: request = %u, blkno = %llu, "
  242. "generation = %u, node_num = %u, priv1 = %u\n", request,
  243. (unsigned long long)blkno, generation, node_num,
  244. be32_to_cpu(msg->md1.v_generic1));
  245. if (!ocfs2_is_valid_vote_request(request)) {
  246. mlog(ML_ERROR, "Invalid vote request %d from node %u\n",
  247. request, node_num);
  248. vote_response = OCFS2_RESPONSE_BAD_MSG;
  249. goto respond;
  250. }
  251. vote_response = OCFS2_RESPONSE_OK;
  252. switch (request) {
  253. case OCFS2_VOTE_REQ_UMOUNT:
  254. ocfs2_process_umount_request(osb, node_num);
  255. goto respond;
  256. case OCFS2_VOTE_REQ_MOUNT:
  257. ocfs2_process_mount_request(osb, node_num);
  258. goto respond;
  259. default:
  260. /* avoids a gcc warning */
  261. break;
  262. }
  263. /* We cannot process the remaining message types before we're
  264. * fully mounted. It's perfectly safe however to send a 'yes'
  265. * response as we can't possibly have any of the state they're
  266. * asking us to modify yet. */
  267. if (atomic_read(&osb->vol_state) == VOLUME_INIT)
  268. goto respond;
  269. /* If we get here, then the request is against an inode. */
  270. inode = ocfs2_ilookup_for_vote(osb, blkno,
  271. request == OCFS2_VOTE_REQ_DELETE);
  272. /* Not finding the inode is perfectly valid - it means we're
  273. * not interested in what the other node is about to do to it
  274. * so in those cases we automatically respond with an
  275. * affirmative. Cluster locking ensures that we won't race
  276. * interest in the inode with this vote request. */
  277. if (!inode)
  278. goto respond;
  279. /* Check generation values. It's possible for us to get a
  280. * request against a stale inode. If so then we proceed as if
  281. * we had not found an inode in the first place. */
  282. if (inode->i_generation != generation) {
  283. mlog(0, "generation passed %u != inode generation = %u, "
  284. "ip_flags = %x, ip_blkno = %llu, msg %llu, i_count = %u, "
  285. "message type = %u\n", generation, inode->i_generation,
  286. OCFS2_I(inode)->ip_flags,
  287. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  288. (unsigned long long)blkno, atomic_read(&inode->i_count),
  289. request);
  290. iput(inode);
  291. inode = NULL;
  292. goto respond;
  293. }
  294. switch (request) {
  295. case OCFS2_VOTE_REQ_DELETE:
  296. vote_response = ocfs2_process_delete_request(inode,
  297. &orphaned_slot);
  298. break;
  299. default:
  300. mlog(ML_ERROR, "node %u, invalid request: %u\n",
  301. node_num, request);
  302. vote_response = OCFS2_RESPONSE_BAD_MSG;
  303. }
  304. respond:
  305. /* Response struture is small so we just put it on the stack
  306. * and stuff it inline. */
  307. memset(&response, 0, sizeof(struct ocfs2_response_msg));
  308. response.r_hdr.h_response_id = hdr->h_response_id;
  309. response.r_hdr.h_blkno = hdr->h_blkno;
  310. response.r_hdr.h_generation = hdr->h_generation;
  311. response.r_hdr.h_node_num = cpu_to_be32(osb->node_num);
  312. response.r_response = cpu_to_be32(vote_response);
  313. response.r_orphaned_slot = cpu_to_be32(orphaned_slot);
  314. net_status = o2net_send_message(OCFS2_MESSAGE_TYPE_RESPONSE,
  315. osb->net_key,
  316. &response,
  317. sizeof(struct ocfs2_response_msg),
  318. node_num,
  319. NULL);
  320. /* We still want to error print for ENOPROTOOPT here. The
  321. * sending node shouldn't have unregistered his net handler
  322. * without sending an unmount vote 1st */
  323. if (net_status < 0
  324. && net_status != -ETIMEDOUT
  325. && net_status != -ENOTCONN)
  326. mlog(ML_ERROR, "message to node %u fails with error %d!\n",
  327. node_num, net_status);
  328. if (inode)
  329. iput(inode);
  330. }
  331. static void ocfs2_vote_thread_do_work(struct ocfs2_super *osb)
  332. {
  333. unsigned long processed;
  334. struct ocfs2_lock_res *lockres;
  335. struct ocfs2_vote_work *work;
  336. mlog_entry_void();
  337. spin_lock(&osb->vote_task_lock);
  338. /* grab this early so we know to try again if a state change and
  339. * wake happens part-way through our work */
  340. osb->vote_work_sequence = osb->vote_wake_sequence;
  341. processed = osb->blocked_lock_count;
  342. while (processed) {
  343. BUG_ON(list_empty(&osb->blocked_lock_list));
  344. lockres = list_entry(osb->blocked_lock_list.next,
  345. struct ocfs2_lock_res, l_blocked_list);
  346. list_del_init(&lockres->l_blocked_list);
  347. osb->blocked_lock_count--;
  348. spin_unlock(&osb->vote_task_lock);
  349. BUG_ON(!processed);
  350. processed--;
  351. ocfs2_process_blocked_lock(osb, lockres);
  352. spin_lock(&osb->vote_task_lock);
  353. }
  354. while (osb->vote_count) {
  355. BUG_ON(list_empty(&osb->vote_list));
  356. work = list_entry(osb->vote_list.next,
  357. struct ocfs2_vote_work, w_list);
  358. list_del(&work->w_list);
  359. osb->vote_count--;
  360. spin_unlock(&osb->vote_task_lock);
  361. ocfs2_process_vote(osb, &work->w_msg);
  362. kfree(work);
  363. spin_lock(&osb->vote_task_lock);
  364. }
  365. spin_unlock(&osb->vote_task_lock);
  366. mlog_exit_void();
  367. }
  368. static int ocfs2_vote_thread_lists_empty(struct ocfs2_super *osb)
  369. {
  370. int empty = 0;
  371. spin_lock(&osb->vote_task_lock);
  372. if (list_empty(&osb->blocked_lock_list) &&
  373. list_empty(&osb->vote_list))
  374. empty = 1;
  375. spin_unlock(&osb->vote_task_lock);
  376. return empty;
  377. }
  378. static int ocfs2_vote_thread_should_wake(struct ocfs2_super *osb)
  379. {
  380. int should_wake = 0;
  381. spin_lock(&osb->vote_task_lock);
  382. if (osb->vote_work_sequence != osb->vote_wake_sequence)
  383. should_wake = 1;
  384. spin_unlock(&osb->vote_task_lock);
  385. return should_wake;
  386. }
  387. int ocfs2_vote_thread(void *arg)
  388. {
  389. int status = 0;
  390. struct ocfs2_super *osb = arg;
  391. /* only quit once we've been asked to stop and there is no more
  392. * work available */
  393. while (!(kthread_should_stop() &&
  394. ocfs2_vote_thread_lists_empty(osb))) {
  395. wait_event_interruptible(osb->vote_event,
  396. ocfs2_vote_thread_should_wake(osb) ||
  397. kthread_should_stop());
  398. mlog(0, "vote_thread: awoken\n");
  399. ocfs2_vote_thread_do_work(osb);
  400. }
  401. osb->vote_task = NULL;
  402. return status;
  403. }
  404. static struct ocfs2_net_wait_ctxt *ocfs2_new_net_wait_ctxt(unsigned int response_id)
  405. {
  406. struct ocfs2_net_wait_ctxt *w;
  407. w = kcalloc(1, sizeof(*w), GFP_NOFS);
  408. if (!w) {
  409. mlog_errno(-ENOMEM);
  410. goto bail;
  411. }
  412. INIT_LIST_HEAD(&w->n_list);
  413. init_waitqueue_head(&w->n_event);
  414. ocfs2_node_map_init(&w->n_node_map);
  415. w->n_response_id = response_id;
  416. w->n_callback = NULL;
  417. bail:
  418. return w;
  419. }
  420. static unsigned int ocfs2_new_response_id(struct ocfs2_super *osb)
  421. {
  422. unsigned int ret;
  423. spin_lock(&osb->net_response_lock);
  424. ret = ++osb->net_response_ids;
  425. spin_unlock(&osb->net_response_lock);
  426. return ret;
  427. }
  428. static void ocfs2_dequeue_net_wait_ctxt(struct ocfs2_super *osb,
  429. struct ocfs2_net_wait_ctxt *w)
  430. {
  431. spin_lock(&osb->net_response_lock);
  432. list_del(&w->n_list);
  433. spin_unlock(&osb->net_response_lock);
  434. }
  435. static void ocfs2_queue_net_wait_ctxt(struct ocfs2_super *osb,
  436. struct ocfs2_net_wait_ctxt *w)
  437. {
  438. spin_lock(&osb->net_response_lock);
  439. list_add_tail(&w->n_list,
  440. &osb->net_response_list);
  441. spin_unlock(&osb->net_response_lock);
  442. }
  443. static void __ocfs2_mark_node_responded(struct ocfs2_super *osb,
  444. struct ocfs2_net_wait_ctxt *w,
  445. int node_num)
  446. {
  447. assert_spin_locked(&osb->net_response_lock);
  448. ocfs2_node_map_clear_bit(osb, &w->n_node_map, node_num);
  449. if (ocfs2_node_map_is_empty(osb, &w->n_node_map))
  450. wake_up(&w->n_event);
  451. }
  452. /* Intended to be called from the node down callback, we fake remove
  453. * the node from all our response contexts */
  454. void ocfs2_remove_node_from_vote_queues(struct ocfs2_super *osb,
  455. int node_num)
  456. {
  457. struct list_head *p;
  458. struct ocfs2_net_wait_ctxt *w = NULL;
  459. spin_lock(&osb->net_response_lock);
  460. list_for_each(p, &osb->net_response_list) {
  461. w = list_entry(p, struct ocfs2_net_wait_ctxt, n_list);
  462. __ocfs2_mark_node_responded(osb, w, node_num);
  463. }
  464. spin_unlock(&osb->net_response_lock);
  465. }
  466. static int ocfs2_broadcast_vote(struct ocfs2_super *osb,
  467. struct ocfs2_vote_msg *request,
  468. unsigned int response_id,
  469. int *response,
  470. struct ocfs2_net_response_cb *callback)
  471. {
  472. int status, i, remote_err;
  473. struct ocfs2_net_wait_ctxt *w = NULL;
  474. int dequeued = 0;
  475. mlog_entry_void();
  476. w = ocfs2_new_net_wait_ctxt(response_id);
  477. if (!w) {
  478. status = -ENOMEM;
  479. mlog_errno(status);
  480. goto bail;
  481. }
  482. w->n_callback = callback;
  483. /* we're pretty much ready to go at this point, and this fills
  484. * in n_response which we need anyway... */
  485. ocfs2_queue_net_wait_ctxt(osb, w);
  486. i = ocfs2_node_map_iterate(osb, &osb->mounted_map, 0);
  487. while (i != O2NM_INVALID_NODE_NUM) {
  488. if (i != osb->node_num) {
  489. mlog(0, "trying to send request to node %i\n", i);
  490. ocfs2_node_map_set_bit(osb, &w->n_node_map, i);
  491. remote_err = 0;
  492. status = o2net_send_message(OCFS2_MESSAGE_TYPE_VOTE,
  493. osb->net_key,
  494. request,
  495. sizeof(*request),
  496. i,
  497. &remote_err);
  498. if (status == -ETIMEDOUT) {
  499. mlog(0, "remote node %d timed out!\n", i);
  500. status = -EAGAIN;
  501. goto bail;
  502. }
  503. if (remote_err < 0) {
  504. status = remote_err;
  505. mlog(0, "remote error %d on node %d!\n",
  506. remote_err, i);
  507. mlog_errno(status);
  508. goto bail;
  509. }
  510. if (status < 0) {
  511. mlog_errno(status);
  512. goto bail;
  513. }
  514. }
  515. i++;
  516. i = ocfs2_node_map_iterate(osb, &osb->mounted_map, i);
  517. mlog(0, "next is %d, i am %d\n", i, osb->node_num);
  518. }
  519. mlog(0, "done sending, now waiting on responses...\n");
  520. wait_event(w->n_event, ocfs2_node_map_is_empty(osb, &w->n_node_map));
  521. ocfs2_dequeue_net_wait_ctxt(osb, w);
  522. dequeued = 1;
  523. *response = w->n_response;
  524. status = 0;
  525. bail:
  526. if (w) {
  527. if (!dequeued)
  528. ocfs2_dequeue_net_wait_ctxt(osb, w);
  529. kfree(w);
  530. }
  531. mlog_exit(status);
  532. return status;
  533. }
  534. static struct ocfs2_vote_msg * ocfs2_new_vote_request(struct ocfs2_super *osb,
  535. u64 blkno,
  536. unsigned int generation,
  537. enum ocfs2_vote_request type,
  538. u32 priv)
  539. {
  540. struct ocfs2_vote_msg *request;
  541. struct ocfs2_msg_hdr *hdr;
  542. BUG_ON(!ocfs2_is_valid_vote_request(type));
  543. request = kcalloc(1, sizeof(*request), GFP_NOFS);
  544. if (!request) {
  545. mlog_errno(-ENOMEM);
  546. } else {
  547. hdr = &request->v_hdr;
  548. hdr->h_node_num = cpu_to_be32(osb->node_num);
  549. hdr->h_request = cpu_to_be32(type);
  550. hdr->h_blkno = cpu_to_be64(blkno);
  551. hdr->h_generation = cpu_to_be32(generation);
  552. request->md1.v_generic1 = cpu_to_be32(priv);
  553. }
  554. return request;
  555. }
  556. /* Complete the buildup of a new vote request and process the
  557. * broadcast return value. */
  558. static int ocfs2_do_request_vote(struct ocfs2_super *osb,
  559. struct ocfs2_vote_msg *request,
  560. struct ocfs2_net_response_cb *callback)
  561. {
  562. int status, response;
  563. unsigned int response_id;
  564. struct ocfs2_msg_hdr *hdr;
  565. response_id = ocfs2_new_response_id(osb);
  566. hdr = &request->v_hdr;
  567. hdr->h_response_id = cpu_to_be32(response_id);
  568. status = ocfs2_broadcast_vote(osb, request, response_id, &response,
  569. callback);
  570. if (status < 0) {
  571. mlog_errno(status);
  572. goto bail;
  573. }
  574. status = response;
  575. bail:
  576. return status;
  577. }
  578. static int ocfs2_request_vote(struct inode *inode,
  579. struct ocfs2_vote_msg *request,
  580. struct ocfs2_net_response_cb *callback)
  581. {
  582. int status;
  583. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  584. if (ocfs2_inode_is_new(inode))
  585. return 0;
  586. status = -EAGAIN;
  587. while (status == -EAGAIN) {
  588. if (!(osb->s_mount_opt & OCFS2_MOUNT_NOINTR) &&
  589. signal_pending(current))
  590. return -ERESTARTSYS;
  591. status = ocfs2_super_lock(osb, 0);
  592. if (status < 0) {
  593. mlog_errno(status);
  594. break;
  595. }
  596. status = 0;
  597. if (!ocfs2_node_map_is_only(osb, &osb->mounted_map,
  598. osb->node_num))
  599. status = ocfs2_do_request_vote(osb, request, callback);
  600. ocfs2_super_unlock(osb, 0);
  601. }
  602. return status;
  603. }
  604. static void ocfs2_delete_response_cb(void *priv,
  605. struct ocfs2_response_msg *resp)
  606. {
  607. int orphaned_slot, node;
  608. struct inode *inode = priv;
  609. orphaned_slot = be32_to_cpu(resp->r_orphaned_slot);
  610. node = be32_to_cpu(resp->r_hdr.h_node_num);
  611. mlog(0, "node %d tells us that inode %llu is orphaned in slot %d\n",
  612. node, (unsigned long long)OCFS2_I(inode)->ip_blkno,
  613. orphaned_slot);
  614. /* The other node may not actually know which slot the inode
  615. * is orphaned in. */
  616. if (orphaned_slot == OCFS2_INVALID_SLOT)
  617. return;
  618. /* Ok, the responding node knows which slot this inode is
  619. * orphaned in. We verify that the information is correct and
  620. * then record this in the inode. ocfs2_delete_inode will use
  621. * this information to determine which lock to take. */
  622. spin_lock(&OCFS2_I(inode)->ip_lock);
  623. mlog_bug_on_msg(OCFS2_I(inode)->ip_orphaned_slot != orphaned_slot &&
  624. OCFS2_I(inode)->ip_orphaned_slot
  625. != OCFS2_INVALID_SLOT, "Inode %llu: Node %d says it's "
  626. "orphaned in slot %d, we think it's in %d\n",
  627. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  628. be32_to_cpu(resp->r_hdr.h_node_num),
  629. orphaned_slot, OCFS2_I(inode)->ip_orphaned_slot);
  630. OCFS2_I(inode)->ip_orphaned_slot = orphaned_slot;
  631. spin_unlock(&OCFS2_I(inode)->ip_lock);
  632. }
  633. int ocfs2_request_delete_vote(struct inode *inode)
  634. {
  635. int orphaned_slot, status;
  636. struct ocfs2_net_response_cb delete_cb;
  637. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  638. struct ocfs2_vote_msg *request;
  639. spin_lock(&OCFS2_I(inode)->ip_lock);
  640. orphaned_slot = OCFS2_I(inode)->ip_orphaned_slot;
  641. spin_unlock(&OCFS2_I(inode)->ip_lock);
  642. delete_cb.rc_cb = ocfs2_delete_response_cb;
  643. delete_cb.rc_priv = inode;
  644. mlog(0, "Inode %llu, we start thinking orphaned slot is %d\n",
  645. (unsigned long long)OCFS2_I(inode)->ip_blkno, orphaned_slot);
  646. status = -ENOMEM;
  647. request = ocfs2_new_vote_request(osb, OCFS2_I(inode)->ip_blkno,
  648. inode->i_generation,
  649. OCFS2_VOTE_REQ_DELETE, orphaned_slot);
  650. if (request) {
  651. status = ocfs2_request_vote(inode, request, &delete_cb);
  652. kfree(request);
  653. }
  654. return status;
  655. }
  656. int ocfs2_request_mount_vote(struct ocfs2_super *osb)
  657. {
  658. int status;
  659. struct ocfs2_vote_msg *request = NULL;
  660. request = ocfs2_new_vote_request(osb, 0ULL, 0,
  661. OCFS2_VOTE_REQ_MOUNT, 0);
  662. if (!request) {
  663. status = -ENOMEM;
  664. goto bail;
  665. }
  666. status = -EAGAIN;
  667. while (status == -EAGAIN) {
  668. if (!(osb->s_mount_opt & OCFS2_MOUNT_NOINTR) &&
  669. signal_pending(current)) {
  670. status = -ERESTARTSYS;
  671. goto bail;
  672. }
  673. if (ocfs2_node_map_is_only(osb, &osb->mounted_map,
  674. osb->node_num)) {
  675. status = 0;
  676. goto bail;
  677. }
  678. status = ocfs2_do_request_vote(osb, request, NULL);
  679. }
  680. bail:
  681. kfree(request);
  682. return status;
  683. }
  684. int ocfs2_request_umount_vote(struct ocfs2_super *osb)
  685. {
  686. int status;
  687. struct ocfs2_vote_msg *request = NULL;
  688. request = ocfs2_new_vote_request(osb, 0ULL, 0,
  689. OCFS2_VOTE_REQ_UMOUNT, 0);
  690. if (!request) {
  691. status = -ENOMEM;
  692. goto bail;
  693. }
  694. status = -EAGAIN;
  695. while (status == -EAGAIN) {
  696. /* Do not check signals on this vote... We really want
  697. * this one to go all the way through. */
  698. if (ocfs2_node_map_is_only(osb, &osb->mounted_map,
  699. osb->node_num)) {
  700. status = 0;
  701. goto bail;
  702. }
  703. status = ocfs2_do_request_vote(osb, request, NULL);
  704. }
  705. bail:
  706. kfree(request);
  707. return status;
  708. }
  709. /* TODO: This should eventually be a hash table! */
  710. static struct ocfs2_net_wait_ctxt * __ocfs2_find_net_wait_ctxt(struct ocfs2_super *osb,
  711. u32 response_id)
  712. {
  713. struct list_head *p;
  714. struct ocfs2_net_wait_ctxt *w = NULL;
  715. list_for_each(p, &osb->net_response_list) {
  716. w = list_entry(p, struct ocfs2_net_wait_ctxt, n_list);
  717. if (response_id == w->n_response_id)
  718. break;
  719. w = NULL;
  720. }
  721. return w;
  722. }
  723. /* Translate response codes into local node errno values */
  724. static inline int ocfs2_translate_response(int response)
  725. {
  726. int ret;
  727. switch (response) {
  728. case OCFS2_RESPONSE_OK:
  729. ret = 0;
  730. break;
  731. case OCFS2_RESPONSE_BUSY:
  732. ret = -EBUSY;
  733. break;
  734. default:
  735. ret = -EINVAL;
  736. }
  737. return ret;
  738. }
  739. static int ocfs2_handle_response_message(struct o2net_msg *msg,
  740. u32 len,
  741. void *data)
  742. {
  743. unsigned int response_id, node_num;
  744. int response_status;
  745. struct ocfs2_super *osb = data;
  746. struct ocfs2_response_msg *resp;
  747. struct ocfs2_net_wait_ctxt * w;
  748. struct ocfs2_net_response_cb *resp_cb;
  749. resp = (struct ocfs2_response_msg *) msg->buf;
  750. response_id = be32_to_cpu(resp->r_hdr.h_response_id);
  751. node_num = be32_to_cpu(resp->r_hdr.h_node_num);
  752. response_status =
  753. ocfs2_translate_response(be32_to_cpu(resp->r_response));
  754. mlog(0, "received response message:\n");
  755. mlog(0, "h_response_id = %u\n", response_id);
  756. mlog(0, "h_request = %u\n", be32_to_cpu(resp->r_hdr.h_request));
  757. mlog(0, "h_blkno = %llu\n",
  758. (unsigned long long)be64_to_cpu(resp->r_hdr.h_blkno));
  759. mlog(0, "h_generation = %u\n", be32_to_cpu(resp->r_hdr.h_generation));
  760. mlog(0, "h_node_num = %u\n", node_num);
  761. mlog(0, "r_response = %d\n", response_status);
  762. spin_lock(&osb->net_response_lock);
  763. w = __ocfs2_find_net_wait_ctxt(osb, response_id);
  764. if (!w) {
  765. mlog(0, "request not found!\n");
  766. goto bail;
  767. }
  768. resp_cb = w->n_callback;
  769. if (response_status && (!w->n_response)) {
  770. /* we only really need one negative response so don't
  771. * set it twice. */
  772. w->n_response = response_status;
  773. }
  774. if (resp_cb) {
  775. spin_unlock(&osb->net_response_lock);
  776. resp_cb->rc_cb(resp_cb->rc_priv, resp);
  777. spin_lock(&osb->net_response_lock);
  778. }
  779. __ocfs2_mark_node_responded(osb, w, node_num);
  780. bail:
  781. spin_unlock(&osb->net_response_lock);
  782. return 0;
  783. }
  784. static int ocfs2_handle_vote_message(struct o2net_msg *msg,
  785. u32 len,
  786. void *data)
  787. {
  788. int status;
  789. struct ocfs2_super *osb = data;
  790. struct ocfs2_vote_work *work;
  791. work = kmalloc(sizeof(struct ocfs2_vote_work), GFP_NOFS);
  792. if (!work) {
  793. status = -ENOMEM;
  794. mlog_errno(status);
  795. goto bail;
  796. }
  797. INIT_LIST_HEAD(&work->w_list);
  798. memcpy(&work->w_msg, msg->buf, sizeof(struct ocfs2_vote_msg));
  799. mlog(0, "scheduling vote request:\n");
  800. mlog(0, "h_response_id = %u\n",
  801. be32_to_cpu(work->w_msg.v_hdr.h_response_id));
  802. mlog(0, "h_request = %u\n", be32_to_cpu(work->w_msg.v_hdr.h_request));
  803. mlog(0, "h_blkno = %llu\n",
  804. (unsigned long long)be64_to_cpu(work->w_msg.v_hdr.h_blkno));
  805. mlog(0, "h_generation = %u\n",
  806. be32_to_cpu(work->w_msg.v_hdr.h_generation));
  807. mlog(0, "h_node_num = %u\n",
  808. be32_to_cpu(work->w_msg.v_hdr.h_node_num));
  809. mlog(0, "v_generic1 = %u\n", be32_to_cpu(work->w_msg.md1.v_generic1));
  810. spin_lock(&osb->vote_task_lock);
  811. list_add_tail(&work->w_list, &osb->vote_list);
  812. osb->vote_count++;
  813. spin_unlock(&osb->vote_task_lock);
  814. ocfs2_kick_vote_thread(osb);
  815. status = 0;
  816. bail:
  817. return status;
  818. }
  819. void ocfs2_unregister_net_handlers(struct ocfs2_super *osb)
  820. {
  821. if (!osb->net_key)
  822. return;
  823. o2net_unregister_handler_list(&osb->osb_net_handlers);
  824. if (!list_empty(&osb->net_response_list))
  825. mlog(ML_ERROR, "net response list not empty!\n");
  826. osb->net_key = 0;
  827. }
  828. int ocfs2_register_net_handlers(struct ocfs2_super *osb)
  829. {
  830. int status = 0;
  831. status = o2net_register_handler(OCFS2_MESSAGE_TYPE_RESPONSE,
  832. osb->net_key,
  833. sizeof(struct ocfs2_response_msg),
  834. ocfs2_handle_response_message,
  835. osb, &osb->osb_net_handlers);
  836. if (status) {
  837. mlog_errno(status);
  838. goto bail;
  839. }
  840. status = o2net_register_handler(OCFS2_MESSAGE_TYPE_VOTE,
  841. osb->net_key,
  842. sizeof(struct ocfs2_vote_msg),
  843. ocfs2_handle_vote_message,
  844. osb, &osb->osb_net_handlers);
  845. if (status) {
  846. mlog_errno(status);
  847. goto bail;
  848. }
  849. bail:
  850. if (status < 0)
  851. ocfs2_unregister_net_handlers(osb);
  852. return status;
  853. }