journal.c 74 KB

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