journal.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * journal.c
  5. *
  6. * Defines functions of journalling api
  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/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/kthread.h>
  30. #define MLOG_MASK_PREFIX ML_JOURNAL
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "dir.h"
  35. #include "dlmglue.h"
  36. #include "extent_map.h"
  37. #include "heartbeat.h"
  38. #include "inode.h"
  39. #include "journal.h"
  40. #include "localalloc.h"
  41. #include "slot_map.h"
  42. #include "super.h"
  43. #include "sysfile.h"
  44. #include "buffer_head_io.h"
  45. DEFINE_SPINLOCK(trans_inc_lock);
  46. static int ocfs2_force_read_journal(struct inode *inode);
  47. static int ocfs2_recover_node(struct ocfs2_super *osb,
  48. int node_num);
  49. static int __ocfs2_recovery_thread(void *arg);
  50. static int ocfs2_commit_cache(struct ocfs2_super *osb);
  51. static int ocfs2_wait_on_mount(struct ocfs2_super *osb);
  52. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  53. int dirty, int replayed);
  54. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  55. int slot_num);
  56. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  57. int slot);
  58. static int ocfs2_commit_thread(void *arg);
  59. /*
  60. * The recovery_list is a simple linked list of node numbers to recover.
  61. * It is protected by the recovery_lock.
  62. */
  63. struct ocfs2_recovery_map {
  64. unsigned int rm_used;
  65. unsigned int *rm_entries;
  66. };
  67. int ocfs2_recovery_init(struct ocfs2_super *osb)
  68. {
  69. struct ocfs2_recovery_map *rm;
  70. mutex_init(&osb->recovery_lock);
  71. osb->disable_recovery = 0;
  72. osb->recovery_thread_task = NULL;
  73. init_waitqueue_head(&osb->recovery_event);
  74. rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
  75. osb->max_slots * sizeof(unsigned int),
  76. GFP_KERNEL);
  77. if (!rm) {
  78. mlog_errno(-ENOMEM);
  79. return -ENOMEM;
  80. }
  81. rm->rm_entries = (unsigned int *)((char *)rm +
  82. sizeof(struct ocfs2_recovery_map));
  83. osb->recovery_map = rm;
  84. return 0;
  85. }
  86. /* we can't grab the goofy sem lock from inside wait_event, so we use
  87. * memory barriers to make sure that we'll see the null task before
  88. * being woken up */
  89. static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
  90. {
  91. mb();
  92. return osb->recovery_thread_task != NULL;
  93. }
  94. void ocfs2_recovery_exit(struct ocfs2_super *osb)
  95. {
  96. struct ocfs2_recovery_map *rm;
  97. /* disable any new recovery threads and wait for any currently
  98. * running ones to exit. Do this before setting the vol_state. */
  99. mutex_lock(&osb->recovery_lock);
  100. osb->disable_recovery = 1;
  101. mutex_unlock(&osb->recovery_lock);
  102. wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
  103. /* At this point, we know that no more recovery threads can be
  104. * launched, so wait for any recovery completion work to
  105. * complete. */
  106. flush_workqueue(ocfs2_wq);
  107. /*
  108. * Now that recovery is shut down, and the osb is about to be
  109. * freed, the osb_lock is not taken here.
  110. */
  111. rm = osb->recovery_map;
  112. /* XXX: Should we bug if there are dirty entries? */
  113. kfree(rm);
  114. }
  115. static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
  116. unsigned int node_num)
  117. {
  118. int i;
  119. struct ocfs2_recovery_map *rm = osb->recovery_map;
  120. assert_spin_locked(&osb->osb_lock);
  121. for (i = 0; i < rm->rm_used; i++) {
  122. if (rm->rm_entries[i] == node_num)
  123. return 1;
  124. }
  125. return 0;
  126. }
  127. /* Behaves like test-and-set. Returns the previous value */
  128. static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
  129. unsigned int node_num)
  130. {
  131. struct ocfs2_recovery_map *rm = osb->recovery_map;
  132. spin_lock(&osb->osb_lock);
  133. if (__ocfs2_recovery_map_test(osb, node_num)) {
  134. spin_unlock(&osb->osb_lock);
  135. return 1;
  136. }
  137. /* XXX: Can this be exploited? Not from o2dlm... */
  138. BUG_ON(rm->rm_used >= osb->max_slots);
  139. rm->rm_entries[rm->rm_used] = node_num;
  140. rm->rm_used++;
  141. spin_unlock(&osb->osb_lock);
  142. return 0;
  143. }
  144. static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
  145. unsigned int node_num)
  146. {
  147. int i;
  148. struct ocfs2_recovery_map *rm = osb->recovery_map;
  149. spin_lock(&osb->osb_lock);
  150. for (i = 0; i < rm->rm_used; i++) {
  151. if (rm->rm_entries[i] == node_num)
  152. break;
  153. }
  154. if (i < rm->rm_used) {
  155. /* XXX: be careful with the pointer math */
  156. memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
  157. (rm->rm_used - i - 1) * sizeof(unsigned int));
  158. rm->rm_used--;
  159. }
  160. spin_unlock(&osb->osb_lock);
  161. }
  162. static int ocfs2_commit_cache(struct ocfs2_super *osb)
  163. {
  164. int status = 0;
  165. unsigned int flushed;
  166. unsigned long old_id;
  167. struct ocfs2_journal *journal = NULL;
  168. mlog_entry_void();
  169. journal = osb->journal;
  170. /* Flush all pending commits and checkpoint the journal. */
  171. down_write(&journal->j_trans_barrier);
  172. if (atomic_read(&journal->j_num_trans) == 0) {
  173. up_write(&journal->j_trans_barrier);
  174. mlog(0, "No transactions for me to flush!\n");
  175. goto finally;
  176. }
  177. jbd2_journal_lock_updates(journal->j_journal);
  178. status = jbd2_journal_flush(journal->j_journal);
  179. jbd2_journal_unlock_updates(journal->j_journal);
  180. if (status < 0) {
  181. up_write(&journal->j_trans_barrier);
  182. mlog_errno(status);
  183. goto finally;
  184. }
  185. old_id = ocfs2_inc_trans_id(journal);
  186. flushed = atomic_read(&journal->j_num_trans);
  187. atomic_set(&journal->j_num_trans, 0);
  188. up_write(&journal->j_trans_barrier);
  189. mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
  190. journal->j_trans_id, flushed);
  191. ocfs2_wake_downconvert_thread(osb);
  192. wake_up(&journal->j_checkpointed);
  193. finally:
  194. mlog_exit(status);
  195. return status;
  196. }
  197. /* pass it NULL and it will allocate a new handle object for you. If
  198. * you pass it a handle however, it may still return error, in which
  199. * case it has free'd the passed handle for you. */
  200. handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
  201. {
  202. journal_t *journal = osb->journal->j_journal;
  203. handle_t *handle;
  204. BUG_ON(!osb || !osb->journal->j_journal);
  205. if (ocfs2_is_hard_readonly(osb))
  206. return ERR_PTR(-EROFS);
  207. BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
  208. BUG_ON(max_buffs <= 0);
  209. /* JBD might support this, but our journalling code doesn't yet. */
  210. if (journal_current_handle()) {
  211. mlog(ML_ERROR, "Recursive transaction attempted!\n");
  212. BUG();
  213. }
  214. down_read(&osb->journal->j_trans_barrier);
  215. handle = jbd2_journal_start(journal, max_buffs);
  216. if (IS_ERR(handle)) {
  217. up_read(&osb->journal->j_trans_barrier);
  218. mlog_errno(PTR_ERR(handle));
  219. if (is_journal_aborted(journal)) {
  220. ocfs2_abort(osb->sb, "Detected aborted journal");
  221. handle = ERR_PTR(-EROFS);
  222. }
  223. } else {
  224. if (!ocfs2_mount_local(osb))
  225. atomic_inc(&(osb->journal->j_num_trans));
  226. }
  227. return handle;
  228. }
  229. int ocfs2_commit_trans(struct ocfs2_super *osb,
  230. handle_t *handle)
  231. {
  232. int ret;
  233. struct ocfs2_journal *journal = osb->journal;
  234. BUG_ON(!handle);
  235. ret = jbd2_journal_stop(handle);
  236. if (ret < 0)
  237. mlog_errno(ret);
  238. up_read(&journal->j_trans_barrier);
  239. return ret;
  240. }
  241. /*
  242. * 'nblocks' is what you want to add to the current
  243. * transaction. extend_trans will either extend the current handle by
  244. * nblocks, or commit it and start a new one with nblocks credits.
  245. *
  246. * This might call jbd2_journal_restart() which will commit dirty buffers
  247. * and then restart the transaction. Before calling
  248. * ocfs2_extend_trans(), any changed blocks should have been
  249. * dirtied. After calling it, all blocks which need to be changed must
  250. * go through another set of journal_access/journal_dirty calls.
  251. *
  252. * WARNING: This will not release any semaphores or disk locks taken
  253. * during the transaction, so make sure they were taken *before*
  254. * start_trans or we'll have ordering deadlocks.
  255. *
  256. * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
  257. * good because transaction ids haven't yet been recorded on the
  258. * cluster locks associated with this handle.
  259. */
  260. int ocfs2_extend_trans(handle_t *handle, int nblocks)
  261. {
  262. int status;
  263. BUG_ON(!handle);
  264. BUG_ON(!nblocks);
  265. mlog_entry_void();
  266. mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
  267. #ifdef CONFIG_OCFS2_DEBUG_FS
  268. status = 1;
  269. #else
  270. status = jbd2_journal_extend(handle, nblocks);
  271. if (status < 0) {
  272. mlog_errno(status);
  273. goto bail;
  274. }
  275. #endif
  276. if (status > 0) {
  277. mlog(0,
  278. "jbd2_journal_extend failed, trying "
  279. "jbd2_journal_restart\n");
  280. status = jbd2_journal_restart(handle, nblocks);
  281. if (status < 0) {
  282. mlog_errno(status);
  283. goto bail;
  284. }
  285. }
  286. status = 0;
  287. bail:
  288. mlog_exit(status);
  289. return status;
  290. }
  291. int ocfs2_journal_access(handle_t *handle,
  292. struct inode *inode,
  293. struct buffer_head *bh,
  294. int type)
  295. {
  296. int status;
  297. BUG_ON(!inode);
  298. BUG_ON(!handle);
  299. BUG_ON(!bh);
  300. mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
  301. (unsigned long long)bh->b_blocknr, type,
  302. (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
  303. "OCFS2_JOURNAL_ACCESS_CREATE" :
  304. "OCFS2_JOURNAL_ACCESS_WRITE",
  305. bh->b_size);
  306. /* we can safely remove this assertion after testing. */
  307. if (!buffer_uptodate(bh)) {
  308. mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
  309. mlog(ML_ERROR, "b_blocknr=%llu\n",
  310. (unsigned long long)bh->b_blocknr);
  311. BUG();
  312. }
  313. /* Set the current transaction information on the inode so
  314. * that the locking code knows whether it can drop it's locks
  315. * on this inode or not. We're protected from the commit
  316. * thread updating the current transaction id until
  317. * ocfs2_commit_trans() because ocfs2_start_trans() took
  318. * j_trans_barrier for us. */
  319. ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
  320. mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
  321. switch (type) {
  322. case OCFS2_JOURNAL_ACCESS_CREATE:
  323. case OCFS2_JOURNAL_ACCESS_WRITE:
  324. status = jbd2_journal_get_write_access(handle, bh);
  325. break;
  326. case OCFS2_JOURNAL_ACCESS_UNDO:
  327. status = jbd2_journal_get_undo_access(handle, bh);
  328. break;
  329. default:
  330. status = -EINVAL;
  331. mlog(ML_ERROR, "Uknown access type!\n");
  332. }
  333. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  334. if (status < 0)
  335. mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
  336. status, type);
  337. mlog_exit(status);
  338. return status;
  339. }
  340. int ocfs2_journal_dirty(handle_t *handle,
  341. struct buffer_head *bh)
  342. {
  343. int status;
  344. mlog_entry("(bh->b_blocknr=%llu)\n",
  345. (unsigned long long)bh->b_blocknr);
  346. status = jbd2_journal_dirty_metadata(handle, bh);
  347. if (status < 0)
  348. mlog(ML_ERROR, "Could not dirty metadata buffer. "
  349. "(bh->b_blocknr=%llu)\n",
  350. (unsigned long long)bh->b_blocknr);
  351. mlog_exit(status);
  352. return status;
  353. }
  354. #ifdef CONFIG_OCFS2_COMPAT_JBD
  355. int ocfs2_journal_dirty_data(handle_t *handle,
  356. struct buffer_head *bh)
  357. {
  358. int err = journal_dirty_data(handle, bh);
  359. if (err)
  360. mlog_errno(err);
  361. /* TODO: When we can handle it, abort the handle and go RO on
  362. * error here. */
  363. return err;
  364. }
  365. #endif
  366. #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
  367. void ocfs2_set_journal_params(struct ocfs2_super *osb)
  368. {
  369. journal_t *journal = osb->journal->j_journal;
  370. unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
  371. if (osb->osb_commit_interval)
  372. commit_interval = osb->osb_commit_interval;
  373. spin_lock(&journal->j_state_lock);
  374. journal->j_commit_interval = commit_interval;
  375. if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
  376. journal->j_flags |= JBD2_BARRIER;
  377. else
  378. journal->j_flags &= ~JBD2_BARRIER;
  379. spin_unlock(&journal->j_state_lock);
  380. }
  381. int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
  382. {
  383. int status = -1;
  384. struct inode *inode = NULL; /* the journal inode */
  385. journal_t *j_journal = NULL;
  386. struct ocfs2_dinode *di = NULL;
  387. struct buffer_head *bh = NULL;
  388. struct ocfs2_super *osb;
  389. int inode_lock = 0;
  390. mlog_entry_void();
  391. BUG_ON(!journal);
  392. osb = journal->j_osb;
  393. /* already have the inode for our journal */
  394. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  395. osb->slot_num);
  396. if (inode == NULL) {
  397. status = -EACCES;
  398. mlog_errno(status);
  399. goto done;
  400. }
  401. if (is_bad_inode(inode)) {
  402. mlog(ML_ERROR, "access error (bad inode)\n");
  403. iput(inode);
  404. inode = NULL;
  405. status = -EACCES;
  406. goto done;
  407. }
  408. SET_INODE_JOURNAL(inode);
  409. OCFS2_I(inode)->ip_open_count++;
  410. /* Skip recovery waits here - journal inode metadata never
  411. * changes in a live cluster so it can be considered an
  412. * exception to the rule. */
  413. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  414. if (status < 0) {
  415. if (status != -ERESTARTSYS)
  416. mlog(ML_ERROR, "Could not get lock on journal!\n");
  417. goto done;
  418. }
  419. inode_lock = 1;
  420. di = (struct ocfs2_dinode *)bh->b_data;
  421. if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
  422. mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
  423. inode->i_size);
  424. status = -EINVAL;
  425. goto done;
  426. }
  427. mlog(0, "inode->i_size = %lld\n", inode->i_size);
  428. mlog(0, "inode->i_blocks = %llu\n",
  429. (unsigned long long)inode->i_blocks);
  430. mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
  431. /* call the kernels journal init function now */
  432. j_journal = jbd2_journal_init_inode(inode);
  433. if (j_journal == NULL) {
  434. mlog(ML_ERROR, "Linux journal layer error\n");
  435. status = -EINVAL;
  436. goto done;
  437. }
  438. mlog(0, "Returned from jbd2_journal_init_inode\n");
  439. mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
  440. *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
  441. OCFS2_JOURNAL_DIRTY_FL);
  442. journal->j_journal = j_journal;
  443. journal->j_inode = inode;
  444. journal->j_bh = bh;
  445. ocfs2_set_journal_params(osb);
  446. journal->j_state = OCFS2_JOURNAL_LOADED;
  447. status = 0;
  448. done:
  449. if (status < 0) {
  450. if (inode_lock)
  451. ocfs2_inode_unlock(inode, 1);
  452. brelse(bh);
  453. if (inode) {
  454. OCFS2_I(inode)->ip_open_count--;
  455. iput(inode);
  456. }
  457. }
  458. mlog_exit(status);
  459. return status;
  460. }
  461. static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
  462. {
  463. le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
  464. }
  465. static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
  466. {
  467. return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
  468. }
  469. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  470. int dirty, int replayed)
  471. {
  472. int status;
  473. unsigned int flags;
  474. struct ocfs2_journal *journal = osb->journal;
  475. struct buffer_head *bh = journal->j_bh;
  476. struct ocfs2_dinode *fe;
  477. mlog_entry_void();
  478. fe = (struct ocfs2_dinode *)bh->b_data;
  479. if (!OCFS2_IS_VALID_DINODE(fe)) {
  480. /* This is called from startup/shutdown which will
  481. * handle the errors in a specific manner, so no need
  482. * to call ocfs2_error() here. */
  483. mlog(ML_ERROR, "Journal dinode %llu has invalid "
  484. "signature: %.*s",
  485. (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
  486. fe->i_signature);
  487. status = -EIO;
  488. goto out;
  489. }
  490. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  491. if (dirty)
  492. flags |= OCFS2_JOURNAL_DIRTY_FL;
  493. else
  494. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  495. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  496. if (replayed)
  497. ocfs2_bump_recovery_generation(fe);
  498. status = ocfs2_write_block(osb, bh, journal->j_inode);
  499. if (status < 0)
  500. mlog_errno(status);
  501. out:
  502. mlog_exit(status);
  503. return status;
  504. }
  505. /*
  506. * If the journal has been kmalloc'd it needs to be freed after this
  507. * call.
  508. */
  509. void ocfs2_journal_shutdown(struct ocfs2_super *osb)
  510. {
  511. struct ocfs2_journal *journal = NULL;
  512. int status = 0;
  513. struct inode *inode = NULL;
  514. int num_running_trans = 0;
  515. mlog_entry_void();
  516. BUG_ON(!osb);
  517. journal = osb->journal;
  518. if (!journal)
  519. goto done;
  520. inode = journal->j_inode;
  521. if (journal->j_state != OCFS2_JOURNAL_LOADED)
  522. goto done;
  523. /* need to inc inode use count - jbd2_journal_destroy will iput. */
  524. if (!igrab(inode))
  525. BUG();
  526. num_running_trans = atomic_read(&(osb->journal->j_num_trans));
  527. if (num_running_trans > 0)
  528. mlog(0, "Shutting down journal: must wait on %d "
  529. "running transactions!\n",
  530. num_running_trans);
  531. /* Do a commit_cache here. It will flush our journal, *and*
  532. * release any locks that are still held.
  533. * set the SHUTDOWN flag and release the trans lock.
  534. * the commit thread will take the trans lock for us below. */
  535. journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
  536. /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
  537. * drop the trans_lock (which we want to hold until we
  538. * completely destroy the journal. */
  539. if (osb->commit_task) {
  540. /* Wait for the commit thread */
  541. mlog(0, "Waiting for ocfs2commit to exit....\n");
  542. kthread_stop(osb->commit_task);
  543. osb->commit_task = NULL;
  544. }
  545. BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
  546. if (ocfs2_mount_local(osb)) {
  547. jbd2_journal_lock_updates(journal->j_journal);
  548. status = jbd2_journal_flush(journal->j_journal);
  549. jbd2_journal_unlock_updates(journal->j_journal);
  550. if (status < 0)
  551. mlog_errno(status);
  552. }
  553. if (status == 0) {
  554. /*
  555. * Do not toggle if flush was unsuccessful otherwise
  556. * will leave dirty metadata in a "clean" journal
  557. */
  558. status = ocfs2_journal_toggle_dirty(osb, 0, 0);
  559. if (status < 0)
  560. mlog_errno(status);
  561. }
  562. /* Shutdown the kernel journal system */
  563. jbd2_journal_destroy(journal->j_journal);
  564. journal->j_journal = NULL;
  565. OCFS2_I(inode)->ip_open_count--;
  566. /* unlock our journal */
  567. ocfs2_inode_unlock(inode, 1);
  568. brelse(journal->j_bh);
  569. journal->j_bh = NULL;
  570. journal->j_state = OCFS2_JOURNAL_FREE;
  571. // up_write(&journal->j_trans_barrier);
  572. done:
  573. if (inode)
  574. iput(inode);
  575. mlog_exit_void();
  576. }
  577. static void ocfs2_clear_journal_error(struct super_block *sb,
  578. journal_t *journal,
  579. int slot)
  580. {
  581. int olderr;
  582. olderr = jbd2_journal_errno(journal);
  583. if (olderr) {
  584. mlog(ML_ERROR, "File system error %d recorded in "
  585. "journal %u.\n", olderr, slot);
  586. mlog(ML_ERROR, "File system on device %s needs checking.\n",
  587. sb->s_id);
  588. jbd2_journal_ack_err(journal);
  589. jbd2_journal_clear_err(journal);
  590. }
  591. }
  592. int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
  593. {
  594. int status = 0;
  595. struct ocfs2_super *osb;
  596. mlog_entry_void();
  597. BUG_ON(!journal);
  598. osb = journal->j_osb;
  599. status = jbd2_journal_load(journal->j_journal);
  600. if (status < 0) {
  601. mlog(ML_ERROR, "Failed to load journal!\n");
  602. goto done;
  603. }
  604. ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
  605. status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
  606. if (status < 0) {
  607. mlog_errno(status);
  608. goto done;
  609. }
  610. /* Launch the commit thread */
  611. if (!local) {
  612. osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
  613. "ocfs2cmt");
  614. if (IS_ERR(osb->commit_task)) {
  615. status = PTR_ERR(osb->commit_task);
  616. osb->commit_task = NULL;
  617. mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
  618. "error=%d", status);
  619. goto done;
  620. }
  621. } else
  622. osb->commit_task = NULL;
  623. done:
  624. mlog_exit(status);
  625. return status;
  626. }
  627. /* 'full' flag tells us whether we clear out all blocks or if we just
  628. * mark the journal clean */
  629. int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
  630. {
  631. int status;
  632. mlog_entry_void();
  633. BUG_ON(!journal);
  634. status = jbd2_journal_wipe(journal->j_journal, full);
  635. if (status < 0) {
  636. mlog_errno(status);
  637. goto bail;
  638. }
  639. status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
  640. if (status < 0)
  641. mlog_errno(status);
  642. bail:
  643. mlog_exit(status);
  644. return status;
  645. }
  646. static int ocfs2_recovery_completed(struct ocfs2_super *osb)
  647. {
  648. int empty;
  649. struct ocfs2_recovery_map *rm = osb->recovery_map;
  650. spin_lock(&osb->osb_lock);
  651. empty = (rm->rm_used == 0);
  652. spin_unlock(&osb->osb_lock);
  653. return empty;
  654. }
  655. void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
  656. {
  657. wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
  658. }
  659. /*
  660. * JBD Might read a cached version of another nodes journal file. We
  661. * don't want this as this file changes often and we get no
  662. * notification on those changes. The only way to be sure that we've
  663. * got the most up to date version of those blocks then is to force
  664. * read them off disk. Just searching through the buffer cache won't
  665. * work as there may be pages backing this file which are still marked
  666. * up to date. We know things can't change on this file underneath us
  667. * as we have the lock by now :)
  668. */
  669. static int ocfs2_force_read_journal(struct inode *inode)
  670. {
  671. int status = 0;
  672. int i;
  673. u64 v_blkno, p_blkno, p_blocks, num_blocks;
  674. #define CONCURRENT_JOURNAL_FILL 32ULL
  675. struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
  676. mlog_entry_void();
  677. memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
  678. num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
  679. v_blkno = 0;
  680. while (v_blkno < num_blocks) {
  681. status = ocfs2_extent_map_get_blocks(inode, v_blkno,
  682. &p_blkno, &p_blocks, NULL);
  683. if (status < 0) {
  684. mlog_errno(status);
  685. goto bail;
  686. }
  687. if (p_blocks > CONCURRENT_JOURNAL_FILL)
  688. p_blocks = CONCURRENT_JOURNAL_FILL;
  689. /* We are reading journal data which should not
  690. * be put in the uptodate cache */
  691. status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
  692. p_blkno, p_blocks, bhs);
  693. if (status < 0) {
  694. mlog_errno(status);
  695. goto bail;
  696. }
  697. for(i = 0; i < p_blocks; i++) {
  698. brelse(bhs[i]);
  699. bhs[i] = NULL;
  700. }
  701. v_blkno += p_blocks;
  702. }
  703. bail:
  704. for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
  705. brelse(bhs[i]);
  706. mlog_exit(status);
  707. return status;
  708. }
  709. struct ocfs2_la_recovery_item {
  710. struct list_head lri_list;
  711. int lri_slot;
  712. struct ocfs2_dinode *lri_la_dinode;
  713. struct ocfs2_dinode *lri_tl_dinode;
  714. };
  715. /* Does the second half of the recovery process. By this point, the
  716. * node is marked clean and can actually be considered recovered,
  717. * hence it's no longer in the recovery map, but there's still some
  718. * cleanup we can do which shouldn't happen within the recovery thread
  719. * as locking in that context becomes very difficult if we are to take
  720. * recovering nodes into account.
  721. *
  722. * NOTE: This function can and will sleep on recovery of other nodes
  723. * during cluster locking, just like any other ocfs2 process.
  724. */
  725. void ocfs2_complete_recovery(struct work_struct *work)
  726. {
  727. int ret;
  728. struct ocfs2_journal *journal =
  729. container_of(work, struct ocfs2_journal, j_recovery_work);
  730. struct ocfs2_super *osb = journal->j_osb;
  731. struct ocfs2_dinode *la_dinode, *tl_dinode;
  732. struct ocfs2_la_recovery_item *item, *n;
  733. LIST_HEAD(tmp_la_list);
  734. mlog_entry_void();
  735. mlog(0, "completing recovery from keventd\n");
  736. spin_lock(&journal->j_lock);
  737. list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
  738. spin_unlock(&journal->j_lock);
  739. list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
  740. list_del_init(&item->lri_list);
  741. mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
  742. la_dinode = item->lri_la_dinode;
  743. if (la_dinode) {
  744. mlog(0, "Clean up local alloc %llu\n",
  745. (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
  746. ret = ocfs2_complete_local_alloc_recovery(osb,
  747. la_dinode);
  748. if (ret < 0)
  749. mlog_errno(ret);
  750. kfree(la_dinode);
  751. }
  752. tl_dinode = item->lri_tl_dinode;
  753. if (tl_dinode) {
  754. mlog(0, "Clean up truncate log %llu\n",
  755. (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
  756. ret = ocfs2_complete_truncate_log_recovery(osb,
  757. tl_dinode);
  758. if (ret < 0)
  759. mlog_errno(ret);
  760. kfree(tl_dinode);
  761. }
  762. ret = ocfs2_recover_orphans(osb, item->lri_slot);
  763. if (ret < 0)
  764. mlog_errno(ret);
  765. kfree(item);
  766. }
  767. mlog(0, "Recovery completion\n");
  768. mlog_exit_void();
  769. }
  770. /* NOTE: This function always eats your references to la_dinode and
  771. * tl_dinode, either manually on error, or by passing them to
  772. * ocfs2_complete_recovery */
  773. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  774. int slot_num,
  775. struct ocfs2_dinode *la_dinode,
  776. struct ocfs2_dinode *tl_dinode)
  777. {
  778. struct ocfs2_la_recovery_item *item;
  779. item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
  780. if (!item) {
  781. /* Though we wish to avoid it, we are in fact safe in
  782. * skipping local alloc cleanup as fsck.ocfs2 is more
  783. * than capable of reclaiming unused space. */
  784. if (la_dinode)
  785. kfree(la_dinode);
  786. if (tl_dinode)
  787. kfree(tl_dinode);
  788. mlog_errno(-ENOMEM);
  789. return;
  790. }
  791. INIT_LIST_HEAD(&item->lri_list);
  792. item->lri_la_dinode = la_dinode;
  793. item->lri_slot = slot_num;
  794. item->lri_tl_dinode = tl_dinode;
  795. spin_lock(&journal->j_lock);
  796. list_add_tail(&item->lri_list, &journal->j_la_cleanups);
  797. queue_work(ocfs2_wq, &journal->j_recovery_work);
  798. spin_unlock(&journal->j_lock);
  799. }
  800. /* Called by the mount code to queue recovery the last part of
  801. * recovery for it's own slot. */
  802. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
  803. {
  804. struct ocfs2_journal *journal = osb->journal;
  805. if (osb->dirty) {
  806. /* No need to queue up our truncate_log as regular
  807. * cleanup will catch that. */
  808. ocfs2_queue_recovery_completion(journal,
  809. osb->slot_num,
  810. osb->local_alloc_copy,
  811. NULL);
  812. ocfs2_schedule_truncate_log_flush(osb, 0);
  813. osb->local_alloc_copy = NULL;
  814. osb->dirty = 0;
  815. }
  816. }
  817. static int __ocfs2_recovery_thread(void *arg)
  818. {
  819. int status, node_num;
  820. struct ocfs2_super *osb = arg;
  821. struct ocfs2_recovery_map *rm = osb->recovery_map;
  822. mlog_entry_void();
  823. status = ocfs2_wait_on_mount(osb);
  824. if (status < 0) {
  825. goto bail;
  826. }
  827. restart:
  828. status = ocfs2_super_lock(osb, 1);
  829. if (status < 0) {
  830. mlog_errno(status);
  831. goto bail;
  832. }
  833. spin_lock(&osb->osb_lock);
  834. while (rm->rm_used) {
  835. /* It's always safe to remove entry zero, as we won't
  836. * clear it until ocfs2_recover_node() has succeeded. */
  837. node_num = rm->rm_entries[0];
  838. spin_unlock(&osb->osb_lock);
  839. status = ocfs2_recover_node(osb, node_num);
  840. if (!status) {
  841. ocfs2_recovery_map_clear(osb, node_num);
  842. } else {
  843. mlog(ML_ERROR,
  844. "Error %d recovering node %d on device (%u,%u)!\n",
  845. status, node_num,
  846. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  847. mlog(ML_ERROR, "Volume requires unmount.\n");
  848. }
  849. spin_lock(&osb->osb_lock);
  850. }
  851. spin_unlock(&osb->osb_lock);
  852. mlog(0, "All nodes recovered\n");
  853. /* Refresh all journal recovery generations from disk */
  854. status = ocfs2_check_journals_nolocks(osb);
  855. status = (status == -EROFS) ? 0 : status;
  856. if (status < 0)
  857. mlog_errno(status);
  858. ocfs2_super_unlock(osb, 1);
  859. /* We always run recovery on our own orphan dir - the dead
  860. * node(s) may have disallowd a previos inode delete. Re-processing
  861. * is therefore required. */
  862. ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
  863. NULL);
  864. bail:
  865. mutex_lock(&osb->recovery_lock);
  866. if (!status && !ocfs2_recovery_completed(osb)) {
  867. mutex_unlock(&osb->recovery_lock);
  868. goto restart;
  869. }
  870. osb->recovery_thread_task = NULL;
  871. mb(); /* sync with ocfs2_recovery_thread_running */
  872. wake_up(&osb->recovery_event);
  873. mutex_unlock(&osb->recovery_lock);
  874. mlog_exit(status);
  875. /* no one is callint kthread_stop() for us so the kthread() api
  876. * requires that we call do_exit(). And it isn't exported, but
  877. * complete_and_exit() seems to be a minimal wrapper around it. */
  878. complete_and_exit(NULL, status);
  879. return status;
  880. }
  881. void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
  882. {
  883. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  884. node_num, osb->node_num);
  885. mutex_lock(&osb->recovery_lock);
  886. if (osb->disable_recovery)
  887. goto out;
  888. /* People waiting on recovery will wait on
  889. * the recovery map to empty. */
  890. if (ocfs2_recovery_map_set(osb, node_num))
  891. mlog(0, "node %d already in recovery map.\n", node_num);
  892. mlog(0, "starting recovery thread...\n");
  893. if (osb->recovery_thread_task)
  894. goto out;
  895. osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
  896. "ocfs2rec");
  897. if (IS_ERR(osb->recovery_thread_task)) {
  898. mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
  899. osb->recovery_thread_task = NULL;
  900. }
  901. out:
  902. mutex_unlock(&osb->recovery_lock);
  903. wake_up(&osb->recovery_event);
  904. mlog_exit_void();
  905. }
  906. static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
  907. int slot_num,
  908. struct buffer_head **bh,
  909. struct inode **ret_inode)
  910. {
  911. int status = -EACCES;
  912. struct inode *inode = NULL;
  913. BUG_ON(slot_num >= osb->max_slots);
  914. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  915. slot_num);
  916. if (!inode || is_bad_inode(inode)) {
  917. mlog_errno(status);
  918. goto bail;
  919. }
  920. SET_INODE_JOURNAL(inode);
  921. status = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, bh,
  922. OCFS2_BH_IGNORE_CACHE);
  923. if (status < 0) {
  924. mlog_errno(status);
  925. goto bail;
  926. }
  927. status = 0;
  928. bail:
  929. if (inode) {
  930. if (status || !ret_inode)
  931. iput(inode);
  932. else
  933. *ret_inode = inode;
  934. }
  935. return status;
  936. }
  937. /* Does the actual journal replay and marks the journal inode as
  938. * clean. Will only replay if the journal inode is marked dirty. */
  939. static int ocfs2_replay_journal(struct ocfs2_super *osb,
  940. int node_num,
  941. int slot_num)
  942. {
  943. int status;
  944. int got_lock = 0;
  945. unsigned int flags;
  946. struct inode *inode = NULL;
  947. struct ocfs2_dinode *fe;
  948. journal_t *journal = NULL;
  949. struct buffer_head *bh = NULL;
  950. u32 slot_reco_gen;
  951. status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
  952. if (status) {
  953. mlog_errno(status);
  954. goto done;
  955. }
  956. fe = (struct ocfs2_dinode *)bh->b_data;
  957. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  958. brelse(bh);
  959. bh = NULL;
  960. /*
  961. * As the fs recovery is asynchronous, there is a small chance that
  962. * another node mounted (and recovered) the slot before the recovery
  963. * thread could get the lock. To handle that, we dirty read the journal
  964. * inode for that slot to get the recovery generation. If it is
  965. * different than what we expected, the slot has been recovered.
  966. * If not, it needs recovery.
  967. */
  968. if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
  969. mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
  970. osb->slot_recovery_generations[slot_num], slot_reco_gen);
  971. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  972. status = -EBUSY;
  973. goto done;
  974. }
  975. /* Continue with recovery as the journal has not yet been recovered */
  976. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  977. if (status < 0) {
  978. mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
  979. if (status != -ERESTARTSYS)
  980. mlog(ML_ERROR, "Could not lock journal!\n");
  981. goto done;
  982. }
  983. got_lock = 1;
  984. fe = (struct ocfs2_dinode *) bh->b_data;
  985. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  986. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  987. if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
  988. mlog(0, "No recovery required for node %d\n", node_num);
  989. /* Refresh recovery generation for the slot */
  990. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  991. goto done;
  992. }
  993. mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
  994. node_num, slot_num,
  995. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  996. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  997. status = ocfs2_force_read_journal(inode);
  998. if (status < 0) {
  999. mlog_errno(status);
  1000. goto done;
  1001. }
  1002. mlog(0, "calling journal_init_inode\n");
  1003. journal = jbd2_journal_init_inode(inode);
  1004. if (journal == NULL) {
  1005. mlog(ML_ERROR, "Linux journal layer error\n");
  1006. status = -EIO;
  1007. goto done;
  1008. }
  1009. status = jbd2_journal_load(journal);
  1010. if (status < 0) {
  1011. mlog_errno(status);
  1012. if (!igrab(inode))
  1013. BUG();
  1014. jbd2_journal_destroy(journal);
  1015. goto done;
  1016. }
  1017. ocfs2_clear_journal_error(osb->sb, journal, slot_num);
  1018. /* wipe the journal */
  1019. mlog(0, "flushing the journal.\n");
  1020. jbd2_journal_lock_updates(journal);
  1021. status = jbd2_journal_flush(journal);
  1022. jbd2_journal_unlock_updates(journal);
  1023. if (status < 0)
  1024. mlog_errno(status);
  1025. /* This will mark the node clean */
  1026. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  1027. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  1028. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  1029. /* Increment recovery generation to indicate successful recovery */
  1030. ocfs2_bump_recovery_generation(fe);
  1031. osb->slot_recovery_generations[slot_num] =
  1032. ocfs2_get_recovery_generation(fe);
  1033. status = ocfs2_write_block(osb, bh, inode);
  1034. if (status < 0)
  1035. mlog_errno(status);
  1036. if (!igrab(inode))
  1037. BUG();
  1038. jbd2_journal_destroy(journal);
  1039. done:
  1040. /* drop the lock on this nodes journal */
  1041. if (got_lock)
  1042. ocfs2_inode_unlock(inode, 1);
  1043. if (inode)
  1044. iput(inode);
  1045. brelse(bh);
  1046. mlog_exit(status);
  1047. return status;
  1048. }
  1049. /*
  1050. * Do the most important parts of node recovery:
  1051. * - Replay it's journal
  1052. * - Stamp a clean local allocator file
  1053. * - Stamp a clean truncate log
  1054. * - Mark the node clean
  1055. *
  1056. * If this function completes without error, a node in OCFS2 can be
  1057. * said to have been safely recovered. As a result, failure during the
  1058. * second part of a nodes recovery process (local alloc recovery) is
  1059. * far less concerning.
  1060. */
  1061. static int ocfs2_recover_node(struct ocfs2_super *osb,
  1062. int node_num)
  1063. {
  1064. int status = 0;
  1065. int slot_num;
  1066. struct ocfs2_dinode *la_copy = NULL;
  1067. struct ocfs2_dinode *tl_copy = NULL;
  1068. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  1069. node_num, osb->node_num);
  1070. mlog(0, "checking node %d\n", node_num);
  1071. /* Should not ever be called to recover ourselves -- in that
  1072. * case we should've called ocfs2_journal_load instead. */
  1073. BUG_ON(osb->node_num == node_num);
  1074. slot_num = ocfs2_node_num_to_slot(osb, node_num);
  1075. if (slot_num == -ENOENT) {
  1076. status = 0;
  1077. mlog(0, "no slot for this node, so no recovery required.\n");
  1078. goto done;
  1079. }
  1080. mlog(0, "node %d was using slot %d\n", node_num, slot_num);
  1081. status = ocfs2_replay_journal(osb, node_num, slot_num);
  1082. if (status < 0) {
  1083. if (status == -EBUSY) {
  1084. mlog(0, "Skipping recovery for slot %u (node %u) "
  1085. "as another node has recovered it\n", slot_num,
  1086. node_num);
  1087. status = 0;
  1088. goto done;
  1089. }
  1090. mlog_errno(status);
  1091. goto done;
  1092. }
  1093. /* Stamp a clean local alloc file AFTER recovering the journal... */
  1094. status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
  1095. if (status < 0) {
  1096. mlog_errno(status);
  1097. goto done;
  1098. }
  1099. /* An error from begin_truncate_log_recovery is not
  1100. * serious enough to warrant halting the rest of
  1101. * recovery. */
  1102. status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
  1103. if (status < 0)
  1104. mlog_errno(status);
  1105. /* Likewise, this would be a strange but ultimately not so
  1106. * harmful place to get an error... */
  1107. status = ocfs2_clear_slot(osb, slot_num);
  1108. if (status < 0)
  1109. mlog_errno(status);
  1110. /* This will kfree the memory pointed to by la_copy and tl_copy */
  1111. ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
  1112. tl_copy);
  1113. status = 0;
  1114. done:
  1115. mlog_exit(status);
  1116. return status;
  1117. }
  1118. /* Test node liveness by trylocking his journal. If we get the lock,
  1119. * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
  1120. * still alive (we couldn't get the lock) and < 0 on error. */
  1121. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  1122. int slot_num)
  1123. {
  1124. int status, flags;
  1125. struct inode *inode = NULL;
  1126. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1127. slot_num);
  1128. if (inode == NULL) {
  1129. mlog(ML_ERROR, "access error\n");
  1130. status = -EACCES;
  1131. goto bail;
  1132. }
  1133. if (is_bad_inode(inode)) {
  1134. mlog(ML_ERROR, "access error (bad inode)\n");
  1135. iput(inode);
  1136. inode = NULL;
  1137. status = -EACCES;
  1138. goto bail;
  1139. }
  1140. SET_INODE_JOURNAL(inode);
  1141. flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
  1142. status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
  1143. if (status < 0) {
  1144. if (status != -EAGAIN)
  1145. mlog_errno(status);
  1146. goto bail;
  1147. }
  1148. ocfs2_inode_unlock(inode, 1);
  1149. bail:
  1150. if (inode)
  1151. iput(inode);
  1152. return status;
  1153. }
  1154. /* Call this underneath ocfs2_super_lock. It also assumes that the
  1155. * slot info struct has been updated from disk. */
  1156. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
  1157. {
  1158. unsigned int node_num;
  1159. int status, i;
  1160. u32 gen;
  1161. struct buffer_head *bh = NULL;
  1162. struct ocfs2_dinode *di;
  1163. /* This is called with the super block cluster lock, so we
  1164. * know that the slot map can't change underneath us. */
  1165. for (i = 0; i < osb->max_slots; i++) {
  1166. /* Read journal inode to get the recovery generation */
  1167. status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
  1168. if (status) {
  1169. mlog_errno(status);
  1170. goto bail;
  1171. }
  1172. di = (struct ocfs2_dinode *)bh->b_data;
  1173. gen = ocfs2_get_recovery_generation(di);
  1174. brelse(bh);
  1175. bh = NULL;
  1176. spin_lock(&osb->osb_lock);
  1177. osb->slot_recovery_generations[i] = gen;
  1178. mlog(0, "Slot %u recovery generation is %u\n", i,
  1179. osb->slot_recovery_generations[i]);
  1180. if (i == osb->slot_num) {
  1181. spin_unlock(&osb->osb_lock);
  1182. continue;
  1183. }
  1184. status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
  1185. if (status == -ENOENT) {
  1186. spin_unlock(&osb->osb_lock);
  1187. continue;
  1188. }
  1189. if (__ocfs2_recovery_map_test(osb, node_num)) {
  1190. spin_unlock(&osb->osb_lock);
  1191. continue;
  1192. }
  1193. spin_unlock(&osb->osb_lock);
  1194. /* Ok, we have a slot occupied by another node which
  1195. * is not in the recovery map. We trylock his journal
  1196. * file here to test if he's alive. */
  1197. status = ocfs2_trylock_journal(osb, i);
  1198. if (!status) {
  1199. /* Since we're called from mount, we know that
  1200. * the recovery thread can't race us on
  1201. * setting / checking the recovery bits. */
  1202. ocfs2_recovery_thread(osb, node_num);
  1203. } else if ((status < 0) && (status != -EAGAIN)) {
  1204. mlog_errno(status);
  1205. goto bail;
  1206. }
  1207. }
  1208. status = 0;
  1209. bail:
  1210. mlog_exit(status);
  1211. return status;
  1212. }
  1213. struct ocfs2_orphan_filldir_priv {
  1214. struct inode *head;
  1215. struct ocfs2_super *osb;
  1216. };
  1217. static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
  1218. loff_t pos, u64 ino, unsigned type)
  1219. {
  1220. struct ocfs2_orphan_filldir_priv *p = priv;
  1221. struct inode *iter;
  1222. if (name_len == 1 && !strncmp(".", name, 1))
  1223. return 0;
  1224. if (name_len == 2 && !strncmp("..", name, 2))
  1225. return 0;
  1226. /* Skip bad inodes so that recovery can continue */
  1227. iter = ocfs2_iget(p->osb, ino,
  1228. OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
  1229. if (IS_ERR(iter))
  1230. return 0;
  1231. mlog(0, "queue orphan %llu\n",
  1232. (unsigned long long)OCFS2_I(iter)->ip_blkno);
  1233. /* No locking is required for the next_orphan queue as there
  1234. * is only ever a single process doing orphan recovery. */
  1235. OCFS2_I(iter)->ip_next_orphan = p->head;
  1236. p->head = iter;
  1237. return 0;
  1238. }
  1239. static int ocfs2_queue_orphans(struct ocfs2_super *osb,
  1240. int slot,
  1241. struct inode **head)
  1242. {
  1243. int status;
  1244. struct inode *orphan_dir_inode = NULL;
  1245. struct ocfs2_orphan_filldir_priv priv;
  1246. loff_t pos = 0;
  1247. priv.osb = osb;
  1248. priv.head = *head;
  1249. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1250. ORPHAN_DIR_SYSTEM_INODE,
  1251. slot);
  1252. if (!orphan_dir_inode) {
  1253. status = -ENOENT;
  1254. mlog_errno(status);
  1255. return status;
  1256. }
  1257. mutex_lock(&orphan_dir_inode->i_mutex);
  1258. status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
  1259. if (status < 0) {
  1260. mlog_errno(status);
  1261. goto out;
  1262. }
  1263. status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
  1264. ocfs2_orphan_filldir);
  1265. if (status) {
  1266. mlog_errno(status);
  1267. goto out_cluster;
  1268. }
  1269. *head = priv.head;
  1270. out_cluster:
  1271. ocfs2_inode_unlock(orphan_dir_inode, 0);
  1272. out:
  1273. mutex_unlock(&orphan_dir_inode->i_mutex);
  1274. iput(orphan_dir_inode);
  1275. return status;
  1276. }
  1277. static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
  1278. int slot)
  1279. {
  1280. int ret;
  1281. spin_lock(&osb->osb_lock);
  1282. ret = !osb->osb_orphan_wipes[slot];
  1283. spin_unlock(&osb->osb_lock);
  1284. return ret;
  1285. }
  1286. static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
  1287. int slot)
  1288. {
  1289. spin_lock(&osb->osb_lock);
  1290. /* Mark ourselves such that new processes in delete_inode()
  1291. * know to quit early. */
  1292. ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1293. while (osb->osb_orphan_wipes[slot]) {
  1294. /* If any processes are already in the middle of an
  1295. * orphan wipe on this dir, then we need to wait for
  1296. * them. */
  1297. spin_unlock(&osb->osb_lock);
  1298. wait_event_interruptible(osb->osb_wipe_event,
  1299. ocfs2_orphan_recovery_can_continue(osb, slot));
  1300. spin_lock(&osb->osb_lock);
  1301. }
  1302. spin_unlock(&osb->osb_lock);
  1303. }
  1304. static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
  1305. int slot)
  1306. {
  1307. ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1308. }
  1309. /*
  1310. * Orphan recovery. Each mounted node has it's own orphan dir which we
  1311. * must run during recovery. Our strategy here is to build a list of
  1312. * the inodes in the orphan dir and iget/iput them. The VFS does
  1313. * (most) of the rest of the work.
  1314. *
  1315. * Orphan recovery can happen at any time, not just mount so we have a
  1316. * couple of extra considerations.
  1317. *
  1318. * - We grab as many inodes as we can under the orphan dir lock -
  1319. * doing iget() outside the orphan dir risks getting a reference on
  1320. * an invalid inode.
  1321. * - We must be sure not to deadlock with other processes on the
  1322. * system wanting to run delete_inode(). This can happen when they go
  1323. * to lock the orphan dir and the orphan recovery process attempts to
  1324. * iget() inside the orphan dir lock. This can be avoided by
  1325. * advertising our state to ocfs2_delete_inode().
  1326. */
  1327. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  1328. int slot)
  1329. {
  1330. int ret = 0;
  1331. struct inode *inode = NULL;
  1332. struct inode *iter;
  1333. struct ocfs2_inode_info *oi;
  1334. mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
  1335. ocfs2_mark_recovering_orphan_dir(osb, slot);
  1336. ret = ocfs2_queue_orphans(osb, slot, &inode);
  1337. ocfs2_clear_recovering_orphan_dir(osb, slot);
  1338. /* Error here should be noted, but we want to continue with as
  1339. * many queued inodes as we've got. */
  1340. if (ret)
  1341. mlog_errno(ret);
  1342. while (inode) {
  1343. oi = OCFS2_I(inode);
  1344. mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
  1345. iter = oi->ip_next_orphan;
  1346. spin_lock(&oi->ip_lock);
  1347. /* The remote delete code may have set these on the
  1348. * assumption that the other node would wipe them
  1349. * successfully. If they are still in the node's
  1350. * orphan dir, we need to reset that state. */
  1351. oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
  1352. /* Set the proper information to get us going into
  1353. * ocfs2_delete_inode. */
  1354. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  1355. spin_unlock(&oi->ip_lock);
  1356. iput(inode);
  1357. inode = iter;
  1358. }
  1359. return ret;
  1360. }
  1361. static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
  1362. {
  1363. /* This check is good because ocfs2 will wait on our recovery
  1364. * thread before changing it to something other than MOUNTED
  1365. * or DISABLED. */
  1366. wait_event(osb->osb_mount_event,
  1367. atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
  1368. atomic_read(&osb->vol_state) == VOLUME_DISABLED);
  1369. /* If there's an error on mount, then we may never get to the
  1370. * MOUNTED flag, but this is set right before
  1371. * dismount_volume() so we can trust it. */
  1372. if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
  1373. mlog(0, "mount error, exiting!\n");
  1374. return -EBUSY;
  1375. }
  1376. return 0;
  1377. }
  1378. static int ocfs2_commit_thread(void *arg)
  1379. {
  1380. int status;
  1381. struct ocfs2_super *osb = arg;
  1382. struct ocfs2_journal *journal = osb->journal;
  1383. /* we can trust j_num_trans here because _should_stop() is only set in
  1384. * shutdown and nobody other than ourselves should be able to start
  1385. * transactions. committing on shutdown might take a few iterations
  1386. * as final transactions put deleted inodes on the list */
  1387. while (!(kthread_should_stop() &&
  1388. atomic_read(&journal->j_num_trans) == 0)) {
  1389. wait_event_interruptible(osb->checkpoint_event,
  1390. atomic_read(&journal->j_num_trans)
  1391. || kthread_should_stop());
  1392. status = ocfs2_commit_cache(osb);
  1393. if (status < 0)
  1394. mlog_errno(status);
  1395. if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
  1396. mlog(ML_KTHREAD,
  1397. "commit_thread: %u transactions pending on "
  1398. "shutdown\n",
  1399. atomic_read(&journal->j_num_trans));
  1400. }
  1401. }
  1402. return 0;
  1403. }
  1404. /* Reads all the journal inodes without taking any cluster locks. Used
  1405. * for hard readonly access to determine whether any journal requires
  1406. * recovery. Also used to refresh the recovery generation numbers after
  1407. * a journal has been recovered by another node.
  1408. */
  1409. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
  1410. {
  1411. int ret = 0;
  1412. unsigned int slot;
  1413. struct buffer_head *di_bh = NULL;
  1414. struct ocfs2_dinode *di;
  1415. int journal_dirty = 0;
  1416. for(slot = 0; slot < osb->max_slots; slot++) {
  1417. ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
  1418. if (ret) {
  1419. mlog_errno(ret);
  1420. goto out;
  1421. }
  1422. di = (struct ocfs2_dinode *) di_bh->b_data;
  1423. osb->slot_recovery_generations[slot] =
  1424. ocfs2_get_recovery_generation(di);
  1425. if (le32_to_cpu(di->id1.journal1.ij_flags) &
  1426. OCFS2_JOURNAL_DIRTY_FL)
  1427. journal_dirty = 1;
  1428. brelse(di_bh);
  1429. di_bh = NULL;
  1430. }
  1431. out:
  1432. if (journal_dirty)
  1433. ret = -EROFS;
  1434. return ret;
  1435. }