journal.c 51 KB

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