journal.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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_KERNEL);
  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 = %hu\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 %"MLFu64", I'm doing a last iput for!",
  409. 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 = %lu\n", inode->i_blocks);
  475. mlog(0, "inode->ip_clusters = %u\n", OCFS2_I(inode)->ip_clusters);
  476. /* call the kernels journal init function now */
  477. j_journal = journal_init_inode(inode);
  478. if (j_journal == NULL) {
  479. mlog(ML_ERROR, "Linux journal layer error\n");
  480. status = -EINVAL;
  481. goto done;
  482. }
  483. mlog(0, "Returned from journal_init_inode\n");
  484. mlog(0, "j_journal->j_maxlen = %u\n", j_journal->j_maxlen);
  485. *dirty = (le32_to_cpu(di->id1.journal1.ij_flags) &
  486. OCFS2_JOURNAL_DIRTY_FL);
  487. journal->j_journal = j_journal;
  488. journal->j_inode = inode;
  489. journal->j_bh = bh;
  490. ocfs2_set_journal_params(osb);
  491. journal->j_state = OCFS2_JOURNAL_LOADED;
  492. status = 0;
  493. done:
  494. if (status < 0) {
  495. if (meta_lock)
  496. ocfs2_meta_unlock(inode, 1);
  497. if (bh != NULL)
  498. brelse(bh);
  499. if (inode) {
  500. OCFS2_I(inode)->ip_open_count--;
  501. iput(inode);
  502. }
  503. }
  504. mlog_exit(status);
  505. return status;
  506. }
  507. static int ocfs2_journal_toggle_dirty(struct ocfs2_super *osb,
  508. int dirty)
  509. {
  510. int status;
  511. unsigned int flags;
  512. struct ocfs2_journal *journal = osb->journal;
  513. struct buffer_head *bh = journal->j_bh;
  514. struct ocfs2_dinode *fe;
  515. mlog_entry_void();
  516. fe = (struct ocfs2_dinode *)bh->b_data;
  517. if (!OCFS2_IS_VALID_DINODE(fe)) {
  518. /* This is called from startup/shutdown which will
  519. * handle the errors in a specific manner, so no need
  520. * to call ocfs2_error() here. */
  521. mlog(ML_ERROR, "Journal dinode %"MLFu64" has invalid "
  522. "signature: %.*s", fe->i_blkno, 7, fe->i_signature);
  523. status = -EIO;
  524. goto out;
  525. }
  526. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  527. if (dirty)
  528. flags |= OCFS2_JOURNAL_DIRTY_FL;
  529. else
  530. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  531. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  532. status = ocfs2_write_block(osb, bh, journal->j_inode);
  533. if (status < 0)
  534. mlog_errno(status);
  535. out:
  536. mlog_exit(status);
  537. return status;
  538. }
  539. /*
  540. * If the journal has been kmalloc'd it needs to be freed after this
  541. * call.
  542. */
  543. void ocfs2_journal_shutdown(struct ocfs2_super *osb)
  544. {
  545. struct ocfs2_journal *journal = NULL;
  546. int status = 0;
  547. struct inode *inode = NULL;
  548. int num_running_trans = 0;
  549. mlog_entry_void();
  550. BUG_ON(!osb);
  551. journal = osb->journal;
  552. if (!journal)
  553. goto done;
  554. inode = journal->j_inode;
  555. if (journal->j_state != OCFS2_JOURNAL_LOADED)
  556. goto done;
  557. /* need to inc inode use count as journal_destroy will iput. */
  558. if (!igrab(inode))
  559. BUG();
  560. num_running_trans = atomic_read(&(osb->journal->j_num_trans));
  561. if (num_running_trans > 0)
  562. mlog(0, "Shutting down journal: must wait on %d "
  563. "running transactions!\n",
  564. num_running_trans);
  565. /* Do a commit_cache here. It will flush our journal, *and*
  566. * release any locks that are still held.
  567. * set the SHUTDOWN flag and release the trans lock.
  568. * the commit thread will take the trans lock for us below. */
  569. journal->j_state = OCFS2_JOURNAL_IN_SHUTDOWN;
  570. /* The OCFS2_JOURNAL_IN_SHUTDOWN will signal to commit_cache to not
  571. * drop the trans_lock (which we want to hold until we
  572. * completely destroy the journal. */
  573. if (osb->commit_task) {
  574. /* Wait for the commit thread */
  575. mlog(0, "Waiting for ocfs2commit to exit....\n");
  576. kthread_stop(osb->commit_task);
  577. osb->commit_task = NULL;
  578. }
  579. BUG_ON(atomic_read(&(osb->journal->j_num_trans)) != 0);
  580. status = ocfs2_journal_toggle_dirty(osb, 0);
  581. if (status < 0)
  582. mlog_errno(status);
  583. /* Shutdown the kernel journal system */
  584. journal_destroy(journal->j_journal);
  585. OCFS2_I(inode)->ip_open_count--;
  586. /* unlock our journal */
  587. ocfs2_meta_unlock(inode, 1);
  588. brelse(journal->j_bh);
  589. journal->j_bh = NULL;
  590. journal->j_state = OCFS2_JOURNAL_FREE;
  591. // up_write(&journal->j_trans_barrier);
  592. done:
  593. if (inode)
  594. iput(inode);
  595. mlog_exit_void();
  596. }
  597. static void ocfs2_clear_journal_error(struct super_block *sb,
  598. journal_t *journal,
  599. int slot)
  600. {
  601. int olderr;
  602. olderr = journal_errno(journal);
  603. if (olderr) {
  604. mlog(ML_ERROR, "File system error %d recorded in "
  605. "journal %u.\n", olderr, slot);
  606. mlog(ML_ERROR, "File system on device %s needs checking.\n",
  607. sb->s_id);
  608. journal_ack_err(journal);
  609. journal_clear_err(journal);
  610. }
  611. }
  612. int ocfs2_journal_load(struct ocfs2_journal *journal)
  613. {
  614. int status = 0;
  615. struct ocfs2_super *osb;
  616. mlog_entry_void();
  617. if (!journal)
  618. BUG();
  619. osb = journal->j_osb;
  620. status = journal_load(journal->j_journal);
  621. if (status < 0) {
  622. mlog(ML_ERROR, "Failed to load journal!\n");
  623. goto done;
  624. }
  625. ocfs2_clear_journal_error(osb->sb, journal->j_journal, osb->slot_num);
  626. status = ocfs2_journal_toggle_dirty(osb, 1);
  627. if (status < 0) {
  628. mlog_errno(status);
  629. goto done;
  630. }
  631. /* Launch the commit thread */
  632. osb->commit_task = kthread_run(ocfs2_commit_thread, osb, "ocfs2cmt-%d",
  633. osb->osb_id);
  634. if (IS_ERR(osb->commit_task)) {
  635. status = PTR_ERR(osb->commit_task);
  636. osb->commit_task = NULL;
  637. mlog(ML_ERROR, "unable to launch ocfs2commit thread, error=%d",
  638. status);
  639. goto done;
  640. }
  641. done:
  642. mlog_exit(status);
  643. return status;
  644. }
  645. /* 'full' flag tells us whether we clear out all blocks or if we just
  646. * mark the journal clean */
  647. int ocfs2_journal_wipe(struct ocfs2_journal *journal, int full)
  648. {
  649. int status;
  650. mlog_entry_void();
  651. BUG_ON(!journal);
  652. status = journal_wipe(journal->j_journal, full);
  653. if (status < 0) {
  654. mlog_errno(status);
  655. goto bail;
  656. }
  657. status = ocfs2_journal_toggle_dirty(journal->j_osb, 0);
  658. if (status < 0)
  659. mlog_errno(status);
  660. bail:
  661. mlog_exit(status);
  662. return status;
  663. }
  664. /*
  665. * JBD Might read a cached version of another nodes journal file. We
  666. * don't want this as this file changes often and we get no
  667. * notification on those changes. The only way to be sure that we've
  668. * got the most up to date version of those blocks then is to force
  669. * read them off disk. Just searching through the buffer cache won't
  670. * work as there may be pages backing this file which are still marked
  671. * up to date. We know things can't change on this file underneath us
  672. * as we have the lock by now :)
  673. */
  674. static int ocfs2_force_read_journal(struct inode *inode)
  675. {
  676. int status = 0;
  677. int i, p_blocks;
  678. u64 v_blkno, p_blkno;
  679. #define CONCURRENT_JOURNAL_FILL 32
  680. struct buffer_head *bhs[CONCURRENT_JOURNAL_FILL];
  681. mlog_entry_void();
  682. BUG_ON(inode->i_blocks !=
  683. ocfs2_align_bytes_to_sectors(i_size_read(inode)));
  684. memset(bhs, 0, sizeof(struct buffer_head *) * CONCURRENT_JOURNAL_FILL);
  685. mlog(0, "Force reading %lu blocks\n",
  686. (inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9)));
  687. v_blkno = 0;
  688. while (v_blkno <
  689. (inode->i_blocks >> (inode->i_sb->s_blocksize_bits - 9))) {
  690. status = ocfs2_extent_map_get_blocks(inode, v_blkno,
  691. 1, &p_blkno,
  692. &p_blocks);
  693. if (status < 0) {
  694. mlog_errno(status);
  695. goto bail;
  696. }
  697. if (p_blocks > CONCURRENT_JOURNAL_FILL)
  698. p_blocks = CONCURRENT_JOURNAL_FILL;
  699. status = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
  700. p_blkno, p_blocks, bhs, 0,
  701. inode);
  702. if (status < 0) {
  703. mlog_errno(status);
  704. goto bail;
  705. }
  706. for(i = 0; i < p_blocks; i++) {
  707. brelse(bhs[i]);
  708. bhs[i] = NULL;
  709. }
  710. v_blkno += p_blocks;
  711. }
  712. bail:
  713. for(i = 0; i < CONCURRENT_JOURNAL_FILL; i++)
  714. if (bhs[i])
  715. brelse(bhs[i]);
  716. mlog_exit(status);
  717. return status;
  718. }
  719. struct ocfs2_la_recovery_item {
  720. struct list_head lri_list;
  721. int lri_slot;
  722. struct ocfs2_dinode *lri_la_dinode;
  723. struct ocfs2_dinode *lri_tl_dinode;
  724. };
  725. /* Does the second half of the recovery process. By this point, the
  726. * node is marked clean and can actually be considered recovered,
  727. * hence it's no longer in the recovery map, but there's still some
  728. * cleanup we can do which shouldn't happen within the recovery thread
  729. * as locking in that context becomes very difficult if we are to take
  730. * recovering nodes into account.
  731. *
  732. * NOTE: This function can and will sleep on recovery of other nodes
  733. * during cluster locking, just like any other ocfs2 process.
  734. */
  735. void ocfs2_complete_recovery(void *data)
  736. {
  737. int ret;
  738. struct ocfs2_super *osb = data;
  739. struct ocfs2_journal *journal = osb->journal;
  740. struct ocfs2_dinode *la_dinode, *tl_dinode;
  741. struct ocfs2_la_recovery_item *item;
  742. struct list_head *p, *n;
  743. LIST_HEAD(tmp_la_list);
  744. mlog_entry_void();
  745. mlog(0, "completing recovery from keventd\n");
  746. spin_lock(&journal->j_lock);
  747. list_splice_init(&journal->j_la_cleanups, &tmp_la_list);
  748. spin_unlock(&journal->j_lock);
  749. list_for_each_safe(p, n, &tmp_la_list) {
  750. item = list_entry(p, struct ocfs2_la_recovery_item, lri_list);
  751. list_del_init(&item->lri_list);
  752. mlog(0, "Complete recovery for slot %d\n", item->lri_slot);
  753. la_dinode = item->lri_la_dinode;
  754. if (la_dinode) {
  755. mlog(0, "Clean up local alloc %"MLFu64"\n",
  756. la_dinode->i_blkno);
  757. ret = ocfs2_complete_local_alloc_recovery(osb,
  758. la_dinode);
  759. if (ret < 0)
  760. mlog_errno(ret);
  761. kfree(la_dinode);
  762. }
  763. tl_dinode = item->lri_tl_dinode;
  764. if (tl_dinode) {
  765. mlog(0, "Clean up truncate log %"MLFu64"\n",
  766. tl_dinode->i_blkno);
  767. ret = ocfs2_complete_truncate_log_recovery(osb,
  768. tl_dinode);
  769. if (ret < 0)
  770. mlog_errno(ret);
  771. kfree(tl_dinode);
  772. }
  773. ret = ocfs2_recover_orphans(osb, item->lri_slot);
  774. if (ret < 0)
  775. mlog_errno(ret);
  776. kfree(item);
  777. }
  778. mlog(0, "Recovery completion\n");
  779. mlog_exit_void();
  780. }
  781. /* NOTE: This function always eats your references to la_dinode and
  782. * tl_dinode, either manually on error, or by passing them to
  783. * ocfs2_complete_recovery */
  784. static void ocfs2_queue_recovery_completion(struct ocfs2_journal *journal,
  785. int slot_num,
  786. struct ocfs2_dinode *la_dinode,
  787. struct ocfs2_dinode *tl_dinode)
  788. {
  789. struct ocfs2_la_recovery_item *item;
  790. item = kmalloc(sizeof(struct ocfs2_la_recovery_item), GFP_KERNEL);
  791. if (!item) {
  792. /* Though we wish to avoid it, we are in fact safe in
  793. * skipping local alloc cleanup as fsck.ocfs2 is more
  794. * than capable of reclaiming unused space. */
  795. if (la_dinode)
  796. kfree(la_dinode);
  797. if (tl_dinode)
  798. kfree(tl_dinode);
  799. mlog_errno(-ENOMEM);
  800. return;
  801. }
  802. INIT_LIST_HEAD(&item->lri_list);
  803. item->lri_la_dinode = la_dinode;
  804. item->lri_slot = slot_num;
  805. item->lri_tl_dinode = tl_dinode;
  806. spin_lock(&journal->j_lock);
  807. list_add_tail(&item->lri_list, &journal->j_la_cleanups);
  808. queue_work(ocfs2_wq, &journal->j_recovery_work);
  809. spin_unlock(&journal->j_lock);
  810. }
  811. /* Called by the mount code to queue recovery the last part of
  812. * recovery for it's own slot. */
  813. void ocfs2_complete_mount_recovery(struct ocfs2_super *osb)
  814. {
  815. struct ocfs2_journal *journal = osb->journal;
  816. if (osb->dirty) {
  817. /* No need to queue up our truncate_log as regular
  818. * cleanup will catch that. */
  819. ocfs2_queue_recovery_completion(journal,
  820. osb->slot_num,
  821. osb->local_alloc_copy,
  822. NULL);
  823. ocfs2_schedule_truncate_log_flush(osb, 0);
  824. osb->local_alloc_copy = NULL;
  825. osb->dirty = 0;
  826. }
  827. }
  828. static int __ocfs2_recovery_thread(void *arg)
  829. {
  830. int status, node_num;
  831. struct ocfs2_super *osb = arg;
  832. mlog_entry_void();
  833. status = ocfs2_wait_on_mount(osb);
  834. if (status < 0) {
  835. goto bail;
  836. }
  837. restart:
  838. status = ocfs2_super_lock(osb, 1);
  839. if (status < 0) {
  840. mlog_errno(status);
  841. goto bail;
  842. }
  843. while(!ocfs2_node_map_is_empty(osb, &osb->recovery_map)) {
  844. node_num = ocfs2_node_map_first_set_bit(osb,
  845. &osb->recovery_map);
  846. if (node_num == O2NM_INVALID_NODE_NUM) {
  847. mlog(0, "Out of nodes to recover.\n");
  848. break;
  849. }
  850. status = ocfs2_recover_node(osb, node_num);
  851. if (status < 0) {
  852. mlog(ML_ERROR,
  853. "Error %d recovering node %d on device (%u,%u)!\n",
  854. status, node_num,
  855. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  856. mlog(ML_ERROR, "Volume requires unmount.\n");
  857. continue;
  858. }
  859. ocfs2_recovery_map_clear(osb, node_num);
  860. }
  861. ocfs2_super_unlock(osb, 1);
  862. /* We always run recovery on our own orphan dir - the dead
  863. * node(s) may have voted "no" on an inode delete earlier. A
  864. * revote is therefore required. */
  865. ocfs2_queue_recovery_completion(osb->journal, osb->slot_num, NULL,
  866. NULL);
  867. bail:
  868. mutex_lock(&osb->recovery_lock);
  869. if (!status &&
  870. !ocfs2_node_map_is_empty(osb, &osb->recovery_map)) {
  871. mutex_unlock(&osb->recovery_lock);
  872. goto restart;
  873. }
  874. osb->recovery_thread_task = NULL;
  875. mb(); /* sync with ocfs2_recovery_thread_running */
  876. wake_up(&osb->recovery_event);
  877. mutex_unlock(&osb->recovery_lock);
  878. mlog_exit(status);
  879. /* no one is callint kthread_stop() for us so the kthread() api
  880. * requires that we call do_exit(). And it isn't exported, but
  881. * complete_and_exit() seems to be a minimal wrapper around it. */
  882. complete_and_exit(NULL, status);
  883. return status;
  884. }
  885. void ocfs2_recovery_thread(struct ocfs2_super *osb, int node_num)
  886. {
  887. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  888. node_num, osb->node_num);
  889. mutex_lock(&osb->recovery_lock);
  890. if (osb->disable_recovery)
  891. goto out;
  892. /* People waiting on recovery will wait on
  893. * the recovery map to empty. */
  894. if (!ocfs2_recovery_map_set(osb, node_num))
  895. mlog(0, "node %d already be in recovery.\n", node_num);
  896. mlog(0, "starting recovery thread...\n");
  897. if (osb->recovery_thread_task)
  898. goto out;
  899. osb->recovery_thread_task = kthread_run(__ocfs2_recovery_thread, osb,
  900. "ocfs2rec-%d", osb->osb_id);
  901. if (IS_ERR(osb->recovery_thread_task)) {
  902. mlog_errno((int)PTR_ERR(osb->recovery_thread_task));
  903. osb->recovery_thread_task = NULL;
  904. }
  905. out:
  906. mutex_unlock(&osb->recovery_lock);
  907. wake_up(&osb->recovery_event);
  908. mlog_exit_void();
  909. }
  910. /* Does the actual journal replay and marks the journal inode as
  911. * clean. Will only replay if the journal inode is marked dirty. */
  912. static int ocfs2_replay_journal(struct ocfs2_super *osb,
  913. int node_num,
  914. int slot_num)
  915. {
  916. int status;
  917. int got_lock = 0;
  918. unsigned int flags;
  919. struct inode *inode = NULL;
  920. struct ocfs2_dinode *fe;
  921. journal_t *journal = NULL;
  922. struct buffer_head *bh = NULL;
  923. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  924. slot_num);
  925. if (inode == NULL) {
  926. status = -EACCES;
  927. mlog_errno(status);
  928. goto done;
  929. }
  930. if (is_bad_inode(inode)) {
  931. status = -EACCES;
  932. iput(inode);
  933. inode = NULL;
  934. mlog_errno(status);
  935. goto done;
  936. }
  937. SET_INODE_JOURNAL(inode);
  938. status = ocfs2_meta_lock_full(inode, NULL, &bh, 1,
  939. OCFS2_META_LOCK_RECOVERY);
  940. if (status < 0) {
  941. mlog(0, "status returned from ocfs2_meta_lock=%d\n", status);
  942. if (status != -ERESTARTSYS)
  943. mlog(ML_ERROR, "Could not lock journal!\n");
  944. goto done;
  945. }
  946. got_lock = 1;
  947. fe = (struct ocfs2_dinode *) bh->b_data;
  948. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  949. if (!(flags & OCFS2_JOURNAL_DIRTY_FL)) {
  950. mlog(0, "No recovery required for node %d\n", node_num);
  951. goto done;
  952. }
  953. mlog(ML_NOTICE, "Recovering node %d from slot %d on device (%u,%u)\n",
  954. node_num, slot_num,
  955. MAJOR(osb->sb->s_dev), MINOR(osb->sb->s_dev));
  956. OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters);
  957. status = ocfs2_force_read_journal(inode);
  958. if (status < 0) {
  959. mlog_errno(status);
  960. goto done;
  961. }
  962. mlog(0, "calling journal_init_inode\n");
  963. journal = journal_init_inode(inode);
  964. if (journal == NULL) {
  965. mlog(ML_ERROR, "Linux journal layer error\n");
  966. status = -EIO;
  967. goto done;
  968. }
  969. status = journal_load(journal);
  970. if (status < 0) {
  971. mlog_errno(status);
  972. if (!igrab(inode))
  973. BUG();
  974. journal_destroy(journal);
  975. goto done;
  976. }
  977. ocfs2_clear_journal_error(osb->sb, journal, slot_num);
  978. /* wipe the journal */
  979. mlog(0, "flushing the journal.\n");
  980. journal_lock_updates(journal);
  981. status = journal_flush(journal);
  982. journal_unlock_updates(journal);
  983. if (status < 0)
  984. mlog_errno(status);
  985. /* This will mark the node clean */
  986. flags = le32_to_cpu(fe->id1.journal1.ij_flags);
  987. flags &= ~OCFS2_JOURNAL_DIRTY_FL;
  988. fe->id1.journal1.ij_flags = cpu_to_le32(flags);
  989. status = ocfs2_write_block(osb, bh, inode);
  990. if (status < 0)
  991. mlog_errno(status);
  992. if (!igrab(inode))
  993. BUG();
  994. journal_destroy(journal);
  995. done:
  996. /* drop the lock on this nodes journal */
  997. if (got_lock)
  998. ocfs2_meta_unlock(inode, 1);
  999. if (inode)
  1000. iput(inode);
  1001. if (bh)
  1002. brelse(bh);
  1003. mlog_exit(status);
  1004. return status;
  1005. }
  1006. /*
  1007. * Do the most important parts of node recovery:
  1008. * - Replay it's journal
  1009. * - Stamp a clean local allocator file
  1010. * - Stamp a clean truncate log
  1011. * - Mark the node clean
  1012. *
  1013. * If this function completes without error, a node in OCFS2 can be
  1014. * said to have been safely recovered. As a result, failure during the
  1015. * second part of a nodes recovery process (local alloc recovery) is
  1016. * far less concerning.
  1017. */
  1018. static int ocfs2_recover_node(struct ocfs2_super *osb,
  1019. int node_num)
  1020. {
  1021. int status = 0;
  1022. int slot_num;
  1023. struct ocfs2_slot_info *si = osb->slot_info;
  1024. struct ocfs2_dinode *la_copy = NULL;
  1025. struct ocfs2_dinode *tl_copy = NULL;
  1026. mlog_entry("(node_num=%d, osb->node_num = %d)\n",
  1027. node_num, osb->node_num);
  1028. mlog(0, "checking node %d\n", node_num);
  1029. /* Should not ever be called to recover ourselves -- in that
  1030. * case we should've called ocfs2_journal_load instead. */
  1031. BUG_ON(osb->node_num == node_num);
  1032. slot_num = ocfs2_node_num_to_slot(si, node_num);
  1033. if (slot_num == OCFS2_INVALID_SLOT) {
  1034. status = 0;
  1035. mlog(0, "no slot for this node, so no recovery required.\n");
  1036. goto done;
  1037. }
  1038. mlog(0, "node %d was using slot %d\n", node_num, slot_num);
  1039. status = ocfs2_replay_journal(osb, node_num, slot_num);
  1040. if (status < 0) {
  1041. mlog_errno(status);
  1042. goto done;
  1043. }
  1044. /* Stamp a clean local alloc file AFTER recovering the journal... */
  1045. status = ocfs2_begin_local_alloc_recovery(osb, slot_num, &la_copy);
  1046. if (status < 0) {
  1047. mlog_errno(status);
  1048. goto done;
  1049. }
  1050. /* An error from begin_truncate_log_recovery is not
  1051. * serious enough to warrant halting the rest of
  1052. * recovery. */
  1053. status = ocfs2_begin_truncate_log_recovery(osb, slot_num, &tl_copy);
  1054. if (status < 0)
  1055. mlog_errno(status);
  1056. /* Likewise, this would be a strange but ultimately not so
  1057. * harmful place to get an error... */
  1058. ocfs2_clear_slot(si, slot_num);
  1059. status = ocfs2_update_disk_slots(osb, si);
  1060. if (status < 0)
  1061. mlog_errno(status);
  1062. /* This will kfree the memory pointed to by la_copy and tl_copy */
  1063. ocfs2_queue_recovery_completion(osb->journal, slot_num, la_copy,
  1064. tl_copy);
  1065. status = 0;
  1066. done:
  1067. mlog_exit(status);
  1068. return status;
  1069. }
  1070. /* Test node liveness by trylocking his journal. If we get the lock,
  1071. * we drop it here. Return 0 if we got the lock, -EAGAIN if node is
  1072. * still alive (we couldn't get the lock) and < 0 on error. */
  1073. static int ocfs2_trylock_journal(struct ocfs2_super *osb,
  1074. int slot_num)
  1075. {
  1076. int status, flags;
  1077. struct inode *inode = NULL;
  1078. inode = ocfs2_get_system_file_inode(osb, JOURNAL_SYSTEM_INODE,
  1079. slot_num);
  1080. if (inode == NULL) {
  1081. mlog(ML_ERROR, "access error\n");
  1082. status = -EACCES;
  1083. goto bail;
  1084. }
  1085. if (is_bad_inode(inode)) {
  1086. mlog(ML_ERROR, "access error (bad inode)\n");
  1087. iput(inode);
  1088. inode = NULL;
  1089. status = -EACCES;
  1090. goto bail;
  1091. }
  1092. SET_INODE_JOURNAL(inode);
  1093. flags = OCFS2_META_LOCK_RECOVERY | OCFS2_META_LOCK_NOQUEUE;
  1094. status = ocfs2_meta_lock_full(inode, NULL, NULL, 1, flags);
  1095. if (status < 0) {
  1096. if (status != -EAGAIN)
  1097. mlog_errno(status);
  1098. goto bail;
  1099. }
  1100. ocfs2_meta_unlock(inode, 1);
  1101. bail:
  1102. if (inode)
  1103. iput(inode);
  1104. return status;
  1105. }
  1106. /* Call this underneath ocfs2_super_lock. It also assumes that the
  1107. * slot info struct has been updated from disk. */
  1108. int ocfs2_mark_dead_nodes(struct ocfs2_super *osb)
  1109. {
  1110. int status, i, node_num;
  1111. struct ocfs2_slot_info *si = osb->slot_info;
  1112. /* This is called with the super block cluster lock, so we
  1113. * know that the slot map can't change underneath us. */
  1114. spin_lock(&si->si_lock);
  1115. for(i = 0; i < si->si_num_slots; i++) {
  1116. if (i == osb->slot_num)
  1117. continue;
  1118. if (ocfs2_is_empty_slot(si, i))
  1119. continue;
  1120. node_num = si->si_global_node_nums[i];
  1121. if (ocfs2_node_map_test_bit(osb, &osb->recovery_map, node_num))
  1122. continue;
  1123. spin_unlock(&si->si_lock);
  1124. /* Ok, we have a slot occupied by another node which
  1125. * is not in the recovery map. We trylock his journal
  1126. * file here to test if he's alive. */
  1127. status = ocfs2_trylock_journal(osb, i);
  1128. if (!status) {
  1129. /* Since we're called from mount, we know that
  1130. * the recovery thread can't race us on
  1131. * setting / checking the recovery bits. */
  1132. ocfs2_recovery_thread(osb, node_num);
  1133. } else if ((status < 0) && (status != -EAGAIN)) {
  1134. mlog_errno(status);
  1135. goto bail;
  1136. }
  1137. spin_lock(&si->si_lock);
  1138. }
  1139. spin_unlock(&si->si_lock);
  1140. status = 0;
  1141. bail:
  1142. mlog_exit(status);
  1143. return status;
  1144. }
  1145. static int ocfs2_recover_orphans(struct ocfs2_super *osb,
  1146. int slot)
  1147. {
  1148. int status = 0;
  1149. int have_disk_lock = 0;
  1150. struct inode *inode = NULL;
  1151. struct inode *iter;
  1152. struct inode *orphan_dir_inode = NULL;
  1153. unsigned long offset, blk, local;
  1154. struct buffer_head *bh = NULL;
  1155. struct ocfs2_dir_entry *de;
  1156. struct super_block *sb = osb->sb;
  1157. struct ocfs2_inode_info *oi;
  1158. mlog(0, "Recover inodes from orphan dir in slot %d\n", slot);
  1159. orphan_dir_inode = ocfs2_get_system_file_inode(osb,
  1160. ORPHAN_DIR_SYSTEM_INODE,
  1161. slot);
  1162. if (!orphan_dir_inode) {
  1163. status = -ENOENT;
  1164. mlog_errno(status);
  1165. goto out;
  1166. }
  1167. mutex_lock(&orphan_dir_inode->i_mutex);
  1168. status = ocfs2_meta_lock(orphan_dir_inode, NULL, NULL, 0);
  1169. if (status < 0) {
  1170. mutex_unlock(&orphan_dir_inode->i_mutex);
  1171. mlog_errno(status);
  1172. goto out;
  1173. }
  1174. have_disk_lock = 1;
  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. mutex_unlock(&orphan_dir_inode->i_mutex);
  1184. if (bh)
  1185. brelse(bh);
  1186. mlog_errno(status);
  1187. goto out;
  1188. }
  1189. local = 0;
  1190. while(offset < i_size_read(orphan_dir_inode)
  1191. && local < sb->s_blocksize) {
  1192. de = (struct ocfs2_dir_entry *) (bh->b_data + local);
  1193. if (!ocfs2_check_dir_entry(orphan_dir_inode,
  1194. de, bh, local)) {
  1195. mutex_unlock(&orphan_dir_inode->i_mutex);
  1196. status = -EINVAL;
  1197. mlog_errno(status);
  1198. brelse(bh);
  1199. goto out;
  1200. }
  1201. local += le16_to_cpu(de->rec_len);
  1202. offset += le16_to_cpu(de->rec_len);
  1203. /* I guess we silently fail on no inode? */
  1204. if (!le64_to_cpu(de->inode))
  1205. continue;
  1206. if (de->file_type > OCFS2_FT_MAX) {
  1207. mlog(ML_ERROR,
  1208. "block %llu contains invalid de: "
  1209. "inode = %"MLFu64", rec_len = %u, "
  1210. "name_len = %u, file_type = %u, "
  1211. "name='%.*s'\n",
  1212. (unsigned long long)bh->b_blocknr,
  1213. le64_to_cpu(de->inode),
  1214. le16_to_cpu(de->rec_len),
  1215. de->name_len,
  1216. de->file_type,
  1217. de->name_len,
  1218. de->name);
  1219. continue;
  1220. }
  1221. if (de->name_len == 1 && !strncmp(".", de->name, 1))
  1222. continue;
  1223. if (de->name_len == 2 && !strncmp("..", de->name, 2))
  1224. continue;
  1225. iter = ocfs2_iget(osb, le64_to_cpu(de->inode));
  1226. if (IS_ERR(iter))
  1227. continue;
  1228. mlog(0, "queue orphan %"MLFu64"\n",
  1229. OCFS2_I(iter)->ip_blkno);
  1230. OCFS2_I(iter)->ip_next_orphan = inode;
  1231. inode = iter;
  1232. }
  1233. brelse(bh);
  1234. }
  1235. mutex_unlock(&orphan_dir_inode->i_mutex);
  1236. ocfs2_meta_unlock(orphan_dir_inode, 0);
  1237. have_disk_lock = 0;
  1238. iput(orphan_dir_inode);
  1239. orphan_dir_inode = NULL;
  1240. while (inode) {
  1241. oi = OCFS2_I(inode);
  1242. mlog(0, "iput orphan %"MLFu64"\n", oi->ip_blkno);
  1243. iter = oi->ip_next_orphan;
  1244. spin_lock(&oi->ip_lock);
  1245. /* Delete voting may have set these on the assumption
  1246. * that the other node would wipe them successfully.
  1247. * If they are still in the node's orphan dir, we need
  1248. * to reset that state. */
  1249. oi->ip_flags &= ~(OCFS2_INODE_DELETED|OCFS2_INODE_SKIP_DELETE);
  1250. /* Set the proper information to get us going into
  1251. * ocfs2_delete_inode. */
  1252. oi->ip_flags |= OCFS2_INODE_MAYBE_ORPHANED;
  1253. oi->ip_orphaned_slot = slot;
  1254. spin_unlock(&oi->ip_lock);
  1255. iput(inode);
  1256. inode = iter;
  1257. }
  1258. out:
  1259. if (have_disk_lock)
  1260. ocfs2_meta_unlock(orphan_dir_inode, 0);
  1261. if (orphan_dir_inode)
  1262. iput(orphan_dir_inode);
  1263. return status;
  1264. }
  1265. static int ocfs2_wait_on_mount(struct ocfs2_super *osb)
  1266. {
  1267. /* This check is good because ocfs2 will wait on our recovery
  1268. * thread before changing it to something other than MOUNTED
  1269. * or DISABLED. */
  1270. wait_event(osb->osb_mount_event,
  1271. atomic_read(&osb->vol_state) == VOLUME_MOUNTED ||
  1272. atomic_read(&osb->vol_state) == VOLUME_DISABLED);
  1273. /* If there's an error on mount, then we may never get to the
  1274. * MOUNTED flag, but this is set right before
  1275. * dismount_volume() so we can trust it. */
  1276. if (atomic_read(&osb->vol_state) == VOLUME_DISABLED) {
  1277. mlog(0, "mount error, exiting!\n");
  1278. return -EBUSY;
  1279. }
  1280. return 0;
  1281. }
  1282. static int ocfs2_commit_thread(void *arg)
  1283. {
  1284. int status;
  1285. struct ocfs2_super *osb = arg;
  1286. struct ocfs2_journal *journal = osb->journal;
  1287. /* we can trust j_num_trans here because _should_stop() is only set in
  1288. * shutdown and nobody other than ourselves should be able to start
  1289. * transactions. committing on shutdown might take a few iterations
  1290. * as final transactions put deleted inodes on the list */
  1291. while (!(kthread_should_stop() &&
  1292. atomic_read(&journal->j_num_trans) == 0)) {
  1293. wait_event_interruptible_timeout(osb->checkpoint_event,
  1294. atomic_read(&journal->j_num_trans)
  1295. || kthread_should_stop(),
  1296. OCFS2_CHECKPOINT_INTERVAL);
  1297. status = ocfs2_commit_cache(osb);
  1298. if (status < 0)
  1299. mlog_errno(status);
  1300. if (kthread_should_stop() && atomic_read(&journal->j_num_trans)){
  1301. mlog(ML_KTHREAD,
  1302. "commit_thread: %u transactions pending on "
  1303. "shutdown\n",
  1304. atomic_read(&journal->j_num_trans));
  1305. }
  1306. }
  1307. return 0;
  1308. }
  1309. /* Look for a dirty journal without taking any cluster locks. Used for
  1310. * hard readonly access to determine whether the file system journals
  1311. * require recovery. */
  1312. int ocfs2_check_journals_nolocks(struct ocfs2_super *osb)
  1313. {
  1314. int ret = 0;
  1315. unsigned int slot;
  1316. struct buffer_head *di_bh;
  1317. struct ocfs2_dinode *di;
  1318. struct inode *journal = NULL;
  1319. for(slot = 0; slot < osb->max_slots; slot++) {
  1320. journal = ocfs2_get_system_file_inode(osb,
  1321. JOURNAL_SYSTEM_INODE,
  1322. slot);
  1323. if (!journal || is_bad_inode(journal)) {
  1324. ret = -EACCES;
  1325. mlog_errno(ret);
  1326. goto out;
  1327. }
  1328. di_bh = NULL;
  1329. ret = ocfs2_read_block(osb, OCFS2_I(journal)->ip_blkno, &di_bh,
  1330. 0, journal);
  1331. if (ret < 0) {
  1332. mlog_errno(ret);
  1333. goto out;
  1334. }
  1335. di = (struct ocfs2_dinode *) di_bh->b_data;
  1336. if (le32_to_cpu(di->id1.journal1.ij_flags) &
  1337. OCFS2_JOURNAL_DIRTY_FL)
  1338. ret = -EROFS;
  1339. brelse(di_bh);
  1340. if (ret)
  1341. break;
  1342. }
  1343. out:
  1344. if (journal)
  1345. iput(journal);
  1346. return ret;
  1347. }