journal.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437
  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 <linux/seq_file.h>
  39. #include <linux/math64.h>
  40. #include <linux/hash.h>
  41. #include <linux/log2.h>
  42. #include <linux/vmalloc.h>
  43. #define CREATE_TRACE_POINTS
  44. #include <trace/events/jbd2.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/page.h>
  47. EXPORT_SYMBOL(jbd2_journal_start);
  48. EXPORT_SYMBOL(jbd2_journal_restart);
  49. EXPORT_SYMBOL(jbd2_journal_extend);
  50. EXPORT_SYMBOL(jbd2_journal_stop);
  51. EXPORT_SYMBOL(jbd2_journal_lock_updates);
  52. EXPORT_SYMBOL(jbd2_journal_unlock_updates);
  53. EXPORT_SYMBOL(jbd2_journal_get_write_access);
  54. EXPORT_SYMBOL(jbd2_journal_get_create_access);
  55. EXPORT_SYMBOL(jbd2_journal_get_undo_access);
  56. EXPORT_SYMBOL(jbd2_journal_set_triggers);
  57. EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
  58. EXPORT_SYMBOL(jbd2_journal_release_buffer);
  59. EXPORT_SYMBOL(jbd2_journal_forget);
  60. #if 0
  61. EXPORT_SYMBOL(journal_sync_buffer);
  62. #endif
  63. EXPORT_SYMBOL(jbd2_journal_flush);
  64. EXPORT_SYMBOL(jbd2_journal_revoke);
  65. EXPORT_SYMBOL(jbd2_journal_init_dev);
  66. EXPORT_SYMBOL(jbd2_journal_init_inode);
  67. EXPORT_SYMBOL(jbd2_journal_update_format);
  68. EXPORT_SYMBOL(jbd2_journal_check_used_features);
  69. EXPORT_SYMBOL(jbd2_journal_check_available_features);
  70. EXPORT_SYMBOL(jbd2_journal_set_features);
  71. EXPORT_SYMBOL(jbd2_journal_load);
  72. EXPORT_SYMBOL(jbd2_journal_destroy);
  73. EXPORT_SYMBOL(jbd2_journal_abort);
  74. EXPORT_SYMBOL(jbd2_journal_errno);
  75. EXPORT_SYMBOL(jbd2_journal_ack_err);
  76. EXPORT_SYMBOL(jbd2_journal_clear_err);
  77. EXPORT_SYMBOL(jbd2_log_wait_commit);
  78. EXPORT_SYMBOL(jbd2_log_start_commit);
  79. EXPORT_SYMBOL(jbd2_journal_start_commit);
  80. EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
  81. EXPORT_SYMBOL(jbd2_journal_wipe);
  82. EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
  83. EXPORT_SYMBOL(jbd2_journal_invalidatepage);
  84. EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
  85. EXPORT_SYMBOL(jbd2_journal_force_commit);
  86. EXPORT_SYMBOL(jbd2_journal_file_inode);
  87. EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
  88. EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
  89. EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
  90. static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
  91. static void __journal_abort_soft (journal_t *journal, int errno);
  92. static int jbd2_journal_create_slab(size_t slab_size);
  93. /*
  94. * Helper function used to manage commit timeouts
  95. */
  96. static void commit_timeout(unsigned long __data)
  97. {
  98. struct task_struct * p = (struct task_struct *) __data;
  99. wake_up_process(p);
  100. }
  101. /*
  102. * kjournald2: The main thread function used to manage a logging device
  103. * journal.
  104. *
  105. * This kernel thread is responsible for two things:
  106. *
  107. * 1) COMMIT: Every so often we need to commit the current state of the
  108. * filesystem to disk. The journal thread is responsible for writing
  109. * all of the metadata buffers to disk.
  110. *
  111. * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
  112. * of the data in that part of the log has been rewritten elsewhere on
  113. * the disk. Flushing these old buffers to reclaim space in the log is
  114. * known as checkpointing, and this thread is responsible for that job.
  115. */
  116. static int kjournald2(void *arg)
  117. {
  118. journal_t *journal = arg;
  119. transaction_t *transaction;
  120. /*
  121. * Set up an interval timer which can be used to trigger a commit wakeup
  122. * after the commit interval expires
  123. */
  124. setup_timer(&journal->j_commit_timer, commit_timeout,
  125. (unsigned long)current);
  126. /* Record that the journal thread is running */
  127. journal->j_task = current;
  128. wake_up(&journal->j_wait_done_commit);
  129. /*
  130. * And now, wait forever for commit wakeup events.
  131. */
  132. spin_lock(&journal->j_state_lock);
  133. loop:
  134. if (journal->j_flags & JBD2_UNMOUNT)
  135. goto end_loop;
  136. jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
  137. journal->j_commit_sequence, journal->j_commit_request);
  138. if (journal->j_commit_sequence != journal->j_commit_request) {
  139. jbd_debug(1, "OK, requests differ\n");
  140. spin_unlock(&journal->j_state_lock);
  141. del_timer_sync(&journal->j_commit_timer);
  142. jbd2_journal_commit_transaction(journal);
  143. spin_lock(&journal->j_state_lock);
  144. goto loop;
  145. }
  146. wake_up(&journal->j_wait_done_commit);
  147. if (freezing(current)) {
  148. /*
  149. * The simpler the better. Flushing journal isn't a
  150. * good idea, because that depends on threads that may
  151. * be already stopped.
  152. */
  153. jbd_debug(1, "Now suspending kjournald2\n");
  154. spin_unlock(&journal->j_state_lock);
  155. refrigerator();
  156. spin_lock(&journal->j_state_lock);
  157. } else {
  158. /*
  159. * We assume on resume that commits are already there,
  160. * so we don't sleep
  161. */
  162. DEFINE_WAIT(wait);
  163. int should_sleep = 1;
  164. prepare_to_wait(&journal->j_wait_commit, &wait,
  165. TASK_INTERRUPTIBLE);
  166. if (journal->j_commit_sequence != journal->j_commit_request)
  167. should_sleep = 0;
  168. transaction = journal->j_running_transaction;
  169. if (transaction && time_after_eq(jiffies,
  170. transaction->t_expires))
  171. should_sleep = 0;
  172. if (journal->j_flags & JBD2_UNMOUNT)
  173. should_sleep = 0;
  174. if (should_sleep) {
  175. spin_unlock(&journal->j_state_lock);
  176. schedule();
  177. spin_lock(&journal->j_state_lock);
  178. }
  179. finish_wait(&journal->j_wait_commit, &wait);
  180. }
  181. jbd_debug(1, "kjournald2 wakes\n");
  182. /*
  183. * Were we woken up by a commit wakeup event?
  184. */
  185. transaction = journal->j_running_transaction;
  186. if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
  187. journal->j_commit_request = transaction->t_tid;
  188. jbd_debug(1, "woke because of timeout\n");
  189. }
  190. goto loop;
  191. end_loop:
  192. spin_unlock(&journal->j_state_lock);
  193. del_timer_sync(&journal->j_commit_timer);
  194. journal->j_task = NULL;
  195. wake_up(&journal->j_wait_done_commit);
  196. jbd_debug(1, "Journal thread exiting.\n");
  197. return 0;
  198. }
  199. static int jbd2_journal_start_thread(journal_t *journal)
  200. {
  201. struct task_struct *t;
  202. t = kthread_run(kjournald2, journal, "jbd2/%s",
  203. journal->j_devname);
  204. if (IS_ERR(t))
  205. return PTR_ERR(t);
  206. wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
  207. return 0;
  208. }
  209. static void journal_kill_thread(journal_t *journal)
  210. {
  211. spin_lock(&journal->j_state_lock);
  212. journal->j_flags |= JBD2_UNMOUNT;
  213. while (journal->j_task) {
  214. wake_up(&journal->j_wait_commit);
  215. spin_unlock(&journal->j_state_lock);
  216. wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
  217. spin_lock(&journal->j_state_lock);
  218. }
  219. spin_unlock(&journal->j_state_lock);
  220. }
  221. /*
  222. * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
  223. *
  224. * Writes a metadata buffer to a given disk block. The actual IO is not
  225. * performed but a new buffer_head is constructed which labels the data
  226. * to be written with the correct destination disk block.
  227. *
  228. * Any magic-number escaping which needs to be done will cause a
  229. * copy-out here. If the buffer happens to start with the
  230. * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
  231. * magic number is only written to the log for descripter blocks. In
  232. * this case, we copy the data and replace the first word with 0, and we
  233. * return a result code which indicates that this buffer needs to be
  234. * marked as an escaped buffer in the corresponding log descriptor
  235. * block. The missing word can then be restored when the block is read
  236. * during recovery.
  237. *
  238. * If the source buffer has already been modified by a new transaction
  239. * since we took the last commit snapshot, we use the frozen copy of
  240. * that data for IO. If we end up using the existing buffer_head's data
  241. * for the write, then we *have* to lock the buffer to prevent anyone
  242. * else from using and possibly modifying it while the IO is in
  243. * progress.
  244. *
  245. * The function returns a pointer to the buffer_heads to be used for IO.
  246. *
  247. * We assume that the journal has already been locked in this function.
  248. *
  249. * Return value:
  250. * <0: Error
  251. * >=0: Finished OK
  252. *
  253. * On success:
  254. * Bit 0 set == escape performed on the data
  255. * Bit 1 set == buffer copy-out performed (kfree the data after IO)
  256. */
  257. int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
  258. struct journal_head *jh_in,
  259. struct journal_head **jh_out,
  260. unsigned long long blocknr)
  261. {
  262. int need_copy_out = 0;
  263. int done_copy_out = 0;
  264. int do_escape = 0;
  265. char *mapped_data;
  266. struct buffer_head *new_bh;
  267. struct journal_head *new_jh;
  268. struct page *new_page;
  269. unsigned int new_offset;
  270. struct buffer_head *bh_in = jh2bh(jh_in);
  271. journal_t *journal = transaction->t_journal;
  272. /*
  273. * The buffer really shouldn't be locked: only the current committing
  274. * transaction is allowed to write it, so nobody else is allowed
  275. * to do any IO.
  276. *
  277. * akpm: except if we're journalling data, and write() output is
  278. * also part of a shared mapping, and another thread has
  279. * decided to launch a writepage() against this buffer.
  280. */
  281. J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
  282. new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
  283. /* keep subsequent assertions sane */
  284. new_bh->b_state = 0;
  285. init_buffer(new_bh, NULL, NULL);
  286. atomic_set(&new_bh->b_count, 1);
  287. new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
  288. /*
  289. * If a new transaction has already done a buffer copy-out, then
  290. * we use that version of the data for the commit.
  291. */
  292. jbd_lock_bh_state(bh_in);
  293. repeat:
  294. if (jh_in->b_frozen_data) {
  295. done_copy_out = 1;
  296. new_page = virt_to_page(jh_in->b_frozen_data);
  297. new_offset = offset_in_page(jh_in->b_frozen_data);
  298. } else {
  299. new_page = jh2bh(jh_in)->b_page;
  300. new_offset = offset_in_page(jh2bh(jh_in)->b_data);
  301. }
  302. mapped_data = kmap_atomic(new_page, KM_USER0);
  303. /*
  304. * Fire data frozen trigger if data already wasn't frozen. Do this
  305. * before checking for escaping, as the trigger may modify the magic
  306. * offset. If a copy-out happens afterwards, it will have the correct
  307. * data in the buffer.
  308. */
  309. if (!done_copy_out)
  310. jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
  311. jh_in->b_triggers);
  312. /*
  313. * Check for escaping
  314. */
  315. if (*((__be32 *)(mapped_data + new_offset)) ==
  316. cpu_to_be32(JBD2_MAGIC_NUMBER)) {
  317. need_copy_out = 1;
  318. do_escape = 1;
  319. }
  320. kunmap_atomic(mapped_data, KM_USER0);
  321. /*
  322. * Do we need to do a data copy?
  323. */
  324. if (need_copy_out && !done_copy_out) {
  325. char *tmp;
  326. jbd_unlock_bh_state(bh_in);
  327. tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
  328. if (!tmp) {
  329. jbd2_journal_put_journal_head(new_jh);
  330. return -ENOMEM;
  331. }
  332. jbd_lock_bh_state(bh_in);
  333. if (jh_in->b_frozen_data) {
  334. jbd2_free(tmp, bh_in->b_size);
  335. goto repeat;
  336. }
  337. jh_in->b_frozen_data = tmp;
  338. mapped_data = kmap_atomic(new_page, KM_USER0);
  339. memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
  340. kunmap_atomic(mapped_data, KM_USER0);
  341. new_page = virt_to_page(tmp);
  342. new_offset = offset_in_page(tmp);
  343. done_copy_out = 1;
  344. /*
  345. * This isn't strictly necessary, as we're using frozen
  346. * data for the escaping, but it keeps consistency with
  347. * b_frozen_data usage.
  348. */
  349. jh_in->b_frozen_triggers = jh_in->b_triggers;
  350. }
  351. /*
  352. * Did we need to do an escaping? Now we've done all the
  353. * copying, we can finally do so.
  354. */
  355. if (do_escape) {
  356. mapped_data = kmap_atomic(new_page, KM_USER0);
  357. *((unsigned int *)(mapped_data + new_offset)) = 0;
  358. kunmap_atomic(mapped_data, KM_USER0);
  359. }
  360. set_bh_page(new_bh, new_page, new_offset);
  361. new_jh->b_transaction = NULL;
  362. new_bh->b_size = jh2bh(jh_in)->b_size;
  363. new_bh->b_bdev = transaction->t_journal->j_dev;
  364. new_bh->b_blocknr = blocknr;
  365. set_buffer_mapped(new_bh);
  366. set_buffer_dirty(new_bh);
  367. *jh_out = new_jh;
  368. /*
  369. * The to-be-written buffer needs to get moved to the io queue,
  370. * and the original buffer whose contents we are shadowing or
  371. * copying is moved to the transaction's shadow queue.
  372. */
  373. JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
  374. spin_lock(&journal->j_list_lock);
  375. __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
  376. spin_unlock(&journal->j_list_lock);
  377. jbd_unlock_bh_state(bh_in);
  378. JBUFFER_TRACE(new_jh, "file as BJ_IO");
  379. jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
  380. return do_escape | (done_copy_out << 1);
  381. }
  382. /*
  383. * Allocation code for the journal file. Manage the space left in the
  384. * journal, so that we can begin checkpointing when appropriate.
  385. */
  386. /*
  387. * __jbd2_log_space_left: Return the number of free blocks left in the journal.
  388. *
  389. * Called with the journal already locked.
  390. *
  391. * Called under j_state_lock
  392. */
  393. int __jbd2_log_space_left(journal_t *journal)
  394. {
  395. int left = journal->j_free;
  396. assert_spin_locked(&journal->j_state_lock);
  397. /*
  398. * Be pessimistic here about the number of those free blocks which
  399. * might be required for log descriptor control blocks.
  400. */
  401. #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
  402. left -= MIN_LOG_RESERVED_BLOCKS;
  403. if (left <= 0)
  404. return 0;
  405. left -= (left >> 3);
  406. return left;
  407. }
  408. /*
  409. * Called under j_state_lock. Returns true if a transaction commit was started.
  410. */
  411. int __jbd2_log_start_commit(journal_t *journal, tid_t target)
  412. {
  413. /*
  414. * Are we already doing a recent enough commit?
  415. */
  416. if (!tid_geq(journal->j_commit_request, target)) {
  417. /*
  418. * We want a new commit: OK, mark the request and wakup the
  419. * commit thread. We do _not_ do the commit ourselves.
  420. */
  421. journal->j_commit_request = target;
  422. jbd_debug(1, "JBD: requesting commit %d/%d\n",
  423. journal->j_commit_request,
  424. journal->j_commit_sequence);
  425. wake_up(&journal->j_wait_commit);
  426. return 1;
  427. }
  428. return 0;
  429. }
  430. int jbd2_log_start_commit(journal_t *journal, tid_t tid)
  431. {
  432. int ret;
  433. spin_lock(&journal->j_state_lock);
  434. ret = __jbd2_log_start_commit(journal, tid);
  435. spin_unlock(&journal->j_state_lock);
  436. return ret;
  437. }
  438. /*
  439. * Force and wait upon a commit if the calling process is not within
  440. * transaction. This is used for forcing out undo-protected data which contains
  441. * bitmaps, when the fs is running out of space.
  442. *
  443. * We can only force the running transaction if we don't have an active handle;
  444. * otherwise, we will deadlock.
  445. *
  446. * Returns true if a transaction was started.
  447. */
  448. int jbd2_journal_force_commit_nested(journal_t *journal)
  449. {
  450. transaction_t *transaction = NULL;
  451. tid_t tid;
  452. spin_lock(&journal->j_state_lock);
  453. if (journal->j_running_transaction && !current->journal_info) {
  454. transaction = journal->j_running_transaction;
  455. __jbd2_log_start_commit(journal, transaction->t_tid);
  456. } else if (journal->j_committing_transaction)
  457. transaction = journal->j_committing_transaction;
  458. if (!transaction) {
  459. spin_unlock(&journal->j_state_lock);
  460. return 0; /* Nothing to retry */
  461. }
  462. tid = transaction->t_tid;
  463. spin_unlock(&journal->j_state_lock);
  464. jbd2_log_wait_commit(journal, tid);
  465. return 1;
  466. }
  467. /*
  468. * Start a commit of the current running transaction (if any). Returns true
  469. * if a transaction is going to be committed (or is currently already
  470. * committing), and fills its tid in at *ptid
  471. */
  472. int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
  473. {
  474. int ret = 0;
  475. spin_lock(&journal->j_state_lock);
  476. if (journal->j_running_transaction) {
  477. tid_t tid = journal->j_running_transaction->t_tid;
  478. __jbd2_log_start_commit(journal, tid);
  479. /* There's a running transaction and we've just made sure
  480. * it's commit has been scheduled. */
  481. if (ptid)
  482. *ptid = tid;
  483. ret = 1;
  484. } else if (journal->j_committing_transaction) {
  485. /*
  486. * If ext3_write_super() recently started a commit, then we
  487. * have to wait for completion of that transaction
  488. */
  489. if (ptid)
  490. *ptid = journal->j_committing_transaction->t_tid;
  491. ret = 1;
  492. }
  493. spin_unlock(&journal->j_state_lock);
  494. return ret;
  495. }
  496. /*
  497. * Wait for a specified commit to complete.
  498. * The caller may not hold the journal lock.
  499. */
  500. int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
  501. {
  502. int err = 0;
  503. #ifdef CONFIG_JBD2_DEBUG
  504. spin_lock(&journal->j_state_lock);
  505. if (!tid_geq(journal->j_commit_request, tid)) {
  506. printk(KERN_EMERG
  507. "%s: error: j_commit_request=%d, tid=%d\n",
  508. __func__, journal->j_commit_request, tid);
  509. }
  510. spin_unlock(&journal->j_state_lock);
  511. #endif
  512. spin_lock(&journal->j_state_lock);
  513. while (tid_gt(tid, journal->j_commit_sequence)) {
  514. jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
  515. tid, journal->j_commit_sequence);
  516. wake_up(&journal->j_wait_commit);
  517. spin_unlock(&journal->j_state_lock);
  518. wait_event(journal->j_wait_done_commit,
  519. !tid_gt(tid, journal->j_commit_sequence));
  520. spin_lock(&journal->j_state_lock);
  521. }
  522. spin_unlock(&journal->j_state_lock);
  523. if (unlikely(is_journal_aborted(journal))) {
  524. printk(KERN_EMERG "journal commit I/O error\n");
  525. err = -EIO;
  526. }
  527. return err;
  528. }
  529. /*
  530. * Log buffer allocation routines:
  531. */
  532. int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
  533. {
  534. unsigned long blocknr;
  535. spin_lock(&journal->j_state_lock);
  536. J_ASSERT(journal->j_free > 1);
  537. blocknr = journal->j_head;
  538. journal->j_head++;
  539. journal->j_free--;
  540. if (journal->j_head == journal->j_last)
  541. journal->j_head = journal->j_first;
  542. spin_unlock(&journal->j_state_lock);
  543. return jbd2_journal_bmap(journal, blocknr, retp);
  544. }
  545. /*
  546. * Conversion of logical to physical block numbers for the journal
  547. *
  548. * On external journals the journal blocks are identity-mapped, so
  549. * this is a no-op. If needed, we can use j_blk_offset - everything is
  550. * ready.
  551. */
  552. int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
  553. unsigned long long *retp)
  554. {
  555. int err = 0;
  556. unsigned long long ret;
  557. if (journal->j_inode) {
  558. ret = bmap(journal->j_inode, blocknr);
  559. if (ret)
  560. *retp = ret;
  561. else {
  562. printk(KERN_ALERT "%s: journal block not found "
  563. "at offset %lu on %s\n",
  564. __func__, blocknr, journal->j_devname);
  565. err = -EIO;
  566. __journal_abort_soft(journal, err);
  567. }
  568. } else {
  569. *retp = blocknr; /* +journal->j_blk_offset */
  570. }
  571. return err;
  572. }
  573. /*
  574. * We play buffer_head aliasing tricks to write data/metadata blocks to
  575. * the journal without copying their contents, but for journal
  576. * descriptor blocks we do need to generate bona fide buffers.
  577. *
  578. * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
  579. * the buffer's contents they really should run flush_dcache_page(bh->b_page).
  580. * But we don't bother doing that, so there will be coherency problems with
  581. * mmaps of blockdevs which hold live JBD-controlled filesystems.
  582. */
  583. struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
  584. {
  585. struct buffer_head *bh;
  586. unsigned long long blocknr;
  587. int err;
  588. err = jbd2_journal_next_log_block(journal, &blocknr);
  589. if (err)
  590. return NULL;
  591. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  592. if (!bh)
  593. return NULL;
  594. lock_buffer(bh);
  595. memset(bh->b_data, 0, journal->j_blocksize);
  596. set_buffer_uptodate(bh);
  597. unlock_buffer(bh);
  598. BUFFER_TRACE(bh, "return this buffer");
  599. return jbd2_journal_add_journal_head(bh);
  600. }
  601. struct jbd2_stats_proc_session {
  602. journal_t *journal;
  603. struct transaction_stats_s *stats;
  604. int start;
  605. int max;
  606. };
  607. static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
  608. {
  609. return *pos ? NULL : SEQ_START_TOKEN;
  610. }
  611. static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
  612. {
  613. return NULL;
  614. }
  615. static int jbd2_seq_info_show(struct seq_file *seq, void *v)
  616. {
  617. struct jbd2_stats_proc_session *s = seq->private;
  618. if (v != SEQ_START_TOKEN)
  619. return 0;
  620. seq_printf(seq, "%lu transaction, each up to %u blocks\n",
  621. s->stats->ts_tid,
  622. s->journal->j_max_transaction_buffers);
  623. if (s->stats->ts_tid == 0)
  624. return 0;
  625. seq_printf(seq, "average: \n %ums waiting for transaction\n",
  626. jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
  627. seq_printf(seq, " %ums running transaction\n",
  628. jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
  629. seq_printf(seq, " %ums transaction was being locked\n",
  630. jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
  631. seq_printf(seq, " %ums flushing data (in ordered mode)\n",
  632. jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
  633. seq_printf(seq, " %ums logging transaction\n",
  634. jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
  635. seq_printf(seq, " %lluus average transaction commit time\n",
  636. div_u64(s->journal->j_average_commit_time, 1000));
  637. seq_printf(seq, " %lu handles per transaction\n",
  638. s->stats->run.rs_handle_count / s->stats->ts_tid);
  639. seq_printf(seq, " %lu blocks per transaction\n",
  640. s->stats->run.rs_blocks / s->stats->ts_tid);
  641. seq_printf(seq, " %lu logged blocks per transaction\n",
  642. s->stats->run.rs_blocks_logged / s->stats->ts_tid);
  643. return 0;
  644. }
  645. static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
  646. {
  647. }
  648. static const struct seq_operations jbd2_seq_info_ops = {
  649. .start = jbd2_seq_info_start,
  650. .next = jbd2_seq_info_next,
  651. .stop = jbd2_seq_info_stop,
  652. .show = jbd2_seq_info_show,
  653. };
  654. static int jbd2_seq_info_open(struct inode *inode, struct file *file)
  655. {
  656. journal_t *journal = PDE(inode)->data;
  657. struct jbd2_stats_proc_session *s;
  658. int rc, size;
  659. s = kmalloc(sizeof(*s), GFP_KERNEL);
  660. if (s == NULL)
  661. return -ENOMEM;
  662. size = sizeof(struct transaction_stats_s);
  663. s->stats = kmalloc(size, GFP_KERNEL);
  664. if (s->stats == NULL) {
  665. kfree(s);
  666. return -ENOMEM;
  667. }
  668. spin_lock(&journal->j_history_lock);
  669. memcpy(s->stats, &journal->j_stats, size);
  670. s->journal = journal;
  671. spin_unlock(&journal->j_history_lock);
  672. rc = seq_open(file, &jbd2_seq_info_ops);
  673. if (rc == 0) {
  674. struct seq_file *m = file->private_data;
  675. m->private = s;
  676. } else {
  677. kfree(s->stats);
  678. kfree(s);
  679. }
  680. return rc;
  681. }
  682. static int jbd2_seq_info_release(struct inode *inode, struct file *file)
  683. {
  684. struct seq_file *seq = file->private_data;
  685. struct jbd2_stats_proc_session *s = seq->private;
  686. kfree(s->stats);
  687. kfree(s);
  688. return seq_release(inode, file);
  689. }
  690. static const struct file_operations jbd2_seq_info_fops = {
  691. .owner = THIS_MODULE,
  692. .open = jbd2_seq_info_open,
  693. .read = seq_read,
  694. .llseek = seq_lseek,
  695. .release = jbd2_seq_info_release,
  696. };
  697. static struct proc_dir_entry *proc_jbd2_stats;
  698. static void jbd2_stats_proc_init(journal_t *journal)
  699. {
  700. journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
  701. if (journal->j_proc_entry) {
  702. proc_create_data("info", S_IRUGO, journal->j_proc_entry,
  703. &jbd2_seq_info_fops, journal);
  704. }
  705. }
  706. static void jbd2_stats_proc_exit(journal_t *journal)
  707. {
  708. remove_proc_entry("info", journal->j_proc_entry);
  709. remove_proc_entry(journal->j_devname, proc_jbd2_stats);
  710. }
  711. /*
  712. * Management for journal control blocks: functions to create and
  713. * destroy journal_t structures, and to initialise and read existing
  714. * journal blocks from disk. */
  715. /* First: create and setup a journal_t object in memory. We initialise
  716. * very few fields yet: that has to wait until we have created the
  717. * journal structures from from scratch, or loaded them from disk. */
  718. static journal_t * journal_init_common (void)
  719. {
  720. journal_t *journal;
  721. int err;
  722. journal = kzalloc(sizeof(*journal), GFP_KERNEL);
  723. if (!journal)
  724. goto fail;
  725. init_waitqueue_head(&journal->j_wait_transaction_locked);
  726. init_waitqueue_head(&journal->j_wait_logspace);
  727. init_waitqueue_head(&journal->j_wait_done_commit);
  728. init_waitqueue_head(&journal->j_wait_checkpoint);
  729. init_waitqueue_head(&journal->j_wait_commit);
  730. init_waitqueue_head(&journal->j_wait_updates);
  731. mutex_init(&journal->j_barrier);
  732. mutex_init(&journal->j_checkpoint_mutex);
  733. spin_lock_init(&journal->j_revoke_lock);
  734. spin_lock_init(&journal->j_list_lock);
  735. spin_lock_init(&journal->j_state_lock);
  736. journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
  737. journal->j_min_batch_time = 0;
  738. journal->j_max_batch_time = 15000; /* 15ms */
  739. /* The journal is marked for error until we succeed with recovery! */
  740. journal->j_flags = JBD2_ABORT;
  741. /* Set up a default-sized revoke table for the new mount. */
  742. err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
  743. if (err) {
  744. kfree(journal);
  745. goto fail;
  746. }
  747. spin_lock_init(&journal->j_history_lock);
  748. return journal;
  749. fail:
  750. return NULL;
  751. }
  752. /* jbd2_journal_init_dev and jbd2_journal_init_inode:
  753. *
  754. * Create a journal structure assigned some fixed set of disk blocks to
  755. * the journal. We don't actually touch those disk blocks yet, but we
  756. * need to set up all of the mapping information to tell the journaling
  757. * system where the journal blocks are.
  758. *
  759. */
  760. /**
  761. * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
  762. * @bdev: Block device on which to create the journal
  763. * @fs_dev: Device which hold journalled filesystem for this journal.
  764. * @start: Block nr Start of journal.
  765. * @len: Length of the journal in blocks.
  766. * @blocksize: blocksize of journalling device
  767. *
  768. * Returns: a newly created journal_t *
  769. *
  770. * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
  771. * range of blocks on an arbitrary block device.
  772. *
  773. */
  774. journal_t * jbd2_journal_init_dev(struct block_device *bdev,
  775. struct block_device *fs_dev,
  776. unsigned long long start, int len, int blocksize)
  777. {
  778. journal_t *journal = journal_init_common();
  779. struct buffer_head *bh;
  780. char *p;
  781. int n;
  782. if (!journal)
  783. return NULL;
  784. /* journal descriptor can store up to n blocks -bzzz */
  785. journal->j_blocksize = blocksize;
  786. jbd2_stats_proc_init(journal);
  787. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  788. journal->j_wbufsize = n;
  789. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  790. if (!journal->j_wbuf) {
  791. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  792. __func__);
  793. goto out_err;
  794. }
  795. journal->j_dev = bdev;
  796. journal->j_fs_dev = fs_dev;
  797. journal->j_blk_offset = start;
  798. journal->j_maxlen = len;
  799. bdevname(journal->j_dev, journal->j_devname);
  800. p = journal->j_devname;
  801. while ((p = strchr(p, '/')))
  802. *p = '!';
  803. bh = __getblk(journal->j_dev, start, journal->j_blocksize);
  804. if (!bh) {
  805. printk(KERN_ERR
  806. "%s: Cannot get buffer for journal superblock\n",
  807. __func__);
  808. goto out_err;
  809. }
  810. journal->j_sb_buffer = bh;
  811. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  812. return journal;
  813. out_err:
  814. kfree(journal->j_wbuf);
  815. jbd2_stats_proc_exit(journal);
  816. kfree(journal);
  817. return NULL;
  818. }
  819. /**
  820. * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
  821. * @inode: An inode to create the journal in
  822. *
  823. * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
  824. * the journal. The inode must exist already, must support bmap() and
  825. * must have all data blocks preallocated.
  826. */
  827. journal_t * jbd2_journal_init_inode (struct inode *inode)
  828. {
  829. struct buffer_head *bh;
  830. journal_t *journal = journal_init_common();
  831. char *p;
  832. int err;
  833. int n;
  834. unsigned long long blocknr;
  835. if (!journal)
  836. return NULL;
  837. journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
  838. journal->j_inode = inode;
  839. bdevname(journal->j_dev, journal->j_devname);
  840. p = journal->j_devname;
  841. while ((p = strchr(p, '/')))
  842. *p = '!';
  843. p = journal->j_devname + strlen(journal->j_devname);
  844. sprintf(p, "-%lu", journal->j_inode->i_ino);
  845. jbd_debug(1,
  846. "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
  847. journal, inode->i_sb->s_id, inode->i_ino,
  848. (long long) inode->i_size,
  849. inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
  850. journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
  851. journal->j_blocksize = inode->i_sb->s_blocksize;
  852. jbd2_stats_proc_init(journal);
  853. /* journal descriptor can store up to n blocks -bzzz */
  854. n = journal->j_blocksize / sizeof(journal_block_tag_t);
  855. journal->j_wbufsize = n;
  856. journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
  857. if (!journal->j_wbuf) {
  858. printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
  859. __func__);
  860. goto out_err;
  861. }
  862. err = jbd2_journal_bmap(journal, 0, &blocknr);
  863. /* If that failed, give up */
  864. if (err) {
  865. printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
  866. __func__);
  867. goto out_err;
  868. }
  869. bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
  870. if (!bh) {
  871. printk(KERN_ERR
  872. "%s: Cannot get buffer for journal superblock\n",
  873. __func__);
  874. goto out_err;
  875. }
  876. journal->j_sb_buffer = bh;
  877. journal->j_superblock = (journal_superblock_t *)bh->b_data;
  878. return journal;
  879. out_err:
  880. kfree(journal->j_wbuf);
  881. jbd2_stats_proc_exit(journal);
  882. kfree(journal);
  883. return NULL;
  884. }
  885. /*
  886. * If the journal init or create aborts, we need to mark the journal
  887. * superblock as being NULL to prevent the journal destroy from writing
  888. * back a bogus superblock.
  889. */
  890. static void journal_fail_superblock (journal_t *journal)
  891. {
  892. struct buffer_head *bh = journal->j_sb_buffer;
  893. brelse(bh);
  894. journal->j_sb_buffer = NULL;
  895. }
  896. /*
  897. * Given a journal_t structure, initialise the various fields for
  898. * startup of a new journaling session. We use this both when creating
  899. * a journal, and after recovering an old journal to reset it for
  900. * subsequent use.
  901. */
  902. static int journal_reset(journal_t *journal)
  903. {
  904. journal_superblock_t *sb = journal->j_superblock;
  905. unsigned long long first, last;
  906. first = be32_to_cpu(sb->s_first);
  907. last = be32_to_cpu(sb->s_maxlen);
  908. if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
  909. printk(KERN_ERR "JBD: Journal too short (blocks %llu-%llu).\n",
  910. first, last);
  911. journal_fail_superblock(journal);
  912. return -EINVAL;
  913. }
  914. journal->j_first = first;
  915. journal->j_last = last;
  916. journal->j_head = first;
  917. journal->j_tail = first;
  918. journal->j_free = last - first;
  919. journal->j_tail_sequence = journal->j_transaction_sequence;
  920. journal->j_commit_sequence = journal->j_transaction_sequence - 1;
  921. journal->j_commit_request = journal->j_commit_sequence;
  922. journal->j_max_transaction_buffers = journal->j_maxlen / 4;
  923. /* Add the dynamic fields and write it to disk. */
  924. jbd2_journal_update_superblock(journal, 1);
  925. return jbd2_journal_start_thread(journal);
  926. }
  927. /**
  928. * void jbd2_journal_update_superblock() - Update journal sb on disk.
  929. * @journal: The journal to update.
  930. * @wait: Set to '0' if you don't want to wait for IO completion.
  931. *
  932. * Update a journal's dynamic superblock fields and write it to disk,
  933. * optionally waiting for the IO to complete.
  934. */
  935. void jbd2_journal_update_superblock(journal_t *journal, int wait)
  936. {
  937. journal_superblock_t *sb = journal->j_superblock;
  938. struct buffer_head *bh = journal->j_sb_buffer;
  939. /*
  940. * As a special case, if the on-disk copy is already marked as needing
  941. * no recovery (s_start == 0) and there are no outstanding transactions
  942. * in the filesystem, then we can safely defer the superblock update
  943. * until the next commit by setting JBD2_FLUSHED. This avoids
  944. * attempting a write to a potential-readonly device.
  945. */
  946. if (sb->s_start == 0 && journal->j_tail_sequence ==
  947. journal->j_transaction_sequence) {
  948. jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
  949. "(start %ld, seq %d, errno %d)\n",
  950. journal->j_tail, journal->j_tail_sequence,
  951. journal->j_errno);
  952. goto out;
  953. }
  954. if (buffer_write_io_error(bh)) {
  955. /*
  956. * Oh, dear. A previous attempt to write the journal
  957. * superblock failed. This could happen because the
  958. * USB device was yanked out. Or it could happen to
  959. * be a transient write error and maybe the block will
  960. * be remapped. Nothing we can do but to retry the
  961. * write and hope for the best.
  962. */
  963. printk(KERN_ERR "JBD2: previous I/O error detected "
  964. "for journal superblock update for %s.\n",
  965. journal->j_devname);
  966. clear_buffer_write_io_error(bh);
  967. set_buffer_uptodate(bh);
  968. }
  969. spin_lock(&journal->j_state_lock);
  970. jbd_debug(1,"JBD: updating superblock (start %ld, seq %d, errno %d)\n",
  971. journal->j_tail, journal->j_tail_sequence, journal->j_errno);
  972. sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
  973. sb->s_start = cpu_to_be32(journal->j_tail);
  974. sb->s_errno = cpu_to_be32(journal->j_errno);
  975. spin_unlock(&journal->j_state_lock);
  976. BUFFER_TRACE(bh, "marking dirty");
  977. mark_buffer_dirty(bh);
  978. if (wait) {
  979. sync_dirty_buffer(bh);
  980. if (buffer_write_io_error(bh)) {
  981. printk(KERN_ERR "JBD2: I/O error detected "
  982. "when updating journal superblock for %s.\n",
  983. journal->j_devname);
  984. clear_buffer_write_io_error(bh);
  985. set_buffer_uptodate(bh);
  986. }
  987. } else
  988. ll_rw_block(SWRITE, 1, &bh);
  989. out:
  990. /* If we have just flushed the log (by marking s_start==0), then
  991. * any future commit will have to be careful to update the
  992. * superblock again to re-record the true start of the log. */
  993. spin_lock(&journal->j_state_lock);
  994. if (sb->s_start)
  995. journal->j_flags &= ~JBD2_FLUSHED;
  996. else
  997. journal->j_flags |= JBD2_FLUSHED;
  998. spin_unlock(&journal->j_state_lock);
  999. }
  1000. /*
  1001. * Read the superblock for a given journal, performing initial
  1002. * validation of the format.
  1003. */
  1004. static int journal_get_superblock(journal_t *journal)
  1005. {
  1006. struct buffer_head *bh;
  1007. journal_superblock_t *sb;
  1008. int err = -EIO;
  1009. bh = journal->j_sb_buffer;
  1010. J_ASSERT(bh != NULL);
  1011. if (!buffer_uptodate(bh)) {
  1012. ll_rw_block(READ, 1, &bh);
  1013. wait_on_buffer(bh);
  1014. if (!buffer_uptodate(bh)) {
  1015. printk (KERN_ERR
  1016. "JBD: IO error reading journal superblock\n");
  1017. goto out;
  1018. }
  1019. }
  1020. sb = journal->j_superblock;
  1021. err = -EINVAL;
  1022. if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
  1023. sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
  1024. printk(KERN_WARNING "JBD: no valid journal superblock found\n");
  1025. goto out;
  1026. }
  1027. switch(be32_to_cpu(sb->s_header.h_blocktype)) {
  1028. case JBD2_SUPERBLOCK_V1:
  1029. journal->j_format_version = 1;
  1030. break;
  1031. case JBD2_SUPERBLOCK_V2:
  1032. journal->j_format_version = 2;
  1033. break;
  1034. default:
  1035. printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
  1036. goto out;
  1037. }
  1038. if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
  1039. journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
  1040. else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
  1041. printk (KERN_WARNING "JBD: journal file too short\n");
  1042. goto out;
  1043. }
  1044. return 0;
  1045. out:
  1046. journal_fail_superblock(journal);
  1047. return err;
  1048. }
  1049. /*
  1050. * Load the on-disk journal superblock and read the key fields into the
  1051. * journal_t.
  1052. */
  1053. static int load_superblock(journal_t *journal)
  1054. {
  1055. int err;
  1056. journal_superblock_t *sb;
  1057. err = journal_get_superblock(journal);
  1058. if (err)
  1059. return err;
  1060. sb = journal->j_superblock;
  1061. journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
  1062. journal->j_tail = be32_to_cpu(sb->s_start);
  1063. journal->j_first = be32_to_cpu(sb->s_first);
  1064. journal->j_last = be32_to_cpu(sb->s_maxlen);
  1065. journal->j_errno = be32_to_cpu(sb->s_errno);
  1066. return 0;
  1067. }
  1068. /**
  1069. * int jbd2_journal_load() - Read journal from disk.
  1070. * @journal: Journal to act on.
  1071. *
  1072. * Given a journal_t structure which tells us which disk blocks contain
  1073. * a journal, read the journal from disk to initialise the in-memory
  1074. * structures.
  1075. */
  1076. int jbd2_journal_load(journal_t *journal)
  1077. {
  1078. int err;
  1079. journal_superblock_t *sb;
  1080. err = load_superblock(journal);
  1081. if (err)
  1082. return err;
  1083. sb = journal->j_superblock;
  1084. /* If this is a V2 superblock, then we have to check the
  1085. * features flags on it. */
  1086. if (journal->j_format_version >= 2) {
  1087. if ((sb->s_feature_ro_compat &
  1088. ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
  1089. (sb->s_feature_incompat &
  1090. ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
  1091. printk (KERN_WARNING
  1092. "JBD: Unrecognised features on journal\n");
  1093. return -EINVAL;
  1094. }
  1095. }
  1096. /*
  1097. * Create a slab for this blocksize
  1098. */
  1099. err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
  1100. if (err)
  1101. return err;
  1102. /* Let the recovery code check whether it needs to recover any
  1103. * data from the journal. */
  1104. if (jbd2_journal_recover(journal))
  1105. goto recovery_error;
  1106. if (journal->j_failed_commit) {
  1107. printk(KERN_ERR "JBD2: journal transaction %u on %s "
  1108. "is corrupt.\n", journal->j_failed_commit,
  1109. journal->j_devname);
  1110. return -EIO;
  1111. }
  1112. /* OK, we've finished with the dynamic journal bits:
  1113. * reinitialise the dynamic contents of the superblock in memory
  1114. * and reset them on disk. */
  1115. if (journal_reset(journal))
  1116. goto recovery_error;
  1117. journal->j_flags &= ~JBD2_ABORT;
  1118. journal->j_flags |= JBD2_LOADED;
  1119. return 0;
  1120. recovery_error:
  1121. printk (KERN_WARNING "JBD: recovery failed\n");
  1122. return -EIO;
  1123. }
  1124. /**
  1125. * void jbd2_journal_destroy() - Release a journal_t structure.
  1126. * @journal: Journal to act on.
  1127. *
  1128. * Release a journal_t structure once it is no longer in use by the
  1129. * journaled object.
  1130. * Return <0 if we couldn't clean up the journal.
  1131. */
  1132. int jbd2_journal_destroy(journal_t *journal)
  1133. {
  1134. int err = 0;
  1135. /* Wait for the commit thread to wake up and die. */
  1136. journal_kill_thread(journal);
  1137. /* Force a final log commit */
  1138. if (journal->j_running_transaction)
  1139. jbd2_journal_commit_transaction(journal);
  1140. /* Force any old transactions to disk */
  1141. /* Totally anal locking here... */
  1142. spin_lock(&journal->j_list_lock);
  1143. while (journal->j_checkpoint_transactions != NULL) {
  1144. spin_unlock(&journal->j_list_lock);
  1145. mutex_lock(&journal->j_checkpoint_mutex);
  1146. jbd2_log_do_checkpoint(journal);
  1147. mutex_unlock(&journal->j_checkpoint_mutex);
  1148. spin_lock(&journal->j_list_lock);
  1149. }
  1150. J_ASSERT(journal->j_running_transaction == NULL);
  1151. J_ASSERT(journal->j_committing_transaction == NULL);
  1152. J_ASSERT(journal->j_checkpoint_transactions == NULL);
  1153. spin_unlock(&journal->j_list_lock);
  1154. if (journal->j_sb_buffer) {
  1155. if (!is_journal_aborted(journal)) {
  1156. /* We can now mark the journal as empty. */
  1157. journal->j_tail = 0;
  1158. journal->j_tail_sequence =
  1159. ++journal->j_transaction_sequence;
  1160. jbd2_journal_update_superblock(journal, 1);
  1161. } else {
  1162. err = -EIO;
  1163. }
  1164. brelse(journal->j_sb_buffer);
  1165. }
  1166. if (journal->j_proc_entry)
  1167. jbd2_stats_proc_exit(journal);
  1168. if (journal->j_inode)
  1169. iput(journal->j_inode);
  1170. if (journal->j_revoke)
  1171. jbd2_journal_destroy_revoke(journal);
  1172. kfree(journal->j_wbuf);
  1173. kfree(journal);
  1174. return err;
  1175. }
  1176. /**
  1177. *int jbd2_journal_check_used_features () - Check if features specified are used.
  1178. * @journal: Journal to check.
  1179. * @compat: bitmask of compatible features
  1180. * @ro: bitmask of features that force read-only mount
  1181. * @incompat: bitmask of incompatible features
  1182. *
  1183. * Check whether the journal uses all of a given set of
  1184. * features. Return true (non-zero) if it does.
  1185. **/
  1186. int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
  1187. unsigned long ro, unsigned long incompat)
  1188. {
  1189. journal_superblock_t *sb;
  1190. if (!compat && !ro && !incompat)
  1191. return 1;
  1192. if (journal->j_format_version == 1)
  1193. return 0;
  1194. sb = journal->j_superblock;
  1195. if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
  1196. ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
  1197. ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
  1198. return 1;
  1199. return 0;
  1200. }
  1201. /**
  1202. * int jbd2_journal_check_available_features() - Check feature set in journalling layer
  1203. * @journal: Journal to check.
  1204. * @compat: bitmask of compatible features
  1205. * @ro: bitmask of features that force read-only mount
  1206. * @incompat: bitmask of incompatible features
  1207. *
  1208. * Check whether the journaling code supports the use of
  1209. * all of a given set of features on this journal. Return true
  1210. * (non-zero) if it can. */
  1211. int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
  1212. unsigned long ro, unsigned long incompat)
  1213. {
  1214. journal_superblock_t *sb;
  1215. if (!compat && !ro && !incompat)
  1216. return 1;
  1217. sb = journal->j_superblock;
  1218. /* We can support any known requested features iff the
  1219. * superblock is in version 2. Otherwise we fail to support any
  1220. * extended sb features. */
  1221. if (journal->j_format_version != 2)
  1222. return 0;
  1223. if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
  1224. (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
  1225. (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
  1226. return 1;
  1227. return 0;
  1228. }
  1229. /**
  1230. * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
  1231. * @journal: Journal to act on.
  1232. * @compat: bitmask of compatible features
  1233. * @ro: bitmask of features that force read-only mount
  1234. * @incompat: bitmask of incompatible features
  1235. *
  1236. * Mark a given journal feature as present on the
  1237. * superblock. Returns true if the requested features could be set.
  1238. *
  1239. */
  1240. int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
  1241. unsigned long ro, unsigned long incompat)
  1242. {
  1243. journal_superblock_t *sb;
  1244. if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
  1245. return 1;
  1246. if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
  1247. return 0;
  1248. jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
  1249. compat, ro, incompat);
  1250. sb = journal->j_superblock;
  1251. sb->s_feature_compat |= cpu_to_be32(compat);
  1252. sb->s_feature_ro_compat |= cpu_to_be32(ro);
  1253. sb->s_feature_incompat |= cpu_to_be32(incompat);
  1254. return 1;
  1255. }
  1256. /*
  1257. * jbd2_journal_clear_features () - Clear a given journal feature in the
  1258. * superblock
  1259. * @journal: Journal to act on.
  1260. * @compat: bitmask of compatible features
  1261. * @ro: bitmask of features that force read-only mount
  1262. * @incompat: bitmask of incompatible features
  1263. *
  1264. * Clear a given journal feature as present on the
  1265. * superblock.
  1266. */
  1267. void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
  1268. unsigned long ro, unsigned long incompat)
  1269. {
  1270. journal_superblock_t *sb;
  1271. jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
  1272. compat, ro, incompat);
  1273. sb = journal->j_superblock;
  1274. sb->s_feature_compat &= ~cpu_to_be32(compat);
  1275. sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
  1276. sb->s_feature_incompat &= ~cpu_to_be32(incompat);
  1277. }
  1278. EXPORT_SYMBOL(jbd2_journal_clear_features);
  1279. /**
  1280. * int jbd2_journal_update_format () - Update on-disk journal structure.
  1281. * @journal: Journal to act on.
  1282. *
  1283. * Given an initialised but unloaded journal struct, poke about in the
  1284. * on-disk structure to update it to the most recent supported version.
  1285. */
  1286. int jbd2_journal_update_format (journal_t *journal)
  1287. {
  1288. journal_superblock_t *sb;
  1289. int err;
  1290. err = journal_get_superblock(journal);
  1291. if (err)
  1292. return err;
  1293. sb = journal->j_superblock;
  1294. switch (be32_to_cpu(sb->s_header.h_blocktype)) {
  1295. case JBD2_SUPERBLOCK_V2:
  1296. return 0;
  1297. case JBD2_SUPERBLOCK_V1:
  1298. return journal_convert_superblock_v1(journal, sb);
  1299. default:
  1300. break;
  1301. }
  1302. return -EINVAL;
  1303. }
  1304. static int journal_convert_superblock_v1(journal_t *journal,
  1305. journal_superblock_t *sb)
  1306. {
  1307. int offset, blocksize;
  1308. struct buffer_head *bh;
  1309. printk(KERN_WARNING
  1310. "JBD: Converting superblock from version 1 to 2.\n");
  1311. /* Pre-initialise new fields to zero */
  1312. offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
  1313. blocksize = be32_to_cpu(sb->s_blocksize);
  1314. memset(&sb->s_feature_compat, 0, blocksize-offset);
  1315. sb->s_nr_users = cpu_to_be32(1);
  1316. sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
  1317. journal->j_format_version = 2;
  1318. bh = journal->j_sb_buffer;
  1319. BUFFER_TRACE(bh, "marking dirty");
  1320. mark_buffer_dirty(bh);
  1321. sync_dirty_buffer(bh);
  1322. return 0;
  1323. }
  1324. /**
  1325. * int jbd2_journal_flush () - Flush journal
  1326. * @journal: Journal to act on.
  1327. *
  1328. * Flush all data for a given journal to disk and empty the journal.
  1329. * Filesystems can use this when remounting readonly to ensure that
  1330. * recovery does not need to happen on remount.
  1331. */
  1332. int jbd2_journal_flush(journal_t *journal)
  1333. {
  1334. int err = 0;
  1335. transaction_t *transaction = NULL;
  1336. unsigned long old_tail;
  1337. spin_lock(&journal->j_state_lock);
  1338. /* Force everything buffered to the log... */
  1339. if (journal->j_running_transaction) {
  1340. transaction = journal->j_running_transaction;
  1341. __jbd2_log_start_commit(journal, transaction->t_tid);
  1342. } else if (journal->j_committing_transaction)
  1343. transaction = journal->j_committing_transaction;
  1344. /* Wait for the log commit to complete... */
  1345. if (transaction) {
  1346. tid_t tid = transaction->t_tid;
  1347. spin_unlock(&journal->j_state_lock);
  1348. jbd2_log_wait_commit(journal, tid);
  1349. } else {
  1350. spin_unlock(&journal->j_state_lock);
  1351. }
  1352. /* ...and flush everything in the log out to disk. */
  1353. spin_lock(&journal->j_list_lock);
  1354. while (!err && journal->j_checkpoint_transactions != NULL) {
  1355. spin_unlock(&journal->j_list_lock);
  1356. mutex_lock(&journal->j_checkpoint_mutex);
  1357. err = jbd2_log_do_checkpoint(journal);
  1358. mutex_unlock(&journal->j_checkpoint_mutex);
  1359. spin_lock(&journal->j_list_lock);
  1360. }
  1361. spin_unlock(&journal->j_list_lock);
  1362. if (is_journal_aborted(journal))
  1363. return -EIO;
  1364. jbd2_cleanup_journal_tail(journal);
  1365. /* Finally, mark the journal as really needing no recovery.
  1366. * This sets s_start==0 in the underlying superblock, which is
  1367. * the magic code for a fully-recovered superblock. Any future
  1368. * commits of data to the journal will restore the current
  1369. * s_start value. */
  1370. spin_lock(&journal->j_state_lock);
  1371. old_tail = journal->j_tail;
  1372. journal->j_tail = 0;
  1373. spin_unlock(&journal->j_state_lock);
  1374. jbd2_journal_update_superblock(journal, 1);
  1375. spin_lock(&journal->j_state_lock);
  1376. journal->j_tail = old_tail;
  1377. J_ASSERT(!journal->j_running_transaction);
  1378. J_ASSERT(!journal->j_committing_transaction);
  1379. J_ASSERT(!journal->j_checkpoint_transactions);
  1380. J_ASSERT(journal->j_head == journal->j_tail);
  1381. J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
  1382. spin_unlock(&journal->j_state_lock);
  1383. return 0;
  1384. }
  1385. /**
  1386. * int jbd2_journal_wipe() - Wipe journal contents
  1387. * @journal: Journal to act on.
  1388. * @write: flag (see below)
  1389. *
  1390. * Wipe out all of the contents of a journal, safely. This will produce
  1391. * a warning if the journal contains any valid recovery information.
  1392. * Must be called between journal_init_*() and jbd2_journal_load().
  1393. *
  1394. * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
  1395. * we merely suppress recovery.
  1396. */
  1397. int jbd2_journal_wipe(journal_t *journal, int write)
  1398. {
  1399. journal_superblock_t *sb;
  1400. int err = 0;
  1401. J_ASSERT (!(journal->j_flags & JBD2_LOADED));
  1402. err = load_superblock(journal);
  1403. if (err)
  1404. return err;
  1405. sb = journal->j_superblock;
  1406. if (!journal->j_tail)
  1407. goto no_recovery;
  1408. printk (KERN_WARNING "JBD: %s recovery information on journal\n",
  1409. write ? "Clearing" : "Ignoring");
  1410. err = jbd2_journal_skip_recovery(journal);
  1411. if (write)
  1412. jbd2_journal_update_superblock(journal, 1);
  1413. no_recovery:
  1414. return err;
  1415. }
  1416. /*
  1417. * Journal abort has very specific semantics, which we describe
  1418. * for journal abort.
  1419. *
  1420. * Two internal functions, which provide abort to the jbd layer
  1421. * itself are here.
  1422. */
  1423. /*
  1424. * Quick version for internal journal use (doesn't lock the journal).
  1425. * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
  1426. * and don't attempt to make any other journal updates.
  1427. */
  1428. void __jbd2_journal_abort_hard(journal_t *journal)
  1429. {
  1430. transaction_t *transaction;
  1431. if (journal->j_flags & JBD2_ABORT)
  1432. return;
  1433. printk(KERN_ERR "Aborting journal on device %s.\n",
  1434. journal->j_devname);
  1435. spin_lock(&journal->j_state_lock);
  1436. journal->j_flags |= JBD2_ABORT;
  1437. transaction = journal->j_running_transaction;
  1438. if (transaction)
  1439. __jbd2_log_start_commit(journal, transaction->t_tid);
  1440. spin_unlock(&journal->j_state_lock);
  1441. }
  1442. /* Soft abort: record the abort error status in the journal superblock,
  1443. * but don't do any other IO. */
  1444. static void __journal_abort_soft (journal_t *journal, int errno)
  1445. {
  1446. if (journal->j_flags & JBD2_ABORT)
  1447. return;
  1448. if (!journal->j_errno)
  1449. journal->j_errno = errno;
  1450. __jbd2_journal_abort_hard(journal);
  1451. if (errno)
  1452. jbd2_journal_update_superblock(journal, 1);
  1453. }
  1454. /**
  1455. * void jbd2_journal_abort () - Shutdown the journal immediately.
  1456. * @journal: the journal to shutdown.
  1457. * @errno: an error number to record in the journal indicating
  1458. * the reason for the shutdown.
  1459. *
  1460. * Perform a complete, immediate shutdown of the ENTIRE
  1461. * journal (not of a single transaction). This operation cannot be
  1462. * undone without closing and reopening the journal.
  1463. *
  1464. * The jbd2_journal_abort function is intended to support higher level error
  1465. * recovery mechanisms such as the ext2/ext3 remount-readonly error
  1466. * mode.
  1467. *
  1468. * Journal abort has very specific semantics. Any existing dirty,
  1469. * unjournaled buffers in the main filesystem will still be written to
  1470. * disk by bdflush, but the journaling mechanism will be suspended
  1471. * immediately and no further transaction commits will be honoured.
  1472. *
  1473. * Any dirty, journaled buffers will be written back to disk without
  1474. * hitting the journal. Atomicity cannot be guaranteed on an aborted
  1475. * filesystem, but we _do_ attempt to leave as much data as possible
  1476. * behind for fsck to use for cleanup.
  1477. *
  1478. * Any attempt to get a new transaction handle on a journal which is in
  1479. * ABORT state will just result in an -EROFS error return. A
  1480. * jbd2_journal_stop on an existing handle will return -EIO if we have
  1481. * entered abort state during the update.
  1482. *
  1483. * Recursive transactions are not disturbed by journal abort until the
  1484. * final jbd2_journal_stop, which will receive the -EIO error.
  1485. *
  1486. * Finally, the jbd2_journal_abort call allows the caller to supply an errno
  1487. * which will be recorded (if possible) in the journal superblock. This
  1488. * allows a client to record failure conditions in the middle of a
  1489. * transaction without having to complete the transaction to record the
  1490. * failure to disk. ext3_error, for example, now uses this
  1491. * functionality.
  1492. *
  1493. * Errors which originate from within the journaling layer will NOT
  1494. * supply an errno; a null errno implies that absolutely no further
  1495. * writes are done to the journal (unless there are any already in
  1496. * progress).
  1497. *
  1498. */
  1499. void jbd2_journal_abort(journal_t *journal, int errno)
  1500. {
  1501. __journal_abort_soft(journal, errno);
  1502. }
  1503. /**
  1504. * int jbd2_journal_errno () - returns the journal's error state.
  1505. * @journal: journal to examine.
  1506. *
  1507. * This is the errno number set with jbd2_journal_abort(), the last
  1508. * time the journal was mounted - if the journal was stopped
  1509. * without calling abort this will be 0.
  1510. *
  1511. * If the journal has been aborted on this mount time -EROFS will
  1512. * be returned.
  1513. */
  1514. int jbd2_journal_errno(journal_t *journal)
  1515. {
  1516. int err;
  1517. spin_lock(&journal->j_state_lock);
  1518. if (journal->j_flags & JBD2_ABORT)
  1519. err = -EROFS;
  1520. else
  1521. err = journal->j_errno;
  1522. spin_unlock(&journal->j_state_lock);
  1523. return err;
  1524. }
  1525. /**
  1526. * int jbd2_journal_clear_err () - clears the journal's error state
  1527. * @journal: journal to act on.
  1528. *
  1529. * An error must be cleared or acked to take a FS out of readonly
  1530. * mode.
  1531. */
  1532. int jbd2_journal_clear_err(journal_t *journal)
  1533. {
  1534. int err = 0;
  1535. spin_lock(&journal->j_state_lock);
  1536. if (journal->j_flags & JBD2_ABORT)
  1537. err = -EROFS;
  1538. else
  1539. journal->j_errno = 0;
  1540. spin_unlock(&journal->j_state_lock);
  1541. return err;
  1542. }
  1543. /**
  1544. * void jbd2_journal_ack_err() - Ack journal err.
  1545. * @journal: journal to act on.
  1546. *
  1547. * An error must be cleared or acked to take a FS out of readonly
  1548. * mode.
  1549. */
  1550. void jbd2_journal_ack_err(journal_t *journal)
  1551. {
  1552. spin_lock(&journal->j_state_lock);
  1553. if (journal->j_errno)
  1554. journal->j_flags |= JBD2_ACK_ERR;
  1555. spin_unlock(&journal->j_state_lock);
  1556. }
  1557. int jbd2_journal_blocks_per_page(struct inode *inode)
  1558. {
  1559. return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  1560. }
  1561. /*
  1562. * helper functions to deal with 32 or 64bit block numbers.
  1563. */
  1564. size_t journal_tag_bytes(journal_t *journal)
  1565. {
  1566. if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
  1567. return JBD2_TAG_SIZE64;
  1568. else
  1569. return JBD2_TAG_SIZE32;
  1570. }
  1571. /*
  1572. * JBD memory management
  1573. *
  1574. * These functions are used to allocate block-sized chunks of memory
  1575. * used for making copies of buffer_head data. Very often it will be
  1576. * page-sized chunks of data, but sometimes it will be in
  1577. * sub-page-size chunks. (For example, 16k pages on Power systems
  1578. * with a 4k block file system.) For blocks smaller than a page, we
  1579. * use a SLAB allocator. There are slab caches for each block size,
  1580. * which are allocated at mount time, if necessary, and we only free
  1581. * (all of) the slab caches when/if the jbd2 module is unloaded. For
  1582. * this reason we don't need to a mutex to protect access to
  1583. * jbd2_slab[] allocating or releasing memory; only in
  1584. * jbd2_journal_create_slab().
  1585. */
  1586. #define JBD2_MAX_SLABS 8
  1587. static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
  1588. static DECLARE_MUTEX(jbd2_slab_create_sem);
  1589. static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
  1590. "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
  1591. "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
  1592. };
  1593. static void jbd2_journal_destroy_slabs(void)
  1594. {
  1595. int i;
  1596. for (i = 0; i < JBD2_MAX_SLABS; i++) {
  1597. if (jbd2_slab[i])
  1598. kmem_cache_destroy(jbd2_slab[i]);
  1599. jbd2_slab[i] = NULL;
  1600. }
  1601. }
  1602. static int jbd2_journal_create_slab(size_t size)
  1603. {
  1604. int i = order_base_2(size) - 10;
  1605. size_t slab_size;
  1606. if (size == PAGE_SIZE)
  1607. return 0;
  1608. if (i >= JBD2_MAX_SLABS)
  1609. return -EINVAL;
  1610. if (unlikely(i < 0))
  1611. i = 0;
  1612. down(&jbd2_slab_create_sem);
  1613. if (jbd2_slab[i]) {
  1614. up(&jbd2_slab_create_sem);
  1615. return 0; /* Already created */
  1616. }
  1617. slab_size = 1 << (i+10);
  1618. jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
  1619. slab_size, 0, NULL);
  1620. up(&jbd2_slab_create_sem);
  1621. if (!jbd2_slab[i]) {
  1622. printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
  1623. return -ENOMEM;
  1624. }
  1625. return 0;
  1626. }
  1627. static struct kmem_cache *get_slab(size_t size)
  1628. {
  1629. int i = order_base_2(size) - 10;
  1630. BUG_ON(i >= JBD2_MAX_SLABS);
  1631. if (unlikely(i < 0))
  1632. i = 0;
  1633. BUG_ON(jbd2_slab[i] == NULL);
  1634. return jbd2_slab[i];
  1635. }
  1636. void *jbd2_alloc(size_t size, gfp_t flags)
  1637. {
  1638. void *ptr;
  1639. BUG_ON(size & (size-1)); /* Must be a power of 2 */
  1640. flags |= __GFP_REPEAT;
  1641. if (size == PAGE_SIZE)
  1642. ptr = (void *)__get_free_pages(flags, 0);
  1643. else if (size > PAGE_SIZE) {
  1644. int order = get_order(size);
  1645. if (order < 3)
  1646. ptr = (void *)__get_free_pages(flags, order);
  1647. else
  1648. ptr = vmalloc(size);
  1649. } else
  1650. ptr = kmem_cache_alloc(get_slab(size), flags);
  1651. /* Check alignment; SLUB has gotten this wrong in the past,
  1652. * and this can lead to user data corruption! */
  1653. BUG_ON(((unsigned long) ptr) & (size-1));
  1654. return ptr;
  1655. }
  1656. void jbd2_free(void *ptr, size_t size)
  1657. {
  1658. if (size == PAGE_SIZE) {
  1659. free_pages((unsigned long)ptr, 0);
  1660. return;
  1661. }
  1662. if (size > PAGE_SIZE) {
  1663. int order = get_order(size);
  1664. if (order < 3)
  1665. free_pages((unsigned long)ptr, order);
  1666. else
  1667. vfree(ptr);
  1668. return;
  1669. }
  1670. kmem_cache_free(get_slab(size), ptr);
  1671. };
  1672. /*
  1673. * Journal_head storage management
  1674. */
  1675. static struct kmem_cache *jbd2_journal_head_cache;
  1676. #ifdef CONFIG_JBD2_DEBUG
  1677. static atomic_t nr_journal_heads = ATOMIC_INIT(0);
  1678. #endif
  1679. static int journal_init_jbd2_journal_head_cache(void)
  1680. {
  1681. int retval;
  1682. J_ASSERT(jbd2_journal_head_cache == NULL);
  1683. jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
  1684. sizeof(struct journal_head),
  1685. 0, /* offset */
  1686. SLAB_TEMPORARY, /* flags */
  1687. NULL); /* ctor */
  1688. retval = 0;
  1689. if (!jbd2_journal_head_cache) {
  1690. retval = -ENOMEM;
  1691. printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
  1692. }
  1693. return retval;
  1694. }
  1695. static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
  1696. {
  1697. if (jbd2_journal_head_cache) {
  1698. kmem_cache_destroy(jbd2_journal_head_cache);
  1699. jbd2_journal_head_cache = NULL;
  1700. }
  1701. }
  1702. /*
  1703. * journal_head splicing and dicing
  1704. */
  1705. static struct journal_head *journal_alloc_journal_head(void)
  1706. {
  1707. struct journal_head *ret;
  1708. static unsigned long last_warning;
  1709. #ifdef CONFIG_JBD2_DEBUG
  1710. atomic_inc(&nr_journal_heads);
  1711. #endif
  1712. ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
  1713. if (!ret) {
  1714. jbd_debug(1, "out of memory for journal_head\n");
  1715. if (time_after(jiffies, last_warning + 5*HZ)) {
  1716. printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
  1717. __func__);
  1718. last_warning = jiffies;
  1719. }
  1720. while (!ret) {
  1721. yield();
  1722. ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
  1723. }
  1724. }
  1725. return ret;
  1726. }
  1727. static void journal_free_journal_head(struct journal_head *jh)
  1728. {
  1729. #ifdef CONFIG_JBD2_DEBUG
  1730. atomic_dec(&nr_journal_heads);
  1731. memset(jh, JBD2_POISON_FREE, sizeof(*jh));
  1732. #endif
  1733. kmem_cache_free(jbd2_journal_head_cache, jh);
  1734. }
  1735. /*
  1736. * A journal_head is attached to a buffer_head whenever JBD has an
  1737. * interest in the buffer.
  1738. *
  1739. * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
  1740. * is set. This bit is tested in core kernel code where we need to take
  1741. * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
  1742. * there.
  1743. *
  1744. * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
  1745. *
  1746. * When a buffer has its BH_JBD bit set it is immune from being released by
  1747. * core kernel code, mainly via ->b_count.
  1748. *
  1749. * A journal_head may be detached from its buffer_head when the journal_head's
  1750. * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
  1751. * Various places in JBD call jbd2_journal_remove_journal_head() to indicate that the
  1752. * journal_head can be dropped if needed.
  1753. *
  1754. * Various places in the kernel want to attach a journal_head to a buffer_head
  1755. * _before_ attaching the journal_head to a transaction. To protect the
  1756. * journal_head in this situation, jbd2_journal_add_journal_head elevates the
  1757. * journal_head's b_jcount refcount by one. The caller must call
  1758. * jbd2_journal_put_journal_head() to undo this.
  1759. *
  1760. * So the typical usage would be:
  1761. *
  1762. * (Attach a journal_head if needed. Increments b_jcount)
  1763. * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
  1764. * ...
  1765. * jh->b_transaction = xxx;
  1766. * jbd2_journal_put_journal_head(jh);
  1767. *
  1768. * Now, the journal_head's b_jcount is zero, but it is safe from being released
  1769. * because it has a non-zero b_transaction.
  1770. */
  1771. /*
  1772. * Give a buffer_head a journal_head.
  1773. *
  1774. * Doesn't need the journal lock.
  1775. * May sleep.
  1776. */
  1777. struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
  1778. {
  1779. struct journal_head *jh;
  1780. struct journal_head *new_jh = NULL;
  1781. repeat:
  1782. if (!buffer_jbd(bh)) {
  1783. new_jh = journal_alloc_journal_head();
  1784. memset(new_jh, 0, sizeof(*new_jh));
  1785. }
  1786. jbd_lock_bh_journal_head(bh);
  1787. if (buffer_jbd(bh)) {
  1788. jh = bh2jh(bh);
  1789. } else {
  1790. J_ASSERT_BH(bh,
  1791. (atomic_read(&bh->b_count) > 0) ||
  1792. (bh->b_page && bh->b_page->mapping));
  1793. if (!new_jh) {
  1794. jbd_unlock_bh_journal_head(bh);
  1795. goto repeat;
  1796. }
  1797. jh = new_jh;
  1798. new_jh = NULL; /* We consumed it */
  1799. set_buffer_jbd(bh);
  1800. bh->b_private = jh;
  1801. jh->b_bh = bh;
  1802. get_bh(bh);
  1803. BUFFER_TRACE(bh, "added journal_head");
  1804. }
  1805. jh->b_jcount++;
  1806. jbd_unlock_bh_journal_head(bh);
  1807. if (new_jh)
  1808. journal_free_journal_head(new_jh);
  1809. return bh->b_private;
  1810. }
  1811. /*
  1812. * Grab a ref against this buffer_head's journal_head. If it ended up not
  1813. * having a journal_head, return NULL
  1814. */
  1815. struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
  1816. {
  1817. struct journal_head *jh = NULL;
  1818. jbd_lock_bh_journal_head(bh);
  1819. if (buffer_jbd(bh)) {
  1820. jh = bh2jh(bh);
  1821. jh->b_jcount++;
  1822. }
  1823. jbd_unlock_bh_journal_head(bh);
  1824. return jh;
  1825. }
  1826. static void __journal_remove_journal_head(struct buffer_head *bh)
  1827. {
  1828. struct journal_head *jh = bh2jh(bh);
  1829. J_ASSERT_JH(jh, jh->b_jcount >= 0);
  1830. get_bh(bh);
  1831. if (jh->b_jcount == 0) {
  1832. if (jh->b_transaction == NULL &&
  1833. jh->b_next_transaction == NULL &&
  1834. jh->b_cp_transaction == NULL) {
  1835. J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
  1836. J_ASSERT_BH(bh, buffer_jbd(bh));
  1837. J_ASSERT_BH(bh, jh2bh(jh) == bh);
  1838. BUFFER_TRACE(bh, "remove journal_head");
  1839. if (jh->b_frozen_data) {
  1840. printk(KERN_WARNING "%s: freeing "
  1841. "b_frozen_data\n",
  1842. __func__);
  1843. jbd2_free(jh->b_frozen_data, bh->b_size);
  1844. }
  1845. if (jh->b_committed_data) {
  1846. printk(KERN_WARNING "%s: freeing "
  1847. "b_committed_data\n",
  1848. __func__);
  1849. jbd2_free(jh->b_committed_data, bh->b_size);
  1850. }
  1851. bh->b_private = NULL;
  1852. jh->b_bh = NULL; /* debug, really */
  1853. clear_buffer_jbd(bh);
  1854. __brelse(bh);
  1855. journal_free_journal_head(jh);
  1856. } else {
  1857. BUFFER_TRACE(bh, "journal_head was locked");
  1858. }
  1859. }
  1860. }
  1861. /*
  1862. * jbd2_journal_remove_journal_head(): if the buffer isn't attached to a transaction
  1863. * and has a zero b_jcount then remove and release its journal_head. If we did
  1864. * see that the buffer is not used by any transaction we also "logically"
  1865. * decrement ->b_count.
  1866. *
  1867. * We in fact take an additional increment on ->b_count as a convenience,
  1868. * because the caller usually wants to do additional things with the bh
  1869. * after calling here.
  1870. * The caller of jbd2_journal_remove_journal_head() *must* run __brelse(bh) at some
  1871. * time. Once the caller has run __brelse(), the buffer is eligible for
  1872. * reaping by try_to_free_buffers().
  1873. */
  1874. void jbd2_journal_remove_journal_head(struct buffer_head *bh)
  1875. {
  1876. jbd_lock_bh_journal_head(bh);
  1877. __journal_remove_journal_head(bh);
  1878. jbd_unlock_bh_journal_head(bh);
  1879. }
  1880. /*
  1881. * Drop a reference on the passed journal_head. If it fell to zero then try to
  1882. * release the journal_head from the buffer_head.
  1883. */
  1884. void jbd2_journal_put_journal_head(struct journal_head *jh)
  1885. {
  1886. struct buffer_head *bh = jh2bh(jh);
  1887. jbd_lock_bh_journal_head(bh);
  1888. J_ASSERT_JH(jh, jh->b_jcount > 0);
  1889. --jh->b_jcount;
  1890. if (!jh->b_jcount && !jh->b_transaction) {
  1891. __journal_remove_journal_head(bh);
  1892. __brelse(bh);
  1893. }
  1894. jbd_unlock_bh_journal_head(bh);
  1895. }
  1896. /*
  1897. * Initialize jbd inode head
  1898. */
  1899. void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
  1900. {
  1901. jinode->i_transaction = NULL;
  1902. jinode->i_next_transaction = NULL;
  1903. jinode->i_vfs_inode = inode;
  1904. jinode->i_flags = 0;
  1905. INIT_LIST_HEAD(&jinode->i_list);
  1906. }
  1907. /*
  1908. * Function to be called before we start removing inode from memory (i.e.,
  1909. * clear_inode() is a fine place to be called from). It removes inode from
  1910. * transaction's lists.
  1911. */
  1912. void jbd2_journal_release_jbd_inode(journal_t *journal,
  1913. struct jbd2_inode *jinode)
  1914. {
  1915. int writeout = 0;
  1916. if (!journal)
  1917. return;
  1918. restart:
  1919. spin_lock(&journal->j_list_lock);
  1920. /* Is commit writing out inode - we have to wait */
  1921. if (jinode->i_flags & JI_COMMIT_RUNNING) {
  1922. wait_queue_head_t *wq;
  1923. DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
  1924. wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
  1925. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1926. spin_unlock(&journal->j_list_lock);
  1927. schedule();
  1928. finish_wait(wq, &wait.wait);
  1929. goto restart;
  1930. }
  1931. /* Do we need to wait for data writeback? */
  1932. if (journal->j_committing_transaction == jinode->i_transaction)
  1933. writeout = 1;
  1934. if (jinode->i_transaction) {
  1935. list_del(&jinode->i_list);
  1936. jinode->i_transaction = NULL;
  1937. }
  1938. spin_unlock(&journal->j_list_lock);
  1939. }
  1940. /*
  1941. * debugfs tunables
  1942. */
  1943. #ifdef CONFIG_JBD2_DEBUG
  1944. u8 jbd2_journal_enable_debug __read_mostly;
  1945. EXPORT_SYMBOL(jbd2_journal_enable_debug);
  1946. #define JBD2_DEBUG_NAME "jbd2-debug"
  1947. static struct dentry *jbd2_debugfs_dir;
  1948. static struct dentry *jbd2_debug;
  1949. static void __init jbd2_create_debugfs_entry(void)
  1950. {
  1951. jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
  1952. if (jbd2_debugfs_dir)
  1953. jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
  1954. S_IRUGO | S_IWUSR,
  1955. jbd2_debugfs_dir,
  1956. &jbd2_journal_enable_debug);
  1957. }
  1958. static void __exit jbd2_remove_debugfs_entry(void)
  1959. {
  1960. debugfs_remove(jbd2_debug);
  1961. debugfs_remove(jbd2_debugfs_dir);
  1962. }
  1963. #else
  1964. static void __init jbd2_create_debugfs_entry(void)
  1965. {
  1966. }
  1967. static void __exit jbd2_remove_debugfs_entry(void)
  1968. {
  1969. }
  1970. #endif
  1971. #ifdef CONFIG_PROC_FS
  1972. #define JBD2_STATS_PROC_NAME "fs/jbd2"
  1973. static void __init jbd2_create_jbd_stats_proc_entry(void)
  1974. {
  1975. proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
  1976. }
  1977. static void __exit jbd2_remove_jbd_stats_proc_entry(void)
  1978. {
  1979. if (proc_jbd2_stats)
  1980. remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
  1981. }
  1982. #else
  1983. #define jbd2_create_jbd_stats_proc_entry() do {} while (0)
  1984. #define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
  1985. #endif
  1986. struct kmem_cache *jbd2_handle_cache;
  1987. static int __init journal_init_handle_cache(void)
  1988. {
  1989. jbd2_handle_cache = kmem_cache_create("jbd2_journal_handle",
  1990. sizeof(handle_t),
  1991. 0, /* offset */
  1992. SLAB_TEMPORARY, /* flags */
  1993. NULL); /* ctor */
  1994. if (jbd2_handle_cache == NULL) {
  1995. printk(KERN_EMERG "JBD: failed to create handle cache\n");
  1996. return -ENOMEM;
  1997. }
  1998. return 0;
  1999. }
  2000. static void jbd2_journal_destroy_handle_cache(void)
  2001. {
  2002. if (jbd2_handle_cache)
  2003. kmem_cache_destroy(jbd2_handle_cache);
  2004. }
  2005. /*
  2006. * Module startup and shutdown
  2007. */
  2008. static int __init journal_init_caches(void)
  2009. {
  2010. int ret;
  2011. ret = jbd2_journal_init_revoke_caches();
  2012. if (ret == 0)
  2013. ret = journal_init_jbd2_journal_head_cache();
  2014. if (ret == 0)
  2015. ret = journal_init_handle_cache();
  2016. return ret;
  2017. }
  2018. static void jbd2_journal_destroy_caches(void)
  2019. {
  2020. jbd2_journal_destroy_revoke_caches();
  2021. jbd2_journal_destroy_jbd2_journal_head_cache();
  2022. jbd2_journal_destroy_handle_cache();
  2023. jbd2_journal_destroy_slabs();
  2024. }
  2025. static int __init journal_init(void)
  2026. {
  2027. int ret;
  2028. BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
  2029. ret = journal_init_caches();
  2030. if (ret == 0) {
  2031. jbd2_create_debugfs_entry();
  2032. jbd2_create_jbd_stats_proc_entry();
  2033. } else {
  2034. jbd2_journal_destroy_caches();
  2035. }
  2036. return ret;
  2037. }
  2038. static void __exit journal_exit(void)
  2039. {
  2040. #ifdef CONFIG_JBD2_DEBUG
  2041. int n = atomic_read(&nr_journal_heads);
  2042. if (n)
  2043. printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
  2044. #endif
  2045. jbd2_remove_debugfs_entry();
  2046. jbd2_remove_jbd_stats_proc_entry();
  2047. jbd2_journal_destroy_caches();
  2048. }
  2049. /*
  2050. * jbd2_dev_to_name is a utility function used by the jbd2 and ext4
  2051. * tracing infrastructure to map a dev_t to a device name.
  2052. *
  2053. * The caller should use rcu_read_lock() in order to make sure the
  2054. * device name stays valid until its done with it. We use
  2055. * rcu_read_lock() as well to make sure we're safe in case the caller
  2056. * gets sloppy, and because rcu_read_lock() is cheap and can be safely
  2057. * nested.
  2058. */
  2059. struct devname_cache {
  2060. struct rcu_head rcu;
  2061. dev_t device;
  2062. char devname[BDEVNAME_SIZE];
  2063. };
  2064. #define CACHE_SIZE_BITS 6
  2065. static struct devname_cache *devcache[1 << CACHE_SIZE_BITS];
  2066. static DEFINE_SPINLOCK(devname_cache_lock);
  2067. static void free_devcache(struct rcu_head *rcu)
  2068. {
  2069. kfree(rcu);
  2070. }
  2071. const char *jbd2_dev_to_name(dev_t device)
  2072. {
  2073. int i = hash_32(device, CACHE_SIZE_BITS);
  2074. char *ret;
  2075. struct block_device *bd;
  2076. static struct devname_cache *new_dev;
  2077. rcu_read_lock();
  2078. if (devcache[i] && devcache[i]->device == device) {
  2079. ret = devcache[i]->devname;
  2080. rcu_read_unlock();
  2081. return ret;
  2082. }
  2083. rcu_read_unlock();
  2084. new_dev = kmalloc(sizeof(struct devname_cache), GFP_KERNEL);
  2085. if (!new_dev)
  2086. return "NODEV-ALLOCFAILURE"; /* Something non-NULL */
  2087. spin_lock(&devname_cache_lock);
  2088. if (devcache[i]) {
  2089. if (devcache[i]->device == device) {
  2090. kfree(new_dev);
  2091. ret = devcache[i]->devname;
  2092. spin_unlock(&devname_cache_lock);
  2093. return ret;
  2094. }
  2095. call_rcu(&devcache[i]->rcu, free_devcache);
  2096. }
  2097. devcache[i] = new_dev;
  2098. devcache[i]->device = device;
  2099. bd = bdget(device);
  2100. if (bd) {
  2101. bdevname(bd, devcache[i]->devname);
  2102. bdput(bd);
  2103. } else
  2104. __bdevname(device, devcache[i]->devname);
  2105. ret = devcache[i]->devname;
  2106. spin_unlock(&devname_cache_lock);
  2107. return ret;
  2108. }
  2109. EXPORT_SYMBOL(jbd2_dev_to_name);
  2110. MODULE_LICENSE("GPL");
  2111. module_init(journal_init);
  2112. module_exit(journal_exit);