journal.c 62 KB

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