journal.c 58 KB

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