journal.c 73 KB

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