journal.c 57 KB

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