journal.c 50 KB

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