journal.c 65 KB

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