journal.c 90 KB

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