journal.c 57 KB

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