journal.c 61 KB

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