journal.c 43 KB

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