journal.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  1. /*
  2. * linux/fs/jbd2/journal.c
  3. *
  4. * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
  5. *
  6. * Copyright 1998 Red Hat corp --- All Rights Reserved
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * Generic filesystem journal-writing code; part of the ext2fs
  13. * journaling system.
  14. *
  15. * This file manages journals: areas of disk reserved for logging
  16. * transactional updates. This includes the kernel journaling thread
  17. * which is responsible for scheduling updates to the log.
  18. *
  19. * We do not actually manage the physical storage of the journal in this
  20. * file: that is left to a per-journal policy function, which allows us
  21. * to store the journal within a filesystem-specified area for ext2
  22. * journaling (ext2 can use a reserved inode for storing the log).
  23. */
  24. #include <linux/module.h>
  25. #include <linux/time.h>
  26. #include <linux/fs.h>
  27. #include <linux/jbd2.h>
  28. #include <linux/errno.h>
  29. #include <linux/slab.h>
  30. #include <linux/init.h>
  31. #include <linux/mm.h>
  32. #include <linux/freezer.h>
  33. #include <linux/pagemap.h>
  34. #include <linux/kthread.h>
  35. #include <linux/poison.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/seq_file.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/page.h>
  41. EXPORT_SYMBOL(jbd2_journal_start);
  42. EXPORT_SYMBOL(jbd2_journal_restart);
  43. EXPORT_SYMBOL(jbd2_journal_extend);
  44. EXPORT_SYMBOL(jbd2_journal_stop);
  45. EXPORT_SYMBOL(jbd2_journal_lock_updates);
  46. EXPORT_SYMBOL(jbd2_journal_unlock_updates);
  47. EXPORT_SYMBOL(jbd2_journal_get_write_access);
  48. EXPORT_SYMBOL(jbd2_journal_get_create_access);
  49. EXPORT_SYMBOL(jbd2_journal_get_undo_access);
  50. EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
  51. EXPORT_SYMBOL(jbd2_journal_release_buffer);
  52. EXPORT_SYMBOL(jbd2_journal_forget);
  53. #if 0
  54. EXPORT_SYMBOL(journal_sync_buffer);
  55. #endif
  56. EXPORT_SYMBOL(jbd2_journal_flush);
  57. EXPORT_SYMBOL(jbd2_journal_revoke);
  58. EXPORT_SYMBOL(jbd2_journal_init_dev);
  59. EXPORT_SYMBOL(jbd2_journal_init_inode);
  60. EXPORT_SYMBOL(jbd2_journal_update_format);
  61. EXPORT_SYMBOL(jbd2_journal_check_used_features);
  62. EXPORT_SYMBOL(jbd2_journal_check_available_features);
  63. EXPORT_SYMBOL(jbd2_journal_set_features);
  64. EXPORT_SYMBOL(jbd2_journal_create);
  65. EXPORT_SYMBOL(jbd2_journal_load);
  66. EXPORT_SYMBOL(jbd2_journal_destroy);
  67. EXPORT_SYMBOL(jbd2_journal_abort);
  68. EXPORT_SYMBOL(jbd2_journal_errno);
  69. EXPORT_SYMBOL(jbd2_journal_ack_err);
  70. EXPORT_SYMBOL(jbd2_journal_clear_err);
  71. EXPORT_SYMBOL(jbd2_log_wait_commit);
  72. EXPORT_SYMBOL(jbd2_journal_start_commit);
  73. EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
  74. EXPORT_SYMBOL(jbd2_journal_wipe);
  75. EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
  76. EXPORT_SYMBOL(jbd2_journal_invalidatepage);
  77. EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
  78. EXPORT_SYMBOL(jbd2_journal_force_commit);
  79. EXPORT_SYMBOL(jbd2_journal_file_inode);
  80. EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
  81. EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
  82. EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
  83. static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
  84. static void __journal_abort_soft (journal_t *journal, int errno);
  85. /*
  86. * Helper function used to manage commit timeouts
  87. */
  88. static void commit_timeout(unsigned long __data)
  89. {
  90. struct task_struct * p = (struct task_struct *) __data;
  91. wake_up_process(p);
  92. }
  93. /*
  94. * kjournald2: The main thread function used to manage a logging device
  95. * journal.
  96. *
  97. * This kernel thread is responsible for two things:
  98. *
  99. * 1) COMMIT: Every so often we need to commit the current state of the
  100. * filesystem to disk. The journal thread is responsible for writing
  101. * all of the metadata buffers to disk.
  102. *
  103. * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
  104. * of the data in that part of the log has been rewritten elsewhere on
  105. * the disk. Flushing these old buffers to reclaim space in the log is
  106. * known as checkpointing, and this thread is responsible for that job.
  107. */
  108. static int kjournald2(void *arg)
  109. {
  110. journal_t *journal = arg;
  111. transaction_t *transaction;
  112. /*
  113. * Set up an interval timer which can be used to trigger a commit wakeup
  114. * after the commit interval expires
  115. */
  116. setup_timer(&journal->j_commit_timer, commit_timeout,
  117. (unsigned long)current);
  118. /* Record that the journal thread is running */
  119. journal->j_task = current;
  120. wake_up(&journal->j_wait_done_commit);
  121. printk(KERN_INFO "kjournald2 starting. Commit interval %ld seconds\n",
  122. journal->j_commit_interval / HZ);
  123. /*
  124. * And now, wait forever for commit wakeup events.
  125. */
  126. spin_lock(&journal->j_state_lock);
  127. loop:
  128. if (journal->j_flags & JBD2_UNMOUNT)
  129. goto end_loop;
  130. jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
  131. journal->j_commit_sequence, journal->j_commit_request);
  132. if (journal->j_commit_sequence != journal->j_commit_request) {
  133. jbd_debug(1, "OK, requests differ\n");
  134. spin_unlock(&journal->j_state_lock);
  135. del_timer_sync(&journal->j_commit_timer);
  136. jbd2_journal_commit_transaction(journal);
  137. spin_lock(&journal->j_state_lock);
  138. goto loop;
  139. }
  140. wake_up(&journal->j_wait_done_commit);
  141. if (freezing(current)) {
  142. /*
  143. * The simpler the better. Flushing journal isn't a
  144. * good idea, because that depends on threads that may
  145. * be already stopped.
  146. */
  147. jbd_debug(1, "Now suspending kjournald2\n");
  148. spin_unlock(&journal->j_state_lock);
  149. refrigerator();
  150. spin_lock(&journal->j_state_lock);
  151. } else {
  152. /*
  153. * We assume on resume that commits are already there,
  154. * so we don't sleep
  155. */
  156. DEFINE_WAIT(wait);
  157. int should_sleep = 1;
  158. prepare_to_wait(&journal->j_wait_commit, &wait,
  159. TASK_INTERRUPTIBLE);
  160. if (journal->j_commit_sequence != journal->j_commit_request)
  161. should_sleep = 0;
  162. transaction = journal->j_running_transaction;
  163. if (transaction && time_after_eq(jiffies,
  164. transaction->t_expires))
  165. should_sleep = 0;
  166. if (journal->j_flags & JBD2_UNMOUNT)
  167. should_sleep = 0;
  168. if (should_sleep) {
  169. spin_unlock(&journal->j_state_lock);
  170. schedule();
  171. spin_lock(&journal->j_state_lock);
  172. }
  173. finish_wait(&journal->j_wait_commit, &wait);
  174. }
  175. jbd_debug(1, "kjournald2 wakes\n");
  176. /*
  177. * Were we woken up by a commit wakeup event?
  178. */
  179. transaction = journal->j_running_transaction;
  180. if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
  181. journal->j_commit_request = transaction->t_tid;
  182. jbd_debug(1, "woke because of timeout\n");
  183. }
  184. goto loop;
  185. end_loop:
  186. spin_unlock(&journal->j_state_lock);
  187. del_timer_sync(&journal->j_commit_timer);
  188. journal->j_task = NULL;
  189. wake_up(&journal->j_wait_done_commit);
  190. jbd_debug(1, "Journal thread exiting.\n");
  191. return 0;
  192. }
  193. static int jbd2_journal_start_thread(journal_t *journal)
  194. {
  195. struct task_struct *t;
  196. t = kthread_run(kjournald2, journal, "kjournald2");
  197. if (IS_ERR(t))
  198. return PTR_ERR(t);
  199. wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
  200. return 0;
  201. }
  202. static void journal_kill_thread(journal_t *journal)
  203. {
  204. spin_lock(&journal->j_state_lock);
  205. journal->j_flags |= JBD2_UNMOUNT;
  206. while (journal->j_task) {
  207. wake_up(&journal->j_wait_commit);
  208. spin_unlock(&journal->j_state_lock);
  209. wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
  210. spin_lock(&journal->j_state_lock);
  211. }
  212. spin_unlock(&journal->j_state_lock);
  213. }
  214. /*
  215. * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
  216. *
  217. * Writes a metadata buffer to a given disk block. The actual IO is not
  218. * performed but a new buffer_head is constructed which labels the data
  219. * to be written with the correct destination disk block.
  220. *
  221. * Any magic-number escaping which needs to be done will cause a
  222. * copy-out here. If the buffer happens to start with the
  223. * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
  224. * magic number is only written to the log for descripter blocks. In
  225. * this case, we copy the data and replace the first word with 0, and we
  226. * return a result code which indicates that this buffer needs to be
  227. * marked as an escaped buffer in the corresponding log descriptor
  228. * block. The missing word can then be restored when the block is read
  229. * during recovery.
  230. *
  231. * If the source buffer has already been modified by a new transaction
  232. * since we took the last commit snapshot, we use the frozen copy of
  233. * that data for IO. If we end up using the existing buffer_head's data
  234. * for the write, then we *have* to lock the buffer to prevent anyone
  235. * else from using and possibly modifying it while the IO is in
  236. * progress.
  237. *
  238. * The function returns a pointer to the buffer_heads to be used for IO.
  239. *
  240. * We assume that the journal has already been locked in this function.
  241. *
  242. * Return value:
  243. * <0: Error
  244. * >=0: Finished OK
  245. *
  246. * On success:
  247. * Bit 0 set == escape performed on the data
  248. * Bit 1 set == buffer copy-out performed (kfree the data after IO)
  249. */
  250. int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
  251. struct journal_head *jh_in,
  252. struct journal_head **jh_out,
  253. unsigned long long blocknr)
  254. {
  255. int need_copy_out = 0;
  256. int done_copy_out = 0;
  257. int do_escape = 0;
  258. char *mapped_data;
  259. struct buffer_head *new_bh;
  260. struct journal_head *new_jh;
  261. struct page *new_page;
  262. unsigned int new_offset;
  263. struct buffer_head *bh_in = jh2bh(jh_in);
  264. /*
  265. * The buffer really shouldn't be locked: only the current committing
  266. * transaction is allowed to write it, so nobody else is allowed
  267. * to do any IO.
  268. *
  269. * akpm: except if we're journalling data, and write() output is
  270. * also part of a shared mapping, and another thread has
  271. * decided to launch a writepage() against this buffer.
  272. */
  273. J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
  274. new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
  275. /*
  276. * If a new transaction has already done a buffer copy-out, then
  277. * we use that version of the data for the commit.
  278. */
  279. jbd_lock_bh_state(bh_in);
  280. repeat:
  281. if (jh_in->b_frozen_data) {
  282. done_copy_out = 1;
  283. new_page = virt_to_page(jh_in->b_frozen_data);
  284. new_offset = offset_in_page(jh_in->b_frozen_data);
  285. } else {
  286. new_page = jh2bh(jh_in)->b_page;
  287. new_offset = offset_in_page(jh2bh(jh_in)->b_data);
  288. }
  289. mapped_data = kmap_atomic(new_page, KM_USER0);
  290. /*
  291. * Check for escaping
  292. */
  293. if (*((__be32 *)(mapped_data + new_offset)) ==
  294. cpu_to_be32(JBD2_MAGIC_NUMBER)) {
  295. need_copy_out = 1;
  296. do_escape = 1;
  297. }
  298. kunmap_atomic(mapped_data, KM_USER0);
  299. /*
  300. * Do we need to do a data copy?
  301. */
  302. if (need_copy_out && !done_copy_out) {
  303. char *tmp;
  304. jbd_unlock_bh_state(bh_in);
  305. tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
  306. jbd_lock_bh_state(bh_in);
  307. if (jh_in->b_frozen_data) {
  308. jbd2_free(tmp, bh_in->b_size);
  309. goto repeat;
  310. }
  311. jh_in->b_frozen_data = tmp;
  312. mapped_data = kmap_atomic(new_page, KM_USER0);
  313. memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
  314. kunmap_atomic(mapped_data, KM_USER0);
  315. new_page = virt_to_page(tmp);
  316. new_offset = offset_in_page(tmp);
  317. done_copy_out = 1;
  318. }
  319. /*
  320. * Did we need to do an escaping? Now we've done all the
  321. * copying, we can finally do so.
  322. */
  323. if (do_escape) {
  324. mapped_data = kmap_atomic(new_page, KM_USER0);
  325. *((unsigned int *)(mapped_data + new_offset)) = 0;
  326. kunmap_atomic(mapped_data, KM_USER0);
  327. }
  328. /* keep subsequent assertions sane */
  329. new_bh->b_state = 0;
  330. init_buffer(new_bh, NULL, NULL);
  331. atomic_set(&new_bh->b_count, 1);
  332. jbd_unlock_bh_state(bh_in);
  333. new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
  334. set_bh_page(new_bh, new_page, new_offset);
  335. new_jh->b_transaction = NULL;
  336. new_bh->b_size = jh2bh(jh_in)->b_size;
  337. new_bh->b_bdev = transaction->t_journal->j_dev;
  338. new_bh->b_blocknr = blocknr;
  339. set_buffer_mapped(new_bh);
  340. set_buffer_dirty(new_bh);
  341. *jh_out = new_jh;
  342. /*
  343. * The to-be-written buffer needs to get moved to the io queue,
  344. * and the original buffer whose contents we are shadowing or
  345. * copying is moved to the transaction's shadow queue.
  346. */
  347. JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
  348. jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
  349. JBUFFER_TRACE(new_jh, "file as BJ_IO");
  350. jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
  351. return do_escape | (done_copy_out << 1);
  352. }
  353. /*
  354. * Allocation code for the journal file. Manage the space left in the
  355. * journal, so that we can begin checkpointing when appropriate.
  356. */
  357. /*
  358. * __jbd2_log_space_left: Return the number of free blocks left in the journal.
  359. *
  360. * Called with the journal already locked.
  361. *
  362. * Called under j_state_lock
  363. */
  364. int __jbd2_log_space_left(journal_t *journal)
  365. {
  366. int left = journal->j_free;
  367. assert_spin_locked(&journal->j_state_lock);
  368. /*
  369. * Be pessimistic here about the number of those free blocks which
  370. * might be required for log descriptor control blocks.
  371. */
  372. #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
  373. left -= MIN_LOG_RESERVED_BLOCKS;
  374. if (left <= 0)
  375. return 0;
  376. left -= (left >> 3);
  377. return left;
  378. }
  379. /*
  380. * Called under j_state_lock. Returns true if a transaction was started.
  381. */
  382. int __jbd2_log_start_commit(journal_t *journal, tid_t target)
  383. {
  384. /*
  385. * Are we already doing a recent enough commit?
  386. */
  387. if (!tid_geq(journal->j_commit_request, target)) {
  388. /*
  389. * We want a new commit: OK, mark the request and wakup the
  390. * commit thread. We do _not_ do the commit ourselves.
  391. */
  392. journal->j_commit_request = target;
  393. jbd_debug(1, "JBD: requesting commit %d/%d\n",
  394. journal->j_commit_request,
  395. journal->j_commit_sequence);
  396. wake_up(&journal->j_wait_commit);
  397. return 1;
  398. }
  399. return 0;
  400. }
  401. int jbd2_log_start_commit(journal_t *journal, tid_t tid)
  402. {
  403. int ret;
  404. spin_lock(&journal->j_state_lock);
  405. ret = __jbd2_log_start_commit(journal, tid);
  406. spin_unlock(&journal->j_state_lock);
  407. return ret;
  408. }
  409. /*
  410. * Force and wait upon a commit if the calling process is not within
  411. * transaction. This is used for forcing out undo-protected data which contains
  412. * bitmaps, when the fs is running out of space.
  413. *
  414. * We can only force the running transaction if we don't have an active handle;
  415. * otherwise, we will deadlock.
  416. *
  417. * Returns true if a transaction was started.
  418. */
  419. int jbd2_journal_force_commit_nested(journal_t *journal)
  420. {
  421. transaction_t *transaction = NULL;
  422. tid_t tid;
  423. spin_lock(&journal->j_state_lock);
  424. if (journal->j_running_transaction && !current->journal_info) {
  425. transaction = journal->j_running_transaction;
  426. __jbd2_log_start_commit(journal, transaction->t_tid);
  427. } else if (journal->j_committing_transaction)
  428. transaction = journal->j_committing_transaction;
  429. if (!transaction) {
  430. spin_unlock(&journal->j_state_lock);
  431. return 0; /* Nothing to retry */
  432. }
  433. tid = transaction->t_tid;
  434. spin_unlock(&journal->j_state_lock);
  435. jbd2_log_wait_commit(journal, tid);
  436. return 1;
  437. }
  438. /*
  439. * Start a commit of the current running transaction (if any). Returns true
  440. * if a transaction was started, and fills its tid in at *ptid
  441. */
  442. int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
  443. {
  444. int ret = 0;
  445. spin_lock(&journal->j_state_lock);
  446. if (journal->j_running_transaction) {
  447. tid_t tid = journal->j_running_transaction->t_tid;
  448. ret = __jbd2_log_start_commit(journal, tid);
  449. if (ret && ptid)
  450. *ptid = tid;
  451. } else if (journal->j_committing_transaction && ptid) {
  452. /*
  453. * If ext3_write_super() recently started a commit, then we
  454. * have to wait for completion of that transaction
  455. */
  456. *ptid = journal->j_committing_transaction->t_tid;
  457. ret = 1;
  458. }
  459. spin_unlock(&journal->j_state_lock);
  460. return ret;
  461. }
  462. /*
  463. * Wait for a specified commit to complete.
  464. * The caller may not hold the journal lock.
  465. */
  466. int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
  467. {
  468. int err = 0;
  469. #ifdef CONFIG_JBD2_DEBUG
  470. spin_lock(&journal->j_state_lock);
  471. if (!tid_geq(journal->j_commit_request, tid)) {
  472. printk(KERN_EMERG
  473. "%s: error: j_commit_request=%d, tid=%d\n",
  474. __func__, journal->j_commit_request, tid);
  475. }
  476. spin_unlock(&journal->j_state_lock);
  477. #endif
  478. spin_lock(&journal->j_state_lock);
  479. while (tid_gt(tid, journal->j_commit_sequence)) {
  480. jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
  481. tid, journal->j_commit_sequence);
  482. wake_up(&journal->j_wait_commit);
  483. spin_unlock(&journal->j_state_lock);
  484. wait_event(journal->j_wait_done_commit,
  485. !tid_gt(tid, journal->j_commit_sequence));
  486. spin_lock(&journal->j_state_lock);
  487. }
  488. spin_unlock(&journal->j_state_lock);
  489. if (unlikely(is_journal_aborted(journal))) {
  490. printk(KERN_EMERG "journal commit I/O error\n");
  491. err = -EIO;
  492. }
  493. return err;
  494. }
  495. /*
  496. * Log buffer allocation routines:
  497. */
  498. int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
  499. {
  500. unsigned long blocknr;
  501. spin_lock(&journal->j_state_lock);
  502. J_ASSERT(journal->j_free > 1);
  503. blocknr = journal->j_head;
  504. journal->j_head++;
  505. journal->j_free--;
  506. if (journal->j_head == journal->j_last)
  507. journal->j_head = journal->j_first;
  508. spin_unlock(&journal->j_state_lock);
  509. return jbd2_journal_bmap(journal, blocknr, retp);
  510. }
  511. /*
  512. * Conversion of logical to physical block numbers for the journal
  513. *
  514. * On external journals the journal blocks are identity-mapped, so
  515. * this is a no-op. If needed, we can use j_blk_offset - everything is
  516. * ready.
  517. */
  518. int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
  519. unsigned long long *retp)
  520. {
  521. int err = 0;
  522. unsigned long long ret;
  523. if (journal->j_inode) {
  524. ret = bmap(journal->j_inode, blocknr);
  525. if (ret)
  526. *retp = ret;
  527. else {
  528. char b[BDEVNAME_SIZE];
  529. printk(KERN_ALERT "%s: journal block not found "
  530. "at offset %lu on %s\n",
  531. __func__,
  532. blocknr,
  533. bdevname(journal->j_dev, b));
  534. err = -EIO;
  535. __journal_abort_soft(journal, err);
  536. }
  537. } else {
  538. *retp = blocknr; /* +journal->j_blk_offset */
  539. }
  540. return err;
  541. }
  542. /*
  543. * We play buffer_head aliasing tricks to write data/metadata blocks to
  544. * the journal without copying their contents, but for journal
  545. * descriptor blocks we do need to generate bona fide buffers.
  546. *
  547. * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
  548. * the buffer's contents they really should run flush_dcache_page(bh->b_page).
  549. * But we don't bother doing that, so there will be coherency problems with
  550. * mmaps of blockdevs which hold live JBD-controlled filesystems.
  551. */
  552. struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
  553. {
  554. struct buffer_head *bh;
  555. unsigned long long blocknr;
  556. int err;
  557. err = jbd2_journal_next_log_block(journal, &blocknr);
  558. if (err)
  559. return NULL;
  560. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  561. lock_buffer(bh);
  562. memset(bh->b_data, 0, journal->j_blocksize);
  563. set_buffer_uptodate(bh);
  564. unlock_buffer(bh);
  565. BUFFER_TRACE(bh, "return this buffer");
  566. return jbd2_journal_add_journal_head(bh);
  567. }
  568. struct jbd2_stats_proc_session {
  569. journal_t *journal;
  570. struct transaction_stats_s *stats;
  571. int start;
  572. int max;
  573. };
  574. static void *jbd2_history_skip_empty(struct jbd2_stats_proc_session *s,
  575. struct transaction_stats_s *ts,
  576. int first)
  577. {
  578. if (ts == s->stats + s->max)
  579. ts = s->stats;
  580. if (!first && ts == s->stats + s->start)
  581. return NULL;
  582. while (ts->ts_type == 0) {
  583. ts++;
  584. if (ts == s->stats + s->max)
  585. ts = s->stats;
  586. if (ts == s->stats + s->start)
  587. return NULL;
  588. }
  589. return ts;
  590. }
  591. static void *jbd2_seq_history_start(struct seq_file *seq, loff_t *pos)
  592. {
  593. struct jbd2_stats_proc_session *s = seq->private;
  594. struct transaction_stats_s *ts;
  595. int l = *pos;
  596. if (l == 0)
  597. return SEQ_START_TOKEN;
  598. ts = jbd2_history_skip_empty(s, s->stats + s->start, 1);
  599. if (!ts)
  600. return NULL;
  601. l--;
  602. while (l) {
  603. ts = jbd2_history_skip_empty(s, ++ts, 0);
  604. if (!ts)
  605. break;
  606. l--;
  607. }
  608. return ts;
  609. }
  610. static void *jbd2_seq_history_next(struct seq_file *seq, void *v, loff_t *pos)
  611. {
  612. struct jbd2_stats_proc_session *s = seq->private;
  613. struct transaction_stats_s *ts = v;
  614. ++*pos;
  615. if (v == SEQ_START_TOKEN)
  616. return jbd2_history_skip_empty(s, s->stats + s->start, 1);
  617. else
  618. return jbd2_history_skip_empty(s, ++ts, 0);
  619. }
  620. static int jbd2_seq_history_show(struct seq_file *seq, void *v)
  621. {
  622. struct transaction_stats_s *ts = v;
  623. if (v == SEQ_START_TOKEN) {
  624. seq_printf(seq, "%-4s %-5s %-5s %-5s %-5s %-5s %-5s %-6s %-5s "
  625. "%-5s %-5s %-5s %-5s %-5s\n", "R/C", "tid",
  626. "wait", "run", "lock", "flush", "log", "hndls",
  627. "block", "inlog", "ctime", "write", "drop",
  628. "close");
  629. return 0;
  630. }
  631. if (ts->ts_type == JBD2_STATS_RUN)
  632. seq_printf(seq, "%-4s %-5lu %-5u %-5u %-5u %-5u %-5u "
  633. "%-6lu %-5lu %-5lu\n", "R", ts->ts_tid,
  634. jiffies_to_msecs(ts->u.run.rs_wait),
  635. jiffies_to_msecs(ts->u.run.rs_running),
  636. jiffies_to_msecs(ts->u.run.rs_locked),
  637. jiffies_to_msecs(ts->u.run.rs_flushing),
  638. jiffies_to_msecs(ts->u.run.rs_logging),
  639. ts->u.run.rs_handle_count,
  640. ts->u.run.rs_blocks,
  641. ts->u.run.rs_blocks_logged);
  642. else if (ts->ts_type == JBD2_STATS_CHECKPOINT)
  643. seq_printf(seq, "%-4s %-5lu %48s %-5u %-5lu %-5lu %-5lu\n",
  644. "C", ts->ts_tid, " ",
  645. jiffies_to_msecs(ts->u.chp.cs_chp_time),
  646. ts->u.chp.cs_written, ts->u.chp.cs_dropped,
  647. ts->u.chp.cs_forced_to_close);
  648. else
  649. J_ASSERT(0);
  650. return 0;
  651. }
  652. static void jbd2_seq_history_stop(struct seq_file *seq, void *v)
  653. {
  654. }
  655. static struct seq_operations jbd2_seq_history_ops = {
  656. .start = jbd2_seq_history_start,
  657. .next = jbd2_seq_history_next,
  658. .stop = jbd2_seq_history_stop,
  659. .show = jbd2_seq_history_show,
  660. };
  661. static int jbd2_seq_history_open(struct inode *inode, struct file *file)
  662. {
  663. journal_t *journal = PDE(inode)->data;
  664. struct jbd2_stats_proc_session *s;
  665. int rc, size;
  666. s = kmalloc(sizeof(*s), GFP_KERNEL);
  667. if (s == NULL)
  668. return -ENOMEM;
  669. size = sizeof(struct transaction_stats_s) * journal->j_history_max;
  670. s->stats = kmalloc(size, GFP_KERNEL);
  671. if (s->stats == NULL) {
  672. kfree(s);
  673. return -ENOMEM;
  674. }
  675. spin_lock(&journal->j_history_lock);
  676. memcpy(s->stats, journal->j_history, size);
  677. s->max = journal->j_history_max;
  678. s->start = journal->j_history_cur % s->max;
  679. spin_unlock(&journal->j_history_lock);
  680. rc = seq_open(file, &jbd2_seq_history_ops);
  681. if (rc == 0) {
  682. struct seq_file *m = file->private_data;
  683. m->private = s;
  684. } else {
  685. kfree(s->stats);
  686. kfree(s);
  687. }
  688. return rc;
  689. }
  690. static int jbd2_seq_history_release(struct inode *inode, struct file *file)
  691. {
  692. struct seq_file *seq = file->private_data;
  693. struct jbd2_stats_proc_session *s = seq->private;
  694. kfree(s->stats);
  695. kfree(s);
  696. return seq_release(inode, file);
  697. }
  698. static struct file_operations jbd2_seq_history_fops = {
  699. .owner = THIS_MODULE,
  700. .open = jbd2_seq_history_open,
  701. .read = seq_read,
  702. .llseek = seq_lseek,
  703. .release = jbd2_seq_history_release,
  704. };
  705. static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
  706. {
  707. return *pos ? NULL : SEQ_START_TOKEN;
  708. }
  709. static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
  710. {
  711. return NULL;
  712. }
  713. static int jbd2_seq_info_show(struct seq_file *seq, void *v)
  714. {
  715. struct jbd2_stats_proc_session *s = seq->private;
  716. if (v != SEQ_START_TOKEN)
  717. return 0;
  718. seq_printf(seq, "%lu transaction, each upto %u blocks\n",
  719. s->stats->ts_tid,
  720. s->journal->j_max_transaction_buffers);
  721. if (s->stats->ts_tid == 0)
  722. return 0;
  723. seq_printf(seq, "average: \n %ums waiting for transaction\n",
  724. jiffies_to_msecs(s->stats->u.run.rs_wait / s->stats->ts_tid));
  725. seq_printf(seq, " %ums running transaction\n",
  726. jiffies_to_msecs(s->stats->u.run.rs_running / s->stats->ts_tid));
  727. seq_printf(seq, " %ums transaction was being locked\n",
  728. jiffies_to_msecs(s->stats->u.run.rs_locked / s->stats->ts_tid));
  729. seq_printf(seq, " %ums flushing data (in ordered mode)\n",
  730. jiffies_to_msecs(s->stats->u.run.rs_flushing / s->stats->ts_tid));
  731. seq_printf(seq, " %ums logging transaction\n",
  732. jiffies_to_msecs(s->stats->u.run.rs_logging / s->stats->ts_tid));
  733. seq_printf(seq, " %lu handles per transaction\n",
  734. s->stats->u.run.rs_handle_count / s->stats->ts_tid);
  735. seq_printf(seq, " %lu blocks per transaction\n",
  736. s->stats->u.run.rs_blocks / s->stats->ts_tid);
  737. seq_printf(seq, " %lu logged blocks per transaction\n",
  738. s->stats->u.run.rs_blocks_logged / s->stats->ts_tid);
  739. return 0;
  740. }
  741. static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
  742. {
  743. }
  744. static struct seq_operations jbd2_seq_info_ops = {
  745. .start = jbd2_seq_info_start,
  746. .next = jbd2_seq_info_next,
  747. .stop = jbd2_seq_info_stop,
  748. .show = jbd2_seq_info_show,
  749. };
  750. static int jbd2_seq_info_open(struct inode *inode, struct file *file)
  751. {
  752. journal_t *journal = PDE(inode)->data;
  753. struct jbd2_stats_proc_session *s;
  754. int rc, size;
  755. s = kmalloc(sizeof(*s), GFP_KERNEL);
  756. if (s == NULL)
  757. return -ENOMEM;
  758. size = sizeof(struct transaction_stats_s);
  759. s->stats = kmalloc(size, GFP_KERNEL);
  760. if (s->stats == NULL) {
  761. kfree(s);
  762. return -ENOMEM;
  763. }
  764. spin_lock(&journal->j_history_lock);
  765. memcpy(s->stats, &journal->j_stats, size);
  766. s->journal = journal;
  767. spin_unlock(&journal->j_history_lock);
  768. rc = seq_open(file, &jbd2_seq_info_ops);
  769. if (rc == 0) {
  770. struct seq_file *m = file->private_data;
  771. m->private = s;
  772. } else {
  773. kfree(s->stats);
  774. kfree(s);
  775. }
  776. return rc;
  777. }
  778. static int jbd2_seq_info_release(struct inode *inode, struct file *file)
  779. {
  780. struct seq_file *seq = file->private_data;
  781. struct jbd2_stats_proc_session *s = seq->private;
  782. kfree(s->stats);
  783. kfree(s);
  784. return seq_release(inode, file);
  785. }
  786. static struct file_operations jbd2_seq_info_fops = {
  787. .owner = THIS_MODULE,
  788. .open = jbd2_seq_info_open,
  789. .read = seq_read,
  790. .llseek = seq_lseek,
  791. .release = jbd2_seq_info_release,
  792. };
  793. static struct proc_dir_entry *proc_jbd2_stats;
  794. static void jbd2_stats_proc_init(journal_t *journal)
  795. {
  796. char name[BDEVNAME_SIZE];
  797. bdevname(journal->j_dev, name);
  798. journal->j_proc_entry = proc_mkdir(name, proc_jbd2_stats);
  799. if (journal->j_proc_entry) {
  800. proc_create_data("history", S_IRUGO, journal->j_proc_entry,
  801. &jbd2_seq_history_fops, journal);
  802. proc_create_data("info", S_IRUGO, journal->j_proc_entry,
  803. &jbd2_seq_info_fops, journal);
  804. }
  805. }
  806. static void jbd2_stats_proc_exit(journal_t *journal)
  807. {
  808. char name[BDEVNAME_SIZE];
  809. bdevname(journal->j_dev, name);
  810. remove_proc_entry("info", journal->j_proc_entry);
  811. remove_proc_entry("history", journal->j_proc_entry);
  812. remove_proc_entry(name, proc_jbd2_stats);
  813. }
  814. static void journal_init_stats(journal_t *journal)
  815. {
  816. int size;
  817. if (!proc_jbd2_stats)
  818. return;
  819. journal->j_history_max = 100;
  820. size = sizeof(struct transaction_stats_s) * journal->j_history_max;
  821. journal->j_history = kzalloc(size, GFP_KERNEL);
  822. if (!journal->j_history) {
  823. journal->j_history_max = 0;
  824. return;
  825. }
  826. spin_lock_init(&journal->j_history_lock);
  827. }
  828. /*
  829. * Management for journal control blocks: functions to create and
  830. * destroy journal_t structures, and to initialise and read existing
  831. * journal blocks from disk. */
  832. /* First: create and setup a journal_t object in memory. We initialise
  833. * very few fields yet: that has to wait until we have created the
  834. * journal structures from from scratch, or loaded them from disk. */
  835. static journal_t * journal_init_common (void)
  836. {
  837. journal_t *journal;
  838. int err;
  839. journal = kzalloc(sizeof(*journal), GFP_KERNEL|__GFP_NOFAIL);
  840. if (!journal)
  841. goto fail;
  842. init_waitqueue_head(&journal->j_wait_transaction_locked);
  843. init_waitqueue_head(&journal->j_wait_logspace);
  844. init_waitqueue_head(&journal->j_wait_done_commit);
  845. init_waitqueue_head(&journal->j_wait_checkpoint);
  846. init_waitqueue_head(&journal->j_wait_commit);
  847. init_waitqueue_head(&journal->j_wait_updates);
  848. mutex_init(&journal->j_barrier);
  849. mutex_init(&journal->j_checkpoint_mutex);
  850. spin_lock_init(&journal->j_revoke_lock);
  851. spin_lock_init(&journal->j_list_lock);
  852. spin_lock_init(&journal->j_state_lock);
  853. journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
  854. /* The journal is marked for error until we succeed with recovery! */
  855. journal->j_flags = JBD2_ABORT;
  856. /* Set up a default-sized revoke table for the new mount. */
  857. err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
  858. if (err) {
  859. kfree(journal);
  860. goto fail;
  861. }
  862. journal_init_stats(journal);
  863. return journal;
  864. fail:
  865. return NULL;
  866. }
  867. /* jbd2_journal_init_dev and jbd2_journal_init_inode:
  868. *
  869. * Create a journal structure assigned some fixed set of disk blocks to
  870. * the journal. We don't actually touch those disk blocks yet, but we
  871. * need to set up all of the mapping information to tell the journaling
  872. * system where the journal blocks are.
  873. *
  874. */
  875. /**
  876. * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
  877. * @bdev: Block device on which to create the journal
  878. * @fs_dev: Device which hold journalled filesystem for this journal.
  879. * @start: Block nr Start of journal.
  880. * @len: Length of the journal in blocks.
  881. * @blocksize: blocksize of journalling device
  882. *
  883. * Returns: a newly created journal_t *
  884. *
  885. * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
  886. * range of blocks on an arbitrary block device.
  887. *
  888. */
  889. journal_t * jbd2_journal_init_dev(struct block_device *bdev,
  890. struct block_device *fs_dev,
  891. unsigned long long start, int len, int blocksize)
  892. {
  893. journal_t *journal = journal_init_common();
  894. struct buffer_head *bh;
  895. int n;
  896. if (!journal)
  897. return NULL;
  898. /* journal descriptor can store up to n blocks -bzzz */
  899. journal->j_blocksize = blocksize;
  900. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  901. journal->j_wbufsize = n;
  902. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  903. if (!journal->j_wbuf) {
  904. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  905. __func__);
  906. kfree(journal);
  907. journal = NULL;
  908. goto out;
  909. }
  910. journal->j_dev = bdev;
  911. journal->j_fs_dev = fs_dev;
  912. journal->j_blk_offset = start;
  913. journal->j_maxlen = len;
  914. jbd2_stats_proc_init(journal);
  915. bh = __getblk(journal->j_dev, start, journal->j_blocksize);
  916. J_ASSERT(bh != NULL);
  917. journal->j_sb_buffer = bh;
  918. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  919. out:
  920. return journal;
  921. }
  922. /**
  923. * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
  924. * @inode: An inode to create the journal in
  925. *
  926. * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
  927. * the journal. The inode must exist already, must support bmap() and
  928. * must have all data blocks preallocated.
  929. */
  930. journal_t * jbd2_journal_init_inode (struct inode *inode)
  931. {
  932. struct buffer_head *bh;
  933. journal_t *journal = journal_init_common();
  934. int err;
  935. int n;
  936. unsigned long long blocknr;
  937. if (!journal)
  938. return NULL;
  939. journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
  940. journal->j_inode = inode;
  941. jbd_debug(1,
  942. "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
  943. journal, inode->i_sb->s_id, inode->i_ino,
  944. (long long) inode->i_size,
  945. inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
  946. journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
  947. journal->j_blocksize = inode->i_sb->s_blocksize;
  948. jbd2_stats_proc_init(journal);
  949. /* journal descriptor can store up to n blocks -bzzz */
  950. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  951. journal->j_wbufsize = n;
  952. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  953. if (!journal->j_wbuf) {
  954. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  955. __func__);
  956. kfree(journal);
  957. return NULL;
  958. }
  959. err = jbd2_journal_bmap(journal, 0, &blocknr);
  960. /* If that failed, give up */
  961. if (err) {
  962. printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
  963. __func__);
  964. kfree(journal);
  965. return NULL;
  966. }
  967. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  968. J_ASSERT(bh != NULL);
  969. journal->j_sb_buffer = bh;
  970. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  971. return journal;
  972. }
  973. /*
  974. * If the journal init or create aborts, we need to mark the journal
  975. * superblock as being NULL to prevent the journal destroy from writing
  976. * back a bogus superblock.
  977. */
  978. static void journal_fail_superblock (journal_t *journal)
  979. {
  980. struct buffer_head *bh = journal->j_sb_buffer;
  981. brelse(bh);
  982. journal->j_sb_buffer = NULL;
  983. }
  984. /*
  985. * Given a journal_t structure, initialise the various fields for
  986. * startup of a new journaling session. We use this both when creating
  987. * a journal, and after recovering an old journal to reset it for
  988. * subsequent use.
  989. */
  990. static int journal_reset(journal_t *journal)
  991. {
  992. journal_superblock_t *sb = journal->j_superblock;
  993. unsigned long long first, last;
  994. first = be32_to_cpu(sb->s_first);
  995. last = be32_to_cpu(sb->s_maxlen);
  996. journal->j_first = first;
  997. journal->j_last = last;
  998. journal->j_head = first;
  999. journal->j_tail = first;
  1000. journal->j_free = last - first;
  1001. journal->j_tail_sequence = journal->j_transaction_sequence;
  1002. journal->j_commit_sequence = journal->j_transaction_sequence - 1;
  1003. journal->j_commit_request = journal->j_commit_sequence;
  1004. journal->j_max_transaction_buffers = journal->j_maxlen / 4;
  1005. /* Add the dynamic fields and write it to disk. */
  1006. jbd2_journal_update_superblock(journal, 1);
  1007. return jbd2_journal_start_thread(journal);
  1008. }
  1009. /**
  1010. * int jbd2_journal_create() - Initialise the new journal file
  1011. * @journal: Journal to create. This structure must have been initialised
  1012. *
  1013. * Given a journal_t structure which tells us which disk blocks we can
  1014. * use, create a new journal superblock and initialise all of the
  1015. * journal fields from scratch.
  1016. **/
  1017. int jbd2_journal_create(journal_t *journal)
  1018. {
  1019. unsigned long long blocknr;
  1020. struct buffer_head *bh;
  1021. journal_superblock_t *sb;
  1022. int i, err;
  1023. if (journal->j_maxlen < JBD2_MIN_JOURNAL_BLOCKS) {
  1024. printk (KERN_ERR "Journal length (%d blocks) too short.\n",
  1025. journal->j_maxlen);
  1026. journal_fail_superblock(journal);
  1027. return -EINVAL;
  1028. }
  1029. if (journal->j_inode == NULL) {
  1030. /*
  1031. * We don't know what block to start at!
  1032. */
  1033. printk(KERN_EMERG
  1034. "%s: creation of journal on external device!\n",
  1035. __func__);
  1036. BUG();
  1037. }
  1038. /* Zero out the entire journal on disk. We cannot afford to
  1039. have any blocks on disk beginning with JBD2_MAGIC_NUMBER. */
  1040. jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
  1041. for (i = 0; i < journal->j_maxlen; i++) {
  1042. err = jbd2_journal_bmap(journal, i, &blocknr);
  1043. if (err)
  1044. return err;
  1045. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  1046. lock_buffer(bh);
  1047. memset (bh->b_data, 0, journal->j_blocksize);
  1048. BUFFER_TRACE(bh, "marking dirty");
  1049. mark_buffer_dirty(bh);
  1050. BUFFER_TRACE(bh, "marking uptodate");
  1051. set_buffer_uptodate(bh);
  1052. unlock_buffer(bh);
  1053. __brelse(bh);
  1054. }
  1055. sync_blockdev(journal->j_dev);
  1056. jbd_debug(1, "JBD: journal cleared.\n");
  1057. /* OK, fill in the initial static fields in the new superblock */
  1058. sb = journal->j_superblock;
  1059. sb->s_header.h_magic = cpu_to_be32(JBD2_MAGIC_NUMBER);
  1060. sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
  1061. sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
  1062. sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
  1063. sb->s_first = cpu_to_be32(1);
  1064. journal->j_transaction_sequence = 1;
  1065. journal->j_flags &= ~JBD2_ABORT;
  1066. journal->j_format_version = 2;
  1067. return journal_reset(journal);
  1068. }
  1069. /**
  1070. * void jbd2_journal_update_superblock() - Update journal sb on disk.
  1071. * @journal: The journal to update.
  1072. * @wait: Set to '0' if you don't want to wait for IO completion.
  1073. *
  1074. * Update a journal's dynamic superblock fields and write it to disk,
  1075. * optionally waiting for the IO to complete.
  1076. */
  1077. void jbd2_journal_update_superblock(journal_t *journal, int wait)
  1078. {
  1079. journal_superblock_t *sb = journal->j_superblock;
  1080. struct buffer_head *bh = journal->j_sb_buffer;
  1081. /*
  1082. * As a special case, if the on-disk copy is already marked as needing
  1083. * no recovery (s_start == 0) and there are no outstanding transactions
  1084. * in the filesystem, then we can safely defer the superblock update
  1085. * until the next commit by setting JBD2_FLUSHED. This avoids
  1086. * attempting a write to a potential-readonly device.
  1087. */
  1088. if (sb->s_start == 0 && journal->j_tail_sequence ==
  1089. journal->j_transaction_sequence) {
  1090. jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
  1091. "(start %ld, seq %d, errno %d)\n",
  1092. journal->j_tail, journal->j_tail_sequence,
  1093. journal->j_errno);
  1094. goto out;
  1095. }
  1096. spin_lock(&journal->j_state_lock);
  1097. jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
  1098. journal->j_tail, journal->j_tail_sequence, journal->j_errno);
  1099. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  1100. sb->s_start = cpu_to_be32(journal->j_tail);
  1101. sb->s_errno = cpu_to_be32(journal->j_errno);
  1102. spin_unlock(&journal->j_state_lock);
  1103. BUFFER_TRACE(bh, "marking dirty");
  1104. mark_buffer_dirty(bh);
  1105. if (wait)
  1106. sync_dirty_buffer(bh);
  1107. else
  1108. ll_rw_block(SWRITE, 1, &bh);
  1109. out:
  1110. /* If we have just flushed the log (by marking s_start==0), then
  1111. * any future commit will have to be careful to update the
  1112. * superblock again to re-record the true start of the log. */
  1113. spin_lock(&journal->j_state_lock);
  1114. if (sb->s_start)
  1115. journal->j_flags &= ~JBD2_FLUSHED;
  1116. else
  1117. journal->j_flags |= JBD2_FLUSHED;
  1118. spin_unlock(&journal->j_state_lock);
  1119. }
  1120. /*
  1121. * Read the superblock for a given journal, performing initial
  1122. * validation of the format.
  1123. */
  1124. static int journal_get_superblock(journal_t *journal)
  1125. {
  1126. struct buffer_head *bh;
  1127. journal_superblock_t *sb;
  1128. int err = -EIO;
  1129. bh = journal->j_sb_buffer;
  1130. J_ASSERT(bh != NULL);
  1131. if (!buffer_uptodate(bh)) {
  1132. ll_rw_block(READ, 1, &bh);
  1133. wait_on_buffer(bh);
  1134. if (!buffer_uptodate(bh)) {
  1135. printk (KERN_ERR
  1136. "JBD: IO error reading journal superblock\n");
  1137. goto out;
  1138. }
  1139. }
  1140. sb = journal->j_superblock;
  1141. err = -EINVAL;
  1142. if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
  1143. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  1144. printk(KERN_WARNING "JBD: no valid journal superblock found\n");
  1145. goto out;
  1146. }
  1147. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  1148. case JBD2_SUPERBLOCK_V1:
  1149. journal->j_format_version = 1;
  1150. break;
  1151. case JBD2_SUPERBLOCK_V2:
  1152. journal->j_format_version = 2;
  1153. break;
  1154. default:
  1155. printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
  1156. goto out;
  1157. }
  1158. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  1159. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  1160. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  1161. printk (KERN_WARNING "JBD: journal file too short\n");
  1162. goto out;
  1163. }
  1164. return 0;
  1165. out:
  1166. journal_fail_superblock(journal);
  1167. return err;
  1168. }
  1169. /*
  1170. * Load the on-disk journal superblock and read the key fields into the
  1171. * journal_t.
  1172. */
  1173. static int load_superblock(journal_t *journal)
  1174. {
  1175. int err;
  1176. journal_superblock_t *sb;
  1177. err = journal_get_superblock(journal);
  1178. if (err)
  1179. return err;
  1180. sb = journal->j_superblock;
  1181. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1182. journal->j_tail = be32_to_cpu(sb->s_start);
  1183. journal->j_first = be32_to_cpu(sb->s_first);
  1184. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1185. journal->j_errno = be32_to_cpu(sb->s_errno);
  1186. return 0;
  1187. }
  1188. /**
  1189. * int jbd2_journal_load() - Read journal from disk.
  1190. * @journal: Journal to act on.
  1191. *
  1192. * Given a journal_t structure which tells us which disk blocks contain
  1193. * a journal, read the journal from disk to initialise the in-memory
  1194. * structures.
  1195. */
  1196. int jbd2_journal_load(journal_t *journal)
  1197. {
  1198. int err;
  1199. journal_superblock_t *sb;
  1200. err = load_superblock(journal);
  1201. if (err)
  1202. return err;
  1203. sb = journal->j_superblock;
  1204. /* If this is a V2 superblock, then we have to check the
  1205. * features flags on it. */
  1206. if (journal->j_format_version >= 2) {
  1207. if ((sb->s_feature_ro_compat &
  1208. ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
  1209. (sb->s_feature_incompat &
  1210. ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
  1211. printk (KERN_WARNING
  1212. "JBD: Unrecognised features on journal\n");
  1213. return -EINVAL;
  1214. }
  1215. }
  1216. /* Let the recovery code check whether it needs to recover any
  1217. * data from the journal. */
  1218. if (jbd2_journal_recover(journal))
  1219. goto recovery_error;
  1220. /* OK, we've finished with the dynamic journal bits:
  1221. * reinitialise the dynamic contents of the superblock in memory
  1222. * and reset them on disk. */
  1223. if (journal_reset(journal))
  1224. goto recovery_error;
  1225. journal->j_flags &= ~JBD2_ABORT;
  1226. journal->j_flags |= JBD2_LOADED;
  1227. return 0;
  1228. recovery_error:
  1229. printk (KERN_WARNING "JBD: recovery failed\n");
  1230. return -EIO;
  1231. }
  1232. /**
  1233. * void jbd2_journal_destroy() - Release a journal_t structure.
  1234. * @journal: Journal to act on.
  1235. *
  1236. * Release a journal_t structure once it is no longer in use by the
  1237. * journaled object.
  1238. */
  1239. void jbd2_journal_destroy(journal_t *journal)
  1240. {
  1241. /* Wait for the commit thread to wake up and die. */
  1242. journal_kill_thread(journal);
  1243. /* Force a final log commit */
  1244. if (journal->j_running_transaction)
  1245. jbd2_journal_commit_transaction(journal);
  1246. /* Force any old transactions to disk */
  1247. /* Totally anal locking here... */
  1248. spin_lock(&journal->j_list_lock);
  1249. while (journal->j_checkpoint_transactions != NULL) {
  1250. spin_unlock(&journal->j_list_lock);
  1251. jbd2_log_do_checkpoint(journal);
  1252. spin_lock(&journal->j_list_lock);
  1253. }
  1254. J_ASSERT(journal->j_running_transaction == NULL);
  1255. J_ASSERT(journal->j_committing_transaction == NULL);
  1256. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1257. spin_unlock(&journal->j_list_lock);
  1258. /* We can now mark the journal as empty. */
  1259. journal->j_tail = 0;
  1260. journal->j_tail_sequence = ++journal->j_transaction_sequence;
  1261. if (journal->j_sb_buffer) {
  1262. jbd2_journal_update_superblock(journal, 1);
  1263. brelse(journal->j_sb_buffer);
  1264. }
  1265. if (journal->j_proc_entry)
  1266. jbd2_stats_proc_exit(journal);
  1267. if (journal->j_inode)
  1268. iput(journal->j_inode);
  1269. if (journal->j_revoke)
  1270. jbd2_journal_destroy_revoke(journal);
  1271. kfree(journal->j_wbuf);
  1272. kfree(journal);
  1273. }
  1274. /**
  1275. *int jbd2_journal_check_used_features () - Check if features specified are used.
  1276. * @journal: Journal to check.
  1277. * @compat: bitmask of compatible features
  1278. * @ro: bitmask of features that force read-only mount
  1279. * @incompat: bitmask of incompatible features
  1280. *
  1281. * Check whether the journal uses all of a given set of
  1282. * features. Return true (non-zero) if it does.
  1283. **/
  1284. int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
  1285. unsigned long ro, unsigned long incompat)
  1286. {
  1287. journal_superblock_t *sb;
  1288. if (!compat && !ro && !incompat)
  1289. return 1;
  1290. if (journal->j_format_version == 1)
  1291. return 0;
  1292. sb = journal->j_superblock;
  1293. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1294. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1295. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1296. return 1;
  1297. return 0;
  1298. }
  1299. /**
  1300. * int jbd2_journal_check_available_features() - Check feature set in journalling layer
  1301. * @journal: Journal to check.
  1302. * @compat: bitmask of compatible features
  1303. * @ro: bitmask of features that force read-only mount
  1304. * @incompat: bitmask of incompatible features
  1305. *
  1306. * Check whether the journaling code supports the use of
  1307. * all of a given set of features on this journal. Return true
  1308. * (non-zero) if it can. */
  1309. int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
  1310. unsigned long ro, unsigned long incompat)
  1311. {
  1312. journal_superblock_t *sb;
  1313. if (!compat && !ro && !incompat)
  1314. return 1;
  1315. sb = journal->j_superblock;
  1316. /* We can support any known requested features iff the
  1317. * superblock is in version 2. Otherwise we fail to support any
  1318. * extended sb features. */
  1319. if (journal->j_format_version != 2)
  1320. return 0;
  1321. if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
  1322. (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1323. (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
  1324. return 1;
  1325. return 0;
  1326. }
  1327. /**
  1328. * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
  1329. * @journal: Journal to act on.
  1330. * @compat: bitmask of compatible features
  1331. * @ro: bitmask of features that force read-only mount
  1332. * @incompat: bitmask of incompatible features
  1333. *
  1334. * Mark a given journal feature as present on the
  1335. * superblock. Returns true if the requested features could be set.
  1336. *
  1337. */
  1338. int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
  1339. unsigned long ro, unsigned long incompat)
  1340. {
  1341. journal_superblock_t *sb;
  1342. if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
  1343. return 1;
  1344. if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
  1345. return 0;
  1346. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1347. compat, ro, incompat);
  1348. sb = journal->j_superblock;
  1349. sb->s_feature_compat |= cpu_to_be32(compat);
  1350. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1351. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1352. return 1;
  1353. }
  1354. /*
  1355. * jbd2_journal_clear_features () - Clear a given journal feature in the
  1356. * superblock
  1357. * @journal: Journal to act on.
  1358. * @compat: bitmask of compatible features
  1359. * @ro: bitmask of features that force read-only mount
  1360. * @incompat: bitmask of incompatible features
  1361. *
  1362. * Clear a given journal feature as present on the
  1363. * superblock.
  1364. */
  1365. void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
  1366. unsigned long ro, unsigned long incompat)
  1367. {
  1368. journal_superblock_t *sb;
  1369. jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
  1370. compat, ro, incompat);
  1371. sb = journal->j_superblock;
  1372. sb->s_feature_compat &= ~cpu_to_be32(compat);
  1373. sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
  1374. sb->s_feature_incompat &= ~cpu_to_be32(incompat);
  1375. }
  1376. EXPORT_SYMBOL(jbd2_journal_clear_features);
  1377. /**
  1378. * int jbd2_journal_update_format () - Update on-disk journal structure.
  1379. * @journal: Journal to act on.
  1380. *
  1381. * Given an initialised but unloaded journal struct, poke about in the
  1382. * on-disk structure to update it to the most recent supported version.
  1383. */
  1384. int jbd2_journal_update_format (journal_t *journal)
  1385. {
  1386. journal_superblock_t *sb;
  1387. int err;
  1388. err = journal_get_superblock(journal);
  1389. if (err)
  1390. return err;
  1391. sb = journal->j_superblock;
  1392. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1393. case JBD2_SUPERBLOCK_V2:
  1394. return 0;
  1395. case JBD2_SUPERBLOCK_V1:
  1396. return journal_convert_superblock_v1(journal, sb);
  1397. default:
  1398. break;
  1399. }
  1400. return -EINVAL;
  1401. }
  1402. static int journal_convert_superblock_v1(journal_t *journal,
  1403. journal_superblock_t *sb)
  1404. {
  1405. int offset, blocksize;
  1406. struct buffer_head *bh;
  1407. printk(KERN_WARNING
  1408. "JBD: Converting superblock from version 1 to 2.\n");
  1409. /* Pre-initialise new fields to zero */
  1410. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1411. blocksize = be32_to_cpu(sb->s_blocksize);
  1412. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1413. sb->s_nr_users = cpu_to_be32(1);
  1414. sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
  1415. journal->j_format_version = 2;
  1416. bh = journal->j_sb_buffer;
  1417. BUFFER_TRACE(bh, "marking dirty");
  1418. mark_buffer_dirty(bh);
  1419. sync_dirty_buffer(bh);
  1420. return 0;
  1421. }
  1422. /**
  1423. * int jbd2_journal_flush () - Flush journal
  1424. * @journal: Journal to act on.
  1425. *
  1426. * Flush all data for a given journal to disk and empty the journal.
  1427. * Filesystems can use this when remounting readonly to ensure that
  1428. * recovery does not need to happen on remount.
  1429. */
  1430. int jbd2_journal_flush(journal_t *journal)
  1431. {
  1432. int err = 0;
  1433. transaction_t *transaction = NULL;
  1434. unsigned long old_tail;
  1435. spin_lock(&journal->j_state_lock);
  1436. /* Force everything buffered to the log... */
  1437. if (journal->j_running_transaction) {
  1438. transaction = journal->j_running_transaction;
  1439. __jbd2_log_start_commit(journal, transaction->t_tid);
  1440. } else if (journal->j_committing_transaction)
  1441. transaction = journal->j_committing_transaction;
  1442. /* Wait for the log commit to complete... */
  1443. if (transaction) {
  1444. tid_t tid = transaction->t_tid;
  1445. spin_unlock(&journal->j_state_lock);
  1446. jbd2_log_wait_commit(journal, tid);
  1447. } else {
  1448. spin_unlock(&journal->j_state_lock);
  1449. }
  1450. /* ...and flush everything in the log out to disk. */
  1451. spin_lock(&journal->j_list_lock);
  1452. while (!err && journal->j_checkpoint_transactions != NULL) {
  1453. spin_unlock(&journal->j_list_lock);
  1454. err = jbd2_log_do_checkpoint(journal);
  1455. spin_lock(&journal->j_list_lock);
  1456. }
  1457. spin_unlock(&journal->j_list_lock);
  1458. jbd2_cleanup_journal_tail(journal);
  1459. /* Finally, mark the journal as really needing no recovery.
  1460. * This sets s_start==0 in the underlying superblock, which is
  1461. * the magic code for a fully-recovered superblock. Any future
  1462. * commits of data to the journal will restore the current
  1463. * s_start value. */
  1464. spin_lock(&journal->j_state_lock);
  1465. old_tail = journal->j_tail;
  1466. journal->j_tail = 0;
  1467. spin_unlock(&journal->j_state_lock);
  1468. jbd2_journal_update_superblock(journal, 1);
  1469. spin_lock(&journal->j_state_lock);
  1470. journal->j_tail = old_tail;
  1471. J_ASSERT(!journal->j_running_transaction);
  1472. J_ASSERT(!journal->j_committing_transaction);
  1473. J_ASSERT(!journal->j_checkpoint_transactions);
  1474. J_ASSERT(journal->j_head == journal->j_tail);
  1475. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1476. spin_unlock(&journal->j_state_lock);
  1477. return err;
  1478. }
  1479. /**
  1480. * int jbd2_journal_wipe() - Wipe journal contents
  1481. * @journal: Journal to act on.
  1482. * @write: flag (see below)
  1483. *
  1484. * Wipe out all of the contents of a journal, safely. This will produce
  1485. * a warning if the journal contains any valid recovery information.
  1486. * Must be called between journal_init_*() and jbd2_journal_load().
  1487. *
  1488. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1489. * we merely suppress recovery.
  1490. */
  1491. int jbd2_journal_wipe(journal_t *journal, int write)
  1492. {
  1493. journal_superblock_t *sb;
  1494. int err = 0;
  1495. J_ASSERT (!(journal->j_flags & JBD2_LOADED));
  1496. err = load_superblock(journal);
  1497. if (err)
  1498. return err;
  1499. sb = journal->j_superblock;
  1500. if (!journal->j_tail)
  1501. goto no_recovery;
  1502. printk (KERN_WARNING "JBD: %s recovery information on journal\n",
  1503. write ? "Clearing" : "Ignoring");
  1504. err = jbd2_journal_skip_recovery(journal);
  1505. if (write)
  1506. jbd2_journal_update_superblock(journal, 1);
  1507. no_recovery:
  1508. return err;
  1509. }
  1510. /*
  1511. * journal_dev_name: format a character string to describe on what
  1512. * device this journal is present.
  1513. */
  1514. static const char *journal_dev_name(journal_t *journal, char *buffer)
  1515. {
  1516. struct block_device *bdev;
  1517. if (journal->j_inode)
  1518. bdev = journal->j_inode->i_sb->s_bdev;
  1519. else
  1520. bdev = journal->j_dev;
  1521. return bdevname(bdev, buffer);
  1522. }
  1523. /*
  1524. * Journal abort has very specific semantics, which we describe
  1525. * for journal abort.
  1526. *
  1527. * Two internal function, which provide abort to te jbd layer
  1528. * itself are here.
  1529. */
  1530. /*
  1531. * Quick version for internal journal use (doesn't lock the journal).
  1532. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1533. * and don't attempt to make any other journal updates.
  1534. */
  1535. void __jbd2_journal_abort_hard(journal_t *journal)
  1536. {
  1537. transaction_t *transaction;
  1538. char b[BDEVNAME_SIZE];
  1539. if (journal->j_flags & JBD2_ABORT)
  1540. return;
  1541. printk(KERN_ERR "Aborting journal on device %s.\n",
  1542. journal_dev_name(journal, b));
  1543. spin_lock(&journal->j_state_lock);
  1544. journal->j_flags |= JBD2_ABORT;
  1545. transaction = journal->j_running_transaction;
  1546. if (transaction)
  1547. __jbd2_log_start_commit(journal, transaction->t_tid);
  1548. spin_unlock(&journal->j_state_lock);
  1549. }
  1550. /* Soft abort: record the abort error status in the journal superblock,
  1551. * but don't do any other IO. */
  1552. static void __journal_abort_soft (journal_t *journal, int errno)
  1553. {
  1554. if (journal->j_flags & JBD2_ABORT)
  1555. return;
  1556. if (!journal->j_errno)
  1557. journal->j_errno = errno;
  1558. __jbd2_journal_abort_hard(journal);
  1559. if (errno)
  1560. jbd2_journal_update_superblock(journal, 1);
  1561. }
  1562. /**
  1563. * void jbd2_journal_abort () - Shutdown the journal immediately.
  1564. * @journal: the journal to shutdown.
  1565. * @errno: an error number to record in the journal indicating
  1566. * the reason for the shutdown.
  1567. *
  1568. * Perform a complete, immediate shutdown of the ENTIRE
  1569. * journal (not of a single transaction). This operation cannot be
  1570. * undone without closing and reopening the journal.
  1571. *
  1572. * The jbd2_journal_abort function is intended to support higher level error
  1573. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1574. * mode.
  1575. *
  1576. * Journal abort has very specific semantics. Any existing dirty,
  1577. * unjournaled buffers in the main filesystem will still be written to
  1578. * disk by bdflush, but the journaling mechanism will be suspended
  1579. * immediately and no further transaction commits will be honoured.
  1580. *
  1581. * Any dirty, journaled buffers will be written back to disk without
  1582. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1583. * filesystem, but we _do_ attempt to leave as much data as possible
  1584. * behind for fsck to use for cleanup.
  1585. *
  1586. * Any attempt to get a new transaction handle on a journal which is in
  1587. * ABORT state will just result in an -EROFS error return. A
  1588. * jbd2_journal_stop on an existing handle will return -EIO if we have
  1589. * entered abort state during the update.
  1590. *
  1591. * Recursive transactions are not disturbed by journal abort until the
  1592. * final jbd2_journal_stop, which will receive the -EIO error.
  1593. *
  1594. * Finally, the jbd2_journal_abort call allows the caller to supply an errno
  1595. * which will be recorded (if possible) in the journal superblock. This
  1596. * allows a client to record failure conditions in the middle of a
  1597. * transaction without having to complete the transaction to record the
  1598. * failure to disk. ext3_error, for example, now uses this
  1599. * functionality.
  1600. *
  1601. * Errors which originate from within the journaling layer will NOT
  1602. * supply an errno; a null errno implies that absolutely no further
  1603. * writes are done to the journal (unless there are any already in
  1604. * progress).
  1605. *
  1606. */
  1607. void jbd2_journal_abort(journal_t *journal, int errno)
  1608. {
  1609. __journal_abort_soft(journal, errno);
  1610. }
  1611. /**
  1612. * int jbd2_journal_errno () - returns the journal's error state.
  1613. * @journal: journal to examine.
  1614. *
  1615. * This is the errno numbet set with jbd2_journal_abort(), the last
  1616. * time the journal was mounted - if the journal was stopped
  1617. * without calling abort this will be 0.
  1618. *
  1619. * If the journal has been aborted on this mount time -EROFS will
  1620. * be returned.
  1621. */
  1622. int jbd2_journal_errno(journal_t *journal)
  1623. {
  1624. int err;
  1625. spin_lock(&journal->j_state_lock);
  1626. if (journal->j_flags & JBD2_ABORT)
  1627. err = -EROFS;
  1628. else
  1629. err = journal->j_errno;
  1630. spin_unlock(&journal->j_state_lock);
  1631. return err;
  1632. }
  1633. /**
  1634. * int jbd2_journal_clear_err () - clears the journal's error state
  1635. * @journal: journal to act on.
  1636. *
  1637. * An error must be cleared or Acked to take a FS out of readonly
  1638. * mode.
  1639. */
  1640. int jbd2_journal_clear_err(journal_t *journal)
  1641. {
  1642. int err = 0;
  1643. spin_lock(&journal->j_state_lock);
  1644. if (journal->j_flags & JBD2_ABORT)
  1645. err = -EROFS;
  1646. else
  1647. journal->j_errno = 0;
  1648. spin_unlock(&journal->j_state_lock);
  1649. return err;
  1650. }
  1651. /**
  1652. * void jbd2_journal_ack_err() - Ack journal err.
  1653. * @journal: journal to act on.
  1654. *
  1655. * An error must be cleared or Acked to take a FS out of readonly
  1656. * mode.
  1657. */
  1658. void jbd2_journal_ack_err(journal_t *journal)
  1659. {
  1660. spin_lock(&journal->j_state_lock);
  1661. if (journal->j_errno)
  1662. journal->j_flags |= JBD2_ACK_ERR;
  1663. spin_unlock(&journal->j_state_lock);
  1664. }
  1665. int jbd2_journal_blocks_per_page(struct inode *inode)
  1666. {
  1667. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1668. }
  1669. /*
  1670. * helper functions to deal with 32 or 64bit block numbers.
  1671. */
  1672. size_t journal_tag_bytes(journal_t *journal)
  1673. {
  1674. if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
  1675. return JBD2_TAG_SIZE64;
  1676. else
  1677. return JBD2_TAG_SIZE32;
  1678. }
  1679. /*
  1680. * Journal_head storage management
  1681. */
  1682. static struct kmem_cache *jbd2_journal_head_cache;
  1683. #ifdef CONFIG_JBD2_DEBUG
  1684. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1685. #endif
  1686. static int journal_init_jbd2_journal_head_cache(void)
  1687. {
  1688. int retval;
  1689. J_ASSERT(jbd2_journal_head_cache == NULL);
  1690. jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
  1691. sizeof(struct journal_head),
  1692. 0, /* offset */
  1693. SLAB_TEMPORARY, /* flags */
  1694. NULL); /* ctor */
  1695. retval = 0;
  1696. if (!jbd2_journal_head_cache) {
  1697. retval = -ENOMEM;
  1698. printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
  1699. }
  1700. return retval;
  1701. }
  1702. static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
  1703. {
  1704. if (jbd2_journal_head_cache) {
  1705. kmem_cache_destroy(jbd2_journal_head_cache);
  1706. jbd2_journal_head_cache = NULL;
  1707. }
  1708. }
  1709. /*
  1710. * journal_head splicing and dicing
  1711. */
  1712. static struct journal_head *journal_alloc_journal_head(void)
  1713. {
  1714. struct journal_head *ret;
  1715. static unsigned long last_warning;
  1716. #ifdef CONFIG_JBD2_DEBUG
  1717. atomic_inc(&nr_journal_heads);
  1718. #endif
  1719. ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
  1720. if (!ret) {
  1721. jbd_debug(1, "out of memory for journal_head\n");
  1722. if (time_after(jiffies, last_warning + 5*HZ)) {
  1723. printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
  1724. __func__);
  1725. last_warning = jiffies;
  1726. }
  1727. while (!ret) {
  1728. yield();
  1729. ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
  1730. }
  1731. }
  1732. return ret;
  1733. }
  1734. static void journal_free_journal_head(struct journal_head *jh)
  1735. {
  1736. #ifdef CONFIG_JBD2_DEBUG
  1737. atomic_dec(&nr_journal_heads);
  1738. memset(jh, JBD2_POISON_FREE, sizeof(*jh));
  1739. #endif
  1740. kmem_cache_free(jbd2_journal_head_cache, jh);
  1741. }
  1742. /*
  1743. * A journal_head is attached to a buffer_head whenever JBD has an
  1744. * interest in the buffer.
  1745. *
  1746. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1747. * is set. This bit is tested in core kernel code where we need to take
  1748. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1749. * there.
  1750. *
  1751. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1752. *
  1753. * When a buffer has its BH_JBD bit set it is immune from being released by
  1754. * core kernel code, mainly via ->b_count.
  1755. *
  1756. * A journal_head may be detached from its buffer_head when the journal_head's
  1757. * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
  1758. * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
  1759. * journal_head can be dropped if needed.
  1760. *
  1761. * Various places in the kernel want to attach a journal_head to a buffer_head
  1762. * _before_ attaching the journal_head to a transaction. To protect the
  1763. * journal_head in this situation, jbd2_journal_add_journal_head elevates the
  1764. * journal_head's b_jcount refcount by one. The caller must call
  1765. * jbd2_journal_put_journal_head() to undo this.
  1766. *
  1767. * So the typical usage would be:
  1768. *
  1769. * (Attach a journal_head if needed. Increments b_jcount)
  1770. * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  1771. * ...
  1772. * jh->b_transaction = xxx;
  1773. * jbd2_journal_put_journal_head(jh);
  1774. *
  1775. * Now, the journal_head's b_jcount is zero, but it is safe from being released
  1776. * because it has a non-zero b_transaction.
  1777. */
  1778. /*
  1779. * Give a buffer_head a journal_head.
  1780. *
  1781. * Doesn't need the journal lock.
  1782. * May sleep.
  1783. */
  1784. struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
  1785. {
  1786. struct journal_head *jh;
  1787. struct journal_head *new_jh = NULL;
  1788. repeat:
  1789. if (!buffer_jbd(bh)) {
  1790. new_jh = journal_alloc_journal_head();
  1791. memset(new_jh, 0, sizeof(*new_jh));
  1792. }
  1793. jbd_lock_bh_journal_head(bh);
  1794. if (buffer_jbd(bh)) {
  1795. jh = bh2jh(bh);
  1796. } else {
  1797. J_ASSERT_BH(bh,
  1798. (atomic_read(&bh->b_count) > 0) ||
  1799. (bh->b_page && bh->b_page->mapping));
  1800. if (!new_jh) {
  1801. jbd_unlock_bh_journal_head(bh);
  1802. goto repeat;
  1803. }
  1804. jh = new_jh;
  1805. new_jh = NULL; /* We consumed it */
  1806. set_buffer_jbd(bh);
  1807. bh->b_private = jh;
  1808. jh->b_bh = bh;
  1809. get_bh(bh);
  1810. BUFFER_TRACE(bh, "added journal_head");
  1811. }
  1812. jh->b_jcount++;
  1813. jbd_unlock_bh_journal_head(bh);
  1814. if (new_jh)
  1815. journal_free_journal_head(new_jh);
  1816. return bh->b_private;
  1817. }
  1818. /*
  1819. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1820. * having a journal_head, return NULL
  1821. */
  1822. struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
  1823. {
  1824. struct journal_head *jh = NULL;
  1825. jbd_lock_bh_journal_head(bh);
  1826. if (buffer_jbd(bh)) {
  1827. jh = bh2jh(bh);
  1828. jh->b_jcount++;
  1829. }
  1830. jbd_unlock_bh_journal_head(bh);
  1831. return jh;
  1832. }
  1833. static void __journal_remove_journal_head(struct buffer_head *bh)
  1834. {
  1835. struct journal_head *jh = bh2jh(bh);
  1836. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1837. get_bh(bh);
  1838. if (jh->b_jcount == 0) {
  1839. if (jh->b_transaction == NULL &&
  1840. jh->b_next_transaction == NULL &&
  1841. jh->b_cp_transaction == NULL) {
  1842. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1843. J_ASSERT_BH(bh, buffer_jbd(bh));
  1844. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1845. BUFFER_TRACE(bh, "remove journal_head");
  1846. if (jh->b_frozen_data) {
  1847. printk(KERN_WARNING "%s: freeing "
  1848. "b_frozen_data\n",
  1849. __func__);
  1850. jbd2_free(jh->b_frozen_data, bh->b_size);
  1851. }
  1852. if (jh->b_committed_data) {
  1853. printk(KERN_WARNING "%s: freeing "
  1854. "b_committed_data\n",
  1855. __func__);
  1856. jbd2_free(jh->b_committed_data, bh->b_size);
  1857. }
  1858. bh->b_private = NULL;
  1859. jh->b_bh = NULL; /* debug, really */
  1860. clear_buffer_jbd(bh);
  1861. __brelse(bh);
  1862. journal_free_journal_head(jh);
  1863. } else {
  1864. BUFFER_TRACE(bh, "journal_head was locked");
  1865. }
  1866. }
  1867. }
  1868. /*
  1869. * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
  1870. * and has a zero b_jcount then remove and release its journal_head. If we did
  1871. * see that the buffer is not used by any transaction we also "logically"
  1872. * decrement ->b_count.
  1873. *
  1874. * We in fact take an additional increment on ->b_count as a convenience,
  1875. * because the caller usually wants to do additional things with the bh
  1876. * after calling here.
  1877. * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
  1878. * time. Once the caller has run __brelse(), the buffer is eligible for
  1879. * reaping by try_to_free_buffers().
  1880. */
  1881. void jbd2_journal_remove_journal_head(struct buffer_head *bh)
  1882. {
  1883. jbd_lock_bh_journal_head(bh);
  1884. __journal_remove_journal_head(bh);
  1885. jbd_unlock_bh_journal_head(bh);
  1886. }
  1887. /*
  1888. * Drop a reference on the passed journal_head. If it fell to zero then try to
  1889. * release the journal_head from the buffer_head.
  1890. */
  1891. void jbd2_journal_put_journal_head(struct journal_head *jh)
  1892. {
  1893. struct buffer_head *bh = jh2bh(jh);
  1894. jbd_lock_bh_journal_head(bh);
  1895. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1896. --jh->b_jcount;
  1897. if (!jh->b_jcount && !jh->b_transaction) {
  1898. __journal_remove_journal_head(bh);
  1899. __brelse(bh);
  1900. }
  1901. jbd_unlock_bh_journal_head(bh);
  1902. }
  1903. /*
  1904. * Initialize jbd inode head
  1905. */
  1906. void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
  1907. {
  1908. jinode->i_transaction = NULL;
  1909. jinode->i_next_transaction = NULL;
  1910. jinode->i_vfs_inode = inode;
  1911. jinode->i_flags = 0;
  1912. INIT_LIST_HEAD(&jinode->i_list);
  1913. }
  1914. /*
  1915. * Function to be called before we start removing inode from memory (i.e.,
  1916. * clear_inode() is a fine place to be called from). It removes inode from
  1917. * transaction's lists.
  1918. */
  1919. void jbd2_journal_release_jbd_inode(journal_t *journal,
  1920. struct jbd2_inode *jinode)
  1921. {
  1922. int writeout = 0;
  1923. if (!journal)
  1924. return;
  1925. restart:
  1926. spin_lock(&journal->j_list_lock);
  1927. /* Is commit writing out inode - we have to wait */
  1928. if (jinode->i_flags & JI_COMMIT_RUNNING) {
  1929. wait_queue_head_t *wq;
  1930. DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
  1931. wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
  1932. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1933. spin_unlock(&journal->j_list_lock);
  1934. schedule();
  1935. finish_wait(wq, &wait.wait);
  1936. goto restart;
  1937. }
  1938. /* Do we need to wait for data writeback? */
  1939. if (journal->j_committing_transaction == jinode->i_transaction)
  1940. writeout = 1;
  1941. if (jinode->i_transaction) {
  1942. list_del(&jinode->i_list);
  1943. jinode->i_transaction = NULL;
  1944. }
  1945. spin_unlock(&journal->j_list_lock);
  1946. }
  1947. /*
  1948. * debugfs tunables
  1949. */
  1950. #ifdef CONFIG_JBD2_DEBUG
  1951. u8 jbd2_journal_enable_debug __read_mostly;
  1952. EXPORT_SYMBOL(jbd2_journal_enable_debug);
  1953. #define JBD2_DEBUG_NAME "jbd2-debug"
  1954. static struct dentry *jbd2_debugfs_dir;
  1955. static struct dentry *jbd2_debug;
  1956. static void __init jbd2_create_debugfs_entry(void)
  1957. {
  1958. jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
  1959. if (jbd2_debugfs_dir)
  1960. jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME, S_IRUGO,
  1961. jbd2_debugfs_dir,
  1962. &jbd2_journal_enable_debug);
  1963. }
  1964. static void __exit jbd2_remove_debugfs_entry(void)
  1965. {
  1966. debugfs_remove(jbd2_debug);
  1967. debugfs_remove(jbd2_debugfs_dir);
  1968. }
  1969. #else
  1970. static void __init jbd2_create_debugfs_entry(void)
  1971. {
  1972. }
  1973. static void __exit jbd2_remove_debugfs_entry(void)
  1974. {
  1975. }
  1976. #endif
  1977. #ifdef CONFIG_PROC_FS
  1978. #define JBD2_STATS_PROC_NAME "fs/jbd2"
  1979. static void __init jbd2_create_jbd_stats_proc_entry(void)
  1980. {
  1981. proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
  1982. }
  1983. static void __exit jbd2_remove_jbd_stats_proc_entry(void)
  1984. {
  1985. if (proc_jbd2_stats)
  1986. remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
  1987. }
  1988. #else
  1989. #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
  1990. #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
  1991. #endif
  1992. struct kmem_cache *jbd2_handle_cache;
  1993. static int __init journal_init_handle_cache(void)
  1994. {
  1995. jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
  1996. sizeof(handle_t),
  1997. 0, /* offset */
  1998. SLAB_TEMPORARY, /* flags */
  1999. NULL); /* ctor */
  2000. if (jbd2_handle_cache == NULL) {
  2001. printk(KERN_EMERG "JBD: failed to create handle cache\n");
  2002. return -ENOMEM;
  2003. }
  2004. return 0;
  2005. }
  2006. static void jbd2_journal_destroy_handle_cache(void)
  2007. {
  2008. if (jbd2_handle_cache)
  2009. kmem_cache_destroy(jbd2_handle_cache);
  2010. }
  2011. /*
  2012. * Module startup and shutdown
  2013. */
  2014. static int __init journal_init_caches(void)
  2015. {
  2016. int ret;
  2017. ret = jbd2_journal_init_revoke_caches();
  2018. if (ret == 0)
  2019. ret = journal_init_jbd2_journal_head_cache();
  2020. if (ret == 0)
  2021. ret = journal_init_handle_cache();
  2022. return ret;
  2023. }
  2024. static void jbd2_journal_destroy_caches(void)
  2025. {
  2026. jbd2_journal_destroy_revoke_caches();
  2027. jbd2_journal_destroy_jbd2_journal_head_cache();
  2028. jbd2_journal_destroy_handle_cache();
  2029. }
  2030. static int __init journal_init(void)
  2031. {
  2032. int ret;
  2033. BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
  2034. ret = journal_init_caches();
  2035. if (ret == 0) {
  2036. jbd2_create_debugfs_entry();
  2037. jbd2_create_jbd_stats_proc_entry();
  2038. } else {
  2039. jbd2_journal_destroy_caches();
  2040. }
  2041. return ret;
  2042. }
  2043. static void __exit journal_exit(void)
  2044. {
  2045. #ifdef CONFIG_JBD2_DEBUG
  2046. int n = atomic_read(&nr_journal_heads);
  2047. if (n)
  2048. printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
  2049. #endif
  2050. jbd2_remove_debugfs_entry();
  2051. jbd2_remove_jbd_stats_proc_entry();
  2052. jbd2_journal_destroy_caches();
  2053. }
  2054. MODULE_LICENSE("GPL");
  2055. module_init(journal_init);
  2056. module_exit(journal_exit);