journal.c 51 KB

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