journal.c 65 KB

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