journal.c 54 KB

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