journal.c 56 KB

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