journal.c 58 KB

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