journal.c 73 KB

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