journal.c 65 KB

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