journal.c 55 KB

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