journal.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * journal.c
  4. *
  5. * Defines functions of journalling api
  6. *
  7. * Copyright (C) 2003, 2004 Oracle. All rights reserved.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/types.h>
  11. #include <linux/slab.h>
  12. #include <linux/highmem.h>
  13. #include <linux/kthread.h>
  14. #include <linux/time.h>
  15. #include <linux/random.h>
  16. #include <linux/delay.h>
  17. #include <linux/writeback.h>
  18. #include <cluster/masklog.h>
  19. #include "ocfs2.h"
  20. #include "alloc.h"
  21. #include "blockcheck.h"
  22. #include "dir.h"
  23. #include "dlmglue.h"
  24. #include "extent_map.h"
  25. #include "heartbeat.h"
  26. #include "inode.h"
  27. #include "journal.h"
  28. #include "localalloc.h"
  29. #include "slot_map.h"
  30. #include "super.h"
  31. #include "sysfile.h"
  32. #include "uptodate.h"
  33. #include "quota.h"
  34. #include "file.h"
  35. #include "namei.h"
  36. #include "buffer_head_io.h"
  37. #include "ocfs2_trace.h"
  38. DEFINE_SPINLOCK(trans_inc_lock);
  39. #define ORPHAN_SCAN_SCHEDULE_TIMEOUT 300000
  40. static int ocfs2_force_read_journal(struct inode *inode);
  41. static int ocfs2_recover_node(struct ocfs2_super *osb,
  42. int node_num, int slot_num);
  43. static int __ocfs2_recovery_thread(void *arg);
  44. static int ocfs2_commit_cache(struct ocfs2_super *osb);
  45. static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
  46. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  47. int dirty, int replayed);
  48. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  49. int slot_num);
  50. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  51. int slot,
  52. enum ocfs2_orphan_reco_type orphan_reco_type);
  53. static int ocfs2_commit_thread(void *arg);
  54. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  55. int slot_num,
  56. struct ocfs2_dinode *la_dinode,
  57. struct ocfs2_dinode *tl_dinode,
  58. struct ocfs2_quota_recovery *qrec,
  59. enum ocfs2_orphan_reco_type orphan_reco_type);
  60. static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
  61. {
  62. return __ocfs2_wait_on_mount(osb, 0);
  63. }
  64. static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
  65. {
  66. return __ocfs2_wait_on_mount(osb, 1);
  67. }
  68. /*
  69. * This replay_map is to track online/offline slots, so we could recover
  70. * offline slots during recovery and mount
  71. */
  72. enum ocfs2_replay_state {
  73. REPLAY_UNNEEDED = 0, /* Replay is not needed, so ignore this map */
  74. REPLAY_NEEDED, /* Replay slots marked in rm_replay_slots */
  75. REPLAY_DONE /* Replay was already queued */
  76. };
  77. struct ocfs2_replay_map {
  78. unsigned int rm_slots;
  79. enum ocfs2_replay_state rm_state;
  80. unsigned char rm_replay_slots[] __counted_by(rm_slots);
  81. };
  82. static void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
  83. {
  84. if (!osb->replay_map)
  85. return;
  86. /* If we've already queued the replay, we don't have any more to do */
  87. if (osb->replay_map->rm_state == REPLAY_DONE)
  88. return;
  89. osb->replay_map->rm_state = state;
  90. }
  91. int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
  92. {
  93. struct ocfs2_replay_map *replay_map;
  94. int i, node_num;
  95. /* If replay map is already set, we don't do it again */
  96. if (osb->replay_map)
  97. return 0;
  98. replay_map = kzalloc_flex(*replay_map, rm_replay_slots, osb->max_slots);
  99. if (!replay_map) {
  100. mlog_errno(-ENOMEM);
  101. return -ENOMEM;
  102. }
  103. spin_lock(&osb->osb_lock);
  104. replay_map->rm_slots = osb->max_slots;
  105. replay_map->rm_state = REPLAY_UNNEEDED;
  106. /* set rm_replay_slots for offline slot(s) */
  107. for (i = 0; i < replay_map->rm_slots; i++) {
  108. if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
  109. replay_map->rm_replay_slots[i] = 1;
  110. }
  111. osb->replay_map = replay_map;
  112. spin_unlock(&osb->osb_lock);
  113. return 0;
  114. }
  115. static void ocfs2_queue_replay_slots(struct ocfs2_super *osb,
  116. enum ocfs2_orphan_reco_type orphan_reco_type)
  117. {
  118. struct ocfs2_replay_map *replay_map = osb->replay_map;
  119. int i;
  120. if (!replay_map)
  121. return;
  122. if (replay_map->rm_state != REPLAY_NEEDED)
  123. return;
  124. for (i = 0; i < replay_map->rm_slots; i++)
  125. if (replay_map->rm_replay_slots[i])
  126. ocfs2_queue_recovery_completion(osb->journal, i, NULL,
  127. NULL, NULL,
  128. orphan_reco_type);
  129. replay_map->rm_state = REPLAY_DONE;
  130. }
  131. void ocfs2_free_replay_slots(struct ocfs2_super *osb)
  132. {
  133. struct ocfs2_replay_map *replay_map = osb->replay_map;
  134. if (!osb->replay_map)
  135. return;
  136. kfree(replay_map);
  137. osb->replay_map = NULL;
  138. }
  139. int ocfs2_recovery_init(struct ocfs2_super *osb)
  140. {
  141. struct ocfs2_recovery_map *rm;
  142. mutex_init(&osb->recovery_lock);
  143. osb->recovery_state = OCFS2_REC_ENABLED;
  144. osb->recovery_thread_task = NULL;
  145. init_waitqueue_head(&osb->recovery_event);
  146. rm = kzalloc_flex(*rm, rm_entries, osb->max_slots);
  147. if (!rm) {
  148. mlog_errno(-ENOMEM);
  149. return -ENOMEM;
  150. }
  151. osb->recovery_map = rm;
  152. return 0;
  153. }
  154. static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
  155. {
  156. return osb->recovery_thread_task != NULL;
  157. }
  158. static void ocfs2_recovery_disable(struct ocfs2_super *osb,
  159. enum ocfs2_recovery_state state)
  160. {
  161. mutex_lock(&osb->recovery_lock);
  162. /*
  163. * If recovery thread is not running, we can directly transition to
  164. * final state.
  165. */
  166. if (!ocfs2_recovery_thread_running(osb)) {
  167. osb->recovery_state = state + 1;
  168. goto out_lock;
  169. }
  170. osb->recovery_state = state;
  171. /* Wait for recovery thread to acknowledge state transition */
  172. wait_event_cmd(osb->recovery_event,
  173. !ocfs2_recovery_thread_running(osb) ||
  174. osb->recovery_state >= state + 1,
  175. mutex_unlock(&osb->recovery_lock),
  176. mutex_lock(&osb->recovery_lock));
  177. out_lock:
  178. mutex_unlock(&osb->recovery_lock);
  179. /*
  180. * At this point we know that no more recovery work can be queued so
  181. * wait for any recovery completion work to complete.
  182. */
  183. if (osb->ocfs2_wq)
  184. flush_workqueue(osb->ocfs2_wq);
  185. }
  186. void ocfs2_recovery_disable_quota(struct ocfs2_super *osb)
  187. {
  188. ocfs2_recovery_disable(osb, OCFS2_REC_QUOTA_WANT_DISABLE);
  189. }
  190. void ocfs2_recovery_exit(struct ocfs2_super *osb)
  191. {
  192. struct ocfs2_recovery_map *rm;
  193. /* disable any new recovery threads and wait for any currently
  194. * running ones to exit. Do this before setting the vol_state. */
  195. ocfs2_recovery_disable(osb, OCFS2_REC_WANT_DISABLE);
  196. /*
  197. * Now that recovery is shut down, and the osb is about to be
  198. * freed, the osb_lock is not taken here.
  199. */
  200. rm = osb->recovery_map;
  201. /* XXX: Should we bug if there are dirty entries? */
  202. kfree(rm);
  203. }
  204. static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
  205. unsigned int node_num)
  206. {
  207. int i;
  208. struct ocfs2_recovery_map *rm = osb->recovery_map;
  209. assert_spin_locked(&osb->osb_lock);
  210. for (i = 0; i < rm->rm_used; i++) {
  211. if (rm->rm_entries[i] == node_num)
  212. return 1;
  213. }
  214. return 0;
  215. }
  216. /* Behaves like test-and-set. Returns the previous value */
  217. static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
  218. unsigned int node_num)
  219. {
  220. struct ocfs2_recovery_map *rm = osb->recovery_map;
  221. spin_lock(&osb->osb_lock);
  222. if (__ocfs2_recovery_map_test(osb, node_num)) {
  223. spin_unlock(&osb->osb_lock);
  224. return 1;
  225. }
  226. /* XXX: Can this be exploited? Not from o2dlm... */
  227. BUG_ON(rm->rm_used >= osb->max_slots);
  228. rm->rm_entries[rm->rm_used] = node_num;
  229. rm->rm_used++;
  230. spin_unlock(&osb->osb_lock);
  231. return 0;
  232. }
  233. static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
  234. unsigned int node_num)
  235. {
  236. int i;
  237. struct ocfs2_recovery_map *rm = osb->recovery_map;
  238. spin_lock(&osb->osb_lock);
  239. for (i = 0; i < rm->rm_used; i++) {
  240. if (rm->rm_entries[i] == node_num)
  241. break;
  242. }
  243. if (i < rm->rm_used) {
  244. /* XXX: be careful with the pointer math */
  245. memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
  246. (rm->rm_used - i - 1) * sizeof(unsigned int));
  247. rm->rm_used--;
  248. }
  249. spin_unlock(&osb->osb_lock);
  250. }
  251. static int ocfs2_commit_cache(struct ocfs2_super *osb)
  252. {
  253. int status = 0;
  254. unsigned int flushed;
  255. struct ocfs2_journal *journal = NULL;
  256. journal = osb->journal;
  257. /* Flush all pending commits and checkpoint the journal. */
  258. down_write(&journal->j_trans_barrier);
  259. flushed = atomic_read(&journal->j_num_trans);
  260. trace_ocfs2_commit_cache_begin(flushed);
  261. if (flushed == 0) {
  262. up_write(&journal->j_trans_barrier);
  263. goto finally;
  264. }
  265. jbd2_journal_lock_updates(journal->j_journal);
  266. status = jbd2_journal_flush(journal->j_journal, 0);
  267. jbd2_journal_unlock_updates(journal->j_journal);
  268. if (status < 0) {
  269. up_write(&journal->j_trans_barrier);
  270. mlog_errno(status);
  271. goto finally;
  272. }
  273. ocfs2_inc_trans_id(journal);
  274. flushed = atomic_read(&journal->j_num_trans);
  275. atomic_set(&journal->j_num_trans, 0);
  276. up_write(&journal->j_trans_barrier);
  277. trace_ocfs2_commit_cache_end(journal->j_trans_id, flushed);
  278. ocfs2_wake_downconvert_thread(osb);
  279. wake_up(&journal->j_checkpointed);
  280. finally:
  281. return status;
  282. }
  283. handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
  284. {
  285. journal_t *journal = osb->journal->j_journal;
  286. handle_t *handle;
  287. BUG_ON(!osb || !osb->journal->j_journal);
  288. if (ocfs2_is_hard_readonly(osb))
  289. return ERR_PTR(-EROFS);
  290. BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
  291. BUG_ON(max_buffs <= 0);
  292. /* Nested transaction? Just return the handle... */
  293. if (journal_current_handle())
  294. return jbd2_journal_start(journal, max_buffs);
  295. sb_start_intwrite(osb->sb);
  296. down_read(&osb->journal->j_trans_barrier);
  297. handle = jbd2_journal_start(journal, max_buffs);
  298. if (IS_ERR(handle)) {
  299. up_read(&osb->journal->j_trans_barrier);
  300. sb_end_intwrite(osb->sb);
  301. mlog_errno(PTR_ERR(handle));
  302. if (is_journal_aborted(journal)) {
  303. ocfs2_abort(osb->sb, "Detected aborted journal\n");
  304. handle = ERR_PTR(-EROFS);
  305. }
  306. } else {
  307. if (!ocfs2_mount_local(osb))
  308. atomic_inc(&(osb->journal->j_num_trans));
  309. }
  310. return handle;
  311. }
  312. int ocfs2_commit_trans(struct ocfs2_super *osb,
  313. handle_t *handle)
  314. {
  315. int ret, nested;
  316. struct ocfs2_journal *journal = osb->journal;
  317. BUG_ON(!handle);
  318. nested = handle->h_ref > 1;
  319. ret = jbd2_journal_stop(handle);
  320. if (ret < 0)
  321. mlog_errno(ret);
  322. if (!nested) {
  323. up_read(&journal->j_trans_barrier);
  324. sb_end_intwrite(osb->sb);
  325. }
  326. return ret;
  327. }
  328. /*
  329. * 'nblocks' is what you want to add to the current transaction.
  330. *
  331. * This might call jbd2_journal_restart() which will commit dirty buffers
  332. * and then restart the transaction. Before calling
  333. * ocfs2_extend_trans(), any changed blocks should have been
  334. * dirtied. After calling it, all blocks which need to be changed must
  335. * go through another set of journal_access/journal_dirty calls.
  336. *
  337. * WARNING: This will not release any semaphores or disk locks taken
  338. * during the transaction, so make sure they were taken *before*
  339. * start_trans or we'll have ordering deadlocks.
  340. *
  341. * WARNING2: Note that we do *not* drop j_trans_barrier here. This is
  342. * good because transaction ids haven't yet been recorded on the
  343. * cluster locks associated with this handle.
  344. */
  345. int ocfs2_extend_trans(handle_t *handle, int nblocks)
  346. {
  347. int status, old_nblocks;
  348. BUG_ON(!handle);
  349. BUG_ON(nblocks < 0);
  350. if (!nblocks)
  351. return 0;
  352. old_nblocks = jbd2_handle_buffer_credits(handle);
  353. trace_ocfs2_extend_trans(old_nblocks, nblocks);
  354. #ifdef CONFIG_OCFS2_DEBUG_FS
  355. status = 1;
  356. #else
  357. status = jbd2_journal_extend(handle, nblocks, 0);
  358. if (status < 0) {
  359. mlog_errno(status);
  360. goto bail;
  361. }
  362. #endif
  363. if (status > 0) {
  364. trace_ocfs2_extend_trans_restart(old_nblocks + nblocks);
  365. status = jbd2_journal_restart(handle,
  366. old_nblocks + nblocks);
  367. if (status < 0) {
  368. mlog_errno(status);
  369. goto bail;
  370. }
  371. }
  372. status = 0;
  373. bail:
  374. return status;
  375. }
  376. /*
  377. * Make sure handle has at least 'nblocks' credits available. If it does not
  378. * have that many credits available, we will try to extend the handle to have
  379. * enough credits. If that fails, we will restart transaction to have enough
  380. * credits. Similar notes regarding data consistency and locking implications
  381. * as for ocfs2_extend_trans() apply here.
  382. */
  383. int ocfs2_assure_trans_credits(handle_t *handle, int nblocks)
  384. {
  385. int old_nblks = jbd2_handle_buffer_credits(handle);
  386. trace_ocfs2_assure_trans_credits(old_nblks);
  387. if (old_nblks >= nblocks)
  388. return 0;
  389. return ocfs2_extend_trans(handle, nblocks - old_nblks);
  390. }
  391. /*
  392. * If we have fewer than thresh credits, extend by OCFS2_MAX_TRANS_DATA.
  393. * If that fails, restart the transaction & regain write access for the
  394. * buffer head which is used for metadata modifications.
  395. * Taken from Ext4: extend_or_restart_transaction()
  396. */
  397. int ocfs2_allocate_extend_trans(handle_t *handle, int thresh)
  398. {
  399. int status, old_nblks;
  400. BUG_ON(!handle);
  401. old_nblks = jbd2_handle_buffer_credits(handle);
  402. trace_ocfs2_allocate_extend_trans(old_nblks, thresh);
  403. if (old_nblks < thresh)
  404. return 0;
  405. status = jbd2_journal_extend(handle, OCFS2_MAX_TRANS_DATA, 0);
  406. if (status < 0) {
  407. mlog_errno(status);
  408. goto bail;
  409. }
  410. if (status > 0) {
  411. status = jbd2_journal_restart(handle, OCFS2_MAX_TRANS_DATA);
  412. if (status < 0)
  413. mlog_errno(status);
  414. }
  415. bail:
  416. return status;
  417. }
  418. static inline struct ocfs2_triggers *to_ocfs2_trigger(struct jbd2_buffer_trigger_type *triggers)
  419. {
  420. return container_of(triggers, struct ocfs2_triggers, ot_triggers);
  421. }
  422. static void ocfs2_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
  423. struct buffer_head *bh,
  424. void *data, size_t size)
  425. {
  426. struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
  427. /*
  428. * We aren't guaranteed to have the superblock here, so we
  429. * must unconditionally compute the ecc data.
  430. * __ocfs2_journal_access() will only set the triggers if
  431. * metaecc is enabled.
  432. */
  433. ocfs2_block_check_compute(data, size, data + ot->ot_offset);
  434. }
  435. /*
  436. * Quota blocks have their own trigger because the struct ocfs2_block_check
  437. * offset depends on the blocksize.
  438. */
  439. static void ocfs2_dq_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
  440. struct buffer_head *bh,
  441. void *data, size_t size)
  442. {
  443. struct ocfs2_disk_dqtrailer *dqt =
  444. ocfs2_block_dqtrailer(size, data);
  445. /*
  446. * We aren't guaranteed to have the superblock here, so we
  447. * must unconditionally compute the ecc data.
  448. * __ocfs2_journal_access() will only set the triggers if
  449. * metaecc is enabled.
  450. */
  451. ocfs2_block_check_compute(data, size, &dqt->dq_check);
  452. }
  453. /*
  454. * Directory blocks also have their own trigger because the
  455. * struct ocfs2_block_check offset depends on the blocksize.
  456. */
  457. static void ocfs2_db_frozen_trigger(struct jbd2_buffer_trigger_type *triggers,
  458. struct buffer_head *bh,
  459. void *data, size_t size)
  460. {
  461. struct ocfs2_dir_block_trailer *trailer =
  462. ocfs2_dir_trailer_from_size(size, data);
  463. /*
  464. * We aren't guaranteed to have the superblock here, so we
  465. * must unconditionally compute the ecc data.
  466. * __ocfs2_journal_access() will only set the triggers if
  467. * metaecc is enabled.
  468. */
  469. ocfs2_block_check_compute(data, size, &trailer->db_check);
  470. }
  471. static void ocfs2_abort_trigger(struct jbd2_buffer_trigger_type *triggers,
  472. struct buffer_head *bh)
  473. {
  474. struct ocfs2_triggers *ot = to_ocfs2_trigger(triggers);
  475. mlog(ML_ERROR,
  476. "ocfs2_abort_trigger called by JBD2. bh = 0x%lx, "
  477. "bh->b_blocknr = %llu\n",
  478. (unsigned long)bh,
  479. (unsigned long long)bh->b_blocknr);
  480. ocfs2_error(ot->sb,
  481. "JBD2 has aborted our journal, ocfs2 cannot continue\n");
  482. }
  483. static void ocfs2_setup_csum_triggers(struct super_block *sb,
  484. enum ocfs2_journal_trigger_type type,
  485. struct ocfs2_triggers *ot)
  486. {
  487. BUG_ON(type >= OCFS2_JOURNAL_TRIGGER_COUNT);
  488. switch (type) {
  489. case OCFS2_JTR_DI:
  490. ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
  491. ot->ot_offset = offsetof(struct ocfs2_dinode, i_check);
  492. break;
  493. case OCFS2_JTR_EB:
  494. ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
  495. ot->ot_offset = offsetof(struct ocfs2_extent_block, h_check);
  496. break;
  497. case OCFS2_JTR_RB:
  498. ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
  499. ot->ot_offset = offsetof(struct ocfs2_refcount_block, rf_check);
  500. break;
  501. case OCFS2_JTR_GD:
  502. ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
  503. ot->ot_offset = offsetof(struct ocfs2_group_desc, bg_check);
  504. break;
  505. case OCFS2_JTR_DB:
  506. ot->ot_triggers.t_frozen = ocfs2_db_frozen_trigger;
  507. break;
  508. case OCFS2_JTR_XB:
  509. ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
  510. ot->ot_offset = offsetof(struct ocfs2_xattr_block, xb_check);
  511. break;
  512. case OCFS2_JTR_DQ:
  513. ot->ot_triggers.t_frozen = ocfs2_dq_frozen_trigger;
  514. break;
  515. case OCFS2_JTR_DR:
  516. ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
  517. ot->ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check);
  518. break;
  519. case OCFS2_JTR_DL:
  520. ot->ot_triggers.t_frozen = ocfs2_frozen_trigger;
  521. ot->ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check);
  522. break;
  523. case OCFS2_JTR_NONE:
  524. /* To make compiler happy... */
  525. return;
  526. }
  527. ot->ot_triggers.t_abort = ocfs2_abort_trigger;
  528. ot->sb = sb;
  529. }
  530. void ocfs2_initialize_journal_triggers(struct super_block *sb,
  531. struct ocfs2_triggers triggers[])
  532. {
  533. enum ocfs2_journal_trigger_type type;
  534. for (type = OCFS2_JTR_DI; type < OCFS2_JOURNAL_TRIGGER_COUNT; type++)
  535. ocfs2_setup_csum_triggers(sb, type, &triggers[type]);
  536. }
  537. static int __ocfs2_journal_access(handle_t *handle,
  538. struct ocfs2_caching_info *ci,
  539. struct buffer_head *bh,
  540. struct ocfs2_triggers *triggers,
  541. int type)
  542. {
  543. int status;
  544. struct ocfs2_super *osb =
  545. OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  546. BUG_ON(!ci || !ci->ci_ops);
  547. BUG_ON(!handle);
  548. BUG_ON(!bh);
  549. trace_ocfs2_journal_access(
  550. (unsigned long long)ocfs2_metadata_cache_owner(ci),
  551. (unsigned long long)bh->b_blocknr, type, bh->b_size);
  552. /* we can safely remove this assertion after testing. */
  553. if (!buffer_uptodate(bh)) {
  554. mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
  555. mlog(ML_ERROR, "b_blocknr=%llu, b_state=0x%lx\n",
  556. (unsigned long long)bh->b_blocknr, bh->b_state);
  557. lock_buffer(bh);
  558. /*
  559. * A previous transaction with a couple of buffer heads fail
  560. * to checkpoint, so all the bhs are marked as BH_Write_EIO.
  561. * For current transaction, the bh is just among those error
  562. * bhs which previous transaction handle. We can't just clear
  563. * its BH_Write_EIO and reuse directly, since other bhs are
  564. * not written to disk yet and that will cause metadata
  565. * inconsistency. So we should set fs read-only to avoid
  566. * further damage.
  567. */
  568. if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) {
  569. unlock_buffer(bh);
  570. return ocfs2_error(osb->sb, "A previous attempt to "
  571. "write this buffer head failed\n");
  572. }
  573. unlock_buffer(bh);
  574. }
  575. /* Set the current transaction information on the ci so
  576. * that the locking code knows whether it can drop it's locks
  577. * on this ci or not. We're protected from the commit
  578. * thread updating the current transaction id until
  579. * ocfs2_commit_trans() because ocfs2_start_trans() took
  580. * j_trans_barrier for us. */
  581. ocfs2_set_ci_lock_trans(osb->journal, ci);
  582. ocfs2_metadata_cache_io_lock(ci);
  583. switch (type) {
  584. case OCFS2_JOURNAL_ACCESS_CREATE:
  585. case OCFS2_JOURNAL_ACCESS_WRITE:
  586. status = jbd2_journal_get_write_access(handle, bh);
  587. break;
  588. case OCFS2_JOURNAL_ACCESS_UNDO:
  589. status = jbd2_journal_get_undo_access(handle, bh);
  590. break;
  591. default:
  592. status = -EINVAL;
  593. mlog(ML_ERROR, "Unknown access type!\n");
  594. }
  595. if (!status && ocfs2_meta_ecc(osb) && triggers)
  596. jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
  597. ocfs2_metadata_cache_io_unlock(ci);
  598. if (status < 0)
  599. mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
  600. status, type);
  601. return status;
  602. }
  603. int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci,
  604. struct buffer_head *bh, int type)
  605. {
  606. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  607. return __ocfs2_journal_access(handle, ci, bh,
  608. &osb->s_journal_triggers[OCFS2_JTR_DI],
  609. type);
  610. }
  611. int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci,
  612. struct buffer_head *bh, int type)
  613. {
  614. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  615. return __ocfs2_journal_access(handle, ci, bh,
  616. &osb->s_journal_triggers[OCFS2_JTR_EB],
  617. type);
  618. }
  619. int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci,
  620. struct buffer_head *bh, int type)
  621. {
  622. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  623. return __ocfs2_journal_access(handle, ci, bh,
  624. &osb->s_journal_triggers[OCFS2_JTR_RB],
  625. type);
  626. }
  627. int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci,
  628. struct buffer_head *bh, int type)
  629. {
  630. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  631. return __ocfs2_journal_access(handle, ci, bh,
  632. &osb->s_journal_triggers[OCFS2_JTR_GD],
  633. type);
  634. }
  635. int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci,
  636. struct buffer_head *bh, int type)
  637. {
  638. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  639. return __ocfs2_journal_access(handle, ci, bh,
  640. &osb->s_journal_triggers[OCFS2_JTR_DB],
  641. type);
  642. }
  643. int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci,
  644. struct buffer_head *bh, int type)
  645. {
  646. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  647. return __ocfs2_journal_access(handle, ci, bh,
  648. &osb->s_journal_triggers[OCFS2_JTR_XB],
  649. type);
  650. }
  651. int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci,
  652. struct buffer_head *bh, int type)
  653. {
  654. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  655. return __ocfs2_journal_access(handle, ci, bh,
  656. &osb->s_journal_triggers[OCFS2_JTR_DQ],
  657. type);
  658. }
  659. int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci,
  660. struct buffer_head *bh, int type)
  661. {
  662. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  663. return __ocfs2_journal_access(handle, ci, bh,
  664. &osb->s_journal_triggers[OCFS2_JTR_DR],
  665. type);
  666. }
  667. int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci,
  668. struct buffer_head *bh, int type)
  669. {
  670. struct ocfs2_super *osb = OCFS2_SB(ocfs2_metadata_cache_get_super(ci));
  671. return __ocfs2_journal_access(handle, ci, bh,
  672. &osb->s_journal_triggers[OCFS2_JTR_DL],
  673. type);
  674. }
  675. int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci,
  676. struct buffer_head *bh, int type)
  677. {
  678. return __ocfs2_journal_access(handle, ci, bh, NULL, type);
  679. }
  680. void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh)
  681. {
  682. int status;
  683. trace_ocfs2_journal_dirty((unsigned long long)bh->b_blocknr);
  684. status = jbd2_journal_dirty_metadata(handle, bh);
  685. if (status) {
  686. mlog_errno(status);
  687. if (!is_handle_aborted(handle)) {
  688. journal_t *journal = handle->h_transaction->t_journal;
  689. mlog(ML_ERROR, "jbd2_journal_dirty_metadata failed: "
  690. "handle type %u started at line %u, credits %u/%u "
  691. "errcode %d. Aborting transaction and journal.\n",
  692. handle->h_type, handle->h_line_no,
  693. handle->h_requested_credits,
  694. jbd2_handle_buffer_credits(handle), status);
  695. handle->h_err = status;
  696. jbd2_journal_abort_handle(handle);
  697. jbd2_journal_abort(journal, status);
  698. }
  699. }
  700. }
  701. #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
  702. void ocfs2_set_journal_params(struct ocfs2_super *osb)
  703. {
  704. journal_t *journal = osb->journal->j_journal;
  705. unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
  706. if (osb->osb_commit_interval)
  707. commit_interval = osb->osb_commit_interval;
  708. write_lock(&journal->j_state_lock);
  709. journal->j_commit_interval = commit_interval;
  710. if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
  711. journal->j_flags |= JBD2_BARRIER;
  712. else
  713. journal->j_flags &= ~JBD2_BARRIER;
  714. write_unlock(&journal->j_state_lock);
  715. }
  716. /*
  717. * alloc & initialize skeleton for journal structure.
  718. * ocfs2_journal_init() will make fs have journal ability.
  719. */
  720. int ocfs2_journal_alloc(struct ocfs2_super *osb)
  721. {
  722. int status = 0;
  723. struct ocfs2_journal *journal;
  724. journal = kzalloc_obj(struct ocfs2_journal);
  725. if (!journal) {
  726. mlog(ML_ERROR, "unable to alloc journal\n");
  727. status = -ENOMEM;
  728. goto bail;
  729. }
  730. osb->journal = journal;
  731. journal->j_osb = osb;
  732. atomic_set(&journal->j_num_trans, 0);
  733. init_rwsem(&journal->j_trans_barrier);
  734. init_waitqueue_head(&journal->j_checkpointed);
  735. spin_lock_init(&journal->j_lock);
  736. journal->j_trans_id = 1UL;
  737. INIT_LIST_HEAD(&journal->j_la_cleanups);
  738. INIT_WORK(&journal->j_recovery_work, ocfs2_complete_recovery);
  739. journal->j_state = OCFS2_JOURNAL_FREE;
  740. bail:
  741. return status;
  742. }
  743. static int ocfs2_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
  744. {
  745. return filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping,
  746. jinode->i_dirty_start, jinode->i_dirty_end);
  747. }
  748. int ocfs2_journal_init(struct ocfs2_super *osb, int *dirty)
  749. {
  750. int status = -1;
  751. struct inode *inode = NULL; /* the journal inode */
  752. journal_t *j_journal = NULL;
  753. struct ocfs2_journal *journal = osb->journal;
  754. struct ocfs2_dinode *di = NULL;
  755. struct buffer_head *bh = NULL;
  756. int inode_lock = 0;
  757. BUG_ON(!journal);
  758. /* already have the inode for our journal */
  759. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  760. osb->slot_num);
  761. if (inode == NULL) {
  762. status = -EACCES;
  763. mlog_errno(status);
  764. goto done;
  765. }
  766. if (is_bad_inode(inode)) {
  767. mlog(ML_ERROR, "access error (bad inode)\n");
  768. iput(inode);
  769. inode = NULL;
  770. status = -EACCES;
  771. goto done;
  772. }
  773. SET_INODE_JOURNAL(inode);
  774. OCFS2_I(inode)->ip_open_count++;
  775. /* Skip recovery waits here - journal inode metadata never
  776. * changes in a live cluster so it can be considered an
  777. * exception to the rule. */
  778. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  779. if (status < 0) {
  780. if (status != -ERESTARTSYS)
  781. mlog(ML_ERROR, "Could not get lock on journal!\n");
  782. goto done;
  783. }
  784. inode_lock = 1;
  785. di = (struct ocfs2_dinode *)bh->b_data;
  786. if (i_size_read(inode) < OCFS2_MIN_JOURNAL_SIZE) {
  787. mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
  788. i_size_read(inode));
  789. status = -EINVAL;
  790. goto done;
  791. }
  792. trace_ocfs2_journal_init(i_size_read(inode),
  793. (unsigned long long)inode->i_blocks,
  794. OCFS2_I(inode)->ip_clusters);
  795. /* call the kernels journal init function now */
  796. j_journal = jbd2_journal_init_inode(inode);
  797. if (IS_ERR(j_journal)) {
  798. mlog(ML_ERROR, "Linux journal layer error\n");
  799. status = PTR_ERR(j_journal);
  800. goto done;
  801. }
  802. trace_ocfs2_journal_init_maxlen(j_journal->j_total_len);
  803. *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
  804. OCFS2_JOURNAL_DIRTY_FL);
  805. journal->j_journal = j_journal;
  806. journal->j_journal->j_submit_inode_data_buffers =
  807. ocfs2_journal_submit_inode_data_buffers;
  808. journal->j_journal->j_finish_inode_data_buffers =
  809. jbd2_journal_finish_inode_data_buffers;
  810. journal->j_inode = inode;
  811. journal->j_bh = bh;
  812. ocfs2_set_journal_params(osb);
  813. journal->j_state = OCFS2_JOURNAL_LOADED;
  814. status = 0;
  815. done:
  816. if (status < 0) {
  817. if (inode_lock)
  818. ocfs2_inode_unlock(inode, 1);
  819. brelse(bh);
  820. if (inode) {
  821. OCFS2_I(inode)->ip_open_count--;
  822. iput(inode);
  823. }
  824. }
  825. return status;
  826. }
  827. static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
  828. {
  829. le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
  830. }
  831. static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
  832. {
  833. return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
  834. }
  835. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  836. int dirty, int replayed)
  837. {
  838. int status;
  839. unsigned int flags;
  840. struct ocfs2_journal *journal = osb->journal;
  841. struct buffer_head *bh = journal->j_bh;
  842. struct ocfs2_dinode *fe;
  843. fe = (struct ocfs2_dinode *)bh->b_data;
  844. /* The journal bh on the osb always comes from ocfs2_journal_init()
  845. * and was validated there inside ocfs2_inode_lock_full(). It's a
  846. * code bug if we mess it up. */
  847. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  848. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  849. if (dirty)
  850. flags |= OCFS2_JOURNAL_DIRTY_FL;
  851. else
  852. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  853. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  854. if (replayed)
  855. ocfs2_bump_recovery_generation(fe);
  856. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
  857. status = ocfs2_write_block(osb, bh, INODE_CACHE(journal->j_inode));
  858. if (status < 0)
  859. mlog_errno(status);
  860. return status;
  861. }
  862. /*
  863. * If the journal has been kmalloc'd it needs to be freed after this
  864. * call.
  865. */
  866. void ocfs2_journal_shutdown(struct ocfs2_super *osb)
  867. {
  868. struct ocfs2_journal *journal = NULL;
  869. int status = 0;
  870. struct inode *inode = NULL;
  871. int num_running_trans = 0;
  872. BUG_ON(!osb);
  873. journal = osb->journal;
  874. if (!journal)
  875. goto done;
  876. inode = journal->j_inode;
  877. if (journal->j_state != OCFS2_JOURNAL_LOADED)
  878. goto done;
  879. /* need to inc inode use count - jbd2_journal_destroy will iput. */
  880. if (!igrab(inode))
  881. BUG();
  882. num_running_trans = atomic_read(&(journal->j_num_trans));
  883. trace_ocfs2_journal_shutdown(num_running_trans);
  884. /* Do a commit_cache here. It will flush our journal, *and*
  885. * release any locks that are still held.
  886. * set the SHUTDOWN flag and release the trans lock.
  887. * the commit thread will take the trans lock for us below. */
  888. journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
  889. /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
  890. * drop the trans_lock (which we want to hold until we
  891. * completely destroy the journal. */
  892. if (osb->commit_task) {
  893. /* Wait for the commit thread */
  894. trace_ocfs2_journal_shutdown_wait(osb->commit_task);
  895. kthread_stop(osb->commit_task);
  896. osb->commit_task = NULL;
  897. }
  898. BUG_ON(atomic_read(&(journal->j_num_trans)) != 0);
  899. if (ocfs2_mount_local(osb) &&
  900. (journal->j_journal->j_flags & JBD2_LOADED)) {
  901. jbd2_journal_lock_updates(journal->j_journal);
  902. status = jbd2_journal_flush(journal->j_journal, 0);
  903. jbd2_journal_unlock_updates(journal->j_journal);
  904. if (status < 0)
  905. mlog_errno(status);
  906. }
  907. /* Shutdown the kernel journal system */
  908. if (!jbd2_journal_destroy(journal->j_journal) && !status) {
  909. /*
  910. * Do not toggle if flush was unsuccessful otherwise
  911. * will leave dirty metadata in a "clean" journal
  912. */
  913. status = ocfs2_journal_toggle_dirty(osb, 0, 0);
  914. if (status < 0)
  915. mlog_errno(status);
  916. }
  917. journal->j_journal = NULL;
  918. OCFS2_I(inode)->ip_open_count--;
  919. /* unlock our journal */
  920. ocfs2_inode_unlock(inode, 1);
  921. brelse(journal->j_bh);
  922. journal->j_bh = NULL;
  923. journal->j_state = OCFS2_JOURNAL_FREE;
  924. done:
  925. iput(inode);
  926. kfree(journal);
  927. osb->journal = NULL;
  928. }
  929. static void ocfs2_clear_journal_error(struct super_block *sb,
  930. journal_t *journal,
  931. int slot)
  932. {
  933. int olderr;
  934. olderr = jbd2_journal_errno(journal);
  935. if (olderr) {
  936. mlog(ML_ERROR, "File system error %d recorded in "
  937. "journal %u.\n", olderr, slot);
  938. mlog(ML_ERROR, "File system on device %s needs checking.\n",
  939. sb->s_id);
  940. jbd2_journal_ack_err(journal);
  941. jbd2_journal_clear_err(journal);
  942. }
  943. }
  944. int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
  945. {
  946. int status = 0;
  947. struct ocfs2_super *osb;
  948. BUG_ON(!journal);
  949. osb = journal->j_osb;
  950. status = jbd2_journal_load(journal->j_journal);
  951. if (status < 0) {
  952. mlog(ML_ERROR, "Failed to load journal!\n");
  953. goto done;
  954. }
  955. ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
  956. if (replayed) {
  957. jbd2_journal_lock_updates(journal->j_journal);
  958. status = jbd2_journal_flush(journal->j_journal, 0);
  959. jbd2_journal_unlock_updates(journal->j_journal);
  960. if (status < 0)
  961. mlog_errno(status);
  962. }
  963. status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
  964. if (status < 0) {
  965. mlog_errno(status);
  966. goto done;
  967. }
  968. /* Launch the commit thread */
  969. if (!local) {
  970. osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
  971. "ocfs2cmt-%s", osb->uuid_str);
  972. if (IS_ERR(osb->commit_task)) {
  973. status = PTR_ERR(osb->commit_task);
  974. osb->commit_task = NULL;
  975. mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
  976. "error=%d", status);
  977. goto done;
  978. }
  979. } else
  980. osb->commit_task = NULL;
  981. done:
  982. return status;
  983. }
  984. /* 'full' flag tells us whether we clear out all blocks or if we just
  985. * mark the journal clean */
  986. int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
  987. {
  988. int status;
  989. BUG_ON(!journal);
  990. status = jbd2_journal_wipe(journal->j_journal, full);
  991. if (status < 0) {
  992. mlog_errno(status);
  993. goto bail;
  994. }
  995. status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
  996. if (status < 0)
  997. mlog_errno(status);
  998. bail:
  999. return status;
  1000. }
  1001. static int ocfs2_recovery_completed(struct ocfs2_super *osb)
  1002. {
  1003. int empty;
  1004. struct ocfs2_recovery_map *rm = osb->recovery_map;
  1005. spin_lock(&osb->osb_lock);
  1006. empty = (rm->rm_used == 0);
  1007. spin_unlock(&osb->osb_lock);
  1008. return empty;
  1009. }
  1010. void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
  1011. {
  1012. wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
  1013. }
  1014. /*
  1015. * JBD Might read a cached version of another nodes journal file. We
  1016. * don't want this as this file changes often and we get no
  1017. * notification on those changes. The only way to be sure that we've
  1018. * got the most up to date version of those blocks then is to force
  1019. * read them off disk. Just searching through the buffer cache won't
  1020. * work as there may be pages backing this file which are still marked
  1021. * up to date. We know things can't change on this file underneath us
  1022. * as we have the lock by now :)
  1023. */
  1024. static int ocfs2_force_read_journal(struct inode *inode)
  1025. {
  1026. int status = 0;
  1027. int i;
  1028. u64 v_blkno, p_blkno, p_blocks, num_blocks;
  1029. struct buffer_head *bh = NULL;
  1030. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1031. num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  1032. v_blkno = 0;
  1033. while (v_blkno < num_blocks) {
  1034. status = ocfs2_extent_map_get_blocks(inode, v_blkno,
  1035. &p_blkno, &p_blocks, NULL);
  1036. if (status < 0) {
  1037. mlog_errno(status);
  1038. goto bail;
  1039. }
  1040. for (i = 0; i < p_blocks; i++, p_blkno++) {
  1041. bh = __find_get_block_nonatomic(osb->sb->s_bdev, p_blkno,
  1042. osb->sb->s_blocksize);
  1043. /* block not cached. */
  1044. if (!bh)
  1045. continue;
  1046. brelse(bh);
  1047. bh = NULL;
  1048. /* We are reading journal data which should not
  1049. * be put in the uptodate cache.
  1050. */
  1051. status = ocfs2_read_blocks_sync(osb, p_blkno, 1, &bh);
  1052. if (status < 0) {
  1053. mlog_errno(status);
  1054. goto bail;
  1055. }
  1056. brelse(bh);
  1057. bh = NULL;
  1058. }
  1059. v_blkno += p_blocks;
  1060. }
  1061. bail:
  1062. return status;
  1063. }
  1064. struct ocfs2_la_recovery_item {
  1065. struct list_head lri_list;
  1066. int lri_slot;
  1067. struct ocfs2_dinode *lri_la_dinode;
  1068. struct ocfs2_dinode *lri_tl_dinode;
  1069. struct ocfs2_quota_recovery *lri_qrec;
  1070. enum ocfs2_orphan_reco_type lri_orphan_reco_type;
  1071. };
  1072. /* Does the second half of the recovery process. By this point, the
  1073. * node is marked clean and can actually be considered recovered,
  1074. * hence it's no longer in the recovery map, but there's still some
  1075. * cleanup we can do which shouldn't happen within the recovery thread
  1076. * as locking in that context becomes very difficult if we are to take
  1077. * recovering nodes into account.
  1078. *
  1079. * NOTE: This function can and will sleep on recovery of other nodes
  1080. * during cluster locking, just like any other ocfs2 process.
  1081. */
  1082. void ocfs2_complete_recovery(struct work_struct *work)
  1083. {
  1084. int ret = 0;
  1085. struct ocfs2_journal *journal =
  1086. container_of(work, struct ocfs2_journal, j_recovery_work);
  1087. struct ocfs2_super *osb = journal->j_osb;
  1088. struct ocfs2_dinode *la_dinode, *tl_dinode;
  1089. struct ocfs2_la_recovery_item *item, *n;
  1090. struct ocfs2_quota_recovery *qrec;
  1091. enum ocfs2_orphan_reco_type orphan_reco_type;
  1092. LIST_HEAD(tmp_la_list);
  1093. trace_ocfs2_complete_recovery(
  1094. (unsigned long long)OCFS2_I(journal->j_inode)->ip_blkno);
  1095. spin_lock(&journal->j_lock);
  1096. list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
  1097. spin_unlock(&journal->j_lock);
  1098. list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
  1099. list_del_init(&item->lri_list);
  1100. ocfs2_wait_on_quotas(osb);
  1101. la_dinode = item->lri_la_dinode;
  1102. tl_dinode = item->lri_tl_dinode;
  1103. qrec = item->lri_qrec;
  1104. orphan_reco_type = item->lri_orphan_reco_type;
  1105. trace_ocfs2_complete_recovery_slot(item->lri_slot,
  1106. la_dinode ? le64_to_cpu(la_dinode->i_blkno) : 0,
  1107. tl_dinode ? le64_to_cpu(tl_dinode->i_blkno) : 0,
  1108. qrec);
  1109. if (la_dinode) {
  1110. ret = ocfs2_complete_local_alloc_recovery(osb,
  1111. la_dinode);
  1112. if (ret < 0)
  1113. mlog_errno(ret);
  1114. kfree(la_dinode);
  1115. }
  1116. if (tl_dinode) {
  1117. ret = ocfs2_complete_truncate_log_recovery(osb,
  1118. tl_dinode);
  1119. if (ret < 0)
  1120. mlog_errno(ret);
  1121. kfree(tl_dinode);
  1122. }
  1123. ret = ocfs2_recover_orphans(osb, item->lri_slot,
  1124. orphan_reco_type);
  1125. if (ret < 0)
  1126. mlog_errno(ret);
  1127. if (qrec) {
  1128. ret = ocfs2_finish_quota_recovery(osb, qrec,
  1129. item->lri_slot);
  1130. if (ret < 0)
  1131. mlog_errno(ret);
  1132. /* Recovery info is already freed now */
  1133. }
  1134. kfree(item);
  1135. }
  1136. trace_ocfs2_complete_recovery_end(ret);
  1137. }
  1138. /* NOTE: This function always eats your references to la_dinode and
  1139. * tl_dinode, either manually on error, or by passing them to
  1140. * ocfs2_complete_recovery */
  1141. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  1142. int slot_num,
  1143. struct ocfs2_dinode *la_dinode,
  1144. struct ocfs2_dinode *tl_dinode,
  1145. struct ocfs2_quota_recovery *qrec,
  1146. enum ocfs2_orphan_reco_type orphan_reco_type)
  1147. {
  1148. struct ocfs2_la_recovery_item *item;
  1149. item = kmalloc_obj(struct ocfs2_la_recovery_item, GFP_NOFS);
  1150. if (!item) {
  1151. /* Though we wish to avoid it, we are in fact safe in
  1152. * skipping local alloc cleanup as fsck.ocfs2 is more
  1153. * than capable of reclaiming unused space. */
  1154. kfree(la_dinode);
  1155. kfree(tl_dinode);
  1156. if (qrec)
  1157. ocfs2_free_quota_recovery(qrec);
  1158. mlog_errno(-ENOMEM);
  1159. return;
  1160. }
  1161. INIT_LIST_HEAD(&item->lri_list);
  1162. item->lri_la_dinode = la_dinode;
  1163. item->lri_slot = slot_num;
  1164. item->lri_tl_dinode = tl_dinode;
  1165. item->lri_qrec = qrec;
  1166. item->lri_orphan_reco_type = orphan_reco_type;
  1167. spin_lock(&journal->j_lock);
  1168. list_add_tail(&item->lri_list, &journal->j_la_cleanups);
  1169. queue_work(journal->j_osb->ocfs2_wq, &journal->j_recovery_work);
  1170. spin_unlock(&journal->j_lock);
  1171. }
  1172. /* Called by the mount code to queue recovery the last part of
  1173. * recovery for it's own and offline slot(s). */
  1174. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
  1175. {
  1176. struct ocfs2_journal *journal = osb->journal;
  1177. if (ocfs2_is_hard_readonly(osb))
  1178. return;
  1179. /* No need to queue up our truncate_log as regular cleanup will catch
  1180. * that */
  1181. ocfs2_queue_recovery_completion(journal, osb->slot_num,
  1182. osb->local_alloc_copy, NULL, NULL,
  1183. ORPHAN_NEED_TRUNCATE);
  1184. ocfs2_schedule_truncate_log_flush(osb, 0);
  1185. osb->local_alloc_copy = NULL;
  1186. /* queue to recover orphan slots for all offline slots */
  1187. ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
  1188. ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
  1189. ocfs2_free_replay_slots(osb);
  1190. }
  1191. void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
  1192. {
  1193. if (osb->quota_rec) {
  1194. ocfs2_queue_recovery_completion(osb->journal,
  1195. osb->slot_num,
  1196. NULL,
  1197. NULL,
  1198. osb->quota_rec,
  1199. ORPHAN_NEED_TRUNCATE);
  1200. osb->quota_rec = NULL;
  1201. }
  1202. }
  1203. static int __ocfs2_recovery_thread(void *arg)
  1204. {
  1205. int status, node_num, slot_num;
  1206. struct ocfs2_super *osb = arg;
  1207. struct ocfs2_recovery_map *rm = osb->recovery_map;
  1208. int *rm_quota = NULL;
  1209. int rm_quota_used = 0, i;
  1210. struct ocfs2_quota_recovery *qrec;
  1211. /* Whether the quota supported. */
  1212. int quota_enabled = OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb,
  1213. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)
  1214. || OCFS2_HAS_RO_COMPAT_FEATURE(osb->sb,
  1215. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA);
  1216. status = ocfs2_wait_on_mount(osb);
  1217. if (status < 0) {
  1218. goto bail;
  1219. }
  1220. if (quota_enabled) {
  1221. rm_quota = kzalloc_objs(int, osb->max_slots, GFP_NOFS);
  1222. if (!rm_quota) {
  1223. status = -ENOMEM;
  1224. goto bail;
  1225. }
  1226. }
  1227. restart:
  1228. if (quota_enabled) {
  1229. mutex_lock(&osb->recovery_lock);
  1230. /* Confirm that recovery thread will no longer recover quotas */
  1231. if (osb->recovery_state == OCFS2_REC_QUOTA_WANT_DISABLE) {
  1232. osb->recovery_state = OCFS2_REC_QUOTA_DISABLED;
  1233. wake_up(&osb->recovery_event);
  1234. }
  1235. if (osb->recovery_state >= OCFS2_REC_QUOTA_DISABLED)
  1236. quota_enabled = 0;
  1237. mutex_unlock(&osb->recovery_lock);
  1238. }
  1239. status = ocfs2_super_lock(osb, 1);
  1240. if (status < 0) {
  1241. mlog_errno(status);
  1242. goto bail;
  1243. }
  1244. status = ocfs2_compute_replay_slots(osb);
  1245. if (status < 0)
  1246. mlog_errno(status);
  1247. /* queue recovery for our own slot */
  1248. ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
  1249. NULL, NULL, ORPHAN_NO_NEED_TRUNCATE);
  1250. spin_lock(&osb->osb_lock);
  1251. while (rm->rm_used) {
  1252. /* It's always safe to remove entry zero, as we won't
  1253. * clear it until ocfs2_recover_node() has succeeded. */
  1254. node_num = rm->rm_entries[0];
  1255. spin_unlock(&osb->osb_lock);
  1256. slot_num = ocfs2_node_num_to_slot(osb, node_num);
  1257. trace_ocfs2_recovery_thread_node(node_num, slot_num);
  1258. if (slot_num == -ENOENT) {
  1259. status = 0;
  1260. goto skip_recovery;
  1261. }
  1262. /* It is a bit subtle with quota recovery. We cannot do it
  1263. * immediately because we have to obtain cluster locks from
  1264. * quota files and we also don't want to just skip it because
  1265. * then quota usage would be out of sync until some node takes
  1266. * the slot. So we remember which nodes need quota recovery
  1267. * and when everything else is done, we recover quotas. */
  1268. if (quota_enabled) {
  1269. for (i = 0; i < rm_quota_used
  1270. && rm_quota[i] != slot_num; i++)
  1271. ;
  1272. if (i == rm_quota_used)
  1273. rm_quota[rm_quota_used++] = slot_num;
  1274. }
  1275. status = ocfs2_recover_node(osb, node_num, slot_num);
  1276. skip_recovery:
  1277. if (!status) {
  1278. ocfs2_recovery_map_clear(osb, node_num);
  1279. } else {
  1280. mlog(ML_ERROR,
  1281. "Error %d recovering node %d on device (%u,%u)!\n",
  1282. status, node_num,
  1283. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1284. mlog(ML_ERROR, "Volume requires unmount.\n");
  1285. }
  1286. spin_lock(&osb->osb_lock);
  1287. }
  1288. spin_unlock(&osb->osb_lock);
  1289. trace_ocfs2_recovery_thread_end(status);
  1290. /* Refresh all journal recovery generations from disk */
  1291. status = ocfs2_check_journals_nolocks(osb);
  1292. status = (status == -EROFS) ? 0 : status;
  1293. if (status < 0)
  1294. mlog_errno(status);
  1295. /* Now it is right time to recover quotas... We have to do this under
  1296. * superblock lock so that no one can start using the slot (and crash)
  1297. * before we recover it */
  1298. if (quota_enabled) {
  1299. for (i = 0; i < rm_quota_used; i++) {
  1300. qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
  1301. if (IS_ERR(qrec)) {
  1302. status = PTR_ERR(qrec);
  1303. mlog_errno(status);
  1304. continue;
  1305. }
  1306. ocfs2_queue_recovery_completion(osb->journal,
  1307. rm_quota[i],
  1308. NULL, NULL, qrec,
  1309. ORPHAN_NEED_TRUNCATE);
  1310. }
  1311. }
  1312. ocfs2_super_unlock(osb, 1);
  1313. /* queue recovery for offline slots */
  1314. ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
  1315. bail:
  1316. mutex_lock(&osb->recovery_lock);
  1317. if (!status && !ocfs2_recovery_completed(osb)) {
  1318. mutex_unlock(&osb->recovery_lock);
  1319. goto restart;
  1320. }
  1321. ocfs2_free_replay_slots(osb);
  1322. osb->recovery_thread_task = NULL;
  1323. if (osb->recovery_state == OCFS2_REC_WANT_DISABLE)
  1324. osb->recovery_state = OCFS2_REC_DISABLED;
  1325. wake_up(&osb->recovery_event);
  1326. mutex_unlock(&osb->recovery_lock);
  1327. kfree(rm_quota);
  1328. return status;
  1329. }
  1330. void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
  1331. {
  1332. int was_set = -1;
  1333. mutex_lock(&osb->recovery_lock);
  1334. if (osb->recovery_state < OCFS2_REC_WANT_DISABLE)
  1335. was_set = ocfs2_recovery_map_set(osb, node_num);
  1336. trace_ocfs2_recovery_thread(node_num, osb->node_num,
  1337. osb->recovery_state, osb->recovery_thread_task, was_set);
  1338. if (osb->recovery_state >= OCFS2_REC_WANT_DISABLE)
  1339. goto out;
  1340. if (osb->recovery_thread_task)
  1341. goto out;
  1342. osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
  1343. "ocfs2rec-%s", osb->uuid_str);
  1344. if (IS_ERR(osb->recovery_thread_task)) {
  1345. mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
  1346. osb->recovery_thread_task = NULL;
  1347. }
  1348. out:
  1349. mutex_unlock(&osb->recovery_lock);
  1350. wake_up(&osb->recovery_event);
  1351. }
  1352. static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
  1353. int slot_num,
  1354. struct buffer_head **bh,
  1355. struct inode **ret_inode)
  1356. {
  1357. int status = -EACCES;
  1358. struct inode *inode = NULL;
  1359. BUG_ON(slot_num >= osb->max_slots);
  1360. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1361. slot_num);
  1362. if (!inode || is_bad_inode(inode)) {
  1363. mlog_errno(status);
  1364. goto bail;
  1365. }
  1366. SET_INODE_JOURNAL(inode);
  1367. status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
  1368. if (status < 0) {
  1369. mlog_errno(status);
  1370. goto bail;
  1371. }
  1372. status = 0;
  1373. bail:
  1374. if (inode) {
  1375. if (status || !ret_inode)
  1376. iput(inode);
  1377. else
  1378. *ret_inode = inode;
  1379. }
  1380. return status;
  1381. }
  1382. /* Does the actual journal replay and marks the journal inode as
  1383. * clean. Will only replay if the journal inode is marked dirty. */
  1384. static int ocfs2_replay_journal(struct ocfs2_super *osb,
  1385. int node_num,
  1386. int slot_num)
  1387. {
  1388. int status;
  1389. int got_lock = 0;
  1390. unsigned int flags;
  1391. struct inode *inode = NULL;
  1392. struct ocfs2_dinode *fe;
  1393. journal_t *journal = NULL;
  1394. struct buffer_head *bh = NULL;
  1395. u32 slot_reco_gen;
  1396. status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
  1397. if (status) {
  1398. mlog_errno(status);
  1399. goto done;
  1400. }
  1401. fe = (struct ocfs2_dinode *)bh->b_data;
  1402. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  1403. brelse(bh);
  1404. bh = NULL;
  1405. /*
  1406. * As the fs recovery is asynchronous, there is a small chance that
  1407. * another node mounted (and recovered) the slot before the recovery
  1408. * thread could get the lock. To handle that, we dirty read the journal
  1409. * inode for that slot to get the recovery generation. If it is
  1410. * different than what we expected, the slot has been recovered.
  1411. * If not, it needs recovery.
  1412. */
  1413. if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
  1414. trace_ocfs2_replay_journal_recovered(slot_num,
  1415. osb->slot_recovery_generations[slot_num], slot_reco_gen);
  1416. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  1417. status = -EBUSY;
  1418. goto done;
  1419. }
  1420. /* Continue with recovery as the journal has not yet been recovered */
  1421. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  1422. if (status < 0) {
  1423. trace_ocfs2_replay_journal_lock_err(status);
  1424. if (status != -ERESTARTSYS)
  1425. mlog(ML_ERROR, "Could not lock journal!\n");
  1426. goto done;
  1427. }
  1428. got_lock = 1;
  1429. fe = (struct ocfs2_dinode *) bh->b_data;
  1430. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  1431. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  1432. if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
  1433. trace_ocfs2_replay_journal_skip(node_num);
  1434. /* Refresh recovery generation for the slot */
  1435. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  1436. goto done;
  1437. }
  1438. /* we need to run complete recovery for offline orphan slots */
  1439. ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
  1440. printk(KERN_NOTICE "ocfs2: Begin replay journal (node %d, slot %d) on "\
  1441. "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
  1442. MINOR(osb->sb->s_dev));
  1443. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1444. status = ocfs2_force_read_journal(inode);
  1445. if (status < 0) {
  1446. mlog_errno(status);
  1447. goto done;
  1448. }
  1449. journal = jbd2_journal_init_inode(inode);
  1450. if (IS_ERR(journal)) {
  1451. mlog(ML_ERROR, "Linux journal layer error\n");
  1452. status = PTR_ERR(journal);
  1453. goto done;
  1454. }
  1455. status = jbd2_journal_load(journal);
  1456. if (status < 0) {
  1457. mlog_errno(status);
  1458. BUG_ON(!igrab(inode));
  1459. jbd2_journal_destroy(journal);
  1460. goto done;
  1461. }
  1462. ocfs2_clear_journal_error(osb->sb, journal, slot_num);
  1463. /* wipe the journal */
  1464. jbd2_journal_lock_updates(journal);
  1465. status = jbd2_journal_flush(journal, 0);
  1466. jbd2_journal_unlock_updates(journal);
  1467. if (status < 0)
  1468. mlog_errno(status);
  1469. /* This will mark the node clean */
  1470. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  1471. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  1472. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  1473. /* Increment recovery generation to indicate successful recovery */
  1474. ocfs2_bump_recovery_generation(fe);
  1475. osb->slot_recovery_generations[slot_num] =
  1476. ocfs2_get_recovery_generation(fe);
  1477. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
  1478. status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
  1479. if (status < 0)
  1480. mlog_errno(status);
  1481. BUG_ON(!igrab(inode));
  1482. jbd2_journal_destroy(journal);
  1483. printk(KERN_NOTICE "ocfs2: End replay journal (node %d, slot %d) on "\
  1484. "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
  1485. MINOR(osb->sb->s_dev));
  1486. done:
  1487. /* drop the lock on this nodes journal */
  1488. if (got_lock)
  1489. ocfs2_inode_unlock(inode, 1);
  1490. iput(inode);
  1491. brelse(bh);
  1492. return status;
  1493. }
  1494. /*
  1495. * Do the most important parts of node recovery:
  1496. * - Replay it's journal
  1497. * - Stamp a clean local allocator file
  1498. * - Stamp a clean truncate log
  1499. * - Mark the node clean
  1500. *
  1501. * If this function completes without error, a node in OCFS2 can be
  1502. * said to have been safely recovered. As a result, failure during the
  1503. * second part of a nodes recovery process (local alloc recovery) is
  1504. * far less concerning.
  1505. */
  1506. static int ocfs2_recover_node(struct ocfs2_super *osb,
  1507. int node_num, int slot_num)
  1508. {
  1509. int status = 0;
  1510. struct ocfs2_dinode *la_copy = NULL;
  1511. struct ocfs2_dinode *tl_copy = NULL;
  1512. trace_ocfs2_recover_node(node_num, slot_num, osb->node_num);
  1513. /* Should not ever be called to recover ourselves -- in that
  1514. * case we should've called ocfs2_journal_load instead. */
  1515. BUG_ON(osb->node_num == node_num);
  1516. status = ocfs2_replay_journal(osb, node_num, slot_num);
  1517. if (status < 0) {
  1518. if (status == -EBUSY) {
  1519. trace_ocfs2_recover_node_skip(slot_num, node_num);
  1520. status = 0;
  1521. goto done;
  1522. }
  1523. mlog_errno(status);
  1524. goto done;
  1525. }
  1526. /* Stamp a clean local alloc file AFTER recovering the journal... */
  1527. status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
  1528. if (status < 0) {
  1529. mlog_errno(status);
  1530. goto done;
  1531. }
  1532. /* An error from begin_truncate_log_recovery is not
  1533. * serious enough to warrant halting the rest of
  1534. * recovery. */
  1535. status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
  1536. if (status < 0)
  1537. mlog_errno(status);
  1538. /* Likewise, this would be a strange but ultimately not so
  1539. * harmful place to get an error... */
  1540. status = ocfs2_clear_slot(osb, slot_num);
  1541. if (status < 0)
  1542. mlog_errno(status);
  1543. /* This will kfree the memory pointed to by la_copy and tl_copy */
  1544. ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
  1545. tl_copy, NULL, ORPHAN_NEED_TRUNCATE);
  1546. status = 0;
  1547. done:
  1548. return status;
  1549. }
  1550. /* Test node liveness by trylocking his journal. If we get the lock,
  1551. * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
  1552. * still alive (we couldn't get the lock) and < 0 on error. */
  1553. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  1554. int slot_num)
  1555. {
  1556. int status, flags;
  1557. struct inode *inode = NULL;
  1558. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1559. slot_num);
  1560. if (inode == NULL) {
  1561. mlog(ML_ERROR, "access error\n");
  1562. status = -EACCES;
  1563. goto bail;
  1564. }
  1565. if (is_bad_inode(inode)) {
  1566. mlog(ML_ERROR, "access error (bad inode)\n");
  1567. iput(inode);
  1568. inode = NULL;
  1569. status = -EACCES;
  1570. goto bail;
  1571. }
  1572. SET_INODE_JOURNAL(inode);
  1573. flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
  1574. status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
  1575. if (status < 0) {
  1576. if (status != -EAGAIN)
  1577. mlog_errno(status);
  1578. goto bail;
  1579. }
  1580. ocfs2_inode_unlock(inode, 1);
  1581. bail:
  1582. iput(inode);
  1583. return status;
  1584. }
  1585. /* Call this underneath ocfs2_super_lock. It also assumes that the
  1586. * slot info struct has been updated from disk. */
  1587. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
  1588. {
  1589. unsigned int node_num;
  1590. int status, i;
  1591. u32 gen;
  1592. struct buffer_head *bh = NULL;
  1593. struct ocfs2_dinode *di;
  1594. /* This is called with the super block cluster lock, so we
  1595. * know that the slot map can't change underneath us. */
  1596. for (i = 0; i < osb->max_slots; i++) {
  1597. /* Read journal inode to get the recovery generation */
  1598. status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
  1599. if (status) {
  1600. mlog_errno(status);
  1601. goto bail;
  1602. }
  1603. di = (struct ocfs2_dinode *)bh->b_data;
  1604. gen = ocfs2_get_recovery_generation(di);
  1605. brelse(bh);
  1606. bh = NULL;
  1607. spin_lock(&osb->osb_lock);
  1608. osb->slot_recovery_generations[i] = gen;
  1609. trace_ocfs2_mark_dead_nodes(i,
  1610. osb->slot_recovery_generations[i]);
  1611. if (i == osb->slot_num) {
  1612. spin_unlock(&osb->osb_lock);
  1613. continue;
  1614. }
  1615. status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
  1616. if (status == -ENOENT) {
  1617. spin_unlock(&osb->osb_lock);
  1618. continue;
  1619. }
  1620. if (__ocfs2_recovery_map_test(osb, node_num)) {
  1621. spin_unlock(&osb->osb_lock);
  1622. continue;
  1623. }
  1624. spin_unlock(&osb->osb_lock);
  1625. /* Ok, we have a slot occupied by another node which
  1626. * is not in the recovery map. We trylock his journal
  1627. * file here to test if he's alive. */
  1628. status = ocfs2_trylock_journal(osb, i);
  1629. if (!status) {
  1630. /* Since we're called from mount, we know that
  1631. * the recovery thread can't race us on
  1632. * setting / checking the recovery bits. */
  1633. ocfs2_recovery_thread(osb, node_num);
  1634. } else if ((status < 0) && (status != -EAGAIN)) {
  1635. mlog_errno(status);
  1636. goto bail;
  1637. }
  1638. }
  1639. status = 0;
  1640. bail:
  1641. return status;
  1642. }
  1643. /*
  1644. * Scan timer should get fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT. Add some
  1645. * randomness to the timeout to minimize multiple nodes firing the timer at the
  1646. * same time.
  1647. */
  1648. static inline unsigned long ocfs2_orphan_scan_timeout(void)
  1649. {
  1650. unsigned long time;
  1651. get_random_bytes(&time, sizeof(time));
  1652. time = ORPHAN_SCAN_SCHEDULE_TIMEOUT + (time % 5000);
  1653. return msecs_to_jiffies(time);
  1654. }
  1655. /*
  1656. * ocfs2_queue_orphan_scan calls ocfs2_queue_recovery_completion for
  1657. * every slot, queuing a recovery of the slot on the ocfs2_wq thread. This
  1658. * is done to catch any orphans that are left over in orphan directories.
  1659. *
  1660. * It scans all slots, even ones that are in use. It does so to handle the
  1661. * case described below:
  1662. *
  1663. * Node 1 has an inode it was using. The dentry went away due to memory
  1664. * pressure. Node 1 closes the inode, but it's on the free list. The node
  1665. * has the open lock.
  1666. * Node 2 unlinks the inode. It grabs the dentry lock to notify others,
  1667. * but node 1 has no dentry and doesn't get the message. It trylocks the
  1668. * open lock, sees that another node has a PR, and does nothing.
  1669. * Later node 2 runs its orphan dir. It igets the inode, trylocks the
  1670. * open lock, sees the PR still, and does nothing.
  1671. * Basically, we have to trigger an orphan iput on node 1. The only way
  1672. * for this to happen is if node 1 runs node 2's orphan dir.
  1673. *
  1674. * ocfs2_queue_orphan_scan gets called every ORPHAN_SCAN_SCHEDULE_TIMEOUT
  1675. * seconds. It gets an EX lock on os_lockres and checks sequence number
  1676. * stored in LVB. If the sequence number has changed, it means some other
  1677. * node has done the scan. This node skips the scan and tracks the
  1678. * sequence number. If the sequence number didn't change, it means a scan
  1679. * hasn't happened. The node queues a scan and increments the
  1680. * sequence number in the LVB.
  1681. */
  1682. static void ocfs2_queue_orphan_scan(struct ocfs2_super *osb)
  1683. {
  1684. struct ocfs2_orphan_scan *os;
  1685. int status, i;
  1686. u32 seqno = 0;
  1687. os = &osb->osb_orphan_scan;
  1688. if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
  1689. goto out;
  1690. trace_ocfs2_queue_orphan_scan_begin(os->os_count, os->os_seqno,
  1691. atomic_read(&os->os_state));
  1692. status = ocfs2_orphan_scan_lock(osb, &seqno);
  1693. if (status < 0) {
  1694. if (status != -EAGAIN)
  1695. mlog_errno(status);
  1696. goto out;
  1697. }
  1698. /* Do no queue the tasks if the volume is being umounted */
  1699. if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
  1700. goto unlock;
  1701. if (os->os_seqno != seqno) {
  1702. os->os_seqno = seqno;
  1703. goto unlock;
  1704. }
  1705. for (i = 0; i < osb->max_slots; i++)
  1706. ocfs2_queue_recovery_completion(osb->journal, i, NULL, NULL,
  1707. NULL, ORPHAN_NO_NEED_TRUNCATE);
  1708. /*
  1709. * We queued a recovery on orphan slots, increment the sequence
  1710. * number and update LVB so other node will skip the scan for a while
  1711. */
  1712. seqno++;
  1713. os->os_count++;
  1714. os->os_scantime = ktime_get_seconds();
  1715. unlock:
  1716. ocfs2_orphan_scan_unlock(osb, seqno);
  1717. out:
  1718. trace_ocfs2_queue_orphan_scan_end(os->os_count, os->os_seqno,
  1719. atomic_read(&os->os_state));
  1720. return;
  1721. }
  1722. /* Worker task that gets fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT millsec */
  1723. static void ocfs2_orphan_scan_work(struct work_struct *work)
  1724. {
  1725. struct ocfs2_orphan_scan *os;
  1726. struct ocfs2_super *osb;
  1727. os = container_of(work, struct ocfs2_orphan_scan,
  1728. os_orphan_scan_work.work);
  1729. osb = os->os_osb;
  1730. mutex_lock(&os->os_lock);
  1731. ocfs2_queue_orphan_scan(osb);
  1732. if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE)
  1733. queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
  1734. ocfs2_orphan_scan_timeout());
  1735. mutex_unlock(&os->os_lock);
  1736. }
  1737. void ocfs2_orphan_scan_stop(struct ocfs2_super *osb)
  1738. {
  1739. struct ocfs2_orphan_scan *os;
  1740. os = &osb->osb_orphan_scan;
  1741. if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE) {
  1742. atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
  1743. mutex_lock(&os->os_lock);
  1744. cancel_delayed_work(&os->os_orphan_scan_work);
  1745. mutex_unlock(&os->os_lock);
  1746. }
  1747. }
  1748. void ocfs2_orphan_scan_init(struct ocfs2_super *osb)
  1749. {
  1750. struct ocfs2_orphan_scan *os;
  1751. os = &osb->osb_orphan_scan;
  1752. os->os_osb = osb;
  1753. os->os_count = 0;
  1754. os->os_seqno = 0;
  1755. mutex_init(&os->os_lock);
  1756. INIT_DELAYED_WORK(&os->os_orphan_scan_work, ocfs2_orphan_scan_work);
  1757. }
  1758. void ocfs2_orphan_scan_start(struct ocfs2_super *osb)
  1759. {
  1760. struct ocfs2_orphan_scan *os;
  1761. os = &osb->osb_orphan_scan;
  1762. os->os_scantime = ktime_get_seconds();
  1763. if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
  1764. atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
  1765. else {
  1766. atomic_set(&os->os_state, ORPHAN_SCAN_ACTIVE);
  1767. queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
  1768. ocfs2_orphan_scan_timeout());
  1769. }
  1770. }
  1771. struct ocfs2_orphan_filldir_priv {
  1772. struct dir_context ctx;
  1773. struct inode *head;
  1774. struct ocfs2_super *osb;
  1775. enum ocfs2_orphan_reco_type orphan_reco_type;
  1776. };
  1777. static bool ocfs2_orphan_filldir(struct dir_context *ctx, const char *name,
  1778. int name_len, loff_t pos, u64 ino,
  1779. unsigned type)
  1780. {
  1781. struct ocfs2_orphan_filldir_priv *p =
  1782. container_of(ctx, struct ocfs2_orphan_filldir_priv, ctx);
  1783. struct inode *iter;
  1784. if (name_len == 1 && !strncmp(".", name, 1))
  1785. return true;
  1786. if (name_len == 2 && !strncmp("..", name, 2))
  1787. return true;
  1788. /* do not include dio entry in case of orphan scan */
  1789. if ((p->orphan_reco_type == ORPHAN_NO_NEED_TRUNCATE) &&
  1790. (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
  1791. OCFS2_DIO_ORPHAN_PREFIX_LEN)))
  1792. return true;
  1793. /* Skip bad inodes so that recovery can continue */
  1794. iter = ocfs2_iget(p->osb, ino,
  1795. OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
  1796. if (IS_ERR(iter))
  1797. return true;
  1798. if (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
  1799. OCFS2_DIO_ORPHAN_PREFIX_LEN))
  1800. OCFS2_I(iter)->ip_flags |= OCFS2_INODE_DIO_ORPHAN_ENTRY;
  1801. /* Skip inodes which are already added to recover list, since dio may
  1802. * happen concurrently with unlink/rename */
  1803. if (OCFS2_I(iter)->ip_next_orphan) {
  1804. iput(iter);
  1805. return true;
  1806. }
  1807. trace_ocfs2_orphan_filldir((unsigned long long)OCFS2_I(iter)->ip_blkno);
  1808. /* No locking is required for the next_orphan queue as there
  1809. * is only ever a single process doing orphan recovery. */
  1810. OCFS2_I(iter)->ip_next_orphan = p->head;
  1811. p->head = iter;
  1812. return true;
  1813. }
  1814. static int ocfs2_queue_orphans(struct ocfs2_super *osb,
  1815. int slot,
  1816. struct inode **head,
  1817. enum ocfs2_orphan_reco_type orphan_reco_type)
  1818. {
  1819. int status;
  1820. struct inode *orphan_dir_inode = NULL;
  1821. struct ocfs2_orphan_filldir_priv priv = {
  1822. .ctx.actor = ocfs2_orphan_filldir,
  1823. .osb = osb,
  1824. .head = *head,
  1825. .orphan_reco_type = orphan_reco_type
  1826. };
  1827. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1828. ORPHAN_DIR_SYSTEM_INODE,
  1829. slot);
  1830. if (!orphan_dir_inode) {
  1831. status = -ENOENT;
  1832. mlog_errno(status);
  1833. return status;
  1834. }
  1835. inode_lock(orphan_dir_inode);
  1836. status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
  1837. if (status < 0) {
  1838. mlog_errno(status);
  1839. goto out;
  1840. }
  1841. status = ocfs2_dir_foreach(orphan_dir_inode, &priv.ctx);
  1842. if (status) {
  1843. mlog_errno(status);
  1844. goto out_cluster;
  1845. }
  1846. *head = priv.head;
  1847. out_cluster:
  1848. ocfs2_inode_unlock(orphan_dir_inode, 0);
  1849. out:
  1850. inode_unlock(orphan_dir_inode);
  1851. iput(orphan_dir_inode);
  1852. return status;
  1853. }
  1854. static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
  1855. int slot)
  1856. {
  1857. int ret;
  1858. spin_lock(&osb->osb_lock);
  1859. ret = !osb->osb_orphan_wipes[slot];
  1860. spin_unlock(&osb->osb_lock);
  1861. return ret;
  1862. }
  1863. static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
  1864. int slot)
  1865. {
  1866. spin_lock(&osb->osb_lock);
  1867. /* Mark ourselves such that new processes in delete_inode()
  1868. * know to quit early. */
  1869. ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1870. while (osb->osb_orphan_wipes[slot]) {
  1871. /* If any processes are already in the middle of an
  1872. * orphan wipe on this dir, then we need to wait for
  1873. * them. */
  1874. spin_unlock(&osb->osb_lock);
  1875. wait_event_interruptible(osb->osb_wipe_event,
  1876. ocfs2_orphan_recovery_can_continue(osb, slot));
  1877. spin_lock(&osb->osb_lock);
  1878. }
  1879. spin_unlock(&osb->osb_lock);
  1880. }
  1881. static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
  1882. int slot)
  1883. {
  1884. ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1885. }
  1886. /*
  1887. * Orphan recovery. Each mounted node has it's own orphan dir which we
  1888. * must run during recovery. Our strategy here is to build a list of
  1889. * the inodes in the orphan dir and iget/iput them. The VFS does
  1890. * (most) of the rest of the work.
  1891. *
  1892. * Orphan recovery can happen at any time, not just mount so we have a
  1893. * couple of extra considerations.
  1894. *
  1895. * - We grab as many inodes as we can under the orphan dir lock -
  1896. * doing iget() outside the orphan dir risks getting a reference on
  1897. * an invalid inode.
  1898. * - We must be sure not to deadlock with other processes on the
  1899. * system wanting to run delete_inode(). This can happen when they go
  1900. * to lock the orphan dir and the orphan recovery process attempts to
  1901. * iget() inside the orphan dir lock. This can be avoided by
  1902. * advertising our state to ocfs2_delete_inode().
  1903. */
  1904. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  1905. int slot,
  1906. enum ocfs2_orphan_reco_type orphan_reco_type)
  1907. {
  1908. int ret = 0;
  1909. struct inode *inode = NULL;
  1910. struct inode *iter;
  1911. struct ocfs2_inode_info *oi;
  1912. struct buffer_head *di_bh = NULL;
  1913. struct ocfs2_dinode *di = NULL;
  1914. trace_ocfs2_recover_orphans(slot);
  1915. ocfs2_mark_recovering_orphan_dir(osb, slot);
  1916. ret = ocfs2_queue_orphans(osb, slot, &inode, orphan_reco_type);
  1917. ocfs2_clear_recovering_orphan_dir(osb, slot);
  1918. /* Error here should be noted, but we want to continue with as
  1919. * many queued inodes as we've got. */
  1920. if (ret)
  1921. mlog_errno(ret);
  1922. while (inode) {
  1923. oi = OCFS2_I(inode);
  1924. trace_ocfs2_recover_orphans_iput(
  1925. (unsigned long long)oi->ip_blkno);
  1926. iter = oi->ip_next_orphan;
  1927. oi->ip_next_orphan = NULL;
  1928. if (oi->ip_flags & OCFS2_INODE_DIO_ORPHAN_ENTRY) {
  1929. inode_lock(inode);
  1930. ret = ocfs2_rw_lock(inode, 1);
  1931. if (ret < 0) {
  1932. mlog_errno(ret);
  1933. goto unlock_mutex;
  1934. }
  1935. /*
  1936. * We need to take and drop the inode lock to
  1937. * force read inode from disk.
  1938. */
  1939. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1940. if (ret) {
  1941. mlog_errno(ret);
  1942. goto unlock_rw;
  1943. }
  1944. di = (struct ocfs2_dinode *)di_bh->b_data;
  1945. if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) {
  1946. ret = ocfs2_truncate_file(inode, di_bh,
  1947. i_size_read(inode));
  1948. if (ret < 0) {
  1949. if (ret != -ENOSPC)
  1950. mlog_errno(ret);
  1951. goto unlock_inode;
  1952. }
  1953. ret = ocfs2_del_inode_from_orphan(osb, inode,
  1954. di_bh, 0, 0);
  1955. if (ret)
  1956. mlog_errno(ret);
  1957. }
  1958. unlock_inode:
  1959. ocfs2_inode_unlock(inode, 1);
  1960. brelse(di_bh);
  1961. di_bh = NULL;
  1962. unlock_rw:
  1963. ocfs2_rw_unlock(inode, 1);
  1964. unlock_mutex:
  1965. inode_unlock(inode);
  1966. /* clear dio flag in ocfs2_inode_info */
  1967. oi->ip_flags &= ~OCFS2_INODE_DIO_ORPHAN_ENTRY;
  1968. } else {
  1969. spin_lock(&oi->ip_lock);
  1970. /* Set the proper information to get us going into
  1971. * ocfs2_delete_inode. */
  1972. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  1973. spin_unlock(&oi->ip_lock);
  1974. }
  1975. iput(inode);
  1976. inode = iter;
  1977. }
  1978. return ret;
  1979. }
  1980. static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
  1981. {
  1982. /* This check is good because ocfs2 will wait on our recovery
  1983. * thread before changing it to something other than MOUNTED
  1984. * or DISABLED. */
  1985. wait_event(osb->osb_mount_event,
  1986. (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
  1987. atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
  1988. atomic_read(&osb->vol_state) == VOLUME_DISABLED);
  1989. /* If there's an error on mount, then we may never get to the
  1990. * MOUNTED flag, but this is set right before
  1991. * dismount_volume() so we can trust it. */
  1992. if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
  1993. trace_ocfs2_wait_on_mount(VOLUME_DISABLED);
  1994. mlog(0, "mount error, exiting!\n");
  1995. return -EBUSY;
  1996. }
  1997. return 0;
  1998. }
  1999. static int ocfs2_commit_thread(void *arg)
  2000. {
  2001. int status;
  2002. struct ocfs2_super *osb = arg;
  2003. struct ocfs2_journal *journal = osb->journal;
  2004. /* we can trust j_num_trans here because _should_stop() is only set in
  2005. * shutdown and nobody other than ourselves should be able to start
  2006. * transactions. committing on shutdown might take a few iterations
  2007. * as final transactions put deleted inodes on the list */
  2008. while (!(kthread_should_stop() &&
  2009. atomic_read(&journal->j_num_trans) == 0)) {
  2010. wait_event_interruptible(osb->checkpoint_event,
  2011. atomic_read(&journal->j_num_trans)
  2012. || kthread_should_stop());
  2013. status = ocfs2_commit_cache(osb);
  2014. if (status < 0) {
  2015. static unsigned long abort_warn_time;
  2016. /* Warn about this once per minute */
  2017. if (printk_timed_ratelimit(&abort_warn_time, 60*HZ))
  2018. mlog(ML_ERROR, "status = %d, journal is "
  2019. "already aborted.\n", status);
  2020. /*
  2021. * After ocfs2_commit_cache() fails, j_num_trans has a
  2022. * non-zero value. Sleep here to avoid a busy-wait
  2023. * loop.
  2024. */
  2025. msleep_interruptible(1000);
  2026. }
  2027. if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
  2028. mlog(ML_KTHREAD,
  2029. "commit_thread: %u transactions pending on "
  2030. "shutdown\n",
  2031. atomic_read(&journal->j_num_trans));
  2032. }
  2033. }
  2034. return 0;
  2035. }
  2036. /* Reads all the journal inodes without taking any cluster locks. Used
  2037. * for hard readonly access to determine whether any journal requires
  2038. * recovery. Also used to refresh the recovery generation numbers after
  2039. * a journal has been recovered by another node.
  2040. */
  2041. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
  2042. {
  2043. int ret = 0;
  2044. unsigned int slot;
  2045. struct buffer_head *di_bh = NULL;
  2046. struct ocfs2_dinode *di;
  2047. int journal_dirty = 0;
  2048. for(slot = 0; slot < osb->max_slots; slot++) {
  2049. ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
  2050. if (ret) {
  2051. mlog_errno(ret);
  2052. goto out;
  2053. }
  2054. di = (struct ocfs2_dinode *) di_bh->b_data;
  2055. osb->slot_recovery_generations[slot] =
  2056. ocfs2_get_recovery_generation(di);
  2057. if (le32_to_cpu(di->id1.journal1.ij_flags) &
  2058. OCFS2_JOURNAL_DIRTY_FL)
  2059. journal_dirty = 1;
  2060. brelse(di_bh);
  2061. di_bh = NULL;
  2062. }
  2063. out:
  2064. if (journal_dirty)
  2065. ret = -EROFS;
  2066. return ret;
  2067. }