journal.c 43 KB

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