journal.c 66 KB

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