journal.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  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. /* JBD might support this, but our journalling code doesn't yet. */
  210. if (journal_current_handle()) {
  211. mlog(ML_ERROR, "Recursive transaction attempted!\n");
  212. BUG();
  213. }
  214. down_read(&osb->journal->j_trans_barrier);
  215. handle = jbd2_journal_start(journal, max_buffs);
  216. if (IS_ERR(handle)) {
  217. up_read(&osb->journal->j_trans_barrier);
  218. mlog_errno(PTR_ERR(handle));
  219. if (is_journal_aborted(journal)) {
  220. ocfs2_abort(osb->sb, "Detected aborted journal");
  221. handle = ERR_PTR(-EROFS);
  222. }
  223. } else {
  224. if (!ocfs2_mount_local(osb))
  225. atomic_inc(&(osb->journal->j_num_trans));
  226. }
  227. return handle;
  228. }
  229. int ocfs2_commit_trans(struct ocfs2_super *osb,
  230. handle_t *handle)
  231. {
  232. int ret;
  233. struct ocfs2_journal *journal = osb->journal;
  234. BUG_ON(!handle);
  235. ret = jbd2_journal_stop(handle);
  236. if (ret < 0)
  237. mlog_errno(ret);
  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. #ifdef CONFIG_OCFS2_COMPAT_JBD
  355. int ocfs2_journal_dirty_data(handle_t *handle,
  356. struct buffer_head *bh)
  357. {
  358. int err = journal_dirty_data(handle, bh);
  359. if (err)
  360. mlog_errno(err);
  361. /* TODO: When we can handle it, abort the handle and go RO on
  362. * error here. */
  363. return err;
  364. }
  365. #endif
  366. #define OCFS2_DEFAULT_COMMIT_INTERVAL (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE)
  367. void ocfs2_set_journal_params(struct ocfs2_super *osb)
  368. {
  369. journal_t *journal = osb->journal->j_journal;
  370. unsigned long commit_interval = OCFS2_DEFAULT_COMMIT_INTERVAL;
  371. if (osb->osb_commit_interval)
  372. commit_interval = osb->osb_commit_interval;
  373. spin_lock(&journal->j_state_lock);
  374. journal->j_commit_interval = commit_interval;
  375. if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
  376. journal->j_flags |= JBD2_BARRIER;
  377. else
  378. journal->j_flags &= ~JBD2_BARRIER;
  379. spin_unlock(&journal->j_state_lock);
  380. }
  381. int ocfs2_journal_init(struct ocfs2_journal *journal, int *dirty)
  382. {
  383. int status = -1;
  384. struct inode *inode = NULL; /* the journal inode */
  385. journal_t *j_journal = NULL;
  386. struct ocfs2_dinode *di = NULL;
  387. struct buffer_head *bh = NULL;
  388. struct ocfs2_super *osb;
  389. int inode_lock = 0;
  390. mlog_entry_void();
  391. BUG_ON(!journal);
  392. osb = journal->j_osb;
  393. /* already have the inode for our journal */
  394. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  395. osb->slot_num);
  396. if (inode == NULL) {
  397. status = -EACCES;
  398. mlog_errno(status);
  399. goto done;
  400. }
  401. if (is_bad_inode(inode)) {
  402. mlog(ML_ERROR, "access error (bad inode)\n");
  403. iput(inode);
  404. inode = NULL;
  405. status = -EACCES;
  406. goto done;
  407. }
  408. SET_INODE_JOURNAL(inode);
  409. OCFS2_I(inode)->ip_open_count++;
  410. /* Skip recovery waits here - journal inode metadata never
  411. * changes in a live cluster so it can be considered an
  412. * exception to the rule. */
  413. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  414. if (status < 0) {
  415. if (status != -ERESTARTSYS)
  416. mlog(ML_ERROR, "Could not get lock on journal!\n");
  417. goto done;
  418. }
  419. inode_lock = 1;
  420. di = (struct ocfs2_dinode *)bh->b_data;
  421. if (inode->i_size < OCFS2_MIN_JOURNAL_SIZE) {
  422. mlog(ML_ERROR, "Journal file size (%lld) is too small!\n",
  423. inode->i_size);
  424. status = -EINVAL;
  425. goto done;
  426. }
  427. mlog(0, "inode->i_size = %lld\n", inode->i_size);
  428. mlog(0, "inode->i_blocks = %llu\n",
  429. (unsigned long long)inode->i_blocks);
  430. mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
  431. /* call the kernels journal init function now */
  432. j_journal = jbd2_journal_init_inode(inode);
  433. if (j_journal == NULL) {
  434. mlog(ML_ERROR, "Linux journal layer error\n");
  435. status = -EINVAL;
  436. goto done;
  437. }
  438. mlog(0, "Returned from jbd2_journal_init_inode\n");
  439. mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
  440. *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
  441. OCFS2_JOURNAL_DIRTY_FL);
  442. journal->j_journal = j_journal;
  443. journal->j_inode = inode;
  444. journal->j_bh = bh;
  445. ocfs2_set_journal_params(osb);
  446. journal->j_state = OCFS2_JOURNAL_LOADED;
  447. status = 0;
  448. done:
  449. if (status < 0) {
  450. if (inode_lock)
  451. ocfs2_inode_unlock(inode, 1);
  452. brelse(bh);
  453. if (inode) {
  454. OCFS2_I(inode)->ip_open_count--;
  455. iput(inode);
  456. }
  457. }
  458. mlog_exit(status);
  459. return status;
  460. }
  461. static void ocfs2_bump_recovery_generation(struct ocfs2_dinode *di)
  462. {
  463. le32_add_cpu(&(di->id1.journal1.ij_recovery_generation), 1);
  464. }
  465. static u32 ocfs2_get_recovery_generation(struct ocfs2_dinode *di)
  466. {
  467. return le32_to_cpu(di->id1.journal1.ij_recovery_generation);
  468. }
  469. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  470. int dirty, int replayed)
  471. {
  472. int status;
  473. unsigned int flags;
  474. struct ocfs2_journal *journal = osb->journal;
  475. struct buffer_head *bh = journal->j_bh;
  476. struct ocfs2_dinode *fe;
  477. mlog_entry_void();
  478. fe = (struct ocfs2_dinode *)bh->b_data;
  479. if (!OCFS2_IS_VALID_DINODE(fe)) {
  480. /* This is called from startup/shutdown which will
  481. * handle the errors in a specific manner, so no need
  482. * to call ocfs2_error() here. */
  483. mlog(ML_ERROR, "Journal dinode %llu has invalid "
  484. "signature: %.*s",
  485. (unsigned long long)le64_to_cpu(fe->i_blkno), 7,
  486. fe->i_signature);
  487. status = -EIO;
  488. goto out;
  489. }
  490. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  491. if (dirty)
  492. flags |= OCFS2_JOURNAL_DIRTY_FL;
  493. else
  494. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  495. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  496. if (replayed)
  497. ocfs2_bump_recovery_generation(fe);
  498. status = ocfs2_write_block(osb, bh, journal->j_inode);
  499. if (status < 0)
  500. mlog_errno(status);
  501. out:
  502. mlog_exit(status);
  503. return status;
  504. }
  505. /*
  506. * If the journal has been kmalloc'd it needs to be freed after this
  507. * call.
  508. */
  509. void ocfs2_journal_shutdown(struct ocfs2_super *osb)
  510. {
  511. struct ocfs2_journal *journal = NULL;
  512. int status = 0;
  513. struct inode *inode = NULL;
  514. int num_running_trans = 0;
  515. mlog_entry_void();
  516. BUG_ON(!osb);
  517. journal = osb->journal;
  518. if (!journal)
  519. goto done;
  520. inode = journal->j_inode;
  521. if (journal->j_state != OCFS2_JOURNAL_LOADED)
  522. goto done;
  523. /* need to inc inode use count - jbd2_journal_destroy will iput. */
  524. if (!igrab(inode))
  525. BUG();
  526. num_running_trans = atomic_read(&(osb->journal->j_num_trans));
  527. if (num_running_trans > 0)
  528. mlog(0, "Shutting down journal: must wait on %d "
  529. "running transactions!\n",
  530. num_running_trans);
  531. /* Do a commit_cache here. It will flush our journal, *and*
  532. * release any locks that are still held.
  533. * set the SHUTDOWN flag and release the trans lock.
  534. * the commit thread will take the trans lock for us below. */
  535. journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
  536. /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
  537. * drop the trans_lock (which we want to hold until we
  538. * completely destroy the journal. */
  539. if (osb->commit_task) {
  540. /* Wait for the commit thread */
  541. mlog(0, "Waiting for ocfs2commit to exit....\n");
  542. kthread_stop(osb->commit_task);
  543. osb->commit_task = NULL;
  544. }
  545. BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
  546. if (ocfs2_mount_local(osb)) {
  547. jbd2_journal_lock_updates(journal->j_journal);
  548. status = jbd2_journal_flush(journal->j_journal);
  549. jbd2_journal_unlock_updates(journal->j_journal);
  550. if (status < 0)
  551. mlog_errno(status);
  552. }
  553. if (status == 0) {
  554. /*
  555. * Do not toggle if flush was unsuccessful otherwise
  556. * will leave dirty metadata in a "clean" journal
  557. */
  558. status = ocfs2_journal_toggle_dirty(osb, 0, 0);
  559. if (status < 0)
  560. mlog_errno(status);
  561. }
  562. /* Shutdown the kernel journal system */
  563. jbd2_journal_destroy(journal->j_journal);
  564. OCFS2_I(inode)->ip_open_count--;
  565. /* unlock our journal */
  566. ocfs2_inode_unlock(inode, 1);
  567. brelse(journal->j_bh);
  568. journal->j_bh = NULL;
  569. journal->j_state = OCFS2_JOURNAL_FREE;
  570. // up_write(&journal->j_trans_barrier);
  571. done:
  572. if (inode)
  573. iput(inode);
  574. mlog_exit_void();
  575. }
  576. static void ocfs2_clear_journal_error(struct super_block *sb,
  577. journal_t *journal,
  578. int slot)
  579. {
  580. int olderr;
  581. olderr = jbd2_journal_errno(journal);
  582. if (olderr) {
  583. mlog(ML_ERROR, "File system error %d recorded in "
  584. "journal %u.\n", olderr, slot);
  585. mlog(ML_ERROR, "File system on device %s needs checking.\n",
  586. sb->s_id);
  587. jbd2_journal_ack_err(journal);
  588. jbd2_journal_clear_err(journal);
  589. }
  590. }
  591. int ocfs2_journal_load(struct ocfs2_journal *journal, int local, int replayed)
  592. {
  593. int status = 0;
  594. struct ocfs2_super *osb;
  595. mlog_entry_void();
  596. BUG_ON(!journal);
  597. osb = journal->j_osb;
  598. status = jbd2_journal_load(journal->j_journal);
  599. if (status < 0) {
  600. mlog(ML_ERROR, "Failed to load journal!\n");
  601. goto done;
  602. }
  603. ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
  604. status = ocfs2_journal_toggle_dirty(osb, 1, replayed);
  605. if (status < 0) {
  606. mlog_errno(status);
  607. goto done;
  608. }
  609. /* Launch the commit thread */
  610. if (!local) {
  611. osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
  612. "ocfs2cmt");
  613. if (IS_ERR(osb->commit_task)) {
  614. status = PTR_ERR(osb->commit_task);
  615. osb->commit_task = NULL;
  616. mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
  617. "error=%d", status);
  618. goto done;
  619. }
  620. } else
  621. osb->commit_task = NULL;
  622. done:
  623. mlog_exit(status);
  624. return status;
  625. }
  626. /* 'full' flag tells us whether we clear out all blocks or if we just
  627. * mark the journal clean */
  628. int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
  629. {
  630. int status;
  631. mlog_entry_void();
  632. BUG_ON(!journal);
  633. status = jbd2_journal_wipe(journal->j_journal, full);
  634. if (status < 0) {
  635. mlog_errno(status);
  636. goto bail;
  637. }
  638. status = ocfs2_journal_toggle_dirty(journal->j_osb, 0, 0);
  639. if (status < 0)
  640. mlog_errno(status);
  641. bail:
  642. mlog_exit(status);
  643. return status;
  644. }
  645. static int ocfs2_recovery_completed(struct ocfs2_super *osb)
  646. {
  647. int empty;
  648. struct ocfs2_recovery_map *rm = osb->recovery_map;
  649. spin_lock(&osb->osb_lock);
  650. empty = (rm->rm_used == 0);
  651. spin_unlock(&osb->osb_lock);
  652. return empty;
  653. }
  654. void ocfs2_wait_for_recovery(struct ocfs2_super *osb)
  655. {
  656. wait_event(osb->recovery_event, ocfs2_recovery_completed(osb));
  657. }
  658. /*
  659. * JBD Might read a cached version of another nodes journal file. We
  660. * don't want this as this file changes often and we get no
  661. * notification on those changes. The only way to be sure that we've
  662. * got the most up to date version of those blocks then is to force
  663. * read them off disk. Just searching through the buffer cache won't
  664. * work as there may be pages backing this file which are still marked
  665. * up to date. We know things can't change on this file underneath us
  666. * as we have the lock by now :)
  667. */
  668. static int ocfs2_force_read_journal(struct inode *inode)
  669. {
  670. int status = 0;
  671. int i;
  672. u64 v_blkno, p_blkno, p_blocks, num_blocks;
  673. #define CONCURRENT_JOURNAL_FILL 32ULL
  674. struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
  675. mlog_entry_void();
  676. memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
  677. num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
  678. v_blkno = 0;
  679. while (v_blkno < num_blocks) {
  680. status = ocfs2_extent_map_get_blocks(inode, v_blkno,
  681. &p_blkno, &p_blocks, NULL);
  682. if (status < 0) {
  683. mlog_errno(status);
  684. goto bail;
  685. }
  686. if (p_blocks > CONCURRENT_JOURNAL_FILL)
  687. p_blocks = CONCURRENT_JOURNAL_FILL;
  688. /* We are reading journal data which should not
  689. * be put in the uptodate cache */
  690. status = ocfs2_read_blocks_sync(OCFS2_SB(inode->i_sb),
  691. p_blkno, p_blocks, bhs);
  692. if (status < 0) {
  693. mlog_errno(status);
  694. goto bail;
  695. }
  696. for(i = 0; i < p_blocks; i++) {
  697. brelse(bhs[i]);
  698. bhs[i] = NULL;
  699. }
  700. v_blkno += p_blocks;
  701. }
  702. bail:
  703. for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
  704. brelse(bhs[i]);
  705. mlog_exit(status);
  706. return status;
  707. }
  708. struct ocfs2_la_recovery_item {
  709. struct list_head lri_list;
  710. int lri_slot;
  711. struct ocfs2_dinode *lri_la_dinode;
  712. struct ocfs2_dinode *lri_tl_dinode;
  713. };
  714. /* Does the second half of the recovery process. By this point, the
  715. * node is marked clean and can actually be considered recovered,
  716. * hence it's no longer in the recovery map, but there's still some
  717. * cleanup we can do which shouldn't happen within the recovery thread
  718. * as locking in that context becomes very difficult if we are to take
  719. * recovering nodes into account.
  720. *
  721. * NOTE: This function can and will sleep on recovery of other nodes
  722. * during cluster locking, just like any other ocfs2 process.
  723. */
  724. void ocfs2_complete_recovery(struct work_struct *work)
  725. {
  726. int ret;
  727. struct ocfs2_journal *journal =
  728. container_of(work, struct ocfs2_journal, j_recovery_work);
  729. struct ocfs2_super *osb = journal->j_osb;
  730. struct ocfs2_dinode *la_dinode, *tl_dinode;
  731. struct ocfs2_la_recovery_item *item, *n;
  732. LIST_HEAD(tmp_la_list);
  733. mlog_entry_void();
  734. mlog(0, "completing recovery from keventd\n");
  735. spin_lock(&journal->j_lock);
  736. list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
  737. spin_unlock(&journal->j_lock);
  738. list_for_each_entry_safe(item, n, &tmp_la_list, lri_list) {
  739. list_del_init(&item->lri_list);
  740. mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
  741. la_dinode = item->lri_la_dinode;
  742. if (la_dinode) {
  743. mlog(0, "Clean up local alloc %llu\n",
  744. (unsigned long long)le64_to_cpu(la_dinode->i_blkno));
  745. ret = ocfs2_complete_local_alloc_recovery(osb,
  746. la_dinode);
  747. if (ret < 0)
  748. mlog_errno(ret);
  749. kfree(la_dinode);
  750. }
  751. tl_dinode = item->lri_tl_dinode;
  752. if (tl_dinode) {
  753. mlog(0, "Clean up truncate log %llu\n",
  754. (unsigned long long)le64_to_cpu(tl_dinode->i_blkno));
  755. ret = ocfs2_complete_truncate_log_recovery(osb,
  756. tl_dinode);
  757. if (ret < 0)
  758. mlog_errno(ret);
  759. kfree(tl_dinode);
  760. }
  761. ret = ocfs2_recover_orphans(osb, item->lri_slot);
  762. if (ret < 0)
  763. mlog_errno(ret);
  764. kfree(item);
  765. }
  766. mlog(0, "Recovery completion\n");
  767. mlog_exit_void();
  768. }
  769. /* NOTE: This function always eats your references to la_dinode and
  770. * tl_dinode, either manually on error, or by passing them to
  771. * ocfs2_complete_recovery */
  772. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  773. int slot_num,
  774. struct ocfs2_dinode *la_dinode,
  775. struct ocfs2_dinode *tl_dinode)
  776. {
  777. struct ocfs2_la_recovery_item *item;
  778. item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
  779. if (!item) {
  780. /* Though we wish to avoid it, we are in fact safe in
  781. * skipping local alloc cleanup as fsck.ocfs2 is more
  782. * than capable of reclaiming unused space. */
  783. if (la_dinode)
  784. kfree(la_dinode);
  785. if (tl_dinode)
  786. kfree(tl_dinode);
  787. mlog_errno(-ENOMEM);
  788. return;
  789. }
  790. INIT_LIST_HEAD(&item->lri_list);
  791. item->lri_la_dinode = la_dinode;
  792. item->lri_slot = slot_num;
  793. item->lri_tl_dinode = tl_dinode;
  794. spin_lock(&journal->j_lock);
  795. list_add_tail(&item->lri_list, &journal->j_la_cleanups);
  796. queue_work(ocfs2_wq, &journal->j_recovery_work);
  797. spin_unlock(&journal->j_lock);
  798. }
  799. /* Called by the mount code to queue recovery the last part of
  800. * recovery for it's own slot. */
  801. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
  802. {
  803. struct ocfs2_journal *journal = osb->journal;
  804. if (osb->dirty) {
  805. /* No need to queue up our truncate_log as regular
  806. * cleanup will catch that. */
  807. ocfs2_queue_recovery_completion(journal,
  808. osb->slot_num,
  809. osb->local_alloc_copy,
  810. NULL);
  811. ocfs2_schedule_truncate_log_flush(osb, 0);
  812. osb->local_alloc_copy = NULL;
  813. osb->dirty = 0;
  814. }
  815. }
  816. static int __ocfs2_recovery_thread(void *arg)
  817. {
  818. int status, node_num;
  819. struct ocfs2_super *osb = arg;
  820. struct ocfs2_recovery_map *rm = osb->recovery_map;
  821. mlog_entry_void();
  822. status = ocfs2_wait_on_mount(osb);
  823. if (status < 0) {
  824. goto bail;
  825. }
  826. restart:
  827. status = ocfs2_super_lock(osb, 1);
  828. if (status < 0) {
  829. mlog_errno(status);
  830. goto bail;
  831. }
  832. spin_lock(&osb->osb_lock);
  833. while (rm->rm_used) {
  834. /* It's always safe to remove entry zero, as we won't
  835. * clear it until ocfs2_recover_node() has succeeded. */
  836. node_num = rm->rm_entries[0];
  837. spin_unlock(&osb->osb_lock);
  838. status = ocfs2_recover_node(osb, node_num);
  839. if (!status) {
  840. ocfs2_recovery_map_clear(osb, node_num);
  841. } else {
  842. mlog(ML_ERROR,
  843. "Error %d recovering node %d on device (%u,%u)!\n",
  844. status, node_num,
  845. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  846. mlog(ML_ERROR, "Volume requires unmount.\n");
  847. }
  848. spin_lock(&osb->osb_lock);
  849. }
  850. spin_unlock(&osb->osb_lock);
  851. mlog(0, "All nodes recovered\n");
  852. /* Refresh all journal recovery generations from disk */
  853. status = ocfs2_check_journals_nolocks(osb);
  854. status = (status == -EROFS) ? 0 : status;
  855. if (status < 0)
  856. mlog_errno(status);
  857. ocfs2_super_unlock(osb, 1);
  858. /* We always run recovery on our own orphan dir - the dead
  859. * node(s) may have disallowd a previos inode delete. Re-processing
  860. * is therefore required. */
  861. ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
  862. NULL);
  863. bail:
  864. mutex_lock(&osb->recovery_lock);
  865. if (!status && !ocfs2_recovery_completed(osb)) {
  866. mutex_unlock(&osb->recovery_lock);
  867. goto restart;
  868. }
  869. osb->recovery_thread_task = NULL;
  870. mb(); /* sync with ocfs2_recovery_thread_running */
  871. wake_up(&osb->recovery_event);
  872. mutex_unlock(&osb->recovery_lock);
  873. mlog_exit(status);
  874. /* no one is callint kthread_stop() for us so the kthread() api
  875. * requires that we call do_exit(). And it isn't exported, but
  876. * complete_and_exit() seems to be a minimal wrapper around it. */
  877. complete_and_exit(NULL, status);
  878. return status;
  879. }
  880. void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
  881. {
  882. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  883. node_num, osb->node_num);
  884. mutex_lock(&osb->recovery_lock);
  885. if (osb->disable_recovery)
  886. goto out;
  887. /* People waiting on recovery will wait on
  888. * the recovery map to empty. */
  889. if (ocfs2_recovery_map_set(osb, node_num))
  890. mlog(0, "node %d already in recovery map.\n", node_num);
  891. mlog(0, "starting recovery thread...\n");
  892. if (osb->recovery_thread_task)
  893. goto out;
  894. osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
  895. "ocfs2rec");
  896. if (IS_ERR(osb->recovery_thread_task)) {
  897. mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
  898. osb->recovery_thread_task = NULL;
  899. }
  900. out:
  901. mutex_unlock(&osb->recovery_lock);
  902. wake_up(&osb->recovery_event);
  903. mlog_exit_void();
  904. }
  905. static int ocfs2_read_journal_inode(struct ocfs2_super *osb,
  906. int slot_num,
  907. struct buffer_head **bh,
  908. struct inode **ret_inode)
  909. {
  910. int status = -EACCES;
  911. struct inode *inode = NULL;
  912. BUG_ON(slot_num >= osb->max_slots);
  913. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  914. slot_num);
  915. if (!inode || is_bad_inode(inode)) {
  916. mlog_errno(status);
  917. goto bail;
  918. }
  919. SET_INODE_JOURNAL(inode);
  920. status = ocfs2_read_blocks(inode, OCFS2_I(inode)->ip_blkno, 1, bh,
  921. OCFS2_BH_IGNORE_CACHE);
  922. if (status < 0) {
  923. mlog_errno(status);
  924. goto bail;
  925. }
  926. status = 0;
  927. bail:
  928. if (inode) {
  929. if (status || !ret_inode)
  930. iput(inode);
  931. else
  932. *ret_inode = inode;
  933. }
  934. return status;
  935. }
  936. /* Does the actual journal replay and marks the journal inode as
  937. * clean. Will only replay if the journal inode is marked dirty. */
  938. static int ocfs2_replay_journal(struct ocfs2_super *osb,
  939. int node_num,
  940. int slot_num)
  941. {
  942. int status;
  943. int got_lock = 0;
  944. unsigned int flags;
  945. struct inode *inode = NULL;
  946. struct ocfs2_dinode *fe;
  947. journal_t *journal = NULL;
  948. struct buffer_head *bh = NULL;
  949. u32 slot_reco_gen;
  950. status = ocfs2_read_journal_inode(osb, slot_num, &bh, &inode);
  951. if (status) {
  952. mlog_errno(status);
  953. goto done;
  954. }
  955. fe = (struct ocfs2_dinode *)bh->b_data;
  956. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  957. brelse(bh);
  958. bh = NULL;
  959. /*
  960. * As the fs recovery is asynchronous, there is a small chance that
  961. * another node mounted (and recovered) the slot before the recovery
  962. * thread could get the lock. To handle that, we dirty read the journal
  963. * inode for that slot to get the recovery generation. If it is
  964. * different than what we expected, the slot has been recovered.
  965. * If not, it needs recovery.
  966. */
  967. if (osb->slot_recovery_generations[slot_num] != slot_reco_gen) {
  968. mlog(0, "Slot %u already recovered (old/new=%u/%u)\n", slot_num,
  969. osb->slot_recovery_generations[slot_num], slot_reco_gen);
  970. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  971. status = -EBUSY;
  972. goto done;
  973. }
  974. /* Continue with recovery as the journal has not yet been recovered */
  975. status = ocfs2_inode_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  976. if (status < 0) {
  977. mlog(0, "status returned from ocfs2_inode_lock=%d\n", status);
  978. if (status != -ERESTARTSYS)
  979. mlog(ML_ERROR, "Could not lock journal!\n");
  980. goto done;
  981. }
  982. got_lock = 1;
  983. fe = (struct ocfs2_dinode *) bh->b_data;
  984. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  985. slot_reco_gen = ocfs2_get_recovery_generation(fe);
  986. if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
  987. mlog(0, "No recovery required for node %d\n", node_num);
  988. /* Refresh recovery generation for the slot */
  989. osb->slot_recovery_generations[slot_num] = slot_reco_gen;
  990. goto done;
  991. }
  992. mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
  993. node_num, slot_num,
  994. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  995. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  996. status = ocfs2_force_read_journal(inode);
  997. if (status < 0) {
  998. mlog_errno(status);
  999. goto done;
  1000. }
  1001. mlog(0, "calling journal_init_inode\n");
  1002. journal = jbd2_journal_init_inode(inode);
  1003. if (journal == NULL) {
  1004. mlog(ML_ERROR, "Linux journal layer error\n");
  1005. status = -EIO;
  1006. goto done;
  1007. }
  1008. status = jbd2_journal_load(journal);
  1009. if (status < 0) {
  1010. mlog_errno(status);
  1011. if (!igrab(inode))
  1012. BUG();
  1013. jbd2_journal_destroy(journal);
  1014. goto done;
  1015. }
  1016. ocfs2_clear_journal_error(osb->sb, journal, slot_num);
  1017. /* wipe the journal */
  1018. mlog(0, "flushing the journal.\n");
  1019. jbd2_journal_lock_updates(journal);
  1020. status = jbd2_journal_flush(journal);
  1021. jbd2_journal_unlock_updates(journal);
  1022. if (status < 0)
  1023. mlog_errno(status);
  1024. /* This will mark the node clean */
  1025. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  1026. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  1027. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  1028. /* Increment recovery generation to indicate successful recovery */
  1029. ocfs2_bump_recovery_generation(fe);
  1030. osb->slot_recovery_generations[slot_num] =
  1031. ocfs2_get_recovery_generation(fe);
  1032. status = ocfs2_write_block(osb, bh, inode);
  1033. if (status < 0)
  1034. mlog_errno(status);
  1035. if (!igrab(inode))
  1036. BUG();
  1037. jbd2_journal_destroy(journal);
  1038. done:
  1039. /* drop the lock on this nodes journal */
  1040. if (got_lock)
  1041. ocfs2_inode_unlock(inode, 1);
  1042. if (inode)
  1043. iput(inode);
  1044. brelse(bh);
  1045. mlog_exit(status);
  1046. return status;
  1047. }
  1048. /*
  1049. * Do the most important parts of node recovery:
  1050. * - Replay it's journal
  1051. * - Stamp a clean local allocator file
  1052. * - Stamp a clean truncate log
  1053. * - Mark the node clean
  1054. *
  1055. * If this function completes without error, a node in OCFS2 can be
  1056. * said to have been safely recovered. As a result, failure during the
  1057. * second part of a nodes recovery process (local alloc recovery) is
  1058. * far less concerning.
  1059. */
  1060. static int ocfs2_recover_node(struct ocfs2_super *osb,
  1061. int node_num)
  1062. {
  1063. int status = 0;
  1064. int slot_num;
  1065. struct ocfs2_dinode *la_copy = NULL;
  1066. struct ocfs2_dinode *tl_copy = NULL;
  1067. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  1068. node_num, osb->node_num);
  1069. mlog(0, "checking node %d\n", node_num);
  1070. /* Should not ever be called to recover ourselves -- in that
  1071. * case we should've called ocfs2_journal_load instead. */
  1072. BUG_ON(osb->node_num == node_num);
  1073. slot_num = ocfs2_node_num_to_slot(osb, node_num);
  1074. if (slot_num == -ENOENT) {
  1075. status = 0;
  1076. mlog(0, "no slot for this node, so no recovery required.\n");
  1077. goto done;
  1078. }
  1079. mlog(0, "node %d was using slot %d\n", node_num, slot_num);
  1080. status = ocfs2_replay_journal(osb, node_num, slot_num);
  1081. if (status < 0) {
  1082. if (status == -EBUSY) {
  1083. mlog(0, "Skipping recovery for slot %u (node %u) "
  1084. "as another node has recovered it\n", slot_num,
  1085. node_num);
  1086. status = 0;
  1087. goto done;
  1088. }
  1089. mlog_errno(status);
  1090. goto done;
  1091. }
  1092. /* Stamp a clean local alloc file AFTER recovering the journal... */
  1093. status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
  1094. if (status < 0) {
  1095. mlog_errno(status);
  1096. goto done;
  1097. }
  1098. /* An error from begin_truncate_log_recovery is not
  1099. * serious enough to warrant halting the rest of
  1100. * recovery. */
  1101. status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
  1102. if (status < 0)
  1103. mlog_errno(status);
  1104. /* Likewise, this would be a strange but ultimately not so
  1105. * harmful place to get an error... */
  1106. status = ocfs2_clear_slot(osb, slot_num);
  1107. if (status < 0)
  1108. mlog_errno(status);
  1109. /* This will kfree the memory pointed to by la_copy and tl_copy */
  1110. ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
  1111. tl_copy);
  1112. status = 0;
  1113. done:
  1114. mlog_exit(status);
  1115. return status;
  1116. }
  1117. /* Test node liveness by trylocking his journal. If we get the lock,
  1118. * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
  1119. * still alive (we couldn't get the lock) and < 0 on error. */
  1120. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  1121. int slot_num)
  1122. {
  1123. int status, flags;
  1124. struct inode *inode = NULL;
  1125. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1126. slot_num);
  1127. if (inode == NULL) {
  1128. mlog(ML_ERROR, "access error\n");
  1129. status = -EACCES;
  1130. goto bail;
  1131. }
  1132. if (is_bad_inode(inode)) {
  1133. mlog(ML_ERROR, "access error (bad inode)\n");
  1134. iput(inode);
  1135. inode = NULL;
  1136. status = -EACCES;
  1137. goto bail;
  1138. }
  1139. SET_INODE_JOURNAL(inode);
  1140. flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
  1141. status = ocfs2_inode_lock_full(inode, NULL, 1, flags);
  1142. if (status < 0) {
  1143. if (status != -EAGAIN)
  1144. mlog_errno(status);
  1145. goto bail;
  1146. }
  1147. ocfs2_inode_unlock(inode, 1);
  1148. bail:
  1149. if (inode)
  1150. iput(inode);
  1151. return status;
  1152. }
  1153. /* Call this underneath ocfs2_super_lock. It also assumes that the
  1154. * slot info struct has been updated from disk. */
  1155. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
  1156. {
  1157. unsigned int node_num;
  1158. int status, i;
  1159. u32 gen;
  1160. struct buffer_head *bh = NULL;
  1161. struct ocfs2_dinode *di;
  1162. /* This is called with the super block cluster lock, so we
  1163. * know that the slot map can't change underneath us. */
  1164. for (i = 0; i < osb->max_slots; i++) {
  1165. /* Read journal inode to get the recovery generation */
  1166. status = ocfs2_read_journal_inode(osb, i, &bh, NULL);
  1167. if (status) {
  1168. mlog_errno(status);
  1169. goto bail;
  1170. }
  1171. di = (struct ocfs2_dinode *)bh->b_data;
  1172. gen = ocfs2_get_recovery_generation(di);
  1173. brelse(bh);
  1174. bh = NULL;
  1175. spin_lock(&osb->osb_lock);
  1176. osb->slot_recovery_generations[i] = gen;
  1177. mlog(0, "Slot %u recovery generation is %u\n", i,
  1178. osb->slot_recovery_generations[i]);
  1179. if (i == osb->slot_num) {
  1180. spin_unlock(&osb->osb_lock);
  1181. continue;
  1182. }
  1183. status = ocfs2_slot_to_node_num_locked(osb, i, &node_num);
  1184. if (status == -ENOENT) {
  1185. spin_unlock(&osb->osb_lock);
  1186. continue;
  1187. }
  1188. if (__ocfs2_recovery_map_test(osb, node_num)) {
  1189. spin_unlock(&osb->osb_lock);
  1190. continue;
  1191. }
  1192. spin_unlock(&osb->osb_lock);
  1193. /* Ok, we have a slot occupied by another node which
  1194. * is not in the recovery map. We trylock his journal
  1195. * file here to test if he's alive. */
  1196. status = ocfs2_trylock_journal(osb, i);
  1197. if (!status) {
  1198. /* Since we're called from mount, we know that
  1199. * the recovery thread can't race us on
  1200. * setting / checking the recovery bits. */
  1201. ocfs2_recovery_thread(osb, node_num);
  1202. } else if ((status < 0) && (status != -EAGAIN)) {
  1203. mlog_errno(status);
  1204. goto bail;
  1205. }
  1206. }
  1207. status = 0;
  1208. bail:
  1209. mlog_exit(status);
  1210. return status;
  1211. }
  1212. struct ocfs2_orphan_filldir_priv {
  1213. struct inode *head;
  1214. struct ocfs2_super *osb;
  1215. };
  1216. static int ocfs2_orphan_filldir(void *priv, const char *name, int name_len,
  1217. loff_t pos, u64 ino, unsigned type)
  1218. {
  1219. struct ocfs2_orphan_filldir_priv *p = priv;
  1220. struct inode *iter;
  1221. if (name_len == 1 && !strncmp(".", name, 1))
  1222. return 0;
  1223. if (name_len == 2 && !strncmp("..", name, 2))
  1224. return 0;
  1225. /* Skip bad inodes so that recovery can continue */
  1226. iter = ocfs2_iget(p->osb, ino,
  1227. OCFS2_FI_FLAG_ORPHAN_RECOVERY, 0);
  1228. if (IS_ERR(iter))
  1229. return 0;
  1230. mlog(0, "queue orphan %llu\n",
  1231. (unsigned long long)OCFS2_I(iter)->ip_blkno);
  1232. /* No locking is required for the next_orphan queue as there
  1233. * is only ever a single process doing orphan recovery. */
  1234. OCFS2_I(iter)->ip_next_orphan = p->head;
  1235. p->head = iter;
  1236. return 0;
  1237. }
  1238. static int ocfs2_queue_orphans(struct ocfs2_super *osb,
  1239. int slot,
  1240. struct inode **head)
  1241. {
  1242. int status;
  1243. struct inode *orphan_dir_inode = NULL;
  1244. struct ocfs2_orphan_filldir_priv priv;
  1245. loff_t pos = 0;
  1246. priv.osb = osb;
  1247. priv.head = *head;
  1248. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1249. ORPHAN_DIR_SYSTEM_INODE,
  1250. slot);
  1251. if (!orphan_dir_inode) {
  1252. status = -ENOENT;
  1253. mlog_errno(status);
  1254. return status;
  1255. }
  1256. mutex_lock(&orphan_dir_inode->i_mutex);
  1257. status = ocfs2_inode_lock(orphan_dir_inode, NULL, 0);
  1258. if (status < 0) {
  1259. mlog_errno(status);
  1260. goto out;
  1261. }
  1262. status = ocfs2_dir_foreach(orphan_dir_inode, &pos, &priv,
  1263. ocfs2_orphan_filldir);
  1264. if (status) {
  1265. mlog_errno(status);
  1266. goto out_cluster;
  1267. }
  1268. *head = priv.head;
  1269. out_cluster:
  1270. ocfs2_inode_unlock(orphan_dir_inode, 0);
  1271. out:
  1272. mutex_unlock(&orphan_dir_inode->i_mutex);
  1273. iput(orphan_dir_inode);
  1274. return status;
  1275. }
  1276. static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
  1277. int slot)
  1278. {
  1279. int ret;
  1280. spin_lock(&osb->osb_lock);
  1281. ret = !osb->osb_orphan_wipes[slot];
  1282. spin_unlock(&osb->osb_lock);
  1283. return ret;
  1284. }
  1285. static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
  1286. int slot)
  1287. {
  1288. spin_lock(&osb->osb_lock);
  1289. /* Mark ourselves such that new processes in delete_inode()
  1290. * know to quit early. */
  1291. ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1292. while (osb->osb_orphan_wipes[slot]) {
  1293. /* If any processes are already in the middle of an
  1294. * orphan wipe on this dir, then we need to wait for
  1295. * them. */
  1296. spin_unlock(&osb->osb_lock);
  1297. wait_event_interruptible(osb->osb_wipe_event,
  1298. ocfs2_orphan_recovery_can_continue(osb, slot));
  1299. spin_lock(&osb->osb_lock);
  1300. }
  1301. spin_unlock(&osb->osb_lock);
  1302. }
  1303. static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
  1304. int slot)
  1305. {
  1306. ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1307. }
  1308. /*
  1309. * Orphan recovery. Each mounted node has it's own orphan dir which we
  1310. * must run during recovery. Our strategy here is to build a list of
  1311. * the inodes in the orphan dir and iget/iput them. The VFS does
  1312. * (most) of the rest of the work.
  1313. *
  1314. * Orphan recovery can happen at any time, not just mount so we have a
  1315. * couple of extra considerations.
  1316. *
  1317. * - We grab as many inodes as we can under the orphan dir lock -
  1318. * doing iget() outside the orphan dir risks getting a reference on
  1319. * an invalid inode.
  1320. * - We must be sure not to deadlock with other processes on the
  1321. * system wanting to run delete_inode(). This can happen when they go
  1322. * to lock the orphan dir and the orphan recovery process attempts to
  1323. * iget() inside the orphan dir lock. This can be avoided by
  1324. * advertising our state to ocfs2_delete_inode().
  1325. */
  1326. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  1327. int slot)
  1328. {
  1329. int ret = 0;
  1330. struct inode *inode = NULL;
  1331. struct inode *iter;
  1332. struct ocfs2_inode_info *oi;
  1333. mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
  1334. ocfs2_mark_recovering_orphan_dir(osb, slot);
  1335. ret = ocfs2_queue_orphans(osb, slot, &inode);
  1336. ocfs2_clear_recovering_orphan_dir(osb, slot);
  1337. /* Error here should be noted, but we want to continue with as
  1338. * many queued inodes as we've got. */
  1339. if (ret)
  1340. mlog_errno(ret);
  1341. while (inode) {
  1342. oi = OCFS2_I(inode);
  1343. mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
  1344. iter = oi->ip_next_orphan;
  1345. spin_lock(&oi->ip_lock);
  1346. /* The remote delete code may have set these on the
  1347. * assumption that the other node would wipe them
  1348. * successfully. If they are still in the node's
  1349. * orphan dir, we need to reset that state. */
  1350. oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
  1351. /* Set the proper information to get us going into
  1352. * ocfs2_delete_inode. */
  1353. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  1354. spin_unlock(&oi->ip_lock);
  1355. iput(inode);
  1356. inode = iter;
  1357. }
  1358. return ret;
  1359. }
  1360. static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
  1361. {
  1362. /* This check is good because ocfs2 will wait on our recovery
  1363. * thread before changing it to something other than MOUNTED
  1364. * or DISABLED. */
  1365. wait_event(osb->osb_mount_event,
  1366. atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
  1367. atomic_read(&osb->vol_state) == VOLUME_DISABLED);
  1368. /* If there's an error on mount, then we may never get to the
  1369. * MOUNTED flag, but this is set right before
  1370. * dismount_volume() so we can trust it. */
  1371. if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
  1372. mlog(0, "mount error, exiting!\n");
  1373. return -EBUSY;
  1374. }
  1375. return 0;
  1376. }
  1377. static int ocfs2_commit_thread(void *arg)
  1378. {
  1379. int status;
  1380. struct ocfs2_super *osb = arg;
  1381. struct ocfs2_journal *journal = osb->journal;
  1382. /* we can trust j_num_trans here because _should_stop() is only set in
  1383. * shutdown and nobody other than ourselves should be able to start
  1384. * transactions. committing on shutdown might take a few iterations
  1385. * as final transactions put deleted inodes on the list */
  1386. while (!(kthread_should_stop() &&
  1387. atomic_read(&journal->j_num_trans) == 0)) {
  1388. wait_event_interruptible(osb->checkpoint_event,
  1389. atomic_read(&journal->j_num_trans)
  1390. || kthread_should_stop());
  1391. status = ocfs2_commit_cache(osb);
  1392. if (status < 0)
  1393. mlog_errno(status);
  1394. if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
  1395. mlog(ML_KTHREAD,
  1396. "commit_thread: %u transactions pending on "
  1397. "shutdown\n",
  1398. atomic_read(&journal->j_num_trans));
  1399. }
  1400. }
  1401. return 0;
  1402. }
  1403. /* Reads all the journal inodes without taking any cluster locks. Used
  1404. * for hard readonly access to determine whether any journal requires
  1405. * recovery. Also used to refresh the recovery generation numbers after
  1406. * a journal has been recovered by another node.
  1407. */
  1408. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
  1409. {
  1410. int ret = 0;
  1411. unsigned int slot;
  1412. struct buffer_head *di_bh = NULL;
  1413. struct ocfs2_dinode *di;
  1414. int journal_dirty = 0;
  1415. for(slot = 0; slot < osb->max_slots; slot++) {
  1416. ret = ocfs2_read_journal_inode(osb, slot, &di_bh, NULL);
  1417. if (ret) {
  1418. mlog_errno(ret);
  1419. goto out;
  1420. }
  1421. di = (struct ocfs2_dinode *) di_bh->b_data;
  1422. osb->slot_recovery_generations[slot] =
  1423. ocfs2_get_recovery_generation(di);
  1424. if (le32_to_cpu(di->id1.journal1.ij_flags) &
  1425. OCFS2_JOURNAL_DIRTY_FL)
  1426. journal_dirty = 1;
  1427. brelse(di_bh);
  1428. di_bh = NULL;
  1429. }
  1430. out:
  1431. if (journal_dirty)
  1432. ret = -EROFS;
  1433. return ret;
  1434. }