journal.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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 %"MLFu64", I'm doing a last iput for!",
  409. 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 %"MLFu64" has invalid "
  522. "signature: %.*s", fe->i_blkno, 7, fe->i_signature);
  523. status = -EIO;
  524. goto out;
  525. }
  526. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  527. if (dirty)
  528. flags |= OCFS2_JOURNAL_DIRTY_FL;
  529. else
  530. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  531. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  532. status = ocfs2_write_block(osb, bh, journal->j_inode);
  533. if (status < 0)
  534. mlog_errno(status);
  535. out:
  536. mlog_exit(status);
  537. return status;
  538. }
  539. /*
  540. * If the journal has been kmalloc'd it needs to be freed after this
  541. * call.
  542. */
  543. void ocfs2_journal_shutdown(struct ocfs2_super *osb)
  544. {
  545. struct ocfs2_journal *journal = NULL;
  546. int status = 0;
  547. struct inode *inode = NULL;
  548. int num_running_trans = 0;
  549. mlog_entry_void();
  550. BUG_ON(!osb);
  551. journal = osb->journal;
  552. if (!journal)
  553. goto done;
  554. inode = journal->j_inode;
  555. if (journal->j_state != OCFS2_JOURNAL_LOADED)
  556. goto done;
  557. /* need to inc inode use count as journal_destroy will iput. */
  558. if (!igrab(inode))
  559. BUG();
  560. num_running_trans = atomic_read(&(osb->journal->j_num_trans));
  561. if (num_running_trans > 0)
  562. mlog(0, "Shutting down journal: must wait on %d "
  563. "running transactions!\n",
  564. num_running_trans);
  565. /* Do a commit_cache here. It will flush our journal, *and*
  566. * release any locks that are still held.
  567. * set the SHUTDOWN flag and release the trans lock.
  568. * the commit thread will take the trans lock for us below. */
  569. journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
  570. /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
  571. * drop the trans_lock (which we want to hold until we
  572. * completely destroy the journal. */
  573. if (osb->commit_task) {
  574. /* Wait for the commit thread */
  575. mlog(0, "Waiting for ocfs2commit to exit....\n");
  576. kthread_stop(osb->commit_task);
  577. osb->commit_task = NULL;
  578. }
  579. BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
  580. status = ocfs2_journal_toggle_dirty(osb, 0);
  581. if (status < 0)
  582. mlog_errno(status);
  583. /* Shutdown the kernel journal system */
  584. journal_destroy(journal->j_journal);
  585. OCFS2_I(inode)->ip_open_count--;
  586. /* unlock our journal */
  587. ocfs2_meta_unlock(inode, 1);
  588. brelse(journal->j_bh);
  589. journal->j_bh = NULL;
  590. journal->j_state = OCFS2_JOURNAL_FREE;
  591. // up_write(&journal->j_trans_barrier);
  592. done:
  593. if (inode)
  594. iput(inode);
  595. mlog_exit_void();
  596. }
  597. static void ocfs2_clear_journal_error(struct super_block *sb,
  598. journal_t *journal,
  599. int slot)
  600. {
  601. int olderr;
  602. olderr = journal_errno(journal);
  603. if (olderr) {
  604. mlog(ML_ERROR, "File system error %d recorded in "
  605. "journal %u.\n", olderr, slot);
  606. mlog(ML_ERROR, "File system on device %s needs checking.\n",
  607. sb->s_id);
  608. journal_ack_err(journal);
  609. journal_clear_err(journal);
  610. }
  611. }
  612. int ocfs2_journal_load(struct ocfs2_journal *journal)
  613. {
  614. int status = 0;
  615. struct ocfs2_super *osb;
  616. mlog_entry_void();
  617. if (!journal)
  618. BUG();
  619. osb = journal->j_osb;
  620. status = journal_load(journal->j_journal);
  621. if (status < 0) {
  622. mlog(ML_ERROR, "Failed to load journal!\n");
  623. goto done;
  624. }
  625. ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
  626. status = ocfs2_journal_toggle_dirty(osb, 1);
  627. if (status < 0) {
  628. mlog_errno(status);
  629. goto done;
  630. }
  631. /* Launch the commit thread */
  632. osb->commit_task = kthread_run(ocfs2_commit_thread, osb, "ocfs2cmt-%d",
  633. osb->osb_id);
  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 %lu blocks\n",
  686. (inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9)));
  687. v_blkno = 0;
  688. while (v_blkno <
  689. (inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9))) {
  690. status = ocfs2_extent_map_get_blocks(inode, v_blkno,
  691. 1, &p_blkno,
  692. &p_blocks);
  693. if (status < 0) {
  694. mlog_errno(status);
  695. goto bail;
  696. }
  697. if (p_blocks > CONCURRENT_JOURNAL_FILL)
  698. p_blocks = CONCURRENT_JOURNAL_FILL;
  699. status = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  700. p_blkno, p_blocks, bhs, 0,
  701. inode);
  702. if (status < 0) {
  703. mlog_errno(status);
  704. goto bail;
  705. }
  706. for(i = 0; i < p_blocks; i++) {
  707. brelse(bhs[i]);
  708. bhs[i] = NULL;
  709. }
  710. v_blkno += p_blocks;
  711. }
  712. bail:
  713. for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
  714. if (bhs[i])
  715. brelse(bhs[i]);
  716. mlog_exit(status);
  717. return status;
  718. }
  719. struct ocfs2_la_recovery_item {
  720. struct list_head lri_list;
  721. int lri_slot;
  722. struct ocfs2_dinode *lri_la_dinode;
  723. struct ocfs2_dinode *lri_tl_dinode;
  724. };
  725. /* Does the second half of the recovery process. By this point, the
  726. * node is marked clean and can actually be considered recovered,
  727. * hence it's no longer in the recovery map, but there's still some
  728. * cleanup we can do which shouldn't happen within the recovery thread
  729. * as locking in that context becomes very difficult if we are to take
  730. * recovering nodes into account.
  731. *
  732. * NOTE: This function can and will sleep on recovery of other nodes
  733. * during cluster locking, just like any other ocfs2 process.
  734. */
  735. void ocfs2_complete_recovery(void *data)
  736. {
  737. int ret;
  738. struct ocfs2_super *osb = data;
  739. struct ocfs2_journal *journal = osb->journal;
  740. struct ocfs2_dinode *la_dinode, *tl_dinode;
  741. struct ocfs2_la_recovery_item *item;
  742. struct list_head *p, *n;
  743. LIST_HEAD(tmp_la_list);
  744. mlog_entry_void();
  745. mlog(0, "completing recovery from keventd\n");
  746. spin_lock(&journal->j_lock);
  747. list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
  748. spin_unlock(&journal->j_lock);
  749. list_for_each_safe(p, n, &tmp_la_list) {
  750. item = list_entry(p, struct ocfs2_la_recovery_item, lri_list);
  751. list_del_init(&item->lri_list);
  752. mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
  753. la_dinode = item->lri_la_dinode;
  754. if (la_dinode) {
  755. mlog(0, "Clean up local alloc %"MLFu64"\n",
  756. la_dinode->i_blkno);
  757. ret = ocfs2_complete_local_alloc_recovery(osb,
  758. la_dinode);
  759. if (ret < 0)
  760. mlog_errno(ret);
  761. kfree(la_dinode);
  762. }
  763. tl_dinode = item->lri_tl_dinode;
  764. if (tl_dinode) {
  765. mlog(0, "Clean up truncate log %"MLFu64"\n",
  766. tl_dinode->i_blkno);
  767. ret = ocfs2_complete_truncate_log_recovery(osb,
  768. tl_dinode);
  769. if (ret < 0)
  770. mlog_errno(ret);
  771. kfree(tl_dinode);
  772. }
  773. ret = ocfs2_recover_orphans(osb, item->lri_slot);
  774. if (ret < 0)
  775. mlog_errno(ret);
  776. kfree(item);
  777. }
  778. mlog(0, "Recovery completion\n");
  779. mlog_exit_void();
  780. }
  781. /* NOTE: This function always eats your references to la_dinode and
  782. * tl_dinode, either manually on error, or by passing them to
  783. * ocfs2_complete_recovery */
  784. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  785. int slot_num,
  786. struct ocfs2_dinode *la_dinode,
  787. struct ocfs2_dinode *tl_dinode)
  788. {
  789. struct ocfs2_la_recovery_item *item;
  790. item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_KERNEL);
  791. if (!item) {
  792. /* Though we wish to avoid it, we are in fact safe in
  793. * skipping local alloc cleanup as fsck.ocfs2 is more
  794. * than capable of reclaiming unused space. */
  795. if (la_dinode)
  796. kfree(la_dinode);
  797. if (tl_dinode)
  798. kfree(tl_dinode);
  799. mlog_errno(-ENOMEM);
  800. return;
  801. }
  802. INIT_LIST_HEAD(&item->lri_list);
  803. item->lri_la_dinode = la_dinode;
  804. item->lri_slot = slot_num;
  805. item->lri_tl_dinode = tl_dinode;
  806. spin_lock(&journal->j_lock);
  807. list_add_tail(&item->lri_list, &journal->j_la_cleanups);
  808. queue_work(ocfs2_wq, &journal->j_recovery_work);
  809. spin_unlock(&journal->j_lock);
  810. }
  811. /* Called by the mount code to queue recovery the last part of
  812. * recovery for it's own slot. */
  813. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
  814. {
  815. struct ocfs2_journal *journal = osb->journal;
  816. if (osb->dirty) {
  817. /* No need to queue up our truncate_log as regular
  818. * cleanup will catch that. */
  819. ocfs2_queue_recovery_completion(journal,
  820. osb->slot_num,
  821. osb->local_alloc_copy,
  822. NULL);
  823. ocfs2_schedule_truncate_log_flush(osb, 0);
  824. osb->local_alloc_copy = NULL;
  825. osb->dirty = 0;
  826. }
  827. }
  828. static int __ocfs2_recovery_thread(void *arg)
  829. {
  830. int status, node_num;
  831. struct ocfs2_super *osb = arg;
  832. mlog_entry_void();
  833. status = ocfs2_wait_on_mount(osb);
  834. if (status < 0) {
  835. goto bail;
  836. }
  837. restart:
  838. status = ocfs2_super_lock(osb, 1);
  839. if (status < 0) {
  840. mlog_errno(status);
  841. goto bail;
  842. }
  843. while(!ocfs2_node_map_is_empty(osb, &osb->recovery_map)) {
  844. node_num = ocfs2_node_map_first_set_bit(osb,
  845. &osb->recovery_map);
  846. if (node_num == O2NM_INVALID_NODE_NUM) {
  847. mlog(0, "Out of nodes to recover.\n");
  848. break;
  849. }
  850. status = ocfs2_recover_node(osb, node_num);
  851. if (status < 0) {
  852. mlog(ML_ERROR,
  853. "Error %d recovering node %d on device (%u,%u)!\n",
  854. status, node_num,
  855. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  856. mlog(ML_ERROR, "Volume requires unmount.\n");
  857. continue;
  858. }
  859. ocfs2_recovery_map_clear(osb, node_num);
  860. }
  861. ocfs2_super_unlock(osb, 1);
  862. /* We always run recovery on our own orphan dir - the dead
  863. * node(s) may have voted "no" on an inode delete earlier. A
  864. * revote is therefore required. */
  865. ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
  866. NULL);
  867. bail:
  868. mutex_lock(&osb->recovery_lock);
  869. if (!status &&
  870. !ocfs2_node_map_is_empty(osb, &osb->recovery_map)) {
  871. mutex_unlock(&osb->recovery_lock);
  872. goto restart;
  873. }
  874. osb->recovery_thread_task = NULL;
  875. mb(); /* sync with ocfs2_recovery_thread_running */
  876. wake_up(&osb->recovery_event);
  877. mutex_unlock(&osb->recovery_lock);
  878. mlog_exit(status);
  879. /* no one is callint kthread_stop() for us so the kthread() api
  880. * requires that we call do_exit(). And it isn't exported, but
  881. * complete_and_exit() seems to be a minimal wrapper around it. */
  882. complete_and_exit(NULL, status);
  883. return status;
  884. }
  885. void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
  886. {
  887. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  888. node_num, osb->node_num);
  889. mutex_lock(&osb->recovery_lock);
  890. if (osb->disable_recovery)
  891. goto out;
  892. /* People waiting on recovery will wait on
  893. * the recovery map to empty. */
  894. if (!ocfs2_recovery_map_set(osb, node_num))
  895. mlog(0, "node %d already be in recovery.\n", node_num);
  896. mlog(0, "starting recovery thread...\n");
  897. if (osb->recovery_thread_task)
  898. goto out;
  899. osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
  900. "ocfs2rec-%d", osb->osb_id);
  901. if (IS_ERR(osb->recovery_thread_task)) {
  902. mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
  903. osb->recovery_thread_task = NULL;
  904. }
  905. out:
  906. mutex_unlock(&osb->recovery_lock);
  907. wake_up(&osb->recovery_event);
  908. mlog_exit_void();
  909. }
  910. /* Does the actual journal replay and marks the journal inode as
  911. * clean. Will only replay if the journal inode is marked dirty. */
  912. static int ocfs2_replay_journal(struct ocfs2_super *osb,
  913. int node_num,
  914. int slot_num)
  915. {
  916. int status;
  917. int got_lock = 0;
  918. unsigned int flags;
  919. struct inode *inode = NULL;
  920. struct ocfs2_dinode *fe;
  921. journal_t *journal = NULL;
  922. struct buffer_head *bh = NULL;
  923. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  924. slot_num);
  925. if (inode == NULL) {
  926. status = -EACCES;
  927. mlog_errno(status);
  928. goto done;
  929. }
  930. if (is_bad_inode(inode)) {
  931. status = -EACCES;
  932. iput(inode);
  933. inode = NULL;
  934. mlog_errno(status);
  935. goto done;
  936. }
  937. SET_INODE_JOURNAL(inode);
  938. status = ocfs2_meta_lock_full(inode, NULL, &bh, 1,
  939. OCFS2_META_LOCK_RECOVERY);
  940. if (status < 0) {
  941. mlog(0, "status returned from ocfs2_meta_lock=%d\n", status);
  942. if (status != -ERESTARTSYS)
  943. mlog(ML_ERROR, "Could not lock journal!\n");
  944. goto done;
  945. }
  946. got_lock = 1;
  947. fe = (struct ocfs2_dinode *) bh->b_data;
  948. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  949. if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
  950. mlog(0, "No recovery required for node %d\n", node_num);
  951. goto done;
  952. }
  953. mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
  954. node_num, slot_num,
  955. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  956. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  957. status = ocfs2_force_read_journal(inode);
  958. if (status < 0) {
  959. mlog_errno(status);
  960. goto done;
  961. }
  962. mlog(0, "calling journal_init_inode\n");
  963. journal = journal_init_inode(inode);
  964. if (journal == NULL) {
  965. mlog(ML_ERROR, "Linux journal layer error\n");
  966. status = -EIO;
  967. goto done;
  968. }
  969. status = journal_load(journal);
  970. if (status < 0) {
  971. mlog_errno(status);
  972. if (!igrab(inode))
  973. BUG();
  974. journal_destroy(journal);
  975. goto done;
  976. }
  977. ocfs2_clear_journal_error(osb->sb, journal, slot_num);
  978. /* wipe the journal */
  979. mlog(0, "flushing the journal.\n");
  980. journal_lock_updates(journal);
  981. status = journal_flush(journal);
  982. journal_unlock_updates(journal);
  983. if (status < 0)
  984. mlog_errno(status);
  985. /* This will mark the node clean */
  986. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  987. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  988. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  989. status = ocfs2_write_block(osb, bh, inode);
  990. if (status < 0)
  991. mlog_errno(status);
  992. if (!igrab(inode))
  993. BUG();
  994. journal_destroy(journal);
  995. done:
  996. /* drop the lock on this nodes journal */
  997. if (got_lock)
  998. ocfs2_meta_unlock(inode, 1);
  999. if (inode)
  1000. iput(inode);
  1001. if (bh)
  1002. brelse(bh);
  1003. mlog_exit(status);
  1004. return status;
  1005. }
  1006. /*
  1007. * Do the most important parts of node recovery:
  1008. * - Replay it's journal
  1009. * - Stamp a clean local allocator file
  1010. * - Stamp a clean truncate log
  1011. * - Mark the node clean
  1012. *
  1013. * If this function completes without error, a node in OCFS2 can be
  1014. * said to have been safely recovered. As a result, failure during the
  1015. * second part of a nodes recovery process (local alloc recovery) is
  1016. * far less concerning.
  1017. */
  1018. static int ocfs2_recover_node(struct ocfs2_super *osb,
  1019. int node_num)
  1020. {
  1021. int status = 0;
  1022. int slot_num;
  1023. struct ocfs2_slot_info *si = osb->slot_info;
  1024. struct ocfs2_dinode *la_copy = NULL;
  1025. struct ocfs2_dinode *tl_copy = NULL;
  1026. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  1027. node_num, osb->node_num);
  1028. mlog(0, "checking node %d\n", node_num);
  1029. /* Should not ever be called to recover ourselves -- in that
  1030. * case we should've called ocfs2_journal_load instead. */
  1031. BUG_ON(osb->node_num == node_num);
  1032. slot_num = ocfs2_node_num_to_slot(si, node_num);
  1033. if (slot_num == OCFS2_INVALID_SLOT) {
  1034. status = 0;
  1035. mlog(0, "no slot for this node, so no recovery required.\n");
  1036. goto done;
  1037. }
  1038. mlog(0, "node %d was using slot %d\n", node_num, slot_num);
  1039. status = ocfs2_replay_journal(osb, node_num, slot_num);
  1040. if (status < 0) {
  1041. mlog_errno(status);
  1042. goto done;
  1043. }
  1044. /* Stamp a clean local alloc file AFTER recovering the journal... */
  1045. status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
  1046. if (status < 0) {
  1047. mlog_errno(status);
  1048. goto done;
  1049. }
  1050. /* An error from begin_truncate_log_recovery is not
  1051. * serious enough to warrant halting the rest of
  1052. * recovery. */
  1053. status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
  1054. if (status < 0)
  1055. mlog_errno(status);
  1056. /* Likewise, this would be a strange but ultimately not so
  1057. * harmful place to get an error... */
  1058. ocfs2_clear_slot(si, slot_num);
  1059. status = ocfs2_update_disk_slots(osb, si);
  1060. if (status < 0)
  1061. mlog_errno(status);
  1062. /* This will kfree the memory pointed to by la_copy and tl_copy */
  1063. ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
  1064. tl_copy);
  1065. status = 0;
  1066. done:
  1067. mlog_exit(status);
  1068. return status;
  1069. }
  1070. /* Test node liveness by trylocking his journal. If we get the lock,
  1071. * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
  1072. * still alive (we couldn't get the lock) and < 0 on error. */
  1073. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  1074. int slot_num)
  1075. {
  1076. int status, flags;
  1077. struct inode *inode = NULL;
  1078. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1079. slot_num);
  1080. if (inode == NULL) {
  1081. mlog(ML_ERROR, "access error\n");
  1082. status = -EACCES;
  1083. goto bail;
  1084. }
  1085. if (is_bad_inode(inode)) {
  1086. mlog(ML_ERROR, "access error (bad inode)\n");
  1087. iput(inode);
  1088. inode = NULL;
  1089. status = -EACCES;
  1090. goto bail;
  1091. }
  1092. SET_INODE_JOURNAL(inode);
  1093. flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
  1094. status = ocfs2_meta_lock_full(inode, NULL, NULL, 1, flags);
  1095. if (status < 0) {
  1096. if (status != -EAGAIN)
  1097. mlog_errno(status);
  1098. goto bail;
  1099. }
  1100. ocfs2_meta_unlock(inode, 1);
  1101. bail:
  1102. if (inode)
  1103. iput(inode);
  1104. return status;
  1105. }
  1106. /* Call this underneath ocfs2_super_lock. It also assumes that the
  1107. * slot info struct has been updated from disk. */
  1108. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
  1109. {
  1110. int status, i, node_num;
  1111. struct ocfs2_slot_info *si = osb->slot_info;
  1112. /* This is called with the super block cluster lock, so we
  1113. * know that the slot map can't change underneath us. */
  1114. spin_lock(&si->si_lock);
  1115. for(i = 0; i < si->si_num_slots; i++) {
  1116. if (i == osb->slot_num)
  1117. continue;
  1118. if (ocfs2_is_empty_slot(si, i))
  1119. continue;
  1120. node_num = si->si_global_node_nums[i];
  1121. if (ocfs2_node_map_test_bit(osb, &osb->recovery_map, node_num))
  1122. continue;
  1123. spin_unlock(&si->si_lock);
  1124. /* Ok, we have a slot occupied by another node which
  1125. * is not in the recovery map. We trylock his journal
  1126. * file here to test if he's alive. */
  1127. status = ocfs2_trylock_journal(osb, i);
  1128. if (!status) {
  1129. /* Since we're called from mount, we know that
  1130. * the recovery thread can't race us on
  1131. * setting / checking the recovery bits. */
  1132. ocfs2_recovery_thread(osb, node_num);
  1133. } else if ((status < 0) && (status != -EAGAIN)) {
  1134. mlog_errno(status);
  1135. goto bail;
  1136. }
  1137. spin_lock(&si->si_lock);
  1138. }
  1139. spin_unlock(&si->si_lock);
  1140. status = 0;
  1141. bail:
  1142. mlog_exit(status);
  1143. return status;
  1144. }
  1145. static int ocfs2_queue_orphans(struct ocfs2_super *osb,
  1146. int slot,
  1147. struct inode **head)
  1148. {
  1149. int status;
  1150. struct inode *orphan_dir_inode = NULL;
  1151. struct inode *iter;
  1152. unsigned long offset, blk, local;
  1153. struct buffer_head *bh = NULL;
  1154. struct ocfs2_dir_entry *de;
  1155. struct super_block *sb = osb->sb;
  1156. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1157. ORPHAN_DIR_SYSTEM_INODE,
  1158. slot);
  1159. if (!orphan_dir_inode) {
  1160. status = -ENOENT;
  1161. mlog_errno(status);
  1162. return status;
  1163. }
  1164. mutex_lock(&orphan_dir_inode->i_mutex);
  1165. status = ocfs2_meta_lock(orphan_dir_inode, NULL, NULL, 0);
  1166. if (status < 0) {
  1167. mlog_errno(status);
  1168. goto out;
  1169. }
  1170. offset = 0;
  1171. iter = NULL;
  1172. while(offset < i_size_read(orphan_dir_inode)) {
  1173. blk = offset >> sb->s_blocksize_bits;
  1174. bh = ocfs2_bread(orphan_dir_inode, blk, &status, 0);
  1175. if (!bh)
  1176. status = -EINVAL;
  1177. if (status < 0) {
  1178. if (bh)
  1179. brelse(bh);
  1180. mlog_errno(status);
  1181. goto out_unlock;
  1182. }
  1183. local = 0;
  1184. while(offset < i_size_read(orphan_dir_inode)
  1185. && local < sb->s_blocksize) {
  1186. de = (struct ocfs2_dir_entry *) (bh->b_data + local);
  1187. if (!ocfs2_check_dir_entry(orphan_dir_inode,
  1188. de, bh, local)) {
  1189. status = -EINVAL;
  1190. mlog_errno(status);
  1191. brelse(bh);
  1192. goto out_unlock;
  1193. }
  1194. local += le16_to_cpu(de->rec_len);
  1195. offset += le16_to_cpu(de->rec_len);
  1196. /* I guess we silently fail on no inode? */
  1197. if (!le64_to_cpu(de->inode))
  1198. continue;
  1199. if (de->file_type > OCFS2_FT_MAX) {
  1200. mlog(ML_ERROR,
  1201. "block %llu contains invalid de: "
  1202. "inode = %"MLFu64", rec_len = %u, "
  1203. "name_len = %u, file_type = %u, "
  1204. "name='%.*s'\n",
  1205. (unsigned long long)bh->b_blocknr,
  1206. le64_to_cpu(de->inode),
  1207. le16_to_cpu(de->rec_len),
  1208. de->name_len,
  1209. de->file_type,
  1210. de->name_len,
  1211. de->name);
  1212. continue;
  1213. }
  1214. if (de->name_len == 1 && !strncmp(".", de->name, 1))
  1215. continue;
  1216. if (de->name_len == 2 && !strncmp("..", de->name, 2))
  1217. continue;
  1218. iter = ocfs2_iget(osb, le64_to_cpu(de->inode));
  1219. if (IS_ERR(iter))
  1220. continue;
  1221. mlog(0, "queue orphan %"MLFu64"\n",
  1222. OCFS2_I(iter)->ip_blkno);
  1223. /* No locking is required for the next_orphan
  1224. * queue as there is only ever a single
  1225. * process doing orphan recovery. */
  1226. OCFS2_I(iter)->ip_next_orphan = *head;
  1227. *head = iter;
  1228. }
  1229. brelse(bh);
  1230. }
  1231. out_unlock:
  1232. ocfs2_meta_unlock(orphan_dir_inode, 0);
  1233. out:
  1234. mutex_unlock(&orphan_dir_inode->i_mutex);
  1235. iput(orphan_dir_inode);
  1236. return status;
  1237. }
  1238. static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
  1239. int slot)
  1240. {
  1241. int ret;
  1242. spin_lock(&osb->osb_lock);
  1243. ret = !osb->osb_orphan_wipes[slot];
  1244. spin_unlock(&osb->osb_lock);
  1245. return ret;
  1246. }
  1247. static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
  1248. int slot)
  1249. {
  1250. spin_lock(&osb->osb_lock);
  1251. /* Mark ourselves such that new processes in delete_inode()
  1252. * know to quit early. */
  1253. ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1254. while (osb->osb_orphan_wipes[slot]) {
  1255. /* If any processes are already in the middle of an
  1256. * orphan wipe on this dir, then we need to wait for
  1257. * them. */
  1258. spin_unlock(&osb->osb_lock);
  1259. wait_event_interruptible(osb->osb_wipe_event,
  1260. ocfs2_orphan_recovery_can_continue(osb, slot));
  1261. spin_lock(&osb->osb_lock);
  1262. }
  1263. spin_unlock(&osb->osb_lock);
  1264. }
  1265. static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
  1266. int slot)
  1267. {
  1268. ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1269. }
  1270. /*
  1271. * Orphan recovery. Each mounted node has it's own orphan dir which we
  1272. * must run during recovery. Our strategy here is to build a list of
  1273. * the inodes in the orphan dir and iget/iput them. The VFS does
  1274. * (most) of the rest of the work.
  1275. *
  1276. * Orphan recovery can happen at any time, not just mount so we have a
  1277. * couple of extra considerations.
  1278. *
  1279. * - We grab as many inodes as we can under the orphan dir lock -
  1280. * doing iget() outside the orphan dir risks getting a reference on
  1281. * an invalid inode.
  1282. * - We must be sure not to deadlock with other processes on the
  1283. * system wanting to run delete_inode(). This can happen when they go
  1284. * to lock the orphan dir and the orphan recovery process attempts to
  1285. * iget() inside the orphan dir lock. This can be avoided by
  1286. * advertising our state to ocfs2_delete_inode().
  1287. */
  1288. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  1289. int slot)
  1290. {
  1291. int ret = 0;
  1292. struct inode *inode = NULL;
  1293. struct inode *iter;
  1294. struct ocfs2_inode_info *oi;
  1295. mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
  1296. ocfs2_mark_recovering_orphan_dir(osb, slot);
  1297. ret = ocfs2_queue_orphans(osb, slot, &inode);
  1298. ocfs2_clear_recovering_orphan_dir(osb, slot);
  1299. /* Error here should be noted, but we want to continue with as
  1300. * many queued inodes as we've got. */
  1301. if (ret)
  1302. mlog_errno(ret);
  1303. while (inode) {
  1304. oi = OCFS2_I(inode);
  1305. mlog(0, "iput orphan %"MLFu64"\n", oi->ip_blkno);
  1306. iter = oi->ip_next_orphan;
  1307. spin_lock(&oi->ip_lock);
  1308. /* Delete voting may have set these on the assumption
  1309. * that the other node would wipe them successfully.
  1310. * If they are still in the node's orphan dir, we need
  1311. * to reset that state. */
  1312. oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
  1313. /* Set the proper information to get us going into
  1314. * ocfs2_delete_inode. */
  1315. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  1316. oi->ip_orphaned_slot = slot;
  1317. spin_unlock(&oi->ip_lock);
  1318. iput(inode);
  1319. inode = iter;
  1320. }
  1321. return ret;
  1322. }
  1323. static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
  1324. {
  1325. /* This check is good because ocfs2 will wait on our recovery
  1326. * thread before changing it to something other than MOUNTED
  1327. * or DISABLED. */
  1328. wait_event(osb->osb_mount_event,
  1329. atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
  1330. atomic_read(&osb->vol_state) == VOLUME_DISABLED);
  1331. /* If there's an error on mount, then we may never get to the
  1332. * MOUNTED flag, but this is set right before
  1333. * dismount_volume() so we can trust it. */
  1334. if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
  1335. mlog(0, "mount error, exiting!\n");
  1336. return -EBUSY;
  1337. }
  1338. return 0;
  1339. }
  1340. static int ocfs2_commit_thread(void *arg)
  1341. {
  1342. int status;
  1343. struct ocfs2_super *osb = arg;
  1344. struct ocfs2_journal *journal = osb->journal;
  1345. /* we can trust j_num_trans here because _should_stop() is only set in
  1346. * shutdown and nobody other than ourselves should be able to start
  1347. * transactions. committing on shutdown might take a few iterations
  1348. * as final transactions put deleted inodes on the list */
  1349. while (!(kthread_should_stop() &&
  1350. atomic_read(&journal->j_num_trans) == 0)) {
  1351. wait_event_interruptible(osb->checkpoint_event,
  1352. atomic_read(&journal->j_num_trans)
  1353. || kthread_should_stop());
  1354. status = ocfs2_commit_cache(osb);
  1355. if (status < 0)
  1356. mlog_errno(status);
  1357. if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
  1358. mlog(ML_KTHREAD,
  1359. "commit_thread: %u transactions pending on "
  1360. "shutdown\n",
  1361. atomic_read(&journal->j_num_trans));
  1362. }
  1363. }
  1364. return 0;
  1365. }
  1366. /* Look for a dirty journal without taking any cluster locks. Used for
  1367. * hard readonly access to determine whether the file system journals
  1368. * require recovery. */
  1369. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
  1370. {
  1371. int ret = 0;
  1372. unsigned int slot;
  1373. struct buffer_head *di_bh;
  1374. struct ocfs2_dinode *di;
  1375. struct inode *journal = NULL;
  1376. for(slot = 0; slot < osb->max_slots; slot++) {
  1377. journal = ocfs2_get_system_file_inode(osb,
  1378. JOURNAL_SYSTEM_INODE,
  1379. slot);
  1380. if (!journal || is_bad_inode(journal)) {
  1381. ret = -EACCES;
  1382. mlog_errno(ret);
  1383. goto out;
  1384. }
  1385. di_bh = NULL;
  1386. ret = ocfs2_read_block(osb, OCFS2_I(journal)->ip_blkno, &di_bh,
  1387. 0, journal);
  1388. if (ret < 0) {
  1389. mlog_errno(ret);
  1390. goto out;
  1391. }
  1392. di = (struct ocfs2_dinode *) di_bh->b_data;
  1393. if (le32_to_cpu(di->id1.journal1.ij_flags) &
  1394. OCFS2_JOURNAL_DIRTY_FL)
  1395. ret = -EROFS;
  1396. brelse(di_bh);
  1397. if (ret)
  1398. break;
  1399. }
  1400. out:
  1401. if (journal)
  1402. iput(journal);
  1403. return ret;
  1404. }