journal.c 41 KB

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