journal.c 57 KB

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