journal.c 43 KB

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