journal.c 37 KB

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