journal.c 74 KB

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