journal.c 37 KB

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