vote.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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_KERNEL);
  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_KERNEL);
  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. if (request)
  826. kfree(request);
  827. return status;
  828. }
  829. int ocfs2_request_umount_vote(struct ocfs2_super *osb)
  830. {
  831. int status;
  832. struct ocfs2_vote_msg *request = NULL;
  833. request = ocfs2_new_vote_request(osb, 0ULL, 0,
  834. OCFS2_VOTE_REQ_UMOUNT, 0);
  835. if (!request) {
  836. status = -ENOMEM;
  837. goto bail;
  838. }
  839. status = -EAGAIN;
  840. while (status == -EAGAIN) {
  841. /* Do not check signals on this vote... We really want
  842. * this one to go all the way through. */
  843. if (ocfs2_node_map_is_only(osb, &osb->mounted_map,
  844. osb->node_num)) {
  845. status = 0;
  846. goto bail;
  847. }
  848. status = ocfs2_do_request_vote(osb, request, NULL);
  849. }
  850. bail:
  851. if (request)
  852. kfree(request);
  853. return status;
  854. }
  855. /* TODO: This should eventually be a hash table! */
  856. static struct ocfs2_net_wait_ctxt * __ocfs2_find_net_wait_ctxt(struct ocfs2_super *osb,
  857. u32 response_id)
  858. {
  859. struct list_head *p;
  860. struct ocfs2_net_wait_ctxt *w = NULL;
  861. list_for_each(p, &osb->net_response_list) {
  862. w = list_entry(p, struct ocfs2_net_wait_ctxt, n_list);
  863. if (response_id == w->n_response_id)
  864. break;
  865. w = NULL;
  866. }
  867. return w;
  868. }
  869. /* Translate response codes into local node errno values */
  870. static inline int ocfs2_translate_response(int response)
  871. {
  872. int ret;
  873. switch (response) {
  874. case OCFS2_RESPONSE_OK:
  875. ret = 0;
  876. break;
  877. case OCFS2_RESPONSE_BUSY:
  878. ret = -EBUSY;
  879. break;
  880. default:
  881. ret = -EINVAL;
  882. }
  883. return ret;
  884. }
  885. static int ocfs2_handle_response_message(struct o2net_msg *msg,
  886. u32 len,
  887. void *data)
  888. {
  889. unsigned int response_id, node_num;
  890. int response_status;
  891. struct ocfs2_super *osb = data;
  892. struct ocfs2_response_msg *resp;
  893. struct ocfs2_net_wait_ctxt * w;
  894. struct ocfs2_net_response_cb *resp_cb;
  895. resp = (struct ocfs2_response_msg *) msg->buf;
  896. response_id = be32_to_cpu(resp->r_hdr.h_response_id);
  897. node_num = be32_to_cpu(resp->r_hdr.h_node_num);
  898. response_status =
  899. ocfs2_translate_response(be32_to_cpu(resp->r_response));
  900. mlog(0, "received response message:\n");
  901. mlog(0, "h_response_id = %u\n", response_id);
  902. mlog(0, "h_request = %u\n", be32_to_cpu(resp->r_hdr.h_request));
  903. mlog(0, "h_blkno = %llu\n",
  904. (unsigned long long)be64_to_cpu(resp->r_hdr.h_blkno));
  905. mlog(0, "h_generation = %u\n", be32_to_cpu(resp->r_hdr.h_generation));
  906. mlog(0, "h_node_num = %u\n", node_num);
  907. mlog(0, "r_response = %d\n", response_status);
  908. spin_lock(&osb->net_response_lock);
  909. w = __ocfs2_find_net_wait_ctxt(osb, response_id);
  910. if (!w) {
  911. mlog(0, "request not found!\n");
  912. goto bail;
  913. }
  914. resp_cb = w->n_callback;
  915. if (response_status && (!w->n_response)) {
  916. /* we only really need one negative response so don't
  917. * set it twice. */
  918. w->n_response = response_status;
  919. }
  920. if (resp_cb) {
  921. spin_unlock(&osb->net_response_lock);
  922. resp_cb->rc_cb(resp_cb->rc_priv, resp);
  923. spin_lock(&osb->net_response_lock);
  924. }
  925. __ocfs2_mark_node_responded(osb, w, node_num);
  926. bail:
  927. spin_unlock(&osb->net_response_lock);
  928. return 0;
  929. }
  930. static int ocfs2_handle_vote_message(struct o2net_msg *msg,
  931. u32 len,
  932. void *data)
  933. {
  934. int status;
  935. struct ocfs2_super *osb = data;
  936. struct ocfs2_vote_work *work;
  937. work = kmalloc(sizeof(struct ocfs2_vote_work), GFP_KERNEL);
  938. if (!work) {
  939. status = -ENOMEM;
  940. mlog_errno(status);
  941. goto bail;
  942. }
  943. INIT_LIST_HEAD(&work->w_list);
  944. memcpy(&work->w_msg, msg->buf, sizeof(struct ocfs2_vote_msg));
  945. mlog(0, "scheduling vote request:\n");
  946. mlog(0, "h_response_id = %u\n",
  947. be32_to_cpu(work->w_msg.v_hdr.h_response_id));
  948. mlog(0, "h_request = %u\n", be32_to_cpu(work->w_msg.v_hdr.h_request));
  949. mlog(0, "h_blkno = %llu\n",
  950. (unsigned long long)be64_to_cpu(work->w_msg.v_hdr.h_blkno));
  951. mlog(0, "h_generation = %u\n",
  952. be32_to_cpu(work->w_msg.v_hdr.h_generation));
  953. mlog(0, "h_node_num = %u\n",
  954. be32_to_cpu(work->w_msg.v_hdr.h_node_num));
  955. mlog(0, "v_generic1 = %u\n", be32_to_cpu(work->w_msg.md1.v_generic1));
  956. spin_lock(&osb->vote_task_lock);
  957. list_add_tail(&work->w_list, &osb->vote_list);
  958. osb->vote_count++;
  959. spin_unlock(&osb->vote_task_lock);
  960. ocfs2_kick_vote_thread(osb);
  961. status = 0;
  962. bail:
  963. return status;
  964. }
  965. void ocfs2_unregister_net_handlers(struct ocfs2_super *osb)
  966. {
  967. if (!osb->net_key)
  968. return;
  969. o2net_unregister_handler_list(&osb->osb_net_handlers);
  970. if (!list_empty(&osb->net_response_list))
  971. mlog(ML_ERROR, "net response list not empty!\n");
  972. osb->net_key = 0;
  973. }
  974. int ocfs2_register_net_handlers(struct ocfs2_super *osb)
  975. {
  976. int status = 0;
  977. status = o2net_register_handler(OCFS2_MESSAGE_TYPE_RESPONSE,
  978. osb->net_key,
  979. sizeof(struct ocfs2_response_msg),
  980. ocfs2_handle_response_message,
  981. osb, &osb->osb_net_handlers);
  982. if (status) {
  983. mlog_errno(status);
  984. goto bail;
  985. }
  986. status = o2net_register_handler(OCFS2_MESSAGE_TYPE_VOTE,
  987. osb->net_key,
  988. sizeof(struct ocfs2_vote_msg),
  989. ocfs2_handle_vote_message,
  990. osb, &osb->osb_net_handlers);
  991. if (status) {
  992. mlog_errno(status);
  993. goto bail;
  994. }
  995. bail:
  996. if (status < 0)
  997. ocfs2_unregister_net_handlers(osb);
  998. return status;
  999. }