journal.c 43 KB

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