journal.c 55 KB

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