journal.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363
  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(osb->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\n");
  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, b_state=0x%lx\n",
  550. (unsigned long long)bh->b_blocknr, bh->b_state);
  551. lock_buffer(bh);
  552. /*
  553. * A previous transaction with a couple of buffer heads fail
  554. * to checkpoint, so all the bhs are marked as BH_Write_EIO.
  555. * For current transaction, the bh is just among those error
  556. * bhs which previous transaction handle. We can't just clear
  557. * its BH_Write_EIO and reuse directly, since other bhs are
  558. * not written to disk yet and that will cause metadata
  559. * inconsistency. So we should set fs read-only to avoid
  560. * further damage.
  561. */
  562. if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) {
  563. unlock_buffer(bh);
  564. return ocfs2_error(osb->sb, "A previous attempt to "
  565. "write this buffer head failed\n");
  566. }
  567. unlock_buffer(bh);
  568. }
  569. /* Set the current transaction information on the ci so
  570. * that the locking code knows whether it can drop it's locks
  571. * on this ci or not. We're protected from the commit
  572. * thread updating the current transaction id until
  573. * ocfs2_commit_trans() because ocfs2_start_trans() took
  574. * j_trans_barrier for us. */
  575. ocfs2_set_ci_lock_trans(osb->journal, ci);
  576. ocfs2_metadata_cache_io_lock(ci);
  577. switch (type) {
  578. case OCFS2_JOURNAL_ACCESS_CREATE:
  579. case OCFS2_JOURNAL_ACCESS_WRITE:
  580. status = jbd2_journal_get_write_access(handle, bh);
  581. break;
  582. case OCFS2_JOURNAL_ACCESS_UNDO:
  583. status = jbd2_journal_get_undo_access(handle, bh);
  584. break;
  585. default:
  586. status = -EINVAL;
  587. mlog(ML_ERROR, "Unknown access type!\n");
  588. }
  589. if (!status && ocfs2_meta_ecc(osb) && triggers)
  590. jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
  591. ocfs2_metadata_cache_io_unlock(ci);
  592. if (status < 0)
  593. mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
  594. status, type);
  595. return status;
  596. }
  597. int ocfs2_journal_access_di(handle_t *handle, struct ocfs2_caching_info *ci,
  598. struct buffer_head *bh, int type)
  599. {
  600. return __ocfs2_journal_access(handle, ci, bh, &di_triggers, type);
  601. }
  602. int ocfs2_journal_access_eb(handle_t *handle, struct ocfs2_caching_info *ci,
  603. struct buffer_head *bh, int type)
  604. {
  605. return __ocfs2_journal_access(handle, ci, bh, &eb_triggers, type);
  606. }
  607. int ocfs2_journal_access_rb(handle_t *handle, struct ocfs2_caching_info *ci,
  608. struct buffer_head *bh, int type)
  609. {
  610. return __ocfs2_journal_access(handle, ci, bh, &rb_triggers,
  611. type);
  612. }
  613. int ocfs2_journal_access_gd(handle_t *handle, struct ocfs2_caching_info *ci,
  614. struct buffer_head *bh, int type)
  615. {
  616. return __ocfs2_journal_access(handle, ci, bh, &gd_triggers, type);
  617. }
  618. int ocfs2_journal_access_db(handle_t *handle, struct ocfs2_caching_info *ci,
  619. struct buffer_head *bh, int type)
  620. {
  621. return __ocfs2_journal_access(handle, ci, bh, &db_triggers, type);
  622. }
  623. int ocfs2_journal_access_xb(handle_t *handle, struct ocfs2_caching_info *ci,
  624. struct buffer_head *bh, int type)
  625. {
  626. return __ocfs2_journal_access(handle, ci, bh, &xb_triggers, type);
  627. }
  628. int ocfs2_journal_access_dq(handle_t *handle, struct ocfs2_caching_info *ci,
  629. struct buffer_head *bh, int type)
  630. {
  631. return __ocfs2_journal_access(handle, ci, bh, &dq_triggers, type);
  632. }
  633. int ocfs2_journal_access_dr(handle_t *handle, struct ocfs2_caching_info *ci,
  634. struct buffer_head *bh, int type)
  635. {
  636. return __ocfs2_journal_access(handle, ci, bh, &dr_triggers, type);
  637. }
  638. int ocfs2_journal_access_dl(handle_t *handle, struct ocfs2_caching_info *ci,
  639. struct buffer_head *bh, int type)
  640. {
  641. return __ocfs2_journal_access(handle, ci, bh, &dl_triggers, type);
  642. }
  643. int ocfs2_journal_access(handle_t *handle, struct ocfs2_caching_info *ci,
  644. struct buffer_head *bh, int type)
  645. {
  646. return __ocfs2_journal_access(handle, ci, bh, NULL, type);
  647. }
  648. void ocfs2_journal_dirty(handle_t *handle, struct buffer_head *bh)
  649. {
  650. int status;
  651. trace_ocfs2_journal_dirty((unsigned long long)bh->b_blocknr);
  652. status = jbd2_journal_dirty_metadata(handle, bh);
  653. if (status) {
  654. mlog_errno(status);
  655. if (!is_handle_aborted(handle)) {
  656. journal_t *journal = handle->h_transaction->t_journal;
  657. struct super_block *sb = bh->b_bdev->bd_super;
  658. mlog(ML_ERROR, "jbd2_journal_dirty_metadata failed. "
  659. "Aborting transaction and journal.\n");
  660. handle->h_err = status;
  661. jbd2_journal_abort_handle(handle);
  662. jbd2_journal_abort(journal, status);
  663. ocfs2_abort(sb, "Journal already aborted.\n");
  664. }
  665. }
  666. }
  667. #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
  668. void ocfs2_set_journal_params(struct ocfs2_super *osb)
  669. {
  670. journal_t *journal = osb->journal->j_journal;
  671. unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
  672. if (osb->osb_commit_interval)
  673. commit_interval = osb->osb_commit_interval;
  674. write_lock(&journal->j_state_lock);
  675. journal->j_commit_interval = commit_interval;
  676. if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
  677. journal->j_flags |= JBD2_BARRIER;
  678. else
  679. journal->j_flags &= ~JBD2_BARRIER;
  680. write_unlock(&journal->j_state_lock);
  681. }
  682. int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
  683. {
  684. int status = -1;
  685. struct inode *inode = NULL; /* the journal inode */
  686. journal_t *j_journal = NULL;
  687. struct ocfs2_dinode *di = NULL;
  688. struct buffer_head *bh = NULL;
  689. struct ocfs2_super *osb;
  690. int inode_lock = 0;
  691. BUG_ON(!journal);
  692. osb = journal->j_osb;
  693. /* already have the inode for our journal */
  694. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  695. osb->slot_num);
  696. if (inode == NULL) {
  697. status = -EACCES;
  698. mlog_errno(status);
  699. goto done;
  700. }
  701. if (is_bad_inode(inode)) {
  702. mlog(ML_ERROR, "access error (bad inode)\n");
  703. iput(inode);
  704. inode = NULL;
  705. status = -EACCES;
  706. goto done;
  707. }
  708. SET_INODE_JOURNAL(inode);
  709. OCFS2_I(inode)->ip_open_count++;
  710. /* Skip recovery waits here - journal inode metadata never
  711. * changes in a live cluster so it can be considered an
  712. * exception to the rule. */
  713. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  714. if (status < 0) {
  715. if (status != -ERESTARTSYS)
  716. mlog(ML_ERROR, "Could not get lock on journal!\n");
  717. goto done;
  718. }
  719. inode_lock = 1;
  720. di = (struct ocfs2_dinode *)bh->b_data;
  721. if (i_size_read(inode) < OCFS2_MIN_JOURNAL_SIZE) {
  722. mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
  723. i_size_read(inode));
  724. status = -EINVAL;
  725. goto done;
  726. }
  727. trace_ocfs2_journal_init(i_size_read(inode),
  728. (unsigned long long)inode->i_blocks,
  729. OCFS2_I(inode)->ip_clusters);
  730. /* call the kernels journal init function now */
  731. j_journal = jbd2_journal_init_inode(inode);
  732. if (j_journal == NULL) {
  733. mlog(ML_ERROR, "Linux journal layer error\n");
  734. status = -EINVAL;
  735. goto done;
  736. }
  737. trace_ocfs2_journal_init_maxlen(j_journal->j_maxlen);
  738. *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
  739. OCFS2_JOURNAL_DIRTY_FL);
  740. journal->j_journal = j_journal;
  741. journal->j_inode = inode;
  742. journal->j_bh = bh;
  743. ocfs2_set_journal_params(osb);
  744. journal->j_state = OCFS2_JOURNAL_LOADED;
  745. status = 0;
  746. done:
  747. if (status < 0) {
  748. if (inode_lock)
  749. ocfs2_inode_unlock(inode, 1);
  750. brelse(bh);
  751. if (inode) {
  752. OCFS2_I(inode)->ip_open_count--;
  753. iput(inode);
  754. }
  755. }
  756. return status;
  757. }
  758. static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
  759. {
  760. le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
  761. }
  762. static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
  763. {
  764. return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
  765. }
  766. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  767. int dirty, int replayed)
  768. {
  769. int status;
  770. unsigned int flags;
  771. struct ocfs2_journal *journal = osb->journal;
  772. struct buffer_head *bh = journal->j_bh;
  773. struct ocfs2_dinode *fe;
  774. fe = (struct ocfs2_dinode *)bh->b_data;
  775. /* The journal bh on the osb always comes from ocfs2_journal_init()
  776. * and was validated there inside ocfs2_inode_lock_full(). It's a
  777. * code bug if we mess it up. */
  778. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  779. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  780. if (dirty)
  781. flags |= OCFS2_JOURNAL_DIRTY_FL;
  782. else
  783. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  784. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  785. if (replayed)
  786. ocfs2_bump_recovery_generation(fe);
  787. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
  788. status = ocfs2_write_block(osb, bh, INODE_CACHE(journal->j_inode));
  789. if (status < 0)
  790. mlog_errno(status);
  791. return status;
  792. }
  793. /*
  794. * If the journal has been kmalloc'd it needs to be freed after this
  795. * call.
  796. */
  797. void ocfs2_journal_shutdown(struct ocfs2_super *osb)
  798. {
  799. struct ocfs2_journal *journal = NULL;
  800. int status = 0;
  801. struct inode *inode = NULL;
  802. int num_running_trans = 0;
  803. BUG_ON(!osb);
  804. journal = osb->journal;
  805. if (!journal)
  806. goto done;
  807. inode = journal->j_inode;
  808. if (journal->j_state != OCFS2_JOURNAL_LOADED)
  809. goto done;
  810. /* need to inc inode use count - jbd2_journal_destroy will iput. */
  811. if (!igrab(inode))
  812. BUG();
  813. num_running_trans = atomic_read(&(osb->journal->j_num_trans));
  814. trace_ocfs2_journal_shutdown(num_running_trans);
  815. /* Do a commit_cache here. It will flush our journal, *and*
  816. * release any locks that are still held.
  817. * set the SHUTDOWN flag and release the trans lock.
  818. * the commit thread will take the trans lock for us below. */
  819. journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
  820. /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
  821. * drop the trans_lock (which we want to hold until we
  822. * completely destroy the journal. */
  823. if (osb->commit_task) {
  824. /* Wait for the commit thread */
  825. trace_ocfs2_journal_shutdown_wait(osb->commit_task);
  826. kthread_stop(osb->commit_task);
  827. osb->commit_task = NULL;
  828. }
  829. BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
  830. if (ocfs2_mount_local(osb)) {
  831. jbd2_journal_lock_updates(journal->j_journal);
  832. status = jbd2_journal_flush(journal->j_journal);
  833. jbd2_journal_unlock_updates(journal->j_journal);
  834. if (status < 0)
  835. mlog_errno(status);
  836. }
  837. if (status == 0) {
  838. /*
  839. * Do not toggle if flush was unsuccessful otherwise
  840. * will leave dirty metadata in a "clean" journal
  841. */
  842. status = ocfs2_journal_toggle_dirty(osb, 0, 0);
  843. if (status < 0)
  844. mlog_errno(status);
  845. }
  846. /* Shutdown the kernel journal system */
  847. jbd2_journal_destroy(journal->j_journal);
  848. journal->j_journal = NULL;
  849. OCFS2_I(inode)->ip_open_count--;
  850. /* unlock our journal */
  851. ocfs2_inode_unlock(inode, 1);
  852. brelse(journal->j_bh);
  853. journal->j_bh = NULL;
  854. journal->j_state = OCFS2_JOURNAL_FREE;
  855. // up_write(&journal->j_trans_barrier);
  856. done:
  857. iput(inode);
  858. }
  859. static void ocfs2_clear_journal_error(struct super_block *sb,
  860. journal_t *journal,
  861. int slot)
  862. {
  863. int olderr;
  864. olderr = jbd2_journal_errno(journal);
  865. if (olderr) {
  866. mlog(ML_ERROR, "File system error %d recorded in "
  867. "journal %u.\n", olderr, slot);
  868. mlog(ML_ERROR, "File system on device %s needs checking.\n",
  869. sb->s_id);
  870. jbd2_journal_ack_err(journal);
  871. jbd2_journal_clear_err(journal);
  872. }
  873. }
  874. int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
  875. {
  876. int status = 0;
  877. struct ocfs2_super *osb;
  878. BUG_ON(!journal);
  879. osb = journal->j_osb;
  880. status = jbd2_journal_load(journal->j_journal);
  881. if (status < 0) {
  882. mlog(ML_ERROR, "Failed to load journal!\n");
  883. goto done;
  884. }
  885. ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
  886. status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
  887. if (status < 0) {
  888. mlog_errno(status);
  889. goto done;
  890. }
  891. /* Launch the commit thread */
  892. if (!local) {
  893. osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
  894. "ocfs2cmt-%s", osb->uuid_str);
  895. if (IS_ERR(osb->commit_task)) {
  896. status = PTR_ERR(osb->commit_task);
  897. osb->commit_task = NULL;
  898. mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
  899. "error=%d", status);
  900. goto done;
  901. }
  902. } else
  903. osb->commit_task = NULL;
  904. done:
  905. return status;
  906. }
  907. /* 'full' flag tells us whether we clear out all blocks or if we just
  908. * mark the journal clean */
  909. int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
  910. {
  911. int status;
  912. BUG_ON(!journal);
  913. status = jbd2_journal_wipe(journal->j_journal, full);
  914. if (status < 0) {
  915. mlog_errno(status);
  916. goto bail;
  917. }
  918. status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
  919. if (status < 0)
  920. mlog_errno(status);
  921. bail:
  922. return status;
  923. }
  924. static int ocfs2_recovery_completed(struct ocfs2_super *osb)
  925. {
  926. int empty;
  927. struct ocfs2_recovery_map *rm = osb->recovery_map;
  928. spin_lock(&osb->osb_lock);
  929. empty = (rm->rm_used == 0);
  930. spin_unlock(&osb->osb_lock);
  931. return empty;
  932. }
  933. void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
  934. {
  935. wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
  936. }
  937. /*
  938. * JBD Might read a cached version of another nodes journal file. We
  939. * don't want this as this file changes often and we get no
  940. * notification on those changes. The only way to be sure that we've
  941. * got the most up to date version of those blocks then is to force
  942. * read them off disk. Just searching through the buffer cache won't
  943. * work as there may be pages backing this file which are still marked
  944. * up to date. We know things can't change on this file underneath us
  945. * as we have the lock by now :)
  946. */
  947. static int ocfs2_force_read_journal(struct inode *inode)
  948. {
  949. int status = 0;
  950. int i;
  951. u64 v_blkno, p_blkno, p_blocks, num_blocks;
  952. struct buffer_head *bh = NULL;
  953. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  954. num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
  955. v_blkno = 0;
  956. while (v_blkno < num_blocks) {
  957. status = ocfs2_extent_map_get_blocks(inode, v_blkno,
  958. &p_blkno, &p_blocks, NULL);
  959. if (status < 0) {
  960. mlog_errno(status);
  961. goto bail;
  962. }
  963. for (i = 0; i < p_blocks; i++, p_blkno++) {
  964. bh = __find_get_block(osb->sb->s_bdev, p_blkno,
  965. osb->sb->s_blocksize);
  966. /* block not cached. */
  967. if (!bh)
  968. continue;
  969. brelse(bh);
  970. bh = NULL;
  971. /* We are reading journal data which should not
  972. * be put in the uptodate cache.
  973. */
  974. status = ocfs2_read_blocks_sync(osb, p_blkno, 1, &bh);
  975. if (status < 0) {
  976. mlog_errno(status);
  977. goto bail;
  978. }
  979. brelse(bh);
  980. bh = NULL;
  981. }
  982. v_blkno += p_blocks;
  983. }
  984. bail:
  985. return status;
  986. }
  987. struct ocfs2_la_recovery_item {
  988. struct list_head lri_list;
  989. int lri_slot;
  990. struct ocfs2_dinode *lri_la_dinode;
  991. struct ocfs2_dinode *lri_tl_dinode;
  992. struct ocfs2_quota_recovery *lri_qrec;
  993. enum ocfs2_orphan_reco_type lri_orphan_reco_type;
  994. };
  995. /* Does the second half of the recovery process. By this point, the
  996. * node is marked clean and can actually be considered recovered,
  997. * hence it's no longer in the recovery map, but there's still some
  998. * cleanup we can do which shouldn't happen within the recovery thread
  999. * as locking in that context becomes very difficult if we are to take
  1000. * recovering nodes into account.
  1001. *
  1002. * NOTE: This function can and will sleep on recovery of other nodes
  1003. * during cluster locking, just like any other ocfs2 process.
  1004. */
  1005. void ocfs2_complete_recovery(struct work_struct *work)
  1006. {
  1007. int ret = 0;
  1008. struct ocfs2_journal *journal =
  1009. container_of(work, struct ocfs2_journal, j_recovery_work);
  1010. struct ocfs2_super *osb = journal->j_osb;
  1011. struct ocfs2_dinode *la_dinode, *tl_dinode;
  1012. struct ocfs2_la_recovery_item *item, *n;
  1013. struct ocfs2_quota_recovery *qrec;
  1014. enum ocfs2_orphan_reco_type orphan_reco_type;
  1015. LIST_HEAD(tmp_la_list);
  1016. trace_ocfs2_complete_recovery(
  1017. (unsigned long long)OCFS2_I(journal->j_inode)->ip_blkno);
  1018. spin_lock(&journal->j_lock);
  1019. list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
  1020. spin_unlock(&journal->j_lock);
  1021. list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
  1022. list_del_init(&item->lri_list);
  1023. ocfs2_wait_on_quotas(osb);
  1024. la_dinode = item->lri_la_dinode;
  1025. tl_dinode = item->lri_tl_dinode;
  1026. qrec = item->lri_qrec;
  1027. orphan_reco_type = item->lri_orphan_reco_type;
  1028. trace_ocfs2_complete_recovery_slot(item->lri_slot,
  1029. la_dinode ? le64_to_cpu(la_dinode->i_blkno) : 0,
  1030. tl_dinode ? le64_to_cpu(tl_dinode->i_blkno) : 0,
  1031. qrec);
  1032. if (la_dinode) {
  1033. ret = ocfs2_complete_local_alloc_recovery(osb,
  1034. la_dinode);
  1035. if (ret < 0)
  1036. mlog_errno(ret);
  1037. kfree(la_dinode);
  1038. }
  1039. if (tl_dinode) {
  1040. ret = ocfs2_complete_truncate_log_recovery(osb,
  1041. tl_dinode);
  1042. if (ret < 0)
  1043. mlog_errno(ret);
  1044. kfree(tl_dinode);
  1045. }
  1046. ret = ocfs2_recover_orphans(osb, item->lri_slot,
  1047. orphan_reco_type);
  1048. if (ret < 0)
  1049. mlog_errno(ret);
  1050. if (qrec) {
  1051. ret = ocfs2_finish_quota_recovery(osb, qrec,
  1052. item->lri_slot);
  1053. if (ret < 0)
  1054. mlog_errno(ret);
  1055. /* Recovery info is already freed now */
  1056. }
  1057. kfree(item);
  1058. }
  1059. trace_ocfs2_complete_recovery_end(ret);
  1060. }
  1061. /* NOTE: This function always eats your references to la_dinode and
  1062. * tl_dinode, either manually on error, or by passing them to
  1063. * ocfs2_complete_recovery */
  1064. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  1065. int slot_num,
  1066. struct ocfs2_dinode *la_dinode,
  1067. struct ocfs2_dinode *tl_dinode,
  1068. struct ocfs2_quota_recovery *qrec,
  1069. enum ocfs2_orphan_reco_type orphan_reco_type)
  1070. {
  1071. struct ocfs2_la_recovery_item *item;
  1072. item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
  1073. if (!item) {
  1074. /* Though we wish to avoid it, we are in fact safe in
  1075. * skipping local alloc cleanup as fsck.ocfs2 is more
  1076. * than capable of reclaiming unused space. */
  1077. kfree(la_dinode);
  1078. kfree(tl_dinode);
  1079. if (qrec)
  1080. ocfs2_free_quota_recovery(qrec);
  1081. mlog_errno(-ENOMEM);
  1082. return;
  1083. }
  1084. INIT_LIST_HEAD(&item->lri_list);
  1085. item->lri_la_dinode = la_dinode;
  1086. item->lri_slot = slot_num;
  1087. item->lri_tl_dinode = tl_dinode;
  1088. item->lri_qrec = qrec;
  1089. item->lri_orphan_reco_type = orphan_reco_type;
  1090. spin_lock(&journal->j_lock);
  1091. list_add_tail(&item->lri_list, &journal->j_la_cleanups);
  1092. queue_work(journal->j_osb->ocfs2_wq, &journal->j_recovery_work);
  1093. spin_unlock(&journal->j_lock);
  1094. }
  1095. /* Called by the mount code to queue recovery the last part of
  1096. * recovery for it's own and offline slot(s). */
  1097. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
  1098. {
  1099. struct ocfs2_journal *journal = osb->journal;
  1100. if (ocfs2_is_hard_readonly(osb))
  1101. return;
  1102. /* No need to queue up our truncate_log as regular cleanup will catch
  1103. * that */
  1104. ocfs2_queue_recovery_completion(journal, osb->slot_num,
  1105. osb->local_alloc_copy, NULL, NULL,
  1106. ORPHAN_NEED_TRUNCATE);
  1107. ocfs2_schedule_truncate_log_flush(osb, 0);
  1108. osb->local_alloc_copy = NULL;
  1109. osb->dirty = 0;
  1110. /* queue to recover orphan slots for all offline slots */
  1111. ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
  1112. ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
  1113. ocfs2_free_replay_slots(osb);
  1114. }
  1115. void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
  1116. {
  1117. if (osb->quota_rec) {
  1118. ocfs2_queue_recovery_completion(osb->journal,
  1119. osb->slot_num,
  1120. NULL,
  1121. NULL,
  1122. osb->quota_rec,
  1123. ORPHAN_NEED_TRUNCATE);
  1124. osb->quota_rec = NULL;
  1125. }
  1126. }
  1127. static int __ocfs2_recovery_thread(void *arg)
  1128. {
  1129. int status, node_num, slot_num;
  1130. struct ocfs2_super *osb = arg;
  1131. struct ocfs2_recovery_map *rm = osb->recovery_map;
  1132. int *rm_quota = NULL;
  1133. int rm_quota_used = 0, i;
  1134. struct ocfs2_quota_recovery *qrec;
  1135. status = ocfs2_wait_on_mount(osb);
  1136. if (status < 0) {
  1137. goto bail;
  1138. }
  1139. rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
  1140. if (!rm_quota) {
  1141. status = -ENOMEM;
  1142. goto bail;
  1143. }
  1144. restart:
  1145. status = ocfs2_super_lock(osb, 1);
  1146. if (status < 0) {
  1147. mlog_errno(status);
  1148. goto bail;
  1149. }
  1150. status = ocfs2_compute_replay_slots(osb);
  1151. if (status < 0)
  1152. mlog_errno(status);
  1153. /* queue recovery for our own slot */
  1154. ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
  1155. NULL, NULL, ORPHAN_NO_NEED_TRUNCATE);
  1156. spin_lock(&osb->osb_lock);
  1157. while (rm->rm_used) {
  1158. /* It's always safe to remove entry zero, as we won't
  1159. * clear it until ocfs2_recover_node() has succeeded. */
  1160. node_num = rm->rm_entries[0];
  1161. spin_unlock(&osb->osb_lock);
  1162. slot_num = ocfs2_node_num_to_slot(osb, node_num);
  1163. trace_ocfs2_recovery_thread_node(node_num, slot_num);
  1164. if (slot_num == -ENOENT) {
  1165. status = 0;
  1166. goto skip_recovery;
  1167. }
  1168. /* It is a bit subtle with quota recovery. We cannot do it
  1169. * immediately because we have to obtain cluster locks from
  1170. * quota files and we also don't want to just skip it because
  1171. * then quota usage would be out of sync until some node takes
  1172. * the slot. So we remember which nodes need quota recovery
  1173. * and when everything else is done, we recover quotas. */
  1174. for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
  1175. if (i == rm_quota_used)
  1176. rm_quota[rm_quota_used++] = slot_num;
  1177. status = ocfs2_recover_node(osb, node_num, slot_num);
  1178. skip_recovery:
  1179. if (!status) {
  1180. ocfs2_recovery_map_clear(osb, node_num);
  1181. } else {
  1182. mlog(ML_ERROR,
  1183. "Error %d recovering node %d on device (%u,%u)!\n",
  1184. status, node_num,
  1185. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1186. mlog(ML_ERROR, "Volume requires unmount.\n");
  1187. }
  1188. spin_lock(&osb->osb_lock);
  1189. }
  1190. spin_unlock(&osb->osb_lock);
  1191. trace_ocfs2_recovery_thread_end(status);
  1192. /* Refresh all journal recovery generations from disk */
  1193. status = ocfs2_check_journals_nolocks(osb);
  1194. status = (status == -EROFS) ? 0 : status;
  1195. if (status < 0)
  1196. mlog_errno(status);
  1197. /* Now it is right time to recover quotas... We have to do this under
  1198. * superblock lock so that no one can start using the slot (and crash)
  1199. * before we recover it */
  1200. for (i = 0; i < rm_quota_used; i++) {
  1201. qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
  1202. if (IS_ERR(qrec)) {
  1203. status = PTR_ERR(qrec);
  1204. mlog_errno(status);
  1205. continue;
  1206. }
  1207. ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
  1208. NULL, NULL, qrec,
  1209. ORPHAN_NEED_TRUNCATE);
  1210. }
  1211. ocfs2_super_unlock(osb, 1);
  1212. /* queue recovery for offline slots */
  1213. ocfs2_queue_replay_slots(osb, ORPHAN_NEED_TRUNCATE);
  1214. bail:
  1215. mutex_lock(&osb->recovery_lock);
  1216. if (!status && !ocfs2_recovery_completed(osb)) {
  1217. mutex_unlock(&osb->recovery_lock);
  1218. goto restart;
  1219. }
  1220. ocfs2_free_replay_slots(osb);
  1221. osb->recovery_thread_task = NULL;
  1222. mb(); /* sync with ocfs2_recovery_thread_running */
  1223. wake_up(&osb->recovery_event);
  1224. mutex_unlock(&osb->recovery_lock);
  1225. kfree(rm_quota);
  1226. /* no one is callint kthread_stop() for us so the kthread() api
  1227. * requires that we call do_exit(). And it isn't exported, but
  1228. * complete_and_exit() seems to be a minimal wrapper around it. */
  1229. complete_and_exit(NULL, status);
  1230. }
  1231. void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
  1232. {
  1233. mutex_lock(&osb->recovery_lock);
  1234. trace_ocfs2_recovery_thread(node_num, osb->node_num,
  1235. osb->disable_recovery, osb->recovery_thread_task,
  1236. osb->disable_recovery ?
  1237. -1 : ocfs2_recovery_map_set(osb, node_num));
  1238. if (osb->disable_recovery)
  1239. goto out;
  1240. if (osb->recovery_thread_task)
  1241. goto out;
  1242. osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
  1243. "ocfs2rec-%s", osb->uuid_str);
  1244. if (IS_ERR(osb->recovery_thread_task)) {
  1245. mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
  1246. osb->recovery_thread_task = NULL;
  1247. }
  1248. out:
  1249. mutex_unlock(&osb->recovery_lock);
  1250. wake_up(&osb->recovery_event);
  1251. }
  1252. static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
  1253. int slot_num,
  1254. struct buffer_head **bh,
  1255. struct inode **ret_inode)
  1256. {
  1257. int status = -EACCES;
  1258. struct inode *inode = NULL;
  1259. BUG_ON(slot_num >= osb->max_slots);
  1260. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1261. slot_num);
  1262. if (!inode || is_bad_inode(inode)) {
  1263. mlog_errno(status);
  1264. goto bail;
  1265. }
  1266. SET_INODE_JOURNAL(inode);
  1267. status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
  1268. if (status < 0) {
  1269. mlog_errno(status);
  1270. goto bail;
  1271. }
  1272. status = 0;
  1273. bail:
  1274. if (inode) {
  1275. if (status || !ret_inode)
  1276. iput(inode);
  1277. else
  1278. *ret_inode = inode;
  1279. }
  1280. return status;
  1281. }
  1282. /* Does the actual journal replay and marks the journal inode as
  1283. * clean. Will only replay if the journal inode is marked dirty. */
  1284. static int ocfs2_replay_journal(struct ocfs2_super *osb,
  1285. int node_num,
  1286. int slot_num)
  1287. {
  1288. int status;
  1289. int got_lock = 0;
  1290. unsigned int flags;
  1291. struct inode *inode = NULL;
  1292. struct ocfs2_dinode *fe;
  1293. journal_t *journal = NULL;
  1294. struct buffer_head *bh = NULL;
  1295. u32 slot_reco_gen;
  1296. status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
  1297. if (status) {
  1298. mlog_errno(status);
  1299. goto done;
  1300. }
  1301. fe = (struct ocfs2_dinode *)bh->b_data;
  1302. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  1303. brelse(bh);
  1304. bh = NULL;
  1305. /*
  1306. * As the fs recovery is asynchronous, there is a small chance that
  1307. * another node mounted (and recovered) the slot before the recovery
  1308. * thread could get the lock. To handle that, we dirty read the journal
  1309. * inode for that slot to get the recovery generation. If it is
  1310. * different than what we expected, the slot has been recovered.
  1311. * If not, it needs recovery.
  1312. */
  1313. if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
  1314. trace_ocfs2_replay_journal_recovered(slot_num,
  1315. osb->slot_recovery_generations[slot_num], slot_reco_gen);
  1316. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  1317. status = -EBUSY;
  1318. goto done;
  1319. }
  1320. /* Continue with recovery as the journal has not yet been recovered */
  1321. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  1322. if (status < 0) {
  1323. trace_ocfs2_replay_journal_lock_err(status);
  1324. if (status != -ERESTARTSYS)
  1325. mlog(ML_ERROR, "Could not lock journal!\n");
  1326. goto done;
  1327. }
  1328. got_lock = 1;
  1329. fe = (struct ocfs2_dinode *) bh->b_data;
  1330. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  1331. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  1332. if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
  1333. trace_ocfs2_replay_journal_skip(node_num);
  1334. /* Refresh recovery generation for the slot */
  1335. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  1336. goto done;
  1337. }
  1338. /* we need to run complete recovery for offline orphan slots */
  1339. ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
  1340. printk(KERN_NOTICE "ocfs2: Begin replay journal (node %d, slot %d) on "\
  1341. "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
  1342. MINOR(osb->sb->s_dev));
  1343. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1344. status = ocfs2_force_read_journal(inode);
  1345. if (status < 0) {
  1346. mlog_errno(status);
  1347. goto done;
  1348. }
  1349. journal = jbd2_journal_init_inode(inode);
  1350. if (journal == NULL) {
  1351. mlog(ML_ERROR, "Linux journal layer error\n");
  1352. status = -EIO;
  1353. goto done;
  1354. }
  1355. status = jbd2_journal_load(journal);
  1356. if (status < 0) {
  1357. mlog_errno(status);
  1358. if (!igrab(inode))
  1359. BUG();
  1360. jbd2_journal_destroy(journal);
  1361. goto done;
  1362. }
  1363. ocfs2_clear_journal_error(osb->sb, journal, slot_num);
  1364. /* wipe the journal */
  1365. jbd2_journal_lock_updates(journal);
  1366. status = jbd2_journal_flush(journal);
  1367. jbd2_journal_unlock_updates(journal);
  1368. if (status < 0)
  1369. mlog_errno(status);
  1370. /* This will mark the node clean */
  1371. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  1372. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  1373. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  1374. /* Increment recovery generation to indicate successful recovery */
  1375. ocfs2_bump_recovery_generation(fe);
  1376. osb->slot_recovery_generations[slot_num] =
  1377. ocfs2_get_recovery_generation(fe);
  1378. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
  1379. status = ocfs2_write_block(osb, bh, INODE_CACHE(inode));
  1380. if (status < 0)
  1381. mlog_errno(status);
  1382. if (!igrab(inode))
  1383. BUG();
  1384. jbd2_journal_destroy(journal);
  1385. printk(KERN_NOTICE "ocfs2: End replay journal (node %d, slot %d) on "\
  1386. "device (%u,%u)\n", node_num, slot_num, MAJOR(osb->sb->s_dev),
  1387. MINOR(osb->sb->s_dev));
  1388. done:
  1389. /* drop the lock on this nodes journal */
  1390. if (got_lock)
  1391. ocfs2_inode_unlock(inode, 1);
  1392. iput(inode);
  1393. brelse(bh);
  1394. return status;
  1395. }
  1396. /*
  1397. * Do the most important parts of node recovery:
  1398. * - Replay it's journal
  1399. * - Stamp a clean local allocator file
  1400. * - Stamp a clean truncate log
  1401. * - Mark the node clean
  1402. *
  1403. * If this function completes without error, a node in OCFS2 can be
  1404. * said to have been safely recovered. As a result, failure during the
  1405. * second part of a nodes recovery process (local alloc recovery) is
  1406. * far less concerning.
  1407. */
  1408. static int ocfs2_recover_node(struct ocfs2_super *osb,
  1409. int node_num, int slot_num)
  1410. {
  1411. int status = 0;
  1412. struct ocfs2_dinode *la_copy = NULL;
  1413. struct ocfs2_dinode *tl_copy = NULL;
  1414. trace_ocfs2_recover_node(node_num, slot_num, osb->node_num);
  1415. /* Should not ever be called to recover ourselves -- in that
  1416. * case we should've called ocfs2_journal_load instead. */
  1417. BUG_ON(osb->node_num == node_num);
  1418. status = ocfs2_replay_journal(osb, node_num, slot_num);
  1419. if (status < 0) {
  1420. if (status == -EBUSY) {
  1421. trace_ocfs2_recover_node_skip(slot_num, node_num);
  1422. status = 0;
  1423. goto done;
  1424. }
  1425. mlog_errno(status);
  1426. goto done;
  1427. }
  1428. /* Stamp a clean local alloc file AFTER recovering the journal... */
  1429. status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
  1430. if (status < 0) {
  1431. mlog_errno(status);
  1432. goto done;
  1433. }
  1434. /* An error from begin_truncate_log_recovery is not
  1435. * serious enough to warrant halting the rest of
  1436. * recovery. */
  1437. status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
  1438. if (status < 0)
  1439. mlog_errno(status);
  1440. /* Likewise, this would be a strange but ultimately not so
  1441. * harmful place to get an error... */
  1442. status = ocfs2_clear_slot(osb, slot_num);
  1443. if (status < 0)
  1444. mlog_errno(status);
  1445. /* This will kfree the memory pointed to by la_copy and tl_copy */
  1446. ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
  1447. tl_copy, NULL, ORPHAN_NEED_TRUNCATE);
  1448. status = 0;
  1449. done:
  1450. return status;
  1451. }
  1452. /* Test node liveness by trylocking his journal. If we get the lock,
  1453. * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
  1454. * still alive (we couldn't get the lock) and < 0 on error. */
  1455. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  1456. int slot_num)
  1457. {
  1458. int status, flags;
  1459. struct inode *inode = NULL;
  1460. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1461. slot_num);
  1462. if (inode == NULL) {
  1463. mlog(ML_ERROR, "access error\n");
  1464. status = -EACCES;
  1465. goto bail;
  1466. }
  1467. if (is_bad_inode(inode)) {
  1468. mlog(ML_ERROR, "access error (bad inode)\n");
  1469. iput(inode);
  1470. inode = NULL;
  1471. status = -EACCES;
  1472. goto bail;
  1473. }
  1474. SET_INODE_JOURNAL(inode);
  1475. flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
  1476. status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
  1477. if (status < 0) {
  1478. if (status != -EAGAIN)
  1479. mlog_errno(status);
  1480. goto bail;
  1481. }
  1482. ocfs2_inode_unlock(inode, 1);
  1483. bail:
  1484. iput(inode);
  1485. return status;
  1486. }
  1487. /* Call this underneath ocfs2_super_lock. It also assumes that the
  1488. * slot info struct has been updated from disk. */
  1489. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
  1490. {
  1491. unsigned int node_num;
  1492. int status, i;
  1493. u32 gen;
  1494. struct buffer_head *bh = NULL;
  1495. struct ocfs2_dinode *di;
  1496. /* This is called with the super block cluster lock, so we
  1497. * know that the slot map can't change underneath us. */
  1498. for (i = 0; i < osb->max_slots; i++) {
  1499. /* Read journal inode to get the recovery generation */
  1500. status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
  1501. if (status) {
  1502. mlog_errno(status);
  1503. goto bail;
  1504. }
  1505. di = (struct ocfs2_dinode *)bh->b_data;
  1506. gen = ocfs2_get_recovery_generation(di);
  1507. brelse(bh);
  1508. bh = NULL;
  1509. spin_lock(&osb->osb_lock);
  1510. osb->slot_recovery_generations[i] = gen;
  1511. trace_ocfs2_mark_dead_nodes(i,
  1512. osb->slot_recovery_generations[i]);
  1513. if (i == osb->slot_num) {
  1514. spin_unlock(&osb->osb_lock);
  1515. continue;
  1516. }
  1517. status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
  1518. if (status == -ENOENT) {
  1519. spin_unlock(&osb->osb_lock);
  1520. continue;
  1521. }
  1522. if (__ocfs2_recovery_map_test(osb, node_num)) {
  1523. spin_unlock(&osb->osb_lock);
  1524. continue;
  1525. }
  1526. spin_unlock(&osb->osb_lock);
  1527. /* Ok, we have a slot occupied by another node which
  1528. * is not in the recovery map. We trylock his journal
  1529. * file here to test if he's alive. */
  1530. status = ocfs2_trylock_journal(osb, i);
  1531. if (!status) {
  1532. /* Since we're called from mount, we know that
  1533. * the recovery thread can't race us on
  1534. * setting / checking the recovery bits. */
  1535. ocfs2_recovery_thread(osb, node_num);
  1536. } else if ((status < 0) && (status != -EAGAIN)) {
  1537. mlog_errno(status);
  1538. goto bail;
  1539. }
  1540. }
  1541. status = 0;
  1542. bail:
  1543. return status;
  1544. }
  1545. /*
  1546. * Scan timer should get fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT. Add some
  1547. * randomness to the timeout to minimize multple nodes firing the timer at the
  1548. * same time.
  1549. */
  1550. static inline unsigned long ocfs2_orphan_scan_timeout(void)
  1551. {
  1552. unsigned long time;
  1553. get_random_bytes(&time, sizeof(time));
  1554. time = ORPHAN_SCAN_SCHEDULE_TIMEOUT + (time % 5000);
  1555. return msecs_to_jiffies(time);
  1556. }
  1557. /*
  1558. * ocfs2_queue_orphan_scan calls ocfs2_queue_recovery_completion for
  1559. * every slot, queuing a recovery of the slot on the ocfs2_wq thread. This
  1560. * is done to catch any orphans that are left over in orphan directories.
  1561. *
  1562. * It scans all slots, even ones that are in use. It does so to handle the
  1563. * case described below:
  1564. *
  1565. * Node 1 has an inode it was using. The dentry went away due to memory
  1566. * pressure. Node 1 closes the inode, but it's on the free list. The node
  1567. * has the open lock.
  1568. * Node 2 unlinks the inode. It grabs the dentry lock to notify others,
  1569. * but node 1 has no dentry and doesn't get the message. It trylocks the
  1570. * open lock, sees that another node has a PR, and does nothing.
  1571. * Later node 2 runs its orphan dir. It igets the inode, trylocks the
  1572. * open lock, sees the PR still, and does nothing.
  1573. * Basically, we have to trigger an orphan iput on node 1. The only way
  1574. * for this to happen is if node 1 runs node 2's orphan dir.
  1575. *
  1576. * ocfs2_queue_orphan_scan gets called every ORPHAN_SCAN_SCHEDULE_TIMEOUT
  1577. * seconds. It gets an EX lock on os_lockres and checks sequence number
  1578. * stored in LVB. If the sequence number has changed, it means some other
  1579. * node has done the scan. This node skips the scan and tracks the
  1580. * sequence number. If the sequence number didn't change, it means a scan
  1581. * hasn't happened. The node queues a scan and increments the
  1582. * sequence number in the LVB.
  1583. */
  1584. static void ocfs2_queue_orphan_scan(struct ocfs2_super *osb)
  1585. {
  1586. struct ocfs2_orphan_scan *os;
  1587. int status, i;
  1588. u32 seqno = 0;
  1589. os = &osb->osb_orphan_scan;
  1590. if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
  1591. goto out;
  1592. trace_ocfs2_queue_orphan_scan_begin(os->os_count, os->os_seqno,
  1593. atomic_read(&os->os_state));
  1594. status = ocfs2_orphan_scan_lock(osb, &seqno);
  1595. if (status < 0) {
  1596. if (status != -EAGAIN)
  1597. mlog_errno(status);
  1598. goto out;
  1599. }
  1600. /* Do no queue the tasks if the volume is being umounted */
  1601. if (atomic_read(&os->os_state) == ORPHAN_SCAN_INACTIVE)
  1602. goto unlock;
  1603. if (os->os_seqno != seqno) {
  1604. os->os_seqno = seqno;
  1605. goto unlock;
  1606. }
  1607. for (i = 0; i < osb->max_slots; i++)
  1608. ocfs2_queue_recovery_completion(osb->journal, i, NULL, NULL,
  1609. NULL, ORPHAN_NO_NEED_TRUNCATE);
  1610. /*
  1611. * We queued a recovery on orphan slots, increment the sequence
  1612. * number and update LVB so other node will skip the scan for a while
  1613. */
  1614. seqno++;
  1615. os->os_count++;
  1616. os->os_scantime = CURRENT_TIME;
  1617. unlock:
  1618. ocfs2_orphan_scan_unlock(osb, seqno);
  1619. out:
  1620. trace_ocfs2_queue_orphan_scan_end(os->os_count, os->os_seqno,
  1621. atomic_read(&os->os_state));
  1622. return;
  1623. }
  1624. /* Worker task that gets fired every ORPHAN_SCAN_SCHEDULE_TIMEOUT millsec */
  1625. static void ocfs2_orphan_scan_work(struct work_struct *work)
  1626. {
  1627. struct ocfs2_orphan_scan *os;
  1628. struct ocfs2_super *osb;
  1629. os = container_of(work, struct ocfs2_orphan_scan,
  1630. os_orphan_scan_work.work);
  1631. osb = os->os_osb;
  1632. mutex_lock(&os->os_lock);
  1633. ocfs2_queue_orphan_scan(osb);
  1634. if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE)
  1635. queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
  1636. ocfs2_orphan_scan_timeout());
  1637. mutex_unlock(&os->os_lock);
  1638. }
  1639. void ocfs2_orphan_scan_stop(struct ocfs2_super *osb)
  1640. {
  1641. struct ocfs2_orphan_scan *os;
  1642. os = &osb->osb_orphan_scan;
  1643. if (atomic_read(&os->os_state) == ORPHAN_SCAN_ACTIVE) {
  1644. atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
  1645. mutex_lock(&os->os_lock);
  1646. cancel_delayed_work(&os->os_orphan_scan_work);
  1647. mutex_unlock(&os->os_lock);
  1648. }
  1649. }
  1650. void ocfs2_orphan_scan_init(struct ocfs2_super *osb)
  1651. {
  1652. struct ocfs2_orphan_scan *os;
  1653. os = &osb->osb_orphan_scan;
  1654. os->os_osb = osb;
  1655. os->os_count = 0;
  1656. os->os_seqno = 0;
  1657. mutex_init(&os->os_lock);
  1658. INIT_DELAYED_WORK(&os->os_orphan_scan_work, ocfs2_orphan_scan_work);
  1659. }
  1660. void ocfs2_orphan_scan_start(struct ocfs2_super *osb)
  1661. {
  1662. struct ocfs2_orphan_scan *os;
  1663. os = &osb->osb_orphan_scan;
  1664. os->os_scantime = CURRENT_TIME;
  1665. if (ocfs2_is_hard_readonly(osb) || ocfs2_mount_local(osb))
  1666. atomic_set(&os->os_state, ORPHAN_SCAN_INACTIVE);
  1667. else {
  1668. atomic_set(&os->os_state, ORPHAN_SCAN_ACTIVE);
  1669. queue_delayed_work(osb->ocfs2_wq, &os->os_orphan_scan_work,
  1670. ocfs2_orphan_scan_timeout());
  1671. }
  1672. }
  1673. struct ocfs2_orphan_filldir_priv {
  1674. struct dir_context ctx;
  1675. struct inode *head;
  1676. struct ocfs2_super *osb;
  1677. enum ocfs2_orphan_reco_type orphan_reco_type;
  1678. };
  1679. static int ocfs2_orphan_filldir(struct dir_context *ctx, const char *name,
  1680. int name_len, loff_t pos, u64 ino,
  1681. unsigned type)
  1682. {
  1683. struct ocfs2_orphan_filldir_priv *p =
  1684. container_of(ctx, struct ocfs2_orphan_filldir_priv, ctx);
  1685. struct inode *iter;
  1686. if (name_len == 1 && !strncmp(".", name, 1))
  1687. return 0;
  1688. if (name_len == 2 && !strncmp("..", name, 2))
  1689. return 0;
  1690. /* do not include dio entry in case of orphan scan */
  1691. if ((p->orphan_reco_type == ORPHAN_NO_NEED_TRUNCATE) &&
  1692. (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
  1693. OCFS2_DIO_ORPHAN_PREFIX_LEN)))
  1694. return 0;
  1695. /* Skip bad inodes so that recovery can continue */
  1696. iter = ocfs2_iget(p->osb, ino,
  1697. OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
  1698. if (IS_ERR(iter))
  1699. return 0;
  1700. if (!strncmp(name, OCFS2_DIO_ORPHAN_PREFIX,
  1701. OCFS2_DIO_ORPHAN_PREFIX_LEN))
  1702. OCFS2_I(iter)->ip_flags |= OCFS2_INODE_DIO_ORPHAN_ENTRY;
  1703. /* Skip inodes which are already added to recover list, since dio may
  1704. * happen concurrently with unlink/rename */
  1705. if (OCFS2_I(iter)->ip_next_orphan) {
  1706. iput(iter);
  1707. return 0;
  1708. }
  1709. trace_ocfs2_orphan_filldir((unsigned long long)OCFS2_I(iter)->ip_blkno);
  1710. /* No locking is required for the next_orphan queue as there
  1711. * is only ever a single process doing orphan recovery. */
  1712. OCFS2_I(iter)->ip_next_orphan = p->head;
  1713. p->head = iter;
  1714. return 0;
  1715. }
  1716. static int ocfs2_queue_orphans(struct ocfs2_super *osb,
  1717. int slot,
  1718. struct inode **head,
  1719. enum ocfs2_orphan_reco_type orphan_reco_type)
  1720. {
  1721. int status;
  1722. struct inode *orphan_dir_inode = NULL;
  1723. struct ocfs2_orphan_filldir_priv priv = {
  1724. .ctx.actor = ocfs2_orphan_filldir,
  1725. .osb = osb,
  1726. .head = *head,
  1727. .orphan_reco_type = orphan_reco_type
  1728. };
  1729. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1730. ORPHAN_DIR_SYSTEM_INODE,
  1731. slot);
  1732. if (!orphan_dir_inode) {
  1733. status = -ENOENT;
  1734. mlog_errno(status);
  1735. return status;
  1736. }
  1737. inode_lock(orphan_dir_inode);
  1738. status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
  1739. if (status < 0) {
  1740. mlog_errno(status);
  1741. goto out;
  1742. }
  1743. status = ocfs2_dir_foreach(orphan_dir_inode, &priv.ctx);
  1744. if (status) {
  1745. mlog_errno(status);
  1746. goto out_cluster;
  1747. }
  1748. *head = priv.head;
  1749. out_cluster:
  1750. ocfs2_inode_unlock(orphan_dir_inode, 0);
  1751. out:
  1752. inode_unlock(orphan_dir_inode);
  1753. iput(orphan_dir_inode);
  1754. return status;
  1755. }
  1756. static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
  1757. int slot)
  1758. {
  1759. int ret;
  1760. spin_lock(&osb->osb_lock);
  1761. ret = !osb->osb_orphan_wipes[slot];
  1762. spin_unlock(&osb->osb_lock);
  1763. return ret;
  1764. }
  1765. static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
  1766. int slot)
  1767. {
  1768. spin_lock(&osb->osb_lock);
  1769. /* Mark ourselves such that new processes in delete_inode()
  1770. * know to quit early. */
  1771. ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1772. while (osb->osb_orphan_wipes[slot]) {
  1773. /* If any processes are already in the middle of an
  1774. * orphan wipe on this dir, then we need to wait for
  1775. * them. */
  1776. spin_unlock(&osb->osb_lock);
  1777. wait_event_interruptible(osb->osb_wipe_event,
  1778. ocfs2_orphan_recovery_can_continue(osb, slot));
  1779. spin_lock(&osb->osb_lock);
  1780. }
  1781. spin_unlock(&osb->osb_lock);
  1782. }
  1783. static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
  1784. int slot)
  1785. {
  1786. ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1787. }
  1788. /*
  1789. * Orphan recovery. Each mounted node has it's own orphan dir which we
  1790. * must run during recovery. Our strategy here is to build a list of
  1791. * the inodes in the orphan dir and iget/iput them. The VFS does
  1792. * (most) of the rest of the work.
  1793. *
  1794. * Orphan recovery can happen at any time, not just mount so we have a
  1795. * couple of extra considerations.
  1796. *
  1797. * - We grab as many inodes as we can under the orphan dir lock -
  1798. * doing iget() outside the orphan dir risks getting a reference on
  1799. * an invalid inode.
  1800. * - We must be sure not to deadlock with other processes on the
  1801. * system wanting to run delete_inode(). This can happen when they go
  1802. * to lock the orphan dir and the orphan recovery process attempts to
  1803. * iget() inside the orphan dir lock. This can be avoided by
  1804. * advertising our state to ocfs2_delete_inode().
  1805. */
  1806. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  1807. int slot,
  1808. enum ocfs2_orphan_reco_type orphan_reco_type)
  1809. {
  1810. int ret = 0;
  1811. struct inode *inode = NULL;
  1812. struct inode *iter;
  1813. struct ocfs2_inode_info *oi;
  1814. struct buffer_head *di_bh = NULL;
  1815. struct ocfs2_dinode *di = NULL;
  1816. trace_ocfs2_recover_orphans(slot);
  1817. ocfs2_mark_recovering_orphan_dir(osb, slot);
  1818. ret = ocfs2_queue_orphans(osb, slot, &inode, orphan_reco_type);
  1819. ocfs2_clear_recovering_orphan_dir(osb, slot);
  1820. /* Error here should be noted, but we want to continue with as
  1821. * many queued inodes as we've got. */
  1822. if (ret)
  1823. mlog_errno(ret);
  1824. while (inode) {
  1825. oi = OCFS2_I(inode);
  1826. trace_ocfs2_recover_orphans_iput(
  1827. (unsigned long long)oi->ip_blkno);
  1828. iter = oi->ip_next_orphan;
  1829. oi->ip_next_orphan = NULL;
  1830. if (oi->ip_flags & OCFS2_INODE_DIO_ORPHAN_ENTRY) {
  1831. inode_lock(inode);
  1832. ret = ocfs2_rw_lock(inode, 1);
  1833. if (ret < 0) {
  1834. mlog_errno(ret);
  1835. goto unlock_mutex;
  1836. }
  1837. /*
  1838. * We need to take and drop the inode lock to
  1839. * force read inode from disk.
  1840. */
  1841. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1842. if (ret) {
  1843. mlog_errno(ret);
  1844. goto unlock_rw;
  1845. }
  1846. di = (struct ocfs2_dinode *)di_bh->b_data;
  1847. if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL)) {
  1848. ret = ocfs2_truncate_file(inode, di_bh,
  1849. i_size_read(inode));
  1850. if (ret < 0) {
  1851. if (ret != -ENOSPC)
  1852. mlog_errno(ret);
  1853. goto unlock_inode;
  1854. }
  1855. ret = ocfs2_del_inode_from_orphan(osb, inode,
  1856. di_bh, 0, 0);
  1857. if (ret)
  1858. mlog_errno(ret);
  1859. }
  1860. unlock_inode:
  1861. ocfs2_inode_unlock(inode, 1);
  1862. brelse(di_bh);
  1863. di_bh = NULL;
  1864. unlock_rw:
  1865. ocfs2_rw_unlock(inode, 1);
  1866. unlock_mutex:
  1867. inode_unlock(inode);
  1868. /* clear dio flag in ocfs2_inode_info */
  1869. oi->ip_flags &= ~OCFS2_INODE_DIO_ORPHAN_ENTRY;
  1870. } else {
  1871. spin_lock(&oi->ip_lock);
  1872. /* Set the proper information to get us going into
  1873. * ocfs2_delete_inode. */
  1874. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  1875. spin_unlock(&oi->ip_lock);
  1876. }
  1877. iput(inode);
  1878. inode = iter;
  1879. }
  1880. return ret;
  1881. }
  1882. static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
  1883. {
  1884. /* This check is good because ocfs2 will wait on our recovery
  1885. * thread before changing it to something other than MOUNTED
  1886. * or DISABLED. */
  1887. wait_event(osb->osb_mount_event,
  1888. (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
  1889. atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
  1890. atomic_read(&osb->vol_state) == VOLUME_DISABLED);
  1891. /* If there's an error on mount, then we may never get to the
  1892. * MOUNTED flag, but this is set right before
  1893. * dismount_volume() so we can trust it. */
  1894. if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
  1895. trace_ocfs2_wait_on_mount(VOLUME_DISABLED);
  1896. mlog(0, "mount error, exiting!\n");
  1897. return -EBUSY;
  1898. }
  1899. return 0;
  1900. }
  1901. static int ocfs2_commit_thread(void *arg)
  1902. {
  1903. int status;
  1904. struct ocfs2_super *osb = arg;
  1905. struct ocfs2_journal *journal = osb->journal;
  1906. /* we can trust j_num_trans here because _should_stop() is only set in
  1907. * shutdown and nobody other than ourselves should be able to start
  1908. * transactions. committing on shutdown might take a few iterations
  1909. * as final transactions put deleted inodes on the list */
  1910. while (!(kthread_should_stop() &&
  1911. atomic_read(&journal->j_num_trans) == 0)) {
  1912. wait_event_interruptible(osb->checkpoint_event,
  1913. atomic_read(&journal->j_num_trans)
  1914. || kthread_should_stop());
  1915. status = ocfs2_commit_cache(osb);
  1916. if (status < 0) {
  1917. static unsigned long abort_warn_time;
  1918. /* Warn about this once per minute */
  1919. if (printk_timed_ratelimit(&abort_warn_time, 60*HZ))
  1920. mlog(ML_ERROR, "status = %d, journal is "
  1921. "already aborted.\n", status);
  1922. /*
  1923. * After ocfs2_commit_cache() fails, j_num_trans has a
  1924. * non-zero value. Sleep here to avoid a busy-wait
  1925. * loop.
  1926. */
  1927. msleep_interruptible(1000);
  1928. }
  1929. if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
  1930. mlog(ML_KTHREAD,
  1931. "commit_thread: %u transactions pending on "
  1932. "shutdown\n",
  1933. atomic_read(&journal->j_num_trans));
  1934. }
  1935. }
  1936. return 0;
  1937. }
  1938. /* Reads all the journal inodes without taking any cluster locks. Used
  1939. * for hard readonly access to determine whether any journal requires
  1940. * recovery. Also used to refresh the recovery generation numbers after
  1941. * a journal has been recovered by another node.
  1942. */
  1943. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
  1944. {
  1945. int ret = 0;
  1946. unsigned int slot;
  1947. struct buffer_head *di_bh = NULL;
  1948. struct ocfs2_dinode *di;
  1949. int journal_dirty = 0;
  1950. for(slot = 0; slot < osb->max_slots; slot++) {
  1951. ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
  1952. if (ret) {
  1953. mlog_errno(ret);
  1954. goto out;
  1955. }
  1956. di = (struct ocfs2_dinode *) di_bh->b_data;
  1957. osb->slot_recovery_generations[slot] =
  1958. ocfs2_get_recovery_generation(di);
  1959. if (le32_to_cpu(di->id1.journal1.ij_flags) &
  1960. OCFS2_JOURNAL_DIRTY_FL)
  1961. journal_dirty = 1;
  1962. brelse(di_bh);
  1963. di_bh = NULL;
  1964. }
  1965. out:
  1966. if (journal_dirty)
  1967. ret = -EROFS;
  1968. return ret;
  1969. }