journal.c 75 KB

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