journal.c 63 KB

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