journal.c 62 KB

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