journal.c 73 KB

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