journal.c 58 KB

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