journal.c 53 KB

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