journal.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * journal.c
  5. *
  6. * Defines functions of journalling api
  7. *
  8. * Copyright (C) 2003, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <linux/kthread.h>
  30. #define MLOG_MASK_PREFIX ML_JOURNAL
  31. #include <cluster/masklog.h>
  32. #include "ocfs2.h"
  33. #include "alloc.h"
  34. #include "dlmglue.h"
  35. #include "extent_map.h"
  36. #include "heartbeat.h"
  37. #include "inode.h"
  38. #include "journal.h"
  39. #include "localalloc.h"
  40. #include "namei.h"
  41. #include "slot_map.h"
  42. #include "super.h"
  43. #include "vote.h"
  44. #include "sysfile.h"
  45. #include "buffer_head_io.h"
  46. DEFINE_SPINLOCK(trans_inc_lock);
  47. static int ocfs2_force_read_journal(struct inode *inode);
  48. static int ocfs2_recover_node(struct ocfs2_super *osb,
  49. int node_num);
  50. static int __ocfs2_recovery_thread(void *arg);
  51. static int ocfs2_commit_cache(struct ocfs2_super *osb);
  52. static int ocfs2_wait_on_mount(struct ocfs2_super *osb);
  53. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  54. int dirty);
  55. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  56. int slot_num);
  57. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  58. int slot);
  59. static int ocfs2_commit_thread(void *arg);
  60. static int ocfs2_commit_cache(struct ocfs2_super *osb)
  61. {
  62. int status = 0;
  63. unsigned int flushed;
  64. unsigned long old_id;
  65. struct ocfs2_journal *journal = NULL;
  66. mlog_entry_void();
  67. journal = osb->journal;
  68. /* Flush all pending commits and checkpoint the journal. */
  69. down_write(&journal->j_trans_barrier);
  70. if (atomic_read(&journal->j_num_trans) == 0) {
  71. up_write(&journal->j_trans_barrier);
  72. mlog(0, "No transactions for me to flush!\n");
  73. goto finally;
  74. }
  75. journal_lock_updates(journal->j_journal);
  76. status = journal_flush(journal->j_journal);
  77. journal_unlock_updates(journal->j_journal);
  78. if (status < 0) {
  79. up_write(&journal->j_trans_barrier);
  80. mlog_errno(status);
  81. goto finally;
  82. }
  83. old_id = ocfs2_inc_trans_id(journal);
  84. flushed = atomic_read(&journal->j_num_trans);
  85. atomic_set(&journal->j_num_trans, 0);
  86. up_write(&journal->j_trans_barrier);
  87. mlog(0, "commit_thread: flushed transaction %lu (%u handles)\n",
  88. journal->j_trans_id, flushed);
  89. ocfs2_kick_vote_thread(osb);
  90. wake_up(&journal->j_checkpointed);
  91. finally:
  92. mlog_exit(status);
  93. return status;
  94. }
  95. /* pass it NULL and it will allocate a new handle object for you. If
  96. * you pass it a handle however, it may still return error, in which
  97. * case it has free'd the passed handle for you. */
  98. handle_t *ocfs2_start_trans(struct ocfs2_super *osb, int max_buffs)
  99. {
  100. journal_t *journal = osb->journal->j_journal;
  101. handle_t *handle;
  102. BUG_ON(!osb || !osb->journal->j_journal);
  103. if (ocfs2_is_hard_readonly(osb))
  104. return ERR_PTR(-EROFS);
  105. BUG_ON(osb->journal->j_state == OCFS2_JOURNAL_FREE);
  106. BUG_ON(max_buffs <= 0);
  107. /* JBD might support this, but our journalling code doesn't yet. */
  108. if (journal_current_handle()) {
  109. mlog(ML_ERROR, "Recursive transaction attempted!\n");
  110. BUG();
  111. }
  112. down_read(&osb->journal->j_trans_barrier);
  113. handle = journal_start(journal, max_buffs);
  114. if (IS_ERR(handle)) {
  115. up_read(&osb->journal->j_trans_barrier);
  116. mlog_errno(PTR_ERR(handle));
  117. if (is_journal_aborted(journal)) {
  118. ocfs2_abort(osb->sb, "Detected aborted journal");
  119. handle = ERR_PTR(-EROFS);
  120. }
  121. } else {
  122. 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", (unsigned long long)fe->i_blkno, 7,
  359. fe->i_signature);
  360. status = -EIO;
  361. goto out;
  362. }
  363. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  364. if (dirty)
  365. flags |= OCFS2_JOURNAL_DIRTY_FL;
  366. else
  367. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  368. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  369. status = ocfs2_write_block(osb, bh, journal->j_inode);
  370. if (status < 0)
  371. mlog_errno(status);
  372. out:
  373. mlog_exit(status);
  374. return status;
  375. }
  376. /*
  377. * If the journal has been kmalloc'd it needs to be freed after this
  378. * call.
  379. */
  380. void ocfs2_journal_shutdown(struct ocfs2_super *osb)
  381. {
  382. struct ocfs2_journal *journal = NULL;
  383. int status = 0;
  384. struct inode *inode = NULL;
  385. int num_running_trans = 0;
  386. mlog_entry_void();
  387. BUG_ON(!osb);
  388. journal = osb->journal;
  389. if (!journal)
  390. goto done;
  391. inode = journal->j_inode;
  392. if (journal->j_state != OCFS2_JOURNAL_LOADED)
  393. goto done;
  394. /* need to inc inode use count as journal_destroy will iput. */
  395. if (!igrab(inode))
  396. BUG();
  397. num_running_trans = atomic_read(&(osb->journal->j_num_trans));
  398. if (num_running_trans > 0)
  399. mlog(0, "Shutting down journal: must wait on %d "
  400. "running transactions!\n",
  401. num_running_trans);
  402. /* Do a commit_cache here. It will flush our journal, *and*
  403. * release any locks that are still held.
  404. * set the SHUTDOWN flag and release the trans lock.
  405. * the commit thread will take the trans lock for us below. */
  406. journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
  407. /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
  408. * drop the trans_lock (which we want to hold until we
  409. * completely destroy the journal. */
  410. if (osb->commit_task) {
  411. /* Wait for the commit thread */
  412. mlog(0, "Waiting for ocfs2commit to exit....\n");
  413. kthread_stop(osb->commit_task);
  414. osb->commit_task = NULL;
  415. }
  416. BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
  417. if (ocfs2_mount_local(osb)) {
  418. journal_lock_updates(journal->j_journal);
  419. status = journal_flush(journal->j_journal);
  420. journal_unlock_updates(journal->j_journal);
  421. if (status < 0)
  422. mlog_errno(status);
  423. }
  424. if (status == 0) {
  425. /*
  426. * Do not toggle if flush was unsuccessful otherwise
  427. * will leave dirty metadata in a "clean" journal
  428. */
  429. status = ocfs2_journal_toggle_dirty(osb, 0);
  430. if (status < 0)
  431. mlog_errno(status);
  432. }
  433. /* Shutdown the kernel journal system */
  434. journal_destroy(journal->j_journal);
  435. OCFS2_I(inode)->ip_open_count--;
  436. /* unlock our journal */
  437. ocfs2_meta_unlock(inode, 1);
  438. brelse(journal->j_bh);
  439. journal->j_bh = NULL;
  440. journal->j_state = OCFS2_JOURNAL_FREE;
  441. // up_write(&journal->j_trans_barrier);
  442. done:
  443. if (inode)
  444. iput(inode);
  445. mlog_exit_void();
  446. }
  447. static void ocfs2_clear_journal_error(struct super_block *sb,
  448. journal_t *journal,
  449. int slot)
  450. {
  451. int olderr;
  452. olderr = journal_errno(journal);
  453. if (olderr) {
  454. mlog(ML_ERROR, "File system error %d recorded in "
  455. "journal %u.\n", olderr, slot);
  456. mlog(ML_ERROR, "File system on device %s needs checking.\n",
  457. sb->s_id);
  458. journal_ack_err(journal);
  459. journal_clear_err(journal);
  460. }
  461. }
  462. int ocfs2_journal_load(struct ocfs2_journal *journal, int local)
  463. {
  464. int status = 0;
  465. struct ocfs2_super *osb;
  466. mlog_entry_void();
  467. if (!journal)
  468. BUG();
  469. osb = journal->j_osb;
  470. status = journal_load(journal->j_journal);
  471. if (status < 0) {
  472. mlog(ML_ERROR, "Failed to load journal!\n");
  473. goto done;
  474. }
  475. ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
  476. status = ocfs2_journal_toggle_dirty(osb, 1);
  477. if (status < 0) {
  478. mlog_errno(status);
  479. goto done;
  480. }
  481. /* Launch the commit thread */
  482. if (!local) {
  483. osb->commit_task = kthread_run(ocfs2_commit_thread, osb,
  484. "ocfs2cmt");
  485. if (IS_ERR(osb->commit_task)) {
  486. status = PTR_ERR(osb->commit_task);
  487. osb->commit_task = NULL;
  488. mlog(ML_ERROR, "unable to launch ocfs2commit thread, "
  489. "error=%d", status);
  490. goto done;
  491. }
  492. } else
  493. osb->commit_task = NULL;
  494. done:
  495. mlog_exit(status);
  496. return status;
  497. }
  498. /* 'full' flag tells us whether we clear out all blocks or if we just
  499. * mark the journal clean */
  500. int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
  501. {
  502. int status;
  503. mlog_entry_void();
  504. BUG_ON(!journal);
  505. status = journal_wipe(journal->j_journal, full);
  506. if (status < 0) {
  507. mlog_errno(status);
  508. goto bail;
  509. }
  510. status = ocfs2_journal_toggle_dirty(journal->j_osb, 0);
  511. if (status < 0)
  512. mlog_errno(status);
  513. bail:
  514. mlog_exit(status);
  515. return status;
  516. }
  517. /*
  518. * JBD Might read a cached version of another nodes journal file. We
  519. * don't want this as this file changes often and we get no
  520. * notification on those changes. The only way to be sure that we've
  521. * got the most up to date version of those blocks then is to force
  522. * read them off disk. Just searching through the buffer cache won't
  523. * work as there may be pages backing this file which are still marked
  524. * up to date. We know things can't change on this file underneath us
  525. * as we have the lock by now :)
  526. */
  527. static int ocfs2_force_read_journal(struct inode *inode)
  528. {
  529. int status = 0;
  530. int i;
  531. u64 v_blkno, p_blkno, p_blocks, num_blocks;
  532. #define CONCURRENT_JOURNAL_FILL 32ULL
  533. struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
  534. mlog_entry_void();
  535. memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
  536. num_blocks = ocfs2_blocks_for_bytes(inode->i_sb, inode->i_size);
  537. v_blkno = 0;
  538. while (v_blkno < num_blocks) {
  539. status = ocfs2_extent_map_get_blocks(inode, v_blkno,
  540. &p_blkno, &p_blocks, NULL);
  541. if (status < 0) {
  542. mlog_errno(status);
  543. goto bail;
  544. }
  545. if (p_blocks > CONCURRENT_JOURNAL_FILL)
  546. p_blocks = CONCURRENT_JOURNAL_FILL;
  547. /* We are reading journal data which should not
  548. * be put in the uptodate cache */
  549. status = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  550. p_blkno, p_blocks, bhs, 0,
  551. NULL);
  552. if (status < 0) {
  553. mlog_errno(status);
  554. goto bail;
  555. }
  556. for(i = 0; i < p_blocks; i++) {
  557. brelse(bhs[i]);
  558. bhs[i] = NULL;
  559. }
  560. v_blkno += p_blocks;
  561. }
  562. bail:
  563. for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
  564. if (bhs[i])
  565. brelse(bhs[i]);
  566. mlog_exit(status);
  567. return status;
  568. }
  569. struct ocfs2_la_recovery_item {
  570. struct list_head lri_list;
  571. int lri_slot;
  572. struct ocfs2_dinode *lri_la_dinode;
  573. struct ocfs2_dinode *lri_tl_dinode;
  574. };
  575. /* Does the second half of the recovery process. By this point, the
  576. * node is marked clean and can actually be considered recovered,
  577. * hence it's no longer in the recovery map, but there's still some
  578. * cleanup we can do which shouldn't happen within the recovery thread
  579. * as locking in that context becomes very difficult if we are to take
  580. * recovering nodes into account.
  581. *
  582. * NOTE: This function can and will sleep on recovery of other nodes
  583. * during cluster locking, just like any other ocfs2 process.
  584. */
  585. void ocfs2_complete_recovery(struct work_struct *work)
  586. {
  587. int ret;
  588. struct ocfs2_journal *journal =
  589. container_of(work, struct ocfs2_journal, j_recovery_work);
  590. struct ocfs2_super *osb = journal->j_osb;
  591. struct ocfs2_dinode *la_dinode, *tl_dinode;
  592. struct ocfs2_la_recovery_item *item;
  593. struct list_head *p, *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_safe(p, n, &tmp_la_list) {
  601. item = list_entry(p, struct ocfs2_la_recovery_item, lri_list);
  602. list_del_init(&item->lri_list);
  603. mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
  604. la_dinode = item->lri_la_dinode;
  605. if (la_dinode) {
  606. mlog(0, "Clean up local alloc %llu\n",
  607. (unsigned long long)la_dinode->i_blkno);
  608. ret = ocfs2_complete_local_alloc_recovery(osb,
  609. la_dinode);
  610. if (ret < 0)
  611. mlog_errno(ret);
  612. kfree(la_dinode);
  613. }
  614. tl_dinode = item->lri_tl_dinode;
  615. if (tl_dinode) {
  616. mlog(0, "Clean up truncate log %llu\n",
  617. (unsigned long long)tl_dinode->i_blkno);
  618. ret = ocfs2_complete_truncate_log_recovery(osb,
  619. tl_dinode);
  620. if (ret < 0)
  621. mlog_errno(ret);
  622. kfree(tl_dinode);
  623. }
  624. ret = ocfs2_recover_orphans(osb, item->lri_slot);
  625. if (ret < 0)
  626. mlog_errno(ret);
  627. kfree(item);
  628. }
  629. mlog(0, "Recovery completion\n");
  630. mlog_exit_void();
  631. }
  632. /* NOTE: This function always eats your references to la_dinode and
  633. * tl_dinode, either manually on error, or by passing them to
  634. * ocfs2_complete_recovery */
  635. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  636. int slot_num,
  637. struct ocfs2_dinode *la_dinode,
  638. struct ocfs2_dinode *tl_dinode)
  639. {
  640. struct ocfs2_la_recovery_item *item;
  641. item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_NOFS);
  642. if (!item) {
  643. /* Though we wish to avoid it, we are in fact safe in
  644. * skipping local alloc cleanup as fsck.ocfs2 is more
  645. * than capable of reclaiming unused space. */
  646. if (la_dinode)
  647. kfree(la_dinode);
  648. if (tl_dinode)
  649. kfree(tl_dinode);
  650. mlog_errno(-ENOMEM);
  651. return;
  652. }
  653. INIT_LIST_HEAD(&item->lri_list);
  654. item->lri_la_dinode = la_dinode;
  655. item->lri_slot = slot_num;
  656. item->lri_tl_dinode = tl_dinode;
  657. spin_lock(&journal->j_lock);
  658. list_add_tail(&item->lri_list, &journal->j_la_cleanups);
  659. queue_work(ocfs2_wq, &journal->j_recovery_work);
  660. spin_unlock(&journal->j_lock);
  661. }
  662. /* Called by the mount code to queue recovery the last part of
  663. * recovery for it's own slot. */
  664. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
  665. {
  666. struct ocfs2_journal *journal = osb->journal;
  667. if (osb->dirty) {
  668. /* No need to queue up our truncate_log as regular
  669. * cleanup will catch that. */
  670. ocfs2_queue_recovery_completion(journal,
  671. osb->slot_num,
  672. osb->local_alloc_copy,
  673. NULL);
  674. ocfs2_schedule_truncate_log_flush(osb, 0);
  675. osb->local_alloc_copy = NULL;
  676. osb->dirty = 0;
  677. }
  678. }
  679. static int __ocfs2_recovery_thread(void *arg)
  680. {
  681. int status, node_num;
  682. struct ocfs2_super *osb = arg;
  683. mlog_entry_void();
  684. status = ocfs2_wait_on_mount(osb);
  685. if (status < 0) {
  686. goto bail;
  687. }
  688. restart:
  689. status = ocfs2_super_lock(osb, 1);
  690. if (status < 0) {
  691. mlog_errno(status);
  692. goto bail;
  693. }
  694. while(!ocfs2_node_map_is_empty(osb, &osb->recovery_map)) {
  695. node_num = ocfs2_node_map_first_set_bit(osb,
  696. &osb->recovery_map);
  697. if (node_num == O2NM_INVALID_NODE_NUM) {
  698. mlog(0, "Out of nodes to recover.\n");
  699. break;
  700. }
  701. status = ocfs2_recover_node(osb, node_num);
  702. if (status < 0) {
  703. mlog(ML_ERROR,
  704. "Error %d recovering node %d on device (%u,%u)!\n",
  705. status, node_num,
  706. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  707. mlog(ML_ERROR, "Volume requires unmount.\n");
  708. continue;
  709. }
  710. ocfs2_recovery_map_clear(osb, node_num);
  711. }
  712. ocfs2_super_unlock(osb, 1);
  713. /* We always run recovery on our own orphan dir - the dead
  714. * node(s) may have voted "no" on an inode delete earlier. A
  715. * revote is therefore required. */
  716. ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
  717. NULL);
  718. bail:
  719. mutex_lock(&osb->recovery_lock);
  720. if (!status &&
  721. !ocfs2_node_map_is_empty(osb, &osb->recovery_map)) {
  722. mutex_unlock(&osb->recovery_lock);
  723. goto restart;
  724. }
  725. osb->recovery_thread_task = NULL;
  726. mb(); /* sync with ocfs2_recovery_thread_running */
  727. wake_up(&osb->recovery_event);
  728. mutex_unlock(&osb->recovery_lock);
  729. mlog_exit(status);
  730. /* no one is callint kthread_stop() for us so the kthread() api
  731. * requires that we call do_exit(). And it isn't exported, but
  732. * complete_and_exit() seems to be a minimal wrapper around it. */
  733. complete_and_exit(NULL, status);
  734. return status;
  735. }
  736. void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
  737. {
  738. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  739. node_num, osb->node_num);
  740. mutex_lock(&osb->recovery_lock);
  741. if (osb->disable_recovery)
  742. goto out;
  743. /* People waiting on recovery will wait on
  744. * the recovery map to empty. */
  745. if (!ocfs2_recovery_map_set(osb, node_num))
  746. mlog(0, "node %d already be in recovery.\n", node_num);
  747. mlog(0, "starting recovery thread...\n");
  748. if (osb->recovery_thread_task)
  749. goto out;
  750. osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
  751. "ocfs2rec");
  752. if (IS_ERR(osb->recovery_thread_task)) {
  753. mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
  754. osb->recovery_thread_task = NULL;
  755. }
  756. out:
  757. mutex_unlock(&osb->recovery_lock);
  758. wake_up(&osb->recovery_event);
  759. mlog_exit_void();
  760. }
  761. /* Does the actual journal replay and marks the journal inode as
  762. * clean. Will only replay if the journal inode is marked dirty. */
  763. static int ocfs2_replay_journal(struct ocfs2_super *osb,
  764. int node_num,
  765. int slot_num)
  766. {
  767. int status;
  768. int got_lock = 0;
  769. unsigned int flags;
  770. struct inode *inode = NULL;
  771. struct ocfs2_dinode *fe;
  772. journal_t *journal = NULL;
  773. struct buffer_head *bh = NULL;
  774. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  775. slot_num);
  776. if (inode == NULL) {
  777. status = -EACCES;
  778. mlog_errno(status);
  779. goto done;
  780. }
  781. if (is_bad_inode(inode)) {
  782. status = -EACCES;
  783. iput(inode);
  784. inode = NULL;
  785. mlog_errno(status);
  786. goto done;
  787. }
  788. SET_INODE_JOURNAL(inode);
  789. status = ocfs2_meta_lock_full(inode, &bh, 1, OCFS2_META_LOCK_RECOVERY);
  790. if (status < 0) {
  791. mlog(0, "status returned from ocfs2_meta_lock=%d\n", status);
  792. if (status != -ERESTARTSYS)
  793. mlog(ML_ERROR, "Could not lock journal!\n");
  794. goto done;
  795. }
  796. got_lock = 1;
  797. fe = (struct ocfs2_dinode *) bh->b_data;
  798. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  799. if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
  800. mlog(0, "No recovery required for node %d\n", node_num);
  801. goto done;
  802. }
  803. mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
  804. node_num, slot_num,
  805. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  806. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  807. status = ocfs2_force_read_journal(inode);
  808. if (status < 0) {
  809. mlog_errno(status);
  810. goto done;
  811. }
  812. mlog(0, "calling journal_init_inode\n");
  813. journal = journal_init_inode(inode);
  814. if (journal == NULL) {
  815. mlog(ML_ERROR, "Linux journal layer error\n");
  816. status = -EIO;
  817. goto done;
  818. }
  819. status = journal_load(journal);
  820. if (status < 0) {
  821. mlog_errno(status);
  822. if (!igrab(inode))
  823. BUG();
  824. journal_destroy(journal);
  825. goto done;
  826. }
  827. ocfs2_clear_journal_error(osb->sb, journal, slot_num);
  828. /* wipe the journal */
  829. mlog(0, "flushing the journal.\n");
  830. journal_lock_updates(journal);
  831. status = journal_flush(journal);
  832. journal_unlock_updates(journal);
  833. if (status < 0)
  834. mlog_errno(status);
  835. /* This will mark the node clean */
  836. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  837. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  838. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  839. status = ocfs2_write_block(osb, bh, inode);
  840. if (status < 0)
  841. mlog_errno(status);
  842. if (!igrab(inode))
  843. BUG();
  844. journal_destroy(journal);
  845. done:
  846. /* drop the lock on this nodes journal */
  847. if (got_lock)
  848. ocfs2_meta_unlock(inode, 1);
  849. if (inode)
  850. iput(inode);
  851. if (bh)
  852. brelse(bh);
  853. mlog_exit(status);
  854. return status;
  855. }
  856. /*
  857. * Do the most important parts of node recovery:
  858. * - Replay it's journal
  859. * - Stamp a clean local allocator file
  860. * - Stamp a clean truncate log
  861. * - Mark the node clean
  862. *
  863. * If this function completes without error, a node in OCFS2 can be
  864. * said to have been safely recovered. As a result, failure during the
  865. * second part of a nodes recovery process (local alloc recovery) is
  866. * far less concerning.
  867. */
  868. static int ocfs2_recover_node(struct ocfs2_super *osb,
  869. int node_num)
  870. {
  871. int status = 0;
  872. int slot_num;
  873. struct ocfs2_slot_info *si = osb->slot_info;
  874. struct ocfs2_dinode *la_copy = NULL;
  875. struct ocfs2_dinode *tl_copy = NULL;
  876. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  877. node_num, osb->node_num);
  878. mlog(0, "checking node %d\n", node_num);
  879. /* Should not ever be called to recover ourselves -- in that
  880. * case we should've called ocfs2_journal_load instead. */
  881. BUG_ON(osb->node_num == node_num);
  882. slot_num = ocfs2_node_num_to_slot(si, node_num);
  883. if (slot_num == OCFS2_INVALID_SLOT) {
  884. status = 0;
  885. mlog(0, "no slot for this node, so no recovery required.\n");
  886. goto done;
  887. }
  888. mlog(0, "node %d was using slot %d\n", node_num, slot_num);
  889. status = ocfs2_replay_journal(osb, node_num, slot_num);
  890. if (status < 0) {
  891. mlog_errno(status);
  892. goto done;
  893. }
  894. /* Stamp a clean local alloc file AFTER recovering the journal... */
  895. status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
  896. if (status < 0) {
  897. mlog_errno(status);
  898. goto done;
  899. }
  900. /* An error from begin_truncate_log_recovery is not
  901. * serious enough to warrant halting the rest of
  902. * recovery. */
  903. status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
  904. if (status < 0)
  905. mlog_errno(status);
  906. /* Likewise, this would be a strange but ultimately not so
  907. * harmful place to get an error... */
  908. ocfs2_clear_slot(si, slot_num);
  909. status = ocfs2_update_disk_slots(osb, si);
  910. if (status < 0)
  911. mlog_errno(status);
  912. /* This will kfree the memory pointed to by la_copy and tl_copy */
  913. ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
  914. tl_copy);
  915. status = 0;
  916. done:
  917. mlog_exit(status);
  918. return status;
  919. }
  920. /* Test node liveness by trylocking his journal. If we get the lock,
  921. * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
  922. * still alive (we couldn't get the lock) and < 0 on error. */
  923. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  924. int slot_num)
  925. {
  926. int status, flags;
  927. struct inode *inode = NULL;
  928. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  929. slot_num);
  930. if (inode == NULL) {
  931. mlog(ML_ERROR, "access error\n");
  932. status = -EACCES;
  933. goto bail;
  934. }
  935. if (is_bad_inode(inode)) {
  936. mlog(ML_ERROR, "access error (bad inode)\n");
  937. iput(inode);
  938. inode = NULL;
  939. status = -EACCES;
  940. goto bail;
  941. }
  942. SET_INODE_JOURNAL(inode);
  943. flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
  944. status = ocfs2_meta_lock_full(inode, NULL, 1, flags);
  945. if (status < 0) {
  946. if (status != -EAGAIN)
  947. mlog_errno(status);
  948. goto bail;
  949. }
  950. ocfs2_meta_unlock(inode, 1);
  951. bail:
  952. if (inode)
  953. iput(inode);
  954. return status;
  955. }
  956. /* Call this underneath ocfs2_super_lock. It also assumes that the
  957. * slot info struct has been updated from disk. */
  958. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
  959. {
  960. int status, i, node_num;
  961. struct ocfs2_slot_info *si = osb->slot_info;
  962. /* This is called with the super block cluster lock, so we
  963. * know that the slot map can't change underneath us. */
  964. spin_lock(&si->si_lock);
  965. for(i = 0; i < si->si_num_slots; i++) {
  966. if (i == osb->slot_num)
  967. continue;
  968. if (ocfs2_is_empty_slot(si, i))
  969. continue;
  970. node_num = si->si_global_node_nums[i];
  971. if (ocfs2_node_map_test_bit(osb, &osb->recovery_map, node_num))
  972. continue;
  973. spin_unlock(&si->si_lock);
  974. /* Ok, we have a slot occupied by another node which
  975. * is not in the recovery map. We trylock his journal
  976. * file here to test if he's alive. */
  977. status = ocfs2_trylock_journal(osb, i);
  978. if (!status) {
  979. /* Since we're called from mount, we know that
  980. * the recovery thread can't race us on
  981. * setting / checking the recovery bits. */
  982. ocfs2_recovery_thread(osb, node_num);
  983. } else if ((status < 0) && (status != -EAGAIN)) {
  984. mlog_errno(status);
  985. goto bail;
  986. }
  987. spin_lock(&si->si_lock);
  988. }
  989. spin_unlock(&si->si_lock);
  990. status = 0;
  991. bail:
  992. mlog_exit(status);
  993. return status;
  994. }
  995. static int ocfs2_queue_orphans(struct ocfs2_super *osb,
  996. int slot,
  997. struct inode **head)
  998. {
  999. int status;
  1000. struct inode *orphan_dir_inode = NULL;
  1001. struct inode *iter;
  1002. unsigned long offset, blk, local;
  1003. struct buffer_head *bh = NULL;
  1004. struct ocfs2_dir_entry *de;
  1005. struct super_block *sb = osb->sb;
  1006. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1007. ORPHAN_DIR_SYSTEM_INODE,
  1008. slot);
  1009. if (!orphan_dir_inode) {
  1010. status = -ENOENT;
  1011. mlog_errno(status);
  1012. return status;
  1013. }
  1014. mutex_lock(&orphan_dir_inode->i_mutex);
  1015. status = ocfs2_meta_lock(orphan_dir_inode, NULL, 0);
  1016. if (status < 0) {
  1017. mlog_errno(status);
  1018. goto out;
  1019. }
  1020. offset = 0;
  1021. iter = NULL;
  1022. while(offset < i_size_read(orphan_dir_inode)) {
  1023. blk = offset >> sb->s_blocksize_bits;
  1024. bh = ocfs2_bread(orphan_dir_inode, blk, &status, 0);
  1025. if (!bh)
  1026. status = -EINVAL;
  1027. if (status < 0) {
  1028. if (bh)
  1029. brelse(bh);
  1030. mlog_errno(status);
  1031. goto out_unlock;
  1032. }
  1033. local = 0;
  1034. while(offset < i_size_read(orphan_dir_inode)
  1035. && local < sb->s_blocksize) {
  1036. de = (struct ocfs2_dir_entry *) (bh->b_data + local);
  1037. if (!ocfs2_check_dir_entry(orphan_dir_inode,
  1038. de, bh, local)) {
  1039. status = -EINVAL;
  1040. mlog_errno(status);
  1041. brelse(bh);
  1042. goto out_unlock;
  1043. }
  1044. local += le16_to_cpu(de->rec_len);
  1045. offset += le16_to_cpu(de->rec_len);
  1046. /* I guess we silently fail on no inode? */
  1047. if (!le64_to_cpu(de->inode))
  1048. continue;
  1049. if (de->file_type > OCFS2_FT_MAX) {
  1050. mlog(ML_ERROR,
  1051. "block %llu contains invalid de: "
  1052. "inode = %llu, rec_len = %u, "
  1053. "name_len = %u, file_type = %u, "
  1054. "name='%.*s'\n",
  1055. (unsigned long long)bh->b_blocknr,
  1056. (unsigned long long)le64_to_cpu(de->inode),
  1057. le16_to_cpu(de->rec_len),
  1058. de->name_len,
  1059. de->file_type,
  1060. de->name_len,
  1061. de->name);
  1062. continue;
  1063. }
  1064. if (de->name_len == 1 && !strncmp(".", de->name, 1))
  1065. continue;
  1066. if (de->name_len == 2 && !strncmp("..", de->name, 2))
  1067. continue;
  1068. iter = ocfs2_iget(osb, le64_to_cpu(de->inode),
  1069. OCFS2_FI_FLAG_ORPHAN_RECOVERY);
  1070. if (IS_ERR(iter))
  1071. continue;
  1072. mlog(0, "queue orphan %llu\n",
  1073. (unsigned long long)OCFS2_I(iter)->ip_blkno);
  1074. /* No locking is required for the next_orphan
  1075. * queue as there is only ever a single
  1076. * process doing orphan recovery. */
  1077. OCFS2_I(iter)->ip_next_orphan = *head;
  1078. *head = iter;
  1079. }
  1080. brelse(bh);
  1081. }
  1082. out_unlock:
  1083. ocfs2_meta_unlock(orphan_dir_inode, 0);
  1084. out:
  1085. mutex_unlock(&orphan_dir_inode->i_mutex);
  1086. iput(orphan_dir_inode);
  1087. return status;
  1088. }
  1089. static int ocfs2_orphan_recovery_can_continue(struct ocfs2_super *osb,
  1090. int slot)
  1091. {
  1092. int ret;
  1093. spin_lock(&osb->osb_lock);
  1094. ret = !osb->osb_orphan_wipes[slot];
  1095. spin_unlock(&osb->osb_lock);
  1096. return ret;
  1097. }
  1098. static void ocfs2_mark_recovering_orphan_dir(struct ocfs2_super *osb,
  1099. int slot)
  1100. {
  1101. spin_lock(&osb->osb_lock);
  1102. /* Mark ourselves such that new processes in delete_inode()
  1103. * know to quit early. */
  1104. ocfs2_node_map_set_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1105. while (osb->osb_orphan_wipes[slot]) {
  1106. /* If any processes are already in the middle of an
  1107. * orphan wipe on this dir, then we need to wait for
  1108. * them. */
  1109. spin_unlock(&osb->osb_lock);
  1110. wait_event_interruptible(osb->osb_wipe_event,
  1111. ocfs2_orphan_recovery_can_continue(osb, slot));
  1112. spin_lock(&osb->osb_lock);
  1113. }
  1114. spin_unlock(&osb->osb_lock);
  1115. }
  1116. static void ocfs2_clear_recovering_orphan_dir(struct ocfs2_super *osb,
  1117. int slot)
  1118. {
  1119. ocfs2_node_map_clear_bit(osb, &osb->osb_recovering_orphan_dirs, slot);
  1120. }
  1121. /*
  1122. * Orphan recovery. Each mounted node has it's own orphan dir which we
  1123. * must run during recovery. Our strategy here is to build a list of
  1124. * the inodes in the orphan dir and iget/iput them. The VFS does
  1125. * (most) of the rest of the work.
  1126. *
  1127. * Orphan recovery can happen at any time, not just mount so we have a
  1128. * couple of extra considerations.
  1129. *
  1130. * - We grab as many inodes as we can under the orphan dir lock -
  1131. * doing iget() outside the orphan dir risks getting a reference on
  1132. * an invalid inode.
  1133. * - We must be sure not to deadlock with other processes on the
  1134. * system wanting to run delete_inode(). This can happen when they go
  1135. * to lock the orphan dir and the orphan recovery process attempts to
  1136. * iget() inside the orphan dir lock. This can be avoided by
  1137. * advertising our state to ocfs2_delete_inode().
  1138. */
  1139. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  1140. int slot)
  1141. {
  1142. int ret = 0;
  1143. struct inode *inode = NULL;
  1144. struct inode *iter;
  1145. struct ocfs2_inode_info *oi;
  1146. mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
  1147. ocfs2_mark_recovering_orphan_dir(osb, slot);
  1148. ret = ocfs2_queue_orphans(osb, slot, &inode);
  1149. ocfs2_clear_recovering_orphan_dir(osb, slot);
  1150. /* Error here should be noted, but we want to continue with as
  1151. * many queued inodes as we've got. */
  1152. if (ret)
  1153. mlog_errno(ret);
  1154. while (inode) {
  1155. oi = OCFS2_I(inode);
  1156. mlog(0, "iput orphan %llu\n", (unsigned long long)oi->ip_blkno);
  1157. iter = oi->ip_next_orphan;
  1158. spin_lock(&oi->ip_lock);
  1159. /* Delete voting may have set these on the assumption
  1160. * that the other node would wipe them successfully.
  1161. * If they are still in the node's orphan dir, we need
  1162. * to reset that state. */
  1163. oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
  1164. /* Set the proper information to get us going into
  1165. * ocfs2_delete_inode. */
  1166. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  1167. spin_unlock(&oi->ip_lock);
  1168. iput(inode);
  1169. inode = iter;
  1170. }
  1171. return ret;
  1172. }
  1173. static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
  1174. {
  1175. /* This check is good because ocfs2 will wait on our recovery
  1176. * thread before changing it to something other than MOUNTED
  1177. * or DISABLED. */
  1178. wait_event(osb->osb_mount_event,
  1179. atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
  1180. atomic_read(&osb->vol_state) == VOLUME_DISABLED);
  1181. /* If there's an error on mount, then we may never get to the
  1182. * MOUNTED flag, but this is set right before
  1183. * dismount_volume() so we can trust it. */
  1184. if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
  1185. mlog(0, "mount error, exiting!\n");
  1186. return -EBUSY;
  1187. }
  1188. return 0;
  1189. }
  1190. static int ocfs2_commit_thread(void *arg)
  1191. {
  1192. int status;
  1193. struct ocfs2_super *osb = arg;
  1194. struct ocfs2_journal *journal = osb->journal;
  1195. /* we can trust j_num_trans here because _should_stop() is only set in
  1196. * shutdown and nobody other than ourselves should be able to start
  1197. * transactions. committing on shutdown might take a few iterations
  1198. * as final transactions put deleted inodes on the list */
  1199. while (!(kthread_should_stop() &&
  1200. atomic_read(&journal->j_num_trans) == 0)) {
  1201. wait_event_interruptible(osb->checkpoint_event,
  1202. atomic_read(&journal->j_num_trans)
  1203. || kthread_should_stop());
  1204. status = ocfs2_commit_cache(osb);
  1205. if (status < 0)
  1206. mlog_errno(status);
  1207. if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
  1208. mlog(ML_KTHREAD,
  1209. "commit_thread: %u transactions pending on "
  1210. "shutdown\n",
  1211. atomic_read(&journal->j_num_trans));
  1212. }
  1213. }
  1214. return 0;
  1215. }
  1216. /* Look for a dirty journal without taking any cluster locks. Used for
  1217. * hard readonly access to determine whether the file system journals
  1218. * require recovery. */
  1219. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
  1220. {
  1221. int ret = 0;
  1222. unsigned int slot;
  1223. struct buffer_head *di_bh;
  1224. struct ocfs2_dinode *di;
  1225. struct inode *journal = NULL;
  1226. for(slot = 0; slot < osb->max_slots; slot++) {
  1227. journal = ocfs2_get_system_file_inode(osb,
  1228. JOURNAL_SYSTEM_INODE,
  1229. slot);
  1230. if (!journal || is_bad_inode(journal)) {
  1231. ret = -EACCES;
  1232. mlog_errno(ret);
  1233. goto out;
  1234. }
  1235. di_bh = NULL;
  1236. ret = ocfs2_read_block(osb, OCFS2_I(journal)->ip_blkno, &di_bh,
  1237. 0, journal);
  1238. if (ret < 0) {
  1239. mlog_errno(ret);
  1240. goto out;
  1241. }
  1242. di = (struct ocfs2_dinode *) di_bh->b_data;
  1243. if (le32_to_cpu(di->id1.journal1.ij_flags) &
  1244. OCFS2_JOURNAL_DIRTY_FL)
  1245. ret = -EROFS;
  1246. brelse(di_bh);
  1247. if (ret)
  1248. break;
  1249. }
  1250. out:
  1251. if (journal)
  1252. iput(journal);
  1253. return ret;
  1254. }