journal.c 63 KB

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