journal.c 54 KB

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