journal.c 61 KB

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