journal.c 60 KB

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