journal.c 38 KB

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