journal.c 42 KB

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