vote.c 31 KB

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