journal.c 43 KB

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