vote.c 31 KB

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