journal.c 90 KB

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