journal.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  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. /* Add the dynamic fields and write it to disk. */
  831. journal_update_sb_log_tail(journal);
  832. mutex_unlock(&journal->j_checkpoint_mutex);
  833. }
  834. return journal_start_thread(journal);
  835. }
  836. /**
  837. * int journal_create() - Initialise the new journal file
  838. * @journal: Journal to create. This structure must have been initialised
  839. *
  840. * Given a journal_t structure which tells us which disk blocks we can
  841. * use, create a new journal superblock and initialise all of the
  842. * journal fields from scratch.
  843. **/
  844. int journal_create(journal_t *journal)
  845. {
  846. unsigned int blocknr;
  847. struct buffer_head *bh;
  848. journal_superblock_t *sb;
  849. int i, err;
  850. if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
  851. printk (KERN_ERR "Journal length (%d blocks) too short.\n",
  852. journal->j_maxlen);
  853. journal_fail_superblock(journal);
  854. return -EINVAL;
  855. }
  856. if (journal->j_inode == NULL) {
  857. /*
  858. * We don't know what block to start at!
  859. */
  860. printk(KERN_EMERG
  861. "%s: creation of journal on external device!\n",
  862. __func__);
  863. BUG();
  864. }
  865. /* Zero out the entire journal on disk. We cannot afford to
  866. have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
  867. jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
  868. for (i = 0; i < journal->j_maxlen; i++) {
  869. err = journal_bmap(journal, i, &blocknr);
  870. if (err)
  871. return err;
  872. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  873. if (unlikely(!bh))
  874. return -ENOMEM;
  875. lock_buffer(bh);
  876. memset (bh->b_data, 0, journal->j_blocksize);
  877. BUFFER_TRACE(bh, "marking dirty");
  878. mark_buffer_dirty(bh);
  879. BUFFER_TRACE(bh, "marking uptodate");
  880. set_buffer_uptodate(bh);
  881. unlock_buffer(bh);
  882. __brelse(bh);
  883. }
  884. sync_blockdev(journal->j_dev);
  885. jbd_debug(1, "JBD: journal cleared.\n");
  886. /* OK, fill in the initial static fields in the new superblock */
  887. sb = journal->j_superblock;
  888. sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
  889. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  890. sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
  891. sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
  892. sb->s_first = cpu_to_be32(1);
  893. journal->j_transaction_sequence = 1;
  894. journal->j_flags &= ~JFS_ABORT;
  895. journal->j_format_version = 2;
  896. return journal_reset(journal);
  897. }
  898. static void journal_write_superblock(journal_t *journal)
  899. {
  900. struct buffer_head *bh = journal->j_sb_buffer;
  901. trace_journal_write_superblock(journal);
  902. if (buffer_write_io_error(bh)) {
  903. char b[BDEVNAME_SIZE];
  904. /*
  905. * Oh, dear. A previous attempt to write the journal
  906. * superblock failed. This could happen because the
  907. * USB device was yanked out. Or it could happen to
  908. * be a transient write error and maybe the block will
  909. * be remapped. Nothing we can do but to retry the
  910. * write and hope for the best.
  911. */
  912. printk(KERN_ERR "JBD: previous I/O error detected "
  913. "for journal superblock update for %s.\n",
  914. journal_dev_name(journal, b));
  915. clear_buffer_write_io_error(bh);
  916. set_buffer_uptodate(bh);
  917. }
  918. BUFFER_TRACE(bh, "marking dirty");
  919. mark_buffer_dirty(bh);
  920. sync_dirty_buffer(bh);
  921. if (buffer_write_io_error(bh)) {
  922. char b[BDEVNAME_SIZE];
  923. printk(KERN_ERR "JBD: I/O error detected "
  924. "when updating journal superblock for %s.\n",
  925. journal_dev_name(journal, b));
  926. clear_buffer_write_io_error(bh);
  927. set_buffer_uptodate(bh);
  928. }
  929. }
  930. /**
  931. * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
  932. * @journal: The journal to update.
  933. *
  934. * Update a journal's superblock information about log tail and write it to
  935. * disk, waiting for the IO to complete.
  936. */
  937. void journal_update_sb_log_tail(journal_t *journal)
  938. {
  939. journal_superblock_t *sb = journal->j_superblock;
  940. BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
  941. spin_lock(&journal->j_state_lock);
  942. jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
  943. journal->j_tail, journal->j_tail_sequence, journal->j_errno);
  944. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  945. sb->s_start = cpu_to_be32(journal->j_tail);
  946. spin_unlock(&journal->j_state_lock);
  947. journal_write_superblock(journal);
  948. /* Log is no longer empty */
  949. spin_lock(&journal->j_state_lock);
  950. WARN_ON(!sb->s_sequence);
  951. journal->j_flags &= ~JFS_FLUSHED;
  952. spin_unlock(&journal->j_state_lock);
  953. }
  954. /**
  955. * mark_journal_empty() - Mark on disk journal as empty.
  956. * @journal: The journal to update.
  957. *
  958. * Update a journal's dynamic superblock fields to show that journal is empty.
  959. * Write updated superblock to disk waiting for IO to complete.
  960. */
  961. static void mark_journal_empty(journal_t *journal)
  962. {
  963. journal_superblock_t *sb = journal->j_superblock;
  964. BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
  965. spin_lock(&journal->j_state_lock);
  966. jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
  967. journal->j_tail_sequence);
  968. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  969. sb->s_start = cpu_to_be32(0);
  970. spin_unlock(&journal->j_state_lock);
  971. journal_write_superblock(journal);
  972. spin_lock(&journal->j_state_lock);
  973. /* Log is empty */
  974. journal->j_flags |= JFS_FLUSHED;
  975. spin_unlock(&journal->j_state_lock);
  976. }
  977. /**
  978. * journal_update_sb_errno() - Update error in the journal.
  979. * @journal: The journal to update.
  980. *
  981. * Update a journal's errno. Write updated superblock to disk waiting for IO
  982. * to complete.
  983. */
  984. static void journal_update_sb_errno(journal_t *journal)
  985. {
  986. journal_superblock_t *sb = journal->j_superblock;
  987. spin_lock(&journal->j_state_lock);
  988. jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
  989. journal->j_errno);
  990. sb->s_errno = cpu_to_be32(journal->j_errno);
  991. spin_unlock(&journal->j_state_lock);
  992. journal_write_superblock(journal);
  993. }
  994. /*
  995. * Read the superblock for a given journal, performing initial
  996. * validation of the format.
  997. */
  998. static int journal_get_superblock(journal_t *journal)
  999. {
  1000. struct buffer_head *bh;
  1001. journal_superblock_t *sb;
  1002. int err = -EIO;
  1003. bh = journal->j_sb_buffer;
  1004. J_ASSERT(bh != NULL);
  1005. if (!buffer_uptodate(bh)) {
  1006. ll_rw_block(READ, 1, &bh);
  1007. wait_on_buffer(bh);
  1008. if (!buffer_uptodate(bh)) {
  1009. printk (KERN_ERR
  1010. "JBD: IO error reading journal superblock\n");
  1011. goto out;
  1012. }
  1013. }
  1014. sb = journal->j_superblock;
  1015. err = -EINVAL;
  1016. if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
  1017. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  1018. printk(KERN_WARNING "JBD: no valid journal superblock found\n");
  1019. goto out;
  1020. }
  1021. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  1022. case JFS_SUPERBLOCK_V1:
  1023. journal->j_format_version = 1;
  1024. break;
  1025. case JFS_SUPERBLOCK_V2:
  1026. journal->j_format_version = 2;
  1027. break;
  1028. default:
  1029. printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
  1030. goto out;
  1031. }
  1032. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  1033. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  1034. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  1035. printk (KERN_WARNING "JBD: journal file too short\n");
  1036. goto out;
  1037. }
  1038. if (be32_to_cpu(sb->s_first) == 0 ||
  1039. be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
  1040. printk(KERN_WARNING
  1041. "JBD: Invalid start block of journal: %u\n",
  1042. be32_to_cpu(sb->s_first));
  1043. goto out;
  1044. }
  1045. return 0;
  1046. out:
  1047. journal_fail_superblock(journal);
  1048. return err;
  1049. }
  1050. /*
  1051. * Load the on-disk journal superblock and read the key fields into the
  1052. * journal_t.
  1053. */
  1054. static int load_superblock(journal_t *journal)
  1055. {
  1056. int err;
  1057. journal_superblock_t *sb;
  1058. err = journal_get_superblock(journal);
  1059. if (err)
  1060. return err;
  1061. sb = journal->j_superblock;
  1062. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1063. journal->j_tail = be32_to_cpu(sb->s_start);
  1064. journal->j_first = be32_to_cpu(sb->s_first);
  1065. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1066. journal->j_errno = be32_to_cpu(sb->s_errno);
  1067. return 0;
  1068. }
  1069. /**
  1070. * int journal_load() - Read journal from disk.
  1071. * @journal: Journal to act on.
  1072. *
  1073. * Given a journal_t structure which tells us which disk blocks contain
  1074. * a journal, read the journal from disk to initialise the in-memory
  1075. * structures.
  1076. */
  1077. int journal_load(journal_t *journal)
  1078. {
  1079. int err;
  1080. journal_superblock_t *sb;
  1081. err = load_superblock(journal);
  1082. if (err)
  1083. return err;
  1084. sb = journal->j_superblock;
  1085. /* If this is a V2 superblock, then we have to check the
  1086. * features flags on it. */
  1087. if (journal->j_format_version >= 2) {
  1088. if ((sb->s_feature_ro_compat &
  1089. ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
  1090. (sb->s_feature_incompat &
  1091. ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
  1092. printk (KERN_WARNING
  1093. "JBD: Unrecognised features on journal\n");
  1094. return -EINVAL;
  1095. }
  1096. }
  1097. /* Let the recovery code check whether it needs to recover any
  1098. * data from the journal. */
  1099. if (journal_recover(journal))
  1100. goto recovery_error;
  1101. /* OK, we've finished with the dynamic journal bits:
  1102. * reinitialise the dynamic contents of the superblock in memory
  1103. * and reset them on disk. */
  1104. if (journal_reset(journal))
  1105. goto recovery_error;
  1106. journal->j_flags &= ~JFS_ABORT;
  1107. journal->j_flags |= JFS_LOADED;
  1108. return 0;
  1109. recovery_error:
  1110. printk (KERN_WARNING "JBD: recovery failed\n");
  1111. return -EIO;
  1112. }
  1113. /**
  1114. * void journal_destroy() - Release a journal_t structure.
  1115. * @journal: Journal to act on.
  1116. *
  1117. * Release a journal_t structure once it is no longer in use by the
  1118. * journaled object.
  1119. * Return <0 if we couldn't clean up the journal.
  1120. */
  1121. int journal_destroy(journal_t *journal)
  1122. {
  1123. int err = 0;
  1124. /* Wait for the commit thread to wake up and die. */
  1125. journal_kill_thread(journal);
  1126. /* Force a final log commit */
  1127. if (journal->j_running_transaction)
  1128. journal_commit_transaction(journal);
  1129. /* Force any old transactions to disk */
  1130. /* We cannot race with anybody but must keep assertions happy */
  1131. mutex_lock(&journal->j_checkpoint_mutex);
  1132. /* Totally anal locking here... */
  1133. spin_lock(&journal->j_list_lock);
  1134. while (journal->j_checkpoint_transactions != NULL) {
  1135. spin_unlock(&journal->j_list_lock);
  1136. log_do_checkpoint(journal);
  1137. spin_lock(&journal->j_list_lock);
  1138. }
  1139. J_ASSERT(journal->j_running_transaction == NULL);
  1140. J_ASSERT(journal->j_committing_transaction == NULL);
  1141. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1142. spin_unlock(&journal->j_list_lock);
  1143. if (journal->j_sb_buffer) {
  1144. if (!is_journal_aborted(journal)) {
  1145. journal->j_tail_sequence =
  1146. ++journal->j_transaction_sequence;
  1147. mark_journal_empty(journal);
  1148. } else
  1149. err = -EIO;
  1150. brelse(journal->j_sb_buffer);
  1151. }
  1152. mutex_unlock(&journal->j_checkpoint_mutex);
  1153. if (journal->j_inode)
  1154. iput(journal->j_inode);
  1155. if (journal->j_revoke)
  1156. journal_destroy_revoke(journal);
  1157. kfree(journal->j_wbuf);
  1158. kfree(journal);
  1159. return err;
  1160. }
  1161. /**
  1162. *int journal_check_used_features () - Check if features specified are used.
  1163. * @journal: Journal to check.
  1164. * @compat: bitmask of compatible features
  1165. * @ro: bitmask of features that force read-only mount
  1166. * @incompat: bitmask of incompatible features
  1167. *
  1168. * Check whether the journal uses all of a given set of
  1169. * features. Return true (non-zero) if it does.
  1170. **/
  1171. int journal_check_used_features (journal_t *journal, unsigned long compat,
  1172. unsigned long ro, unsigned long incompat)
  1173. {
  1174. journal_superblock_t *sb;
  1175. if (!compat && !ro && !incompat)
  1176. return 1;
  1177. if (journal->j_format_version == 1)
  1178. return 0;
  1179. sb = journal->j_superblock;
  1180. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1181. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1182. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1183. return 1;
  1184. return 0;
  1185. }
  1186. /**
  1187. * int journal_check_available_features() - Check feature set in journalling layer
  1188. * @journal: Journal to check.
  1189. * @compat: bitmask of compatible features
  1190. * @ro: bitmask of features that force read-only mount
  1191. * @incompat: bitmask of incompatible features
  1192. *
  1193. * Check whether the journaling code supports the use of
  1194. * all of a given set of features on this journal. Return true
  1195. * (non-zero) if it can. */
  1196. int journal_check_available_features (journal_t *journal, unsigned long compat,
  1197. unsigned long ro, unsigned long incompat)
  1198. {
  1199. if (!compat && !ro && !incompat)
  1200. return 1;
  1201. /* We can support any known requested features iff the
  1202. * superblock is in version 2. Otherwise we fail to support any
  1203. * extended sb features. */
  1204. if (journal->j_format_version != 2)
  1205. return 0;
  1206. if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
  1207. (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1208. (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
  1209. return 1;
  1210. return 0;
  1211. }
  1212. /**
  1213. * int journal_set_features () - Mark a given journal feature in the superblock
  1214. * @journal: Journal to act on.
  1215. * @compat: bitmask of compatible features
  1216. * @ro: bitmask of features that force read-only mount
  1217. * @incompat: bitmask of incompatible features
  1218. *
  1219. * Mark a given journal feature as present on the
  1220. * superblock. Returns true if the requested features could be set.
  1221. *
  1222. */
  1223. int journal_set_features (journal_t *journal, unsigned long compat,
  1224. unsigned long ro, unsigned long incompat)
  1225. {
  1226. journal_superblock_t *sb;
  1227. if (journal_check_used_features(journal, compat, ro, incompat))
  1228. return 1;
  1229. if (!journal_check_available_features(journal, compat, ro, incompat))
  1230. return 0;
  1231. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1232. compat, ro, incompat);
  1233. sb = journal->j_superblock;
  1234. sb->s_feature_compat |= cpu_to_be32(compat);
  1235. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1236. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1237. return 1;
  1238. }
  1239. /**
  1240. * int journal_update_format () - Update on-disk journal structure.
  1241. * @journal: Journal to act on.
  1242. *
  1243. * Given an initialised but unloaded journal struct, poke about in the
  1244. * on-disk structure to update it to the most recent supported version.
  1245. */
  1246. int journal_update_format (journal_t *journal)
  1247. {
  1248. journal_superblock_t *sb;
  1249. int err;
  1250. err = journal_get_superblock(journal);
  1251. if (err)
  1252. return err;
  1253. sb = journal->j_superblock;
  1254. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1255. case JFS_SUPERBLOCK_V2:
  1256. return 0;
  1257. case JFS_SUPERBLOCK_V1:
  1258. return journal_convert_superblock_v1(journal, sb);
  1259. default:
  1260. break;
  1261. }
  1262. return -EINVAL;
  1263. }
  1264. static int journal_convert_superblock_v1(journal_t *journal,
  1265. journal_superblock_t *sb)
  1266. {
  1267. int offset, blocksize;
  1268. struct buffer_head *bh;
  1269. printk(KERN_WARNING
  1270. "JBD: Converting superblock from version 1 to 2.\n");
  1271. /* Pre-initialise new fields to zero */
  1272. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1273. blocksize = be32_to_cpu(sb->s_blocksize);
  1274. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1275. sb->s_nr_users = cpu_to_be32(1);
  1276. sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
  1277. journal->j_format_version = 2;
  1278. bh = journal->j_sb_buffer;
  1279. BUFFER_TRACE(bh, "marking dirty");
  1280. mark_buffer_dirty(bh);
  1281. sync_dirty_buffer(bh);
  1282. return 0;
  1283. }
  1284. /**
  1285. * int journal_flush () - Flush journal
  1286. * @journal: Journal to act on.
  1287. *
  1288. * Flush all data for a given journal to disk and empty the journal.
  1289. * Filesystems can use this when remounting readonly to ensure that
  1290. * recovery does not need to happen on remount.
  1291. */
  1292. int journal_flush(journal_t *journal)
  1293. {
  1294. int err = 0;
  1295. transaction_t *transaction = NULL;
  1296. spin_lock(&journal->j_state_lock);
  1297. /* Force everything buffered to the log... */
  1298. if (journal->j_running_transaction) {
  1299. transaction = journal->j_running_transaction;
  1300. __log_start_commit(journal, transaction->t_tid);
  1301. } else if (journal->j_committing_transaction)
  1302. transaction = journal->j_committing_transaction;
  1303. /* Wait for the log commit to complete... */
  1304. if (transaction) {
  1305. tid_t tid = transaction->t_tid;
  1306. spin_unlock(&journal->j_state_lock);
  1307. log_wait_commit(journal, tid);
  1308. } else {
  1309. spin_unlock(&journal->j_state_lock);
  1310. }
  1311. /* ...and flush everything in the log out to disk. */
  1312. spin_lock(&journal->j_list_lock);
  1313. while (!err && journal->j_checkpoint_transactions != NULL) {
  1314. spin_unlock(&journal->j_list_lock);
  1315. mutex_lock(&journal->j_checkpoint_mutex);
  1316. err = log_do_checkpoint(journal);
  1317. mutex_unlock(&journal->j_checkpoint_mutex);
  1318. spin_lock(&journal->j_list_lock);
  1319. }
  1320. spin_unlock(&journal->j_list_lock);
  1321. if (is_journal_aborted(journal))
  1322. return -EIO;
  1323. mutex_lock(&journal->j_checkpoint_mutex);
  1324. cleanup_journal_tail(journal);
  1325. /* Finally, mark the journal as really needing no recovery.
  1326. * This sets s_start==0 in the underlying superblock, which is
  1327. * the magic code for a fully-recovered superblock. Any future
  1328. * commits of data to the journal will restore the current
  1329. * s_start value. */
  1330. mark_journal_empty(journal);
  1331. mutex_unlock(&journal->j_checkpoint_mutex);
  1332. spin_lock(&journal->j_state_lock);
  1333. J_ASSERT(!journal->j_running_transaction);
  1334. J_ASSERT(!journal->j_committing_transaction);
  1335. J_ASSERT(!journal->j_checkpoint_transactions);
  1336. J_ASSERT(journal->j_head == journal->j_tail);
  1337. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1338. spin_unlock(&journal->j_state_lock);
  1339. return 0;
  1340. }
  1341. /**
  1342. * int journal_wipe() - Wipe journal contents
  1343. * @journal: Journal to act on.
  1344. * @write: flag (see below)
  1345. *
  1346. * Wipe out all of the contents of a journal, safely. This will produce
  1347. * a warning if the journal contains any valid recovery information.
  1348. * Must be called between journal_init_*() and journal_load().
  1349. *
  1350. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1351. * we merely suppress recovery.
  1352. */
  1353. int journal_wipe(journal_t *journal, int write)
  1354. {
  1355. int err = 0;
  1356. J_ASSERT (!(journal->j_flags & JFS_LOADED));
  1357. err = load_superblock(journal);
  1358. if (err)
  1359. return err;
  1360. if (!journal->j_tail)
  1361. goto no_recovery;
  1362. printk (KERN_WARNING "JBD: %s recovery information on journal\n",
  1363. write ? "Clearing" : "Ignoring");
  1364. err = journal_skip_recovery(journal);
  1365. if (write) {
  1366. /* Lock to make assertions happy... */
  1367. mutex_lock(&journal->j_checkpoint_mutex);
  1368. mark_journal_empty(journal);
  1369. mutex_unlock(&journal->j_checkpoint_mutex);
  1370. }
  1371. no_recovery:
  1372. return err;
  1373. }
  1374. /*
  1375. * journal_dev_name: format a character string to describe on what
  1376. * device this journal is present.
  1377. */
  1378. static const char *journal_dev_name(journal_t *journal, char *buffer)
  1379. {
  1380. struct block_device *bdev;
  1381. if (journal->j_inode)
  1382. bdev = journal->j_inode->i_sb->s_bdev;
  1383. else
  1384. bdev = journal->j_dev;
  1385. return bdevname(bdev, buffer);
  1386. }
  1387. /*
  1388. * Journal abort has very specific semantics, which we describe
  1389. * for journal abort.
  1390. *
  1391. * Two internal function, which provide abort to te jbd layer
  1392. * itself are here.
  1393. */
  1394. /*
  1395. * Quick version for internal journal use (doesn't lock the journal).
  1396. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1397. * and don't attempt to make any other journal updates.
  1398. */
  1399. static void __journal_abort_hard(journal_t *journal)
  1400. {
  1401. transaction_t *transaction;
  1402. char b[BDEVNAME_SIZE];
  1403. if (journal->j_flags & JFS_ABORT)
  1404. return;
  1405. printk(KERN_ERR "Aborting journal on device %s.\n",
  1406. journal_dev_name(journal, b));
  1407. spin_lock(&journal->j_state_lock);
  1408. journal->j_flags |= JFS_ABORT;
  1409. transaction = journal->j_running_transaction;
  1410. if (transaction)
  1411. __log_start_commit(journal, transaction->t_tid);
  1412. spin_unlock(&journal->j_state_lock);
  1413. }
  1414. /* Soft abort: record the abort error status in the journal superblock,
  1415. * but don't do any other IO. */
  1416. static void __journal_abort_soft (journal_t *journal, int errno)
  1417. {
  1418. if (journal->j_flags & JFS_ABORT)
  1419. return;
  1420. if (!journal->j_errno)
  1421. journal->j_errno = errno;
  1422. __journal_abort_hard(journal);
  1423. if (errno)
  1424. journal_update_sb_errno(journal);
  1425. }
  1426. /**
  1427. * void journal_abort () - Shutdown the journal immediately.
  1428. * @journal: the journal to shutdown.
  1429. * @errno: an error number to record in the journal indicating
  1430. * the reason for the shutdown.
  1431. *
  1432. * Perform a complete, immediate shutdown of the ENTIRE
  1433. * journal (not of a single transaction). This operation cannot be
  1434. * undone without closing and reopening the journal.
  1435. *
  1436. * The journal_abort function is intended to support higher level error
  1437. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1438. * mode.
  1439. *
  1440. * Journal abort has very specific semantics. Any existing dirty,
  1441. * unjournaled buffers in the main filesystem will still be written to
  1442. * disk by bdflush, but the journaling mechanism will be suspended
  1443. * immediately and no further transaction commits will be honoured.
  1444. *
  1445. * Any dirty, journaled buffers will be written back to disk without
  1446. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1447. * filesystem, but we _do_ attempt to leave as much data as possible
  1448. * behind for fsck to use for cleanup.
  1449. *
  1450. * Any attempt to get a new transaction handle on a journal which is in
  1451. * ABORT state will just result in an -EROFS error return. A
  1452. * journal_stop on an existing handle will return -EIO if we have
  1453. * entered abort state during the update.
  1454. *
  1455. * Recursive transactions are not disturbed by journal abort until the
  1456. * final journal_stop, which will receive the -EIO error.
  1457. *
  1458. * Finally, the journal_abort call allows the caller to supply an errno
  1459. * which will be recorded (if possible) in the journal superblock. This
  1460. * allows a client to record failure conditions in the middle of a
  1461. * transaction without having to complete the transaction to record the
  1462. * failure to disk. ext3_error, for example, now uses this
  1463. * functionality.
  1464. *
  1465. * Errors which originate from within the journaling layer will NOT
  1466. * supply an errno; a null errno implies that absolutely no further
  1467. * writes are done to the journal (unless there are any already in
  1468. * progress).
  1469. *
  1470. */
  1471. void journal_abort(journal_t *journal, int errno)
  1472. {
  1473. __journal_abort_soft(journal, errno);
  1474. }
  1475. /**
  1476. * int journal_errno () - returns the journal's error state.
  1477. * @journal: journal to examine.
  1478. *
  1479. * This is the errno numbet set with journal_abort(), the last
  1480. * time the journal was mounted - if the journal was stopped
  1481. * without calling abort this will be 0.
  1482. *
  1483. * If the journal has been aborted on this mount time -EROFS will
  1484. * be returned.
  1485. */
  1486. int journal_errno(journal_t *journal)
  1487. {
  1488. int err;
  1489. spin_lock(&journal->j_state_lock);
  1490. if (journal->j_flags & JFS_ABORT)
  1491. err = -EROFS;
  1492. else
  1493. err = journal->j_errno;
  1494. spin_unlock(&journal->j_state_lock);
  1495. return err;
  1496. }
  1497. /**
  1498. * int journal_clear_err () - clears the journal's error state
  1499. * @journal: journal to act on.
  1500. *
  1501. * An error must be cleared or Acked to take a FS out of readonly
  1502. * mode.
  1503. */
  1504. int journal_clear_err(journal_t *journal)
  1505. {
  1506. int err = 0;
  1507. spin_lock(&journal->j_state_lock);
  1508. if (journal->j_flags & JFS_ABORT)
  1509. err = -EROFS;
  1510. else
  1511. journal->j_errno = 0;
  1512. spin_unlock(&journal->j_state_lock);
  1513. return err;
  1514. }
  1515. /**
  1516. * void journal_ack_err() - Ack journal err.
  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. void journal_ack_err(journal_t *journal)
  1523. {
  1524. spin_lock(&journal->j_state_lock);
  1525. if (journal->j_errno)
  1526. journal->j_flags |= JFS_ACK_ERR;
  1527. spin_unlock(&journal->j_state_lock);
  1528. }
  1529. int journal_blocks_per_page(struct inode *inode)
  1530. {
  1531. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1532. }
  1533. /*
  1534. * Journal_head storage management
  1535. */
  1536. static struct kmem_cache *journal_head_cache;
  1537. #ifdef CONFIG_JBD_DEBUG
  1538. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1539. #endif
  1540. static int journal_init_journal_head_cache(void)
  1541. {
  1542. int retval;
  1543. J_ASSERT(journal_head_cache == NULL);
  1544. journal_head_cache = kmem_cache_create("journal_head",
  1545. sizeof(struct journal_head),
  1546. 0, /* offset */
  1547. SLAB_TEMPORARY, /* flags */
  1548. NULL); /* ctor */
  1549. retval = 0;
  1550. if (!journal_head_cache) {
  1551. retval = -ENOMEM;
  1552. printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
  1553. }
  1554. return retval;
  1555. }
  1556. static void journal_destroy_journal_head_cache(void)
  1557. {
  1558. if (journal_head_cache) {
  1559. kmem_cache_destroy(journal_head_cache);
  1560. journal_head_cache = NULL;
  1561. }
  1562. }
  1563. /*
  1564. * journal_head splicing and dicing
  1565. */
  1566. static struct journal_head *journal_alloc_journal_head(void)
  1567. {
  1568. struct journal_head *ret;
  1569. #ifdef CONFIG_JBD_DEBUG
  1570. atomic_inc(&nr_journal_heads);
  1571. #endif
  1572. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1573. if (ret == NULL) {
  1574. jbd_debug(1, "out of memory for journal_head\n");
  1575. printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
  1576. __func__);
  1577. while (ret == NULL) {
  1578. yield();
  1579. ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
  1580. }
  1581. }
  1582. return ret;
  1583. }
  1584. static void journal_free_journal_head(struct journal_head *jh)
  1585. {
  1586. #ifdef CONFIG_JBD_DEBUG
  1587. atomic_dec(&nr_journal_heads);
  1588. memset(jh, JBD_POISON_FREE, sizeof(*jh));
  1589. #endif
  1590. kmem_cache_free(journal_head_cache, jh);
  1591. }
  1592. /*
  1593. * A journal_head is attached to a buffer_head whenever JBD has an
  1594. * interest in the buffer.
  1595. *
  1596. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1597. * is set. This bit is tested in core kernel code where we need to take
  1598. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1599. * there.
  1600. *
  1601. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1602. *
  1603. * When a buffer has its BH_JBD bit set it is immune from being released by
  1604. * core kernel code, mainly via ->b_count.
  1605. *
  1606. * A journal_head is detached from its buffer_head when the journal_head's
  1607. * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
  1608. * transaction (b_cp_transaction) hold their references to b_jcount.
  1609. *
  1610. * Various places in the kernel want to attach a journal_head to a buffer_head
  1611. * _before_ attaching the journal_head to a transaction. To protect the
  1612. * journal_head in this situation, journal_add_journal_head elevates the
  1613. * journal_head's b_jcount refcount by one. The caller must call
  1614. * journal_put_journal_head() to undo this.
  1615. *
  1616. * So the typical usage would be:
  1617. *
  1618. * (Attach a journal_head if needed. Increments b_jcount)
  1619. * struct journal_head *jh = journal_add_journal_head(bh);
  1620. * ...
  1621. * (Get another reference for transaction)
  1622. * journal_grab_journal_head(bh);
  1623. * jh->b_transaction = xxx;
  1624. * (Put original reference)
  1625. * journal_put_journal_head(jh);
  1626. */
  1627. /*
  1628. * Give a buffer_head a journal_head.
  1629. *
  1630. * May sleep.
  1631. */
  1632. struct journal_head *journal_add_journal_head(struct buffer_head *bh)
  1633. {
  1634. struct journal_head *jh;
  1635. struct journal_head *new_jh = NULL;
  1636. repeat:
  1637. if (!buffer_jbd(bh)) {
  1638. new_jh = journal_alloc_journal_head();
  1639. memset(new_jh, 0, sizeof(*new_jh));
  1640. }
  1641. jbd_lock_bh_journal_head(bh);
  1642. if (buffer_jbd(bh)) {
  1643. jh = bh2jh(bh);
  1644. } else {
  1645. J_ASSERT_BH(bh,
  1646. (atomic_read(&bh->b_count) > 0) ||
  1647. (bh->b_page && bh->b_page->mapping));
  1648. if (!new_jh) {
  1649. jbd_unlock_bh_journal_head(bh);
  1650. goto repeat;
  1651. }
  1652. jh = new_jh;
  1653. new_jh = NULL; /* We consumed it */
  1654. set_buffer_jbd(bh);
  1655. bh->b_private = jh;
  1656. jh->b_bh = bh;
  1657. get_bh(bh);
  1658. BUFFER_TRACE(bh, "added journal_head");
  1659. }
  1660. jh->b_jcount++;
  1661. jbd_unlock_bh_journal_head(bh);
  1662. if (new_jh)
  1663. journal_free_journal_head(new_jh);
  1664. return bh->b_private;
  1665. }
  1666. /*
  1667. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1668. * having a journal_head, return NULL
  1669. */
  1670. struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
  1671. {
  1672. struct journal_head *jh = NULL;
  1673. jbd_lock_bh_journal_head(bh);
  1674. if (buffer_jbd(bh)) {
  1675. jh = bh2jh(bh);
  1676. jh->b_jcount++;
  1677. }
  1678. jbd_unlock_bh_journal_head(bh);
  1679. return jh;
  1680. }
  1681. static void __journal_remove_journal_head(struct buffer_head *bh)
  1682. {
  1683. struct journal_head *jh = bh2jh(bh);
  1684. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1685. J_ASSERT_JH(jh, jh->b_transaction == NULL);
  1686. J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
  1687. J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
  1688. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1689. J_ASSERT_BH(bh, buffer_jbd(bh));
  1690. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1691. BUFFER_TRACE(bh, "remove journal_head");
  1692. if (jh->b_frozen_data) {
  1693. printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
  1694. jbd_free(jh->b_frozen_data, bh->b_size);
  1695. }
  1696. if (jh->b_committed_data) {
  1697. printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
  1698. jbd_free(jh->b_committed_data, bh->b_size);
  1699. }
  1700. bh->b_private = NULL;
  1701. jh->b_bh = NULL; /* debug, really */
  1702. clear_buffer_jbd(bh);
  1703. journal_free_journal_head(jh);
  1704. }
  1705. /*
  1706. * Drop a reference on the passed journal_head. If it fell to zero then
  1707. * release the journal_head from the buffer_head.
  1708. */
  1709. void journal_put_journal_head(struct journal_head *jh)
  1710. {
  1711. struct buffer_head *bh = jh2bh(jh);
  1712. jbd_lock_bh_journal_head(bh);
  1713. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1714. --jh->b_jcount;
  1715. if (!jh->b_jcount) {
  1716. __journal_remove_journal_head(bh);
  1717. jbd_unlock_bh_journal_head(bh);
  1718. __brelse(bh);
  1719. } else
  1720. jbd_unlock_bh_journal_head(bh);
  1721. }
  1722. /*
  1723. * debugfs tunables
  1724. */
  1725. #ifdef CONFIG_JBD_DEBUG
  1726. u8 journal_enable_debug __read_mostly;
  1727. EXPORT_SYMBOL(journal_enable_debug);
  1728. static struct dentry *jbd_debugfs_dir;
  1729. static struct dentry *jbd_debug;
  1730. static void __init jbd_create_debugfs_entry(void)
  1731. {
  1732. jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
  1733. if (jbd_debugfs_dir)
  1734. jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
  1735. jbd_debugfs_dir,
  1736. &journal_enable_debug);
  1737. }
  1738. static void __exit jbd_remove_debugfs_entry(void)
  1739. {
  1740. debugfs_remove(jbd_debug);
  1741. debugfs_remove(jbd_debugfs_dir);
  1742. }
  1743. #else
  1744. static inline void jbd_create_debugfs_entry(void)
  1745. {
  1746. }
  1747. static inline void jbd_remove_debugfs_entry(void)
  1748. {
  1749. }
  1750. #endif
  1751. struct kmem_cache *jbd_handle_cache;
  1752. static int __init journal_init_handle_cache(void)
  1753. {
  1754. jbd_handle_cache = kmem_cache_create("journal_handle",
  1755. sizeof(handle_t),
  1756. 0, /* offset */
  1757. SLAB_TEMPORARY, /* flags */
  1758. NULL); /* ctor */
  1759. if (jbd_handle_cache == NULL) {
  1760. printk(KERN_EMERG "JBD: failed to create handle cache\n");
  1761. return -ENOMEM;
  1762. }
  1763. return 0;
  1764. }
  1765. static void journal_destroy_handle_cache(void)
  1766. {
  1767. if (jbd_handle_cache)
  1768. kmem_cache_destroy(jbd_handle_cache);
  1769. }
  1770. /*
  1771. * Module startup and shutdown
  1772. */
  1773. static int __init journal_init_caches(void)
  1774. {
  1775. int ret;
  1776. ret = journal_init_revoke_caches();
  1777. if (ret == 0)
  1778. ret = journal_init_journal_head_cache();
  1779. if (ret == 0)
  1780. ret = journal_init_handle_cache();
  1781. return ret;
  1782. }
  1783. static void journal_destroy_caches(void)
  1784. {
  1785. journal_destroy_revoke_caches();
  1786. journal_destroy_journal_head_cache();
  1787. journal_destroy_handle_cache();
  1788. }
  1789. static int __init journal_init(void)
  1790. {
  1791. int ret;
  1792. BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
  1793. ret = journal_init_caches();
  1794. if (ret != 0)
  1795. journal_destroy_caches();
  1796. jbd_create_debugfs_entry();
  1797. return ret;
  1798. }
  1799. static void __exit journal_exit(void)
  1800. {
  1801. #ifdef CONFIG_JBD_DEBUG
  1802. int n = atomic_read(&nr_journal_heads);
  1803. if (n)
  1804. printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
  1805. #endif
  1806. jbd_remove_debugfs_entry();
  1807. journal_destroy_caches();
  1808. }
  1809. MODULE_LICENSE("GPL");
  1810. module_init(journal_init);
  1811. module_exit(journal_exit);