journal.c 57 KB

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