journal.c 55 KB

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