journal.c 63 KB

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