journal.c 39 KB

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