journal.c 72 KB

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