journal.c 37 KB

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