journal.c 69 KB

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