journal.c 57 KB

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