journal.c 62 KB

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