journal.c 37 KB

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