journal.c 72 KB

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