journal.c 72 KB

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