journal.c 83 KB

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