journal.c 58 KB

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