journal.c 54 KB

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