journal.c 89 KB

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