journal.c 55 KB

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