journal.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  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. #define MLOG_MASK_PREFIX ML_JOURNAL
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "blockcheck.h"
  35. #include "dir.h"
  36. #include "dlmglue.h"
  37. #include "extent_map.h"
  38. #include "heartbeat.h"
  39. #include "inode.h"
  40. #include "journal.h"
  41. #include "localalloc.h"
  42. #include "slot_map.h"
  43. #include "super.h"
  44. #include "sysfile.h"
  45. #include "quota.h"
  46. #include "buffer_head_io.h"
  47. DEFINE_SPINLOCK(trans_inc_lock);
  48. static int ocfs2_force_read_journal(struct inode *inode);
  49. static int ocfs2_recover_node(struct ocfs2_super *osb,
  50. int node_num, int slot_num);
  51. static int __ocfs2_recovery_thread(void *arg);
  52. static int ocfs2_commit_cache(struct ocfs2_super *osb);
  53. static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota);
  54. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  55. int dirty, int replayed);
  56. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  57. int slot_num);
  58. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  59. int slot);
  60. static int ocfs2_commit_thread(void *arg);
  61. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  62. int slot_num,
  63. struct ocfs2_dinode *la_dinode,
  64. struct ocfs2_dinode *tl_dinode,
  65. struct ocfs2_quota_recovery *qrec);
  66. static inline int ocfs2_wait_on_mount(struct ocfs2_super *osb)
  67. {
  68. return __ocfs2_wait_on_mount(osb, 0);
  69. }
  70. static inline int ocfs2_wait_on_quotas(struct ocfs2_super *osb)
  71. {
  72. return __ocfs2_wait_on_mount(osb, 1);
  73. }
  74. /*
  75. * This replay_map is to track online/offline slots, so we could recover
  76. * offline slots during recovery and mount
  77. */
  78. enum ocfs2_replay_state {
  79. REPLAY_UNNEEDED = 0, /* Replay is not needed, so ignore this map */
  80. REPLAY_NEEDED, /* Replay slots marked in rm_replay_slots */
  81. REPLAY_DONE /* Replay was already queued */
  82. };
  83. struct ocfs2_replay_map {
  84. unsigned int rm_slots;
  85. enum ocfs2_replay_state rm_state;
  86. unsigned char rm_replay_slots[0];
  87. };
  88. void ocfs2_replay_map_set_state(struct ocfs2_super *osb, int state)
  89. {
  90. if (!osb->replay_map)
  91. return;
  92. /* If we've already queued the replay, we don't have any more to do */
  93. if (osb->replay_map->rm_state == REPLAY_DONE)
  94. return;
  95. osb->replay_map->rm_state = state;
  96. }
  97. int ocfs2_compute_replay_slots(struct ocfs2_super *osb)
  98. {
  99. struct ocfs2_replay_map *replay_map;
  100. int i, node_num;
  101. /* If replay map is already set, we don't do it again */
  102. if (osb->replay_map)
  103. return 0;
  104. replay_map = kzalloc(sizeof(struct ocfs2_replay_map) +
  105. (osb->max_slots * sizeof(char)), GFP_KERNEL);
  106. if (!replay_map) {
  107. mlog_errno(-ENOMEM);
  108. return -ENOMEM;
  109. }
  110. spin_lock(&osb->osb_lock);
  111. replay_map->rm_slots = osb->max_slots;
  112. replay_map->rm_state = REPLAY_UNNEEDED;
  113. /* set rm_replay_slots for offline slot(s) */
  114. for (i = 0; i < replay_map->rm_slots; i++) {
  115. if (ocfs2_slot_to_node_num_locked(osb, i, &node_num) == -ENOENT)
  116. replay_map->rm_replay_slots[i] = 1;
  117. }
  118. osb->replay_map = replay_map;
  119. spin_unlock(&osb->osb_lock);
  120. return 0;
  121. }
  122. void ocfs2_queue_replay_slots(struct ocfs2_super *osb)
  123. {
  124. struct ocfs2_replay_map *replay_map = osb->replay_map;
  125. int i;
  126. if (!replay_map)
  127. return;
  128. if (replay_map->rm_state != REPLAY_NEEDED)
  129. return;
  130. for (i = 0; i < replay_map->rm_slots; i++)
  131. if (replay_map->rm_replay_slots[i])
  132. ocfs2_queue_recovery_completion(osb->journal, i, NULL,
  133. NULL, NULL);
  134. replay_map->rm_state = REPLAY_DONE;
  135. }
  136. void ocfs2_free_replay_slots(struct ocfs2_super *osb)
  137. {
  138. struct ocfs2_replay_map *replay_map = osb->replay_map;
  139. if (!osb->replay_map)
  140. return;
  141. kfree(replay_map);
  142. osb->replay_map = NULL;
  143. }
  144. int ocfs2_recovery_init(struct ocfs2_super *osb)
  145. {
  146. struct ocfs2_recovery_map *rm;
  147. mutex_init(&osb->recovery_lock);
  148. osb->disable_recovery = 0;
  149. osb->recovery_thread_task = NULL;
  150. init_waitqueue_head(&osb->recovery_event);
  151. rm = kzalloc(sizeof(struct ocfs2_recovery_map) +
  152. osb->max_slots * sizeof(unsigned int),
  153. GFP_KERNEL);
  154. if (!rm) {
  155. mlog_errno(-ENOMEM);
  156. return -ENOMEM;
  157. }
  158. rm->rm_entries = (unsigned int *)((char *)rm +
  159. sizeof(struct ocfs2_recovery_map));
  160. osb->recovery_map = rm;
  161. return 0;
  162. }
  163. /* we can't grab the goofy sem lock from inside wait_event, so we use
  164. * memory barriers to make sure that we'll see the null task before
  165. * being woken up */
  166. static int ocfs2_recovery_thread_running(struct ocfs2_super *osb)
  167. {
  168. mb();
  169. return osb->recovery_thread_task != NULL;
  170. }
  171. void ocfs2_recovery_exit(struct ocfs2_super *osb)
  172. {
  173. struct ocfs2_recovery_map *rm;
  174. /* disable any new recovery threads and wait for any currently
  175. * running ones to exit. Do this before setting the vol_state. */
  176. mutex_lock(&osb->recovery_lock);
  177. osb->disable_recovery = 1;
  178. mutex_unlock(&osb->recovery_lock);
  179. wait_event(osb->recovery_event, !ocfs2_recovery_thread_running(osb));
  180. /* At this point, we know that no more recovery threads can be
  181. * launched, so wait for any recovery completion work to
  182. * complete. */
  183. flush_workqueue(ocfs2_wq);
  184. /*
  185. * Now that recovery is shut down, and the osb is about to be
  186. * freed, the osb_lock is not taken here.
  187. */
  188. rm = osb->recovery_map;
  189. /* XXX: Should we bug if there are dirty entries? */
  190. kfree(rm);
  191. }
  192. static int __ocfs2_recovery_map_test(struct ocfs2_super *osb,
  193. unsigned int node_num)
  194. {
  195. int i;
  196. struct ocfs2_recovery_map *rm = osb->recovery_map;
  197. assert_spin_locked(&osb->osb_lock);
  198. for (i = 0; i < rm->rm_used; i++) {
  199. if (rm->rm_entries[i] == node_num)
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. /* Behaves like test-and-set. Returns the previous value */
  205. static int ocfs2_recovery_map_set(struct ocfs2_super *osb,
  206. unsigned int node_num)
  207. {
  208. struct ocfs2_recovery_map *rm = osb->recovery_map;
  209. spin_lock(&osb->osb_lock);
  210. if (__ocfs2_recovery_map_test(osb, node_num)) {
  211. spin_unlock(&osb->osb_lock);
  212. return 1;
  213. }
  214. /* XXX: Can this be exploited? Not from o2dlm... */
  215. BUG_ON(rm->rm_used >= osb->max_slots);
  216. rm->rm_entries[rm->rm_used] = node_num;
  217. rm->rm_used++;
  218. spin_unlock(&osb->osb_lock);
  219. return 0;
  220. }
  221. static void ocfs2_recovery_map_clear(struct ocfs2_super *osb,
  222. unsigned int node_num)
  223. {
  224. int i;
  225. struct ocfs2_recovery_map *rm = osb->recovery_map;
  226. spin_lock(&osb->osb_lock);
  227. for (i = 0; i < rm->rm_used; i++) {
  228. if (rm->rm_entries[i] == node_num)
  229. break;
  230. }
  231. if (i < rm->rm_used) {
  232. /* XXX: be careful with the pointer math */
  233. memmove(&(rm->rm_entries[i]), &(rm->rm_entries[i + 1]),
  234. (rm->rm_used - i - 1) * sizeof(unsigned int));
  235. rm->rm_used--;
  236. }
  237. spin_unlock(&osb->osb_lock);
  238. }
  239. static int ocfs2_commit_cache(struct ocfs2_super *osb)
  240. {
  241. int status = 0;
  242. unsigned int flushed;
  243. unsigned long old_id;
  244. struct ocfs2_journal *journal = NULL;
  245. mlog_entry_void();
  246. journal = osb->journal;
  247. /* Flush all pending commits and checkpoint the journal. */
  248. down_write(&journal->j_trans_barrier);
  249. if (atomic_read(&journal->j_num_trans) == 0) {
  250. up_write(&journal->j_trans_barrier);
  251. mlog(0, "No transactions for me to flush!\n");
  252. goto finally;
  253. }
  254. jbd2_journal_lock_updates(journal->j_journal);
  255. status = jbd2_journal_flush(journal->j_journal);
  256. jbd2_journal_unlock_updates(journal->j_journal);
  257. if (status < 0) {
  258. up_write(&journal->j_trans_barrier);
  259. mlog_errno(status);
  260. goto finally;
  261. }
  262. old_id = ocfs2_inc_trans_id(journal);
  263. flushed = atomic_read(&journal->j_num_trans);
  264. atomic_set(&journal->j_num_trans, 0);
  265. up_write(&journal->j_trans_barrier);
  266. mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
  267. journal->j_trans_id, flushed);
  268. ocfs2_wake_downconvert_thread(osb);
  269. wake_up(&journal->j_checkpointed);
  270. finally:
  271. mlog_exit(status);
  272. return status;
  273. }
  274. /* pass it NULL and it will allocate a new handle object for you. If
  275. * you pass it a handle however, it may still return error, in which
  276. * case it has free'd the passed handle for you. */
  277. handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
  278. {
  279. journal_t *journal = osb->journal->j_journal;
  280. handle_t *handle;
  281. BUG_ON(!osb || !osb->journal->j_journal);
  282. if (ocfs2_is_hard_readonly(osb))
  283. return ERR_PTR(-EROFS);
  284. BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
  285. BUG_ON(max_buffs <= 0);
  286. /* Nested transaction? Just return the handle... */
  287. if (journal_current_handle())
  288. return jbd2_journal_start(journal, max_buffs);
  289. down_read(&osb->journal->j_trans_barrier);
  290. handle = jbd2_journal_start(journal, max_buffs);
  291. if (IS_ERR(handle)) {
  292. up_read(&osb->journal->j_trans_barrier);
  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. return ret;
  317. }
  318. /*
  319. * 'nblocks' is what you want to add to the current
  320. * transaction. extend_trans will either extend the current handle by
  321. * nblocks, or commit it and start a new one with nblocks credits.
  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;
  340. BUG_ON(!handle);
  341. BUG_ON(!nblocks);
  342. mlog_entry_void();
  343. mlog(0, "Trying to extend transaction by %d blocks\n", nblocks);
  344. #ifdef CONFIG_OCFS2_DEBUG_FS
  345. status = 1;
  346. #else
  347. status = jbd2_journal_extend(handle, nblocks);
  348. if (status < 0) {
  349. mlog_errno(status);
  350. goto bail;
  351. }
  352. #endif
  353. if (status > 0) {
  354. mlog(0,
  355. "jbd2_journal_extend failed, trying "
  356. "jbd2_journal_restart\n");
  357. status = jbd2_journal_restart(handle, nblocks);
  358. if (status < 0) {
  359. mlog_errno(status);
  360. goto bail;
  361. }
  362. }
  363. status = 0;
  364. bail:
  365. mlog_exit(status);
  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_commit_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_commit_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_commit_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_commit = ocfs2_commit_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_commit = ocfs2_commit_trigger,
  448. .t_abort = ocfs2_abort_trigger,
  449. },
  450. .ot_offset = offsetof(struct ocfs2_extent_block, h_check),
  451. };
  452. static struct ocfs2_triggers gd_triggers = {
  453. .ot_triggers = {
  454. .t_commit = ocfs2_commit_trigger,
  455. .t_abort = ocfs2_abort_trigger,
  456. },
  457. .ot_offset = offsetof(struct ocfs2_group_desc, bg_check),
  458. };
  459. static struct ocfs2_triggers db_triggers = {
  460. .ot_triggers = {
  461. .t_commit = ocfs2_db_commit_trigger,
  462. .t_abort = ocfs2_abort_trigger,
  463. },
  464. };
  465. static struct ocfs2_triggers xb_triggers = {
  466. .ot_triggers = {
  467. .t_commit = ocfs2_commit_trigger,
  468. .t_abort = ocfs2_abort_trigger,
  469. },
  470. .ot_offset = offsetof(struct ocfs2_xattr_block, xb_check),
  471. };
  472. static struct ocfs2_triggers dq_triggers = {
  473. .ot_triggers = {
  474. .t_commit = ocfs2_dq_commit_trigger,
  475. .t_abort = ocfs2_abort_trigger,
  476. },
  477. };
  478. static struct ocfs2_triggers dr_triggers = {
  479. .ot_triggers = {
  480. .t_commit = ocfs2_commit_trigger,
  481. .t_abort = ocfs2_abort_trigger,
  482. },
  483. .ot_offset = offsetof(struct ocfs2_dx_root_block, dr_check),
  484. };
  485. static struct ocfs2_triggers dl_triggers = {
  486. .ot_triggers = {
  487. .t_commit = ocfs2_commit_trigger,
  488. .t_abort = ocfs2_abort_trigger,
  489. },
  490. .ot_offset = offsetof(struct ocfs2_dx_leaf, dl_check),
  491. };
  492. static int __ocfs2_journal_access(handle_t *handle,
  493. struct inode *inode,
  494. struct buffer_head *bh,
  495. struct ocfs2_triggers *triggers,
  496. int type)
  497. {
  498. int status;
  499. BUG_ON(!inode);
  500. BUG_ON(!handle);
  501. BUG_ON(!bh);
  502. mlog_entry("bh->b_blocknr=%llu, type=%d (\"%s\"), bh->b_size = %zu\n",
  503. (unsigned long long)bh->b_blocknr, type,
  504. (type == OCFS2_JOURNAL_ACCESS_CREATE) ?
  505. "OCFS2_JOURNAL_ACCESS_CREATE" :
  506. "OCFS2_JOURNAL_ACCESS_WRITE",
  507. bh->b_size);
  508. /* we can safely remove this assertion after testing. */
  509. if (!buffer_uptodate(bh)) {
  510. mlog(ML_ERROR, "giving me a buffer that's not uptodate!\n");
  511. mlog(ML_ERROR, "b_blocknr=%llu\n",
  512. (unsigned long long)bh->b_blocknr);
  513. BUG();
  514. }
  515. /* Set the current transaction information on the inode so
  516. * that the locking code knows whether it can drop it's locks
  517. * on this inode or not. We're protected from the commit
  518. * thread updating the current transaction id until
  519. * ocfs2_commit_trans() because ocfs2_start_trans() took
  520. * j_trans_barrier for us. */
  521. ocfs2_set_inode_lock_trans(OCFS2_SB(inode->i_sb)->journal, inode);
  522. mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
  523. switch (type) {
  524. case OCFS2_JOURNAL_ACCESS_CREATE:
  525. case OCFS2_JOURNAL_ACCESS_WRITE:
  526. status = jbd2_journal_get_write_access(handle, bh);
  527. break;
  528. case OCFS2_JOURNAL_ACCESS_UNDO:
  529. status = jbd2_journal_get_undo_access(handle, bh);
  530. break;
  531. default:
  532. status = -EINVAL;
  533. mlog(ML_ERROR, "Uknown access type!\n");
  534. }
  535. if (!status && ocfs2_meta_ecc(OCFS2_SB(inode->i_sb)) && triggers)
  536. jbd2_journal_set_triggers(bh, &triggers->ot_triggers);
  537. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  538. if (status < 0)
  539. mlog(ML_ERROR, "Error %d getting %d access to buffer!\n",
  540. status, type);
  541. mlog_exit(status);
  542. return status;
  543. }
  544. int ocfs2_journal_access_di(handle_t *handle, struct inode *inode,
  545. struct buffer_head *bh, int type)
  546. {
  547. return __ocfs2_journal_access(handle, inode, bh, &di_triggers,
  548. type);
  549. }
  550. int ocfs2_journal_access_eb(handle_t *handle, struct inode *inode,
  551. struct buffer_head *bh, int type)
  552. {
  553. return __ocfs2_journal_access(handle, inode, bh, &eb_triggers,
  554. type);
  555. }
  556. int ocfs2_journal_access_gd(handle_t *handle, struct inode *inode,
  557. struct buffer_head *bh, int type)
  558. {
  559. return __ocfs2_journal_access(handle, inode, bh, &gd_triggers,
  560. type);
  561. }
  562. int ocfs2_journal_access_db(handle_t *handle, struct inode *inode,
  563. struct buffer_head *bh, int type)
  564. {
  565. return __ocfs2_journal_access(handle, inode, bh, &db_triggers,
  566. type);
  567. }
  568. int ocfs2_journal_access_xb(handle_t *handle, struct inode *inode,
  569. struct buffer_head *bh, int type)
  570. {
  571. return __ocfs2_journal_access(handle, inode, bh, &xb_triggers,
  572. type);
  573. }
  574. int ocfs2_journal_access_dq(handle_t *handle, struct inode *inode,
  575. struct buffer_head *bh, int type)
  576. {
  577. return __ocfs2_journal_access(handle, inode, bh, &dq_triggers,
  578. type);
  579. }
  580. int ocfs2_journal_access_dr(handle_t *handle, struct inode *inode,
  581. struct buffer_head *bh, int type)
  582. {
  583. return __ocfs2_journal_access(handle, inode, bh, &dr_triggers,
  584. type);
  585. }
  586. int ocfs2_journal_access_dl(handle_t *handle, struct inode *inode,
  587. struct buffer_head *bh, int type)
  588. {
  589. return __ocfs2_journal_access(handle, inode, bh, &dl_triggers,
  590. type);
  591. }
  592. int ocfs2_journal_access(handle_t *handle, struct inode *inode,
  593. struct buffer_head *bh, int type)
  594. {
  595. return __ocfs2_journal_access(handle, inode, bh, NULL, type);
  596. }
  597. int ocfs2_journal_dirty(handle_t *handle,
  598. struct buffer_head *bh)
  599. {
  600. int status;
  601. mlog_entry("(bh->b_blocknr=%llu)\n",
  602. (unsigned long long)bh->b_blocknr);
  603. status = jbd2_journal_dirty_metadata(handle, bh);
  604. if (status < 0)
  605. mlog(ML_ERROR, "Could not dirty metadata buffer. "
  606. "(bh->b_blocknr=%llu)\n",
  607. (unsigned long long)bh->b_blocknr);
  608. mlog_exit(status);
  609. return status;
  610. }
  611. #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
  612. void ocfs2_set_journal_params(struct ocfs2_super *osb)
  613. {
  614. journal_t *journal = osb->journal->j_journal;
  615. unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
  616. if (osb->osb_commit_interval)
  617. commit_interval = osb->osb_commit_interval;
  618. spin_lock(&journal->j_state_lock);
  619. journal->j_commit_interval = commit_interval;
  620. if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
  621. journal->j_flags |= JBD2_BARRIER;
  622. else
  623. journal->j_flags &= ~JBD2_BARRIER;
  624. spin_unlock(&journal->j_state_lock);
  625. }
  626. int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
  627. {
  628. int status = -1;
  629. struct inode *inode = NULL; /* the journal inode */
  630. journal_t *j_journal = NULL;
  631. struct ocfs2_dinode *di = NULL;
  632. struct buffer_head *bh = NULL;
  633. struct ocfs2_super *osb;
  634. int inode_lock = 0;
  635. mlog_entry_void();
  636. BUG_ON(!journal);
  637. osb = journal->j_osb;
  638. /* already have the inode for our journal */
  639. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  640. osb->slot_num);
  641. if (inode == NULL) {
  642. status = -EACCES;
  643. mlog_errno(status);
  644. goto done;
  645. }
  646. if (is_bad_inode(inode)) {
  647. mlog(ML_ERROR, "access error (bad inode)\n");
  648. iput(inode);
  649. inode = NULL;
  650. status = -EACCES;
  651. goto done;
  652. }
  653. SET_INODE_JOURNAL(inode);
  654. OCFS2_I(inode)->ip_open_count++;
  655. /* Skip recovery waits here - journal inode metadata never
  656. * changes in a live cluster so it can be considered an
  657. * exception to the rule. */
  658. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  659. if (status < 0) {
  660. if (status != -ERESTARTSYS)
  661. mlog(ML_ERROR, "Could not get lock on journal!\n");
  662. goto done;
  663. }
  664. inode_lock = 1;
  665. di = (struct ocfs2_dinode *)bh->b_data;
  666. if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
  667. mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
  668. inode->i_size);
  669. status = -EINVAL;
  670. goto done;
  671. }
  672. mlog(0, "inode->i_size = %lld\n", inode->i_size);
  673. mlog(0, "inode->i_blocks = %llu\n",
  674. (unsigned long long)inode->i_blocks);
  675. mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
  676. /* call the kernels journal init function now */
  677. j_journal = jbd2_journal_init_inode(inode);
  678. if (j_journal == NULL) {
  679. mlog(ML_ERROR, "Linux journal layer error\n");
  680. status = -EINVAL;
  681. goto done;
  682. }
  683. mlog(0, "Returned from jbd2_journal_init_inode\n");
  684. mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
  685. *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
  686. OCFS2_JOURNAL_DIRTY_FL);
  687. journal->j_journal = j_journal;
  688. journal->j_inode = inode;
  689. journal->j_bh = bh;
  690. ocfs2_set_journal_params(osb);
  691. journal->j_state = OCFS2_JOURNAL_LOADED;
  692. status = 0;
  693. done:
  694. if (status < 0) {
  695. if (inode_lock)
  696. ocfs2_inode_unlock(inode, 1);
  697. brelse(bh);
  698. if (inode) {
  699. OCFS2_I(inode)->ip_open_count--;
  700. iput(inode);
  701. }
  702. }
  703. mlog_exit(status);
  704. return status;
  705. }
  706. static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
  707. {
  708. le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
  709. }
  710. static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
  711. {
  712. return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
  713. }
  714. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  715. int dirty, int replayed)
  716. {
  717. int status;
  718. unsigned int flags;
  719. struct ocfs2_journal *journal = osb->journal;
  720. struct buffer_head *bh = journal->j_bh;
  721. struct ocfs2_dinode *fe;
  722. mlog_entry_void();
  723. fe = (struct ocfs2_dinode *)bh->b_data;
  724. /* The journal bh on the osb always comes from ocfs2_journal_init()
  725. * and was validated there inside ocfs2_inode_lock_full(). It's a
  726. * code bug if we mess it up. */
  727. BUG_ON(!OCFS2_IS_VALID_DINODE(fe));
  728. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  729. if (dirty)
  730. flags |= OCFS2_JOURNAL_DIRTY_FL;
  731. else
  732. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  733. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  734. if (replayed)
  735. ocfs2_bump_recovery_generation(fe);
  736. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
  737. status = ocfs2_write_block(osb, bh, journal->j_inode);
  738. if (status < 0)
  739. mlog_errno(status);
  740. mlog_exit(status);
  741. return status;
  742. }
  743. /*
  744. * If the journal has been kmalloc'd it needs to be freed after this
  745. * call.
  746. */
  747. void ocfs2_journal_shutdown(struct ocfs2_super *osb)
  748. {
  749. struct ocfs2_journal *journal = NULL;
  750. int status = 0;
  751. struct inode *inode = NULL;
  752. int num_running_trans = 0;
  753. mlog_entry_void();
  754. BUG_ON(!osb);
  755. journal = osb->journal;
  756. if (!journal)
  757. goto done;
  758. inode = journal->j_inode;
  759. if (journal->j_state != OCFS2_JOURNAL_LOADED)
  760. goto done;
  761. /* need to inc inode use count - jbd2_journal_destroy will iput. */
  762. if (!igrab(inode))
  763. BUG();
  764. num_running_trans = atomic_read(&(osb->journal->j_num_trans));
  765. if (num_running_trans > 0)
  766. mlog(0, "Shutting down journal: must wait on %d "
  767. "running transactions!\n",
  768. num_running_trans);
  769. /* Do a commit_cache here. It will flush our journal, *and*
  770. * release any locks that are still held.
  771. * set the SHUTDOWN flag and release the trans lock.
  772. * the commit thread will take the trans lock for us below. */
  773. journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
  774. /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
  775. * drop the trans_lock (which we want to hold until we
  776. * completely destroy the journal. */
  777. if (osb->commit_task) {
  778. /* Wait for the commit thread */
  779. mlog(0, "Waiting for ocfs2commit to exit....\n");
  780. kthread_stop(osb->commit_task);
  781. osb->commit_task = NULL;
  782. }
  783. BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
  784. if (ocfs2_mount_local(osb)) {
  785. jbd2_journal_lock_updates(journal->j_journal);
  786. status = jbd2_journal_flush(journal->j_journal);
  787. jbd2_journal_unlock_updates(journal->j_journal);
  788. if (status < 0)
  789. mlog_errno(status);
  790. }
  791. if (status == 0) {
  792. /*
  793. * Do not toggle if flush was unsuccessful otherwise
  794. * will leave dirty metadata in a "clean" journal
  795. */
  796. status = ocfs2_journal_toggle_dirty(osb, 0, 0);
  797. if (status < 0)
  798. mlog_errno(status);
  799. }
  800. /* Shutdown the kernel journal system */
  801. jbd2_journal_destroy(journal->j_journal);
  802. journal->j_journal = NULL;
  803. OCFS2_I(inode)->ip_open_count--;
  804. /* unlock our journal */
  805. ocfs2_inode_unlock(inode, 1);
  806. brelse(journal->j_bh);
  807. journal->j_bh = NULL;
  808. journal->j_state = OCFS2_JOURNAL_FREE;
  809. // up_write(&journal->j_trans_barrier);
  810. done:
  811. if (inode)
  812. iput(inode);
  813. mlog_exit_void();
  814. }
  815. static void ocfs2_clear_journal_error(struct super_block *sb,
  816. journal_t *journal,
  817. int slot)
  818. {
  819. int olderr;
  820. olderr = jbd2_journal_errno(journal);
  821. if (olderr) {
  822. mlog(ML_ERROR, "File system error %d recorded in "
  823. "journal %u.\n", olderr, slot);
  824. mlog(ML_ERROR, "File system on device %s needs checking.\n",
  825. sb->s_id);
  826. jbd2_journal_ack_err(journal);
  827. jbd2_journal_clear_err(journal);
  828. }
  829. }
  830. int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
  831. {
  832. int status = 0;
  833. struct ocfs2_super *osb;
  834. mlog_entry_void();
  835. BUG_ON(!journal);
  836. osb = journal->j_osb;
  837. status = jbd2_journal_load(journal->j_journal);
  838. if (status < 0) {
  839. mlog(ML_ERROR, "Failed to load journal!\n");
  840. goto done;
  841. }
  842. ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
  843. status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
  844. if (status < 0) {
  845. mlog_errno(status);
  846. goto done;
  847. }
  848. /* Launch the commit thread */
  849. if (!local) {
  850. osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
  851. "ocfs2cmt");
  852. if (IS_ERR(osb->commit_task)) {
  853. status = PTR_ERR(osb->commit_task);
  854. osb->commit_task = NULL;
  855. mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
  856. "error=%d", status);
  857. goto done;
  858. }
  859. } else
  860. osb->commit_task = NULL;
  861. done:
  862. mlog_exit(status);
  863. return status;
  864. }
  865. /* 'full' flag tells us whether we clear out all blocks or if we just
  866. * mark the journal clean */
  867. int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
  868. {
  869. int status;
  870. mlog_entry_void();
  871. BUG_ON(!journal);
  872. status = jbd2_journal_wipe(journal->j_journal, full);
  873. if (status < 0) {
  874. mlog_errno(status);
  875. goto bail;
  876. }
  877. status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
  878. if (status < 0)
  879. mlog_errno(status);
  880. bail:
  881. mlog_exit(status);
  882. return status;
  883. }
  884. static int ocfs2_recovery_completed(struct ocfs2_super *osb)
  885. {
  886. int empty;
  887. struct ocfs2_recovery_map *rm = osb->recovery_map;
  888. spin_lock(&osb->osb_lock);
  889. empty = (rm->rm_used == 0);
  890. spin_unlock(&osb->osb_lock);
  891. return empty;
  892. }
  893. void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
  894. {
  895. wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
  896. }
  897. /*
  898. * JBD Might read a cached version of another nodes journal file. We
  899. * don't want this as this file changes often and we get no
  900. * notification on those changes. The only way to be sure that we've
  901. * got the most up to date version of those blocks then is to force
  902. * read them off disk. Just searching through the buffer cache won't
  903. * work as there may be pages backing this file which are still marked
  904. * up to date. We know things can't change on this file underneath us
  905. * as we have the lock by now :)
  906. */
  907. static int ocfs2_force_read_journal(struct inode *inode)
  908. {
  909. int status = 0;
  910. int i;
  911. u64 v_blkno, p_blkno, p_blocks, num_blocks;
  912. #define CONCURRENT_JOURNAL_FILL 32ULL
  913. struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
  914. mlog_entry_void();
  915. memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
  916. num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
  917. v_blkno = 0;
  918. while (v_blkno < num_blocks) {
  919. status = ocfs2_extent_map_get_blocks(inode, v_blkno,
  920. &p_blkno, &p_blocks, NULL);
  921. if (status < 0) {
  922. mlog_errno(status);
  923. goto bail;
  924. }
  925. if (p_blocks > CONCURRENT_JOURNAL_FILL)
  926. p_blocks = CONCURRENT_JOURNAL_FILL;
  927. /* We are reading journal data which should not
  928. * be put in the uptodate cache */
  929. status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
  930. p_blkno, p_blocks, bhs);
  931. if (status < 0) {
  932. mlog_errno(status);
  933. goto bail;
  934. }
  935. for(i = 0; i < p_blocks; i++) {
  936. brelse(bhs[i]);
  937. bhs[i] = NULL;
  938. }
  939. v_blkno += p_blocks;
  940. }
  941. bail:
  942. for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
  943. brelse(bhs[i]);
  944. mlog_exit(status);
  945. return status;
  946. }
  947. struct ocfs2_la_recovery_item {
  948. struct list_head lri_list;
  949. int lri_slot;
  950. struct ocfs2_dinode *lri_la_dinode;
  951. struct ocfs2_dinode *lri_tl_dinode;
  952. struct ocfs2_quota_recovery *lri_qrec;
  953. };
  954. /* Does the second half of the recovery process. By this point, the
  955. * node is marked clean and can actually be considered recovered,
  956. * hence it's no longer in the recovery map, but there's still some
  957. * cleanup we can do which shouldn't happen within the recovery thread
  958. * as locking in that context becomes very difficult if we are to take
  959. * recovering nodes into account.
  960. *
  961. * NOTE: This function can and will sleep on recovery of other nodes
  962. * during cluster locking, just like any other ocfs2 process.
  963. */
  964. void ocfs2_complete_recovery(struct work_struct *work)
  965. {
  966. int ret;
  967. struct ocfs2_journal *journal =
  968. container_of(work, struct ocfs2_journal, j_recovery_work);
  969. struct ocfs2_super *osb = journal->j_osb;
  970. struct ocfs2_dinode *la_dinode, *tl_dinode;
  971. struct ocfs2_la_recovery_item *item, *n;
  972. struct ocfs2_quota_recovery *qrec;
  973. LIST_HEAD(tmp_la_list);
  974. mlog_entry_void();
  975. mlog(0, "completing recovery from keventd\n");
  976. spin_lock(&journal->j_lock);
  977. list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
  978. spin_unlock(&journal->j_lock);
  979. list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
  980. list_del_init(&item->lri_list);
  981. mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
  982. ocfs2_wait_on_quotas(osb);
  983. la_dinode = item->lri_la_dinode;
  984. if (la_dinode) {
  985. mlog(0, "Clean up local alloc %llu\n",
  986. (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
  987. ret = ocfs2_complete_local_alloc_recovery(osb,
  988. la_dinode);
  989. if (ret < 0)
  990. mlog_errno(ret);
  991. kfree(la_dinode);
  992. }
  993. tl_dinode = item->lri_tl_dinode;
  994. if (tl_dinode) {
  995. mlog(0, "Clean up truncate log %llu\n",
  996. (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
  997. ret = ocfs2_complete_truncate_log_recovery(osb,
  998. tl_dinode);
  999. if (ret < 0)
  1000. mlog_errno(ret);
  1001. kfree(tl_dinode);
  1002. }
  1003. ret = ocfs2_recover_orphans(osb, item->lri_slot);
  1004. if (ret < 0)
  1005. mlog_errno(ret);
  1006. qrec = item->lri_qrec;
  1007. if (qrec) {
  1008. mlog(0, "Recovering quota files");
  1009. ret = ocfs2_finish_quota_recovery(osb, qrec,
  1010. item->lri_slot);
  1011. if (ret < 0)
  1012. mlog_errno(ret);
  1013. /* Recovery info is already freed now */
  1014. }
  1015. kfree(item);
  1016. }
  1017. mlog(0, "Recovery completion\n");
  1018. mlog_exit_void();
  1019. }
  1020. /* NOTE: This function always eats your references to la_dinode and
  1021. * tl_dinode, either manually on error, or by passing them to
  1022. * ocfs2_complete_recovery */
  1023. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  1024. int slot_num,
  1025. struct ocfs2_dinode *la_dinode,
  1026. struct ocfs2_dinode *tl_dinode,
  1027. struct ocfs2_quota_recovery *qrec)
  1028. {
  1029. struct ocfs2_la_recovery_item *item;
  1030. item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
  1031. if (!item) {
  1032. /* Though we wish to avoid it, we are in fact safe in
  1033. * skipping local alloc cleanup as fsck.ocfs2 is more
  1034. * than capable of reclaiming unused space. */
  1035. if (la_dinode)
  1036. kfree(la_dinode);
  1037. if (tl_dinode)
  1038. kfree(tl_dinode);
  1039. if (qrec)
  1040. ocfs2_free_quota_recovery(qrec);
  1041. mlog_errno(-ENOMEM);
  1042. return;
  1043. }
  1044. INIT_LIST_HEAD(&item->lri_list);
  1045. item->lri_la_dinode = la_dinode;
  1046. item->lri_slot = slot_num;
  1047. item->lri_tl_dinode = tl_dinode;
  1048. item->lri_qrec = qrec;
  1049. spin_lock(&journal->j_lock);
  1050. list_add_tail(&item->lri_list, &journal->j_la_cleanups);
  1051. queue_work(ocfs2_wq, &journal->j_recovery_work);
  1052. spin_unlock(&journal->j_lock);
  1053. }
  1054. /* Called by the mount code to queue recovery the last part of
  1055. * recovery for it's own and offline slot(s). */
  1056. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
  1057. {
  1058. struct ocfs2_journal *journal = osb->journal;
  1059. /* No need to queue up our truncate_log as regular cleanup will catch
  1060. * that */
  1061. ocfs2_queue_recovery_completion(journal, osb->slot_num,
  1062. osb->local_alloc_copy, NULL, NULL);
  1063. ocfs2_schedule_truncate_log_flush(osb, 0);
  1064. osb->local_alloc_copy = NULL;
  1065. osb->dirty = 0;
  1066. /* queue to recover orphan slots for all offline slots */
  1067. ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
  1068. ocfs2_queue_replay_slots(osb);
  1069. ocfs2_free_replay_slots(osb);
  1070. }
  1071. void ocfs2_complete_quota_recovery(struct ocfs2_super *osb)
  1072. {
  1073. if (osb->quota_rec) {
  1074. ocfs2_queue_recovery_completion(osb->journal,
  1075. osb->slot_num,
  1076. NULL,
  1077. NULL,
  1078. osb->quota_rec);
  1079. osb->quota_rec = NULL;
  1080. }
  1081. }
  1082. static int __ocfs2_recovery_thread(void *arg)
  1083. {
  1084. int status, node_num, slot_num;
  1085. struct ocfs2_super *osb = arg;
  1086. struct ocfs2_recovery_map *rm = osb->recovery_map;
  1087. int *rm_quota = NULL;
  1088. int rm_quota_used = 0, i;
  1089. struct ocfs2_quota_recovery *qrec;
  1090. mlog_entry_void();
  1091. status = ocfs2_wait_on_mount(osb);
  1092. if (status < 0) {
  1093. goto bail;
  1094. }
  1095. rm_quota = kzalloc(osb->max_slots * sizeof(int), GFP_NOFS);
  1096. if (!rm_quota) {
  1097. status = -ENOMEM;
  1098. goto bail;
  1099. }
  1100. restart:
  1101. status = ocfs2_super_lock(osb, 1);
  1102. if (status < 0) {
  1103. mlog_errno(status);
  1104. goto bail;
  1105. }
  1106. status = ocfs2_compute_replay_slots(osb);
  1107. if (status < 0)
  1108. mlog_errno(status);
  1109. /* queue recovery for our own slot */
  1110. ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
  1111. NULL, NULL);
  1112. spin_lock(&osb->osb_lock);
  1113. while (rm->rm_used) {
  1114. /* It's always safe to remove entry zero, as we won't
  1115. * clear it until ocfs2_recover_node() has succeeded. */
  1116. node_num = rm->rm_entries[0];
  1117. spin_unlock(&osb->osb_lock);
  1118. mlog(0, "checking node %d\n", node_num);
  1119. slot_num = ocfs2_node_num_to_slot(osb, node_num);
  1120. if (slot_num == -ENOENT) {
  1121. status = 0;
  1122. mlog(0, "no slot for this node, so no recovery"
  1123. "required.\n");
  1124. goto skip_recovery;
  1125. }
  1126. mlog(0, "node %d was using slot %d\n", node_num, slot_num);
  1127. /* It is a bit subtle with quota recovery. We cannot do it
  1128. * immediately because we have to obtain cluster locks from
  1129. * quota files and we also don't want to just skip it because
  1130. * then quota usage would be out of sync until some node takes
  1131. * the slot. So we remember which nodes need quota recovery
  1132. * and when everything else is done, we recover quotas. */
  1133. for (i = 0; i < rm_quota_used && rm_quota[i] != slot_num; i++);
  1134. if (i == rm_quota_used)
  1135. rm_quota[rm_quota_used++] = slot_num;
  1136. status = ocfs2_recover_node(osb, node_num, slot_num);
  1137. skip_recovery:
  1138. if (!status) {
  1139. ocfs2_recovery_map_clear(osb, node_num);
  1140. } else {
  1141. mlog(ML_ERROR,
  1142. "Error %d recovering node %d on device (%u,%u)!\n",
  1143. status, node_num,
  1144. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1145. mlog(ML_ERROR, "Volume requires unmount.\n");
  1146. }
  1147. spin_lock(&osb->osb_lock);
  1148. }
  1149. spin_unlock(&osb->osb_lock);
  1150. mlog(0, "All nodes recovered\n");
  1151. /* Refresh all journal recovery generations from disk */
  1152. status = ocfs2_check_journals_nolocks(osb);
  1153. status = (status == -EROFS) ? 0 : status;
  1154. if (status < 0)
  1155. mlog_errno(status);
  1156. /* Now it is right time to recover quotas... We have to do this under
  1157. * superblock lock so that noone can start using the slot (and crash)
  1158. * before we recover it */
  1159. for (i = 0; i < rm_quota_used; i++) {
  1160. qrec = ocfs2_begin_quota_recovery(osb, rm_quota[i]);
  1161. if (IS_ERR(qrec)) {
  1162. status = PTR_ERR(qrec);
  1163. mlog_errno(status);
  1164. continue;
  1165. }
  1166. ocfs2_queue_recovery_completion(osb->journal, rm_quota[i],
  1167. NULL, NULL, qrec);
  1168. }
  1169. ocfs2_super_unlock(osb, 1);
  1170. /* queue recovery for offline slots */
  1171. ocfs2_queue_replay_slots(osb);
  1172. bail:
  1173. mutex_lock(&osb->recovery_lock);
  1174. if (!status && !ocfs2_recovery_completed(osb)) {
  1175. mutex_unlock(&osb->recovery_lock);
  1176. goto restart;
  1177. }
  1178. ocfs2_free_replay_slots(osb);
  1179. osb->recovery_thread_task = NULL;
  1180. mb(); /* sync with ocfs2_recovery_thread_running */
  1181. wake_up(&osb->recovery_event);
  1182. mutex_unlock(&osb->recovery_lock);
  1183. if (rm_quota)
  1184. kfree(rm_quota);
  1185. mlog_exit(status);
  1186. /* no one is callint kthread_stop() for us so the kthread() api
  1187. * requires that we call do_exit(). And it isn't exported, but
  1188. * complete_and_exit() seems to be a minimal wrapper around it. */
  1189. complete_and_exit(NULL, status);
  1190. return status;
  1191. }
  1192. void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
  1193. {
  1194. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  1195. node_num, osb->node_num);
  1196. mutex_lock(&osb->recovery_lock);
  1197. if (osb->disable_recovery)
  1198. goto out;
  1199. /* People waiting on recovery will wait on
  1200. * the recovery map to empty. */
  1201. if (ocfs2_recovery_map_set(osb, node_num))
  1202. mlog(0, "node %d already in recovery map.\n", node_num);
  1203. mlog(0, "starting recovery thread...\n");
  1204. if (osb->recovery_thread_task)
  1205. goto out;
  1206. osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
  1207. "ocfs2rec");
  1208. if (IS_ERR(osb->recovery_thread_task)) {
  1209. mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
  1210. osb->recovery_thread_task = NULL;
  1211. }
  1212. out:
  1213. mutex_unlock(&osb->recovery_lock);
  1214. wake_up(&osb->recovery_event);
  1215. mlog_exit_void();
  1216. }
  1217. static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
  1218. int slot_num,
  1219. struct buffer_head **bh,
  1220. struct inode **ret_inode)
  1221. {
  1222. int status = -EACCES;
  1223. struct inode *inode = NULL;
  1224. BUG_ON(slot_num >= osb->max_slots);
  1225. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1226. slot_num);
  1227. if (!inode || is_bad_inode(inode)) {
  1228. mlog_errno(status);
  1229. goto bail;
  1230. }
  1231. SET_INODE_JOURNAL(inode);
  1232. status = ocfs2_read_inode_block_full(inode, bh, OCFS2_BH_IGNORE_CACHE);
  1233. if (status < 0) {
  1234. mlog_errno(status);
  1235. goto bail;
  1236. }
  1237. status = 0;
  1238. bail:
  1239. if (inode) {
  1240. if (status || !ret_inode)
  1241. iput(inode);
  1242. else
  1243. *ret_inode = inode;
  1244. }
  1245. return status;
  1246. }
  1247. /* Does the actual journal replay and marks the journal inode as
  1248. * clean. Will only replay if the journal inode is marked dirty. */
  1249. static int ocfs2_replay_journal(struct ocfs2_super *osb,
  1250. int node_num,
  1251. int slot_num)
  1252. {
  1253. int status;
  1254. int got_lock = 0;
  1255. unsigned int flags;
  1256. struct inode *inode = NULL;
  1257. struct ocfs2_dinode *fe;
  1258. journal_t *journal = NULL;
  1259. struct buffer_head *bh = NULL;
  1260. u32 slot_reco_gen;
  1261. status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
  1262. if (status) {
  1263. mlog_errno(status);
  1264. goto done;
  1265. }
  1266. fe = (struct ocfs2_dinode *)bh->b_data;
  1267. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  1268. brelse(bh);
  1269. bh = NULL;
  1270. /*
  1271. * As the fs recovery is asynchronous, there is a small chance that
  1272. * another node mounted (and recovered) the slot before the recovery
  1273. * thread could get the lock. To handle that, we dirty read the journal
  1274. * inode for that slot to get the recovery generation. If it is
  1275. * different than what we expected, the slot has been recovered.
  1276. * If not, it needs recovery.
  1277. */
  1278. if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
  1279. mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
  1280. osb->slot_recovery_generations[slot_num], slot_reco_gen);
  1281. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  1282. status = -EBUSY;
  1283. goto done;
  1284. }
  1285. /* Continue with recovery as the journal has not yet been recovered */
  1286. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  1287. if (status < 0) {
  1288. mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
  1289. if (status != -ERESTARTSYS)
  1290. mlog(ML_ERROR, "Could not lock journal!\n");
  1291. goto done;
  1292. }
  1293. got_lock = 1;
  1294. fe = (struct ocfs2_dinode *) bh->b_data;
  1295. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  1296. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  1297. if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
  1298. mlog(0, "No recovery required for node %d\n", node_num);
  1299. /* Refresh recovery generation for the slot */
  1300. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  1301. goto done;
  1302. }
  1303. /* we need to run complete recovery for offline orphan slots */
  1304. ocfs2_replay_map_set_state(osb, REPLAY_NEEDED);
  1305. mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
  1306. node_num, slot_num,
  1307. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  1308. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  1309. status = ocfs2_force_read_journal(inode);
  1310. if (status < 0) {
  1311. mlog_errno(status);
  1312. goto done;
  1313. }
  1314. mlog(0, "calling journal_init_inode\n");
  1315. journal = jbd2_journal_init_inode(inode);
  1316. if (journal == NULL) {
  1317. mlog(ML_ERROR, "Linux journal layer error\n");
  1318. status = -EIO;
  1319. goto done;
  1320. }
  1321. status = jbd2_journal_load(journal);
  1322. if (status < 0) {
  1323. mlog_errno(status);
  1324. if (!igrab(inode))
  1325. BUG();
  1326. jbd2_journal_destroy(journal);
  1327. goto done;
  1328. }
  1329. ocfs2_clear_journal_error(osb->sb, journal, slot_num);
  1330. /* wipe the journal */
  1331. mlog(0, "flushing the journal.\n");
  1332. jbd2_journal_lock_updates(journal);
  1333. status = jbd2_journal_flush(journal);
  1334. jbd2_journal_unlock_updates(journal);
  1335. if (status < 0)
  1336. mlog_errno(status);
  1337. /* This will mark the node clean */
  1338. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  1339. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  1340. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  1341. /* Increment recovery generation to indicate successful recovery */
  1342. ocfs2_bump_recovery_generation(fe);
  1343. osb->slot_recovery_generations[slot_num] =
  1344. ocfs2_get_recovery_generation(fe);
  1345. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &fe->i_check);
  1346. status = ocfs2_write_block(osb, bh, inode);
  1347. if (status < 0)
  1348. mlog_errno(status);
  1349. if (!igrab(inode))
  1350. BUG();
  1351. jbd2_journal_destroy(journal);
  1352. done:
  1353. /* drop the lock on this nodes journal */
  1354. if (got_lock)
  1355. ocfs2_inode_unlock(inode, 1);
  1356. if (inode)
  1357. iput(inode);
  1358. brelse(bh);
  1359. mlog_exit(status);
  1360. return status;
  1361. }
  1362. /*
  1363. * Do the most important parts of node recovery:
  1364. * - Replay it's journal
  1365. * - Stamp a clean local allocator file
  1366. * - Stamp a clean truncate log
  1367. * - Mark the node clean
  1368. *
  1369. * If this function completes without error, a node in OCFS2 can be
  1370. * said to have been safely recovered. As a result, failure during the
  1371. * second part of a nodes recovery process (local alloc recovery) is
  1372. * far less concerning.
  1373. */
  1374. static int ocfs2_recover_node(struct ocfs2_super *osb,
  1375. int node_num, int slot_num)
  1376. {
  1377. int status = 0;
  1378. struct ocfs2_dinode *la_copy = NULL;
  1379. struct ocfs2_dinode *tl_copy = NULL;
  1380. mlog_entry("(node_num=%d, slot_num=%d, osb->node_num = %d)\n",
  1381. node_num, slot_num, osb->node_num);
  1382. /* Should not ever be called to recover ourselves -- in that
  1383. * case we should've called ocfs2_journal_load instead. */
  1384. BUG_ON(osb->node_num == node_num);
  1385. status = ocfs2_replay_journal(osb, node_num, slot_num);
  1386. if (status < 0) {
  1387. if (status == -EBUSY) {
  1388. mlog(0, "Skipping recovery for slot %u (node %u) "
  1389. "as another node has recovered it\n", slot_num,
  1390. node_num);
  1391. status = 0;
  1392. goto done;
  1393. }
  1394. mlog_errno(status);
  1395. goto done;
  1396. }
  1397. /* Stamp a clean local alloc file AFTER recovering the journal... */
  1398. status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
  1399. if (status < 0) {
  1400. mlog_errno(status);
  1401. goto done;
  1402. }
  1403. /* An error from begin_truncate_log_recovery is not
  1404. * serious enough to warrant halting the rest of
  1405. * recovery. */
  1406. status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
  1407. if (status < 0)
  1408. mlog_errno(status);
  1409. /* Likewise, this would be a strange but ultimately not so
  1410. * harmful place to get an error... */
  1411. status = ocfs2_clear_slot(osb, slot_num);
  1412. if (status < 0)
  1413. mlog_errno(status);
  1414. /* This will kfree the memory pointed to by la_copy and tl_copy */
  1415. ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
  1416. tl_copy, NULL);
  1417. status = 0;
  1418. done:
  1419. mlog_exit(status);
  1420. return status;
  1421. }
  1422. /* Test node liveness by trylocking his journal. If we get the lock,
  1423. * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
  1424. * still alive (we couldn't get the lock) and < 0 on error. */
  1425. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  1426. int slot_num)
  1427. {
  1428. int status, flags;
  1429. struct inode *inode = NULL;
  1430. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1431. slot_num);
  1432. if (inode == NULL) {
  1433. mlog(ML_ERROR, "access error\n");
  1434. status = -EACCES;
  1435. goto bail;
  1436. }
  1437. if (is_bad_inode(inode)) {
  1438. mlog(ML_ERROR, "access error (bad inode)\n");
  1439. iput(inode);
  1440. inode = NULL;
  1441. status = -EACCES;
  1442. goto bail;
  1443. }
  1444. SET_INODE_JOURNAL(inode);
  1445. flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
  1446. status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
  1447. if (status < 0) {
  1448. if (status != -EAGAIN)
  1449. mlog_errno(status);
  1450. goto bail;
  1451. }
  1452. ocfs2_inode_unlock(inode, 1);
  1453. bail:
  1454. if (inode)
  1455. iput(inode);
  1456. return status;
  1457. }
  1458. /* Call this underneath ocfs2_super_lock. It also assumes that the
  1459. * slot info struct has been updated from disk. */
  1460. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
  1461. {
  1462. unsigned int node_num;
  1463. int status, i;
  1464. u32 gen;
  1465. struct buffer_head *bh = NULL;
  1466. struct ocfs2_dinode *di;
  1467. /* This is called with the super block cluster lock, so we
  1468. * know that the slot map can't change underneath us. */
  1469. for (i = 0; i < osb->max_slots; i++) {
  1470. /* Read journal inode to get the recovery generation */
  1471. status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
  1472. if (status) {
  1473. mlog_errno(status);
  1474. goto bail;
  1475. }
  1476. di = (struct ocfs2_dinode *)bh->b_data;
  1477. gen = ocfs2_get_recovery_generation(di);
  1478. brelse(bh);
  1479. bh = NULL;
  1480. spin_lock(&osb->osb_lock);
  1481. osb->slot_recovery_generations[i] = gen;
  1482. mlog(0, "Slot %u recovery generation is %u\n", i,
  1483. osb->slot_recovery_generations[i]);
  1484. if (i == osb->slot_num) {
  1485. spin_unlock(&osb->osb_lock);
  1486. continue;
  1487. }
  1488. status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
  1489. if (status == -ENOENT) {
  1490. spin_unlock(&osb->osb_lock);
  1491. continue;
  1492. }
  1493. if (__ocfs2_recovery_map_test(osb, node_num)) {
  1494. spin_unlock(&osb->osb_lock);
  1495. continue;
  1496. }
  1497. spin_unlock(&osb->osb_lock);
  1498. /* Ok, we have a slot occupied by another node which
  1499. * is not in the recovery map. We trylock his journal
  1500. * file here to test if he's alive. */
  1501. status = ocfs2_trylock_journal(osb, i);
  1502. if (!status) {
  1503. /* Since we're called from mount, we know that
  1504. * the recovery thread can't race us on
  1505. * setting / checking the recovery bits. */
  1506. ocfs2_recovery_thread(osb, node_num);
  1507. } else if ((status < 0) && (status != -EAGAIN)) {
  1508. mlog_errno(status);
  1509. goto bail;
  1510. }
  1511. }
  1512. status = 0;
  1513. bail:
  1514. mlog_exit(status);
  1515. return status;
  1516. }
  1517. struct ocfs2_orphan_filldir_priv {
  1518. struct inode *head;
  1519. struct ocfs2_super *osb;
  1520. };
  1521. static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
  1522. loff_t pos, u64 ino, unsigned type)
  1523. {
  1524. struct ocfs2_orphan_filldir_priv *p = priv;
  1525. struct inode *iter;
  1526. if (name_len == 1 && !strncmp(".", name, 1))
  1527. return 0;
  1528. if (name_len == 2 && !strncmp("..", name, 2))
  1529. return 0;
  1530. /* Skip bad inodes so that recovery can continue */
  1531. iter = ocfs2_iget(p->osb, ino,
  1532. OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
  1533. if (IS_ERR(iter))
  1534. return 0;
  1535. mlog(0, "queue orphan %llu\n",
  1536. (unsigned long long)OCFS2_I(iter)->ip_blkno);
  1537. /* No locking is required for the next_orphan queue as there
  1538. * is only ever a single process doing orphan recovery. */
  1539. OCFS2_I(iter)->ip_next_orphan = p->head;
  1540. p->head = iter;
  1541. return 0;
  1542. }
  1543. static int ocfs2_queue_orphans(struct ocfs2_super *osb,
  1544. int slot,
  1545. struct inode **head)
  1546. {
  1547. int status;
  1548. struct inode *orphan_dir_inode = NULL;
  1549. struct ocfs2_orphan_filldir_priv priv;
  1550. loff_t pos = 0;
  1551. priv.osb = osb;
  1552. priv.head = *head;
  1553. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1554. ORPHAN_DIR_SYSTEM_INODE,
  1555. slot);
  1556. if (!orphan_dir_inode) {
  1557. status = -ENOENT;
  1558. mlog_errno(status);
  1559. return status;
  1560. }
  1561. mutex_lock(&orphan_dir_inode->i_mutex);
  1562. status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
  1563. if (status < 0) {
  1564. mlog_errno(status);
  1565. goto out;
  1566. }
  1567. status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
  1568. ocfs2_orphan_filldir);
  1569. if (status) {
  1570. mlog_errno(status);
  1571. goto out_cluster;
  1572. }
  1573. *head = priv.head;
  1574. out_cluster:
  1575. ocfs2_inode_unlock(orphan_dir_inode, 0);
  1576. out:
  1577. mutex_unlock(&orphan_dir_inode->i_mutex);
  1578. iput(orphan_dir_inode);
  1579. return status;
  1580. }
  1581. static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
  1582. int slot)
  1583. {
  1584. int ret;
  1585. spin_lock(&osb->osb_lock);
  1586. ret = !osb->osb_orphan_wipes[slot];
  1587. spin_unlock(&osb->osb_lock);
  1588. return ret;
  1589. }
  1590. static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
  1591. int slot)
  1592. {
  1593. spin_lock(&osb->osb_lock);
  1594. /* Mark ourselves such that new processes in delete_inode()
  1595. * know to quit early. */
  1596. ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1597. while (osb->osb_orphan_wipes[slot]) {
  1598. /* If any processes are already in the middle of an
  1599. * orphan wipe on this dir, then we need to wait for
  1600. * them. */
  1601. spin_unlock(&osb->osb_lock);
  1602. wait_event_interruptible(osb->osb_wipe_event,
  1603. ocfs2_orphan_recovery_can_continue(osb, slot));
  1604. spin_lock(&osb->osb_lock);
  1605. }
  1606. spin_unlock(&osb->osb_lock);
  1607. }
  1608. static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
  1609. int slot)
  1610. {
  1611. ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1612. }
  1613. /*
  1614. * Orphan recovery. Each mounted node has it's own orphan dir which we
  1615. * must run during recovery. Our strategy here is to build a list of
  1616. * the inodes in the orphan dir and iget/iput them. The VFS does
  1617. * (most) of the rest of the work.
  1618. *
  1619. * Orphan recovery can happen at any time, not just mount so we have a
  1620. * couple of extra considerations.
  1621. *
  1622. * - We grab as many inodes as we can under the orphan dir lock -
  1623. * doing iget() outside the orphan dir risks getting a reference on
  1624. * an invalid inode.
  1625. * - We must be sure not to deadlock with other processes on the
  1626. * system wanting to run delete_inode(). This can happen when they go
  1627. * to lock the orphan dir and the orphan recovery process attempts to
  1628. * iget() inside the orphan dir lock. This can be avoided by
  1629. * advertising our state to ocfs2_delete_inode().
  1630. */
  1631. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  1632. int slot)
  1633. {
  1634. int ret = 0;
  1635. struct inode *inode = NULL;
  1636. struct inode *iter;
  1637. struct ocfs2_inode_info *oi;
  1638. mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
  1639. ocfs2_mark_recovering_orphan_dir(osb, slot);
  1640. ret = ocfs2_queue_orphans(osb, slot, &inode);
  1641. ocfs2_clear_recovering_orphan_dir(osb, slot);
  1642. /* Error here should be noted, but we want to continue with as
  1643. * many queued inodes as we've got. */
  1644. if (ret)
  1645. mlog_errno(ret);
  1646. while (inode) {
  1647. oi = OCFS2_I(inode);
  1648. mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
  1649. iter = oi->ip_next_orphan;
  1650. spin_lock(&oi->ip_lock);
  1651. /* The remote delete code may have set these on the
  1652. * assumption that the other node would wipe them
  1653. * successfully. If they are still in the node's
  1654. * orphan dir, we need to reset that state. */
  1655. oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
  1656. /* Set the proper information to get us going into
  1657. * ocfs2_delete_inode. */
  1658. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  1659. spin_unlock(&oi->ip_lock);
  1660. iput(inode);
  1661. inode = iter;
  1662. }
  1663. return ret;
  1664. }
  1665. static int __ocfs2_wait_on_mount(struct ocfs2_super *osb, int quota)
  1666. {
  1667. /* This check is good because ocfs2 will wait on our recovery
  1668. * thread before changing it to something other than MOUNTED
  1669. * or DISABLED. */
  1670. wait_event(osb->osb_mount_event,
  1671. (!quota && atomic_read(&osb->vol_state) == VOLUME_MOUNTED) ||
  1672. atomic_read(&osb->vol_state) == VOLUME_MOUNTED_QUOTAS ||
  1673. atomic_read(&osb->vol_state) == VOLUME_DISABLED);
  1674. /* If there's an error on mount, then we may never get to the
  1675. * MOUNTED flag, but this is set right before
  1676. * dismount_volume() so we can trust it. */
  1677. if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
  1678. mlog(0, "mount error, exiting!\n");
  1679. return -EBUSY;
  1680. }
  1681. return 0;
  1682. }
  1683. static int ocfs2_commit_thread(void *arg)
  1684. {
  1685. int status;
  1686. struct ocfs2_super *osb = arg;
  1687. struct ocfs2_journal *journal = osb->journal;
  1688. /* we can trust j_num_trans here because _should_stop() is only set in
  1689. * shutdown and nobody other than ourselves should be able to start
  1690. * transactions. committing on shutdown might take a few iterations
  1691. * as final transactions put deleted inodes on the list */
  1692. while (!(kthread_should_stop() &&
  1693. atomic_read(&journal->j_num_trans) == 0)) {
  1694. wait_event_interruptible(osb->checkpoint_event,
  1695. atomic_read(&journal->j_num_trans)
  1696. || kthread_should_stop());
  1697. status = ocfs2_commit_cache(osb);
  1698. if (status < 0)
  1699. mlog_errno(status);
  1700. if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
  1701. mlog(ML_KTHREAD,
  1702. "commit_thread: %u transactions pending on "
  1703. "shutdown\n",
  1704. atomic_read(&journal->j_num_trans));
  1705. }
  1706. }
  1707. return 0;
  1708. }
  1709. /* Reads all the journal inodes without taking any cluster locks. Used
  1710. * for hard readonly access to determine whether any journal requires
  1711. * recovery. Also used to refresh the recovery generation numbers after
  1712. * a journal has been recovered by another node.
  1713. */
  1714. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
  1715. {
  1716. int ret = 0;
  1717. unsigned int slot;
  1718. struct buffer_head *di_bh = NULL;
  1719. struct ocfs2_dinode *di;
  1720. int journal_dirty = 0;
  1721. for(slot = 0; slot < osb->max_slots; slot++) {
  1722. ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
  1723. if (ret) {
  1724. mlog_errno(ret);
  1725. goto out;
  1726. }
  1727. di = (struct ocfs2_dinode *) di_bh->b_data;
  1728. osb->slot_recovery_generations[slot] =
  1729. ocfs2_get_recovery_generation(di);
  1730. if (le32_to_cpu(di->id1.journal1.ij_flags) &
  1731. OCFS2_JOURNAL_DIRTY_FL)
  1732. journal_dirty = 1;
  1733. brelse(di_bh);
  1734. di_bh = NULL;
  1735. }
  1736. out:
  1737. if (journal_dirty)
  1738. ret = -EROFS;
  1739. return ret;
  1740. }