journal.c 46 KB

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