journal.c 73 KB

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